author | berem |
Mon, 07 Dec 2009 15:36:29 +0100 | |
changeset 228 | 43831b683764 |
parent 221 | c6aed7e5f070 |
child 231 | b8527b0abe75 |
permissions | -rwxr-xr-x |
219 | 1 |
/* |
2 |
* (c) 2007 Mario de Sousa and 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 |
* This is one of the versions available for the 4th stage. |
|
27 |
* |
|
28 |
* This 4th stage generates a c++ source program equivalent |
|
29 |
* to the IL and ST code. |
|
30 |
*/ |
|
31 |
||
32 |
||
33 |
#define INLINE_RESULT_TEMP_VAR "__res" |
|
34 |
||
35 |
class generate_c_inline_c: public generate_c_typedecl_c { |
|
36 |
||
221
c6aed7e5f070
Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents:
219
diff
changeset
|
37 |
public: |
c6aed7e5f070
Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents:
219
diff
changeset
|
38 |
typedef enum { |
c6aed7e5f070
Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents:
219
diff
changeset
|
39 |
expression_vg, |
228
43831b683764
Adding support for setter and getter on complex datatype in IL
berem
parents:
221
diff
changeset
|
40 |
assignment_vg, |
43831b683764
Adding support for setter and getter on complex datatype in IL
berem
parents:
221
diff
changeset
|
41 |
complextype_base_vg, |
43831b683764
Adding support for setter and getter on complex datatype in IL
berem
parents:
221
diff
changeset
|
42 |
complextype_suffix_vg |
221
c6aed7e5f070
Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents:
219
diff
changeset
|
43 |
} variablegeneration_t; |
c6aed7e5f070
Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents:
219
diff
changeset
|
44 |
|
219 | 45 |
private: |
46 |
||
47 |
/* The name of the IL default variable... */ |
|
48 |
#define IL_DEFVAR VAR_LEADER "IL_DEFVAR" |
|
49 |
/* The name of the variable used to pass the result of a |
|
50 |
* parenthesised instruction list to the immediately preceding |
|
51 |
* scope ... |
|
52 |
*/ |
|
53 |
il_default_variable_c default_variable_name; |
|
54 |
||
228
43831b683764
Adding support for setter and getter on complex datatype in IL
berem
parents:
221
diff
changeset
|
55 |
symbol_c* current_array_type; |
43831b683764
Adding support for setter and getter on complex datatype in IL
berem
parents:
221
diff
changeset
|
56 |
|
219 | 57 |
int fcall_number; |
58 |
symbol_c *fbname; |
|
59 |
||
60 |
search_expression_type_c *search_expression_type; |
|
61 |
||
62 |
search_varfb_instance_type_c *search_varfb_instance_type; |
|
63 |
||
64 |
search_base_type_c search_base_type; |
|
65 |
||
221
c6aed7e5f070
Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents:
219
diff
changeset
|
66 |
variablegeneration_t wanted_variablegeneration; |
c6aed7e5f070
Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents:
219
diff
changeset
|
67 |
|
219 | 68 |
public: |
69 |
generate_c_inline_c(stage4out_c *s4o_ptr, symbol_c *name, symbol_c *scope, const char *variable_prefix = NULL) |
|
70 |
: generate_c_typedecl_c(s4o_ptr), |
|
71 |
default_variable_name(IL_DEFVAR, NULL) |
|
72 |
{ |
|
73 |
search_expression_type = new search_expression_type_c(scope); |
|
74 |
search_varfb_instance_type = new search_varfb_instance_type_c(scope); |
|
75 |
this->set_variable_prefix(variable_prefix); |
|
76 |
fcall_number = 0; |
|
77 |
fbname = name; |
|
221
c6aed7e5f070
Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents:
219
diff
changeset
|
78 |
wanted_variablegeneration = expression_vg; |
219 | 79 |
} |
80 |
||
81 |
virtual ~generate_c_inline_c(void) { |
|
82 |
delete search_varfb_instance_type; |
|
83 |
} |
|
84 |
||
85 |
void *generate_inline(symbol_c *function_name, |
|
86 |
symbol_c *return_data_type, |
|
87 |
std::list<FUNCTION_PARAM*> param_list) { |
|
88 |
std::list<FUNCTION_PARAM*>::iterator pt; |
|
89 |
||
90 |
fcall_number++; |
|
91 |
||
92 |
s4o.print(s4o.indent_spaces); |
|
93 |
s4o.print("inline "); |
|
94 |
return_data_type->accept(*this); |
|
95 |
s4o.print(" __"); |
|
96 |
fbname->accept(*this); |
|
97 |
s4o.print("_"); |
|
98 |
function_name->accept(*this); |
|
99 |
s4o.print_integer(fcall_number); |
|
100 |
s4o.print("("); |
|
101 |
s4o.indent_right(); |
|
102 |
||
103 |
PARAM_LIST_ITERATOR() { |
|
104 |
if (PARAM_DIRECTION == function_param_iterator_c::direction_in) { |
|
105 |
PARAM_TYPE->accept(*this); |
|
106 |
s4o.print(" "); |
|
107 |
PARAM_NAME->accept(*this); |
|
108 |
s4o.print(",\n" + s4o.indent_spaces); |
|
109 |
} |
|
110 |
} |
|
111 |
fbname->accept(*this); |
|
112 |
s4o.print(" *"); |
|
113 |
s4o.print(FB_FUNCTION_PARAM); |
|
114 |
s4o.indent_left(); |
|
115 |
s4o.print(")\n" + s4o.indent_spaces); |
|
116 |
s4o.print("{\n"); |
|
117 |
s4o.indent_right(); |
|
118 |
||
119 |
s4o.print(s4o.indent_spaces); |
|
120 |
return_data_type->accept(*this); |
|
121 |
s4o.print(" "), |
|
122 |
s4o.print(INLINE_RESULT_TEMP_VAR); |
|
123 |
s4o.print(";\n"); |
|
124 |
||
125 |
PARAM_LIST_ITERATOR() { |
|
126 |
if ((PARAM_DIRECTION == function_param_iterator_c::direction_out || |
|
127 |
PARAM_DIRECTION == function_param_iterator_c::direction_inout) && |
|
128 |
PARAM_VALUE != NULL) { |
|
129 |
s4o.print(s4o.indent_spaces); |
|
130 |
PARAM_TYPE->accept(*this); |
|
131 |
s4o.print(" "); |
|
132 |
s4o.print(TEMP_VAR); |
|
133 |
PARAM_NAME->accept(*this); |
|
134 |
s4o.print(" = "); |
|
135 |
print_check_function(PARAM_TYPE, PARAM_VALUE); |
|
136 |
s4o.print(";\n"); |
|
137 |
} |
|
138 |
} |
|
139 |
||
140 |
s4o.print(s4o.indent_spaces + INLINE_RESULT_TEMP_VAR), |
|
141 |
s4o.print(" = "); |
|
142 |
function_name->accept(*this); |
|
143 |
s4o.print("("); |
|
144 |
s4o.indent_right(); |
|
145 |
||
146 |
PARAM_LIST_ITERATOR() { |
|
147 |
if (pt != param_list.begin()) |
|
148 |
s4o.print(",\n" + s4o.indent_spaces); |
|
149 |
if (PARAM_DIRECTION == function_param_iterator_c::direction_in) |
|
150 |
PARAM_NAME->accept(*this); |
|
151 |
else if (PARAM_VALUE != NULL){ |
|
152 |
s4o.print("&"); |
|
153 |
s4o.print(TEMP_VAR); |
|
154 |
PARAM_NAME->accept(*this); |
|
155 |
} |
|
156 |
else { |
|
157 |
s4o.print("NULL"); |
|
158 |
} |
|
159 |
} |
|
160 |
s4o.print(");\n"); |
|
161 |
s4o.indent_left(); |
|
162 |
||
163 |
PARAM_LIST_ITERATOR() { |
|
164 |
if ((PARAM_DIRECTION == function_param_iterator_c::direction_out || |
|
165 |
PARAM_DIRECTION == function_param_iterator_c::direction_inout) && |
|
166 |
PARAM_VALUE != NULL) { |
|
167 |
||
168 |
s4o.print(s4o.indent_spaces); |
|
228
43831b683764
Adding support for setter and getter on complex datatype in IL
berem
parents:
221
diff
changeset
|
169 |
print_setter(PARAM_VALUE, PARAM_TYPE, PARAM_NAME); |
43831b683764
Adding support for setter and getter on complex datatype in IL
berem
parents:
221
diff
changeset
|
170 |
s4o.print(";\n"); |
219 | 171 |
} |
172 |
} |
|
173 |
s4o.print(s4o.indent_spaces + "return "); |
|
174 |
s4o.print(INLINE_RESULT_TEMP_VAR); |
|
175 |
s4o.print(";\n"); |
|
176 |
||
177 |
s4o.indent_left(); |
|
178 |
s4o.print(s4o.indent_spaces + "}\n\n"); |
|
179 |
||
180 |
return NULL; |
|
181 |
} |
|
182 |
||
183 |
private: |
|
184 |
||
228
43831b683764
Adding support for setter and getter on complex datatype in IL
berem
parents:
221
diff
changeset
|
185 |
void *print_getter(symbol_c *symbol) { |
43831b683764
Adding support for setter and getter on complex datatype in IL
berem
parents:
221
diff
changeset
|
186 |
unsigned int vartype = search_varfb_instance_type->get_vartype(symbol); |
43831b683764
Adding support for setter and getter on complex datatype in IL
berem
parents:
221
diff
changeset
|
187 |
if (vartype == search_var_instance_decl_c::external_vt) |
43831b683764
Adding support for setter and getter on complex datatype in IL
berem
parents:
221
diff
changeset
|
188 |
s4o.print(GET_EXTERNAL); |
43831b683764
Adding support for setter and getter on complex datatype in IL
berem
parents:
221
diff
changeset
|
189 |
else if (vartype == search_var_instance_decl_c::located_vt) |
43831b683764
Adding support for setter and getter on complex datatype in IL
berem
parents:
221
diff
changeset
|
190 |
s4o.print(GET_LOCATED); |
43831b683764
Adding support for setter and getter on complex datatype in IL
berem
parents:
221
diff
changeset
|
191 |
else |
43831b683764
Adding support for setter and getter on complex datatype in IL
berem
parents:
221
diff
changeset
|
192 |
s4o.print(GET_VAR); |
43831b683764
Adding support for setter and getter on complex datatype in IL
berem
parents:
221
diff
changeset
|
193 |
s4o.print("("); |
43831b683764
Adding support for setter and getter on complex datatype in IL
berem
parents:
221
diff
changeset
|
194 |
|
43831b683764
Adding support for setter and getter on complex datatype in IL
berem
parents:
221
diff
changeset
|
195 |
wanted_variablegeneration = complextype_base_vg; |
43831b683764
Adding support for setter and getter on complex datatype in IL
berem
parents:
221
diff
changeset
|
196 |
symbol->accept(*this); |
43831b683764
Adding support for setter and getter on complex datatype in IL
berem
parents:
221
diff
changeset
|
197 |
if (search_varfb_instance_type->type_is_complex()) |
43831b683764
Adding support for setter and getter on complex datatype in IL
berem
parents:
221
diff
changeset
|
198 |
s4o.print(","); |
43831b683764
Adding support for setter and getter on complex datatype in IL
berem
parents:
221
diff
changeset
|
199 |
wanted_variablegeneration = complextype_suffix_vg; |
43831b683764
Adding support for setter and getter on complex datatype in IL
berem
parents:
221
diff
changeset
|
200 |
symbol->accept(*this); |
43831b683764
Adding support for setter and getter on complex datatype in IL
berem
parents:
221
diff
changeset
|
201 |
s4o.print(")"); |
43831b683764
Adding support for setter and getter on complex datatype in IL
berem
parents:
221
diff
changeset
|
202 |
wanted_variablegeneration = expression_vg; |
43831b683764
Adding support for setter and getter on complex datatype in IL
berem
parents:
221
diff
changeset
|
203 |
return NULL; |
43831b683764
Adding support for setter and getter on complex datatype in IL
berem
parents:
221
diff
changeset
|
204 |
} |
43831b683764
Adding support for setter and getter on complex datatype in IL
berem
parents:
221
diff
changeset
|
205 |
|
43831b683764
Adding support for setter and getter on complex datatype in IL
berem
parents:
221
diff
changeset
|
206 |
void *print_setter(symbol_c* symbol, |
43831b683764
Adding support for setter and getter on complex datatype in IL
berem
parents:
221
diff
changeset
|
207 |
symbol_c* type, |
43831b683764
Adding support for setter and getter on complex datatype in IL
berem
parents:
221
diff
changeset
|
208 |
symbol_c* value) { |
43831b683764
Adding support for setter and getter on complex datatype in IL
berem
parents:
221
diff
changeset
|
209 |
unsigned int vartype = search_varfb_instance_type->get_vartype(symbol); |
43831b683764
Adding support for setter and getter on complex datatype in IL
berem
parents:
221
diff
changeset
|
210 |
if (vartype == search_var_instance_decl_c::external_vt) |
43831b683764
Adding support for setter and getter on complex datatype in IL
berem
parents:
221
diff
changeset
|
211 |
s4o.print(SET_EXTERNAL); |
43831b683764
Adding support for setter and getter on complex datatype in IL
berem
parents:
221
diff
changeset
|
212 |
else if (vartype == search_var_instance_decl_c::located_vt) |
43831b683764
Adding support for setter and getter on complex datatype in IL
berem
parents:
221
diff
changeset
|
213 |
s4o.print(SET_LOCATED); |
43831b683764
Adding support for setter and getter on complex datatype in IL
berem
parents:
221
diff
changeset
|
214 |
else |
43831b683764
Adding support for setter and getter on complex datatype in IL
berem
parents:
221
diff
changeset
|
215 |
s4o.print(SET_VAR); |
43831b683764
Adding support for setter and getter on complex datatype in IL
berem
parents:
221
diff
changeset
|
216 |
s4o.print("("); |
43831b683764
Adding support for setter and getter on complex datatype in IL
berem
parents:
221
diff
changeset
|
217 |
|
43831b683764
Adding support for setter and getter on complex datatype in IL
berem
parents:
221
diff
changeset
|
218 |
wanted_variablegeneration = complextype_base_vg; |
43831b683764
Adding support for setter and getter on complex datatype in IL
berem
parents:
221
diff
changeset
|
219 |
symbol->accept(*this); |
43831b683764
Adding support for setter and getter on complex datatype in IL
berem
parents:
221
diff
changeset
|
220 |
s4o.print(","); |
43831b683764
Adding support for setter and getter on complex datatype in IL
berem
parents:
221
diff
changeset
|
221 |
wanted_variablegeneration = expression_vg; |
43831b683764
Adding support for setter and getter on complex datatype in IL
berem
parents:
221
diff
changeset
|
222 |
print_check_function(type, value, NULL, true); |
43831b683764
Adding support for setter and getter on complex datatype in IL
berem
parents:
221
diff
changeset
|
223 |
if (search_varfb_instance_type->type_is_complex()) { |
43831b683764
Adding support for setter and getter on complex datatype in IL
berem
parents:
221
diff
changeset
|
224 |
s4o.print(","); |
43831b683764
Adding support for setter and getter on complex datatype in IL
berem
parents:
221
diff
changeset
|
225 |
wanted_variablegeneration = complextype_suffix_vg; |
43831b683764
Adding support for setter and getter on complex datatype in IL
berem
parents:
221
diff
changeset
|
226 |
symbol->accept(*this); |
43831b683764
Adding support for setter and getter on complex datatype in IL
berem
parents:
221
diff
changeset
|
227 |
} |
43831b683764
Adding support for setter and getter on complex datatype in IL
berem
parents:
221
diff
changeset
|
228 |
s4o.print(")"); |
43831b683764
Adding support for setter and getter on complex datatype in IL
berem
parents:
221
diff
changeset
|
229 |
wanted_variablegeneration = expression_vg; |
43831b683764
Adding support for setter and getter on complex datatype in IL
berem
parents:
221
diff
changeset
|
230 |
return NULL; |
43831b683764
Adding support for setter and getter on complex datatype in IL
berem
parents:
221
diff
changeset
|
231 |
} |
43831b683764
Adding support for setter and getter on complex datatype in IL
berem
parents:
221
diff
changeset
|
232 |
|
219 | 233 |
/*********************/ |
234 |
/* B 1.4 - Variables */ |
|
235 |
/*********************/ |
|
236 |
void *visit(symbolic_variable_c *symbol) { |
|
228
43831b683764
Adding support for setter and getter on complex datatype in IL
berem
parents:
221
diff
changeset
|
237 |
unsigned int vartype; |
43831b683764
Adding support for setter and getter on complex datatype in IL
berem
parents:
221
diff
changeset
|
238 |
if (wanted_variablegeneration == complextype_base_vg) |
43831b683764
Adding support for setter and getter on complex datatype in IL
berem
parents:
221
diff
changeset
|
239 |
generate_c_base_c::visit(symbol); |
43831b683764
Adding support for setter and getter on complex datatype in IL
berem
parents:
221
diff
changeset
|
240 |
else if (wanted_variablegeneration == complextype_suffix_vg) |
43831b683764
Adding support for setter and getter on complex datatype in IL
berem
parents:
221
diff
changeset
|
241 |
return NULL; |
43831b683764
Adding support for setter and getter on complex datatype in IL
berem
parents:
221
diff
changeset
|
242 |
else if (wanted_variablegeneration == expression_vg) { |
43831b683764
Adding support for setter and getter on complex datatype in IL
berem
parents:
221
diff
changeset
|
243 |
vartype = search_varfb_instance_type->get_vartype(symbol); |
221
c6aed7e5f070
Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents:
219
diff
changeset
|
244 |
if (vartype == search_var_instance_decl_c::external_vt) { |
c6aed7e5f070
Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents:
219
diff
changeset
|
245 |
s4o.print(GET_EXTERNAL); |
c6aed7e5f070
Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents:
219
diff
changeset
|
246 |
s4o.print("("); |
c6aed7e5f070
Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents:
219
diff
changeset
|
247 |
symbol->var_name->accept(*this); |
c6aed7e5f070
Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents:
219
diff
changeset
|
248 |
} |
c6aed7e5f070
Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents:
219
diff
changeset
|
249 |
else { |
c6aed7e5f070
Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents:
219
diff
changeset
|
250 |
if (vartype == search_var_instance_decl_c::located_vt) |
c6aed7e5f070
Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents:
219
diff
changeset
|
251 |
s4o.print(GET_LOCATED); |
c6aed7e5f070
Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents:
219
diff
changeset
|
252 |
else |
c6aed7e5f070
Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents:
219
diff
changeset
|
253 |
s4o.print(GET_VAR); |
c6aed7e5f070
Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents:
219
diff
changeset
|
254 |
s4o.print("("); |
c6aed7e5f070
Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents:
219
diff
changeset
|
255 |
generate_c_base_c::visit(symbol); |
c6aed7e5f070
Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents:
219
diff
changeset
|
256 |
} |
c6aed7e5f070
Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents:
219
diff
changeset
|
257 |
s4o.print(")"); |
219 | 258 |
} |
221
c6aed7e5f070
Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents:
219
diff
changeset
|
259 |
else |
219 | 260 |
generate_c_base_c::visit(symbol); |
261 |
return NULL; |
|
262 |
} |
|
263 |
||
264 |
/********************************************/ |
|
265 |
/* B.1.4.1 Directly Represented Variables */ |
|
266 |
/********************************************/ |
|
267 |
// direct_variable: direct_variable_token {$$ = new direct_variable_c($1);}; |
|
268 |
void *visit(direct_variable_c *symbol) { |
|
269 |
TRACE("direct_variable_c"); |
|
270 |
/* Do not use print_token() as it will change everything into uppercase */ |
|
271 |
if (strlen(symbol->value) == 0) ERROR; |
|
272 |
s4o.print(GET_LOCATED); |
|
273 |
s4o.print("("); |
|
274 |
this->print_variable_prefix(); |
|
275 |
s4o.printlocation(symbol->value + 1); |
|
276 |
s4o.print(")"); |
|
277 |
return NULL; |
|
278 |
} |
|
279 |
||
228
43831b683764
Adding support for setter and getter on complex datatype in IL
berem
parents:
221
diff
changeset
|
280 |
/*************************************/ |
43831b683764
Adding support for setter and getter on complex datatype in IL
berem
parents:
221
diff
changeset
|
281 |
/* B.1.4.2 Multi-element Variables */ |
43831b683764
Adding support for setter and getter on complex datatype in IL
berem
parents:
221
diff
changeset
|
282 |
/*************************************/ |
43831b683764
Adding support for setter and getter on complex datatype in IL
berem
parents:
221
diff
changeset
|
283 |
|
43831b683764
Adding support for setter and getter on complex datatype in IL
berem
parents:
221
diff
changeset
|
284 |
// SYM_REF2(structured_variable_c, record_variable, field_selector) |
43831b683764
Adding support for setter and getter on complex datatype in IL
berem
parents:
221
diff
changeset
|
285 |
void *visit(structured_variable_c *symbol) { |
43831b683764
Adding support for setter and getter on complex datatype in IL
berem
parents:
221
diff
changeset
|
286 |
TRACE("structured_variable_c"); |
43831b683764
Adding support for setter and getter on complex datatype in IL
berem
parents:
221
diff
changeset
|
287 |
switch (wanted_variablegeneration) { |
43831b683764
Adding support for setter and getter on complex datatype in IL
berem
parents:
221
diff
changeset
|
288 |
case complextype_base_vg: |
43831b683764
Adding support for setter and getter on complex datatype in IL
berem
parents:
221
diff
changeset
|
289 |
symbol->record_variable->accept(*this); |
43831b683764
Adding support for setter and getter on complex datatype in IL
berem
parents:
221
diff
changeset
|
290 |
break; |
43831b683764
Adding support for setter and getter on complex datatype in IL
berem
parents:
221
diff
changeset
|
291 |
case complextype_suffix_vg: |
43831b683764
Adding support for setter and getter on complex datatype in IL
berem
parents:
221
diff
changeset
|
292 |
symbol->record_variable->accept(*this); |
43831b683764
Adding support for setter and getter on complex datatype in IL
berem
parents:
221
diff
changeset
|
293 |
s4o.print("."); |
43831b683764
Adding support for setter and getter on complex datatype in IL
berem
parents:
221
diff
changeset
|
294 |
symbol->field_selector->accept(*this); |
43831b683764
Adding support for setter and getter on complex datatype in IL
berem
parents:
221
diff
changeset
|
295 |
break; |
43831b683764
Adding support for setter and getter on complex datatype in IL
berem
parents:
221
diff
changeset
|
296 |
default: |
43831b683764
Adding support for setter and getter on complex datatype in IL
berem
parents:
221
diff
changeset
|
297 |
if (this->is_variable_prefix_null()) { |
43831b683764
Adding support for setter and getter on complex datatype in IL
berem
parents:
221
diff
changeset
|
298 |
symbol->record_variable->accept(*this); |
43831b683764
Adding support for setter and getter on complex datatype in IL
berem
parents:
221
diff
changeset
|
299 |
s4o.print("."); |
43831b683764
Adding support for setter and getter on complex datatype in IL
berem
parents:
221
diff
changeset
|
300 |
symbol->field_selector->accept(*this); |
43831b683764
Adding support for setter and getter on complex datatype in IL
berem
parents:
221
diff
changeset
|
301 |
} |
43831b683764
Adding support for setter and getter on complex datatype in IL
berem
parents:
221
diff
changeset
|
302 |
else |
43831b683764
Adding support for setter and getter on complex datatype in IL
berem
parents:
221
diff
changeset
|
303 |
print_getter(symbol); |
43831b683764
Adding support for setter and getter on complex datatype in IL
berem
parents:
221
diff
changeset
|
304 |
break; |
43831b683764
Adding support for setter and getter on complex datatype in IL
berem
parents:
221
diff
changeset
|
305 |
} |
43831b683764
Adding support for setter and getter on complex datatype in IL
berem
parents:
221
diff
changeset
|
306 |
return NULL; |
43831b683764
Adding support for setter and getter on complex datatype in IL
berem
parents:
221
diff
changeset
|
307 |
} |
43831b683764
Adding support for setter and getter on complex datatype in IL
berem
parents:
221
diff
changeset
|
308 |
|
43831b683764
Adding support for setter and getter on complex datatype in IL
berem
parents:
221
diff
changeset
|
309 |
/* subscripted_variable '[' subscript_list ']' */ |
43831b683764
Adding support for setter and getter on complex datatype in IL
berem
parents:
221
diff
changeset
|
310 |
//SYM_REF2(array_variable_c, subscripted_variable, subscript_list) |
43831b683764
Adding support for setter and getter on complex datatype in IL
berem
parents:
221
diff
changeset
|
311 |
void *visit(array_variable_c *symbol) { |
43831b683764
Adding support for setter and getter on complex datatype in IL
berem
parents:
221
diff
changeset
|
312 |
switch (wanted_variablegeneration) { |
43831b683764
Adding support for setter and getter on complex datatype in IL
berem
parents:
221
diff
changeset
|
313 |
case complextype_base_vg: |
43831b683764
Adding support for setter and getter on complex datatype in IL
berem
parents:
221
diff
changeset
|
314 |
symbol->subscripted_variable->accept(*this); |
43831b683764
Adding support for setter and getter on complex datatype in IL
berem
parents:
221
diff
changeset
|
315 |
break; |
43831b683764
Adding support for setter and getter on complex datatype in IL
berem
parents:
221
diff
changeset
|
316 |
case complextype_suffix_vg: |
43831b683764
Adding support for setter and getter on complex datatype in IL
berem
parents:
221
diff
changeset
|
317 |
current_array_type = search_varfb_instance_type->get_rawtype(symbol->subscripted_variable); |
43831b683764
Adding support for setter and getter on complex datatype in IL
berem
parents:
221
diff
changeset
|
318 |
symbol->subscripted_variable->accept(*this); |
43831b683764
Adding support for setter and getter on complex datatype in IL
berem
parents:
221
diff
changeset
|
319 |
if (current_array_type != NULL) { |
43831b683764
Adding support for setter and getter on complex datatype in IL
berem
parents:
221
diff
changeset
|
320 |
symbol->subscript_list->accept(*this); |
43831b683764
Adding support for setter and getter on complex datatype in IL
berem
parents:
221
diff
changeset
|
321 |
current_array_type = NULL; |
43831b683764
Adding support for setter and getter on complex datatype in IL
berem
parents:
221
diff
changeset
|
322 |
} |
43831b683764
Adding support for setter and getter on complex datatype in IL
berem
parents:
221
diff
changeset
|
323 |
break; |
43831b683764
Adding support for setter and getter on complex datatype in IL
berem
parents:
221
diff
changeset
|
324 |
default: |
43831b683764
Adding support for setter and getter on complex datatype in IL
berem
parents:
221
diff
changeset
|
325 |
if (this->is_variable_prefix_null()) { |
43831b683764
Adding support for setter and getter on complex datatype in IL
berem
parents:
221
diff
changeset
|
326 |
current_array_type = search_varfb_instance_type->get_rawtype(symbol->subscripted_variable); |
43831b683764
Adding support for setter and getter on complex datatype in IL
berem
parents:
221
diff
changeset
|
327 |
symbol->subscripted_variable->accept(*this); |
43831b683764
Adding support for setter and getter on complex datatype in IL
berem
parents:
221
diff
changeset
|
328 |
if (current_array_type != NULL) { |
43831b683764
Adding support for setter and getter on complex datatype in IL
berem
parents:
221
diff
changeset
|
329 |
symbol->subscript_list->accept(*this); |
43831b683764
Adding support for setter and getter on complex datatype in IL
berem
parents:
221
diff
changeset
|
330 |
current_array_type = NULL; |
43831b683764
Adding support for setter and getter on complex datatype in IL
berem
parents:
221
diff
changeset
|
331 |
} |
43831b683764
Adding support for setter and getter on complex datatype in IL
berem
parents:
221
diff
changeset
|
332 |
} |
43831b683764
Adding support for setter and getter on complex datatype in IL
berem
parents:
221
diff
changeset
|
333 |
else |
43831b683764
Adding support for setter and getter on complex datatype in IL
berem
parents:
221
diff
changeset
|
334 |
print_getter(symbol); |
43831b683764
Adding support for setter and getter on complex datatype in IL
berem
parents:
221
diff
changeset
|
335 |
break; |
43831b683764
Adding support for setter and getter on complex datatype in IL
berem
parents:
221
diff
changeset
|
336 |
} |
43831b683764
Adding support for setter and getter on complex datatype in IL
berem
parents:
221
diff
changeset
|
337 |
return NULL; |
43831b683764
Adding support for setter and getter on complex datatype in IL
berem
parents:
221
diff
changeset
|
338 |
} |
43831b683764
Adding support for setter and getter on complex datatype in IL
berem
parents:
221
diff
changeset
|
339 |
|
219 | 340 |
/****************************************/ |
341 |
/* B.2 - Language IL (Instruction List) */ |
|
342 |
/****************************************/ |
|
343 |
||
344 |
/***********************************/ |
|
345 |
/* B 2.1 Instructions and Operands */ |
|
346 |
/***********************************/ |
|
347 |
||
348 |
void *visit(il_function_call_c *symbol) { |
|
349 |
function_declaration_c *f_decl = function_symtable.find_value(symbol->function_name); |
|
350 |
||
351 |
if (f_decl != function_symtable.end_value()) { |
|
352 |
DECLARE_PARAM_LIST() |
|
353 |
bool has_output_params = false; |
|
354 |
||
355 |
/* determine the base data type returned by the function being called... */ |
|
356 |
search_base_type_c search_base_type; |
|
357 |
symbol_c *return_data_type = (symbol_c *)f_decl->type_name->accept(search_base_type); |
|
358 |
if (NULL == return_data_type) ERROR; |
|
359 |
||
360 |
/* loop through each function parameter, find the value we should pass |
|
361 |
* to it, and then output the c equivalent... |
|
362 |
*/ |
|
363 |
||
364 |
function_param_iterator_c fp_iterator(f_decl); |
|
365 |
identifier_c *param_name; |
|
366 |
function_call_param_iterator_c function_call_param_iterator(symbol); |
|
367 |
for(int i = 1; (param_name = fp_iterator.next()) != NULL; i++) { |
|
368 |
symbol_c *param_type = fp_iterator.param_type(); |
|
369 |
if (param_type == NULL) ERROR; |
|
370 |
||
371 |
function_param_iterator_c::param_direction_t param_direction = fp_iterator.param_direction(); |
|
372 |
||
373 |
symbol_c *param_value = NULL; |
|
374 |
||
375 |
/* if it is the first parameter, semantics specifies that we should |
|
376 |
* get the value off the IL default variable! |
|
377 |
*/ |
|
378 |
if (1 == i) |
|
379 |
param_value = &this->default_variable_name; |
|
380 |
||
381 |
/* Get the value from a foo(<param_name> = <param_value>) style call */ |
|
382 |
/* NOTE: the following line of code is not required in this case, but it doesn't |
|
383 |
* harm to leave it in, as in the case of a non-formal syntax function call, |
|
384 |
* it will always return NULL. |
|
385 |
* We leave it in in case we later decide to merge this part of the code together |
|
386 |
* with the function calling code in generate_c_st_c, which does require |
|
387 |
* the following line... |
|
388 |
*/ |
|
389 |
if (param_value == NULL) |
|
390 |
param_value = function_call_param_iterator.search_f(param_name); |
|
391 |
||
392 |
/* Get the value from a foo(<param_value>) style call */ |
|
393 |
if (param_value == NULL) |
|
394 |
param_value = function_call_param_iterator.next_nf(); |
|
395 |
||
396 |
if (param_value == NULL && param_direction == function_param_iterator_c::direction_in) { |
|
397 |
/* No value given for parameter, so we must use the default... */ |
|
398 |
/* First check whether default value specified in function declaration...*/ |
|
399 |
param_value = fp_iterator.default_value(); |
|
400 |
} |
|
401 |
||
402 |
ADD_PARAM_LIST(param_name, param_value, param_type, fp_iterator.param_direction()) |
|
403 |
} /* for(...) */ |
|
404 |
||
405 |
PARAM_LIST_ITERATOR() { |
|
406 |
if ((PARAM_DIRECTION == function_param_iterator_c::direction_out || |
|
407 |
PARAM_DIRECTION == function_param_iterator_c::direction_inout) && |
|
408 |
PARAM_VALUE != NULL) { |
|
409 |
has_output_params = true; |
|
410 |
} |
|
411 |
} |
|
412 |
||
413 |
if (has_output_params) |
|
414 |
generate_inline(symbol->function_name, return_data_type, param_list); |
|
415 |
||
416 |
CLEAR_PARAM_LIST() |
|
417 |
} |
|
418 |
return NULL; |
|
419 |
} |
|
420 |
||
421 |
void *visit(il_formal_funct_call_c *symbol) { |
|
422 |
function_declaration_c *f_decl = function_symtable.find_value(symbol->function_name); |
|
423 |
||
424 |
if (f_decl != function_symtable.end_value()) { |
|
425 |
DECLARE_PARAM_LIST() |
|
426 |
bool has_output_params = false; |
|
427 |
||
428 |
/* determine the base data type returned by the function being called... */ |
|
429 |
search_base_type_c search_base_type; |
|
430 |
symbol_c *return_data_type = (symbol_c *)f_decl->type_name->accept(search_base_type); |
|
431 |
if (NULL == return_data_type) ERROR; |
|
432 |
||
433 |
/* loop through each function parameter, find the value we should pass |
|
434 |
* to it, and then output the c equivalent... |
|
435 |
*/ |
|
436 |
||
437 |
function_param_iterator_c fp_iterator(f_decl); |
|
438 |
identifier_c *param_name; |
|
439 |
function_call_param_iterator_c function_call_param_iterator(symbol); |
|
440 |
for(int i = 1; (param_name = fp_iterator.next()) != NULL; i++) { |
|
441 |
symbol_c *param_type = fp_iterator.param_type(); |
|
442 |
if (param_type == NULL) ERROR; |
|
443 |
||
444 |
function_param_iterator_c::param_direction_t param_direction = fp_iterator.param_direction(); |
|
445 |
||
446 |
||
447 |
symbol_c *param_value = NULL; |
|
448 |
||
449 |
/* Get the value from a foo(<param_name> = <param_value>) style call */ |
|
450 |
if (param_value == NULL) |
|
451 |
param_value = function_call_param_iterator.search_f(param_name); |
|
452 |
||
453 |
/* Get the value from a foo(<param_value>) style call */ |
|
454 |
/* NOTE: the following line of code is not required in this case, but it doesn't |
|
455 |
* harm to leave it in, as in the case of a formal syntax function call, |
|
456 |
* it will always return NULL. |
|
457 |
* We leave it in in case we later decide to merge this part of the code together |
|
458 |
* with the function calling code in generate_c_st_c, which does require |
|
459 |
* the following line... |
|
460 |
*/ |
|
461 |
if (param_value == NULL) |
|
462 |
param_value = function_call_param_iterator.next_nf(); |
|
463 |
||
464 |
if (param_value == NULL) { |
|
465 |
/* No value given for parameter, so we must use the default... */ |
|
466 |
/* First check whether default value specified in function declaration...*/ |
|
467 |
param_value = fp_iterator.default_value(); |
|
468 |
} |
|
469 |
||
470 |
ADD_PARAM_LIST(param_name, param_value, param_type, fp_iterator.param_direction()) |
|
471 |
} |
|
472 |
||
473 |
PARAM_LIST_ITERATOR() { |
|
474 |
if ((PARAM_DIRECTION == function_param_iterator_c::direction_out || |
|
475 |
PARAM_DIRECTION == function_param_iterator_c::direction_inout) && |
|
476 |
PARAM_VALUE != NULL) { |
|
477 |
has_output_params = true; |
|
478 |
} |
|
479 |
} |
|
480 |
||
481 |
if (has_output_params) |
|
482 |
generate_inline(symbol->function_name, return_data_type, param_list); |
|
483 |
||
484 |
CLEAR_PARAM_LIST() |
|
485 |
} |
|
486 |
return NULL; |
|
487 |
} |
|
488 |
||
489 |
/***************************************/ |
|
490 |
/* B.3 - Language ST (Structured Text) */ |
|
491 |
/***************************************/ |
|
492 |
/***********************/ |
|
493 |
/* B 3.1 - Expressions */ |
|
494 |
/***********************/ |
|
495 |
||
496 |
void *visit(function_invocation_c *symbol) { |
|
497 |
function_declaration_c *f_decl = function_symtable.find_value(symbol->function_name); |
|
498 |
||
499 |
if (f_decl != function_symtable.end_value()) { |
|
500 |
DECLARE_PARAM_LIST() |
|
501 |
bool has_output_params = false; |
|
502 |
||
503 |
symbol_c *parameter_assignment_list = NULL; |
|
504 |
if (NULL != symbol-> formal_param_list) parameter_assignment_list = symbol-> formal_param_list; |
|
505 |
if (NULL != symbol->nonformal_param_list) parameter_assignment_list = symbol->nonformal_param_list; |
|
506 |
if (NULL == parameter_assignment_list) ERROR; |
|
507 |
||
508 |
/* determine the base data type returned by the function being called... */ |
|
509 |
search_base_type_c search_base_type; |
|
510 |
symbol_c *return_data_type = (symbol_c *)f_decl->type_name->accept(search_base_type); |
|
511 |
if (NULL == return_data_type) ERROR; |
|
512 |
||
513 |
/* loop through each function parameter, find the value we should pass |
|
514 |
* to it, and then output the c equivalent... |
|
515 |
*/ |
|
516 |
function_param_iterator_c fp_iterator(f_decl); |
|
517 |
identifier_c *param_name; |
|
518 |
function_call_param_iterator_c function_call_param_iterator(symbol); |
|
519 |
for(int i = 1; (param_name = fp_iterator.next()) != NULL; i++) { |
|
520 |
||
521 |
function_param_iterator_c::param_direction_t param_direction = fp_iterator.param_direction(); |
|
522 |
||
523 |
/* Get the value from a foo(<param_name> = <param_value>) style call */ |
|
524 |
symbol_c *param_value = function_call_param_iterator.search_f(param_name); |
|
525 |
||
526 |
/* Get the value from a foo(<param_value>) style call */ |
|
527 |
if (param_value == NULL) |
|
528 |
param_value = function_call_param_iterator.next_nf(); |
|
529 |
||
530 |
if (param_value == NULL && param_direction == function_param_iterator_c::direction_in) { |
|
531 |
/* No value given for parameter, so we must use the default... */ |
|
532 |
/* First check whether default value specified in function declaration...*/ |
|
533 |
param_value = fp_iterator.default_value(); |
|
534 |
} |
|
535 |
||
536 |
symbol_c *param_type = fp_iterator.param_type(); |
|
537 |
if (param_type == NULL) ERROR; |
|
538 |
||
539 |
ADD_PARAM_LIST(param_name, param_value, param_type, param_direction) |
|
540 |
} /* for(...) */ |
|
541 |
// symbol->parameter_assignment->accept(*this); |
|
542 |
||
543 |
PARAM_LIST_ITERATOR() { |
|
544 |
if ((PARAM_DIRECTION == function_param_iterator_c::direction_out || |
|
545 |
PARAM_DIRECTION == function_param_iterator_c::direction_inout) && |
|
546 |
PARAM_VALUE != NULL) { |
|
547 |
has_output_params = true; |
|
548 |
} |
|
549 |
} |
|
550 |
||
551 |
if (has_output_params) |
|
552 |
generate_inline(symbol->function_name, return_data_type, param_list); |
|
553 |
||
554 |
CLEAR_PARAM_LIST() |
|
555 |
} |
|
556 |
return NULL; |
|
557 |
} |
|
558 |
||
559 |
}; /* generate_c_inline_c */ |
|
560 |
||
561 |
||
562 |
/***********************************************************************/ |
|
563 |
/***********************************************************************/ |
|
564 |
/***********************************************************************/ |
|
565 |
/***********************************************************************/ |
|
566 |
||
567 |
||
568 |
class generate_c_inlinefcall_c: public iterator_visitor_c { |
|
569 |
||
570 |
private: |
|
571 |
generate_c_inline_c *generate_c_inline; |
|
572 |
||
573 |
public: |
|
574 |
generate_c_inlinefcall_c(stage4out_c *s4o_ptr, symbol_c *name, symbol_c *scope, const char *variable_prefix = NULL) { |
|
575 |
generate_c_inline = new generate_c_inline_c(s4o_ptr, name, scope, variable_prefix); |
|
576 |
} |
|
577 |
||
578 |
virtual ~generate_c_inlinefcall_c(void) { |
|
579 |
delete generate_c_inline; |
|
580 |
} |
|
581 |
||
582 |
private: |
|
583 |
||
584 |
void *visit(function_invocation_c *symbol) { |
|
585 |
return symbol->accept(*generate_c_inline); |
|
586 |
} |
|
587 |
||
588 |
void *visit(il_function_call_c *symbol) { |
|
589 |
return symbol->accept(*generate_c_inline); |
|
590 |
} |
|
591 |
||
592 |
void *visit(il_formal_funct_call_c *symbol) { |
|
593 |
return symbol->accept(*generate_c_inline); |
|
594 |
} |
|
595 |
}; /* generate_c_inlinefcall_c */ |
|
596 |
||
597 |
||
598 |