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
3from lark import Token
4
5from parselib.transforms import TopDown
6from ..utils import dprint, is_tree_type
7from ..grammar import UnexpectedHCodeStructureError
8
9
11 """Expands integer literal into int"""
12 def __init__(self, structure):
13 super().__init__()
14 self.structure = structure
17 self.field_access = None
18
19 def portbinding(self, tree):
21 self.port_binding_module = str(self.structure[self.current_module][tree.children[0]])
22 self.__push_up(tree)
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_bindingis_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
# if we are in port binding, we shall take the ids directly is_port_binding
__push_up(self, current_node)
Definition top_down.py:29