comparison python/target.py @ 200:5e391d9a3381

Split off asm nodes
author Windel Bouwman
date Sun, 09 Jun 2013 16:06:49 +0200
parents a690473b79e2
children d5debbfc0200
comparison
equal deleted inserted replaced
199:a690473b79e2 200:5e391d9a3381
1 from asmnodes import ASymbol, AInstruction
2 from ppci import CompilerError
1 3
2 """ 4 """
3 Base classes for defining a target 5 Base classes for defining a target
4 """ 6 """
5 7
16 def __init__(self, opcode): 18 def __init__(self, opcode):
17 self.opcode = opcode 19 self.opcode = opcode
18 20
19 class Target: 21 class Target:
20 def __init__(self): 22 def __init__(self):
23 self.registers = []
21 self.instructions = [] 24 self.instructions = []
22 def createInstruction(self, vi):
23 pass
24 pass
25 25
26 def mapOperand(self, operand):
27 """ Try to map an operand to a target type """
28 if type(operand) is ASymbol:
29 # Try to map to register:
30 regs = {}
31 for r in self.registers:
32 regs[r.name] = r
33 if operand.name in regs:
34 return regs[operand.name]
35 else:
36 return
26 37
38 def mapInstruction(self, vi):
39 """ Map ast tree to real instruction for this target """
40
41 # map to real operands:
42 rops = tuple(map(self.mapOperand, vi.operands))
43 optypes = tuple(map(type, rops))
44
45 # look for a suitable instruction
46 for ic in self.instructions:
47 if ic.mnemonic == vi.opcode and ic.operands == optypes:
48 ri = ic(*rops)
49 return ri
50 raise CompilerError('No suitable instruction found')
51