110
|
1
|
155
|
2 class Instruction:
|
104
|
3 """ Base class for all instructions. """
|
70
|
4 pass
|
|
5
|
170
|
6 # Label:
|
|
7 class LabelInstruction(Instruction):
|
|
8 def __init__(self, labid):
|
|
9 self.labid = labid
|
|
10 def __repr__(self):
|
|
11 return '{0}:'.format(self.labid)
|
|
12
|
158
|
13 # Function calling:
|
70
|
14 class CallInstruction(Instruction):
|
110
|
15 def __init__(self, callee, arguments):
|
|
16 super().__init__()
|
|
17 self.callee = callee
|
|
18 self.arguments = arguments
|
158
|
19 def __repr__(self):
|
|
20 return 'CALL {0}'.format(self.callee)
|
110
|
21
|
158
|
22 class RetInstruction(Instruction):
|
|
23 def __repr__(self):
|
|
24 return 'RET'
|
70
|
25
|
170
|
26 class MoveInstruction(Instruction):
|
|
27 def __init__(self, name, value):
|
|
28 self.name = name
|
|
29 self.value = value
|
|
30 def __repr__(self):
|
|
31 return '{0} = {1}'.format(self.name, self.value)
|
|
32
|
70
|
33 class BinaryOperator(Instruction):
|
158
|
34 def __init__(self, name, operation, value1, value2):
|
157
|
35 #print('operation is in binops:', operation in BinOps)
|
104
|
36 # Check types of the two operands:
|
158
|
37 self.name = name
|
104
|
38 self.value1 = value1
|
|
39 self.value2 = value2
|
|
40 self.operation = operation
|
158
|
41 def __repr__(self):
|
170
|
42 return '{0} = {2} {1} {3}'.format(self.name, self.operation, self.value1, self.value2)
|
70
|
43
|
170
|
44 # Memory functions:
|
158
|
45 class LoadInstruction(Instruction):
|
|
46 def __init__(self, name, value):
|
157
|
47 self.value = value
|
158
|
48 self.name = name
|
|
49 def __repr__(self):
|
170
|
50 return '{1} = [{0}]'.format(self.name, self.value)
|
157
|
51
|
160
|
52 class StoreInstruction(Instruction):
|
|
53 def __init__(self, name, value):
|
|
54 self.name = name
|
|
55 self.value = value
|
|
56 def __repr__(self):
|
170
|
57 return '[{0}] = {1}'.format(self.name, self.value)
|
160
|
58
|
158
|
59 class BranchInstruction(Instruction):
|
170
|
60 def __init__(self, target):
|
|
61 self.t1 = target
|
158
|
62 def __repr__(self):
|
|
63 return 'BRANCH {0}'.format(self.t1)
|
170
|
64
|
|
65 class IfInstruction(Instruction):
|
|
66 def __init__(self, cond, lab1, lab2):
|
|
67 self.cond = cond
|
|
68 self.lab1 = lab1
|
|
69 self.lab2 = lab2
|
|
70 def __repr__(self):
|
|
71 return 'IF {0} THEN {1} ELSE {2}'.format(self.cond, self.lab1, self.lab2)
|
|
72
|