1 /* |
|
2 * matiec - a compiler for the programming languages defined in IEC 61131-3 |
|
3 * |
|
4 * Copyright (C) 2009-2011 Mario de Sousa (msousa@fe.up.pt) |
|
5 * Copyright (C) 2007-2011 Laurent Bessard and Edouard Tisserant |
|
6 * |
|
7 * This program is free software: you can redistribute it and/or modify |
|
8 * it under the terms of the GNU General Public License as published by |
|
9 * the Free Software Foundation, either version 3 of the License, or |
|
10 * (at your option) any later version. |
|
11 * |
|
12 * This program is distributed in the hope that it will be useful, |
|
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
15 * GNU General Public License for more details. |
|
16 * |
|
17 * You should have received a copy of the GNU General Public License |
|
18 * along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
19 * |
|
20 * |
|
21 * This code is made available on the understanding that it will not be |
|
22 * used in safety-critical situations without a full and competent review. |
|
23 */ |
|
24 |
|
25 /* |
|
26 * An IEC 61131-3 compiler. |
|
27 * |
|
28 * Based on the |
|
29 * FINAL DRAFT - IEC 61131-3, 2nd Ed. (2001-12-10) |
|
30 * |
|
31 */ |
|
32 |
|
33 /* Verify whether the semantic rules of data type compatibility are being followed. |
|
34 * |
|
35 * For example: |
|
36 */ |
|
37 |
|
38 #include "../absyntax_utils/absyntax_utils.hh" |
|
39 |
|
40 class visit_expression_type_c: public search_constant_type_c { |
|
41 |
|
42 private: |
|
43 search_varfb_instance_type_c *search_varfb_instance_type; |
|
44 search_base_type_c search_base_type; |
|
45 |
|
46 /* When calling a function block, we must first find it's type, |
|
47 * by searching through the declarations of the variables currently |
|
48 * in scope. |
|
49 * This class does just that... |
|
50 * A new bject instance is instantiated whenever we start checking semantics |
|
51 * for a function block type declaration, or a program declaration. |
|
52 * This object instance will then later be called while the |
|
53 * function block's or the program's body is being handled. |
|
54 * |
|
55 * Note that functions cannot contain calls to function blocks, |
|
56 * so we do not create an object instance when handling |
|
57 * a function declaration. |
|
58 */ |
|
59 // search_var_instance_decl_c *search_var_instance_decl; |
|
60 |
|
61 /* This variable was created to pass information from |
|
62 * visit_expression_type_c::visit(case_statement_c *symbol) function to |
|
63 * visit_expression_type_c::visit(case_list_c *symbol) function. |
|
64 */ |
|
65 symbol_c *case_expression_type; |
|
66 |
|
67 /* In IL code, once we find a type mismatch error, it is best to |
|
68 * ignore any further errors until the end of the logicl operation, |
|
69 * i.e. until the next LD. |
|
70 * However, we cannot clear the il_error flag on all LD operations, |
|
71 * as these may also be used within parenthesis. LD operations |
|
72 * within parenthesis may not clear the error flag. |
|
73 * We therefore need a counter to know how deep inside a parenthesis |
|
74 * structure we are. |
|
75 */ |
|
76 int il_parenthesis_level; |
|
77 bool il_error; |
|
78 bool error_found; |
|
79 |
|
80 /* the current data type of the data stored in the IL stack, i.e. the default variable */ |
|
81 symbol_c *il_default_variable_type; |
|
82 /* the current IL operand being analysed - its symbol and its data type */ |
|
83 symbol_c *il_operand_type; |
|
84 symbol_c *il_operand; |
|
85 |
|
86 |
|
87 public: |
|
88 visit_expression_type_c(symbol_c *ignore); |
|
89 virtual ~visit_expression_type_c(void); |
|
90 |
|
91 bool get_error_found(void); |
|
92 |
|
93 typedef struct { |
|
94 symbol_c *value; |
|
95 symbol_c *type; |
|
96 } value_and_type_t; |
|
97 |
|
98 /* A helper function... */ |
|
99 bool is_ANY_ELEMENTARY_type (symbol_c *type_symbol); |
|
100 bool is_ANY_SAFEELEMENTARY_type (symbol_c *type_symbol); |
|
101 bool is_ANY_ELEMENTARY_compatible (symbol_c *type_symbol); |
|
102 |
|
103 bool is_ANY_MAGNITUDE_type (symbol_c *type_symbol); |
|
104 bool is_ANY_SAFEMAGNITUDE_type (symbol_c *type_symbol); |
|
105 bool is_ANY_MAGNITUDE_compatible (symbol_c *type_symbol); |
|
106 |
|
107 bool is_ANY_DATE_type (symbol_c *type_symbol); |
|
108 bool is_ANY_SAFEDATE_type (symbol_c *type_symbol); |
|
109 bool is_ANY_DATE_compatible (symbol_c *type_symbol); |
|
110 |
|
111 bool is_ANY_STRING_type (symbol_c *type_symbol); |
|
112 bool is_ANY_SAFESTRING_type (symbol_c *type_symbol); |
|
113 bool is_ANY_STRING_compatible (symbol_c *type_symbol); |
|
114 |
|
115 bool is_ANY_INT_type (symbol_c *type_symbol); |
|
116 bool is_ANY_SAFEINT_type (symbol_c *type_symbol); |
|
117 bool is_ANY_INT_compatible (symbol_c *type_symbol); |
|
118 |
|
119 bool is_ANY_REAL_type (symbol_c *type_symbol); |
|
120 bool is_ANY_SAFEREAL_type (symbol_c *type_symbol); |
|
121 bool is_ANY_REAL_compatible (symbol_c *type_symbol); |
|
122 |
|
123 bool is_ANY_NUM_type (symbol_c *type_symbol); |
|
124 bool is_ANY_SAFENUM_type (symbol_c *type_symbol); |
|
125 bool is_ANY_NUM_compatible (symbol_c *type_symbol); |
|
126 |
|
127 bool is_ANY_BIT_type (symbol_c *type_symbol); |
|
128 bool is_ANY_SAFEBIT_type (symbol_c *type_symbol); |
|
129 bool is_ANY_BIT_compatible (symbol_c *type_symbol); |
|
130 |
|
131 bool is_BOOL_type (symbol_c *type_symbol); |
|
132 bool is_SAFEBOOL_type (symbol_c *type_symbol); |
|
133 bool is_ANY_BOOL_compatible (symbol_c *type_symbol); |
|
134 |
|
135 bool is_nonneg_literal_integer_type (symbol_c *type_symbol); |
|
136 bool is_literal_integer_type (symbol_c *type_symbol); |
|
137 bool is_literal_real_type (symbol_c *type_symbol); |
|
138 bool is_literal_bool_type (symbol_c *type_symbol); |
|
139 |
|
140 bool is_ANY_ELEMENTARY_OR_ENUMERATED_compatible (symbol_c *type_symbol); |
|
141 |
|
142 /* Determine the common data type between two data types. |
|
143 * If no common data type found, return NULL. |
|
144 * |
|
145 * If data types are identical, return the first (any would do...). |
|
146 * If any of the datat types is a literal, we confirm that |
|
147 * the literal uses less bits than the fixed size data type. |
|
148 * e.g. BYTE and 1024 returns NULL |
|
149 * BYTE and 255 returns BYTE |
|
150 * |
|
151 * If two literals, then return the literal that requires more bits... |
|
152 */ |
|
153 symbol_c *common_type__(symbol_c *first_type, symbol_c *second_type); |
|
154 /* Determine the common data type between two data types. |
|
155 * Unlike the common_type__() function, we stop the compiler with an ERROR |
|
156 * if no common data type is found. |
|
157 */ |
|
158 symbol_c *common_type(symbol_c *first_type, symbol_c *second_type); |
|
159 |
|
160 symbol_c *common_literal(symbol_c *first_type, symbol_c *second_type); |
|
161 symbol_c *overloaded_return_type(symbol_c *type); |
|
162 |
|
163 /* Return TRUE if the second (value) data type may be assigned to a variable of the first (variable) data type |
|
164 * such as: |
|
165 * var_type value_type |
|
166 * BOOL BYTE#7 -> returns false |
|
167 * INT INT#7 -> returns true |
|
168 * INT 7 -> returns true |
|
169 * REAL 7.89 -> returns true |
|
170 * REAL 7 -> returns true |
|
171 * INT 7.89 -> returns false |
|
172 * SAFEBOOL BOOL#1 -> returns false !!! |
|
173 * etc... |
|
174 * |
|
175 * NOTE: It is assumed that the var_type is the data type of an lvalue |
|
176 */ |
|
177 bool is_valid_assignment(symbol_c *var_type, symbol_c *value_type); |
|
178 |
|
179 /* Return TRUE if there is a common data type, otherwise return FALSE |
|
180 * i.e., return TRUE if both data types may be used simultaneously in an expression |
|
181 * such as: |
|
182 * BOOL#0 AND BYTE#7 -> returns false |
|
183 * 0 AND BYTE#7 -> returns true |
|
184 * INT#10 AND INT#7 -> returns true |
|
185 * INT#10 AND 7 -> returns true |
|
186 * REAL#34.3 AND 7.89 -> returns true |
|
187 * REAL#34.3 AND 7 -> returns true |
|
188 * INT#10 AND 7.89 -> returns false |
|
189 * SAFEBOOL#0 AND BOOL#1 -> returns true !!! |
|
190 * etc... |
|
191 */ |
|
192 bool is_compatible_type(symbol_c *first_type, symbol_c *second_type); |
|
193 |
|
194 /* check semantics of FB call in the IL language using input operators */ |
|
195 /* e.g. CU, CLK, IN, PT, SR, ... */ |
|
196 void check_il_fbcall(symbol_c *symbol, const char *input_operator); |
|
197 /* check the semantics of a FB or Function non-formal call */ |
|
198 /* e.g. foo(1, 2, 3, 4); */ |
|
199 /* If error_count pointer is NULL, print out error messages. |
|
200 * If error_count pointer is != NULL, do not print out error messages, but tally up |
|
201 * how many errors were found. |
|
202 */ |
|
203 void check_nonformal_call(symbol_c *f_call, symbol_c *f_decl, bool use_il_defvar = false, int *error_count = NULL); |
|
204 /* check the semantics of a FB or Function formal call */ |
|
205 /* e.g. foo(IN1 := 1, OUT1 =>x, EN := true); */ |
|
206 /* If error_count pointer is NULL, print out error messages. |
|
207 * If error_count pointer is != NULL, do not print out error messages, but tally up |
|
208 * how many errors were found. |
|
209 */ |
|
210 void check_formal_call(symbol_c *f_call, symbol_c *f_decl, int *error_count = NULL); |
|
211 |
|
212 |
|
213 void *compute_standard_function_default(function_invocation_c *st_symbol, il_formal_funct_call_c *il_symbol); |
|
214 void *compute_standard_function_il(il_function_call_c *symbol, symbol_c *param_data_type); |
|
215 |
|
216 |
|
217 /* A helper function... */ |
|
218 typedef bool (visit_expression_type_c::*is_data_type_t)(symbol_c *type_symbol); /* a pointer to a function! */ |
|
219 // symbol_c *compute_boolean_expression(symbol_c *left_exp, symbol_c *right_exp, is_data_type_t is_data_type); |
|
220 // symbol_c *compute_numeric_expression(symbol_c *left_exp, symbol_c *right_exp, is_data_type_t is_data_type); |
|
221 // symbol_c *compute_expression(symbol_c *left_exp, symbol_c *right_exp, is_data_type_t is_data_type); |
|
222 symbol_c *compute_expression(symbol_c *left_type, symbol_c *right_type, is_data_type_t is_data_type, |
|
223 symbol_c *left_expr, symbol_c *right_expr); |
|
224 |
|
225 |
|
226 /* a helper function... */ |
|
227 symbol_c *base_type(symbol_c *symbol); |
|
228 |
|
229 /* a helper function... */ |
|
230 void *verify_null(symbol_c *symbol); |
|
231 |
|
232 /********************************/ |
|
233 /* B 1.3.3 - Derived data types */ |
|
234 /********************************/ |
|
235 void *visit(data_type_declaration_c *symbol); |
|
236 |
|
237 /*********************/ |
|
238 /* B 1.4 - Variables */ |
|
239 /*********************/ |
|
240 void *visit(symbolic_variable_c *symbol); |
|
241 |
|
242 /********************************************/ |
|
243 /* B 1.4.1 - Directly Represented Variables */ |
|
244 /********************************************/ |
|
245 void *visit(direct_variable_c *symbol); |
|
246 |
|
247 /*************************************/ |
|
248 /* B 1.4.2 - Multi-element variables */ |
|
249 /*************************************/ |
|
250 |
|
251 void *visit(array_variable_c *symbol); |
|
252 void *visit(structured_variable_c *symbol); |
|
253 |
|
254 /********************************/ |
|
255 /* B 1.7 Configuration elements */ |
|
256 /********************************/ |
|
257 void *visit(configuration_declaration_c *symbol); |
|
258 |
|
259 /****************************************/ |
|
260 /* B.2 - Language IL (Instruction List) */ |
|
261 /****************************************/ |
|
262 /***********************************/ |
|
263 /* B 2.1 Instructions and Operands */ |
|
264 /***********************************/ |
|
265 // void *visit(instruction_list_c *symbol); |
|
266 void *visit(il_simple_operation_c *symbol); |
|
267 void *visit(il_function_call_c *symbol); |
|
268 void *visit(il_expression_c *symbol); |
|
269 // void *visit(il_jump_operation_c *symbol); |
|
270 void *visit(il_fb_call_c *symbol); |
|
271 void *visit(il_formal_funct_call_c *symbol); |
|
272 /* |
|
273 void *visit(il_operand_list_c *symbol); |
|
274 void *visit(simple_instr_list_c *symbol); |
|
275 void *visit(il_param_list_c *symbol); |
|
276 void *visit(il_param_assignment_c *symbol); |
|
277 void *visit(il_param_out_assignment_c *symbol); |
|
278 */ |
|
279 |
|
280 /*******************/ |
|
281 /* B 2.2 Operators */ |
|
282 /*******************/ |
|
283 void *visit(LD_operator_c *symbol); |
|
284 void *visit(LDN_operator_c *symbol); |
|
285 void *visit(ST_operator_c *symbol); |
|
286 void *visit(STN_operator_c *symbol); |
|
287 void *visit(NOT_operator_c *symbol); |
|
288 void *visit(S_operator_c *symbol); |
|
289 void *visit(R_operator_c *symbol); |
|
290 void *visit(S1_operator_c *symbol); |
|
291 void *visit(R1_operator_c *symbol); |
|
292 void *visit(CLK_operator_c *symbol); |
|
293 void *visit(CU_operator_c *symbol); |
|
294 void *visit(CD_operator_c *symbol); |
|
295 void *visit(PV_operator_c *symbol); |
|
296 void *visit(IN_operator_c *symbol); |
|
297 void *visit(PT_operator_c *symbol); |
|
298 void *visit(AND_operator_c *symbol); |
|
299 void *visit(OR_operator_c *symbol); |
|
300 void *visit(XOR_operator_c *symbol); |
|
301 void *visit(ANDN_operator_c *symbol); |
|
302 void *visit(ORN_operator_c *symbol); |
|
303 void *visit(XORN_operator_c *symbol); |
|
304 void *visit(ADD_operator_c *symbol); |
|
305 void *visit(SUB_operator_c *symbol); |
|
306 void *visit(MUL_operator_c *symbol); |
|
307 void *visit(DIV_operator_c *symbol); |
|
308 void *visit(MOD_operator_c *symbol); |
|
309 void *visit(GT_operator_c *symbol); |
|
310 void *visit(GE_operator_c *symbol); |
|
311 void *visit(EQ_operator_c *symbol); |
|
312 void *visit(LT_operator_c *symbol); |
|
313 void *visit(LE_operator_c *symbol); |
|
314 void *visit(NE_operator_c *symbol); |
|
315 void *visit(CAL_operator_c *symbol); |
|
316 void *visit(CALC_operator_c *symbol); |
|
317 void *visit(CALCN_operator_c *symbol); |
|
318 void *visit(RET_operator_c *symbol); |
|
319 void *visit(RETC_operator_c *symbol); |
|
320 void *visit(RETCN_operator_c *symbol); |
|
321 void *visit(JMP_operator_c *symbol); |
|
322 void *visit(JMPC_operator_c *symbol); |
|
323 void *visit(JMPCN_operator_c *symbol); |
|
324 /* Symbol class handled together with function call checks */ |
|
325 // void *visit(il_assign_operator_c *symbol, variable_name); |
|
326 /* Symbol class handled together with function call checks */ |
|
327 // void *visit(il_assign_operator_c *symbol, option, variable_name); |
|
328 |
|
329 |
|
330 |
|
331 /***************************************/ |
|
332 /* B.3 - Language ST (Structured Text) */ |
|
333 /***************************************/ |
|
334 /***********************/ |
|
335 /* B 3.1 - Expressions */ |
|
336 /***********************/ |
|
337 void *visit(or_expression_c *symbol); |
|
338 void *visit(xor_expression_c *symbol); |
|
339 void *visit(and_expression_c *symbol); |
|
340 void *visit(equ_expression_c *symbol); |
|
341 void *visit(notequ_expression_c *symbol); |
|
342 void *visit(lt_expression_c *symbol); |
|
343 void *visit(gt_expression_c *symbol); |
|
344 void *visit(le_expression_c *symbol); |
|
345 void *visit(ge_expression_c *symbol); |
|
346 void *visit(add_expression_c *symbol); |
|
347 void *visit(sub_expression_c *symbol); |
|
348 void *visit(mul_expression_c *symbol); |
|
349 void *visit(div_expression_c *symbol); |
|
350 void *visit(mod_expression_c *symbol); |
|
351 void *visit(power_expression_c *symbol); |
|
352 void *visit(neg_expression_c *symbol); |
|
353 void *visit(not_expression_c *symbol); |
|
354 void *visit(function_invocation_c *symbol); |
|
355 |
|
356 /*********************************/ |
|
357 /* B 3.2.1 Assignment Statements */ |
|
358 /*********************************/ |
|
359 void *visit(assignment_statement_c *symbol); |
|
360 |
|
361 /*****************************************/ |
|
362 /* B 3.2.2 Subprogram Control Statements */ |
|
363 /*****************************************/ |
|
364 void *visit(fb_invocation_c *symbol); |
|
365 |
|
366 /********************************/ |
|
367 /* B 3.2.3 Selection Statements */ |
|
368 /********************************/ |
|
369 |
|
370 void *visit(if_statement_c *symbol); |
|
371 // void *visit(elseif_statement_list_c *symbol); |
|
372 void *visit(elseif_statement_c *symbol); |
|
373 void *visit(case_statement_c *symbol); |
|
374 // void *visit(case_element_list_c *symbol); |
|
375 // void *visit(case_element_c *symbol); |
|
376 void *visit(case_list_c *symbol); |
|
377 |
|
378 /********************************/ |
|
379 /* B 3.2.4 Iteration Statements */ |
|
380 /********************************/ |
|
381 |
|
382 void *visit(for_statement_c *symbol); |
|
383 void *visit(while_statement_c *symbol); |
|
384 void *visit(repeat_statement_c *symbol); |
|
385 |
|
386 |
|
387 //TODO: delete this functions. Why are they needed? |
|
388 void *visit(program_declaration_c *symbol); |
|
389 void *visit(function_declaration_c *symbol); |
|
390 void *visit(function_block_declaration_c *symbol); |
|
391 |
|
392 }; // visit_expression_type_c |
|
393 |
|