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