author | lbessard |
Mon, 27 Aug 2007 14:52:15 +0200 | |
changeset 52 | f44458d1fa29 |
parent 49 | c96d1a4c23f8 |
child 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, |
|
48 |
stepdef_sd, |
|
49 |
stepundef_sd, |
|
50 |
actiondef_sd, |
|
51 |
actionundef_sd |
|
52 |
} sfcdeclaration_t; |
|
17
38754701ac41
SFC state machine first approach generator implemented
lbessard
parents:
diff
changeset
|
53 |
|
38754701ac41
SFC state machine first approach generator implemented
lbessard
parents:
diff
changeset
|
54 |
private: |
38754701ac41
SFC state machine first approach generator implemented
lbessard
parents:
diff
changeset
|
55 |
char step_number; |
49 | 56 |
char action_number; |
57 |
char transition_number; |
|
58 |
||
52 | 59 |
sfcdeclaration_t wanted_sfcdeclaration; |
17
38754701ac41
SFC state machine first approach generator implemented
lbessard
parents:
diff
changeset
|
60 |
|
38754701ac41
SFC state machine first approach generator implemented
lbessard
parents:
diff
changeset
|
61 |
public: |
52 | 62 |
generate_cc_sfcdecl_c(stage4out_c *s4o_ptr, sfcdeclaration_t sfcdeclaration) |
49 | 63 |
: generate_cc_typedecl_c(s4o_ptr) { |
52 | 64 |
wanted_sfcdeclaration = sfcdeclaration; |
49 | 65 |
} |
66 |
~generate_cc_sfcdecl_c(void) {} |
|
67 |
||
68 |
void print(symbol_c *symbol, const char *variable_prefix = NULL) { |
|
69 |
this->set_variable_prefix(variable_prefix); |
|
70 |
||
71 |
symbol->accept(*this); |
|
72 |
} |
|
73 |
||
17
38754701ac41
SFC state machine first approach generator implemented
lbessard
parents:
diff
changeset
|
74 |
/*********************************************/ |
38754701ac41
SFC state machine first approach generator implemented
lbessard
parents:
diff
changeset
|
75 |
/* B.1.6 Sequential function chart elements */ |
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 |
|
49 | 78 |
void *visit(sequential_function_chart_c *symbol) { |
79 |
step_number = 0; |
|
80 |
action_number = 0; |
|
81 |
transition_number = 0; |
|
52 | 82 |
switch (wanted_sfcdeclaration) { |
83 |
case sfcdecl_sd: |
|
49 | 84 |
for(int i = 0; i < symbol->n; i++) |
85 |
symbol->elements[i]->accept(*this); |
|
86 |
||
87 |
/* steps table declaration */ |
|
88 |
s4o.print(s4o.indent_spaces + "STEP step_list["); |
|
89 |
s4o.print_integer(step_number); |
|
90 |
s4o.print("];\n"); |
|
91 |
s4o.print(s4o.indent_spaces + "UINT nb_steps = "); |
|
92 |
s4o.print_integer(step_number); |
|
93 |
s4o.print(";\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 = "); |
|
100 |
s4o.print_integer(action_number); |
|
101 |
s4o.print(";\n"); |
|
102 |
||
103 |
/* transitions table declaration */ |
|
104 |
s4o.print(s4o.indent_spaces + "USINT transition_list["); |
|
105 |
s4o.print_integer(transition_number); |
|
106 |
s4o.print("];\n"); |
|
107 |
break; |
|
52 | 108 |
case sfcinit_sd: |
49 | 109 |
/* steps table initialisation */ |
110 |
s4o.print(s4o.indent_spaces + "STEP temp_step = {0, 0, 0};\n"); |
|
111 |
s4o.print(s4o.indent_spaces + "for(UINT i = 0; i < "); |
|
112 |
print_variable_prefix(); |
|
113 |
s4o.print("nb_steps; i++) {\n"); |
|
114 |
s4o.indent_right(); |
|
115 |
s4o.print(s4o.indent_spaces); |
|
116 |
print_variable_prefix(); |
|
117 |
s4o.print("step_list[i] = temp_step;\n"); |
|
118 |
s4o.indent_left(); |
|
119 |
s4o.print(s4o.indent_spaces + "}\n"); |
|
120 |
for(int i = 0; i < symbol->n; i++) |
|
121 |
symbol->elements[i]->accept(*this); |
|
122 |
||
123 |
/* actions table initialisation */ |
|
124 |
s4o.print(s4o.indent_spaces + "ACTION temp_action = {0, 0, 0, 0, 0, 0};\n"); |
|
125 |
s4o.print(s4o.indent_spaces + "for(UINT i = 0; i < "); |
|
126 |
print_variable_prefix(); |
|
127 |
s4o.print("nb_actions; i++) {\n"); |
|
128 |
s4o.indent_right(); |
|
129 |
s4o.print(s4o.indent_spaces); |
|
130 |
print_variable_prefix(); |
|
131 |
s4o.print("action_list[i] = temp_action;\n"); |
|
132 |
s4o.indent_left(); |
|
133 |
s4o.print(s4o.indent_spaces + "}\n"); |
|
134 |
break; |
|
52 | 135 |
case stepdef_sd: |
49 | 136 |
s4o.print("// Steps definitions\n"); |
137 |
for(int i = 0; i < symbol->n; i++) |
|
138 |
symbol->elements[i]->accept(*this); |
|
139 |
s4o.print("\n"); |
|
140 |
break; |
|
52 | 141 |
case actiondef_sd: |
49 | 142 |
s4o.print("// Actions definitions\n"); |
143 |
for(int i = 0; i < symbol->n; i++) |
|
144 |
symbol->elements[i]->accept(*this); |
|
145 |
s4o.print("\n"); |
|
146 |
break; |
|
52 | 147 |
case stepundef_sd: |
49 | 148 |
s4o.print("// Steps undefinitions\n"); |
149 |
for(int i = 0; i < symbol->n; i++) |
|
150 |
symbol->elements[i]->accept(*this); |
|
151 |
s4o.print("\n"); |
|
152 |
break; |
|
52 | 153 |
case actionundef_sd: |
49 | 154 |
s4o.print("// Actions undefinitions\n"); |
155 |
for(int i = 0; i < symbol->n; i++) |
|
156 |
symbol->elements[i]->accept(*this); |
|
157 |
s4o.print("\n"); |
|
158 |
break; |
|
159 |
} |
|
160 |
return NULL; |
|
161 |
} |
|
162 |
||
17
38754701ac41
SFC state machine first approach generator implemented
lbessard
parents:
diff
changeset
|
163 |
void *visit(initial_step_c *symbol) { |
52 | 164 |
switch (wanted_sfcdeclaration) { |
165 |
case sfcdecl_sd: |
|
166 |
step_number++; |
|
167 |
break; |
|
168 |
case sfcinit_sd: |
|
49 | 169 |
s4o.print(s4o.indent_spaces); |
170 |
print_variable_prefix(); |
|
171 |
s4o.print("action_list["); |
|
172 |
s4o.print_integer(step_number); |
|
173 |
s4o.print("].state = 1;\n"); |
|
174 |
step_number++; |
|
175 |
break; |
|
52 | 176 |
case stepdef_sd: |
49 | 177 |
s4o.print("#define "); |
178 |
s4o.print(SFC_STEP_ACTION_PREFIX); |
|
179 |
symbol->step_name->accept(*this); |
|
180 |
s4o.print(" "); |
|
181 |
s4o.print_integer(step_number); |
|
182 |
s4o.print("\n"); |
|
183 |
step_number++; |
|
184 |
break; |
|
52 | 185 |
case stepundef_sd: |
49 | 186 |
s4o.print("#undef "); |
187 |
s4o.print(SFC_STEP_ACTION_PREFIX); |
|
188 |
symbol->step_name->accept(*this); |
|
189 |
s4o.print("\n"); |
|
190 |
break; |
|
191 |
default: |
|
192 |
break; |
|
193 |
} |
|
17
38754701ac41
SFC state machine first approach generator implemented
lbessard
parents:
diff
changeset
|
194 |
return NULL; |
38754701ac41
SFC state machine first approach generator implemented
lbessard
parents:
diff
changeset
|
195 |
} |
38754701ac41
SFC state machine first approach generator implemented
lbessard
parents:
diff
changeset
|
196 |
|
38754701ac41
SFC state machine first approach generator implemented
lbessard
parents:
diff
changeset
|
197 |
void *visit(step_c *symbol) { |
52 | 198 |
switch (wanted_sfcdeclaration) { |
199 |
case sfcdecl_sd: |
|
200 |
step_number++; |
|
201 |
break; |
|
202 |
case stepdef_sd: |
|
49 | 203 |
s4o.print("#define "); |
204 |
s4o.print(SFC_STEP_ACTION_PREFIX); |
|
205 |
symbol->step_name->accept(*this); |
|
206 |
s4o.print(" "); |
|
207 |
s4o.print_integer(step_number); |
|
208 |
s4o.print("\n"); |
|
209 |
step_number++; |
|
210 |
break; |
|
52 | 211 |
case stepundef_sd: |
49 | 212 |
s4o.print("#undef "); |
213 |
s4o.print(SFC_STEP_ACTION_PREFIX); |
|
214 |
symbol->step_name->accept(*this); |
|
215 |
s4o.print("\n"); |
|
216 |
break; |
|
217 |
default: |
|
218 |
break; |
|
219 |
} |
|
220 |
return NULL; |
|
221 |
} |
|
222 |
||
223 |
void *visit(transition_c *symbol) { |
|
52 | 224 |
switch (wanted_sfcdeclaration) { |
225 |
case sfcdecl_sd: |
|
49 | 226 |
transition_number++; |
227 |
break; |
|
228 |
default: |
|
229 |
break; |
|
230 |
} |
|
231 |
return NULL; |
|
232 |
} |
|
17
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(action_c *symbol) { |
52 | 235 |
switch (wanted_sfcdeclaration) { |
236 |
case actiondef_sd: |
|
49 | 237 |
s4o.print("#define "); |
238 |
s4o.print(SFC_STEP_ACTION_PREFIX); |
|
239 |
symbol->action_name->accept(*this); |
|
240 |
s4o.print(" "); |
|
241 |
s4o.print_integer(action_number); |
|
242 |
s4o.print("\n"); |
|
243 |
action_number++; |
|
244 |
break; |
|
52 | 245 |
case actionundef_sd: |
49 | 246 |
s4o.print("#undef "); |
247 |
s4o.print(SFC_STEP_ACTION_PREFIX); |
|
248 |
symbol->action_name->accept(*this); |
|
249 |
s4o.print("\n"); |
|
250 |
break; |
|
52 | 251 |
case sfcdecl_sd: |
49 | 252 |
action_number++; |
253 |
break; |
|
254 |
default: |
|
255 |
break; |
|
256 |
} |
|
257 |
return NULL; |
|
258 |
} |
|
259 |
||
260 |
}; /* generate_cc_sfcdecl_c */ |
|
261 |