Mercurial > lcfOS
view python/ir/instruction.py @ 269:5f8c04a8d26b
Towards better modularity
author | Windel Bouwman |
---|---|
date | Sun, 18 Aug 2013 17:43:18 +0200 |
parents | 5ec7580976d9 |
children | e64bae57cda8 |
line wrap: on
line source
from .function import Function class Expression: pass class Const(Expression): def __init__(self, value): self.value = value def __repr__(self): return 'Constant {}'.format(self.value) # Function calling: class Call(Expression): def __init__(self, f, arguments): self.f = f assert type(f) is Function self.arguments = arguments def __repr__(self): args = ','.join([str(arg) for arg in self.arguments]) return '{}({})'.format(self.f.name, args) # Data operations class Binop(Expression): ops = ['+', '-', '*', '/', '|', '&', '<<', '>>'] def __init__(self, value1, operation, value2): assert operation in Binop.ops self.value1 = value1 self.value2 = value2 self.operation = operation def __repr__(self): a, b = self.value1, self.value2 return '({} {} {})'.format(a, self.operation, b) def Add(a, b): return Binop(a, '+', b) class Alloc(Expression): """ Allocates space on the stack """ def __init__(self): super().__init__() def __repr__(self): return 'Alloc' class Temp(Expression): """ Temporary storage, same as register """ def __init__(self, name): self.name = name def __repr__(self): return 'T_{}_'.format(self.name) class Mem(Expression): def __init__(self, e): self.e = e def __repr__(self): return '[{}]'.format(self.e) class Statement: """ Base class for all instructions. """ pass class Move(Statement): def __init__(self, dst, src): self.dst = dst self.src = src def __repr__(self): return '{} = {}'.format(self.dst, self.src) class Exp(Statement): def __init__(self, e): self.e = e class Label(Statement): def __init__(self, name): self.name = name self.statements = [] def __repr__(self): return 'LABEL {}:'.format(self.name) # Branching: class Jump(Statement): def __init__(self, target): self.target = target self.Targets = [target] def __repr__(self): return 'BRANCH {}'.format(self.target.name) class CJump(Statement): def __init__(self, a, cond, b, lab_yes, lab_no): assert cond in ['==', '<', '>', '>=', '<=', '!='] self.a = a self.cond = cond self.b = b self.lab_yes = lab_yes self.lab_no = lab_no self.Targets = [lab_yes, lab_no] def __repr__(self): return 'IF {} {} {} THEN {} ELSE {}'.format(self.a, self.cond, self.b, self.lab_yes, self.lab_no) class Return(Statement): def __init__(self, value): self.value = value self.Targets = [] # TODO: Do this in another way def __repr__(self): if self.value: return 'ret {0}'.format(self.value) else: return 'ret'