author | lbessard |
Wed, 17 Oct 2007 17:51:24 +0200 | |
changeset 66 | 501e9d494744 |
parent 56 | 6e87bbc2abe9 |
permissions | -rw-r--r-- |
17
38754701ac41
SFC state machine first approach generator implemented
lbessard
parents:
diff
changeset
|
1 |
/* |
38754701ac41
SFC state machine first approach generator implemented
lbessard
parents:
diff
changeset
|
2 |
* (c) 2007 Mario de Sousa, Laurent Bessard |
38754701ac41
SFC state machine first approach generator implemented
lbessard
parents:
diff
changeset
|
3 |
* |
38754701ac41
SFC state machine first approach generator implemented
lbessard
parents:
diff
changeset
|
4 |
* Offered to the public under the terms of the GNU General Public License |
38754701ac41
SFC state machine first approach generator implemented
lbessard
parents:
diff
changeset
|
5 |
* as published by the Free Software Foundation; either version 2 of the |
38754701ac41
SFC state machine first approach generator implemented
lbessard
parents:
diff
changeset
|
6 |
* License, or (at your option) any later version. |
38754701ac41
SFC state machine first approach generator implemented
lbessard
parents:
diff
changeset
|
7 |
* |
38754701ac41
SFC state machine first approach generator implemented
lbessard
parents:
diff
changeset
|
8 |
* This program is distributed in the hope that it will be useful, but |
38754701ac41
SFC state machine first approach generator implemented
lbessard
parents:
diff
changeset
|
9 |
* WITHOUT ANY WARRANTY; without even the implied warranty of |
38754701ac41
SFC state machine first approach generator implemented
lbessard
parents:
diff
changeset
|
10 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General |
38754701ac41
SFC state machine first approach generator implemented
lbessard
parents:
diff
changeset
|
11 |
* Public License for more details. |
38754701ac41
SFC state machine first approach generator implemented
lbessard
parents:
diff
changeset
|
12 |
* |
38754701ac41
SFC state machine first approach generator implemented
lbessard
parents:
diff
changeset
|
13 |
* This code is made available on the understanding that it will not be |
38754701ac41
SFC state machine first approach generator implemented
lbessard
parents:
diff
changeset
|
14 |
* used in safety-critical situations without a full and competent review. |
38754701ac41
SFC state machine first approach generator implemented
lbessard
parents:
diff
changeset
|
15 |
*/ |
38754701ac41
SFC state machine first approach generator implemented
lbessard
parents:
diff
changeset
|
16 |
|
38754701ac41
SFC state machine first approach generator implemented
lbessard
parents:
diff
changeset
|
17 |
/* |
38754701ac41
SFC state machine first approach generator implemented
lbessard
parents:
diff
changeset
|
18 |
* An IEC 61131-3 IL and ST compiler. |
38754701ac41
SFC state machine first approach generator implemented
lbessard
parents:
diff
changeset
|
19 |
* |
38754701ac41
SFC state machine first approach generator implemented
lbessard
parents:
diff
changeset
|
20 |
* Based on the |
38754701ac41
SFC state machine first approach generator implemented
lbessard
parents:
diff
changeset
|
21 |
* FINAL DRAFT - IEC 61131-3, 2nd Ed. (2001-12-10) |
38754701ac41
SFC state machine first approach generator implemented
lbessard
parents:
diff
changeset
|
22 |
* |
38754701ac41
SFC state machine first approach generator implemented
lbessard
parents:
diff
changeset
|
23 |
*/ |
38754701ac41
SFC state machine first approach generator implemented
lbessard
parents:
diff
changeset
|
24 |
|
38754701ac41
SFC state machine first approach generator implemented
lbessard
parents:
diff
changeset
|
25 |
|
38754701ac41
SFC state machine first approach generator implemented
lbessard
parents:
diff
changeset
|
26 |
/* |
38754701ac41
SFC state machine first approach generator implemented
lbessard
parents:
diff
changeset
|
27 |
* Conversion of sfc networks (i.e. SFC code). |
38754701ac41
SFC state machine first approach generator implemented
lbessard
parents:
diff
changeset
|
28 |
* |
38754701ac41
SFC state machine first approach generator implemented
lbessard
parents:
diff
changeset
|
29 |
* This is part of the 4th stage that generates |
38754701ac41
SFC state machine first approach generator implemented
lbessard
parents:
diff
changeset
|
30 |
* a c++ source program equivalent to the SFC, IL and ST |
38754701ac41
SFC state machine first approach generator implemented
lbessard
parents:
diff
changeset
|
31 |
* code. |
38754701ac41
SFC state machine first approach generator implemented
lbessard
parents:
diff
changeset
|
32 |
*/ |
38754701ac41
SFC state machine first approach generator implemented
lbessard
parents:
diff
changeset
|
33 |
|
38754701ac41
SFC state machine first approach generator implemented
lbessard
parents:
diff
changeset
|
34 |
|
38754701ac41
SFC state machine first approach generator implemented
lbessard
parents:
diff
changeset
|
35 |
|
38754701ac41
SFC state machine first approach generator implemented
lbessard
parents:
diff
changeset
|
36 |
|
38754701ac41
SFC state machine first approach generator implemented
lbessard
parents:
diff
changeset
|
37 |
/***********************************************************************/ |
38754701ac41
SFC state machine first approach generator implemented
lbessard
parents:
diff
changeset
|
38 |
/***********************************************************************/ |
38754701ac41
SFC state machine first approach generator implemented
lbessard
parents:
diff
changeset
|
39 |
/***********************************************************************/ |
38754701ac41
SFC state machine first approach generator implemented
lbessard
parents:
diff
changeset
|
40 |
/***********************************************************************/ |
38754701ac41
SFC state machine first approach generator implemented
lbessard
parents:
diff
changeset
|
41 |
|
49 | 42 |
class generate_cc_sfcdecl_c: protected generate_cc_typedecl_c { |
43 |
||
44 |
public: |
|
45 |
typedef enum { |
|
52 | 46 |
sfcdecl_sd, |
47 |
sfcinit_sd, |
|
66 | 48 |
stepcount_sd, |
52 | 49 |
stepdef_sd, |
50 |
stepundef_sd, |
|
51 |
actiondef_sd, |
|
66 | 52 |
actionundef_sd, |
53 |
actioncount_sd |
|
52 | 54 |
} sfcdeclaration_t; |
17
38754701ac41
SFC state machine first approach generator implemented
lbessard
parents:
diff
changeset
|
55 |
|
38754701ac41
SFC state machine first approach generator implemented
lbessard
parents:
diff
changeset
|
56 |
private: |
38754701ac41
SFC state machine first approach generator implemented
lbessard
parents:
diff
changeset
|
57 |
char step_number; |
49 | 58 |
char action_number; |
59 |
char transition_number; |
|
60 |
||
52 | 61 |
sfcdeclaration_t wanted_sfcdeclaration; |
17
38754701ac41
SFC state machine first approach generator implemented
lbessard
parents:
diff
changeset
|
62 |
|
38754701ac41
SFC state machine first approach generator implemented
lbessard
parents:
diff
changeset
|
63 |
public: |
52 | 64 |
generate_cc_sfcdecl_c(stage4out_c *s4o_ptr, sfcdeclaration_t sfcdeclaration) |
49 | 65 |
: generate_cc_typedecl_c(s4o_ptr) { |
52 | 66 |
wanted_sfcdeclaration = sfcdeclaration; |
49 | 67 |
} |
68 |
~generate_cc_sfcdecl_c(void) {} |
|
69 |
||
70 |
void print(symbol_c *symbol, const char *variable_prefix = NULL) { |
|
71 |
this->set_variable_prefix(variable_prefix); |
|
72 |
||
73 |
symbol->accept(*this); |
|
74 |
} |
|
75 |
||
17
38754701ac41
SFC state machine first approach generator implemented
lbessard
parents:
diff
changeset
|
76 |
/*********************************************/ |
38754701ac41
SFC state machine first approach generator implemented
lbessard
parents:
diff
changeset
|
77 |
/* B.1.6 Sequential function chart elements */ |
38754701ac41
SFC state machine first approach generator implemented
lbessard
parents:
diff
changeset
|
78 |
/*********************************************/ |
38754701ac41
SFC state machine first approach generator implemented
lbessard
parents:
diff
changeset
|
79 |
|
49 | 80 |
void *visit(sequential_function_chart_c *symbol) { |
81 |
step_number = 0; |
|
82 |
action_number = 0; |
|
83 |
transition_number = 0; |
|
52 | 84 |
switch (wanted_sfcdeclaration) { |
85 |
case sfcdecl_sd: |
|
49 | 86 |
for(int i = 0; i < symbol->n; i++) |
87 |
symbol->elements[i]->accept(*this); |
|
88 |
||
89 |
/* steps table declaration */ |
|
90 |
s4o.print(s4o.indent_spaces + "STEP step_list["); |
|
91 |
s4o.print_integer(step_number); |
|
92 |
s4o.print("];\n"); |
|
66 | 93 |
s4o.print(s4o.indent_spaces + "UINT nb_steps;\n"); |
49 | 94 |
|
95 |
/* actions table declaration */ |
|
96 |
s4o.print(s4o.indent_spaces + "ACTION action_list["); |
|
97 |
s4o.print_integer(action_number); |
|
98 |
s4o.print("];\n"); |
|
66 | 99 |
s4o.print(s4o.indent_spaces + "UINT nb_actions;\n"); |
49 | 100 |
|
101 |
/* transitions table declaration */ |
|
102 |
s4o.print(s4o.indent_spaces + "USINT transition_list["); |
|
103 |
s4o.print_integer(transition_number); |
|
104 |
s4o.print("];\n"); |
|
66 | 105 |
|
106 |
/* period declaration */ |
|
107 |
s4o.print(s4o.indent_spaces + "TIME period;\n"); |
|
49 | 108 |
break; |
52 | 109 |
case sfcinit_sd: |
66 | 110 |
s4o.print(s4o.indent_spaces); |
111 |
s4o.print("UINT i;\n"); |
|
112 |
||
113 |
/* steps table count */ |
|
114 |
wanted_sfcdeclaration = stepcount_sd; |
|
115 |
for(int i = 0; i < symbol->n; i++) |
|
116 |
symbol->elements[i]->accept(*this); |
|
117 |
s4o.print(s4o.indent_spaces); |
|
118 |
print_variable_prefix(); |
|
119 |
s4o.print("nb_steps = "); |
|
120 |
s4o.print_integer(step_number); |
|
121 |
s4o.print(";\n"); |
|
122 |
step_number = 0; |
|
123 |
wanted_sfcdeclaration = sfcinit_sd; |
|
124 |
||
49 | 125 |
/* steps table initialisation */ |
126 |
s4o.print(s4o.indent_spaces + "STEP temp_step = {0, 0, 0};\n"); |
|
66 | 127 |
s4o.print(s4o.indent_spaces + "for(i = 0; i < "); |
49 | 128 |
print_variable_prefix(); |
129 |
s4o.print("nb_steps; i++) {\n"); |
|
130 |
s4o.indent_right(); |
|
131 |
s4o.print(s4o.indent_spaces); |
|
132 |
print_variable_prefix(); |
|
133 |
s4o.print("step_list[i] = temp_step;\n"); |
|
134 |
s4o.indent_left(); |
|
135 |
s4o.print(s4o.indent_spaces + "}\n"); |
|
136 |
for(int i = 0; i < symbol->n; i++) |
|
137 |
symbol->elements[i]->accept(*this); |
|
138 |
||
66 | 139 |
/* steps table count */ |
140 |
wanted_sfcdeclaration = actioncount_sd; |
|
141 |
for(int i = 0; i < symbol->n; i++) |
|
142 |
symbol->elements[i]->accept(*this); |
|
143 |
s4o.print(s4o.indent_spaces); |
|
144 |
print_variable_prefix(); |
|
145 |
s4o.print("nb_actions = "); |
|
146 |
s4o.print_integer(action_number); |
|
147 |
s4o.print(";\n"); |
|
148 |
action_number = 0; |
|
149 |
wanted_sfcdeclaration = sfcinit_sd; |
|
150 |
||
49 | 151 |
/* actions table initialisation */ |
152 |
s4o.print(s4o.indent_spaces + "ACTION temp_action = {0, 0, 0, 0, 0, 0};\n"); |
|
66 | 153 |
s4o.print(s4o.indent_spaces + "for(i = 0; i < "); |
49 | 154 |
print_variable_prefix(); |
155 |
s4o.print("nb_actions; i++) {\n"); |
|
156 |
s4o.indent_right(); |
|
157 |
s4o.print(s4o.indent_spaces); |
|
158 |
print_variable_prefix(); |
|
159 |
s4o.print("action_list[i] = temp_action;\n"); |
|
66 | 160 |
|
161 |
/* period initialisation */ |
|
162 |
s4o.print(s4o.indent_spaces); |
|
163 |
print_variable_prefix(); |
|
164 |
s4o.print("period = __time_to_timespec(1, 0, 0, 0, 0, 0);\n"); |
|
165 |
||
49 | 166 |
s4o.indent_left(); |
167 |
s4o.print(s4o.indent_spaces + "}\n"); |
|
168 |
break; |
|
52 | 169 |
case stepdef_sd: |
49 | 170 |
s4o.print("// Steps definitions\n"); |
171 |
for(int i = 0; i < symbol->n; i++) |
|
172 |
symbol->elements[i]->accept(*this); |
|
173 |
s4o.print("\n"); |
|
174 |
break; |
|
52 | 175 |
case actiondef_sd: |
49 | 176 |
s4o.print("// Actions definitions\n"); |
177 |
for(int i = 0; i < symbol->n; i++) |
|
178 |
symbol->elements[i]->accept(*this); |
|
179 |
s4o.print("\n"); |
|
180 |
break; |
|
52 | 181 |
case stepundef_sd: |
49 | 182 |
s4o.print("// Steps undefinitions\n"); |
183 |
for(int i = 0; i < symbol->n; i++) |
|
184 |
symbol->elements[i]->accept(*this); |
|
185 |
s4o.print("\n"); |
|
186 |
break; |
|
52 | 187 |
case actionundef_sd: |
49 | 188 |
s4o.print("// Actions undefinitions\n"); |
189 |
for(int i = 0; i < symbol->n; i++) |
|
190 |
symbol->elements[i]->accept(*this); |
|
191 |
s4o.print("\n"); |
|
192 |
break; |
|
66 | 193 |
default: |
194 |
break; |
|
49 | 195 |
} |
196 |
return NULL; |
|
197 |
} |
|
198 |
||
17
38754701ac41
SFC state machine first approach generator implemented
lbessard
parents:
diff
changeset
|
199 |
void *visit(initial_step_c *symbol) { |
52 | 200 |
switch (wanted_sfcdeclaration) { |
66 | 201 |
case stepcount_sd: |
52 | 202 |
case sfcdecl_sd: |
203 |
step_number++; |
|
204 |
break; |
|
205 |
case sfcinit_sd: |
|
49 | 206 |
s4o.print(s4o.indent_spaces); |
207 |
print_variable_prefix(); |
|
66 | 208 |
s4o.print("step_list["); |
49 | 209 |
s4o.print_integer(step_number); |
210 |
s4o.print("].state = 1;\n"); |
|
211 |
step_number++; |
|
212 |
break; |
|
52 | 213 |
case stepdef_sd: |
49 | 214 |
s4o.print("#define "); |
215 |
s4o.print(SFC_STEP_ACTION_PREFIX); |
|
216 |
symbol->step_name->accept(*this); |
|
217 |
s4o.print(" "); |
|
218 |
s4o.print_integer(step_number); |
|
219 |
s4o.print("\n"); |
|
220 |
step_number++; |
|
221 |
break; |
|
52 | 222 |
case stepundef_sd: |
49 | 223 |
s4o.print("#undef "); |
224 |
s4o.print(SFC_STEP_ACTION_PREFIX); |
|
225 |
symbol->step_name->accept(*this); |
|
226 |
s4o.print("\n"); |
|
227 |
break; |
|
228 |
default: |
|
229 |
break; |
|
230 |
} |
|
17
38754701ac41
SFC state machine first approach generator implemented
lbessard
parents:
diff
changeset
|
231 |
return NULL; |
38754701ac41
SFC state machine first approach generator implemented
lbessard
parents:
diff
changeset
|
232 |
} |
38754701ac41
SFC state machine first approach generator implemented
lbessard
parents:
diff
changeset
|
233 |
|
38754701ac41
SFC state machine first approach generator implemented
lbessard
parents:
diff
changeset
|
234 |
void *visit(step_c *symbol) { |
52 | 235 |
switch (wanted_sfcdeclaration) { |
66 | 236 |
case stepcount_sd: |
52 | 237 |
case sfcdecl_sd: |
238 |
step_number++; |
|
239 |
break; |
|
240 |
case stepdef_sd: |
|
49 | 241 |
s4o.print("#define "); |
242 |
s4o.print(SFC_STEP_ACTION_PREFIX); |
|
243 |
symbol->step_name->accept(*this); |
|
244 |
s4o.print(" "); |
|
245 |
s4o.print_integer(step_number); |
|
246 |
s4o.print("\n"); |
|
247 |
step_number++; |
|
248 |
break; |
|
52 | 249 |
case stepundef_sd: |
49 | 250 |
s4o.print("#undef "); |
251 |
s4o.print(SFC_STEP_ACTION_PREFIX); |
|
252 |
symbol->step_name->accept(*this); |
|
253 |
s4o.print("\n"); |
|
254 |
break; |
|
255 |
default: |
|
256 |
break; |
|
257 |
} |
|
258 |
return NULL; |
|
259 |
} |
|
260 |
||
261 |
void *visit(transition_c *symbol) { |
|
52 | 262 |
switch (wanted_sfcdeclaration) { |
263 |
case sfcdecl_sd: |
|
49 | 264 |
transition_number++; |
265 |
break; |
|
266 |
default: |
|
267 |
break; |
|
268 |
} |
|
269 |
return NULL; |
|
270 |
} |
|
17
38754701ac41
SFC state machine first approach generator implemented
lbessard
parents:
diff
changeset
|
271 |
|
38754701ac41
SFC state machine first approach generator implemented
lbessard
parents:
diff
changeset
|
272 |
void *visit(action_c *symbol) { |
52 | 273 |
switch (wanted_sfcdeclaration) { |
274 |
case actiondef_sd: |
|
49 | 275 |
s4o.print("#define "); |
276 |
s4o.print(SFC_STEP_ACTION_PREFIX); |
|
277 |
symbol->action_name->accept(*this); |
|
278 |
s4o.print(" "); |
|
279 |
s4o.print_integer(action_number); |
|
280 |
s4o.print("\n"); |
|
281 |
action_number++; |
|
282 |
break; |
|
52 | 283 |
case actionundef_sd: |
49 | 284 |
s4o.print("#undef "); |
285 |
s4o.print(SFC_STEP_ACTION_PREFIX); |
|
286 |
symbol->action_name->accept(*this); |
|
287 |
s4o.print("\n"); |
|
288 |
break; |
|
66 | 289 |
case actioncount_sd: |
52 | 290 |
case sfcdecl_sd: |
49 | 291 |
action_number++; |
292 |
break; |
|
293 |
default: |
|
294 |
break; |
|
295 |
} |
|
296 |
return NULL; |
|
297 |
} |
|
298 |
||
56 | 299 |
void *visit(instruction_list_c *symbol) { |
300 |
return NULL; |
|
301 |
} |
|
302 |
||
303 |
void *visit(statement_list_c *symbol) { |
|
304 |
return NULL; |
|
305 |
} |
|
306 |
||
49 | 307 |
}; /* generate_cc_sfcdecl_c */ |
308 |