-
Notifications
You must be signed in to change notification settings - Fork 1
/
codegen_functions.h
350 lines (236 loc) · 9.92 KB
/
codegen_functions.h
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
#ifndef CODEGEN_FUNCTIONS_H
#define CODEGEN_FUNCTIONS_H
#include <stdbool.h>
#include "token_struct.h"
#include "utils.h"
/* ------------------------------------------------------------- */
/* Code Generator Constants */
/* ------------------------------------------------------------- */
/* Register Types */
/* Register Type GP (Global Pointer) */
#define REGISTER_TYPE_GP 1
/* Register Type SP (Stack Pointer) */
#define REGISTER_TYPE_SP 2
/* Register Type FP (Frame Pointer) */
#define REGISTER_TYPE_FP 3
/* Function Prefix */
#define FUNCTION_PREFIX "function_"
/* Global reserved variables Prefix */
#define GLOBAL_SYSTEM_VARIABLE_PREFIX "_"
/* Global variables Prefix */
#define GLOBAL_VARIABLE_PREFIX "__"
/* Value of a nil variable */
#define NIL_TYPE_MASK "0x7FFFFFFD"
#define NIL_TYPE_VALUE "0x80000002"
/* Logical type */
#define VOID_TYPE 0
/* Integer type */
#define NUMBER_TYPE 1
/* Standard way to add itens to main queue */
#define addInstructionMainQueue(VAR) instructionQueueEnqueueInstruction( main_instruction_queue, \
formatedInstruction(VAR), false)
/* New array of instructions with variadic parameters */
#define addInstructionMainQueueFormated(VAR, ...) instructionQueueEnqueueInstruction( main_instruction_queue, \
formatedInstruction(VAR, ##__VA_ARGS__), \
false)
/* ------------------------------------------------------------- */
/* Global Structures */
/* ------------------------------------------------------------- */
/* Generated Code */
extern InstructionQueue *header_instruction_queue;
/* Main Generated Code */
extern InstructionQueue *main_instruction_queue;
/* Global Symbol Table */
extern SymbolTable *global_symbol_table;
/* ------------------------------------------------------------- */
/* Code Generator Functions */
/* ------------------------------------------------------------- */
/* Generate all code */
bool cgenAllCode(TokenNode *root_token);
/* Copy global variable definitions */
bool copyGlobalVariables();
/* Generate blocks of code */
bool cgenBlockCode(TokenNode *block_token, SymbolTable *previous_scope);
/* Generate code for command */
bool cgenCommand(TokenNode *command_token, SymbolTable *actual_symbol_table);
/* Generate code for command list */
bool cgenCommandList(TokenNode *command_list_token, SymbolTable *actual_symbol_table);
/* Generate code for command return */
bool cgenComandReturn(TokenNode *comand_return_token, SymbolTable *actual_symbol_table);
/* Generate code for function call */
bool cgenCallFunction(TokenNode *call_function_token, SymbolTable *actual_symbol_table);
/* Generate code for assign */
bool cgenAssign(TokenNode *assign_token, SymbolTable *actual_symbol_table);
/* Generate code for command block */
bool cgenCommandBlock(TokenNode *command_block_token, SymbolTable *actual_symbol_table);
/* Generate code for while */
bool cgenWhile(TokenNode *while_token, SymbolTable *actual_symbol_table);
/* Generate code for for */
bool cgenFor(TokenNode *for_token, SymbolTable *actual_symbol_table);
/* Generate code for if */
bool cgenIf(TokenNode *if_token, SymbolTable *actual_symbol_table);
/* Generate code for function definition call */
bool cgenFunction(TokenNode *function_def_token, SymbolTable *actual_symbol_table);
/* Generate code for local variable operation */
bool cgenLocalVariable(TokenNode *local_variable_token, SymbolTable *actual_symbol_table);
/* Generate code for expressions. */
bool cgenExpression(TokenNode *exp_token, SymbolTable *symbol_table);
/* Generate code for list expression */
int cgenExpressionList(TokenNode *list_exp_token, SymbolTable *symbol_table);
/* Generate code for name list */
bool cgenNameList(TokenNode *list_exp_token, SymbolTable *symbol_table);
/* ------------------------------------------------------------- */
/* Code Generator Models */
/* ------------------------------------------------------------- */
/* Generated code Header */
extern const char mips_header[];
/* System Functions */
extern const char mips_main[];
/* Close main */
extern const char mips_footer[];
/* Define a global variable */
extern const char mips_global_define[];
/* Define a local variable */
extern const char mips_local_define[];
/* Store a global variable */
extern const char mips_global_store[];
/* Load a global variable into $a0 */
extern const char mips_global_load[];
/* Store a local variable */
extern const char mips_local_store[];
/* Load a local variable into $a0 */
extern const char mips_local_load[];
/* Push temporary return of a expression */
extern const char mips_push_a0[];
/* Pop stack value */
extern const char mips_pop[];
/* Pop stack params */
extern const char mips_pop_params[];
/* Pop local variables on stack */
extern const char mips_pop_local[];
/* Load top value to $t1 */
extern const char mips_top_t1[];
/* Load top value to $a0 */
extern const char mips_top_a0[];
/* Load a static number into $a0 */
extern const char mips_static_number_load[];
/* Move $a0 to $t1 */
extern const char mips_move_a0_t1[];
/* Move from $a0 to $t4 */
extern const char mips_move_a0_t4[];
/* Move from $a0 to $a2 */
extern const char mips_move_a0_a2[];
/* Not a number in $a0 */
extern const char mips_not_a0[];
/* Convert $a0 to a negative number */
extern const char mips_neg_a0[];
/* Add value of $t1 with $a0 and store in $a0 */
extern const char mips_add_a0_t1_a0[];
/* Sub value of $t1 with $a0 and store in $a0 */
extern const char mips_sub_a0_t1_a0[];
/* Div value of $t1 with $a0 and store in $a0 */
extern const char mips_div_a0_t1_a0[];
/* Mul value of $t1 with $a0 and store in $a0 */
extern const char mips_mul_a0_t1_a0[];
/* And the value of $t1 with $a0 and store in $a0 */
extern const char mips_and_a0_t1_a0[];
/* Or the value of $t1 with $a0 and store in $a0 */
extern const char mips_or_a0_t1_a0[];
/* Check if is greater than, the value of $t1 with $a0 and store in $a0 */
extern const char mips_gt_a0_t1_a0[];
/* Check if is less than, the value of $t1 with $a0 and store in $a0 */
extern const char mips_lt_a0_t1_a0[];
/* Check if $a0 is equal $t1 */
extern const char mips_eq_a0_t1_a0[];
/* Check if $a0 is not equal $t1 */
extern const char mips_neq_a0_t1_a0[];
/* Check if $a0 is greater or equal $t1 */
extern const char mips_gte_a0_t1_a0[];
/* Check if $a0 is less or equal $t1 */
extern const char mips_lte_a0_t1_a0[];
/* Conditional type If-elseif-else */
extern const char mips_start_if[];
/* Check if condition */
extern const char mips_check_if[];
/* Check the next if condition */
extern const char mips_next_if[];
/* If end condition */
extern const char mips_end_if[];
/* Header for else if */
extern const char mips_elseif_start[];
/* Header for else */
extern const char mips_else_start[];
/* Loop type while */
extern const char mips_start_while[];
/* While loop check while condition */
extern const char mips_check_while[];
/* While end loop */
extern const char mips_end_while[];
/* Loop type for */
extern const char mips_for_ini[];
/* Store for interval type */
extern const char mips_for_interval[];
/* Begin of for */
extern const char mips_start_for[];
/* For condition check */
extern const char mips_for_check[];
/* Store */
extern const char mips_for_load_inc[];
/* Mips default for inc */
extern const char mips_for_inc[];
/* End of for defition */
extern const char mips_end_for[];
/* Function Call start model */
extern const char mips_start_function_call[];
/* Function Call end model */
extern const char mips_end_function_call[];
/* Start of function definition */
extern const char mips_start_function_def[];
/* Continuation of function definition */
extern const char mips_start_function_def2[];
/* Empty return */
extern const char mips_end_of_function[];
/* End of function definition with nil return */
extern const char mips_end_function_def[];
/* End of function definition without nil return */
extern const char mips_end_function_defX[];
/* End of function definition */
extern const char mips_end_function_def2[];
/* Nil variable value */
extern const char mips_nil[];
/* Check if a given variable is nil */
extern const char mips_check_a0_nil[];
/* Begin of the and check */
extern const char mips_and_sc_header[];
/* Skip the next evaluation if the first has failed */
extern const char mips_and_sc_skip[];
/* End of mips sc */
extern const char mips_and_sc_footer[];
/* Begin of the and check */
extern const char mips_or_sc_header[];
/* Skip the next evaluation if the first has failed */
extern const char mips_or_sc_skip[];
/* End of mips sc */
extern const char mips_or_sc_footer[];
/* Begin of a assign */
extern const char mips_start_assign[];
/* Expression Execution marker */
extern const char mips_marker_exp[];
/* Assign marker */
extern const char mips_marker_assign[];
/* End of a assign */
extern const char mips_end_assign[];
/* Begin of a local assign */
extern const char mips_start_local_assign[];
/* Assign local assign marker */
extern const char mips_marker_local_assign[];
/* End of a local assign */
extern const char mips_end_local_assign[];
/* Begin of the return values model */
extern const char mips_start_return[];
/* Multiple returns stack rearange */
extern const char mips_return_multiple[];
/* End of a return */
extern const char mips_end_return[];
/* ------------------------------------------------------------- */
#endif