-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquadComp.y
493 lines (415 loc) · 14.5 KB
/
quadComp.y
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
%{
#include <stdio.h>
#include <string.h>
#include <math.h>
#include "global.h"
#include "q_operations.h"
extern int yylex(void);
extern void yyerror(const char* s);
extern int yylineno;
// global symbol table
symtabEntry* symbolTable;
// offset relative to the beginning of the function where this variable will be stored
// int memOffset;
// global variable in which the current father (the context, for example the function) is saved
symtabEntry* symbolTableFather = NULL;
%}
//Bison declarations
%union {
int intval;
float floatval;
const struct variable_type *var_type;
struct q_operand operand;
char string[1000];
struct q_op_jump *jmp_op;
}
%start programm
%right ASSIGN
%token RETURN
%token TYPE_VOID
%token VAR_TYPE
%token IDENTIFIER
%token INT_CONSTANT
%token FLOAT_CONSTANT
/* Control structures */
%token IF
%token ELSE
%token WHILE
%token DO
/* Arithemetic and logical operations */
// Adapted from http://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2Bv
//%left '=' // unsure about this, is it really necessary?
%left LOG_OR
%left LOG_AND
%left EQUAL NOT_EQUAL
%left '<' LESS_OR_EQUAL '>' GREATER_OR_EQUAL
%left SHIFTLEFT
%left '+' '-'
%left '*' '/' '%'
%left INC_OP DEC_OP U_PLUS U_MINUS '!'
%type <var_type> VAR_TYPE
%type <intval> INT_CONSTANT
%type <floatval> FLOAT_CONSTANT
%type <string> IDENTIFIER
%type <var_type> declaration
%type <intval> parameter_list
%type <string> id
%type <operand> expression
%type <operand> assignment
%type <intval> stmt_no_marker
%type <jmp_op> if_evaluate_marker
%type <jmp_op> else_marker
%% // grammar rules
programm
: function { DEBUG("Program with single function"); }
| programm function { DEBUG("Program with more functions"); }
;
function
: var_type id set_father '(' parameter_list ')' ';'
{
// $5: number of parameters in parameter_list
getSymboltableEntry(symbolTable, $<string>2, NULL)->parameter = $5;
memOffset = 0;
}
| var_type id set_father '(' parameter_list ')'
{
getSymboltableEntry(symbolTable, $<string>2, NULL)->parameter = $5;
getSymboltableEntry(symbolTable, $<string>2, NULL)->line = q_op_list_get_instr_count();
}
function_body
{
symbolTableFather = NULL; // leaving function scope
DEBUG("Resetting memOffset to 0.");
memOffset = 0;
}
;
set_father
:
{
// function already has a symbol table entry if a prototype was
// declared
// $<string>0 is the function's name (id)
symbolTableFather = getSymboltableEntry(symbolTable, $<string>0, NULL);
if (!symbolTableFather) {
// function not in symbol table
addSymboltableEntry(&symbolTable, $<string>0, FUNC, NOP, memOffset, 0, 0, 0, NULL, 0);
}
symbolTableFather = getSymboltableEntry(symbolTable, $<string>0, NULL);
}
;
function_body
: '{' statement_list '}'
| '{' declaration_list statement_list '}'
;
declaration_list
: declaration ';'
| declaration_list declaration ';'
;
declaration
: VAR_TYPE id
{
$$ = $1; // necessary for declaration list
// $<string>2: two entries up the stack (in this case, this refers to the value of 'id', which is the name of the identifier, assigned to yylval in quadComp.l)
addVarToSymtab(&symbolTable, $<string>2, $1, symbolTableFather);
}
| declaration ',' id // such as 'int x, y, z;'
{
$$ = $1;
addVarToSymtab(&symbolTable, $<string>3, $1, symbolTableFather);
}
;
/* If a function was declared with a prototype, parameter_list was already traversed and the parameter's entered into
the symbol table. So we only add the parameters if they don't yet exist.
The function name resides at $<string>-2 on the stack.
$$ is set to the number of parameters in parameter_list, as this is what is needed
for the function's symbol table entry.
*/
parameter_list
: VAR_TYPE id
{
$$ = 1;
symtabEntry* father = getSymboltableEntry(symbolTable, $<string>-2, NULL);
if ( ! getSymboltableEntry(symbolTable, $<string>2, father) ) {
symtabEntry* newEntry = addVarToSymtab(&symbolTable, $<string>2, $1, father);
newEntry->parameter = 1;
}
else {
// memOffset has to be advanced anyway
memOffset += $1->size;
}
}
| parameter_list ',' VAR_TYPE id
{
$$ = $1 + 1;
symtabEntry* father = getSymboltableEntry(symbolTable, $<string>-2, NULL);
if ( ! getSymboltableEntry(symbolTable, $<string>4, father) ) {
symtabEntry* newEntry = addVarToSymtab(&symbolTable, $<string>4, $3, father);
newEntry->parameter = $$;
}
else {
// memOffset has to be advanced anyway
memOffset += $3->size;
}
}
| TYPE_VOID { $$ = 0; }
;
var_type
: VAR_TYPE { DEBUG("Standart variable type"); }
| TYPE_VOID { DEBUG("Void variable type"); }
;
statement_list
: statement
| statement_list statement
;
statement
: matched_statement
| unmatched_statement
;
matched_statement
: IF '(' assignment ')' if_evaluate_marker matched_statement ELSE else_marker matched_statement {
$8->target = q_op_list_get_instr_count();
}
| assignment ';'
| RETURN ';' {
q_instr_add(Q_INSTR_TYPE_RET, NULL);
}
| RETURN assignment ';' {
q_instr_add(Q_INSTR_TYPE_RET, &($2));
}
| WHILE '(' assignment ')' matched_statement
| DO stmt_no_marker statement WHILE '(' assignment ')' ';' {
// Jump to head of loop if "assignment" evaluated to "true"
q_instr_add(Q_INSTR_TYPE_COND_JUMP, Q_JUMP_WHEN_TRUE($6), $2);
}
| '{' statement_list '}'
| '{' '}'
;
unmatched_statement
: IF '(' assignment ')' if_evaluate_marker statement {
// Fill in correct jump target for the jump instruction generated by if_evaluate_marker
$5->target = q_op_list_get_instr_count();
}
| WHILE '(' assignment ')' unmatched_statement
| IF '(' assignment ')' if_evaluate_marker matched_statement ELSE else_marker unmatched_statement {
$8->target = q_op_list_get_instr_count();
}
;
if_evaluate_marker
: {
/* Check if condition evaluated to false. If so, jump to the end of the "if" block.
* Since we do not know the end just yet, set the jump target to -1. It will be set
* When the whole if-block has been parsed.
*/
$$ = (struct q_op_jump *) q_instr_add(Q_INSTR_TYPE_COND_JUMP, Q_JUMP_WHEN_FALSE($<operand>-1), Q_UNKNOWN_JUMP_TARGET);
}
;
else_marker
: {
$$ = (struct q_op_jump *) q_instr_add(Q_INSTR_TYPE_JUMP, Q_UNKNOWN_JUMP_TARGET);
($<jmp_op>-2)->target = q_op_list_get_instr_count();
}
;
assignment
: expression {
// return value of the expression
$$ = $1;
}
| id ASSIGN expression {
DEBUG("Assignment recognized");
// Lookup identifier in symbol table
symtabEntry *symtabEntry = getSymboltableEntryInScope(symbolTable, symbolTableFather, $1);
if (!symtabEntry) {
fprintf(stderr, "In function %s: Variable not declared: %s\n", symbolTableFather->name, $1);
YYABORT;
}
q_instr_add(Q_INSTR_TYPE_ASSIGN, symtabEntry, $3);
// return the value that was assigned
$$ = $3;
}
;
expression
: INC_OP expression {
if ($2.type == OPD_TYPE_LITERAL) {
fprintf(stderr, "Cannot pre-increment a literal value.\n");
YYABORT;
}
// var := var + 1
q_instr_add(Q_INSTR_TYPE_CALC, $2.data.varEntry, $2, Q_ARITHMETIC_OP_ADD, literal_one);
$$ = $2;
}
| DEC_OP expression {
// TODO
$$ = literal_one;
}
| expression LOG_OR expression {
symtabEntry *retVarEntry = getTempVariable(&symbolTable, &type_integer, symbolTableFather);
$$ = q_operand_init_variable(retVarEntry);
q_instr_add(Q_INSTR_TYPE_COND_JUMP, Q_JUMP_WHEN_TRUE($1), q_op_list_get_instr_count() + 4);
q_instr_add(Q_INSTR_TYPE_COND_JUMP, Q_JUMP_WHEN_TRUE($3), q_op_list_get_instr_count() + 3);
q_instr_add(Q_INSTR_TYPE_ASSIGN, retVarEntry, Q_FALSE);
q_instr_add(Q_INSTR_TYPE_JUMP, q_op_list_get_instr_count() + 2);
q_instr_add(Q_INSTR_TYPE_ASSIGN, retVarEntry, Q_TRUE);
}
| expression LOG_AND expression {
symtabEntry *retVarEntry = getTempVariable(&symbolTable, &type_integer, symbolTableFather);
$$ = q_operand_init_variable(retVarEntry);
q_instr_add(Q_INSTR_TYPE_COND_JUMP, Q_JUMP_WHEN_FALSE($1), q_op_list_get_instr_count() + 4);
q_instr_add(Q_INSTR_TYPE_COND_JUMP, Q_JUMP_WHEN_FALSE($3), q_op_list_get_instr_count() + 3);
q_instr_add(Q_INSTR_TYPE_ASSIGN, retVarEntry, Q_TRUE);
q_instr_add(Q_INSTR_TYPE_JUMP, q_op_list_get_instr_count() + 2);
q_instr_add(Q_INSTR_TYPE_ASSIGN, retVarEntry, Q_FALSE);
}
| expression NOT_EQUAL expression {
symtabEntry *result = getTempVariable(&symbolTable, &type_integer, symbolTableFather);
$$ = q_operand_init_variable(result);
q_instr_add_rel(result, $1, Q_RELATIVE_OP_NOT_EQUAL, $3);
}
| expression EQUAL expression {
symtabEntry *result = getTempVariable(&symbolTable, &type_integer, symbolTableFather);
$$ = q_operand_init_variable(result);
q_instr_add_rel(result, $1, Q_RELATIVE_OP_EQUAL, $3);
}
| expression GREATER_OR_EQUAL expression {
symtabEntry *result = getTempVariable(&symbolTable, &type_integer, symbolTableFather);
$$ = q_operand_init_variable(result);
q_instr_add_rel(result, $1, Q_RELATIVE_OP_GREATER_EQUAL, $3);
}
| expression LESS_OR_EQUAL expression {
symtabEntry *result = getTempVariable(&symbolTable, &type_integer, symbolTableFather);
$$ = q_operand_init_variable(result);
q_instr_add_rel(result, $1, Q_RELATIVE_OP_LOWER_EQUAL, $3);
}
| expression '>' expression {
symtabEntry *result = getTempVariable(&symbolTable, &type_integer, symbolTableFather);
$$ = q_operand_init_variable(result);
q_instr_add_rel(result, $1, Q_RELATIVE_OP_GREATER, $3);
}
| expression '<' expression {
symtabEntry *result = getTempVariable(&symbolTable, &type_integer, symbolTableFather);
$$ = q_operand_init_variable(result);
q_instr_add_rel(result, $1, Q_RELATIVE_OP_LOWER, $3);
}
| expression SHIFTLEFT expression {
symtabEntry *retVarEntry = getTempVariable(&symbolTable, q_op_assignment_get_result_type($1, $3), symbolTableFather);
$$ = q_operand_init_variable(retVarEntry);
if (q_operand_get_type($3)->symtabType == INTEGER) {
if ($3.type == OPD_TYPE_LITERAL) {
struct q_operand rightMulOp;
rightMulOp.type = OPD_TYPE_LITERAL;
rightMulOp.data.literal.type = &type_integer;
rightMulOp.data.literal.value.int_value = (int) pow(2, $3.data.literal.value.int_value);
q_instr_add(Q_INSTR_TYPE_CALC, retVarEntry, $1, Q_ARITHMETIC_OP_MUL, rightMulOp);
}
else {
fprintf(stderr, "Can only shift by a constant value.");
YYABORT;
}
}
else {
fprintf(stderr, "Can't shift by a non-integer value.");
YYABORT;
}
}
| expression '+' expression {
symtabEntry *retVarEntry = getTempVariable(&symbolTable, q_op_assignment_get_result_type($1, $3), symbolTableFather);
$$ = q_operand_init_variable(retVarEntry);
q_instr_add(Q_INSTR_TYPE_CALC, retVarEntry, $1, Q_ARITHMETIC_OP_ADD, $3);
}
| expression '-' expression {
symtabEntry *retVarEntry = getTempVariable(&symbolTable, q_op_assignment_get_result_type($1, $3), symbolTableFather);
$$ = q_operand_init_variable(retVarEntry);
q_instr_add(Q_INSTR_TYPE_CALC, retVarEntry, $1, Q_ARITHMETIC_OP_SUB, $3);
}
| expression '*' expression {
symtabEntry *retVarEntry = getTempVariable(&symbolTable, q_op_assignment_get_result_type($1, $3), symbolTableFather);
$$ = q_operand_init_variable(retVarEntry);
q_instr_add(Q_INSTR_TYPE_CALC, retVarEntry, $1, Q_ARITHMETIC_OP_MUL, $3);
}
| expression '/' expression {
symtabEntry *retVarEntry = getTempVariable(&symbolTable, q_op_assignment_get_result_type($1, $3), symbolTableFather);
$$ = q_operand_init_variable(retVarEntry);
q_instr_add(Q_INSTR_TYPE_CALC, retVarEntry, $1, Q_ARITHMETIC_OP_DIV, $3);
}
| expression '%' expression {
symtabEntry *retVarEntry = getTempVariable(&symbolTable, q_op_assignment_get_result_type($1, $3), symbolTableFather);
$$ = q_operand_init_variable(retVarEntry);
q_instr_add(Q_INSTR_TYPE_CALC, retVarEntry, $1, Q_ARITHMETIC_OP_MOD, $3);
}
| '!' expression {
symtabEntry *retVarEntry = getTempVariable(&symbolTable, &type_integer, symbolTableFather);
$$ = q_operand_init_variable(retVarEntry);
q_instr_add(Q_INSTR_TYPE_COND_JUMP, Q_JUMP_WHEN_FALSE($2), q_op_list_get_instr_count() + 3);
q_instr_add(Q_INSTR_TYPE_ASSIGN, retVarEntry, Q_FALSE);
q_instr_add(Q_INSTR_TYPE_JUMP, q_op_list_get_instr_count() + 2);
q_instr_add(Q_INSTR_TYPE_ASSIGN, retVarEntry, Q_TRUE);
}
| '+' expression %prec U_PLUS {
// TODO
$$ = literal_one;
}
| '-' expression %prec U_MINUS {
// TODO
$$ = literal_one;
}
| FLOAT_CONSTANT {
DEBUG("Float constant recognized");
$$.type = OPD_TYPE_LITERAL;
$$.data.literal.type = &type_real;
$$.data.literal.value.float_value = $1;
}
| INT_CONSTANT {
DEBUG("Int constant recognized");
$$.type = OPD_TYPE_LITERAL;
$$.data.literal.type = &type_integer;
$$.data.literal.value.int_value = $1;
}
| '(' expression ')' {
$$ = $2;
}
| id '(' exp_list ')' {
// TODO
$$ = literal_one;
}
| id '(' ')' {
// TODO
$$ = literal_one;
}
| id {
// Lookup identifier in symbol table
symtabEntry *entry = getSymboltableEntryInScope(symbolTable, symbolTableFather, $1);
if (!entry) {
fprintf(stderr, "In function %s: Variable not declared: %s\n", symbolTableFather->name, $1);
YYABORT;
}
$$ = q_operand_init_variable(entry);
}
;
exp_list
: expression
| exp_list ',' expression
;
id
: IDENTIFIER { strcpy($$, $1); }
;
stmt_no_marker
: {
$$ = q_op_list_get_instr_count();
}
%%
int main(int argc, char **argv) {
printf("Argument count is: %d", argc);
yylineno = 1;
DEBUG("Superquad is now starting the parse process...");
yyparse();
// FILE* symFile;
// symFile = fopen("ourSym.txt", "w");
// fputs("blub\n", symFile);
// writeSymboltable(symbolTable, symFile);
writeSymboltable(symbolTable, stdout);
fprintf(stdout, "\n\n");
q_op_gen_code(stdout);
return 0;
}
void yyerror(const char* s) {
printf("%s in line %d.\n", s, yylineno);
}