Mercurial > lcfOS
diff python/ppci/ir.py @ 394:988f3fb861e4
c3 code generator rewrite
author | Windel Bouwman |
---|---|
date | Thu, 22 May 2014 08:14:12 +0200 |
parents | 9667d78ba79e |
children |
line wrap: on
line diff
--- a/python/ppci/ir.py Sat May 17 21:17:40 2014 +0200 +++ b/python/ppci/ir.py Thu May 22 08:14:12 2014 +0200 @@ -4,7 +4,7 @@ def label_name(dut): - """ Function that returns the assembly code label name """ + """ Returns the assembly code label name """ if isinstance(dut, Block): f = dut.function return label_name(f) + '_' + dut.name @@ -16,6 +16,14 @@ raise NotImplementedError(str(dut)) +class Typ: + def __init__(self): + pass + + +i32 = Typ() +i8 = Typ() + class Module: """ Container unit for variables and functions. """ def __init__(self, name): @@ -46,14 +54,12 @@ Functions = property(get_functions) - def findFunction(self, name): + def find_function(self, name): for f in self.funcs: if f.name == name: return f raise KeyError(name) - getFunction = findFunction - class Function: """ Represents a function. """ @@ -195,9 +201,27 @@ # Instructions: class Value: - pass + """ A value has a type and a name """ + def __init__(self, name, ty): + assert isinstance(ty, Typ) + self.name = name + self.ty = ty + -class Expression: +class User(Value): + """ Value that uses other values """ + def __init__(self, name, ty): + super().__init__(name, ty) + # Create a collection to store the values this value uses. + # TODO: think of better naming.. + self.uses = set() + + def add_use(self, v): + assert isinstance(v, Value) + self.uses.add(v) + + +class Expression(User): """ Base class for an expression """ pass @@ -228,11 +252,14 @@ """ Generic binary operation """ ops = ['+', '-', '*', '/', '|', '&', '<<', '>>'] - def __init__(self, value1, operation, value2): + def __init__(self, a, operation, b, name, ty): + super().__init__(name, ty) assert operation in Binop.ops #assert type(value1) is type(value2) - self.a = value1 - self.b = value2 + assert isinstance(a, Value), str(a) + assert isinstance(b, Value), str(b) + self.a = a + self.b = b self.operation = operation def __repr__(self): @@ -240,9 +267,10 @@ return '({} {} {})'.format(a, self.operation, b) -def Add(a, b): +class Add(Binop): """ Add a and b """ - return Binop(a, '+', b) + def __init__(self, a, b, name, ty): + super().__init__(a, '+', b, name, ty) def Sub(a, b): @@ -250,9 +278,9 @@ return Binop(a, '-', b) -def Mul(a, b): +def Mul(a, b, name, ty): """ Multiply a by b """ - return Binop(a, '*', b) + return Binop(a, '*', b, name, ty) def Div(a, b): @@ -260,6 +288,16 @@ return Binop(a, '/', b) +def Phi(User): + """ Imaginary phi instruction to make SSA possible. """ + def __init__(self, name, ty): + super().__init__(name, ty) + self.inputs = [] + + def add_input(self, value, block): + self.inputs.append((value, block)) + + class Eseq(Expression): """ Sequence of instructions where the last is an expression """ def __init__(self, stmt, e): @@ -280,7 +318,8 @@ class Variable(Expression): - def __init__(self, name): + def __init__(self, name, ty): + super().__init__(name, ty) self.name = name def __repr__(self): @@ -320,6 +359,23 @@ return '[{}]'.format(self.e) +class Load(Value): + """ Load a value from memory """ + def __init__(self, address, name, ty): + super().__init__(name, ty) + assert isinstance(address, Value) + self.address = address + + def __repr__(self): + return 'load {}'.format(self.address) + + +class Store: + """ Store a value into memory """ + def __init__(self, address, value): + self.address = address + + class Addr(Expression): """ Address of label """ def __init__(self, e):