systemc-clang 2.0.0
Parsing SystemC constructs
Loading...
Searching...
No Matches
node_movement.py
Go to the documentation of this file.
1from lark import Tree, Token
2from parselib.utils import dprint
3from parselib.transforms import TopDown
4from ..utils import ContextManager
5
6
8 def __init__(self):
9 super().__init__()
10 # record function per module
11 self.functions = dict()
12 self.current_module = None
13
14 def hmodule(self, tree):
15 self.current_module = tree.children[0]
16 self.__push_up(tree)
17 functions = self.functions.get(self.current_module)
18 if functions:
19 for f in functions:
20 tree.children.append(f)
21 self.current_module = None
22 return tree
23
24 def __add_functions(self, functions):
25 res = self.functions.setdefault(self.current_module, [])
26 res.extend(functions)
27
28 def hprocess(self, tree):
29 functions = list(filter(lambda x: isinstance(x, Tree) and x.data == 'hfunction', tree.children))
30 tree.children = list(filter(lambda x: not (isinstance(x, Tree) and x.data == 'hfunction'), tree.children))
31 self.__add_functions(functions)
32 return tree
33
34
36 def __init__(self):
38 super().__init__()
39
41 assert tree.data == 'portdecltype', "Expecting portdecltype"
42 new_tree = Tree('sigdecltype', tree.children, meta=tree.meta)
43 new_tree.children[0] = Tree('sigdecl', [tree.children[0].children[0]])
44 new_tree.meta.direction = tree.direction
45 return new_tree
46
47 def modportsiglist(self, tree):
48 with self.ctx.add_values(port_decl_to_remove=[]):
49 self.__push_up(tree)
50 if self.ctx.port_decl_to_remove:
51 tree.children = list(filter(lambda x: x not in self.ctx.port_decl_to_remove, tree.children))
52 # move it to varlist
53 insertion_pos = -1
54 # find the last sigdecltype, or last portdecltype if no sigdecltype exists
55 for i in range(len(tree.children) - 1, -1, -1):
56 if tree.children[i].data == 'sigdecltype':
57 insertion_pos = i
58 break
59 elif tree.children[i].data == 'portdecltype':
60 insertion_pos = i
61 break
62 new_sig_decls = list(map(self.portdecltype_to_sigdecltype, self.ctx.port_decl_to_remove))
63 tree.children = tree.children[:insertion_pos + 1] + new_sig_decls + tree.children[insertion_pos + 1:]
64
65 # for port in self.ctx.port_decl_to_remove:
66 # tree.children.append(
67 # self.portdecltype_to_sigdecltype(port)
68 # )
69
70 return tree
71
72 def portdecltype(self, tree):
73 with self.ctx.add_values(current_port_decl_node=tree):
74 self.__push_up(tree)
75 return tree
76
77 def htype(self, tree):
78 # If this is a port array
79 if self.ctx.current_port_decl_node and tree.children[0] == 'array':
80 if self.ctx.current_port_decl_node.children[0].data == 'inportdecl':
81 with self.ctx.add_values(port_erasure=True):
82 self.__push_up(tree)
83 self.ctx.current_port_decl_node.direction = 'input'
84 self.ctx.port_decl_to_remove.append(self.ctx.current_port_decl_node)
85 elif self.ctx.current_port_decl_node.children[0].data == 'outportdecl':
86 self.ctx.current_port_decl_node.direction = 'output'
87 else:
88 raise ValueError("Unknown port type")
89 elif self.ctx.port_erasure:
90 if tree.children[0] in ['sc_in', 'sc_out']:
91 return tree.children[1]
92 return tree
93 return tree
94
__push_up(self, current_node)
Definition top_down.py:29