Mercurial > lcfOS
comparison python/target/basetarget.py @ 292:534b94b40aa8
Fixup reorganize
author | Windel Bouwman |
---|---|
date | Wed, 27 Nov 2013 08:06:42 +0100 |
parents | 7b38782ed496 |
children | b145f8e6050b |
comparison
equal
deleted
inserted
replaced
290:7b38782ed496 | 292:534b94b40aa8 |
---|---|
10 """ Single machine operand """ | 10 """ Single machine operand """ |
11 pass | 11 pass |
12 | 12 |
13 # standard immediates: | 13 # standard immediates: |
14 | 14 |
15 class Imm8: | 15 class ImmBase: |
16 def __init__(self, imm): | 16 def __init__(self, imm): |
17 assert imm < 256 | 17 assert type(imm) is int |
18 assert imm < self.Max() | |
18 self.imm = imm | 19 self.imm = imm |
19 | 20 |
20 @classmethod | 21 @classmethod |
21 def Create(cls, vop): | 22 def Max(cls): |
22 if type(vop) is ANumber and vop.number < 256: | 23 return 2**cls.bits |
23 return cls(vop.number) | |
24 | |
25 class Imm7: | |
26 def __init__(self, imm): | |
27 assert imm < 128 | |
28 self.imm = imm | |
29 | 24 |
30 @classmethod | 25 @classmethod |
31 def Create(cls, vop): | 26 def Create(cls, vop): |
32 if type(vop) is ANumber and vop.number < 128: | 27 if type(vop) is ANumber and vop.number < cls.Max(): |
33 return cls(vop.number) | 28 return cls(vop.number) |
34 | 29 |
35 class Imm3: | |
36 def __init__(self, imm): | |
37 assert imm < 8 | |
38 assert type(imm) is int | |
39 self.imm = imm | |
40 | 30 |
41 @classmethod | 31 class Imm3(ImmBase): |
42 def Create(cls, vop): | 32 bits = 3 |
43 if type(vop) is ANumber and vop.number < 8: | |
44 return cls(vop.number) | |
45 | 33 |
46 class Imm32: | |
47 def __init__(self, imm): | |
48 assert imm < 2**32 | |
49 assert type(imm) is int | |
50 self.imm = imm | |
51 | 34 |
52 @classmethod | 35 class Imm7(ImmBase): |
53 def Create(cls, vop): | 36 bits = 7 |
54 if type(vop) is ANumber and vop.number < 2**32: | 37 |
55 return cls(vop.number) | 38 |
39 class Imm8(ImmBase): | |
40 bits = 8 | |
41 | |
42 | |
43 class Imm32(ImmBase): | |
44 bits = 32 | |
56 | 45 |
57 | 46 |
58 class LabelRef: | 47 class LabelRef: |
59 def __init__(self, name): | 48 def __init__(self, name): |
60 assert type(name) is str | 49 assert type(name) is str |
63 @classmethod | 52 @classmethod |
64 def Create(cls, vop): | 53 def Create(cls, vop): |
65 if type(vop) is ASymbol: | 54 if type(vop) is ASymbol: |
66 return cls(vop.name) | 55 return cls(vop.name) |
67 | 56 |
57 | |
68 class Instruction: | 58 class Instruction: |
59 """ Base instruction class """ | |
69 def encode(self): | 60 def encode(self): |
70 raise NotImplementedError('Instruction {0} has no encode yet, TODO'.format(type(self))) | 61 raise NotImplementedError('Instruction {0} has no encode yet, TODO'.format(type(self))) |
62 | |
71 def resolve(self, f): | 63 def resolve(self, f): |
72 pass | 64 pass |
73 | 65 |
74 | 66 |
75 class Nop(Instruction): | 67 class Nop(Instruction): |
76 """ Instruction that does nothing and has zero size """ | 68 """ Instruction that does nothing and has zero size """ |
77 def encode(self): | 69 def encode(self): |
78 return bytes() | 70 return bytes() |
79 | 71 |
80 | |
81 | 72 |
82 class PseudoInstruction(Instruction): | 73 class PseudoInstruction(Instruction): |
83 pass | 74 pass |
84 | 75 |
85 | 76 |
125 while (address % self.align) != 0: | 116 while (address % self.align) != 0: |
126 address += 1 | 117 address += 1 |
127 pad.append(0) | 118 pad.append(0) |
128 return bytes(pad) | 119 return bytes(pad) |
129 | 120 |
121 | |
130 class DebugInfo(PseudoInstruction): | 122 class DebugInfo(PseudoInstruction): |
131 def __init__(self, i): | 123 def __init__(self, i): |
132 self.info = i | 124 self.info = i |
133 | 125 |
134 def __repr__(self): | 126 def __repr__(self): |
135 return 'DebugInfo: {}'.format(self.info) | 127 return 'DebugInfo: {}'.format(self.info) |
136 | 128 |
137 def encode(self): | 129 def encode(self): |
138 return bytes() | 130 return bytes() |
131 | |
139 | 132 |
140 class Register(Operand): | 133 class Register(Operand): |
141 def __init__(self, name): | 134 def __init__(self, name): |
142 self.name = name | 135 self.name = name |
143 | 136 |
193 | 186 |
194 # Check if we succeeded: | 187 # Check if we succeeded: |
195 if all(isinstance(rop, optype) for rop, optype in zip(rops, ic.operands)): | 188 if all(isinstance(rop, optype) for rop, optype in zip(rops, ic.operands)): |
196 return ic(*rops) | 189 return ic(*rops) |
197 raise CompilerError('No suitable instruction found for "{0}"'.format(vi)) | 190 raise CompilerError('No suitable instruction found for "{0}"'.format(vi)) |
198 |