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
from
lark
import
Token
4
5
from
parselib.transforms
import
TopDown
6
from
..utils
import
dprint, is_tree_type
7
from
..grammar
import
UnexpectedHCodeStructureError
8
9
10
class
LiteralExpansion
(
TopDown
):
11
"""Expands integer literal into int"""
12
def
__init__
(self, structure):
13
super().
__init__
()
14
self.
structure
= structure
15
self.
is_port_binding
is_port_binding
=
False
16
self.
port_binding_module
=
None
17
self.
field_access
=
None
18
19
def
portbinding
(self, tree):
20
self.
is_port_binding
is_port_binding
=
True
21
self.
port_binding_module
= str(self.
structure
[self.
current_module
][tree.children[0]])
22
self.
__push_up
(tree)
23
self.
is_port_binding
is_port_binding
=
False
24
self.
port_binding_module
=
None
25
return
tree
26
27
def
hfieldaccess
(self, tree):
28
"""
29
hFieldaccess NONAME [
30
hBinop ARRAYSUBSCRIPT [
31
hBinop ARRAYSUBSCRIPT [
32
hVarref pa_scclang_global_15 NOLIST
33
hVarref sig_scclang_global_1 NOLIST
34
]
35
hVarref sig_scclang_global_1 NOLIST
36
]
37
hField x NOLIST
38
]
39
"""
40
# Field access is dedicated to struct, so no need to worry about the module
41
field_name = tree.children[1].children[0]
42
if
self.
field_access
is
not
None
:
43
self.
field_access
.append(field_name)
44
else
:
45
self.
field_access
= [field_name]
46
self.
__push_up
(tree)
47
return
tree.children[0]
48
49
50
def
hvarref
(self, tree):
51
if
'##'
in
tree.children[0]:
52
orig_token = tree.children[0]
53
new_val =
''
54
parts = orig_token.split(
'##'
)
55
start = self.
current_module
56
if
self.
is_port_binding
is_port_binding
:
# if we are in port binding, we shall take the ids directly
57
start = self.
port_binding_module
58
if
start
in
self.
structure
:
59
m = self.
structure
[start]
60
end_point =
False
61
else
:
62
m = {}
63
end_point =
True
64
for
part
in
parts[:-1]:
65
new_val += part
66
if
not
end_point:
67
if
part
in
m:
68
delim =
'.'
69
m = self.
structure
[str(m[part])]
70
else
:
71
end_point =
True
72
delim =
'_'
73
else
:
74
delim =
'_'
75
new_val += delim
76
new_val += parts[-1]
77
78
# new_val = tree.children[0].value.replace('##', '_')
79
new_token = Token(
'ID'
, new_val)
80
tree.children[0] = new_token
81
if
self.
field_access
is
not
None
:
82
tree.children[0] +=
'_'
+
'_'
.join(self.
field_access
)
83
self.
field_access
=
None
84
return
tree
85
86
def
idlit
(self, tree):
87
str_literal = tree.children[0]
88
return
str_literal
89
90
def
numlit
(self, tree):
91
num_literal = int(tree.children[0])
92
return
num_literal
93
94
def
numlitwidth
(self, tree):
95
self.
__push_up
(tree)
96
tree.children[0] = int(tree.children[0])
# only supports decimal literals and primitive types for now
97
return
tree
98
99
def
htypeint
(self, tree):
100
return
int(tree.children[0])
101
102
def
htype
(self, tree):
103
self.
__push_up
(tree)
104
if
len(tree.children) == 1
and
isinstance(tree.children[0], int):
105
return
tree.children[0]
106
else
:
107
return
tree
108
109
def
hsensvar
(self, tree):
110
self.
__push_up
(tree)
111
if
len(tree.children) == 1
and
is_tree_type(tree.children[0],
'hasync'
):
112
tree.children = tree.children[0].children
113
elif
len(tree.children) != 2:
114
raise
UnexpectedHCodeStructureError
(
'hSensvar node should have 2 children'
)
115
return
tree
116
117
def
hasync
(self, tree):
118
self.
__push_up
(tree)
119
assert
len(tree.children) == 2
120
if
tree.children[1].children[0] == 0:
# negedge
121
tree.children[1] = Token(
'NEG'
,
'neg'
)
122
elif
tree.children[1].children[0] == 1:
123
tree.children[1] = Token(
'POS'
,
'pos'
)
124
return
tree
125
126
def
npa
(self, tree):
127
return
tree.children[0]
128
129
def
hmodule
(self, tree):
130
self.
current_module
= tree.children[0]
131
self.
__push_up
(tree)
132
self.
current_module
=
None
133
return
tree
parselib.grammar.UnexpectedHCodeStructureError
Definition
grammar.py:328
parselib.transforms.literal_expansion.LiteralExpansion
Definition
literal_expansion.py:10
parselib.transforms.literal_expansion.LiteralExpansion.current_module
current_module
Definition
literal_expansion.py:130
parselib.transforms.literal_expansion.LiteralExpansion.__init__
__init__(self, structure)
Definition
literal_expansion.py:12
parselib.transforms.literal_expansion.LiteralExpansion.hvarref
hvarref(self, tree)
Definition
literal_expansion.py:50
parselib.transforms.literal_expansion.LiteralExpansion.hsensvar
hsensvar(self, tree)
Definition
literal_expansion.py:109
parselib.transforms.literal_expansion.LiteralExpansion.numlit
numlit(self, tree)
Definition
literal_expansion.py:90
parselib.transforms.literal_expansion.LiteralExpansion.hfieldaccess
hfieldaccess(self, tree)
Definition
literal_expansion.py:27
parselib.transforms.literal_expansion.LiteralExpansion.hasync
hasync(self, tree)
Definition
literal_expansion.py:117
parselib.transforms.literal_expansion.LiteralExpansion.structure
structure
Definition
literal_expansion.py:14
parselib.transforms.literal_expansion.LiteralExpansion.field_access
list field_access
Definition
literal_expansion.py:17
parselib.transforms.literal_expansion.LiteralExpansion.hmodule
hmodule(self, tree)
Definition
literal_expansion.py:129
parselib.transforms.literal_expansion.LiteralExpansion.portbinding
portbinding(self, tree)
Definition
literal_expansion.py:19
parselib.transforms.literal_expansion.LiteralExpansion.is_port_binding
# if we are in port binding, we shall take the ids directly is_port_binding
Definition
literal_expansion.py:56
parselib.transforms.literal_expansion.LiteralExpansion.htype
htype(self, tree)
Definition
literal_expansion.py:102
parselib.transforms.literal_expansion.LiteralExpansion.is_port_binding
bool is_port_binding
Definition
literal_expansion.py:15
parselib.transforms.literal_expansion.LiteralExpansion.htypeint
htypeint(self, tree)
Definition
literal_expansion.py:99
parselib.transforms.literal_expansion.LiteralExpansion.npa
npa(self, tree)
Definition
literal_expansion.py:126
parselib.transforms.literal_expansion.LiteralExpansion.idlit
idlit(self, tree)
Definition
literal_expansion.py:86
parselib.transforms.literal_expansion.LiteralExpansion.port_binding_module
port_binding_module
Definition
literal_expansion.py:16
parselib.transforms.literal_expansion.LiteralExpansion.numlitwidth
numlitwidth(self, tree)
Definition
literal_expansion.py:94
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