22 """expand non-primitive htype into primitive sub-field type"""
25 assert htype.data ==
'htype'
31 stack_top = stack.pop()
32 assert stack_top.data ==
'htype'
33 type_name, *type_params = stack_top.children
39 for child
in type_params:
40 if isinstance(child, Tree):
48 new_type = copy.deepcopy(htype)
49 stack.append(new_type)
51 stack_top = stack.pop()
52 type_name, *type_params = stack_top.children
54 stack_top.children = v.children
56 for child
in type_params:
57 if isinstance(child, Tree):
59 res.append((k, new_type))
62 assert False,
'Cannot expand more than 1 custom type'
72 """portdecl is the name:
73 Tree(outportdecl, [Token(ID, 'dout')])
74 typeinfo is the real type info:
75 Tree(htypeinfo, [Tree(htype, [Token(TYPESTR, 'sc_out'), Tree(htype, [Token(TYPESTR, 'fp_t'), 52, 11])])])
79 for suffix, tpe
in res:
81 for x
in portdecl.children:
83 new_token = copy.deepcopy(x)
84 new_token = new_token.update(value=new_token.value + (
'_' + suffix
if suffix
else ''))
85 children.append(new_token)
86 port = Tree(portdecl.data, children, portdecl.meta)
87 res_types.append([port, tpe])
103 assert tree.data
in [
'vardeclinit',
'funcparami',
'funcparamio']
104 if len(tree.children) == 2:
106 expanded_ports = self.
__expand_helper(Tree(
'stub', [tree.children[0]]), tree.children[1])
107 expanded_ports = [[var.children[0], tpe]
for var, tpe
in expanded_ports]
108 res = [Tree(tree.data, x, tree.meta)
for x
in expanded_ports]
110 elif len(tree.children) == 3:
113 if expanded_type
is None:
117 new_node = copy.copy(tree)
118 new_node.children = tree.children[0:3:2]
121 new_node.children[0] = Tree(
'hliteral', children=[new_node.children[0]])
124 decls = self.
__expand_helper(new_node.children[0], Tree(data=
'htypeinfo', children=[expanded_type]))
125 for x
in zip(decls, res):
126 var_name_decl, var_type = x[0]
127 var_name, init_val = x[1].children
130 assert var_name_decl == var_name
131 x[1].children[0] = x[1].children[0].children[0]
132 x[1].children.insert(1, var_type)
136 assert False,
'vardeclinit/funcparami/funcparamio should contain 2 or 3 sub-trees'
142 """expand identifiers in sensitivity list with fields"""
145 for sense_var
in tree.children:
150 type_name = var_type.children[0]
151 type_params = var_type.children[1:]
154 for field_name, _
in fields:
155 new_children.append(sense_var +
'_' + field_name)
157 new_children.append(sense_var)
158 tree.children = new_children
165 for node
in tree.children:
166 if node.data ==
'portdecltype':
167 var_name = node.children[0].children[0]
168 var_type = node.children[1].children[0]
169 var_type_name = var_type.children[1].children[0]
173 elif node.data ==
'sigdecltype':
174 var_name = node.children[0].children[0]
175 var_type = node.children[1].children[0]
178 var_tokens = map(
lambda x:
179 filter(
lambda y: isinstance(y, str), x.children),
180 var_type.iter_subtrees_topdown())
181 for var_type_name
in itertools.chain.from_iterable(var_tokens):
189 var_name = node.children[0]
190 var_type = node.children[1].children[0]
191 var_tokens = map(
lambda x:
192 filter(
lambda y: isinstance(y, str), x.children),
193 var_type.iter_subtrees_topdown())
194 type_name = var_type.children[0]
199 new_children.append(Tree(
'moduleinst', node.children, node.meta))
203 if type_name ==
'array':
205 sub_type_name = var_type.children[1].children[0]
206 if not Primitive.get_primitive(sub_type_name)
and not sub_type_name
in self.
typestypestypestypes:
208 inst_arr_name = node.children[0]
209 n_inst = var_type.children[2][0]
210 inst_type = Tree(
'htypeinfo', children=[var_type.children[1]])
211 for i
in range(n_inst):
212 inst_name = inst_arr_name +
'#' + str(i)
213 new_children.append(Tree(
'moduleinst', [inst_name, inst_type], node.meta))
215 array_of_typedef =
False
216 for var_type_name
in itertools.chain.from_iterable(var_tokens):
220 elif var_type_name ==
"array":
223 new_children.extend(res)
228 tree.children = new_children
242 assert isinstance(tree, Tree)
243 if tree.data
in [
'hliteral',
'hvarref']:
244 var_name = tree.children[0]
247 elif tree.data ==
'harrayref':
250 if tree.children[0].data ==
'hsigassignr':
251 var_name = tree.children[1].children[0]
253 var_name = tree.children[0].children[0]
256 elif tree.data ==
'hbinop' and tree.children[0] ==
'ARRAYSUBSCRIPT':
257 var_name = tree.children[1].children[0]
260 elif tree.data ==
'hvarref':
261 var_name = tree.children[0]
264 elif tree.data ==
'syscread':
267 assert tree.children[1].data
in [
'hliteral',
'hvarref',
'harrayref'], f
'Actual: {tree.children[1].data} ({tree})'
268 if tree.children[1].data
in [
'harrayref']:
271 var_name = tree.children[1].children[0]
274 elif tree.data ==
'hvarinitlist':
277 for element
in tree.children:
283 """append the field_name to the expandable variable in tree"""
285 assert isinstance(tree, Tree)
286 if tree.data
in [
'hliteral',
'hvarref']:
287 var_name = tree.children[0]
289 tree.children[0] = var_name +
'_' + field_name
290 elif tree.data ==
'harrayref':
291 var_name = tree.children[0].children[0]
293 tree.children[0].children[0] = var_name +
'_' + field_name
294 elif tree.data ==
'hbinop' and tree.children[0] ==
'ARRAYSUBSCRIPT':
295 var_name = tree.children[1].children[0]
297 tree.children[1].children[0] = var_name +
'_' + field_name
298 elif tree.data ==
'syscread':
299 assert tree.children[1].data
in [
'hliteral',
'hvarref',
'harrayref']
300 if tree.children[1].data
in [
'harrayref']:
303 var_name = tree.children[1].children[0]
305 tree.children[1].children[0] = var_name +
'_' + field_name
306 elif tree.data ==
'hvarinitlist':
307 for t
in tree.children:
320 """detects the expandable variable on lhs and rhs and
321 expand them with the fields"""
323 lhs, rhs = tree.children
330 if lhs_var
is not None and (
not self.
__is_all_none(rhs_var)
or rhs.data ==
'hliteral')
and (rhs_var
is not None or rhs.data ==
'hliteral'):
332 assert lhs_expanded_type
is not None,
'{} should have expanded type'.format(lhs_var)
335 if isinstance(rhs_var,list):
337 if lhs_type.children[0] != rhs_type.children[0]:
338 raise RuntimeError(
'Type does not match between LHS and RHS')
339 for remaining_rhs_var
in rhs_var:
341 if rhs_type.children[0] != rhs_var_type.children[0]:
342 raise RuntimeError(
'Type does not match among RHS elements')
343 elif rhs.data !=
'hliteral':
346 if lhs_type.children[0] != rhs_type.children[0]:
347 raise RuntimeError(
'Type does not match between LHS and RHS')
350 assert rhs.data ==
'hliteral'
351 type_name = lhs_type.children[0]
352 type_params = lhs_type.children[1:]
356 for field_member, _
in fields:
357 new_assign = copy.deepcopy(tree)
358 if tree.data ==
'blkassign':
359 new_assign.must_block = tree.must_block
360 assert type(new_assign.must_block) == type(
False)
361 new_lhs, new_rhs = new_assign.children
363 if rhs.data ==
'hliteral':
364 new_assign.children[1] = Tree(
'hliteral', [0], meta=rhs.meta)
367 res.append(new_assign)
375 raise RuntimeError(
'Error while expanding blkassign, LHS and RHS expandability does not match')
425 for sense_var
in tree.children[1:]:
432 type_name = var_type.children[0]
433 type_params = var_type.children[1:]
436 for field_name, _
in fields:
437 new_sense_var = copy.deepcopy(sense_var)
442 new_children.append(new_sense_var)
444 new_children.append(sense_var)
445 tree.children[1:] = new_children
460 if expand_data
is None:
461 expand_data = [
'vardeclinit']
463 for node
in tree.children:
464 if node.data
in expand_data:
465 var_name = node.children[0]
466 var_type = node.children[1].children[0]
467 var_tokens = map(
lambda x:
468 filter(
lambda y: isinstance(y, str), x.children),
469 var_type.iter_subtrees_topdown())
470 type_name = var_type.children[0]
471 if 'funcparamio' in expand_data:
476 assert False,
'Type {} not found or module instantiation cannot reside in process: {}, {}'.format(type_name, var_name, type_name)
477 for var_type_name
in itertools.chain.from_iterable(var_tokens):
482 new_children.extend(res)
484 new_children.append(node)
497 module_name, *bindings = tree.children
499 for binding
in bindings:
500 mod_name, sub, par = binding.children
501 sub_v = sub.children[0]
502 if is_tree_type(par,
'hbindingarrayref'):
503 par_v = get_ids_in_tree(par)[0]
505 par_v = par.children[0]
511 for field
in tpe.fields:
512 new_sub = copy.deepcopy(sub)
513 new_par = copy.deepcopy(par)
517 x.value +=
'_' + field.children[0]
519 alternate_ids(new_sub, [__alternate])
520 alternate_ids(new_par, [__alternate])
523 new_binding = copy.copy(binding)
524 new_binding.children = [mod_name, new_sub, new_par]
525 b.append(new_binding)
526 new_bindings.extend(b)
528 new_bindings.append(binding)
529 tree.children = [module_name, new_bindings]
536 for sense_var
in tree.children[1:]:
538 var_ids = get_ids_in_tree(sense_var.children[0])
539 if len(var_ids) != 1:
540 raise ValueError(
'Sensitivity variable should only have one ID')
541 var_name = var_ids[0]
546 type_name = var_type.children[0]
547 type_params = var_type.children[1:]
551 def __alternate(x, y):
554 for field_name, _
in fields:
555 new_sense_var = copy.deepcopy(sense_var)
557 set_ids_in_tree_dfs(new_sense_var, [
lambda x: __alternate(x, field_name)])
558 new_children.append(new_sense_var)
561 new_children.append(sense_var)
562 tree.children[1:] = new_children