author | lbessard |
Tue, 10 Jul 2007 14:29:31 +0200 | |
changeset 31 | d833bf7567b1 |
parent 26 | 36d378bd852e |
child 36 | e7d67b27877f |
permissions | -rw-r--r-- |
25 | 1 |
# Get definitions |
2 |
from plcopen.structures import * |
|
3 |
||
4 |
#import pprint |
|
5 |
#pp = pprint.PrettyPrinter(indent=4) |
|
6 |
||
7 |
def ANY_to_compiler_test_type_GEN(typename, paramname): |
|
8 |
""" |
|
9 |
Convert ANY_XXX IEC type declaration into IEC2CC's generated type test. |
|
10 |
This tests are defined in search_expression_type.cc |
|
11 |
""" |
|
12 |
return {"ANY" : "", |
|
13 |
"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
|
14 |
"ANY_NBIT" : "if(search_expression_type->is_nbinary_type(%(paramname)s_type_symbol))", |
25 | 15 |
"ANY_NUM" : "if(search_expression_type->is_num_type(%(paramname)s_type_symbol))", |
16 |
"ANY_REAL" : "if(search_expression_type->is_real_type(%(paramname)s_type_symbol))", |
|
17 |
"ANY_INT" : "if(search_expression_type->is_integer_type(%(paramname)s_type_symbol))" |
|
18 |
}.get(typename, |
|
19 |
"if (typeid(*last_type_symbol) == typeid(%(typename)s_type_name_c))")%{ |
|
20 |
"paramname" : paramname, "typename": typename.lower()} |
|
21 |
||
22 |
def recurse_and_indent(fdecls, indent, do_type_search_only = False, do_il = False): |
|
23 |
""" |
|
24 |
This function generate visit(function_invocation) code for |
|
25 |
- ST code generator |
|
26 |
- IL code generator |
|
27 |
- search_expression_type class for ST |
|
28 |
- search_expression_type class for IL |
|
29 |
||
30 |
Input data is a |
|
31 |
"{fname : {IN[0]paramname : {IN[0]paramtype : {IN[1]paraname : {IN[1]paramtype : {... : {IN[N]paraname : {IN[N]paramtype : (fdecl,)}}}}}}" |
|
32 |
nested dictionary structure. |
|
33 |
""" |
|
34 |
if type(fdecls) != type(tuple()): |
|
35 |
res = "" |
|
36 |
for Paramname, ParamTypes in fdecls.iteritems(): |
|
37 |
if do_il: |
|
38 |
res += """ |
|
39 |
{""" |
|
40 |
if not do_type_search_only: |
|
41 |
res += """ |
|
42 |
/* Get the value from a foo(<param_name> = <param_value>) style call */ |
|
43 |
symbol_c *%(input_name)s_param_value = &this->default_variable_name; |
|
44 |
"""%{"input_name":Paramname} |
|
45 |
res += """ |
|
46 |
symbol_c *%(input_name)s_type_symbol = param_data_type; |
|
47 |
last_type_symbol = param_data_type; |
|
48 |
"""%{"input_name":Paramname} |
|
49 |
else: |
|
50 |
res += """ |
|
51 |
{ |
|
52 |
identifier_c param_name("%(input_name)s"); |
|
53 |
/* Get the value from a foo(<param_name> = <param_value>) style call */ |
|
54 |
symbol_c *%(input_name)s_param_value = function_call_param_iterator.search(¶m_name); |
|
55 |
||
56 |
/* Get the value from a foo(<param_value>) style call */ |
|
57 |
if (%(input_name)s_param_value == NULL) |
|
58 |
%(input_name)s_param_value = function_call_param_iterator.next(); |
|
59 |
symbol_c *%(input_name)s_type_symbol = search_expression_type->get_type(%(input_name)s_param_value); |
|
60 |
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 ; |
|
61 |
"""%{"input_name":Paramname} |
|
62 |
||
63 |
for ParamType,NextParamDecl in ParamTypes.iteritems(): |
|
64 |
||
65 |
res += """ |
|
66 |
%(type_test)s |
|
67 |
{ |
|
68 |
%(if_good_type_code)s |
|
69 |
} |
|
70 |
"""%{ |
|
71 |
"type_test":ANY_to_compiler_test_type_GEN(ParamType,Paramname), |
|
72 |
"if_good_type_code":recurse_and_indent(NextParamDecl,indent,do_type_search_only).replace('\n','\n ')} |
|
73 |
||
74 |
res += """ |
|
75 |
ERROR; |
|
76 |
} |
|
77 |
""" |
|
78 |
||
79 |
return res.replace('\n','\n'+indent) |
|
80 |
else: |
|
81 |
res = "\n" |
|
82 |
fdecl=fdecls[0] |
|
83 |
||
84 |
result_type_rule = fdecl["return_type_rule"] |
|
85 |
res += { |
|
86 |
"copy_input" : "symbol_c * return_type_symbol = last_type_symbol;\n", |
|
87 |
"defined" : "symbol_c * return_type_symbol = &search_constant_type_c::%s_type_name;\n"%fdecl["outputs"][0][1].lower(), |
|
88 |
}.get(result_type_rule, "symbol_c * return_type_symbol = %s;\n"%result_type_rule) |
|
89 |
||
90 |
if not do_type_search_only: |
|
91 |
code_gen = eval(fdecl["python_eval_c_code_format"]) |
|
92 |
||
93 |
code_gen_dic_decl = {} |
|
94 |
for paramname,paramtype,unused in fdecl["inputs"]: |
|
95 |
code_gen_dic_decl[paramname+"_value"] = '");\n%s_param_value->accept(*this);\ns4o.print("'%(paramname) |
|
96 |
code_gen_dic_decl[paramname+"_type"] = '");\n%s_type_symbol->accept(*this);\ns4o.print("'%(paramname) |
|
97 |
code_gen_dic_decl["return_type"] = '");\nreturn_type_symbol->accept(*this);\ns4o.print("' |
|
98 |
code_gen_dic_decl["param_count"] = '");\ns4o.print_integer(nb_param);\ns4o.print("' |
|
99 |
code_gen_dic_decl["start_bool_filter"] = '");\nif (search_expression_type->is_bool_type(last_type_symbol))\n s4o.print("(' |
|
100 |
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("' |
|
101 |
||
102 |
if type(code_gen) == type(tuple()): |
|
103 |
res += 's4o.print("%s");\n'%(code_gen[0]%code_gen_dic_decl) |
|
104 |
static_param_accept_list = [] |
|
105 |
for paramname,paramtype,unused in fdecl["inputs"]: |
|
106 |
static_param_accept_list.append("%s_param_value->accept(*this);\n"%(paramname)) |
|
107 |
res += ('s4o.print("%s");\n'%(code_gen[1])).join(static_param_accept_list) |
|
108 |
code = 's4o.print("%s");\nparam_value->accept(*this);\n'%(code_gen[1]) |
|
109 |
end_code = 's4o.print("%s");\nreturn NULL;\n'%(code_gen[2]%code_gen_dic_decl) |
|
110 |
else: |
|
111 |
code = '' |
|
112 |
end_code = ('s4o.print("' + code_gen%code_gen_dic_decl + '");\nreturn NULL;\n').replace('s4o.print("");\n','') |
|
113 |
||
114 |
if fdecl["extensible"]: |
|
115 |
res += (""" |
|
116 |
int base_num = %d; |
|
117 |
symbol_c *param_value = NULL; |
|
118 |
do{ |
|
119 |
char my_name[10]; |
|
120 |
sprintf(my_name, "IN%%d", base_num++); |
|
121 |
identifier_c param_name(my_name); |
|
122 |
||
123 |
/* Get the value from a foo(<param_name> = <param_value>) style call */ |
|
124 |
param_value = function_call_param_iterator.search(¶m_name); |
|
125 |
||
126 |
/* Get the value from a foo(<param_value>) style call */ |
|
127 |
if (param_value == NULL) |
|
128 |
param_value = function_call_param_iterator.next(); |
|
129 |
if (param_value != NULL){ |
|
130 |
symbol_c *current_type_symbol = search_expression_type->get_type(param_value); |
|
131 |
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 ; |
|
132 |
||
133 |
/*Function specific CODE */ |
|
134 |
%s |
|
135 |
} |
|
136 |
||
137 |
}while(param_value != NULL); |
|
138 |
%s |
|
139 |
"""%(fdecl["baseinputnumber"]+2, code.replace('\n','\n '), end_code)) |
|
140 |
else: |
|
141 |
#res += code + end_code |
|
142 |
res += end_code |
|
143 |
else: |
|
144 |
res += "return return_type_symbol;\n" |
|
145 |
||
146 |
||
147 |
return res.replace('\n','\n'+indent) |
|
148 |
||
149 |
################################################################### |
|
150 |
### ### |
|
151 |
### MAIN ### |
|
152 |
### ### |
|
153 |
################################################################### |
|
154 |
||
155 |
""" |
|
156 |
Reorganize std_decl from structure.py |
|
157 |
into a nested dictionnary structure (i.e. a tree): |
|
158 |
"{fname : {IN[0]paramname : {IN[0]paramtype : {IN[1]paraname : {IN[1]paramtype : {... : {IN[N]paraname : {IN[N]paramtype : (fdecl,)}}}}}}" |
|
159 |
Keep ptrack of original declaration order in a |
|
160 |
separated list called official_order |
|
161 |
""" |
|
162 |
std_fdecls = {} |
|
163 |
official_order = [] |
|
164 |
for section in std_decl: |
|
165 |
for fdecl in section["list"]: |
|
166 |
if len(official_order)==0 or official_order[-1] != fdecl["name"]: |
|
167 |
official_order.append(fdecl["name"]) |
|
168 |
# store all func by name in a dict |
|
169 |
std_fdecls_fdecl_name = std_fdecls.get(fdecl["name"], {}) |
|
170 |
current = std_fdecls_fdecl_name |
|
171 |
for i in fdecl["inputs"]: |
|
172 |
current[i[0]] = current.get(i[0], {}) |
|
173 |
current = current[i[0]] |
|
174 |
last = current |
|
175 |
current[i[1]] = current.get(i[1], {}) |
|
176 |
current = current[i[1]] |
|
177 |
last[i[1]]=(fdecl,) |
|
178 |
std_fdecls[fdecl["name"]] = std_fdecls_fdecl_name |
|
179 |
||
180 |
################################################################### |
|
181 |
||
182 |
""" |
|
183 |
Generate the long enumeration of std function types |
|
184 |
""" |
|
185 |
function_type_decl = """ |
|
186 |
/**** |
|
187 |
* IEC 61131-3 standard function lib |
|
188 |
* generated code, do not edit by hand |
|
189 |
*/ |
|
190 |
typedef enum { |
|
191 |
""" |
|
192 |
for fname, fdecls in [ (fname,std_fdecls[fname]) for fname in official_order ]: |
|
193 |
function_type_decl += " function_"+fname.lower()+",\n" |
|
194 |
||
195 |
function_type_decl += """ function_none |
|
196 |
} function_type_t; |
|
197 |
""" |
|
198 |
################################################################### |
|
199 |
""" |
|
200 |
Generate the funct that return enumerated according function name |
|
201 |
""" |
|
202 |
get_function_type_decl = """ |
|
203 |
/**** |
|
204 |
* IEC 61131-3 standard function lib |
|
205 |
* generated code, do not edit by hand |
|
206 |
*/ |
|
207 |
function_type_t get_function_type(identifier_c *function_name) { |
|
208 |
""" |
|
209 |
for fname, fdecls in [ (fname,std_fdecls[fname]) for fname in official_order ]: |
|
210 |
get_function_type_decl += """ |
|
211 |
if (!strcasecmp(function_name->value, "%s")) |
|
212 |
return function_%s; |
|
213 |
"""%(fname,fname.lower()) |
|
214 |
||
215 |
get_function_type_decl += """ |
|
216 |
else return function_none; |
|
217 |
} |
|
218 |
||
219 |
""" |
|
220 |
################################################################### |
|
221 |
""" |
|
222 |
Generate the part of generate_cc_st_c::visit(function_invocation) |
|
223 |
that is responsible to generate C code for std lib calls. |
|
224 |
""" |
|
225 |
st_code_gen = """ |
|
226 |
/**** |
|
227 |
* IEC 61131-3 standard function lib |
|
228 |
* generated code, do not edit by hand |
|
229 |
*/ |
|
230 |
switch(current_function_type){ |
|
231 |
""" |
|
232 |
||
233 |
for fname, fdecls in [ (fname,std_fdecls[fname]) for fname in official_order ]: |
|
234 |
st_code_gen += """ |
|
235 |
/**** |
|
236 |
*%s |
|
237 |
*/ |
|
238 |
case function_%s : |
|
239 |
{ |
|
240 |
symbol_c *last_type_symbol = NULL; |
|
241 |
""" %(fname, fname.lower()) |
|
242 |
indent = " " |
|
243 |
||
244 |
st_code_gen += recurse_and_indent(fdecls, indent).replace('\n','\n ') |
|
245 |
||
246 |
st_code_gen += """ |
|
247 |
}/*function_%s*/ |
|
248 |
break; |
|
249 |
""" %(fname.lower()) |
|
250 |
st_code_gen += """ |
|
251 |
case function_none : |
|
252 |
ERROR; |
|
253 |
} |
|
254 |
return NULL; |
|
255 |
""" |
|
256 |
||
257 |
################################################################### |
|
258 |
""" |
|
259 |
Generate the part of generate_cc_il_c::visit(il_function_call) |
|
260 |
that is responsible to generate C code for std lib calls. |
|
261 |
""" |
|
262 |
il_code_gen = """ |
|
263 |
/**** |
|
264 |
* IEC 61131-3 standard function lib |
|
265 |
* generated code, do not edit by hand |
|
266 |
*/ |
|
267 |
switch(current_function_type){ |
|
268 |
""" |
|
269 |
||
270 |
for fname, fdecls in [ (fname,std_fdecls[fname]) for fname in official_order ]: |
|
271 |
il_code_gen += """ |
|
272 |
/**** |
|
273 |
*%s |
|
274 |
*/ |
|
275 |
case function_%s : |
|
276 |
{ |
|
277 |
symbol_c *last_type_symbol = NULL; |
|
278 |
""" %(fname, fname.lower()) |
|
279 |
indent = " " |
|
280 |
||
281 |
il_code_gen += recurse_and_indent(fdecls, indent, do_il=True).replace('\n','\n ') |
|
282 |
||
283 |
il_code_gen += """ |
|
284 |
}/*function_%s*/ |
|
285 |
break; |
|
286 |
""" %(fname.lower()) |
|
287 |
il_code_gen += """ |
|
288 |
case function_none : |
|
289 |
ERROR; |
|
290 |
} |
|
291 |
return NULL; |
|
292 |
""" |
|
293 |
||
294 |
################################################################### |
|
295 |
""" |
|
296 |
Generate the part of search_expression_type_c::visit(function_invocation) |
|
297 |
that is responsible of returning type symbol for function invocation. |
|
298 |
""" |
|
299 |
search_type_code = """ |
|
300 |
/**** |
|
301 |
* IEC 61131-3 standard function lib |
|
302 |
* generated code, do not edit by hand |
|
303 |
*/ |
|
304 |
||
305 |
void *compute_standard_function_st(function_invocation_c *symbol) { |
|
306 |
||
307 |
function_type_t current_function_type = get_function_type((identifier_c *)symbol->function_name); |
|
308 |
function_call_param_iterator_c function_call_param_iterator(symbol); |
|
309 |
search_expression_type_c* search_expression_type = this; |
|
310 |
||
311 |
switch(current_function_type){ |
|
312 |
""" |
|
313 |
||
314 |
for fname, fdecls in [ (fname,std_fdecls[fname]) for fname in official_order ]: |
|
315 |
search_type_code += """ |
|
316 |
/**** |
|
317 |
*%s |
|
318 |
*/ |
|
319 |
case function_%s : |
|
320 |
{ |
|
321 |
symbol_c *last_type_symbol = NULL; |
|
322 |
""" %(fname, fname.lower()) |
|
323 |
indent = " " |
|
324 |
||
325 |
search_type_code += recurse_and_indent(fdecls, indent, True).replace('\n','\n ') |
|
326 |
||
327 |
search_type_code += """ |
|
328 |
}/*function_%s*/ |
|
329 |
break; |
|
330 |
""" %(fname.lower()) |
|
331 |
search_type_code += """ |
|
332 |
case function_none : |
|
333 |
ERROR; |
|
334 |
} |
|
335 |
return NULL; |
|
336 |
} |
|
337 |
||
338 |
void *compute_standard_function_il(il_function_call_c *symbol, symbol_c *param_data_type) { |
|
339 |
||
340 |
function_type_t current_function_type = get_function_type((identifier_c *)symbol->function_name); |
|
341 |
function_call_param_iterator_c function_call_param_iterator(symbol); |
|
342 |
search_expression_type_c* search_expression_type = this; |
|
343 |
||
344 |
switch(current_function_type){ |
|
345 |
""" |
|
346 |
||
347 |
for fname, fdecls in [ (fname,std_fdecls[fname]) for fname in official_order ]: |
|
348 |
search_type_code += """ |
|
349 |
/**** |
|
350 |
*%s |
|
351 |
*/ |
|
352 |
case function_%s : |
|
353 |
{ |
|
354 |
symbol_c *last_type_symbol = NULL; |
|
355 |
""" %(fname, fname.lower()) |
|
356 |
indent = " " |
|
357 |
||
358 |
search_type_code += recurse_and_indent(fdecls, indent, True, True).replace('\n','\n ') |
|
359 |
||
360 |
search_type_code += """ |
|
361 |
}/*function_%s*/ |
|
362 |
break; |
|
363 |
""" %(fname.lower()) |
|
364 |
search_type_code += """ |
|
365 |
case function_none : |
|
366 |
ERROR; |
|
367 |
} |
|
368 |
return NULL; |
|
369 |
} |
|
370 |
""" |
|
371 |
||
372 |
################################################################### |
|
373 |
################################################################### |
|
374 |
################################################################### |
|
375 |
""" |
|
376 |
Generate the C implementation of the IEC standard function library. |
|
377 |
""" |
|
378 |
iec_std_lib_generated = """ |
|
379 |
/**** |
|
380 |
* IEC 61131-3 standard function lib |
|
381 |
* generated code, do not edit by hand |
|
382 |
*/ |
|
383 |
||
384 |
/* Macro that expand to subtypes */ |
|
385 |
""" |
|
386 |
for typename, parenttypename in TypeHierarchy_list: |
|
387 |
if (typename.startswith("ANY")): |
|
388 |
iec_std_lib_generated += "#define " + typename + "(DO)" |
|
389 |
for typename2, parenttypename2 in TypeHierarchy_list: |
|
390 |
if(parenttypename2 == typename): |
|
391 |
if(typename2.startswith("ANY")): |
|
392 |
iec_std_lib_generated += " " + typename2 + "(DO)" |
|
393 |
else: |
|
394 |
iec_std_lib_generated += " DO(" + typename2 + ")" |
|
395 |
iec_std_lib_generated += "\n" |
|
396 |
else: |
|
397 |
break |
|
398 |
||
399 |
if len(sys.argv) != 2 : |
|
400 |
print "Usage: " + sys.argv[0] + "path_name\n -> create files in path_name" |
|
401 |
sys.exit(0) |
|
402 |
||
403 |
# Now, print that out, or write to files from sys.argv |
|
404 |
for name, ext in [ |
|
405 |
('function_type_decl','h'), |
|
406 |
('get_function_type_decl','c'), |
|
407 |
('st_code_gen','c'), |
|
408 |
('il_code_gen','c'), |
|
26
36d378bd852e
Stage4 changes according to new STD lib implementation
etisserant
parents:
25
diff
changeset
|
409 |
('search_type_code','c')#, |
36d378bd852e
Stage4 changes according to new STD lib implementation
etisserant
parents:
25
diff
changeset
|
410 |
#('iec_std_lib_generated','h') |
36d378bd852e
Stage4 changes according to new STD lib implementation
etisserant
parents:
25
diff
changeset
|
411 |
]: |
25 | 412 |
fd = open(os.path.join(sys.argv[1],name+'.'+ext),'w') |
413 |
fd.write(eval(name)) |
|
414 |
fd.close() |