annotate python/ir/instruction.py @ 173:c1d2b6b9f9a7

Rework into passes
author Windel Bouwman
date Fri, 19 Apr 2013 12:42:21 +0200
parents 3eb9b9e2958d
children 3eb06f5fb987
rev   line source
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
1 from .basicblock import BasicBlock
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
2
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
3 class Value:
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
4 """ Temporary SSA value (value that is assigned only once! """
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
5 def __init__(self, name):
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
6 # TODO: add typing? for now only handle integers
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
7 self.name = name
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
8 self.interferes = set()
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
9 self.reg = None
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
10 def __repr__(self):
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
11 if self.reg:
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
12 n = self.reg
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
13 else:
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
14 n = self.name
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
15 return '{0}'.format(n)
110
9e552d34bd60 Work on compiler
Windel Bouwman
parents: 104
diff changeset
16
155
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 147
diff changeset
17 class Instruction:
104
ed230e947dc6 Added hexviewer
windel
parents: 99
diff changeset
18 """ Base class for all instructions. """
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
19 def __init__(self):
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
20 # live variables at this node:
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
21 self.live_in = set()
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
22 self.live_out = set()
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
23 # What variables this instruction uses and defines:
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
24 self.defs = set()
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
25 self.uses = set()
173
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents: 171
diff changeset
26 def getParent(self):
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents: 171
diff changeset
27 return self.parent
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents: 171
diff changeset
28 Parent = property(getParent)
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents: 171
diff changeset
29 @property
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents: 171
diff changeset
30 def Targets(self):
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents: 171
diff changeset
31 return self.getTargets()
170
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 160
diff changeset
32
158
9683a4cd848f Added some functions for code generation
Windel Bouwman
parents: 157
diff changeset
33 # Function calling:
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
34 class Call(Instruction):
110
9e552d34bd60 Work on compiler
Windel Bouwman
parents: 104
diff changeset
35 def __init__(self, callee, arguments):
9e552d34bd60 Work on compiler
Windel Bouwman
parents: 104
diff changeset
36 super().__init__()
9e552d34bd60 Work on compiler
Windel Bouwman
parents: 104
diff changeset
37 self.callee = callee
9e552d34bd60 Work on compiler
Windel Bouwman
parents: 104
diff changeset
38 self.arguments = arguments
158
9683a4cd848f Added some functions for code generation
Windel Bouwman
parents: 157
diff changeset
39 def __repr__(self):
9683a4cd848f Added some functions for code generation
Windel Bouwman
parents: 157
diff changeset
40 return 'CALL {0}'.format(self.callee)
110
9e552d34bd60 Work on compiler
Windel Bouwman
parents: 104
diff changeset
41
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
42 class Return(Instruction):
158
9683a4cd848f Added some functions for code generation
Windel Bouwman
parents: 157
diff changeset
43 def __repr__(self):
9683a4cd848f Added some functions for code generation
Windel Bouwman
parents: 157
diff changeset
44 return 'RET'
173
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents: 171
diff changeset
45 def getTargets(self):
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents: 171
diff changeset
46 return []
70
35286e8abd03 Added some llvm classes
windel
parents:
diff changeset
47
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
48 class ImmLoad(Instruction):
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
49 def __init__(self, target, value):
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
50 super().__init__()
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
51 self.target = target
170
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 160
diff changeset
52 self.value = value
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
53 self.defs.add(target)
170
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 160
diff changeset
54 def __repr__(self):
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
55 return '{0} = {1}'.format(self.target, self.value)
170
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 160
diff changeset
56
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
57 # Data operations
70
35286e8abd03 Added some llvm classes
windel
parents:
diff changeset
58 class BinaryOperator(Instruction):
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
59 def __init__(self, result, operation, value1, value2):
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
60 super().__init__()
157
8f3924b6076e Added some code generator things
Windel Bouwman
parents: 155
diff changeset
61 #print('operation is in binops:', operation in BinOps)
104
ed230e947dc6 Added hexviewer
windel
parents: 99
diff changeset
62 # Check types of the two operands:
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
63 self.result = result
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
64 self.defs.add(result)
104
ed230e947dc6 Added hexviewer
windel
parents: 99
diff changeset
65 self.value1 = value1
ed230e947dc6 Added hexviewer
windel
parents: 99
diff changeset
66 self.value2 = value2
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
67 self.uses.add(value1)
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
68 self.uses.add(value2)
104
ed230e947dc6 Added hexviewer
windel
parents: 99
diff changeset
69 self.operation = operation
158
9683a4cd848f Added some functions for code generation
Windel Bouwman
parents: 157
diff changeset
70 def __repr__(self):
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
71 return '{0} = {2} {1} {3}'.format(self.result, self.operation, self.value1, self.value2)
70
35286e8abd03 Added some llvm classes
windel
parents:
diff changeset
72
170
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 160
diff changeset
73 # Memory functions:
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
74 class Load(Instruction):
158
9683a4cd848f Added some functions for code generation
Windel Bouwman
parents: 157
diff changeset
75 def __init__(self, name, value):
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
76 super().__init__()
157
8f3924b6076e Added some code generator things
Windel Bouwman
parents: 155
diff changeset
77 self.value = value
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
78 self.defs.add(value)
158
9683a4cd848f Added some functions for code generation
Windel Bouwman
parents: 157
diff changeset
79 self.name = name
9683a4cd848f Added some functions for code generation
Windel Bouwman
parents: 157
diff changeset
80 def __repr__(self):
170
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 160
diff changeset
81 return '{1} = [{0}]'.format(self.name, self.value)
157
8f3924b6076e Added some code generator things
Windel Bouwman
parents: 155
diff changeset
82
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
83 class Store(Instruction):
160
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents: 158
diff changeset
84 def __init__(self, name, value):
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
85 super().__init__()
160
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents: 158
diff changeset
86 self.name = name
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents: 158
diff changeset
87 self.value = value
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
88 self.uses.add(value)
160
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents: 158
diff changeset
89 def __repr__(self):
170
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 160
diff changeset
90 return '[{0}] = {1}'.format(self.name, self.value)
160
10330be89bc2 Started from scratch with code edit
Windel Bouwman
parents: 158
diff changeset
91
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
92 # Branching:
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
93 class Branch(Instruction):
170
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 160
diff changeset
94 def __init__(self, target):
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
95 super().__init__()
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
96 assert type(target) is BasicBlock
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
97 self.target = target
158
9683a4cd848f Added some functions for code generation
Windel Bouwman
parents: 157
diff changeset
98 def __repr__(self):
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
99 return 'BRANCH {0}'.format(self.target)
173
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents: 171
diff changeset
100 def getTargets(self):
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents: 171
diff changeset
101 return [self.target]
170
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 160
diff changeset
102
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
103 class ConditionalBranch(Instruction):
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
104 def __init__(self, a, cond, b, lab1, lab2):
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
105 super().__init__()
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
106 self.a = a
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
107 assert type(a) is Value
170
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 160
diff changeset
108 self.cond = cond
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
109 self.b = b
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
110 self.uses.add(a)
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
111 self.uses.add(b)
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
112 assert type(b) is Value
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
113 assert type(lab1) is BasicBlock
170
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 160
diff changeset
114 self.lab1 = lab1
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
115 assert type(lab2) is BasicBlock
170
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 160
diff changeset
116 self.lab2 = lab2
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 160
diff changeset
117 def __repr__(self):
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
118 return 'IF {0} {1} {2} THEN {3} ELSE {4}'.format(self.a, self.cond, self.b, self.lab1, self.lab2)
173
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents: 171
diff changeset
119 def getTargets(self):
c1d2b6b9f9a7 Rework into passes
Windel Bouwman
parents: 171
diff changeset
120 return [self.lab1, self.lab2]
170
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 160
diff changeset
121
171
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
122 class PhiNode(Instruction):
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
123 def __init__(self):
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
124 super().__init__()
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
125 self.incBB = []
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
126 def addIncoming(self, bb):
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
127 self.incBB.append(bb)
3eb9b9e2958d Improved IR code
Windel Bouwman
parents: 170
diff changeset
128