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
6f4753202b9a Added more recipes
Windel Bouwman
parents: 306
diff changeset
1 from ppci.asmnodes import ASymbol, AInstruction, ANumber
200
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
2 from ppci import CompilerError
199
a690473b79e2 Added msp430 target
Windel Bouwman
parents:
diff changeset
3
a690473b79e2 Added msp430 target
Windel Bouwman
parents:
diff changeset
4 """
a690473b79e2 Added msp430 target
Windel Bouwman
parents:
diff changeset
5 Base classes for defining a target
a690473b79e2 Added msp430 target
Windel Bouwman
parents:
diff changeset
6 """
a690473b79e2 Added msp430 target
Windel Bouwman
parents:
diff changeset
7
a690473b79e2 Added msp430 target
Windel Bouwman
parents:
diff changeset
8 # Machine code interface:
a690473b79e2 Added msp430 target
Windel Bouwman
parents:
diff changeset
9 class Operand:
a690473b79e2 Added msp430 target
Windel Bouwman
parents:
diff changeset
10 """ Single machine operand """
a690473b79e2 Added msp430 target
Windel Bouwman
parents:
diff changeset
11 pass
a690473b79e2 Added msp430 target
Windel Bouwman
parents:
diff changeset
12
206
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
13 # standard immediates:
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
14
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
15 class ImmBase:
206
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
16 def __init__(self, imm):
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
17 assert type(imm) is int
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
18 assert imm < self.Max()
206
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
19 self.imm = imm
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
20
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
21 @classmethod
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
22 def Max(cls):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
23 return 2**cls.bits
275
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 249
diff changeset
24
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 249
diff changeset
25 @classmethod
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 249
diff changeset
26 def Create(cls, vop):
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
27 if type(vop) is ANumber and vop.number < cls.Max():
275
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 249
diff changeset
28 return cls(vop.number)
6f2423df0675 Fixed serve arm-as
Windel Bouwman
parents: 249
diff changeset
29
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
30
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
31 class Imm3(ImmBase):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
32 bits = 3
206
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
33
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
34
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
35 class Imm7(ImmBase):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
36 bits = 7
206
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
37
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
38
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
39 class Imm8(ImmBase):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
40 bits = 8
235
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
41
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
42
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
43 class Imm32(ImmBase):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
44 bits = 32
235
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
45
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
46
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
47 class LabelRef:
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
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
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
50 self.name = name
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
51
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
52 @classmethod
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
53 def Create(cls, vop):
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
54 if type(vop) is ASymbol:
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
55 return cls(vop.name)
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
56
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
57
234
Windel Bouwman
parents: 219
diff changeset
58 class Instruction:
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
59 """ Base instruction class """
234
Windel Bouwman
parents: 219
diff changeset
60 def encode(self):
335
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
61 return bytes()
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
62
335
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
63 def relocations(self):
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
64 return []
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
65
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
66 def symbols(self):
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
67 return []
234
Windel Bouwman
parents: 219
diff changeset
68
Windel Bouwman
parents: 219
diff changeset
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
Windel Bouwman
parents: 219
diff changeset
76 class PseudoInstruction(Instruction):
Windel Bouwman
parents: 219
diff changeset
77 pass
Windel Bouwman
parents: 219
diff changeset
78
Windel Bouwman
parents: 219
diff changeset
79
Windel Bouwman
parents: 219
diff changeset
80 class Label(PseudoInstruction):
206
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
81 def __init__(self, name):
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
82 self.name = name
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
83
235
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
84 def __repr__(self):
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
85 return '{}:'.format(self.name)
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
86
335
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
87 def symbols(self):
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
88 return [self.name]
235
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
89
206
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
90 @classmethod
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
91 def Create(cls, vop):
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
92 if type(vop) is ASymbol:
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
93 name = vop.name
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
94 return cls(name)
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
95
235
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
96
234
Windel Bouwman
parents: 219
diff changeset
97 class Comment(PseudoInstruction):
Windel Bouwman
parents: 219
diff changeset
98 def __init__(self, txt):
Windel Bouwman
parents: 219
diff changeset
99 self.txt = txt
249
e41e4109addd Added current position arrow
Windel Bouwman
parents: 236
diff changeset
100
235
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
101 def encode(self):
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
102 return bytes()
249
e41e4109addd Added current position arrow
Windel Bouwman
parents: 236
diff changeset
103
234
Windel Bouwman
parents: 219
diff changeset
104 def __repr__(self):
Windel Bouwman
parents: 219
diff changeset
105 return '; {}'.format(self.txt)
Windel Bouwman
parents: 219
diff changeset
106
235
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
107
234
Windel Bouwman
parents: 219
diff changeset
108 class Alignment(PseudoInstruction):
Windel Bouwman
parents: 219
diff changeset
109 def __init__(self, a):
Windel Bouwman
parents: 219
diff changeset
110 self.align = a
235
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
111
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
112 def __repr__(self):
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
113 return 'ALIGN({})'.format(self.align)
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
114
234
Windel Bouwman
parents: 219
diff changeset
115 def encode(self):
Windel Bouwman
parents: 219
diff changeset
116 pad = []
335
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
117 # TODO
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
118 address = 0
235
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
119 while (address % self.align) != 0:
234
Windel Bouwman
parents: 219
diff changeset
120 address += 1
Windel Bouwman
parents: 219
diff changeset
121 pad.append(0)
Windel Bouwman
parents: 219
diff changeset
122 return bytes(pad)
Windel Bouwman
parents: 219
diff changeset
123
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
124
249
e41e4109addd Added current position arrow
Windel Bouwman
parents: 236
diff changeset
125 class DebugInfo(PseudoInstruction):
e41e4109addd Added current position arrow
Windel Bouwman
parents: 236
diff changeset
126 def __init__(self, i):
e41e4109addd Added current position arrow
Windel Bouwman
parents: 236
diff changeset
127 self.info = i
e41e4109addd Added current position arrow
Windel Bouwman
parents: 236
diff changeset
128
e41e4109addd Added current position arrow
Windel Bouwman
parents: 236
diff changeset
129 def __repr__(self):
e41e4109addd Added current position arrow
Windel Bouwman
parents: 236
diff changeset
130 return 'DebugInfo: {}'.format(self.info)
e41e4109addd Added current position arrow
Windel Bouwman
parents: 236
diff changeset
131
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
132
199
a690473b79e2 Added msp430 target
Windel Bouwman
parents:
diff changeset
133 class Register(Operand):
a690473b79e2 Added msp430 target
Windel Bouwman
parents:
diff changeset
134 def __init__(self, name):
a690473b79e2 Added msp430 target
Windel Bouwman
parents:
diff changeset
135 self.name = name
a690473b79e2 Added msp430 target
Windel Bouwman
parents:
diff changeset
136
a690473b79e2 Added msp430 target
Windel Bouwman
parents:
diff changeset
137
a690473b79e2 Added msp430 target
Windel Bouwman
parents:
diff changeset
138 class Target:
201
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
139 def __init__(self, name, desc=''):
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
140 self.name = name
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
141 self.desc = desc
200
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
142 self.registers = []
341
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 335
diff changeset
143 self.byte_sizes = {'int' : 4} # For front end!
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 335
diff changeset
144
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 335
diff changeset
145 # For assembler:
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 335
diff changeset
146 self.assembler_rules = []
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 335
diff changeset
147 self.asm_keywords = []
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 335
diff changeset
148
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 335
diff changeset
149 def add_keyword(self, kw):
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 335
diff changeset
150 self.asm_keywords.append(kw)
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 335
diff changeset
151
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 335
diff changeset
152 def add_instruction(self, rhs, f):
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 335
diff changeset
153 self.add_rule('instruction', rhs, f)
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 335
diff changeset
154
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 335
diff changeset
155 def add_rule(self, lhs, rhs, f):
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 335
diff changeset
156 self.assembler_rules.append((lhs, rhs, f))
200
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
157
201
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
158 def instruction(self, cls):
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
159 """ Decorator function that registers an instruction to this target """
202
f22b431f4113 Added arm add instruction
Windel Bouwman
parents: 201
diff changeset
160 self.addInstruction(cls)
201
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
161 return cls
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
162
341
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 335
diff changeset
163 def addInstruction(self, i):
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 335
diff changeset
164 pass
202
f22b431f4113 Added arm add instruction
Windel Bouwman
parents: 201
diff changeset
165