-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathcode_generator.h
304 lines (262 loc) · 7.96 KB
/
code_generator.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
/**
* Project: Implementace překladače imperativního jazyka IFJ17.
*
* @brief Code generator interface.
*
* @author Dominik Harmim <[email protected]>
*/
#ifndef _CODE_GENERATOR_H
#define _CODE_GENERATOR_H
#include <stdio.h>
#include <stdbool.h>
#include "symtable.h"
#include "scanner.h"
#include "expression.h"
/**
* Inicialization of code generator.
* Define built-in functions, etc.
*
* @return True if it was successful, false otherwise.
*/
bool code_generator_start();
/**
* Clear resources.
*/
void code_generator_clear();
/**
* Flush generated code to destination file.
*
* @param destination_file Pointer to destination file.
*/
void code_generator_flush(FILE *destination_file);
/**
* Generates start of main scope.
*
* @return True if it was successful, false otherwise.
*/
bool generate_main_scope_start();
/**
* Generates end of main scope.
*
* @return True if it was successful, false otherwise.
*/
bool generate_main_scope_end();
/**
* Generates start of function.
*
* @param function_id Function identifier.
* @return True if it was successful, false otherwise.
*/
bool generate_function_start(char *function_id);
/**
* Generates end of function.
*
* @param function_id Function identifier.
* @return True if it was successful, false otherwise.
*/
bool generate_function_end(char *function_id);
/**
* Generates declaration and definition of function return value.
*
* @param type Data type of function return value.
* @return True if it was successful, false otherwise.
*/
bool generate_function_retval(Data_type type);
/**
* Generates function call (jump to the function).
*
* @param function_id Function identifier.
* @return True if it was successful, false otherwise.
*/
bool generate_function_call(char *function_id);
/**
* Generates assignment function return value of L-variable.
*
* @param l_val_id Identifier of L-variable.
* @param l_type L-variable data type.
* @param ret_type Data type of function return value.
* @return True if it was successful, false otherwise.
*/
bool generate_function_retval_assign(char *l_val_id, Data_type l_type, Data_type ret_type);
/**
* Generates local variable for function parameter.
*
* @param param_id Parameter identifier.
* @param index Parameter index (position).
* @return True if it was successful, false otherwise.
*/
bool generate_function_param_declare(char *param_id, int index);
/**
* Generates code for preparation for pass parameters to function.
*
* @return True if it was successful, false otherwise.
*/
bool generate_function_before_pass_params();
/**
* Generates passing parameter to function.
*
* @param token Token with passed parameter value.
* @param index Parameter index (position).
* @return True if it was successful, false otherwise.
*/
bool generate_function_pass_param(Token token, int index);
/**
* Generates convert passed parameter data type.
*
* @param from Data type to convert from.
* @param to Data type to conver to.
* @param index Parameter index (position).
* @return True if it was successful, false otherwise.
*/
bool generate_function_convert_passed_param(Data_type from, Data_type to, int index);
/**
* Generates return statement in function.
*
* @param function_id Function identifier.
* @return True if it was successful, false otherwise.
*/
bool generate_function_return(char *function_id);
/**
* Generates variable declaration.
*
* @param var_id Variable identifier.
* @return True if it was successful, false otherwise.
*/
bool generate_var_declare(char *var_id);
/**
* Generates assignment default value to variable.
*
* @param var_id Variable identifier.
* @param type Data type of variable.
* @return True if it was successful, false otherwise.
*/
bool generate_var_default_value(char *var_id, Data_type type);
/**
* Generates input statement.
*
* @param var_id Identifier of variable read value.
* @param type Data type of read value.
* @return True if it was successful, false otherwise.
*/
bool generate_input(char *var_id, Data_type type);
/**
* Generates print of expression result.
*
* @return True if it was successful, false otherwise.
*/
bool generate_print();
/**
* Generates push value to data stack.
*
* @param token Token with value to be pushed.
* @return True if it was successful, false otherwise.
*/
bool generate_push(Token token);
/**
* Generates operation with top data stack items.
*
* @param rule Expression rule.
* @return True if it was successful, false otherwise.
*/
bool generate_stack_operation(Prec_rules_enum rule);
/**
* Generates concatition of top data stack items.
*
* @return True if it was successful, false otherwise.
*/
bool generate_concat_stack_strings();
/**
* Generates save expression result from data stack.
*
* @param var_id Variable identifier for expression result.
* @param ret_type Data type of expression in data stack.
* @param l_type Data type of variable for save result.
* @param frame Variable frame of given variable.
* @return True if it was successful, false otherwise.
*/
bool generate_save_expression_result(char *var_id, Data_type ret_type, Data_type l_type, char *frame);
/**
* Generates convert data stack top item to double.
*
* @return True if it was successful, false otherwise.
*/
bool generate_stack_op1_to_double();
/**
* Generates convert data stack top item to integer.
*
* @return True if it was successful, false otherwise.
*/
bool generate_stack_op1_to_integer();
/**
* Generates convert data stack top-1 item to double.
*
* @return True if it was successful, false otherwise.
*/
bool generate_stack_op2_to_double();
/**
* Generates convert data stack top-1 item to integer.
*
* @return True if it was successful, false otherwise.
*/
bool generate_stack_op2_to_integer();
/**
* Generates If-Then head (before processing If-Then expression).
*
* @return True if it was successful, false otherwise.
*/
bool generate_if_head();
/**
* Generates If-Then start (after processing If-Then expression).
*
* @param function_id Identifier of function in which is this statement.
* @param label_index Index of label.
* @param label_deep Deep of label.
* @return True if it was successful, false otherwise.
*/
bool generate_if_start(char *function_id, int label_index, int label_deep);
/**
* Generates If-Then Else part (after processing If-Then statement).
*
* @param function_id Identifier of function in which is this statement.
* @param label_index Index of label.
* @param label_deep Deep of label.
* @return True if it was successful, false otherwise.
*/
bool generate_if_else_part(char *function_id, int label_index, int label_deep);
/**
* Generates If-Then end (after processing If-Then Else statement).
*
* @param function_id Identifier of function in which is this statement.
* @param label_index Index of label.
* @param label_deep Deep of label.
* @return True if it was successful, false otherwise.
*/
bool generate_if_end(char *function_id, int label_index, int label_deep);
/**
* Generates Do-While head (before processing expression).
*
* @param function_id Identifier of function in which is this statement.
* @param label_index Index of label.
* @param label_deep Deep of label.
* @return True if it was successful, false otherwise.
*/
bool generate_while_head(char *function_id, int label_index, int label_deep);
/**
* Generates Do-While head (after processing expression).
*
* @param function_id Identifier of function in which is this statement.
* @param label_index Index of label.
* @param label_deep Deep of label.
* @return True if it was successful, false otherwise.
*/
bool generate_while_start(char *function_id, int label_index, int label_deep);
/**
* Generates Do-While head (after processing statement).
*
* @param function_id Identifier of function in which is this statement.
* @param label_index Index of label.
* @param label_deep Deep of label.
* @return True if it was successful, false otherwise.
*/
bool generate_while_end(char *function_id, int label_index, int label_deep);
#endif //_CODE_GENERATOR_H