comparison python/ir/instruction.py @ 174:3eb06f5fb987

Added memory alloc for locals
author Windel Bouwman
date Fri, 19 Apr 2013 19:22:52 +0200
parents c1d2b6b9f9a7
children a51b3c956386
comparison
equal deleted inserted replaced
173:c1d2b6b9f9a7 174:3eb06f5fb987
5 def __init__(self, name): 5 def __init__(self, name):
6 # TODO: add typing? for now only handle integers 6 # TODO: add typing? for now only handle integers
7 self.name = name 7 self.name = name
8 self.interferes = set() 8 self.interferes = set()
9 self.reg = None 9 self.reg = None
10 self.used_by = []
10 def __repr__(self): 11 def __repr__(self):
11 if self.reg: 12 if self.reg:
12 n = self.reg 13 n = self.reg
13 else: 14 else:
14 n = self.name 15 n = self.name
15 return '{0}'.format(n) 16 return '{0}'.format(n) # + str(self.IsUsed)
17 @property
18 def IsUsed(self):
19 return len(self.used_by) > 0
20
21 class Use:
22 def __init__(self, user, val):
23 self.user = user
24 self.val = val
25 self.val.used_by.append(self.user)
26 def delete(self):
27 self.val.used_by.remove(self.user)
16 28
17 class Instruction: 29 class Instruction:
18 """ Base class for all instructions. """ 30 """ Base class for all instructions. """
19 def __init__(self): 31 def __init__(self):
20 # live variables at this node: 32 # live variables at this node:
21 self.live_in = set() 33 self.live_in = set()
22 self.live_out = set() 34 self.live_out = set()
23 # What variables this instruction uses and defines: 35 # What variables this instruction uses and defines:
24 self.defs = set() 36 self.defs = []
25 self.uses = set() 37 self.uses = []
38 def delete(self):
39 for use in self.uses:
40 use.delete()
41 self.uses.clear()
42 def addUse(self, val):
43 self.uses.append(Use(self, val))
44 def addDef(self, v):
45 self.defs.append(v)
26 def getParent(self): 46 def getParent(self):
27 return self.parent 47 return self.parent
28 Parent = property(getParent) 48 def setParent(self, p):
49 self.parent = p
50 Parent = property(getParent, setParent)
29 @property 51 @property
30 def Targets(self): 52 def Targets(self):
31 return self.getTargets() 53 return self.getTargets()
32 54
33 # Function calling: 55 # Function calling:
38 self.arguments = arguments 60 self.arguments = arguments
39 def __repr__(self): 61 def __repr__(self):
40 return 'CALL {0}'.format(self.callee) 62 return 'CALL {0}'.format(self.callee)
41 63
42 class Return(Instruction): 64 class Return(Instruction):
65 def __init__(self, value=None):
66 super().__init__()
67 self.value = value
68 if value:
69 self.addUse(value)
43 def __repr__(self): 70 def __repr__(self):
44 return 'RET' 71 if self.value:
72 return 'Return {0}'.format(self.value)
73 else:
74 return 'Return'
45 def getTargets(self): 75 def getTargets(self):
46 return [] 76 return []
77
78 class Alloc(Instruction):
79 """ Allocates space on the stack """
80 def __init__(self, value):
81 super().__init__()
82 self.value = value
83 self.addDef(value)
84 def __repr__(self):
85 return '{0} = alloc'.format(self.value)
47 86
48 class ImmLoad(Instruction): 87 class ImmLoad(Instruction):
49 def __init__(self, target, value): 88 def __init__(self, target, value):
50 super().__init__() 89 super().__init__()
51 self.target = target 90 self.target = target
52 self.value = value 91 self.value = value
53 self.defs.add(target) 92 self.addDef(target)
54 def __repr__(self): 93 def __repr__(self):
55 return '{0} = {1}'.format(self.target, self.value) 94 return '{0} = {1}'.format(self.target, self.value)
56 95
57 # Data operations 96 # Data operations
58 class BinaryOperator(Instruction): 97 class BinaryOperator(Instruction):
59 def __init__(self, result, operation, value1, value2): 98 def __init__(self, result, operation, value1, value2):
60 super().__init__() 99 super().__init__()
61 #print('operation is in binops:', operation in BinOps) 100 #print('operation is in binops:', operation in BinOps)
62 # Check types of the two operands: 101 # Check types of the two operands:
63 self.result = result 102 self.result = result
64 self.defs.add(result) 103 self.addDef(result)
65 self.value1 = value1 104 self.value1 = value1
66 self.value2 = value2 105 self.value2 = value2
67 self.uses.add(value1) 106 self.addUse(value1)
68 self.uses.add(value2) 107 self.addUse(value2)
69 self.operation = operation 108 self.operation = operation
70 def __repr__(self): 109 def __repr__(self):
71 return '{0} = {2} {1} {3}'.format(self.result, self.operation, self.value1, self.value2) 110 return '{0} = {2} {1} {3}'.format(self.result, self.operation, self.value1, self.value2)
72 111
73 # Memory functions: 112 # Memory functions:
74 class Load(Instruction): 113 class Load(Instruction):
75 def __init__(self, name, value): 114 def __init__(self, location, value):
76 super().__init__() 115 super().__init__()
116 assert type(value) is Value
117 assert type(location) is Value, "Location must be a value"
77 self.value = value 118 self.value = value
78 self.defs.add(value) 119 self.addDef(value)
79 self.name = name 120 self.location = location
121 self.addUse(self.location)
80 def __repr__(self): 122 def __repr__(self):
81 return '{1} = [{0}]'.format(self.name, self.value) 123 return '{1} = [{0}]'.format(self.location, self.value)
82 124
83 class Store(Instruction): 125 class Store(Instruction):
84 def __init__(self, name, value): 126 def __init__(self, location, value):
85 super().__init__() 127 super().__init__()
86 self.name = name 128 assert type(value) is Value
129 assert type(location) is Value, "Location must be a value"
130 self.location = location
87 self.value = value 131 self.value = value
88 self.uses.add(value) 132 self.addUse(value)
89 def __repr__(self): 133 def __repr__(self):
90 return '[{0}] = {1}'.format(self.name, self.value) 134 return '[{0}] = {1}'.format(self.location, self.value)
91 135
92 # Branching: 136 # Branching:
93 class Branch(Instruction): 137 class Branch(Instruction):
94 def __init__(self, target): 138 def __init__(self, target):
95 super().__init__() 139 super().__init__()
105 super().__init__() 149 super().__init__()
106 self.a = a 150 self.a = a
107 assert type(a) is Value 151 assert type(a) is Value
108 self.cond = cond 152 self.cond = cond
109 self.b = b 153 self.b = b
110 self.uses.add(a) 154 self.addUse(a)
111 self.uses.add(b) 155 self.addUse(b)
112 assert type(b) is Value 156 assert type(b) is Value
113 assert type(lab1) is BasicBlock 157 assert type(lab1) is BasicBlock
114 self.lab1 = lab1 158 self.lab1 = lab1
115 assert type(lab2) is BasicBlock 159 assert type(lab2) is BasicBlock
116 self.lab2 = lab2 160 self.lab2 = lab2