Mercurial > lcfOS
annotate python/ppci/target/basetarget.py @ 342:86b02c98a717 devel
Moved target directory
author | Windel Bouwman |
---|---|
date | Sat, 01 Mar 2014 15:40:31 +0100 |
parents | python/target/basetarget.py@4d204f6f7d4e |
children | b4882ff0ed06 |
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 | |
234 | 47 class Instruction: |
292 | 48 """ Base instruction class """ |
234 | 49 def encode(self): |
335 | 50 return bytes() |
292 | 51 |
335 | 52 def relocations(self): |
53 return [] | |
54 | |
55 def symbols(self): | |
56 return [] | |
234 | 57 |
58 | |
280
02385f62f250
Rework from str interface to Instruction interface
Windel Bouwman
parents:
277
diff
changeset
|
59 class Nop(Instruction): |
02385f62f250
Rework from str interface to Instruction interface
Windel Bouwman
parents:
277
diff
changeset
|
60 """ Instruction that does nothing and has zero size """ |
02385f62f250
Rework from str interface to Instruction interface
Windel Bouwman
parents:
277
diff
changeset
|
61 def encode(self): |
02385f62f250
Rework from str interface to Instruction interface
Windel Bouwman
parents:
277
diff
changeset
|
62 return bytes() |
02385f62f250
Rework from str interface to Instruction interface
Windel Bouwman
parents:
277
diff
changeset
|
63 |
02385f62f250
Rework from str interface to Instruction interface
Windel Bouwman
parents:
277
diff
changeset
|
64 |
234 | 65 class PseudoInstruction(Instruction): |
66 pass | |
67 | |
68 | |
69 class Label(PseudoInstruction): | |
206 | 70 def __init__(self, name): |
71 self.name = name | |
72 | |
235 | 73 def __repr__(self): |
74 return '{}:'.format(self.name) | |
75 | |
335 | 76 def symbols(self): |
77 return [self.name] | |
235 | 78 |
206 | 79 @classmethod |
80 def Create(cls, vop): | |
81 if type(vop) is ASymbol: | |
82 name = vop.name | |
83 return cls(name) | |
84 | |
235 | 85 |
234 | 86 class Comment(PseudoInstruction): |
87 def __init__(self, txt): | |
88 self.txt = txt | |
249 | 89 |
235 | 90 def encode(self): |
91 return bytes() | |
249 | 92 |
234 | 93 def __repr__(self): |
94 return '; {}'.format(self.txt) | |
95 | |
235 | 96 |
234 | 97 class Alignment(PseudoInstruction): |
98 def __init__(self, a): | |
99 self.align = a | |
235 | 100 |
101 def __repr__(self): | |
102 return 'ALIGN({})'.format(self.align) | |
103 | |
234 | 104 def encode(self): |
105 pad = [] | |
335 | 106 # TODO |
107 address = 0 | |
235 | 108 while (address % self.align) != 0: |
234 | 109 address += 1 |
110 pad.append(0) | |
111 return bytes(pad) | |
112 | |
292 | 113 |
249 | 114 class DebugInfo(PseudoInstruction): |
115 def __init__(self, i): | |
116 self.info = i | |
117 | |
118 def __repr__(self): | |
119 return 'DebugInfo: {}'.format(self.info) | |
120 | |
292 | 121 |
199 | 122 class Register(Operand): |
123 def __init__(self, name): | |
124 self.name = name | |
125 | |
126 | |
127 class Target: | |
201 | 128 def __init__(self, name, desc=''): |
129 self.name = name | |
130 self.desc = desc | |
200 | 131 self.registers = [] |
341 | 132 self.byte_sizes = {'int' : 4} # For front end! |
133 | |
134 # For assembler: | |
135 self.assembler_rules = [] | |
136 self.asm_keywords = [] | |
137 | |
138 def add_keyword(self, kw): | |
139 self.asm_keywords.append(kw) | |
140 | |
141 def add_instruction(self, rhs, f): | |
142 self.add_rule('instruction', rhs, f) | |
143 | |
144 def add_rule(self, lhs, rhs, f): | |
145 self.assembler_rules.append((lhs, rhs, f)) | |
200 | 146 |
201 | 147 def instruction(self, cls): |
148 """ Decorator function that registers an instruction to this target """ | |
202 | 149 self.addInstruction(cls) |
201 | 150 return cls |
151 | |
341 | 152 def addInstruction(self, i): |
153 pass | |
202 | 154 |