author | lbessard |
Thu, 25 Sep 2008 10:25:30 +0200 | |
changeset 144 | 0ee0055a8ffe |
parent 139 | 668a54686827 |
child 145 | 72ae82e65dbc |
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 |
* This is one of the versions available for the 4th stage. |
|
28 |
* |
|
29 |
* This 4th stage generates a c++ source program equivalent |
|
30 |
* to the IL and ST code. |
|
31 |
*/ |
|
32 |
||
33 |
||
34 |
||
35 |
||
36 |
||
37 |
||
38 |
// #include <stdio.h> /* required for NULL */ |
|
39 |
#include <string> |
|
40 |
#include <iostream> |
|
41 |
#include <sstream> |
|
42 |
#include <typeinfo> |
|
111 | 43 |
#include <list> |
139
668a54686827
added missing includes on some platform (gentoo/gcc-4.3.1)
etisserant
parents:
138
diff
changeset
|
44 |
#include <strings.h> |
70 | 45 |
|
46 |
#include "../../util/symtable.hh" |
|
47 |
#include "../../util/dsymtable.hh" |
|
48 |
#include "../../absyntax/visitor.hh" |
|
49 |
||
50 |
#include "../stage4.hh" |
|
51 |
||
52 |
||
53 |
||
54 |
||
55 |
||
56 |
||
57 |
||
58 |
||
59 |
//#define DEBUG |
|
60 |
#ifdef DEBUG |
|
61 |
#define TRACE(classname) printf("\n____%s____\n",classname); |
|
62 |
#else |
|
63 |
#define TRACE(classname) |
|
64 |
#endif |
|
65 |
||
66 |
||
67 |
||
68 |
#define ERROR error_exit(__FILE__,__LINE__) |
|
69 |
/* function defined in main.cc */ |
|
70 |
extern void error_exit(const char *file_name, int line_no); |
|
71 |
||
72 |
||
73 |
||
74 |
||
75 |
||
76 |
||
77 |
||
78 |
/* A symbol table with all globally declared functions... */ |
|
79 |
function_declaration_c null_symbol1(NULL,NULL,NULL,NULL); |
|
80 |
dsymtable_c<function_declaration_c *, &null_symbol1> function_symtable; |
|
81 |
||
82 |
/* A symbol table with all globally declared functions block types... */ |
|
83 |
function_block_declaration_c null_symbol2(NULL,NULL,NULL); |
|
84 |
symtable_c<function_block_declaration_c *, &null_symbol2> function_block_type_symtable; |
|
85 |
||
86 |
/* A symbol table with all globally declared program types... */ |
|
87 |
program_declaration_c null_symbol3(NULL,NULL,NULL); |
|
88 |
symtable_c<program_declaration_c *, &null_symbol3> program_type_symtable; |
|
89 |
||
90 |
/* A symbol table with all user declared type definitions... */ |
|
91 |
/* Note that function block types and program types have their |
|
92 |
* own symbol tables, so do not get placed in this symbol table! |
|
93 |
*/ |
|
94 |
symbol_c null_symbol4; |
|
95 |
symtable_c<symbol_c *, &null_symbol4> type_symtable; |
|
96 |
||
97 |
||
98 |
||
99 |
/***********************************************************************/ |
|
100 |
/***********************************************************************/ |
|
101 |
/***********************************************************************/ |
|
102 |
/***********************************************************************/ |
|
103 |
||
104 |
||
105 |
/* returns 0 if the names are equal!! */ |
|
106 |
/* NOTE: it must ignore case!! */ |
|
107 |
static int compare_identifiers(symbol_c *ident1, symbol_c *ident2) { |
|
108 |
||
109 |
token_c *name1 = dynamic_cast<token_c *>(ident1); |
|
110 |
token_c *name2 = dynamic_cast<token_c *>(ident2); |
|
111 |
||
112 |
if ((name1 == NULL) || (name2 == NULL)) |
|
113 |
/* invalid identifiers... */ |
|
114 |
return -1; |
|
115 |
||
116 |
if (strcasecmp(name1->value, name2->value) == 0) |
|
117 |
return 0; |
|
118 |
||
119 |
/* identifiers do not match! */ |
|
120 |
return 1; |
|
121 |
} |
|
122 |
||
123 |
||
124 |
/***********************************************************************/ |
|
125 |
/***********************************************************************/ |
|
126 |
/***********************************************************************/ |
|
127 |
/***********************************************************************/ |
|
128 |
||
129 |
/* Unlike Programs and Configurations which get mapped onto C++ classes, |
|
130 |
* Function Blocks are mapped onto a C structure containing the variables, and |
|
131 |
* a C function containing the code in the FB's body. This is to allow direct allocation |
|
132 |
* of a FB variable (which is really an instance of the C data structure) to |
|
133 |
* a member of a union variable (note that classes with constructors cannot |
|
134 |
* be mebers of a union), which is done in IL when loading a FB onto IL's |
|
135 |
* default variable. |
|
136 |
* |
|
137 |
* So as not to clash the names of the C data structure and the C function, |
|
138 |
* the C structure is given a name identical to that of the FB name, whereas |
|
139 |
* the name of the function is the FB name with a constant string appended. |
|
140 |
* The value of that constant string which is appended is defined in the following |
|
141 |
* constant. |
|
142 |
* In order not to clash with any variable in the IL and ST source codem the |
|
143 |
* following constant should contain a double underscore, which is not allowed |
|
144 |
* in IL and ST. |
|
145 |
* |
|
146 |
* e.g.: FUNTION_BLOCK TEST |
|
147 |
* is mapped onto a TEST data structure, and a TEST_body__ function. |
|
148 |
*/ |
|
149 |
||
150 |
#define FB_FUNCTION_SUFFIX "_body__" |
|
151 |
||
152 |
/* Idem as body, but for initializer FB function */ |
|
153 |
#define FB_INIT_SUFFIX "_init__" |
|
154 |
||
155 |
/* Idem as body, but for run CONFIG and RESOURCE function */ |
|
156 |
#define FB_RUN_SUFFIX "_run__" |
|
157 |
||
158 |
/* The FB body function is passed as the only parameter a pointer to the FB data |
|
159 |
* structure instance. The name of this parameter is given by the following constant. |
|
160 |
* In order not to clash with any variable in the IL and ST source codem the |
|
161 |
* following constant should contain a double underscore, which is not allowed |
|
162 |
* in IL and ST. |
|
163 |
* |
|
164 |
* e.g.: the body of FUNTION_BLOCK TEST |
|
165 |
* is mapped onto the C function |
|
166 |
* TEST_body__(TEST *data__) |
|
167 |
*/ |
|
168 |
||
169 |
#define FB_FUNCTION_PARAM "data__" |
|
170 |
||
171 |
||
172 |
#define SFC_STEP_ACTION_PREFIX "__SFC_" |
|
173 |
||
174 |
/***********************************************************************/ |
|
175 |
/***********************************************************************/ |
|
176 |
/***********************************************************************/ |
|
177 |
/***********************************************************************/ |
|
178 |
||
179 |
||
180 |
#include "spec_init_separator.cc" |
|
181 |
#include "function_param_iterator.cc" |
|
182 |
#include "function_call_iterator.cc" |
|
183 |
#include "function_call_param_iterator.cc" |
|
184 |
#include "type_initial_value.cc" |
|
185 |
#include "search_fb_instance_decl.cc" |
|
111 | 186 |
#include "search_fb_typedecl.cc" |
70 | 187 |
#include "search_base_type.cc" |
188 |
#include "search_var_instance_decl.cc" |
|
189 |
#include "decompose_var_instance_name.cc" |
|
190 |
#include "search_varfb_instance_type.cc" |
|
191 |
#include "search_constant_type.cc" |
|
192 |
#include "search_expression_type.cc" |
|
193 |
||
194 |
#include "generate_c_base.cc" |
|
195 |
#include "generate_c_typedecl.cc" |
|
196 |
#include "generate_c_sfcdecl.cc" |
|
197 |
#include "generate_c_vardecl.cc" |
|
198 |
#include "generate_c_configbody.cc" |
|
199 |
#include "generate_location_list.cc" |
|
111 | 200 |
#include "generate_var_list.cc" |
70 | 201 |
|
202 |
/***********************************************************************/ |
|
203 |
/***********************************************************************/ |
|
204 |
/***********************************************************************/ |
|
205 |
/***********************************************************************/ |
|
206 |
||
207 |
/* Generate a name for a temporary variable. |
|
208 |
* Each new name generated is appended a different number, |
|
209 |
* starting off from 0. |
|
210 |
* After calling reset(), the names will start off again from 0. |
|
211 |
*/ |
|
212 |
#define VAR_LEADER "__" |
|
213 |
#define TEMP_VAR VAR_LEADER "TMP_" |
|
214 |
#define SOURCE_VAR VAR_LEADER "SRC_" |
|
215 |
||
216 |
#include "generate_c_tempvardecl.cc" |
|
217 |
||
218 |
#include "generate_c_st.cc" |
|
219 |
#include "generate_c_il.cc" |
|
220 |
||
221 |
#include "generate_c.hh" |
|
222 |
||
223 |
/***********************************************************************/ |
|
224 |
/***********************************************************************/ |
|
225 |
/***********************************************************************/ |
|
226 |
/***********************************************************************/ |
|
227 |
||
228 |
#define MILLISECOND 1000000 |
|
229 |
#define SECOND 1000 * MILLISECOND |
|
230 |
||
231 |
/* A helper class that knows how to generate code for both the IL and ST languages... */ |
|
232 |
class calculate_time_c: public iterator_visitor_c { |
|
233 |
private: |
|
234 |
unsigned long time; |
|
235 |
float current_value; |
|
236 |
||
237 |
public: |
|
238 |
calculate_time_c(void){time = 0;}; |
|
239 |
||
240 |
unsigned long get_time(void) {return time;}; |
|
241 |
||
242 |
void *get_integer_value(token_c *token) { |
|
243 |
std::string str = ""; |
|
244 |
for (unsigned int i = 0; i < strlen(token->value); i++) |
|
245 |
if (token->value[i] != '_') |
|
246 |
str += token->value[i]; |
|
247 |
current_value = atof(str.c_str()); |
|
248 |
return NULL; |
|
249 |
} |
|
250 |
||
251 |
void *get_float_value(token_c *token) { |
|
252 |
current_value = atof(token->value); |
|
253 |
return NULL; |
|
254 |
} |
|
255 |
||
256 |
/******************************/ |
|
257 |
/* B 1.2.1 - Numeric Literals */ |
|
258 |
/******************************/ |
|
259 |
||
260 |
void *visit(integer_c *symbol) {return get_integer_value(symbol);} |
|
261 |
||
262 |
/************************/ |
|
263 |
/* B 1.2.3.1 - Duration */ |
|
264 |
/************************/ |
|
265 |
||
266 |
/* SYM_REF2(duration_c, neg, interval) */ |
|
267 |
void *visit(duration_c *symbol) { |
|
268 |
if (symbol->neg != NULL) |
|
269 |
ERROR; |
|
270 |
symbol->interval->accept(*this); |
|
271 |
return NULL; |
|
272 |
} |
|
273 |
||
274 |
/* SYM_TOKEN(fixed_point_c) */ |
|
275 |
void *visit(fixed_point_c *symbol) {return get_float_value(symbol);} |
|
276 |
||
277 |
/* SYM_REF2(days_c, days, hours) */ |
|
278 |
void *visit(days_c *symbol) { |
|
279 |
if (symbol->hours) |
|
280 |
symbol->hours->accept(*this); |
|
281 |
symbol->days->accept(*this); |
|
282 |
time += (unsigned long)(current_value * 24 * 3600 * SECOND); |
|
283 |
return NULL; |
|
284 |
} |
|
285 |
||
286 |
/* SYM_REF2(hours_c, hours, minutes) */ |
|
287 |
void *visit(hours_c *symbol) { |
|
288 |
if (symbol->minutes) |
|
289 |
symbol->minutes->accept(*this); |
|
290 |
symbol->hours->accept(*this); |
|
291 |
time += (unsigned long)(current_value * 3600 * SECOND); |
|
292 |
return NULL; |
|
293 |
} |
|
294 |
||
295 |
/* SYM_REF2(minutes_c, minutes, seconds) */ |
|
296 |
void *visit(minutes_c *symbol) { |
|
297 |
if (symbol->seconds) |
|
298 |
symbol->seconds->accept(*this); |
|
299 |
symbol->minutes->accept(*this); |
|
300 |
time += (unsigned long)(current_value * 60 * SECOND); |
|
301 |
return NULL; |
|
302 |
} |
|
303 |
||
304 |
/* SYM_REF2(seconds_c, seconds, milliseconds) */ |
|
305 |
void *visit(seconds_c *symbol) { |
|
306 |
if (symbol->milliseconds) |
|
307 |
symbol->milliseconds->accept(*this); |
|
308 |
symbol->seconds->accept(*this); |
|
309 |
time += (unsigned long)(current_value * SECOND); |
|
310 |
return NULL; |
|
311 |
} |
|
312 |
||
313 |
/* SYM_REF2(milliseconds_c, milliseconds, unused) */ |
|
314 |
void *visit(milliseconds_c *symbol) { |
|
315 |
symbol->milliseconds->accept(*this); |
|
316 |
time += (unsigned long)(current_value * MILLISECOND); |
|
317 |
return NULL; |
|
318 |
} |
|
319 |
}; |
|
320 |
||
321 |
/***********************************************************************/ |
|
322 |
/***********************************************************************/ |
|
323 |
/***********************************************************************/ |
|
324 |
/***********************************************************************/ |
|
325 |
||
326 |
class calculate_common_ticktime_c: public iterator_visitor_c { |
|
327 |
private: |
|
328 |
unsigned long common_ticktime; |
|
329 |
||
330 |
public: |
|
331 |
calculate_common_ticktime_c(void){common_ticktime = 0;} |
|
332 |
||
333 |
unsigned long euclide(unsigned long a, unsigned long b) { |
|
334 |
unsigned long c = a % b; |
|
335 |
if (c == 0) |
|
336 |
return b; |
|
337 |
else |
|
338 |
return euclide(b, c); |
|
339 |
} |
|
340 |
||
341 |
void update_ticktime(unsigned long time) { |
|
342 |
if (common_ticktime == 0) |
|
343 |
common_ticktime = time; |
|
344 |
else if (time > common_ticktime) |
|
345 |
common_ticktime = euclide(time, common_ticktime); |
|
346 |
else |
|
347 |
common_ticktime = euclide(common_ticktime, time); |
|
348 |
} |
|
349 |
||
350 |
unsigned long get_ticktime(void) { |
|
351 |
return common_ticktime; |
|
352 |
} |
|
353 |
||
354 |
/* TASK task_name task_initialization */ |
|
355 |
//SYM_REF2(task_configuration_c, task_name, task_initialization) |
|
356 |
void *visit(task_initialization_c *symbol) { |
|
357 |
calculate_time_c calculate_time; |
|
358 |
unsigned long time = 0; |
|
359 |
if (symbol->interval_data_source != NULL) { |
|
360 |
symbol->interval_data_source->accept(calculate_time); |
|
361 |
time = calculate_time.get_time(); |
|
362 |
} |
|
363 |
if (time > 0) |
|
364 |
update_ticktime(time); |
|
365 |
return NULL; |
|
366 |
} |
|
367 |
}; |
|
368 |
||
369 |
/***********************************************************************/ |
|
370 |
/***********************************************************************/ |
|
371 |
/***********************************************************************/ |
|
372 |
/***********************************************************************/ |
|
373 |
||
374 |
/* A helper class that knows how to generate code for both the IL and ST languages... */ |
|
375 |
class generate_c_SFC_IL_ST_c: public null_visitor_c { |
|
376 |
private: |
|
377 |
stage4out_c *s4o_ptr; |
|
378 |
symbol_c *scope; |
|
379 |
const char *variable_prefix; |
|
380 |
||
381 |
public: |
|
382 |
generate_c_SFC_IL_ST_c(stage4out_c *s4o_ptr, symbol_c *scope, const char *variable_prefix = NULL); |
|
383 |
/*********************************************/ |
|
384 |
/* B.1.6 Sequential function chart elements */ |
|
385 |
/*********************************************/ |
|
386 |
||
387 |
/*| sequential_function_chart sfc_network*/ |
|
388 |
void *visit(sequential_function_chart_c * symbol); |
|
389 |
||
390 |
/****************************************/ |
|
391 |
/* B.2 - Language IL (Instruction List) */ |
|
392 |
/****************************************/ |
|
393 |
||
394 |
/***********************************/ |
|
395 |
/* B 2.1 Instructions and Operands */ |
|
396 |
/***********************************/ |
|
397 |
/*| instruction_list il_instruction */ |
|
398 |
void *visit(instruction_list_c *symbol); |
|
399 |
||
400 |
/* Remainder implemented in generate_c_il_c... */ |
|
401 |
||
402 |
/***************************************/ |
|
403 |
/* B.3 - Language ST (Structured Text) */ |
|
404 |
/***************************************/ |
|
405 |
/***********************/ |
|
406 |
/* B 3.1 - Expressions */ |
|
407 |
/***********************/ |
|
408 |
/* Implemented in generate_c_st_c */ |
|
409 |
||
410 |
/********************/ |
|
411 |
/* B 3.2 Statements */ |
|
412 |
/********************/ |
|
413 |
void *visit(statement_list_c *symbol); |
|
414 |
||
415 |
/* Remainder implemented in generate_c_st_c... */ |
|
416 |
}; |
|
417 |
||
418 |
#include "generate_c_sfc.cc" |
|
419 |
||
420 |
generate_c_SFC_IL_ST_c::generate_c_SFC_IL_ST_c(stage4out_c *s4o_ptr, symbol_c *scope, const char *variable_prefix) { |
|
421 |
if (NULL == scope) ERROR; |
|
422 |
this->s4o_ptr = s4o_ptr; |
|
423 |
this->scope = scope; |
|
424 |
this->variable_prefix = variable_prefix; |
|
425 |
} |
|
426 |
||
427 |
void *generate_c_SFC_IL_ST_c::visit(sequential_function_chart_c * symbol) { |
|
428 |
generate_c_sfc_c generate_c_sfc(s4o_ptr, scope, variable_prefix); |
|
429 |
generate_c_sfc.generate(symbol); |
|
430 |
return NULL; |
|
431 |
} |
|
432 |
||
433 |
void *generate_c_SFC_IL_ST_c::visit(instruction_list_c *symbol) { |
|
434 |
generate_c_il_c generate_c_il(s4o_ptr, scope, variable_prefix); |
|
435 |
generate_c_il.generate(symbol); |
|
436 |
return NULL; |
|
437 |
} |
|
438 |
||
439 |
void *generate_c_SFC_IL_ST_c::visit(statement_list_c *symbol) { |
|
440 |
generate_c_st_c generate_c_st(s4o_ptr, scope, variable_prefix); |
|
441 |
generate_c_st.generate(symbol); |
|
442 |
return NULL; |
|
443 |
} |
|
444 |
||
445 |
||
446 |
||
447 |
||
448 |
/***********************************************************************/ |
|
449 |
/***********************************************************************/ |
|
450 |
/***********************************************************************/ |
|
451 |
/***********************************************************************/ |
|
452 |
/***********************************************************************/ |
|
453 |
||
454 |
||
455 |
class generate_c_pous_c: public generate_c_typedecl_c { |
|
456 |
||
457 |
public: |
|
121
9e8ce092e169
Adding support for POU struct definition in POUS.h
lbessard
parents:
120
diff
changeset
|
458 |
generate_c_pous_c(stage4out_c *s4o_ptr, stage4out_c *s4o_incl_ptr) |
9e8ce092e169
Adding support for POU struct definition in POUS.h
lbessard
parents:
120
diff
changeset
|
459 |
: generate_c_typedecl_c(s4o_ptr, s4o_incl_ptr) {}; |
70 | 460 |
virtual ~generate_c_pous_c(void) {} |
461 |
||
462 |
||
463 |
public: |
|
464 |
||
465 |
/*************************/ |
|
466 |
/* B.1 - Common elements */ |
|
467 |
/*************************/ |
|
468 |
/*******************************************/ |
|
469 |
/* B 1.1 - Letters, digits and identifiers */ |
|
470 |
/*******************************************/ |
|
471 |
/* done in base class(es) */ |
|
472 |
||
473 |
/*********************/ |
|
474 |
/* B 1.2 - Constants */ |
|
475 |
/*********************/ |
|
476 |
/* originally empty... */ |
|
477 |
||
478 |
/******************************/ |
|
479 |
/* B 1.2.1 - Numeric Literals */ |
|
480 |
/******************************/ |
|
481 |
/* done in base class(es) */ |
|
482 |
||
483 |
/*******************************/ |
|
484 |
/* B.1.2.2 Character Strings */ |
|
485 |
/*******************************/ |
|
486 |
/* done in base class(es) */ |
|
487 |
||
488 |
/***************************/ |
|
489 |
/* B 1.2.3 - Time Literals */ |
|
490 |
/***************************/ |
|
491 |
/************************/ |
|
492 |
/* B 1.2.3.1 - Duration */ |
|
493 |
/************************/ |
|
494 |
/* done in base class(es) */ |
|
495 |
||
496 |
/************************************/ |
|
497 |
/* B 1.2.3.2 - Time of day and Date */ |
|
498 |
/************************************/ |
|
499 |
/* done in base class(es) */ |
|
500 |
||
501 |
/**********************/ |
|
502 |
/* B.1.3 - Data types */ |
|
503 |
/**********************/ |
|
504 |
/***********************************/ |
|
505 |
/* B 1.3.1 - Elementary Data Types */ |
|
506 |
/***********************************/ |
|
507 |
/* done in base class(es) */ |
|
508 |
||
509 |
/********************************/ |
|
510 |
/* B.1.3.2 - Generic data types */ |
|
511 |
/********************************/ |
|
512 |
/* originally empty... */ |
|
513 |
||
514 |
/********************************/ |
|
515 |
/* B 1.3.3 - Derived data types */ |
|
516 |
/********************************/ |
|
517 |
/* done in base class(es) */ |
|
518 |
||
519 |
/*********************/ |
|
520 |
/* B 1.4 - Variables */ |
|
521 |
/*********************/ |
|
522 |
/* done in base class(es) */ |
|
523 |
||
524 |
/********************************************/ |
|
525 |
/* B.1.4.1 Directly Represented Variables */ |
|
526 |
/********************************************/ |
|
527 |
/* done in base class(es) */ |
|
528 |
||
529 |
/*************************************/ |
|
530 |
/* B.1.4.2 Multi-element Variables */ |
|
531 |
/*************************************/ |
|
532 |
/* done in base class(es) */ |
|
533 |
||
534 |
/******************************************/ |
|
535 |
/* B 1.4.3 - Declaration & Initialisation */ |
|
536 |
/******************************************/ |
|
537 |
/* done in base class(es) */ |
|
538 |
||
539 |
/**************************************/ |
|
540 |
/* B.1.5 - Program organization units */ |
|
541 |
/**************************************/ |
|
542 |
/***********************/ |
|
543 |
/* B 1.5.1 - Functions */ |
|
544 |
/***********************/ |
|
545 |
||
546 |
public: |
|
547 |
/* FUNCTION derived_function_name ':' elementary_type_name io_OR_function_var_declarations_list function_body END_FUNCTION */ |
|
548 |
/* | FUNCTION derived_function_name ':' derived_type_name io_OR_function_var_declarations_list function_body END_FUNCTION */ |
|
549 |
void *visit(function_declaration_c *symbol) { |
|
550 |
generate_c_vardecl_c *vardecl; |
|
551 |
TRACE("function_declaration_c"); |
|
552 |
||
553 |
/* start off by adding this declaration to the global |
|
554 |
* function declaration symbol table... |
|
555 |
*/ |
|
556 |
function_symtable.insert(symbol->derived_function_name, symbol); |
|
557 |
||
558 |
/* (A) Function declaration... */ |
|
559 |
/* (A.1) Function return type */ |
|
560 |
s4o.print("// FUNCTION\n"); |
|
561 |
symbol->type_name->accept(*this); /* return type */ |
|
562 |
s4o.print(" "); |
|
563 |
/* (A.2) Function name */ |
|
564 |
symbol->derived_function_name->accept(*this); |
|
565 |
s4o.print("("); |
|
566 |
||
567 |
/* (A.3) Function parameters */ |
|
568 |
s4o.indent_right(); |
|
569 |
vardecl = new generate_c_vardecl_c(&s4o, |
|
570 |
generate_c_vardecl_c::finterface_vf, |
|
571 |
generate_c_vardecl_c::input_vt | |
|
572 |
generate_c_vardecl_c::output_vt | |
|
573 |
generate_c_vardecl_c::inoutput_vt); |
|
574 |
vardecl->print(symbol->var_declarations_list); |
|
575 |
delete vardecl; |
|
576 |
s4o.indent_left(); |
|
577 |
||
578 |
s4o.print(")\n" + s4o.indent_spaces + "{\n"); |
|
579 |
||
580 |
/* (B) Function local variable declaration */ |
|
581 |
/* (B.1) Variables declared in ST source code */ |
|
582 |
s4o.indent_right(); |
|
583 |
vardecl = new generate_c_vardecl_c(&s4o, generate_c_vardecl_c::localinit_vf, generate_c_vardecl_c::private_vt); |
|
584 |
vardecl->print(symbol->var_declarations_list); |
|
585 |
delete vardecl; |
|
586 |
||
587 |
/* (B.2) Temporary variable for function's return value */ |
|
588 |
/* It will have the same name as the function itself! */ |
|
589 |
s4o.print(s4o.indent_spaces); |
|
590 |
symbol->type_name->accept(*this); /* return type */ |
|
591 |
s4o.print(" "); |
|
592 |
symbol->derived_function_name->accept(*this); |
|
593 |
s4o.print(" = "); |
|
594 |
{ |
|
595 |
/* get the default value of this variable's type */ |
|
596 |
symbol_c *default_value = (symbol_c *)symbol->type_name->accept(*type_initial_value_c::instance()); |
|
597 |
if (default_value == NULL) ERROR; |
|
598 |
default_value->accept(*this); |
|
599 |
} |
|
600 |
s4o.print(";\n\n"); |
|
601 |
||
602 |
/* (C) Function body */ |
|
603 |
generate_c_SFC_IL_ST_c generate_c_code(&s4o, symbol); |
|
604 |
symbol->function_body->accept(generate_c_code); |
|
605 |
s4o.print(s4o.indent_spaces + "return "); |
|
606 |
symbol->derived_function_name->accept(*this); |
|
607 |
s4o.print(";\n"); |
|
608 |
s4o.indent_left(); |
|
609 |
s4o.print(s4o.indent_spaces + "}\n\n\n"); |
|
610 |
||
611 |
return NULL; |
|
612 |
} |
|
613 |
||
614 |
||
615 |
/* The remaining var_declarations_list_c, function_var_decls_c |
|
616 |
* and var2_init_decl_list_c are handled in the generate_c_vardecl_c class |
|
617 |
*/ |
|
618 |
||
619 |
||
620 |
/*****************************/ |
|
621 |
/* B 1.5.2 - Function Blocks */ |
|
622 |
/*****************************/ |
|
623 |
public: |
|
624 |
/* FUNCTION_BLOCK derived_function_block_name io_OR_other_var_declarations function_block_body END_FUNCTION_BLOCK */ |
|
625 |
//SYM_REF4(function_block_declaration_c, fblock_name, var_declarations, fblock_body, unused) |
|
626 |
void *visit(function_block_declaration_c *symbol) { |
|
627 |
generate_c_vardecl_c *vardecl; |
|
628 |
generate_c_sfcdecl_c *sfcdecl; |
|
121
9e8ce092e169
Adding support for POU struct definition in POUS.h
lbessard
parents:
120
diff
changeset
|
629 |
generate_c_typedecl_c *typedecl; |
70 | 630 |
TRACE("function_block_declaration_c"); |
631 |
||
632 |
/* start off by adding this declaration to the global |
|
633 |
* function block declaration symbol table... |
|
634 |
*/ |
|
635 |
function_block_type_symtable.insert(symbol->fblock_name, symbol); |
|
636 |
||
637 |
/* (A) Function Block data structure declaration... */ |
|
121
9e8ce092e169
Adding support for POU struct definition in POUS.h
lbessard
parents:
120
diff
changeset
|
638 |
typedecl = new generate_c_typedecl_c(&s4o_incl); |
70 | 639 |
/* (A.1) Data structure declaration */ |
121
9e8ce092e169
Adding support for POU struct definition in POUS.h
lbessard
parents:
120
diff
changeset
|
640 |
s4o_incl.print("// FUNCTION_BLOCK "); |
9e8ce092e169
Adding support for POU struct definition in POUS.h
lbessard
parents:
120
diff
changeset
|
641 |
symbol->fblock_name->accept(*typedecl); |
9e8ce092e169
Adding support for POU struct definition in POUS.h
lbessard
parents:
120
diff
changeset
|
642 |
s4o_incl.print("\n// Data part\n"); |
9e8ce092e169
Adding support for POU struct definition in POUS.h
lbessard
parents:
120
diff
changeset
|
643 |
s4o_incl.print("typedef struct {\n"); |
9e8ce092e169
Adding support for POU struct definition in POUS.h
lbessard
parents:
120
diff
changeset
|
644 |
s4o_incl.indent_right(); |
70 | 645 |
/* (A.2) Public variables: i.e. the function parameters... */ |
121
9e8ce092e169
Adding support for POU struct definition in POUS.h
lbessard
parents:
120
diff
changeset
|
646 |
s4o_incl.print(s4o_incl.indent_spaces + "// FB Interface - IN, OUT, IN_OUT variables\n"); |
9e8ce092e169
Adding support for POU struct definition in POUS.h
lbessard
parents:
120
diff
changeset
|
647 |
vardecl = new generate_c_vardecl_c(&s4o_incl, |
70 | 648 |
generate_c_vardecl_c::local_vf, |
649 |
generate_c_vardecl_c::input_vt | |
|
650 |
generate_c_vardecl_c::output_vt | |
|
651 |
generate_c_vardecl_c::inoutput_vt); |
|
652 |
vardecl->print(symbol->var_declarations); |
|
653 |
delete vardecl; |
|
121
9e8ce092e169
Adding support for POU struct definition in POUS.h
lbessard
parents:
120
diff
changeset
|
654 |
s4o_incl.print("\n"); |
70 | 655 |
/* (A.3) Private internal variables */ |
121
9e8ce092e169
Adding support for POU struct definition in POUS.h
lbessard
parents:
120
diff
changeset
|
656 |
s4o_incl.print(s4o_incl.indent_spaces + "// FB private variables - TEMP, private and located variables\n"); |
9e8ce092e169
Adding support for POU struct definition in POUS.h
lbessard
parents:
120
diff
changeset
|
657 |
vardecl = new generate_c_vardecl_c(&s4o_incl, |
70 | 658 |
generate_c_vardecl_c::local_vf, |
659 |
generate_c_vardecl_c::temp_vt | |
|
660 |
generate_c_vardecl_c::private_vt | |
|
661 |
generate_c_vardecl_c::located_vt | |
|
662 |
generate_c_vardecl_c::external_vt); |
|
663 |
vardecl->print(symbol->var_declarations); |
|
664 |
delete vardecl; |
|
665 |
/* (A.4) Generate private internal variables for SFC */ |
|
121
9e8ce092e169
Adding support for POU struct definition in POUS.h
lbessard
parents:
120
diff
changeset
|
666 |
sfcdecl = new generate_c_sfcdecl_c(&s4o_incl, generate_c_sfcdecl_c::sfcdecl_sd); |
70 | 667 |
sfcdecl->print(symbol->fblock_body); |
668 |
delete sfcdecl; |
|
121
9e8ce092e169
Adding support for POU struct definition in POUS.h
lbessard
parents:
120
diff
changeset
|
669 |
s4o_incl.print("\n"); |
9e8ce092e169
Adding support for POU struct definition in POUS.h
lbessard
parents:
120
diff
changeset
|
670 |
|
70 | 671 |
/* (A.5) Function Block data structure type name. */ |
121
9e8ce092e169
Adding support for POU struct definition in POUS.h
lbessard
parents:
120
diff
changeset
|
672 |
s4o_incl.indent_left(); |
9e8ce092e169
Adding support for POU struct definition in POUS.h
lbessard
parents:
120
diff
changeset
|
673 |
s4o_incl.print("} "); |
9e8ce092e169
Adding support for POU struct definition in POUS.h
lbessard
parents:
120
diff
changeset
|
674 |
symbol->fblock_name->accept(*typedecl); |
9e8ce092e169
Adding support for POU struct definition in POUS.h
lbessard
parents:
120
diff
changeset
|
675 |
s4o_incl.print(";\n\n"); |
9e8ce092e169
Adding support for POU struct definition in POUS.h
lbessard
parents:
120
diff
changeset
|
676 |
delete typedecl; |
70 | 677 |
|
678 |
/* (B) Constructor */ |
|
679 |
/* (B.1) Constructor name... */ |
|
680 |
s4o.print(s4o.indent_spaces + "void "); |
|
681 |
symbol->fblock_name->accept(*this); |
|
682 |
s4o.print(FB_INIT_SUFFIX); |
|
683 |
s4o.print("("); |
|
684 |
||
685 |
/* first and only parameter is a pointer to the data */ |
|
686 |
symbol->fblock_name->accept(*this); |
|
687 |
s4o.print(" *"); |
|
688 |
s4o.print(FB_FUNCTION_PARAM); |
|
689 |
s4o.print(") {\n"); |
|
690 |
s4o.indent_right(); |
|
691 |
||
692 |
/* (B.2) Member initializations... */ |
|
693 |
s4o.print(s4o.indent_spaces); |
|
694 |
vardecl = new generate_c_vardecl_c(&s4o, |
|
695 |
generate_c_vardecl_c::constructorinit_vf, |
|
696 |
generate_c_vardecl_c::input_vt | |
|
697 |
generate_c_vardecl_c::output_vt | |
|
698 |
generate_c_vardecl_c::inoutput_vt | |
|
699 |
generate_c_vardecl_c::private_vt | |
|
700 |
generate_c_vardecl_c::located_vt | |
|
701 |
generate_c_vardecl_c::external_vt); |
|
702 |
vardecl->print(symbol->var_declarations, NULL, FB_FUNCTION_PARAM"->"); |
|
703 |
delete vardecl; |
|
704 |
s4o.print("\n"); |
|
705 |
/* (B.3) Generate private internal variables for SFC */ |
|
706 |
sfcdecl = new generate_c_sfcdecl_c(&s4o, generate_c_sfcdecl_c::sfcinit_sd); |
|
707 |
sfcdecl->print(symbol->fblock_body, FB_FUNCTION_PARAM"->"); |
|
708 |
delete sfcdecl; |
|
709 |
s4o.indent_left(); |
|
710 |
s4o.print(s4o.indent_spaces + "}\n\n"); |
|
711 |
||
712 |
||
713 |
/* (C) Function with FB body */ |
|
714 |
/* (C.1) Step definitions */ |
|
715 |
sfcdecl = new generate_c_sfcdecl_c(&s4o, generate_c_sfcdecl_c::stepdef_sd); |
|
716 |
sfcdecl->print(symbol->fblock_body); |
|
717 |
delete sfcdecl; |
|
718 |
||
719 |
/* (C.2) Action definitions */ |
|
720 |
sfcdecl = new generate_c_sfcdecl_c(&s4o, generate_c_sfcdecl_c::actiondef_sd); |
|
721 |
sfcdecl->print(symbol->fblock_body); |
|
722 |
delete sfcdecl; |
|
723 |
||
724 |
/* (C.3) Function declaration */ |
|
725 |
s4o.print("// Code part\n"); |
|
726 |
/* function interface */ |
|
727 |
s4o.print("void "); |
|
728 |
symbol->fblock_name->accept(*this); |
|
729 |
s4o.print(FB_FUNCTION_SUFFIX); |
|
730 |
s4o.print("("); |
|
731 |
/* first and only parameter is a pointer to the data */ |
|
732 |
symbol->fblock_name->accept(*this); |
|
733 |
s4o.print(" *"); |
|
734 |
s4o.print(FB_FUNCTION_PARAM); |
|
735 |
s4o.print(") {\n"); |
|
736 |
s4o.indent_right(); |
|
737 |
||
738 |
/* (C.4) Initialize TEMP variables */ |
|
739 |
/* function body */ |
|
740 |
s4o.print(s4o.indent_spaces + "// Initialise TEMP variables\n"); |
|
741 |
vardecl = new generate_c_vardecl_c(&s4o, |
|
742 |
generate_c_vardecl_c::init_vf, |
|
743 |
generate_c_vardecl_c::temp_vt); |
|
744 |
vardecl->print(symbol->var_declarations, NULL, FB_FUNCTION_PARAM"->"); |
|
745 |
delete vardecl; |
|
746 |
s4o.print("\n"); |
|
747 |
||
748 |
/* (C.5) Function code */ |
|
749 |
generate_c_SFC_IL_ST_c generate_c_code(&s4o, symbol, FB_FUNCTION_PARAM"->"); |
|
750 |
symbol->fblock_body->accept(generate_c_code); |
|
751 |
s4o.indent_left(); |
|
752 |
s4o.print(s4o.indent_spaces + "} // "); |
|
753 |
symbol->fblock_name->accept(*this); |
|
754 |
s4o.print(FB_FUNCTION_SUFFIX); |
|
755 |
s4o.print(s4o.indent_spaces + "() \n\n"); |
|
756 |
||
757 |
/* (C.6) Step undefinitions */ |
|
758 |
sfcdecl = new generate_c_sfcdecl_c(&s4o, generate_c_sfcdecl_c::stepundef_sd); |
|
759 |
sfcdecl->print(symbol->fblock_body); |
|
760 |
delete sfcdecl; |
|
761 |
||
762 |
/* (C.7) Action undefinitions */ |
|
763 |
sfcdecl = new generate_c_sfcdecl_c(&s4o, generate_c_sfcdecl_c::actionundef_sd); |
|
764 |
sfcdecl->print(symbol->fblock_body); |
|
765 |
delete sfcdecl; |
|
766 |
||
767 |
s4o.indent_left(); |
|
768 |
s4o.print("\n\n\n\n"); |
|
769 |
||
770 |
return NULL; |
|
771 |
} |
|
772 |
||
773 |
||
774 |
/* The remaining temp_var_decls_c, temp_var_decls_list_c |
|
775 |
* and non_retentive_var_decls_c are handled in the generate_c_vardecl_c class |
|
776 |
*/ |
|
777 |
||
778 |
||
779 |
/**********************/ |
|
780 |
/* B 1.5.3 - Programs */ |
|
781 |
/**********************/ |
|
782 |
||
783 |
||
784 |
||
785 |
public: |
|
786 |
/* PROGRAM program_type_name program_var_declarations_list function_block_body END_PROGRAM */ |
|
787 |
//SYM_REF4(program_declaration_c, program_type_name, var_declarations, function_block_body, unused) |
|
788 |
void *visit(program_declaration_c *symbol) { |
|
789 |
generate_c_vardecl_c *vardecl; |
|
790 |
generate_c_sfcdecl_c *sfcdecl; |
|
121
9e8ce092e169
Adding support for POU struct definition in POUS.h
lbessard
parents:
120
diff
changeset
|
791 |
generate_c_typedecl_c *typedecl; |
70 | 792 |
TRACE("program_declaration_c"); |
793 |
||
794 |
/* start off by adding this declaration to the global |
|
795 |
* program declaration symbol table... |
|
796 |
*/ |
|
797 |
program_type_symtable.insert(symbol->program_type_name, symbol); |
|
798 |
||
799 |
/* (A) Program data structure declaration... */ |
|
121
9e8ce092e169
Adding support for POU struct definition in POUS.h
lbessard
parents:
120
diff
changeset
|
800 |
typedecl = new generate_c_typedecl_c(&s4o_incl); |
70 | 801 |
/* (A.1) Data structure declaration */ |
121
9e8ce092e169
Adding support for POU struct definition in POUS.h
lbessard
parents:
120
diff
changeset
|
802 |
s4o_incl.print("// PROGRAM "); |
9e8ce092e169
Adding support for POU struct definition in POUS.h
lbessard
parents:
120
diff
changeset
|
803 |
symbol->program_type_name->accept(*typedecl); |
9e8ce092e169
Adding support for POU struct definition in POUS.h
lbessard
parents:
120
diff
changeset
|
804 |
s4o_incl.print("\n// Data part\n"); |
9e8ce092e169
Adding support for POU struct definition in POUS.h
lbessard
parents:
120
diff
changeset
|
805 |
s4o_incl.print("typedef struct {\n"); |
9e8ce092e169
Adding support for POU struct definition in POUS.h
lbessard
parents:
120
diff
changeset
|
806 |
s4o_incl.indent_right(); |
70 | 807 |
|
808 |
/* (A.2) Public variables: i.e. the program parameters... */ |
|
121
9e8ce092e169
Adding support for POU struct definition in POUS.h
lbessard
parents:
120
diff
changeset
|
809 |
s4o_incl.print(s4o_incl.indent_spaces + "// PROGRAM Interface - IN, OUT, IN_OUT variables\n"); |
9e8ce092e169
Adding support for POU struct definition in POUS.h
lbessard
parents:
120
diff
changeset
|
810 |
vardecl = new generate_c_vardecl_c(&s4o_incl, |
70 | 811 |
generate_c_vardecl_c::local_vf, |
812 |
generate_c_vardecl_c::input_vt | |
|
813 |
generate_c_vardecl_c::output_vt | |
|
814 |
generate_c_vardecl_c::inoutput_vt); |
|
815 |
vardecl->print(symbol->var_declarations); |
|
816 |
delete vardecl; |
|
121
9e8ce092e169
Adding support for POU struct definition in POUS.h
lbessard
parents:
120
diff
changeset
|
817 |
s4o_incl.print("\n"); |
70 | 818 |
/* (A.3) Private internal variables */ |
121
9e8ce092e169
Adding support for POU struct definition in POUS.h
lbessard
parents:
120
diff
changeset
|
819 |
s4o_incl.print(s4o_incl.indent_spaces + "// PROGRAM private variables - TEMP, private and located variables\n"); |
9e8ce092e169
Adding support for POU struct definition in POUS.h
lbessard
parents:
120
diff
changeset
|
820 |
vardecl = new generate_c_vardecl_c(&s4o_incl, |
70 | 821 |
generate_c_vardecl_c::local_vf, |
822 |
generate_c_vardecl_c::temp_vt | |
|
823 |
generate_c_vardecl_c::private_vt | |
|
824 |
generate_c_vardecl_c::located_vt | |
|
825 |
generate_c_vardecl_c::external_vt); |
|
826 |
vardecl->print(symbol->var_declarations); |
|
827 |
delete vardecl; |
|
828 |
/* (A.4) Generate private internal variables for SFC */ |
|
121
9e8ce092e169
Adding support for POU struct definition in POUS.h
lbessard
parents:
120
diff
changeset
|
829 |
sfcdecl = new generate_c_sfcdecl_c(&s4o_incl, generate_c_sfcdecl_c::sfcdecl_sd); |
70 | 830 |
sfcdecl->print(symbol->function_block_body); |
831 |
delete sfcdecl; |
|
832 |
||
833 |
/* (A.5) Program data structure type name. */ |
|
121
9e8ce092e169
Adding support for POU struct definition in POUS.h
lbessard
parents:
120
diff
changeset
|
834 |
s4o_incl.indent_left(); |
9e8ce092e169
Adding support for POU struct definition in POUS.h
lbessard
parents:
120
diff
changeset
|
835 |
s4o_incl.print("} "); |
9e8ce092e169
Adding support for POU struct definition in POUS.h
lbessard
parents:
120
diff
changeset
|
836 |
symbol->program_type_name->accept(*typedecl); |
9e8ce092e169
Adding support for POU struct definition in POUS.h
lbessard
parents:
120
diff
changeset
|
837 |
s4o_incl.print(";\n\n"); |
9e8ce092e169
Adding support for POU struct definition in POUS.h
lbessard
parents:
120
diff
changeset
|
838 |
delete typedecl; |
70 | 839 |
|
840 |
/* (B) Constructor */ |
|
841 |
/* (B.1) Constructor name... */ |
|
842 |
s4o.print(s4o.indent_spaces + "void "); |
|
843 |
symbol->program_type_name->accept(*this); |
|
844 |
s4o.print(FB_INIT_SUFFIX); |
|
845 |
s4o.print("("); |
|
846 |
||
847 |
/* first and only parameter is a pointer to the data */ |
|
848 |
symbol->program_type_name->accept(*this); |
|
849 |
s4o.print(" *"); |
|
850 |
s4o.print(FB_FUNCTION_PARAM); |
|
851 |
s4o.print(") {\n"); |
|
852 |
s4o.indent_right(); |
|
853 |
||
854 |
/* (B.2) Member initializations... */ |
|
855 |
s4o.print(s4o.indent_spaces); |
|
856 |
vardecl = new generate_c_vardecl_c(&s4o, |
|
857 |
generate_c_vardecl_c::constructorinit_vf, |
|
858 |
generate_c_vardecl_c::input_vt | |
|
859 |
generate_c_vardecl_c::output_vt | |
|
860 |
generate_c_vardecl_c::inoutput_vt | |
|
861 |
generate_c_vardecl_c::private_vt | |
|
862 |
generate_c_vardecl_c::located_vt | |
|
863 |
generate_c_vardecl_c::external_vt); |
|
864 |
vardecl->print(symbol->var_declarations, NULL, FB_FUNCTION_PARAM"->"); |
|
865 |
delete vardecl; |
|
866 |
s4o.print("\n"); |
|
867 |
/* (B.3) Generate private internal variables for SFC */ |
|
868 |
sfcdecl = new generate_c_sfcdecl_c(&s4o, generate_c_sfcdecl_c::sfcinit_sd); |
|
869 |
sfcdecl->print(symbol->function_block_body,FB_FUNCTION_PARAM"->"); |
|
870 |
delete sfcdecl; |
|
871 |
||
872 |
s4o.indent_left(); |
|
873 |
s4o.print(s4o.indent_spaces + "}\n\n"); |
|
874 |
||
875 |
/* (C) Function with PROGRAM body */ |
|
876 |
/* (C.1) Step definitions */ |
|
877 |
sfcdecl = new generate_c_sfcdecl_c(&s4o, generate_c_sfcdecl_c::stepdef_sd); |
|
878 |
sfcdecl->print(symbol->function_block_body); |
|
879 |
delete sfcdecl; |
|
880 |
||
881 |
/* (C.2) Action definitions */ |
|
882 |
sfcdecl = new generate_c_sfcdecl_c(&s4o, generate_c_sfcdecl_c::actiondef_sd); |
|
883 |
sfcdecl->print(symbol->function_block_body); |
|
884 |
delete sfcdecl; |
|
885 |
||
886 |
/* (C.3) Function declaration */ |
|
887 |
s4o.print("// Code part\n"); |
|
888 |
/* function interface */ |
|
889 |
s4o.print("void "); |
|
890 |
symbol->program_type_name->accept(*this); |
|
891 |
s4o.print(FB_FUNCTION_SUFFIX); |
|
892 |
s4o.print("("); |
|
893 |
/* first and only parameter is a pointer to the data */ |
|
894 |
symbol->program_type_name->accept(*this); |
|
895 |
s4o.print(" *"); |
|
896 |
s4o.print(FB_FUNCTION_PARAM); |
|
897 |
s4o.print(") {\n"); |
|
898 |
s4o.indent_right(); |
|
899 |
||
900 |
/* (C.4) Initialize TEMP variables */ |
|
901 |
/* function body */ |
|
902 |
s4o.print(s4o.indent_spaces + "// Initialise TEMP variables\n"); |
|
903 |
vardecl = new generate_c_vardecl_c(&s4o, |
|
904 |
generate_c_vardecl_c::init_vf, |
|
905 |
generate_c_vardecl_c::temp_vt); |
|
906 |
vardecl->print(symbol->var_declarations, NULL, FB_FUNCTION_PARAM"->"); |
|
907 |
delete vardecl; |
|
908 |
s4o.print("\n"); |
|
909 |
||
910 |
/* (C.5) Function code */ |
|
911 |
generate_c_SFC_IL_ST_c generate_c_code(&s4o, symbol, FB_FUNCTION_PARAM"->"); |
|
912 |
symbol->function_block_body->accept(generate_c_code); |
|
913 |
s4o.indent_left(); |
|
914 |
s4o.print(s4o.indent_spaces + "} // "); |
|
915 |
symbol->program_type_name->accept(*this); |
|
916 |
s4o.print(FB_FUNCTION_SUFFIX); |
|
917 |
s4o.print(s4o.indent_spaces + "() \n\n"); |
|
918 |
||
919 |
/* (C.6) Step undefinitions */ |
|
920 |
sfcdecl = new generate_c_sfcdecl_c(&s4o, generate_c_sfcdecl_c::stepundef_sd); |
|
921 |
sfcdecl->print(symbol->function_block_body); |
|
922 |
delete sfcdecl; |
|
923 |
||
924 |
/* (C.7) Action undefinitions */ |
|
925 |
sfcdecl = new generate_c_sfcdecl_c(&s4o, generate_c_sfcdecl_c::actionundef_sd); |
|
926 |
sfcdecl->print(symbol->function_block_body); |
|
927 |
delete sfcdecl; |
|
928 |
||
929 |
s4o.indent_left(); |
|
930 |
s4o.print("\n\n\n\n"); |
|
931 |
||
932 |
return NULL; |
|
933 |
} |
|
934 |
||
935 |
}; /* generate_c_pous_c */ |
|
936 |
||
937 |
/***********************************************************************/ |
|
938 |
/***********************************************************************/ |
|
939 |
/***********************************************************************/ |
|
940 |
/***********************************************************************/ |
|
941 |
/***********************************************************************/ |
|
942 |
/***********************************************************************/ |
|
943 |
/***********************************************************************/ |
|
944 |
/***********************************************************************/ |
|
945 |
||
946 |
class generate_c_config_c: public generate_c_typedecl_c { |
|
947 |
||
948 |
public: |
|
949 |
generate_c_config_c(stage4out_c *s4o_ptr) |
|
950 |
: generate_c_typedecl_c(s4o_ptr) {}; |
|
951 |
virtual ~generate_c_config_c(void) {} |
|
952 |
||
953 |
typedef enum { |
|
954 |
initprotos_dt, |
|
955 |
initdeclare_dt, |
|
956 |
runprotos_dt, |
|
957 |
rundeclare_dt |
|
958 |
} declaretype_t; |
|
959 |
||
960 |
declaretype_t wanted_declaretype; |
|
961 |
||
962 |
/********************************/ |
|
963 |
/* B 1.7 Configuration elements */ |
|
964 |
/********************************/ |
|
965 |
||
966 |
||
967 |
public: |
|
968 |
/* |
|
969 |
CONFIGURATION configuration_name |
|
970 |
optional_global_var_declarations |
|
971 |
(resource_declaration_list | single_resource_declaration) |
|
972 |
optional_access_declarations |
|
973 |
optional_instance_specific_initializations |
|
974 |
END_CONFIGURATION |
|
975 |
*/ |
|
976 |
/* |
|
977 |
SYM_REF6(configuration_declaration_c, configuration_name, global_var_declarations, resource_declarations, access_declarations, instance_specific_initializations, unused) |
|
978 |
*/ |
|
979 |
void *visit(configuration_declaration_c *symbol) { |
|
980 |
generate_c_vardecl_c *vardecl; |
|
981 |
||
982 |
/* Insert the header... */ |
|
983 |
s4o.print("/*******************************************/\n"); |
|
984 |
s4o.print("/* FILE GENERATED BY iec2c */\n"); |
|
985 |
s4o.print("/* Editing this file is not recommended... */\n"); |
|
986 |
s4o.print("/*******************************************/\n\n"); |
|
987 |
s4o.print("#include \"iec_std_lib.h\"\n\n"); |
|
988 |
||
989 |
/* (A) configuration declaration... */ |
|
990 |
/* (A.1) configuration name in comment */ |
|
991 |
s4o.print("// CONFIGURATION "); |
|
992 |
symbol->configuration_name->accept(*this); |
|
993 |
s4o.print("\n"); |
|
994 |
||
995 |
/* (A.2) Global variables */ |
|
996 |
vardecl = new generate_c_vardecl_c(&s4o, |
|
997 |
generate_c_vardecl_c::local_vf, |
|
998 |
generate_c_vardecl_c::global_vt); |
|
999 |
vardecl->print(symbol); |
|
1000 |
delete vardecl; |
|
1001 |
s4o.print("\n"); |
|
1002 |
||
1003 |
/* (B) Initialisation Function */ |
|
1004 |
/* (B.1) Ressources initialisation protos... */ |
|
1005 |
wanted_declaretype = initprotos_dt; |
|
1006 |
symbol->resource_declarations->accept(*this); |
|
1007 |
s4o.print("\n"); |
|
1008 |
||
1009 |
/* (B.2) Initialisation function name... */ |
|
1010 |
s4o.print(s4o.indent_spaces + "void config"); |
|
1011 |
s4o.print(FB_INIT_SUFFIX); |
|
1012 |
s4o.print("(void) {\n"); |
|
1013 |
s4o.indent_right(); |
|
1014 |
||
1015 |
/* (B.3) Global variables initializations... */ |
|
1016 |
s4o.print(s4o.indent_spaces); |
|
1017 |
vardecl = new generate_c_vardecl_c(&s4o, |
|
1018 |
generate_c_vardecl_c::constructorinit_vf, |
|
1019 |
generate_c_vardecl_c::global_vt); |
|
1020 |
vardecl->print(symbol); |
|
1021 |
delete vardecl; |
|
1022 |
s4o.print("\n"); |
|
1023 |
||
1024 |
/* (B.3) Resources initializations... */ |
|
1025 |
wanted_declaretype = initdeclare_dt; |
|
1026 |
symbol->resource_declarations->accept(*this); |
|
1027 |
||
1028 |
s4o.indent_left(); |
|
1029 |
s4o.print(s4o.indent_spaces + "}\n\n"); |
|
1030 |
||
1031 |
||
1032 |
/* (C) Run Function*/ |
|
1033 |
/* (C.1) Resources run functions protos... */ |
|
1034 |
wanted_declaretype = runprotos_dt; |
|
1035 |
symbol->resource_declarations->accept(*this); |
|
1036 |
s4o.print("\n"); |
|
1037 |
||
1038 |
/* (C.2) Run function name... */ |
|
1039 |
s4o.print(s4o.indent_spaces + "void config"); |
|
1040 |
s4o.print(FB_RUN_SUFFIX); |
|
1041 |
s4o.print("(int tick) {\n"); |
|
1042 |
s4o.indent_right(); |
|
1043 |
||
1044 |
/* (C.3) Resources initializations... */ |
|
1045 |
wanted_declaretype = rundeclare_dt; |
|
1046 |
symbol->resource_declarations->accept(*this); |
|
1047 |
||
1048 |
/* (C.3) Close Public Function body */ |
|
1049 |
s4o.indent_left(); |
|
1050 |
s4o.print(s4o.indent_spaces + "}\n"); |
|
1051 |
||
1052 |
return NULL; |
|
1053 |
} |
|
1054 |
||
1055 |
void *visit(resource_declaration_c *symbol) { |
|
1056 |
if (wanted_declaretype == initprotos_dt || wanted_declaretype == runprotos_dt) { |
|
1057 |
s4o.print(s4o.indent_spaces + "void "); |
|
1058 |
symbol->resource_name->accept(*this); |
|
1059 |
if (wanted_declaretype == initprotos_dt) { |
|
1060 |
s4o.print(FB_INIT_SUFFIX); |
|
1061 |
s4o.print("(void);\n"); |
|
1062 |
} |
|
1063 |
else { |
|
1064 |
s4o.print(FB_RUN_SUFFIX); |
|
1065 |
s4o.print("(int tick);\n"); |
|
1066 |
} |
|
1067 |
} |
|
1068 |
if (wanted_declaretype == initdeclare_dt || wanted_declaretype == rundeclare_dt) { |
|
1069 |
s4o.print(s4o.indent_spaces); |
|
1070 |
symbol->resource_name->accept(*this); |
|
1071 |
if (wanted_declaretype == initdeclare_dt) { |
|
1072 |
s4o.print(FB_INIT_SUFFIX); |
|
1073 |
s4o.print("();\n"); |
|
1074 |
} |
|
1075 |
else { |
|
1076 |
s4o.print(FB_RUN_SUFFIX); |
|
1077 |
s4o.print("(tick);\n"); |
|
1078 |
} |
|
1079 |
} |
|
1080 |
return NULL; |
|
1081 |
} |
|
1082 |
||
1083 |
void *visit(single_resource_declaration_c *symbol) { |
|
1084 |
if (wanted_declaretype == initprotos_dt || wanted_declaretype == runprotos_dt) { |
|
1085 |
s4o.print(s4o.indent_spaces + "void RESOURCE"); |
|
1086 |
if (wanted_declaretype == initprotos_dt) { |
|
1087 |
s4o.print(FB_INIT_SUFFIX); |
|
1088 |
s4o.print("(void);\n"); |
|
1089 |
} |
|
1090 |
else { |
|
1091 |
s4o.print(FB_RUN_SUFFIX); |
|
1092 |
s4o.print("(int tick);\n"); |
|
1093 |
} |
|
1094 |
} |
|
1095 |
if (wanted_declaretype == initdeclare_dt || wanted_declaretype == rundeclare_dt) { |
|
1096 |
s4o.print(s4o.indent_spaces + "RESOURCE"); |
|
1097 |
if (wanted_declaretype == initdeclare_dt) { |
|
1098 |
s4o.print(FB_INIT_SUFFIX); |
|
1099 |
s4o.print("();\n"); |
|
1100 |
} |
|
1101 |
else { |
|
1102 |
s4o.print(FB_RUN_SUFFIX); |
|
1103 |
s4o.print("(tick);\n"); |
|
1104 |
} |
|
1105 |
} |
|
1106 |
return NULL; |
|
1107 |
} |
|
1108 |
||
1109 |
}; |
|
1110 |
||
1111 |
/***********************************************************************/ |
|
1112 |
/***********************************************************************/ |
|
1113 |
/***********************************************************************/ |
|
1114 |
/***********************************************************************/ |
|
1115 |
/***********************************************************************/ |
|
1116 |
/***********************************************************************/ |
|
1117 |
/***********************************************************************/ |
|
1118 |
/***********************************************************************/ |
|
1119 |
||
1120 |
||
1121 |
class generate_c_resources_c: public generate_c_typedecl_c { |
|
1122 |
||
1123 |
search_var_instance_decl_c *search_config_instance; |
|
1124 |
search_var_instance_decl_c *search_resource_instance; |
|
1125 |
||
1126 |
private: |
|
1127 |
/* The name of the resource curretnly being processed... */ |
|
1128 |
symbol_c *current_resource_name; |
|
1129 |
symbol_c *current_global_vars; |
|
1130 |
||
1131 |
public: |
|
1132 |
generate_c_resources_c(stage4out_c *s4o_ptr, symbol_c *config_scope, symbol_c *resource_scope, unsigned long time) |
|
1133 |
: generate_c_typedecl_c(s4o_ptr) { |
|
1134 |
search_config_instance = new search_var_instance_decl_c(config_scope); |
|
1135 |
search_resource_instance = new search_var_instance_decl_c(resource_scope); |
|
1136 |
common_ticktime = time; |
|
1137 |
current_resource_name = NULL; |
|
1138 |
current_global_vars = NULL; |
|
1139 |
}; |
|
1140 |
virtual ~generate_c_resources_c(void) { |
|
1141 |
delete search_config_instance; |
|
1142 |
delete search_resource_instance; |
|
1143 |
} |
|
1144 |
||
1145 |
typedef enum { |
|
1146 |
declare_dt, |
|
1147 |
init_dt, |
|
1148 |
run_dt |
|
1149 |
} declaretype_t; |
|
1150 |
||
1151 |
declaretype_t wanted_declaretype; |
|
1152 |
||
1153 |
unsigned long common_ticktime; |
|
1154 |
||
1155 |
const char *current_program_name; |
|
1156 |
||
1157 |
typedef enum { |
|
1158 |
assign_at, |
|
1159 |
send_at |
|
1160 |
} assigntype_t; |
|
1161 |
||
1162 |
assigntype_t wanted_assigntype; |
|
1163 |
||
1164 |
/********************************/ |
|
1165 |
/* B 1.7 Configuration elements */ |
|
1166 |
/********************************/ |
|
1167 |
||
1168 |
/* |
|
1169 |
RESOURCE resource_name ON resource_type_name |
|
1170 |
optional_global_var_declarations |
|
1171 |
single_resource_declaration |
|
1172 |
END_RESOURCE |
|
1173 |
*/ |
|
1174 |
// SYM_REF4(resource_declaration_c, resource_name, resource_type_name, global_var_declarations, resource_declaration) |
|
1175 |
void *visit(resource_declaration_c *symbol) { |
|
1176 |
current_resource_name = symbol->resource_name; |
|
1177 |
current_global_vars = symbol->global_var_declarations; |
|
1178 |
||
1179 |
symbol->resource_declaration->accept(*this); |
|
1180 |
||
1181 |
current_resource_name = NULL; |
|
1182 |
current_global_vars = NULL; |
|
1183 |
return NULL; |
|
1184 |
} |
|
1185 |
||
1186 |
/* task_configuration_list program_configuration_list */ |
|
1187 |
// SYM_REF2(single_resource_declaration_c, task_configuration_list, program_configuration_list) |
|
1188 |
void *visit(single_resource_declaration_c *symbol) { |
|
1189 |
bool single_resource = current_resource_name == NULL; |
|
1190 |
if (single_resource) |
|
1191 |
current_resource_name = new identifier_c("RESOURCE"); |
|
1192 |
generate_c_vardecl_c *vardecl; |
|
1193 |
||
1194 |
/* Insert the header... */ |
|
1195 |
s4o.print("/*******************************************/\n"); |
|
1196 |
s4o.print("/* FILE GENERATED BY iec2c */\n"); |
|
1197 |
s4o.print("/* Editing this file is not recommended... */\n"); |
|
1198 |
s4o.print("/*******************************************/\n\n"); |
|
1199 |
s4o.print("#include \"iec_std_lib.h\"\n\n"); |
|
1200 |
||
1201 |
/* (A) resource declaration... */ |
|
1202 |
/* (A.1) resource name in comment */ |
|
1203 |
s4o.print("// RESOURCE "); |
|
1204 |
current_resource_name->accept(*this); |
|
120
74640e3c7f53
Bug with D and L action qualifier and timing management in SFC generated fixed
lbessard
parents:
112
diff
changeset
|
1205 |
s4o.print("\n\n"); |
74640e3c7f53
Bug with D and L action qualifier and timing management in SFC generated fixed
lbessard
parents:
112
diff
changeset
|
1206 |
|
74640e3c7f53
Bug with D and L action qualifier and timing management in SFC generated fixed
lbessard
parents:
112
diff
changeset
|
1207 |
s4o.print("extern int common_ticktime__;\n\n"); |
70 | 1208 |
|
1209 |
/* (A.2) Global variables... */ |
|
1210 |
if (current_global_vars != NULL) { |
|
1211 |
vardecl = new generate_c_vardecl_c(&s4o, |
|
1212 |
generate_c_vardecl_c::local_vf, |
|
138
8b32fd4d0194
Some miscelanous static, extern and pointer status variable changes in C generated code for configuration and resources
etisserant
parents:
121
diff
changeset
|
1213 |
generate_c_vardecl_c::global_vt, |
8b32fd4d0194
Some miscelanous static, extern and pointer status variable changes in C generated code for configuration and resources
etisserant
parents:
121
diff
changeset
|
1214 |
current_resource_name); |
70 | 1215 |
vardecl->print(current_global_vars); |
1216 |
delete vardecl; |
|
120
74640e3c7f53
Bug with D and L action qualifier and timing management in SFC generated fixed
lbessard
parents:
112
diff
changeset
|
1217 |
s4o.print("\n"); |
70 | 1218 |
} |
1219 |
||
1220 |
/* (A.3) POUs inclusion */ |
|
121
9e8ce092e169
Adding support for POU struct definition in POUS.h
lbessard
parents:
120
diff
changeset
|
1221 |
s4o.print("#include \"POUS.h\"\n\n"); |
70 | 1222 |
s4o.print("#include \"POUS.c\"\n\n"); |
1223 |
||
1224 |
/* (A.4) Resource programs declaration... */ |
|
1225 |
wanted_declaretype = declare_dt; |
|
1226 |
symbol->program_configuration_list->accept(*this); |
|
1227 |
s4o.print("\n"); |
|
1228 |
||
1229 |
/* (B) resource initialisation function... */ |
|
1230 |
/* (B.1) initialisation function name... */ |
|
1231 |
s4o.print("void "); |
|
1232 |
current_resource_name->accept(*this); |
|
1233 |
s4o.print(FB_INIT_SUFFIX); |
|
1234 |
s4o.print("(void) {\n"); |
|
1235 |
s4o.indent_right(); |
|
1236 |
||
1237 |
/* (B.2) Global variables initialisations... */ |
|
1238 |
if (current_global_vars != NULL) { |
|
1239 |
s4o.print(s4o.indent_spaces); |
|
1240 |
vardecl = new generate_c_vardecl_c(&s4o, |
|
1241 |
generate_c_vardecl_c::constructorinit_vf, |
|
1242 |
generate_c_vardecl_c::global_vt); |
|
1243 |
vardecl->print(current_global_vars); |
|
1244 |
delete vardecl; |
|
1245 |
} |
|
1246 |
s4o.print("\n"); |
|
1247 |
||
1248 |
/* (B.3) Resource programs initialisations... */ |
|
1249 |
wanted_declaretype = init_dt; |
|
1250 |
symbol->program_configuration_list->accept(*this); |
|
1251 |
||
1252 |
s4o.indent_left(); |
|
1253 |
s4o.print("}\n\n"); |
|
1254 |
||
1255 |
/* (C) Resource run function... */ |
|
1256 |
/* (C.1) Run function name... */ |
|
1257 |
s4o.print("void "); |
|
1258 |
current_resource_name->accept(*this); |
|
1259 |
s4o.print(FB_RUN_SUFFIX); |
|
1260 |
s4o.print("(int tick) {\n"); |
|
1261 |
s4o.indent_right(); |
|
1262 |
||
1263 |
/* (C.2) Task management... */ |
|
1264 |
symbol->task_configuration_list->accept(*this); |
|
1265 |
||
1266 |
/* (C.3) Program run declaration... */ |
|
1267 |
wanted_declaretype = run_dt; |
|
1268 |
symbol->program_configuration_list->accept(*this); |
|
1269 |
||
1270 |
s4o.indent_left(); |
|
1271 |
s4o.print("}\n\n"); |
|
1272 |
||
1273 |
if (single_resource) { |
|
1274 |
delete current_resource_name; |
|
1275 |
current_resource_name = NULL; |
|
1276 |
} |
|
1277 |
return NULL; |
|
1278 |
} |
|
1279 |
||
1280 |
/* PROGRAM [RETAIN | NON_RETAIN] program_name [WITH task_name] ':' program_type_name ['(' prog_conf_elements ')'] */ |
|
1281 |
//SYM_REF6(program_configuration_c, retain_option, program_name, task_name, program_type_name, prog_conf_elements, unused) |
|
1282 |
void *visit(program_configuration_c *symbol) { |
|
1283 |
if (wanted_declaretype == declare_dt) { |
|
1284 |
s4o.print(s4o.indent_spaces); |
|
1285 |
symbol->program_type_name->accept(*this); |
|
1286 |
s4o.print(" "); |
|
138
8b32fd4d0194
Some miscelanous static, extern and pointer status variable changes in C generated code for configuration and resources
etisserant
parents:
121
diff
changeset
|
1287 |
current_resource_name->accept(*this); |
8b32fd4d0194
Some miscelanous static, extern and pointer status variable changes in C generated code for configuration and resources
etisserant
parents:
121
diff
changeset
|
1288 |
s4o.print("__"); |
70 | 1289 |
symbol->program_name->accept(*this); |
138
8b32fd4d0194
Some miscelanous static, extern and pointer status variable changes in C generated code for configuration and resources
etisserant
parents:
121
diff
changeset
|
1290 |
s4o.print(";\n#define "); |
8b32fd4d0194
Some miscelanous static, extern and pointer status variable changes in C generated code for configuration and resources
etisserant
parents:
121
diff
changeset
|
1291 |
symbol->program_name->accept(*this); |
8b32fd4d0194
Some miscelanous static, extern and pointer status variable changes in C generated code for configuration and resources
etisserant
parents:
121
diff
changeset
|
1292 |
s4o.print(" "); |
8b32fd4d0194
Some miscelanous static, extern and pointer status variable changes in C generated code for configuration and resources
etisserant
parents:
121
diff
changeset
|
1293 |
current_resource_name->accept(*this); |
8b32fd4d0194
Some miscelanous static, extern and pointer status variable changes in C generated code for configuration and resources
etisserant
parents:
121
diff
changeset
|
1294 |
s4o.print("__"); |
8b32fd4d0194
Some miscelanous static, extern and pointer status variable changes in C generated code for configuration and resources
etisserant
parents:
121
diff
changeset
|
1295 |
symbol->program_name->accept(*this); |
8b32fd4d0194
Some miscelanous static, extern and pointer status variable changes in C generated code for configuration and resources
etisserant
parents:
121
diff
changeset
|
1296 |
s4o.print("\n"); |
70 | 1297 |
} |
1298 |
if (wanted_declaretype == init_dt) { |
|
1299 |
s4o.print(s4o.indent_spaces); |
|
1300 |
symbol->program_type_name->accept(*this); |
|
1301 |
s4o.print(FB_INIT_SUFFIX); |
|
1302 |
s4o.print("(&"); |
|
1303 |
symbol->program_name->accept(*this); |
|
1304 |
s4o.print(");\n"); |
|
1305 |
} |
|
1306 |
if (wanted_declaretype == run_dt) { |
|
1307 |
current_program_name = ((identifier_c*)(symbol->program_name))->value; |
|
1308 |
if (symbol->task_name != NULL) { |
|
1309 |
s4o.print(s4o.indent_spaces); |
|
1310 |
s4o.print("if (!"); |
|
1311 |
symbol->task_name->accept(*this); |
|
1312 |
s4o.print(") {\n"); |
|
1313 |
s4o.indent_right(); |
|
1314 |
} |
|
1315 |
||
1316 |
wanted_assigntype = assign_at; |
|
1317 |
if (symbol->prog_conf_elements != NULL) |
|
1318 |
symbol->prog_conf_elements->accept(*this); |
|
1319 |
||
1320 |
s4o.print(s4o.indent_spaces); |
|
1321 |
symbol->program_type_name->accept(*this); |
|
1322 |
s4o.print(FB_FUNCTION_SUFFIX); |
|
1323 |
s4o.print("(&"); |
|
1324 |
symbol->program_name->accept(*this); |
|
1325 |
s4o.print(");\n"); |
|
1326 |
||
1327 |
wanted_assigntype = send_at; |
|
1328 |
if (symbol->prog_conf_elements != NULL) |
|
1329 |
symbol->prog_conf_elements->accept(*this); |
|
1330 |
||
1331 |
if (symbol->task_name != NULL) { |
|
1332 |
s4o.indent_left(); |
|
1333 |
s4o.print(s4o.indent_spaces + "}\n"); |
|
1334 |
} |
|
1335 |
} |
|
1336 |
return NULL; |
|
1337 |
} |
|
1338 |
||
1339 |
/* TASK task_name task_initialization */ |
|
1340 |
//SYM_REF2(task_configuration_c, task_name, task_initialization) |
|
1341 |
void *visit(task_configuration_c *symbol) { |
|
1342 |
s4o.print(s4o.indent_spaces + "int "); |
|
1343 |
symbol->task_name->accept(*this); |
|
1344 |
s4o.print(" = "); |
|
1345 |
symbol->task_initialization->accept(*this); |
|
1346 |
s4o.print(";\n"); |
|
1347 |
return NULL; |
|
1348 |
} |
|
1349 |
||
1350 |
/* '(' [SINGLE ASSIGN data_source ','] [INTERVAL ASSIGN data_source ','] PRIORITY ASSIGN integer ')' */ |
|
1351 |
//SYM_REF4(task_initialization_c, single_data_source, interval_data_source, priority_data_source, unused) |
|
1352 |
void *visit(task_initialization_c *symbol) { |
|
1353 |
if (symbol->interval_data_source != NULL) { |
|
1354 |
calculate_time_c calculate_time; |
|
1355 |
symbol->interval_data_source->accept(calculate_time); |
|
1356 |
unsigned long time = calculate_time.get_time(); |
|
1357 |
if (time != 0) { |
|
1358 |
s4o.print("tick % "); |
|
1359 |
s4o.print_integer((int)(time / common_ticktime)); |
|
1360 |
} |
|
1361 |
else |
|
1362 |
s4o.print("1"); |
|
1363 |
} |
|
1364 |
else |
|
1365 |
s4o.print("1"); |
|
1366 |
return NULL; |
|
1367 |
} |
|
1368 |
||
1369 |
/* any_symbolic_variable ASSIGN prog_data_source */ |
|
1370 |
//SYM_REF2(prog_cnxn_assign_c, symbolic_variable, prog_data_source) |
|
1371 |
void *visit(prog_cnxn_assign_c *symbol) { |
|
1372 |
if (wanted_assigntype == assign_at) { |
|
1373 |
symbol_c *var_decl; |
|
1374 |
unsigned int vartype = 0; |
|
1375 |
symbol_c *current_var_reference = ((global_var_reference_c *)(symbol->prog_data_source))->global_var_name; |
|
1376 |
var_decl = search_resource_instance->get_decl(current_var_reference); |
|
1377 |
if (var_decl == NULL) { |
|
1378 |
var_decl = search_config_instance->get_decl(current_var_reference); |
|
1379 |
if (var_decl == NULL) |
|
1380 |
ERROR; |
|
1381 |
else |
|
1382 |
vartype = search_config_instance->get_vartype(); |
|
1383 |
} |
|
1384 |
else |
|
1385 |
vartype = search_resource_instance->get_vartype(); |
|
1386 |
||
1387 |
s4o.print(s4o.indent_spaces + "{extern "); |
|
1388 |
var_decl->accept(*this); |
|
1389 |
s4o.print(" "); |
|
1390 |
symbol->prog_data_source->accept(*this); |
|
1391 |
s4o.print("; "); |
|
1392 |
s4o.print(current_program_name); |
|
1393 |
s4o.print("."); |
|
1394 |
symbol->symbolic_variable->accept(*this); |
|
1395 |
s4o.print(" = "); |
|
1396 |
if (vartype || search_var_instance_decl_c::global_vt) |
|
1397 |
s4o.print("*"); |
|
1398 |
symbol->prog_data_source->accept(*this); |
|
1399 |
s4o.print(";}\n"); |
|
1400 |
} |
|
1401 |
return NULL; |
|
1402 |
} |
|
1403 |
||
1404 |
/* any_symbolic_variable SENDTO data_sink */ |
|
1405 |
//SYM_REF2(prog_cnxn_sendto_c, symbolic_variable, data_sink) |
|
1406 |
void *visit(prog_cnxn_sendto_c *symbol) { |
|
1407 |
if (wanted_assigntype == send_at) { |
|
1408 |
symbol_c *var_decl; |
|
1409 |
unsigned int vartype = 0; |
|
1410 |
symbol_c *current_var_reference = ((global_var_reference_c *)(symbol->data_sink))->global_var_name; |
|
1411 |
var_decl = search_resource_instance->get_decl(current_var_reference); |
|
1412 |
if (var_decl == NULL) { |
|
1413 |
var_decl = search_config_instance->get_decl(current_var_reference); |
|
1414 |
if (var_decl == NULL) |
|
1415 |
ERROR; |
|
1416 |
else |
|
1417 |
vartype = search_config_instance->get_vartype(); |
|
1418 |
} |
|
1419 |
else |
|
1420 |
vartype = search_resource_instance->get_vartype(); |
|
1421 |
||
1422 |
s4o.print(s4o.indent_spaces); |
|
1423 |
s4o.print(s4o.indent_spaces + "{extern "); |
|
1424 |
var_decl->accept(*this); |
|
1425 |
s4o.print(" "); |
|
1426 |
symbol->data_sink->accept(*this); |
|
1427 |
s4o.print("; "); |
|
1428 |
if (vartype || search_var_instance_decl_c::global_vt) |
|
1429 |
s4o.print("*"); |
|
1430 |
symbol->data_sink->accept(*this); |
|
1431 |
s4o.print(" = "); |
|
1432 |
s4o.print(current_program_name); |
|
1433 |
s4o.print("."); |
|
1434 |
symbol->symbolic_variable->accept(*this); |
|
1435 |
s4o.print("};\n"); |
|
1436 |
} |
|
1437 |
return NULL; |
|
1438 |
} |
|
1439 |
||
1440 |
}; |
|
1441 |
||
1442 |
/***********************************************************************/ |
|
1443 |
/***********************************************************************/ |
|
1444 |
/***********************************************************************/ |
|
1445 |
/***********************************************************************/ |
|
1446 |
/***********************************************************************/ |
|
1447 |
/***********************************************************************/ |
|
1448 |
/***********************************************************************/ |
|
1449 |
/***********************************************************************/ |
|
1450 |
||
1451 |
class generate_c_c: public iterator_visitor_c { |
|
1452 |
protected: |
|
1453 |
stage4out_c &s4o; |
|
1454 |
stage4out_c pous_s4o; |
|
121
9e8ce092e169
Adding support for POU struct definition in POUS.h
lbessard
parents:
120
diff
changeset
|
1455 |
stage4out_c pous_incl_s4o; |
70 | 1456 |
stage4out_c located_variables_s4o; |
111 | 1457 |
stage4out_c variables_s4o; |
70 | 1458 |
generate_c_pous_c generate_c_pous; |
111 | 1459 |
|
70 | 1460 |
symbol_c *current_configuration; |
1461 |
||
1462 |
const char *current_name; |
|
1463 |
const char *current_builddir; |
|
1464 |
||
1465 |
unsigned long common_ticktime; |
|
1466 |
||
1467 |
public: |
|
1468 |
generate_c_c(stage4out_c *s4o_ptr, const char *builddir): |
|
1469 |
s4o(*s4o_ptr), |
|
1470 |
pous_s4o(builddir, "POUS", "c"), |
|
121
9e8ce092e169
Adding support for POU struct definition in POUS.h
lbessard
parents:
120
diff
changeset
|
1471 |
pous_incl_s4o(builddir, "POUS", "h"), |
70 | 1472 |
located_variables_s4o(builddir, "LOCATED_VARIABLES","h"), |
112 | 1473 |
variables_s4o(builddir, "VARIABLES","csv"), |
121
9e8ce092e169
Adding support for POU struct definition in POUS.h
lbessard
parents:
120
diff
changeset
|
1474 |
generate_c_pous(&pous_s4o, &pous_incl_s4o) { |
70 | 1475 |
current_builddir = builddir; |
1476 |
current_configuration = NULL; |
|
1477 |
} |
|
1478 |
||
1479 |
~generate_c_c(void) {} |
|
1480 |
||
1481 |
/***************************/ |
|
1482 |
/* B 0 - Programming Model */ |
|
1483 |
/***************************/ |
|
1484 |
void *visit(library_c *symbol) { |
|
121
9e8ce092e169
Adding support for POU struct definition in POUS.h
lbessard
parents:
120
diff
changeset
|
1485 |
pous_incl_s4o.print("#ifndef __POUS_H\n#define __POUS_H\n\n"); |
70 | 1486 |
for(int i = 0; i < symbol->n; i++) { |
1487 |
symbol->elements[i]->accept(*this); |
|
1488 |
} |
|
121
9e8ce092e169
Adding support for POU struct definition in POUS.h
lbessard
parents:
120
diff
changeset
|
1489 |
pous_incl_s4o.print("#endif //__POUS_H\n"); |
9e8ce092e169
Adding support for POU struct definition in POUS.h
lbessard
parents:
120
diff
changeset
|
1490 |
|
9e8ce092e169
Adding support for POU struct definition in POUS.h
lbessard
parents:
120
diff
changeset
|
1491 |
generate_var_list_c generate_var_list(&variables_s4o, symbol); |
9e8ce092e169
Adding support for POU struct definition in POUS.h
lbessard
parents:
120
diff
changeset
|
1492 |
generate_var_list.generate_programs(symbol); |
9e8ce092e169
Adding support for POU struct definition in POUS.h
lbessard
parents:
120
diff
changeset
|
1493 |
generate_var_list.generate_variables(symbol); |
111 | 1494 |
|
98 | 1495 |
generate_location_list_c generate_location_list(&located_variables_s4o); |
1496 |
symbol->accept(generate_location_list); |
|
70 | 1497 |
return NULL; |
1498 |
} |
|
1499 |
||
1500 |
/*************************/ |
|
1501 |
/* B.1 - Common elements */ |
|
1502 |
/*************************/ |
|
1503 |
/*******************************************/ |
|
1504 |
/* B 1.1 - Letters, digits and identifiers */ |
|
1505 |
/*******************************************/ |
|
1506 |
void *visit(identifier_c *symbol) { |
|
1507 |
current_name = symbol->value; |
|
1508 |
return NULL; |
|
1509 |
} |
|
1510 |
||
98 | 1511 |
/********************************/ |
1512 |
/* B 1.3.3 - Derived data types */ |
|
1513 |
/********************************/ |
|
1514 |
/* TYPE type_declaration_list END_TYPE */ |
|
1515 |
void *visit(data_type_declaration_c *symbol) { |
|
1516 |
symbol->accept(generate_c_pous); |
|
1517 |
return NULL; |
|
1518 |
} |
|
1519 |
||
70 | 1520 |
/**************************************/ |
1521 |
/* B.1.5 - Program organization units */ |
|
1522 |
/**************************************/ |
|
1523 |
/***********************/ |
|
1524 |
/* B 1.5.1 - Functions */ |
|
1525 |
/***********************/ |
|
1526 |
void *visit(function_declaration_c *symbol) { |
|
1527 |
symbol->accept(generate_c_pous); |
|
1528 |
return NULL; |
|
1529 |
} |
|
1530 |
||
1531 |
/*****************************/ |
|
1532 |
/* B 1.5.2 - Function Blocks */ |
|
1533 |
/*****************************/ |
|
1534 |
void *visit(function_block_declaration_c *symbol) { |
|
1535 |
symbol->accept(generate_c_pous); |
|
1536 |
return NULL; |
|
1537 |
} |
|
1538 |
||
1539 |
/**********************/ |
|
1540 |
/* B 1.5.3 - Programs */ |
|
1541 |
/**********************/ |
|
1542 |
void *visit(program_declaration_c *symbol) { |
|
1543 |
symbol->accept(generate_c_pous); |
|
1544 |
return NULL; |
|
1545 |
} |
|
1546 |
||
1547 |
||
1548 |
/********************************/ |
|
1549 |
/* B 1.7 Configuration elements */ |
|
1550 |
/********************************/ |
|
1551 |
void *visit(configuration_declaration_c *symbol) { |
|
1552 |
static int configuration_count = 0; |
|
1553 |
||
1554 |
if (configuration_count++) { |
|
1555 |
/* the first configuration is the one we will use!! */ |
|
1556 |
ERROR; |
|
1557 |
} |
|
1558 |
||
1559 |
current_configuration = symbol; |
|
1560 |
||
1561 |
calculate_common_ticktime_c calculate_common_ticktime; |
|
1562 |
symbol->accept(calculate_common_ticktime); |
|
1563 |
common_ticktime = calculate_common_ticktime.get_ticktime(); |
|
1564 |
||
1565 |
symbol->configuration_name->accept(*this); |
|
1566 |
stage4out_c config_s4o(current_builddir, current_name, "c"); |
|
1567 |
generate_c_config_c generate_c_config(&config_s4o); |
|
1568 |
symbol->accept(generate_c_config); |
|
1569 |
||
1570 |
config_s4o.print("int common_ticktime__ = "); |
|
1571 |
config_s4o.print_integer((int)(common_ticktime / 1000000)); |
|
1572 |
config_s4o.print("; /*ms*/\n"); |
|
1573 |
||
1574 |
symbol->resource_declarations->accept(*this); |
|
1575 |
||
1576 |
current_configuration = NULL; |
|
1577 |
||
1578 |
return NULL; |
|
1579 |
} |
|
1580 |
||
1581 |
void *visit(resource_declaration_c *symbol) { |
|
1582 |
symbol->resource_name->accept(*this); |
|
1583 |
stage4out_c resources_s4o(current_builddir, current_name, "c"); |
|
1584 |
generate_c_resources_c generate_c_resources(&resources_s4o, current_configuration, symbol, common_ticktime); |
|
1585 |
symbol->accept(generate_c_resources); |
|
1586 |
return NULL; |
|
1587 |
} |
|
1588 |
||
1589 |
void *visit(single_resource_declaration_c *symbol) { |
|
1590 |
stage4out_c resources_s4o(current_builddir, "RESOURCE", "c"); |
|
1591 |
generate_c_resources_c generate_c_resources(&resources_s4o, current_configuration, symbol, common_ticktime); |
|
1592 |
symbol->accept(generate_c_resources); |
|
1593 |
return NULL; |
|
1594 |
} |
|
1595 |
||
1596 |
}; |
|
1597 |
||
1598 |
/***********************************************************************/ |
|
1599 |
/***********************************************************************/ |
|
1600 |
/***********************************************************************/ |
|
1601 |
/***********************************************************************/ |
|
1602 |
/***********************************************************************/ |
|
1603 |
/***********************************************************************/ |
|
1604 |
/***********************************************************************/ |
|
1605 |
/***********************************************************************/ |
|
1606 |
||
1607 |
||
1608 |
||
1609 |
||
1610 |
visitor_c *new_code_generator(stage4out_c *s4o, const char *builddir) {return new generate_c_c(s4o, builddir);} |
|
1611 |
void delete_code_generator(visitor_c *code_generator) {delete code_generator;} |
|
1612 |
||
1613 |