|
1 /* |
|
2 * matiec - a compiler for the programming languages defined in IEC 61131-3 |
|
3 * |
|
4 * Copyright (C) 2009-2012 Mario de Sousa (msousa@fe.up.pt) |
|
5 * Copyright (C) 2012 Manuele Conti (manuele.conti@sirius-es.it) |
|
6 * Copyright (C) 2012 Matteo Facchinetti (matteo.facchinetti@sirius-es.it) |
|
7 * |
|
8 * |
|
9 * This program is free software: you can redistribute it and/or modify |
|
10 * it under the terms of the GNU General Public License as published by |
|
11 * the Free Software Foundation, either version 3 of the License, or |
|
12 * (at your option) any later version. |
|
13 * |
|
14 * This program is distributed in the hope that it will be useful, |
|
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
17 * GNU General Public License for more details. |
|
18 * |
|
19 * You should have received a copy of the GNU General Public License |
|
20 * along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
21 * |
|
22 * |
|
23 * This code is made available on the understanding that it will not be |
|
24 * used in safety-critical situations without a full and competent review. |
|
25 */ |
|
26 |
|
27 /* |
|
28 * An IEC 61131-3 compiler. |
|
29 * |
|
30 * Based on the |
|
31 * FINAL DRAFT - IEC 61131-3, 2nd Ed. (2001-12-10) |
|
32 * |
|
33 */ |
|
34 |
|
35 |
|
36 /* NOTE: The algorithm implemented here assumes that candidate datatype lists have already been filled! |
|
37 * BEFORE running this visitor, be sure to CALL the fill_candidate_datatype_c visitor! |
|
38 */ |
|
39 |
|
40 |
|
41 /* |
|
42 * Choose, from the list of all the possible datatypes each expression may take, the single datatype that it will in fact take. |
|
43 * The resulting (chosen) datatype, will be stored in the symbol_c.datatype variable, leaving the candidate datatype list untouched! |
|
44 * |
|
45 * For rvalue expressions, this decision will be based on the datatype of the lvalue expression. |
|
46 * For lvalue expressions, the candidate datatype list should have a single entry. |
|
47 * |
|
48 * For example, the very simple literal '0' in 'foo := 0', may represent a: |
|
49 * BOOL, BYTE, WORD, DWORD, LWORD, USINT, SINT, UINT, INT, UDINT, DINT, ULINT, LINT (as well as the SAFE versions of these data tyes too!) |
|
50 * |
|
51 * In this class, the datatype of '0' will be set to the same datatype as the 'foo' variable. |
|
52 * If the intersection of the candidate datatype lists of the left and right side expressions is empty, |
|
53 * then a datatype error has been found, and the datatype is either left at NULL, or set to a pointer of an invalid_type_name_c object! |
|
54 */ |
|
55 |
|
56 |
|
57 #include "../absyntax_utils/absyntax_utils.hh" |
|
58 #include "datatype_functions.hh" |
|
59 |
|
60 class narrow_candidate_datatypes_c: public iterator_visitor_c { |
|
61 |
|
62 private: |
|
63 search_varfb_instance_type_c *search_varfb_instance_type; |
|
64 search_base_type_c search_base_type; |
|
65 symbol_c *il_operand; |
|
66 il_instruction_c *fake_prev_il_instruction; |
|
67 std::vector <symbol_c *> *prev_il_instructions; |
|
68 std::vector <symbol_c *> *prev_il_instructions_intersected_datatypes; |
|
69 |
|
70 bool is_widening_compatible(const struct widen_entry widen_table[], symbol_c *left_type, symbol_c *right_type, symbol_c *result_type, bool *deprecated_status = NULL); |
|
71 |
|
72 void narrow_function_invocation(symbol_c *f_call, generic_function_call_t fcall_data); |
|
73 void narrow_nonformal_call(symbol_c *f_call, symbol_c *f_decl, int *ext_parm_count = NULL); |
|
74 void narrow_formal_call(symbol_c *f_call, symbol_c *f_decl, int *ext_parm_count = NULL); |
|
75 void *narrow_implicit_il_fb_call(symbol_c *il_instruction, const char *param_name, symbol_c *&called_fb_declaration); |
|
76 |
|
77 void *handle_il_instruction(symbol_c *symbol); |
|
78 void *narrow_binary_operator (const struct widen_entry widen_table[], symbol_c *symbol, bool *deprecated_operation = NULL); |
|
79 void *narrow_binary_expression(const struct widen_entry widen_table[], symbol_c *symbol, symbol_c *l_expr, symbol_c *r_expr, bool *deprecated_operation = NULL); |
|
80 |
|
81 void *narrow_conditional_flow_control_IL_instruction(symbol_c *symbol); |
|
82 |
|
83 |
|
84 public: |
|
85 narrow_candidate_datatypes_c(symbol_c *ignore); |
|
86 virtual ~narrow_candidate_datatypes_c(void); |
|
87 |
|
88 symbol_c *base_type(symbol_c *symbol); |
|
89 |
|
90 /**********************/ |
|
91 /* B 1.3 - Data types */ |
|
92 /**********************/ |
|
93 /********************************/ |
|
94 /* B 1.3.3 - Derived data types */ |
|
95 /********************************/ |
|
96 void *visit(subrange_c *symbol); |
|
97 void *visit(simple_spec_init_c *symbol); |
|
98 |
|
99 /*********************/ |
|
100 /* B 1.4 - Variables */ |
|
101 /*********************/ |
|
102 /********************************************/ |
|
103 /* B 1.4.1 - Directly Represented Variables */ |
|
104 /********************************************/ |
|
105 /*************************************/ |
|
106 /* B 1.4.2 - Multi-element variables */ |
|
107 /*************************************/ |
|
108 void *visit(array_variable_c *symbol); |
|
109 void *visit(subscript_list_c *symbol); |
|
110 |
|
111 /******************************************/ |
|
112 /* B 1.4.3 - Declaration & Initialisation */ |
|
113 /******************************************/ |
|
114 void *visit(var1_list_c *symbol); |
|
115 void *visit(location_c *symbol); |
|
116 void *visit(located_var_decl_c *symbol); |
|
117 |
|
118 /**************************************/ |
|
119 /* B 1.5 - Program organization units */ |
|
120 /**************************************/ |
|
121 /***********************/ |
|
122 /* B 1.5.1 - Functions */ |
|
123 /***********************/ |
|
124 void *visit(function_declaration_c *symbol); |
|
125 |
|
126 /*****************************/ |
|
127 /* B 1.5.2 - Function blocks */ |
|
128 /*****************************/ |
|
129 void *visit(function_block_declaration_c *symbol); |
|
130 |
|
131 /**********************/ |
|
132 /* B 1.5.3 - Programs */ |
|
133 /**********************/ |
|
134 void *visit(program_declaration_c *symbol); |
|
135 |
|
136 /********************************/ |
|
137 /* B 1.7 Configuration elements */ |
|
138 /********************************/ |
|
139 void *visit(configuration_declaration_c *symbol); |
|
140 /****************************************/ |
|
141 /* B.2 - Language IL (Instruction List) */ |
|
142 /****************************************/ |
|
143 /***********************************/ |
|
144 /* B 2.1 Instructions and Operands */ |
|
145 /***********************************/ |
|
146 void *visit(instruction_list_c *symbol); |
|
147 void *visit(il_instruction_c *symbol); |
|
148 void *visit(il_simple_operation_c *symbol); |
|
149 void *visit(il_function_call_c *symbol); |
|
150 void *visit(il_expression_c *symbol); |
|
151 void *visit(il_jump_operation_c *symbol); |
|
152 void *visit(il_fb_call_c *symbol); |
|
153 void *visit(il_formal_funct_call_c *symbol); |
|
154 // void *visit(il_operand_list_c *symbol); |
|
155 void *visit(simple_instr_list_c *symbol); |
|
156 void *visit(il_simple_instruction_c*symbol); |
|
157 // void *visit(il_param_list_c *symbol); |
|
158 // void *visit(il_param_assignment_c *symbol); |
|
159 // void *visit(il_param_out_assignment_c *symbol); |
|
160 |
|
161 /*******************/ |
|
162 /* B 2.2 Operators */ |
|
163 /*******************/ |
|
164 void *visit(LD_operator_c *symbol); |
|
165 void *visit(LDN_operator_c *symbol); |
|
166 void *visit(ST_operator_c *symbol); |
|
167 void *visit(STN_operator_c *symbol); |
|
168 void *visit(NOT_operator_c *symbol); |
|
169 void *visit(S_operator_c *symbol); |
|
170 void *visit(R_operator_c *symbol); |
|
171 void *visit(S1_operator_c *symbol); |
|
172 void *visit(R1_operator_c *symbol); |
|
173 void *visit(CLK_operator_c *symbol); |
|
174 void *visit(CU_operator_c *symbol); |
|
175 void *visit(CD_operator_c *symbol); |
|
176 void *visit(PV_operator_c *symbol); |
|
177 void *visit(IN_operator_c *symbol); |
|
178 void *visit(PT_operator_c *symbol); |
|
179 void *visit(AND_operator_c *symbol); |
|
180 void *visit(OR_operator_c *symbol); |
|
181 void *visit(XOR_operator_c *symbol); |
|
182 void *visit(ANDN_operator_c *symbol); |
|
183 void *visit(ORN_operator_c *symbol); |
|
184 void *visit(XORN_operator_c *symbol); |
|
185 void *visit(ADD_operator_c *symbol); |
|
186 void *visit(SUB_operator_c *symbol); |
|
187 void *visit(MUL_operator_c *symbol); |
|
188 void *visit(DIV_operator_c *symbol); |
|
189 void *visit(MOD_operator_c *symbol); |
|
190 void *visit(GT_operator_c *symbol); |
|
191 void *visit(GE_operator_c *symbol); |
|
192 void *visit(EQ_operator_c *symbol); |
|
193 void *visit(LT_operator_c *symbol); |
|
194 void *visit(LE_operator_c *symbol); |
|
195 void *visit(NE_operator_c *symbol); |
|
196 void *visit(CAL_operator_c *symbol); |
|
197 void *visit(CALC_operator_c *symbol); |
|
198 void *visit(CALCN_operator_c *symbol); |
|
199 void *visit(RET_operator_c *symbol); |
|
200 void *visit(RETC_operator_c *symbol); |
|
201 void *visit(RETCN_operator_c *symbol); |
|
202 void *visit(JMP_operator_c *symbol); |
|
203 void *visit(JMPC_operator_c *symbol); |
|
204 void *visit(JMPCN_operator_c *symbol); |
|
205 /* Symbol class handled together with function call checks */ |
|
206 // void *visit(il_assign_operator_c *symbol, variable_name); |
|
207 /* Symbol class handled together with function call checks */ |
|
208 // void *visit(il_assign_operator_c *symbol, option, variable_name); |
|
209 /***************************************/ |
|
210 /* B.3 - Language ST (Structured Text) */ |
|
211 /***************************************/ |
|
212 /***********************/ |
|
213 /* B 3.1 - Expressions */ |
|
214 /***********************/ |
|
215 void *visit(or_expression_c *symbol); |
|
216 void *visit(xor_expression_c *symbol); |
|
217 void *visit(and_expression_c *symbol); |
|
218 void *visit(equ_expression_c *symbol); |
|
219 void *visit(notequ_expression_c *symbol); |
|
220 void *visit(lt_expression_c *symbol); |
|
221 void *visit(gt_expression_c *symbol); |
|
222 void *visit(le_expression_c *symbol); |
|
223 void *visit(ge_expression_c *symbol); |
|
224 void *visit(add_expression_c *symbol); |
|
225 void *visit(sub_expression_c *symbol); |
|
226 void *visit(mul_expression_c *symbol); |
|
227 void *visit(div_expression_c *symbol); |
|
228 void *visit(mod_expression_c *symbol); |
|
229 void *visit(power_expression_c *symbol); |
|
230 void *visit(neg_expression_c *symbol); |
|
231 void *visit(not_expression_c *symbol); |
|
232 |
|
233 void *visit(function_invocation_c *symbol); |
|
234 |
|
235 /*********************************/ |
|
236 /* B 3.2.1 Assignment Statements */ |
|
237 /*********************************/ |
|
238 void *visit(assignment_statement_c *symbol); |
|
239 |
|
240 /*****************************************/ |
|
241 /* B 3.2.2 Subprogram Control Statements */ |
|
242 /*****************************************/ |
|
243 void *visit(fb_invocation_c *symbol); |
|
244 |
|
245 /********************************/ |
|
246 /* B 3.2.3 Selection Statements */ |
|
247 /********************************/ |
|
248 void *visit(if_statement_c *symbol); |
|
249 void *visit(elseif_statement_c *symbol); |
|
250 void *visit(case_statement_c *symbol); |
|
251 void *visit(case_element_list_c *symbol); |
|
252 void *visit(case_element_c *symbol); |
|
253 void *visit(case_list_c *symbol); |
|
254 |
|
255 /********************************/ |
|
256 /* B 3.2.4 Iteration Statements */ |
|
257 /********************************/ |
|
258 void *visit(for_statement_c *symbol); |
|
259 void *visit(while_statement_c *symbol); |
|
260 void *visit(repeat_statement_c *symbol); |
|
261 |
|
262 }; // narrow_candidate_datatypes_c |
|
263 |
|
264 |
|
265 |
|
266 |
|
267 |
|
268 |
|
269 |
|
270 |