systemc-clang 2.0.0
Parsing SystemC constructs
Loading...
Searching...
No Matches
literal_expansion.py
Go to the documentation of this file.
1import warnings
2
3import lark
4from lark import Token
5
6from parselib.transforms import TopDown
7from ..utils import dprint, is_tree_type, get_ids_in_tree
8from ..grammar import UnexpectedHCodeStructureError
9
10
12 """Expands integer literal into int"""
13 def __init__(self, structure):
14 super().__init__()
15 self.structure = structure
16 self.is_port_binding = False
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
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
__push_up(self, current_node)
Definition top_down.py:29