comparison python/target.py @ 206:6c6bf8890d8a

Added push and pop encodings
author Windel Bouwman
date Fri, 28 Jun 2013 16:49:38 +0200
parents d77cb5962cc5
children 1fa3e0050b49
comparison
equal deleted inserted replaced
205:d77cb5962cc5 206:6c6bf8890d8a
1 from asmnodes import ASymbol, AInstruction, ALabel 1 from asmnodes import ASymbol, AInstruction, ALabel, ANumber
2 from ppci import CompilerError 2 from ppci import CompilerError
3 3
4 """ 4 """
5 Base classes for defining a target 5 Base classes for defining a target
6 """ 6 """
7 7
8 # Machine code interface: 8 # Machine code interface:
9 class Operand: 9 class Operand:
10 """ Single machine operand """ 10 """ Single machine operand """
11 pass 11 pass
12
13 # standard immediates:
14
15 class Imm8:
16 def __init__(self, imm):
17 assert imm < 256
18 self.imm = imm
19
20 @classmethod
21 def Create(cls, vop):
22 if type(vop) is ANumber and vop.number < 256:
23 return cls(vop.number)
24
25 class Imm3:
26 def __init__(self, imm):
27 assert imm < 8
28 assert type(imm) is int
29 self.imm = imm
30
31 @classmethod
32 def Create(cls, vop):
33 if type(vop) is ANumber and vop.number < 8:
34 return cls(vop.number)
35
36 class Label:
37 def __init__(self, name):
38 self.name = name
39
40 @classmethod
41 def Create(cls, vop):
42 if type(vop) is ASymbol:
43 name = vop.name
44 return cls(name)
12 45
13 class Register(Operand): 46 class Register(Operand):
14 def __init__(self, name): 47 def __init__(self, name):
15 self.name = name 48 self.name = name
16 49
27 60
28 def instruction(self, cls): 61 def instruction(self, cls):
29 """ Decorator function that registers an instruction to this target """ 62 """ Decorator function that registers an instruction to this target """
30 self.addInstruction(cls) 63 self.addInstruction(cls)
31 return cls 64 return cls
65
66 def check(self):
67 """ Check target """
68 for i in self.instructions:
69 assert hasattr(i, 'mnemonic')
70 assert hasattr(i, 'operands'), str(i)
71 assert type(i.mnemonic) is str
72 assert type(i.operands) is tuple, str(i)
32 73
33 def addInstruction(self, ins_class): 74 def addInstruction(self, ins_class):
34 self.instructions.append(ins_class) 75 self.instructions.append(ins_class)
35 76
36 def mapOperand(self, operand): 77 def mapOperand(self, operand):