Mercurial > lcfOS
diff python/ppci/target/basetarget.py @ 346:3bb7dcfe5529
expanded arm target
author | Windel Bouwman |
---|---|
date | Fri, 07 Mar 2014 17:05:32 +0100 |
parents | b4882ff0ed06 |
children | 5477e499b039 |
line wrap: on
line diff
--- a/python/ppci/target/basetarget.py Sun Mar 02 17:12:08 2014 +0100 +++ b/python/ppci/target/basetarget.py Fri Mar 07 17:05:32 2014 +0100 @@ -1,49 +1,9 @@ -from ppci.asmnodes import ASymbol, AInstruction, ANumber from ppci import CompilerError """ Base classes for defining a target """ -# Machine code interface: -class Operand: - """ Single machine operand """ - pass - -# standard immediates: - -class ImmBase: - def __init__(self, imm): - assert type(imm) is int - assert imm < self.Max() - self.imm = imm - - @classmethod - def Max(cls): - return 2**cls.bits - - @classmethod - def Create(cls, vop): - if type(vop) is ANumber and vop.number < cls.Max(): - return cls(vop.number) - - -class Imm3(ImmBase): - bits = 3 - - -class Imm7(ImmBase): - bits = 7 - - -class Imm8(ImmBase): - bits = 8 - - -class Imm32(ImmBase): - bits = 32 - - class Instruction: """ Base instruction class """ def encode(self): @@ -76,12 +36,6 @@ def symbols(self): return [self.name] - @classmethod - def Create(cls, vop): - if type(vop) is ASymbol: - name = vop.name - return cls(name) - class Comment(PseudoInstruction): def __init__(self, txt): @@ -119,7 +73,7 @@ return 'DebugInfo: {}'.format(self.info) -class Register(Operand): +class Register: def __init__(self, name): self.name = name @@ -131,10 +85,16 @@ self.registers = [] self.byte_sizes = {'int' : 4} # For front end! + # For lowering: + self.lower_functions = {} + # For assembler: self.assembler_rules = [] self.asm_keywords = [] + self.generate_base_rules() + + def generate_base_rules(self): # Base rules for constants: self.add_rule('imm32', ['val32'], lambda x: x[0].val) self.add_rule('imm32', ['imm16'], lambda x: x[0]) @@ -162,11 +122,15 @@ def add_rule(self, lhs, rhs, f): self.assembler_rules.append((lhs, rhs, f)) - def instruction(self, cls): - """ Decorator function that registers an instruction to this target """ - self.addInstruction(cls) - return cls + def lower_frame_to_stream(self, frame, outs): + """ Lower instructions from frame to output stream """ + for im in frame.instructions: + if isinstance(im.assem, Instruction): + outs.emit(im.assem) + else: + ins = self.lower_functions[im.assem](im) + outs.emit(ins) - def addInstruction(self, i): - pass - + def add_lowering(self, cls, f): + """ Add a function to the table of lowering options for this target """ + self.lower_functions[cls] = f