view python/ir/instruction.py @ 272:e64bae57cda8

refactor ir
author Windel Bouwman
date Sat, 31 Aug 2013 17:58:54 +0200
parents 5ec7580976d9
children
line wrap: on
line source

from .module 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):
    """ Convenience call """
    return Binop(a, '+', b)


class Alloc(Expression):
    """ Allocates space on the stack """
    def __init__(self):
        super().__init__()

    def __repr__(self):
        return 'Alloc'


class Variable(Expression):
    def __init__(self, offset):
        self.offset = offset

    def __repr__(self):
        return 'Variable'


class LocalVariable(Variable):
    pass


class GlobalVariable(Variable):
    pass


class Parameter(Variable):
    pass


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)