view python/ppci/ir2tree.py @ 330:a79ac866732f

Added kernel recipe
author Windel Bouwman
date Fri, 07 Feb 2014 12:08:40 +0100
parents e9fe6988497c
children 5477e499b039
line wrap: on
line source

from tree import Tree
from . import ir

""" Create a tree from ir code. """

f_map = {}    # Mapping from types to tree creation functions

def register(tp):
    """ Register a function for type tp """
    def reg_f(f):
        f_map[tp] = f
        return f
    return reg_f

@register(ir.Binop)
def binop_to_tree(e):
    names = {'+':'ADDI32', '-':'SUBI32', '|':'ORI32', '<<':'SHLI32',
        '*':'MULI32'}
    op = names[e.operation]
    return Tree(op, makeTree(e.a), makeTree(e.b))

@register(ir.Temp)
def temp_to_tree(e):
    t = Tree('REGI32')
    t.value = e
    return t

@register(ir.Const)
def const_to_tree(e):
    t = Tree('CONSTI32')
    t.value = e.value
    return t

@register(ir.Mem)
def mem_to_tree(e):
    return Tree('MEMI32', makeTree(e.e))

@register(ir.Call)
def call_to_tree(e):
    t = Tree('CALL')
    t.value = e
    return t

def makeTree(ir_node):
    """ Transform an ir node into a tree usable for matching """
    return f_map[type(ir_node)](ir_node)