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 * Function call parameter iterator. |
|
28 * It will iterate through the formal parameters of a function call |
|
29 * (i.e. function calls using the foo(<param1>, <param2>, ...) syntax). |
|
30 * and/or search through the non-formal parameters of a function call |
|
31 * (i.e. function calls using the foo(<name1> = <param1>, <name2> = <param2>, ...) syntax). |
|
32 * |
|
33 * Calls to function blocks and programs are also supported. |
|
34 * |
|
35 * This is part of the 4th stage that generates |
|
36 * a c++ source program equivalent to the IL and ST |
|
37 * code. |
|
38 */ |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 //#include <stdio.h> /* required for NULL */ |
|
46 //#include <string> |
|
47 //#include <iostream> |
|
48 |
|
49 //#include "../../util/symtable.hh" |
|
50 |
|
51 //#include "generate_c.hh" |
|
52 |
|
53 |
|
54 #include "../../absyntax/visitor.hh" |
|
55 |
|
56 |
|
57 /* |
|
58 * Function call parameter iterator. |
|
59 * It will iterate through the formal parameters of a function call |
|
60 * (i.e. function calls using the foo(<param1>, <param2>, ...) syntax). |
|
61 * and/or search through the non-formal parameters of a function call |
|
62 * (i.e. function calls using the foo(<name1> = <param1>, <name2> = <param2>, ...) syntax). |
|
63 * |
|
64 * Calls to function blocks and programs are also supported. |
|
65 * |
|
66 * Note that calls to next() will only iterate through formal parameters, |
|
67 * and calls to search() will only serach through non-formal parameters. |
|
68 */ |
|
69 |
|
70 class function_call_param_iterator_c : public null_visitor_c { |
|
71 private: |
|
72 /* a pointer to the function call |
|
73 * (or function block or program call!) |
|
74 */ |
|
75 symbol_c *f_call; |
|
76 int next_param, param_count; |
|
77 identifier_c *search_param_name; |
|
78 |
|
79 /* Which operation of the class was called... |
|
80 * Search a parameter, or iterate to the next parameter. |
|
81 */ |
|
82 typedef enum {iterate_op, search_op} operation_t; |
|
83 operation_t current_operation; |
|
84 |
|
85 private: |
|
86 void *search_list(list_c *list) { |
|
87 switch (current_operation) { |
|
88 case iterate_op: |
|
89 for(int i = 0; i < list->n; i++) { |
|
90 void *res = list->elements[i]->accept(*this); |
|
91 if (NULL != res) { |
|
92 /* It went through the handle_parameter_assignment() function, |
|
93 * and is therefore a parameter assignment (<param> = <value>), |
|
94 * and not a simple expression (<value>). |
|
95 */ |
|
96 /* we do nothing... */ |
|
97 } else { |
|
98 param_count++; |
|
99 if (param_count == next_param) { |
|
100 return list->elements[i]; |
|
101 } |
|
102 } |
|
103 } |
|
104 return NULL; |
|
105 break; |
|
106 |
|
107 case search_op: |
|
108 for(int i = 0; i < list->n; i++) { |
|
109 void *res = list->elements[i]->accept(*this); |
|
110 if (res != NULL) |
|
111 return res; |
|
112 } |
|
113 return NULL; |
|
114 break; |
|
115 } /* switch */ |
|
116 return NULL; |
|
117 } |
|
118 |
|
119 |
|
120 |
|
121 void *handle_parameter_assignment(symbol_c *variable_name, symbol_c *expression) { |
|
122 switch (current_operation) { |
|
123 case iterate_op: |
|
124 /* UGLY HACK -> this will be detected in the search_list() function */ |
|
125 return (void *)this; /* anything, as long as it is not NULL!! */ |
|
126 break; |
|
127 |
|
128 case search_op: |
|
129 identifier_c *variable_name2 = dynamic_cast<identifier_c *>(variable_name); |
|
130 |
|
131 if (variable_name2 == NULL) { |
|
132 en_param_c *en_param = dynamic_cast<en_param_c *>(variable_name); |
|
133 if (en_param != NULL) |
|
134 variable_name2 = new identifier_c("EN"); |
|
135 } |
|
136 |
|
137 if (variable_name2 == NULL) { |
|
138 eno_param_c *eno_param = dynamic_cast<eno_param_c *>(variable_name); |
|
139 if (eno_param != NULL) |
|
140 variable_name2 = new identifier_c("ENO"); |
|
141 } |
|
142 |
|
143 if (variable_name2 == NULL) ERROR; |
|
144 |
|
145 if (strcasecmp(search_param_name->value, variable_name2->value) == 0) |
|
146 /* FOUND! This is the same parameter!! */ |
|
147 return (void *)expression; |
|
148 return NULL; |
|
149 break; |
|
150 } |
|
151 |
|
152 ERROR; |
|
153 return NULL; |
|
154 } |
|
155 |
|
156 |
|
157 |
|
158 public: |
|
159 /* start off at the first parameter once again... */ |
|
160 void reset(void) { |
|
161 next_param = param_count = 0; |
|
162 } |
|
163 |
|
164 /* initialise the iterator object. |
|
165 * We must be given a reference to the function/program/function block call |
|
166 * that will be analysed... |
|
167 */ |
|
168 function_call_param_iterator_c(symbol_c *f_call) { |
|
169 /* It is expected that f_call will reference one of the following: |
|
170 * program_configuration_c |
|
171 * function_invocation_c |
|
172 * fb_invocation_c |
|
173 * il_function_call_c |
|
174 * il_formal_funct_call_c |
|
175 * ... (have I missed any?) |
|
176 */ |
|
177 this->f_call = f_call; |
|
178 search_param_name = NULL; |
|
179 reset(); |
|
180 } |
|
181 |
|
182 /* Skip to the next parameter. After object creation, |
|
183 * the object references on parameter _before_ the first, so |
|
184 * this function must be called once to get the object to |
|
185 * reference the first parameter... |
|
186 * |
|
187 * Returns whatever is being passed to the parameter! |
|
188 */ |
|
189 symbol_c *next(void) { |
|
190 param_count = 0; |
|
191 next_param++; |
|
192 current_operation = function_call_param_iterator_c::iterate_op; |
|
193 void *res = f_call->accept(*this); |
|
194 return (symbol_c *)res; |
|
195 } |
|
196 |
|
197 /* Search for the value passed to the parameter named <param_name>... */ |
|
198 symbol_c *search(symbol_c *param_name) { |
|
199 if (NULL == param_name) ERROR; |
|
200 search_param_name = dynamic_cast<identifier_c *>(param_name); |
|
201 if (NULL == search_param_name) ERROR; |
|
202 current_operation = function_call_param_iterator_c::search_op; |
|
203 void *res = f_call->accept(*this); |
|
204 return (symbol_c *)res; |
|
205 } |
|
206 |
|
207 |
|
208 |
|
209 |
|
210 private: |
|
211 /********************************/ |
|
212 /* B 1.7 Configuration elements */ |
|
213 /********************************/ |
|
214 |
|
215 /* |
|
216 CONFIGURATION configuration_name |
|
217 optional_global_var_declarations |
|
218 (resource_declaration_list | single_resource_declaration) |
|
219 optional_access_declarations |
|
220 optional_instance_specific_initializations |
|
221 END_CONFIGURATION |
|
222 */ |
|
223 /* |
|
224 SYM_REF6(configuration_declaration_c, configuration_name, global_var_declarations, resource_declarations, access_declarations, instance_specific_initializations, unused) |
|
225 */ |
|
226 |
|
227 /* helper symbol for configuration_declaration */ |
|
228 /* |
|
229 SYM_LIST(resource_declaration_list_c) |
|
230 */ |
|
231 |
|
232 /* |
|
233 RESOURCE resource_name ON resource_type_name |
|
234 optional_global_var_declarations |
|
235 single_resource_declaration |
|
236 END_RESOURCE |
|
237 */ |
|
238 /* |
|
239 SYM_REF4(resource_declaration_c, resource_name, resource_type_name, global_var_declarations, resource_declaration) |
|
240 */ |
|
241 |
|
242 /* task_configuration_list program_configuration_list */ |
|
243 /* |
|
244 SYM_REF2(single_resource_declaration_c, task_configuration_list, program_configuration_list) |
|
245 */ |
|
246 |
|
247 /* helper symbol for single_resource_declaration */ |
|
248 /* |
|
249 SYM_LIST(task_configuration_list_c) |
|
250 */ |
|
251 |
|
252 /* helper symbol for single_resource_declaration */ |
|
253 /* |
|
254 SYM_LIST(program_configuration_list_c) |
|
255 */ |
|
256 |
|
257 /* helper symbol for |
|
258 * - access_path |
|
259 * - instance_specific_init |
|
260 */ |
|
261 /* |
|
262 SYM_LIST(any_fb_name_list_c) |
|
263 */ |
|
264 |
|
265 /* [resource_name '.'] global_var_name ['.' structure_element_name] */ |
|
266 /* |
|
267 SYM_REF4(global_var_reference_c, resource_name, global_var_name, structure_element_name, unused) |
|
268 */ |
|
269 |
|
270 /* prev_declared_program_name '.' symbolic_variable */ |
|
271 /* |
|
272 SYM_REF2(program_output_reference_c, program_name, symbolic_variable) |
|
273 */ |
|
274 |
|
275 /* TASK task_name task_initialization */ |
|
276 /* |
|
277 SYM_REF2(task_configuration_c, task_name, task_initialization) |
|
278 */ |
|
279 |
|
280 /* '(' [SINGLE ASSIGN data_source ','] [INTERVAL ASSIGN data_source ','] PRIORITY ASSIGN integer ')' */ |
|
281 /* |
|
282 SYM_REF4(task_initialization_c, single_data_source, interval_data_source, priority_data_source, unused) |
|
283 */ |
|
284 |
|
285 /* PROGRAM [RETAIN | NON_RETAIN] program_name [WITH task_name] ':' program_type_name ['(' prog_conf_elements ')'] */ |
|
286 // SYM_REF6(program_configuration_c, retain_option, program_name, task_name, program_type_name, prog_conf_elements, unused) |
|
287 void *visit(program_configuration_c *symbol) { |
|
288 TRACE("program_configuration_c"); |
|
289 return symbol->prog_conf_elements->accept(*this); |
|
290 } |
|
291 |
|
292 /* prog_conf_elements ',' prog_conf_element */ |
|
293 // SYM_LIST(prog_conf_elements_c) |
|
294 void *visit(prog_conf_elements_c *symbol) { |
|
295 TRACE("prog_conf_elements_c"); |
|
296 return search_list(symbol); |
|
297 } |
|
298 |
|
299 /* fb_name WITH task_name */ |
|
300 /* |
|
301 SYM_REF2(fb_task_c, fb_name, task_name) |
|
302 */ |
|
303 |
|
304 /* any_symbolic_variable ASSIGN prog_data_source */ |
|
305 // SYM_REF2(prog_cnxn_assign_c, symbolic_variable, prog_data_source) |
|
306 void *visit(prog_cnxn_assign_c *symbol) { |
|
307 TRACE("prog_cnxn_assign_c"); |
|
308 |
|
309 /* NOTE: symbolic_variable may be something other than a symbolic_variable_c, but I (Mario) |
|
310 * do not understand the semantics that should be implmeneted if it is not a |
|
311 * symbolic_variable, so for the moment we simply give up! |
|
312 */ |
|
313 symbolic_variable_c *symb_var = dynamic_cast<symbolic_variable_c *>(symbol->symbolic_variable); |
|
314 if (NULL == symb_var) |
|
315 ERROR; |
|
316 |
|
317 return handle_parameter_assignment(symb_var->var_name, symbol->prog_data_source); |
|
318 } |
|
319 |
|
320 /* any_symbolic_variable SENDTO data_sink */ |
|
321 // SYM_REF2(prog_cnxn_sendto_c, symbolic_variable, prog_data_source) |
|
322 void *visit(prog_cnxn_sendto_c *symbol) { |
|
323 TRACE("prog_cnxn_sendto_c"); |
|
324 |
|
325 /* NOTE: symbolic_variable may be something other than a symbolic_variable_c, but I (Mario) |
|
326 * do not understand the semantics that should be implmeneted if it is not a |
|
327 * symbolic_variable, so for the moment we simply give up! |
|
328 */ |
|
329 symbolic_variable_c *symb_var = dynamic_cast<symbolic_variable_c *>(symbol->symbolic_variable); |
|
330 if (NULL == symb_var) |
|
331 ERROR; |
|
332 |
|
333 return handle_parameter_assignment(symb_var->var_name, symbol->data_sink); |
|
334 } |
|
335 |
|
336 /* VAR_CONFIG instance_specific_init_list END_VAR */ |
|
337 /* |
|
338 SYM_REF2(instance_specific_initializations_c, instance_specific_init_list, unused) |
|
339 */ |
|
340 |
|
341 /* helper symbol for instance_specific_initializations */ |
|
342 /* |
|
343 SYM_LIST(instance_specific_init_list_c) |
|
344 */ |
|
345 |
|
346 /* resource_name '.' program_name '.' {fb_name '.'} |
|
347 ((variable_name [location] ':' located_var_spec_init) | (fb_name ':' fb_initialization)) |
|
348 */ |
|
349 /* |
|
350 SYM_REF6(instance_specific_init_c, resource_name, program_name, any_fb_name_list, variable_name, location, initialization) |
|
351 */ |
|
352 |
|
353 /* helper symbol for instance_specific_init */ |
|
354 /* function_block_type_name ':=' structure_initialization */ |
|
355 /* |
|
356 SYM_REF2(fb_initialization_c, function_block_type_name, structure_initialization) |
|
357 */ |
|
358 |
|
359 |
|
360 |
|
361 |
|
362 |
|
363 |
|
364 /****************************************/ |
|
365 /* B.2 - Language IL (Instruction List) */ |
|
366 /****************************************/ |
|
367 /***********************************/ |
|
368 /* B 2.1 Instructions and Operands */ |
|
369 /***********************************/ |
|
370 |
|
371 /* | function_name [il_operand_list] */ |
|
372 // SYM_REF2(il_function_call_c, function_name, il_operand_list) |
|
373 void *visit(il_function_call_c *symbol) { |
|
374 TRACE("il_function_call_c"); |
|
375 if (NULL != symbol->il_operand_list) |
|
376 return symbol->il_operand_list->accept(*this); |
|
377 return NULL; |
|
378 } |
|
379 |
|
380 |
|
381 /* | function_name '(' eol_list [il_param_list] ')' */ |
|
382 // SYM_REF2(il_formal_funct_call_c, function_name, il_param_list) |
|
383 void *visit(il_formal_funct_call_c *symbol) { |
|
384 TRACE("il_formal_funct_call_c"); |
|
385 if (NULL != symbol->il_param_list) |
|
386 return symbol->il_param_list->accept(*this); |
|
387 return NULL; |
|
388 } |
|
389 |
|
390 |
|
391 /* il_call_operator prev_declared_fb_name |
|
392 * | il_call_operator prev_declared_fb_name '(' ')' |
|
393 * | il_call_operator prev_declared_fb_name '(' eol_list ')' |
|
394 * | il_call_operator prev_declared_fb_name '(' il_operand_list ')' |
|
395 * | il_call_operator prev_declared_fb_name '(' eol_list il_param_list ')' |
|
396 */ |
|
397 // SYM_REF4(il_fb_call_c, il_call_operator, fb_name, il_operand_list, il_param_list) |
|
398 void *visit(il_fb_call_c *symbol) { |
|
399 TRACE("il_fb_call_c"); |
|
400 /* the following should never occur. In reality the syntax parser |
|
401 * will guarantee that they never occur, but it makes it easier to |
|
402 * understand the remaining code :-) |
|
403 */ |
|
404 //if ((NULL == symbol->il_operand_list) && (NULL == symbol->il_param_list)) ERROR; |
|
405 //if ((NULL != symbol->il_operand_list) && (NULL != symbol->il_param_list)) ERROR; |
|
406 |
|
407 if (NULL != symbol->il_operand_list) |
|
408 return symbol->il_operand_list->accept(*this); |
|
409 if (NULL != symbol->il_param_list) |
|
410 return symbol->il_param_list->accept(*this); |
|
411 return NULL; |
|
412 } |
|
413 |
|
414 |
|
415 |
|
416 /* | il_operand_list ',' il_operand */ |
|
417 // SYM_LIST(il_operand_list_c) |
|
418 void *visit(il_operand_list_c *symbol) { |
|
419 TRACE("il_operand_list_c"); |
|
420 return search_list(symbol); |
|
421 } |
|
422 |
|
423 |
|
424 /* | il_initial_param_list il_param_instruction */ |
|
425 // SYM_LIST(il_param_list_c) |
|
426 void *visit(il_param_list_c *symbol) { |
|
427 TRACE("il_param_list_c"); |
|
428 return search_list(symbol); |
|
429 } |
|
430 |
|
431 /* il_assign_operator il_operand |
|
432 * | il_assign_operator '(' eol_list simple_instr_list ')' |
|
433 */ |
|
434 // SYM_REF4(il_param_assignment_c, il_assign_operator, il_operand, simple_instr_list, unused) |
|
435 void *visit(il_param_assignment_c *symbol) { |
|
436 TRACE("il_param_assignment_c"); |
|
437 |
|
438 // TODO : We do not yet handle a instruction list passed as parameter !!! |
|
439 // since we do not yet support it, it is best to simply stop than to fail silently... |
|
440 if (NULL != symbol->simple_instr_list) ERROR; |
|
441 |
|
442 return handle_parameter_assignment(symbol->il_assign_operator, symbol->il_operand); |
|
443 } |
|
444 |
|
445 /* il_assign_out_operator variable */ |
|
446 // SYM_REF2(il_param_out_assignment_c, il_assign_out_operator, variable); |
|
447 void *visit(il_param_out_assignment_c *symbol) { |
|
448 TRACE("il_param_out_assignment_c"); |
|
449 return handle_parameter_assignment((symbol_c *)symbol->il_assign_out_operator->accept(*this), symbol->variable); |
|
450 } |
|
451 |
|
452 |
|
453 /*******************/ |
|
454 /* B 2.2 Operators */ |
|
455 /*******************/ |
|
456 /*| [NOT] any_identifier SENDTO */ |
|
457 // SYM_REF2(il_assign_out_operator_c, option, variable_name) |
|
458 void *visit(il_assign_out_operator_c *symbol) { |
|
459 TRACE("il_assign_out_operator_c"); |
|
460 |
|
461 // TODO : Handle not_param !!! |
|
462 // we do not yet support it, so it is best to simply stop than to fail silently... |
|
463 if (NULL != symbol->option) ERROR; |
|
464 |
|
465 return (void *)symbol->variable_name; |
|
466 } |
|
467 |
|
468 |
|
469 |
|
470 |
|
471 /***************************************/ |
|
472 /* B.3 - Language ST (Structured Text) */ |
|
473 /***************************************/ |
|
474 /***********************/ |
|
475 /* B 3.1 - Expressions */ |
|
476 /***********************/ |
|
477 |
|
478 /* |
|
479 SYM_REF2(function_invocation_c, function_name, parameter_assignment_list) |
|
480 */ |
|
481 void *visit(function_invocation_c *symbol) { |
|
482 TRACE("function_invocation_c"); |
|
483 if ((symbol_c *)symbol == f_call && symbol->parameter_assignment_list != NULL) |
|
484 return symbol->parameter_assignment_list->accept(*this); |
|
485 else |
|
486 return NULL; |
|
487 } |
|
488 |
|
489 |
|
490 |
|
491 |
|
492 /********************/ |
|
493 /* B 3.2 Statements */ |
|
494 /********************/ |
|
495 |
|
496 /*********************************/ |
|
497 /* B 3.2.1 Assignment Statements */ |
|
498 /*********************************/ |
|
499 /* |
|
500 SYM_REF2(assignment_statement_c, l_exp, r_exp) |
|
501 */ |
|
502 |
|
503 /*****************************************/ |
|
504 /* B 3.2.2 Subprogram Control Statements */ |
|
505 /*****************************************/ |
|
506 /* RETURN */ |
|
507 // SYM_REF0(return_statement_c) |
|
508 |
|
509 |
|
510 /* fb_name '(' [param_assignment_list] ')' */ |
|
511 /* param_assignment_list -> may be NULL ! */ |
|
512 // SYM_REF2(fb_invocation_c, fb_name, param_assignment_list) |
|
513 void *visit(fb_invocation_c *symbol) { |
|
514 TRACE("fb_invocation_c"); |
|
515 if (symbol->param_assignment_list != NULL) |
|
516 return symbol->param_assignment_list->accept(*this); |
|
517 else |
|
518 return NULL; |
|
519 } |
|
520 |
|
521 /* helper symbol for fb_invocation */ |
|
522 /* param_assignment_list ',' param_assignment */ |
|
523 // SYM_LIST(param_assignment_list_c) |
|
524 void *visit(param_assignment_list_c *symbol) { |
|
525 TRACE("param_assignment_list_c"); |
|
526 return search_list(symbol); |
|
527 } |
|
528 |
|
529 /* variable_name ASSIGN expression */ |
|
530 // SYM_REF2(input_variable_param_assignment_c, variable_name, expression) |
|
531 void *visit(input_variable_param_assignment_c *symbol) { |
|
532 TRACE("input_variable_param_assignment_c"); |
|
533 return handle_parameter_assignment(symbol->variable_name, symbol->expression); |
|
534 } |
|
535 |
|
536 /* [NOT] variable_name '=>' variable */ |
|
537 // SYM_REF4(output_variable_param_assignment_c, not_param, variable_name, variable, unused) |
|
538 void *visit(output_variable_param_assignment_c *symbol) { |
|
539 TRACE("output_variable_param_assignment_c"); |
|
540 // TODO : Handle not_param !!! |
|
541 if (NULL != symbol->not_param) ERROR; // we do not yet support it, so it is best to simply stop than to fail silently... |
|
542 |
|
543 return handle_parameter_assignment(symbol->variable_name, symbol->variable); |
|
544 } |
|
545 |
|
546 /* helper CLASS for output_variable_param_assignment */ |
|
547 // SYM_REF0(not_paramassign_c) |
|
548 // TODO... ??? |
|
549 |
|
550 |
|
551 |
|
552 |
|
553 |
|
554 }; |
|
555 |
|
556 |
|
557 |
|
558 |
|
559 |
|
560 |
|