author | lbessard |
Fri, 19 Dec 2008 15:09:29 +0100 | |
changeset 160 | 59d58f5e6caa |
parent 149 | 05ca171a3d57 |
child 169 | bea932bc60b0 |
permissions | -rwxr-xr-x |
70 | 1 |
/* |
2 |
* (c) 2003 Mario de Sousa |
|
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 |
/* |
|
27 |
* Conversion of il statements (i.e. IL code). |
|
28 |
* |
|
29 |
* This is part of the 4th stage that generates |
|
30 |
* a c++ source program equivalent to the IL and ST |
|
31 |
* code. |
|
32 |
*/ |
|
33 |
||
34 |
||
35 |
||
36 |
||
37 |
/***********************************************************************/ |
|
38 |
/***********************************************************************/ |
|
39 |
/***********************************************************************/ |
|
40 |
/***********************************************************************/ |
|
41 |
||
42 |
/* Returns the data type of an il_operand. |
|
43 |
* |
|
44 |
* Note that the il_operand may be a variable, in which case |
|
45 |
* we return the type of the variable instance. |
|
46 |
* The il_operand may also be a constant, in which case |
|
47 |
* we return the data type of that constant. |
|
48 |
* |
|
49 |
* The variable instance may be a member of a structured variable, |
|
50 |
* or an element in an array, or any combination of the two. |
|
51 |
* |
|
52 |
* The class constructor must be given the search scope |
|
53 |
* (function, function block or program within which |
|
54 |
* the possible il_operand variable instance was declared). |
|
55 |
*/ |
|
56 |
||
57 |
class search_il_operand_type_c { |
|
58 |
||
59 |
private: |
|
60 |
search_varfb_instance_type_c search_varfb_instance_type; |
|
61 |
search_constant_type_c search_constant_type; |
|
62 |
||
63 |
public: |
|
64 |
search_il_operand_type_c(symbol_c *search_scope): search_varfb_instance_type(search_scope) {} |
|
65 |
||
66 |
public: |
|
67 |
symbol_c *get_type(symbol_c *il_operand) { |
|
68 |
symbol_c *res; |
|
69 |
||
70 |
/* We first assume that it is a constant... */ |
|
71 |
res = search_constant_type.get_type(il_operand); |
|
72 |
if (res != NULL) return res; |
|
73 |
||
74 |
/* Nope, now we assume it is a variable, and determine its type... */ |
|
75 |
res = search_varfb_instance_type.get_type(il_operand); |
|
76 |
if (NULL != res) return res; |
|
77 |
||
78 |
/* not found */ |
|
79 |
return NULL; |
|
80 |
} |
|
81 |
}; |
|
82 |
||
83 |
||
84 |
||
85 |
/***********************************************************************/ |
|
86 |
/***********************************************************************/ |
|
87 |
/***********************************************************************/ |
|
88 |
/***********************************************************************/ |
|
89 |
||
90 |
||
91 |
/* A new class to ouput the il default variable to c++ code |
|
92 |
* We use this class, inheriting from symbol_c, so it may be used |
|
93 |
* as any other symbol_c object in the intermediate parse tree, |
|
94 |
* more specifically, so it can be used as any other il operand. |
|
95 |
* This makes the rest of the code much easier... |
|
96 |
* |
|
97 |
* Nevertheless, the basic visitor class visitor_c does not know |
|
98 |
* how to visit this new il_default_variable_c class, so we have |
|
99 |
* to extend that too. |
|
100 |
* In reality extending the basic symbols doesn't quite work out |
|
101 |
* as cleanly as desired (we need to use dynamic_cast in the |
|
102 |
* accept method of the il_default_variable_c), but it is cleaner |
|
103 |
* than the alternative... |
|
104 |
*/ |
|
105 |
class il_default_variable_c; |
|
106 |
||
107 |
/* This visitor class is not really required, we could place the |
|
108 |
* visit() method directly in genertae_cc_il_c, but doing it in |
|
109 |
* a seperate class makes the architecture more evident... |
|
110 |
*/ |
|
111 |
class il_default_variable_visitor_c { |
|
112 |
public: |
|
113 |
virtual void *visit(il_default_variable_c *symbol) = 0; |
|
114 |
||
115 |
virtual ~il_default_variable_visitor_c(void) {return;} |
|
116 |
}; |
|
117 |
||
118 |
||
119 |
/* A class to print out to the resulting C++ code |
|
120 |
* the IL default variable name. |
|
121 |
* |
|
122 |
* It includes a reference to its name, |
|
123 |
* and the data type of the data currently stored |
|
124 |
* in this C++ variable... This is required because the |
|
125 |
* C++ variable is a union, and we must know which member |
|
126 |
* of the union top reference!! |
|
127 |
* |
|
128 |
* Note that we also need to keep track of the data type of |
|
129 |
* the value currently being stored in the default variable. |
|
130 |
* This is required so we can process parenthesis, |
|
131 |
* |
|
132 |
* e.g. : |
|
133 |
* LD var1 |
|
134 |
* AND ( |
|
135 |
* LD var2 |
|
136 |
* OR var3 |
|
137 |
* ) |
|
138 |
* |
|
139 |
* Note that we only execute the 'AND (' operation when we come across |
|
140 |
* the ')', i.e. once we have evaluated the result of the |
|
141 |
* instructions inside the parenthesis. |
|
142 |
* When we do execute the 'AND (' operation, we need to know the data type |
|
143 |
* of the operand, which in this case is the result of the evaluation of the |
|
144 |
* instruction list inside the parenthesis. We can only know this if we |
|
145 |
* keep track of the data type currently stored in the default variable! |
|
146 |
* |
|
147 |
* We use the current_type inside the generate_c_il::default_variable_name variable |
|
148 |
* to track this! |
|
149 |
*/ |
|
150 |
class il_default_variable_c: public symbol_c { |
|
151 |
public: |
|
152 |
symbol_c *var_name; /* in principle, this should point to an indentifier_c */ |
|
153 |
symbol_c *current_type; |
|
154 |
||
155 |
public: |
|
156 |
il_default_variable_c(const char *var_name_str, symbol_c *current_type); |
|
157 |
virtual void *accept(visitor_c &visitor); |
|
158 |
}; |
|
159 |
||
160 |
||
161 |
/***********************************************************************/ |
|
162 |
/***********************************************************************/ |
|
163 |
/***********************************************************************/ |
|
164 |
/***********************************************************************/ |
|
165 |
||
166 |
||
167 |
||
168 |
class generate_c_il_c: public generate_c_typedecl_c, il_default_variable_visitor_c { |
|
169 |
||
170 |
private: |
|
171 |
/* When compiling il code, it becomes necessary to determine the |
|
172 |
* data type of il operands. To do this, we must first find the |
|
173 |
* il operand's declaration, within the scope of the function block |
|
174 |
* or function currently being processed. |
|
175 |
* The following object does just that... |
|
176 |
* This object instance will then later be called while the |
|
177 |
* remaining il code is being handled. |
|
178 |
*/ |
|
179 |
//search_il_operand_type_c *search_il_operand_type; |
|
180 |
search_expression_type_c *search_expression_type; |
|
181 |
||
182 |
/* The initial value that should be given to the IL default variable |
|
183 |
* imediately after a parenthesis is opened. |
|
184 |
* This variable is only used to pass data from the |
|
185 |
* il_expression_c visitor to the simple_instr_list_c visitor. |
|
186 |
* |
|
187 |
* e.g.: |
|
188 |
* LD var1 |
|
189 |
* AND ( var2 |
|
190 |
* OR var3 |
|
191 |
* ) |
|
192 |
* |
|
193 |
* In the above code sample, the line 'AND ( var2' constitutes |
|
194 |
* an il_expression_c, where var2 should be loaded into the |
|
195 |
* il default variable before continuing with the expression |
|
196 |
* inside the parenthesis. |
|
197 |
* Unfortunately, only the simple_instr_list_c may do the |
|
198 |
* initial laoding of the var2 bariable following the parenthesis, |
|
199 |
* so the il_expression_c visitor will have to pass 'var2' as a |
|
200 |
* parameter to the simple_instr_list_c visitor. |
|
201 |
* Ergo, the existance of the following parameter...! |
|
202 |
*/ |
|
203 |
symbol_c *il_default_variable_init_value; |
|
204 |
||
205 |
/* Operand to the IL operation currently being processed... */ |
|
206 |
/* These variables are used to pass data from the |
|
207 |
* il_simple_operation_c and il_expression_c visitors |
|
208 |
* to the il operator visitors (i.e. LD_operator_c, |
|
209 |
* LDN_operator_c, ST_operator_c, STN_operator_c, ...) |
|
210 |
*/ |
|
211 |
symbol_c *current_operand; |
|
212 |
symbol_c *current_operand_type; |
|
213 |
||
214 |
/* Label to which the current IL jump operation should jump to... */ |
|
215 |
/* This variable is used to pass data from the |
|
216 |
* il_jump_operation_c visitor |
|
217 |
* to the il jump operator visitors (i.e. JMP_operator_c, |
|
218 |
* JMPC_operator_c, JMPCN_operator_c, ...) |
|
219 |
*/ |
|
220 |
symbol_c *jump_label; |
|
221 |
||
222 |
/* The result of the comparison IL operations (GT, EQ, LT, ...) |
|
223 |
* is a boolean variable. |
|
224 |
* This class keeps track of the current data type stored in the |
|
225 |
* il default variable. This is usually done by keeping a reference |
|
226 |
* to the data type of the last operand. Nevertheless, in the case of |
|
227 |
* the comparison IL operators, the data type of the result (a boolean) |
|
228 |
* is not the data type of the operand. We therefore need an object |
|
229 |
* of the boolean data type to keep as a reference of the current |
|
230 |
* data type. |
|
231 |
* The following object is it... |
|
232 |
*/ |
|
233 |
bool_type_name_c bool_type; |
|
234 |
||
235 |
/* the data type of the IL default variable... */ |
|
236 |
#define IL_DEFVAR_T VAR_LEADER "IL_DEFVAR_T" |
|
237 |
/* The name of the IL default variable... */ |
|
238 |
#define IL_DEFVAR VAR_LEADER "IL_DEFVAR" |
|
239 |
/* The name of the variable used to pass the result of a |
|
240 |
* parenthesised instruction list to the immediately preceding |
|
241 |
* scope ... |
|
242 |
*/ |
|
243 |
#define IL_DEFVAR_BACK VAR_LEADER "IL_DEFVAR_BACK" |
|
244 |
il_default_variable_c default_variable_name; |
|
245 |
il_default_variable_c default_variable_back_name; |
|
246 |
||
247 |
/* Some function calls in the body of functions or function blocks |
|
248 |
* may leave some parameters to their default values, and |
|
249 |
* ignore some output parameters of the function being called. |
|
250 |
* Our conversion of ST functions to C++ does not contemplate that, |
|
251 |
* i.e. each called function must get all it's input and output |
|
252 |
* parameters set correctly. |
|
253 |
* For input parameters we merely need to call the function with |
|
254 |
* the apropriate default value, but for output parameters |
|
255 |
* we must create temporary variables to hold the output value. |
|
256 |
* |
|
257 |
* We declare all the temporary output variables at the begining of |
|
258 |
* the body of each function or function block, and use them as |
|
259 |
* in function calls later on as they become necessary... |
|
260 |
* Note that we cannot create these variables just before a function |
|
261 |
* call, as the function call itself may be integrated within an |
|
262 |
* expression, or another function call! |
|
263 |
* |
|
264 |
* The variables are declared in the exact same order in which they |
|
265 |
* will be used later on during the function calls, which allows us |
|
266 |
* to simply re-create the name that was used for the temporary variable |
|
267 |
* instead of keeping it in some list. |
|
268 |
* The names are recreated by the temp_var_name_factory, after reset() |
|
269 |
* has been called! |
|
270 |
* |
|
271 |
* This function will genertae code similar to... |
|
272 |
* |
|
273 |
* INT __TMP_0 = 23; |
|
274 |
* REAL __TMP_1 = 45.5; |
|
275 |
* ... |
|
276 |
*/ |
|
277 |
temp_var_name_c temp_var_name_factory; |
|
278 |
||
279 |
/* When calling a function block, we must first find it's type, |
|
280 |
* by searching through the declarations of the variables currently |
|
281 |
* in scope. |
|
282 |
* This class does just that... |
|
283 |
* A new class is instantiated whenever we begin generating the code |
|
284 |
* for a function block type declaration, or a program declaration. |
|
285 |
* This object instance will then later be called while the |
|
286 |
* function block's or the program's body is being handled. |
|
287 |
* |
|
288 |
* Note that functions cannot contain calls to function blocks, |
|
289 |
* so we do not create an object instance when handling |
|
290 |
* a function declaration. |
|
291 |
*/ |
|
292 |
search_fb_instance_decl_c *search_fb_instance_decl; |
|
293 |
||
98 | 294 |
search_varfb_instance_type_c *search_varfb_instance_type; |
295 |
||
296 |
search_base_type_c search_base_type; |
|
70 | 297 |
|
146
eef5e62048c7
Adding support for EN/ENO params in function and function blocks (standard function not supported yet)
lbessard
parents:
123
diff
changeset
|
298 |
bool current_param_is_pointer; |
eef5e62048c7
Adding support for EN/ENO params in function and function blocks (standard function not supported yet)
lbessard
parents:
123
diff
changeset
|
299 |
|
70 | 300 |
public: |
301 |
generate_c_il_c(stage4out_c *s4o_ptr, symbol_c *scope, const char *variable_prefix = NULL) |
|
302 |
: generate_c_typedecl_c(s4o_ptr), |
|
303 |
default_variable_name(IL_DEFVAR, NULL), |
|
304 |
default_variable_back_name(IL_DEFVAR_BACK, NULL) |
|
305 |
{ |
|
306 |
//search_il_operand_type = new search_il_operand_type_c(scope); |
|
307 |
search_expression_type = new search_expression_type_c(scope); |
|
308 |
search_fb_instance_decl = new search_fb_instance_decl_c(scope); |
|
98 | 309 |
search_varfb_instance_type = new search_varfb_instance_type_c(scope); |
70 | 310 |
current_operand = NULL; |
311 |
current_operand_type = NULL; |
|
312 |
il_default_variable_init_value = NULL; |
|
146
eef5e62048c7
Adding support for EN/ENO params in function and function blocks (standard function not supported yet)
lbessard
parents:
123
diff
changeset
|
313 |
current_param_is_pointer = false; |
70 | 314 |
this->set_variable_prefix(variable_prefix); |
315 |
} |
|
316 |
||
317 |
virtual ~generate_c_il_c(void) { |
|
318 |
delete search_fb_instance_decl; |
|
319 |
//delete search_il_operand_type; |
|
320 |
delete search_expression_type; |
|
98 | 321 |
delete search_varfb_instance_type; |
70 | 322 |
} |
323 |
||
324 |
void generate(instruction_list_c *il) { |
|
325 |
generate_c_tempvardecl_c generate_c_tempvardecl(&s4o); |
|
326 |
generate_c_tempvardecl.generate(il, &temp_var_name_factory); |
|
327 |
il->accept(*this); |
|
328 |
} |
|
329 |
||
330 |
/* Declare the backup to the default variable, that will store the result |
|
331 |
* of the IL operations executed inside a parenthesis... |
|
332 |
*/ |
|
333 |
void declare_backup_variable(void) { |
|
334 |
s4o.print(s4o.indent_spaces); |
|
335 |
s4o.print(IL_DEFVAR_T); |
|
336 |
s4o.print(" "); |
|
337 |
print_backup_variable(); |
|
338 |
s4o.print(";\n"); |
|
339 |
} |
|
340 |
||
341 |
void print_backup_variable(void) { |
|
342 |
this->default_variable_back_name.accept(*this); |
|
343 |
} |
|
344 |
||
345 |
private: |
|
346 |
/* A helper function... */ |
|
347 |
/* |
|
348 |
bool is_bool_type(symbol_c *type_symbol) { |
|
349 |
return (NULL != dynamic_cast<bool_type_name_c *>(type_symbol)); |
|
350 |
} |
|
351 |
*/ |
|
352 |
||
353 |
/* A helper function... */ |
|
354 |
void *XXX_operator(symbol_c *lo, const char *op, symbol_c *ro) { |
|
355 |
if ((NULL == lo) || (NULL == ro)) ERROR; |
|
356 |
if (NULL == op) ERROR; |
|
357 |
||
358 |
lo->accept(*this); |
|
359 |
s4o.print(op); |
|
360 |
ro->accept(*this); |
|
361 |
return NULL; |
|
362 |
} |
|
363 |
||
364 |
/* A helper function... */ |
|
365 |
void *XXX_function(const char *func, symbol_c *lo, symbol_c *ro) { |
|
366 |
if ((NULL == lo) || (NULL == ro)) ERROR; |
|
367 |
if (NULL == func) ERROR; |
|
368 |
||
369 |
lo->accept(*this); |
|
370 |
s4o.print(" = "); |
|
371 |
s4o.print(func); |
|
372 |
s4o.print("("); |
|
373 |
lo->accept(*this); |
|
374 |
s4o.print(", "); |
|
375 |
ro->accept(*this); |
|
376 |
s4o.print(")"); |
|
377 |
return NULL; |
|
378 |
} |
|
379 |
||
380 |
/* A helper function... */ |
|
381 |
void *XXX_CAL_operator(const char *param_name, symbol_c *fb_name) { |
|
382 |
if (NULL == fb_name) ERROR; |
|
383 |
symbolic_variable_c *sv = dynamic_cast<symbolic_variable_c *>(fb_name); |
|
384 |
if (NULL == sv) ERROR; |
|
385 |
identifier_c *id = dynamic_cast<identifier_c *>(sv->var_name); |
|
386 |
if (NULL == id) ERROR; |
|
387 |
||
388 |
identifier_c param(param_name); |
|
389 |
||
390 |
//SYM_REF3(il_param_assignment_c, il_assign_operator, il_operand, simple_instr_list) |
|
391 |
il_param_assignment_c il_param_assignment(¶m, &this->default_variable_name, NULL); |
|
392 |
// SYM_LIST(il_param_list_c) |
|
393 |
il_param_list_c il_param_list; |
|
394 |
il_param_list.add_element(&il_param_assignment); |
|
395 |
CAL_operator_c CAL_operator; |
|
396 |
// SYM_REF4(il_fb_call_c, il_call_operator, fb_name, il_operand_list, il_param_list) |
|
397 |
il_fb_call_c il_fb_call(&CAL_operator, id, NULL, &il_param_list); |
|
398 |
||
399 |
il_fb_call.accept(*this); |
|
400 |
return NULL; |
|
401 |
} |
|
402 |
||
403 |
/* A helper function... */ |
|
404 |
void *CMP_operator(symbol_c *o, const char *operation) { |
|
405 |
if (NULL == o) ERROR; |
|
406 |
if (NULL == this->default_variable_name.current_type) ERROR; |
|
407 |
||
408 |
symbol_c *backup = this->default_variable_name.current_type; |
|
409 |
this->default_variable_name.current_type = &(this->bool_type); |
|
410 |
this->default_variable_name.accept(*this); |
|
411 |
this->default_variable_name.current_type = backup; |
|
412 |
||
413 |
s4o.print(" = "); |
|
414 |
s4o.print(operation); |
|
415 |
this->default_variable_name.current_type->accept(*this); |
|
149 | 416 |
s4o.print("(__BOOL_LITERAL(TRUE), NULL, 2, "); |
70 | 417 |
this->default_variable_name.accept(*this); |
418 |
s4o.print(", "); |
|
419 |
o->accept(*this); |
|
420 |
s4o.print(")"); |
|
421 |
||
422 |
/* the data type resulting from this operation... */ |
|
423 |
this->default_variable_name.current_type = &(this->bool_type); |
|
424 |
return NULL; |
|
425 |
} |
|
426 |
||
427 |
||
428 |
/* A helper function... */ |
|
429 |
void C_modifier(void) { |
|
430 |
if (search_expression_type->is_bool_type(default_variable_name.current_type)) { |
|
431 |
s4o.print("if ("); |
|
432 |
this->default_variable_name.accept(*this); |
|
433 |
s4o.print(") "); |
|
434 |
} |
|
435 |
else {ERROR;} |
|
436 |
} |
|
437 |
||
438 |
/* A helper function... */ |
|
439 |
void CN_modifier(void) { |
|
440 |
if (search_expression_type->is_bool_type(default_variable_name.current_type)) { |
|
441 |
s4o.print("if (!"); |
|
442 |
this->default_variable_name.accept(*this); |
|
443 |
s4o.print(") "); |
|
444 |
} |
|
445 |
else {ERROR;} |
|
446 |
} |
|
447 |
||
448 |
||
449 |
public: |
|
450 |
void *visit(il_default_variable_c *symbol) { |
|
451 |
//s4o.print("il_default_variable_c VISITOR!!\n"); |
|
452 |
symbol->var_name->accept(*this); |
|
453 |
if (NULL != symbol->current_type) { |
|
454 |
s4o.print("."); |
|
455 |
symbol->current_type->accept(*this); |
|
456 |
s4o.print("var"); |
|
457 |
} |
|
458 |
return NULL; |
|
459 |
} |
|
460 |
||
461 |
||
462 |
private: |
|
463 |
||
146
eef5e62048c7
Adding support for EN/ENO params in function and function blocks (standard function not supported yet)
lbessard
parents:
123
diff
changeset
|
464 |
void *visit(eno_param_c *symbol) { |
eef5e62048c7
Adding support for EN/ENO params in function and function blocks (standard function not supported yet)
lbessard
parents:
123
diff
changeset
|
465 |
if (this->is_variable_prefix_null()) { |
eef5e62048c7
Adding support for EN/ENO params in function and function blocks (standard function not supported yet)
lbessard
parents:
123
diff
changeset
|
466 |
s4o.print("*"); |
eef5e62048c7
Adding support for EN/ENO params in function and function blocks (standard function not supported yet)
lbessard
parents:
123
diff
changeset
|
467 |
} |
eef5e62048c7
Adding support for EN/ENO params in function and function blocks (standard function not supported yet)
lbessard
parents:
123
diff
changeset
|
468 |
else { |
eef5e62048c7
Adding support for EN/ENO params in function and function blocks (standard function not supported yet)
lbessard
parents:
123
diff
changeset
|
469 |
this->print_variable_prefix(); |
eef5e62048c7
Adding support for EN/ENO params in function and function blocks (standard function not supported yet)
lbessard
parents:
123
diff
changeset
|
470 |
} |
eef5e62048c7
Adding support for EN/ENO params in function and function blocks (standard function not supported yet)
lbessard
parents:
123
diff
changeset
|
471 |
s4o.print("ENO"); |
eef5e62048c7
Adding support for EN/ENO params in function and function blocks (standard function not supported yet)
lbessard
parents:
123
diff
changeset
|
472 |
return NULL; |
eef5e62048c7
Adding support for EN/ENO params in function and function blocks (standard function not supported yet)
lbessard
parents:
123
diff
changeset
|
473 |
} |
eef5e62048c7
Adding support for EN/ENO params in function and function blocks (standard function not supported yet)
lbessard
parents:
123
diff
changeset
|
474 |
|
eef5e62048c7
Adding support for EN/ENO params in function and function blocks (standard function not supported yet)
lbessard
parents:
123
diff
changeset
|
475 |
/*********************/ |
eef5e62048c7
Adding support for EN/ENO params in function and function blocks (standard function not supported yet)
lbessard
parents:
123
diff
changeset
|
476 |
/* B 1.4 - Variables */ |
eef5e62048c7
Adding support for EN/ENO params in function and function blocks (standard function not supported yet)
lbessard
parents:
123
diff
changeset
|
477 |
/*********************/ |
eef5e62048c7
Adding support for EN/ENO params in function and function blocks (standard function not supported yet)
lbessard
parents:
123
diff
changeset
|
478 |
void *visit(symbolic_variable_c *symbol) { |
eef5e62048c7
Adding support for EN/ENO params in function and function blocks (standard function not supported yet)
lbessard
parents:
123
diff
changeset
|
479 |
unsigned int vartype = search_varfb_instance_type->get_vartype(symbol); |
eef5e62048c7
Adding support for EN/ENO params in function and function blocks (standard function not supported yet)
lbessard
parents:
123
diff
changeset
|
480 |
if (!current_param_is_pointer && (vartype == search_var_instance_decl_c::external_vt || vartype == search_var_instance_decl_c::located_vt)) { |
eef5e62048c7
Adding support for EN/ENO params in function and function blocks (standard function not supported yet)
lbessard
parents:
123
diff
changeset
|
481 |
s4o.print("*("); |
eef5e62048c7
Adding support for EN/ENO params in function and function blocks (standard function not supported yet)
lbessard
parents:
123
diff
changeset
|
482 |
generate_c_base_c::visit(symbol); |
eef5e62048c7
Adding support for EN/ENO params in function and function blocks (standard function not supported yet)
lbessard
parents:
123
diff
changeset
|
483 |
s4o.print(")"); |
eef5e62048c7
Adding support for EN/ENO params in function and function blocks (standard function not supported yet)
lbessard
parents:
123
diff
changeset
|
484 |
} |
eef5e62048c7
Adding support for EN/ENO params in function and function blocks (standard function not supported yet)
lbessard
parents:
123
diff
changeset
|
485 |
else if (current_param_is_pointer && vartype != search_var_instance_decl_c::external_vt && vartype != search_var_instance_decl_c::located_vt) { |
eef5e62048c7
Adding support for EN/ENO params in function and function blocks (standard function not supported yet)
lbessard
parents:
123
diff
changeset
|
486 |
s4o.print("&("); |
eef5e62048c7
Adding support for EN/ENO params in function and function blocks (standard function not supported yet)
lbessard
parents:
123
diff
changeset
|
487 |
generate_c_base_c::visit(symbol); |
eef5e62048c7
Adding support for EN/ENO params in function and function blocks (standard function not supported yet)
lbessard
parents:
123
diff
changeset
|
488 |
s4o.print(")"); |
eef5e62048c7
Adding support for EN/ENO params in function and function blocks (standard function not supported yet)
lbessard
parents:
123
diff
changeset
|
489 |
} |
eef5e62048c7
Adding support for EN/ENO params in function and function blocks (standard function not supported yet)
lbessard
parents:
123
diff
changeset
|
490 |
else { |
eef5e62048c7
Adding support for EN/ENO params in function and function blocks (standard function not supported yet)
lbessard
parents:
123
diff
changeset
|
491 |
generate_c_base_c::visit(symbol); |
eef5e62048c7
Adding support for EN/ENO params in function and function blocks (standard function not supported yet)
lbessard
parents:
123
diff
changeset
|
492 |
} |
eef5e62048c7
Adding support for EN/ENO params in function and function blocks (standard function not supported yet)
lbessard
parents:
123
diff
changeset
|
493 |
return NULL; |
eef5e62048c7
Adding support for EN/ENO params in function and function blocks (standard function not supported yet)
lbessard
parents:
123
diff
changeset
|
494 |
} |
eef5e62048c7
Adding support for EN/ENO params in function and function blocks (standard function not supported yet)
lbessard
parents:
123
diff
changeset
|
495 |
|
70 | 496 |
/********************************************/ |
497 |
/* B.1.4.1 Directly Represented Variables */ |
|
498 |
/********************************************/ |
|
499 |
// direct_variable: direct_variable_token {$$ = new direct_variable_c($1);}; |
|
500 |
void *visit(direct_variable_c *symbol) { |
|
501 |
TRACE("direct_variable_c"); |
|
502 |
/* Do not use print_token() as it will change everything into uppercase */ |
|
503 |
if (strlen(symbol->value) == 0) ERROR; |
|
146
eef5e62048c7
Adding support for EN/ENO params in function and function blocks (standard function not supported yet)
lbessard
parents:
123
diff
changeset
|
504 |
if (!current_param_is_pointer) { |
eef5e62048c7
Adding support for EN/ENO params in function and function blocks (standard function not supported yet)
lbessard
parents:
123
diff
changeset
|
505 |
s4o.print("*("); |
eef5e62048c7
Adding support for EN/ENO params in function and function blocks (standard function not supported yet)
lbessard
parents:
123
diff
changeset
|
506 |
} |
70 | 507 |
this->print_variable_prefix(); |
508 |
s4o.printlocation(symbol->value + 1); |
|
146
eef5e62048c7
Adding support for EN/ENO params in function and function blocks (standard function not supported yet)
lbessard
parents:
123
diff
changeset
|
509 |
if (!current_param_is_pointer) { |
eef5e62048c7
Adding support for EN/ENO params in function and function blocks (standard function not supported yet)
lbessard
parents:
123
diff
changeset
|
510 |
s4o.print(")"); |
eef5e62048c7
Adding support for EN/ENO params in function and function blocks (standard function not supported yet)
lbessard
parents:
123
diff
changeset
|
511 |
} |
70 | 512 |
return NULL; |
513 |
} |
|
514 |
||
515 |
/****************************************/ |
|
516 |
/* B.2 - Language IL (Instruction List) */ |
|
517 |
/****************************************/ |
|
518 |
||
519 |
/***********************************/ |
|
520 |
/* B 2.1 Instructions and Operands */ |
|
521 |
/***********************************/ |
|
522 |
||
523 |
/* please see the comment before the RET_operator_c visitor for details... */ |
|
524 |
#define END_LABEL VAR_LEADER "end" |
|
525 |
||
526 |
/*| instruction_list il_instruction */ |
|
527 |
void *visit(instruction_list_c *symbol) { |
|
528 |
||
529 |
/* Declare the backup to the default variable, that will store the result |
|
530 |
* of the IL operations executed inside a parenthesis... |
|
531 |
*/ |
|
532 |
declare_backup_variable(); |
|
533 |
||
534 |
/* Declare the default variable, that will store the result of the IL operations... */ |
|
535 |
s4o.print(s4o.indent_spaces); |
|
536 |
s4o.print(IL_DEFVAR_T); |
|
537 |
s4o.print(" "); |
|
538 |
this->default_variable_name.accept(*this); |
|
539 |
s4o.print(";\n\n"); |
|
540 |
||
541 |
print_list(symbol, s4o.indent_spaces, ";\n" + s4o.indent_spaces, ";\n"); |
|
542 |
||
543 |
/* write the label marking the end of the code block */ |
|
544 |
/* please see the comment before the RET_operator_c visitor for details... */ |
|
545 |
s4o.print("\n"); |
|
546 |
s4o.print(s4o.indent_spaces); |
|
547 |
s4o.print(END_LABEL); |
|
548 |
s4o.print(":\n"); |
|
549 |
s4o.indent_right(); |
|
550 |
/* since every label must be followed by at least one statement, and |
|
551 |
* only the functions will introduce the return statement after this label, |
|
552 |
* function blocks written in IL would result in invalid C++ code. |
|
553 |
* To work around this we introduce the equivalent of a 'nop' operation |
|
554 |
* to humour the compiler... |
|
555 |
*/ |
|
556 |
s4o.print(s4o.indent_spaces); |
|
557 |
s4o.print("/* to humour the compiler, we insert a nop */\n"); |
|
558 |
s4o.print(s4o.indent_spaces); |
|
559 |
this->default_variable_name.accept(*this); |
|
560 |
s4o.print(" = "); |
|
561 |
this->default_variable_name.accept(*this); |
|
562 |
s4o.print(";\n"); |
|
563 |
s4o.indent_left(); |
|
564 |
||
565 |
return NULL; |
|
566 |
} |
|
567 |
||
568 |
||
569 |
/* | label ':' [il_incomplete_instruction] eol_list */ |
|
570 |
// SYM_REF2(il_instruction_c, label, il_instruction) |
|
571 |
void *visit(il_instruction_c *symbol) { |
|
572 |
if (NULL != symbol->label) { |
|
573 |
symbol->label->accept(*this); |
|
574 |
s4o.print(":\n"); |
|
575 |
s4o.print(s4o.indent_spaces); |
|
576 |
} |
|
577 |
symbol->il_instruction->accept(*this); |
|
578 |
return NULL; |
|
579 |
} |
|
580 |
||
581 |
/* | il_simple_operator [il_operand] */ |
|
582 |
//SYM_REF2(il_simple_operation_c, il_simple_operator, il_operand) |
|
583 |
void *visit(il_simple_operation_c *symbol) { |
|
584 |
this->current_operand = symbol->il_operand; |
|
585 |
if (NULL == this->current_operand) { |
|
586 |
this->current_operand_type = NULL; |
|
587 |
} else { |
|
588 |
this->current_operand_type = search_expression_type->get_type(this->current_operand); |
|
589 |
if (NULL == this->current_operand_type) ERROR; |
|
590 |
} |
|
591 |
||
592 |
symbol->il_simple_operator->accept(*this); |
|
593 |
||
594 |
this->current_operand = NULL; |
|
595 |
this->current_operand_type = NULL; |
|
596 |
return NULL; |
|
597 |
} |
|
598 |
||
599 |
||
600 |
/* | function_name [il_operand_list] */ |
|
601 |
// SYM_REF2(il_function_call_c, function_name, il_operand_list) |
|
602 |
void *visit(il_function_call_c *symbol) { |
|
603 |
function_declaration_c *f_decl = function_symtable.find_value(symbol->function_name); |
|
604 |
||
149 | 605 |
symbol_c* function_type_prefix = NULL; |
606 |
symbol_c* function_name = NULL; |
|
607 |
symbol_c* function_type_suffix = NULL; |
|
608 |
std::list<FUNCTION_PARAM> param_list; |
|
609 |
FUNCTION_PARAM *param; |
|
610 |
||
611 |
symbol_c *param_data_type = default_variable_name.current_type; |
|
612 |
symbol_c *return_data_type = NULL; |
|
613 |
||
70 | 614 |
if (f_decl == function_symtable.end_value()) { |
615 |
function_type_t current_function_type = get_function_type((identifier_c *)symbol->function_name); |
|
616 |
if (current_function_type == function_none) ERROR; |
|
617 |
||
149 | 618 |
return_data_type = (symbol_c *)search_expression_type->compute_standard_function_il(symbol, param_data_type); |
70 | 619 |
if (NULL == return_data_type) ERROR; |
620 |
||
621 |
function_call_param_iterator_c function_call_param_iterator(symbol); |
|
622 |
||
149 | 623 |
/* Add the value from EN param */ |
624 |
ADD_PARAM_LIST((symbol_c*)(new boolean_literal_c((symbol_c*)(new bool_type_name_c()), new boolean_true_c())), |
|
625 |
(symbol_c*)(new bool_type_name_c()), |
|
626 |
function_param_iterator_c::direction_in) |
|
627 |
||
628 |
/* Add the value from ENO param */ |
|
629 |
ADD_PARAM_LIST(NULL, (symbol_c*)(new bool_type_name_c()), function_param_iterator_c::direction_out) |
|
630 |
||
70 | 631 |
int nb_param = 1; |
632 |
if (symbol->il_operand_list != NULL) |
|
633 |
nb_param += ((list_c *)symbol->il_operand_list)->n; |
|
634 |
||
146
eef5e62048c7
Adding support for EN/ENO params in function and function blocks (standard function not supported yet)
lbessard
parents:
123
diff
changeset
|
635 |
#include "il_code_gen.c" |
70 | 636 |
|
637 |
} |
|
638 |
else { |
|
639 |
/* determine the base data type returned by the function being called... */ |
|
640 |
search_base_type_c search_base_type; |
|
149 | 641 |
return_data_type = (symbol_c *)f_decl->type_name->accept(search_base_type); |
70 | 642 |
if (NULL == return_data_type) ERROR; |
643 |
||
149 | 644 |
function_name = symbol->function_name; |
645 |
||
70 | 646 |
/* loop through each function parameter, find the value we should pass |
647 |
* to it, and then output the c equivalent... |
|
648 |
*/ |
|
649 |
||
650 |
function_param_iterator_c fp_iterator(f_decl); |
|
651 |
identifier_c *param_name; |
|
652 |
function_call_param_iterator_c function_call_param_iterator(symbol); |
|
653 |
for(int i = 1; (param_name = fp_iterator.next()) != NULL; i++) { |
|
654 |
symbol_c *param_type = fp_iterator.param_type(); |
|
655 |
if (param_type == NULL) ERROR; |
|
149 | 656 |
|
70 | 657 |
function_param_iterator_c::param_direction_t param_direction = fp_iterator.param_direction(); |
149 | 658 |
|
70 | 659 |
symbol_c *param_value = NULL; |
660 |
||
661 |
/* if it is the first parameter, semantics specifies that we should |
|
662 |
* get the value off the IL default variable! |
|
663 |
*/ |
|
664 |
if (1 == i) |
|
665 |
param_value = &this->default_variable_name; |
|
666 |
||
667 |
/* Get the value from a foo(<param_name> = <param_value>) style call */ |
|
668 |
/* NOTE: the following line of code is not required in this case, but it doesn't |
|
669 |
* harm to leave it in, as in the case of a non-formal syntax function call, |
|
670 |
* it will always return NULL. |
|
671 |
* We leave it in in case we later decide to merge this part of the code together |
|
672 |
* with the function calling code in generate_c_st_c, which does require |
|
673 |
* the following line... |
|
674 |
*/ |
|
675 |
if (param_value == NULL) |
|
676 |
param_value = function_call_param_iterator.search(param_name); |
|
677 |
||
678 |
/* Get the value from a foo(<param_value>) style call */ |
|
679 |
if (param_value == NULL) |
|
680 |
param_value = function_call_param_iterator.next(); |
|
149 | 681 |
|
682 |
if (param_value == NULL && param_direction == function_param_iterator_c::direction_in) { |
|
683 |
/* No value given for parameter, so we must use the default... */ |
|
684 |
/* First check whether default value specified in function declaration...*/ |
|
685 |
param_value = fp_iterator.default_value(); |
|
686 |
} |
|
687 |
||
688 |
ADD_PARAM_LIST(param_value, param_type, fp_iterator.param_direction()) |
|
689 |
} /* for(...) */ |
|
690 |
} |
|
691 |
||
692 |
default_variable_name.current_type = return_data_type; |
|
693 |
this->default_variable_name.accept(*this); |
|
694 |
default_variable_name.current_type = param_data_type; |
|
695 |
s4o.print(" = "); |
|
696 |
||
697 |
if (function_type_prefix != NULL) { |
|
698 |
s4o.print("("); |
|
699 |
function_type_prefix->accept(*this); |
|
700 |
s4o.print(")"); |
|
701 |
} |
|
702 |
if (function_name != NULL) |
|
703 |
function_name->accept(*this); |
|
704 |
if (function_type_suffix != NULL) |
|
705 |
function_type_suffix->accept(*this); |
|
706 |
s4o.print("("); |
|
707 |
s4o.indent_right(); |
|
708 |
||
709 |
std::list<FUNCTION_PARAM>::iterator pt; |
|
710 |
for(pt = param_list.begin(); pt != param_list.end(); pt++) { |
|
711 |
if (pt != param_list.begin()) |
|
712 |
s4o.print(",\n"+s4o.indent_spaces); |
|
713 |
symbol_c *param_value = pt->param_value; |
|
714 |
symbol_c *param_type = pt->param_type; |
|
715 |
||
716 |
switch (pt->param_direction) { |
|
717 |
case function_param_iterator_c::direction_in: |
|
718 |
if (param_value == NULL) { |
|
719 |
/* If not, get the default value of this variable's type */ |
|
720 |
param_value = (symbol_c *)param_type->accept(*type_initial_value_c::instance()); |
|
721 |
} |
|
722 |
if (param_value == NULL) ERROR; |
|
723 |
if (search_base_type.type_is_subrange(param_type)) { |
|
724 |
s4o.print("__CHECK_"); |
|
725 |
param_type->accept(*this); |
|
726 |
s4o.print("("); |
|
727 |
} |
|
728 |
param_value->accept(*this); |
|
729 |
if (search_base_type.type_is_subrange(param_type)) |
|
730 |
s4o.print(")"); |
|
731 |
break; |
|
732 |
case function_param_iterator_c::direction_out: |
|
733 |
case function_param_iterator_c::direction_inout: |
|
734 |
current_param_is_pointer = true; |
|
735 |
if (param_value == NULL) { |
|
736 |
s4o.print("NULL"); |
|
737 |
} else { |
|
70 | 738 |
param_value->accept(*this); |
149 | 739 |
} |
740 |
current_param_is_pointer = false; |
|
741 |
break; |
|
742 |
case function_param_iterator_c::direction_extref: |
|
743 |
/* TODO! */ |
|
744 |
ERROR; |
|
745 |
break; |
|
746 |
} /* switch */ |
|
747 |
} |
|
748 |
||
749 |
s4o.print(")"); |
|
750 |
/* the data type returned by the function, and stored in the il default variable... */ |
|
751 |
default_variable_name.current_type = return_data_type; |
|
70 | 752 |
return NULL; |
753 |
} |
|
754 |
||
755 |
||
756 |
/* | il_expr_operator '(' [il_operand] eol_list [simple_instr_list] ')' */ |
|
757 |
//SYM_REF4(il_expression_c, il_expr_operator, il_operand, simple_instr_list, unused) |
|
758 |
void *visit(il_expression_c *symbol) { |
|
759 |
/* We will be recursevely interpreting an instruction list, |
|
760 |
* so we store a backup of the data type of the value currently stored |
|
761 |
* in the default variable, and set the current data type to NULL |
|
762 |
*/ |
|
763 |
symbol_c *old_current_default_variable_data_type = this->default_variable_name.current_type; |
|
764 |
this->default_variable_name.current_type = NULL; |
|
765 |
||
766 |
/* Pass the symbol->il_operand to the simple_instr_list visitor |
|
767 |
* using the il_default_variable_init_value parameter... |
|
768 |
* Note that the simple_instr_list_c visitor will set this parameter |
|
769 |
* to NULL as soon as it does not require it any longer, |
|
770 |
* so we don't do it here again after the |
|
771 |
* symbol->simple_instr_list->accept(*this); |
|
772 |
* returns... |
|
773 |
*/ |
|
774 |
this->il_default_variable_init_value = symbol->il_operand; |
|
775 |
||
776 |
/* Now do the parenthesised instructions... */ |
|
777 |
/* NOTE: the following code line will get the variable |
|
778 |
* this->default_variable_name.current_type updated! |
|
779 |
*/ |
|
780 |
symbol->simple_instr_list->accept(*this); |
|
781 |
||
782 |
/* Now do the operation, using the previous result! */ |
|
783 |
/* NOTE: The result of the previous instruction list will be stored |
|
784 |
* in a variable named IL_DEFVAR_BACK. This is done in the visitor |
|
785 |
* to instruction_list_c objects... |
|
786 |
*/ |
|
787 |
this->current_operand = &(this->default_variable_back_name); |
|
788 |
this->current_operand_type = this->default_variable_back_name.current_type; |
|
789 |
||
790 |
this->default_variable_name.current_type = old_current_default_variable_data_type; |
|
791 |
if (NULL == this->current_operand_type) ERROR; |
|
792 |
||
793 |
symbol->il_expr_operator->accept(*this); |
|
794 |
||
795 |
this->current_operand = NULL; |
|
796 |
this->current_operand_type = NULL; |
|
797 |
this->default_variable_back_name.current_type = NULL; |
|
798 |
return NULL; |
|
799 |
} |
|
800 |
||
801 |
/* il_jump_operator label */ |
|
802 |
// SYM_REF2(il_jump_operation_c, il_jump_operator, label) |
|
803 |
void *visit(il_jump_operation_c *symbol) { |
|
804 |
/* Pass the symbol->label to the il_jump_operation visitor |
|
805 |
* using the jump_label parameter... |
|
806 |
*/ |
|
807 |
this->jump_label = symbol->label; |
|
808 |
symbol->il_jump_operator->accept(*this); |
|
809 |
this->jump_label = NULL; |
|
810 |
||
811 |
return NULL; |
|
812 |
} |
|
813 |
||
814 |
/* il_call_operator prev_declared_fb_name |
|
815 |
* | il_call_operator prev_declared_fb_name '(' ')' |
|
816 |
* | il_call_operator prev_declared_fb_name '(' eol_list ')' |
|
817 |
* | il_call_operator prev_declared_fb_name '(' il_operand_list ')' |
|
818 |
* | il_call_operator prev_declared_fb_name '(' eol_list il_param_list ')' |
|
819 |
*/ |
|
820 |
// SYM_REF4(il_fb_call_c, il_call_operator, fb_name, il_operand_list, il_param_list) |
|
821 |
void *visit(il_fb_call_c *symbol) { |
|
822 |
symbol->il_call_operator->accept(*this); |
|
823 |
s4o.print("{\n"); |
|
824 |
s4o.indent_right(); |
|
825 |
s4o.print(s4o.indent_spaces); |
|
826 |
||
827 |
/* first figure out what is the name of the function block type of the function block being called... */ |
|
828 |
symbol_c *function_block_type_name = this->search_fb_instance_decl->get_type_name(symbol->fb_name); |
|
829 |
/* should never occur. The function block instance MUST have been declared... */ |
|
830 |
if (function_block_type_name == NULL) ERROR; |
|
831 |
||
832 |
/* Now find the declaration of the function block type being called... */ |
|
833 |
function_block_declaration_c *fb_decl = function_block_type_symtable.find_value(function_block_type_name); |
|
834 |
/* should never occur. The function block type being called MUST be in the symtable... */ |
|
835 |
if (fb_decl == function_block_type_symtable.end_value()) ERROR; |
|
836 |
||
837 |
/* loop through each function block parameter, find the value we should pass |
|
838 |
* to it, and then output the c equivalent... |
|
839 |
*/ |
|
840 |
function_param_iterator_c fp_iterator(fb_decl); |
|
841 |
identifier_c *param_name; |
|
842 |
function_call_param_iterator_c function_call_param_iterator(symbol); |
|
843 |
for(int i = 1; (param_name = fp_iterator.next()) != NULL; i++) { |
|
844 |
function_param_iterator_c::param_direction_t param_direction = fp_iterator.param_direction(); |
|
845 |
||
846 |
/* Get the value from a foo(<param_name> = <param_value>) style call */ |
|
847 |
symbol_c *param_value = function_call_param_iterator.search(param_name); |
|
848 |
||
849 |
/* Get the value from a foo(<param_value>) style call */ |
|
850 |
if (param_value == NULL) |
|
851 |
param_value = function_call_param_iterator.next(); |
|
852 |
||
98 | 853 |
symbol_c *param_type = fp_iterator.param_type(); |
854 |
if (param_type == NULL) ERROR; |
|
855 |
||
123 | 856 |
/* now output the value assignment */ |
70 | 857 |
if (param_value != NULL) |
858 |
if ((param_direction == function_param_iterator_c::direction_in) || |
|
859 |
(param_direction == function_param_iterator_c::direction_inout)) { |
|
860 |
symbol->fb_name->accept(*this); |
|
861 |
s4o.print("."); |
|
862 |
param_name->accept(*this); |
|
863 |
s4o.print(" = "); |
|
123 | 864 |
if (search_base_type.type_is_subrange(param_type)) { |
98 | 865 |
s4o.print("__CHECK_"); |
866 |
param_type->accept(*this); |
|
867 |
s4o.print("("); |
|
868 |
} |
|
70 | 869 |
param_value->accept(*this); |
123 | 870 |
if (search_base_type.type_is_subrange(param_type)) |
98 | 871 |
s4o.print(")"); |
70 | 872 |
s4o.print(";\n" + s4o.indent_spaces); |
873 |
} |
|
874 |
} /* for(...) */ |
|
875 |
||
876 |
/* now call the function... */ |
|
877 |
function_block_type_name->accept(*this); |
|
878 |
s4o.print(FB_FUNCTION_SUFFIX); |
|
879 |
s4o.print("(&"); |
|
880 |
symbol->fb_name->accept(*this); |
|
881 |
s4o.print(")"); |
|
882 |
||
883 |
/* loop through each function parameter, find the variable to which |
|
884 |
* we should atribute the value of all output or inoutput parameters. |
|
885 |
*/ |
|
886 |
fp_iterator.reset(); |
|
887 |
function_call_param_iterator.reset(); |
|
888 |
for(int i = 1; (param_name = fp_iterator.next()) != NULL; i++) { |
|
889 |
function_param_iterator_c::param_direction_t param_direction = fp_iterator.param_direction(); |
|
890 |
||
891 |
/* Get the value from a foo(<param_name> = <param_value>) style call */ |
|
892 |
symbol_c *param_value = function_call_param_iterator.search(param_name); |
|
893 |
||
894 |
/* Get the value from a foo(<param_value>) style call */ |
|
895 |
if (param_value == NULL) |
|
896 |
param_value = function_call_param_iterator.next(); |
|
897 |
||
898 |
/* now output the value assignment */ |
|
899 |
if (param_value != NULL) |
|
900 |
if ((param_direction == function_param_iterator_c::direction_out) || |
|
901 |
(param_direction == function_param_iterator_c::direction_inout)) { |
|
98 | 902 |
symbol_c *param_type = search_varfb_instance_type->get_type(param_value, false); |
903 |
||
70 | 904 |
s4o.print(";\n"+ s4o.indent_spaces); |
905 |
param_value->accept(*this); |
|
906 |
s4o.print(" = "); |
|
123 | 907 |
if (search_base_type.type_is_subrange(param_type)) { |
98 | 908 |
s4o.print("__CHECK_"); |
909 |
param_type->accept(*this); |
|
910 |
s4o.print("("); |
|
911 |
} |
|
70 | 912 |
symbol->fb_name->accept(*this); |
913 |
s4o.print("."); |
|
914 |
param_name->accept(*this); |
|
123 | 915 |
if (search_base_type.type_is_subrange(param_type)) |
98 | 916 |
s4o.print(")"); |
70 | 917 |
} |
918 |
} /* for(...) */ |
|
919 |
||
920 |
s4o.print(";\n"); |
|
921 |
s4o.indent_left(); |
|
922 |
s4o.print(s4o.indent_spaces); |
|
923 |
s4o.print("}"); |
|
924 |
||
925 |
return NULL; |
|
926 |
} |
|
927 |
||
928 |
||
929 |
||
930 |
/* | function_name '(' eol_list [il_param_list] ')' */ |
|
931 |
// SYM_REF2(il_formal_funct_call_c, function_name, il_param_list) |
|
932 |
void *visit(il_formal_funct_call_c *symbol) { |
|
933 |
function_declaration_c *f_decl = function_symtable.find_value(symbol->function_name); |
|
934 |
||
149 | 935 |
symbol_c* function_type_prefix = NULL; |
936 |
symbol_c* function_name = NULL; |
|
937 |
symbol_c* function_type_suffix = NULL; |
|
938 |
std::list<FUNCTION_PARAM> param_list; |
|
939 |
FUNCTION_PARAM *param; |
|
940 |
||
941 |
symbol_c *return_data_type = NULL; |
|
942 |
||
943 |
if (f_decl == function_symtable.end_value()) { |
|
944 |
function_type_t current_function_type = get_function_type((identifier_c *)symbol->function_name); |
|
945 |
if (current_function_type == function_none) ERROR; |
|
946 |
||
947 |
return_data_type = (symbol_c *)search_expression_type->compute_standard_function_default(NULL, symbol); |
|
948 |
if (NULL == return_data_type) ERROR; |
|
949 |
||
950 |
function_call_param_iterator_c function_call_param_iterator(symbol); |
|
951 |
||
952 |
identifier_c en_param_name("EN"); |
|
953 |
/* Get the value from EN param */ |
|
954 |
symbol_c *EN_param_value = function_call_param_iterator.search(&en_param_name); |
|
955 |
if (EN_param_value == NULL) |
|
956 |
EN_param_value = (symbol_c*)(new boolean_literal_c((symbol_c*)(new bool_type_name_c()), new boolean_true_c())); |
|
957 |
ADD_PARAM_LIST(EN_param_value, (symbol_c*)(new bool_type_name_c()), function_param_iterator_c::direction_in) |
|
958 |
||
959 |
identifier_c eno_param_name("EN0"); |
|
960 |
/* Get the value from ENO param */ |
|
961 |
symbol_c *ENO_param_value = function_call_param_iterator.search(&eno_param_name); |
|
962 |
ADD_PARAM_LIST(ENO_param_value, (symbol_c*)(new bool_type_name_c()), function_param_iterator_c::direction_out) |
|
963 |
||
964 |
int nb_param = 0; |
|
965 |
if (symbol->il_param_list != NULL) |
|
966 |
nb_param += ((list_c *)symbol->il_param_list)->n; |
|
967 |
||
968 |
#include "st_code_gen.c" |
|
969 |
||
970 |
} |
|
971 |
else { |
|
972 |
/* determine the base data type returned by the function being called... */ |
|
973 |
search_base_type_c search_base_type; |
|
974 |
return_data_type = (symbol_c *)f_decl->type_name->accept(search_base_type); |
|
975 |
if (NULL == return_data_type) ERROR; |
|
976 |
||
977 |
function_name = symbol->function_name; |
|
978 |
||
979 |
/* loop through each function parameter, find the value we should pass |
|
980 |
* to it, and then output the c equivalent... |
|
981 |
*/ |
|
982 |
||
983 |
function_param_iterator_c fp_iterator(f_decl); |
|
984 |
identifier_c *param_name; |
|
985 |
function_call_param_iterator_c function_call_param_iterator(symbol); |
|
986 |
for(int i = 1; (param_name = fp_iterator.next()) != NULL; i++) { |
|
987 |
symbol_c *param_type = fp_iterator.param_type(); |
|
988 |
if (param_type == NULL) ERROR; |
|
989 |
||
990 |
function_param_iterator_c::param_direction_t param_direction = fp_iterator.param_direction(); |
|
991 |
||
992 |
||
993 |
symbol_c *param_value = NULL; |
|
994 |
||
995 |
/* Get the value from a foo(<param_name> = <param_value>) style call */ |
|
996 |
if (param_value == NULL) |
|
997 |
param_value = function_call_param_iterator.search(param_name); |
|
998 |
||
999 |
/* Get the value from a foo(<param_value>) style call */ |
|
1000 |
/* NOTE: the following line of code is not required in this case, but it doesn't |
|
1001 |
* harm to leave it in, as in the case of a formal syntax function call, |
|
1002 |
* it will always return NULL. |
|
1003 |
* We leave it in in case we later decide to merge this part of the code together |
|
1004 |
* with the function calling code in generate_c_st_c, which does require |
|
1005 |
* the following line... |
|
1006 |
*/ |
|
1007 |
if (param_value == NULL) |
|
1008 |
param_value = function_call_param_iterator.next(); |
|
1009 |
||
1010 |
if (param_value == NULL) { |
|
1011 |
/* No value given for parameter, so we must use the default... */ |
|
1012 |
/* First check whether default value specified in function declaration...*/ |
|
1013 |
param_value = fp_iterator.default_value(); |
|
1014 |
} |
|
1015 |
||
1016 |
ADD_PARAM_LIST(param_value, param_type, fp_iterator.param_direction()) |
|
1017 |
} |
|
1018 |
} |
|
1019 |
||
1020 |
default_variable_name.current_type = return_data_type; |
|
1021 |
this->default_variable_name.accept(*this); |
|
1022 |
s4o.print(" = "); |
|
1023 |
||
1024 |
if (function_type_prefix != NULL) { |
|
1025 |
s4o.print("("); |
|
1026 |
function_type_prefix->accept(*this); |
|
1027 |
s4o.print(")"); |
|
1028 |
} |
|
1029 |
if (function_name != NULL) |
|
1030 |
function_name->accept(*this); |
|
1031 |
if (function_type_suffix != NULL) |
|
1032 |
function_type_suffix->accept(*this); |
|
70 | 1033 |
s4o.print("("); |
149 | 1034 |
s4o.indent_right(); |
1035 |
||
1036 |
std::list<FUNCTION_PARAM>::iterator pt; |
|
1037 |
for(pt = param_list.begin(); pt != param_list.end(); pt++) { |
|
1038 |
if (pt != param_list.begin()) |
|
1039 |
s4o.print(",\n"+s4o.indent_spaces); |
|
1040 |
symbol_c *param_value = pt->param_value; |
|
1041 |
symbol_c *param_type = pt->param_type; |
|
1042 |
||
1043 |
switch (pt->param_direction) { |
|
70 | 1044 |
case function_param_iterator_c::direction_in: |
1045 |
if (param_value == NULL) { |
|
1046 |
/* If not, get the default value of this variable's type */ |
|
1047 |
param_value = (symbol_c *)param_type->accept(*type_initial_value_c::instance()); |
|
1048 |
} |
|
1049 |
if (param_value == NULL) ERROR; |
|
123 | 1050 |
if (search_base_type.type_is_subrange(param_type)) { |
98 | 1051 |
s4o.print("__CHECK_"); |
1052 |
param_type->accept(*this); |
|
1053 |
s4o.print("("); |
|
1054 |
} |
|
70 | 1055 |
param_value->accept(*this); |
123 | 1056 |
if (search_base_type.type_is_subrange(param_type)) |
98 | 1057 |
s4o.print(")"); |
149 | 1058 |
break; |
70 | 1059 |
case function_param_iterator_c::direction_out: |
1060 |
case function_param_iterator_c::direction_inout: |
|
149 | 1061 |
current_param_is_pointer = true; |
70 | 1062 |
if (param_value == NULL) { |
149 | 1063 |
s4o.print("NULL"); |
1064 |
} else { |
|
70 | 1065 |
param_value->accept(*this); |
149 | 1066 |
} |
1067 |
current_param_is_pointer = false; |
|
1068 |
break; |
|
70 | 1069 |
case function_param_iterator_c::direction_extref: |
1070 |
/* TODO! */ |
|
1071 |
ERROR; |
|
149 | 1072 |
break; |
70 | 1073 |
} /* switch */ |
1074 |
} /* for(...) */ |
|
1075 |
||
1076 |
// symbol->parameter_assignment->accept(*this); |
|
1077 |
s4o.print(")"); |
|
149 | 1078 |
/* the data type returned by the function, and stored in the il default variable... */ |
70 | 1079 |
return NULL; |
1080 |
} |
|
1081 |
||
1082 |
||
1083 |
/* | il_operand_list ',' il_operand */ |
|
1084 |
// SYM_LIST(il_operand_list_c) |
|
1085 |
void *visit(il_operand_list_c *symbol) {ERROR; return NULL;} // should never get called! |
|
1086 |
||
1087 |
||
1088 |
/* | simple_instr_list il_simple_instruction */ |
|
1089 |
// SYM_LIST(simple_instr_list_c) |
|
1090 |
void *visit(simple_instr_list_c *symbol) { |
|
1091 |
/* A simple_instr_list_c is used to store a list of il operations |
|
1092 |
* being done within parenthesis... |
|
1093 |
* |
|
1094 |
* e.g.: |
|
1095 |
* LD var1 |
|
1096 |
* AND ( var2 |
|
1097 |
* OR var3 |
|
1098 |
* OR var4 |
|
1099 |
* ) |
|
1100 |
* |
|
1101 |
* This will be converted to C++ by defining a new scope |
|
1102 |
* with a new il default variable, and executing the il operands |
|
1103 |
* within this new scope. |
|
1104 |
* At the end of the scope the result, i.e. the value currently stored |
|
1105 |
* in the il default variable is copied to the variable used to take this |
|
1106 |
* value to the outside scope... |
|
1107 |
* |
|
1108 |
* The above example will result in the following C++ code: |
|
1109 |
* {__IL_DEFVAR_T __IL_DEFVAR_BACK; |
|
1110 |
* __IL_DEFVAR_T __IL_DEFVAR; |
|
1111 |
* |
|
1112 |
* __IL_DEFVAR.INTvar = var1; |
|
1113 |
* { |
|
1114 |
* __IL_DEFVAR_T __IL_DEFVAR; |
|
1115 |
* |
|
1116 |
* __IL_DEFVAR.INTvar = var2; |
|
1117 |
* __IL_DEFVAR.INTvar |= var3; |
|
1118 |
* __IL_DEFVAR.INTvar |= var4; |
|
1119 |
* |
|
1120 |
* __IL_DEFVAR_BACK = __IL_DEFVAR; |
|
1121 |
* } |
|
1122 |
* __IL_DEFVAR.INTvar &= __IL_DEFVAR_BACK.INTvar; |
|
1123 |
* |
|
1124 |
* } |
|
1125 |
* |
|
1126 |
* The intial value of the il default variable (in the above |
|
1127 |
* example 'var2') is passed to this simple_instr_list_c visitor |
|
1128 |
* using the il_default_variable_init_value parameter. |
|
1129 |
* Since it is possible to have parenthesis inside other parenthesis |
|
1130 |
* recursively, we reset the il_default_variable_init_value to NULL |
|
1131 |
* as soon as we no longer require it, as it may be used once again |
|
1132 |
* in the line |
|
1133 |
* print_list(symbol, s4o.indent_spaces, ";\n" + s4o.indent_spaces, ";\n"); |
|
1134 |
* |
|
1135 |
*/ |
|
1136 |
||
1137 |
/* Declare the default variable, that will store the result of the IL operations... */ |
|
1138 |
s4o.print("{\n"); |
|
1139 |
s4o.indent_right(); |
|
1140 |
||
1141 |
s4o.print(s4o.indent_spaces); |
|
1142 |
s4o.print(IL_DEFVAR_T); |
|
1143 |
s4o.print(" "); |
|
1144 |
this->default_variable_name.accept(*this); |
|
1145 |
s4o.print(";\n\n"); |
|
1146 |
||
1147 |
/* Check whether we should initiliase the il default variable... */ |
|
1148 |
if (NULL != this->il_default_variable_init_value) { |
|
1149 |
/* Yes, we must... */ |
|
1150 |
/* We will do it by instatiating a LD operator, and having this |
|
1151 |
* same generate_c_il_c class visiting it! |
|
1152 |
*/ |
|
1153 |
LD_operator_c ld_oper; |
|
1154 |
il_simple_operation_c il_simple_oper(&ld_oper, this->il_default_variable_init_value); |
|
1155 |
||
1156 |
s4o.print(s4o.indent_spaces); |
|
1157 |
il_simple_oper.accept(*this); |
|
1158 |
s4o.print(";\n"); |
|
1159 |
} |
|
1160 |
||
1161 |
/* this parameter no longer required... */ |
|
1162 |
this->il_default_variable_init_value = NULL; |
|
1163 |
||
1164 |
print_list(symbol, s4o.indent_spaces, ";\n" + s4o.indent_spaces, ";\n"); |
|
1165 |
||
1166 |
/* copy the result in the default variable to the variable |
|
1167 |
* used to pass the data out to the scope enclosing |
|
1168 |
* the current scope! |
|
1169 |
* |
|
1170 |
* We also need to update the data type currently stored within |
|
1171 |
* the variable used to pass the data to the outside scope... |
|
1172 |
*/ |
|
1173 |
this->default_variable_back_name.current_type = this->default_variable_name.current_type; |
|
1174 |
s4o.print("\n"); |
|
1175 |
s4o.print(s4o.indent_spaces); |
|
1176 |
this->default_variable_back_name.accept(*this); |
|
1177 |
s4o.print(" = "); |
|
1178 |
this->default_variable_name.accept(*this); |
|
1179 |
s4o.print(";\n"); |
|
1180 |
||
1181 |
s4o.indent_left(); |
|
1182 |
s4o.print(s4o.indent_spaces); |
|
1183 |
s4o.print("}\n"); |
|
1184 |
s4o.print(s4o.indent_spaces); |
|
1185 |
return NULL; |
|
1186 |
} |
|
1187 |
||
1188 |
/* | il_initial_param_list il_param_instruction */ |
|
1189 |
// SYM_LIST(il_param_list_c) |
|
1190 |
void *visit(il_param_list_c *symbol) {ERROR; return NULL;} // should never get called! |
|
1191 |
||
1192 |
/* il_assign_operator il_operand |
|
1193 |
* | il_assign_operator '(' eol_list simple_instr_list ')' |
|
1194 |
*/ |
|
1195 |
// SYM_REF4(il_param_assignment_c, il_assign_operator, il_operand, simple_instr_list, unused) |
|
1196 |
void *visit(il_param_assignment_c *symbol) {ERROR; return NULL;} // should never get called! |
|
1197 |
||
1198 |
/* il_assign_out_operator variable */ |
|
1199 |
// SYM_REF2(il_param_out_assignment_c, il_assign_out_operator, variable); |
|
1200 |
void *visit(il_param_out_assignment_c *symbol) {ERROR; return NULL;} // should never get called! |
|
1201 |
||
1202 |
/*******************/ |
|
1203 |
/* B 2.2 Operators */ |
|
1204 |
/*******************/ |
|
1205 |
||
1206 |
void *visit(LD_operator_c *symbol) { |
|
1207 |
/* the data type resulting from this operation... */ |
|
1208 |
this->default_variable_name.current_type = this->current_operand_type; |
|
1209 |
XXX_operator(&(this->default_variable_name), " = ", this->current_operand); |
|
1210 |
return NULL; |
|
1211 |
} |
|
1212 |
||
1213 |
void *visit(LDN_operator_c *symbol) { |
|
1214 |
/* the data type resulting from this operation... */ |
|
1215 |
this->default_variable_name.current_type = this->current_operand_type; |
|
1216 |
XXX_operator(&(this->default_variable_name), |
|
1217 |
search_expression_type->is_bool_type(this->current_operand_type)?" = !":" = ~", |
|
1218 |
this->current_operand); |
|
1219 |
return NULL; |
|
1220 |
} |
|
1221 |
||
1222 |
void *visit(ST_operator_c *symbol) { |
|
98 | 1223 |
symbol_c *operand_type = search_varfb_instance_type->get_type(this->current_operand, false); |
123 | 1224 |
|
98 | 1225 |
this->current_operand->accept(*this); |
1226 |
s4o.print(" = "); |
|
123 | 1227 |
if (search_base_type.type_is_subrange(operand_type)) { |
98 | 1228 |
s4o.print("__CHECK_"); |
1229 |
operand_type->accept(*this); |
|
1230 |
s4o.print("("); |
|
1231 |
} |
|
1232 |
this->default_variable_name.accept(*this); |
|
123 | 1233 |
if (search_base_type.type_is_subrange(operand_type)) |
98 | 1234 |
s4o.print(")"); |
70 | 1235 |
/* the data type resulting from this operation is unchamged. */ |
1236 |
return NULL; |
|
1237 |
} |
|
1238 |
||
1239 |
void *visit(STN_operator_c *symbol) { |
|
98 | 1240 |
symbol_c *operand_type = search_varfb_instance_type->get_type(this->current_operand, false); |
123 | 1241 |
|
98 | 1242 |
this->current_operand->accept(*this); |
1243 |
s4o.print(" = "); |
|
123 | 1244 |
if (search_base_type.type_is_subrange(operand_type)) { |
98 | 1245 |
s4o.print("__CHECK_"); |
1246 |
operand_type->accept(*this); |
|
1247 |
s4o.print("("); |
|
1248 |
} |
|
1249 |
if (search_expression_type->is_bool_type(this->current_operand_type)) |
|
1250 |
s4o.print("!"); |
|
1251 |
else |
|
1252 |
s4o.print("~"); |
|
1253 |
this->default_variable_name.accept(*this); |
|
123 | 1254 |
if (search_base_type.type_is_subrange(operand_type)) |
98 | 1255 |
s4o.print(")"); |
70 | 1256 |
/* the data type resulting from this operation is unchamged. */ |
1257 |
return NULL; |
|
1258 |
} |
|
1259 |
||
1260 |
void *visit(NOT_operator_c *symbol) { |
|
1261 |
if ((NULL != this->current_operand) || (NULL != this->current_operand_type)) ERROR; |
|
1262 |
XXX_operator(&(this->default_variable_name), |
|
1263 |
search_expression_type->is_bool_type(this->default_variable_name.current_type)?" = !":" = ~", |
|
1264 |
&(this->default_variable_name)); |
|
1265 |
/* the data type resulting from this operation is unchanged. */ |
|
1266 |
return NULL; |
|
1267 |
} |
|
1268 |
||
1269 |
void *visit(S_operator_c *symbol) { |
|
1270 |
if ((NULL == this->current_operand) || (NULL == this->current_operand_type)) ERROR; |
|
1271 |
||
1272 |
C_modifier(); |
|
1273 |
this->current_operand->accept(*this); |
|
1274 |
s4o.print(search_expression_type->is_bool_type(this->current_operand_type)?" = true":" = 1"); |
|
1275 |
/* the data type resulting from this operation is unchanged! */ |
|
1276 |
return NULL; |
|
1277 |
} |
|
1278 |
||
1279 |
void *visit(R_operator_c *symbol) { |
|
1280 |
if ((NULL == this->current_operand) || (NULL == this->current_operand_type)) ERROR; |
|
1281 |
||
1282 |
C_modifier(); |
|
1283 |
this->current_operand->accept(*this); |
|
1284 |
s4o.print(search_expression_type->is_bool_type(this->current_operand_type)?" = false":" = 0"); |
|
1285 |
/* the data type resulting from this operation is unchanged! */ |
|
1286 |
return NULL; |
|
1287 |
} |
|
1288 |
||
1289 |
void *visit(S1_operator_c *symbol) {return XXX_CAL_operator("S1", this->current_operand);} |
|
1290 |
void *visit(R1_operator_c *symbol) {return XXX_CAL_operator("R1", this->current_operand);} |
|
1291 |
void *visit(CLK_operator_c *symbol) {return XXX_CAL_operator("CLK", this->current_operand);} |
|
1292 |
void *visit(CU_operator_c *symbol) {return XXX_CAL_operator("CU", this->current_operand);} |
|
1293 |
void *visit(CD_operator_c *symbol) {return XXX_CAL_operator("CD", this->current_operand);} |
|
1294 |
void *visit(PV_operator_c *symbol) {return XXX_CAL_operator("PV", this->current_operand);} |
|
1295 |
void *visit(IN_operator_c *symbol) {return XXX_CAL_operator("IN", this->current_operand);} |
|
1296 |
void *visit(PT_operator_c *symbol) {return XXX_CAL_operator("PT", this->current_operand);} |
|
1297 |
||
1298 |
void *visit(AND_operator_c *symbol) { |
|
1299 |
if (search_expression_type->is_binary_type(this->default_variable_name.current_type) && |
|
1300 |
search_expression_type->is_same_type(this->default_variable_name.current_type, this->current_operand_type)) { |
|
1301 |
XXX_operator(&(this->default_variable_name), " &= ", this->current_operand); |
|
1302 |
/* the data type resulting from this operation... */ |
|
1303 |
this->default_variable_name.current_type = this->current_operand_type; |
|
1304 |
} |
|
1305 |
else {ERROR;} |
|
1306 |
return NULL; |
|
1307 |
} |
|
1308 |
||
1309 |
void *visit(OR_operator_c *symbol) { |
|
1310 |
if (search_expression_type->is_binary_type(this->default_variable_name.current_type) && |
|
1311 |
search_expression_type->is_same_type(this->default_variable_name.current_type, this->current_operand_type)) { |
|
1312 |
XXX_operator(&(this->default_variable_name), " |= ", this->current_operand); |
|
1313 |
/* the data type resulting from this operation... */ |
|
1314 |
this->default_variable_name.current_type = this->current_operand_type; |
|
1315 |
} |
|
1316 |
else {ERROR;} |
|
1317 |
return NULL; |
|
1318 |
} |
|
1319 |
||
1320 |
void *visit(XOR_operator_c *symbol) { |
|
1321 |
if (search_expression_type->is_binary_type(this->default_variable_name.current_type) && |
|
1322 |
search_expression_type->is_same_type(this->default_variable_name.current_type, this->current_operand_type)) { |
|
1323 |
// '^' is a bit by bit exclusive OR !! Also seems to work with boolean types! |
|
1324 |
XXX_operator(&(this->default_variable_name), " ^= ", this->current_operand); |
|
1325 |
/* the data type resulting from this operation... */ |
|
1326 |
this->default_variable_name.current_type = this->current_operand_type; |
|
1327 |
} |
|
1328 |
else {ERROR;} |
|
1329 |
return NULL; |
|
1330 |
} |
|
1331 |
||
1332 |
void *visit(ANDN_operator_c *symbol) { |
|
1333 |
if (search_expression_type->is_binary_type(this->default_variable_name.current_type) && |
|
1334 |
search_expression_type->is_same_type(this->default_variable_name.current_type, this->current_operand_type)) { |
|
1335 |
XXX_operator(&(this->default_variable_name), |
|
1336 |
search_expression_type->is_bool_type(this->current_operand_type)?" &= !":" &= ~", |
|
1337 |
this->current_operand); |
|
1338 |
/* the data type resulting from this operation... */ |
|
1339 |
this->default_variable_name.current_type = this->current_operand_type; |
|
1340 |
} |
|
1341 |
else {ERROR;} |
|
1342 |
return NULL; |
|
1343 |
} |
|
1344 |
||
1345 |
void *visit(ORN_operator_c *symbol) { |
|
1346 |
if (search_expression_type->is_binary_type(this->default_variable_name.current_type) && |
|
1347 |
search_expression_type->is_same_type(this->default_variable_name.current_type, this->current_operand_type)) { |
|
1348 |
XXX_operator(&(this->default_variable_name), |
|
1349 |
search_expression_type->is_bool_type(this->current_operand_type)?" |= !":" |= ~", |
|
1350 |
this->current_operand); |
|
1351 |
/* the data type resulting from this operation... */ |
|
1352 |
this->default_variable_name.current_type = this->current_operand_type; |
|
1353 |
} |
|
1354 |
else {ERROR;} |
|
1355 |
return NULL; |
|
1356 |
} |
|
1357 |
||
1358 |
void *visit(XORN_operator_c *symbol) { |
|
1359 |
if (search_expression_type->is_binary_type(this->default_variable_name.current_type) && |
|
1360 |
search_expression_type->is_same_type(this->default_variable_name.current_type, this->current_operand_type)) { |
|
1361 |
XXX_operator(&(this->default_variable_name), |
|
1362 |
// bit by bit exclusive OR !! Also seems to work with boolean types! |
|
1363 |
search_expression_type->is_bool_type(this->current_operand_type)?" ^= !":" ^= ~", |
|
1364 |
this->current_operand); |
|
1365 |
/* the data type resulting from this operation... */ |
|
1366 |
this->default_variable_name.current_type = this->current_operand_type; |
|
1367 |
} |
|
1368 |
else {ERROR;} |
|
1369 |
return NULL; |
|
1370 |
} |
|
1371 |
||
1372 |
void *visit(ADD_operator_c *symbol) { |
|
1373 |
if (search_expression_type->is_time_type(this->default_variable_name.current_type) && |
|
1374 |
search_expression_type->is_time_type(this->current_operand_type)) { |
|
149 | 1375 |
XXX_function("__TIME_ADD", &(this->default_variable_name), this->current_operand); |
70 | 1376 |
/* the data type resulting from this operation... */ |
1377 |
this->default_variable_name.current_type = this->current_operand_type; |
|
1378 |
return NULL; |
|
1379 |
} |
|
1380 |
if (search_expression_type->is_num_type(this->default_variable_name.current_type) && |
|
1381 |
search_expression_type->is_same_type(this->default_variable_name.current_type, this->current_operand_type)) { |
|
1382 |
XXX_operator(&(this->default_variable_name), " += ", this->current_operand); |
|
1383 |
/* the data type resulting from this operation... */ |
|
1384 |
this->default_variable_name.current_type = this->current_operand_type; |
|
1385 |
return NULL; |
|
1386 |
} |
|
1387 |
ERROR; |
|
1388 |
return NULL; |
|
1389 |
} |
|
1390 |
||
1391 |
void *visit(SUB_operator_c *symbol) { |
|
1392 |
if (search_expression_type->is_time_type(this->default_variable_name.current_type) && |
|
1393 |
search_expression_type->is_time_type(this->current_operand_type)) { |
|
149 | 1394 |
XXX_function("__TIME_SUB", &(this->default_variable_name), this->current_operand); |
70 | 1395 |
/* the data type resulting from this operation... */ |
1396 |
this->default_variable_name.current_type = this->current_operand_type; |
|
1397 |
return NULL; |
|
1398 |
} |
|
1399 |
if (search_expression_type->is_num_type(this->default_variable_name.current_type) && |
|
1400 |
search_expression_type->is_same_type(this->default_variable_name.current_type, this->current_operand_type)) { |
|
1401 |
XXX_operator(&(this->default_variable_name), " -= ", this->current_operand); |
|
1402 |
/* the data type resulting from this operation... */ |
|
1403 |
this->default_variable_name.current_type = this->current_operand_type; |
|
1404 |
return NULL; |
|
1405 |
} |
|
1406 |
ERROR; |
|
1407 |
return NULL; |
|
1408 |
} |
|
1409 |
||
1410 |
void *visit(MUL_operator_c *symbol) { |
|
1411 |
if (search_expression_type->is_time_type(this->default_variable_name.current_type) && |
|
1412 |
search_expression_type->is_integer_type(this->current_operand_type)) { |
|
149 | 1413 |
XXX_function("__TIME_MUL", &(this->default_variable_name), this->current_operand); |
70 | 1414 |
/* the data type resulting from this operation... */ |
1415 |
return NULL; |
|
1416 |
} |
|
1417 |
if (search_expression_type->is_num_type(this->default_variable_name.current_type) && |
|
1418 |
search_expression_type->is_same_type(this->default_variable_name.current_type, this->current_operand_type)) { |
|
1419 |
XXX_operator(&(this->default_variable_name), " *= ", this->current_operand); |
|
1420 |
/* the data type resulting from this operation... */ |
|
1421 |
this->default_variable_name.current_type = this->current_operand_type; |
|
1422 |
return NULL; |
|
1423 |
} |
|
1424 |
ERROR; |
|
1425 |
return NULL; |
|
1426 |
} |
|
1427 |
||
1428 |
void *visit(DIV_operator_c *symbol) { |
|
149 | 1429 |
if (search_expression_type->is_time_type(this->default_variable_name.current_type) && |
1430 |
search_expression_type->is_integer_type(this->current_operand_type)) { |
|
1431 |
XXX_function("__TIME_DIV", &(this->default_variable_name), this->current_operand); |
|
1432 |
/* the data type resulting from this operation... */ |
|
1433 |
return NULL; |
|
1434 |
} |
|
70 | 1435 |
if (search_expression_type->is_num_type(this->default_variable_name.current_type) && |
1436 |
search_expression_type->is_same_type(this->default_variable_name.current_type, this->current_operand_type)) { |
|
1437 |
XXX_operator(&(this->default_variable_name), " /= ", this->current_operand); |
|
1438 |
/* the data type resulting from this operation... */ |
|
1439 |
this->default_variable_name.current_type = this->current_operand_type; |
|
1440 |
} |
|
149 | 1441 |
ERROR; |
70 | 1442 |
return NULL; |
1443 |
} |
|
1444 |
||
1445 |
void *visit(MOD_operator_c *symbol) { |
|
1446 |
if (search_expression_type->is_num_type(this->default_variable_name.current_type) && |
|
1447 |
search_expression_type->is_same_type(this->default_variable_name.current_type, this->current_operand_type)) { |
|
1448 |
XXX_operator(&(this->default_variable_name), " %= ", this->current_operand); |
|
1449 |
/* the data type resulting from this operation... */ |
|
1450 |
this->default_variable_name.current_type = this->current_operand_type; |
|
1451 |
} |
|
149 | 1452 |
ERROR; |
70 | 1453 |
return NULL; |
1454 |
} |
|
1455 |
||
1456 |
void *visit(GT_operator_c *symbol) { |
|
123 | 1457 |
if (!search_base_type.type_is_enumerated(this->default_variable_name.current_type) && |
1458 |
search_expression_type->is_same_type(this->default_variable_name.current_type, this->current_operand_type)) |
|
1459 |
return CMP_operator(this->current_operand, "__gt_"); |
|
1460 |
ERROR; |
|
1461 |
return NULL; |
|
70 | 1462 |
} |
1463 |
||
1464 |
void *visit(GE_operator_c *symbol) { |
|
123 | 1465 |
if (!search_base_type.type_is_enumerated(this->default_variable_name.current_type) && |
1466 |
search_expression_type->is_same_type(this->default_variable_name.current_type, this->current_operand_type)) |
|
1467 |
return CMP_operator(this->current_operand, "__ge_"); |
|
1468 |
ERROR; |
|
1469 |
return NULL; |
|
70 | 1470 |
} |
1471 |
||
1472 |
void *visit(EQ_operator_c *symbol) { |
|
123 | 1473 |
if (search_expression_type->is_same_type(this->default_variable_name.current_type, this->current_operand_type)) |
1474 |
return CMP_operator(this->current_operand, "__eq_"); |
|
1475 |
ERROR; |
|
1476 |
return NULL; |
|
70 | 1477 |
} |
1478 |
||
1479 |
void *visit(LT_operator_c *symbol) { |
|
123 | 1480 |
if (!search_base_type.type_is_enumerated(this->default_variable_name.current_type) && |
1481 |
search_expression_type->is_same_type(this->default_variable_name.current_type, this->current_operand_type)) |
|
1482 |
return CMP_operator(this->current_operand, "__lt_"); |
|
1483 |
ERROR; |
|
1484 |
return NULL; |
|
70 | 1485 |
} |
1486 |
||
1487 |
void *visit(LE_operator_c *symbol) { |
|
123 | 1488 |
if (!search_base_type.type_is_enumerated(this->default_variable_name.current_type) && |
1489 |
search_expression_type->is_same_type(this->default_variable_name.current_type, this->current_operand_type)) |
|
1490 |
return CMP_operator(this->current_operand, "__le_"); |
|
1491 |
ERROR; |
|
1492 |
return NULL; |
|
70 | 1493 |
} |
1494 |
||
1495 |
void *visit(NE_operator_c *symbol) { |
|
123 | 1496 |
if (search_expression_type->is_same_type(this->default_variable_name.current_type, this->current_operand_type)) |
1497 |
return CMP_operator(this->current_operand, "__ne_"); |
|
1498 |
ERROR; |
|
1499 |
return NULL; |
|
70 | 1500 |
} |
1501 |
||
1502 |
||
1503 |
//SYM_REF0(CAL_operator_c) |
|
1504 |
// This method will be called from within the il_fb_call_c visitor method |
|
1505 |
void *visit(CAL_operator_c *symbol) {return NULL;} |
|
1506 |
||
1507 |
//SYM_REF0(CALC_operator_c) |
|
1508 |
// This method will be called from within the il_fb_call_c visitor method |
|
1509 |
void *visit(CALC_operator_c *symbol) {C_modifier(); return NULL;} |
|
1510 |
||
1511 |
//SYM_REF0(CALCN_operator_c) |
|
1512 |
// This method will be called from within the il_fb_call_c visitor method |
|
1513 |
void *visit(CALCN_operator_c *symbol) {CN_modifier(); return NULL;} |
|
1514 |
||
1515 |
/* NOTE: The semantics of the RET operator requires us to return a value |
|
1516 |
* if the IL code is inside a function, but simply return no value if |
|
1517 |
* the IL code is inside a function block or program! |
|
1518 |
* Nevertheless, it is the generate_c_c class itself that |
|
1519 |
* introduces the 'reaturn <value>' into the c++ code at the end |
|
1520 |
* of every function. This class does not know whether the IL code |
|
1521 |
* is inside a function or a function block. |
|
1522 |
* We work around this by jumping to the end of the code, |
|
1523 |
* that will be marked by the END_LABEL label in the |
|
1524 |
* instruction_list_c visitor... |
|
1525 |
*/ |
|
1526 |
// SYM_REF0(RET_operator_c) |
|
1527 |
void *visit(RET_operator_c *symbol) { |
|
1528 |
s4o.print("goto ");s4o.print(END_LABEL); |
|
1529 |
return NULL; |
|
1530 |
} |
|
1531 |
||
1532 |
// SYM_REF0(RETC_operator_c) |
|
1533 |
void *visit(RETC_operator_c *symbol) { |
|
1534 |
C_modifier(); |
|
1535 |
s4o.print("goto ");s4o.print(END_LABEL); |
|
1536 |
return NULL; |
|
1537 |
} |
|
1538 |
||
1539 |
// SYM_REF0(RETCN_operator_c) |
|
1540 |
void *visit(RETCN_operator_c *symbol) { |
|
1541 |
CN_modifier(); |
|
1542 |
s4o.print("goto ");s4o.print(END_LABEL); |
|
1543 |
return NULL; |
|
1544 |
} |
|
1545 |
||
1546 |
//SYM_REF0(JMP_operator_c) |
|
1547 |
void *visit(JMP_operator_c *symbol) { |
|
1548 |
if (NULL == this->jump_label) ERROR; |
|
1549 |
||
1550 |
s4o.print("goto "); |
|
1551 |
this->jump_label->accept(*this); |
|
1552 |
/* the data type resulting from this operation is unchanged! */ |
|
1553 |
return NULL; |
|
1554 |
} |
|
1555 |
||
1556 |
// SYM_REF0(JMPC_operator_c) |
|
1557 |
void *visit(JMPC_operator_c *symbol) { |
|
1558 |
if (NULL == this->jump_label) ERROR; |
|
1559 |
||
1560 |
C_modifier(); |
|
1561 |
s4o.print("goto "); |
|
1562 |
this->jump_label->accept(*this); |
|
1563 |
/* the data type resulting from this operation is unchanged! */ |
|
1564 |
return NULL; |
|
1565 |
} |
|
1566 |
||
1567 |
// SYM_REF0(JMPCN_operator_c) |
|
1568 |
void *visit(JMPCN_operator_c *symbol) { |
|
1569 |
if (NULL == this->jump_label) ERROR; |
|
1570 |
||
1571 |
CN_modifier(); |
|
1572 |
s4o.print("goto "); |
|
1573 |
this->jump_label->accept(*this); |
|
1574 |
/* the data type resulting from this operation is unchanged! */ |
|
1575 |
return NULL; |
|
1576 |
} |
|
1577 |
||
1578 |
#if 0 |
|
1579 |
/*| [NOT] any_identifier SENDTO */ |
|
1580 |
SYM_REF2(il_assign_out_operator_c, option, variable_name) |
|
1581 |
#endif |
|
1582 |
||
1583 |
}; /* generate_c_il_c */ |
|
1584 |
||
1585 |
||
1586 |
||
1587 |
||
1588 |
||
1589 |
||
1590 |
||
1591 |
||
1592 |
||
1593 |
/* The implementation of the single visit() member function |
|
1594 |
* of il_default_variable_c. |
|
1595 |
* It can only come after the full declaration of |
|
1596 |
* generate_c_il_c. Since we define and declare |
|
1597 |
* generate_c_il_c simultaneously, it can only come |
|
1598 |
* after the definition... |
|
1599 |
*/ |
|
1600 |
void *il_default_variable_c::accept(visitor_c &visitor) { |
|
1601 |
/* An ugly hack!! */ |
|
1602 |
/* This is required because we need to over-ride the base |
|
1603 |
* accept(visitor_c &) method of the class symbol_c, |
|
1604 |
* so this method may be called through a symbol_c * |
|
1605 |
* reference! |
|
1606 |
* |
|
1607 |
* But, the visitor_c does not include a visitor to |
|
1608 |
* an il_default_variable_c, which means that we couldn't |
|
1609 |
* simply call visitor.visit(this); |
|
1610 |
* |
|
1611 |
* We therefore need to use the dynamic_cast hack!! |
|
1612 |
* |
|
1613 |
* Note too that we can't cast a visitor_c to a |
|
1614 |
* il_default_variable_visitor_c, since they are not related. |
|
1615 |
* Nor may the il_default_variable_visitor_c inherit from |
|
1616 |
* visitor_c, because then generate_c_il_c would contain |
|
1617 |
* two visitor_c base classes, one each through |
|
1618 |
* il_default_variable_visitor_c and generate_c_type_c |
|
1619 |
* |
|
1620 |
* We could use virtual inheritance of the visitor_c, but it |
|
1621 |
* would probably create more problems than it is worth! |
|
1622 |
*/ |
|
1623 |
generate_c_il_c *v; |
|
1624 |
v = dynamic_cast<generate_c_il_c *>(&visitor); |
|
1625 |
if (v == NULL) ERROR; |
|
1626 |
||
1627 |
return v->visit(this); |
|
1628 |
} |
|
1629 |
||
1630 |
||
1631 |
||
1632 |
||
1633 |
il_default_variable_c::il_default_variable_c(const char *var_name_str, symbol_c *current_type) { |
|
1634 |
if (NULL == var_name_str) ERROR; |
|
1635 |
/* Note: current_type may start off with NULL */ |
|
1636 |
||
1637 |
this->var_name = new identifier_c(var_name_str); |
|
1638 |
if (NULL == this->var_name) ERROR; |
|
1639 |
||
1640 |
this->current_type = current_type; |
|
1641 |
} |