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 st statements (i.e. ST 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 class generate_cc_st_c: public generate_cc_typedecl_c { |
|
42 |
|
43 private: |
|
44 /* When calling a function block, we must first find it's type, |
|
45 * by searching through the declarations of the variables currently |
|
46 * in scope. |
|
47 * This class does just that... |
|
48 * A new class is instantiated whenever we begin generating the code |
|
49 * for a function block type declaration, or a program declaration. |
|
50 * This object instance will then later be called while the |
|
51 * function block's or the program's body is being handled. |
|
52 * |
|
53 * Note that functions cannot contain calls to function blocks, |
|
54 * so we do not create an object instance when handling |
|
55 * a function declaration. |
|
56 */ |
|
57 search_fb_instance_decl_c *search_fb_instance_decl; |
|
58 |
|
59 /* When compiling st code, it becomes necessary to determine the |
|
60 * data type of st expressions. To do this, we must first find the |
|
61 * st operand's declaration, within the scope of the function block |
|
62 * or function currently being processed. |
|
63 * The following object does just that... |
|
64 * This object instance will then later be called while the |
|
65 * remaining st code is being handled. |
|
66 */ |
|
67 search_expression_type_c *search_expression_type; |
|
68 |
|
69 search_varfb_instance_type_c *search_varfb_instance_type; |
|
70 |
|
71 public: |
|
72 generate_cc_st_c(stage4out_c *s4o_ptr, symbol_c *scope, const char *variable_prefix = NULL) |
|
73 : generate_cc_typedecl_c(s4o_ptr) { |
|
74 search_fb_instance_decl = new search_fb_instance_decl_c(scope); |
|
75 search_expression_type = new search_expression_type_c(scope); |
|
76 search_varfb_instance_type = new search_varfb_instance_type_c(scope); |
|
77 this->set_variable_prefix(variable_prefix); |
|
78 } |
|
79 |
|
80 virtual ~generate_cc_st_c(void) { |
|
81 delete search_fb_instance_decl; |
|
82 delete search_expression_type; |
|
83 delete search_varfb_instance_type; |
|
84 } |
|
85 |
|
86 |
|
87 private: |
|
88 /* Some function calls in the body of functions or function blocks |
|
89 * may leave some parameters to their default values, and |
|
90 * ignore some output parameters of the function being called. |
|
91 * Our conversion of ST functions to C++ does not contemplate that, |
|
92 * i.e. each called function must get all it's input and output |
|
93 * parameters set correctly. |
|
94 * For input parameters we merely need to call the function with |
|
95 * the apropriate default value, but for output parameters |
|
96 * we must create temporary variables to hold the output value. |
|
97 * |
|
98 * We declare all the temporary output variables at the begining of |
|
99 * the body of each function or function block, and use them as |
|
100 * in function calls later on as they become necessary... |
|
101 * Note that we cannot create these variables just before a function |
|
102 * call, as the function call itself may be integrated within an |
|
103 * expression, or another function call! |
|
104 * |
|
105 * The variables are declared in the exact same order in which they |
|
106 * will be used later on during the function calls, which allows us |
|
107 * to simply re-create the name that was used for the temporary variable |
|
108 * instead of keeping it in some list. |
|
109 * The names are recreated by the temp_var_name_factory, after reset() |
|
110 * has been called! |
|
111 * |
|
112 * This function will genertae code similar to... |
|
113 * |
|
114 * INT __TMP_0 = 23; |
|
115 * REAL __TMP_1 = 45.5; |
|
116 * ... |
|
117 */ |
|
118 temp_var_name_c temp_var_name_factory; |
|
119 |
|
120 public: |
|
121 void generate(statement_list_c *stl) { |
|
122 generate_cc_tempvardecl_c generate_cc_tempvardecl(&s4o); |
|
123 generate_cc_tempvardecl.generate(stl, &temp_var_name_factory); |
|
124 stl->accept(*this); |
|
125 } |
|
126 |
|
127 private: |
|
128 |
|
129 /*********************/ |
|
130 /* B 1.4 - Variables */ |
|
131 /*********************/ |
|
132 void *visit(symbolic_variable_c *symbol) { |
|
133 unsigned int vartype = search_varfb_instance_type->get_vartype(symbol); |
|
134 if (vartype == search_var_instance_decl_c::external_vt || vartype == search_var_instance_decl_c::located_vt) { |
|
135 s4o.print("*("); |
|
136 generate_cc_base_c::visit(symbol); |
|
137 s4o.print(")"); |
|
138 } |
|
139 else { |
|
140 generate_cc_base_c::visit(symbol); |
|
141 } |
|
142 return NULL; |
|
143 } |
|
144 |
|
145 /********************************************/ |
|
146 /* B.1.4.1 Directly Represented Variables */ |
|
147 /********************************************/ |
|
148 // direct_variable: direct_variable_token {$$ = new direct_variable_c($1);}; |
|
149 void *visit(direct_variable_c *symbol) { |
|
150 TRACE("direct_variable_c"); |
|
151 /* Do not use print_token() as it will change everything into uppercase */ |
|
152 if (strlen(symbol->value) == 0) ERROR; |
|
153 s4o.print("*("); |
|
154 this->print_variable_prefix(); |
|
155 s4o.printlocation(symbol->value + 1); |
|
156 s4o.print(")"); |
|
157 return NULL; |
|
158 } |
|
159 |
|
160 /***************************************/ |
|
161 /* B.3 - Language ST (Structured Text) */ |
|
162 /***************************************/ |
|
163 /***********************/ |
|
164 /* B 3.1 - Expressions */ |
|
165 /***********************/ |
|
166 void *visit(or_expression_c *symbol) { |
|
167 symbol_c *left_type = search_expression_type->get_type(symbol->l_exp); |
|
168 symbol_c *right_type = search_expression_type->get_type(symbol->r_exp); |
|
169 if (!search_expression_type->is_same_type(left_type, right_type)) |
|
170 ERROR; |
|
171 if (search_expression_type->is_bool_type(left_type)) |
|
172 return print_binary_expression(symbol->l_exp, symbol->r_exp, " || "); |
|
173 if (search_expression_type->is_binary_type(left_type)) |
|
174 return print_binary_expression(symbol->l_exp, symbol->r_exp, " | "); |
|
175 ERROR; |
|
176 return NULL; |
|
177 } |
|
178 |
|
179 void *visit(xor_expression_c *symbol) { |
|
180 symbol_c *left_type = search_expression_type->get_type(symbol->l_exp); |
|
181 symbol_c *right_type = search_expression_type->get_type(symbol->r_exp); |
|
182 if (!search_expression_type->is_same_type(left_type, right_type)) |
|
183 ERROR; |
|
184 if (search_expression_type->is_bool_type(left_type)) { |
|
185 s4o.print("("); |
|
186 symbol->l_exp->accept(*this); |
|
187 s4o.print(" && !"); |
|
188 symbol->r_exp->accept(*this); |
|
189 s4o.print(") || (!"); |
|
190 symbol->l_exp->accept(*this); |
|
191 s4o.print(" && "); |
|
192 symbol->r_exp->accept(*this); |
|
193 s4o.print(")"); |
|
194 } |
|
195 if (search_expression_type->is_binary_type(left_type)) |
|
196 return print_binary_expression(symbol->l_exp, symbol->r_exp, " ^ "); |
|
197 ERROR; |
|
198 return NULL; |
|
199 } |
|
200 |
|
201 void *visit(and_expression_c *symbol) { |
|
202 symbol_c *left_type = search_expression_type->get_type(symbol->l_exp); |
|
203 symbol_c *right_type = search_expression_type->get_type(symbol->r_exp); |
|
204 if (!search_expression_type->is_same_type(left_type, right_type)) |
|
205 ERROR; |
|
206 if (search_expression_type->is_bool_type(left_type)) |
|
207 return print_binary_expression(symbol->l_exp, symbol->r_exp, " && "); |
|
208 if (search_expression_type->is_binary_type(left_type)) |
|
209 return print_binary_expression(symbol->l_exp, symbol->r_exp, " & "); |
|
210 ERROR; |
|
211 return NULL; |
|
212 } |
|
213 |
|
214 void *visit(equ_expression_c *symbol) { |
|
215 symbol_c *left_type = search_expression_type->get_type(symbol->l_exp); |
|
216 symbol_c *right_type = search_expression_type->get_type(symbol->r_exp); |
|
217 if (!search_expression_type->is_same_type(left_type, right_type)) |
|
218 ERROR; |
|
219 if (search_expression_type->is_time_type(left_type)) |
|
220 return print_compare_function("__eq_", left_type, symbol->l_exp, symbol->r_exp); |
|
221 return print_binary_expression(symbol->l_exp, symbol->r_exp, " == "); |
|
222 } |
|
223 |
|
224 void *visit(notequ_expression_c *symbol) { |
|
225 symbol_c *left_type = search_expression_type->get_type(symbol->l_exp); |
|
226 symbol_c *right_type = search_expression_type->get_type(symbol->r_exp); |
|
227 if (!search_expression_type->is_same_type(left_type, right_type)) |
|
228 ERROR; |
|
229 if (search_expression_type->is_time_type(left_type)) |
|
230 return print_compare_function("__ne_", left_type, symbol->l_exp, symbol->r_exp); |
|
231 return print_binary_expression(symbol->l_exp, symbol->r_exp, " != "); |
|
232 } |
|
233 |
|
234 void *visit(lt_expression_c *symbol) { |
|
235 symbol_c *left_type = search_expression_type->get_type(symbol->l_exp); |
|
236 symbol_c *right_type = search_expression_type->get_type(symbol->r_exp); |
|
237 if (!search_expression_type->is_same_type(left_type, right_type)) |
|
238 ERROR; |
|
239 if (search_expression_type->is_time_type(left_type)) |
|
240 return print_compare_function("__lt_", left_type, symbol->l_exp, symbol->r_exp); |
|
241 return print_binary_expression(symbol->l_exp, symbol->r_exp, " < "); |
|
242 } |
|
243 |
|
244 void *visit(gt_expression_c *symbol) { |
|
245 symbol_c *left_type = search_expression_type->get_type(symbol->l_exp); |
|
246 symbol_c *right_type = search_expression_type->get_type(symbol->r_exp); |
|
247 if (!search_expression_type->is_same_type(left_type, right_type)) |
|
248 ERROR; |
|
249 if (search_expression_type->is_time_type(left_type)) |
|
250 return print_compare_function("__gt_", left_type, symbol->l_exp, symbol->r_exp); |
|
251 return print_binary_expression(symbol->l_exp, symbol->r_exp, " > "); |
|
252 } |
|
253 |
|
254 void *visit(le_expression_c *symbol) { |
|
255 symbol_c *left_type = search_expression_type->get_type(symbol->l_exp); |
|
256 symbol_c *right_type = search_expression_type->get_type(symbol->r_exp); |
|
257 if (!search_expression_type->is_same_type(left_type, right_type)) |
|
258 ERROR; |
|
259 if (search_expression_type->is_time_type(left_type)) |
|
260 return print_compare_function("__le_", left_type, symbol->l_exp, symbol->r_exp); |
|
261 return print_binary_expression(symbol->l_exp, symbol->r_exp, " <= "); |
|
262 } |
|
263 |
|
264 void *visit(ge_expression_c *symbol) { |
|
265 symbol_c *left_type = search_expression_type->get_type(symbol->l_exp); |
|
266 symbol_c *right_type = search_expression_type->get_type(symbol->r_exp); |
|
267 if (!search_expression_type->is_same_type(left_type, right_type)) |
|
268 ERROR; |
|
269 if (search_expression_type->is_time_type(left_type)) |
|
270 return print_compare_function("__ge_", left_type, symbol->l_exp, symbol->r_exp); |
|
271 return print_binary_expression(symbol->l_exp, symbol->r_exp, " >= "); |
|
272 } |
|
273 |
|
274 void *visit(add_expression_c *symbol) { |
|
275 symbol_c *left_type = search_expression_type->get_type(symbol->l_exp); |
|
276 symbol_c *right_type = search_expression_type->get_type(symbol->r_exp); |
|
277 if ((typeid(*left_type) == typeid(time_type_name_c) && typeid(*right_type) == typeid(time_type_name_c)) || |
|
278 (typeid(*left_type) == typeid(tod_type_name_c) && typeid(*right_type) == typeid(time_type_name_c)) || |
|
279 (typeid(*left_type) == typeid(dt_type_name_c) && typeid(*right_type) == typeid(time_type_name_c))) |
|
280 return print_binary_function("__time_add", symbol->l_exp, symbol->r_exp); |
|
281 if (!search_expression_type->is_same_type(left_type, right_type)) |
|
282 ERROR; |
|
283 if (search_expression_type->is_integer_type(left_type) || search_expression_type->is_real_type(left_type)) |
|
284 return print_binary_expression(symbol->l_exp, symbol->r_exp, " + "); |
|
285 ERROR; |
|
286 return NULL; |
|
287 } |
|
288 |
|
289 void *visit(sub_expression_c *symbol) { |
|
290 symbol_c *left_type = search_expression_type->get_type(symbol->l_exp); |
|
291 symbol_c *right_type = search_expression_type->get_type(symbol->r_exp); |
|
292 if ((typeid(*left_type) == typeid(time_type_name_c) && typeid(*right_type) == typeid(time_type_name_c)) || |
|
293 (typeid(*left_type) == typeid(date_type_name_c) && typeid(*right_type) == typeid(date_type_name_c)) || |
|
294 (typeid(*left_type) == typeid(tod_type_name_c) && typeid(*right_type) == typeid(time_type_name_c)) || |
|
295 (typeid(*left_type) == typeid(tod_type_name_c) && typeid(*right_type) == typeid(tod_type_name_c)) || |
|
296 (typeid(*left_type) == typeid(dt_type_name_c) && typeid(*right_type) == typeid(time_type_name_c)) || |
|
297 (typeid(*left_type) == typeid(dt_type_name_c) && typeid(*right_type) == typeid(dt_type_name_c))) |
|
298 return print_binary_function("__time_sub", symbol->l_exp, symbol->r_exp); |
|
299 if (!search_expression_type->is_same_type(left_type, right_type)) |
|
300 ERROR; |
|
301 if (search_expression_type->is_integer_type(left_type) || search_expression_type->is_real_type(left_type)) |
|
302 return print_binary_expression(symbol->l_exp, symbol->r_exp, " - "); |
|
303 ERROR; |
|
304 return NULL; |
|
305 } |
|
306 |
|
307 void *visit(mul_expression_c *symbol) { |
|
308 symbol_c *left_type = search_expression_type->get_type(symbol->l_exp); |
|
309 symbol_c *right_type = search_expression_type->get_type(symbol->r_exp); |
|
310 if ((typeid(*left_type) == typeid(time_type_name_c) && search_expression_type->is_integer_type(right_type)) || |
|
311 (typeid(*left_type) == typeid(time_type_name_c) && search_expression_type->is_real_type(right_type))) |
|
312 return print_binary_function("__time_mul", symbol->l_exp, symbol->r_exp); |
|
313 if (!search_expression_type->is_same_type(left_type, right_type)) |
|
314 ERROR; |
|
315 if (search_expression_type->is_integer_type(left_type) || search_expression_type->is_real_type(left_type)) |
|
316 return print_binary_expression(symbol->l_exp, symbol->r_exp, " * "); |
|
317 ERROR; |
|
318 return NULL; |
|
319 } |
|
320 |
|
321 void *visit(div_expression_c *symbol) { |
|
322 symbol_c *left_type = search_expression_type->get_type(symbol->l_exp); |
|
323 symbol_c *right_type = search_expression_type->get_type(symbol->r_exp); |
|
324 if (!search_expression_type->is_same_type(left_type, right_type)) |
|
325 ERROR; |
|
326 if (search_expression_type->is_integer_type(left_type) || search_expression_type->is_real_type(left_type)) |
|
327 return print_binary_expression(symbol->l_exp, symbol->r_exp, " / "); |
|
328 ERROR; |
|
329 return NULL; |
|
330 } |
|
331 |
|
332 void *visit(mod_expression_c *symbol) { |
|
333 symbol_c *left_type = search_expression_type->get_type(symbol->l_exp); |
|
334 symbol_c *right_type = search_expression_type->get_type(symbol->r_exp); |
|
335 if (!search_expression_type->is_same_type(left_type, right_type)) |
|
336 ERROR; |
|
337 if (search_expression_type->is_integer_type(left_type) || search_expression_type->is_real_type(left_type)) { |
|
338 s4o.print("(("); |
|
339 symbol->r_exp->accept(*this); |
|
340 s4o.print(" == 0)?0:"); |
|
341 print_binary_expression(symbol->l_exp, symbol->r_exp, " % "); |
|
342 s4o.print(")"); |
|
343 return NULL; |
|
344 } |
|
345 ERROR; |
|
346 return NULL; |
|
347 } |
|
348 |
|
349 /* TODO: power expression... */ |
|
350 void *visit(power_expression_c *symbol) {ERROR; return print_binary_expression(symbol->l_exp, symbol->r_exp, " ** ");} |
|
351 void *visit(neg_expression_c *symbol) {return print_unary_expression(symbol->exp, " -");} |
|
352 |
|
353 void *visit(not_expression_c *symbol) { |
|
354 symbol_c *exp_type = search_expression_type->get_type(symbol->exp); |
|
355 if (!search_expression_type->is_binary_type(exp_type)) |
|
356 ERROR; |
|
357 return print_unary_expression(symbol->exp, search_expression_type->is_bool_type(exp_type)?"!":"~"); |
|
358 } |
|
359 |
|
360 void *visit(function_invocation_c *symbol) { |
|
361 function_declaration_c *f_decl = function_symtable.find_value(symbol->function_name); |
|
362 |
|
363 if (f_decl == function_symtable.end_value()) { |
|
364 /* The function called is not in the symtable, so we test if it is a |
|
365 * standard function defined in standard */ |
|
366 |
|
367 function_type_t current_function_type = get_function_type((identifier_c *)symbol->function_name); |
|
368 if (current_function_type == function_none) ERROR; |
|
369 |
|
370 symbol_c *function_return_type = search_expression_type->get_type(symbol); |
|
371 |
|
372 function_call_param_iterator_c function_call_param_iterator(symbol); |
|
373 |
|
374 int nb_param = ((list_c *)symbol->parameter_assignment_list)->n; |
|
375 |
|
376 #include "st_code_gen.c" |
|
377 |
|
378 #if 0 |
|
379 for(int current_param = 0; current_param < nb_param; current_param++) { |
|
380 symbol_c *param_name = NULL; |
|
381 switch (current_function_type) { |
|
382 case function_add: |
|
383 case function_and: |
|
384 case function_or: |
|
385 param_name = generate_param_name("IN%d", current_param + 1); |
|
386 break; |
|
387 case function_sub: |
|
388 if (current_param < 2) |
|
389 param_name = generate_param_name("IN%d", current_param + 1); |
|
390 else |
|
391 ERROR; |
|
392 break; |
|
393 default: ERROR; |
|
394 } |
|
395 |
|
396 /* Get the value from a foo(<param_name> = <param_value>) style call */ |
|
397 symbol_c *param_value = function_call_param_iterator.search(param_name); |
|
398 delete param_name; |
|
399 |
|
400 /* Get the value from a foo(<param_value>) style call */ |
|
401 if (param_value == NULL) |
|
402 param_value = function_call_param_iterator.next(); |
|
403 |
|
404 if (param_value == NULL) ERROR; |
|
405 |
|
406 switch (current_function_type) { |
|
407 case function_add: |
|
408 if (search_expression_type->is_time_type(function_return_type)) { |
|
409 if (current_param == 0) { |
|
410 s4o.print("__time_add("); |
|
411 param_value->accept(*this); |
|
412 } |
|
413 else if (current_param == 1) { |
|
414 s4o.print(", "); |
|
415 param_value->accept(*this); |
|
416 s4o.print(")"); |
|
417 } |
|
418 else ERROR; |
|
419 } |
|
420 else { |
|
421 if (current_param == 0) |
|
422 s4o.print("("); |
|
423 else |
|
424 s4o.print(" + "); |
|
425 param_value->accept(*this); |
|
426 if (current_param == nb_param - 1) |
|
427 s4o.print(")"); |
|
428 } |
|
429 break; |
|
430 case function_sub: |
|
431 if (search_expression_type->is_time_type(function_return_type)) { |
|
432 if (current_param == 0) { |
|
433 s4o.print("__time_sub("); |
|
434 param_value->accept(*this); |
|
435 } |
|
436 else if (current_param == 1) { |
|
437 s4o.print(", "); |
|
438 param_value->accept(*this); |
|
439 s4o.print(")"); |
|
440 } |
|
441 else ERROR; |
|
442 } |
|
443 else { |
|
444 if (current_param == 0) { |
|
445 s4o.print("("); |
|
446 param_value->accept(*this); |
|
447 } |
|
448 else if (current_param == 1) { |
|
449 s4o.print(" - "); |
|
450 param_value->accept(*this); |
|
451 s4o.print(")"); |
|
452 } |
|
453 else ERROR; |
|
454 } |
|
455 break; |
|
456 case function_and: |
|
457 if (current_param == 0) |
|
458 s4o.print("("); |
|
459 else |
|
460 if (search_expression_type->is_bool_type(function_return_type)) |
|
461 s4o.print(" && "); |
|
462 else |
|
463 s4o.print(" & "); |
|
464 param_value->accept(*this); |
|
465 if (current_param == nb_param - 1) |
|
466 s4o.print(")"); |
|
467 break; |
|
468 case function_or: |
|
469 if (current_param == 0) |
|
470 s4o.print("("); |
|
471 else |
|
472 if (search_expression_type->is_bool_type(function_return_type)) |
|
473 s4o.print(" || "); |
|
474 else |
|
475 s4o.print(" | "); |
|
476 param_value->accept(*this); |
|
477 if (current_param == nb_param - 1) |
|
478 s4o.print(")"); |
|
479 break; |
|
480 default: ERROR; |
|
481 } |
|
482 } /* for(...) */ |
|
483 #endif |
|
484 } |
|
485 else { |
|
486 /* loop through each function parameter, find the value we should pass |
|
487 * to it, and then output the c equivalent... |
|
488 */ |
|
489 function_param_iterator_c fp_iterator(f_decl); |
|
490 |
|
491 symbol->function_name->accept(*this); |
|
492 s4o.print("("); |
|
493 s4o.indent_right(); |
|
494 |
|
495 identifier_c *param_name; |
|
496 function_call_param_iterator_c function_call_param_iterator(symbol); |
|
497 for(int i = 1; (param_name = fp_iterator.next()) != NULL; i++) { |
|
498 if (i != 1) |
|
499 s4o.print(",\n"+s4o.indent_spaces); |
|
500 |
|
501 function_param_iterator_c::param_direction_t param_direction = fp_iterator.param_direction(); |
|
502 |
|
503 /* Get the value from a foo(<param_name> = <param_value>) style call */ |
|
504 symbol_c *param_value = function_call_param_iterator.search(param_name); |
|
505 |
|
506 /* Get the value from a foo(<param_value>) style call */ |
|
507 if (param_value == NULL) |
|
508 param_value = function_call_param_iterator.next(); |
|
509 |
|
510 symbol_c *param_type = fp_iterator.param_type(); |
|
511 if (param_type == NULL) ERROR; |
|
512 |
|
513 switch (param_direction) { |
|
514 case function_param_iterator_c::direction_in: |
|
515 if (param_value == NULL) { |
|
516 /* No value given for parameter, so we must use the default... */ |
|
517 /* First check whether default value specified in function declaration...*/ |
|
518 param_value = fp_iterator.default_value(); |
|
519 } |
|
520 if (param_value == NULL) { |
|
521 /* If not, get the default value of this variable's type */ |
|
522 param_value = (symbol_c *)param_type->accept(*type_initial_value_c::instance()); |
|
523 } |
|
524 if (param_value == NULL) ERROR; |
|
525 param_value->accept(*this); |
|
526 break; |
|
527 case function_param_iterator_c::direction_out: |
|
528 case function_param_iterator_c::direction_inout: |
|
529 if (param_value == NULL) { |
|
530 /* no parameter value given, so we pass a previously declared temporary variable. */ |
|
531 std::string *temp_var_name = temp_var_name_factory.new_name(); |
|
532 s4o.print(*temp_var_name); |
|
533 delete temp_var_name; |
|
534 } else { |
|
535 param_value->accept(*this); |
|
536 } |
|
537 break; |
|
538 case function_param_iterator_c::direction_extref: |
|
539 /* TODO! */ |
|
540 ERROR; |
|
541 break; |
|
542 } /* switch */ |
|
543 } /* for(...) */ |
|
544 // symbol->parameter_assignment->accept(*this); |
|
545 s4o.print(")"); |
|
546 s4o.indent_left(); |
|
547 } |
|
548 |
|
549 return NULL; |
|
550 } |
|
551 |
|
552 /********************/ |
|
553 /* B 3.2 Statements */ |
|
554 /********************/ |
|
555 void *visit(statement_list_c *symbol) { |
|
556 return print_list(symbol, s4o.indent_spaces, ";\n" + s4o.indent_spaces, ";\n"); |
|
557 } |
|
558 |
|
559 /*********************************/ |
|
560 /* B 3.2.1 Assignment Statements */ |
|
561 /*********************************/ |
|
562 void *visit(assignment_statement_c *symbol) { |
|
563 symbol->l_exp->accept(*this); |
|
564 s4o.print(" = "); |
|
565 symbol->r_exp->accept(*this); |
|
566 return NULL; |
|
567 } |
|
568 |
|
569 /*****************************************/ |
|
570 /* B 3.2.2 Subprogram Control Statements */ |
|
571 /*****************************************/ |
|
572 |
|
573 /* fb_name '(' [param_assignment_list] ')' */ |
|
574 /* param_assignment_list -> may be NULL ! */ |
|
575 //SYM_REF2(fb_invocation_c, fb_name, param_assignment_list) |
|
576 void *visit(fb_invocation_c *symbol) { |
|
577 TRACE("fb_invocation_c"); |
|
578 /* first figure out what is the name of the function block type of the function block being called... */ |
|
579 symbol_c *function_block_type_name = this->search_fb_instance_decl->get_type_name(symbol->fb_name); |
|
580 /* should never occur. The function block instance MUST have been declared... */ |
|
581 if (function_block_type_name == NULL) ERROR; |
|
582 |
|
583 /* Now find the declaration of the function block type being called... */ |
|
584 function_block_declaration_c *fb_decl = function_block_type_symtable.find_value(function_block_type_name); |
|
585 /* should never occur. The function block type being called MUST be in the symtable... */ |
|
586 if (fb_decl == function_block_type_symtable.end_value()) ERROR; |
|
587 |
|
588 /* loop through each function block parameter, find the value we should pass |
|
589 * to it, and then output the c equivalent... |
|
590 */ |
|
591 function_param_iterator_c fp_iterator(fb_decl); |
|
592 identifier_c *param_name; |
|
593 function_call_param_iterator_c function_call_param_iterator(symbol); |
|
594 for(int i = 1; (param_name = fp_iterator.next()) != NULL; i++) { |
|
595 function_param_iterator_c::param_direction_t param_direction = fp_iterator.param_direction(); |
|
596 |
|
597 /* Get the value from a foo(<param_name> = <param_value>) style call */ |
|
598 symbol_c *param_value = function_call_param_iterator.search(param_name); |
|
599 |
|
600 /* Get the value from a foo(<param_value>) style call */ |
|
601 if (param_value == NULL) |
|
602 param_value = function_call_param_iterator.next(); |
|
603 |
|
604 /* now output the value assignment */ |
|
605 if (param_value != NULL) |
|
606 if ((param_direction == function_param_iterator_c::direction_in) || |
|
607 (param_direction == function_param_iterator_c::direction_inout)) { |
|
608 print_variable_prefix(); |
|
609 symbol->fb_name->accept(*this); |
|
610 s4o.print("."); |
|
611 param_name->accept(*this); |
|
612 s4o.print(" = "); |
|
613 param_value->accept(*this); |
|
614 s4o.print(";\n" + s4o.indent_spaces); |
|
615 } |
|
616 } /* for(...) */ |
|
617 |
|
618 /* now call the function... */ |
|
619 function_block_type_name->accept(*this); |
|
620 s4o.print(FB_FUNCTION_SUFFIX); |
|
621 s4o.print("(&"); |
|
622 print_variable_prefix(); |
|
623 symbol->fb_name->accept(*this); |
|
624 s4o.print(")"); |
|
625 |
|
626 /* loop through each function parameter, find the variable to which |
|
627 * we should atribute the value of all output or inoutput parameters. |
|
628 */ |
|
629 fp_iterator.reset(); |
|
630 function_call_param_iterator.reset(); |
|
631 for(int i = 1; (param_name = fp_iterator.next()) != NULL; i++) { |
|
632 function_param_iterator_c::param_direction_t param_direction = fp_iterator.param_direction(); |
|
633 |
|
634 /* Get the value from a foo(<param_name> = <param_value>) style call */ |
|
635 symbol_c *param_value = function_call_param_iterator.search(param_name); |
|
636 |
|
637 /* Get the value from a foo(<param_value>) style call */ |
|
638 if (param_value == NULL) |
|
639 param_value = function_call_param_iterator.next(); |
|
640 |
|
641 /* now output the value assignment */ |
|
642 if (param_value != NULL) |
|
643 if ((param_direction == function_param_iterator_c::direction_out) || |
|
644 (param_direction == function_param_iterator_c::direction_inout)) { |
|
645 s4o.print(";\n"+ s4o.indent_spaces); |
|
646 param_value->accept(*this); |
|
647 s4o.print(" = "); |
|
648 print_variable_prefix(); |
|
649 symbol->fb_name->accept(*this); |
|
650 s4o.print("."); |
|
651 param_name->accept(*this); |
|
652 } |
|
653 } /* for(...) */ |
|
654 |
|
655 return NULL; |
|
656 } |
|
657 |
|
658 |
|
659 |
|
660 |
|
661 /* helper symbol for fb_invocation */ |
|
662 /* param_assignment_list ',' param_assignment */ |
|
663 void *visit(param_assignment_list_c *symbol) { |
|
664 TRACE("param_assignment_list_c"); |
|
665 /* this should never be called... */ |
|
666 ERROR; |
|
667 return NULL; |
|
668 // return print_list(symbol, "", ", "); |
|
669 } |
|
670 |
|
671 |
|
672 void *visit(input_variable_param_assignment_c *symbol) { |
|
673 TRACE("input_variable_param_assignment_c"); |
|
674 /* this should never be called... */ |
|
675 ERROR; |
|
676 return NULL; |
|
677 /* |
|
678 symbol->variable_name->accept(*this); |
|
679 s4o.print(" = "); |
|
680 symbol->expression->accept(*this); |
|
681 return NULL; |
|
682 */ |
|
683 } |
|
684 |
|
685 void *visit(output_variable_param_assignment_c *symbol) { |
|
686 TRACE("output_variable_param_assignment_c"); |
|
687 /* this should never be called... */ |
|
688 ERROR; |
|
689 return NULL; |
|
690 /* |
|
691 s4o.print(s4o.indent_spaces); |
|
692 if (symbol->not_param != NULL) |
|
693 symbol->not_param->accept(*this); |
|
694 symbol->variable_name->accept(*this); |
|
695 s4o.print(" => "); |
|
696 symbol->variable->accept(*this); |
|
697 return NULL; |
|
698 */ |
|
699 } |
|
700 |
|
701 // TODO: the NOT symbol in function (block) calls... |
|
702 void *visit(not_paramassign_c *symbol) { |
|
703 TRACE("not_paramassign_c"); |
|
704 /* this should never be called... */ |
|
705 ERROR; |
|
706 return NULL; |
|
707 /* |
|
708 s4o.print("NOT "); |
|
709 return NULL; |
|
710 */ |
|
711 } |
|
712 |
|
713 |
|
714 /********************************/ |
|
715 /* B 3.2.3 Selection Statements */ |
|
716 /********************************/ |
|
717 void *visit(if_statement_c *symbol) { |
|
718 s4o.print("if ("); |
|
719 symbol->expression->accept(*this); |
|
720 s4o.print(") {\n"); |
|
721 s4o.indent_right(); |
|
722 symbol->statement_list->accept(*this); |
|
723 s4o.indent_left(); |
|
724 symbol->elseif_statement_list->accept(*this); |
|
725 |
|
726 if (symbol->else_statement_list != NULL) { |
|
727 s4o.print(s4o.indent_spaces); s4o.print("} else {\n"); |
|
728 s4o.indent_right(); |
|
729 symbol->else_statement_list->accept(*this); |
|
730 s4o.indent_left(); |
|
731 } |
|
732 s4o.print(s4o.indent_spaces); s4o.print("}"); |
|
733 return NULL; |
|
734 } |
|
735 |
|
736 /* helper symbol for if_statement */ |
|
737 void *visit(elseif_statement_list_c *symbol) {return print_list(symbol);} |
|
738 |
|
739 /* helper symbol for elseif_statement_list */ |
|
740 void *visit(elseif_statement_c *symbol) { |
|
741 s4o.print(s4o.indent_spaces); s4o.print("} else if ("); |
|
742 symbol->expression->accept(*this); |
|
743 s4o.print(") {\n"); |
|
744 s4o.indent_right(); |
|
745 symbol->statement_list->accept(*this); |
|
746 s4o.indent_left(); |
|
747 return NULL; |
|
748 } |
|
749 |
|
750 void *visit(case_statement_c *symbol) { |
|
751 s4o.print("switch("); |
|
752 symbol->expression->accept(*this); |
|
753 s4o.print(") {\n"); |
|
754 s4o.indent_right(); |
|
755 symbol->case_element_list->accept(*this); |
|
756 if (symbol->statement_list != NULL) { |
|
757 s4o.print(s4o.indent_spaces + "default:\n"); |
|
758 s4o.indent_right(); |
|
759 symbol->statement_list->accept(*this); |
|
760 s4o.print(s4o.indent_spaces + "break;\n"); |
|
761 s4o.indent_left(); |
|
762 } |
|
763 s4o.indent_left(); |
|
764 s4o.print(s4o.indent_spaces + "}"); |
|
765 return NULL; |
|
766 } |
|
767 |
|
768 /* helper symbol for case_statement */ |
|
769 void *visit(case_element_list_c *symbol) {return print_list(symbol);} |
|
770 |
|
771 void *visit(case_element_c *symbol) { |
|
772 s4o.print(s4o.indent_spaces + "case "); |
|
773 symbol->case_list->accept(*this); |
|
774 s4o.print(" :\n"); |
|
775 s4o.indent_right(); |
|
776 symbol->statement_list->accept(*this); |
|
777 s4o.print(s4o.indent_spaces + "break;\n"); |
|
778 s4o.indent_left(); |
|
779 return NULL; |
|
780 } |
|
781 |
|
782 void *visit(case_list_c *symbol) {return print_list(symbol, "", ", ");} |
|
783 |
|
784 /********************************/ |
|
785 /* B 3.2.4 Iteration Statements */ |
|
786 /********************************/ |
|
787 void *visit(for_statement_c *symbol) { |
|
788 s4o.print("for("); |
|
789 symbol->control_variable->accept(*this); |
|
790 s4o.print(" = "); |
|
791 symbol->beg_expression->accept(*this); |
|
792 s4o.print("; "); |
|
793 symbol->control_variable->accept(*this); |
|
794 s4o.print(" != "); |
|
795 symbol->end_expression->accept(*this); |
|
796 s4o.print("; "); |
|
797 symbol->control_variable->accept(*this); |
|
798 if (symbol->by_expression != NULL) { |
|
799 s4o.print(" += "); |
|
800 symbol->by_expression->accept(*this); |
|
801 } else { |
|
802 s4o.print("++"); |
|
803 } |
|
804 s4o.print(") {\n"); |
|
805 s4o.indent_right(); |
|
806 symbol->statement_list->accept(*this); |
|
807 s4o.indent_left(); |
|
808 s4o.print(s4o.indent_spaces); s4o.print("}"); |
|
809 return NULL; |
|
810 } |
|
811 void *visit(while_statement_c *symbol) { |
|
812 s4o.print("while ("); |
|
813 symbol->expression->accept(*this); |
|
814 s4o.print(") {\n"); |
|
815 s4o.indent_right(); |
|
816 symbol->statement_list->accept(*this); |
|
817 s4o.indent_left(); |
|
818 s4o.print(s4o.indent_spaces); s4o.print("}"); |
|
819 return NULL; |
|
820 } |
|
821 void *visit(repeat_statement_c *symbol) { |
|
822 s4o.print("do {\n"); |
|
823 s4o.indent_right(); |
|
824 symbol->statement_list->accept(*this); |
|
825 s4o.indent_left(); |
|
826 s4o.print(s4o.indent_spaces); s4o.print("} while("); |
|
827 symbol->expression->accept(*this); |
|
828 s4o.print(")"); |
|
829 return NULL; |
|
830 } |
|
831 void *visit(exit_statement_c *symbol) { |
|
832 s4o.print("exit(0)"); |
|
833 return NULL; |
|
834 } |
|
835 |
|
836 |
|
837 |
|
838 }; /* generate_cc_st_c */ |
|
839 |
|
840 |
|
841 |
|
842 |
|
843 |
|
844 |
|
845 |
|
846 |
|
847 |
|