systemc-clang 2.0.0
Parsing SystemC constructs
Loading...
Searching...
No Matches
alias_translation.py
Go to the documentation of this file.
1from lark import Tree, Token
2from parselib.utils import dprint
3from parselib.transforms import TopDown
4import copy
5
6
8 """
9 This pass merges separate nodes that are created for easy recognition for grammar,
10 but actually shares the same semantics.
11 TODO: this pass does almost the same thing as the pass NodeMerging Pass, and
12 TODO: the 2 classes should be merged together
13 """
14 COMPOUND_ASSIGN = ["*=", "+=", "-=", "/=", "%=", "|=", "&=", "^=", "<<=", ">>="]
15
17 assert op in AliasTranslation.COMPOUND_ASSIGN
18 new_op = op[:-1]
19 if new_op in ["+", "-", "*", "/", "^"]:
20 return Token('ARITHOP', new_op)
21 else:
22 return Token('NONSUBBINOP', new_op)
23
24 def __init__(self):
25 super().__init__()
26
27 def hsensedge(self, tree):
28 self.__push_up(tree)
29 tree.data = 'hsensvar'
30 return tree
31
32 def syscread(self, tree):
33 """handles the syscread for array ref"""
34 # dprint(tree)
35 # if len(tree.children) == 1 and tree.children[0].data == 'hbinop' and tree.children[0].children[0] == 'ARRAYSUBSCRIPT':
36 # pass
37 # # assert False
38 return tree
39
40 def hsubassign(self, tree):
41 return '-'
42
43 def haddassign(self, tree):
44 return '+'
45
46 def hcompoundasisgn(self, tree):
47 assert False
48
49 def hbinop(self, tree):
50 """
51 Convert a compound assignment into assignment of binary operator
52 Nested compound assignment is not supported
53 """
54 op = tree.children[0]
55 if op in AliasTranslation.COMPOUND_ASSIGN:
56 lhs, rhs = tree.children[1:]
57 # Convert this node into a block assignment node with binary op
59 # Create a new treenode
60 meta = tree.meta
61 binop_node = Tree('hbinop', [new_op] + tree.children[1:], meta)
62 assign = Tree('blkassign', [copy.deepcopy(lhs), binop_node], meta)
63 return assign
64 return tree
65
66 def blkassign(self, tree):
67 # we should detect blocking assignment in a different way
68 tree.must_block = True
69 if tree.children[0].data == 'vassign':
70 tree.children = tree.children[0].children
71 tree.must_block = True
72 elif tree.children[0].data == 'nblkassign':
73 tree.children = tree.children[0].children
74 tree.must_block = False
75 elif tree.children[0].data == 'hmodassign':
76 self.__push_up(tree)
77 op, l, r = tree.children[0].children
78 tree.children = [l, Tree('hbinop', [Token('BINOP', op), l, r], meta=tree.meta)]
79 tree.must_block = False
80 return tree
81
82 def htype(self, tree):
83 is_array = isinstance(tree.children[0], Tree) and tree.children[0].data == 'htypearray'
84 self.__push_up(tree)
85 if is_array:
86 tree.children = tree.children[0]
87 return tree
88
89 def arraydimlength(self, tree):
90 self.__push_up(tree)
91 dim = int(tree.children[0])
92 if len(tree.children) == 1: # the last dim
93 return [dim]
94 else:
95 return [dim] + tree.children[1]
96
97 def htypearray(self, tree):
98 self.__push_up(tree)
99 sz, tpe = tree.children
100 res = ['array', tpe, sz]
101 return res
102
103 def stmt(self, tree):
104 """filters out noop"""
105 self.__push_up(tree)
106 tree.children = list(filter(lambda x: x.data != 'hnoop', tree.children))
107 return tree
108
109 def hunop(self, tree):
110 # self.__push_up(tree)
111 if isinstance(tree.children[0], Tree) and tree.children[0].data == 'hunopdec':
112 tree.children = [Token('UNOP_NON_DEC', '--'), tree.children[0].children[0]]
113 return tree
__push_up(self, current_node)
Definition top_down.py:29