comparison python/target.py @ 201:d5debbfc0200

Added all 27 core instructions of msp430
author Windel Bouwman
date Thu, 13 Jun 2013 00:07:28 +0200
parents 5e391d9a3381
children f22b431f4113
comparison
equal deleted inserted replaced
200:5e391d9a3381 201:d5debbfc0200
13 class Register(Operand): 13 class Register(Operand):
14 def __init__(self, name): 14 def __init__(self, name):
15 self.name = name 15 self.name = name
16 16
17 class Instruction: 17 class Instruction:
18 def __init__(self, opcode): 18 def encode(self):
19 self.opcode = opcode 19 raise NotImplementedError('TODO')
20 20
21 class Target: 21 class Target:
22 def __init__(self): 22 def __init__(self, name, desc=''):
23 self.name = name
24 self.desc = desc
23 self.registers = [] 25 self.registers = []
24 self.instructions = [] 26 self.instructions = []
27
28 def instruction(self, cls):
29 """ Decorator function that registers an instruction to this target """
30 self.instructions.append(cls)
31 return cls
25 32
26 def mapOperand(self, operand): 33 def mapOperand(self, operand):
27 """ Try to map an operand to a target type """ 34 """ Try to map an operand to a target type """
28 if type(operand) is ASymbol: 35 if type(operand) is ASymbol:
29 # Try to map to register: 36 # Try to map to register:
30 regs = {} 37 regs = {}
31 for r in self.registers: 38 for r in self.registers:
32 regs[r.name] = r 39 regs[r.name] = r
33 if operand.name in regs: 40 if operand.name in regs:
34 return regs[operand.name] 41 return regs[operand.name]
35 else: 42 raise CompilerError('Cannot map {0}'.format(operand))
36 return
37 43
38 def mapInstruction(self, vi): 44 def mapInstruction(self, vi):
39 """ Map ast tree to real instruction for this target """ 45 """ Map ast tree to real instruction for this target """
40 46
41 # map to real operands: 47 # map to real operands:
45 # look for a suitable instruction 51 # look for a suitable instruction
46 for ic in self.instructions: 52 for ic in self.instructions:
47 if ic.mnemonic == vi.opcode and ic.operands == optypes: 53 if ic.mnemonic == vi.opcode and ic.operands == optypes:
48 ri = ic(*rops) 54 ri = ic(*rops)
49 return ri 55 return ri
50 raise CompilerError('No suitable instruction found') 56 raise CompilerError('No suitable instruction found for "{0}"'.format(vi))
51 57