Mercurial > lcfOS
diff python/ppci/target/basetarget.py @ 342:86b02c98a717 devel
Moved target directory
author | Windel Bouwman |
---|---|
date | Sat, 01 Mar 2014 15:40:31 +0100 |
parents | python/target/basetarget.py@4d204f6f7d4e |
children | b4882ff0ed06 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/python/ppci/target/basetarget.py Sat Mar 01 15:40:31 2014 +0100 @@ -0,0 +1,154 @@ +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): + return bytes() + + def relocations(self): + return [] + + def symbols(self): + return [] + + +class Nop(Instruction): + """ Instruction that does nothing and has zero size """ + def encode(self): + return bytes() + + +class PseudoInstruction(Instruction): + pass + + +class Label(PseudoInstruction): + def __init__(self, name): + self.name = name + + def __repr__(self): + return '{}:'.format(self.name) + + 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): + self.txt = txt + + def encode(self): + return bytes() + + def __repr__(self): + return '; {}'.format(self.txt) + + +class Alignment(PseudoInstruction): + def __init__(self, a): + self.align = a + + def __repr__(self): + return 'ALIGN({})'.format(self.align) + + def encode(self): + pad = [] + # TODO + address = 0 + while (address % self.align) != 0: + address += 1 + pad.append(0) + return bytes(pad) + + +class DebugInfo(PseudoInstruction): + def __init__(self, i): + self.info = i + + def __repr__(self): + return 'DebugInfo: {}'.format(self.info) + + +class Register(Operand): + def __init__(self, name): + self.name = name + + +class Target: + def __init__(self, name, desc=''): + self.name = name + self.desc = desc + self.registers = [] + self.byte_sizes = {'int' : 4} # For front end! + + # For assembler: + self.assembler_rules = [] + self.asm_keywords = [] + + def add_keyword(self, kw): + self.asm_keywords.append(kw) + + def add_instruction(self, rhs, f): + self.add_rule('instruction', rhs, f) + + 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 addInstruction(self, i): + pass +