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