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,
|
|
53 |
actioncount_sd
|
|
54 |
} sfcdeclaration_t;
|
|
55 |
|
|
56 |
private:
|
|
57 |
char step_number;
|
|
58 |
char action_number;
|
|
59 |
char transition_number;
|
|
60 |
|
|
61 |
sfcdeclaration_t wanted_sfcdeclaration;
|
|
62 |
|
|
63 |
public:
|
|
64 |
generate_c_sfcdecl_c(stage4out_c *s4o_ptr, sfcdeclaration_t sfcdeclaration)
|
|
65 |
: generate_c_typedecl_c(s4o_ptr) {
|
|
66 |
wanted_sfcdeclaration = sfcdeclaration;
|
|
67 |
}
|
|
68 |
~generate_c_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 |
|
|
76 |
/*********************************************/
|
|
77 |
/* B.1.6 Sequential function chart elements */
|
|
78 |
/*********************************************/
|
|
79 |
|
|
80 |
void *visit(sequential_function_chart_c *symbol) {
|
|
81 |
step_number = 0;
|
|
82 |
action_number = 0;
|
|
83 |
transition_number = 0;
|
|
84 |
switch (wanted_sfcdeclaration) {
|
|
85 |
case sfcdecl_sd:
|
|
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");
|
|
93 |
s4o.print(s4o.indent_spaces + "UINT nb_steps;\n");
|
|
94 |
|
|
95 |
/* actions table declaration */
|
|
96 |
s4o.print(s4o.indent_spaces + "ACTION action_list[");
|
|
97 |
s4o.print_integer(action_number);
|
|
98 |
s4o.print("];\n");
|
|
99 |
s4o.print(s4o.indent_spaces + "UINT nb_actions;\n");
|
|
100 |
|
|
101 |
/* transitions table declaration */
|
|
102 |
s4o.print(s4o.indent_spaces + "USINT transition_list[");
|
|
103 |
s4o.print_integer(transition_number);
|
|
104 |
s4o.print("];\n");
|
|
105 |
|
|
106 |
/* period declaration */
|
|
107 |
s4o.print(s4o.indent_spaces + "TIME period;\n");
|
|
108 |
break;
|
|
109 |
case sfcinit_sd:
|
|
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 |
|
|
125 |
/* steps table initialisation */
|
|
126 |
s4o.print(s4o.indent_spaces + "STEP temp_step = {0, 0, 0};\n");
|
|
127 |
s4o.print(s4o.indent_spaces + "for(i = 0; i < ");
|
|
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 |
|
|
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 |
|
|
151 |
/* actions table initialisation */
|
|
152 |
s4o.print(s4o.indent_spaces + "ACTION temp_action = {0, 0, 0, 0, 0, 0};\n");
|
|
153 |
s4o.print(s4o.indent_spaces + "for(i = 0; i < ");
|
|
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");
|
|
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 |
|
|
166 |
s4o.indent_left();
|
|
167 |
s4o.print(s4o.indent_spaces + "}\n");
|
|
168 |
break;
|
|
169 |
case stepdef_sd:
|
|
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;
|
|
175 |
case actiondef_sd:
|
|
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;
|
|
181 |
case stepundef_sd:
|
|
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;
|
|
187 |
case actionundef_sd:
|
|
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;
|
|
193 |
default:
|
|
194 |
break;
|
|
195 |
}
|
|
196 |
return NULL;
|
|
197 |
}
|
|
198 |
|
|
199 |
void *visit(initial_step_c *symbol) {
|
|
200 |
switch (wanted_sfcdeclaration) {
|
|
201 |
case stepcount_sd:
|
|
202 |
case sfcdecl_sd:
|
|
203 |
step_number++;
|
|
204 |
break;
|
|
205 |
case sfcinit_sd:
|
|
206 |
s4o.print(s4o.indent_spaces);
|
|
207 |
print_variable_prefix();
|
|
208 |
s4o.print("step_list[");
|
|
209 |
s4o.print_integer(step_number);
|
|
210 |
s4o.print("].state = 1;\n");
|
|
211 |
step_number++;
|
|
212 |
break;
|
|
213 |
case stepdef_sd:
|
|
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;
|
|
222 |
case stepundef_sd:
|
|
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 |
}
|
|
231 |
return NULL;
|
|
232 |
}
|
|
233 |
|
|
234 |
void *visit(step_c *symbol) {
|
|
235 |
switch (wanted_sfcdeclaration) {
|
|
236 |
case stepcount_sd:
|
|
237 |
case sfcdecl_sd:
|
|
238 |
step_number++;
|
|
239 |
break;
|
|
240 |
case stepdef_sd:
|
|
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;
|
|
249 |
case stepundef_sd:
|
|
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) {
|
|
262 |
switch (wanted_sfcdeclaration) {
|
|
263 |
case sfcdecl_sd:
|
|
264 |
transition_number++;
|
|
265 |
break;
|
|
266 |
default:
|
|
267 |
break;
|
|
268 |
}
|
|
269 |
return NULL;
|
|
270 |
}
|
|
271 |
|
|
272 |
void *visit(action_c *symbol) {
|
|
273 |
switch (wanted_sfcdeclaration) {
|
|
274 |
case actiondef_sd:
|
|
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;
|
|
283 |
case actionundef_sd:
|
|
284 |
s4o.print("#undef ");
|
|
285 |
s4o.print(SFC_STEP_ACTION_PREFIX);
|
|
286 |
symbol->action_name->accept(*this);
|
|
287 |
s4o.print("\n");
|
|
288 |
break;
|
|
289 |
case actioncount_sd:
|
|
290 |
case sfcdecl_sd:
|
|
291 |
action_number++;
|
|
292 |
break;
|
|
293 |
default:
|
|
294 |
break;
|
|
295 |
}
|
|
296 |
return NULL;
|
|
297 |
}
|
|
298 |
|
|
299 |
void *visit(instruction_list_c *symbol) {
|
|
300 |
return NULL;
|
|
301 |
}
|
|
302 |
|
|
303 |
void *visit(statement_list_c *symbol) {
|
|
304 |
return NULL;
|
|
305 |
}
|
|
306 |
|
|
307 |
}; /* generate_c_sfcdecl_c */
|
|
308 |
|