author | Mario de Sousa <msousa@fe.up.pt> |
Thu, 14 Apr 2011 17:35:25 +0100 | |
changeset 286 | a4f4990d5c66 |
parent 283 | 214d3fdee596 |
child 318 | 746a357b8747 |
permissions | -rwxr-xr-x |
70 | 1 |
/* |
267
0a1204bcc9af
starting to add proper header file with declaration of standard functions
Mario de Sousa <msousa@fe.up.pt>
parents:
262
diff
changeset
|
2 |
* matiec - a compiler for the programming languages defined in IEC 61131-3 |
70 | 3 |
* |
267
0a1204bcc9af
starting to add proper header file with declaration of standard functions
Mario de Sousa <msousa@fe.up.pt>
parents:
262
diff
changeset
|
4 |
* Copyright (C) 2003-2011 Mario de Sousa (msousa@fe.up.pt) |
279
c0453b7f99df
Re-generated std lib related code, with updated headers, updated all forgotten headers
Edouard Tisserant
parents:
272
diff
changeset
|
5 |
* Copyright (C) 2007-2011 Laurent Bessard and Edouard Tisserant |
70 | 6 |
* |
267
0a1204bcc9af
starting to add proper header file with declaration of standard functions
Mario de Sousa <msousa@fe.up.pt>
parents:
262
diff
changeset
|
7 |
* This program is free software: you can redistribute it and/or modify |
0a1204bcc9af
starting to add proper header file with declaration of standard functions
Mario de Sousa <msousa@fe.up.pt>
parents:
262
diff
changeset
|
8 |
* it under the terms of the GNU General Public License as published by |
0a1204bcc9af
starting to add proper header file with declaration of standard functions
Mario de Sousa <msousa@fe.up.pt>
parents:
262
diff
changeset
|
9 |
* the Free Software Foundation, either version 3 of the License, or |
0a1204bcc9af
starting to add proper header file with declaration of standard functions
Mario de Sousa <msousa@fe.up.pt>
parents:
262
diff
changeset
|
10 |
* (at your option) any later version. |
0a1204bcc9af
starting to add proper header file with declaration of standard functions
Mario de Sousa <msousa@fe.up.pt>
parents:
262
diff
changeset
|
11 |
* |
0a1204bcc9af
starting to add proper header file with declaration of standard functions
Mario de Sousa <msousa@fe.up.pt>
parents:
262
diff
changeset
|
12 |
* This program is distributed in the hope that it will be useful, |
0a1204bcc9af
starting to add proper header file with declaration of standard functions
Mario de Sousa <msousa@fe.up.pt>
parents:
262
diff
changeset
|
13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
0a1204bcc9af
starting to add proper header file with declaration of standard functions
Mario de Sousa <msousa@fe.up.pt>
parents:
262
diff
changeset
|
14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
0a1204bcc9af
starting to add proper header file with declaration of standard functions
Mario de Sousa <msousa@fe.up.pt>
parents:
262
diff
changeset
|
15 |
* GNU General Public License for more details. |
0a1204bcc9af
starting to add proper header file with declaration of standard functions
Mario de Sousa <msousa@fe.up.pt>
parents:
262
diff
changeset
|
16 |
* |
0a1204bcc9af
starting to add proper header file with declaration of standard functions
Mario de Sousa <msousa@fe.up.pt>
parents:
262
diff
changeset
|
17 |
* You should have received a copy of the GNU General Public License |
0a1204bcc9af
starting to add proper header file with declaration of standard functions
Mario de Sousa <msousa@fe.up.pt>
parents:
262
diff
changeset
|
18 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
0a1204bcc9af
starting to add proper header file with declaration of standard functions
Mario de Sousa <msousa@fe.up.pt>
parents:
262
diff
changeset
|
19 |
* |
70 | 20 |
* |
21 |
* This code is made available on the understanding that it will not be |
|
22 |
* used in safety-critical situations without a full and competent review. |
|
23 |
*/ |
|
24 |
||
25 |
#include <string> |
|
26 |
#include <iostream> |
|
27 |
#include <sstream> |
|
28 |
#include <typeinfo> |
|
111 | 29 |
#include <list> |
139
668a54686827
added missing includes on some platform (gentoo/gcc-4.3.1)
etisserant
parents:
138
diff
changeset
|
30 |
#include <strings.h> |
70 | 31 |
|
32 |
#include "../../util/symtable.hh" |
|
33 |
#include "../../util/dsymtable.hh" |
|
34 |
#include "../../absyntax/visitor.hh" |
|
181
38d6eb056260
Moving absyntax utility files out from stage4/generate_c
mario
parents:
178
diff
changeset
|
35 |
#include "../../absyntax_utils/absyntax_utils.hh" |
70 | 36 |
|
37 |
#include "../stage4.hh" |
|
38 |
||
39 |
//#define DEBUG |
|
40 |
#ifdef DEBUG |
|
41 |
#define TRACE(classname) printf("\n____%s____\n",classname); |
|
42 |
#else |
|
43 |
#define TRACE(classname) |
|
44 |
#endif |
|
45 |
||
46 |
#define ERROR error_exit(__FILE__,__LINE__) |
|
47 |
/* function defined in main.cc */ |
|
48 |
extern void error_exit(const char *file_name, int line_no); |
|
49 |
||
50 |
/***********************************************************************/ |
|
51 |
||
52 |
/* Unlike Programs and Configurations which get mapped onto C++ classes, |
|
53 |
* Function Blocks are mapped onto a C structure containing the variables, and |
|
54 |
* a C function containing the code in the FB's body. This is to allow direct allocation |
|
55 |
* of a FB variable (which is really an instance of the C data structure) to |
|
56 |
* a member of a union variable (note that classes with constructors cannot |
|
57 |
* be mebers of a union), which is done in IL when loading a FB onto IL's |
|
58 |
* default variable. |
|
59 |
* |
|
60 |
* So as not to clash the names of the C data structure and the C function, |
|
61 |
* the C structure is given a name identical to that of the FB name, whereas |
|
62 |
* the name of the function is the FB name with a constant string appended. |
|
63 |
* The value of that constant string which is appended is defined in the following |
|
64 |
* constant. |
|
65 |
* In order not to clash with any variable in the IL and ST source codem the |
|
66 |
* following constant should contain a double underscore, which is not allowed |
|
67 |
* in IL and ST. |
|
68 |
* |
|
69 |
* e.g.: FUNTION_BLOCK TEST |
|
70 |
* is mapped onto a TEST data structure, and a TEST_body__ function. |
|
71 |
*/ |
|
72 |
||
73 |
#define FB_FUNCTION_SUFFIX "_body__" |
|
74 |
||
75 |
/* Idem as body, but for initializer FB function */ |
|
76 |
#define FB_INIT_SUFFIX "_init__" |
|
77 |
||
78 |
/* Idem as body, but for run CONFIG and RESOURCE function */ |
|
79 |
#define FB_RUN_SUFFIX "_run__" |
|
80 |
||
81 |
/* The FB body function is passed as the only parameter a pointer to the FB data |
|
82 |
* structure instance. The name of this parameter is given by the following constant. |
|
83 |
* In order not to clash with any variable in the IL and ST source codem the |
|
84 |
* following constant should contain a double underscore, which is not allowed |
|
85 |
* in IL and ST. |
|
86 |
* |
|
87 |
* e.g.: the body of FUNTION_BLOCK TEST |
|
88 |
* is mapped onto the C function |
|
89 |
* TEST_body__(TEST *data__) |
|
90 |
*/ |
|
91 |
||
92 |
#define FB_FUNCTION_PARAM "data__" |
|
93 |
||
94 |
||
95 |
#define SFC_STEP_ACTION_PREFIX "__SFC_" |
|
96 |
||
217
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
210
diff
changeset
|
97 |
|
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
210
diff
changeset
|
98 |
/* Variable declaration symbol for accessor macros */ |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
210
diff
changeset
|
99 |
#define DECLARE_VAR "__DECLARE_VAR" |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
210
diff
changeset
|
100 |
#define DECLARE_GLOBAL "__DECLARE_GLOBAL" |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
210
diff
changeset
|
101 |
#define DECLARE_GLOBAL_LOCATION "__DECLARE_GLOBAL_LOCATION" |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
210
diff
changeset
|
102 |
#define DECLARE_GLOBAL_LOCATED "__DECLARE_GLOBAL_LOCATED" |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
210
diff
changeset
|
103 |
#define DECLARE_EXTERNAL "__DECLARE_EXTERNAL" |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
210
diff
changeset
|
104 |
#define DECLARE_LOCATED "__DECLARE_LOCATED" |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
210
diff
changeset
|
105 |
|
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
210
diff
changeset
|
106 |
/* Variable declaration symbol for accessor macros */ |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
210
diff
changeset
|
107 |
#define INIT_VAR "__INIT_VAR" |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
210
diff
changeset
|
108 |
#define INIT_GLOBAL "__INIT_GLOBAL" |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
210
diff
changeset
|
109 |
#define INIT_GLOBAL_LOCATED "__INIT_GLOBAL_LOCATED" |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
210
diff
changeset
|
110 |
#define INIT_EXTERNAL "__INIT_EXTERNAL" |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
210
diff
changeset
|
111 |
#define INIT_LOCATED "__INIT_LOCATED" |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
210
diff
changeset
|
112 |
#define INIT_LOCATED_VALUE "__INIT_LOCATED_VALUE" |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
210
diff
changeset
|
113 |
|
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
210
diff
changeset
|
114 |
|
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
210
diff
changeset
|
115 |
/* Variable getter symbol for accessor macros */ |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
210
diff
changeset
|
116 |
#define GET_VAR "__GET_VAR" |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
210
diff
changeset
|
117 |
#define GET_EXTERNAL "__GET_EXTERNAL" |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
210
diff
changeset
|
118 |
#define GET_LOCATED "__GET_LOCATED" |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
210
diff
changeset
|
119 |
#define GET_VAR_BY_REF "__GET_VAR_BY_REF" |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
210
diff
changeset
|
120 |
#define GET_EXTERNAL_BY_REF "__GET_EXTERNAL_BY_REF" |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
210
diff
changeset
|
121 |
#define GET_LOCATED_BY_REF "__GET_LOCATED_BY_REF" |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
210
diff
changeset
|
122 |
|
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
210
diff
changeset
|
123 |
/* Variable setter symbol for accessor macros */ |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
210
diff
changeset
|
124 |
#define SET_VAR "__SET_VAR" |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
210
diff
changeset
|
125 |
#define SET_EXTERNAL "__SET_EXTERNAL" |
231
b8527b0abe75
Adding support for forcing global without perturbation from setting external
laurent
parents:
221
diff
changeset
|
126 |
#define SET_COMPLEX_EXTERNAL "__SET_COMPLEX_EXTERNAL" |
217
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
210
diff
changeset
|
127 |
#define SET_LOCATED "__SET_LOCATED" |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
210
diff
changeset
|
128 |
|
70 | 129 |
|
130 |
/* Generate a name for a temporary variable. |
|
131 |
* Each new name generated is appended a different number, |
|
132 |
* starting off from 0. |
|
133 |
* After calling reset(), the names will start off again from 0. |
|
134 |
*/ |
|
135 |
#define VAR_LEADER "__" |
|
136 |
#define TEMP_VAR VAR_LEADER "TMP_" |
|
137 |
#define SOURCE_VAR VAR_LEADER "SRC_" |
|
138 |
||
283
214d3fdee596
fixing C code generation of return_statement_c and exit_statement_c
Mario de Sousa <msousa@fe.up.pt>
parents:
279
diff
changeset
|
139 |
/* please see the comment before the RET_operator_c visitor for details... */ |
214d3fdee596
fixing C code generation of return_statement_c and exit_statement_c
Mario de Sousa <msousa@fe.up.pt>
parents:
279
diff
changeset
|
140 |
#define END_LABEL VAR_LEADER "end" |
214d3fdee596
fixing C code generation of return_statement_c and exit_statement_c
Mario de Sousa <msousa@fe.up.pt>
parents:
279
diff
changeset
|
141 |
|
214d3fdee596
fixing C code generation of return_statement_c and exit_statement_c
Mario de Sousa <msousa@fe.up.pt>
parents:
279
diff
changeset
|
142 |
|
217
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
210
diff
changeset
|
143 |
/***********************************************************************/ |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
210
diff
changeset
|
144 |
/***********************************************************************/ |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
210
diff
changeset
|
145 |
/***********************************************************************/ |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
210
diff
changeset
|
146 |
/***********************************************************************/ |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
210
diff
changeset
|
147 |
|
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
210
diff
changeset
|
148 |
#include "generate_c_base.cc" |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
210
diff
changeset
|
149 |
#include "generate_c_typedecl.cc" |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
210
diff
changeset
|
150 |
#include "generate_c_sfcdecl.cc" |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
210
diff
changeset
|
151 |
#include "generate_c_vardecl.cc" |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
210
diff
changeset
|
152 |
#include "generate_c_configbody.cc" |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
210
diff
changeset
|
153 |
#include "generate_location_list.cc" |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
210
diff
changeset
|
154 |
#include "generate_var_list.cc" |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
210
diff
changeset
|
155 |
|
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
210
diff
changeset
|
156 |
/***********************************************************************/ |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
210
diff
changeset
|
157 |
/***********************************************************************/ |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
210
diff
changeset
|
158 |
/***********************************************************************/ |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
210
diff
changeset
|
159 |
/***********************************************************************/ |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
210
diff
changeset
|
160 |
|
70 | 161 |
#include "generate_c_st.cc" |
162 |
#include "generate_c_il.cc" |
|
217
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
210
diff
changeset
|
163 |
#include "generate_c_inlinefcall.cc" |
70 | 164 |
|
165 |
#include "generate_c.hh" |
|
166 |
||
167 |
/***********************************************************************/ |
|
168 |
/***********************************************************************/ |
|
169 |
/***********************************************************************/ |
|
170 |
/***********************************************************************/ |
|
171 |
||
172 |
#define MILLISECOND 1000000 |
|
173 |
#define SECOND 1000 * MILLISECOND |
|
174 |
||
175 |
/* A helper class that knows how to generate code for both the IL and ST languages... */ |
|
176 |
class calculate_time_c: public iterator_visitor_c { |
|
177 |
private: |
|
210
8387cac2aba6
Adding support for forcing tick count to return to zero as the same time than all tasks firing are synchronized
laurent
parents:
202
diff
changeset
|
178 |
unsigned long long time; |
70 | 179 |
float current_value; |
180 |
||
181 |
public: |
|
182 |
calculate_time_c(void){time = 0;}; |
|
183 |
||
210
8387cac2aba6
Adding support for forcing tick count to return to zero as the same time than all tasks firing are synchronized
laurent
parents:
202
diff
changeset
|
184 |
unsigned long long get_time(void) {return time;}; |
70 | 185 |
|
186 |
void *get_integer_value(token_c *token) { |
|
187 |
std::string str = ""; |
|
188 |
for (unsigned int i = 0; i < strlen(token->value); i++) |
|
189 |
if (token->value[i] != '_') |
|
190 |
str += token->value[i]; |
|
191 |
current_value = atof(str.c_str()); |
|
192 |
return NULL; |
|
193 |
} |
|
194 |
||
195 |
void *get_float_value(token_c *token) { |
|
196 |
current_value = atof(token->value); |
|
197 |
return NULL; |
|
198 |
} |
|
199 |
||
200 |
/******************************/ |
|
201 |
/* B 1.2.1 - Numeric Literals */ |
|
202 |
/******************************/ |
|
203 |
||
204 |
void *visit(integer_c *symbol) {return get_integer_value(symbol);} |
|
205 |
||
206 |
/************************/ |
|
207 |
/* B 1.2.3.1 - Duration */ |
|
208 |
/************************/ |
|
209 |
||
210 |
/* SYM_REF2(duration_c, neg, interval) */ |
|
211 |
void *visit(duration_c *symbol) { |
|
212 |
if (symbol->neg != NULL) |
|
213 |
ERROR; |
|
214 |
symbol->interval->accept(*this); |
|
215 |
return NULL; |
|
216 |
} |
|
217 |
||
218 |
/* SYM_TOKEN(fixed_point_c) */ |
|
219 |
void *visit(fixed_point_c *symbol) {return get_float_value(symbol);} |
|
220 |
||
221 |
/* SYM_REF2(days_c, days, hours) */ |
|
222 |
void *visit(days_c *symbol) { |
|
223 |
if (symbol->hours) |
|
224 |
symbol->hours->accept(*this); |
|
225 |
symbol->days->accept(*this); |
|
210
8387cac2aba6
Adding support for forcing tick count to return to zero as the same time than all tasks firing are synchronized
laurent
parents:
202
diff
changeset
|
226 |
time += (unsigned long long)(current_value * 24 * 3600 * SECOND); |
70 | 227 |
return NULL; |
228 |
} |
|
229 |
||
230 |
/* SYM_REF2(hours_c, hours, minutes) */ |
|
231 |
void *visit(hours_c *symbol) { |
|
232 |
if (symbol->minutes) |
|
233 |
symbol->minutes->accept(*this); |
|
234 |
symbol->hours->accept(*this); |
|
210
8387cac2aba6
Adding support for forcing tick count to return to zero as the same time than all tasks firing are synchronized
laurent
parents:
202
diff
changeset
|
235 |
time += (unsigned long long)(current_value * 3600 * SECOND); |
70 | 236 |
return NULL; |
237 |
} |
|
238 |
||
239 |
/* SYM_REF2(minutes_c, minutes, seconds) */ |
|
240 |
void *visit(minutes_c *symbol) { |
|
241 |
if (symbol->seconds) |
|
242 |
symbol->seconds->accept(*this); |
|
243 |
symbol->minutes->accept(*this); |
|
210
8387cac2aba6
Adding support for forcing tick count to return to zero as the same time than all tasks firing are synchronized
laurent
parents:
202
diff
changeset
|
244 |
time += (unsigned long long)(current_value * 60 * SECOND); |
70 | 245 |
return NULL; |
246 |
} |
|
247 |
||
248 |
/* SYM_REF2(seconds_c, seconds, milliseconds) */ |
|
249 |
void *visit(seconds_c *symbol) { |
|
250 |
if (symbol->milliseconds) |
|
251 |
symbol->milliseconds->accept(*this); |
|
252 |
symbol->seconds->accept(*this); |
|
210
8387cac2aba6
Adding support for forcing tick count to return to zero as the same time than all tasks firing are synchronized
laurent
parents:
202
diff
changeset
|
253 |
time += (unsigned long long)(current_value * SECOND); |
70 | 254 |
return NULL; |
255 |
} |
|
256 |
||
257 |
/* SYM_REF2(milliseconds_c, milliseconds, unused) */ |
|
258 |
void *visit(milliseconds_c *symbol) { |
|
259 |
symbol->milliseconds->accept(*this); |
|
210
8387cac2aba6
Adding support for forcing tick count to return to zero as the same time than all tasks firing are synchronized
laurent
parents:
202
diff
changeset
|
260 |
time += (unsigned long long)(current_value * MILLISECOND); |
70 | 261 |
return NULL; |
262 |
} |
|
263 |
}; |
|
264 |
||
265 |
/***********************************************************************/ |
|
266 |
/***********************************************************************/ |
|
267 |
/***********************************************************************/ |
|
268 |
/***********************************************************************/ |
|
269 |
||
270 |
class calculate_common_ticktime_c: public iterator_visitor_c { |
|
271 |
private: |
|
210
8387cac2aba6
Adding support for forcing tick count to return to zero as the same time than all tasks firing are synchronized
laurent
parents:
202
diff
changeset
|
272 |
unsigned long long common_ticktime; |
8387cac2aba6
Adding support for forcing tick count to return to zero as the same time than all tasks firing are synchronized
laurent
parents:
202
diff
changeset
|
273 |
unsigned long long least_common_ticktime; |
70 | 274 |
|
275 |
public: |
|
210
8387cac2aba6
Adding support for forcing tick count to return to zero as the same time than all tasks firing are synchronized
laurent
parents:
202
diff
changeset
|
276 |
calculate_common_ticktime_c(void){ |
8387cac2aba6
Adding support for forcing tick count to return to zero as the same time than all tasks firing are synchronized
laurent
parents:
202
diff
changeset
|
277 |
common_ticktime = 0; |
8387cac2aba6
Adding support for forcing tick count to return to zero as the same time than all tasks firing are synchronized
laurent
parents:
202
diff
changeset
|
278 |
least_common_ticktime = 0; |
8387cac2aba6
Adding support for forcing tick count to return to zero as the same time than all tasks firing are synchronized
laurent
parents:
202
diff
changeset
|
279 |
} |
8387cac2aba6
Adding support for forcing tick count to return to zero as the same time than all tasks firing are synchronized
laurent
parents:
202
diff
changeset
|
280 |
|
8387cac2aba6
Adding support for forcing tick count to return to zero as the same time than all tasks firing are synchronized
laurent
parents:
202
diff
changeset
|
281 |
unsigned long long euclide(unsigned long long a, unsigned long long b) { |
8387cac2aba6
Adding support for forcing tick count to return to zero as the same time than all tasks firing are synchronized
laurent
parents:
202
diff
changeset
|
282 |
unsigned long long c = a % b; |
70 | 283 |
if (c == 0) |
284 |
return b; |
|
285 |
else |
|
286 |
return euclide(b, c); |
|
287 |
} |
|
288 |
||
210
8387cac2aba6
Adding support for forcing tick count to return to zero as the same time than all tasks firing are synchronized
laurent
parents:
202
diff
changeset
|
289 |
void update_ticktime(unsigned long long time) { |
70 | 290 |
if (common_ticktime == 0) |
291 |
common_ticktime = time; |
|
292 |
else if (time > common_ticktime) |
|
293 |
common_ticktime = euclide(time, common_ticktime); |
|
294 |
else |
|
295 |
common_ticktime = euclide(common_ticktime, time); |
|
210
8387cac2aba6
Adding support for forcing tick count to return to zero as the same time than all tasks firing are synchronized
laurent
parents:
202
diff
changeset
|
296 |
if (least_common_ticktime == 0) |
8387cac2aba6
Adding support for forcing tick count to return to zero as the same time than all tasks firing are synchronized
laurent
parents:
202
diff
changeset
|
297 |
least_common_ticktime = time; |
8387cac2aba6
Adding support for forcing tick count to return to zero as the same time than all tasks firing are synchronized
laurent
parents:
202
diff
changeset
|
298 |
else |
8387cac2aba6
Adding support for forcing tick count to return to zero as the same time than all tasks firing are synchronized
laurent
parents:
202
diff
changeset
|
299 |
least_common_ticktime = (least_common_ticktime * time) / common_ticktime; |
8387cac2aba6
Adding support for forcing tick count to return to zero as the same time than all tasks firing are synchronized
laurent
parents:
202
diff
changeset
|
300 |
} |
8387cac2aba6
Adding support for forcing tick count to return to zero as the same time than all tasks firing are synchronized
laurent
parents:
202
diff
changeset
|
301 |
|
8387cac2aba6
Adding support for forcing tick count to return to zero as the same time than all tasks firing are synchronized
laurent
parents:
202
diff
changeset
|
302 |
unsigned long long get_common_ticktime(void) { |
70 | 303 |
return common_ticktime; |
304 |
} |
|
305 |
||
210
8387cac2aba6
Adding support for forcing tick count to return to zero as the same time than all tasks firing are synchronized
laurent
parents:
202
diff
changeset
|
306 |
unsigned long get_greatest_tick_count(void) { |
8387cac2aba6
Adding support for forcing tick count to return to zero as the same time than all tasks firing are synchronized
laurent
parents:
202
diff
changeset
|
307 |
unsigned long long least_common_tick = least_common_ticktime / common_ticktime; |
8387cac2aba6
Adding support for forcing tick count to return to zero as the same time than all tasks firing are synchronized
laurent
parents:
202
diff
changeset
|
308 |
if (least_common_tick >> 32) |
8387cac2aba6
Adding support for forcing tick count to return to zero as the same time than all tasks firing are synchronized
laurent
parents:
202
diff
changeset
|
309 |
ERROR; |
237 | 310 |
return (unsigned long)(~(((unsigned long)-1) % (unsigned long)least_common_tick) + 1); |
210
8387cac2aba6
Adding support for forcing tick count to return to zero as the same time than all tasks firing are synchronized
laurent
parents:
202
diff
changeset
|
311 |
} |
8387cac2aba6
Adding support for forcing tick count to return to zero as the same time than all tasks firing are synchronized
laurent
parents:
202
diff
changeset
|
312 |
|
70 | 313 |
/* TASK task_name task_initialization */ |
314 |
//SYM_REF2(task_configuration_c, task_name, task_initialization) |
|
315 |
void *visit(task_initialization_c *symbol) { |
|
316 |
calculate_time_c calculate_time; |
|
210
8387cac2aba6
Adding support for forcing tick count to return to zero as the same time than all tasks firing are synchronized
laurent
parents:
202
diff
changeset
|
317 |
unsigned long long time = 0; |
70 | 318 |
if (symbol->interval_data_source != NULL) { |
319 |
symbol->interval_data_source->accept(calculate_time); |
|
320 |
time = calculate_time.get_time(); |
|
321 |
} |
|
322 |
if (time > 0) |
|
323 |
update_ticktime(time); |
|
324 |
return NULL; |
|
325 |
} |
|
326 |
}; |
|
327 |
||
328 |
/***********************************************************************/ |
|
329 |
/***********************************************************************/ |
|
330 |
/***********************************************************************/ |
|
331 |
/***********************************************************************/ |
|
332 |
||
217
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
210
diff
changeset
|
333 |
/* A helper class that knows how to generate code for the SFC, IL and ST languages... */ |
70 | 334 |
class generate_c_SFC_IL_ST_c: public null_visitor_c { |
335 |
private: |
|
336 |
stage4out_c *s4o_ptr; |
|
337 |
symbol_c *scope; |
|
217
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
210
diff
changeset
|
338 |
symbol_c *fbname; |
70 | 339 |
const char *variable_prefix; |
340 |
||
341 |
public: |
|
217
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
210
diff
changeset
|
342 |
generate_c_SFC_IL_ST_c(stage4out_c *s4o_ptr, symbol_c *name, symbol_c *scope, const char *variable_prefix = NULL); |
267
0a1204bcc9af
starting to add proper header file with declaration of standard functions
Mario de Sousa <msousa@fe.up.pt>
parents:
262
diff
changeset
|
343 |
|
0a1204bcc9af
starting to add proper header file with declaration of standard functions
Mario de Sousa <msousa@fe.up.pt>
parents:
262
diff
changeset
|
344 |
/********************/ |
0a1204bcc9af
starting to add proper header file with declaration of standard functions
Mario de Sousa <msousa@fe.up.pt>
parents:
262
diff
changeset
|
345 |
/* 2.1.6 - Pragmas */ |
0a1204bcc9af
starting to add proper header file with declaration of standard functions
Mario de Sousa <msousa@fe.up.pt>
parents:
262
diff
changeset
|
346 |
/********************/ |
0a1204bcc9af
starting to add proper header file with declaration of standard functions
Mario de Sousa <msousa@fe.up.pt>
parents:
262
diff
changeset
|
347 |
void *visit(enable_code_generation_pragma_c * symbol) {s4o_ptr->enable_output(); return NULL;} |
0a1204bcc9af
starting to add proper header file with declaration of standard functions
Mario de Sousa <msousa@fe.up.pt>
parents:
262
diff
changeset
|
348 |
void *visit(disable_code_generation_pragma_c * symbol) {s4o_ptr->disable_output(); return NULL;} |
0a1204bcc9af
starting to add proper header file with declaration of standard functions
Mario de Sousa <msousa@fe.up.pt>
parents:
262
diff
changeset
|
349 |
|
70 | 350 |
/*********************************************/ |
351 |
/* B.1.6 Sequential function chart elements */ |
|
352 |
/*********************************************/ |
|
353 |
/*| sequential_function_chart sfc_network*/ |
|
354 |
void *visit(sequential_function_chart_c * symbol); |
|
355 |
||
356 |
/****************************************/ |
|
357 |
/* B.2 - Language IL (Instruction List) */ |
|
358 |
/****************************************/ |
|
359 |
||
360 |
/***********************************/ |
|
361 |
/* B 2.1 Instructions and Operands */ |
|
362 |
/***********************************/ |
|
363 |
/*| instruction_list il_instruction */ |
|
364 |
void *visit(instruction_list_c *symbol); |
|
365 |
||
366 |
/* Remainder implemented in generate_c_il_c... */ |
|
367 |
||
368 |
/***************************************/ |
|
369 |
/* B.3 - Language ST (Structured Text) */ |
|
370 |
/***************************************/ |
|
371 |
/***********************/ |
|
372 |
/* B 3.1 - Expressions */ |
|
373 |
/***********************/ |
|
374 |
/* Implemented in generate_c_st_c */ |
|
375 |
||
376 |
/********************/ |
|
377 |
/* B 3.2 Statements */ |
|
378 |
/********************/ |
|
379 |
void *visit(statement_list_c *symbol); |
|
380 |
||
381 |
/* Remainder implemented in generate_c_st_c... */ |
|
382 |
}; |
|
383 |
||
384 |
#include "generate_c_sfc.cc" |
|
385 |
||
217
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
210
diff
changeset
|
386 |
generate_c_SFC_IL_ST_c::generate_c_SFC_IL_ST_c(stage4out_c *s4o_ptr, symbol_c *name, symbol_c *scope, const char *variable_prefix) { |
70 | 387 |
if (NULL == scope) ERROR; |
388 |
this->s4o_ptr = s4o_ptr; |
|
389 |
this->scope = scope; |
|
217
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
210
diff
changeset
|
390 |
this->fbname = name; |
70 | 391 |
this->variable_prefix = variable_prefix; |
392 |
} |
|
393 |
||
394 |
void *generate_c_SFC_IL_ST_c::visit(sequential_function_chart_c * symbol) { |
|
217
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
210
diff
changeset
|
395 |
generate_c_sfc_c generate_c_sfc(s4o_ptr, fbname, scope, variable_prefix); |
70 | 396 |
generate_c_sfc.generate(symbol); |
397 |
return NULL; |
|
398 |
} |
|
399 |
||
400 |
void *generate_c_SFC_IL_ST_c::visit(instruction_list_c *symbol) { |
|
217
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
210
diff
changeset
|
401 |
generate_c_il_c generate_c_il(s4o_ptr, fbname, scope, variable_prefix); |
70 | 402 |
generate_c_il.generate(symbol); |
403 |
return NULL; |
|
404 |
} |
|
405 |
||
406 |
void *generate_c_SFC_IL_ST_c::visit(statement_list_c *symbol) { |
|
217
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
210
diff
changeset
|
407 |
generate_c_st_c generate_c_st(s4o_ptr, fbname, scope, variable_prefix); |
70 | 408 |
generate_c_st.generate(symbol); |
409 |
return NULL; |
|
410 |
} |
|
411 |
||
412 |
||
413 |
||
414 |
||
415 |
/***********************************************************************/ |
|
416 |
/***********************************************************************/ |
|
417 |
/***********************************************************************/ |
|
418 |
/***********************************************************************/ |
|
419 |
/***********************************************************************/ |
|
420 |
||
421 |
||
422 |
class generate_c_pous_c: public generate_c_typedecl_c { |
|
267
0a1204bcc9af
starting to add proper header file with declaration of standard functions
Mario de Sousa <msousa@fe.up.pt>
parents:
262
diff
changeset
|
423 |
private: |
0a1204bcc9af
starting to add proper header file with declaration of standard functions
Mario de Sousa <msousa@fe.up.pt>
parents:
262
diff
changeset
|
424 |
stage4out_c *s4o_ptr; |
0a1204bcc9af
starting to add proper header file with declaration of standard functions
Mario de Sousa <msousa@fe.up.pt>
parents:
262
diff
changeset
|
425 |
|
70 | 426 |
public: |
121
9e8ce092e169
Adding support for POU struct definition in POUS.h
lbessard
parents:
120
diff
changeset
|
427 |
generate_c_pous_c(stage4out_c *s4o_ptr, stage4out_c *s4o_incl_ptr) |
267
0a1204bcc9af
starting to add proper header file with declaration of standard functions
Mario de Sousa <msousa@fe.up.pt>
parents:
262
diff
changeset
|
428 |
: generate_c_typedecl_c(s4o_ptr, s4o_incl_ptr) { |
0a1204bcc9af
starting to add proper header file with declaration of standard functions
Mario de Sousa <msousa@fe.up.pt>
parents:
262
diff
changeset
|
429 |
generate_c_pous_c::s4o_ptr = s4o_ptr; |
0a1204bcc9af
starting to add proper header file with declaration of standard functions
Mario de Sousa <msousa@fe.up.pt>
parents:
262
diff
changeset
|
430 |
}; |
70 | 431 |
virtual ~generate_c_pous_c(void) {} |
432 |
||
283
214d3fdee596
fixing C code generation of return_statement_c and exit_statement_c
Mario de Sousa <msousa@fe.up.pt>
parents:
279
diff
changeset
|
433 |
private: |
214d3fdee596
fixing C code generation of return_statement_c and exit_statement_c
Mario de Sousa <msousa@fe.up.pt>
parents:
279
diff
changeset
|
434 |
void print_end_of_block_label(void) { |
214d3fdee596
fixing C code generation of return_statement_c and exit_statement_c
Mario de Sousa <msousa@fe.up.pt>
parents:
279
diff
changeset
|
435 |
/* Print and __end label for return statements! |
214d3fdee596
fixing C code generation of return_statement_c and exit_statement_c
Mario de Sousa <msousa@fe.up.pt>
parents:
279
diff
changeset
|
436 |
* If label is not used by at least one goto, compiler will generate a warning. |
214d3fdee596
fixing C code generation of return_statement_c and exit_statement_c
Mario de Sousa <msousa@fe.up.pt>
parents:
279
diff
changeset
|
437 |
* To work around this we introduce the useless goto. |
214d3fdee596
fixing C code generation of return_statement_c and exit_statement_c
Mario de Sousa <msousa@fe.up.pt>
parents:
279
diff
changeset
|
438 |
*/ |
214d3fdee596
fixing C code generation of return_statement_c and exit_statement_c
Mario de Sousa <msousa@fe.up.pt>
parents:
279
diff
changeset
|
439 |
s4o.print("\n"); |
214d3fdee596
fixing C code generation of return_statement_c and exit_statement_c
Mario de Sousa <msousa@fe.up.pt>
parents:
279
diff
changeset
|
440 |
s4o.print(s4o.indent_spaces); |
214d3fdee596
fixing C code generation of return_statement_c and exit_statement_c
Mario de Sousa <msousa@fe.up.pt>
parents:
279
diff
changeset
|
441 |
s4o.print("/* to humour the compiler, we insert a goto */\n"); |
214d3fdee596
fixing C code generation of return_statement_c and exit_statement_c
Mario de Sousa <msousa@fe.up.pt>
parents:
279
diff
changeset
|
442 |
s4o.print(s4o.indent_spaces); |
214d3fdee596
fixing C code generation of return_statement_c and exit_statement_c
Mario de Sousa <msousa@fe.up.pt>
parents:
279
diff
changeset
|
443 |
s4o.print("goto "); |
214d3fdee596
fixing C code generation of return_statement_c and exit_statement_c
Mario de Sousa <msousa@fe.up.pt>
parents:
279
diff
changeset
|
444 |
s4o.print(END_LABEL); |
214d3fdee596
fixing C code generation of return_statement_c and exit_statement_c
Mario de Sousa <msousa@fe.up.pt>
parents:
279
diff
changeset
|
445 |
s4o.print(";\n"); |
214d3fdee596
fixing C code generation of return_statement_c and exit_statement_c
Mario de Sousa <msousa@fe.up.pt>
parents:
279
diff
changeset
|
446 |
s4o.indent_left(); |
214d3fdee596
fixing C code generation of return_statement_c and exit_statement_c
Mario de Sousa <msousa@fe.up.pt>
parents:
279
diff
changeset
|
447 |
|
214d3fdee596
fixing C code generation of return_statement_c and exit_statement_c
Mario de Sousa <msousa@fe.up.pt>
parents:
279
diff
changeset
|
448 |
/* write the label marking the end of the code block */ |
214d3fdee596
fixing C code generation of return_statement_c and exit_statement_c
Mario de Sousa <msousa@fe.up.pt>
parents:
279
diff
changeset
|
449 |
/* please see the comment before the RET_operator_c visitor for details... */ |
214d3fdee596
fixing C code generation of return_statement_c and exit_statement_c
Mario de Sousa <msousa@fe.up.pt>
parents:
279
diff
changeset
|
450 |
/* also needed for return_statement_c */ |
214d3fdee596
fixing C code generation of return_statement_c and exit_statement_c
Mario de Sousa <msousa@fe.up.pt>
parents:
279
diff
changeset
|
451 |
s4o.print("\n"); |
214d3fdee596
fixing C code generation of return_statement_c and exit_statement_c
Mario de Sousa <msousa@fe.up.pt>
parents:
279
diff
changeset
|
452 |
s4o.print(s4o.indent_spaces); |
214d3fdee596
fixing C code generation of return_statement_c and exit_statement_c
Mario de Sousa <msousa@fe.up.pt>
parents:
279
diff
changeset
|
453 |
s4o.print(END_LABEL); |
214d3fdee596
fixing C code generation of return_statement_c and exit_statement_c
Mario de Sousa <msousa@fe.up.pt>
parents:
279
diff
changeset
|
454 |
s4o.print(":\n"); |
214d3fdee596
fixing C code generation of return_statement_c and exit_statement_c
Mario de Sousa <msousa@fe.up.pt>
parents:
279
diff
changeset
|
455 |
s4o.indent_right(); |
214d3fdee596
fixing C code generation of return_statement_c and exit_statement_c
Mario de Sousa <msousa@fe.up.pt>
parents:
279
diff
changeset
|
456 |
|
214d3fdee596
fixing C code generation of return_statement_c and exit_statement_c
Mario de Sousa <msousa@fe.up.pt>
parents:
279
diff
changeset
|
457 |
/* since every label must be followed by at least one statement, and |
214d3fdee596
fixing C code generation of return_statement_c and exit_statement_c
Mario de Sousa <msousa@fe.up.pt>
parents:
279
diff
changeset
|
458 |
* only the functions will introduce the return statement after this label, |
214d3fdee596
fixing C code generation of return_statement_c and exit_statement_c
Mario de Sousa <msousa@fe.up.pt>
parents:
279
diff
changeset
|
459 |
* function blocks written in IL would result in invalid C++ code. |
214d3fdee596
fixing C code generation of return_statement_c and exit_statement_c
Mario de Sousa <msousa@fe.up.pt>
parents:
279
diff
changeset
|
460 |
* To work around this we introduce the equivalent of a 'nop' operation |
214d3fdee596
fixing C code generation of return_statement_c and exit_statement_c
Mario de Sousa <msousa@fe.up.pt>
parents:
279
diff
changeset
|
461 |
* to humour the compiler... |
214d3fdee596
fixing C code generation of return_statement_c and exit_statement_c
Mario de Sousa <msousa@fe.up.pt>
parents:
279
diff
changeset
|
462 |
*/ |
214d3fdee596
fixing C code generation of return_statement_c and exit_statement_c
Mario de Sousa <msousa@fe.up.pt>
parents:
279
diff
changeset
|
463 |
s4o.print(s4o.indent_spaces); |
214d3fdee596
fixing C code generation of return_statement_c and exit_statement_c
Mario de Sousa <msousa@fe.up.pt>
parents:
279
diff
changeset
|
464 |
s4o.print("/* to humour the compiler, we insert a nop */\n"); |
214d3fdee596
fixing C code generation of return_statement_c and exit_statement_c
Mario de Sousa <msousa@fe.up.pt>
parents:
279
diff
changeset
|
465 |
s4o.print(s4o.indent_spaces); |
214d3fdee596
fixing C code generation of return_statement_c and exit_statement_c
Mario de Sousa <msousa@fe.up.pt>
parents:
279
diff
changeset
|
466 |
s4o.print("if (0);\n\n"); |
214d3fdee596
fixing C code generation of return_statement_c and exit_statement_c
Mario de Sousa <msousa@fe.up.pt>
parents:
279
diff
changeset
|
467 |
} |
214d3fdee596
fixing C code generation of return_statement_c and exit_statement_c
Mario de Sousa <msousa@fe.up.pt>
parents:
279
diff
changeset
|
468 |
|
214d3fdee596
fixing C code generation of return_statement_c and exit_statement_c
Mario de Sousa <msousa@fe.up.pt>
parents:
279
diff
changeset
|
469 |
|
70 | 470 |
|
471 |
public: |
|
267
0a1204bcc9af
starting to add proper header file with declaration of standard functions
Mario de Sousa <msousa@fe.up.pt>
parents:
262
diff
changeset
|
472 |
/********************/ |
0a1204bcc9af
starting to add proper header file with declaration of standard functions
Mario de Sousa <msousa@fe.up.pt>
parents:
262
diff
changeset
|
473 |
/* 2.1.6 - Pragmas */ |
0a1204bcc9af
starting to add proper header file with declaration of standard functions
Mario de Sousa <msousa@fe.up.pt>
parents:
262
diff
changeset
|
474 |
/********************/ |
0a1204bcc9af
starting to add proper header file with declaration of standard functions
Mario de Sousa <msousa@fe.up.pt>
parents:
262
diff
changeset
|
475 |
void *visit(enable_code_generation_pragma_c * symbol) {s4o_ptr->enable_output(); return NULL;} |
0a1204bcc9af
starting to add proper header file with declaration of standard functions
Mario de Sousa <msousa@fe.up.pt>
parents:
262
diff
changeset
|
476 |
void *visit(disable_code_generation_pragma_c * symbol) {s4o_ptr->disable_output(); return NULL;} |
70 | 477 |
|
478 |
/*************************/ |
|
479 |
/* B.1 - Common elements */ |
|
480 |
/*************************/ |
|
481 |
/*******************************************/ |
|
482 |
/* B 1.1 - Letters, digits and identifiers */ |
|
483 |
/*******************************************/ |
|
484 |
/* done in base class(es) */ |
|
485 |
||
486 |
/*********************/ |
|
487 |
/* B 1.2 - Constants */ |
|
488 |
/*********************/ |
|
489 |
/* originally empty... */ |
|
490 |
||
491 |
/******************************/ |
|
492 |
/* B 1.2.1 - Numeric Literals */ |
|
493 |
/******************************/ |
|
494 |
/* done in base class(es) */ |
|
495 |
||
496 |
/*******************************/ |
|
497 |
/* B.1.2.2 Character Strings */ |
|
498 |
/*******************************/ |
|
499 |
/* done in base class(es) */ |
|
500 |
||
501 |
/***************************/ |
|
502 |
/* B 1.2.3 - Time Literals */ |
|
503 |
/***************************/ |
|
504 |
/************************/ |
|
505 |
/* B 1.2.3.1 - Duration */ |
|
506 |
/************************/ |
|
507 |
/* done in base class(es) */ |
|
508 |
||
509 |
/************************************/ |
|
510 |
/* B 1.2.3.2 - Time of day and Date */ |
|
511 |
/************************************/ |
|
512 |
/* done in base class(es) */ |
|
513 |
||
514 |
/**********************/ |
|
515 |
/* B.1.3 - Data types */ |
|
516 |
/**********************/ |
|
517 |
/***********************************/ |
|
518 |
/* B 1.3.1 - Elementary Data Types */ |
|
519 |
/***********************************/ |
|
520 |
/* done in base class(es) */ |
|
521 |
||
522 |
/********************************/ |
|
523 |
/* B.1.3.2 - Generic data types */ |
|
524 |
/********************************/ |
|
525 |
/* originally empty... */ |
|
526 |
||
527 |
/********************************/ |
|
528 |
/* B 1.3.3 - Derived data types */ |
|
529 |
/********************************/ |
|
530 |
/* done in base class(es) */ |
|
531 |
||
532 |
/*********************/ |
|
533 |
/* B 1.4 - Variables */ |
|
534 |
/*********************/ |
|
535 |
/* done in base class(es) */ |
|
536 |
||
537 |
/********************************************/ |
|
538 |
/* B.1.4.1 Directly Represented Variables */ |
|
539 |
/********************************************/ |
|
540 |
/* done in base class(es) */ |
|
541 |
||
542 |
/*************************************/ |
|
543 |
/* B.1.4.2 Multi-element Variables */ |
|
544 |
/*************************************/ |
|
545 |
/* done in base class(es) */ |
|
546 |
||
547 |
/******************************************/ |
|
548 |
/* B 1.4.3 - Declaration & Initialisation */ |
|
549 |
/******************************************/ |
|
550 |
/* done in base class(es) */ |
|
551 |
||
552 |
/**************************************/ |
|
553 |
/* B.1.5 - Program organization units */ |
|
554 |
/**************************************/ |
|
555 |
/***********************/ |
|
556 |
/* B 1.5.1 - Functions */ |
|
557 |
/***********************/ |
|
558 |
||
559 |
public: |
|
560 |
/* FUNCTION derived_function_name ':' elementary_type_name io_OR_function_var_declarations_list function_body END_FUNCTION */ |
|
561 |
/* | FUNCTION derived_function_name ':' derived_type_name io_OR_function_var_declarations_list function_body END_FUNCTION */ |
|
562 |
void *visit(function_declaration_c *symbol) { |
|
563 |
generate_c_vardecl_c *vardecl; |
|
564 |
TRACE("function_declaration_c"); |
|
565 |
||
566 |
/* (A) Function declaration... */ |
|
567 |
/* (A.1) Function return type */ |
|
568 |
s4o.print("// FUNCTION\n"); |
|
569 |
symbol->type_name->accept(*this); /* return type */ |
|
570 |
s4o.print(" "); |
|
571 |
/* (A.2) Function name */ |
|
572 |
symbol->derived_function_name->accept(*this); |
|
573 |
s4o.print("("); |
|
574 |
||
575 |
/* (A.3) Function parameters */ |
|
576 |
s4o.indent_right(); |
|
577 |
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
|
578 |
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
|
579 |
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
|
580 |
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
|
581 |
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
|
582 |
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
|
583 |
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
|
584 |
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
|
585 |
delete vardecl; |
eef5e62048c7
Adding support for EN/ENO params in function and function blocks (standard function not supported yet)
lbessard
parents:
145
diff
changeset
|
586 |
|
eef5e62048c7
Adding support for EN/ENO params in function and function blocks (standard function not supported yet)
lbessard
parents:
145
diff
changeset
|
587 |
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
|
588 |
|
eef5e62048c7
Adding support for EN/ENO params in function and function blocks (standard function not supported yet)
lbessard
parents:
145
diff
changeset
|
589 |
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
|
590 |
|
eef5e62048c7
Adding support for EN/ENO params in function and function blocks (standard function not supported yet)
lbessard
parents:
145
diff
changeset
|
591 |
/* (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
|
592 |
/* (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
|
593 |
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
|
594 |
|
202
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
189
diff
changeset
|
595 |
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
|
596 |
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
|
597 |
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
|
598 |
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
|
599 |
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
|
600 |
generate_c_vardecl_c::eno_vt); |
70 | 601 |
vardecl->print(symbol->var_declarations_list); |
602 |
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
|
603 |
|
eef5e62048c7
Adding support for EN/ENO params in function and function blocks (standard function not supported yet)
lbessard
parents:
145
diff
changeset
|
604 |
/* (B.2) Temporary variable for function's return value */ |
70 | 605 |
/* It will have the same name as the function itself! */ |
606 |
s4o.print(s4o.indent_spaces); |
|
607 |
symbol->type_name->accept(*this); /* return type */ |
|
608 |
s4o.print(" "); |
|
609 |
symbol->derived_function_name->accept(*this); |
|
610 |
s4o.print(" = "); |
|
611 |
{ |
|
612 |
/* get the default value of this variable's type */ |
|
613 |
symbol_c *default_value = (symbol_c *)symbol->type_name->accept(*type_initial_value_c::instance()); |
|
614 |
if (default_value == NULL) ERROR; |
|
615 |
default_value->accept(*this); |
|
616 |
} |
|
617 |
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
|
618 |
|
eef5e62048c7
Adding support for EN/ENO params in function and function blocks (standard function not supported yet)
lbessard
parents:
145
diff
changeset
|
619 |
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
|
620 |
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
|
621 |
s4o.indent_right(); |
202
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
189
diff
changeset
|
622 |
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
|
623 |
s4o.indent_right(); |
202
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
189
diff
changeset
|
624 |
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
|
625 |
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
|
626 |
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
|
627 |
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
|
628 |
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
|
629 |
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
|
630 |
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
|
631 |
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
|
632 |
|
70 | 633 |
/* (C) Function body */ |
217
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
210
diff
changeset
|
634 |
generate_c_SFC_IL_ST_c generate_c_code(&s4o, symbol->derived_function_name, symbol); |
70 | 635 |
symbol->function_body->accept(generate_c_code); |
145 | 636 |
|
283
214d3fdee596
fixing C code generation of return_statement_c and exit_statement_c
Mario de Sousa <msousa@fe.up.pt>
parents:
279
diff
changeset
|
637 |
print_end_of_block_label(); |
214d3fdee596
fixing C code generation of return_statement_c and exit_statement_c
Mario de Sousa <msousa@fe.up.pt>
parents:
279
diff
changeset
|
638 |
|
145 | 639 |
vardecl = new generate_c_vardecl_c(&s4o, |
640 |
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
|
641 |
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
|
642 |
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
|
643 |
generate_c_vardecl_c::eno_vt); |
145 | 644 |
vardecl->print(symbol->var_declarations_list); |
645 |
delete vardecl; |
|
646 |
||
70 | 647 |
s4o.print(s4o.indent_spaces + "return "); |
648 |
symbol->derived_function_name->accept(*this); |
|
649 |
s4o.print(";\n"); |
|
650 |
s4o.indent_left(); |
|
651 |
s4o.print(s4o.indent_spaces + "}\n\n\n"); |
|
652 |
||
653 |
return NULL; |
|
654 |
} |
|
655 |
||
656 |
||
657 |
/* The remaining var_declarations_list_c, function_var_decls_c |
|
658 |
* and var2_init_decl_list_c are handled in the generate_c_vardecl_c class |
|
659 |
*/ |
|
660 |
||
661 |
||
662 |
/*****************************/ |
|
663 |
/* B 1.5.2 - Function Blocks */ |
|
664 |
/*****************************/ |
|
665 |
public: |
|
666 |
/* FUNCTION_BLOCK derived_function_block_name io_OR_other_var_declarations function_block_body END_FUNCTION_BLOCK */ |
|
667 |
//SYM_REF4(function_block_declaration_c, fblock_name, var_declarations, fblock_body, unused) |
|
668 |
void *visit(function_block_declaration_c *symbol) { |
|
669 |
generate_c_vardecl_c *vardecl; |
|
670 |
generate_c_sfcdecl_c *sfcdecl; |
|
121
9e8ce092e169
Adding support for POU struct definition in POUS.h
lbessard
parents:
120
diff
changeset
|
671 |
generate_c_typedecl_c *typedecl; |
234
e4d31cd0e6d8
Using function_call_iterator in generate_c_inlinefcall instead of creating a new visitor
laurent
parents:
231
diff
changeset
|
672 |
generate_c_inlinefcall_c *inlinedecl; |
70 | 673 |
TRACE("function_block_declaration_c"); |
674 |
||
675 |
/* (A) Function Block data structure declaration... */ |
|
121
9e8ce092e169
Adding support for POU struct definition in POUS.h
lbessard
parents:
120
diff
changeset
|
676 |
typedecl = new generate_c_typedecl_c(&s4o_incl); |
70 | 677 |
/* (A.1) Data structure declaration */ |
121
9e8ce092e169
Adding support for POU struct definition in POUS.h
lbessard
parents:
120
diff
changeset
|
678 |
s4o_incl.print("// FUNCTION_BLOCK "); |
9e8ce092e169
Adding support for POU struct definition in POUS.h
lbessard
parents:
120
diff
changeset
|
679 |
symbol->fblock_name->accept(*typedecl); |
9e8ce092e169
Adding support for POU struct definition in POUS.h
lbessard
parents:
120
diff
changeset
|
680 |
s4o_incl.print("\n// Data part\n"); |
9e8ce092e169
Adding support for POU struct definition in POUS.h
lbessard
parents:
120
diff
changeset
|
681 |
s4o_incl.print("typedef struct {\n"); |
9e8ce092e169
Adding support for POU struct definition in POUS.h
lbessard
parents:
120
diff
changeset
|
682 |
s4o_incl.indent_right(); |
70 | 683 |
/* (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
|
684 |
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
|
685 |
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
|
686 |
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
|
687 |
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
|
688 |
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
|
689 |
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
|
690 |
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
|
691 |
generate_c_vardecl_c::eno_vt); |
70 | 692 |
vardecl->print(symbol->var_declarations); |
693 |
delete vardecl; |
|
121
9e8ce092e169
Adding support for POU struct definition in POUS.h
lbessard
parents:
120
diff
changeset
|
694 |
s4o_incl.print("\n"); |
70 | 695 |
/* (A.3) Private internal variables */ |
121
9e8ce092e169
Adding support for POU struct definition in POUS.h
lbessard
parents:
120
diff
changeset
|
696 |
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
|
697 |
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
|
698 |
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
|
699 |
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
|
700 |
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
|
701 |
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
|
702 |
generate_c_vardecl_c::external_vt); |
70 | 703 |
vardecl->print(symbol->var_declarations); |
704 |
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
|
705 |
|
70 | 706 |
/* (A.4) Generate private internal variables for SFC */ |
244
3a478229db8b
Adding support for generating code for variable in step action association instead of action
laurent
parents:
241
diff
changeset
|
707 |
sfcdecl = new generate_c_sfcdecl_c(&s4o_incl, symbol); |
3a478229db8b
Adding support for generating code for variable in step action association instead of action
laurent
parents:
241
diff
changeset
|
708 |
sfcdecl->generate(symbol->fblock_body, generate_c_sfcdecl_c::sfcdecl_sd); |
70 | 709 |
delete sfcdecl; |
121
9e8ce092e169
Adding support for POU struct definition in POUS.h
lbessard
parents:
120
diff
changeset
|
710 |
s4o_incl.print("\n"); |
244
3a478229db8b
Adding support for generating code for variable in step action association instead of action
laurent
parents:
241
diff
changeset
|
711 |
|
70 | 712 |
/* (A.5) Function Block data structure type name. */ |
121
9e8ce092e169
Adding support for POU struct definition in POUS.h
lbessard
parents:
120
diff
changeset
|
713 |
s4o_incl.indent_left(); |
9e8ce092e169
Adding support for POU struct definition in POUS.h
lbessard
parents:
120
diff
changeset
|
714 |
s4o_incl.print("} "); |
9e8ce092e169
Adding support for POU struct definition in POUS.h
lbessard
parents:
120
diff
changeset
|
715 |
symbol->fblock_name->accept(*typedecl); |
9e8ce092e169
Adding support for POU struct definition in POUS.h
lbessard
parents:
120
diff
changeset
|
716 |
s4o_incl.print(";\n\n"); |
9e8ce092e169
Adding support for POU struct definition in POUS.h
lbessard
parents:
120
diff
changeset
|
717 |
delete typedecl; |
70 | 718 |
|
217
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
210
diff
changeset
|
719 |
/* (A.6) Function Block inline function declaration for function invocation */ |
234
e4d31cd0e6d8
Using function_call_iterator in generate_c_inlinefcall instead of creating a new visitor
laurent
parents:
231
diff
changeset
|
720 |
inlinedecl = new generate_c_inlinefcall_c(&s4o, symbol->fblock_name, symbol, FB_FUNCTION_PARAM"->"); |
e4d31cd0e6d8
Using function_call_iterator in generate_c_inlinefcall instead of creating a new visitor
laurent
parents:
231
diff
changeset
|
721 |
inlinedecl->print(symbol->fblock_body); |
e4d31cd0e6d8
Using function_call_iterator in generate_c_inlinefcall instead of creating a new visitor
laurent
parents:
231
diff
changeset
|
722 |
delete inlinedecl; |
217
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
210
diff
changeset
|
723 |
|
70 | 724 |
/* (B) Constructor */ |
725 |
/* (B.1) Constructor name... */ |
|
726 |
s4o.print(s4o.indent_spaces + "void "); |
|
727 |
symbol->fblock_name->accept(*this); |
|
728 |
s4o.print(FB_INIT_SUFFIX); |
|
729 |
s4o.print("("); |
|
730 |
||
731 |
/* first and only parameter is a pointer to the data */ |
|
732 |
symbol->fblock_name->accept(*this); |
|
733 |
s4o.print(" *"); |
|
734 |
s4o.print(FB_FUNCTION_PARAM); |
|
221
c6aed7e5f070
Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents:
217
diff
changeset
|
735 |
s4o.print(", BOOL retain) {\n"); |
70 | 736 |
s4o.indent_right(); |
737 |
||
738 |
/* (B.2) Member initializations... */ |
|
739 |
s4o.print(s4o.indent_spaces); |
|
740 |
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
|
741 |
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
|
742 |
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
|
743 |
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
|
744 |
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
|
745 |
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
|
746 |
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
|
747 |
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
|
748 |
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
|
749 |
generate_c_vardecl_c::eno_vt); |
70 | 750 |
vardecl->print(symbol->var_declarations, NULL, FB_FUNCTION_PARAM"->"); |
751 |
delete vardecl; |
|
752 |
s4o.print("\n"); |
|
244
3a478229db8b
Adding support for generating code for variable in step action association instead of action
laurent
parents:
241
diff
changeset
|
753 |
|
3a478229db8b
Adding support for generating code for variable in step action association instead of action
laurent
parents:
241
diff
changeset
|
754 |
sfcdecl = new generate_c_sfcdecl_c(&s4o, symbol, FB_FUNCTION_PARAM"->"); |
3a478229db8b
Adding support for generating code for variable in step action association instead of action
laurent
parents:
241
diff
changeset
|
755 |
|
70 | 756 |
/* (B.3) Generate private internal variables for SFC */ |
244
3a478229db8b
Adding support for generating code for variable in step action association instead of action
laurent
parents:
241
diff
changeset
|
757 |
sfcdecl->generate(symbol->fblock_body, generate_c_sfcdecl_c::sfcinit_sd); |
3a478229db8b
Adding support for generating code for variable in step action association instead of action
laurent
parents:
241
diff
changeset
|
758 |
|
70 | 759 |
s4o.indent_left(); |
760 |
s4o.print(s4o.indent_spaces + "}\n\n"); |
|
761 |
||
762 |
||
763 |
/* (C) Function with FB body */ |
|
764 |
/* (C.1) Step definitions */ |
|
244
3a478229db8b
Adding support for generating code for variable in step action association instead of action
laurent
parents:
241
diff
changeset
|
765 |
sfcdecl->generate(symbol->fblock_body, generate_c_sfcdecl_c::stepdef_sd); |
70 | 766 |
|
767 |
/* (C.2) Action definitions */ |
|
244
3a478229db8b
Adding support for generating code for variable in step action association instead of action
laurent
parents:
241
diff
changeset
|
768 |
sfcdecl->generate(symbol->fblock_body, generate_c_sfcdecl_c::actiondef_sd); |
3a478229db8b
Adding support for generating code for variable in step action association instead of action
laurent
parents:
241
diff
changeset
|
769 |
|
70 | 770 |
/* (C.3) Function declaration */ |
771 |
s4o.print("// Code part\n"); |
|
772 |
/* function interface */ |
|
773 |
s4o.print("void "); |
|
774 |
symbol->fblock_name->accept(*this); |
|
775 |
s4o.print(FB_FUNCTION_SUFFIX); |
|
776 |
s4o.print("("); |
|
777 |
/* first and only parameter is a pointer to the data */ |
|
778 |
symbol->fblock_name->accept(*this); |
|
779 |
s4o.print(" *"); |
|
780 |
s4o.print(FB_FUNCTION_PARAM); |
|
781 |
s4o.print(") {\n"); |
|
782 |
s4o.indent_right(); |
|
783 |
||
146
eef5e62048c7
Adding support for EN/ENO params in function and function blocks (standard function not supported yet)
lbessard
parents:
145
diff
changeset
|
784 |
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
|
785 |
s4o.print(s4o.indent_spaces + "if (!"); |
217
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
210
diff
changeset
|
786 |
s4o.print(GET_VAR); |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
210
diff
changeset
|
787 |
s4o.print("("); |
146
eef5e62048c7
Adding support for EN/ENO params in function and function blocks (standard function not supported yet)
lbessard
parents:
145
diff
changeset
|
788 |
s4o.print(FB_FUNCTION_PARAM); |
217
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
210
diff
changeset
|
789 |
s4o.print("->EN)) {\n"); |
146
eef5e62048c7
Adding support for EN/ENO params in function and function blocks (standard function not supported yet)
lbessard
parents:
145
diff
changeset
|
790 |
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
|
791 |
s4o.print(s4o.indent_spaces); |
217
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
210
diff
changeset
|
792 |
s4o.print(SET_VAR); |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
210
diff
changeset
|
793 |
s4o.print("("); |
146
eef5e62048c7
Adding support for EN/ENO params in function and function blocks (standard function not supported yet)
lbessard
parents:
145
diff
changeset
|
794 |
s4o.print(FB_FUNCTION_PARAM); |
217
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
210
diff
changeset
|
795 |
s4o.print("->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
|
796 |
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
|
797 |
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
|
798 |
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
|
799 |
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
|
800 |
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
|
801 |
s4o.print(s4o.indent_spaces); |
217
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
210
diff
changeset
|
802 |
s4o.print(SET_VAR); |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
210
diff
changeset
|
803 |
s4o.print("("); |
146
eef5e62048c7
Adding support for EN/ENO params in function and function blocks (standard function not supported yet)
lbessard
parents:
145
diff
changeset
|
804 |
s4o.print(FB_FUNCTION_PARAM); |
217
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
210
diff
changeset
|
805 |
s4o.print("->ENO,__BOOL_LITERAL(TRUE));\n"); |
146
eef5e62048c7
Adding support for EN/ENO params in function and function blocks (standard function not supported yet)
lbessard
parents:
145
diff
changeset
|
806 |
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
|
807 |
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
|
808 |
|
70 | 809 |
/* (C.4) Initialize TEMP variables */ |
810 |
/* function body */ |
|
811 |
s4o.print(s4o.indent_spaces + "// Initialise TEMP variables\n"); |
|
812 |
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
|
813 |
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
|
814 |
generate_c_vardecl_c::temp_vt); |
70 | 815 |
vardecl->print(symbol->var_declarations, NULL, FB_FUNCTION_PARAM"->"); |
816 |
delete vardecl; |
|
817 |
s4o.print("\n"); |
|
818 |
||
819 |
/* (C.5) Function code */ |
|
217
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
210
diff
changeset
|
820 |
generate_c_SFC_IL_ST_c generate_c_code(&s4o, symbol->fblock_name, symbol, FB_FUNCTION_PARAM"->"); |
70 | 821 |
symbol->fblock_body->accept(generate_c_code); |
283
214d3fdee596
fixing C code generation of return_statement_c and exit_statement_c
Mario de Sousa <msousa@fe.up.pt>
parents:
279
diff
changeset
|
822 |
print_end_of_block_label(); |
70 | 823 |
s4o.indent_left(); |
824 |
s4o.print(s4o.indent_spaces + "} // "); |
|
825 |
symbol->fblock_name->accept(*this); |
|
826 |
s4o.print(FB_FUNCTION_SUFFIX); |
|
827 |
s4o.print(s4o.indent_spaces + "() \n\n"); |
|
828 |
||
829 |
/* (C.6) Step undefinitions */ |
|
244
3a478229db8b
Adding support for generating code for variable in step action association instead of action
laurent
parents:
241
diff
changeset
|
830 |
sfcdecl->generate(symbol->fblock_body, generate_c_sfcdecl_c::stepundef_sd); |
3a478229db8b
Adding support for generating code for variable in step action association instead of action
laurent
parents:
241
diff
changeset
|
831 |
|
70 | 832 |
/* (C.7) Action undefinitions */ |
244
3a478229db8b
Adding support for generating code for variable in step action association instead of action
laurent
parents:
241
diff
changeset
|
833 |
sfcdecl->generate(symbol->fblock_body, generate_c_sfcdecl_c::actionundef_sd); |
3a478229db8b
Adding support for generating code for variable in step action association instead of action
laurent
parents:
241
diff
changeset
|
834 |
|
70 | 835 |
delete sfcdecl; |
836 |
||
837 |
s4o.indent_left(); |
|
838 |
s4o.print("\n\n\n\n"); |
|
839 |
||
840 |
return NULL; |
|
841 |
} |
|
842 |
||
843 |
||
844 |
/* The remaining temp_var_decls_c, temp_var_decls_list_c |
|
845 |
* and non_retentive_var_decls_c are handled in the generate_c_vardecl_c class |
|
846 |
*/ |
|
847 |
||
848 |
||
849 |
/**********************/ |
|
850 |
/* B 1.5.3 - Programs */ |
|
851 |
/**********************/ |
|
852 |
||
853 |
||
854 |
||
855 |
public: |
|
856 |
/* PROGRAM program_type_name program_var_declarations_list function_block_body END_PROGRAM */ |
|
857 |
//SYM_REF4(program_declaration_c, program_type_name, var_declarations, function_block_body, unused) |
|
858 |
void *visit(program_declaration_c *symbol) { |
|
859 |
generate_c_vardecl_c *vardecl; |
|
860 |
generate_c_sfcdecl_c *sfcdecl; |
|
121
9e8ce092e169
Adding support for POU struct definition in POUS.h
lbessard
parents:
120
diff
changeset
|
861 |
generate_c_typedecl_c *typedecl; |
234
e4d31cd0e6d8
Using function_call_iterator in generate_c_inlinefcall instead of creating a new visitor
laurent
parents:
231
diff
changeset
|
862 |
generate_c_inlinefcall_c *inlinedecl; |
70 | 863 |
TRACE("program_declaration_c"); |
864 |
||
865 |
/* (A) Program data structure declaration... */ |
|
121
9e8ce092e169
Adding support for POU struct definition in POUS.h
lbessard
parents:
120
diff
changeset
|
866 |
typedecl = new generate_c_typedecl_c(&s4o_incl); |
70 | 867 |
/* (A.1) Data structure declaration */ |
121
9e8ce092e169
Adding support for POU struct definition in POUS.h
lbessard
parents:
120
diff
changeset
|
868 |
s4o_incl.print("// PROGRAM "); |
9e8ce092e169
Adding support for POU struct definition in POUS.h
lbessard
parents:
120
diff
changeset
|
869 |
symbol->program_type_name->accept(*typedecl); |
9e8ce092e169
Adding support for POU struct definition in POUS.h
lbessard
parents:
120
diff
changeset
|
870 |
s4o_incl.print("\n// Data part\n"); |
9e8ce092e169
Adding support for POU struct definition in POUS.h
lbessard
parents:
120
diff
changeset
|
871 |
s4o_incl.print("typedef struct {\n"); |
9e8ce092e169
Adding support for POU struct definition in POUS.h
lbessard
parents:
120
diff
changeset
|
872 |
s4o_incl.indent_right(); |
70 | 873 |
|
874 |
/* (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
|
875 |
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
|
876 |
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
|
877 |
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
|
878 |
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
|
879 |
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
|
880 |
generate_c_vardecl_c::inoutput_vt); |
70 | 881 |
vardecl->print(symbol->var_declarations); |
882 |
delete vardecl; |
|
121
9e8ce092e169
Adding support for POU struct definition in POUS.h
lbessard
parents:
120
diff
changeset
|
883 |
s4o_incl.print("\n"); |
70 | 884 |
/* (A.3) Private internal variables */ |
121
9e8ce092e169
Adding support for POU struct definition in POUS.h
lbessard
parents:
120
diff
changeset
|
885 |
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
|
886 |
vardecl = new generate_c_vardecl_c(&s4o_incl, |
70 | 887 |
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
|
888 |
generate_c_vardecl_c::temp_vt | |
70 | 889 |
generate_c_vardecl_c::private_vt | |
890 |
generate_c_vardecl_c::located_vt | |
|
891 |
generate_c_vardecl_c::external_vt); |
|
892 |
vardecl->print(symbol->var_declarations); |
|
893 |
delete vardecl; |
|
244
3a478229db8b
Adding support for generating code for variable in step action association instead of action
laurent
parents:
241
diff
changeset
|
894 |
|
70 | 895 |
/* (A.4) Generate private internal variables for SFC */ |
244
3a478229db8b
Adding support for generating code for variable in step action association instead of action
laurent
parents:
241
diff
changeset
|
896 |
sfcdecl = new generate_c_sfcdecl_c(&s4o_incl, symbol); |
3a478229db8b
Adding support for generating code for variable in step action association instead of action
laurent
parents:
241
diff
changeset
|
897 |
sfcdecl->generate(symbol->function_block_body, generate_c_sfcdecl_c::sfcdecl_sd); |
70 | 898 |
delete sfcdecl; |
244
3a478229db8b
Adding support for generating code for variable in step action association instead of action
laurent
parents:
241
diff
changeset
|
899 |
s4o_incl.print("\n"); |
70 | 900 |
|
901 |
/* (A.5) Program data structure type name. */ |
|
121
9e8ce092e169
Adding support for POU struct definition in POUS.h
lbessard
parents:
120
diff
changeset
|
902 |
s4o_incl.indent_left(); |
9e8ce092e169
Adding support for POU struct definition in POUS.h
lbessard
parents:
120
diff
changeset
|
903 |
s4o_incl.print("} "); |
9e8ce092e169
Adding support for POU struct definition in POUS.h
lbessard
parents:
120
diff
changeset
|
904 |
symbol->program_type_name->accept(*typedecl); |
9e8ce092e169
Adding support for POU struct definition in POUS.h
lbessard
parents:
120
diff
changeset
|
905 |
s4o_incl.print(";\n\n"); |
9e8ce092e169
Adding support for POU struct definition in POUS.h
lbessard
parents:
120
diff
changeset
|
906 |
delete typedecl; |
70 | 907 |
|
217
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
210
diff
changeset
|
908 |
/* (A.6) Function Block inline function declaration for function invocation */ |
234
e4d31cd0e6d8
Using function_call_iterator in generate_c_inlinefcall instead of creating a new visitor
laurent
parents:
231
diff
changeset
|
909 |
inlinedecl = new generate_c_inlinefcall_c(&s4o, symbol->program_type_name, symbol, FB_FUNCTION_PARAM"->"); |
e4d31cd0e6d8
Using function_call_iterator in generate_c_inlinefcall instead of creating a new visitor
laurent
parents:
231
diff
changeset
|
910 |
inlinedecl->print(symbol->function_block_body); |
e4d31cd0e6d8
Using function_call_iterator in generate_c_inlinefcall instead of creating a new visitor
laurent
parents:
231
diff
changeset
|
911 |
delete inlinedecl; |
217
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
210
diff
changeset
|
912 |
|
70 | 913 |
/* (B) Constructor */ |
914 |
/* (B.1) Constructor name... */ |
|
915 |
s4o.print(s4o.indent_spaces + "void "); |
|
916 |
symbol->program_type_name->accept(*this); |
|
917 |
s4o.print(FB_INIT_SUFFIX); |
|
918 |
s4o.print("("); |
|
919 |
||
920 |
/* first and only parameter is a pointer to the data */ |
|
921 |
symbol->program_type_name->accept(*this); |
|
922 |
s4o.print(" *"); |
|
923 |
s4o.print(FB_FUNCTION_PARAM); |
|
221
c6aed7e5f070
Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents:
217
diff
changeset
|
924 |
s4o.print(", BOOL retain) {\n"); |
70 | 925 |
s4o.indent_right(); |
926 |
||
927 |
/* (B.2) Member initializations... */ |
|
928 |
s4o.print(s4o.indent_spaces); |
|
929 |
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
|
930 |
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
|
931 |
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
|
932 |
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
|
933 |
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
|
934 |
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
|
935 |
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
|
936 |
generate_c_vardecl_c::external_vt); |
70 | 937 |
vardecl->print(symbol->var_declarations, NULL, FB_FUNCTION_PARAM"->"); |
938 |
delete vardecl; |
|
939 |
s4o.print("\n"); |
|
244
3a478229db8b
Adding support for generating code for variable in step action association instead of action
laurent
parents:
241
diff
changeset
|
940 |
|
3a478229db8b
Adding support for generating code for variable in step action association instead of action
laurent
parents:
241
diff
changeset
|
941 |
sfcdecl = new generate_c_sfcdecl_c(&s4o, symbol, FB_FUNCTION_PARAM"->"); |
3a478229db8b
Adding support for generating code for variable in step action association instead of action
laurent
parents:
241
diff
changeset
|
942 |
|
70 | 943 |
/* (B.3) Generate private internal variables for SFC */ |
244
3a478229db8b
Adding support for generating code for variable in step action association instead of action
laurent
parents:
241
diff
changeset
|
944 |
sfcdecl->generate(symbol->function_block_body, generate_c_sfcdecl_c::sfcinit_sd); |
70 | 945 |
|
946 |
s4o.indent_left(); |
|
947 |
s4o.print(s4o.indent_spaces + "}\n\n"); |
|
948 |
||
949 |
/* (C) Function with PROGRAM body */ |
|
950 |
/* (C.1) Step definitions */ |
|
244
3a478229db8b
Adding support for generating code for variable in step action association instead of action
laurent
parents:
241
diff
changeset
|
951 |
sfcdecl->generate(symbol->function_block_body, generate_c_sfcdecl_c::stepdef_sd); |
70 | 952 |
|
953 |
/* (C.2) Action definitions */ |
|
244
3a478229db8b
Adding support for generating code for variable in step action association instead of action
laurent
parents:
241
diff
changeset
|
954 |
sfcdecl->generate(symbol->function_block_body, generate_c_sfcdecl_c::actiondef_sd); |
70 | 955 |
|
956 |
/* (C.3) Function declaration */ |
|
957 |
s4o.print("// Code part\n"); |
|
958 |
/* function interface */ |
|
959 |
s4o.print("void "); |
|
960 |
symbol->program_type_name->accept(*this); |
|
961 |
s4o.print(FB_FUNCTION_SUFFIX); |
|
962 |
s4o.print("("); |
|
963 |
/* first and only parameter is a pointer to the data */ |
|
964 |
symbol->program_type_name->accept(*this); |
|
965 |
s4o.print(" *"); |
|
966 |
s4o.print(FB_FUNCTION_PARAM); |
|
967 |
s4o.print(") {\n"); |
|
968 |
s4o.indent_right(); |
|
969 |
||
970 |
/* (C.4) Initialize TEMP variables */ |
|
971 |
/* function body */ |
|
972 |
s4o.print(s4o.indent_spaces + "// Initialise TEMP variables\n"); |
|
973 |
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
|
974 |
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
|
975 |
generate_c_vardecl_c::temp_vt); |
70 | 976 |
vardecl->print(symbol->var_declarations, NULL, FB_FUNCTION_PARAM"->"); |
977 |
delete vardecl; |
|
978 |
s4o.print("\n"); |
|
979 |
||
980 |
/* (C.5) Function code */ |
|
217
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
210
diff
changeset
|
981 |
generate_c_SFC_IL_ST_c generate_c_code(&s4o, symbol->program_type_name, symbol, FB_FUNCTION_PARAM"->"); |
70 | 982 |
symbol->function_block_body->accept(generate_c_code); |
283
214d3fdee596
fixing C code generation of return_statement_c and exit_statement_c
Mario de Sousa <msousa@fe.up.pt>
parents:
279
diff
changeset
|
983 |
print_end_of_block_label(); |
70 | 984 |
s4o.indent_left(); |
985 |
s4o.print(s4o.indent_spaces + "} // "); |
|
986 |
symbol->program_type_name->accept(*this); |
|
987 |
s4o.print(FB_FUNCTION_SUFFIX); |
|
988 |
s4o.print(s4o.indent_spaces + "() \n\n"); |
|
989 |
||
990 |
/* (C.6) Step undefinitions */ |
|
244
3a478229db8b
Adding support for generating code for variable in step action association instead of action
laurent
parents:
241
diff
changeset
|
991 |
sfcdecl->generate(symbol->function_block_body, generate_c_sfcdecl_c::stepundef_sd); |
3a478229db8b
Adding support for generating code for variable in step action association instead of action
laurent
parents:
241
diff
changeset
|
992 |
|
3a478229db8b
Adding support for generating code for variable in step action association instead of action
laurent
parents:
241
diff
changeset
|
993 |
/* (C.7) Action undefinitions */ |
3a478229db8b
Adding support for generating code for variable in step action association instead of action
laurent
parents:
241
diff
changeset
|
994 |
sfcdecl->generate(symbol->function_block_body, generate_c_sfcdecl_c::actionundef_sd); |
3a478229db8b
Adding support for generating code for variable in step action association instead of action
laurent
parents:
241
diff
changeset
|
995 |
|
70 | 996 |
delete sfcdecl; |
244
3a478229db8b
Adding support for generating code for variable in step action association instead of action
laurent
parents:
241
diff
changeset
|
997 |
|
70 | 998 |
s4o.indent_left(); |
999 |
s4o.print("\n\n\n\n"); |
|
1000 |
||
1001 |
return NULL; |
|
1002 |
} |
|
1003 |
||
1004 |
}; /* generate_c_pous_c */ |
|
1005 |
||
1006 |
/***********************************************************************/ |
|
1007 |
/***********************************************************************/ |
|
1008 |
/***********************************************************************/ |
|
1009 |
/***********************************************************************/ |
|
1010 |
/***********************************************************************/ |
|
1011 |
/***********************************************************************/ |
|
1012 |
/***********************************************************************/ |
|
1013 |
/***********************************************************************/ |
|
1014 |
||
1015 |
class generate_c_config_c: public generate_c_typedecl_c { |
|
267
0a1204bcc9af
starting to add proper header file with declaration of standard functions
Mario de Sousa <msousa@fe.up.pt>
parents:
262
diff
changeset
|
1016 |
private: |
0a1204bcc9af
starting to add proper header file with declaration of standard functions
Mario de Sousa <msousa@fe.up.pt>
parents:
262
diff
changeset
|
1017 |
stage4out_c *s4o_ptr; |
70 | 1018 |
|
1019 |
public: |
|
1020 |
generate_c_config_c(stage4out_c *s4o_ptr) |
|
267
0a1204bcc9af
starting to add proper header file with declaration of standard functions
Mario de Sousa <msousa@fe.up.pt>
parents:
262
diff
changeset
|
1021 |
: generate_c_typedecl_c(s4o_ptr) { |
0a1204bcc9af
starting to add proper header file with declaration of standard functions
Mario de Sousa <msousa@fe.up.pt>
parents:
262
diff
changeset
|
1022 |
generate_c_config_c::s4o_ptr = s4o_ptr; |
0a1204bcc9af
starting to add proper header file with declaration of standard functions
Mario de Sousa <msousa@fe.up.pt>
parents:
262
diff
changeset
|
1023 |
}; |
0a1204bcc9af
starting to add proper header file with declaration of standard functions
Mario de Sousa <msousa@fe.up.pt>
parents:
262
diff
changeset
|
1024 |
|
70 | 1025 |
virtual ~generate_c_config_c(void) {} |
1026 |
||
1027 |
typedef enum { |
|
1028 |
initprotos_dt, |
|
1029 |
initdeclare_dt, |
|
1030 |
runprotos_dt, |
|
1031 |
rundeclare_dt |
|
1032 |
} declaretype_t; |
|
1033 |
||
1034 |
declaretype_t wanted_declaretype; |
|
1035 |
||
267
0a1204bcc9af
starting to add proper header file with declaration of standard functions
Mario de Sousa <msousa@fe.up.pt>
parents:
262
diff
changeset
|
1036 |
|
0a1204bcc9af
starting to add proper header file with declaration of standard functions
Mario de Sousa <msousa@fe.up.pt>
parents:
262
diff
changeset
|
1037 |
public: |
0a1204bcc9af
starting to add proper header file with declaration of standard functions
Mario de Sousa <msousa@fe.up.pt>
parents:
262
diff
changeset
|
1038 |
/********************/ |
0a1204bcc9af
starting to add proper header file with declaration of standard functions
Mario de Sousa <msousa@fe.up.pt>
parents:
262
diff
changeset
|
1039 |
/* 2.1.6 - Pragmas */ |
0a1204bcc9af
starting to add proper header file with declaration of standard functions
Mario de Sousa <msousa@fe.up.pt>
parents:
262
diff
changeset
|
1040 |
/********************/ |
0a1204bcc9af
starting to add proper header file with declaration of standard functions
Mario de Sousa <msousa@fe.up.pt>
parents:
262
diff
changeset
|
1041 |
void *visit(enable_code_generation_pragma_c * symbol) {s4o_ptr->enable_output(); return NULL;} |
0a1204bcc9af
starting to add proper header file with declaration of standard functions
Mario de Sousa <msousa@fe.up.pt>
parents:
262
diff
changeset
|
1042 |
void *visit(disable_code_generation_pragma_c * symbol) {s4o_ptr->disable_output(); return NULL;} |
0a1204bcc9af
starting to add proper header file with declaration of standard functions
Mario de Sousa <msousa@fe.up.pt>
parents:
262
diff
changeset
|
1043 |
|
0a1204bcc9af
starting to add proper header file with declaration of standard functions
Mario de Sousa <msousa@fe.up.pt>
parents:
262
diff
changeset
|
1044 |
|
70 | 1045 |
/********************************/ |
1046 |
/* B 1.7 Configuration elements */ |
|
1047 |
/********************************/ |
|
1048 |
/* |
|
1049 |
CONFIGURATION configuration_name |
|
1050 |
optional_global_var_declarations |
|
1051 |
(resource_declaration_list | single_resource_declaration) |
|
1052 |
optional_access_declarations |
|
1053 |
optional_instance_specific_initializations |
|
1054 |
END_CONFIGURATION |
|
1055 |
*/ |
|
1056 |
/* |
|
1057 |
SYM_REF6(configuration_declaration_c, configuration_name, global_var_declarations, resource_declarations, access_declarations, instance_specific_initializations, unused) |
|
1058 |
*/ |
|
1059 |
void *visit(configuration_declaration_c *symbol) { |
|
1060 |
generate_c_vardecl_c *vardecl; |
|
1061 |
||
1062 |
/* Insert the header... */ |
|
1063 |
s4o.print("/*******************************************/\n"); |
|
1064 |
s4o.print("/* FILE GENERATED BY iec2c */\n"); |
|
1065 |
s4o.print("/* Editing this file is not recommended... */\n"); |
|
1066 |
s4o.print("/*******************************************/\n\n"); |
|
1067 |
s4o.print("#include \"iec_std_lib.h\"\n\n"); |
|
257 | 1068 |
s4o.print("#include \"accessor.h\"\n\n"); |
250
5d2927300e2c
Adding missing support for accessors and retain in configuration files (Bug and fix reported by Manuele Conti)
laurent
parents:
248
diff
changeset
|
1069 |
|
70 | 1070 |
/* (A) configuration declaration... */ |
1071 |
/* (A.1) configuration name in comment */ |
|
1072 |
s4o.print("// CONFIGURATION "); |
|
1073 |
symbol->configuration_name->accept(*this); |
|
1074 |
s4o.print("\n"); |
|
1075 |
||
1076 |
/* (A.2) Global variables */ |
|
1077 |
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
|
1078 |
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
|
1079 |
generate_c_vardecl_c::global_vt); |
70 | 1080 |
vardecl->print(symbol); |
1081 |
delete vardecl; |
|
1082 |
s4o.print("\n"); |
|
1083 |
||
1084 |
/* (B) Initialisation Function */ |
|
1085 |
/* (B.1) Ressources initialisation protos... */ |
|
1086 |
wanted_declaretype = initprotos_dt; |
|
1087 |
symbol->resource_declarations->accept(*this); |
|
1088 |
s4o.print("\n"); |
|
1089 |
||
1090 |
/* (B.2) Initialisation function name... */ |
|
1091 |
s4o.print(s4o.indent_spaces + "void config"); |
|
1092 |
s4o.print(FB_INIT_SUFFIX); |
|
1093 |
s4o.print("(void) {\n"); |
|
1094 |
s4o.indent_right(); |
|
250
5d2927300e2c
Adding missing support for accessors and retain in configuration files (Bug and fix reported by Manuele Conti)
laurent
parents:
248
diff
changeset
|
1095 |
s4o.print(s4o.indent_spaces); |
255
86ef2244aef1
Modify code generated for avoiding warning while compiling when configuration or resource don't have any global variable
laurent
parents:
250
diff
changeset
|
1096 |
s4o.print("BOOL retain;\n"); |
86ef2244aef1
Modify code generated for avoiding warning while compiling when configuration or resource don't have any global variable
laurent
parents:
250
diff
changeset
|
1097 |
s4o.print(s4o.indent_spaces); |
86ef2244aef1
Modify code generated for avoiding warning while compiling when configuration or resource don't have any global variable
laurent
parents:
250
diff
changeset
|
1098 |
s4o.print("retain = 0;\n"); |
70 | 1099 |
|
1100 |
/* (B.3) Global variables initializations... */ |
|
1101 |
s4o.print(s4o.indent_spaces); |
|
1102 |
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
|
1103 |
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
|
1104 |
generate_c_vardecl_c::global_vt); |
70 | 1105 |
vardecl->print(symbol); |
1106 |
delete vardecl; |
|
1107 |
s4o.print("\n"); |
|
1108 |
||
1109 |
/* (B.3) Resources initializations... */ |
|
1110 |
wanted_declaretype = initdeclare_dt; |
|
1111 |
symbol->resource_declarations->accept(*this); |
|
1112 |
||
1113 |
s4o.indent_left(); |
|
1114 |
s4o.print(s4o.indent_spaces + "}\n\n"); |
|
1115 |
||
1116 |
||
1117 |
/* (C) Run Function*/ |
|
1118 |
/* (C.1) Resources run functions protos... */ |
|
1119 |
wanted_declaretype = runprotos_dt; |
|
1120 |
symbol->resource_declarations->accept(*this); |
|
1121 |
s4o.print("\n"); |
|
1122 |
||
1123 |
/* (C.2) Run function name... */ |
|
1124 |
s4o.print(s4o.indent_spaces + "void config"); |
|
1125 |
s4o.print(FB_RUN_SUFFIX); |
|
210
8387cac2aba6
Adding support for forcing tick count to return to zero as the same time than all tasks firing are synchronized
laurent
parents:
202
diff
changeset
|
1126 |
s4o.print("(unsigned long tick) {\n"); |
70 | 1127 |
s4o.indent_right(); |
1128 |
||
1129 |
/* (C.3) Resources initializations... */ |
|
1130 |
wanted_declaretype = rundeclare_dt; |
|
1131 |
symbol->resource_declarations->accept(*this); |
|
1132 |
||
1133 |
/* (C.3) Close Public Function body */ |
|
1134 |
s4o.indent_left(); |
|
1135 |
s4o.print(s4o.indent_spaces + "}\n"); |
|
1136 |
||
1137 |
return NULL; |
|
1138 |
} |
|
1139 |
||
1140 |
void *visit(resource_declaration_c *symbol) { |
|
1141 |
if (wanted_declaretype == initprotos_dt || wanted_declaretype == runprotos_dt) { |
|
1142 |
s4o.print(s4o.indent_spaces + "void "); |
|
1143 |
symbol->resource_name->accept(*this); |
|
1144 |
if (wanted_declaretype == initprotos_dt) { |
|
1145 |
s4o.print(FB_INIT_SUFFIX); |
|
1146 |
s4o.print("(void);\n"); |
|
1147 |
} |
|
1148 |
else { |
|
1149 |
s4o.print(FB_RUN_SUFFIX); |
|
210
8387cac2aba6
Adding support for forcing tick count to return to zero as the same time than all tasks firing are synchronized
laurent
parents:
202
diff
changeset
|
1150 |
s4o.print("(unsigned long tick);\n"); |
70 | 1151 |
} |
1152 |
} |
|
1153 |
if (wanted_declaretype == initdeclare_dt || wanted_declaretype == rundeclare_dt) { |
|
1154 |
s4o.print(s4o.indent_spaces); |
|
1155 |
symbol->resource_name->accept(*this); |
|
1156 |
if (wanted_declaretype == initdeclare_dt) { |
|
1157 |
s4o.print(FB_INIT_SUFFIX); |
|
1158 |
s4o.print("();\n"); |
|
1159 |
} |
|
1160 |
else { |
|
1161 |
s4o.print(FB_RUN_SUFFIX); |
|
1162 |
s4o.print("(tick);\n"); |
|
1163 |
} |
|
1164 |
} |
|
1165 |
return NULL; |
|
1166 |
} |
|
1167 |
||
1168 |
void *visit(single_resource_declaration_c *symbol) { |
|
1169 |
if (wanted_declaretype == initprotos_dt || wanted_declaretype == runprotos_dt) { |
|
1170 |
s4o.print(s4o.indent_spaces + "void RESOURCE"); |
|
1171 |
if (wanted_declaretype == initprotos_dt) { |
|
1172 |
s4o.print(FB_INIT_SUFFIX); |
|
1173 |
s4o.print("(void);\n"); |
|
1174 |
} |
|
1175 |
else { |
|
1176 |
s4o.print(FB_RUN_SUFFIX); |
|
210
8387cac2aba6
Adding support for forcing tick count to return to zero as the same time than all tasks firing are synchronized
laurent
parents:
202
diff
changeset
|
1177 |
s4o.print("(unsigned long tick);\n"); |
70 | 1178 |
} |
1179 |
} |
|
1180 |
if (wanted_declaretype == initdeclare_dt || wanted_declaretype == rundeclare_dt) { |
|
1181 |
s4o.print(s4o.indent_spaces + "RESOURCE"); |
|
1182 |
if (wanted_declaretype == initdeclare_dt) { |
|
1183 |
s4o.print(FB_INIT_SUFFIX); |
|
1184 |
s4o.print("();\n"); |
|
1185 |
} |
|
1186 |
else { |
|
1187 |
s4o.print(FB_RUN_SUFFIX); |
|
1188 |
s4o.print("(tick);\n"); |
|
1189 |
} |
|
1190 |
} |
|
1191 |
return NULL; |
|
1192 |
} |
|
1193 |
||
1194 |
}; |
|
1195 |
||
1196 |
/***********************************************************************/ |
|
1197 |
/***********************************************************************/ |
|
1198 |
/***********************************************************************/ |
|
1199 |
/***********************************************************************/ |
|
1200 |
/***********************************************************************/ |
|
1201 |
/***********************************************************************/ |
|
1202 |
/***********************************************************************/ |
|
1203 |
/***********************************************************************/ |
|
1204 |
||
1205 |
||
1206 |
class generate_c_resources_c: public generate_c_typedecl_c { |
|
1207 |
||
1208 |
search_var_instance_decl_c *search_config_instance; |
|
1209 |
search_var_instance_decl_c *search_resource_instance; |
|
1210 |
||
1211 |
private: |
|
1212 |
/* The name of the resource curretnly being processed... */ |
|
1213 |
symbol_c *current_resource_name; |
|
184 | 1214 |
symbol_c *current_task_name; |
70 | 1215 |
symbol_c *current_global_vars; |
267
0a1204bcc9af
starting to add proper header file with declaration of standard functions
Mario de Sousa <msousa@fe.up.pt>
parents:
262
diff
changeset
|
1216 |
stage4out_c *s4o_ptr; |
70 | 1217 |
|
1218 |
public: |
|
1219 |
generate_c_resources_c(stage4out_c *s4o_ptr, symbol_c *config_scope, symbol_c *resource_scope, unsigned long time) |
|
1220 |
: 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
|
1221 |
search_config_instance = new search_var_instance_decl_c(config_scope); |
70 | 1222 |
search_resource_instance = new search_var_instance_decl_c(resource_scope); |
1223 |
common_ticktime = time; |
|
1224 |
current_resource_name = NULL; |
|
184 | 1225 |
current_task_name = NULL; |
70 | 1226 |
current_global_vars = NULL; |
267
0a1204bcc9af
starting to add proper header file with declaration of standard functions
Mario de Sousa <msousa@fe.up.pt>
parents:
262
diff
changeset
|
1227 |
generate_c_resources_c::s4o_ptr = s4o_ptr; |
70 | 1228 |
}; |
267
0a1204bcc9af
starting to add proper header file with declaration of standard functions
Mario de Sousa <msousa@fe.up.pt>
parents:
262
diff
changeset
|
1229 |
|
70 | 1230 |
virtual ~generate_c_resources_c(void) { |
1231 |
delete search_config_instance; |
|
1232 |
delete search_resource_instance; |
|
1233 |
} |
|
1234 |
||
1235 |
typedef enum { |
|
1236 |
declare_dt, |
|
1237 |
init_dt, |
|
1238 |
run_dt |
|
1239 |
} declaretype_t; |
|
1240 |
||
1241 |
declaretype_t wanted_declaretype; |
|
1242 |
||
1243 |
unsigned long common_ticktime; |
|
1244 |
||
1245 |
const char *current_program_name; |
|
1246 |
||
1247 |
typedef enum { |
|
1248 |
assign_at, |
|
1249 |
send_at |
|
1250 |
} assigntype_t; |
|
1251 |
||
1252 |
assigntype_t wanted_assigntype; |
|
1253 |
||
221
c6aed7e5f070
Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents:
217
diff
changeset
|
1254 |
/* the qualifier of variables that need to be processed... */ |
c6aed7e5f070
Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents:
217
diff
changeset
|
1255 |
static const unsigned int none_vq = 0x0000; |
c6aed7e5f070
Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents:
217
diff
changeset
|
1256 |
static const unsigned int constant_vq = 0x0001; // CONSTANT |
c6aed7e5f070
Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents:
217
diff
changeset
|
1257 |
static const unsigned int retain_vq = 0x0002; // RETAIN |
c6aed7e5f070
Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents:
217
diff
changeset
|
1258 |
static const unsigned int non_retain_vq = 0x0004; // NON_RETAIN |
c6aed7e5f070
Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents:
217
diff
changeset
|
1259 |
|
c6aed7e5f070
Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents:
217
diff
changeset
|
1260 |
/* variable used to store the qualifier of program currently being processed... */ |
c6aed7e5f070
Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents:
217
diff
changeset
|
1261 |
unsigned int current_varqualifier; |
c6aed7e5f070
Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents:
217
diff
changeset
|
1262 |
|
c6aed7e5f070
Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents:
217
diff
changeset
|
1263 |
void *print_retain(void) { |
c6aed7e5f070
Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents:
217
diff
changeset
|
1264 |
s4o.print(","); |
c6aed7e5f070
Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents:
217
diff
changeset
|
1265 |
switch (current_varqualifier) { |
c6aed7e5f070
Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents:
217
diff
changeset
|
1266 |
case retain_vq: |
c6aed7e5f070
Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents:
217
diff
changeset
|
1267 |
s4o.print("1"); |
c6aed7e5f070
Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents:
217
diff
changeset
|
1268 |
break; |
c6aed7e5f070
Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents:
217
diff
changeset
|
1269 |
case non_retain_vq: |
c6aed7e5f070
Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents:
217
diff
changeset
|
1270 |
s4o.print("0"); |
c6aed7e5f070
Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents:
217
diff
changeset
|
1271 |
break; |
c6aed7e5f070
Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents:
217
diff
changeset
|
1272 |
default: |
c6aed7e5f070
Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents:
217
diff
changeset
|
1273 |
s4o.print("retain"); |
c6aed7e5f070
Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents:
217
diff
changeset
|
1274 |
break; |
c6aed7e5f070
Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents:
217
diff
changeset
|
1275 |
} |
c6aed7e5f070
Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents:
217
diff
changeset
|
1276 |
return NULL; |
c6aed7e5f070
Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents:
217
diff
changeset
|
1277 |
} |
c6aed7e5f070
Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents:
217
diff
changeset
|
1278 |
|
267
0a1204bcc9af
starting to add proper header file with declaration of standard functions
Mario de Sousa <msousa@fe.up.pt>
parents:
262
diff
changeset
|
1279 |
|
0a1204bcc9af
starting to add proper header file with declaration of standard functions
Mario de Sousa <msousa@fe.up.pt>
parents:
262
diff
changeset
|
1280 |
/********************/ |
0a1204bcc9af
starting to add proper header file with declaration of standard functions
Mario de Sousa <msousa@fe.up.pt>
parents:
262
diff
changeset
|
1281 |
/* 2.1.6 - Pragmas */ |
0a1204bcc9af
starting to add proper header file with declaration of standard functions
Mario de Sousa <msousa@fe.up.pt>
parents:
262
diff
changeset
|
1282 |
/********************/ |
0a1204bcc9af
starting to add proper header file with declaration of standard functions
Mario de Sousa <msousa@fe.up.pt>
parents:
262
diff
changeset
|
1283 |
void *visit(enable_code_generation_pragma_c * symbol) {s4o_ptr->enable_output(); return NULL;} |
0a1204bcc9af
starting to add proper header file with declaration of standard functions
Mario de Sousa <msousa@fe.up.pt>
parents:
262
diff
changeset
|
1284 |
void *visit(disable_code_generation_pragma_c * symbol) {s4o_ptr->disable_output(); return NULL;} |
0a1204bcc9af
starting to add proper header file with declaration of standard functions
Mario de Sousa <msousa@fe.up.pt>
parents:
262
diff
changeset
|
1285 |
|
0a1204bcc9af
starting to add proper header file with declaration of standard functions
Mario de Sousa <msousa@fe.up.pt>
parents:
262
diff
changeset
|
1286 |
|
221
c6aed7e5f070
Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents:
217
diff
changeset
|
1287 |
/******************************************/ |
c6aed7e5f070
Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents:
217
diff
changeset
|
1288 |
/* B 1.4.3 - Declaration & Initialisation */ |
c6aed7e5f070
Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents:
217
diff
changeset
|
1289 |
/******************************************/ |
c6aed7e5f070
Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents:
217
diff
changeset
|
1290 |
|
c6aed7e5f070
Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents:
217
diff
changeset
|
1291 |
void *visit(constant_option_c *symbol) { |
c6aed7e5f070
Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents:
217
diff
changeset
|
1292 |
current_varqualifier = constant_vq; |
c6aed7e5f070
Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents:
217
diff
changeset
|
1293 |
return NULL; |
c6aed7e5f070
Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents:
217
diff
changeset
|
1294 |
} |
c6aed7e5f070
Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents:
217
diff
changeset
|
1295 |
|
c6aed7e5f070
Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents:
217
diff
changeset
|
1296 |
void *visit(retain_option_c *symbol) { |
c6aed7e5f070
Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents:
217
diff
changeset
|
1297 |
current_varqualifier = retain_vq; |
c6aed7e5f070
Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents:
217
diff
changeset
|
1298 |
return NULL; |
c6aed7e5f070
Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents:
217
diff
changeset
|
1299 |
} |
c6aed7e5f070
Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents:
217
diff
changeset
|
1300 |
|
c6aed7e5f070
Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents:
217
diff
changeset
|
1301 |
void *visit(non_retain_option_c *symbol) { |
c6aed7e5f070
Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents:
217
diff
changeset
|
1302 |
current_varqualifier = non_retain_vq; |
c6aed7e5f070
Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents:
217
diff
changeset
|
1303 |
return NULL; |
c6aed7e5f070
Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents:
217
diff
changeset
|
1304 |
} |
c6aed7e5f070
Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents:
217
diff
changeset
|
1305 |
|
70 | 1306 |
/********************************/ |
1307 |
/* B 1.7 Configuration elements */ |
|
1308 |
/********************************/ |
|
1309 |
||
1310 |
/* |
|
1311 |
RESOURCE resource_name ON resource_type_name |
|
1312 |
optional_global_var_declarations |
|
1313 |
single_resource_declaration |
|
1314 |
END_RESOURCE |
|
1315 |
*/ |
|
1316 |
// SYM_REF4(resource_declaration_c, resource_name, resource_type_name, global_var_declarations, resource_declaration) |
|
1317 |
void *visit(resource_declaration_c *symbol) { |
|
1318 |
current_resource_name = symbol->resource_name; |
|
1319 |
current_global_vars = symbol->global_var_declarations; |
|
1320 |
||
1321 |
symbol->resource_declaration->accept(*this); |
|
1322 |
||
1323 |
current_resource_name = NULL; |
|
1324 |
current_global_vars = NULL; |
|
1325 |
return NULL; |
|
1326 |
} |
|
1327 |
||
1328 |
/* task_configuration_list program_configuration_list */ |
|
1329 |
// SYM_REF2(single_resource_declaration_c, task_configuration_list, program_configuration_list) |
|
1330 |
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
|
1331 |
bool single_resource = current_resource_name == NULL; |
70 | 1332 |
if (single_resource) |
1333 |
current_resource_name = new identifier_c("RESOURCE"); |
|
1334 |
generate_c_vardecl_c *vardecl; |
|
1335 |
||
1336 |
/* Insert the header... */ |
|
1337 |
s4o.print("/*******************************************/\n"); |
|
1338 |
s4o.print("/* FILE GENERATED BY iec2c */\n"); |
|
1339 |
s4o.print("/* Editing this file is not recommended... */\n"); |
|
1340 |
s4o.print("/*******************************************/\n\n"); |
|
1341 |
s4o.print("#include \"iec_std_lib.h\"\n\n"); |
|
1342 |
||
1343 |
/* (A) resource declaration... */ |
|
1344 |
/* (A.1) resource name in comment */ |
|
1345 |
s4o.print("// RESOURCE "); |
|
1346 |
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
|
1347 |
s4o.print("\n\n"); |
74640e3c7f53
Bug with D and L action qualifier and timing management in SFC generated fixed
lbessard
parents:
112
diff
changeset
|
1348 |
|
237 | 1349 |
s4o.print("extern unsigned long long common_ticktime__;\n\n"); |
217
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
210
diff
changeset
|
1350 |
|
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
210
diff
changeset
|
1351 |
s4o.print("#include \"accessor.h\"\n\n"); |
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
210
diff
changeset
|
1352 |
|
70 | 1353 |
/* (A.2) Global variables... */ |
1354 |
if (current_global_vars != NULL) { |
|
1355 |
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
|
1356 |
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
|
1357 |
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
|
1358 |
current_resource_name); |
70 | 1359 |
vardecl->print(current_global_vars); |
1360 |
delete vardecl; |
|
120
74640e3c7f53
Bug with D and L action qualifier and timing management in SFC generated fixed
lbessard
parents:
112
diff
changeset
|
1361 |
s4o.print("\n"); |
70 | 1362 |
} |
1363 |
||
1364 |
/* (A.3) POUs inclusion */ |
|
121
9e8ce092e169
Adding support for POU struct definition in POUS.h
lbessard
parents:
120
diff
changeset
|
1365 |
s4o.print("#include \"POUS.h\"\n\n"); |
70 | 1366 |
s4o.print("#include \"POUS.c\"\n\n"); |
1367 |
||
184 | 1368 |
wanted_declaretype = declare_dt; |
1369 |
||
70 | 1370 |
/* (A.4) Resource programs declaration... */ |
184 | 1371 |
symbol->task_configuration_list->accept(*this); |
1372 |
||
1373 |
/* (A.5) Resource programs declaration... */ |
|
70 | 1374 |
symbol->program_configuration_list->accept(*this); |
184 | 1375 |
|
70 | 1376 |
s4o.print("\n"); |
1377 |
||
1378 |
/* (B) resource initialisation function... */ |
|
1379 |
/* (B.1) initialisation function name... */ |
|
1380 |
s4o.print("void "); |
|
1381 |
current_resource_name->accept(*this); |
|
1382 |
s4o.print(FB_INIT_SUFFIX); |
|
1383 |
s4o.print("(void) {\n"); |
|
1384 |
s4o.indent_right(); |
|
221
c6aed7e5f070
Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents:
217
diff
changeset
|
1385 |
s4o.print(s4o.indent_spaces); |
255
86ef2244aef1
Modify code generated for avoiding warning while compiling when configuration or resource don't have any global variable
laurent
parents:
250
diff
changeset
|
1386 |
s4o.print("BOOL retain;\n"); |
86ef2244aef1
Modify code generated for avoiding warning while compiling when configuration or resource don't have any global variable
laurent
parents:
250
diff
changeset
|
1387 |
s4o.print(s4o.indent_spaces); |
86ef2244aef1
Modify code generated for avoiding warning while compiling when configuration or resource don't have any global variable
laurent
parents:
250
diff
changeset
|
1388 |
s4o.print("retain = 0;\n"); |
70 | 1389 |
|
1390 |
/* (B.2) Global variables initialisations... */ |
|
1391 |
if (current_global_vars != NULL) { |
|
1392 |
s4o.print(s4o.indent_spaces); |
|
1393 |
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
|
1394 |
generate_c_vardecl_c::constructorinit_vf, |
241
0ba6d614573e
Bug with code generation of global located variables in resource fixed thanks to Mario
laurent
parents:
237
diff
changeset
|
1395 |
generate_c_vardecl_c::global_vt, |
0ba6d614573e
Bug with code generation of global located variables in resource fixed thanks to Mario
laurent
parents:
237
diff
changeset
|
1396 |
current_resource_name); |
70 | 1397 |
vardecl->print(current_global_vars); |
1398 |
delete vardecl; |
|
1399 |
} |
|
1400 |
s4o.print("\n"); |
|
1401 |
||
1402 |
wanted_declaretype = init_dt; |
|
184 | 1403 |
|
1404 |
/* (B.3) Tasks initialisations... */ |
|
1405 |
symbol->task_configuration_list->accept(*this); |
|
1406 |
||
1407 |
/* (B.4) Resource programs initialisations... */ |
|
70 | 1408 |
symbol->program_configuration_list->accept(*this); |
1409 |
||
1410 |
s4o.indent_left(); |
|
1411 |
s4o.print("}\n\n"); |
|
1412 |
||
1413 |
/* (C) Resource run function... */ |
|
1414 |
/* (C.1) Run function name... */ |
|
1415 |
s4o.print("void "); |
|
1416 |
current_resource_name->accept(*this); |
|
1417 |
s4o.print(FB_RUN_SUFFIX); |
|
210
8387cac2aba6
Adding support for forcing tick count to return to zero as the same time than all tasks firing are synchronized
laurent
parents:
202
diff
changeset
|
1418 |
s4o.print("(unsigned long tick) {\n"); |
70 | 1419 |
s4o.indent_right(); |
1420 |
||
184 | 1421 |
wanted_declaretype = run_dt; |
1422 |
||
70 | 1423 |
/* (C.2) Task management... */ |
1424 |
symbol->task_configuration_list->accept(*this); |
|
1425 |
||
1426 |
/* (C.3) Program run declaration... */ |
|
1427 |
symbol->program_configuration_list->accept(*this); |
|
1428 |
||
1429 |
s4o.indent_left(); |
|
1430 |
s4o.print("}\n\n"); |
|
1431 |
||
1432 |
if (single_resource) { |
|
1433 |
delete current_resource_name; |
|
1434 |
current_resource_name = NULL; |
|
1435 |
} |
|
1436 |
return NULL; |
|
1437 |
} |
|
1438 |
||
1439 |
/* PROGRAM [RETAIN | NON_RETAIN] program_name [WITH task_name] ':' program_type_name ['(' prog_conf_elements ')'] */ |
|
1440 |
//SYM_REF6(program_configuration_c, retain_option, program_name, task_name, program_type_name, prog_conf_elements, unused) |
|
1441 |
void *visit(program_configuration_c *symbol) { |
|
184 | 1442 |
switch (wanted_declaretype) { |
1443 |
case declare_dt: |
|
70 | 1444 |
s4o.print(s4o.indent_spaces); |
184 | 1445 |
symbol->program_type_name->accept(*this); |
1446 |
s4o.print(" "); |
|
1447 |
current_resource_name->accept(*this); |
|
1448 |
s4o.print("__"); |
|
1449 |
symbol->program_name->accept(*this); |
|
1450 |
s4o.print(";\n#define "); |
|
1451 |
symbol->program_name->accept(*this); |
|
1452 |
s4o.print(" "); |
|
1453 |
current_resource_name->accept(*this); |
|
1454 |
s4o.print("__"); |
|
1455 |
symbol->program_name->accept(*this); |
|
1456 |
s4o.print("\n"); |
|
1457 |
break; |
|
1458 |
case init_dt: |
|
221
c6aed7e5f070
Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents:
217
diff
changeset
|
1459 |
if (symbol->retain_option != NULL) |
c6aed7e5f070
Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents:
217
diff
changeset
|
1460 |
symbol->retain_option->accept(*this); |
184 | 1461 |
s4o.print(s4o.indent_spaces); |
1462 |
symbol->program_type_name->accept(*this); |
|
1463 |
s4o.print(FB_INIT_SUFFIX); |
|
1464 |
s4o.print("(&"); |
|
1465 |
symbol->program_name->accept(*this); |
|
221
c6aed7e5f070
Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents:
217
diff
changeset
|
1466 |
print_retain(); |
184 | 1467 |
s4o.print(");\n"); |
1468 |
break; |
|
1469 |
case run_dt: |
|
1470 |
current_program_name = ((identifier_c*)(symbol->program_name))->value; |
|
1471 |
if (symbol->task_name != NULL) { |
|
1472 |
s4o.print(s4o.indent_spaces); |
|
1473 |
s4o.print("if ("); |
|
1474 |
symbol->task_name->accept(*this); |
|
1475 |
s4o.print(") {\n"); |
|
1476 |
s4o.indent_right(); |
|
1477 |
} |
|
70 | 1478 |
|
184 | 1479 |
wanted_assigntype = assign_at; |
1480 |
if (symbol->prog_conf_elements != NULL) |
|
1481 |
symbol->prog_conf_elements->accept(*this); |
|
1482 |
||
1483 |
s4o.print(s4o.indent_spaces); |
|
1484 |
symbol->program_type_name->accept(*this); |
|
1485 |
s4o.print(FB_FUNCTION_SUFFIX); |
|
1486 |
s4o.print("(&"); |
|
1487 |
symbol->program_name->accept(*this); |
|
1488 |
s4o.print(");\n"); |
|
1489 |
||
1490 |
wanted_assigntype = send_at; |
|
1491 |
if (symbol->prog_conf_elements != NULL) |
|
1492 |
symbol->prog_conf_elements->accept(*this); |
|
1493 |
||
1494 |
if (symbol->task_name != NULL) { |
|
1495 |
s4o.indent_left(); |
|
1496 |
s4o.print(s4o.indent_spaces + "}\n"); |
|
1497 |
} |
|
1498 |
break; |
|
1499 |
default: |
|
1500 |
break; |
|
70 | 1501 |
} |
1502 |
return NULL; |
|
1503 |
} |
|
1504 |
||
1505 |
/* TASK task_name task_initialization */ |
|
1506 |
//SYM_REF2(task_configuration_c, task_name, task_initialization) |
|
1507 |
void *visit(task_configuration_c *symbol) { |
|
184 | 1508 |
current_task_name = symbol->task_name; |
1509 |
switch (wanted_declaretype) { |
|
1510 |
case declare_dt: |
|
1511 |
s4o.print(s4o.indent_spaces + "BOOL "); |
|
1512 |
current_task_name->accept(*this); |
|
1513 |
s4o.print(";\n"); |
|
1514 |
symbol->task_initialization->accept(*this); |
|
1515 |
break; |
|
1516 |
case init_dt: |
|
1517 |
s4o.print(s4o.indent_spaces); |
|
1518 |
current_task_name->accept(*this); |
|
1519 |
s4o.print(" = __BOOL_LITERAL(FALSE);\n"); |
|
1520 |
symbol->task_initialization->accept(*this); |
|
1521 |
break; |
|
1522 |
case run_dt: |
|
1523 |
symbol->task_initialization->accept(*this); |
|
1524 |
break; |
|
1525 |
default: |
|
1526 |
break; |
|
1527 |
} |
|
1528 |
current_task_name = NULL; |
|
70 | 1529 |
return NULL; |
1530 |
} |
|
1531 |
||
1532 |
/* '(' [SINGLE ASSIGN data_source ','] [INTERVAL ASSIGN data_source ','] PRIORITY ASSIGN integer ')' */ |
|
1533 |
//SYM_REF4(task_initialization_c, single_data_source, interval_data_source, priority_data_source, unused) |
|
1534 |
void *visit(task_initialization_c *symbol) { |
|
184 | 1535 |
switch (wanted_declaretype) { |
1536 |
case declare_dt: |
|
1537 |
if (symbol->single_data_source != NULL) { |
|
1538 |
s4o.print(s4o.indent_spaces + "R_TRIG "); |
|
1539 |
current_task_name->accept(*this); |
|
1540 |
s4o.print("_R_TRIG;\n"); |
|
1541 |
} |
|
1542 |
break; |
|
1543 |
case init_dt: |
|
1544 |
if (symbol->single_data_source != NULL) { |
|
1545 |
s4o.print(s4o.indent_spaces + "R_TRIG"); |
|
1546 |
s4o.print(FB_INIT_SUFFIX); |
|
1547 |
s4o.print("(&"); |
|
1548 |
current_task_name->accept(*this); |
|
248
9cb18ec14fa2
Task single parameter code generation broken after addition of accessors fixed.
laurent
parents:
244
diff
changeset
|
1549 |
s4o.print("_R_TRIG, retain);\n"); |
184 | 1550 |
} |
1551 |
break; |
|
1552 |
case run_dt: |
|
1553 |
if (symbol->single_data_source != NULL) { |
|
1554 |
symbol_c *config_var_decl = NULL; |
|
1555 |
symbol_c *res_var_decl = NULL; |
|
248
9cb18ec14fa2
Task single parameter code generation broken after addition of accessors fixed.
laurent
parents:
244
diff
changeset
|
1556 |
unsigned int vartype; |
184 | 1557 |
symbol_c *current_var_reference = ((global_var_reference_c *)(symbol->single_data_source))->global_var_name; |
1558 |
res_var_decl = search_resource_instance->get_decl(current_var_reference); |
|
1559 |
if (res_var_decl == NULL) { |
|
1560 |
config_var_decl = search_config_instance->get_decl(current_var_reference); |
|
1561 |
if (config_var_decl == NULL) |
|
1562 |
ERROR; |
|
248
9cb18ec14fa2
Task single parameter code generation broken after addition of accessors fixed.
laurent
parents:
244
diff
changeset
|
1563 |
vartype = search_config_instance->get_vartype(); |
184 | 1564 |
s4o.print(s4o.indent_spaces + "{extern "); |
1565 |
config_var_decl->accept(*this); |
|
1566 |
s4o.print(" *"); |
|
1567 |
symbol->single_data_source->accept(*this); |
|
1568 |
s4o.print("; "); |
|
1569 |
} |
|
248
9cb18ec14fa2
Task single parameter code generation broken after addition of accessors fixed.
laurent
parents:
244
diff
changeset
|
1570 |
else { |
9cb18ec14fa2
Task single parameter code generation broken after addition of accessors fixed.
laurent
parents:
244
diff
changeset
|
1571 |
vartype = search_resource_instance->get_vartype(); |
184 | 1572 |
s4o.print(s4o.indent_spaces); |
248
9cb18ec14fa2
Task single parameter code generation broken after addition of accessors fixed.
laurent
parents:
244
diff
changeset
|
1573 |
} |
9cb18ec14fa2
Task single parameter code generation broken after addition of accessors fixed.
laurent
parents:
244
diff
changeset
|
1574 |
s4o.print(SET_VAR); |
9cb18ec14fa2
Task single parameter code generation broken after addition of accessors fixed.
laurent
parents:
244
diff
changeset
|
1575 |
s4o.print("("); |
184 | 1576 |
current_task_name->accept(*this); |
248
9cb18ec14fa2
Task single parameter code generation broken after addition of accessors fixed.
laurent
parents:
244
diff
changeset
|
1577 |
s4o.print("_R_TRIG.CLK, *__GET_GLOBAL_"); |
184 | 1578 |
symbol->single_data_source->accept(*this); |
248
9cb18ec14fa2
Task single parameter code generation broken after addition of accessors fixed.
laurent
parents:
244
diff
changeset
|
1579 |
s4o.print("());"); |
184 | 1580 |
if (config_var_decl != NULL) |
1581 |
s4o.print("}"); |
|
1582 |
s4o.print("\n"); |
|
1583 |
s4o.print(s4o.indent_spaces + "R_TRIG"); |
|
1584 |
s4o.print(FB_FUNCTION_SUFFIX); |
|
1585 |
s4o.print("(&"); |
|
1586 |
current_task_name->accept(*this); |
|
1587 |
s4o.print("_R_TRIG);\n"); |
|
1588 |
s4o.print(s4o.indent_spaces); |
|
1589 |
current_task_name->accept(*this); |
|
1590 |
s4o.print(" = "); |
|
248
9cb18ec14fa2
Task single parameter code generation broken after addition of accessors fixed.
laurent
parents:
244
diff
changeset
|
1591 |
s4o.print(GET_VAR); |
9cb18ec14fa2
Task single parameter code generation broken after addition of accessors fixed.
laurent
parents:
244
diff
changeset
|
1592 |
s4o.print("("); |
184 | 1593 |
current_task_name->accept(*this); |
248
9cb18ec14fa2
Task single parameter code generation broken after addition of accessors fixed.
laurent
parents:
244
diff
changeset
|
1594 |
s4o.print("_R_TRIG.Q)"); |
184 | 1595 |
} |
1596 |
else { |
|
1597 |
s4o.print(s4o.indent_spaces); |
|
1598 |
current_task_name->accept(*this); |
|
1599 |
s4o.print(" = "); |
|
1600 |
if (symbol->interval_data_source != NULL) { |
|
1601 |
calculate_time_c calculate_time; |
|
1602 |
symbol->interval_data_source->accept(calculate_time); |
|
1603 |
unsigned long time = calculate_time.get_time(); |
|
1604 |
if (time != 0) { |
|
1605 |
s4o.print("!(tick % "); |
|
1606 |
s4o.print_integer((int)(time / common_ticktime)); |
|
1607 |
s4o.print(")"); |
|
1608 |
} |
|
1609 |
else |
|
1610 |
s4o.print("1"); |
|
1611 |
} |
|
1612 |
else |
|
1613 |
s4o.print("1"); |
|
1614 |
} |
|
1615 |
s4o.print(";\n"); |
|
1616 |
break; |
|
1617 |
default: |
|
1618 |
break; |
|
70 | 1619 |
} |
1620 |
return NULL; |
|
1621 |
} |
|
1622 |
||
1623 |
/* any_symbolic_variable ASSIGN prog_data_source */ |
|
1624 |
//SYM_REF2(prog_cnxn_assign_c, symbolic_variable, prog_data_source) |
|
1625 |
void *visit(prog_cnxn_assign_c *symbol) { |
|
1626 |
if (wanted_assigntype == assign_at) { |
|
1627 |
symbol_c *var_decl; |
|
1628 |
unsigned int vartype = 0; |
|
1629 |
symbol_c *current_var_reference = ((global_var_reference_c *)(symbol->prog_data_source))->global_var_name; |
|
1630 |
var_decl = search_resource_instance->get_decl(current_var_reference); |
|
1631 |
if (var_decl == NULL) { |
|
1632 |
var_decl = search_config_instance->get_decl(current_var_reference); |
|
1633 |
if (var_decl == NULL) |
|
1634 |
ERROR; |
|
1635 |
else |
|
1636 |
vartype = search_config_instance->get_vartype(); |
|
1637 |
} |
|
1638 |
else |
|
1639 |
vartype = search_resource_instance->get_vartype(); |
|
1640 |
||
1641 |
s4o.print(s4o.indent_spaces + "{extern "); |
|
1642 |
var_decl->accept(*this); |
|
202
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
189
diff
changeset
|
1643 |
s4o.print(" *"); |
70 | 1644 |
symbol->prog_data_source->accept(*this); |
1645 |
s4o.print("; "); |
|
202
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
189
diff
changeset
|
1646 |
s4o.printupper(current_program_name); |
70 | 1647 |
s4o.print("."); |
1648 |
symbol->symbolic_variable->accept(*this); |
|
1649 |
s4o.print(" = "); |
|
1650 |
if (vartype || search_var_instance_decl_c::global_vt) |
|
1651 |
s4o.print("*"); |
|
1652 |
symbol->prog_data_source->accept(*this); |
|
1653 |
s4o.print(";}\n"); |
|
1654 |
} |
|
1655 |
return NULL; |
|
1656 |
} |
|
1657 |
||
1658 |
/* any_symbolic_variable SENDTO data_sink */ |
|
1659 |
//SYM_REF2(prog_cnxn_sendto_c, symbolic_variable, data_sink) |
|
1660 |
void *visit(prog_cnxn_sendto_c *symbol) { |
|
1661 |
if (wanted_assigntype == send_at) { |
|
1662 |
symbol_c *var_decl; |
|
1663 |
unsigned int vartype = 0; |
|
1664 |
symbol_c *current_var_reference = ((global_var_reference_c *)(symbol->data_sink))->global_var_name; |
|
1665 |
var_decl = search_resource_instance->get_decl(current_var_reference); |
|
1666 |
if (var_decl == NULL) { |
|
1667 |
var_decl = search_config_instance->get_decl(current_var_reference); |
|
1668 |
if (var_decl == NULL) |
|
1669 |
ERROR; |
|
1670 |
else |
|
1671 |
vartype = search_config_instance->get_vartype(); |
|
1672 |
} |
|
1673 |
else |
|
1674 |
vartype = search_resource_instance->get_vartype(); |
|
1675 |
||
1676 |
s4o.print(s4o.indent_spaces + "{extern "); |
|
1677 |
var_decl->accept(*this); |
|
202
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
189
diff
changeset
|
1678 |
s4o.print(" *"); |
70 | 1679 |
symbol->data_sink->accept(*this); |
1680 |
s4o.print("; "); |
|
1681 |
if (vartype || search_var_instance_decl_c::global_vt) |
|
1682 |
s4o.print("*"); |
|
1683 |
symbol->data_sink->accept(*this); |
|
1684 |
s4o.print(" = "); |
|
202
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
189
diff
changeset
|
1685 |
s4o.printupper(current_program_name); |
70 | 1686 |
s4o.print("."); |
1687 |
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
|
1688 |
s4o.print(";};\n"); |
70 | 1689 |
} |
1690 |
return NULL; |
|
1691 |
} |
|
1692 |
||
1693 |
}; |
|
1694 |
||
1695 |
/***********************************************************************/ |
|
1696 |
/***********************************************************************/ |
|
1697 |
/***********************************************************************/ |
|
1698 |
/***********************************************************************/ |
|
1699 |
/***********************************************************************/ |
|
1700 |
/***********************************************************************/ |
|
1701 |
/***********************************************************************/ |
|
1702 |
/***********************************************************************/ |
|
1703 |
||
1704 |
class generate_c_c: public iterator_visitor_c { |
|
1705 |
protected: |
|
1706 |
stage4out_c &s4o; |
|
1707 |
stage4out_c pous_s4o; |
|
121
9e8ce092e169
Adding support for POU struct definition in POUS.h
lbessard
parents:
120
diff
changeset
|
1708 |
stage4out_c pous_incl_s4o; |
70 | 1709 |
stage4out_c located_variables_s4o; |
111 | 1710 |
stage4out_c variables_s4o; |
70 | 1711 |
generate_c_pous_c generate_c_pous; |
111 | 1712 |
|
70 | 1713 |
symbol_c *current_configuration; |
1714 |
||
1715 |
const char *current_name; |
|
1716 |
const char *current_builddir; |
|
1717 |
||
237 | 1718 |
unsigned long long common_ticktime; |
70 | 1719 |
|
1720 |
public: |
|
1721 |
generate_c_c(stage4out_c *s4o_ptr, const char *builddir): |
|
1722 |
s4o(*s4o_ptr), |
|
1723 |
pous_s4o(builddir, "POUS", "c"), |
|
121
9e8ce092e169
Adding support for POU struct definition in POUS.h
lbessard
parents:
120
diff
changeset
|
1724 |
pous_incl_s4o(builddir, "POUS", "h"), |
70 | 1725 |
located_variables_s4o(builddir, "LOCATED_VARIABLES","h"), |
112 | 1726 |
variables_s4o(builddir, "VARIABLES","csv"), |
121
9e8ce092e169
Adding support for POU struct definition in POUS.h
lbessard
parents:
120
diff
changeset
|
1727 |
generate_c_pous(&pous_s4o, &pous_incl_s4o) { |
70 | 1728 |
current_builddir = builddir; |
1729 |
current_configuration = NULL; |
|
1730 |
} |
|
1731 |
||
1732 |
~generate_c_c(void) {} |
|
1733 |
||
267
0a1204bcc9af
starting to add proper header file with declaration of standard functions
Mario de Sousa <msousa@fe.up.pt>
parents:
262
diff
changeset
|
1734 |
|
0a1204bcc9af
starting to add proper header file with declaration of standard functions
Mario de Sousa <msousa@fe.up.pt>
parents:
262
diff
changeset
|
1735 |
|
0a1204bcc9af
starting to add proper header file with declaration of standard functions
Mario de Sousa <msousa@fe.up.pt>
parents:
262
diff
changeset
|
1736 |
/********************/ |
0a1204bcc9af
starting to add proper header file with declaration of standard functions
Mario de Sousa <msousa@fe.up.pt>
parents:
262
diff
changeset
|
1737 |
/* 2.1.6 - Pragmas */ |
0a1204bcc9af
starting to add proper header file with declaration of standard functions
Mario de Sousa <msousa@fe.up.pt>
parents:
262
diff
changeset
|
1738 |
/********************/ |
0a1204bcc9af
starting to add proper header file with declaration of standard functions
Mario de Sousa <msousa@fe.up.pt>
parents:
262
diff
changeset
|
1739 |
void *visit(enable_code_generation_pragma_c * symbol) { |
0a1204bcc9af
starting to add proper header file with declaration of standard functions
Mario de Sousa <msousa@fe.up.pt>
parents:
262
diff
changeset
|
1740 |
s4o .enable_output(); |
0a1204bcc9af
starting to add proper header file with declaration of standard functions
Mario de Sousa <msousa@fe.up.pt>
parents:
262
diff
changeset
|
1741 |
pous_s4o .enable_output(); |
0a1204bcc9af
starting to add proper header file with declaration of standard functions
Mario de Sousa <msousa@fe.up.pt>
parents:
262
diff
changeset
|
1742 |
pous_incl_s4o .enable_output(); |
0a1204bcc9af
starting to add proper header file with declaration of standard functions
Mario de Sousa <msousa@fe.up.pt>
parents:
262
diff
changeset
|
1743 |
located_variables_s4o.enable_output(); |
0a1204bcc9af
starting to add proper header file with declaration of standard functions
Mario de Sousa <msousa@fe.up.pt>
parents:
262
diff
changeset
|
1744 |
variables_s4o .enable_output(); |
0a1204bcc9af
starting to add proper header file with declaration of standard functions
Mario de Sousa <msousa@fe.up.pt>
parents:
262
diff
changeset
|
1745 |
return NULL; |
0a1204bcc9af
starting to add proper header file with declaration of standard functions
Mario de Sousa <msousa@fe.up.pt>
parents:
262
diff
changeset
|
1746 |
} |
0a1204bcc9af
starting to add proper header file with declaration of standard functions
Mario de Sousa <msousa@fe.up.pt>
parents:
262
diff
changeset
|
1747 |
|
0a1204bcc9af
starting to add proper header file with declaration of standard functions
Mario de Sousa <msousa@fe.up.pt>
parents:
262
diff
changeset
|
1748 |
void *visit(disable_code_generation_pragma_c * symbol) { |
0a1204bcc9af
starting to add proper header file with declaration of standard functions
Mario de Sousa <msousa@fe.up.pt>
parents:
262
diff
changeset
|
1749 |
s4o .disable_output(); |
0a1204bcc9af
starting to add proper header file with declaration of standard functions
Mario de Sousa <msousa@fe.up.pt>
parents:
262
diff
changeset
|
1750 |
pous_s4o .disable_output(); |
0a1204bcc9af
starting to add proper header file with declaration of standard functions
Mario de Sousa <msousa@fe.up.pt>
parents:
262
diff
changeset
|
1751 |
pous_incl_s4o .disable_output(); |
0a1204bcc9af
starting to add proper header file with declaration of standard functions
Mario de Sousa <msousa@fe.up.pt>
parents:
262
diff
changeset
|
1752 |
located_variables_s4o.disable_output(); |
0a1204bcc9af
starting to add proper header file with declaration of standard functions
Mario de Sousa <msousa@fe.up.pt>
parents:
262
diff
changeset
|
1753 |
variables_s4o .disable_output(); |
0a1204bcc9af
starting to add proper header file with declaration of standard functions
Mario de Sousa <msousa@fe.up.pt>
parents:
262
diff
changeset
|
1754 |
return NULL; |
0a1204bcc9af
starting to add proper header file with declaration of standard functions
Mario de Sousa <msousa@fe.up.pt>
parents:
262
diff
changeset
|
1755 |
} |
0a1204bcc9af
starting to add proper header file with declaration of standard functions
Mario de Sousa <msousa@fe.up.pt>
parents:
262
diff
changeset
|
1756 |
|
0a1204bcc9af
starting to add proper header file with declaration of standard functions
Mario de Sousa <msousa@fe.up.pt>
parents:
262
diff
changeset
|
1757 |
|
70 | 1758 |
/***************************/ |
1759 |
/* B 0 - Programming Model */ |
|
1760 |
/***************************/ |
|
1761 |
void *visit(library_c *symbol) { |
|
217
f5dfadf5de54
Adding support for declare, init, get and set macros
laurent
parents:
210
diff
changeset
|
1762 |
pous_incl_s4o.print("#ifndef __POUS_H\n#define __POUS_H\n\n#include \"accessor.h\"\n\n"); |
70 | 1763 |
for(int i = 0; i < symbol->n; i++) { |
1764 |
symbol->elements[i]->accept(*this); |
|
1765 |
} |
|
121
9e8ce092e169
Adding support for POU struct definition in POUS.h
lbessard
parents:
120
diff
changeset
|
1766 |
pous_incl_s4o.print("#endif //__POUS_H\n"); |
9e8ce092e169
Adding support for POU struct definition in POUS.h
lbessard
parents:
120
diff
changeset
|
1767 |
|
9e8ce092e169
Adding support for POU struct definition in POUS.h
lbessard
parents:
120
diff
changeset
|
1768 |
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
|
1769 |
generate_var_list.generate_programs(symbol); |
9e8ce092e169
Adding support for POU struct definition in POUS.h
lbessard
parents:
120
diff
changeset
|
1770 |
generate_var_list.generate_variables(symbol); |
111 | 1771 |
|
98 | 1772 |
generate_location_list_c generate_location_list(&located_variables_s4o); |
1773 |
symbol->accept(generate_location_list); |
|
70 | 1774 |
return NULL; |
1775 |
} |
|
1776 |
||
1777 |
/*************************/ |
|
1778 |
/* B.1 - Common elements */ |
|
1779 |
/*************************/ |
|
1780 |
/*******************************************/ |
|
1781 |
/* B 1.1 - Letters, digits and identifiers */ |
|
1782 |
/*******************************************/ |
|
1783 |
void *visit(identifier_c *symbol) { |
|
1784 |
current_name = symbol->value; |
|
1785 |
return NULL; |
|
1786 |
} |
|
1787 |
||
98 | 1788 |
/********************************/ |
1789 |
/* B 1.3.3 - Derived data types */ |
|
1790 |
/********************************/ |
|
1791 |
/* TYPE type_declaration_list END_TYPE */ |
|
1792 |
void *visit(data_type_declaration_c *symbol) { |
|
1793 |
symbol->accept(generate_c_pous); |
|
1794 |
return NULL; |
|
1795 |
} |
|
1796 |
||
70 | 1797 |
/**************************************/ |
1798 |
/* B.1.5 - Program organization units */ |
|
1799 |
/**************************************/ |
|
1800 |
/***********************/ |
|
1801 |
/* B 1.5.1 - Functions */ |
|
1802 |
/***********************/ |
|
1803 |
void *visit(function_declaration_c *symbol) { |
|
1804 |
symbol->accept(generate_c_pous); |
|
1805 |
return NULL; |
|
1806 |
} |
|
1807 |
||
1808 |
/*****************************/ |
|
1809 |
/* B 1.5.2 - Function Blocks */ |
|
1810 |
/*****************************/ |
|
1811 |
void *visit(function_block_declaration_c *symbol) { |
|
1812 |
symbol->accept(generate_c_pous); |
|
1813 |
return NULL; |
|
1814 |
} |
|
1815 |
||
1816 |
/**********************/ |
|
1817 |
/* B 1.5.3 - Programs */ |
|
1818 |
/**********************/ |
|
1819 |
void *visit(program_declaration_c *symbol) { |
|
1820 |
symbol->accept(generate_c_pous); |
|
1821 |
return NULL; |
|
1822 |
} |
|
1823 |
||
1824 |
||
1825 |
/********************************/ |
|
1826 |
/* B 1.7 Configuration elements */ |
|
1827 |
/********************************/ |
|
1828 |
void *visit(configuration_declaration_c *symbol) { |
|
1829 |
static int configuration_count = 0; |
|
1830 |
||
1831 |
if (configuration_count++) { |
|
1832 |
/* the first configuration is the one we will use!! */ |
|
1833 |
ERROR; |
|
1834 |
} |
|
1835 |
||
1836 |
current_configuration = symbol; |
|
1837 |
||
1838 |
calculate_common_ticktime_c calculate_common_ticktime; |
|
1839 |
symbol->accept(calculate_common_ticktime); |
|
210
8387cac2aba6
Adding support for forcing tick count to return to zero as the same time than all tasks firing are synchronized
laurent
parents:
202
diff
changeset
|
1840 |
common_ticktime = calculate_common_ticktime.get_common_ticktime(); |
248
9cb18ec14fa2
Task single parameter code generation broken after addition of accessors fixed.
laurent
parents:
244
diff
changeset
|
1841 |
if (common_ticktime == 0) { |
9cb18ec14fa2
Task single parameter code generation broken after addition of accessors fixed.
laurent
parents:
244
diff
changeset
|
1842 |
fprintf(stderr, "\nYou must at least define a periodic task to set cycle period!"); |
9cb18ec14fa2
Task single parameter code generation broken after addition of accessors fixed.
laurent
parents:
244
diff
changeset
|
1843 |
ERROR; |
9cb18ec14fa2
Task single parameter code generation broken after addition of accessors fixed.
laurent
parents:
244
diff
changeset
|
1844 |
} |
70 | 1845 |
|
1846 |
symbol->configuration_name->accept(*this); |
|
1847 |
stage4out_c config_s4o(current_builddir, current_name, "c"); |
|
1848 |
generate_c_config_c generate_c_config(&config_s4o); |
|
1849 |
symbol->accept(generate_c_config); |
|
1850 |
||
210
8387cac2aba6
Adding support for forcing tick count to return to zero as the same time than all tasks firing are synchronized
laurent
parents:
202
diff
changeset
|
1851 |
config_s4o.print("unsigned long long common_ticktime__ = "); |
8387cac2aba6
Adding support for forcing tick count to return to zero as the same time than all tasks firing are synchronized
laurent
parents:
202
diff
changeset
|
1852 |
config_s4o.print_long_long_integer(common_ticktime); |
8387cac2aba6
Adding support for forcing tick count to return to zero as the same time than all tasks firing are synchronized
laurent
parents:
202
diff
changeset
|
1853 |
config_s4o.print("; /*ns*/\n"); |
8387cac2aba6
Adding support for forcing tick count to return to zero as the same time than all tasks firing are synchronized
laurent
parents:
202
diff
changeset
|
1854 |
config_s4o.print("unsigned long greatest_tick_count__ = "); |
8387cac2aba6
Adding support for forcing tick count to return to zero as the same time than all tasks firing are synchronized
laurent
parents:
202
diff
changeset
|
1855 |
config_s4o.print_long_integer(calculate_common_ticktime.get_greatest_tick_count()); |
8387cac2aba6
Adding support for forcing tick count to return to zero as the same time than all tasks firing are synchronized
laurent
parents:
202
diff
changeset
|
1856 |
config_s4o.print("; /*tick*/\n"); |
70 | 1857 |
|
1858 |
symbol->resource_declarations->accept(*this); |
|
1859 |
||
1860 |
current_configuration = NULL; |
|
1861 |
||
1862 |
return NULL; |
|
1863 |
} |
|
1864 |
||
1865 |
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
|
1866 |
symbol->resource_name->accept(*this); |
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
189
diff
changeset
|
1867 |
stage4out_c resources_s4o(current_builddir, current_name, "c"); |
70 | 1868 |
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
|
1869 |
symbol->accept(generate_c_resources); |
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
189
diff
changeset
|
1870 |
return NULL; |
70 | 1871 |
} |
1872 |
||
1873 |
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
|
1874 |
stage4out_c resources_s4o(current_builddir, "RESOURCE", "c"); |
70 | 1875 |
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
|
1876 |
symbol->accept(generate_c_resources); |
da1a8186f86f
Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents:
189
diff
changeset
|
1877 |
return NULL; |
70 | 1878 |
} |
1879 |
||
1880 |
}; |
|
1881 |
||
1882 |
/***********************************************************************/ |
|
1883 |
/***********************************************************************/ |
|
1884 |
/***********************************************************************/ |
|
1885 |
/***********************************************************************/ |
|
1886 |
/***********************************************************************/ |
|
1887 |
/***********************************************************************/ |
|
1888 |
/***********************************************************************/ |
|
1889 |
/***********************************************************************/ |
|
1890 |
||
1891 |
||
1892 |
||
1893 |
||
1894 |
visitor_c *new_code_generator(stage4out_c *s4o, const char *builddir) {return new generate_c_c(s4o, builddir);} |
|
1895 |
void delete_code_generator(visitor_c *code_generator) {delete code_generator;} |
|
1896 |
||
1897 |