-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathmodify.py
More file actions
275 lines (212 loc) · 9.46 KB
/
modify.py
File metadata and controls
275 lines (212 loc) · 9.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
import matlab2cpp
import matlab2cpp.node as nmodule
def preorder_transform_AST(node, nargin = False, suggest = False):
# Modify the abstract syntax tree (AST), also try to overload funtions
# node is project node
project = node.project
nodes = nmodule.Node.flatten(project, False, False, False)
# remove the nodes for clear, close and clc so they are not included in the translation
nodes = remove_close_clear_clc(nodes)
# Change right hand side variable to uvec if assigned with find, b = find(a==3)
if suggest:
nodes = modify_find(nodes)
#a multiplication with a complex double results in complex double
#works with fx_decon_demo.m needs more testing and maybe a refactoring
if suggest:
nodes = complex_mul(nodes)
# remove nargin if args.nargin == False, Thus by default. Use -n flag to keep nargin
if nargin == False:
project = remove_nargin(project)
# add temporary variables for multiple return function
project = add_parameters(project)
# change data type from real to complex, if left hand side is real and right hand side is complex in assignment
if suggest:
change_to_complex(project)
return project
def postorder_transform_AST(node):
project = node.project
# move the "using namespace arma ;" node last in the includes list
project = modify_arma_last(project)
#move #define NOMINMAX to first position
project = modify_define_first(project)
return project
# node is project node
def change_to_complex(project):
# for each program in project
for idx, program in enumerate(project):
dictionary = program.ftypes
new_complex_types = {}
new_complex_dim = {}
# for each function (in Funcs) in program
for func in program[1]:
func_name = func.name
# flatten nodes in the function
nodes = func.flatten(False, False, False)
#look for assignment
for n in nodes:
if n.cls in "Assign" and len(n) == 2:
lhs, rhs = n
if lhs.mem and lhs.mem != 4 and rhs.mem == 4:
lhs.mem = 4
# add variable name as key and complex type as type
if (lhs.name in new_complex_dim) and (new_complex_dim[lhs.name] < lhs.dim):
new_complex_dim[lhs.name] = lhs.dim
lhs.type = (lhs.dim, lhs.mem)
new_complex_types[lhs.name] = lhs.type
else:
new_complex_dim[lhs.name] = lhs.dim
lhs.type = (lhs.dim, lhs.mem)
new_complex_types[lhs.name] = lhs.type
dictionary[func_name][lhs.name] = new_complex_types[lhs.name]
# clear the types in the program
nodes = program.flatten(False, False, False)
for idy, node in enumerate(nodes):
node.type = "TYPE"
# use dictionary to set ftypes
program.ftypes = dictionary
# unset the configured flag
project.builder.configured = False
# configure again
project.builder.configure(True)
#print(dictionary)
def complex_mul(nodes):
for node in nodes:
if node.cls == "Assign":
lhs, rhs = node
if rhs.cls == "Mul":
if rhs[0].type == "cx_double":
declares = node.func[0]
for var in declares:
if var.name == lhs.name:
var.type = "cx_double"
return nodes
# remove the nodes for clear, close and clc so they are not included in the translation
def remove_close_clear_clc(nodes):
for n in nodes:
if n.backend == "reserved" and n.name in ("clear", "close", "clc"):
index = n.parent.parent.children.index(n.parent)
del n.parent.parent.children[index]
return nodes
# Change right hand side variable to uvec if assigned with find, b = find(a==3)
def modify_find(nodes):
for n in nodes:
if n.cls == "Assign":
lhs, rhs = n
if rhs.name == "find":
declares = n.func[0]
#print(declares.cls)
for var in declares:
if var.name == lhs.name:
var.type = "uvec"
return nodes
# move the "using namespace arma ;" node last in the includes list
def modify_define_first(project):
for program in project:
includes = program[0]
index = 0
for include in includes:
if include != includes[0] and include.name[:7] == "#define":
define_include = includes.children.pop(index)
includes.children.insert(0, define_include)
index += 1
return project
# move the "using namespace arma ;" node last in the includes list
def modify_arma_last(project):
for program in project:
includes = program[0]
index = 0
for include in includes:
if include != includes[-1] and include.name == "using namespace arma ;":
#includes += [includes.pop(index)] # remove and append arma include node
using_arma = includes.children.pop(index)
includes.children.append(using_arma)
index += 1
return project
# remove nargin if args.nargin == False, Thus by default. Use -n flag to keep nargin
def remove_nargin(project):
# node is project
for program in project:
# for func in funcs
funcs = program[1]
for func in funcs:
#print(func.summary())
block = func[3]
# find node.name == nargin
found_nargin = True
while found_nargin:
found_nargin = False
nodes = nmodule.Node.flatten(block, False, False, False)
# remove if node.group is branch
for n in nodes:
if n.name == "nargin":
# remove branch
if n.group.cls in ("Branch", "Switch"):
parent = n.group.parent
# print(parent.summary())
del parent.children[parent.children.index(n.group)]
# node.group.parent.children.index(node.group)
found_nargin = True
break
else: # node.group is not a branch
found_nargin = False
break
return project
# add temporary variables for multiple return function
def add_parameters(project):
nodes = nmodule.Node.flatten(project, False, False, False)
for n in nodes:
if n.cls == "Get" and n.backend == "func_returns":
func_name = n.name
func_ret_num = 0
if n.parent.cls in ("Assign", "Assigns"):
for sub_node in n.parent:
if sub_node.cls != "Get":
func_ret_num += 1
else:
break
#print(n.summary())
# find the multiple return function
# Check if function is in the same program
# Look first for function in current file
funcs = n.program[1]
found_func = False
func = []
for f in funcs:
if f.name == n.name:
func_found = True
#programs = n.program
func = f
# Look for function in external file
if found_func == False:
for program in n.project:
f = program[1][0]
if f.name == n.name:
func = f
break
#programs = [p[1][0] for p in project if p[1][0]= n.program]
# add needed temporary variables to params
if func:
return_params = func[1]
# dictionary which is used as a counter
type_counter = {"int" : 0, "float" : 0, "double" : 0, "uword" : 0, "cx_double" : 0,
"ivec" : 0, "fvec" : 0, "uvec" : 0, "vec" : 0, "cx_vec" : 0,
"irowvec" : 0, "frowvec" : 0, "urowvec" : 0, "rowvec" : 0, "cx_rowvec" : 0,
"imat" : 0, "fmat" : 0, "umat" : 0, "mat" : 0, "cx_mat" : 0,
"icube" : 0, "fcube" : 0, "ucube" : 0, "cube" : 0, "cx_cube" : 0}
# add or reuse variables, also add change
while func_ret_num < len(return_params):
if return_params[func_ret_num].type not in ("TYPE", "string"):
type_counter[return_params[func_ret_num].type] += 1
name = "_tmp_" + return_params[func_ret_num].type + \
str(type_counter[return_params[func_ret_num].type])
# Allocate Var node. n.parent is the assign node
swap_var = matlab2cpp.collection.Var(n.parent, name)
swap_var.type = return_params[func_ret_num].type
swap_var.backend = return_params[func_ret_num].backend
swap_var.create_declare()
# swap Var and Get (function_returns)
n.parent.children[func_ret_num] = swap_var
n.parent.children[-1] = n
#index += 1
func_ret_num += 1
return project