Mercurial > lcfOS
diff 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 |
line wrap: on
line diff
--- a/python/ir/instruction.py Sun Mar 03 18:14:35 2013 +0100 +++ b/python/ir/instruction.py Fri Mar 08 16:52:44 2013 +0100 @@ -1,34 +1,44 @@ - -def Enum(**enums): - return type('Enum', (), enums) class Instruction: """ Base class for all instructions. """ pass +# Function calling: class CallInstruction(Instruction): def __init__(self, callee, arguments): super().__init__() self.callee = callee self.arguments = arguments + def __repr__(self): + return 'CALL {0}'.format(self.callee) -BinOps = Enum(Add=1, Sub=2, Mul=3) +class RetInstruction(Instruction): + def __repr__(self): + return 'RET' class BinaryOperator(Instruction): - def __init__(self, operation, value1, value2): + def __init__(self, name, operation, value1, value2): assert value1 assert value2 #print('operation is in binops:', operation in BinOps) # Check types of the two operands: + self.name = name self.value1 = value1 self.value2 = value2 self.operation = operation - -class AddInstruction(BinaryOperator): - def __init__(self, a, b): - super().__init__('add', a, b) + def __repr__(self): + return '{0} = {1} {2}, {3}'.format(self.name, self.operation, self.value1, self.value2) -class ImmLoadInstruction(Instruction): - def __init__(self, value): +class LoadInstruction(Instruction): + def __init__(self, name, value): self.value = value + self.name = name + def __repr__(self): + return 'load {0} = {1}'.format(self.name, self.value) +class BranchInstruction(Instruction): + def __init__(self, t1, t2): + self.t1 = t1 + self.t2 = t2 + def __repr__(self): + return 'BRANCH {0}'.format(self.t1)