|
1 /* |
|
2 * (c) 2007 Mario de Sousa, Laurent Bessard |
|
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 sfc networks (i.e. SFC code). |
|
28 * |
|
29 * This is part of the 4th stage that generates |
|
30 * a c++ source program equivalent to the SFC, IL and ST |
|
31 * code. |
|
32 */ |
|
33 |
|
34 #include <list> |
|
35 |
|
36 typedef struct |
|
37 { |
|
38 transition_c *symbol; |
|
39 int priority; |
|
40 int index; |
|
41 } TRANSITION; |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 /***********************************************************************/ |
|
48 /***********************************************************************/ |
|
49 /***********************************************************************/ |
|
50 /***********************************************************************/ |
|
51 |
|
52 class generate_c_sfc_elements_c: public generate_c_base_c { |
|
53 |
|
54 public: |
|
55 typedef enum { |
|
56 transitionlist_sg, |
|
57 transitiontest_sg, |
|
58 stepset_sg, |
|
59 stepreset_sg, |
|
60 actionassociation_sg, |
|
61 actionbody_sg |
|
62 } sfcgeneration_t; |
|
63 |
|
64 private: |
|
65 generate_c_il_c *generate_c_il; |
|
66 generate_c_st_c *generate_c_st; |
|
67 generate_c_SFC_IL_ST_c *generate_c_code; |
|
68 search_var_instance_decl_c *search_var_instance_decl; |
|
69 |
|
70 int transition_number; |
|
71 std::list<TRANSITION> transition_list; |
|
72 |
|
73 symbol_c *current_step; |
|
74 symbol_c *current_action; |
|
75 |
|
76 sfcgeneration_t wanted_sfcgeneration; |
|
77 |
|
78 public: |
|
79 generate_c_sfc_elements_c(stage4out_c *s4o_ptr, symbol_c *scope, const char *variable_prefix = NULL) |
|
80 : generate_c_base_c(s4o_ptr) { |
|
81 generate_c_il = new generate_c_il_c(s4o_ptr, scope, variable_prefix); |
|
82 generate_c_st = new generate_c_st_c(s4o_ptr, scope, variable_prefix); |
|
83 generate_c_code = new generate_c_SFC_IL_ST_c(s4o_ptr, scope, variable_prefix); |
|
84 search_var_instance_decl = new search_var_instance_decl_c(scope); |
|
85 this->set_variable_prefix(variable_prefix); |
|
86 } |
|
87 |
|
88 ~generate_c_sfc_elements_c(void) { |
|
89 delete generate_c_il; |
|
90 delete generate_c_st; |
|
91 delete generate_c_code; |
|
92 delete search_var_instance_decl; |
|
93 } |
|
94 |
|
95 void reset_transition_number(void) {transition_number = 0;} |
|
96 |
|
97 void generate(symbol_c *symbol, sfcgeneration_t generation_type) { |
|
98 wanted_sfcgeneration = generation_type; |
|
99 switch (wanted_sfcgeneration) { |
|
100 case transitiontest_sg: |
|
101 { |
|
102 std::list<TRANSITION>::iterator pt; |
|
103 for(pt = transition_list.begin(); pt != transition_list.end(); pt++) { |
|
104 transition_number = pt->index; |
|
105 pt->symbol->accept(*this); |
|
106 } |
|
107 } |
|
108 break; |
|
109 default: |
|
110 symbol->accept(*this); |
|
111 break; |
|
112 } |
|
113 } |
|
114 |
|
115 void print_step_argument(symbol_c *step_name, const char* argument) { |
|
116 print_variable_prefix(); |
|
117 s4o.print("step_list["); |
|
118 s4o.print(SFC_STEP_ACTION_PREFIX); |
|
119 step_name->accept(*this); |
|
120 s4o.print("]."); |
|
121 s4o.print(argument); |
|
122 } |
|
123 |
|
124 void print_action_argument(symbol_c *action_name, const char* argument) { |
|
125 print_variable_prefix(); |
|
126 s4o.print("action_list["); |
|
127 s4o.print(SFC_STEP_ACTION_PREFIX); |
|
128 action_name->accept(*this); |
|
129 s4o.print("]."); |
|
130 s4o.print(argument); |
|
131 } |
|
132 |
|
133 void print_transition_number(void) { |
|
134 char str[10]; |
|
135 sprintf(str, "%d", transition_number); |
|
136 s4o.print(str); |
|
137 } |
|
138 |
|
139 void print_reset_step(symbol_c *step_name) { |
|
140 s4o.print(s4o.indent_spaces); |
|
141 print_step_argument(step_name, "state"); |
|
142 s4o.print(" = 0;\n"); |
|
143 } |
|
144 |
|
145 void print_set_step(symbol_c *step_name) { |
|
146 s4o.print(s4o.indent_spaces); |
|
147 print_step_argument(step_name, "state"); |
|
148 s4o.print(" = 1;\n" + s4o.indent_spaces); |
|
149 print_step_argument(step_name, "elapsed_time"); |
|
150 s4o.print(" = __time_to_timespec(1, 0, 0, 0, 0, 0);\n"); |
|
151 } |
|
152 |
|
153 bool is_variable(symbol_c *symbol) { |
|
154 /* we try to find the variable instance declaration, to determine its type... */ |
|
155 symbol_c *var_decl = search_var_instance_decl->get_decl(symbol); |
|
156 |
|
157 return var_decl != NULL; |
|
158 } |
|
159 |
|
160 /*********************************************/ |
|
161 /* B.1.6 Sequential function chart elements */ |
|
162 /*********************************************/ |
|
163 |
|
164 void *visit(initial_step_c *symbol) { |
|
165 switch (wanted_sfcgeneration) { |
|
166 case actionassociation_sg: |
|
167 if (((list_c*)symbol->action_association_list)->n > 0) { |
|
168 s4o.print(s4o.indent_spaces + "// "); |
|
169 symbol->step_name->accept(*this); |
|
170 s4o.print(" action associations\n"); |
|
171 current_step = symbol->step_name; |
|
172 s4o.print(s4o.indent_spaces + "{\n"); |
|
173 s4o.indent_right(); |
|
174 s4o.print(s4o.indent_spaces + "char activated = "); |
|
175 print_step_argument(current_step, "state"); |
|
176 s4o.print(" && !"); |
|
177 print_step_argument(current_step, "prev_state"); |
|
178 s4o.print(";\n"); |
|
179 s4o.print(s4o.indent_spaces + "char desactivated = !"); |
|
180 print_step_argument(current_step, "state"); |
|
181 s4o.print(" && "); |
|
182 print_step_argument(current_step, "prev_state"); |
|
183 s4o.print(";\n"); |
|
184 s4o.print(s4o.indent_spaces + "char active = "); |
|
185 print_step_argument(current_step, "state"); |
|
186 s4o.print(";\n"); |
|
187 symbol->action_association_list->accept(*this); |
|
188 s4o.indent_left(); |
|
189 s4o.print(s4o.indent_spaces + "}\n\n"); |
|
190 } |
|
191 break; |
|
192 default: |
|
193 break; |
|
194 } |
|
195 return NULL; |
|
196 } |
|
197 |
|
198 void *visit(step_c *symbol) { |
|
199 switch (wanted_sfcgeneration) { |
|
200 case actionassociation_sg: |
|
201 if (((list_c*)symbol->action_association_list)->n > 0) { |
|
202 s4o.print(s4o.indent_spaces + "// "); |
|
203 symbol->step_name->accept(*this); |
|
204 s4o.print(" action associations\n"); |
|
205 current_step = symbol->step_name; |
|
206 s4o.print(s4o.indent_spaces + "{\n"); |
|
207 s4o.indent_right(); |
|
208 s4o.print(s4o.indent_spaces + "char activated = "); |
|
209 print_step_argument(current_step, "state"); |
|
210 s4o.print(" && !"); |
|
211 print_step_argument(current_step, "prev_state"); |
|
212 s4o.print(";\n"); |
|
213 s4o.print(s4o.indent_spaces + "char desactivated = !"); |
|
214 print_step_argument(current_step, "state"); |
|
215 s4o.print(" && "); |
|
216 print_step_argument(current_step, "prev_state"); |
|
217 s4o.print(";\n"); |
|
218 s4o.print(s4o.indent_spaces + "char active = "); |
|
219 print_step_argument(current_step, "state"); |
|
220 s4o.print(";\n"); |
|
221 symbol->action_association_list->accept(*this); |
|
222 s4o.indent_left(); |
|
223 s4o.print(s4o.indent_spaces + "}\n\n"); |
|
224 } |
|
225 break; |
|
226 default: |
|
227 break; |
|
228 } |
|
229 return NULL; |
|
230 } |
|
231 |
|
232 void *visit(transition_c *symbol) { |
|
233 switch (wanted_sfcgeneration) { |
|
234 case transitionlist_sg: |
|
235 { |
|
236 TRANSITION *transition; |
|
237 transition = new TRANSITION; |
|
238 transition->symbol = symbol; |
|
239 transition->index = transition_number; |
|
240 if (symbol->integer != NULL) { |
|
241 transition->priority = atoi(((token_c *)symbol->integer)->value); |
|
242 std::list<TRANSITION>::iterator pt = transition_list.begin(); |
|
243 while (pt != transition_list.end() && pt->priority <= transition->priority) { |
|
244 pt++; |
|
245 } |
|
246 transition_list.insert(pt, *transition); |
|
247 } |
|
248 else { |
|
249 transition->priority = 0; |
|
250 transition_list.push_back(*transition); |
|
251 } |
|
252 transition_number++; |
|
253 } |
|
254 break; |
|
255 case transitiontest_sg: |
|
256 s4o.print(s4o.indent_spaces + "if ("); |
|
257 symbol->from_steps->accept(*this); |
|
258 s4o.print(") {\n"); |
|
259 s4o.indent_right(); |
|
260 |
|
261 // Calculate transition value |
|
262 if (symbol->transition_condition_il != NULL) { |
|
263 generate_c_il->declare_backup_variable(); |
|
264 s4o.print(s4o.indent_spaces); |
|
265 symbol->transition_condition_il->accept(*generate_c_il); |
|
266 print_variable_prefix(); |
|
267 s4o.print("transition_list["); |
|
268 print_transition_number(); |
|
269 s4o.print("] = "); |
|
270 generate_c_il->print_backup_variable(); |
|
271 s4o.print(";\n"); |
|
272 } |
|
273 if (symbol->transition_condition_st != NULL) { |
|
274 s4o.print(s4o.indent_spaces); |
|
275 print_variable_prefix(); |
|
276 s4o.print("transition_list["); |
|
277 print_transition_number(); |
|
278 s4o.print("] = "); |
|
279 symbol->transition_condition_st->accept(*generate_c_st); |
|
280 s4o.print(";\n"); |
|
281 } |
|
282 if (symbol->integer != NULL) { |
|
283 s4o.print(s4o.indent_spaces + "if ("); |
|
284 print_variable_prefix(); |
|
285 s4o.print("transition_list["); |
|
286 print_transition_number(); |
|
287 s4o.print("]) {\n"); |
|
288 s4o.indent_right(); |
|
289 wanted_sfcgeneration = stepreset_sg; |
|
290 symbol->from_steps->accept(*this); |
|
291 wanted_sfcgeneration = transitiontest_sg; |
|
292 s4o.indent_left(); |
|
293 s4o.print(s4o.indent_spaces + "}\n"); |
|
294 } |
|
295 s4o.indent_left(); |
|
296 s4o.print(s4o.indent_spaces + "}\n" + s4o.indent_spaces + "else {\n"); |
|
297 s4o.indent_right(); |
|
298 s4o.print(s4o.indent_spaces); |
|
299 print_variable_prefix(); |
|
300 s4o.print("transition_list["); |
|
301 print_transition_number(); |
|
302 s4o.print("] = 0;\n"); |
|
303 s4o.indent_left(); |
|
304 s4o.print(s4o.indent_spaces + "}\n"); |
|
305 break; |
|
306 case stepset_sg: |
|
307 s4o.print(s4o.indent_spaces + "if ("); |
|
308 print_variable_prefix(); |
|
309 s4o.print("transition_list["); |
|
310 print_transition_number(); |
|
311 s4o.print("]) {\n"); |
|
312 s4o.indent_right(); |
|
313 symbol->to_steps->accept(*this); |
|
314 s4o.indent_left(); |
|
315 s4o.print(s4o.indent_spaces + "}\n"); |
|
316 transition_number++; |
|
317 break; |
|
318 case stepreset_sg: |
|
319 if (symbol->integer == NULL) { |
|
320 s4o.print(s4o.indent_spaces + "if ("); |
|
321 print_variable_prefix(); |
|
322 s4o.print("transition_list["); |
|
323 print_transition_number(); |
|
324 s4o.print("]) {\n"); |
|
325 s4o.indent_right(); |
|
326 symbol->from_steps->accept(*this); |
|
327 s4o.indent_left(); |
|
328 s4o.print(s4o.indent_spaces + "}\n"); |
|
329 } |
|
330 transition_number++; |
|
331 break; |
|
332 default: |
|
333 break; |
|
334 } |
|
335 return NULL; |
|
336 } |
|
337 |
|
338 void *visit(action_c *symbol) { |
|
339 switch (wanted_sfcgeneration) { |
|
340 case actionbody_sg: |
|
341 s4o.print(s4o.indent_spaces + "if("); |
|
342 print_variable_prefix(); |
|
343 s4o.print("action_list["); |
|
344 s4o.print(SFC_STEP_ACTION_PREFIX); |
|
345 symbol->action_name->accept(*this); |
|
346 s4o.print("].state) {"); |
|
347 s4o.indent_right(); |
|
348 |
|
349 // generate action code |
|
350 symbol->function_block_body->accept(*generate_c_code); |
|
351 |
|
352 s4o.indent_left(); |
|
353 s4o.print(s4o.indent_spaces + "}\n\n"); |
|
354 break; |
|
355 default: |
|
356 break; |
|
357 } |
|
358 return NULL; |
|
359 } |
|
360 |
|
361 void *visit(steps_c *symbol) { |
|
362 if (symbol->step_name != NULL) { |
|
363 switch (wanted_sfcgeneration) { |
|
364 case transitiontest_sg: |
|
365 print_step_argument(symbol->step_name, "state"); |
|
366 break; |
|
367 case stepset_sg: |
|
368 print_set_step(symbol->step_name); |
|
369 break; |
|
370 case stepreset_sg: |
|
371 print_reset_step(symbol->step_name); |
|
372 break; |
|
373 default: |
|
374 break; |
|
375 } |
|
376 } |
|
377 else if (symbol->step_name_list != NULL) { |
|
378 symbol->step_name_list->accept(*this); |
|
379 } |
|
380 return NULL; |
|
381 } |
|
382 |
|
383 void *visit(step_name_list_c *symbol) { |
|
384 switch (wanted_sfcgeneration) { |
|
385 case transitiontest_sg: |
|
386 for(int i = 0; i < symbol->n; i++) { |
|
387 print_step_argument(symbol->elements[i], "state"); |
|
388 if (i < symbol->n - 1) { |
|
389 s4o.print(" && "); |
|
390 } |
|
391 } |
|
392 break; |
|
393 case stepset_sg: |
|
394 for(int i = 0; i < symbol->n; i++) { |
|
395 print_set_step(symbol->elements[i]); |
|
396 } |
|
397 break; |
|
398 case stepreset_sg: |
|
399 for(int i = 0; i < symbol->n; i++) { |
|
400 print_reset_step(symbol->elements[i]); |
|
401 } |
|
402 break; |
|
403 default: |
|
404 break; |
|
405 } |
|
406 return NULL; |
|
407 } |
|
408 |
|
409 void *visit(action_association_list_c* symbol) { |
|
410 switch (wanted_sfcgeneration) { |
|
411 case actionassociation_sg: |
|
412 print_list(symbol, "", "\n", "\n"); |
|
413 break; |
|
414 default: |
|
415 break; |
|
416 } |
|
417 return NULL; |
|
418 } |
|
419 |
|
420 void *visit(action_association_c *symbol) { |
|
421 switch (wanted_sfcgeneration) { |
|
422 case actionassociation_sg: |
|
423 if (symbol->action_qualifier != NULL) { |
|
424 current_action = symbol->action_name; |
|
425 symbol->action_qualifier->accept(*this); |
|
426 } |
|
427 else { |
|
428 s4o.print(s4o.indent_spaces + "if ("); |
|
429 print_step_argument(current_step, "state"); |
|
430 s4o.print(") {\n"); |
|
431 s4o.indent_right(); |
|
432 s4o.print(s4o.indent_spaces); |
|
433 print_action_argument(symbol->action_name, "state"); |
|
434 s4o.print(" = 1;\n"); |
|
435 s4o.indent_left(); |
|
436 s4o.print(s4o.indent_spaces + "}"); |
|
437 } |
|
438 break; |
|
439 default: |
|
440 break; |
|
441 } |
|
442 return NULL; |
|
443 } |
|
444 |
|
445 void *visit(action_qualifier_c *symbol) { |
|
446 switch (wanted_sfcgeneration) { |
|
447 case actionassociation_sg: |
|
448 { |
|
449 char *qualifier = (char *)symbol->action_qualifier->accept(*this); |
|
450 s4o.print(s4o.indent_spaces + "if ("); |
|
451 if (strcmp(qualifier, "N") == 0) { |
|
452 s4o.print("active"); |
|
453 } |
|
454 if (strcmp(qualifier, "P") == 0 || strcmp(qualifier, "SD") == 0 || |
|
455 strcmp(qualifier, "DS") == 0 || strcmp(qualifier, "SL") == 0 || |
|
456 strcmp(qualifier, "S") == 0 || strcmp(qualifier, "R") == 0) { |
|
457 s4o.print("activated"); |
|
458 } |
|
459 if (strcmp(qualifier, "D") == 0 || strcmp(qualifier, "L") == 0) { |
|
460 s4o.print("active && "); |
|
461 print_step_argument(current_step, "elapsed_time"); |
|
462 if (strcmp(qualifier, "D") == 0) { |
|
463 s4o.print(" >= "); |
|
464 } |
|
465 else { |
|
466 s4o.print(" < "); |
|
467 } |
|
468 symbol->action_time->accept(*this); |
|
469 } |
|
470 s4o.print(") {\n"); |
|
471 s4o.indent_right(); |
|
472 s4o.print(s4o.indent_spaces); |
|
473 if (strcmp(qualifier, "N") == 0 || strcmp(qualifier, "P") == 0 || |
|
474 strcmp(qualifier, "D") == 0 || strcmp(qualifier, "L") == 0) { |
|
475 print_action_argument(current_action, "state"); |
|
476 s4o.print(" = 1;\n"); |
|
477 } |
|
478 if (strcmp(qualifier, "S") == 0) { |
|
479 if (is_variable(current_action)) { |
|
480 print_variable_prefix(); |
|
481 current_action->accept(*this); |
|
482 } |
|
483 else |
|
484 print_action_argument(current_action, "set"); |
|
485 s4o.print(" = 1;\n"); |
|
486 } |
|
487 if (strcmp(qualifier, "R") == 0) { |
|
488 if (is_variable(current_action)) { |
|
489 print_variable_prefix(); |
|
490 current_action->accept(*this); |
|
491 s4o.print(" = 0;\n"); |
|
492 } |
|
493 else { |
|
494 print_action_argument(current_action, "reset"); |
|
495 s4o.print(" = 1;\n"); |
|
496 } |
|
497 } |
|
498 if (strcmp(qualifier, "SD") == 0 || strcmp(qualifier, "DS") == 0 || |
|
499 strcmp(qualifier, "SL") == 0) { |
|
500 if (strcmp(qualifier, "SL") == 0) { |
|
501 print_action_argument(current_action, "reset_remaining_time"); |
|
502 } |
|
503 else { |
|
504 print_action_argument(current_action, "set_remaining_time"); |
|
505 } |
|
506 s4o.print(" = "); |
|
507 symbol->action_time->accept(*this); |
|
508 s4o.print(";\n"); |
|
509 } |
|
510 s4o.indent_left(); |
|
511 s4o.print(s4o.indent_spaces + "}"); |
|
512 if (strcmp(qualifier, "DS") == 0) { |
|
513 s4o.print("desactivated"); |
|
514 s4o.indent_right(); |
|
515 s4o.print(s4o.indent_spaces); |
|
516 print_action_argument(current_action, "set_remaining_time"); |
|
517 s4o.print(" = __time_to_timespec(1, 0, 0, 0, 0, 0);\n"); |
|
518 } |
|
519 } |
|
520 break; |
|
521 default: |
|
522 break; |
|
523 } |
|
524 return NULL; |
|
525 } |
|
526 |
|
527 void *visit(qualifier_c *symbol) { |
|
528 return (void *)symbol->value; |
|
529 } |
|
530 |
|
531 void *visit(timed_qualifier_c *symbol) { |
|
532 return (void *)symbol->value; |
|
533 } |
|
534 |
|
535 }; /* generate_c_sfc_actiondecl_c */ |
|
536 |
|
537 |
|
538 |
|
539 /***********************************************************************/ |
|
540 /***********************************************************************/ |
|
541 /***********************************************************************/ |
|
542 /***********************************************************************/ |
|
543 |
|
544 class generate_c_sfc_c: public generate_c_typedecl_c { |
|
545 |
|
546 private: |
|
547 generate_c_sfc_elements_c *generate_c_sfc_elements; |
|
548 |
|
549 public: |
|
550 generate_c_sfc_c(stage4out_c *s4o_ptr, symbol_c *scope, const char *variable_prefix = NULL) |
|
551 : generate_c_typedecl_c(s4o_ptr) { |
|
552 generate_c_sfc_elements = new generate_c_sfc_elements_c(s4o_ptr, scope, variable_prefix); |
|
553 this->set_variable_prefix(variable_prefix); |
|
554 } |
|
555 |
|
556 virtual ~generate_c_sfc_c(void) { |
|
557 delete generate_c_sfc_elements; |
|
558 } |
|
559 |
|
560 /*********************************************/ |
|
561 /* B.1.6 Sequential function chart elements */ |
|
562 /*********************************************/ |
|
563 |
|
564 void *visit(sfc_network_c *symbol) { |
|
565 generate_c_sfc_elements->generate((symbol_c *) symbol, generate_c_sfc_elements_c::transitionlist_sg); |
|
566 s4o.print(s4o.indent_spaces +"INT i;\n\n"); |
|
567 s4o.print(s4o.indent_spaces +"BOOL transition;\n\n"); |
|
568 |
|
569 /* generate step initilizations */ |
|
570 s4o.print(s4o.indent_spaces + "// Steps initialisation\n"); |
|
571 s4o.print(s4o.indent_spaces + "for (i = 0; i < "); |
|
572 print_variable_prefix(); |
|
573 s4o.print("nb_steps; i++) {\n"); |
|
574 s4o.indent_right(); |
|
575 s4o.print(s4o.indent_spaces); |
|
576 print_variable_prefix(); |
|
577 s4o.print("step_list[i].prev_state = "); |
|
578 print_variable_prefix(); |
|
579 s4o.print("step_list[i].state;\n"); |
|
580 s4o.print(s4o.indent_spaces + "if ("); |
|
581 print_variable_prefix(); |
|
582 s4o.print("step_list[i].state) {\n"); |
|
583 s4o.indent_right(); |
|
584 s4o.print(s4o.indent_spaces); |
|
585 print_variable_prefix(); |
|
586 s4o.print("step_list[i].elapsed_time = __time_add("); |
|
587 print_variable_prefix(); |
|
588 s4o.print("step_list[i].elapsed_time, "); |
|
589 print_variable_prefix(); |
|
590 s4o.print("period);\n"); |
|
591 s4o.indent_left(); |
|
592 s4o.print(s4o.indent_spaces + "}\n"); |
|
593 s4o.indent_left(); |
|
594 s4o.print(s4o.indent_spaces + "}\n"); |
|
595 |
|
596 /* generate action initilizations */ |
|
597 s4o.print(s4o.indent_spaces + "// Actions initialisation\n"); |
|
598 s4o.print(s4o.indent_spaces + "for (i = 0; i < "); |
|
599 print_variable_prefix(); |
|
600 s4o.print("nb_actions; i++) {\n"); |
|
601 s4o.indent_right(); |
|
602 s4o.print(s4o.indent_spaces); |
|
603 print_variable_prefix(); |
|
604 s4o.print("action_list[i].state = 0;\n"); |
|
605 s4o.print(s4o.indent_spaces); |
|
606 print_variable_prefix(); |
|
607 s4o.print("action_list[i].set = 0;\n"); |
|
608 s4o.print(s4o.indent_spaces); |
|
609 print_variable_prefix(); |
|
610 s4o.print("action_list[i].reset = 0;\n"); |
|
611 s4o.print(s4o.indent_spaces + "if ("); |
|
612 s4o.print("__gt_TIME(2, "); |
|
613 print_variable_prefix(); |
|
614 s4o.print("action_list[i].set_remaining_time, __time_to_timespec(1, 0, 0, 0, 0, 0))) {\n"); |
|
615 s4o.indent_right(); |
|
616 s4o.print(s4o.indent_spaces); |
|
617 print_variable_prefix(); |
|
618 s4o.print("action_list[i].set_remaining_time = __time_sub("); |
|
619 print_variable_prefix(); |
|
620 s4o.print("action_list[i].set_remaining_time, "); |
|
621 print_variable_prefix(); |
|
622 s4o.print("period);\n"); |
|
623 s4o.print(s4o.indent_spaces + "if ("); |
|
624 s4o.print("__le_TIME(2, "); |
|
625 print_variable_prefix(); |
|
626 s4o.print("action_list[i].set_remaining_time, __time_to_timespec(1, 0, 0, 0, 0, 0))) {\n"); |
|
627 s4o.indent_right(); |
|
628 s4o.print(s4o.indent_spaces); |
|
629 print_variable_prefix(); |
|
630 s4o.print("action_list[i].set_remaining_time = __time_to_timespec(1, 0, 0, 0, 0, 0);\n"); |
|
631 s4o.print(s4o.indent_spaces); |
|
632 print_variable_prefix(); |
|
633 s4o.print("action_list[i].set = 1;\n"); |
|
634 s4o.indent_left(); |
|
635 s4o.print(s4o.indent_spaces + "}\n"); |
|
636 s4o.indent_left(); |
|
637 s4o.print(s4o.indent_spaces + "}\n"); |
|
638 s4o.print(s4o.indent_spaces + "if ("); |
|
639 s4o.print("__gt_TIME(2, "); |
|
640 print_variable_prefix(); |
|
641 s4o.print("action_list[i].reset_remaining_time, __time_to_timespec(1, 0, 0, 0, 0, 0))) {\n"); |
|
642 s4o.indent_right(); |
|
643 s4o.print(s4o.indent_spaces); |
|
644 print_variable_prefix(); |
|
645 s4o.print("action_list[i].reset_remaining_time = __time_sub("); |
|
646 print_variable_prefix(); |
|
647 s4o.print("action_list[i].reset_remaining_time, "); |
|
648 print_variable_prefix(); |
|
649 s4o.print("period);\n"); |
|
650 s4o.print(s4o.indent_spaces + "if ("); |
|
651 s4o.print("__le_TIME(2, "); |
|
652 print_variable_prefix(); |
|
653 s4o.print("action_list[i].reset_remaining_time, __time_to_timespec(1, 0, 0, 0, 0, 0))) {\n"); |
|
654 s4o.indent_right(); |
|
655 s4o.print(s4o.indent_spaces); |
|
656 print_variable_prefix(); |
|
657 s4o.print("action_list[i].reset_remaining_time = __time_to_timespec(1, 0, 0, 0, 0, 0);\n"); |
|
658 s4o.print(s4o.indent_spaces); |
|
659 print_variable_prefix(); |
|
660 s4o.print("action_list[i].reset = 1;\n"); |
|
661 s4o.indent_left(); |
|
662 s4o.print(s4o.indent_spaces + "}\n"); |
|
663 s4o.indent_left(); |
|
664 s4o.print(s4o.indent_spaces + "}\n"); |
|
665 s4o.indent_left(); |
|
666 s4o.print(s4o.indent_spaces + "}\n\n"); |
|
667 |
|
668 /* generate transition tests */ |
|
669 s4o.print(s4o.indent_spaces + "// Transitions fire test\n"); |
|
670 generate_c_sfc_elements->generate((symbol_c *) symbol, generate_c_sfc_elements_c::transitiontest_sg); |
|
671 s4o.print("\n"); |
|
672 |
|
673 /* generate transition reset steps */ |
|
674 s4o.print(s4o.indent_spaces + "// Transitions reset steps\n"); |
|
675 generate_c_sfc_elements->reset_transition_number(); |
|
676 generate_c_sfc_elements->generate((symbol_c *) symbol, generate_c_sfc_elements_c::stepreset_sg); |
|
677 s4o.print("\n"); |
|
678 |
|
679 /* generate transition set steps */ |
|
680 s4o.print(s4o.indent_spaces + "// Transitions set steps\n"); |
|
681 generate_c_sfc_elements->reset_transition_number(); |
|
682 generate_c_sfc_elements->generate((symbol_c *) symbol, generate_c_sfc_elements_c::stepset_sg); |
|
683 s4o.print("\n"); |
|
684 |
|
685 /* generate step association */ |
|
686 s4o.print(s4o.indent_spaces + "// Steps association\n"); |
|
687 generate_c_sfc_elements->generate((symbol_c *) symbol, generate_c_sfc_elements_c::actionassociation_sg); |
|
688 s4o.print("\n"); |
|
689 |
|
690 /* generate action state evaluation */ |
|
691 s4o.print(s4o.indent_spaces + "// Actions state evaluation\n"); |
|
692 s4o.print(s4o.indent_spaces + "for (i = 0; i < "); |
|
693 print_variable_prefix(); |
|
694 s4o.print("nb_actions; i++) {\n"); |
|
695 s4o.indent_right(); |
|
696 s4o.print(s4o.indent_spaces + "if ("); |
|
697 print_variable_prefix(); |
|
698 s4o.print("action_list[i].set) {\n"); |
|
699 s4o.indent_right(); |
|
700 s4o.print(s4o.indent_spaces); |
|
701 print_variable_prefix(); |
|
702 s4o.print("action_list[i].stored = 1;\n"); |
|
703 s4o.indent_left(); |
|
704 s4o.print(s4o.indent_spaces + "}\n" + s4o.indent_spaces + "if ("); |
|
705 print_variable_prefix(); |
|
706 s4o.print("action_list[i].reset) {\n"); |
|
707 s4o.indent_right(); |
|
708 s4o.print(s4o.indent_spaces); |
|
709 print_variable_prefix(); |
|
710 s4o.print("action_list[i].set_remaining_time = __time_to_timespec(1, 0, 0, 0, 0, 0);\n" + s4o.indent_spaces); |
|
711 print_variable_prefix(); |
|
712 s4o.print("action_list[i].reset_remaining_time = __time_to_timespec(1, 0, 0, 0, 0, 0);\n" + s4o.indent_spaces); |
|
713 print_variable_prefix(); |
|
714 s4o.print("action_list[i].stored = 0;\n"); |
|
715 s4o.indent_left(); |
|
716 s4o.print(s4o.indent_spaces + "}\n" + s4o.indent_spaces); |
|
717 print_variable_prefix(); |
|
718 s4o.print("action_list[i].state |= "); |
|
719 print_variable_prefix(); |
|
720 s4o.print("action_list[i].stored;\n"); |
|
721 s4o.indent_left(); |
|
722 s4o.print(s4o.indent_spaces + "}\n\n"); |
|
723 |
|
724 /* generate action execution */ |
|
725 s4o.print(s4o.indent_spaces + "// Actions execution\n"); |
|
726 generate_c_sfc_elements->generate((symbol_c *) symbol, generate_c_sfc_elements_c::actionbody_sg); |
|
727 s4o.print("\n"); |
|
728 |
|
729 return NULL; |
|
730 } |
|
731 |
|
732 void generate(sequential_function_chart_c *sfc) { |
|
733 sfc->accept(*this); |
|
734 } |
|
735 |
|
736 }; /* generate_c_sfc_c */ |