systemc-clang 2.0.0
Parsing SystemC constructs
Loading...
Searching...
No Matches
sort_var_decl.py
Go to the documentation of this file.
1from parselib.transforms import TopDown
2
3
5 """This pass sorts the variable declaration in modules based on their reference dependencies.
6 The tools might require that a variable is declared before used.
7 """
8 def __top_sort_helper(self, u, edges, vis, res):
9 vis[u] = 1 # visiting
10 if u in edges:
11 forbid = list(filter(lambda x: vis[x] == 1, edges[u]))
12 assert len(forbid) == 0, 'Graph is not DAG'
13 nxt = list(filter(lambda x: vis[x] == 0, edges[u]))
14 for v in nxt:
15 self.__top_sort_helper(v, edges, vis, res)
16 vis[u] = 2 # done
17 res.append(u)
18
19 def __top_sort(self, edges):
20 """sort nodes topologically"""
21 in_deg = dict()
22 nodes = set()
23 for u, e in edges.items():
24 nodes = nodes.union(set(e))
25 nodes.add(u)
26 for n in nodes: in_deg.setdefault(n, 0)
27 for u, e in edges.items():
28 for v in e:
29 in_deg[v] += 1
30 init = list(filter(lambda x: in_deg[x] == 0, nodes))
31 vis = dict(map(lambda x: (x, 0), nodes))
32 res = []
33 assert init, 'Graph is not DAG'
34 for u in init:
35 if vis[u] == 0:
36 self.__top_sort_helper(u, edges, vis, res)
37 return res
38
39 def modportsiglist(self, tree):
40 # Detects dependency
41 var_dict = dict()
42 edge = dict()
43 for idx, ch in enumerate(tree.children):
44 if ch.data == 'vardeclinit':
45 var_name = ch.children[0]
46 var_dict[str(var_name)] = (idx, ch)
47 if len(ch.children) == 3:
48 dep = ch.children[2]
49 if dep.data == 'hvarref':
50 prev = dep.children[0]
51 edge.setdefault(var_name, list()).append(prev)
52
53 if len(edge) > 0:
54 sorted_noes = self.__top_sort(edge)
55 indices = sorted(list(map(lambda x: var_dict[x][0], sorted_noes)))
56 for idx, n in zip(indices, sorted_noes):
57 tree.children[idx] = var_dict[n][1]
58 return tree
__top_sort_helper(self, u, edges, vis, res)