author | lbessard |
Mon, 10 Dec 2007 11:13:25 +0100 | |
changeset 132 | 3aaf349203bc |
parent 115 | 017a65259f07 |
child 167 | 893ff98ab3be |
permissions | -rw-r--r-- |
58 | 1 |
#!/usr/bin/env python |
2 |
# -*- coding: utf-8 -*- |
|
3 |
||
4 |
#This file is part of PLCOpenEditor, a library implementing an IEC 61131-3 editor |
|
5 |
#based on the plcopen standard. |
|
6 |
# |
|
7 |
#Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD |
|
8 |
# |
|
9 |
#See COPYING file for copyrights details. |
|
10 |
# |
|
11 |
#This library is free software; you can redistribute it and/or |
|
12 |
#modify it under the terms of the GNU General Public |
|
13 |
#License as published by the Free Software Foundation; either |
|
14 |
#version 2.1 of the License, or (at your option) any later version. |
|
15 |
# |
|
16 |
#This library is distributed in the hope that it will be useful, |
|
17 |
#but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
18 |
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
19 |
#General Public License for more details. |
|
20 |
# |
|
21 |
#You should have received a copy of the GNU General Public |
|
22 |
#License along with this library; if not, write to the Free Software |
|
23 |
#Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
24 |
||
36
e7d67b27877f
Changes in the std library + std lib PLCOpen test (broken)
etisserant
parents:
26
diff
changeset
|
25 |
""" |
e7d67b27877f
Changes in the std library + std lib PLCOpen test (broken)
etisserant
parents:
26
diff
changeset
|
26 |
|
115 | 27 |
THIS CODE GENARATES C++ CODE FOR IEC2C COMPILER |
36
e7d67b27877f
Changes in the std library + std lib PLCOpen test (broken)
etisserant
parents:
26
diff
changeset
|
28 |
GENERATED CODE IS : |
e7d67b27877f
Changes in the std library + std lib PLCOpen test (broken)
etisserant
parents:
26
diff
changeset
|
29 |
|
e7d67b27877f
Changes in the std library + std lib PLCOpen test (broken)
etisserant
parents:
26
diff
changeset
|
30 |
function_type_decl.h - the long enumeration of std function types |
e7d67b27877f
Changes in the std library + std lib PLCOpen test (broken)
etisserant
parents:
26
diff
changeset
|
31 |
get_function_type_decl.c - the funct that return enumerated according function name |
115 | 32 |
st_code_gen.c - part of generate_c_st_c::visit(function_invocation) |
36
e7d67b27877f
Changes in the std library + std lib PLCOpen test (broken)
etisserant
parents:
26
diff
changeset
|
33 |
responsible to generate C code for std lib calls |
115 | 34 |
il_code_gen.c - part of generate_c_il_c::visit(il_function_call) |
36
e7d67b27877f
Changes in the std library + std lib PLCOpen test (broken)
etisserant
parents:
26
diff
changeset
|
35 |
responsible to generate C code for std lib calls |
e7d67b27877f
Changes in the std library + std lib PLCOpen test (broken)
etisserant
parents:
26
diff
changeset
|
36 |
search_type_code.c - called by search_expression_type_c::visit(function_invocation) |
e7d67b27877f
Changes in the std library + std lib PLCOpen test (broken)
etisserant
parents:
26
diff
changeset
|
37 |
return type symbol for std function invocation |
e7d67b27877f
Changes in the std library + std lib PLCOpen test (broken)
etisserant
parents:
26
diff
changeset
|
38 |
|
e7d67b27877f
Changes in the std library + std lib PLCOpen test (broken)
etisserant
parents:
26
diff
changeset
|
39 |
""" |
e7d67b27877f
Changes in the std library + std lib PLCOpen test (broken)
etisserant
parents:
26
diff
changeset
|
40 |
|
25 | 41 |
# Get definitions |
42 |
from plcopen.structures import * |
|
43 |
||
44 |
#import pprint |
|
45 |
#pp = pprint.PrettyPrinter(indent=4) |
|
46 |
||
58 | 47 |
matiec_header = """/* |
48 |
* (c) 2003 Mario de Sousa |
|
49 |
* |
|
50 |
* Offered to the public under the terms of the GNU General Public License |
|
51 |
* as published by the Free Software Foundation; either version 2 of the |
|
52 |
* License, or (at your option) any later version. |
|
53 |
* |
|
54 |
* This program is distributed in the hope that it will be useful, but |
|
55 |
* WITHOUT ANY WARRANTY; without even the implied warranty of |
|
56 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General |
|
57 |
* Public License for more details. |
|
58 |
* |
|
59 |
* This code is made available on the understanding that it will not be |
|
60 |
* used in safety-critical situations without a full and competent review. |
|
61 |
*/ |
|
62 |
||
63 |
/* |
|
64 |
* An IEC 61131-3 IL and ST compiler. |
|
65 |
* |
|
66 |
* Based on the |
|
67 |
* FINAL DRAFT - IEC 61131-3, 2nd Ed. (2001-12-10) |
|
68 |
* |
|
69 |
*/ |
|
70 |
||
71 |
""" |
|
72 |
||
59 | 73 |
matiec_lesser_header = """/* |
74 |
* (c) 2003 Mario de Sousa |
|
75 |
* |
|
76 |
* Offered to the public under the terms of the GNU Lesser General Public |
|
77 |
* License as published by the Free Software Foundation; either version 2 |
|
78 |
* of the License, or (at your option) any later version. |
|
79 |
* |
|
80 |
* This program is distributed in the hope that it will be useful, but |
|
81 |
* WITHOUT ANY WARRANTY; without even the implied warranty of |
|
82 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser |
|
83 |
* General Public License for more details. |
|
84 |
* |
|
85 |
* This code is made available on the understanding that it will not be |
|
86 |
* used in safety-critical situations without a full and competent review. |
|
87 |
*/ |
|
88 |
||
89 |
/* |
|
90 |
* An IEC 61131-3 IL and ST compiler. |
|
91 |
* |
|
92 |
* Based on the |
|
93 |
* FINAL DRAFT - IEC 61131-3, 2nd Ed. (2001-12-10) |
|
94 |
* |
|
95 |
*/ |
|
96 |
||
97 |
""" |
|
98 |
||
25 | 99 |
def ANY_to_compiler_test_type_GEN(typename, paramname): |
100 |
""" |
|
115 | 101 |
Convert ANY_XXX IEC type declaration into IEC2C's generated type test. |
25 | 102 |
This tests are defined in search_expression_type.cc |
103 |
""" |
|
104 |
return {"ANY" : "", |
|
105 |
"ANY_BIT" : "if(search_expression_type->is_binary_type(%(paramname)s_type_symbol))", |
|
26
36d378bd852e
Stage4 changes according to new STD lib implementation
etisserant
parents:
25
diff
changeset
|
106 |
"ANY_NBIT" : "if(search_expression_type->is_nbinary_type(%(paramname)s_type_symbol))", |
25 | 107 |
"ANY_NUM" : "if(search_expression_type->is_num_type(%(paramname)s_type_symbol))", |
108 |
"ANY_REAL" : "if(search_expression_type->is_real_type(%(paramname)s_type_symbol))", |
|
109 |
"ANY_INT" : "if(search_expression_type->is_integer_type(%(paramname)s_type_symbol))" |
|
110 |
}.get(typename, |
|
40 | 111 |
#"if (typeid(*last_type_symbol) == typeid(%(typename)s_type_name_c))")%{ |
112 |
"if(search_expression_type->is_same_type(&search_constant_type_c::%(typename)s_type_name, last_type_symbol))")%{ |
|
25 | 113 |
"paramname" : paramname, "typename": typename.lower()} |
114 |
||
115 |
def recurse_and_indent(fdecls, indent, do_type_search_only = False, do_il = False): |
|
116 |
""" |
|
117 |
This function generate visit(function_invocation) code for |
|
118 |
- ST code generator |
|
119 |
- IL code generator |
|
120 |
- search_expression_type class for ST |
|
121 |
- search_expression_type class for IL |
|
122 |
||
123 |
Input data is a |
|
124 |
"{fname : {IN[0]paramname : {IN[0]paramtype : {IN[1]paraname : {IN[1]paramtype : {... : {IN[N]paraname : {IN[N]paramtype : (fdecl,)}}}}}}" |
|
125 |
nested dictionary structure. |
|
126 |
""" |
|
127 |
if type(fdecls) != type(tuple()): |
|
128 |
res = "" |
|
129 |
for Paramname, ParamTypes in fdecls.iteritems(): |
|
130 |
if do_il: |
|
131 |
res += """ |
|
132 |
{""" |
|
133 |
if not do_type_search_only: |
|
134 |
res += """ |
|
135 |
/* Get the value from a foo(<param_name> = <param_value>) style call */ |
|
136 |
symbol_c *%(input_name)s_param_value = &this->default_variable_name; |
|
137 |
"""%{"input_name":Paramname} |
|
138 |
res += """ |
|
139 |
symbol_c *%(input_name)s_type_symbol = param_data_type; |
|
140 |
last_type_symbol = param_data_type; |
|
141 |
"""%{"input_name":Paramname} |
|
142 |
else: |
|
143 |
res += """ |
|
144 |
{ |
|
145 |
identifier_c param_name("%(input_name)s"); |
|
146 |
/* Get the value from a foo(<param_name> = <param_value>) style call */ |
|
147 |
symbol_c *%(input_name)s_param_value = function_call_param_iterator.search(¶m_name); |
|
148 |
||
149 |
/* Get the value from a foo(<param_value>) style call */ |
|
150 |
if (%(input_name)s_param_value == NULL) |
|
151 |
%(input_name)s_param_value = function_call_param_iterator.next(); |
|
152 |
symbol_c *%(input_name)s_type_symbol = search_expression_type->get_type(%(input_name)s_param_value); |
|
153 |
last_type_symbol = last_type_symbol && search_expression_type->is_same_type(%(input_name)s_type_symbol, last_type_symbol) ? search_expression_type->common_type(%(input_name)s_type_symbol, last_type_symbol) : %(input_name)s_type_symbol ; |
|
154 |
"""%{"input_name":Paramname} |
|
155 |
||
156 |
for ParamType,NextParamDecl in ParamTypes.iteritems(): |
|
157 |
||
158 |
res += """ |
|
159 |
%(type_test)s |
|
160 |
{ |
|
161 |
%(if_good_type_code)s |
|
162 |
} |
|
163 |
"""%{ |
|
164 |
"type_test":ANY_to_compiler_test_type_GEN(ParamType,Paramname), |
|
165 |
"if_good_type_code":recurse_and_indent(NextParamDecl,indent,do_type_search_only).replace('\n','\n ')} |
|
166 |
||
167 |
res += """ |
|
168 |
ERROR; |
|
169 |
} |
|
170 |
""" |
|
171 |
||
172 |
return res.replace('\n','\n'+indent) |
|
173 |
else: |
|
174 |
res = "\n" |
|
175 |
fdecl=fdecls[0] |
|
176 |
||
177 |
result_type_rule = fdecl["return_type_rule"] |
|
178 |
res += { |
|
179 |
"copy_input" : "symbol_c * return_type_symbol = last_type_symbol;\n", |
|
180 |
"defined" : "symbol_c * return_type_symbol = &search_constant_type_c::%s_type_name;\n"%fdecl["outputs"][0][1].lower(), |
|
181 |
}.get(result_type_rule, "symbol_c * return_type_symbol = %s;\n"%result_type_rule) |
|
182 |
||
183 |
if not do_type_search_only: |
|
184 |
code_gen = eval(fdecl["python_eval_c_code_format"]) |
|
185 |
||
186 |
code_gen_dic_decl = {} |
|
187 |
for paramname,paramtype,unused in fdecl["inputs"]: |
|
188 |
code_gen_dic_decl[paramname+"_value"] = '");\n%s_param_value->accept(*this);\ns4o.print("'%(paramname) |
|
189 |
code_gen_dic_decl[paramname+"_type"] = '");\n%s_type_symbol->accept(*this);\ns4o.print("'%(paramname) |
|
190 |
code_gen_dic_decl["return_type"] = '");\nreturn_type_symbol->accept(*this);\ns4o.print("' |
|
191 |
code_gen_dic_decl["param_count"] = '");\ns4o.print_integer(nb_param);\ns4o.print("' |
|
43 | 192 |
code_gen_dic_decl["common_type"] = '");\nlast_type_symbol->accept(*this);\ns4o.print("' |
25 | 193 |
code_gen_dic_decl["start_bool_filter"] = '");\nif (search_expression_type->is_bool_type(last_type_symbol))\n s4o.print("(' |
194 |
code_gen_dic_decl["end_bool_filter"] = '");\nif (search_expression_type->is_bool_type(last_type_symbol)) {\n s4o.print("&1");\n s4o.print(")");\n}\ns4o.print("' |
|
195 |
||
196 |
if type(code_gen) == type(tuple()): |
|
40 | 197 |
res += 's4o.indent_right();\n' |
198 |
res += 's4o.print("%s\\n" + s4o.indent_spaces);\n'%(code_gen[0]%code_gen_dic_decl) |
|
25 | 199 |
static_param_accept_list = [] |
200 |
for paramname,paramtype,unused in fdecl["inputs"]: |
|
201 |
static_param_accept_list.append("%s_param_value->accept(*this);\n"%(paramname)) |
|
40 | 202 |
res += ('s4o.print("%s\\n" + s4o.indent_spaces);\n'%(code_gen[1])).join(static_param_accept_list) |
203 |
code = 's4o.print("%s\\n" + s4o.indent_spaces);\nparam_value->accept(*this);\n'%(code_gen[1]) |
|
204 |
end_code = 's4o.print("%s");\ns4o.indent_left();\nreturn NULL;\n'%(code_gen[2]%code_gen_dic_decl) |
|
25 | 205 |
else: |
206 |
code = '' |
|
207 |
end_code = ('s4o.print("' + code_gen%code_gen_dic_decl + '");\nreturn NULL;\n').replace('s4o.print("");\n','') |
|
208 |
||
209 |
if fdecl["extensible"]: |
|
210 |
res += (""" |
|
211 |
int base_num = %d; |
|
212 |
symbol_c *param_value = NULL; |
|
213 |
do{ |
|
214 |
char my_name[10]; |
|
215 |
sprintf(my_name, "IN%%d", base_num++); |
|
216 |
identifier_c param_name(my_name); |
|
217 |
||
218 |
/* Get the value from a foo(<param_name> = <param_value>) style call */ |
|
219 |
param_value = function_call_param_iterator.search(¶m_name); |
|
220 |
||
221 |
/* Get the value from a foo(<param_value>) style call */ |
|
222 |
if (param_value == NULL) |
|
223 |
param_value = function_call_param_iterator.next(); |
|
224 |
if (param_value != NULL){ |
|
225 |
symbol_c *current_type_symbol = search_expression_type->get_type(param_value); |
|
226 |
last_type_symbol = last_type_symbol && search_expression_type->is_same_type(current_type_symbol, last_type_symbol) ? search_expression_type->common_type(current_type_symbol, last_type_symbol) : current_type_symbol ; |
|
227 |
||
228 |
/*Function specific CODE */ |
|
229 |
%s |
|
230 |
} |
|
231 |
||
232 |
}while(param_value != NULL); |
|
233 |
%s |
|
234 |
"""%(fdecl["baseinputnumber"]+2, code.replace('\n','\n '), end_code)) |
|
235 |
else: |
|
236 |
#res += code + end_code |
|
237 |
res += end_code |
|
238 |
else: |
|
239 |
res += "return return_type_symbol;\n" |
|
240 |
||
241 |
||
242 |
return res.replace('\n','\n'+indent) |
|
243 |
||
244 |
################################################################### |
|
245 |
### ### |
|
246 |
### MAIN ### |
|
247 |
### ### |
|
248 |
################################################################### |
|
249 |
||
250 |
""" |
|
251 |
Reorganize std_decl from structure.py |
|
252 |
into a nested dictionnary structure (i.e. a tree): |
|
253 |
"{fname : {IN[0]paramname : {IN[0]paramtype : {IN[1]paraname : {IN[1]paramtype : {... : {IN[N]paraname : {IN[N]paramtype : (fdecl,)}}}}}}" |
|
254 |
Keep ptrack of original declaration order in a |
|
255 |
separated list called official_order |
|
256 |
""" |
|
257 |
std_fdecls = {} |
|
258 |
official_order = [] |
|
259 |
for section in std_decl: |
|
260 |
for fdecl in section["list"]: |
|
261 |
if len(official_order)==0 or official_order[-1] != fdecl["name"]: |
|
262 |
official_order.append(fdecl["name"]) |
|
263 |
# store all func by name in a dict |
|
264 |
std_fdecls_fdecl_name = std_fdecls.get(fdecl["name"], {}) |
|
265 |
current = std_fdecls_fdecl_name |
|
266 |
for i in fdecl["inputs"]: |
|
267 |
current[i[0]] = current.get(i[0], {}) |
|
268 |
current = current[i[0]] |
|
269 |
last = current |
|
270 |
current[i[1]] = current.get(i[1], {}) |
|
271 |
current = current[i[1]] |
|
272 |
last[i[1]]=(fdecl,) |
|
273 |
std_fdecls[fdecl["name"]] = std_fdecls_fdecl_name |
|
274 |
||
275 |
################################################################### |
|
276 |
||
277 |
""" |
|
278 |
Generate the long enumeration of std function types |
|
279 |
""" |
|
58 | 280 |
function_type_decl = matiec_header + """ |
25 | 281 |
/**** |
282 |
* IEC 61131-3 standard function lib |
|
283 |
* generated code, do not edit by hand |
|
284 |
*/ |
|
285 |
typedef enum { |
|
286 |
""" |
|
287 |
for fname, fdecls in [ (fname,std_fdecls[fname]) for fname in official_order ]: |
|
288 |
function_type_decl += " function_"+fname.lower()+",\n" |
|
289 |
||
290 |
function_type_decl += """ function_none |
|
291 |
} function_type_t; |
|
292 |
""" |
|
293 |
################################################################### |
|
294 |
""" |
|
295 |
Generate the funct that return enumerated according function name |
|
296 |
""" |
|
58 | 297 |
get_function_type_decl = matiec_header + """ |
25 | 298 |
/**** |
299 |
* IEC 61131-3 standard function lib |
|
300 |
* generated code, do not edit by hand |
|
301 |
*/ |
|
302 |
function_type_t get_function_type(identifier_c *function_name) { |
|
303 |
""" |
|
304 |
for fname, fdecls in [ (fname,std_fdecls[fname]) for fname in official_order ]: |
|
305 |
get_function_type_decl += """ |
|
306 |
if (!strcasecmp(function_name->value, "%s")) |
|
307 |
return function_%s; |
|
308 |
"""%(fname,fname.lower()) |
|
309 |
||
310 |
get_function_type_decl += """ |
|
311 |
else return function_none; |
|
312 |
} |
|
313 |
||
314 |
""" |
|
315 |
################################################################### |
|
316 |
""" |
|
115 | 317 |
Generate the part of generate_c_st_c::visit(function_invocation) |
25 | 318 |
that is responsible to generate C code for std lib calls. |
319 |
""" |
|
58 | 320 |
st_code_gen = matiec_header + """ |
25 | 321 |
/**** |
322 |
* IEC 61131-3 standard function lib |
|
323 |
* generated code, do not edit by hand |
|
324 |
*/ |
|
325 |
switch(current_function_type){ |
|
326 |
""" |
|
327 |
||
328 |
for fname, fdecls in [ (fname,std_fdecls[fname]) for fname in official_order ]: |
|
329 |
st_code_gen += """ |
|
330 |
/**** |
|
331 |
*%s |
|
332 |
*/ |
|
333 |
case function_%s : |
|
334 |
{ |
|
335 |
symbol_c *last_type_symbol = NULL; |
|
336 |
""" %(fname, fname.lower()) |
|
337 |
indent = " " |
|
338 |
||
339 |
st_code_gen += recurse_and_indent(fdecls, indent).replace('\n','\n ') |
|
340 |
||
341 |
st_code_gen += """ |
|
342 |
}/*function_%s*/ |
|
343 |
break; |
|
344 |
""" %(fname.lower()) |
|
345 |
st_code_gen += """ |
|
346 |
case function_none : |
|
347 |
ERROR; |
|
348 |
} |
|
349 |
return NULL; |
|
350 |
""" |
|
351 |
||
352 |
################################################################### |
|
353 |
""" |
|
115 | 354 |
Generate the part of generate_c_il_c::visit(il_function_call) |
25 | 355 |
that is responsible to generate C code for std lib calls. |
356 |
""" |
|
58 | 357 |
il_code_gen = matiec_header + """ |
25 | 358 |
/**** |
359 |
* IEC 61131-3 standard function lib |
|
360 |
* generated code, do not edit by hand |
|
361 |
*/ |
|
362 |
switch(current_function_type){ |
|
363 |
""" |
|
364 |
||
365 |
for fname, fdecls in [ (fname,std_fdecls[fname]) for fname in official_order ]: |
|
366 |
il_code_gen += """ |
|
367 |
/**** |
|
368 |
*%s |
|
369 |
*/ |
|
370 |
case function_%s : |
|
371 |
{ |
|
372 |
symbol_c *last_type_symbol = NULL; |
|
373 |
""" %(fname, fname.lower()) |
|
374 |
indent = " " |
|
375 |
||
376 |
il_code_gen += recurse_and_indent(fdecls, indent, do_il=True).replace('\n','\n ') |
|
377 |
||
378 |
il_code_gen += """ |
|
379 |
}/*function_%s*/ |
|
380 |
break; |
|
381 |
""" %(fname.lower()) |
|
382 |
il_code_gen += """ |
|
383 |
case function_none : |
|
384 |
ERROR; |
|
385 |
} |
|
386 |
return NULL; |
|
387 |
""" |
|
388 |
||
389 |
################################################################### |
|
390 |
""" |
|
391 |
Generate the part of search_expression_type_c::visit(function_invocation) |
|
392 |
that is responsible of returning type symbol for function invocation. |
|
393 |
""" |
|
59 | 394 |
search_type_code = matiec_header + """ |
25 | 395 |
/**** |
396 |
* IEC 61131-3 standard function lib |
|
397 |
* generated code, do not edit by hand |
|
398 |
*/ |
|
399 |
||
400 |
void *compute_standard_function_st(function_invocation_c *symbol) { |
|
401 |
||
402 |
function_type_t current_function_type = get_function_type((identifier_c *)symbol->function_name); |
|
403 |
function_call_param_iterator_c function_call_param_iterator(symbol); |
|
404 |
search_expression_type_c* search_expression_type = this; |
|
405 |
||
406 |
switch(current_function_type){ |
|
407 |
""" |
|
408 |
||
409 |
for fname, fdecls in [ (fname,std_fdecls[fname]) for fname in official_order ]: |
|
410 |
search_type_code += """ |
|
411 |
/**** |
|
412 |
*%s |
|
413 |
*/ |
|
414 |
case function_%s : |
|
415 |
{ |
|
416 |
symbol_c *last_type_symbol = NULL; |
|
417 |
""" %(fname, fname.lower()) |
|
418 |
indent = " " |
|
419 |
||
420 |
search_type_code += recurse_and_indent(fdecls, indent, True).replace('\n','\n ') |
|
421 |
||
422 |
search_type_code += """ |
|
423 |
}/*function_%s*/ |
|
424 |
break; |
|
425 |
""" %(fname.lower()) |
|
426 |
search_type_code += """ |
|
427 |
case function_none : |
|
428 |
ERROR; |
|
429 |
} |
|
430 |
return NULL; |
|
431 |
} |
|
432 |
||
433 |
void *compute_standard_function_il(il_function_call_c *symbol, symbol_c *param_data_type) { |
|
434 |
||
435 |
function_type_t current_function_type = get_function_type((identifier_c *)symbol->function_name); |
|
436 |
function_call_param_iterator_c function_call_param_iterator(symbol); |
|
437 |
search_expression_type_c* search_expression_type = this; |
|
438 |
||
439 |
switch(current_function_type){ |
|
440 |
""" |
|
441 |
||
442 |
for fname, fdecls in [ (fname,std_fdecls[fname]) for fname in official_order ]: |
|
443 |
search_type_code += """ |
|
444 |
/**** |
|
445 |
*%s |
|
446 |
*/ |
|
447 |
case function_%s : |
|
448 |
{ |
|
449 |
symbol_c *last_type_symbol = NULL; |
|
450 |
""" %(fname, fname.lower()) |
|
451 |
indent = " " |
|
452 |
||
453 |
search_type_code += recurse_and_indent(fdecls, indent, True, True).replace('\n','\n ') |
|
454 |
||
455 |
search_type_code += """ |
|
456 |
}/*function_%s*/ |
|
457 |
break; |
|
458 |
""" %(fname.lower()) |
|
459 |
search_type_code += """ |
|
460 |
case function_none : |
|
461 |
ERROR; |
|
462 |
} |
|
463 |
return NULL; |
|
464 |
} |
|
465 |
""" |
|
466 |
||
467 |
################################################################### |
|
468 |
################################################################### |
|
469 |
################################################################### |
|
470 |
""" |
|
36
e7d67b27877f
Changes in the std library + std lib PLCOpen test (broken)
etisserant
parents:
26
diff
changeset
|
471 |
Generate the standard_function_names[] for inclusion in bizon generated code |
e7d67b27877f
Changes in the std library + std lib PLCOpen test (broken)
etisserant
parents:
26
diff
changeset
|
472 |
""" |
59 | 473 |
standard_function_names = matiec_header + """ |
36
e7d67b27877f
Changes in the std library + std lib PLCOpen test (broken)
etisserant
parents:
26
diff
changeset
|
474 |
/**** |
e7d67b27877f
Changes in the std library + std lib PLCOpen test (broken)
etisserant
parents:
26
diff
changeset
|
475 |
* IEC 61131-3 standard function lib |
e7d67b27877f
Changes in the std library + std lib PLCOpen test (broken)
etisserant
parents:
26
diff
changeset
|
476 |
* generated code, do not edit by hand |
e7d67b27877f
Changes in the std library + std lib PLCOpen test (broken)
etisserant
parents:
26
diff
changeset
|
477 |
*/ |
e7d67b27877f
Changes in the std library + std lib PLCOpen test (broken)
etisserant
parents:
26
diff
changeset
|
478 |
|
e7d67b27877f
Changes in the std library + std lib PLCOpen test (broken)
etisserant
parents:
26
diff
changeset
|
479 |
const char *standard_function_names[] = { |
e7d67b27877f
Changes in the std library + std lib PLCOpen test (broken)
etisserant
parents:
26
diff
changeset
|
480 |
""" |
e7d67b27877f
Changes in the std library + std lib PLCOpen test (broken)
etisserant
parents:
26
diff
changeset
|
481 |
for fname, fdecls in [ (fname,std_fdecls[fname]) for fname in official_order ]: |
e7d67b27877f
Changes in the std library + std lib PLCOpen test (broken)
etisserant
parents:
26
diff
changeset
|
482 |
standard_function_names += "\""+fname+"\",\n" |
e7d67b27877f
Changes in the std library + std lib PLCOpen test (broken)
etisserant
parents:
26
diff
changeset
|
483 |
standard_function_names += """ |
e7d67b27877f
Changes in the std library + std lib PLCOpen test (broken)
etisserant
parents:
26
diff
changeset
|
484 |
/* end of array marker! Do not remove! */ |
e7d67b27877f
Changes in the std library + std lib PLCOpen test (broken)
etisserant
parents:
26
diff
changeset
|
485 |
NULL |
e7d67b27877f
Changes in the std library + std lib PLCOpen test (broken)
etisserant
parents:
26
diff
changeset
|
486 |
}; |
e7d67b27877f
Changes in the std library + std lib PLCOpen test (broken)
etisserant
parents:
26
diff
changeset
|
487 |
|
e7d67b27877f
Changes in the std library + std lib PLCOpen test (broken)
etisserant
parents:
26
diff
changeset
|
488 |
""" |
e7d67b27877f
Changes in the std library + std lib PLCOpen test (broken)
etisserant
parents:
26
diff
changeset
|
489 |
|
e7d67b27877f
Changes in the std library + std lib PLCOpen test (broken)
etisserant
parents:
26
diff
changeset
|
490 |
################################################################### |
e7d67b27877f
Changes in the std library + std lib PLCOpen test (broken)
etisserant
parents:
26
diff
changeset
|
491 |
################################################################### |
e7d67b27877f
Changes in the std library + std lib PLCOpen test (broken)
etisserant
parents:
26
diff
changeset
|
492 |
################################################################### |
e7d67b27877f
Changes in the std library + std lib PLCOpen test (broken)
etisserant
parents:
26
diff
changeset
|
493 |
""" |
25 | 494 |
Generate the C implementation of the IEC standard function library. |
495 |
""" |
|
59 | 496 |
iec_std_lib_generated = matiec_lesser_header + """ |
25 | 497 |
/**** |
498 |
* IEC 61131-3 standard function lib |
|
499 |
* generated code, do not edit by hand |
|
500 |
*/ |
|
501 |
||
502 |
/* Macro that expand to subtypes */ |
|
503 |
""" |
|
504 |
for typename, parenttypename in TypeHierarchy_list: |
|
505 |
if (typename.startswith("ANY")): |
|
506 |
iec_std_lib_generated += "#define " + typename + "(DO)" |
|
507 |
for typename2, parenttypename2 in TypeHierarchy_list: |
|
508 |
if(parenttypename2 == typename): |
|
509 |
if(typename2.startswith("ANY")): |
|
510 |
iec_std_lib_generated += " " + typename2 + "(DO)" |
|
511 |
else: |
|
512 |
iec_std_lib_generated += " DO(" + typename2 + ")" |
|
513 |
iec_std_lib_generated += "\n" |
|
514 |
else: |
|
515 |
break |
|
516 |
||
517 |
if len(sys.argv) != 2 : |
|
518 |
print "Usage: " + sys.argv[0] + "path_name\n -> create files in path_name" |
|
519 |
sys.exit(0) |
|
520 |
||
521 |
# Now, print that out, or write to files from sys.argv |
|
36
e7d67b27877f
Changes in the std library + std lib PLCOpen test (broken)
etisserant
parents:
26
diff
changeset
|
522 |
for path, name, ext in [ |
115 | 523 |
('stage4/generate_c','function_type_decl','h'), |
524 |
('stage4/generate_c','get_function_type_decl','c'), |
|
525 |
('stage4/generate_c','st_code_gen','c'), |
|
526 |
('stage4/generate_c','il_code_gen','c'), |
|
527 |
('stage4/generate_c','search_type_code','c'), |
|
36
e7d67b27877f
Changes in the std library + std lib PLCOpen test (broken)
etisserant
parents:
26
diff
changeset
|
528 |
('stage1_2','standard_function_names','c'), |
e7d67b27877f
Changes in the std library + std lib PLCOpen test (broken)
etisserant
parents:
26
diff
changeset
|
529 |
('lib', 'iec_std_lib_generated','h') |
26
36d378bd852e
Stage4 changes according to new STD lib implementation
etisserant
parents:
25
diff
changeset
|
530 |
]: |
36
e7d67b27877f
Changes in the std library + std lib PLCOpen test (broken)
etisserant
parents:
26
diff
changeset
|
531 |
fd = open(os.path.join(sys.argv[1], path, name+'.'+ext),'w') |
25 | 532 |
fd.write(eval(name)) |
533 |
fd.close() |
|
36
e7d67b27877f
Changes in the std library + std lib PLCOpen test (broken)
etisserant
parents:
26
diff
changeset
|
534 |
|
e7d67b27877f
Changes in the std library + std lib PLCOpen test (broken)
etisserant
parents:
26
diff
changeset
|
535 |
#print "/* Code to eventually paste in iec_std_lib.h if type hierarchy changed */" |
e7d67b27877f
Changes in the std library + std lib PLCOpen test (broken)
etisserant
parents:
26
diff
changeset
|
536 |
#print "/* you also have to change iec_std_lib.h according to new types */\n\n" |
e7d67b27877f
Changes in the std library + std lib PLCOpen test (broken)
etisserant
parents:
26
diff
changeset
|
537 |
#print iec_std_lib_generated |