Purple  0.1
Standard Language Specification
llvm.h
Go to the documentation of this file.
1
8#ifndef LLVM_H
9#define LLVM_H
10
11#include "scan.h"
12#include "types/number.h"
14
18static const char* numberTypeLLVMReprs[] = {"i1", "i8", "i16", "i32", "i64"};
19
24
28#define REFSTRING_BUF_MAXLEN 256
29
34
36
40typedef enum
41{
47
51static const char* valueTypeStrings[] = {"None", "Virtual Register", "Label", "Constant"};
52
56typedef struct LLVMValue {
66 union {
72 long long int constant;
77
81#define PRINT_LLVMVALUE(val) \
82 printf("LLVMValue Information\n"); \
83 printf("---------------------\n"); \
84 printf("Value Type: %s\n", valueTypeStrings[val.value_type]); \
85 printf("Number Type: %s\n", numberTypeLLVMReprs[val.num_info.number_type]); \
86 printf("Pointer Depth: %d\n", val.num_info.pointer_depth); \
87 if (val.has_name) { \
88 printf("Contents: %s\n", val.value.name); \
89 } else \
90 printf("Contents: %lld\n", val.value.constant);
91
95#define LLVMVALUE_NULL \
96 (LLVMValue) \
97 { \
98 .value_type = LLVMVALUETYPE_NONE, .value.constant = 0, \
99 .num_info = (Number){.number_type = -1, .pointer_depth = 0}, .just_loaded = 0, \
100 .has_name = false \
101 }
102
106#define LLVMVALUE_CONSTANT(c) \
107 (LLVMValue) \
108 { \
109 .value_type = LLVMVALUETYPE_CONSTANT, .value.constant = c, \
110 .num_info = (Number){.number_type = max_numbertype_for_val(c), .pointer_depth = 0}, \
111 .just_loaded = 0, .has_name = false \
112 }
113
117#define LLVMVALUE_VIRTUAL_REGISTER(register_number, n_t) \
118 (LLVMValue) \
119 { \
120 .value_type = LLVMVALUETYPE_VIRTUAL_REGISTER, \
121 .value.virtual_register_index = register_number, .just_loaded = 0, .has_name = false, \
122 .num_info = (Number) \
123 { \
124 .number_type = n_t, .pointer_depth = 0 \
125 } \
126 }
127
131#define LLVMVALUE_VIRTUAL_REGISTER_POINTER(register_number, n_t, depth) \
132 (LLVMValue) \
133 { \
134 .value_type = LLVMVALUETYPE_VIRTUAL_REGISTER, \
135 .value.virtual_register_index = register_number, .just_loaded = 0, .has_name = false, \
136 .num_info = (Number){.number_type = n_t, .pointer_depth = depth}, \
137 }
138
142#define LLVMVALUE_LABEL(label_number) \
143 (LLVMValue) \
144 { \
145 .value_type = LLVMVALUETYPE_LABEL, .value.label_index = label_number, .just_loaded = 0, \
146 .has_name = false \
147 }
148
152#define LLVMVALUE_REGMARKER(llvmvalue) (llvmvalue.value_type == LLVMVALUETYPE_CONSTANT ? "" : "%")
153
154#define LLVMVALUE_SET_JUSTLOADED(llvmvalue, symbol_name) \
155 memset(llvmvalue.just_loaded, 0, MAX_IDENTIFIER_LENGTH); \
156 strcpy(out.just_loaded, symbol_name);
157
159#define PURPLE_LABEL_PREFIX "L"
160
161LLVMValue* llvm_ensure_registers_loaded(int n_registers, LLVMValue registers[], int load_depth);
162
163void llvm_preamble(void);
164void llvm_postamble(void);
165
166bool llvm_stack_allocation(LLVMStackEntryNode* stack_entries);
167
168LLVMValue llvm_binary_arithmetic(TokenType operation, LLVMValue left_virtual_register,
169 LLVMValue right_virtual_register);
173
174LLVMValue llvm_load_global_variable(char* symbol_name);
175void llvm_store_global_variable(char* symbol_name, LLVMValue rvalue_register);
176void llvm_declare_global_number_variable(char* symbol_name, Number n);
178void llvm_declare_assign_global_number_variable(char* symbol_name, Number number);
179void llvm_print_int(LLVMValue print_vr);
180void llvm_print_bool(LLVMValue print_vr);
181LLVMValue llvm_compare(TokenType comparison_type, LLVMValue left_virtual_register,
182 LLVMValue right_virtual_register);
183LLVMValue llvm_compare_jump(TokenType comparison_type, LLVMValue left_virtual_register,
184 LLVMValue right_virtual_register, LLVMValue false_label);
185void llvm_label(LLVMValue label);
186void llvm_jump(LLVMValue label);
187void llvm_conditional_jump(LLVMValue condition_register, LLVMValue true_label,
188 LLVMValue false_label);
189LLVMValue* llvm_function_preamble(char* symbol_name);
190void llvm_function_postamble(void);
191LLVMValue llvm_call_function(LLVMValue* args, unsigned long long int num_args, char* symbol_name);
192const char* type_to_llvm_type(TokenType type);
193void llvm_return(LLVMValue virtual_register, char* symbol_name);
194char* refstring(char* buf, int pointer_depth);
195char* llvmvalue_repr_notype(char* buf, LLVMValue reg);
196LLVMValue llvm_get_address(char* symbol_name);
198void llvm_store_dereference(LLVMValue destination, LLVMValue value);
199void llvm_store_local(char* symbol_name, LLVMValue val);
200
205#define REFSTRING(depth) refstring(_refstring_buf, depth)
206#define LLVM_REPR_NOTYPE(reg) llvmvalue_repr_notype(_llvm_name_buf, reg)
207
208#endif /* LLVM_H */
#define MAX_IDENTIFIER_LENGTH
Definition: identifier.h:12
LLVMValue llvm_load_global_variable(char *symbol_name)
Load a global variable's value into a new virtual register.
Definition: llvm.c:448
static const char * valueTypeStrings[]
String representations of LLVMValueTypes for debugging.
Definition: llvm.h:51
LLVMStackEntryNode * freeVirtualRegistersHead
Head node of linked list containing register indices that are free to have values stored in them.
Definition: translate.c:12
void llvm_postamble(void)
Generated program's postamble.
Definition: llvm.c:169
LLVMValue llvm_compare(TokenType comparison_type, LLVMValue left_virtual_register, LLVMValue right_virtual_register)
Generate code to compare two registers.
Definition: llvm.c:761
static char _refstring_buf[REFSTRING_BUF_MAXLEN]
Temporary buffer used to generate pointer star strings for LLVM code.
Definition: llvm.h:33
void llvm_declare_assign_global_number_variable(char *symbol_name, Number number)
Declare a global variable with an assigned number value.
Definition: llvm.c:588
LLVMValue llvm_store_constant(Number value)
Store a constant number value into a register.
Definition: llvm.c:420
void llvm_label(LLVMValue label)
Generate label code.
Definition: llvm.c:912
void llvm_conditional_jump(LLVMValue condition_register, LLVMValue true_label, LLVMValue false_label)
Generate a conditional jump statement.
Definition: llvm.c:949
void llvm_jump(LLVMValue label)
Generate an unconditional jump statement.
Definition: llvm.c:929
LLVMValue * llvm_ensure_registers_loaded(int n_registers, LLVMValue registers[], int load_depth)
Ensure that the values of a set of registers are loaded.
Definition: llvm.c:68
void llvm_function_postamble(void)
Generates the postamble for a function.
Definition: llvm.c:1054
LLVMValue * llvm_function_preamble(char *symbol_name)
Generates the preamble for a function.
Definition: llvm.c:967
void llvm_store_global_variable(char *symbol_name, LLVMValue rvalue_register)
Store a value into a global variable.
Definition: llvm.c:481
char * llvmvalue_repr_notype(char *buf, LLVMValue reg)
Definition: llvm.c:1243
bool llvm_stack_allocation(LLVMStackEntryNode *stack_entries)
Allocate space on stack for variables.
Definition: llvm.c:204
void llvm_preamble(void)
Generated program's preamble.
Definition: llvm.c:135
void llvm_print_bool(LLVMValue print_vr)
Generate code to print a boolean value.
Definition: llvm.c:637
static char _llvm_name_buf[MAX_IDENTIFIER_LENGTH+3]
Definition: llvm.h:35
LLVMValueType
Types of values possibly returned by ast_to_llvm.
Definition: llvm.h:41
@ LLVMVALUETYPE_VIRTUAL_REGISTER
Definition: llvm.h:43
@ LLVMVALUETYPE_NONE
Definition: llvm.h:42
@ LLVMVALUETYPE_LABEL
Definition: llvm.h:44
@ LLVMVALUETYPE_CONSTANT
Definition: llvm.h:45
void llvm_print_int(LLVMValue print_vr)
Generate code to print an integer.
Definition: llvm.c:599
void llvm_store_dereference(LLVMValue destination, LLVMValue value)
Generates code to store a value into a dereferenced value.
Definition: llvm.c:1333
LLVMValue llvm_binary_arithmetic(TokenType operation, LLVMValue left_virtual_register, LLVMValue right_virtual_register)
Generates LLVM-IR for various binary arithmetic expressions.
Definition: llvm.c:315
struct LLVMValue LLVMValue
Value returned by ast_to_llvm.
LLVMValue llvm_dereference(LLVMValue reg)
Generate a dereference (load) statement.
Definition: llvm.c:1311
#define REFSTRING_BUF_MAXLEN
Size of buffer for generating refstrings.
Definition: llvm.h:28
static const char * numberTypeLLVMReprs[]
LLVM-IR representations of data types.
Definition: llvm.h:18
LLVMValue llvm_get_address(char *symbol_name)
Generate an addressing statement.
Definition: llvm.c:1270
type_register get_next_local_virtual_register(void)
Retrieves the next valid virtual register index.
Definition: llvm.c:437
LLVMValue llvm_call_function(LLVMValue *args, unsigned long long int num_args, char *symbol_name)
Generate a function call statement.
Definition: llvm.c:1083
LLVMValue llvm_int_resize(LLVMValue reg, NumberType new_tye)
Generates an extend or truncate statement to change the bit-width of an integer.
Definition: llvm.c:534
void llvm_return(LLVMValue virtual_register, char *symbol_name)
Generate a return statement.
Definition: llvm.c:1181
char * refstring(char *buf, int pointer_depth)
Generate a string containing pointer stars.
Definition: llvm.c:1228
void llvm_store_local(char *symbol_name, LLVMValue val)
Definition: llvm.c:1373
void llvm_declare_global_number_variable(char *symbol_name, Number n)
Declare a global variable.
Definition: llvm.c:571
const char * type_to_llvm_type(TokenType type)
Convert a TokenType to the string representation of that type in LLVM.
Definition: llvm.c:1066
LLVMValue get_next_label(void)
Get the next valid label.
Definition: llvm.c:902
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.
Definition: llvm.c:877
#define type_register
A too-big data type for register indices in case of stress testing.
Definition: llvm_stack_entry.h:16
#define type_label
A too-big data type for label indices in case of stress testing.
Definition: llvm_stack_entry.h:21
Definitions and function headers for the internal "Number" type.
NumberType
Types of numbers supported by Purple.
Definition: number.h:17
Lexical Scanner function headers.
TokenType
Types of scannable tokens.
Definition: scan.h:20
Node of a linked list containing information about required stack allocation for a piece of data....
Definition: llvm_stack_entry.h:28
Value returned by ast_to_llvm.
Definition: llvm.h:56
char name[MAX_IDENTIFIER_LENGTH]
Definition: llvm.h:70
Number num_info
Definition: llvm.h:60
char just_loaded[MAX_IDENTIFIER_LENGTH]
Definition: llvm.h:62
long long int constant
Definition: llvm.h:72
type_label label_index
Definition: llvm.h:74
bool has_name
Definition: llvm.h:64
type_register virtual_register_index
Definition: llvm.h:68
union LLVMValue::@1 value
LLVMValueType value_type
Definition: llvm.h:58
Container for various kinds of number data.
Definition: number.h:56