1
+ from typing import Optional
2
+
1
3
# Base class for an instruction. To implement a basic instruction that doesn't
2
4
# impact the control-flow, create a new class inheriting from this.
3
5
class Instruction :
4
- _result : str
6
+ # Contains the name of the output register, if any.
7
+ _result : Optional [str ]
8
+ # Contains the instruction opcode.
9
+ _opcode : str
10
+ # Contains all the instruction operands, except result and opcode.
11
+ _operands : list [str ]
5
12
6
13
def __init__ (self , line : str ):
7
- # Contains the name of the output register, if any.
8
- self ._result : str = None
9
- # Contains the instruction opcode.
10
- self ._opcode : str = None
11
- # Contains all the instruction operands, except result and opcode.
12
- self ._operands : list [str ] = []
13
-
14
14
self .line = line
15
15
tokens = line .split ()
16
16
if len (tokens ) > 1 and tokens [1 ] == "=" :
@@ -38,7 +38,7 @@ def operands(self) -> list[str]:
38
38
# Returns the instruction output register. Calling this function is
39
39
# only allowed if has_output_register() is true.
40
40
def output_register (self ) -> str :
41
- assert self .has_output_register ()
41
+ assert self ._result is not None
42
42
return self ._result
43
43
44
44
# Returns true if this function has an output register. False otherwise.
@@ -132,20 +132,11 @@ def static_execution(self, lane):
132
132
class OpConstantComposite (OpConstant ):
133
133
def static_execution (self , lane ):
134
134
result = []
135
- length = self .get_register (self ._operands [0 ])
136
135
for op in self ._operands [1 :]:
137
- result .append (self .get_register (op ))
136
+ result .append (lane .get_register (op ))
138
137
lane .set_register (self ._result , result )
139
138
140
139
141
- class OpConstantComposite (OpConstant ):
142
- def static_execution (self , vm , state ):
143
- output = []
144
- for op in self ._operands [1 :]:
145
- output .append (state .get_register (op ))
146
- state .set_register (self ._result , output )
147
-
148
-
149
140
# Control flow instructions
150
141
class OpFunctionCall (Instruction ):
151
142
def _impl (self , module , lane ):
@@ -217,7 +208,7 @@ def _impl(self, module, lane):
217
208
218
209
219
210
# Convergence instructions
220
- class _MergeInstruction (Instruction ):
211
+ class MergeInstruction (Instruction ):
221
212
def merge_location (self ):
222
213
return self ._operands [0 ]
223
214
@@ -228,11 +219,11 @@ def _impl(self, module, lane):
228
219
lane .handle_convergence_header (self )
229
220
230
221
231
- class OpLoopMerge (_MergeInstruction ):
222
+ class OpLoopMerge (MergeInstruction ):
232
223
pass
233
224
234
225
235
- class OpSelectionMerge (_MergeInstruction ):
226
+ class OpSelectionMerge (MergeInstruction ):
236
227
pass
237
228
238
229
@@ -265,6 +256,13 @@ def _impl(self, module, lane):
265
256
output .append (lane .get_register (op ))
266
257
lane .set_register (self ._result , output )
267
258
259
+ class OpCompositeExtract (Instruction ):
260
+ def _impl (self , module , lane ):
261
+ value = lane .get_register (self ._operands [1 ])
262
+ output = value
263
+ for op in self ._operands [2 :]:
264
+ output = output [int (op )]
265
+ lane .set_register (self ._result , output )
268
266
269
267
class OpStore (Instruction ):
270
268
def _impl (self , module , lane ):
@@ -303,13 +301,6 @@ def _impl(self, module, lane):
303
301
lane .set_register (self ._result , not LHS )
304
302
305
303
306
- class OpSGreaterThan (Instruction ):
307
- def _impl (self , module , lane ):
308
- LHS = lane .get_register (self ._operands [1 ])
309
- RHS = lane .get_register (self ._operands [2 ])
310
- lane .set_register (self ._result , LHS > RHS )
311
-
312
-
313
304
class _LessThan (Instruction ):
314
305
def _impl (self , module , lane ):
315
306
LHS = lane .get_register (self ._operands [1 ])
0 commit comments