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