author | laurent |
Thu, 22 Apr 2010 00:29:30 +0200 | |
changeset 239 | b3063ca418c1 |
parent 229 | ca7bc1324540 |
child 244 | 3a478229db8b |
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 |
||
35 |
||
36 |
||
37 |
/***********************************************************************/ |
|
38 |
/***********************************************************************/ |
|
39 |
/***********************************************************************/ |
|
40 |
/***********************************************************************/ |
|
41 |
||
42 |
class generate_c_sfcdecl_c: protected generate_c_typedecl_c { |
|
43 |
||
44 |
public: |
|
45 |
typedef enum { |
|
46 |
sfcdecl_sd, |
|
47 |
sfcinit_sd, |
|
48 |
stepcount_sd, |
|
49 |
stepdef_sd, |
|
50 |
stepundef_sd, |
|
51 |
actiondef_sd, |
|
52 |
actionundef_sd, |
|
229 | 53 |
actioncount_sd, |
54 |
transitioncount_sd |
|
70 | 55 |
} sfcdeclaration_t; |
56 |
||
57 |
private: |
|
58 |
char step_number; |
|
59 |
char action_number; |
|
60 |
char transition_number; |
|
61 |
||
62 |
sfcdeclaration_t wanted_sfcdeclaration; |
|
63 |
||
64 |
public: |
|
65 |
generate_c_sfcdecl_c(stage4out_c *s4o_ptr, sfcdeclaration_t sfcdeclaration) |
|
66 |
: generate_c_typedecl_c(s4o_ptr) { |
|
67 |
wanted_sfcdeclaration = sfcdeclaration; |
|
68 |
} |
|
69 |
~generate_c_sfcdecl_c(void) {} |
|
70 |
||
71 |
void print(symbol_c *symbol, const char *variable_prefix = NULL) { |
|
72 |
this->set_variable_prefix(variable_prefix); |
|
73 |
||
74 |
symbol->accept(*this); |
|
75 |
} |
|
76 |
||
77 |
/*********************************************/ |
|
78 |
/* B.1.6 Sequential function chart elements */ |
|
79 |
/*********************************************/ |
|
80 |
||
81 |
void *visit(sequential_function_chart_c *symbol) { |
|
82 |
step_number = 0; |
|
83 |
action_number = 0; |
|
84 |
transition_number = 0; |
|
85 |
switch (wanted_sfcdeclaration) { |
|
86 |
case sfcdecl_sd: |
|
87 |
for(int i = 0; i < symbol->n; i++) |
|
88 |
symbol->elements[i]->accept(*this); |
|
89 |
||
90 |
/* steps table declaration */ |
|
141 | 91 |
s4o.print(s4o.indent_spaces + "STEP __step_list["); |
70 | 92 |
s4o.print_integer(step_number); |
93 |
s4o.print("];\n"); |
|
141 | 94 |
s4o.print(s4o.indent_spaces + "UINT __nb_steps;\n"); |
70 | 95 |
|
96 |
/* actions table declaration */ |
|
141 | 97 |
s4o.print(s4o.indent_spaces + "ACTION __action_list["); |
70 | 98 |
s4o.print_integer(action_number); |
99 |
s4o.print("];\n"); |
|
141 | 100 |
s4o.print(s4o.indent_spaces + "UINT __nb_actions;\n"); |
70 | 101 |
|
102 |
/* transitions table declaration */ |
|
221
c6aed7e5f070
Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents:
160
diff
changeset
|
103 |
s4o.print(s4o.indent_spaces + "__IEC_BOOL_t __transition_list["); |
70 | 104 |
s4o.print_integer(transition_number); |
105 |
s4o.print("];\n"); |
|
106 |
||
141 | 107 |
/* transitions debug table declaration */ |
221
c6aed7e5f070
Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents:
160
diff
changeset
|
108 |
s4o.print(s4o.indent_spaces + "__IEC_BOOL_t __debug_transition_list["); |
141 | 109 |
s4o.print_integer(transition_number); |
110 |
s4o.print("];\n"); |
|
229 | 111 |
s4o.print(s4o.indent_spaces + "UINT __nb_transitions;\n"); |
141 | 112 |
|
125
7ec16536bc87
Modifying time qualifier in SFC to take real time enlapsed instead of theoric period time
lbessard
parents:
120
diff
changeset
|
113 |
/* last_ticktime declaration */ |
141 | 114 |
s4o.print(s4o.indent_spaces + "TIME __lasttick_time;\n"); |
70 | 115 |
break; |
116 |
case sfcinit_sd: |
|
117 |
s4o.print(s4o.indent_spaces); |
|
118 |
s4o.print("UINT i;\n"); |
|
119 |
||
120 |
/* steps table count */ |
|
121 |
wanted_sfcdeclaration = stepcount_sd; |
|
122 |
for(int i = 0; i < symbol->n; i++) |
|
123 |
symbol->elements[i]->accept(*this); |
|
124 |
s4o.print(s4o.indent_spaces); |
|
125 |
print_variable_prefix(); |
|
141 | 126 |
s4o.print("__nb_steps = "); |
70 | 127 |
s4o.print_integer(step_number); |
128 |
s4o.print(";\n"); |
|
129 |
step_number = 0; |
|
130 |
wanted_sfcdeclaration = sfcinit_sd; |
|
131 |
||
132 |
/* steps table initialisation */ |
|
221
c6aed7e5f070
Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents:
160
diff
changeset
|
133 |
s4o.print(s4o.indent_spaces + "static const STEP temp_step = {{0, 0}, 0, 0};\n"); |
70 | 134 |
s4o.print(s4o.indent_spaces + "for(i = 0; i < "); |
135 |
print_variable_prefix(); |
|
141 | 136 |
s4o.print("__nb_steps; i++) {\n"); |
70 | 137 |
s4o.indent_right(); |
138 |
s4o.print(s4o.indent_spaces); |
|
139 |
print_variable_prefix(); |
|
141 | 140 |
s4o.print("__step_list[i] = temp_step;\n"); |
70 | 141 |
s4o.indent_left(); |
142 |
s4o.print(s4o.indent_spaces + "}\n"); |
|
143 |
for(int i = 0; i < symbol->n; i++) |
|
144 |
symbol->elements[i]->accept(*this); |
|
145 |
||
146 |
/* steps table count */ |
|
147 |
wanted_sfcdeclaration = actioncount_sd; |
|
148 |
for(int i = 0; i < symbol->n; i++) |
|
149 |
symbol->elements[i]->accept(*this); |
|
150 |
s4o.print(s4o.indent_spaces); |
|
151 |
print_variable_prefix(); |
|
141 | 152 |
s4o.print("__nb_actions = "); |
70 | 153 |
s4o.print_integer(action_number); |
154 |
s4o.print(";\n"); |
|
155 |
action_number = 0; |
|
156 |
wanted_sfcdeclaration = sfcinit_sd; |
|
157 |
||
158 |
/* actions table initialisation */ |
|
221
c6aed7e5f070
Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents:
160
diff
changeset
|
159 |
s4o.print(s4o.indent_spaces + "static const ACTION temp_action = {0, 0, 0, 0, 0, 0};\n"); |
70 | 160 |
s4o.print(s4o.indent_spaces + "for(i = 0; i < "); |
161 |
print_variable_prefix(); |
|
141 | 162 |
s4o.print("__nb_actions; i++) {\n"); |
70 | 163 |
s4o.indent_right(); |
164 |
s4o.print(s4o.indent_spaces); |
|
165 |
print_variable_prefix(); |
|
141 | 166 |
s4o.print("__action_list[i] = temp_action;\n"); |
70 | 167 |
s4o.indent_left(); |
168 |
s4o.print(s4o.indent_spaces + "}\n"); |
|
125
7ec16536bc87
Modifying time qualifier in SFC to take real time enlapsed instead of theoric period time
lbessard
parents:
120
diff
changeset
|
169 |
|
229 | 170 |
/* transitions table count */ |
171 |
wanted_sfcdeclaration = transitioncount_sd; |
|
172 |
for(int i = 0; i < symbol->n; i++) |
|
173 |
symbol->elements[i]->accept(*this); |
|
174 |
s4o.print(s4o.indent_spaces); |
|
175 |
print_variable_prefix(); |
|
176 |
s4o.print("__nb_transitions = "); |
|
177 |
s4o.print_integer(transition_number); |
|
178 |
s4o.print(";\n"); |
|
179 |
transition_number = 0; |
|
180 |
wanted_sfcdeclaration = sfcinit_sd; |
|
181 |
||
125
7ec16536bc87
Modifying time qualifier in SFC to take real time enlapsed instead of theoric period time
lbessard
parents:
120
diff
changeset
|
182 |
/* last_ticktime initialisation */ |
160 | 183 |
s4o.print(s4o.indent_spaces); |
125
7ec16536bc87
Modifying time qualifier in SFC to take real time enlapsed instead of theoric period time
lbessard
parents:
120
diff
changeset
|
184 |
print_variable_prefix(); |
141 | 185 |
s4o.print("__lasttick_time = __CURRENT_TIME;\n"); |
70 | 186 |
break; |
187 |
case stepdef_sd: |
|
188 |
s4o.print("// Steps definitions\n"); |
|
189 |
for(int i = 0; i < symbol->n; i++) |
|
190 |
symbol->elements[i]->accept(*this); |
|
191 |
s4o.print("\n"); |
|
192 |
break; |
|
193 |
case actiondef_sd: |
|
194 |
s4o.print("// Actions definitions\n"); |
|
195 |
for(int i = 0; i < symbol->n; i++) |
|
196 |
symbol->elements[i]->accept(*this); |
|
197 |
s4o.print("\n"); |
|
198 |
break; |
|
199 |
case stepundef_sd: |
|
200 |
s4o.print("// Steps undefinitions\n"); |
|
201 |
for(int i = 0; i < symbol->n; i++) |
|
202 |
symbol->elements[i]->accept(*this); |
|
203 |
s4o.print("\n"); |
|
204 |
break; |
|
205 |
case actionundef_sd: |
|
206 |
s4o.print("// Actions undefinitions\n"); |
|
207 |
for(int i = 0; i < symbol->n; i++) |
|
208 |
symbol->elements[i]->accept(*this); |
|
209 |
s4o.print("\n"); |
|
210 |
break; |
|
211 |
default: |
|
212 |
break; |
|
213 |
} |
|
214 |
return NULL; |
|
215 |
} |
|
216 |
||
217 |
void *visit(initial_step_c *symbol) { |
|
218 |
switch (wanted_sfcdeclaration) { |
|
219 |
case stepcount_sd: |
|
220 |
case sfcdecl_sd: |
|
221 |
step_number++; |
|
222 |
break; |
|
223 |
case sfcinit_sd: |
|
224 |
s4o.print(s4o.indent_spaces); |
|
221
c6aed7e5f070
Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents:
160
diff
changeset
|
225 |
s4o.print(SET_VAR); |
c6aed7e5f070
Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents:
160
diff
changeset
|
226 |
s4o.print("("); |
70 | 227 |
print_variable_prefix(); |
141 | 228 |
s4o.print("__step_list["); |
70 | 229 |
s4o.print_integer(step_number); |
221
c6aed7e5f070
Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents:
160
diff
changeset
|
230 |
s4o.print("].state,1);\n"); |
70 | 231 |
step_number++; |
232 |
break; |
|
233 |
case stepdef_sd: |
|
234 |
s4o.print("#define "); |
|
235 |
s4o.print(SFC_STEP_ACTION_PREFIX); |
|
236 |
symbol->step_name->accept(*this); |
|
237 |
s4o.print(" "); |
|
238 |
s4o.print_integer(step_number); |
|
239 |
s4o.print("\n"); |
|
240 |
step_number++; |
|
241 |
break; |
|
242 |
case stepundef_sd: |
|
243 |
s4o.print("#undef "); |
|
244 |
s4o.print(SFC_STEP_ACTION_PREFIX); |
|
245 |
symbol->step_name->accept(*this); |
|
246 |
s4o.print("\n"); |
|
247 |
break; |
|
248 |
default: |
|
249 |
break; |
|
250 |
} |
|
251 |
return NULL; |
|
252 |
} |
|
253 |
||
254 |
void *visit(step_c *symbol) { |
|
255 |
switch (wanted_sfcdeclaration) { |
|
256 |
case stepcount_sd: |
|
257 |
case sfcdecl_sd: |
|
119
4cbf71106b66
Bug with C code generation for SFC when more than one sfc_network in one sequential_function_chart fixed
lbessard
parents:
116
diff
changeset
|
258 |
case sfcinit_sd: |
70 | 259 |
step_number++; |
260 |
break; |
|
261 |
case stepdef_sd: |
|
262 |
s4o.print("#define "); |
|
263 |
s4o.print(SFC_STEP_ACTION_PREFIX); |
|
264 |
symbol->step_name->accept(*this); |
|
265 |
s4o.print(" "); |
|
266 |
s4o.print_integer(step_number); |
|
267 |
s4o.print("\n"); |
|
268 |
step_number++; |
|
269 |
break; |
|
270 |
case stepundef_sd: |
|
271 |
s4o.print("#undef "); |
|
272 |
s4o.print(SFC_STEP_ACTION_PREFIX); |
|
273 |
symbol->step_name->accept(*this); |
|
274 |
s4o.print("\n"); |
|
275 |
break; |
|
276 |
default: |
|
277 |
break; |
|
278 |
} |
|
279 |
return NULL; |
|
280 |
} |
|
281 |
||
282 |
void *visit(transition_c *symbol) { |
|
283 |
switch (wanted_sfcdeclaration) { |
|
284 |
case sfcdecl_sd: |
|
229 | 285 |
case transitioncount_sd: |
70 | 286 |
transition_number++; |
287 |
break; |
|
288 |
default: |
|
289 |
break; |
|
290 |
} |
|
291 |
return NULL; |
|
292 |
} |
|
293 |
||
294 |
void *visit(action_c *symbol) { |
|
295 |
switch (wanted_sfcdeclaration) { |
|
296 |
case actiondef_sd: |
|
297 |
s4o.print("#define "); |
|
298 |
s4o.print(SFC_STEP_ACTION_PREFIX); |
|
299 |
symbol->action_name->accept(*this); |
|
300 |
s4o.print(" "); |
|
301 |
s4o.print_integer(action_number); |
|
302 |
s4o.print("\n"); |
|
303 |
action_number++; |
|
304 |
break; |
|
305 |
case actionundef_sd: |
|
306 |
s4o.print("#undef "); |
|
307 |
s4o.print(SFC_STEP_ACTION_PREFIX); |
|
308 |
symbol->action_name->accept(*this); |
|
309 |
s4o.print("\n"); |
|
310 |
break; |
|
311 |
case actioncount_sd: |
|
312 |
case sfcdecl_sd: |
|
313 |
action_number++; |
|
314 |
break; |
|
315 |
default: |
|
316 |
break; |
|
317 |
} |
|
318 |
return NULL; |
|
319 |
} |
|
320 |
||
321 |
void *visit(instruction_list_c *symbol) { |
|
322 |
return NULL; |
|
323 |
} |
|
324 |
||
325 |
void *visit(statement_list_c *symbol) { |
|
326 |
return NULL; |
|
327 |
} |
|
328 |
||
329 |
}; /* generate_c_sfcdecl_c */ |
|
330 |