Mercurial > lcfOS
comparison python/dag.py @ 191:6b2bec5653f1
Added assembler testset
author | Windel Bouwman |
---|---|
date | Sun, 26 May 2013 15:28:07 +0200 |
parents | |
children | bd26dc13f270 |
comparison
equal
deleted
inserted
replaced
190:65dda7e7e8bd | 191:6b2bec5653f1 |
---|---|
1 | |
2 # Instruction selection with DAG (Directed Acyclic Graph) | |
3 class DagLeaf: | |
4 def __init__(self, v): | |
5 self.v = v | |
6 | |
7 class DagNode: | |
8 def __init__(self, name): | |
9 self.name = name | |
10 self.children = [] | |
11 def __repr__(self): | |
12 return str(self.name) | |
13 | |
14 class Dag: | |
15 def __init__(self, bb): | |
16 self.mapping = {} | |
17 self.buildFromBB(bb) | |
18 def buildFromBB(self, bb): | |
19 for ins in bb.Instructions: | |
20 if type(ins) is ir.BinaryOperator: | |
21 if not ins.value1 in self.mapping: | |
22 self.mapping[ins.value1] = DagNode(ins.value1) | |
23 if not ins.value2 in self.mapping: | |
24 self.mapping[ins.value2] = DagNode(ins.value2) | |
25 # look for op with left and right operand the same: | |
26 N = None | |
27 lnode = self.mapping[ins.value1] | |
28 rnode = self.mapping[ins.value2] | |
29 for node in self.mapping.values(): | |
30 if node.name == ins.operation: | |
31 if node.children[0] == lnode and node.children[1] == rnode: | |
32 N = node | |
33 break | |
34 if not N: | |
35 # Create a node. | |
36 N = DagNode(ins.operation) | |
37 N.children.append(lnode) | |
38 N.children.append(rnode) | |
39 self.mapping[ins.result] = N | |
40 else: | |
41 pass | |
42 def dumpgv(self, outf): | |
43 outf.write('subgraph {0} {{\n'.format(id(self))) | |
44 for node in self.mapping.values(): | |
45 outf.write('{0} [label="{1}"];\n'.format(id(node), node.name)) | |
46 for c in node.children: | |
47 outf.write('{0} -> {1};\n'.format(id(node), id(c))) | |
48 outf.write('label="dag"}\n') | |
49 | |
50 def insSelect(mod): | |
51 """ Create DAG from ir-code """ | |
52 for bb in mod.BasicBlocks: | |
53 print(bb) | |
54 dag = Dag(bb) | |
55 print(dag.mapping) | |
56 bb.dag = dag | |
57 |