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