Purple  0.1
Standard Language Specification
Classes | Macros | Typedefs | Enumerations | Functions | Variables
llvm.h File Reference

Function headers for LLVM-IR emission. More...

#include "scan.h"
#include "types/number.h"
#include "utils/llvm_stack_entry.h"
Include dependency graph for llvm.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  LLVMValue
 Value returned by ast_to_llvm. More...
 

Macros

#define REFSTRING_BUF_MAXLEN   256
 Size of buffer for generating refstrings. More...
 
#define PRINT_LLVMVALUE(val)
 Debug print method for LLVMValues. More...
 
#define LLVMVALUE_NULL
 A standard "null" LLVMValue struct returned in some scenarios. More...
 
#define LLVMVALUE_CONSTANT(c)
 Generates an LLVMValue struct with a constant value. More...
 
#define LLVMVALUE_VIRTUAL_REGISTER(register_number, n_t)
 Inline-initializes an LLVMValue struct from a virtual register number. More...
 
#define LLVMVALUE_VIRTUAL_REGISTER_POINTER(register_number, n_t, depth)
 Inline-initializes an LLVMValue struct from the number of a virtual register that stores a pointer. More...
 
#define LLVMVALUE_LABEL(label_number)
 Inline initializes an LLVMValue struct from a label number. More...
 
#define LLVMVALUE_REGMARKER(llvmvalue)   (llvmvalue.value_type == LLVMVALUETYPE_CONSTANT ? "" : "%")
 Shorthand for providing a "%" string if needed in generation statements. More...
 
#define LLVMVALUE_SET_JUSTLOADED(llvmvalue, symbol_name)
 
#define PURPLE_LABEL_PREFIX   "L"
 
#define REFSTRING(depth)   refstring(_refstring_buf, depth)
 Wrapper for _refstring - WARNING - only one call to REFSTRING may be made per statement, due to _refstring_buf being used in it. Multiple uses will overwrite all but the last occurrance. More...
 
#define LLVM_REPR_NOTYPE(reg)   llvmvalue_repr_notype(_llvm_name_buf, reg)
 

Typedefs

typedef struct LLVMValue LLVMValue
 Value returned by ast_to_llvm. More...
 

Enumerations

enum  LLVMValueType { LLVMVALUETYPE_NONE , LLVMVALUETYPE_VIRTUAL_REGISTER , LLVMVALUETYPE_LABEL , LLVMVALUETYPE_CONSTANT }
 Types of values possibly returned by ast_to_llvm. More...
 

Functions

LLVMValuellvm_ensure_registers_loaded (int n_registers, LLVMValue registers[], int load_depth)
 Ensure that the values of a set of registers are loaded. More...
 
void llvm_preamble (void)
 Generated program's preamble. More...
 
void llvm_postamble (void)
 Generated program's postamble. More...
 
bool llvm_stack_allocation (LLVMStackEntryNode *stack_entries)
 Allocate space on stack for variables. More...
 
LLVMValue llvm_binary_arithmetic (TokenType operation, LLVMValue left_virtual_register, LLVMValue right_virtual_register)
 Generates LLVM-IR for various binary arithmetic expressions. More...
 
LLVMValue llvm_store_constant (Number value)
 Store a constant number value into a register. More...
 
type_register get_next_local_virtual_register (void)
 Retrieves the next valid virtual register index. More...
 
LLVMValue get_next_label (void)
 Get the next valid label. More...
 
LLVMValue llvm_load_global_variable (char *symbol_name)
 Load a global variable's value into a new virtual register. More...
 
void llvm_store_global_variable (char *symbol_name, LLVMValue rvalue_register)
 Store a value into a global variable. More...
 
void llvm_declare_global_number_variable (char *symbol_name, Number n)
 Declare a global variable. More...
 
LLVMValue llvm_int_resize (LLVMValue reg, NumberType new_tye)
 Generates an extend or truncate statement to change the bit-width of an integer. More...
 
void llvm_declare_assign_global_number_variable (char *symbol_name, Number number)
 Declare a global variable with an assigned number value. More...
 
void llvm_print_int (LLVMValue print_vr)
 Generate code to print an integer. More...
 
void llvm_print_bool (LLVMValue print_vr)
 Generate code to print a boolean value. More...
 
LLVMValue llvm_compare (TokenType comparison_type, LLVMValue left_virtual_register, LLVMValue right_virtual_register)
 Generate code to compare two registers. More...
 
LLVMValue llvm_compare_jump (TokenType comparison_type, LLVMValue left_virtual_register, LLVMValue right_virtual_register, LLVMValue false_label)
 Generate code to compare two registers and conditionally jump based on the result. More...
 
void llvm_label (LLVMValue label)
 Generate label code. More...
 
void llvm_jump (LLVMValue label)
 Generate an unconditional jump statement. More...
 
void llvm_conditional_jump (LLVMValue condition_register, LLVMValue true_label, LLVMValue false_label)
 Generate a conditional jump statement. More...
 
LLVMValuellvm_function_preamble (char *symbol_name)
 Generates the preamble for a function. More...
 
void llvm_function_postamble (void)
 Generates the postamble for a function. More...
 
LLVMValue llvm_call_function (LLVMValue *args, unsigned long long int num_args, char *symbol_name)
 Generate a function call statement. More...
 
const char * type_to_llvm_type (TokenType type)
 Convert a TokenType to the string representation of that type in LLVM. More...
 
void llvm_return (LLVMValue virtual_register, char *symbol_name)
 Generate a return statement. More...
 
char * refstring (char *buf, int pointer_depth)
 Generate a string containing pointer stars. More...
 
char * llvmvalue_repr_notype (char *buf, LLVMValue reg)
 
LLVMValue llvm_get_address (char *symbol_name)
 Generate an addressing statement. More...
 
LLVMValue llvm_dereference (LLVMValue reg)
 Generate a dereference (load) statement. More...
 
void llvm_store_dereference (LLVMValue destination, LLVMValue value)
 Generates code to store a value into a dereferenced value. More...
 
void llvm_store_local (char *symbol_name, LLVMValue val)
 

Variables

static const char * numberTypeLLVMReprs [] = {"i1", "i8", "i16", "i32", "i64"}
 LLVM-IR representations of data types. More...
 
LLVMStackEntryNodefreeVirtualRegistersHead
 Head node of linked list containing register indices that are free to have values stored in them. More...
 
static char _refstring_buf [REFSTRING_BUF_MAXLEN]
 Temporary buffer used to generate pointer star strings for LLVM code. More...
 
static char _llvm_name_buf [MAX_IDENTIFIER_LENGTH+3]
 
static const char * valueTypeStrings [] = {"None", "Virtual Register", "Label", "Constant"}
 String representations of LLVMValueTypes for debugging. More...
 

Detailed Description

Function headers for LLVM-IR emission.

Author
Charles Averill
Date
10-Sep-2022

Macro Definition Documentation

◆ LLVM_REPR_NOTYPE

#define LLVM_REPR_NOTYPE (   reg)    llvmvalue_repr_notype(_llvm_name_buf, reg)

◆ LLVMVALUE_CONSTANT

#define LLVMVALUE_CONSTANT (   c)
Value:
{ \
.value_type = LLVMVALUETYPE_CONSTANT, .value.constant = c, \
.num_info = (Number){.number_type = max_numbertype_for_val(c), .pointer_depth = 0}, \
.just_loaded = 0, .has_name = false \
}
@ LLVMVALUETYPE_CONSTANT
Definition: llvm.h:45
struct LLVMValue LLVMValue
Value returned by ast_to_llvm.
struct Number Number
Container for various kinds of number data.
NumberType max_numbertype_for_val(long long int value)
Finds the maximum NumberType possible for a given value.
Definition: number.c:66

Generates an LLVMValue struct with a constant value.

◆ LLVMVALUE_LABEL

#define LLVMVALUE_LABEL (   label_number)
Value:
{ \
.value_type = LLVMVALUETYPE_LABEL, .value.label_index = label_number, .just_loaded = 0, \
.has_name = false \
}
@ LLVMVALUETYPE_LABEL
Definition: llvm.h:44

Inline initializes an LLVMValue struct from a label number.

◆ LLVMVALUE_NULL

#define LLVMVALUE_NULL
Value:
{ \
.value_type = LLVMVALUETYPE_NONE, .value.constant = 0, \
.num_info = (Number){.number_type = -1, .pointer_depth = 0}, .just_loaded = 0, \
.has_name = false \
}
@ LLVMVALUETYPE_NONE
Definition: llvm.h:42

A standard "null" LLVMValue struct returned in some scenarios.

◆ LLVMVALUE_REGMARKER

#define LLVMVALUE_REGMARKER (   llvmvalue)    (llvmvalue.value_type == LLVMVALUETYPE_CONSTANT ? "" : "%")

Shorthand for providing a "%" string if needed in generation statements.

◆ LLVMVALUE_SET_JUSTLOADED

#define LLVMVALUE_SET_JUSTLOADED (   llvmvalue,
  symbol_name 
)
Value:
memset(llvmvalue.just_loaded, 0, MAX_IDENTIFIER_LENGTH); \
strcpy(out.just_loaded, symbol_name);
#define MAX_IDENTIFIER_LENGTH
Definition: identifier.h:12

◆ LLVMVALUE_VIRTUAL_REGISTER

#define LLVMVALUE_VIRTUAL_REGISTER (   register_number,
  n_t 
)
Value:
{ \
.value.virtual_register_index = register_number, .just_loaded = 0, .has_name = false, \
.num_info = (Number) \
{ \
.number_type = n_t, .pointer_depth = 0 \
} \
}
@ LLVMVALUETYPE_VIRTUAL_REGISTER
Definition: llvm.h:43

Inline-initializes an LLVMValue struct from a virtual register number.

◆ LLVMVALUE_VIRTUAL_REGISTER_POINTER

#define LLVMVALUE_VIRTUAL_REGISTER_POINTER (   register_number,
  n_t,
  depth 
)
Value:
{ \
.value.virtual_register_index = register_number, .just_loaded = 0, .has_name = false, \
.num_info = (Number){.number_type = n_t, .pointer_depth = depth}, \
}

Inline-initializes an LLVMValue struct from the number of a virtual register that stores a pointer.

◆ PRINT_LLVMVALUE

#define PRINT_LLVMVALUE (   val)
Value:
printf("LLVMValue Information\n"); \
printf("---------------------\n"); \
printf("Value Type: %s\n", valueTypeStrings[val.value_type]); \
printf("Number Type: %s\n", numberTypeLLVMReprs[val.num_info.number_type]); \
printf("Pointer Depth: %d\n", val.num_info.pointer_depth); \
if (val.has_name) { \
printf("Contents: %s\n", val.value.name); \
} else \
printf("Contents: %lld\n", val.value.constant);
static const char * valueTypeStrings[]
String representations of LLVMValueTypes for debugging.
Definition: llvm.h:51
static const char * numberTypeLLVMReprs[]
LLVM-IR representations of data types.
Definition: llvm.h:18

Debug print method for LLVMValues.

◆ PURPLE_LABEL_PREFIX

#define PURPLE_LABEL_PREFIX   "L"

Prefix to prepend to LLVM label indices

◆ REFSTRING

#define REFSTRING (   depth)    refstring(_refstring_buf, depth)

Wrapper for _refstring - WARNING - only one call to REFSTRING may be made per statement, due to _refstring_buf being used in it. Multiple uses will overwrite all but the last occurrance.

◆ REFSTRING_BUF_MAXLEN

#define REFSTRING_BUF_MAXLEN   256

Size of buffer for generating refstrings.

Typedef Documentation

◆ LLVMValue

typedef struct LLVMValue LLVMValue

Value returned by ast_to_llvm.

Enumeration Type Documentation

◆ LLVMValueType

Types of values possibly returned by ast_to_llvm.

Enumerator
LLVMVALUETYPE_NONE 
LLVMVALUETYPE_VIRTUAL_REGISTER 
LLVMVALUETYPE_LABEL 
LLVMVALUETYPE_CONSTANT 

Function Documentation

◆ get_next_label()

LLVMValue get_next_label ( void  )

Get the next valid label.

Returns
LLVMValue Next valid label

◆ get_next_local_virtual_register()

type_register get_next_local_virtual_register ( void  )

Retrieves the next valid virtual register index.

Returns
type_register Index of next unused virtual register

◆ llvm_binary_arithmetic()

LLVMValue llvm_binary_arithmetic ( TokenType  operation,
LLVMValue  left_virtual_register,
LLVMValue  right_virtual_register 
)

Generates LLVM-IR for various binary arithmetic expressions.

Parameters
operationOperation to perform
left_virtual_registerOperand left of operation
right_virtual_registerOperand right of operation
Returns
int Number of virtual register in which the result is stored

◆ llvm_call_function()

LLVMValue llvm_call_function ( LLVMValue args,
unsigned long long int  num_args,
char *  symbol_name 
)

Generate a function call statement.

Parameters
argsCurrently unused function parameter
num_argsNumber of args passed
symbol_nameName of function to call
Returns
LLVMValue Output of function, or LLVMVALUE_NULL if it is a void function

◆ llvm_compare()

LLVMValue llvm_compare ( TokenType  comparison_type,
LLVMValue  left_virtual_register,
LLVMValue  right_virtual_register 
)

Generate code to compare two registers.

Parameters
comparison_typeType of comparison to make
left_virtual_registerLLVMValue storing left value register index
right_virtual_registerLLVMValue storing right value register index
Returns
LLVMValue Register index of comparison value

◆ llvm_compare_jump()

LLVMValue llvm_compare_jump ( TokenType  comparison_type,
LLVMValue  left_virtual_register,
LLVMValue  right_virtual_register,
LLVMValue  false_label 
)

Generate code to compare two registers and conditionally jump based on the result.

Parameters
comparison_typeType of comparison to make
left_virtual_registerLLVMValue storing left value register index
right_virtual_registerLLVMValue storing right value register index
false_labelLLVMValue storing label data for the branch in which the condition is false
Returns
LLVMValue Register index of comparison value

◆ llvm_conditional_jump()

void llvm_conditional_jump ( LLVMValue  condition_register,
LLVMValue  true_label,
LLVMValue  false_label 
)

Generate a conditional jump statement.

Parameters
condition_registerLLVMValue holding information about the register from the prior condition
true_labelLabel to jump to if condition is true
false_labelLabel to jump to if condition is false

◆ llvm_declare_assign_global_number_variable()

void llvm_declare_assign_global_number_variable ( char *  symbol_name,
Number  number 
)

Declare a global variable with an assigned number value.

Parameters
symbol_nameName of global variable
numberDefault value of global variable

◆ llvm_declare_global_number_variable()

void llvm_declare_global_number_variable ( char *  symbol_name,
Number  n 
)

Declare a global variable.

Parameters
symbol_nameName of global variable
nNumber information of global variable

◆ llvm_dereference()

LLVMValue llvm_dereference ( LLVMValue  reg)

Generate a dereference (load) statement.

Parameters
regRegister to dereference
Returns
LLVMValue LLVMValue containing loaded value

◆ llvm_ensure_registers_loaded()

LLVMValue * llvm_ensure_registers_loaded ( int  n_registers,
LLVMValue  registers[],
int  load_depth 
)

Ensure that the values of a set of registers are loaded.

Parameters
n_registersNumber of registers to ensure
registersArray of register indices to ensure
number_typeNumberType of registers to ensure
load_depthPointer depth to be considered "loaded"
Returns
LLVMValue* If the registers were not loaded, this array contains the loaded registers

◆ llvm_function_postamble()

void llvm_function_postamble ( void  )

Generates the postamble for a function.

◆ llvm_function_preamble()

LLVMValue * llvm_function_preamble ( char *  symbol_name)

Generates the preamble for a function.

Parameters
symbol_nameName of function to generate for
Returns
LLVMValue* List of LLVMValues corresponding to the latest_llvmvalues for each function input

◆ llvm_get_address()

LLVMValue llvm_get_address ( char *  symbol_name)

Generate an addressing statement.

Parameters
symbol_nameSymbol to take the address of
Returns
LLVMValue LLVMValue containing address of symbol

◆ llvm_int_resize()

LLVMValue llvm_int_resize ( LLVMValue  reg,
NumberType  new_type 
)

Generates an extend or truncate statement to change the bit-width of an integer.

Parameters
regRegister whose contents are to be resized
new_typeNew NumberType to resize to
Returns
LLVMValue Resized LLVMValue

◆ llvm_jump()

void llvm_jump ( LLVMValue  label)

Generate an unconditional jump statement.

Parameters
labelLabel to jump to

◆ llvm_label()

void llvm_label ( LLVMValue  label)

Generate label code.

Parameters
labelLLVMValue containing label information

◆ llvm_load_global_variable()

LLVMValue llvm_load_global_variable ( char *  symbol_name)

Load a global variable's value into a new virtual register.

Parameters
symbol_nameIdentifier name of variable to load
Returns
LLVMValue Register number variable value is held in

◆ llvm_postamble()

void llvm_postamble ( void  )

Generated program's postamble.

◆ llvm_preamble()

void llvm_preamble ( void  )

Generated program's preamble.

◆ llvm_print_bool()

void llvm_print_bool ( LLVMValue  print_vr)

Generate code to print a boolean value.

Parameters
print_vrRegister holding value to print

◆ llvm_print_int()

void llvm_print_int ( LLVMValue  print_vr)

Generate code to print an integer.

Parameters
print_vrRegister holding value to print

◆ llvm_return()

void llvm_return ( LLVMValue  value,
char *  symbol_name 
)

Generate a return statement.

Parameters
valueValue to return
symbol_nameName of function to return from

◆ llvm_stack_allocation()

bool llvm_stack_allocation ( LLVMStackEntryNode stack_entries)

Allocate space on stack for variables.

Parameters
stack_entriesLLVMStackEntryNode pointers holding stack allocation information
Returns
bool True if stack_entries may be freed

◆ llvm_store_constant()

LLVMValue llvm_store_constant ( Number  value)

Store a constant number value into a register.

Parameters
valueNumber struct containing information about the constant
Returns
int Register number value is held in

◆ llvm_store_dereference()

void llvm_store_dereference ( LLVMValue  destination,
LLVMValue  value 
)

Generates code to store a value into a dereferenced value.

Parameters
destinationDestination register or variable to store into
valueValue to store

◆ llvm_store_global_variable()

void llvm_store_global_variable ( char *  symbol_name,
LLVMValue  rvalue_register 
)

Store a value into a global variable.

Parameters
symbol_nameIdentifier name of variable to store new value to
rvalue_registerRegister number of statement's RValue to store

◆ llvm_store_local()

void llvm_store_local ( char *  symbol_name,
LLVMValue  val 
)

◆ llvmvalue_repr_notype()

char * llvmvalue_repr_notype ( char *  buf,
LLVMValue  reg 
)

◆ refstring()

char * refstring ( char *  buf,
int  pointer_depth 
)

Generate a string containing pointer stars.

Parameters
bufBuffer to fill with pointer stars
pointer_depthNumber of pointer stars to fill
Returns
char* Resulting string (for inline use)

◆ type_to_llvm_type()

const char * type_to_llvm_type ( TokenType  type)

Convert a TokenType to the string representation of that type in LLVM.

Parameters
typeTokenType to convert
Returns
const char* Matching LLVM type string

Variable Documentation

◆ _llvm_name_buf

char _llvm_name_buf[MAX_IDENTIFIER_LENGTH+3]
static

◆ _refstring_buf

char _refstring_buf[REFSTRING_BUF_MAXLEN]
static

Temporary buffer used to generate pointer star strings for LLVM code.

◆ freeVirtualRegistersHead

LLVMStackEntryNode* freeVirtualRegistersHead
extern

Head node of linked list containing register indices that are free to have values stored in them.

◆ numberTypeLLVMReprs

const char* numberTypeLLVMReprs[] = {"i1", "i8", "i16", "i32", "i64"}
static

LLVM-IR representations of data types.

◆ valueTypeStrings

const char* valueTypeStrings[] = {"None", "Virtual Register", "Label", "Constant"}
static

String representations of LLVMValueTypes for debugging.