-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcodegen.c
305 lines (272 loc) · 13 KB
/
codegen.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/*
* LLVM code generators for the boris compiler
*
* Author: Yi Zhou
* May 5, 2019
*
*/
# include <stdio.h>
# include <stdlib.h>
# include <stdarg.h>
# include <string.h>
# include "boris.h"
// The strlen and printf for llvm-c implementation is
// from https://github.com/0vercl0k/stuffz/blob/master/llvm-funz/llvm-c-frontend-playing-with-ir.c
LLVMValueRef create_strlen_function(LLVMModuleRef *Module)
{
LLVMValueRef Zero8 = LLVMConstInt(LLVMInt8Type(), 0, 0);
LLVMValueRef Zero32 = LLVMConstInt(LLVMInt32Type(), 0, 0);
LLVMValueRef One32 = LLVMConstInt(LLVMInt32Type(), 1, 0);
LLVMBuilderRef Builder = LLVMCreateBuilder();
/// 1. int strlen(char *);
LLVMTypeRef StrlenArgsTyList[] = { LLVMPointerType(LLVMInt8Type(), 0) };
LLVMTypeRef StrlenTy = LLVMFunctionType(
LLVMInt32Type(),
StrlenArgsTyList,
1,
0
);
LLVMValueRef StrlenFunction = LLVMAddFunction(*Module, "strlen", StrlenTy);
LLVMValueRef s = LLVMGetParam(StrlenFunction, 0);
LLVMSetValueName(s, "s");
LLVMBasicBlockRef InitBasicBlock = LLVMAppendBasicBlock(StrlenFunction, "init");
LLVMBasicBlockRef CheckBasicBlock = LLVMAppendBasicBlock(StrlenFunction, "check");
LLVMBasicBlockRef BodyBasicBlock = LLVMAppendBasicBlock(StrlenFunction, "body");
LLVMBasicBlockRef EndBasicBlock = LLVMAppendBasicBlock(StrlenFunction, "end");
LLVMPositionBuilderAtEnd(Builder, InitBasicBlock);
/// 2. int i = 0;
LLVMValueRef i = LLVMBuildAlloca(Builder, LLVMInt32Type(), "i");
LLVMBuildStore(Builder, Zero32, i);
LLVMBuildBr(Builder, CheckBasicBlock);
/// 3. check:
LLVMPositionBuilderAtEnd(Builder, CheckBasicBlock);
/// 4. if(s[i] == 0)
LLVMValueRef id_if[] = { LLVMBuildLoad(Builder, i, "") };
LLVMValueRef If = LLVMBuildICmp(
Builder,
LLVMIntNE,
Zero8,
LLVMBuildLoad(
Builder,
LLVMBuildGEP(Builder, s, id_if, 1, ""),
""
),
""
);
/// 5. goto end;
LLVMBuildCondBr(Builder, If, BodyBasicBlock, EndBasicBlock);
/// 6. body:
LLVMPositionBuilderAtEnd(Builder, BodyBasicBlock);
/// 7. i += 1;
LLVMBuildStore(
Builder,
LLVMBuildAdd(
Builder,
LLVMBuildLoad(
Builder,
i,
""
),
One32,
""
),
i
);
/// 8. goto check;
LLVMBuildBr(Builder, CheckBasicBlock);
/// 9. end:
LLVMPositionBuilderAtEnd(Builder, EndBasicBlock);
/// 10. return i;
LLVMBuildRet(Builder, LLVMBuildLoad(Builder, i, ""));
return StrlenFunction;
}
// Generates an LLVM value object for a NODETYPE_INT.
// Returns an LLVM value reference.
LLVMValueRef boris_codegen_int(struct pNode* node){
struct iNode* inode = (struct iNode*) node;
return LLVMConstInt(LLVMInt32Type(), inode->ival, 0);
}
// Generates an LLVM value object for NODETYPE_EXPR_PLUS_EXPR.
// Returns an LLVM value reference.
LLVMValueRef boris_codegen_expr_plus_expr(struct pNode* node, LLVMBuilderRef builder, LLVMModuleRef module, \
struct symboltable* global_tb, struct symboltableStack* local_tbstk){
LLVMValueRef left = boris_codegen_expr(node->childs[0], builder, module, global_tb, local_tbstk);
LLVMValueRef right = boris_codegen_expr(node->childs[2], builder, module, global_tb, local_tbstk);
return LLVMBuildAdd(builder, left, right, "expr_plus_expr_tmp");
}
// Generates an LLVM value object for NODETYPE_EXPR_MINUS_EXPR.
// Returns an LLVM value reference.
LLVMValueRef boris_codegen_expr_minus_expr(struct pNode* node, LLVMBuilderRef builder, LLVMModuleRef module, \
struct symboltable* global_tb, struct symboltableStack* local_tbstk){
LLVMValueRef left = boris_codegen_expr(node->childs[0], builder, module, global_tb, local_tbstk);
LLVMValueRef right = boris_codegen_expr(node->childs[2], builder, module, global_tb, local_tbstk);
return LLVMBuildSub(builder, left, right, "expr_minus_expr_tmp");
}
// Generates an LLVM value object for NODETYPE_EXPR_DIV_EXPR.
// Returns an LLVM value reference.
LLVMValueRef boris_codegen_expr_div_expr(struct pNode* node, LLVMBuilderRef builder, LLVMModuleRef module, \
struct symboltable* global_tb, struct symboltableStack* local_tbstk){
LLVMValueRef left = boris_codegen_expr(node->childs[0], builder, module, global_tb, local_tbstk);
LLVMValueRef right = boris_codegen_expr(node->childs[2], builder, module, global_tb, local_tbstk);
return LLVMBuildUDiv(builder, left, right, "expr_div_expr_tmp");
}
// Generates an LLVM value object for NODETYPE_EXPR_MULT_EXPR.
// Returns an LLVM value reference.
LLVMValueRef boris_codegen_expr_mult_expr(struct pNode* node, LLVMBuilderRef builder, LLVMModuleRef module, \
struct symboltable* global_tb, struct symboltableStack* local_tbstk){
LLVMValueRef left = boris_codegen_expr(node->childs[0], builder, module, global_tb, local_tbstk);
LLVMValueRef right = boris_codegen_expr(node->childs[2], builder, module, global_tb, local_tbstk);
return LLVMBuildMul(builder, left, right, "expr_mul_expr_tmp");
}
// Recursively generates LLVM objects to build the code.
// Returns an LLVM value reference.
LLVMValueRef boris_codegen_expr(struct pNode *node, LLVMBuilderRef builder, LLVMModuleRef module, \
struct symboltable* global_tb, struct symboltableStack* local_tbstk){
// Recursively generate a value refenrence for expr
switch(node->pnodetype) {
case NODETYPE_INT: {
return boris_codegen_int(node);
}
case NODETYPE_SINGLE_INT_AS_EXPR:{
return boris_codegen_int(node->childs[0]);
}
case NODETYPE_EXPR_PLUS_EXPR: {
return boris_codegen_expr_plus_expr(node, builder, module, global_tb, local_tbstk);
}
case NODETYPE_EXPR_MINUS_EXPR: {
return boris_codegen_expr_minus_expr(node, builder, module, global_tb, local_tbstk);
}
case NODETYPE_EXPR_MULT_EXPR: {
return boris_codegen_expr_mult_expr(node, builder, module, global_tb, local_tbstk);
}
case NODETYPE_EXPR_DIV_EXPR: {
return boris_codegen_expr_div_expr(node, builder, module, global_tb, local_tbstk);
}
case NODETYPE_LPAR_EXPR_RPAR: {
return boris_codegen_expr(node->childs[1], builder, module, global_tb, local_tbstk);
}
case NODETYPE_SINGLE_ID_AS_EXPR:{
struct sNode* snode = (struct sNode*)node->childs[0];
struct symboltable* matched_tb = get_matched_symboltable(snode->sval, global_tb, local_tbstk);
struct symboltableRecord* record = lookup_symbol(snode->sval, matched_tb->scope, matched_tb);
if (record->valuetype == VALUETYPE_INT) {
return LLVMBuildLoad(builder, record->value->address, "load_singleid_int_temp");
} else if (record->valuetype == VALUETYPE_TUPLE) {
return record->value->address; // tuple type return its address
}else {
printf("codegen. not allowed id type\n");
exit(666);
}
}
case NODETYPE_EXPR_COMMA_EXPR:{
int width = get_expr_width(node, global_tb, local_tbstk);
// allocate tuple
LLVMValueRef tuple_address = LLVMBuildArrayAlloca(builder, LLVMInt32Type(), LLVMConstInt(LLVMInt32Type(), width, 0), "");
// populate tuple
LLVMValueRef results[width];
read_all_values_in_expr(node, results, global_tb, local_tbstk, builder, module);
populate_into_address(results, width, tuple_address, builder);
return tuple_address;
}
case NODETYPE_ARRAY_REF_AS_EXPR:{
struct sNode* snode = (struct sNode*)node->childs[0];
struct pNode* exprnode = node->childs[2];
struct symboltable* matched_tb = get_matched_symboltable(snode->sval, global_tb, local_tbstk);
struct symboltableRecord* record = lookup_symbol(snode->sval, matched_tb->scope, matched_tb);
LLVMValueRef array_address = record->value->address;
LLVMValueRef offset = LLVMBuildSub(builder, boris_codegen_expr(exprnode, builder, module, global_tb, local_tbstk), LLVMConstInt(LLVMInt32Type(), record->value->ivallist_start, 0), ""); // array access should consider its start point
LLVMValueRef indices[] = { offset };
LLVMValueRef element_address = LLVMBuildGEP(builder, array_address, indices, 1, "");
return LLVMBuildLoad(builder, element_address, "load_array_ref_int_temp");
}
case NODETYPE_TUPLE_REF_AS_EXPR:{
struct sNode* snode = (struct sNode*)node->childs[0];
struct pNode* exprnode = node->childs[2];
struct symboltable* matched_tb = get_matched_symboltable(snode->sval, global_tb, local_tbstk);
struct symboltableRecord* record = lookup_symbol(snode->sval, matched_tb->scope, matched_tb);
LLVMValueRef tuple_address = record->value->address;
LLVMValueRef offset = boris_codegen_expr(exprnode, builder, module, global_tb, local_tbstk);
LLVMValueRef indices[] = { offset };
LLVMValueRef element_address = LLVMBuildGEP(builder, tuple_address, indices, 1, "");
return LLVMBuildLoad(builder, element_address, "load_tuple_ref_int_temp");
}
case NODETYPE_FUNC_CALL_AS_EXPR: {
struct sNode* func_name = (struct sNode*)node->childs[0];
struct pNode* exprnode = node->childs[1];
struct symboltableRecord* func_record = lookup_symbol(func_name->sval, GLOBAL_SCOPE, global_tb);
struct symboltableRecordFunction* func_value = (struct symboltableRecordFunction*)func_record->value;
if (func_value->formal_parameter_valuetype == VALUETYPE_INT && func_value->return_valuetype == VALUETYPE_INT){
LLVMValueRef Function = LLVMGetNamedFunction(module, func_name->sval);
LLVMValueRef FunctionArgs[] = { boris_codegen_expr(exprnode, builder, module, global_tb, local_tbstk) };
LLVMValueRef ret = LLVMBuildCall(
builder,
Function,
FunctionArgs,
1,
""
);
return ret;
} else if (func_value->formal_parameter_valuetype == VALUETYPE_TUPLE && func_value->return_valuetype == VALUETYPE_INT){
LLVMValueRef Function = LLVMGetNamedFunction(module, func_name->sval);
LLVMValueRef FunctionArgs[] = { boris_codegen_expr(exprnode, builder, module, global_tb, local_tbstk) };
LLVMValueRef ret = LLVMBuildCall(
builder,
Function,
FunctionArgs,
1,
""
);
return ret;
}else {
printf("no implementation - func call\n" );
}
}
}
return NULL;
}
// customized llvm module for boris language
void begin_boris_module(LLVMBuilderRef builder,LLVMModuleRef module){
//set target (= "x86_64-pc-linux-gnu")
char *triple = LLVMGetDefaultTargetTriple();
LLVMSetTarget(module, triple);
// builtin module print utility
/// define strlen(char*, ...)
create_strlen_function(&module);
/// extern int printf(char*, ...)
LLVMTypeRef PrintfArgsTyList[] = { LLVMPointerType(LLVMInt8Type(), 0) };
LLVMTypeRef PrintfTy = LLVMFunctionType(
LLVMInt32Type(),
PrintfArgsTyList,
0,
1 // IsVarArg
);
LLVMAddFunction(module, "printf", PrintfTy);
/// void main(void)
LLVMTypeRef MainFunctionTy = LLVMFunctionType(
LLVMVoidType(), NULL, 0, 0
);
LLVMValueRef MainFunction = LLVMAddFunction(module, "main", MainFunctionTy);
LLVMBasicBlockRef MainEntry = LLVMAppendBasicBlock(MainFunction, "MainEntry");
LLVMPositionBuilderAtEnd(builder, MainEntry);
//print > Hello From LLVM
//char* message = "> Hello from compiled boris-LLVM object\n";
//boris_codegen_message(message, strlen(message), builder, module);
}
// wrap up llvm module for boris language
void end_boris_module(LLVMBuilderRef builder,LLVMModuleRef module){
LLVMValueRef MainFunction = LLVMGetNamedFunction(module, "main");
LLVMBasicBlockRef EndEntry = LLVMAppendBasicBlock(MainFunction, "EndEntry");
LLVMBuildBr(builder, EndEntry);
LLVMPositionBuilderAtEnd(builder, EndEntry);
LLVMBuildRetVoid(builder);
}
// handle the generated LLVM module before output
void verify_llvm_module_and_output(LLVMModuleRef module){
char *verify_error = NULL;
char *print_error = NULL;
LLVMVerifyModule(module, LLVMAbortProcessAction, &verify_error);
LLVMDisposeMessage(verify_error);
LLVMWriteBitcodeToFile(module, "a.o");
LLVMPrintModuleToFile(module, "a.ll", &print_error);
LLVMDisposeMessage(print_error);
}