Mercurial > lcfOS
diff 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 |
line wrap: on
line diff
--- a/python/ir/instruction.py Fri Apr 19 12:42:21 2013 +0200 +++ b/python/ir/instruction.py Fri Apr 19 19:22:52 2013 +0200 @@ -7,12 +7,24 @@ self.name = name self.interferes = set() self.reg = None + self.used_by = [] def __repr__(self): if self.reg: n = self.reg else: n = self.name - return '{0}'.format(n) + return '{0}'.format(n) # + str(self.IsUsed) + @property + def IsUsed(self): + return len(self.used_by) > 0 + +class Use: + def __init__(self, user, val): + self.user = user + self.val = val + self.val.used_by.append(self.user) + def delete(self): + self.val.used_by.remove(self.user) class Instruction: """ Base class for all instructions. """ @@ -21,11 +33,21 @@ self.live_in = set() self.live_out = set() # What variables this instruction uses and defines: - self.defs = set() - self.uses = set() + self.defs = [] + self.uses = [] + def delete(self): + for use in self.uses: + use.delete() + self.uses.clear() + def addUse(self, val): + self.uses.append(Use(self, val)) + def addDef(self, v): + self.defs.append(v) def getParent(self): return self.parent - Parent = property(getParent) + def setParent(self, p): + self.parent = p + Parent = property(getParent, setParent) @property def Targets(self): return self.getTargets() @@ -40,17 +62,34 @@ return 'CALL {0}'.format(self.callee) class Return(Instruction): + def __init__(self, value=None): + super().__init__() + self.value = value + if value: + self.addUse(value) def __repr__(self): - return 'RET' + if self.value: + return 'Return {0}'.format(self.value) + else: + return 'Return' def getTargets(self): return [] +class Alloc(Instruction): + """ Allocates space on the stack """ + def __init__(self, value): + super().__init__() + self.value = value + self.addDef(value) + def __repr__(self): + return '{0} = alloc'.format(self.value) + class ImmLoad(Instruction): def __init__(self, target, value): super().__init__() self.target = target self.value = value - self.defs.add(target) + self.addDef(target) def __repr__(self): return '{0} = {1}'.format(self.target, self.value) @@ -61,33 +100,38 @@ #print('operation is in binops:', operation in BinOps) # Check types of the two operands: self.result = result - self.defs.add(result) + self.addDef(result) self.value1 = value1 self.value2 = value2 - self.uses.add(value1) - self.uses.add(value2) + self.addUse(value1) + self.addUse(value2) self.operation = operation def __repr__(self): return '{0} = {2} {1} {3}'.format(self.result, self.operation, self.value1, self.value2) # Memory functions: class Load(Instruction): - def __init__(self, name, value): + def __init__(self, location, value): super().__init__() + assert type(value) is Value + assert type(location) is Value, "Location must be a value" self.value = value - self.defs.add(value) - self.name = name + self.addDef(value) + self.location = location + self.addUse(self.location) def __repr__(self): - return '{1} = [{0}]'.format(self.name, self.value) + return '{1} = [{0}]'.format(self.location, self.value) class Store(Instruction): - def __init__(self, name, value): + def __init__(self, location, value): super().__init__() - self.name = name + assert type(value) is Value + assert type(location) is Value, "Location must be a value" + self.location = location self.value = value - self.uses.add(value) + self.addUse(value) def __repr__(self): - return '[{0}] = {1}'.format(self.name, self.value) + return '[{0}] = {1}'.format(self.location, self.value) # Branching: class Branch(Instruction): @@ -107,8 +151,8 @@ assert type(a) is Value self.cond = cond self.b = b - self.uses.add(a) - self.uses.add(b) + self.addUse(a) + self.addUse(b) assert type(b) is Value assert type(lab1) is BasicBlock self.lab1 = lab1