view python/codegenarm.py @ 212:62386bcee1ba

Added parser combinator lib
author Windel Bouwman
date Sun, 30 Jun 2013 19:00:41 +0200
parents 99164160fb0b
children 494828a7adf1
line wrap: on
line source

import ir
from asmnodes import ALabel
import arm_cm3 as arm
from ppci import CompilerError

class ArmCodeGenerator:
    """ Simple code generator """
    def __init__(self, out):
        self.outs = out

    def emit(self, item):
        self.outs.emit(item)

    def generate(self, ircode):
        assert isinstance(ircode, ir.Module)
        print('ARM code generation')
        self.outs.selectSection('data')

        for gvar in ircode.Variables:
            self.emit(ALabel(gvar.name))
            # TODO: use initial value:
            self.emit(arm.dcd_ins(0))

        self.outs.selectSection('code')
        for f in ircode.Functions:
            self.emit(ALabel(f.name))
            self.emit(arm.push_ins(arm.RegisterSet({arm.r2, arm.r3,arm.lr})))
            for bb in f.BasicBlocks:
                self.emit(ALabel(bb.name))
                for ins in bb.Instructions:
                    self.generateInstruction(ins)

    def generateInstruction(self, ins):
        if type(ins) is ir.Branch:
            self.emit(arm.jmp_ins(ins.target))
        elif type(ins) is ir.ImmLoad and ins.value < 255:
            self.emit(arm.mov_ins(arm.r0, arm.Imm8(ins.value)))
            # determine stack frame..
            self.emit(arm.mov_ins(arm.r1, arm.Imm8(9)))
            #self.emit(arm.
        elif type(ins) is ir.ImmLoad and ins.value < (2**32):
            print(ins)
        elif type(ins) is ir.Store:
            print(ins)
        elif type(ins) is ir.Return:
            self.emit(arm.pop_ins(arm.RegisterSet({arm.r2, arm.r3, arm.pc})))
        elif type(ins) is ir.Load:
            print(ins)
        elif type(ins) is ir.BinaryOperator:
            print(ins)
        elif type(ins) is ir.ConditionalBranch:
            print(ins)
        else:
            print(ins)
            raise CompilerError('IR "{}" not covered'.format(ins))