-
Notifications
You must be signed in to change notification settings - Fork 4
/
driver.c
243 lines (199 loc) · 7.72 KB
/
driver.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
#include <stdio.h>
#include <stdlib.h>
#include "ast.h"
#include "vm_state.h"
#include "vm_value.h"
static void
drive_declaration(struct vm_state *, struct ast_declaration *);
static struct vm_value *
drive_expression(struct vm_state *, struct ast_expression *);
static void
drive_statement_list(struct vm_state *, struct ast_statement_list *);
static void
drive_compound_statement(struct vm_state *, struct ast_compound_statement *);
static void
drive_selection_statement(struct vm_state *, struct ast_selection_statement *);
static void
drive_while_statement(struct vm_state *, struct ast_while_statement *);
static void
drive_translation_unit(struct vm_state *, struct ast_translation_unit *);
static void
drive_node(struct vm_state *vm, struct ast_node *node)
{
switch (node->tag) {
case AST_NODE:
fprintf(stderr, "Request to drive generic ast node\n");
exit(EXIT_FAILURE);
case AST_DECLARATION:
drive_declaration(vm, (struct ast_declaration *) node);
return;
case AST_EXPRESSION:
drive_expression(vm, (struct ast_expression *) node);
return;
case AST_STATEMENT_LIST:
drive_statement_list(vm, (struct ast_statement_list *) node);
return;
case AST_COMPOUND_STATEMENT:
drive_compound_statement(vm, (struct ast_compound_statement *) node);
return;
case AST_SELECTION_STATEMENT:
drive_selection_statement(vm, (struct ast_selection_statement *) node);
return;
case AST_WHILE_STATEMENT:
drive_while_statement(vm, (struct ast_while_statement *) node);
return;
case AST_TRANSLATION_UNIT:
drive_translation_unit(vm, (struct ast_translation_unit *) node);
return;
default:
fprintf(stderr, "Request to drive unknonwn node: %d\n", node->tag);
exit(EXIT_FAILURE);
}
}
static void
drive_declaration(struct vm_state *vm,
struct ast_declaration *declaration)
{
struct vm_value *vmval;
vmval = vm_value_new(declaration->type_specifier,
declaration->identifier);
vmval->llvm_value = LLVMBuildAlloca(vm->builder,
vmval->llvm_type,
vmval->identifier);
vm_state_put_value(vm, vmval);
}
static struct vm_value *
drive_expression(struct vm_state *vm,
struct ast_expression *expression)
{
switch (expression->operator) {
case AST_ADD: case AST_SUB:
case AST_MUL: case AST_DIV: {
struct vm_value *lhs, *rhs;
lhs = drive_expression(vm, expression->subexpr[0]);
rhs = drive_expression(vm, expression->subexpr[1]);
return vm_value_build_math_op(vm, expression->operator, lhs, rhs);
}
case AST_GT: case AST_LT:
case AST_EQ: case AST_NE:
case AST_LE: case AST_GE: {
struct vm_value *lhs, *rhs;
lhs = drive_expression(vm, expression->subexpr[0]);
rhs = drive_expression(vm, expression->subexpr[1]);
return vm_value_build_cmp_op(vm, expression->operator, lhs, rhs);
}
case AST_ASSIGN: {
struct vm_value *ret, *lhs, *rhs;
lhs = vm_state_get_value(vm, expression->primary_expr.identifier);
rhs = drive_expression(vm, expression->subexpr[0]);
ret = vm_value_dup(lhs);
LLVMBuildStore(vm->builder, rhs->llvm_value, ret->llvm_value);
return ret;
}
case AST_IDENTIFIER: {
struct vm_value *vmval, *ret;
vmval = vm_state_get_value(vm, expression->primary_expr.identifier);
ret = vm_value_dup(vmval);
ret->llvm_value = LLVMBuildLoad(vm->builder, vmval->llvm_value, "");
return ret;
}
case AST_INT_CONSTANT:
return vm_value_new_from_int_constant(
expression->primary_expr.int_constant);
case AST_FLOAT_CONSTANT:
return vm_value_new_from_float_constant(
expression->primary_expr.float_constant);
default:
fprintf(stderr,
"Request to drive unknown expression operator: %d\n",
expression->operator);
exit(EXIT_FAILURE);
}
return NULL;
}
static void
drive_statement_list(struct vm_state *vm,
struct ast_statement_list *statement_list)
{
int i;
for (i = 0; i < statement_list->number_of_statements; i++)
drive_node(vm, statement_list->statement[i]);
}
static void
drive_selection_statement(struct vm_state *vm,
struct ast_selection_statement *selection_statement)
{
LLVMBasicBlockRef current_block,
condition_block, then_block, else_block,
merge_block;
LLVMValueRef function_value;
struct vm_value *cond_vmval;
current_block = LLVMGetInsertBlock(vm->builder);
function_value = LLVMGetBasicBlockParent(current_block);
condition_block = LLVMAppendBasicBlock(function_value, "if_condition");
then_block = LLVMAppendBasicBlock(function_value, "then_body");
if (selection_statement->else_body != NULL)
else_block = LLVMAppendBasicBlock(function_value, "else_body");
merge_block = LLVMAppendBasicBlock(function_value, "if_merge");
LLVMBuildBr(vm->builder, condition_block);
LLVMPositionBuilderAtEnd(vm->builder, condition_block);
cond_vmval = drive_expression(vm, selection_statement->condition);
if (selection_statement->else_body != NULL) {
LLVMBuildCondBr(vm->builder, cond_vmval->llvm_value,
then_block, else_block);
} else {
LLVMBuildCondBr(vm->builder, cond_vmval->llvm_value,
then_block, merge_block);
}
LLVMPositionBuilderAtEnd(vm->builder, then_block);
drive_compound_statement(vm, selection_statement->then_body);
LLVMBuildBr(vm->builder, merge_block);
if (selection_statement->else_body != NULL) {
LLVMPositionBuilderAtEnd(vm->builder, else_block);
drive_compound_statement(vm, selection_statement->else_body);
LLVMBuildBr(vm->builder, merge_block);
}
LLVMPositionBuilderAtEnd(vm->builder, merge_block);
}
static void
drive_while_statement(struct vm_state *vm,
struct ast_while_statement *while_statement)
{
LLVMBasicBlockRef current_block, condition_block, body_block, merge_block;
LLVMValueRef function_value;
struct vm_value *cond_vmval;
current_block = LLVMGetInsertBlock(vm->builder);
function_value = LLVMGetBasicBlockParent(current_block);
condition_block = LLVMAppendBasicBlock(function_value, "while_condition");
body_block = LLVMAppendBasicBlock(function_value, "while_body");
merge_block = LLVMAppendBasicBlock(function_value, "while_merge");
LLVMBuildBr(vm->builder, condition_block);
LLVMPositionBuilderAtEnd(vm->builder, condition_block);
cond_vmval = drive_expression(vm, while_statement->condition);
LLVMBuildCondBr(vm->builder, cond_vmval->llvm_value,
body_block, merge_block);
LLVMPositionBuilderAtEnd(vm->builder, body_block);
drive_compound_statement(vm, while_statement->body);
LLVMBuildBr(vm->builder, condition_block);
LLVMPositionBuilderAtEnd(vm->builder, merge_block);
}
static void
drive_compound_statement(struct vm_state *vm,
struct ast_compound_statement *compound_statement)
{
drive_statement_list(vm, compound_statement->statement_list);
}
static void
drive_translation_unit(struct vm_state *vm,
struct ast_translation_unit *translation_unit)
{
drive_statement_list(vm, translation_unit->statement_list);
}
void
ast_to_llvm(struct ast_translation_unit *translation_unit)
{
struct vm_state *vm;
vm = vm_state_create("Toy");
drive_translation_unit(vm, translation_unit);
vm_state_destroy(vm);
}