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 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)
 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 PURPLE_LABEL_PREFIX   "L"
 

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

type_registerllvm_ensure_registers_loaded (int n_registers, type_register registers[], NumberType number_type)
 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, NumberType number_type)
 Declare a global variable. More...
 
LLVMValue llvm_signed_extend (LLVMValue reg, NumberType new_type, NumberType old_type)
 
LLVMValue llvm_truncate (LLVMValue reg, NumberType new_type, NumberType old_type)
 
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 (type_register print_vr, TokenType type)
 Generate code to print an integer. More...
 
void llvm_print_bool (type_register 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...
 
void llvm_function_preamble (char *symbol_name)
 
void llvm_function_postamble (void)
 
LLVMValue llvm_call_function (LLVMValue virtual_register, char *symbol_name)
 
const char * type_to_llvm_type (TokenType type)
 
void llvm_return (LLVMValue virtual_register, char *symbol_name)
 

Variables

static const char * numberTypeLLVMReprs [] = {"i1", "i8", "i16", "i32", "i64"}
 LLVM-IR representations of data types. More...
 
LLVMStackEntryNodeloadedRegistersHead
 Head node of linked list containing register indices that have loaded values. More...
 
LLVMStackEntryNodefreeVirtualRegistersHead
 Head node of linked list containing register indices that are free to have values stored in them. More...
 

Detailed Description

Function headers for LLVM-IR emission.

Author
Charles Averill
Date
10-Sep-2022

Macro Definition Documentation

◆ LLVMVALUE_CONSTANT

#define LLVMVALUE_CONSTANT (   c)
Value:
{ \
.value_type = LLVMVALUETYPE_CONSTANT, .value.constant = c \
}
@ LLVMVALUETYPE_CONSTANT
Definition: llvm.h:38
struct LLVMValue LLVMValue
Value returned by ast_to_llvm.

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 \
}
@ LLVMVALUETYPE_LABEL
Definition: llvm.h:37

Inline initializes an LLVMValue struct from a label number.

◆ LLVMVALUE_NULL

#define LLVMVALUE_NULL
Value:
{ \
.value_type = LLVMVALUETYPE_NONE, .value = 0 \
}
@ LLVMVALUETYPE_NONE
Definition: llvm.h:35

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

◆ LLVMVALUE_VIRTUAL_REGISTER

#define LLVMVALUE_VIRTUAL_REGISTER (   register_number,
  n_t 
)
Value:
{ \
.value_type = LLVMVALUETYPE_VIRTUAL_REGISTER, .stores_pointer = false, \
.value.virtual_register_index = register_number, .number_type = n_t \
}
@ LLVMVALUETYPE_VIRTUAL_REGISTER
Definition: llvm.h:36

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

◆ LLVMVALUE_VIRTUAL_REGISTER_POINTER

#define LLVMVALUE_VIRTUAL_REGISTER_POINTER (   register_number,
  n_t 
)
Value:
{ \
.value_type = LLVMVALUETYPE_VIRTUAL_REGISTER, .stores_pointer = true, \
.value.virtual_register_index = register_number, .number_type = n_t \
}

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

◆ PURPLE_LABEL_PREFIX

#define PURPLE_LABEL_PREFIX   "L"

Prefix to prepend to LLVM label indices

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  virtual_register,
char *  symbol_name 
)

◆ 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,
NumberType  number_type 
)

Declare a global variable.

Parameters
symbol_nameName of global variable
number_typeType of number of global variable

◆ llvm_ensure_registers_loaded()

type_register * llvm_ensure_registers_loaded ( int  n_registers,
type_register  registers[],
NumberType  number_type 
)

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
Returns
type_register* If the registers were not loaded, this array contains the loaded registers

◆ llvm_function_postamble()

void llvm_function_postamble ( void  )

◆ llvm_function_preamble()

void llvm_function_preamble ( char *  symbol_name)

◆ 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 ( type_register  print_vr)

Generate code to print a boolean value.

Parameters
print_vrRegister holding value to print

◆ llvm_print_int()

void llvm_print_int ( type_register  print_vr,
TokenType  type 
)

Generate code to print an integer.

Parameters
print_vrRegister holding value to print
typeType of int (byte, char, int, long) to print

◆ llvm_return()

void llvm_return ( LLVMValue  virtual_register,
char *  symbol_name 
)

◆ llvm_signed_extend()

LLVMValue llvm_signed_extend ( LLVMValue  reg,
NumberType  new_type,
NumberType  old_type 
)

◆ 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_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_truncate()

LLVMValue llvm_truncate ( LLVMValue  reg,
NumberType  new_type,
NumberType  old_type 
)

◆ type_to_llvm_type()

const char * type_to_llvm_type ( TokenType  type)

Variable Documentation

◆ freeVirtualRegistersHead

LLVMStackEntryNode* freeVirtualRegistersHead
extern

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

◆ loadedRegistersHead

LLVMStackEntryNode* loadedRegistersHead
extern

Head node of linked list containing register indices that have loaded values.

◆ numberTypeLLVMReprs

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

LLVM-IR representations of data types.