Mercurial > lcfOS
annotate python/target/basetarget.py @ 341:4d204f6f7d4e devel
Rewrite of assembler parts
author | Windel Bouwman |
---|---|
date | Fri, 28 Feb 2014 18:07:14 +0100 |
parents | 582a1aaa3983 |
children |
rev | line source |
---|---|
334 | 1 from ppci.asmnodes import ASymbol, AInstruction, ANumber |
200 | 2 from ppci import CompilerError |
199 | 3 |
4 """ | |
5 Base classes for defining a target | |
6 """ | |
7 | |
8 # Machine code interface: | |
9 class Operand: | |
10 """ Single machine operand """ | |
11 pass | |
12 | |
206 | 13 # standard immediates: |
14 | |
292 | 15 class ImmBase: |
206 | 16 def __init__(self, imm): |
292 | 17 assert type(imm) is int |
18 assert imm < self.Max() | |
206 | 19 self.imm = imm |
20 | |
21 @classmethod | |
292 | 22 def Max(cls): |
23 return 2**cls.bits | |
275 | 24 |
25 @classmethod | |
26 def Create(cls, vop): | |
292 | 27 if type(vop) is ANumber and vop.number < cls.Max(): |
275 | 28 return cls(vop.number) |
29 | |
292 | 30 |
31 class Imm3(ImmBase): | |
32 bits = 3 | |
206 | 33 |
292 | 34 |
35 class Imm7(ImmBase): | |
36 bits = 7 | |
206 | 37 |
292 | 38 |
39 class Imm8(ImmBase): | |
40 bits = 8 | |
235 | 41 |
292 | 42 |
43 class Imm32(ImmBase): | |
44 bits = 32 | |
235 | 45 |
46 | |
47 class LabelRef: | |
48 def __init__(self, name): | |
280
02385f62f250
Rework from str interface to Instruction interface
Windel Bouwman
parents:
277
diff
changeset
|
49 assert type(name) is str |
235 | 50 self.name = name |
51 | |
52 @classmethod | |
53 def Create(cls, vop): | |
54 if type(vop) is ASymbol: | |
55 return cls(vop.name) | |
56 | |
292 | 57 |
234 | 58 class Instruction: |
292 | 59 """ Base instruction class """ |
234 | 60 def encode(self): |
335 | 61 return bytes() |
292 | 62 |
335 | 63 def relocations(self): |
64 return [] | |
65 | |
66 def symbols(self): | |
67 return [] | |
234 | 68 |
69 | |
280
02385f62f250
Rework from str interface to Instruction interface
Windel Bouwman
parents:
277
diff
changeset
|
70 class Nop(Instruction): |
02385f62f250
Rework from str interface to Instruction interface
Windel Bouwman
parents:
277
diff
changeset
|
71 """ Instruction that does nothing and has zero size """ |
02385f62f250
Rework from str interface to Instruction interface
Windel Bouwman
parents:
277
diff
changeset
|
72 def encode(self): |
02385f62f250
Rework from str interface to Instruction interface
Windel Bouwman
parents:
277
diff
changeset
|
73 return bytes() |
02385f62f250
Rework from str interface to Instruction interface
Windel Bouwman
parents:
277
diff
changeset
|
74 |
02385f62f250
Rework from str interface to Instruction interface
Windel Bouwman
parents:
277
diff
changeset
|
75 |
234 | 76 class PseudoInstruction(Instruction): |
77 pass | |
78 | |
79 | |
80 class Label(PseudoInstruction): | |
206 | 81 def __init__(self, name): |
82 self.name = name | |
83 | |
235 | 84 def __repr__(self): |
85 return '{}:'.format(self.name) | |
86 | |
335 | 87 def symbols(self): |
88 return [self.name] | |
235 | 89 |
206 | 90 @classmethod |
91 def Create(cls, vop): | |
92 if type(vop) is ASymbol: | |
93 name = vop.name | |
94 return cls(name) | |
95 | |
235 | 96 |
234 | 97 class Comment(PseudoInstruction): |
98 def __init__(self, txt): | |
99 self.txt = txt | |
249 | 100 |
235 | 101 def encode(self): |
102 return bytes() | |
249 | 103 |
234 | 104 def __repr__(self): |
105 return '; {}'.format(self.txt) | |
106 | |
235 | 107 |
234 | 108 class Alignment(PseudoInstruction): |
109 def __init__(self, a): | |
110 self.align = a | |
235 | 111 |
112 def __repr__(self): | |
113 return 'ALIGN({})'.format(self.align) | |
114 | |
234 | 115 def encode(self): |
116 pad = [] | |
335 | 117 # TODO |
118 address = 0 | |
235 | 119 while (address % self.align) != 0: |
234 | 120 address += 1 |
121 pad.append(0) | |
122 return bytes(pad) | |
123 | |
292 | 124 |
249 | 125 class DebugInfo(PseudoInstruction): |
126 def __init__(self, i): | |
127 self.info = i | |
128 | |
129 def __repr__(self): | |
130 return 'DebugInfo: {}'.format(self.info) | |
131 | |
292 | 132 |
199 | 133 class Register(Operand): |
134 def __init__(self, name): | |
135 self.name = name | |
136 | |
137 | |
138 class Target: | |
201 | 139 def __init__(self, name, desc=''): |
140 self.name = name | |
141 self.desc = desc | |
200 | 142 self.registers = [] |
341 | 143 self.byte_sizes = {'int' : 4} # For front end! |
144 | |
145 # For assembler: | |
146 self.assembler_rules = [] | |
147 self.asm_keywords = [] | |
148 | |
149 def add_keyword(self, kw): | |
150 self.asm_keywords.append(kw) | |
151 | |
152 def add_instruction(self, rhs, f): | |
153 self.add_rule('instruction', rhs, f) | |
154 | |
155 def add_rule(self, lhs, rhs, f): | |
156 self.assembler_rules.append((lhs, rhs, f)) | |
200 | 157 |
201 | 158 def instruction(self, cls): |
159 """ Decorator function that registers an instruction to this target """ | |
202 | 160 self.addInstruction(cls) |
201 | 161 return cls |
162 | |
341 | 163 def addInstruction(self, i): |
164 pass | |
202 | 165 |