|
1 /* |
|
2 * matiec - a compiler for the programming languages defined in IEC 61131-3 |
|
3 * |
|
4 * Copyright (C) 2012 Mario de Sousa (msousa@fe.up.pt) |
|
5 * |
|
6 * This program is free software: you can redistribute it and/or modify |
|
7 * it under the terms of the GNU General Public License as published by |
|
8 * the Free Software Foundation, either version 3 of the License, or |
|
9 * (at your option) any later version. |
|
10 * |
|
11 * This program is distributed in the hope that it will be useful, |
|
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
14 * GNU General Public License for more details. |
|
15 * |
|
16 * You should have received a copy of the GNU General Public License |
|
17 * along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
18 * |
|
19 * |
|
20 * This code is made available on the understanding that it will not be |
|
21 * used in safety-critical situations without a full and competent review. |
|
22 */ |
|
23 |
|
24 /* |
|
25 * An IEC 61131-3 compiler. |
|
26 * |
|
27 * Based on the |
|
28 * FINAL DRAFT - IEC 61131-3, 2nd Ed. (2001-12-10) |
|
29 * |
|
30 */ |
|
31 |
|
32 |
|
33 /* |
|
34 * Do flow control analysis of the IEC 61131-3 code. |
|
35 * |
|
36 * We currently only do this for IL code. |
|
37 * This class will annotate the abstract syntax tree, by filling in the |
|
38 * prev_il_instruction variable in the il_instruction_c, so it points to |
|
39 * the previous il_instruction_c object in the instruction list instruction_list_c. |
|
40 * |
|
41 * Since IL code can contain jumps (JMP), the same il_instruction may effectively have |
|
42 * several previous il_instructions. In order to accommodate this, each il_instruction |
|
43 * will maintain a vector (i..e an array) of pointers to all the previous il_instructions. |
|
44 * We do however attempt to guarantee that the first element in the vector (array) will preferentially |
|
45 * point to the il instruction that is right before / imediately preceding the current il instructions, |
|
46 * i.e. the first element in the array will tend to point to the previous il_instruction |
|
47 * that is not a jump JMP IL instruction! |
|
48 * |
|
49 * The result will essentially be a graph of il_instruction_c objects, each |
|
50 * pointing to the previous il_instruction_c object. |
|
51 * |
|
52 * The reality is we will get several independent and isolated linked lists |
|
53 * (actually, since we now process labels correctly, this is really a graph): |
|
54 * one for each block of IL code (e.g. inside a Function, FB or Program). |
|
55 * Additionally, when the IL code has an expression (expression_c object), we will actually |
|
56 * have one more isolated linked list for the IL code inside that expression. |
|
57 * |
|
58 * e.g. |
|
59 * line_1: LD 1 |
|
60 * line_2: ADD (42 |
|
61 * line_3: ADD B |
|
62 * line_4: ADD C |
|
63 * line_5: ) |
|
64 * line_6: ADD D |
|
65 * line_7: ST E |
|
66 * |
|
67 * will result in two independent linked lists: |
|
68 * main list: line_7 -> line_6 -> line2 -> line_1 |
|
69 * expr list: lin4_4 -> line_3 -> (operand of line_2, i.e. '42') |
|
70 * |
|
71 * |
|
72 * In the main list, each: |
|
73 * line_x: IL_operation IL_operand |
|
74 * is encoded as |
|
75 * il_instruction_c(label, il_incomplete_instruction) |
|
76 * these il_instruction_c objects will point back to the previous il_instruction_c object. |
|
77 * |
|
78 * In the expr list, each |
|
79 * line_x: IL_operation IL_operand |
|
80 * is encoded as |
|
81 * il_simple_instruction_c(il_simple_instruction) |
|
82 * these il_simple_instruction_c objects will point back to the previous il_simple_instruction_c object, |
|
83 * except the for the first il_simple_instruction_c object in the list, which will point back to |
|
84 * the first il_operand (in the above example, '42'), or NULL is it does not exist. |
|
85 * |
|
86 * |
|
87 * label: |
|
88 * identifier_c |
|
89 * |
|
90 * il_incomplete_instruction: |
|
91 * il_simple_operation (il_simple_operation_c, il_function_call_c) |
|
92 * | il_expression (il_expression_c) |
|
93 * | il_jump_operation (il_jump_operation_c) |
|
94 * | il_fb_call (il_fb_call_c) |
|
95 * | il_formal_funct_call (il_formal_funct_call_c) |
|
96 * | il_return_operator (RET_operator_c, RETC_operator_c, RETCN_operator_c) |
|
97 * |
|
98 * |
|
99 * il_expression_c(il_expr_operator, il_operand, simple_instr_list) |
|
100 * |
|
101 * il_operand: |
|
102 * variable (symbolic_variable_c, direct_variable_c, array_variable_c, structured_variable_c) |
|
103 * | enumerated_value (enumerated_value_c) |
|
104 * | constant (lots of literal classes _c) |
|
105 * |
|
106 * simple_instr_list: |
|
107 * list of il_simple_instruction |
|
108 * |
|
109 * il_simple_instruction: |
|
110 * il_simple_operation (il_simple_operation_c, il_function_call_c) |
|
111 * | il_expression (il_expression_c) |
|
112 * | il_formal_funct_call (il_formal_funct_call_c) |
|
113 * |
|
114 */ |
|
115 |
|
116 #include "flow_control_analysis.hh" |
|
117 |
|
118 |
|
119 |
|
120 /* set to 1 to see debug info during execution */ |
|
121 static int debug = 0; |
|
122 |
|
123 flow_control_analysis_c::flow_control_analysis_c(symbol_c *ignore) { |
|
124 prev_il_instruction = NULL; |
|
125 curr_il_instruction = NULL; |
|
126 } |
|
127 |
|
128 flow_control_analysis_c::~flow_control_analysis_c(void) { |
|
129 } |
|
130 |
|
131 |
|
132 |
|
133 /************************************/ |
|
134 /* B 1.5 Program organization units */ |
|
135 /************************************/ |
|
136 /*********************/ |
|
137 /* B 1.5.1 Functions */ |
|
138 /*********************/ |
|
139 void *flow_control_analysis_c::visit(function_declaration_c *symbol) { |
|
140 search_il_label = new search_il_label_c(symbol); |
|
141 if (debug) printf("Doing flow control analysis in body of function %s\n", ((token_c *)(symbol->derived_function_name))->value); |
|
142 symbol->function_body->accept(*this); |
|
143 delete search_il_label; |
|
144 search_il_label = NULL; |
|
145 return NULL; |
|
146 } |
|
147 |
|
148 /***************************/ |
|
149 /* B 1.5.2 Function blocks */ |
|
150 /***************************/ |
|
151 void *flow_control_analysis_c::visit(function_block_declaration_c *symbol) { |
|
152 search_il_label = new search_il_label_c(symbol); |
|
153 if (debug) printf("Doing flow control analysis in body of FB %s\n", ((token_c *)(symbol->fblock_name))->value); |
|
154 symbol->fblock_body->accept(*this); |
|
155 delete search_il_label; |
|
156 search_il_label = NULL; |
|
157 return NULL; |
|
158 } |
|
159 |
|
160 /********************/ |
|
161 /* B 1.5.3 Programs */ |
|
162 /********************/ |
|
163 void *flow_control_analysis_c::visit(program_declaration_c *symbol) { |
|
164 search_il_label = new search_il_label_c(symbol); |
|
165 if (debug) printf("Doing flow control analysis in body of program %s\n", ((token_c *)(symbol->program_type_name))->value); |
|
166 symbol->function_block_body->accept(*this); |
|
167 delete search_il_label; |
|
168 search_il_label = NULL; |
|
169 return NULL; |
|
170 } |
|
171 |
|
172 |
|
173 /********************************/ |
|
174 /* B 1.7 Configuration elements */ |
|
175 /********************************/ |
|
176 void *flow_control_analysis_c::visit(configuration_declaration_c *symbol) { |
|
177 return NULL; |
|
178 } |
|
179 |
|
180 |
|
181 /****************************************/ |
|
182 /* B.2 - Language IL (Instruction List) */ |
|
183 /****************************************/ |
|
184 /***********************************/ |
|
185 /* B 2.1 Instructions and Operands */ |
|
186 /***********************************/ |
|
187 |
|
188 /*| instruction_list il_instruction */ |
|
189 // SYM_LIST(instruction_list_c) |
|
190 void *flow_control_analysis_c::visit(instruction_list_c *symbol) { |
|
191 prev_il_instruction_is_JMP_or_RET = false; |
|
192 for(int i = 0; i < symbol->n; i++) { |
|
193 prev_il_instruction = NULL; |
|
194 if (i > 0) prev_il_instruction = symbol->elements[i-1]; |
|
195 curr_il_instruction = symbol->elements[i]; |
|
196 curr_il_instruction->accept(*this); |
|
197 curr_il_instruction = NULL; |
|
198 } |
|
199 return NULL; |
|
200 } |
|
201 |
|
202 /* | label ':' [il_incomplete_instruction] eol_list */ |
|
203 // SYM_REF2(il_instruction_c, label, il_instruction) |
|
204 // void *visit(instruction_list_c *symbol); |
|
205 void *flow_control_analysis_c::visit(il_instruction_c *symbol) { |
|
206 if ((NULL != prev_il_instruction) && (!prev_il_instruction_is_JMP_or_RET)) |
|
207 /* We try to guarantee that the previous il instruction that is in the previous line, will occupy the first element of the vector. |
|
208 * In order to do that, we use insert() instead of push_back() |
|
209 */ |
|
210 symbol->prev_il_instruction.insert(symbol->prev_il_instruction.begin() , prev_il_instruction); |
|
211 |
|
212 /* check if it is an il_expression_c, a JMP[C[N]], or a RET, and if so, handle it correctly */ |
|
213 prev_il_instruction_is_JMP_or_RET = false; |
|
214 if (NULL != symbol->il_instruction) |
|
215 symbol->il_instruction->accept(*this); |
|
216 return NULL; |
|
217 } |
|
218 |
|
219 |
|
220 |
|
221 /* | il_simple_operator [il_operand] */ |
|
222 // SYM_REF2(il_simple_operation_c, il_simple_operator, il_operand) |
|
223 // void *flow_control_analysis_c::visit(il_simple_operation_c *symbol) |
|
224 |
|
225 |
|
226 |
|
227 /* | function_name [il_operand_list] */ |
|
228 /* NOTE: The parameters 'called_function_declaration' and 'extensible_param_count' are used to pass data between the stage 3 and stage 4. */ |
|
229 // SYM_REF2(il_function_call_c, function_name, il_operand_list, symbol_c *called_function_declaration; int extensible_param_count;) |
|
230 // void *flow_control_analysis_c::visit(il_function_call_c *symbol) |
|
231 |
|
232 |
|
233 /* | il_expr_operator '(' [il_operand] eol_list [simple_instr_list] ')' */ |
|
234 // SYM_REF3(il_expression_c, il_expr_operator, il_operand, simple_instr_list); |
|
235 void *flow_control_analysis_c::visit(il_expression_c *symbol) { |
|
236 if(NULL == symbol->simple_instr_list) |
|
237 /* nothing to do... */ |
|
238 return NULL; |
|
239 |
|
240 symbol_c *save_prev_il_instruction = prev_il_instruction; |
|
241 prev_il_instruction = symbol->il_operand; |
|
242 symbol->simple_instr_list->accept(*this); |
|
243 prev_il_instruction = save_prev_il_instruction; |
|
244 return NULL; |
|
245 } |
|
246 |
|
247 |
|
248 /* il_jump_operator label */ |
|
249 // SYM_REF2(il_jump_operation_c, il_jump_operator, label) |
|
250 void *flow_control_analysis_c::visit(il_jump_operation_c *symbol) { |
|
251 /* search for the il_instruction_c containing the label */ |
|
252 il_instruction_c *destination = search_il_label->find_label(symbol->label); |
|
253 |
|
254 /* give the visit(JMP_operator *) an oportunity to set the prev_il_instruction_is_JMP_or_RET flag! */ |
|
255 symbol->il_jump_operator->accept(*this); |
|
256 /* add, to that il_instruction's list of prev_il_intsructions, the curr_il_instruction */ |
|
257 if (NULL != destination) |
|
258 destination->prev_il_instruction.push_back(curr_il_instruction); |
|
259 return NULL; |
|
260 } |
|
261 |
|
262 |
|
263 /* il_call_operator prev_declared_fb_name |
|
264 * | il_call_operator prev_declared_fb_name '(' ')' |
|
265 * | il_call_operator prev_declared_fb_name '(' eol_list ')' |
|
266 * | il_call_operator prev_declared_fb_name '(' il_operand_list ')' |
|
267 * | il_call_operator prev_declared_fb_name '(' eol_list il_param_list ')' |
|
268 */ |
|
269 /* NOTE: The parameter 'called_fb_declaration'is used to pass data between stage 3 and stage4 (although currently it is not used in stage 4 */ |
|
270 // SYM_REF4(il_fb_call_c, il_call_operator, fb_name, il_operand_list, il_param_list, symbol_c *called_fb_declaration) |
|
271 // void *flow_control_analysis_c::visit(il_fb_call_c *symbol) |
|
272 |
|
273 |
|
274 /* | function_name '(' eol_list [il_param_list] ')' */ |
|
275 /* NOTE: The parameter 'called_function_declaration' is used to pass data between the stage 3 and stage 4. */ |
|
276 // SYM_REF2(il_formal_funct_call_c, function_name, il_param_list, symbol_c *called_function_declaration; int extensible_param_count;) |
|
277 // void *flow_control_analysis_c::visit(il_formal_funct_call_c *symbol) |
|
278 |
|
279 |
|
280 |
|
281 // void *visit(il_operand_list_c *symbol); |
|
282 void *flow_control_analysis_c::visit(simple_instr_list_c *symbol) { |
|
283 for(int i = 0; i < symbol->n; i++) { |
|
284 /* The prev_il_instruction for element[0] was set in visit(il_expression_c *) */ |
|
285 if (i>0) prev_il_instruction = symbol->elements[i-1]; |
|
286 symbol->elements[i]->accept(*this); |
|
287 } |
|
288 return NULL; |
|
289 } |
|
290 |
|
291 |
|
292 // SYM_REF1(il_simple_instruction_c, il_simple_instruction, symbol_c *prev_il_instruction;) |
|
293 void *flow_control_analysis_c::visit(il_simple_instruction_c*symbol) { |
|
294 if (NULL != prev_il_instruction) |
|
295 /* We try to guarantee that the previous il instruction that is in the previous line, will occupy the first element of the vector. |
|
296 * In order to do that, we use insert() instead of push_back() |
|
297 */ |
|
298 symbol->prev_il_instruction.insert(symbol->prev_il_instruction.begin() , prev_il_instruction); |
|
299 return NULL; |
|
300 } |
|
301 |
|
302 |
|
303 /* |
|
304 void *visit(il_param_list_c *symbol); |
|
305 void *visit(il_param_assignment_c *symbol); |
|
306 void *visit(il_param_out_assignment_c *symbol); |
|
307 */ |
|
308 |
|
309 |
|
310 |
|
311 |
|
312 /*******************/ |
|
313 /* B 2.2 Operators */ |
|
314 /*******************/ |
|
315 // void *visit( LD_operator_c *symbol); |
|
316 // void *visit( LDN_operator_c *symbol); |
|
317 // void *visit( ST_operator_c *symbol); |
|
318 // void *visit( STN_operator_c *symbol); |
|
319 // void *visit( NOT_operator_c *symbol); |
|
320 // void *visit( S_operator_c *symbol); |
|
321 // void *visit( R_operator_c *symbol); |
|
322 // void *visit( S1_operator_c *symbol); |
|
323 // void *visit( R1_operator_c *symbol); |
|
324 // void *visit( CLK_operator_c *symbol); |
|
325 // void *visit( CU_operator_c *symbol); |
|
326 // void *visit( CD_operator_c *symbol); |
|
327 // void *visit( PV_operator_c *symbol); |
|
328 // void *visit( IN_operator_c *symbol); |
|
329 // void *visit( PT_operator_c *symbol); |
|
330 // void *visit( AND_operator_c *symbol); |
|
331 // void *visit( OR_operator_c *symbol); |
|
332 // void *visit( XOR_operator_c *symbol); |
|
333 // void *visit( ANDN_operator_c *symbol); |
|
334 // void *visit( ORN_operator_c *symbol); |
|
335 // void *visit( XORN_operator_c *symbol); |
|
336 // void *visit( ADD_operator_c *symbol); |
|
337 // void *visit( SUB_operator_c *symbol); |
|
338 // void *visit( MUL_operator_c *symbol); |
|
339 // void *visit( DIV_operator_c *symbol); |
|
340 // void *visit( MOD_operator_c *symbol); |
|
341 // void *visit( GT_operator_c *symbol); |
|
342 // void *visit( GE_operator_c *symbol); |
|
343 // void *visit( EQ_operator_c *symbol); |
|
344 // void *visit( LT_operator_c *symbol); |
|
345 // void *visit( LE_operator_c *symbol); |
|
346 // void *visit( NE_operator_c *symbol); |
|
347 // void *visit( CAL_operator_c *symbol); |
|
348 // void *visit( CALC_operator_c *symbol); |
|
349 // void *visit(CALCN_operator_c *symbol); |
|
350 |
|
351 /* this next visit function will be called directly from visit(il_instruction_c *) */ |
|
352 void *flow_control_analysis_c::visit( RET_operator_c *symbol) { |
|
353 prev_il_instruction_is_JMP_or_RET = true; |
|
354 return NULL; |
|
355 } |
|
356 |
|
357 // void *visit( RETC_operator_c *symbol); |
|
358 // void *visit(RETCN_operator_c *symbol); |
|
359 |
|
360 /* this next visit function will be called from visit(il_jump_operation_c *) */ |
|
361 void *flow_control_analysis_c::visit( JMP_operator_c *symbol) { |
|
362 prev_il_instruction_is_JMP_or_RET = true; |
|
363 return NULL; |
|
364 } |
|
365 |
|
366 // void *visit( JMPC_operator_c *symbol); |
|
367 // void *visit(JMPCN_operator_c *symbol); |
|
368 |
|
369 /* Symbol class handled together with function call checks */ |
|
370 // void *visit(il_assign_operator_c *symbol, variable_name); |
|
371 /* Symbol class handled together with function call checks */ |
|
372 // void *visit(il_assign_operator_c *symbol, option, variable_name); |
|
373 |