-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode-gen.c
424 lines (420 loc) · 14.2 KB
/
code-gen.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
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
#include <stdio.h>
#include <string.h>
#include "code-gen.h"
#include "defs.h"
char *preamble = "#include \"preamble.h\"\n";
// Creates a new code generator from parse tree.
Generator *new_generator(ParseTree *tree)
{
Generator *generator = calloc(1, sizeof(Generator));
generator->tree = tree;
generator->code_string = calloc(1, 100);
generator->capacity = 100;
generator->length = 0;
return generator;
}
// Creates a new target code string pointed by the generator.
// This function is called in the main generate_code_string function.
void generate_new_code_string(Generator *generator)
{
gen(generator, preamble);
gen(generator, "#include <stdio.h>\n\nint main()\n{\n");
}
// Generates an identifier in the target code string.
void generate_identifier(Generator *generator, char *str)
{
gen(generator, "_");
gen(generator, str);
}
// Returns the string version of an integer.
char *get_str(int val)
{
char *str = malloc(LINE_LIMIT * sizeof(char));
sprintf(str, "%d", val);
return str;
}
// Generates an arbitrary string in the target code string.
// This function is called in almost all of the generating functions.
void gen(Generator *generator, char *str)
{
int len = strlen(str);
while (len + generator->length + 1 > generator->capacity)
{
generator->capacity *= 2;
}
generator->code_string = realloc(generator->code_string, generator->capacity);
generator->length += len;
strcat(generator->code_string, str);
}
// Generates an arbitrary statement in the target code string.
// This function calls the other statement type generating functions.
void generate_statement(Generator *generator, ASTNode *node)
{
switch (node->stmt_type)
{
case STMT_DECL:
generate_declaration_stmt(generator, node);
break;
case STMT_ASSN:
generate_assignment_stmt(generator, node);
break;
case STMT_PRINT:
generate_print_stmt(generator, node);
break;
}
}
// Generates a declaration statement in the target code string.
void generate_declaration_stmt(Generator *generator, ASTNode *node)
{
if (node->var_type.var_type == TYPE_SCALAR)
{
gen(generator, "double ");
generate_identifier(generator, node->var_name);
gen(generator, " = 0;");
}
else
{
gen(generator, "double **");
generate_identifier(generator, node->var_name);
gen(generator, " = allocate_matrix(");
gen(generator, get_str(node->var_type.height));
gen(generator, ", ");
gen(generator, get_str(node->var_type.width));
gen(generator, ");");
}
}
// Generates an assignment statement in the target code string.
void generate_assignment_stmt(Generator *generator, ASTNode *node)
{
if (node->rhs->exp_type == EXP_LIST)
{
ASTNode *list = node->rhs;
gen(generator, "assign_to_mat(");
generate_identifier(generator, node->lhs->ident);
gen(generator, ", ");
gen(generator, get_str(list->exp_result_type.height));
gen(generator, ", ");
gen(generator, get_str(list->exp_result_type.width));
gen(generator, ", ");
for (int i = 0; i < list->num_contents - 1; i++)
{
gen(generator, "(double)");
gen(generator, list->contents[i].literal_str);
gen(generator, ", ");
}
gen(generator, "(double)");
gen(generator, list->contents[list->num_contents - 1].literal_str);
gen(generator, ")");
}
else
{
if (node->lhs->exp_type == EXP_IDENT && node->lhs->exp_result_type.var_type != TYPE_SCALAR)
{
gen(generator, "assign_mat_to_mat(");
generate_identifier(generator, node->lhs->ident);
gen(generator, ", ");
generate_expression(generator, node->rhs);
gen(generator, ", ");
gen(generator, get_str(node->lhs->exp_result_type.height));
gen(generator, ", ");
gen(generator, get_str(node->lhs->exp_result_type.width));
gen(generator, ")");
}
else
{
generate_assignment_dest(generator, node->lhs);
gen(generator, " = ");
generate_expression(generator, node->rhs);
}
}
gen(generator, ";");
}
// Generates an assignment destination in the target code string.
// That is either an identifier or an indexing expression.
void generate_assignment_dest(Generator *generator, ASTNode *node)
{
generate_identifier(generator, node->ident);
if (node->exp_type == EXP_INDEX)
{
gen(generator, "[get_int(");
generate_expression(generator, node->contents);
gen(generator, ") - 1]");
if (node->num_contents == 2)
{
gen(generator, "[get_int(");
generate_expression(generator, &node->contents[1]);
gen(generator, ") - 1]");
}
else
{
gen(generator, "[0]");
}
}
}
// Generates a print statement in the target code string.
void generate_print_stmt(Generator *generator, ASTNode *node)
{
if (node->num_contents == 0)
{
gen(generator, "printsep();");
return;
}
ResultType type = node->contents[0].exp_result_type;
switch (type.var_type)
{
case TYPE_SCALAR:
gen(generator, "print(");
break;
case TYPE_VECTOR:
case TYPE_MATRIX:
gen(generator, "print_mat(");
break;
default:
printf("Uncaught error with the parser.");
exit(1);
break;
}
generate_expression(generator, node->contents);
if (type.var_type == TYPE_VECTOR || type.var_type == TYPE_MATRIX)
{
gen(generator, ", ");
gen(generator, get_str(type.height));
gen(generator, ", ");
gen(generator, get_str(type.width));
}
gen(generator, ");");
}
// Generates an expression in the target code string.
// This function is called wherever an expression can appear in the translated programs.
void generate_expression(Generator *generator, ASTNode *node)
{
switch (node->exp_type)
{
case EXP_LITERAL:
gen(generator, node->literal_str);
break;
case EXP_LIST:
gen(generator, "{");
for (int i = 0; i < node->num_contents - 1; i++)
{
gen(generator, (&node->contents[i])->literal_str);
gen(generator, ",");
}
gen(generator, (&node->contents[node->num_contents - 1])->literal_str);
gen(generator, "}");
break;
case EXP_IDENT:
generate_identifier(generator, node->ident);
break;
case EXP_BINOP:
gen(generator, "(");
if (node->op_type == OP_PLUS)
{
if (node->lhs->exp_result_type.var_type == TYPE_MATRIX || node->lhs->exp_result_type.var_type == TYPE_VECTOR)
{
generate_mat_add_sub(generator, node->lhs, node->rhs, "mat_add");
}
else if (node->lhs->exp_result_type.var_type == TYPE_SCALAR)
{
generate_ord_op(generator, node->lhs, node->rhs, "+");
}
}
else if (node->op_type == OP_MINUS)
{
if (node->lhs->exp_result_type.var_type == TYPE_MATRIX || node->lhs->exp_result_type.var_type == TYPE_VECTOR)
{
generate_mat_add_sub(generator, node->lhs, node->rhs, "mat_sub");
}
else if (node->lhs->exp_result_type.var_type == TYPE_SCALAR)
{
generate_ord_op(generator, node->lhs, node->rhs, "-");
}
}
else if (node->op_type == OP_MULT)
{
if (node->lhs->exp_result_type.var_type == TYPE_MATRIX || node->lhs->exp_result_type.var_type == TYPE_VECTOR)
{
if (node->rhs->exp_result_type.var_type == TYPE_MATRIX || node->rhs->exp_result_type.var_type == TYPE_VECTOR)
{
generate_mat_mul(generator, node->lhs, node->rhs);
}
else if (node->rhs->exp_result_type.var_type == TYPE_SCALAR)
{
generate_sca_mul(generator, node->lhs, node->rhs);
}
}
else if (node->lhs->exp_result_type.var_type == TYPE_SCALAR)
{
if (node->rhs->exp_result_type.var_type == TYPE_MATRIX || node->rhs->exp_result_type.var_type == TYPE_VECTOR)
{
generate_sca_mul(generator, node->rhs, node->lhs);
}
else if (node->rhs->exp_result_type.var_type == TYPE_SCALAR)
{
generate_ord_op(generator, node->lhs, node->rhs, "*");
}
}
}
gen(generator, ")");
break;
case EXP_FUNC_CALL:
gen(generator, node->ident);
gen(generator, "(");
for (int i = 0; i < node->num_contents - 1; i++)
{
generate_expression(generator, &node->contents[i]);
gen(generator, ",");
}
generate_expression(generator, &node->contents[node->num_contents - 1]);
if (strcmp(node->ident, "tr") == 0)
{
gen(generator, ", ");
gen(generator, get_str(node->contents->exp_result_type.height));
gen(generator, ", ");
gen(generator, get_str(node->contents->exp_result_type.width));
}
gen(generator, ")");
break;
case EXP_INDEX:
generate_identifier(generator, node->ident);
if (node->num_contents == 1)
{
gen(generator, "[get_int(");
generate_expression(generator, node->contents);
gen(generator, ") - 1][0]");
}
else if (node->num_contents == 2)
{
gen(generator, "[get_int(");
generate_expression(generator, node->contents);
gen(generator, ") - 1][get_int(");
generate_expression(generator, &node->contents[1]);
gen(generator, ") - 1]");
}
break;
}
}
// Generates a for loop in the target code string.
// This function calls the generate_statement function for each substatement in the loop.
void generate_for_loop(Generator *generator, ASTNode *node)
{
gen(generator, "\t");
generate_for_clause(generator, node->for_clause_1);
gen(generator, "\n\t{\n");
if (node->for_clause_2 != NULL)
{
gen(generator, "\t\t");
generate_for_clause(generator, node->for_clause_2);
gen(generator, "\n\t\t{\n");
}
for (int i = 0; i < node->num_contents; i++)
{
gen(generator, "\t\t");
if (node->for_clause_2 != NULL)
{
gen(generator, "\t");
}
generate_statement(generator, &node->contents[i]);
gen(generator, "\n");
}
if (node->for_clause_2 != NULL)
{
gen(generator, "\t\t}\n");
}
gen(generator, "\t}\n");
}
// Generates a for clause in the target code string.
// This function is called in the generate_for_loop function.
void generate_for_clause(Generator *generator, ForLoopClause *for_clause)
{
char *var_name = for_clause->var->name;
gen(generator, "for (");
generate_identifier(generator, var_name);
gen(generator, " = ");
generate_expression(generator, for_clause->expr1);
gen(generator, "; ");
gen(generator, "(");
generate_identifier(generator, var_name);
gen(generator, ") - (");
generate_expression(generator, for_clause->expr2);
gen(generator, ") <= +EPSILON; ");
generate_identifier(generator, var_name);
gen(generator, " += ");
generate_expression(generator, for_clause->expr3);
gen(generator, ")");
}
// Generates a matrix addition or subtraction in the target code string.
void generate_mat_add_sub(Generator *generator, ASTNode *mat1, ASTNode *mat2, char *func_name)
{
gen(generator, func_name);
gen(generator, "(");
generate_expression(generator, mat1);
gen(generator, ", ");
generate_expression(generator, mat2);
gen(generator, ", ");
gen(generator, get_str(mat1->exp_result_type.height));
gen(generator, ", ");
gen(generator, get_str(mat1->exp_result_type.width));
gen(generator, ")");
}
// Generates an ordinary (scalar) addition, subtraction or multiplication in the target code string.
void generate_ord_op(Generator *generator, ASTNode *sca1, ASTNode *sca2, char *chr)
{
generate_expression(generator, sca1);
gen(generator, chr);
generate_expression(generator, sca2);
}
// Generates a matrix multiplication in the target code string.
void generate_mat_mul(Generator *generator, ASTNode *mat1, ASTNode *mat2)
{
gen(generator, "mat_mul(");
generate_expression(generator, mat1);
gen(generator, ", ");
gen(generator, get_str(mat1->exp_result_type.height));
gen(generator, ", ");
gen(generator, get_str(mat1->exp_result_type.width));
gen(generator, ", ");
generate_expression(generator, mat2);
gen(generator, ", ");
gen(generator, get_str(mat2->exp_result_type.height));
gen(generator, ", ");
gen(generator, get_str(mat2->exp_result_type.width));
gen(generator, ")");
}
// Generates a scalar multiplication (scalar * matrix or matrix * scalar) in the target code string.
void generate_sca_mul(Generator *generator, ASTNode *mat, ASTNode *sca)
{
gen(generator, "mat_sca_mul(");
generate_expression(generator, mat);
gen(generator, ", ");
gen(generator, get_str(mat->exp_result_type.height));
gen(generator, ", ");
gen(generator, get_str(mat->exp_result_type.width));
gen(generator, ", ");
generate_expression(generator, sca);
gen(generator, ")");
}
// The main function that starts generating the target code string.
void generate_code_string(Generator *generator)
{
generate_new_code_string(generator);
ASTNode *root = generator->tree->root;
for (int i = 0; i < root->num_contents; i++)
{
switch (root->contents[i].type)
{
case AST_STMT:
gen(generator, "\t");
generate_statement(generator, &root->contents[i]);
gen(generator, "\n");
continue;
case AST_FOR_LOOP:
generate_for_loop(generator, &root->contents[i]);
continue;
default:
printf("Uncaught error with the parser.\n");
exit(1);
}
}
gen(generator, "}\n");
}