comparison python/ir/instruction.py @ 158:9683a4cd848f

Added some functions for code generation
author Windel Bouwman
date Fri, 08 Mar 2013 16:52:44 +0100
parents 8f3924b6076e
children 10330be89bc2
comparison
equal deleted inserted replaced
157:8f3924b6076e 158:9683a4cd848f
1
2 def Enum(**enums):
3 return type('Enum', (), enums)
4 1
5 class Instruction: 2 class Instruction:
6 """ Base class for all instructions. """ 3 """ Base class for all instructions. """
7 pass 4 pass
8 5
6 # Function calling:
9 class CallInstruction(Instruction): 7 class CallInstruction(Instruction):
10 def __init__(self, callee, arguments): 8 def __init__(self, callee, arguments):
11 super().__init__() 9 super().__init__()
12 self.callee = callee 10 self.callee = callee
13 self.arguments = arguments 11 self.arguments = arguments
12 def __repr__(self):
13 return 'CALL {0}'.format(self.callee)
14 14
15 BinOps = Enum(Add=1, Sub=2, Mul=3) 15 class RetInstruction(Instruction):
16 def __repr__(self):
17 return 'RET'
16 18
17 class BinaryOperator(Instruction): 19 class BinaryOperator(Instruction):
18 def __init__(self, operation, value1, value2): 20 def __init__(self, name, operation, value1, value2):
19 assert value1 21 assert value1
20 assert value2 22 assert value2
21 #print('operation is in binops:', operation in BinOps) 23 #print('operation is in binops:', operation in BinOps)
22 # Check types of the two operands: 24 # Check types of the two operands:
25 self.name = name
23 self.value1 = value1 26 self.value1 = value1
24 self.value2 = value2 27 self.value2 = value2
25 self.operation = operation 28 self.operation = operation
29 def __repr__(self):
30 return '{0} = {1} {2}, {3}'.format(self.name, self.operation, self.value1, self.value2)
26 31
27 class AddInstruction(BinaryOperator): 32 class LoadInstruction(Instruction):
28 def __init__(self, a, b): 33 def __init__(self, name, value):
29 super().__init__('add', a, b) 34 self.value = value
35 self.name = name
36 def __repr__(self):
37 return 'load {0} = {1}'.format(self.name, self.value)
30 38
31 class ImmLoadInstruction(Instruction): 39 class BranchInstruction(Instruction):
32 def __init__(self, value): 40 def __init__(self, t1, t2):
33 self.value = value 41 self.t1 = t1
34 42 self.t2 = t2
43 def __repr__(self):
44 return 'BRANCH {0}'.format(self.t1)