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 variable declaration constructs.
|
|
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 |
//#include <stdio.h> /* required for NULL */
|
|
38 |
//#include <string>
|
|
39 |
//#include <iostream>
|
|
40 |
|
|
41 |
//#include "../../util/symtable.hh"
|
|
42 |
|
|
43 |
|
|
44 |
|
|
45 |
|
98
|
46 |
class generate_c_array_initialization_c: public generate_c_typedecl_c {
|
|
47 |
|
|
48 |
public:
|
|
49 |
typedef enum {
|
|
50 |
none_am,
|
|
51 |
dimensioncount_am,
|
|
52 |
initializationvalue_am,
|
|
53 |
arrayassignment_am,
|
|
54 |
varlistparse_am
|
|
55 |
} arrayinitialization_mode_t;
|
|
56 |
|
|
57 |
arrayinitialization_mode_t current_mode;
|
|
58 |
|
|
59 |
symbol_c* array_default_value;
|
|
60 |
|
|
61 |
private:
|
|
62 |
int dimension_number, current_dimension, array_size;
|
|
63 |
|
|
64 |
public:
|
|
65 |
generate_c_array_initialization_c(stage4out_c *s4o_ptr): generate_c_typedecl_c(s4o_ptr) {}
|
|
66 |
~generate_c_array_initialization_c(void) {}
|
|
67 |
|
|
68 |
void init_array(symbol_c *var1_list, symbol_c *array_specification, symbol_c *array_initialization) {
|
|
69 |
int i;
|
|
70 |
|
|
71 |
dimension_number = 0;
|
|
72 |
current_dimension = 0;
|
|
73 |
array_size = 1;
|
|
74 |
array_default_value = NULL;
|
|
75 |
|
|
76 |
current_mode = dimensioncount_am;
|
|
77 |
array_specification->accept(*this);
|
|
78 |
|
|
79 |
current_mode = initializationvalue_am;
|
|
80 |
s4o.print("\n");
|
|
81 |
s4o.print(s4o.indent_spaces + "{\n");
|
|
82 |
s4o.indent_right();
|
|
83 |
s4o.print(s4o.indent_spaces + "int index[");
|
|
84 |
print_integer(dimension_number);
|
|
85 |
s4o.print("];\n");
|
|
86 |
s4o.print(s4o.indent_spaces);
|
|
87 |
array_specification->accept(*this);
|
|
88 |
s4o.print(" temp = ");
|
|
89 |
array_initialization->accept(*this);
|
|
90 |
s4o.print(";\n");
|
|
91 |
|
|
92 |
current_mode = arrayassignment_am;
|
|
93 |
array_specification->accept(*this);
|
|
94 |
|
|
95 |
current_mode = varlistparse_am;
|
|
96 |
var1_list->accept(*this);
|
|
97 |
|
|
98 |
current_mode = arrayassignment_am;
|
|
99 |
for (i = 0; i < dimension_number; i++) {
|
|
100 |
s4o.indent_left();
|
|
101 |
s4o.print(s4o.indent_spaces + "}\n");
|
|
102 |
}
|
|
103 |
s4o.indent_left();
|
|
104 |
s4o.print(s4o.indent_spaces + "}");
|
|
105 |
}
|
|
106 |
|
|
107 |
void *visit(identifier_c *type_name) {
|
|
108 |
symbol_c *type_decl;
|
|
109 |
switch (current_mode) {
|
|
110 |
case dimensioncount_am:
|
|
111 |
case arrayassignment_am:
|
|
112 |
/* look up the type declaration... */
|
|
113 |
type_decl = type_symtable.find_value(type_name);
|
|
114 |
if (type_decl == type_symtable.end_value())
|
|
115 |
/* Type declaration not found!! */
|
|
116 |
ERROR;
|
|
117 |
type_decl->accept(*this);
|
|
118 |
break;
|
|
119 |
default:
|
|
120 |
print_token(type_name);
|
|
121 |
break;
|
|
122 |
}
|
|
123 |
return NULL;
|
|
124 |
}
|
|
125 |
|
|
126 |
void *visit(var1_list_c *symbol) {
|
|
127 |
int i, j;
|
|
128 |
|
|
129 |
for (i = 0; i < symbol->n; i++) {
|
|
130 |
s4o.print(s4o.indent_spaces);
|
|
131 |
print_variable_prefix();
|
|
132 |
symbol->elements[i]->accept(*this);
|
|
133 |
for (j = 0; j < dimension_number; j++) {
|
|
134 |
s4o.print("[index[");
|
|
135 |
print_integer(j);
|
|
136 |
s4o.print("]]");
|
|
137 |
}
|
|
138 |
s4o.print(" = temp");
|
|
139 |
for (j = 0; j < dimension_number; j++) {
|
|
140 |
s4o.print("[index[");
|
|
141 |
print_integer(j);
|
|
142 |
s4o.print("]]");
|
|
143 |
}
|
|
144 |
s4o.print(";\n");
|
|
145 |
}
|
|
146 |
return NULL;
|
|
147 |
}
|
|
148 |
|
|
149 |
/********************************/
|
|
150 |
/* B 1.3.3 - Derived data types */
|
|
151 |
/********************************/
|
|
152 |
|
|
153 |
/* ARRAY '[' array_subrange_list ']' OF non_generic_type_name */
|
|
154 |
void *visit(array_specification_c *symbol) {
|
|
155 |
switch (current_mode) {
|
|
156 |
case dimensioncount_am:
|
|
157 |
symbol->array_subrange_list->accept(*this);
|
|
158 |
array_default_value = (symbol_c *)symbol->non_generic_type_name->accept(*type_initial_value_c::instance());;
|
|
159 |
break;
|
|
160 |
default:
|
|
161 |
symbol->array_subrange_list->accept(*this);
|
|
162 |
break;
|
|
163 |
}
|
|
164 |
return NULL;
|
|
165 |
}
|
|
166 |
|
|
167 |
/* signed_integer DOTDOT signed_integer */
|
|
168 |
//SYM_REF2(subrange_c, lower_limit, upper_limit)
|
|
169 |
void *visit(subrange_c *symbol) {
|
|
170 |
switch (current_mode) {
|
|
171 |
case dimensioncount_am:
|
|
172 |
dimension_number++;
|
|
173 |
array_size *= extract_integer(symbol->upper_limit) - extract_integer(symbol->lower_limit) + 1;
|
|
174 |
break;
|
|
175 |
case arrayassignment_am:
|
|
176 |
s4o.print(s4o.indent_spaces + "for (index[");
|
|
177 |
print_integer(current_dimension);
|
|
178 |
s4o.print("] = 0; index[");
|
|
179 |
print_integer(current_dimension);
|
|
180 |
s4o.print("] <= ");
|
|
181 |
print_integer(extract_integer(symbol->upper_limit) - extract_integer(symbol->lower_limit));
|
|
182 |
s4o.print("; index[");
|
|
183 |
print_integer(current_dimension);
|
|
184 |
s4o.print("]++) {\n");
|
|
185 |
s4o.indent_right();
|
|
186 |
current_dimension++;
|
|
187 |
break;
|
|
188 |
default:
|
|
189 |
break;
|
|
190 |
}
|
|
191 |
return NULL;
|
|
192 |
}
|
|
193 |
|
|
194 |
/* helper symbol for array_initialization */
|
|
195 |
/* array_initial_elements_list ',' array_initial_elements */
|
|
196 |
void *visit(array_initial_elements_list_c *symbol) {
|
|
197 |
switch (current_mode) {
|
|
198 |
case initializationvalue_am:
|
|
199 |
int i;
|
|
200 |
|
|
201 |
s4o.print("{");
|
|
202 |
for (i = 0; i < symbol->n; i++) {
|
|
203 |
if (i > 0)
|
|
204 |
s4o.print(", ");
|
|
205 |
symbol->elements[i]->accept(*this);
|
|
206 |
array_size--;
|
|
207 |
}
|
|
208 |
if (array_size > 0) {
|
|
209 |
if (symbol->n > 0)
|
|
210 |
s4o.print(", ");
|
|
211 |
for (i = 0; i < array_size; i++) {
|
|
212 |
if (i > 0)
|
|
213 |
s4o.print(", ");
|
|
214 |
array_default_value->accept(*this);
|
|
215 |
}
|
|
216 |
}
|
|
217 |
s4o.print("}");
|
|
218 |
break;
|
|
219 |
default:
|
|
220 |
break;
|
|
221 |
}
|
|
222 |
return NULL;
|
|
223 |
}
|
|
224 |
|
|
225 |
/* integer '(' [array_initial_element] ')' */
|
|
226 |
/* array_initial_element may be NULL ! */
|
|
227 |
void *visit(array_initial_elements_c *symbol) {
|
|
228 |
int initial_element_number;
|
|
229 |
|
|
230 |
switch (current_mode) {
|
|
231 |
case initializationvalue_am:
|
|
232 |
initial_element_number = extract_integer(symbol->integer);
|
|
233 |
|
|
234 |
for (int i = 0; i < initial_element_number; i++) {
|
|
235 |
if (i > 0)
|
|
236 |
s4o.print(", ");
|
|
237 |
if (symbol->array_initial_element != NULL)
|
|
238 |
symbol->array_initial_element->accept(*this);
|
|
239 |
else if (array_default_value != NULL)
|
|
240 |
array_default_value->accept(*this);
|
|
241 |
}
|
|
242 |
if (initial_element_number > 1)
|
|
243 |
array_size -= initial_element_number - 1;
|
|
244 |
break;
|
|
245 |
default:
|
|
246 |
break;
|
|
247 |
}
|
|
248 |
return NULL;
|
|
249 |
}
|
|
250 |
|
|
251 |
};
|
70
|
252 |
|
|
253 |
/***********************************************************************/
|
|
254 |
/***********************************************************************/
|
|
255 |
/***********************************************************************/
|
|
256 |
/***********************************************************************/
|
|
257 |
/***********************************************************************/
|
|
258 |
/***********************************************************************/
|
|
259 |
/***********************************************************************/
|
|
260 |
/***********************************************************************/
|
|
261 |
|
|
262 |
|
|
263 |
|
|
264 |
|
|
265 |
class generate_c_vardecl_c: protected generate_c_typedecl_c {
|
|
266 |
|
|
267 |
/* A Helper class to the main class... */
|
|
268 |
/* print a string, except the first time it is called */
|
|
269 |
/* used to print the separator "," before each variable
|
|
270 |
* declaration, except the first...
|
|
271 |
*
|
|
272 |
* Needs to be done in a seperate class because this is called
|
|
273 |
* from within various locations within the code.
|
|
274 |
*/
|
|
275 |
class next_var_c {
|
|
276 |
private:
|
|
277 |
bool print_flag;
|
|
278 |
std::string str1, str2;
|
|
279 |
|
|
280 |
next_var_c *embedded_scope;
|
|
281 |
|
|
282 |
public:
|
|
283 |
next_var_c(std::string s1, std::string s2) {
|
|
284 |
str1 = s1;
|
|
285 |
str2 = s2;
|
|
286 |
print_flag = false;
|
|
287 |
embedded_scope = NULL;
|
|
288 |
}
|
|
289 |
|
|
290 |
std::string get(void) {
|
|
291 |
if (NULL != embedded_scope)
|
|
292 |
return embedded_scope->get();
|
|
293 |
|
|
294 |
bool old_print_flag = print_flag;
|
|
295 |
print_flag = true;
|
|
296 |
if (!old_print_flag)
|
|
297 |
return str1;
|
|
298 |
else
|
|
299 |
return str2;
|
|
300 |
}
|
|
301 |
|
|
302 |
/* Create a new next_var_c for an embedded scope.
|
|
303 |
* From now on, every call to get() will return the
|
|
304 |
* inner-most scope...!
|
|
305 |
*/
|
|
306 |
void push(std::string s1, std::string s2) {
|
|
307 |
if (NULL != embedded_scope)
|
|
308 |
return embedded_scope->push(s1, s2);
|
|
309 |
|
|
310 |
embedded_scope = new next_var_c(s1, s2);
|
|
311 |
if (NULL == embedded_scope)
|
|
312 |
ERROR;
|
|
313 |
return;
|
|
314 |
}
|
|
315 |
|
|
316 |
/* Remove the inner-most scope... */
|
|
317 |
void pop(void) {
|
|
318 |
if (NULL != embedded_scope)
|
|
319 |
return embedded_scope->pop();
|
|
320 |
|
|
321 |
delete embedded_scope;
|
|
322 |
embedded_scope = NULL;
|
|
323 |
}
|
|
324 |
};
|
|
325 |
|
|
326 |
private:
|
|
327 |
/* used to generate the ',' separating the parameters in a function call */
|
|
328 |
next_var_c *nv;
|
|
329 |
|
|
330 |
|
|
331 |
public:
|
|
332 |
/* the types of variables that need to be processed... */
|
|
333 |
static const unsigned int none_vt = 0x0000;
|
|
334 |
static const unsigned int input_vt = 0x0001; // VAR_INPUT
|
|
335 |
static const unsigned int output_vt = 0x0002; // VAR_OUTPUT
|
|
336 |
static const unsigned int inoutput_vt = 0x0004; // VAR_IN_OUT
|
|
337 |
static const unsigned int private_vt = 0x0008; // VAR
|
|
338 |
static const unsigned int temp_vt = 0x0010; // VAR_TEMP
|
|
339 |
static const unsigned int external_vt = 0x0020; // VAR_EXTERNAL
|
|
340 |
static const unsigned int global_vt = 0x0040; // VAR_GLOBAL
|
|
341 |
// Globals declared inside a resource will not be declared
|
|
342 |
// unless global_vt is acompanied by resource_vt
|
|
343 |
static const unsigned int located_vt = 0x0080; // VAR <var_name> AT <location>
|
|
344 |
static const unsigned int program_vt = 0x0100; // PROGRAM (inside a configuration!)
|
|
345 |
// Programs declared inside a resource will not be declared
|
|
346 |
// unless program_vt is acompanied by resource_vt
|
|
347 |
|
|
348 |
static const unsigned int resource_vt = 0x8000; // RESOURCE (inside a configuration!)
|
|
349 |
// This, just of itself, will not print out any declarations!!
|
|
350 |
// It must be acompanied by either program_vt and/or global_vt
|
|
351 |
|
|
352 |
/* How variables should be declared: as local variables or
|
|
353 |
* variables within a function call interface.
|
|
354 |
*
|
|
355 |
* This will define the format of the output generated
|
|
356 |
* by this class.
|
|
357 |
*
|
|
358 |
* finterface_vf: function interface parameters
|
|
359 |
* e.g. f( int a, long b, real c );
|
|
360 |
* ---------------------
|
|
361 |
* This class/function will produce the
|
|
362 |
* underlined code of the above examples,
|
|
363 |
* and no more!!
|
|
364 |
*
|
|
365 |
* localinit_vf: local declaration. Will include variable
|
|
366 |
* initialisation if it is not a located variable.
|
|
367 |
* e.g.
|
|
368 |
* int a = 9;
|
|
369 |
* long b = 99;
|
|
370 |
* real c = 99.9;
|
|
371 |
*
|
|
372 |
* local_vf: local declaration. Will NOT include variable
|
|
373 |
* initialisation.
|
|
374 |
* e.g.
|
|
375 |
* int a;
|
|
376 |
* long b;
|
|
377 |
* real c;
|
|
378 |
*
|
|
379 |
* init_vf: local initialisation without declaration.
|
|
380 |
* e.g.
|
|
381 |
* a = 9;
|
|
382 |
* b = 99;
|
|
383 |
* c = 99.9;
|
|
384 |
*
|
|
385 |
* constructorinit_vf: initialising of member variables...
|
|
386 |
* e.g. for a constructor...
|
|
387 |
* class_name_c(void)
|
|
388 |
* : a(9), b(99), c(99.9) { // code... }
|
|
389 |
* --------------------
|
|
390 |
* This class/function will produce the
|
|
391 |
* underlined code of the above examples,
|
|
392 |
* and no more!!
|
|
393 |
*
|
|
394 |
* globalinit_vf: initialising of static c++ variables. These
|
|
395 |
* variables may have been declared as static inside
|
|
396 |
* a class, in which case the scope within which they were
|
|
397 |
* previously delcared must be passed as the second parameter
|
|
398 |
* to the print() function.
|
|
399 |
*
|
|
400 |
* e.g.
|
|
401 |
* __plc_pt_c<INT, 8*sizeof(INT)> START_P::loc = __plc_pt_c<INT, 8*sizeof(INT)>("I2");
|
|
402 |
*/
|
|
403 |
typedef enum {finterface_vf,
|
|
404 |
local_vf,
|
|
405 |
localstatic_vf,
|
|
406 |
localinit_vf,
|
|
407 |
init_vf,
|
|
408 |
constructorinit_vf,
|
|
409 |
globalinit_vf
|
|
410 |
} varformat_t;
|
|
411 |
|
|
412 |
|
|
413 |
private:
|
98
|
414 |
generate_c_array_initialization_c *generate_c_array_initialization;
|
|
415 |
|
70
|
416 |
/* variable used to store the types of variables that need to be processed... */
|
|
417 |
/* Only set in the constructor...! */
|
|
418 |
/* Will contain a set of values of generate_c_vardecl_c::XXXX_vt */
|
|
419 |
unsigned int wanted_vartype;
|
|
420 |
/* variable used to store the type of variable currently being processed... */
|
|
421 |
/* Will contain a single value of generate_c_vardecl_c::XXXX_vt */
|
|
422 |
unsigned int current_vartype;
|
|
423 |
|
|
424 |
/* How variables should be declared: as local variables or
|
|
425 |
* variables within a function interface...
|
|
426 |
*/
|
|
427 |
/* Only set in the constructor...! */
|
|
428 |
varformat_t wanted_varformat;
|
|
429 |
|
|
430 |
/* The number of variables already declared. */
|
|
431 |
/* Used to declare 'void' in case no variables are declared in a function interface... */
|
|
432 |
int finterface_var_count;
|
|
433 |
|
|
434 |
|
|
435 |
|
|
436 |
/* Holds the references to the type and initial value
|
|
437 |
* of the variables currently being declared.
|
|
438 |
* Please read the comment under var1_init_decl_c for further
|
|
439 |
* details...
|
|
440 |
*
|
|
441 |
* We make an effort to keep these pointers pointing to NULL
|
|
442 |
* whenever we are outside the scope of variable declaration
|
|
443 |
* (i.e. when we are traversing a part of the parse tree which
|
|
444 |
* is not part of variable declaration) in order tio try to catch
|
|
445 |
* any bugs as soon as possible.
|
|
446 |
*/
|
|
447 |
symbol_c *current_var_type_symbol;
|
|
448 |
symbol_c *current_var_init_symbol;
|
|
449 |
void update_type_init(symbol_c *symbol /* a spec_init_c, subrange_spec_init_c, etc... */ ) {
|
|
450 |
this->current_var_type_symbol = spec_init_sperator_c::get_spec(symbol);
|
|
451 |
this->current_var_init_symbol = spec_init_sperator_c::get_init(symbol);
|
|
452 |
if (NULL == this->current_var_type_symbol)
|
|
453 |
ERROR;
|
|
454 |
if (NULL == this->current_var_init_symbol) {
|
|
455 |
/* We try to find the data type's default value... */
|
|
456 |
this->current_var_init_symbol = (symbol_c *)this->current_var_type_symbol->accept(*type_initial_value_c::instance());
|
|
457 |
/* Note that Function Block 'data types' do not have a default value, so we cannot abort if no default value is found! */
|
|
458 |
/*
|
|
459 |
if (NULL == this->current_var_init_symbol)
|
|
460 |
ERROR;
|
|
461 |
*/
|
|
462 |
}
|
|
463 |
}
|
|
464 |
|
|
465 |
void void_type_init(void) {
|
|
466 |
this->current_var_type_symbol = NULL;
|
|
467 |
this->current_var_init_symbol = NULL;
|
|
468 |
}
|
|
469 |
|
|
470 |
/* Only used when wanted_varformat == globalinit_vf
|
|
471 |
* Holds a pointer to an identifier_c, which in turns contains
|
|
472 |
* the identifier of the scope within which the static member was
|
|
473 |
* declared.
|
|
474 |
*/
|
|
475 |
symbol_c *globalnamespace;
|
|
476 |
|
|
477 |
/* Actually produce the output where variables are declared... */
|
|
478 |
/* Note that located variables are the exception, they
|
|
479 |
* being declared in the located_var_decl_c visitor...
|
|
480 |
*/
|
|
481 |
void *declare_variables(symbol_c *symbol, bool is_fb = false) {
|
|
482 |
list_c *list = dynamic_cast<list_c *>(symbol);
|
|
483 |
/* should NEVER EVER occur!! */
|
|
484 |
if (list == NULL) ERROR;
|
|
485 |
|
|
486 |
/* now to produce the c equivalent... */
|
|
487 |
if ((wanted_varformat == local_vf) ||
|
|
488 |
(wanted_varformat == init_vf) ||
|
|
489 |
(wanted_varformat == localinit_vf)) {
|
|
490 |
for(int i = 0; i < list->n; i++) {
|
|
491 |
s4o.print(s4o.indent_spaces);
|
|
492 |
if (wanted_varformat != init_vf) {
|
|
493 |
this->current_var_type_symbol->accept(*this);
|
|
494 |
s4o.print(" ");
|
|
495 |
}
|
|
496 |
print_variable_prefix();
|
|
497 |
list->elements[i]->accept(*this);
|
|
498 |
if (wanted_varformat != local_vf) {
|
|
499 |
if (this->current_var_init_symbol != NULL) {
|
|
500 |
s4o.print(" = ");
|
|
501 |
this->current_var_init_symbol->accept(*this);
|
|
502 |
}
|
|
503 |
}
|
|
504 |
s4o.print(";\n");
|
|
505 |
}
|
|
506 |
}
|
|
507 |
|
|
508 |
if (wanted_varformat == finterface_vf) {
|
|
509 |
for(int i = 0; i < list->n; i++) {
|
|
510 |
finterface_var_count++;
|
|
511 |
s4o.print(nv->get());
|
|
512 |
s4o.print("\n" + s4o.indent_spaces);
|
|
513 |
this->current_var_type_symbol->accept(*this);
|
|
514 |
if ((current_vartype & (output_vt | inoutput_vt)) != 0)
|
|
515 |
s4o.print(" &");
|
|
516 |
else
|
|
517 |
s4o.print(" ");
|
|
518 |
list->elements[i]->accept(*this);
|
|
519 |
/* We do not print the initial value at function declaration!
|
|
520 |
* It is up to the caller to pass the correct default value
|
|
521 |
* if none is specified in the ST source code
|
|
522 |
*/
|
|
523 |
/* if (this->current_var_init_symbol != NULL) {
|
|
524 |
s4o.print(" = "); this->current_var_init_symbol->accept(*this);}
|
|
525 |
*/
|
|
526 |
}
|
|
527 |
}
|
|
528 |
|
|
529 |
if (wanted_varformat == constructorinit_vf) {
|
|
530 |
for(int i = 0; i < list->n; i++) {
|
|
531 |
if (is_fb) {
|
|
532 |
s4o.print(nv->get());
|
|
533 |
this->current_var_type_symbol->accept(*this);
|
|
534 |
s4o.print(FB_INIT_SUFFIX);
|
|
535 |
s4o.print("(&");
|
|
536 |
this->print_variable_prefix();
|
|
537 |
list->elements[i]->accept(*this);
|
|
538 |
s4o.print(");");
|
|
539 |
}
|
|
540 |
else if (this->current_var_init_symbol != NULL) {
|
|
541 |
s4o.print(nv->get());
|
|
542 |
this->print_variable_prefix();
|
|
543 |
list->elements[i]->accept(*this);
|
|
544 |
s4o.print(" = ");
|
|
545 |
this->current_var_init_symbol->accept(*this);
|
|
546 |
s4o.print(";");
|
|
547 |
}
|
|
548 |
}
|
|
549 |
}
|
|
550 |
|
|
551 |
return NULL;
|
|
552 |
}
|
|
553 |
|
|
554 |
|
|
555 |
|
|
556 |
|
|
557 |
public:
|
|
558 |
generate_c_vardecl_c(stage4out_c *s4o_ptr, varformat_t varformat, unsigned int vartype)
|
|
559 |
: generate_c_typedecl_c(s4o_ptr) {
|
98
|
560 |
generate_c_array_initialization = new generate_c_array_initialization_c(s4o_ptr);
|
70
|
561 |
wanted_varformat = varformat;
|
|
562 |
wanted_vartype = vartype;
|
|
563 |
current_vartype = none_vt;
|
|
564 |
current_var_type_symbol = NULL;
|
|
565 |
current_var_init_symbol = NULL;
|
|
566 |
globalnamespace = NULL;
|
|
567 |
nv = NULL;
|
|
568 |
}
|
|
569 |
|
98
|
570 |
~generate_c_vardecl_c(void) {
|
|
571 |
delete generate_c_array_initialization;
|
|
572 |
}
|
70
|
573 |
|
|
574 |
|
|
575 |
void print(symbol_c *symbol, symbol_c *scope = NULL, const char *variable_prefix = NULL) {
|
|
576 |
this->set_variable_prefix(variable_prefix);
|
98
|
577 |
this->generate_c_array_initialization->set_variable_prefix(variable_prefix);
|
70
|
578 |
if (globalinit_vf == wanted_varformat)
|
|
579 |
globalnamespace = scope;
|
|
580 |
|
|
581 |
finterface_var_count = 0;
|
|
582 |
|
|
583 |
switch (wanted_varformat) {
|
|
584 |
case constructorinit_vf: nv = new next_var_c("", "\n"+s4o.indent_spaces); break;
|
|
585 |
case finterface_vf: /* fall through... */
|
|
586 |
case localinit_vf: /* fall through... */
|
|
587 |
case local_vf: nv = new next_var_c("", ", "); break;
|
|
588 |
default: nv = NULL;
|
|
589 |
} /* switch() */
|
|
590 |
|
|
591 |
symbol->accept(*this);
|
|
592 |
|
|
593 |
/* special case... */
|
|
594 |
if (wanted_varformat == finterface_vf)
|
|
595 |
if (finterface_var_count == 0)
|
|
596 |
s4o.print("void");
|
|
597 |
|
|
598 |
delete nv;
|
|
599 |
nv = NULL;
|
|
600 |
globalnamespace = NULL;
|
|
601 |
}
|
|
602 |
|
|
603 |
|
|
604 |
protected:
|
|
605 |
/***************************/
|
|
606 |
/* B 0 - Programming Model */
|
|
607 |
/***************************/
|
|
608 |
/* leave for derived classes... */
|
|
609 |
|
|
610 |
/*************************/
|
|
611 |
/* B.1 - Common elements */
|
|
612 |
/*************************/
|
|
613 |
/*******************************************/
|
|
614 |
/* B 1.1 - Letters, digits and identifiers */
|
|
615 |
/*******************************************/
|
|
616 |
/* done in base class(es) */
|
|
617 |
|
|
618 |
/*********************/
|
|
619 |
/* B 1.2 - Constants */
|
|
620 |
/*********************/
|
|
621 |
/* originally empty... */
|
|
622 |
|
|
623 |
/******************************/
|
|
624 |
/* B 1.2.1 - Numeric Literals */
|
|
625 |
/******************************/
|
|
626 |
/* done in base class(es) */
|
|
627 |
|
|
628 |
/*******************************/
|
|
629 |
/* B.1.2.2 Character Strings */
|
|
630 |
/*******************************/
|
|
631 |
/* done in base class(es) */
|
|
632 |
|
|
633 |
/***************************/
|
|
634 |
/* B 1.2.3 - Time Literals */
|
|
635 |
/***************************/
|
|
636 |
/************************/
|
|
637 |
/* B 1.2.3.1 - Duration */
|
|
638 |
/************************/
|
|
639 |
/* done in base class(es) */
|
|
640 |
|
|
641 |
/************************************/
|
|
642 |
/* B 1.2.3.2 - Time of day and Date */
|
|
643 |
/************************************/
|
|
644 |
/* done in base class(es) */
|
|
645 |
|
|
646 |
/**********************/
|
|
647 |
/* B.1.3 - Data types */
|
|
648 |
/**********************/
|
|
649 |
/***********************************/
|
|
650 |
/* B 1.3.1 - Elementary Data Types */
|
|
651 |
/***********************************/
|
|
652 |
/* done in base class(es) */
|
|
653 |
|
|
654 |
/********************************/
|
|
655 |
/* B.1.3.2 - Generic data types */
|
|
656 |
/********************************/
|
|
657 |
/* originally empty... */
|
|
658 |
|
|
659 |
/********************************/
|
|
660 |
/* B 1.3.3 - Derived data types */
|
|
661 |
/********************************/
|
|
662 |
/* done in base class(es) */
|
|
663 |
|
|
664 |
/*********************/
|
|
665 |
/* B 1.4 - Variables */
|
|
666 |
/*********************/
|
|
667 |
/* done in base class(es) */
|
|
668 |
|
|
669 |
/********************************************/
|
|
670 |
/* B.1.4.1 Directly Represented Variables */
|
|
671 |
/********************************************/
|
|
672 |
/* done in base class(es) */
|
|
673 |
|
|
674 |
/*************************************/
|
|
675 |
/* B.1.4.2 Multi-element Variables */
|
|
676 |
/*************************************/
|
|
677 |
/* done in base class(es) */
|
|
678 |
|
|
679 |
/******************************************/
|
|
680 |
/* B 1.4.3 - Declaration & Initialisation */
|
|
681 |
/******************************************/
|
|
682 |
void *visit(constant_option_c *symbol) {s4o.print("CONSTANT"); return NULL;}
|
|
683 |
void *visit(retain_option_c *symbol) {s4o.print("RETAIN"); return NULL;}
|
|
684 |
void *visit(non_retain_option_c *symbol) {s4o.print("NON_RETAIN"); return NULL;}
|
|
685 |
|
|
686 |
void *visit(input_declarations_c *symbol) {
|
|
687 |
TRACE("input_declarations_c");
|
|
688 |
if ((wanted_vartype & input_vt) != 0) {
|
|
689 |
/*
|
|
690 |
// TO DO ...
|
|
691 |
if (symbol->option != NULL)
|
|
692 |
symbol->option->accept(*this);
|
|
693 |
*/
|
|
694 |
//s4o.indent_right();
|
|
695 |
current_vartype = input_vt;
|
|
696 |
symbol->input_declaration_list->accept(*this);
|
|
697 |
current_vartype = none_vt;
|
|
698 |
//s4o.indent_left();
|
|
699 |
}
|
|
700 |
return NULL;
|
|
701 |
}
|
|
702 |
|
|
703 |
/* helper symbol for input_declarations */
|
|
704 |
void *visit(input_declaration_list_c *symbol) {
|
|
705 |
TRACE("input_declaration_list_c");
|
|
706 |
print_list(symbol);
|
|
707 |
return NULL;
|
|
708 |
}
|
|
709 |
|
|
710 |
void *visit(edge_declaration_c *symbol) {
|
|
711 |
TRACE("edge_declaration_c");
|
|
712 |
// TO DO ...
|
|
713 |
symbol->var1_list->accept(*this);
|
|
714 |
s4o.print(" : BOOL ");
|
|
715 |
symbol->edge->accept(*this);
|
|
716 |
return NULL;
|
|
717 |
}
|
|
718 |
|
|
719 |
void *visit(raising_edge_option_c *symbol) {
|
|
720 |
// TO DO ...
|
|
721 |
s4o.print("R_EDGE");
|
|
722 |
return NULL;
|
|
723 |
}
|
|
724 |
|
98
|
725 |
/* var1_list ':' array_spec_init */
|
|
726 |
// SYM_REF2(array_var_init_decl_c, var1_list, array_spec_init)
|
|
727 |
void *visit(array_var_init_decl_c *symbol) {
|
|
728 |
TRACE("array_var_init_decl_c");
|
|
729 |
/* Please read the comments inside the var1_init_decl_c
|
|
730 |
* visitor, as they apply here too.
|
|
731 |
*/
|
|
732 |
|
|
733 |
/* Start off by setting the current_var_type_symbol and
|
|
734 |
* current_var_init_symbol private variables...
|
|
735 |
*/
|
|
736 |
update_type_init(symbol->array_spec_init);
|
|
737 |
|
|
738 |
/* now to produce the c equivalent... */
|
|
739 |
if (wanted_varformat == constructorinit_vf)
|
|
740 |
generate_c_array_initialization->init_array(symbol->var1_list, this->current_var_type_symbol, this->current_var_init_symbol);
|
|
741 |
else
|
|
742 |
symbol->var1_list->accept(*this);
|
|
743 |
|
|
744 |
/* Values no longer in scope, and therefore no longer used.
|
|
745 |
* Make an effort to keep them set to NULL when not in use
|
|
746 |
* in order to catch bugs as soon as possible...
|
|
747 |
*/
|
|
748 |
void_type_init();
|
|
749 |
|
|
750 |
return NULL;
|
|
751 |
}
|
70
|
752 |
|
|
753 |
/* var1_list ':' initialized_structure */
|
|
754 |
// SYM_REF2(structured_var_init_decl_c, var1_list, initialized_structure)
|
|
755 |
void *visit(structured_var_init_decl_c *symbol) {
|
|
756 |
TRACE("structured_var_init_decl_c");
|
|
757 |
/* Please read the comments inside the var1_init_decl_c
|
|
758 |
* visitor, as they apply here too.
|
|
759 |
*/
|
|
760 |
|
|
761 |
/* Start off by setting the current_var_type_symbol and
|
|
762 |
* current_var_init_symbol private variables...
|
|
763 |
*/
|
|
764 |
update_type_init(symbol->initialized_structure);
|
|
765 |
|
|
766 |
/* now to produce the c equivalent... */
|
|
767 |
symbol->var1_list->accept(*this);
|
|
768 |
|
|
769 |
/* Values no longer in scope, and therefore no longer used.
|
|
770 |
* Make an effort to keep them set to NULL when not in use
|
|
771 |
* in order to catch bugs as soon as possible...
|
|
772 |
*/
|
|
773 |
void_type_init();
|
|
774 |
|
|
775 |
return NULL;
|
|
776 |
}
|
|
777 |
|
|
778 |
/* fb_name_list ':' function_block_type_name ASSIGN structure_initialization */
|
|
779 |
/* structure_initialization -> may be NULL ! */
|
|
780 |
void *visit(fb_name_decl_c *symbol) {
|
|
781 |
TRACE("fb_name_decl_c");
|
|
782 |
/* Please read the comments inside the var1_init_decl_c
|
|
783 |
* visitor, as they apply here too.
|
|
784 |
*/
|
|
785 |
|
|
786 |
/* Start off by setting the current_var_type_symbol and
|
|
787 |
* current_var_init_symbol private variables...
|
|
788 |
*/
|
|
789 |
update_type_init(symbol);
|
|
790 |
|
|
791 |
/* now to produce the c equivalent... */
|
|
792 |
symbol->fb_name_list->accept(*this);
|
|
793 |
|
|
794 |
/* Values no longer in scope, and therefore no longer used.
|
|
795 |
* Make an effort to keep them set to NULL when not in use
|
|
796 |
* in order to catch bugs as soon as possible...
|
|
797 |
*/
|
|
798 |
void_type_init();
|
|
799 |
|
|
800 |
return NULL;
|
|
801 |
}
|
|
802 |
|
|
803 |
/* fb_name_list ',' fb_name */
|
|
804 |
void *visit(fb_name_list_c *symbol) {
|
|
805 |
TRACE("fb_name_list_c");
|
|
806 |
declare_variables(symbol, true);
|
|
807 |
return NULL;
|
|
808 |
}
|
|
809 |
|
|
810 |
|
|
811 |
/* VAR_OUTPUT [RETAIN | NON_RETAIN] var_init_decl_list END_VAR */
|
|
812 |
/* option -> may be NULL ! */
|
|
813 |
void *visit(output_declarations_c *symbol) {
|
|
814 |
TRACE("output_declarations_c");
|
|
815 |
if ((wanted_vartype & output_vt) != 0) {
|
|
816 |
/*
|
|
817 |
// TO DO ...
|
|
818 |
if (symbol->option != NULL)
|
|
819 |
symbol->option->accept(*this);
|
|
820 |
*/
|
|
821 |
//s4o.indent_right();
|
|
822 |
current_vartype = output_vt;
|
|
823 |
symbol->var_init_decl_list->accept(*this);
|
|
824 |
current_vartype = none_vt;
|
|
825 |
//s4o.indent_left();
|
|
826 |
}
|
|
827 |
return NULL;
|
|
828 |
}
|
|
829 |
|
|
830 |
/* VAR_IN_OUT var_declaration_list END_VAR */
|
|
831 |
void *visit(input_output_declarations_c *symbol) {
|
|
832 |
TRACE("input_output_declarations_c");
|
|
833 |
if ((wanted_vartype & inoutput_vt) != 0) {
|
|
834 |
//s4o.indent_right();
|
|
835 |
current_vartype = inoutput_vt;
|
|
836 |
symbol->var_declaration_list->accept(*this);
|
|
837 |
current_vartype = none_vt;
|
|
838 |
//s4o.indent_left();
|
|
839 |
}
|
|
840 |
return NULL;
|
|
841 |
}
|
|
842 |
|
|
843 |
/* helper symbol for input_output_declarations */
|
|
844 |
/* var_declaration_list var_declaration ';' */
|
|
845 |
void *visit(var_declaration_list_c *symbol) {
|
|
846 |
TRACE("var_declaration_list_c");
|
|
847 |
print_list(symbol);
|
|
848 |
return NULL;
|
|
849 |
}
|
|
850 |
|
|
851 |
#if 0
|
|
852 |
/* var1_list ':' array_specification */
|
|
853 |
SYM_REF2(array_var_declaration_c, var1_list, array_specification)
|
|
854 |
#endif
|
|
855 |
|
98
|
856 |
void *visit(array_initial_elements_list_c *symbol) {
|
|
857 |
s4o.print(";// array initialisation");
|
|
858 |
return NULL;
|
|
859 |
}
|
|
860 |
|
70
|
861 |
/* var1_list ':' structure_type_name */
|
|
862 |
//SYM_REF2(structured_var_declaration_c, var1_list, structure_type_name)
|
|
863 |
void *visit(structured_var_declaration_c *symbol) {
|
|
864 |
TRACE("structured_var_declaration_c");
|
|
865 |
/* Please read the comments inside the var1_init_decl_c
|
|
866 |
* visitor, as they apply here too.
|
|
867 |
*/
|
|
868 |
|
|
869 |
/* Start off by setting the current_var_type_symbol and
|
|
870 |
* current_var_init_symbol private variables...
|
|
871 |
*/
|
|
872 |
update_type_init(symbol->structure_type_name);
|
|
873 |
|
|
874 |
/* now to produce the c equivalent... */
|
|
875 |
symbol->var1_list->accept(*this);
|
|
876 |
|
|
877 |
/* Values no longer in scope, and therefore no longer used.
|
|
878 |
* Make an effort to keep them set to NULL when not in use
|
|
879 |
* in order to catch bugs as soon as possible...
|
|
880 |
*/
|
|
881 |
void_type_init();
|
|
882 |
|
|
883 |
return NULL;
|
|
884 |
}
|
|
885 |
|
|
886 |
|
|
887 |
/* VAR [CONSTANT] var_init_decl_list END_VAR */
|
|
888 |
/* option -> may be NULL ! */
|
|
889 |
/* helper symbol for input_declarations */
|
|
890 |
void *visit(var_declarations_c *symbol) {
|
|
891 |
TRACE("var_declarations_c");
|
|
892 |
if ((wanted_vartype & private_vt) != 0) {
|
|
893 |
/*
|
|
894 |
// TO DO ...
|
|
895 |
if (symbol->option != NULL)
|
|
896 |
symbol->option->accept(*this);
|
|
897 |
*/
|
|
898 |
current_vartype = private_vt;
|
|
899 |
symbol->var_init_decl_list->accept(*this);
|
|
900 |
current_vartype = none_vt;
|
|
901 |
}
|
|
902 |
return NULL;
|
|
903 |
}
|
|
904 |
|
|
905 |
/* VAR RETAIN var_init_decl_list END_VAR */
|
|
906 |
void *visit(retentive_var_declarations_c *symbol) {
|
|
907 |
TRACE("retentive_var_declarations_c");
|
|
908 |
if ((wanted_vartype & private_vt) != 0) {
|
|
909 |
current_vartype = private_vt;
|
|
910 |
symbol->var_init_decl_list->accept(*this);
|
|
911 |
current_vartype = none_vt;
|
|
912 |
}
|
|
913 |
return NULL;
|
|
914 |
}
|
|
915 |
|
|
916 |
/* VAR [CONSTANT|RETAIN|NON_RETAIN] located_var_decl_list END_VAR */
|
|
917 |
/* option -> may be NULL ! */
|
|
918 |
//SYM_REF2(located_var_declarations_c, option, located_var_decl_list)
|
|
919 |
void *visit(located_var_declarations_c *symbol) {
|
|
920 |
TRACE("located_var_declarations_c");
|
|
921 |
if ((wanted_vartype & located_vt) != 0) {
|
|
922 |
/*
|
|
923 |
// TO DO ...
|
|
924 |
if (symbol->option != NULL)
|
|
925 |
symbol->option->accept(*this);
|
|
926 |
*/
|
|
927 |
current_vartype = located_vt;
|
|
928 |
symbol->located_var_decl_list->accept(*this);
|
|
929 |
current_vartype = none_vt;
|
|
930 |
}
|
|
931 |
return NULL;
|
|
932 |
}
|
|
933 |
|
|
934 |
/* helper symbol for located_var_declarations */
|
|
935 |
/* located_var_decl_list located_var_decl ';' */
|
|
936 |
//SYM_LIST(located_var_decl_list_c)
|
|
937 |
void *visit(located_var_decl_list_c *symbol) {
|
|
938 |
TRACE("located_var_decl_list_c");
|
|
939 |
print_list(symbol);
|
|
940 |
return NULL;
|
|
941 |
}
|
|
942 |
|
|
943 |
|
|
944 |
/* [variable_name] location ':' located_var_spec_init */
|
|
945 |
/* variable_name -> may be NULL ! */
|
|
946 |
//SYM_REF4(located_var_decl_c, variable_name, location, located_var_spec_init, unused)
|
|
947 |
void *visit(located_var_decl_c *symbol) {
|
|
948 |
TRACE("located_var_decl_c");
|
|
949 |
/* Please read the comments inside the var1_init_decl_c
|
|
950 |
* visitor, as they apply here too.
|
|
951 |
*/
|
|
952 |
|
|
953 |
/* Start off by setting the current_var_type_symbol and
|
|
954 |
* current_var_init_symbol private variables...
|
|
955 |
*/
|
|
956 |
update_type_init(symbol->located_var_spec_init);
|
|
957 |
|
|
958 |
/* now to produce the c equivalent... */
|
|
959 |
switch(wanted_varformat) {
|
|
960 |
case local_vf:
|
|
961 |
s4o.print(s4o.indent_spaces);
|
|
962 |
this->current_var_type_symbol->accept(*this);
|
|
963 |
s4o.print(" *");
|
|
964 |
if (symbol->variable_name != NULL)
|
|
965 |
symbol->variable_name->accept(*this);
|
|
966 |
else
|
|
967 |
symbol->location->accept(*this);
|
|
968 |
s4o.print(";\n");
|
|
969 |
break;
|
|
970 |
|
|
971 |
case constructorinit_vf:
|
|
972 |
s4o.print(nv->get());
|
|
973 |
s4o.print("{extern ");
|
|
974 |
this->current_var_type_symbol->accept(*this);
|
122
|
975 |
s4o.print("* ");
|
70
|
976 |
symbol->location->accept(*this);
|
|
977 |
s4o.print("; ");
|
|
978 |
print_variable_prefix();
|
|
979 |
if (symbol->variable_name != NULL)
|
|
980 |
symbol->variable_name->accept(*this);
|
|
981 |
else
|
|
982 |
symbol->location->accept(*this);
|
122
|
983 |
s4o.print(" = ");
|
70
|
984 |
symbol->location->accept(*this);
|
|
985 |
s4o.print(";}");
|
|
986 |
break;
|
|
987 |
|
|
988 |
case globalinit_vf:
|
|
989 |
s4o.print(s4o.indent_spaces + "__plc_pt_c<");
|
|
990 |
this->current_var_type_symbol->accept(*this);
|
|
991 |
s4o.print(", 8*sizeof(");
|
|
992 |
this->current_var_type_symbol->accept(*this);
|
|
993 |
s4o.print(")> ");
|
|
994 |
if (this->globalnamespace != NULL) {
|
|
995 |
this->globalnamespace->accept(*this);
|
|
996 |
s4o.print("::");
|
|
997 |
}
|
|
998 |
if (symbol->variable_name != NULL)
|
|
999 |
symbol->variable_name->accept(*this);
|
|
1000 |
else
|
|
1001 |
symbol->location->accept(*this);
|
|
1002 |
|
|
1003 |
s4o.print(" = ");
|
|
1004 |
|
|
1005 |
s4o.print(s4o.indent_spaces + "__plc_pt_c<");
|
|
1006 |
this->current_var_type_symbol->accept(*this);
|
|
1007 |
s4o.print(", 8*sizeof(");
|
|
1008 |
this->current_var_type_symbol->accept(*this);
|
|
1009 |
s4o.print(")>(\"");
|
|
1010 |
symbol->location->accept(*this);
|
|
1011 |
s4o.print("\"");
|
|
1012 |
if (this->current_var_init_symbol != NULL) {
|
|
1013 |
s4o.print(", ");
|
|
1014 |
this->current_var_init_symbol->accept(*this);
|
|
1015 |
}
|
|
1016 |
s4o.print(");\n");
|
|
1017 |
break;
|
|
1018 |
|
|
1019 |
default:
|
|
1020 |
ERROR;
|
|
1021 |
} /* switch() */
|
|
1022 |
|
|
1023 |
/* Values no longer in scope, and therefore no longer used.
|
|
1024 |
* Make an effort to keep them set to NULL when not in use
|
|
1025 |
* in order to catch bugs as soon as possible...
|
|
1026 |
*/
|
|
1027 |
void_type_init();
|
|
1028 |
|
|
1029 |
return NULL;
|
|
1030 |
}
|
|
1031 |
|
|
1032 |
|
|
1033 |
|
|
1034 |
|
|
1035 |
/*| VAR_EXTERNAL [CONSTANT] external_declaration_list END_VAR */
|
|
1036 |
/* option -> may be NULL ! */
|
|
1037 |
//SYM_REF2(external_var_declarations_c, option, external_declaration_list)
|
|
1038 |
void *visit(external_var_declarations_c *symbol) {
|
|
1039 |
TRACE("external_var_declarations_c");
|
|
1040 |
if ((wanted_vartype & external_vt) != 0) {
|
|
1041 |
/*
|
|
1042 |
// TODO ...
|
|
1043 |
if (symbol->option != NULL)
|
|
1044 |
symbol->option->accept(*this);
|
|
1045 |
*/
|
|
1046 |
//s4o.indent_right();
|
|
1047 |
current_vartype = external_vt;
|
|
1048 |
symbol->external_declaration_list->accept(*this);
|
|
1049 |
current_vartype = none_vt;
|
|
1050 |
//s4o.indent_left();
|
|
1051 |
}
|
|
1052 |
return NULL;
|
|
1053 |
}
|
|
1054 |
|
|
1055 |
/* helper symbol for external_var_declarations */
|
|
1056 |
/*| external_declaration_list external_declaration';' */
|
|
1057 |
//SYM_LIST(external_declaration_list_c)
|
|
1058 |
/* helper symbol for input_declarations */
|
|
1059 |
void *visit(external_declaration_list_c *symbol) {
|
|
1060 |
TRACE("external_declaration_list_c");
|
|
1061 |
print_list(symbol);
|
|
1062 |
return NULL;
|
|
1063 |
}
|
|
1064 |
|
|
1065 |
|
|
1066 |
/* global_var_name ':' (simple_specification|subrange_specification|enumerated_specification|array_specification|prev_declared_structure_type_name|function_block_type_name */
|
|
1067 |
//SYM_REF2(external_declaration_c, global_var_name, specification)
|
|
1068 |
void *visit(external_declaration_c *symbol) {
|
|
1069 |
TRACE("external_declaration_c");
|
|
1070 |
/* Please read the comments inside the var1_init_decl_c
|
|
1071 |
* visitor, as they apply here too.
|
|
1072 |
*/
|
|
1073 |
|
|
1074 |
/* Start off by setting the current_var_type_symbol and
|
|
1075 |
* current_var_init_symbol private variables...
|
|
1076 |
*/
|
|
1077 |
this->current_var_type_symbol = symbol->specification;
|
|
1078 |
this->current_var_init_symbol = NULL;
|
|
1079 |
|
|
1080 |
/* now to produce the c equivalent... */
|
|
1081 |
switch (wanted_varformat) {
|
|
1082 |
case local_vf:
|
|
1083 |
case localinit_vf:
|
|
1084 |
s4o.print(s4o.indent_spaces);
|
|
1085 |
this->current_var_type_symbol->accept(*this);
|
|
1086 |
s4o.print(" *");
|
|
1087 |
symbol->global_var_name->accept(*this);
|
|
1088 |
if ((wanted_varformat == localinit_vf) &&
|
|
1089 |
(this->current_var_init_symbol != NULL)) {
|
|
1090 |
s4o.print(" = ");
|
|
1091 |
this->current_var_init_symbol->accept(*this);
|
|
1092 |
}
|
|
1093 |
s4o.print(";\n");
|
|
1094 |
break;
|
|
1095 |
|
|
1096 |
case constructorinit_vf:
|
|
1097 |
s4o.print(nv->get());
|
|
1098 |
s4o.print("{extern ");
|
|
1099 |
this->current_var_type_symbol->accept(*this);
|
|
1100 |
s4o.print(" *");
|
|
1101 |
symbol->global_var_name->accept(*this);
|
|
1102 |
s4o.print("; ");
|
|
1103 |
print_variable_prefix();
|
|
1104 |
symbol->global_var_name->accept(*this);
|
|
1105 |
s4o.print(" = ");
|
|
1106 |
symbol->global_var_name->accept(*this);
|
|
1107 |
s4o.print(";}");
|
|
1108 |
break;
|
|
1109 |
|
|
1110 |
case finterface_vf:
|
|
1111 |
finterface_var_count++;
|
|
1112 |
s4o.print(nv->get());
|
|
1113 |
this->current_var_type_symbol->accept(*this);
|
|
1114 |
s4o.print(" *");
|
|
1115 |
symbol->global_var_name->accept(*this);
|
|
1116 |
break;
|
|
1117 |
|
|
1118 |
default:
|
|
1119 |
ERROR;
|
|
1120 |
}
|
|
1121 |
|
|
1122 |
/* Values no longer in scope, and therefore no longer used.
|
|
1123 |
* Make an effort to keep them set to NULL when not in use
|
|
1124 |
* in order to catch bugs as soon as possible...
|
|
1125 |
*/
|
|
1126 |
void_type_init();
|
|
1127 |
|
|
1128 |
return NULL;
|
|
1129 |
}
|
|
1130 |
|
|
1131 |
|
|
1132 |
|
|
1133 |
/*| VAR_GLOBAL [CONSTANT|RETAIN] global_var_decl_list END_VAR */
|
|
1134 |
/* option -> may be NULL ! */
|
|
1135 |
// SYM_REF2(global_var_declarations_c, option, global_var_decl_list)
|
|
1136 |
void *visit(global_var_declarations_c *symbol) {
|
|
1137 |
TRACE("global_var_declarations_c");
|
|
1138 |
if ((wanted_vartype & global_vt) != 0) {
|
|
1139 |
/*
|
|
1140 |
// TODO ...
|
|
1141 |
if (symbol->option != NULL)
|
|
1142 |
symbol->option->accept(*this);
|
|
1143 |
*/
|
|
1144 |
//s4o.indent_right();
|
|
1145 |
unsigned int previous_vartype = current_vartype;
|
|
1146 |
// previous_vartype will be either none_vt, or resource_vt
|
|
1147 |
current_vartype = global_vt;
|
|
1148 |
symbol->global_var_decl_list->accept(*this);
|
|
1149 |
current_vartype = previous_vartype;
|
|
1150 |
//s4o.indent_left();
|
|
1151 |
}
|
|
1152 |
return NULL;
|
|
1153 |
}
|
|
1154 |
|
|
1155 |
/* helper symbol for global_var_declarations */
|
|
1156 |
/*| global_var_decl_list global_var_decl ';' */
|
|
1157 |
//SYM_LIST(global_var_decl_list_c)
|
|
1158 |
void *visit(global_var_decl_list_c *symbol) {
|
|
1159 |
TRACE("global_var_decl_list_c");
|
|
1160 |
print_list(symbol);
|
|
1161 |
return NULL;
|
|
1162 |
}
|
|
1163 |
|
|
1164 |
|
|
1165 |
|
|
1166 |
/*| global_var_spec ':' [located_var_spec_init|function_block_type_name] */
|
|
1167 |
/* type_specification ->may be NULL ! */
|
|
1168 |
// SYM_REF2(global_var_decl_c, global_var_spec, type_specification)
|
|
1169 |
void *visit(global_var_decl_c *symbol) {
|
|
1170 |
TRACE("global_var_decl_c");
|
|
1171 |
/* Please read the comments inside the var1_init_decl_c
|
|
1172 |
* visitor, as they apply here too.
|
|
1173 |
*/
|
|
1174 |
|
|
1175 |
/* Start off by setting the current_var_type_symbol and
|
|
1176 |
* current_var_init_symbol private variables...
|
|
1177 |
*/
|
|
1178 |
update_type_init(symbol->type_specification);
|
|
1179 |
|
|
1180 |
/* now to produce the c equivalent... */
|
|
1181 |
symbol->global_var_spec->accept(*this);
|
|
1182 |
|
|
1183 |
/* Values no longer in scope, and therefore no longer used.
|
|
1184 |
* Make an effort to keep them set to NULL when not in use
|
|
1185 |
* in order to catch bugs as soon as possible...
|
|
1186 |
*/
|
|
1187 |
void_type_init();
|
|
1188 |
|
|
1189 |
return NULL;
|
|
1190 |
}
|
|
1191 |
|
|
1192 |
|
|
1193 |
/*| global_var_name location */
|
|
1194 |
// SYM_REF2(global_var_spec_c, global_var_name, location)
|
|
1195 |
void *visit(global_var_spec_c *symbol) {
|
|
1196 |
TRACE("global_var_spec_c");
|
|
1197 |
|
|
1198 |
/* now to produce the c equivalent... */
|
|
1199 |
switch(wanted_varformat) {
|
|
1200 |
case local_vf:
|
|
1201 |
case localstatic_vf:
|
|
1202 |
/* NOTE: located variables must be declared static, as the connection to the
|
|
1203 |
* MatPLC point must be initialised at program startup, and not whenever
|
|
1204 |
* a new function block is instantiated!
|
|
1205 |
* Nevertheless, this construct never occurs inside a Function Block, but
|
|
1206 |
* only inside a configuration. In this case, only a single instance will
|
|
1207 |
* be created, directly at startup, so it is not necessary that the variables
|
|
1208 |
* be declared static.
|
|
1209 |
*/
|
|
1210 |
s4o.print(s4o.indent_spaces);
|
|
1211 |
if (symbol->global_var_name != NULL) {
|
|
1212 |
s4o.print("extern ");
|
|
1213 |
this->current_var_type_symbol->accept(*this);
|
122
|
1214 |
s4o.print("* ");
|
70
|
1215 |
symbol->location->accept(*this);
|
|
1216 |
s4o.print(";\n");
|
|
1217 |
if (wanted_varformat == localstatic_vf)
|
|
1218 |
s4o.print("static ");
|
|
1219 |
this->current_var_type_symbol->accept(*this);
|
|
1220 |
s4o.print(" *");
|
|
1221 |
symbol->global_var_name->accept(*this);
|
122
|
1222 |
s4o.print(" = ");
|
70
|
1223 |
symbol->location->accept(*this);
|
|
1224 |
s4o.print(";\n");
|
|
1225 |
}
|
|
1226 |
break;
|
|
1227 |
|
|
1228 |
case constructorinit_vf:
|
|
1229 |
s4o.print(nv->get());
|
|
1230 |
|
|
1231 |
if (symbol->global_var_name != NULL) {
|
|
1232 |
s4o.print("*");
|
|
1233 |
symbol->global_var_name->accept(*this);
|
|
1234 |
}
|
|
1235 |
else
|
|
1236 |
symbol->location->accept(*this);
|
|
1237 |
s4o.print(" = ");
|
|
1238 |
if (this->current_var_init_symbol != NULL) {
|
|
1239 |
this->current_var_init_symbol->accept(*this);
|
|
1240 |
}
|
|
1241 |
s4o.print(";");
|
|
1242 |
break;
|
|
1243 |
|
|
1244 |
default:
|
|
1245 |
ERROR;
|
|
1246 |
} /* switch() */
|
|
1247 |
|
|
1248 |
return NULL;
|
|
1249 |
}
|
|
1250 |
|
|
1251 |
|
|
1252 |
|
|
1253 |
/* AT direct_variable */
|
|
1254 |
// SYM_REF2(location_c, direct_variable, unused)
|
|
1255 |
void *visit(location_c *symbol) {
|
|
1256 |
TRACE("location_c");
|
|
1257 |
return symbol->direct_variable->accept(*this);
|
|
1258 |
}
|
|
1259 |
|
|
1260 |
|
|
1261 |
/*| global_var_list ',' global_var_name */
|
|
1262 |
//SYM_LIST(global_var_list_c)
|
|
1263 |
void *visit(global_var_list_c *symbol) {
|
|
1264 |
TRACE("global_var_list_c");
|
|
1265 |
list_c *list = dynamic_cast<list_c *>(symbol);
|
|
1266 |
/* should NEVER EVER occur!! */
|
|
1267 |
if (list == NULL) ERROR;
|
|
1268 |
|
|
1269 |
/* now to produce the c equivalent... */
|
|
1270 |
switch (wanted_varformat) {
|
|
1271 |
case local_vf:
|
|
1272 |
case localinit_vf:
|
|
1273 |
for(int i = 0; i < list->n; i++) {
|
|
1274 |
s4o.print(s4o.indent_spaces);
|
|
1275 |
this->current_var_type_symbol->accept(*this);
|
|
1276 |
s4o.print(" __");
|
|
1277 |
list->elements[i]->accept(*this);
|
|
1278 |
s4o.print(";\n");
|
|
1279 |
this->current_var_type_symbol->accept(*this);
|
|
1280 |
s4o.print(" *");
|
|
1281 |
list->elements[i]->accept(*this);
|
|
1282 |
s4o.print(" = &__");
|
|
1283 |
list->elements[i]->accept(*this);
|
|
1284 |
#if 0
|
|
1285 |
if (wanted_varformat == localinit_vf) {
|
|
1286 |
if (this->current_var_init_symbol != NULL) {
|
|
1287 |
s4o.print(" = ");
|
|
1288 |
this->current_var_init_symbol->accept(*this);
|
|
1289 |
}
|
|
1290 |
}
|
|
1291 |
#endif
|
|
1292 |
s4o.print(";\n");
|
|
1293 |
}
|
|
1294 |
break;
|
|
1295 |
|
|
1296 |
case constructorinit_vf:
|
|
1297 |
if (this->current_var_init_symbol != NULL) {
|
|
1298 |
for(int i = 0; i < list->n; i++) {
|
|
1299 |
s4o.print(nv->get());
|
|
1300 |
|
|
1301 |
s4o.print("*");
|
|
1302 |
list->elements[i]->accept(*this);
|
|
1303 |
s4o.print(" = ");
|
|
1304 |
this->current_var_init_symbol->accept(*this);
|
|
1305 |
s4o.print(";");
|
|
1306 |
#if 0
|
|
1307 |
/* The following code would be for globalinit_vf !!
|
|
1308 |
* But it is not currently required...
|
|
1309 |
*/
|
|
1310 |
s4o.print(s4o.indent_spaces + "__ext_element_c<");
|
|
1311 |
this->current_var_type_symbol->accept(*this);
|
|
1312 |
s4o.print("> ");
|
|
1313 |
if (this->globalnamespace != NULL) {
|
|
1314 |
this->globalnamespace->accept(*this);
|
|
1315 |
s4o.print("::");
|
|
1316 |
}
|
|
1317 |
list->elements[i]->accept(*this);
|
|
1318 |
|
|
1319 |
if (this->current_var_init_symbol != NULL) {
|
|
1320 |
s4o.print(" = ");
|
|
1321 |
s4o.print("__ext_element_c<");
|
|
1322 |
this->current_var_type_symbol->accept(*this);
|
|
1323 |
s4o.print(">(");
|
|
1324 |
this->current_var_init_symbol->accept(*this);
|
|
1325 |
s4o.print(")");
|
|
1326 |
}
|
|
1327 |
s4o.print(";\n");
|
|
1328 |
#endif
|
|
1329 |
}
|
|
1330 |
}
|
|
1331 |
break;
|
|
1332 |
|
|
1333 |
default:
|
|
1334 |
ERROR; /* not supported, and not needed either... */
|
|
1335 |
}
|
|
1336 |
|
|
1337 |
return NULL;
|
|
1338 |
}
|
|
1339 |
|
|
1340 |
|
|
1341 |
#if 0
|
|
1342 |
/* var1_list ':' single_byte_string_spec */
|
|
1343 |
SYM_REF2(single_byte_string_var_declaration_c, var1_list, single_byte_string_spec)
|
|
1344 |
|
|
1345 |
/* STRING ['[' integer ']'] [ASSIGN single_byte_character_string] */
|
|
1346 |
/* integer ->may be NULL ! */
|
|
1347 |
/* single_byte_character_string ->may be NULL ! */
|
|
1348 |
SYM_REF2(single_byte_string_spec_c, integer, single_byte_character_string)
|
|
1349 |
|
|
1350 |
/* var1_list ':' double_byte_string_spec */
|
|
1351 |
SYM_REF2(double_byte_string_var_declaration_c, var1_list, double_byte_string_spec)
|
|
1352 |
|
|
1353 |
/* WSTRING ['[' integer ']'] [ASSIGN double_byte_character_string] */
|
|
1354 |
/* integer ->may be NULL ! */
|
|
1355 |
/* double_byte_character_string ->may be NULL ! */
|
|
1356 |
SYM_REF2(double_byte_string_spec_c, integer, double_byte_character_string)
|
|
1357 |
|
|
1358 |
/*| VAR [RETAIN|NON_RETAIN] incompl_located_var_decl_list END_VAR */
|
|
1359 |
/* option ->may be NULL ! */
|
|
1360 |
SYM_REF2(incompl_located_var_declarations_c, option, incompl_located_var_decl_list)
|
|
1361 |
|
|
1362 |
/* helper symbol for incompl_located_var_declarations */
|
|
1363 |
/*| incompl_located_var_decl_list incompl_located_var_decl ';' */
|
|
1364 |
SYM_LIST(incompl_located_var_decl_list_c)
|
|
1365 |
|
|
1366 |
/* variable_name incompl_location ':' var_spec */
|
|
1367 |
SYM_REF4(incompl_located_var_decl_c, variable_name, incompl_location, var_spec, unused)
|
|
1368 |
|
|
1369 |
/* AT incompl_location_token */
|
|
1370 |
SYM_TOKEN(incompl_location_c)
|
|
1371 |
#endif
|
|
1372 |
|
|
1373 |
|
|
1374 |
void *visit(falling_edge_option_c *symbol) {
|
|
1375 |
// TO DO ...
|
|
1376 |
s4o.print("F_EDGE");
|
|
1377 |
return NULL;
|
|
1378 |
}
|
|
1379 |
|
|
1380 |
|
|
1381 |
void *visit(var1_init_decl_c *symbol) {
|
|
1382 |
TRACE("var1_init_decl_c");
|
|
1383 |
/* We have several implementation alternatives here...
|
|
1384 |
*
|
|
1385 |
* The issue is that generation of c code
|
|
1386 |
* based on the abstract syntax tree requires the reversal
|
|
1387 |
* of the symbol order compared to st. For e.g.:
|
|
1388 |
* (ST) a, b, c: INT := 98;
|
|
1389 |
* (C ) int a=98, b=98, c=98;
|
|
1390 |
* The spec_init contains the references to 'INT' and '98'.
|
|
1391 |
* The var1_list contains the references to 'a', 'b', and 'c'.
|
|
1392 |
* The var1_init_decl_c contains the references to spec_init and var1_list.
|
|
1393 |
*
|
|
1394 |
* For c code generation, the var1_init_decl_c visitor
|
|
1395 |
* would need to access the internals of other classes
|
|
1396 |
* (e.g. the simple_spec_init_c).
|
|
1397 |
*
|
|
1398 |
* We can do this using one of three methods:
|
|
1399 |
* 1) Create the abstract syntax tree differently;
|
|
1400 |
* 2) Access other classes from within the var1_init_decl_c;
|
|
1401 |
* 3) Pass info between the visitors using global variables
|
|
1402 |
* only acessible by this class (private vars)
|
|
1403 |
*
|
|
1404 |
* In 1), the abstract syntax tree would be built so that
|
|
1405 |
* var1_init_decl_c would contain direct references to
|
|
1406 |
* var1_list_c, to the var type 'INT' and to the initialiser '98'
|
|
1407 |
* (as per the example above).
|
|
1408 |
*
|
|
1409 |
* 2) would have several sub-options to obtain the references
|
|
1410 |
* to 'INT' and '98' from within var1_init_decl_c.
|
|
1411 |
* In 2a), the var1_init_decl_c would use dynamic casts to determine
|
|
1412 |
* the class of spec_init (simple_spec_init_c, subrange_spec_init_c or
|
|
1413 |
* enumerated_spec_init_c), and from there obtain the 'INT' and '98'
|
|
1414 |
* In 2b) var1_init_decl_c would have one reference for each
|
|
1415 |
* simple_spec_init_c, subrange_spec_init_c and enumerated_spec_init_c,
|
|
1416 |
* the apropriate one being initialised by the var1_init_decl_c constructor.
|
|
1417 |
* Note that the constructor would use dynamic casts. In essence, we
|
|
1418 |
* would merely be changing the location of the code with the
|
|
1419 |
* dynamic casts.
|
|
1420 |
* In 2c) we would use three overloaded constructors for var1_init_decl_c
|
|
1421 |
* one each for simple_spec_init_c, etc... This implies
|
|
1422 |
* use type specific pointers to each symbol in the bison
|
|
1423 |
* parser, instead of simply using a symbol_c * for all symbols;
|
|
1424 |
*
|
|
1425 |
* In 3), we use two global but private variables with references to
|
|
1426 |
* 'INT' and '98', that would be initiliased by the visitors to
|
|
1427 |
* simple_spec_init_c, subrange_spec_init_c and enumerated_spec_init_c.
|
|
1428 |
* The visitor to var1_list_c would then use the references in the global
|
|
1429 |
* variables.
|
|
1430 |
*
|
|
1431 |
* I (Mario) have chosen to use 3).
|
|
1432 |
*/
|
|
1433 |
|
|
1434 |
/* Start off by setting the current_var_type_symbol and
|
|
1435 |
* current_var_init_symbol private variables...
|
|
1436 |
*/
|
|
1437 |
update_type_init(symbol->spec_init);
|
|
1438 |
|
|
1439 |
/* now to produce the c equivalent... */
|
|
1440 |
symbol->var1_list->accept(*this);
|
|
1441 |
|
|
1442 |
/* Values no longer in scope, and therefore no longer used.
|
|
1443 |
* Make an effort to keep them set to NULL when not in use
|
|
1444 |
* in order to catch bugs as soon as possible...
|
|
1445 |
*/
|
|
1446 |
void_type_init();
|
|
1447 |
|
|
1448 |
return NULL;
|
|
1449 |
}
|
|
1450 |
|
|
1451 |
|
|
1452 |
|
|
1453 |
void *visit(var1_list_c *symbol) {
|
|
1454 |
TRACE("var1_list_c");
|
|
1455 |
declare_variables(symbol);
|
|
1456 |
return NULL;
|
|
1457 |
}
|
|
1458 |
|
|
1459 |
|
|
1460 |
|
|
1461 |
|
|
1462 |
/* intermediate helper symbol for:
|
|
1463 |
* - non_retentive_var_decls
|
|
1464 |
* - output_declarations
|
|
1465 |
*/
|
|
1466 |
void *visit(var_init_decl_list_c *symbol) {
|
|
1467 |
TRACE("var_init_decl_list_c");
|
|
1468 |
return print_list(symbol);
|
|
1469 |
return NULL;
|
|
1470 |
}
|
|
1471 |
|
|
1472 |
|
|
1473 |
/**************************************/
|
|
1474 |
/* B.1.5 - Program organization units */
|
|
1475 |
/**************************************/
|
|
1476 |
/***********************/
|
|
1477 |
/* B 1.5.1 - Functions */
|
|
1478 |
/***********************/
|
|
1479 |
/* The missing function_declaration_c
|
|
1480 |
* is handled in derived classes
|
|
1481 |
*/
|
|
1482 |
|
|
1483 |
|
|
1484 |
|
|
1485 |
/* intermediate helper symbol for function_declaration */
|
|
1486 |
void *visit(var_declarations_list_c *symbol) {
|
|
1487 |
TRACE("var_declarations_list_c");
|
|
1488 |
return print_list(symbol);
|
|
1489 |
}
|
|
1490 |
|
|
1491 |
|
|
1492 |
void *visit(function_var_decls_c *symbol) {
|
|
1493 |
TRACE("function_var_decls_c");
|
|
1494 |
|
|
1495 |
if ((wanted_vartype & private_vt) != 0) {
|
|
1496 |
/*
|
|
1497 |
// TO DO ...
|
|
1498 |
if (symbol->option != NULL)
|
|
1499 |
symbol->option->accept(*this);
|
|
1500 |
*/
|
|
1501 |
current_vartype = private_vt;
|
|
1502 |
symbol->decl_list->accept(*this);
|
|
1503 |
current_vartype = none_vt;
|
|
1504 |
}
|
|
1505 |
return NULL;
|
|
1506 |
}
|
|
1507 |
|
|
1508 |
|
|
1509 |
/* intermediate helper symbol for function_var_decls */
|
|
1510 |
void *visit(var2_init_decl_list_c *symbol) {
|
|
1511 |
TRACE("var2_init_decl_list_c");
|
|
1512 |
print_list(symbol);
|
|
1513 |
return NULL;
|
|
1514 |
}
|
|
1515 |
|
|
1516 |
|
|
1517 |
/*****************************/
|
|
1518 |
/* B 1.5.2 - Function Blocks */
|
|
1519 |
/*****************************/
|
|
1520 |
|
|
1521 |
/* The missing function_block_declaration_c
|
|
1522 |
* is handled in derived classes
|
|
1523 |
*/
|
|
1524 |
|
|
1525 |
|
|
1526 |
/* VAR_TEMP temp_var_decl_list END_VAR */
|
|
1527 |
void *visit(temp_var_decls_c *symbol) {
|
|
1528 |
TRACE("temp_var_decls_c");
|
|
1529 |
if ((wanted_vartype & temp_vt) != 0) {
|
|
1530 |
current_vartype = temp_vt;
|
|
1531 |
symbol->var_decl_list->accept(*this);
|
|
1532 |
current_vartype = none_vt;
|
|
1533 |
}
|
|
1534 |
return NULL;
|
|
1535 |
}
|
|
1536 |
|
|
1537 |
/* intermediate helper symbol for temp_var_decls */
|
|
1538 |
void *visit(temp_var_decls_list_c *symbol) {
|
|
1539 |
TRACE("temp_var_decls_list_c");
|
|
1540 |
return print_list(symbol);
|
|
1541 |
}
|
|
1542 |
|
|
1543 |
/* VAR NON_RETAIN var_init_decl_list END_VAR */
|
|
1544 |
void *visit(non_retentive_var_decls_c *symbol) {
|
|
1545 |
TRACE("non_retentive_var_decls_c");
|
|
1546 |
// TODO ... guarantee the non-retain semantics!
|
|
1547 |
if ((wanted_vartype & private_vt) != 0) {
|
|
1548 |
current_vartype = private_vt;
|
|
1549 |
symbol->var_decl_list->accept(*this);
|
|
1550 |
current_vartype = none_vt;
|
|
1551 |
}
|
|
1552 |
return NULL;
|
|
1553 |
}
|
|
1554 |
|
|
1555 |
|
|
1556 |
|
|
1557 |
/**********************/
|
|
1558 |
/* B 1.5.3 - Programs */
|
|
1559 |
/**********************/
|
|
1560 |
/* leave for derived classes... */
|
|
1561 |
|
|
1562 |
/*********************************************/
|
|
1563 |
/* B.1.6 Sequential function chart elements */
|
|
1564 |
/*********************************************/
|
|
1565 |
|
|
1566 |
/********************************/
|
|
1567 |
/* B 1.7 Configuration elements */
|
|
1568 |
/********************************/
|
|
1569 |
/* Programs instantiated inside configurations are declared as variables!! */
|
|
1570 |
|
|
1571 |
/*
|
|
1572 |
CONFIGURATION configuration_name
|
|
1573 |
optional_global_var_declarations
|
|
1574 |
(resource_declaration_list | single_resource_declaration)
|
|
1575 |
optional_access_declarations
|
|
1576 |
optional_instance_specific_initializations
|
|
1577 |
END_CONFIGURATION
|
|
1578 |
*/
|
|
1579 |
/*
|
|
1580 |
SYM_REF6(configuration_declaration_c, configuration_name, global_var_declarations, resource_declarations, access_declarations, instance_specific_initializations, unused)
|
|
1581 |
*/
|
|
1582 |
void *visit(configuration_declaration_c *symbol) {
|
|
1583 |
TRACE("configuration_declaration_c");
|
|
1584 |
|
|
1585 |
if(symbol->global_var_declarations)
|
|
1586 |
symbol->global_var_declarations->accept(*this); // will contain VAR_GLOBAL declarations!!
|
|
1587 |
symbol->resource_declarations->accept(*this); // will contain PROGRAM declarations!!
|
|
1588 |
return NULL;
|
|
1589 |
}
|
|
1590 |
|
|
1591 |
|
|
1592 |
/* helper symbol for configuration_declaration */
|
|
1593 |
// SYM_LIST(resource_declaration_list_c)
|
|
1594 |
void *visit(resource_declaration_list_c *symbol) {
|
|
1595 |
TRACE("resource_declaration_list_c");
|
|
1596 |
|
|
1597 |
return print_list(symbol);
|
|
1598 |
}
|
|
1599 |
|
|
1600 |
/*
|
|
1601 |
RESOURCE resource_name ON resource_type_name
|
|
1602 |
optional_global_var_declarations
|
|
1603 |
single_resource_declaration
|
|
1604 |
END_RESOURCE
|
|
1605 |
*/
|
|
1606 |
// SYM_REF4(resource_declaration_c, resource_name, resource_type_name, global_var_declarations, resource_declaration)
|
|
1607 |
void *visit(resource_declaration_c *symbol) {
|
|
1608 |
TRACE("resource_declaration_c");
|
|
1609 |
|
|
1610 |
if ((wanted_vartype & resource_vt) != 0) {
|
|
1611 |
s4o.print(s4o.indent_spaces + "struct {\n");
|
|
1612 |
s4o.indent_right();
|
|
1613 |
|
|
1614 |
current_vartype = resource_vt;
|
|
1615 |
if (NULL != symbol->global_var_declarations)
|
|
1616 |
symbol->global_var_declarations->accept(*this); // will contain VAR_GLOBAL declarations!!
|
|
1617 |
if (NULL != symbol->resource_declaration)
|
|
1618 |
symbol->resource_declaration->accept(*this); // will contain PROGRAM declarations!!
|
|
1619 |
current_vartype = none_vt;
|
|
1620 |
|
|
1621 |
s4o.indent_left();
|
|
1622 |
s4o.print(s4o.indent_spaces + "} ");
|
|
1623 |
symbol->resource_name->accept(*this);
|
|
1624 |
s4o.print(";\n");
|
|
1625 |
}
|
|
1626 |
return NULL;
|
|
1627 |
}
|
|
1628 |
|
|
1629 |
/* task_configuration_list program_configuration_list */
|
|
1630 |
// SYM_REF2(single_resource_declaration_c, task_configuration_list, program_configuration_list)
|
|
1631 |
void *visit(single_resource_declaration_c *symbol) {
|
|
1632 |
TRACE("single_resource_declaration_c");
|
|
1633 |
|
|
1634 |
if ((wanted_vartype & program_vt) != 0) {
|
|
1635 |
unsigned int previous_vartype = current_vartype;
|
|
1636 |
// previous_vartype will be resource_vt
|
|
1637 |
current_vartype = program_vt;
|
|
1638 |
symbol->program_configuration_list->accept(*this);
|
|
1639 |
current_vartype = previous_vartype;
|
|
1640 |
}
|
|
1641 |
return NULL;
|
|
1642 |
}
|
|
1643 |
|
|
1644 |
|
|
1645 |
/* helper symbol for single_resource_declaration */
|
|
1646 |
// SYM_LIST(task_configuration_list_c)
|
|
1647 |
|
|
1648 |
|
|
1649 |
/* helper symbol for single_resource_declaration */
|
|
1650 |
/* | program_configuration_list program_configuration ';' */
|
|
1651 |
// SYM_LIST(program_configuration_list_c)
|
|
1652 |
void *visit(program_configuration_list_c *symbol) {
|
|
1653 |
TRACE("program_configuration_list_c");
|
|
1654 |
|
|
1655 |
return print_list(symbol);
|
|
1656 |
}
|
|
1657 |
|
|
1658 |
/* helper symbol for
|
|
1659 |
* - access_path
|
|
1660 |
* - instance_specific_init
|
|
1661 |
*/
|
|
1662 |
// SYM_LIST(any_fb_name_list_c)
|
|
1663 |
|
|
1664 |
/* [resource_name '.'] global_var_name ['.' structure_element_name] */
|
|
1665 |
// SYM_REF4(global_var_reference_c, resource_name, global_var_name, structure_element_name, unused)
|
|
1666 |
|
|
1667 |
/* prev_declared_program_name '.' symbolic_variable */
|
|
1668 |
// SYM_REF2(program_output_reference_c, program_name, symbolic_variable)
|
|
1669 |
|
|
1670 |
/* TASK task_name task_initialization */
|
|
1671 |
// SYM_REF2(task_configuration_c, task_name, task_initialization)
|
|
1672 |
|
|
1673 |
/* '(' [SINGLE ASSIGN data_source ','] [INTERVAL ASSIGN data_source ','] PRIORITY ASSIGN integer ')' */
|
|
1674 |
// SYM_REF4(task_initialization_c, single_data_source, interval_data_source, priority_data_source, unused)
|
|
1675 |
|
|
1676 |
/* PROGRAM [RETAIN | NON_RETAIN] program_name [WITH task_name] ':' program_type_name ['(' prog_conf_elements ')'] */
|
|
1677 |
// SYM_REF6(program_configuration_c, retain_option, program_name, task_name, program_type_name, prog_conf_elements, unused)
|
|
1678 |
private:
|
|
1679 |
/* a helper function to the program_configuration_c visitor... */
|
|
1680 |
void program_constructor_call(program_configuration_c *symbol) {
|
|
1681 |
program_declaration_c *p_decl = program_type_symtable.find_value(symbol->program_type_name);
|
|
1682 |
|
|
1683 |
if (p_decl == program_type_symtable.end_value())
|
|
1684 |
/* should never occur. The program being called MUST be in the symtable... */
|
|
1685 |
ERROR;
|
|
1686 |
|
|
1687 |
symbol->program_name->accept(*this);
|
|
1688 |
s4o.print("(");
|
|
1689 |
|
|
1690 |
/* loop through each function parameter, find the value we should pass
|
|
1691 |
* to it, and then output the c equivalent...
|
|
1692 |
*/
|
|
1693 |
function_param_iterator_c fp_iterator(p_decl);
|
|
1694 |
function_call_param_iterator_c function_call_param_iterator(symbol);
|
|
1695 |
identifier_c *param_name;
|
|
1696 |
nv->push("", ", ");
|
|
1697 |
for(int i = 1; (param_name = fp_iterator.next()) != NULL; i++) {
|
|
1698 |
|
|
1699 |
symbol_c *param_type = fp_iterator.param_type();
|
|
1700 |
if (param_type == NULL) ERROR;
|
|
1701 |
|
|
1702 |
function_param_iterator_c::param_direction_t param_direction = fp_iterator.param_direction();
|
|
1703 |
|
|
1704 |
#if 0
|
|
1705 |
/* Get the value from a foo(<param_name> = <param_value>) style call */
|
|
1706 |
symbol_c *param_value = function_call_param_iterator.search(param_name);
|
|
1707 |
#endif
|
|
1708 |
|
|
1709 |
switch (param_direction) {
|
|
1710 |
case function_param_iterator_c::direction_in:
|
|
1711 |
case function_param_iterator_c::direction_out:
|
|
1712 |
case function_param_iterator_c::direction_inout:
|
|
1713 |
/* ignore them all!! */
|
|
1714 |
break;
|
|
1715 |
|
|
1716 |
case function_param_iterator_c::direction_extref:
|
|
1717 |
#if 0
|
|
1718 |
if (param_value == NULL)
|
|
1719 |
/* This is illegal in ST and IL languages.
|
|
1720 |
* All variables declared in a VAR_EXTERNAL __must__
|
|
1721 |
* be initialised to reference a specific VAR_GLOBAL variable!!
|
|
1722 |
*
|
|
1723 |
* The semantic checker should have caught this, we check again just the
|
|
1724 |
* same (especially since the semantic checker has not yet been written!).
|
|
1725 |
*/
|
|
1726 |
ERROR;
|
|
1727 |
s4o.print(nv->get());
|
|
1728 |
s4o.print("&");
|
|
1729 |
param_value->accept(*this);
|
|
1730 |
#endif
|
|
1731 |
break;
|
|
1732 |
#if 0
|
|
1733 |
if (param_value == NULL) {
|
|
1734 |
/* no parameter value given, so we pass a previously declared temporary variable. */
|
|
1735 |
std::string *temp_var_name = temp_var_name_factory.new_name();
|
|
1736 |
s4o.print(*temp_var_name);
|
|
1737 |
delete temp_var_name;
|
|
1738 |
} else {
|
|
1739 |
param_value->accept(*this);
|
|
1740 |
}
|
|
1741 |
#endif
|
|
1742 |
break;
|
|
1743 |
} /* switch */
|
|
1744 |
} /* for(...) */
|
|
1745 |
|
|
1746 |
// symbol->parameter_assignment->accept(*this);
|
|
1747 |
s4o.print(")");
|
|
1748 |
nv->pop();
|
|
1749 |
return;
|
|
1750 |
}
|
|
1751 |
|
|
1752 |
|
|
1753 |
public:
|
|
1754 |
void *visit(program_configuration_c *symbol) {
|
|
1755 |
TRACE("program_configuration_c");
|
|
1756 |
|
|
1757 |
/* now to produce the c equivalent... */
|
|
1758 |
switch (wanted_varformat) {
|
|
1759 |
case local_vf:
|
|
1760 |
case localinit_vf:
|
|
1761 |
s4o.print(s4o.indent_spaces);
|
|
1762 |
symbol->program_type_name->accept(*this);
|
|
1763 |
s4o.print(" ");
|
|
1764 |
symbol->program_name->accept(*this);
|
|
1765 |
if (wanted_varformat == localinit_vf) {
|
|
1766 |
// TODO...
|
|
1767 |
// program_call(symbol);
|
|
1768 |
}
|
|
1769 |
s4o.print(";\n");
|
|
1770 |
break;
|
|
1771 |
|
|
1772 |
case constructorinit_vf:
|
|
1773 |
s4o.print(nv->get());
|
|
1774 |
program_constructor_call(symbol);
|
|
1775 |
/*
|
|
1776 |
symbol->program_name->accept(*this);
|
|
1777 |
s4o.print("(");
|
|
1778 |
symbol->prog_conf_elements->accept(*this);
|
|
1779 |
nv->pop();
|
|
1780 |
s4o.print(")");
|
|
1781 |
*/
|
|
1782 |
break;
|
|
1783 |
|
|
1784 |
default:
|
|
1785 |
ERROR; /* not supported, and not needed either... */
|
|
1786 |
}
|
|
1787 |
|
|
1788 |
return NULL;
|
|
1789 |
}
|
|
1790 |
|
|
1791 |
|
|
1792 |
|
|
1793 |
/* prog_conf_elements ',' prog_conf_element */
|
|
1794 |
//SYM_LIST(prog_conf_elements_c)
|
|
1795 |
void *visit(prog_conf_elements_c *symbol) {
|
|
1796 |
TRACE("prog_conf_elements_c");
|
|
1797 |
|
|
1798 |
return print_list(symbol);
|
|
1799 |
}
|
|
1800 |
|
|
1801 |
/* fb_name WITH task_name */
|
|
1802 |
//SYM_REF2(fb_task_c, fb_name, task_name)
|
|
1803 |
void *visit(fb_task_c *symbol) {
|
|
1804 |
TRACE("fb_task_c");
|
|
1805 |
|
|
1806 |
/* TODO...
|
|
1807 |
*
|
|
1808 |
* NOTE: Not yet supported...
|
|
1809 |
* We do not support allocating specific function blocks declared
|
|
1810 |
* inside a program to be executed by a different task from the one
|
|
1811 |
* already executing the program itself.
|
|
1812 |
* This is mostly because I (Mario) simply do not understand the
|
|
1813 |
* semantics the standard expects us to implement in this case. It is all
|
|
1814 |
* very confusing, and very poorly defined in the standard!
|
|
1815 |
*/
|
|
1816 |
ERROR;
|
|
1817 |
return NULL;
|
|
1818 |
}
|
|
1819 |
|
|
1820 |
|
|
1821 |
|
|
1822 |
|
|
1823 |
|
|
1824 |
|
|
1825 |
|
|
1826 |
|
|
1827 |
|
|
1828 |
|
|
1829 |
|
|
1830 |
|
|
1831 |
|
|
1832 |
|
|
1833 |
|
|
1834 |
|
|
1835 |
|
|
1836 |
|
|
1837 |
|
|
1838 |
|
|
1839 |
|
|
1840 |
|
|
1841 |
|
|
1842 |
|
|
1843 |
|
|
1844 |
|
|
1845 |
|
|
1846 |
/* any_symbolic_variable ASSIGN prog_data_source */
|
|
1847 |
// SYM_REF2(prog_cnxn_assign_c, symbolic_variable, prog_data_source)
|
|
1848 |
void *visit(prog_cnxn_assign_c *symbol) {
|
|
1849 |
TRACE("prog_cnxn_assign_c");
|
|
1850 |
|
|
1851 |
/* TODO... */
|
|
1852 |
return NULL;
|
|
1853 |
}
|
|
1854 |
|
|
1855 |
/* any_symbolic_variable SENDTO data_sink */
|
|
1856 |
// SYM_REF2(prog_cnxn_sendto_c, symbolic_variable, prog_data_source)
|
|
1857 |
void *visit(prog_cnxn_sendto_c *symbol) {
|
|
1858 |
TRACE("prog_cnxn_sendto_c");
|
|
1859 |
|
|
1860 |
/* TODO... */
|
|
1861 |
return NULL;
|
|
1862 |
}
|
|
1863 |
|
|
1864 |
#if 0
|
|
1865 |
/* VAR_CONFIG instance_specific_init_list END_VAR_BOGUS */
|
|
1866 |
SYM_REF2(instance_specific_initializations_c, instance_specific_init_list, unused)
|
|
1867 |
|
|
1868 |
/* helper symbol for instance_specific_initializations */
|
|
1869 |
SYM_LIST(instance_specific_init_list_c)
|
|
1870 |
|
|
1871 |
/* resource_name '.' program_name '.' {fb_name '.'}
|
|
1872 |
((variable_name [location] ':' located_var_spec_init) | (fb_name ':' fb_initialization))
|
|
1873 |
*/
|
|
1874 |
SYM_REF6(instance_specific_init_c, resource_name, program_name, any_fb_name_list, variable_name, location, initialization)
|
|
1875 |
|
|
1876 |
/* helper symbol for instance_specific_init */
|
|
1877 |
/* function_block_type_name ':=' structure_initialization */
|
|
1878 |
SYM_REF2(fb_initialization_c, function_block_type_name, structure_initialization)
|
|
1879 |
|
|
1880 |
#endif
|
|
1881 |
|
|
1882 |
|
|
1883 |
|
|
1884 |
|
|
1885 |
/****************************************/
|
|
1886 |
/* B.2 - Language IL (Instruction List) */
|
|
1887 |
/****************************************/
|
|
1888 |
/***********************************/
|
|
1889 |
/* B 2.1 Instructions and Operands */
|
|
1890 |
/***********************************/
|
|
1891 |
/* leave for derived classes... */
|
|
1892 |
|
|
1893 |
/*******************/
|
|
1894 |
/* B 2.2 Operators */
|
|
1895 |
/*******************/
|
|
1896 |
/* leave for derived classes... */
|
|
1897 |
|
|
1898 |
|
|
1899 |
/***************************************/
|
|
1900 |
/* B.3 - Language ST (Structured Text) */
|
|
1901 |
/***************************************/
|
|
1902 |
/***********************/
|
|
1903 |
/* B 3.1 - Expressions */
|
|
1904 |
/***********************/
|
|
1905 |
/* leave for derived classes... */
|
|
1906 |
|
|
1907 |
/********************/
|
|
1908 |
/* B 3.2 Statements */
|
|
1909 |
/********************/
|
|
1910 |
/* leave for derived classes... */
|
|
1911 |
|
|
1912 |
/*********************************/
|
|
1913 |
/* B 3.2.1 Assignment Statements */
|
|
1914 |
/*********************************/
|
|
1915 |
/* leave for derived classes... */
|
|
1916 |
|
|
1917 |
/*****************************************/
|
|
1918 |
/* B 3.2.2 Subprogram Control Statements */
|
|
1919 |
/*****************************************/
|
|
1920 |
/* leave for derived classes... */
|
|
1921 |
|
|
1922 |
/********************************/
|
|
1923 |
/* B 3.2.3 Selection Statements */
|
|
1924 |
/********************************/
|
|
1925 |
/* leave for derived classes... */
|
|
1926 |
|
|
1927 |
/********************************/
|
|
1928 |
/* B 3.2.4 Iteration Statements */
|
|
1929 |
/********************************/
|
|
1930 |
/* leave for derived classes... */
|
|
1931 |
|
|
1932 |
|
|
1933 |
|
|
1934 |
}; /* generate_c_vardecl_c */
|
|
1935 |
|
|
1936 |
|
|
1937 |
|