systemc-clang
2.0.0
Parsing SystemC constructs
Loading...
Searching...
No Matches
plugins
hdl
parselib
transforms
literal_expansion.py
Go to the documentation of this file.
1
import
warnings
2
3
import
lark
4
from
lark
import
Token
5
6
from
parselib.transforms
import
TopDown
7
from
..utils
import
dprint, is_tree_type, get_ids_in_tree
8
from
..grammar
import
UnexpectedHCodeStructureError
9
10
11
class
LiteralExpansion
(
TopDown
):
12
"""Expands integer literal into int"""
13
def
__init__
(self, structure):
14
super().
__init__
()
15
self.
structure
= structure
16
self.
is_port_binding
=
False
17
self.
port_binding_module
=
None
18
self.
field_access
=
None
19
20
def
_get_port_binding_moduel
(self, tree):
21
"""
22
Get the name of the module that corresponds
23
to this port-binding
24
"""
25
if
type(tree) == lark.Tree:
26
if
tree.children[0] !=
'NONAME'
:
27
res = str(self.
structure
[self.
current_module
][tree.children[0]])
28
else
:
29
ids = get_ids_in_tree(tree)
30
res = str(self.
structure
[self.
current_module
][ids[0]])
31
else
:
32
res = str(self.
structure
[self.
current_module
][tree.children[0]])
33
return
res
34
35
def
portbinding
(self, tree):
36
# For now we just move port binding as is
37
# self.is_port_binding = True
38
# port_binding_module = self._get_port_binding_moduel(tree)
39
# self.__push_up(tree)
40
# self.is_port_binding = False
41
# self.port_binding_module = None
42
return
tree
43
44
def
hfieldaccess
(self, tree):
45
"""
46
hFieldaccess NONAME [
47
hBinop ARRAYSUBSCRIPT [
48
hBinop ARRAYSUBSCRIPT [
49
hVarref pa_scclang_global_15 NOLIST
50
hVarref sig_scclang_global_1 NOLIST
51
]
52
hVarref sig_scclang_global_1 NOLIST
53
]
54
hField x NOLIST
55
]
56
"""
57
# Field access is dedicated to struct, so no need to worry about the module
58
field_name = tree.children[1].children[0]
59
if
self.
field_access
is
not
None
:
60
self.
field_access
.append(field_name)
61
else
:
62
self.
field_access
= [field_name]
63
self.
__push_up
(tree)
64
return
tree.children[0]
65
66
67
def
hvarref
(self, tree):
68
if
'##'
in
tree.children[0]:
69
orig_token = tree.children[0]
70
new_val =
''
71
parts = orig_token.split(
'##'
)
72
start = self.
current_module
73
# if self.is_port_binding: # if we are in port binding, we shall take the ids directly
74
# start = self.port_binding_module
75
if
start
in
self.
structure
:
76
m = self.
structure
[start]
77
end_point =
False
78
else
:
79
m = {}
80
end_point =
True
81
for
part
in
parts[:-1]:
82
new_val += part
83
if
not
end_point:
84
if
part
in
m:
85
delim =
'.'
86
m = self.
structure
[str(m[part])]
87
else
:
88
end_point =
True
89
delim =
'_'
90
else
:
91
delim =
'_'
92
new_val += delim
93
new_val += parts[-1]
94
95
# new_val = tree.children[0].value.replace('##', '_')
96
new_token = Token(
'ID'
, new_val)
97
tree.children[0] = new_token
98
if
self.
field_access
is
not
None
:
99
tree.children[0] +=
'_'
+
'_'
.join(self.
field_access
)
100
self.
field_access
=
None
101
return
tree
102
103
def
idlit
(self, tree):
104
str_literal = tree.children[0]
105
return
str_literal
106
107
def
numlit
(self, tree):
108
num_literal = int(tree.children[0])
109
return
num_literal
110
111
def
numlitwidth
(self, tree):
112
self.
__push_up
(tree)
113
tree.children[0] = int(tree.children[0])
# only supports decimal literals and primitive types for now
114
return
tree
115
116
def
htypeint
(self, tree):
117
return
int(tree.children[0])
118
119
def
htype
(self, tree):
120
self.
__push_up
(tree)
121
if
len(tree.children) == 1
and
isinstance(tree.children[0], int):
122
return
tree.children[0]
123
else
:
124
return
tree
125
126
def
hsensvar
(self, tree):
127
self.
__push_up
(tree)
128
if
len(tree.children) == 1
and
is_tree_type(tree.children[0],
'hasync'
):
129
tree.children = tree.children[0].children
130
elif
len(tree.children) != 2:
131
raise
UnexpectedHCodeStructureError
(
'hSensvar node should have 2 children'
)
132
return
tree
133
134
def
hasync
(self, tree):
135
self.
__push_up
(tree)
136
assert
len(tree.children) == 2
137
if
tree.children[1].children[0] == 0:
# negedge
138
tree.children[1] = Token(
'NEG'
,
'neg'
)
139
elif
tree.children[1].children[0] == 1:
140
tree.children[1] = Token(
'POS'
,
'pos'
)
141
return
tree
142
143
def
npa
(self, tree):
144
return
tree.children[0]
145
146
def
hmodule
(self, tree):
147
self.
current_module
= tree.children[0]
148
self.
__push_up
(tree)
149
self.
current_module
=
None
150
return
tree
151
152
153
class
LiteralExpansion2
(
TopDown
):
154
def
__init__
(self):
155
super().
__init__
()
156
157
def
hvarref
(self, tree):
158
if
isinstance(tree.children[0], Token)
and
'##'
in
tree.children[0].value:
159
tree.children[0] = Token(
'ID'
, tree.children[0].value.replace(
'##'
,
'_'
))
160
return
tree
161
162
def
handle_list
(self, lst):
163
for
idx
in
range(len(lst)):
164
if
isinstance(lst[idx], lark.Tree):
165
self.
__push_up
(lst[idx])
166
167
def
portbindinglist
(self, tree):
168
for
c
in
tree.children:
169
if
isinstance(c, list):
170
self.
handle_list
(c)
171
return
tree
parselib.grammar.UnexpectedHCodeStructureError
Definition
grammar.py:332
parselib.transforms.literal_expansion.LiteralExpansion2
Definition
literal_expansion.py:153
parselib.transforms.literal_expansion.LiteralExpansion2.hvarref
hvarref(self, tree)
Definition
literal_expansion.py:157
parselib.transforms.literal_expansion.LiteralExpansion2.__init__
__init__(self)
Definition
literal_expansion.py:154
parselib.transforms.literal_expansion.LiteralExpansion2.handle_list
handle_list(self, lst)
Definition
literal_expansion.py:162
parselib.transforms.literal_expansion.LiteralExpansion2.portbindinglist
portbindinglist(self, tree)
Definition
literal_expansion.py:167
parselib.transforms.literal_expansion.LiteralExpansion
Definition
literal_expansion.py:11
parselib.transforms.literal_expansion.LiteralExpansion.current_module
current_module
Definition
literal_expansion.py:147
parselib.transforms.literal_expansion.LiteralExpansion.__init__
__init__(self, structure)
Definition
literal_expansion.py:13
parselib.transforms.literal_expansion.LiteralExpansion.hvarref
hvarref(self, tree)
Definition
literal_expansion.py:67
parselib.transforms.literal_expansion.LiteralExpansion.hsensvar
hsensvar(self, tree)
Definition
literal_expansion.py:126
parselib.transforms.literal_expansion.LiteralExpansion.numlit
numlit(self, tree)
Definition
literal_expansion.py:107
parselib.transforms.literal_expansion.LiteralExpansion.hfieldaccess
hfieldaccess(self, tree)
Definition
literal_expansion.py:44
parselib.transforms.literal_expansion.LiteralExpansion.hasync
hasync(self, tree)
Definition
literal_expansion.py:134
parselib.transforms.literal_expansion.LiteralExpansion.structure
structure
Definition
literal_expansion.py:15
parselib.transforms.literal_expansion.LiteralExpansion.field_access
list field_access
Definition
literal_expansion.py:18
parselib.transforms.literal_expansion.LiteralExpansion.hmodule
hmodule(self, tree)
Definition
literal_expansion.py:146
parselib.transforms.literal_expansion.LiteralExpansion.portbinding
portbinding(self, tree)
Definition
literal_expansion.py:35
parselib.transforms.literal_expansion.LiteralExpansion.htype
htype(self, tree)
Definition
literal_expansion.py:119
parselib.transforms.literal_expansion.LiteralExpansion.is_port_binding
bool is_port_binding
Definition
literal_expansion.py:16
parselib.transforms.literal_expansion.LiteralExpansion._get_port_binding_moduel
_get_port_binding_moduel(self, tree)
Definition
literal_expansion.py:20
parselib.transforms.literal_expansion.LiteralExpansion.htypeint
htypeint(self, tree)
Definition
literal_expansion.py:116
parselib.transforms.literal_expansion.LiteralExpansion.npa
npa(self, tree)
Definition
literal_expansion.py:143
parselib.transforms.literal_expansion.LiteralExpansion.idlit
idlit(self, tree)
Definition
literal_expansion.py:103
parselib.transforms.literal_expansion.LiteralExpansion.port_binding_module
port_binding_module
Definition
literal_expansion.py:17
parselib.transforms.literal_expansion.LiteralExpansion.numlitwidth
numlitwidth(self, tree)
Definition
literal_expansion.py:111
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
Generated by
1.12.0