Mercurial > lcfOS
comparison python/ir/instruction.py @ 230:88a1e0baef65
Added some tests for IR-code
author | Windel Bouwman |
---|---|
date | Sat, 13 Jul 2013 19:53:44 +0200 |
parents | c3f1ce8b638f |
children | 63bb40758066 |
comparison
equal
deleted
inserted
replaced
229:51d5ed1bd503 | 230:88a1e0baef65 |
---|---|
1 from .basicblock import BasicBlock | 1 from .basicblock import BasicBlock |
2 from .function import Function | 2 from .function import Function |
3 | 3 |
4 | 4 |
5 class Value: | 5 class Value: |
6 """ Temporary SSA value (value that is assigned only once! """ | 6 """ Temporary SSA value (value that is assigned only once! """ |
7 def __init__(self, name): | 7 def __init__(self, name): |
8 # TODO: add typing? for now only handle integers | 8 # TODO: add typing? for now only handle integers |
9 self.name = name | 9 self.name = name |
10 self.used_by = [] | 10 self.used_by = [] |
11 def __repr__(self): | 11 |
12 return '{0}'.format(self.name) # + str(self.IsUsed) | 12 def __repr__(self): |
13 @property | 13 return '{0}'.format(self.name) # + str(self.IsUsed) |
14 def IsUsed(self): | 14 |
15 @property | |
16 def IsUsed(self): | |
15 return len(self.used_by) > 0 | 17 return len(self.used_by) > 0 |
16 | 18 |
17 class Variable(Value): | 19 class Variable(Value): |
18 pass | 20 pass |
19 | 21 |
84 self.value = value | 86 self.value = value |
85 if value: | 87 if value: |
86 self.addUse(value) | 88 self.addUse(value) |
87 def __repr__(self): | 89 def __repr__(self): |
88 if self.value: | 90 if self.value: |
89 return 'Return {0}'.format(self.value) | 91 return 'ret {0}'.format(self.value) |
90 else: | 92 else: |
91 return 'Return' | 93 return 'ret' |
92 def getTargets(self): | 94 def getTargets(self): |
93 return [] | 95 return [] |
94 | 96 |
95 class Alloc(Instruction): | 97 class Alloc(Instruction): |
96 """ Allocates space on the stack """ | 98 """ Allocates space on the stack """ |
112 def __repr__(self): | 114 def __repr__(self): |
113 return '{0} = {1}'.format(self.target, self.value) | 115 return '{0} = {1}'.format(self.target, self.value) |
114 | 116 |
115 # Data operations | 117 # Data operations |
116 class BinaryOperator(Instruction): | 118 class BinaryOperator(Instruction): |
117 def __init__(self, result, operation, value1, value2): | 119 def __init__(self, result, operation, value1, value2): |
118 super().__init__() | 120 super().__init__() |
119 #print('operation is in binops:', operation in BinOps) | 121 #print('operation is in binops:', operation in BinOps) |
120 # Check types of the two operands: | 122 # Check types of the two operands: |
121 assert type(value1) is Value | 123 assert type(value1) is Value, str(value1) + str(type(value1)) |
122 assert type(value2) is Value | 124 assert type(value2) is Value, value2 |
123 self.result = result | 125 self.result = result |
124 self.addDef(result) | 126 self.addDef(result) |
125 self.value1 = value1 | 127 self.value1 = value1 |
126 self.value2 = value2 | 128 self.value2 = value2 |
127 self.addUse(value1) | 129 self.addUse(value1) |
128 self.addUse(value2) | 130 self.addUse(value2) |
129 self.operation = operation | 131 self.operation = operation |
130 def __repr__(self): | 132 def __repr__(self): |
131 return '{0} = {2} {1} {3}'.format(self.result, self.operation, self.value1, self.value2) | 133 a, b = self.value1, self.value2 |
134 return '{} = {} {} {}'.format(self.result, a, self.operation, b) | |
132 | 135 |
133 # Memory functions: | 136 # Memory functions: |
134 class Load(Instruction): | 137 class Load(Instruction): |
135 def __init__(self, location, value): | 138 def __init__(self, location, value): |
136 super().__init__() | 139 super().__init__() |
139 self.value = value | 142 self.value = value |
140 self.addDef(value) | 143 self.addDef(value) |
141 self.location = location | 144 self.location = location |
142 self.addUse(self.location) | 145 self.addUse(self.location) |
143 def __repr__(self): | 146 def __repr__(self): |
144 return '{} <= [{}]'.format(self.value, self.location) | 147 return '{} = [{}]'.format(self.value, self.location) |
145 | 148 |
146 class Store(Instruction): | 149 class Store(Instruction): |
147 def __init__(self, location, value): | 150 def __init__(self, location, value): |
148 super().__init__() | 151 super().__init__() |
149 assert type(value) is Value | 152 assert type(value) is Value, value |
150 assert isinstance(location, Value), "Location must be a value" | 153 assert isinstance(location, Value), "Location must be a value" |
151 self.location = location | 154 self.location = location |
152 self.value = value | 155 self.value = value |
153 self.addUse(value) | 156 self.addUse(value) |
154 self.addUse(location) | 157 self.addUse(location) |
155 def __repr__(self): | 158 def __repr__(self): |
156 return '[{}] <= {}'.format(self.location, self.value) | 159 return '[{}] = {}'.format(self.location, self.value) |
157 | 160 |
158 # Branching: | 161 # Branching: |
159 class Branch(Terminator): | 162 class Branch(Terminator): |
160 def __init__(self, target): | 163 def __init__(self, target): |
161 super().__init__() | 164 super().__init__() |