systemc-clang
2.0.0
Parsing SystemC constructs
Loading...
Searching...
No Matches
plugins
hdl
parselib
transforms
alias_translation.py
Go to the documentation of this file.
1
from
lark
import
Tree, Token
2
from
parselib.utils
import
dprint
3
from
parselib.transforms
import
TopDown
4
import
copy
5
6
7
class
AliasTranslation
(
TopDown
):
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
16
def
_get_op_token_from_compound_assign
(self, op):
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
58
new_op = self.
_get_op_token_from_compound_assign
(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
parselib.transforms.alias_translation.AliasTranslation
Definition
alias_translation.py:7
parselib.transforms.alias_translation.AliasTranslation.stmt
stmt(self, tree)
Definition
alias_translation.py:103
parselib.transforms.alias_translation.AliasTranslation.hcompoundasisgn
hcompoundasisgn(self, tree)
Definition
alias_translation.py:46
parselib.transforms.alias_translation.AliasTranslation.htype
htype(self, tree)
Definition
alias_translation.py:82
parselib.transforms.alias_translation.AliasTranslation.blkassign
blkassign(self, tree)
Definition
alias_translation.py:66
parselib.transforms.alias_translation.AliasTranslation.syscread
syscread(self, tree)
Definition
alias_translation.py:32
parselib.transforms.alias_translation.AliasTranslation.hunop
hunop(self, tree)
Definition
alias_translation.py:109
parselib.transforms.alias_translation.AliasTranslation._get_op_token_from_compound_assign
_get_op_token_from_compound_assign(self, op)
Definition
alias_translation.py:16
parselib.transforms.alias_translation.AliasTranslation.htypearray
htypearray(self, tree)
Definition
alias_translation.py:97
parselib.transforms.alias_translation.AliasTranslation.hsensedge
hsensedge(self, tree)
Definition
alias_translation.py:27
parselib.transforms.alias_translation.AliasTranslation.hbinop
hbinop(self, tree)
Definition
alias_translation.py:49
parselib.transforms.alias_translation.AliasTranslation.hsubassign
hsubassign(self, tree)
Definition
alias_translation.py:40
parselib.transforms.alias_translation.AliasTranslation.arraydimlength
arraydimlength(self, tree)
Definition
alias_translation.py:89
parselib.transforms.alias_translation.AliasTranslation.haddassign
haddassign(self, tree)
Definition
alias_translation.py:43
parselib.transforms.alias_translation.AliasTranslation.__init__
__init__(self)
Definition
alias_translation.py:24
parselib.transforms.top_down.TopDown
Definition
top_down.py:24
parselib.transforms.top_down.TopDown.__push_up
__push_up(self, current_node)
Definition
top_down.py:29
parselib.transforms
Definition
__init__.py:1
parselib.utils
Definition
utils.py:1
Generated by
1.12.0