comparison python/ir/instruction.py @ 170:4348da5ca307

Cleanup of ir dir
author Windel Bouwman
date Fri, 29 Mar 2013 17:33:17 +0100
parents 10330be89bc2
children 3eb9b9e2958d
comparison
equal deleted inserted replaced
169:ee0d30533dae 170:4348da5ca307
1 1
2 class Instruction: 2 class Instruction:
3 """ Base class for all instructions. """ 3 """ Base class for all instructions. """
4 pass 4 pass
5
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)
5 12
6 # Function calling: 13 # Function calling:
7 class CallInstruction(Instruction): 14 class CallInstruction(Instruction):
8 def __init__(self, callee, arguments): 15 def __init__(self, callee, arguments):
9 super().__init__() 16 super().__init__()
14 21
15 class RetInstruction(Instruction): 22 class RetInstruction(Instruction):
16 def __repr__(self): 23 def __repr__(self):
17 return 'RET' 24 return 'RET'
18 25
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
19 class BinaryOperator(Instruction): 33 class BinaryOperator(Instruction):
20 def __init__(self, name, operation, value1, value2): 34 def __init__(self, name, operation, value1, value2):
21 assert value1
22 assert value2
23 #print('operation is in binops:', operation in BinOps) 35 #print('operation is in binops:', operation in BinOps)
24 # Check types of the two operands: 36 # Check types of the two operands:
25 self.name = name 37 self.name = name
26 self.value1 = value1 38 self.value1 = value1
27 self.value2 = value2 39 self.value2 = value2
28 self.operation = operation 40 self.operation = operation
29 def __repr__(self): 41 def __repr__(self):
30 return '{0} = {1} {2}, {3}'.format(self.name, self.operation, self.value1, self.value2) 42 return '{0} = {2} {1} {3}'.format(self.name, self.operation, self.value1, self.value2)
31 43
44 # Memory functions:
32 class LoadInstruction(Instruction): 45 class LoadInstruction(Instruction):
33 def __init__(self, name, value): 46 def __init__(self, name, value):
34 self.value = value 47 self.value = value
35 self.name = name 48 self.name = name
36 def __repr__(self): 49 def __repr__(self):
37 return 'load {0} = {1}'.format(self.name, self.value) 50 return '{1} = [{0}]'.format(self.name, self.value)
38 51
39 class StoreInstruction(Instruction): 52 class StoreInstruction(Instruction):
40 def __init__(self, name, value): 53 def __init__(self, name, value):
41 self.name = name 54 self.name = name
42 self.value = value 55 self.value = value
43 def __repr__(self): 56 def __repr__(self):
44 return 'store {0}'.format(self.name) 57 return '[{0}] = {1}'.format(self.name, self.value)
45 58
46 class BranchInstruction(Instruction): 59 class BranchInstruction(Instruction):
47 def __init__(self, t1, t2): 60 def __init__(self, target):
48 self.t1 = t1 61 self.t1 = target
49 self.t2 = t2
50 def __repr__(self): 62 def __repr__(self):
51 return 'BRANCH {0}'.format(self.t1) 63 return 'BRANCH {0}'.format(self.t1)
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