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