systemc-clang 2.0.0
Parsing SystemC constructs
Loading...
Searching...
No Matches
slice_merge.py
Go to the documentation of this file.
1from lark import Tree, Token
2
3from parselib.transforms import TopDown
4from parselib.utils import dprint
5
6
8 """recognize slice patterns and convert slicing into synthesizable index part select"""
9 def __init__(self):
10 super().__init__()
11
12 def __to_seq(self, tree):
13 return list(map(lambda x: x.children[0], tree.iter_subtrees_topdown()))
14
15 def __is_literal_node(self, t):
16 return isinstance(t, Tree) and t.data == 'hliteral'
17
19 """check if the left hand side is width * (ID + 1) - 1
20 returns result, width, ID
21 """
22 failed = False, None, None
23 if not isinstance(tr, Tree) or len(tr.children) != 3:
24 return failed
25 is_offset_1 = tr.children[0] == '-' and isinstance(tr.children[2], Tree) and tr.children[2].children[0] == 1
26 if not is_offset_1:
27 return failed
28 mult_part = tr.children[1]
29 if not isinstance(mult_part, Tree) or mult_part.data != 'hbinop' or mult_part.children[0] != '*':
30 return failed
31 op0, op1 = mult_part.children[1:]
32 if self.__is_literal_node(op0):
33 width = op0.children[0]
34 idx = op1
35 elif self.__is_literal_node(op1):
36 width = op1.children[0]
37 idx = op0
38 else:
39 return failed
40 if not isinstance(idx, Tree) or idx.data != 'hbinop' or idx.children[0] != '+':
41 return failed
42 op0, op1 = idx.children[1:]
43 if self.__is_literal_node(op0) and op0.children[0] == 1 and len(op1.children) == 1 and op1.data == 'hvarref':
44 id = op1.children[0]
45 return True, width, id
46 elif self.__is_literal_node(op1) and op1.children[0] == 1 and len(op0.children) == 1 and op0.data == 'hvarref':
47 id = op0.children[0]
48 return True, width, id
49 else:
50 return failed
51
52
54 """check if the right hand-side is width * ID
55 returns result, width, ID
56 """
57 lst = self.__to_seq(tr)
58 is_mult = lst[0] == '*'
59 if not is_mult or len(lst) > 3:
60 # is of the form a * b or too complex
61 return False, None, None
62 p1, p2 = lst[1:]
63
64 if isinstance(p1, int) and isinstance(p2, Token):
65 return True, p1, p2
66 elif isinstance(p2, int) and isinstance(p1, Token):
67 return True, p2, p1
68 else:
69 return False, None, None
70
71 def harrayref(self, tree):
72 if tree.children[0].data == 'hslice':
73 self.__push_up(tree)
74 if isinstance(tree.children[0], tuple):
75 tree.children = tree.children[0]
76 return tree
77 else:
78 return tree
79
80 def hslice(self, tree):
81 self.__push_up(tree)
82 if len(tree.children) == 3:
83 var_name, l, r = tree.children
84 lOk, lWidth, lID = self.__check_part_select_left(l)
85 rOk, rWidth, rID = self.__check_part_select_right(r)
86 if lOk and rOk and lWidth == rWidth and lID == rID:
87 mult = Tree(data='hbinop', children=['*', lID, lWidth])
88 index_part = Tree(data='hbinop', children=['+:', mult, lWidth])
89 return var_name, index_part
90 else:
91 return tree
92 elif len(tree.children) == 2:
93 return tree
__push_up(self, current_node)
Definition top_down.py:29