|
1 /* |
|
2 * (c) 2003 Mario de Sousa |
|
3 * |
|
4 * Offered to the public under the terms of the GNU General Public License |
|
5 * as published by the Free Software Foundation; either version 2 of the |
|
6 * License, or (at your option) any later version. |
|
7 * |
|
8 * This program is distributed in the hope that it will be useful, but |
|
9 * WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General |
|
11 * Public License for more details. |
|
12 * |
|
13 * This code is made available on the understanding that it will not be |
|
14 * used in safety-critical situations without a full and competent review. |
|
15 */ |
|
16 |
|
17 /* |
|
18 * An IEC 61131-3 IL and ST compiler. |
|
19 * |
|
20 * Based on the |
|
21 * FINAL DRAFT - IEC 61131-3, 2nd Ed. (2001-12-10) |
|
22 * |
|
23 */ |
|
24 |
|
25 |
|
26 /* |
|
27 * Conversion of il statements (i.e. IL code). |
|
28 * |
|
29 * This is part of the 4th stage that generates |
|
30 * a c++ source program equivalent to the IL and ST |
|
31 * code. |
|
32 */ |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 /***********************************************************************/ |
|
38 /***********************************************************************/ |
|
39 /***********************************************************************/ |
|
40 /***********************************************************************/ |
|
41 |
|
42 /* Returns the data type of an il_operand. |
|
43 * |
|
44 * Note that the il_operand may be a variable, in which case |
|
45 * we return the type of the variable instance. |
|
46 * The il_operand may also be a constant, in which case |
|
47 * we return the data type of that constant. |
|
48 * |
|
49 * The variable instance may be a member of a structured variable, |
|
50 * or an element in an array, or any combination of the two. |
|
51 * |
|
52 * The class constructor must be given the search scope |
|
53 * (function, function block or program within which |
|
54 * the possible il_operand variable instance was declared). |
|
55 */ |
|
56 class search_il_operand_type_c { |
|
57 |
|
58 private: |
|
59 search_varfb_instance_type_c search_varfb_instance_type; |
|
60 search_constant_type_c search_constant_type; |
|
61 |
|
62 public: |
|
63 search_il_operand_type_c(symbol_c *search_scope): search_varfb_instance_type(search_scope) {} |
|
64 |
|
65 public: |
|
66 symbol_c *get_type(symbol_c *il_operand) { |
|
67 symbol_c *res; |
|
68 |
|
69 /* We first assume that it is a constant... */ |
|
70 res = search_constant_type.get_type(il_operand); |
|
71 if (res != NULL) return res; |
|
72 |
|
73 /* Nope, now we assume it is a variable, and determine its type... */ |
|
74 res = search_varfb_instance_type.get_type(il_operand); |
|
75 if (NULL != res) return res; |
|
76 |
|
77 /* not found */ |
|
78 return NULL; |
|
79 } |
|
80 }; |
|
81 |
|
82 |
|
83 |
|
84 /***********************************************************************/ |
|
85 /***********************************************************************/ |
|
86 /***********************************************************************/ |
|
87 /***********************************************************************/ |
|
88 |
|
89 |
|
90 /* A new class to ouput the il default variable to c++ code |
|
91 * We use this class, inheriting from symbol_c, so it may be used |
|
92 * as any other symbol_c object in the intermediate parse tree, |
|
93 * more specifically, so it can be used as any other il operand. |
|
94 * This makes the rest of the code much easier... |
|
95 * |
|
96 * Nevertheless, the basic visitor class visitor_c does not know |
|
97 * how to visit this new il_default_variable_c class, so we have |
|
98 * to extend that too. |
|
99 * In reality extending the basic symbols doesn't quite work out |
|
100 * as cleanly as desired (we need to use dynamic_cast in the |
|
101 * accept method of the il_default_variable_c), but it is cleaner |
|
102 * than the alternative... |
|
103 */ |
|
104 class il_default_variable_c; |
|
105 |
|
106 /* This visitor class is not really required, we could place the |
|
107 * visit() method directly in genertae_cc_il_c, but doing it in |
|
108 * a seperate class makes the architecture more evident... |
|
109 */ |
|
110 class il_default_variable_visitor_c { |
|
111 public: |
|
112 virtual void *visit(il_default_variable_c *symbol) = 0; |
|
113 |
|
114 virtual ~il_default_variable_visitor_c(void) {return;} |
|
115 }; |
|
116 |
|
117 |
|
118 /* A class to print out to the resulting C++ code |
|
119 * the IL default variable name. |
|
120 * |
|
121 * It includes a reference to its name, |
|
122 * and the data type of the data currently stored |
|
123 * in this C++ variable... This is required because the |
|
124 * C++ variable is a union, and we must know which member |
|
125 * of the union top reference!! |
|
126 * |
|
127 * Note that we also need to keep track of the data type of |
|
128 * the value currently being stored in the default variable. |
|
129 * This is required so we can process parenthesis, |
|
130 * |
|
131 * e.g. : |
|
132 * LD var1 |
|
133 * AND ( |
|
134 * LD var2 |
|
135 * OR var3 |
|
136 * ) |
|
137 * |
|
138 * Note that we only execute the 'AND (' operation when we come across |
|
139 * the ')', i.e. once we have evaluated the result of the |
|
140 * instructions inside the parenthesis. |
|
141 * When we do execute the 'AND (' operation, we need to know the data type |
|
142 * of the operand, which in this case is the result of the evaluation of the |
|
143 * instruction list inside the parenthesis. We can only know this if we |
|
144 * keep track of the data type currently stored in the default variable! |
|
145 * |
|
146 * We use the current_type inside the generate_cc_il::default_variable_name variable |
|
147 * to track this! |
|
148 */ |
|
149 class il_default_variable_c: public symbol_c { |
|
150 public: |
|
151 symbol_c *var_name; /* in principle, this should point to an indentifier_c */ |
|
152 symbol_c *current_type; |
|
153 |
|
154 public: |
|
155 il_default_variable_c(const char *var_name_str, symbol_c *current_type); |
|
156 virtual void *accept(visitor_c &visitor); |
|
157 }; |
|
158 |
|
159 |
|
160 /***********************************************************************/ |
|
161 /***********************************************************************/ |
|
162 /***********************************************************************/ |
|
163 /***********************************************************************/ |
|
164 |
|
165 |
|
166 |
|
167 class generate_cc_il_c: public generate_cc_typedecl_c, il_default_variable_visitor_c { |
|
168 |
|
169 private: |
|
170 /* When compiling il code, it becomes necessary to determine the |
|
171 * data type of il operands. To do this, we must first find the |
|
172 * il operand's declaration, within the scope of the function block |
|
173 * or function currently being processed. |
|
174 * The following object does just that... |
|
175 * This object instance will then later be called while the |
|
176 * remaining il code is being handled. |
|
177 */ |
|
178 search_il_operand_type_c *search_il_operand_type; |
|
179 |
|
180 /* The initial value that should be given to the IL default variable |
|
181 * imediately after a parenthesis is opened. |
|
182 * This variable is only used to pass data from the |
|
183 * il_expression_c visitor to the simple_instr_list_c visitor. |
|
184 * |
|
185 * e.g.: |
|
186 * LD var1 |
|
187 * AND ( var2 |
|
188 * OR var3 |
|
189 * ) |
|
190 * |
|
191 * In the above code sample, the line 'AND ( var2' constitutes |
|
192 * an il_expression_c, where var2 should be loaded into the |
|
193 * il default variable before continuing with the expression |
|
194 * inside the parenthesis. |
|
195 * Unfortunately, only the simple_instr_list_c may do the |
|
196 * initial laoding of the var2 bariable following the parenthesis, |
|
197 * so the il_expression_c visitor will have to pass 'var2' as a |
|
198 * parameter to the simple_instr_list_c visitor. |
|
199 * Ergo, the existance of the following parameter...! |
|
200 */ |
|
201 symbol_c *il_default_variable_init_value; |
|
202 |
|
203 /* Operand to the IL operation currently being processed... */ |
|
204 /* These variables are used to pass data from the |
|
205 * il_simple_operation_c and il_expression_c visitors |
|
206 * to the il operator visitors (i.e. LD_operator_c, |
|
207 * LDN_operator_c, ST_operator_c, STN_operator_c, ...) |
|
208 */ |
|
209 symbol_c *current_operand; |
|
210 symbol_c *current_operand_type; |
|
211 |
|
212 /* Label to which the current IL jump operation should jump to... */ |
|
213 /* This variable is used to pass data from the |
|
214 * il_jump_operation_c visitor |
|
215 * to the il jump operator visitors (i.e. JMP_operator_c, |
|
216 * JMPC_operator_c, JMPCN_operator_c, ...) |
|
217 */ |
|
218 symbol_c *jump_label; |
|
219 |
|
220 /* The result of the comparison IL operations (GT, EQ, LT, ...) |
|
221 * is a boolean variable. |
|
222 * This class keeps track of the current data type stored in the |
|
223 * il default variable. This is usually done by keeping a reference |
|
224 * to the data type of the last operand. Nevertheless, in the case of |
|
225 * the comparison IL operators, the data type of the result (a boolean) |
|
226 * is not the data type of the operand. We therefore need an object |
|
227 * of the boolean data type to keep as a reference of the current |
|
228 * data type. |
|
229 * The following object is it... |
|
230 */ |
|
231 bool_type_name_c bool_type; |
|
232 |
|
233 /* the data type of the IL default variable... */ |
|
234 #define IL_DEFVAR_T VAR_LEADER "IL_DEFVAR_T" |
|
235 /* The name of the IL default variable... */ |
|
236 #define IL_DEFVAR VAR_LEADER "IL_DEFVAR" |
|
237 /* The name of the variable used to pass the result of a |
|
238 * parenthesised instruction list to the immediately preceding |
|
239 * scope ... |
|
240 */ |
|
241 #define IL_DEFVAR_BACK VAR_LEADER "IL_DEFVAR_BACK" |
|
242 il_default_variable_c default_variable_name; |
|
243 il_default_variable_c default_variable_back_name; |
|
244 |
|
245 /* Some function calls in the body of functions or function blocks |
|
246 * may leave some parameters to their default values, and |
|
247 * ignore some output parameters of the function being called. |
|
248 * Our conversion of ST functions to C++ does not contemplate that, |
|
249 * i.e. each called function must get all it's input and output |
|
250 * parameters set correctly. |
|
251 * For input parameters we merely need to call the function with |
|
252 * the apropriate default value, but for output parameters |
|
253 * we must create temporary variables to hold the output value. |
|
254 * |
|
255 * We declare all the temporary output variables at the begining of |
|
256 * the body of each function or function block, and use them as |
|
257 * in function calls later on as they become necessary... |
|
258 * Note that we cannot create these variables just before a function |
|
259 * call, as the function call itself may be integrated within an |
|
260 * expression, or another function call! |
|
261 * |
|
262 * The variables are declared in the exact same order in which they |
|
263 * will be used later on during the function calls, which allows us |
|
264 * to simply re-create the name that was used for the temporary variable |
|
265 * instead of keeping it in some list. |
|
266 * The names are recreated by the temp_var_name_factory, after reset() |
|
267 * has been called! |
|
268 * |
|
269 * This function will genertae code similar to... |
|
270 * |
|
271 * INT __TMP_0 = 23; |
|
272 * REAL __TMP_1 = 45.5; |
|
273 * ... |
|
274 */ |
|
275 temp_var_name_c temp_var_name_factory; |
|
276 |
|
277 /* When calling a function block, we must first find it's type, |
|
278 * by searching through the declarations of the variables currently |
|
279 * in scope. |
|
280 * This class does just that... |
|
281 * A new class is instantiated whenever we begin generating the code |
|
282 * for a function block type declaration, or a program declaration. |
|
283 * This object instance will then later be called while the |
|
284 * function block's or the program's body is being handled. |
|
285 * |
|
286 * Note that functions cannot contain calls to function blocks, |
|
287 * so we do not create an object instance when handling |
|
288 * a function declaration. |
|
289 */ |
|
290 search_fb_instance_decl_c *search_fb_instance_decl; |
|
291 |
|
292 |
|
293 public: |
|
294 generate_cc_il_c(stage4out_c *s4o_ptr, symbol_c *scope, const char *variable_prefix = NULL) |
|
295 : generate_cc_typedecl_c(s4o_ptr), |
|
296 default_variable_name(IL_DEFVAR, NULL), |
|
297 default_variable_back_name(IL_DEFVAR_BACK, NULL) |
|
298 { |
|
299 search_il_operand_type = new search_il_operand_type_c(scope); |
|
300 search_fb_instance_decl = new search_fb_instance_decl_c(scope); |
|
301 current_operand = NULL; |
|
302 current_operand_type = NULL; |
|
303 il_default_variable_init_value = NULL; |
|
304 this->set_variable_prefix(variable_prefix); |
|
305 } |
|
306 |
|
307 virtual ~generate_cc_il_c(void) { |
|
308 delete search_fb_instance_decl; |
|
309 delete search_il_operand_type; |
|
310 } |
|
311 |
|
312 void generate(instruction_list_c *il) { |
|
313 generate_cc_tempvardecl_c generate_cc_tempvardecl(&s4o); |
|
314 generate_cc_tempvardecl.generate(il, &temp_var_name_factory); |
|
315 il->accept(*this); |
|
316 } |
|
317 |
|
318 private: |
|
319 /* A helper function... */ |
|
320 bool is_bool_type(symbol_c *type_symbol) { |
|
321 return (NULL != dynamic_cast<bool_type_name_c *>(type_symbol)); |
|
322 } |
|
323 |
|
324 /* A helper function... */ |
|
325 void *XXX_operator(symbol_c *lo, const char *op, symbol_c *ro) { |
|
326 if ((NULL == lo) || (NULL == ro)) ERROR; |
|
327 if (NULL == op) ERROR; |
|
328 |
|
329 lo->accept(*this); |
|
330 s4o.print(op); |
|
331 ro->accept(*this); |
|
332 return NULL; |
|
333 } |
|
334 |
|
335 /* A helper function... */ |
|
336 void *XXX_CAL_operator(const char *param_name, symbol_c *fb_name) { |
|
337 if (NULL == fb_name) ERROR; |
|
338 symbolic_variable_c *sv = dynamic_cast<symbolic_variable_c *>(fb_name); |
|
339 if (NULL == sv) ERROR; |
|
340 identifier_c *id = dynamic_cast<identifier_c *>(sv->var_name); |
|
341 if (NULL == id) ERROR; |
|
342 |
|
343 identifier_c param(param_name); |
|
344 |
|
345 //SYM_REF4(il_param_assignment_c, il_assign_operator, il_operand, simple_instr_list, unused) |
|
346 il_param_assignment_c il_param_assignment(¶m, &this->default_variable_name, NULL, NULL); |
|
347 // SYM_LIST(il_param_list_c) |
|
348 il_param_list_c il_param_list; |
|
349 il_param_list.add_element(&il_param_assignment); |
|
350 CAL_operator_c CAL_operator; |
|
351 // SYM_REF4(il_fb_call_c, il_call_operator, fb_name, il_operand_list, il_param_list) |
|
352 il_fb_call_c il_fb_call(&CAL_operator, id, NULL, &il_param_list); |
|
353 |
|
354 il_fb_call.accept(*this); |
|
355 return NULL; |
|
356 } |
|
357 |
|
358 /* A helper function... */ |
|
359 void *CMP_operator(symbol_c *o, const char *operation) { |
|
360 if (NULL == o) ERROR; |
|
361 if (NULL == this->default_variable_name.current_type) ERROR; |
|
362 |
|
363 symbol_c *backup = this->default_variable_name.current_type; |
|
364 this->default_variable_name.current_type = &(this->bool_type); |
|
365 this->default_variable_name.accept(*this); |
|
366 this->default_variable_name.current_type = backup; |
|
367 |
|
368 s4o.print(" = ("); |
|
369 this->default_variable_name.accept(*this); |
|
370 s4o.print(operation); |
|
371 o->accept(*this); |
|
372 s4o.print(")"); |
|
373 |
|
374 /* the data type resulting from this operation... */ |
|
375 this->default_variable_name.current_type = &(this->bool_type); |
|
376 return NULL; |
|
377 } |
|
378 |
|
379 |
|
380 /* A helper function... */ |
|
381 void C_modifier(void) { |
|
382 if (NULL == this->default_variable_name.current_type) ERROR; |
|
383 |
|
384 s4o.print("if ("); |
|
385 this->default_variable_name.accept(*this); |
|
386 s4o.print(") "); |
|
387 } |
|
388 |
|
389 /* A helper function... */ |
|
390 void CN_modifier(void) { |
|
391 if (NULL == this->default_variable_name.current_type) ERROR; |
|
392 |
|
393 s4o.print("if (!"); |
|
394 this->default_variable_name.accept(*this); |
|
395 s4o.print(") "); |
|
396 } |
|
397 |
|
398 |
|
399 public: |
|
400 void *visit(il_default_variable_c *symbol) { |
|
401 //s4o.print("il_default_variable_c VISITOR!!\n"); |
|
402 symbol->var_name->accept(*this); |
|
403 if (NULL != symbol->current_type) { |
|
404 s4o.print("."); |
|
405 symbol->current_type->accept(*this); |
|
406 s4o.print("var"); |
|
407 } |
|
408 return NULL; |
|
409 } |
|
410 |
|
411 |
|
412 private: |
|
413 /****************************************/ |
|
414 /* B.2 - Language IL (Instruction List) */ |
|
415 /****************************************/ |
|
416 |
|
417 /***********************************/ |
|
418 /* B 2.1 Instructions and Operands */ |
|
419 /***********************************/ |
|
420 |
|
421 /* please see the comment before the RET_operator_c visitor for details... */ |
|
422 #define END_LABEL VAR_LEADER "end" |
|
423 |
|
424 /*| instruction_list il_instruction */ |
|
425 void *visit(instruction_list_c *symbol) { |
|
426 /* Declare the backup to the default variable, that will store the result |
|
427 * of the IL operations executed inside a parenthesis... |
|
428 */ |
|
429 s4o.print(s4o.indent_spaces); |
|
430 s4o.print(IL_DEFVAR_T); |
|
431 s4o.print(" "); |
|
432 this->default_variable_back_name.accept(*this); |
|
433 s4o.print(";\n"); |
|
434 |
|
435 /* Declare the default variable, that will store the result of the IL operations... */ |
|
436 s4o.print(s4o.indent_spaces); |
|
437 s4o.print(IL_DEFVAR_T); |
|
438 s4o.print(" "); |
|
439 this->default_variable_name.accept(*this); |
|
440 s4o.print(";\n\n"); |
|
441 |
|
442 print_list(symbol, s4o.indent_spaces, ";\n" + s4o.indent_spaces, ";\n"); |
|
443 |
|
444 /* write the label marking the end of the code block */ |
|
445 /* please see the comment before the RET_operator_c visitor for details... */ |
|
446 s4o.print("\n"); |
|
447 s4o.print(s4o.indent_spaces); |
|
448 s4o.print(END_LABEL); |
|
449 s4o.print(":\n"); |
|
450 s4o.indent_right(); |
|
451 /* since every label must be followed by at least one statement, and |
|
452 * only the functions will introduce the return statement after this label, |
|
453 * function blocks written in IL would result in invalid C++ code. |
|
454 * To work around this we introduce the equivalent of a 'nop' operation |
|
455 * to humour the compiler... |
|
456 */ |
|
457 s4o.print(s4o.indent_spaces); |
|
458 s4o.print("/* to humour the compiler, we insert a nop */\n"); |
|
459 s4o.print(s4o.indent_spaces); |
|
460 this->default_variable_name.accept(*this); |
|
461 s4o.print(" = "); |
|
462 this->default_variable_name.accept(*this); |
|
463 s4o.print(";\n"); |
|
464 s4o.indent_left(); |
|
465 |
|
466 return NULL; |
|
467 } |
|
468 |
|
469 |
|
470 /* | label ':' [il_incomplete_instruction] eol_list */ |
|
471 // SYM_REF2(il_instruction_c, label, il_instruction) |
|
472 void *visit(il_instruction_c *symbol) { |
|
473 if (NULL != symbol->label) { |
|
474 symbol->label->accept(*this); |
|
475 s4o.print(":\n"); |
|
476 s4o.print(s4o.indent_spaces); |
|
477 } |
|
478 symbol->il_instruction->accept(*this); |
|
479 return NULL; |
|
480 } |
|
481 |
|
482 /* | il_simple_operator [il_operand] */ |
|
483 //SYM_REF2(il_simple_operation_c, il_simple_operator, il_operand) |
|
484 void *visit(il_simple_operation_c *symbol) { |
|
485 this->current_operand = symbol->il_operand; |
|
486 if (NULL == this->current_operand) { |
|
487 this->current_operand_type = NULL; |
|
488 } else { |
|
489 this->current_operand_type = search_il_operand_type->get_type(this->current_operand); |
|
490 if (NULL == this->current_operand_type) ERROR; |
|
491 } |
|
492 |
|
493 symbol->il_simple_operator->accept(*this); |
|
494 |
|
495 this->current_operand = NULL; |
|
496 this->current_operand_type = NULL; |
|
497 return NULL; |
|
498 } |
|
499 |
|
500 |
|
501 /* | function_name [il_operand_list] */ |
|
502 // SYM_REF2(il_function_call_c, function_name, il_operand_list) |
|
503 void *visit(il_function_call_c *symbol) { |
|
504 function_declaration_c *f_decl = function_symtable.find_value(symbol->function_name); |
|
505 |
|
506 if (f_decl == function_symtable.end_value()) |
|
507 /* should never occur. The function being called MUST be in the symtable... */ |
|
508 ERROR; |
|
509 |
|
510 /* determine the base data type returned by the function being called... */ |
|
511 search_base_type_c search_base_type; |
|
512 symbol_c *return_data_type = (symbol_c *)f_decl->type_name->accept(search_base_type); |
|
513 symbol_c *param_data_type = default_variable_name.current_type; |
|
514 if (NULL == return_data_type) ERROR; |
|
515 |
|
516 default_variable_name.current_type = return_data_type; |
|
517 this->default_variable_name.accept(*this); |
|
518 default_variable_name.current_type = param_data_type; |
|
519 s4o.print(" = "); |
|
520 |
|
521 symbol->function_name->accept(*this); |
|
522 s4o.print("("); |
|
523 |
|
524 /* loop through each function parameter, find the value we should pass |
|
525 * to it, and then output the c equivalent... |
|
526 */ |
|
527 |
|
528 function_param_iterator_c fp_iterator(f_decl); |
|
529 identifier_c *param_name; |
|
530 function_call_param_iterator_c function_call_param_iterator(symbol); |
|
531 for(int i = 1; (param_name = fp_iterator.next()) != NULL; i++) { |
|
532 if (i != 1) |
|
533 s4o.print(", "); |
|
534 |
|
535 symbol_c *param_type = fp_iterator.param_type(); |
|
536 if (param_type == NULL) ERROR; |
|
537 |
|
538 function_param_iterator_c::param_direction_t param_direction = fp_iterator.param_direction(); |
|
539 |
|
540 |
|
541 symbol_c *param_value = NULL; |
|
542 |
|
543 /* if it is the first parameter, semantics specifies that we should |
|
544 * get the value off the IL default variable! |
|
545 */ |
|
546 if (1 == i) |
|
547 param_value = &this->default_variable_name; |
|
548 |
|
549 /* Get the value from a foo(<param_name> = <param_value>) style call */ |
|
550 /* NOTE: the following line of code is not required in this case, but it doesn't |
|
551 * harm to leave it in, as in the case of a non-formal syntax function call, |
|
552 * it will always return NULL. |
|
553 * We leave it in in case we later decide to merge this part of the code together |
|
554 * with the function calling code in generate_cc_st_c, which does require |
|
555 * the following line... |
|
556 */ |
|
557 if (param_value == NULL) |
|
558 param_value = function_call_param_iterator.search(param_name); |
|
559 |
|
560 /* Get the value from a foo(<param_value>) style call */ |
|
561 if (param_value == NULL) |
|
562 param_value = function_call_param_iterator.next(); |
|
563 |
|
564 switch (param_direction) { |
|
565 case function_param_iterator_c::direction_in: |
|
566 if (param_value == NULL) { |
|
567 /* No value given for parameter, so we must use the default... */ |
|
568 /* First check whether default value specified in function declaration...*/ |
|
569 param_value = fp_iterator.default_value(); |
|
570 } |
|
571 if (param_value == NULL) { |
|
572 /* If not, get the default value of this variable's type */ |
|
573 param_value = (symbol_c *)param_type->accept(*type_initial_value_c::instance()); |
|
574 } |
|
575 if (param_value == NULL) ERROR; |
|
576 param_value->accept(*this); |
|
577 break; |
|
578 case function_param_iterator_c::direction_out: |
|
579 case function_param_iterator_c::direction_inout: |
|
580 if (param_value == NULL) { |
|
581 /* no parameter value given, so we pass a previously declared temporary variable. */ |
|
582 std::string *temp_var_name = temp_var_name_factory.new_name(); |
|
583 s4o.print(*temp_var_name); |
|
584 delete temp_var_name; |
|
585 } else { |
|
586 param_value->accept(*this); |
|
587 } |
|
588 break; |
|
589 case function_param_iterator_c::direction_extref: |
|
590 /* TODO! */ |
|
591 ERROR; |
|
592 break; |
|
593 } /* switch */ |
|
594 } /* for(...) */ |
|
595 |
|
596 s4o.print(")"); |
|
597 |
|
598 /* the data type returned by the function, and stored in the il default variable... */ |
|
599 default_variable_name.current_type = return_data_type; |
|
600 |
|
601 return NULL; |
|
602 } |
|
603 |
|
604 |
|
605 /* | il_expr_operator '(' [il_operand] eol_list [simple_instr_list] ')' */ |
|
606 //SYM_REF4(il_expression_c, il_expr_operator, il_operand, simple_instr_list, unused) |
|
607 void *visit(il_expression_c *symbol) { |
|
608 /* We will be recursevely interpreting an instruction list, |
|
609 * so we store a backup of the data type of the value currently stored |
|
610 * in the default variable, and set the current data type to NULL |
|
611 */ |
|
612 symbol_c *old_current_default_variable_data_type = this->default_variable_name.current_type; |
|
613 this->default_variable_name.current_type = NULL; |
|
614 |
|
615 /* Pass the symbol->il_operand to the simple_instr_list visitor |
|
616 * using the il_default_variable_init_value parameter... |
|
617 * Note that the simple_instr_list_c visitor will set this parameter |
|
618 * to NULL as soon as it does not require it any longer, |
|
619 * so we don't do it here again after the |
|
620 * symbol->simple_instr_list->accept(*this); |
|
621 * returns... |
|
622 */ |
|
623 this->il_default_variable_init_value = symbol->il_operand; |
|
624 |
|
625 /* Now do the parenthesised instructions... */ |
|
626 /* NOTE: the following code line will get the variable |
|
627 * this->default_variable_name.current_type updated! |
|
628 */ |
|
629 symbol->simple_instr_list->accept(*this); |
|
630 |
|
631 /* Now do the operation, using the previous result! */ |
|
632 /* NOTE: The result of the previous instruction list will be stored |
|
633 * in a variable named IL_DEFVAR_BACK. This is done in the visitor |
|
634 * to instruction_list_c objects... |
|
635 */ |
|
636 this->current_operand = &(this->default_variable_back_name); |
|
637 this->current_operand_type = this->default_variable_back_name.current_type; |
|
638 |
|
639 this->default_variable_name.current_type = old_current_default_variable_data_type; |
|
640 if (NULL == this->current_operand_type) ERROR; |
|
641 |
|
642 symbol->il_expr_operator->accept(*this); |
|
643 |
|
644 this->current_operand = NULL; |
|
645 this->current_operand_type = NULL; |
|
646 this->default_variable_back_name.current_type = NULL; |
|
647 return NULL; |
|
648 } |
|
649 |
|
650 /* il_jump_operator label */ |
|
651 // SYM_REF2(il_jump_operation_c, il_jump_operator, label) |
|
652 void *visit(il_jump_operation_c *symbol) { |
|
653 /* Pass the symbol->label to the il_jump_operation visitor |
|
654 * using the jump_label parameter... |
|
655 */ |
|
656 this->jump_label = symbol->label; |
|
657 symbol->il_jump_operator->accept(*this); |
|
658 this->jump_label = NULL; |
|
659 |
|
660 return NULL; |
|
661 } |
|
662 |
|
663 /* il_call_operator prev_declared_fb_name |
|
664 * | il_call_operator prev_declared_fb_name '(' ')' |
|
665 * | il_call_operator prev_declared_fb_name '(' eol_list ')' |
|
666 * | il_call_operator prev_declared_fb_name '(' il_operand_list ')' |
|
667 * | il_call_operator prev_declared_fb_name '(' eol_list il_param_list ')' |
|
668 */ |
|
669 // SYM_REF4(il_fb_call_c, il_call_operator, fb_name, il_operand_list, il_param_list) |
|
670 void *visit(il_fb_call_c *symbol) { |
|
671 symbol->il_call_operator->accept(*this); |
|
672 s4o.print("{\n"); |
|
673 s4o.indent_right(); |
|
674 s4o.print(s4o.indent_spaces); |
|
675 |
|
676 /* first figure out what is the name of the function block type of the function block being called... */ |
|
677 symbol_c *function_block_type_name = this->search_fb_instance_decl->get_type_name(symbol->fb_name); |
|
678 /* should never occur. The function block instance MUST have been declared... */ |
|
679 if (function_block_type_name == NULL) ERROR; |
|
680 |
|
681 /* Now find the declaration of the function block type being called... */ |
|
682 function_block_declaration_c *fb_decl = function_block_type_symtable.find_value(function_block_type_name); |
|
683 /* should never occur. The function block type being called MUST be in the symtable... */ |
|
684 if (fb_decl == function_block_type_symtable.end_value()) ERROR; |
|
685 |
|
686 /* loop through each function block parameter, find the value we should pass |
|
687 * to it, and then output the c equivalent... |
|
688 */ |
|
689 function_param_iterator_c fp_iterator(fb_decl); |
|
690 identifier_c *param_name; |
|
691 function_call_param_iterator_c function_call_param_iterator(symbol); |
|
692 for(int i = 1; (param_name = fp_iterator.next()) != NULL; i++) { |
|
693 function_param_iterator_c::param_direction_t param_direction = fp_iterator.param_direction(); |
|
694 |
|
695 /* Get the value from a foo(<param_name> = <param_value>) style call */ |
|
696 symbol_c *param_value = function_call_param_iterator.search(param_name); |
|
697 |
|
698 /* Get the value from a foo(<param_value>) style call */ |
|
699 if (param_value == NULL) |
|
700 param_value = function_call_param_iterator.next(); |
|
701 |
|
702 /* now output the value assignment */ |
|
703 if (param_value != NULL) |
|
704 if ((param_direction == function_param_iterator_c::direction_in) || |
|
705 (param_direction == function_param_iterator_c::direction_inout)) { |
|
706 symbol->fb_name->accept(*this); |
|
707 s4o.print("."); |
|
708 param_name->accept(*this); |
|
709 s4o.print(" = "); |
|
710 param_value->accept(*this); |
|
711 s4o.print(";\n" + s4o.indent_spaces); |
|
712 } |
|
713 } /* for(...) */ |
|
714 |
|
715 /* now call the function... */ |
|
716 function_block_type_name->accept(*this); |
|
717 s4o.print(FB_FUNCTION_SUFFIX); |
|
718 s4o.print("(&"); |
|
719 symbol->fb_name->accept(*this); |
|
720 s4o.print(")"); |
|
721 |
|
722 /* loop through each function parameter, find the variable to which |
|
723 * we should atribute the value of all output or inoutput parameters. |
|
724 */ |
|
725 fp_iterator.reset(); |
|
726 function_call_param_iterator.reset(); |
|
727 for(int i = 1; (param_name = fp_iterator.next()) != NULL; i++) { |
|
728 function_param_iterator_c::param_direction_t param_direction = fp_iterator.param_direction(); |
|
729 |
|
730 /* Get the value from a foo(<param_name> = <param_value>) style call */ |
|
731 symbol_c *param_value = function_call_param_iterator.search(param_name); |
|
732 |
|
733 /* Get the value from a foo(<param_value>) style call */ |
|
734 if (param_value == NULL) |
|
735 param_value = function_call_param_iterator.next(); |
|
736 |
|
737 /* now output the value assignment */ |
|
738 if (param_value != NULL) |
|
739 if ((param_direction == function_param_iterator_c::direction_out) || |
|
740 (param_direction == function_param_iterator_c::direction_inout)) { |
|
741 s4o.print(";\n"+ s4o.indent_spaces); |
|
742 param_value->accept(*this); |
|
743 s4o.print(" = "); |
|
744 symbol->fb_name->accept(*this); |
|
745 s4o.print("."); |
|
746 param_name->accept(*this); |
|
747 } |
|
748 } /* for(...) */ |
|
749 |
|
750 s4o.print(";\n"); |
|
751 s4o.indent_left(); |
|
752 s4o.print(s4o.indent_spaces); |
|
753 s4o.print("}"); |
|
754 |
|
755 return NULL; |
|
756 } |
|
757 |
|
758 |
|
759 |
|
760 /* | function_name '(' eol_list [il_param_list] ')' */ |
|
761 // SYM_REF2(il_formal_funct_call_c, function_name, il_param_list) |
|
762 void *visit(il_formal_funct_call_c *symbol) { |
|
763 function_declaration_c *f_decl = function_symtable.find_value(symbol->function_name); |
|
764 |
|
765 if (f_decl == function_symtable.end_value()) |
|
766 /* should never occur. The function being called MUST be in the symtable... */ |
|
767 ERROR; |
|
768 |
|
769 symbol->function_name->accept(*this); |
|
770 s4o.print("("); |
|
771 |
|
772 /* loop through each function parameter, find the value we should pass |
|
773 * to it, and then output the c equivalent... |
|
774 */ |
|
775 |
|
776 function_param_iterator_c fp_iterator(f_decl); |
|
777 identifier_c *param_name; |
|
778 function_call_param_iterator_c function_call_param_iterator(symbol); |
|
779 for(int i = 1; (param_name = fp_iterator.next()) != NULL; i++) { |
|
780 if (i != 1) |
|
781 s4o.print(", "); |
|
782 |
|
783 symbol_c *param_type = fp_iterator.param_type(); |
|
784 if (param_type == NULL) ERROR; |
|
785 |
|
786 function_param_iterator_c::param_direction_t param_direction = fp_iterator.param_direction(); |
|
787 |
|
788 |
|
789 symbol_c *param_value = NULL; |
|
790 |
|
791 /* Get the value from a foo(<param_name> = <param_value>) style call */ |
|
792 if (param_value == NULL) |
|
793 param_value = function_call_param_iterator.search(param_name); |
|
794 |
|
795 /* Get the value from a foo(<param_value>) style call */ |
|
796 /* NOTE: the following line of code is not required in this case, but it doesn't |
|
797 * harm to leave it in, as in the case of a formal syntax function call, |
|
798 * it will always return NULL. |
|
799 * We leave it in in case we later decide to merge this part of the code together |
|
800 * with the function calling code in generate_cc_st_c, which does require |
|
801 * the following line... |
|
802 */ |
|
803 if (param_value == NULL) |
|
804 param_value = function_call_param_iterator.next(); |
|
805 |
|
806 switch (param_direction) { |
|
807 case function_param_iterator_c::direction_in: |
|
808 if (param_value == NULL) { |
|
809 /* No value given for parameter, so we must use the default... */ |
|
810 /* First check whether default value specified in function declaration...*/ |
|
811 param_value = fp_iterator.default_value(); |
|
812 } |
|
813 if (param_value == NULL) { |
|
814 /* If not, get the default value of this variable's type */ |
|
815 param_value = (symbol_c *)param_type->accept(*type_initial_value_c::instance()); |
|
816 } |
|
817 if (param_value == NULL) ERROR; |
|
818 param_value->accept(*this); |
|
819 break; |
|
820 case function_param_iterator_c::direction_out: |
|
821 case function_param_iterator_c::direction_inout: |
|
822 if (param_value == NULL) { |
|
823 /* no parameter value given, so we pass a previously declared temporary variable. */ |
|
824 std::string *temp_var_name = temp_var_name_factory.new_name(); |
|
825 s4o.print(*temp_var_name); |
|
826 delete temp_var_name; |
|
827 } else { |
|
828 param_value->accept(*this); |
|
829 } |
|
830 break; |
|
831 case function_param_iterator_c::direction_extref: |
|
832 /* TODO! */ |
|
833 ERROR; |
|
834 break; |
|
835 } /* switch */ |
|
836 } /* for(...) */ |
|
837 |
|
838 // symbol->parameter_assignment->accept(*this); |
|
839 s4o.print(")"); |
|
840 return NULL; |
|
841 } |
|
842 |
|
843 |
|
844 /* | il_operand_list ',' il_operand */ |
|
845 // SYM_LIST(il_operand_list_c) |
|
846 void *visit(il_operand_list_c *symbol) {ERROR; return NULL;} // should never get called! |
|
847 |
|
848 |
|
849 /* | simple_instr_list il_simple_instruction */ |
|
850 // SYM_LIST(simple_instr_list_c) |
|
851 void *visit(simple_instr_list_c *symbol) { |
|
852 /* A simple_instr_list_c is used to store a list of il operations |
|
853 * being done within parenthesis... |
|
854 * |
|
855 * e.g.: |
|
856 * LD var1 |
|
857 * AND ( var2 |
|
858 * OR var3 |
|
859 * OR var4 |
|
860 * ) |
|
861 * |
|
862 * This will be converted to C++ by defining a new scope |
|
863 * with a new il default variable, and executing the il operands |
|
864 * within this new scope. |
|
865 * At the end of the scope the result, i.e. the value currently stored |
|
866 * in the il default variable is copied to the variable used to take this |
|
867 * value to the outside scope... |
|
868 * |
|
869 * The above example will result in the following C++ code: |
|
870 * {__IL_DEFVAR_T __IL_DEFVAR_BACK; |
|
871 * __IL_DEFVAR_T __IL_DEFVAR; |
|
872 * |
|
873 * __IL_DEFVAR.INTvar = var1; |
|
874 * { |
|
875 * __IL_DEFVAR_T __IL_DEFVAR; |
|
876 * |
|
877 * __IL_DEFVAR.INTvar = var2; |
|
878 * __IL_DEFVAR.INTvar |= var3; |
|
879 * __IL_DEFVAR.INTvar |= var4; |
|
880 * |
|
881 * __IL_DEFVAR_BACK = __IL_DEFVAR; |
|
882 * } |
|
883 * __IL_DEFVAR.INTvar &= __IL_DEFVAR_BACK.INTvar; |
|
884 * |
|
885 * } |
|
886 * |
|
887 * The intial value of the il default variable (in the above |
|
888 * example 'var2') is passed to this simple_instr_list_c visitor |
|
889 * using the il_default_variable_init_value parameter. |
|
890 * Since it is possible to have parenthesis inside other parenthesis |
|
891 * recursively, we reset the il_default_variable_init_value to NULL |
|
892 * as soon as we no longer require it, as it may be used once again |
|
893 * in the line |
|
894 * print_list(symbol, s4o.indent_spaces, ";\n" + s4o.indent_spaces, ";\n"); |
|
895 * |
|
896 */ |
|
897 |
|
898 /* Declare the default variable, that will store the result of the IL operations... */ |
|
899 s4o.print("{\n"); |
|
900 s4o.indent_right(); |
|
901 |
|
902 s4o.print(s4o.indent_spaces); |
|
903 s4o.print(IL_DEFVAR_T); |
|
904 s4o.print(" "); |
|
905 this->default_variable_name.accept(*this); |
|
906 s4o.print(";\n\n"); |
|
907 |
|
908 /* Check whether we should initiliase the il default variable... */ |
|
909 if (NULL != this->il_default_variable_init_value) { |
|
910 /* Yes, we must... */ |
|
911 /* We will do it by instatiating a LD operator, and having this |
|
912 * same generate_cc_il_c class visiting it! |
|
913 */ |
|
914 LD_operator_c ld_oper; |
|
915 il_simple_operation_c il_simple_oper(&ld_oper, this->il_default_variable_init_value); |
|
916 |
|
917 s4o.print(s4o.indent_spaces); |
|
918 il_simple_oper.accept(*this); |
|
919 s4o.print(";\n"); |
|
920 } |
|
921 |
|
922 /* this parameter no longer required... */ |
|
923 this->il_default_variable_init_value = NULL; |
|
924 |
|
925 print_list(symbol, s4o.indent_spaces, ";\n" + s4o.indent_spaces, ";\n"); |
|
926 |
|
927 /* copy the result in the default variable to the variable |
|
928 * used to pass the data out to the scope enclosing |
|
929 * the current scope! |
|
930 * |
|
931 * We also need to update the data type currently stored within |
|
932 * the variable used to pass the data to the outside scope... |
|
933 */ |
|
934 this->default_variable_back_name.current_type = this->default_variable_name.current_type; |
|
935 s4o.print("\n"); |
|
936 s4o.print(s4o.indent_spaces); |
|
937 this->default_variable_back_name.accept(*this); |
|
938 s4o.print(" = "); |
|
939 this->default_variable_name.accept(*this); |
|
940 s4o.print(";\n"); |
|
941 |
|
942 s4o.indent_left(); |
|
943 s4o.print(s4o.indent_spaces); |
|
944 s4o.print("}\n"); |
|
945 s4o.print(s4o.indent_spaces); |
|
946 return NULL; |
|
947 } |
|
948 |
|
949 /* | il_initial_param_list il_param_instruction */ |
|
950 // SYM_LIST(il_param_list_c) |
|
951 void *visit(il_param_list_c *symbol) {ERROR; return NULL;} // should never get called! |
|
952 |
|
953 /* il_assign_operator il_operand |
|
954 * | il_assign_operator '(' eol_list simple_instr_list ')' |
|
955 */ |
|
956 // SYM_REF4(il_param_assignment_c, il_assign_operator, il_operand, simple_instr_list, unused) |
|
957 void *visit(il_param_assignment_c *symbol) {ERROR; return NULL;} // should never get called! |
|
958 |
|
959 /* il_assign_out_operator variable */ |
|
960 // SYM_REF2(il_param_out_assignment_c, il_assign_out_operator, variable); |
|
961 void *visit(il_param_out_assignment_c *symbol) {ERROR; return NULL;} // should never get called! |
|
962 |
|
963 /*******************/ |
|
964 /* B 2.2 Operators */ |
|
965 /*******************/ |
|
966 |
|
967 void *visit(LD_operator_c *symbol) { |
|
968 /* the data type resulting from this operation... */ |
|
969 this->default_variable_name.current_type = this->current_operand_type; |
|
970 XXX_operator(&(this->default_variable_name), " = ", this->current_operand); |
|
971 return NULL; |
|
972 } |
|
973 |
|
974 void *visit(LDN_operator_c *symbol) { |
|
975 /* the data type resulting from this operation... */ |
|
976 this->default_variable_name.current_type = this->current_operand_type; |
|
977 XXX_operator(&(this->default_variable_name), |
|
978 is_bool_type(this->current_operand_type)?" = !":" = ~", |
|
979 this->current_operand); |
|
980 return NULL; |
|
981 } |
|
982 |
|
983 void *visit(ST_operator_c *symbol) { |
|
984 XXX_operator(this->current_operand, " = ",&(this->default_variable_name)); |
|
985 /* the data type resulting from this operation is unchamged. */ |
|
986 return NULL; |
|
987 } |
|
988 |
|
989 void *visit(STN_operator_c *symbol) { |
|
990 XXX_operator(this->current_operand, |
|
991 is_bool_type(this->current_operand_type)?" = !":" = ~", |
|
992 &(this->default_variable_name)); |
|
993 /* the data type resulting from this operation is unchamged. */ |
|
994 return NULL; |
|
995 } |
|
996 |
|
997 void *visit(NOT_operator_c *symbol) { |
|
998 if ((NULL != this->current_operand) || (NULL != this->current_operand_type)) ERROR; |
|
999 XXX_operator(&(this->default_variable_name), |
|
1000 is_bool_type(this->default_variable_name.current_type)?" = !":" = ~", |
|
1001 &(this->default_variable_name)); |
|
1002 /* the data type resulting from this operation is unchanged. */ |
|
1003 return NULL; |
|
1004 } |
|
1005 |
|
1006 void *visit(S_operator_c *symbol) { |
|
1007 if ((NULL == this->current_operand) || (NULL == this->current_operand_type)) ERROR; |
|
1008 |
|
1009 C_modifier(); |
|
1010 this->current_operand->accept(*this); |
|
1011 s4o.print(is_bool_type(this->current_operand_type)?" = true":" = 1"); |
|
1012 /* the data type resulting from this operation is unchanged! */ |
|
1013 return NULL; |
|
1014 } |
|
1015 |
|
1016 void *visit(R_operator_c *symbol) { |
|
1017 if ((NULL == this->current_operand) || (NULL == this->current_operand_type)) ERROR; |
|
1018 |
|
1019 C_modifier(); |
|
1020 this->current_operand->accept(*this); |
|
1021 s4o.print(is_bool_type(this->current_operand_type)?" = false":" = 0"); |
|
1022 /* the data type resulting from this operation is unchanged! */ |
|
1023 return NULL; |
|
1024 } |
|
1025 |
|
1026 void *visit(S1_operator_c *symbol) {return XXX_CAL_operator("S1", this->current_operand);} |
|
1027 void *visit(R1_operator_c *symbol) {return XXX_CAL_operator("R1", this->current_operand);} |
|
1028 void *visit(CLK_operator_c *symbol) {return XXX_CAL_operator("CLK", this->current_operand);} |
|
1029 void *visit(CU_operator_c *symbol) {return XXX_CAL_operator("CU", this->current_operand);} |
|
1030 void *visit(CD_operator_c *symbol) {return XXX_CAL_operator("CD", this->current_operand);} |
|
1031 void *visit(PV_operator_c *symbol) {return XXX_CAL_operator("PV", this->current_operand);} |
|
1032 void *visit(IN_operator_c *symbol) {return XXX_CAL_operator("IN", this->current_operand);} |
|
1033 void *visit(PT_operator_c *symbol) {return XXX_CAL_operator("PT", this->current_operand);} |
|
1034 |
|
1035 void *visit(AND_operator_c *symbol) { |
|
1036 XXX_operator(&(this->default_variable_name), " &= ", this->current_operand); |
|
1037 /* the data type resulting from this operation... */ |
|
1038 this->default_variable_name.current_type = this->current_operand_type; |
|
1039 return NULL; |
|
1040 } |
|
1041 |
|
1042 void *visit(OR_operator_c *symbol) { |
|
1043 XXX_operator(&(this->default_variable_name), " |= ", this->current_operand); |
|
1044 /* the data type resulting from this operation... */ |
|
1045 this->default_variable_name.current_type = this->current_operand_type; |
|
1046 return NULL; |
|
1047 } |
|
1048 |
|
1049 void *visit(XOR_operator_c *symbol) { |
|
1050 // '^' is a bit by bit exclusive OR !! Also seems to work with boolean types! |
|
1051 XXX_operator(&(this->default_variable_name), " ^= ", this->current_operand); |
|
1052 /* the data type resulting from this operation... */ |
|
1053 this->default_variable_name.current_type = this->current_operand_type; |
|
1054 return NULL; |
|
1055 } |
|
1056 |
|
1057 void *visit(ANDN_operator_c *symbol) { |
|
1058 XXX_operator(&(this->default_variable_name), |
|
1059 is_bool_type(this->current_operand_type)?" &= !":" &= ~", |
|
1060 this->current_operand); |
|
1061 /* the data type resulting from this operation... */ |
|
1062 this->default_variable_name.current_type = this->current_operand_type; |
|
1063 return NULL; |
|
1064 } |
|
1065 |
|
1066 void *visit(ORN_operator_c *symbol) { |
|
1067 XXX_operator(&(this->default_variable_name), |
|
1068 is_bool_type(this->current_operand_type)?" |= !":" |= ~", |
|
1069 this->current_operand); |
|
1070 /* the data type resulting from this operation... */ |
|
1071 this->default_variable_name.current_type = this->current_operand_type; |
|
1072 return NULL; |
|
1073 } |
|
1074 |
|
1075 void *visit(XORN_operator_c *symbol) { |
|
1076 XXX_operator(&(this->default_variable_name), |
|
1077 // bit by bit exclusive OR !! Also seems to work with boolean types! |
|
1078 is_bool_type(this->current_operand_type)?" ^= !":" ^= ~", |
|
1079 this->current_operand); |
|
1080 /* the data type resulting from this operation... */ |
|
1081 this->default_variable_name.current_type = this->current_operand_type; |
|
1082 return NULL; |
|
1083 } |
|
1084 |
|
1085 void *visit(ADD_operator_c *symbol) { |
|
1086 XXX_operator(&(this->default_variable_name), " += ", this->current_operand); |
|
1087 /* the data type resulting from this operation... */ |
|
1088 this->default_variable_name.current_type = this->current_operand_type; |
|
1089 return NULL; |
|
1090 } |
|
1091 |
|
1092 void *visit(SUB_operator_c *symbol) { |
|
1093 XXX_operator(&(this->default_variable_name), " -= ", this->current_operand); |
|
1094 /* the data type resulting from this operation... */ |
|
1095 this->default_variable_name.current_type = this->current_operand_type; |
|
1096 return NULL; |
|
1097 } |
|
1098 |
|
1099 void *visit(MUL_operator_c *symbol) { |
|
1100 XXX_operator(&(this->default_variable_name), " *= ", this->current_operand); |
|
1101 /* the data type resulting from this operation... */ |
|
1102 this->default_variable_name.current_type = this->current_operand_type; |
|
1103 return NULL; |
|
1104 } |
|
1105 |
|
1106 void *visit(DIV_operator_c *symbol) { |
|
1107 XXX_operator(&(this->default_variable_name), " /= ", this->current_operand); |
|
1108 /* the data type resulting from this operation... */ |
|
1109 this->default_variable_name.current_type = this->current_operand_type; |
|
1110 return NULL; |
|
1111 } |
|
1112 |
|
1113 void *visit(MOD_operator_c *symbol) { |
|
1114 XXX_operator(&(this->default_variable_name), " %= ", this->current_operand); |
|
1115 /* the data type resulting from this operation... */ |
|
1116 this->default_variable_name.current_type = this->current_operand_type; |
|
1117 return NULL; |
|
1118 } |
|
1119 |
|
1120 void *visit(GT_operator_c *symbol) { |
|
1121 return CMP_operator(this->current_operand, " > "); |
|
1122 } |
|
1123 |
|
1124 void *visit(GE_operator_c *symbol) { |
|
1125 return CMP_operator(this->current_operand, " >= "); |
|
1126 } |
|
1127 |
|
1128 void *visit(EQ_operator_c *symbol) { |
|
1129 return CMP_operator(this->current_operand, " == "); |
|
1130 } |
|
1131 |
|
1132 void *visit(LT_operator_c *symbol) { |
|
1133 return CMP_operator(this->current_operand, " < "); |
|
1134 } |
|
1135 |
|
1136 void *visit(LE_operator_c *symbol) { |
|
1137 return CMP_operator(this->current_operand, " <= "); |
|
1138 } |
|
1139 |
|
1140 void *visit(NE_operator_c *symbol) { |
|
1141 return CMP_operator(this->current_operand, " != "); |
|
1142 } |
|
1143 |
|
1144 |
|
1145 //SYM_REF0(CAL_operator_c) |
|
1146 // This method will be called from within the il_fb_call_c visitor method |
|
1147 void *visit(CAL_operator_c *symbol) {return NULL;} |
|
1148 |
|
1149 //SYM_REF0(CALC_operator_c) |
|
1150 // This method will be called from within the il_fb_call_c visitor method |
|
1151 void *visit(CALC_operator_c *symbol) {C_modifier(); return NULL;} |
|
1152 |
|
1153 //SYM_REF0(CALCN_operator_c) |
|
1154 // This method will be called from within the il_fb_call_c visitor method |
|
1155 void *visit(CALCN_operator_c *symbol) {CN_modifier(); return NULL;} |
|
1156 |
|
1157 /* NOTE: The semantics of the RET operator requires us to return a value |
|
1158 * if the IL code is inside a function, but simply return no value if |
|
1159 * the IL code is inside a function block or program! |
|
1160 * Nevertheless, it is the generate_cc_c class itself that |
|
1161 * introduces the 'reaturn <value>' into the c++ code at the end |
|
1162 * of every function. This class does not know whether the IL code |
|
1163 * is inside a function or a function block. |
|
1164 * We work around this by jumping to the end of the code, |
|
1165 * that will be marked by the END_LABEL label in the |
|
1166 * instruction_list_c visitor... |
|
1167 */ |
|
1168 // SYM_REF0(RET_operator_c) |
|
1169 void *visit(RET_operator_c *symbol) { |
|
1170 s4o.print("goto ");s4o.print(END_LABEL); |
|
1171 return NULL; |
|
1172 } |
|
1173 |
|
1174 // SYM_REF0(RETC_operator_c) |
|
1175 void *visit(RETC_operator_c *symbol) { |
|
1176 C_modifier(); |
|
1177 s4o.print("goto ");s4o.print(END_LABEL); |
|
1178 return NULL; |
|
1179 } |
|
1180 |
|
1181 // SYM_REF0(RETCN_operator_c) |
|
1182 void *visit(RETCN_operator_c *symbol) { |
|
1183 CN_modifier(); |
|
1184 s4o.print("goto ");s4o.print(END_LABEL); |
|
1185 return NULL; |
|
1186 } |
|
1187 |
|
1188 //SYM_REF0(JMP_operator_c) |
|
1189 void *visit(JMP_operator_c *symbol) { |
|
1190 if (NULL == this->jump_label) ERROR; |
|
1191 |
|
1192 s4o.print("goto "); |
|
1193 this->jump_label->accept(*this); |
|
1194 /* the data type resulting from this operation is unchanged! */ |
|
1195 return NULL; |
|
1196 } |
|
1197 |
|
1198 // SYM_REF0(JMPC_operator_c) |
|
1199 void *visit(JMPC_operator_c *symbol) { |
|
1200 if (NULL == this->jump_label) ERROR; |
|
1201 |
|
1202 C_modifier(); |
|
1203 s4o.print("goto "); |
|
1204 this->jump_label->accept(*this); |
|
1205 /* the data type resulting from this operation is unchanged! */ |
|
1206 return NULL; |
|
1207 } |
|
1208 |
|
1209 // SYM_REF0(JMPCN_operator_c) |
|
1210 void *visit(JMPCN_operator_c *symbol) { |
|
1211 if (NULL == this->jump_label) ERROR; |
|
1212 |
|
1213 CN_modifier(); |
|
1214 s4o.print("goto "); |
|
1215 this->jump_label->accept(*this); |
|
1216 /* the data type resulting from this operation is unchanged! */ |
|
1217 return NULL; |
|
1218 } |
|
1219 |
|
1220 #if 0 |
|
1221 /*| [NOT] any_identifier SENDTO */ |
|
1222 SYM_REF2(il_assign_out_operator_c, option, variable_name) |
|
1223 #endif |
|
1224 |
|
1225 }; /* generate_cc_il_c */ |
|
1226 |
|
1227 |
|
1228 |
|
1229 |
|
1230 |
|
1231 |
|
1232 |
|
1233 |
|
1234 |
|
1235 /* The implementation of the single visit() member function |
|
1236 * of il_default_variable_c. |
|
1237 * It can only come after the full declaration of |
|
1238 * generate_cc_il_c. Since we define and declare |
|
1239 * generate_cc_il_c simultaneously, it can only come |
|
1240 * after the definition... |
|
1241 */ |
|
1242 void *il_default_variable_c::accept(visitor_c &visitor) { |
|
1243 /* An ugly hack!! */ |
|
1244 /* This is required because we need to over-ride the base |
|
1245 * accept(visitor_c &) method of the class symbol_c, |
|
1246 * so this method may be called through a symbol_c * |
|
1247 * reference! |
|
1248 * |
|
1249 * But, the visitor_c does not include a visitor to |
|
1250 * an il_default_variable_c, which means that we couldn't |
|
1251 * simply call visitor.visit(this); |
|
1252 * |
|
1253 * We therefore need to use the dynamic_cast hack!! |
|
1254 * |
|
1255 * Note too that we can't cast a visitor_c to a |
|
1256 * il_default_variable_visitor_c, since they are not related. |
|
1257 * Nor may the il_default_variable_visitor_c inherit from |
|
1258 * visitor_c, because then generate_cc_il_c would contain |
|
1259 * two visitor_c base classes, one each through |
|
1260 * il_default_variable_visitor_c and generate_cc_type_c |
|
1261 * |
|
1262 * We could use virtual inheritance of the visitor_c, but it |
|
1263 * would probably create more problems than it is worth! |
|
1264 */ |
|
1265 generate_cc_il_c *v; |
|
1266 v = dynamic_cast<generate_cc_il_c *>(&visitor); |
|
1267 if (v == NULL) ERROR; |
|
1268 |
|
1269 return v->visit(this); |
|
1270 } |
|
1271 |
|
1272 |
|
1273 |
|
1274 |
|
1275 il_default_variable_c::il_default_variable_c(const char *var_name_str, symbol_c *current_type) { |
|
1276 if (NULL == var_name_str) ERROR; |
|
1277 /* Note: current_type may start off with NULL */ |
|
1278 |
|
1279 this->var_name = new identifier_c(var_name_str); |
|
1280 if (NULL == this->var_name) ERROR; |
|
1281 |
|
1282 this->current_type = current_type; |
|
1283 } |