-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.y
More file actions
executable file
·241 lines (201 loc) · 8.47 KB
/
parser.y
File metadata and controls
executable file
·241 lines (201 loc) · 8.47 KB
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
%{
#include <stdio.h>
#include <stdlib.h>
#include <llvm-c/Analysis.h>
#include <llvm-c/Core.h>
#include <llvm-c/ExecutionEngine.h>
#include <llvm-c/IRReader.h>
#include <llvm-c/Transforms/Scalar.h>
#if LLVM_VERSION_MAJOR >= 7
#include <llvm-c/Transforms/Utils.h>
#endif
#include "ast.h"
#include "utils.h"
int yylex(void);
void yyerror(LLVMModuleRef module, LLVMBuilderRef builder, const char* s);
%}
%parse-param {void *module}
%parse-param {void *builder}
%union {
int value;
size_t id;
struct expr *expr;
enum value_type {
ERROR = -1,
UNTYPED = 0,
INTEGER = 1,
BOOLEAN = 2,
FLOAT = 3,
} type;
struct stmt *stmt;
}
%token AND OR
%token GE LE EQ NE
%token FALSE TRUE
%token IF ELSE WHILE DO FOR PRINT
%token BOOL_TYPE INT_TYPE FLOAT_TYPE
%token <id> ID
%token <value> VAL
%token <value> VAL2
%type <expr> expr
%type <stmt> stmt
%type <stmt> stmts
%type <type> type
%nonassoc IF_ALONE
%nonassoc ELSE
%left GE LE EQ NE '>' '<'
%left AND OR
%left INC DEC
%left INCX DECX
%left MODX
%left MULX DIVX
%left '+' '-'
%left '%'
%left '*' '/'
%%
program: decls stmt {
if (valid_stmt($2)) {
codegen_stmt($2, module, builder);
} else {
fprintf(stderr, "INVALID PROGRAM\n");
exit(1);
}
// printf("{\n");
// print_stmt($2, 1);
// printf("}\n");
free_stmt($2);
}
type: BOOL_TYPE { $$ = BOOLEAN; }
| INT_TYPE { $$ = INTEGER; }
| FLOAT_TYPE { $$ = FLOAT; }
decls: decls decl | ;
decl: type ID ';' {
if (vector_get(&global_types, $2)) {
printf("Multiple declarations for identifier %s\n", string_int_rev(&global_ids, $2));
exit(0);
} else {
LLVMTypeRef t;
if($1 == BOOLEAN) {
t = LLVMInt1Type();
} else if($1 == INTEGER) {
t = LLVMInt32Type();
} else if($1 == FLOAT) {
t = LLVMFloatType();
}
LLVMValueRef p = LLVMBuildAlloca(builder, t, string_int_rev(&global_ids, $2));
vector_set(&global_types, $2, p);
}
}
stmts: stmts stmt { $$ = make_seq($1, $2); }
| stmt { $$ = $1; };
stmt: '{' stmts '}' { $$ = $2; }
| ID '=' expr ';' { $$ = make_assign($1, $3); }
| IF '(' expr ')' stmt %prec IF_ALONE { $$ = make_if($3, $5); }
| IF '(' expr ')' stmt ELSE stmt { $$ = make_ifelse($3, $5, $7); }
| WHILE '(' expr ')' stmt { $$ = make_while($3, $5); }
| FOR '(' stmt expr ';' stmt ')' stmt { $$ = make_for($3, $4, $6, $8); }
| DO stmt WHILE '(' expr ')' ';' { $$ = make_dowhile($2, $5); }
| ID INC ';' { $$ = make_assign($1, binop(variable($1), '+', literal(1))); }
| ID DEC ';' { $$ = make_assign($1, binop(variable($1), '-', literal(1))); }
| ID INCX expr ';' { $$ = make_assign($1, binop(variable($1), '+', $3)); }
| ID DECX expr ';' { $$ = make_assign($1, binop(variable($1), '-', $3)); }
| ID MODX expr ';' { $$ = make_assign($1, binop(variable($1), '%', $3)); }
| ID MULX expr ';' { $$ = make_assign($1, binop(variable($1), '*', $3)); }
| ID DIVX expr ';' { $$ = make_assign($1, binop(variable($1), '/', $3)); }
| ID INC { $$ = make_assign($1, binop(variable($1), '+', literal(1))); }
| ID DEC { $$ = make_assign($1, binop(variable($1), '-', literal(1))); }
| ID INCX expr { $$ = make_assign($1, binop(variable($1), '+', $3)); }
| ID DECX expr { $$ = make_assign($1, binop(variable($1), '-', $3)); }
| ID MODX expr { $$ = make_assign($1, binop(variable($1), '%', $3)); }
| ID MULX expr { $$ = make_assign($1, binop(variable($1), '*', $3)); }
| ID DIVX expr { $$ = make_assign($1, binop(variable($1), '/', $3)); }
| PRINT expr ';' { $$ = make_print($2); }
expr: VAL { $$ = literal($1); }
| VAL2 { $$ = decimal($1); }
| FALSE { $$ = bool_lit(0); }
| TRUE { $$ = bool_lit(1); }
| ID { $$ = variable($1); }
| '(' expr ')' { $$ = $2; }
| expr '+' expr { $$ = binop($1, '+', $3); }
| expr '-' expr { $$ = binop($1, '-', $3); }
| expr '*' expr { $$ = binop($1, '*', $3); }
| expr '/' expr { $$ = binop($1, '/', $3); }
| expr '%' expr { $$ = binop($1, '%', $3); }
| expr AND expr { $$ = binop($1, AND, $3); }
| expr OR expr { $$ = binop($1, OR, $3); }
| expr EQ expr { $$ = binop($1, EQ, $3); }
| expr NE expr { $$ = binop($1, NE, $3); }
| expr GE expr { $$ = binop($1, GE, $3); }
| expr LE expr { $$ = binop($1, LE, $3); }
| expr '>' expr { $$ = binop($1, '>', $3); }
| expr '<' expr { $$ = binop($1, '<', $3); }
;
%%
void yyerror(LLVMModuleRef module, LLVMBuilderRef builder, const char* s) {
fprintf(stderr, "%s\n", s);
}
int main(void)
{
LLVMModuleRef module = LLVMModuleCreateWithName("exe");
LLVMBuilderRef builder = LLVMCreateBuilder();
LLVMModuleRef runtime;
char *error;
LLVMMemoryBufferRef buffer;
LLVMExecutionEngineRef engine;
vector_init(&global_types);
string_int_init(&global_ids);
LLVMInitializeNativeTarget();
LLVMInitializeNativeAsmPrinter();
LLVMInitializeNativeAsmParser();
LLVMLinkInMCJIT();
// Create execution engine.
if (LLVMCreateExecutionEngineForModule(&engine, module, &error)) {
fprintf(stderr, "%s\n", error);
return 1;
} else if (LLVMCreateMemoryBufferWithContentsOfFile("runtime.bc", &buffer, &error)) {
fprintf(stderr, "%s\n", error);
return 1;
} else if (LLVMParseIRInContext(LLVMGetGlobalContext(), buffer, &runtime, &error)) {
fprintf(stderr, "%s\n", error);
return 1;
}
// Setup optimizations.
LLVMPassManagerRef pass_manager = LLVMCreateFunctionPassManagerForModule(module);
LLVMAddPromoteMemoryToRegisterPass(pass_manager);
LLVMInitializeFunctionPassManager(pass_manager);
// print_i32
LLVMTypeRef print_i32_args[] = { LLVMInt32Type() };
LLVMAddFunction(module, "print_i32",
LLVMFunctionType(LLVMVoidType(), print_i32_args, 1, 0));
// print_i1
LLVMTypeRef print_i1_args[] = { LLVMInt1Type() };
LLVMAddFunction(module, "print_i1",
LLVMFunctionType(LLVMVoidType(), print_i1_args, 1, 0));
// print_f32
LLVMTypeRef print_f32_args[] = { LLVMFloatType() };
LLVMAddFunction(module, "print_f32",
LLVMFunctionType(LLVMVoidType(), print_f32_args, 1, 0));
// create "main" function
LLVMTypeRef main_type = LLVMFunctionType(LLVMVoidType(), NULL, 0, 0);
LLVMValueRef main = LLVMAddFunction(module, "main", main_type);
LLVMBasicBlockRef main_bb = LLVMAppendBasicBlock(main, "entry");
LLVMPositionBuilderAtEnd(builder, main_bb);
yyparse(module, builder);
LLVMBuildRet(builder, 0);
// Dump entire module.
LLVMDumpModule(module);
LLVMVerifyModule(module, LLVMAbortProcessAction, &error);
LLVMRunFunctionPassManager(pass_manager, main);
// Dump entire module.
LLVMDumpModule(module);
fprintf(stderr, "Generating code\n");
void (*main_fn)() = (void (*)()) LLVMGetPointerToGlobal(engine, main);
fprintf(stderr, "Running\n");
main_fn();
fprintf(stderr, "Done\n");
vector_fini(&global_types);
string_int_fini(&global_ids);
LLVMDisposeBuilder(builder);
LLVMDisposeModule(module);
return 0;
}