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
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
234
Windel Bouwman
parents: 219
diff changeset
47 class Instruction:
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
48 """ Base instruction class """
234
Windel Bouwman
parents: 219
diff changeset
49 def encode(self):
335
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
50 return bytes()
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
51
335
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
52 def relocations(self):
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
53 return []
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
54
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
55 def symbols(self):
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
56 return []
234
Windel Bouwman
parents: 219
diff changeset
57
Windel Bouwman
parents: 219
diff changeset
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
Windel Bouwman
parents: 219
diff changeset
65 class PseudoInstruction(Instruction):
Windel Bouwman
parents: 219
diff changeset
66 pass
Windel Bouwman
parents: 219
diff changeset
67
Windel Bouwman
parents: 219
diff changeset
68
Windel Bouwman
parents: 219
diff changeset
69 class Label(PseudoInstruction):
206
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
70 def __init__(self, name):
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
71 self.name = name
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
72
235
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
73 def __repr__(self):
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
74 return '{}:'.format(self.name)
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
75
335
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
76 def symbols(self):
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
77 return [self.name]
235
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
78
206
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
79 @classmethod
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
80 def Create(cls, vop):
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
81 if type(vop) is ASymbol:
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
82 name = vop.name
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
83 return cls(name)
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
84
235
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
85
234
Windel Bouwman
parents: 219
diff changeset
86 class Comment(PseudoInstruction):
Windel Bouwman
parents: 219
diff changeset
87 def __init__(self, txt):
Windel Bouwman
parents: 219
diff changeset
88 self.txt = txt
249
e41e4109addd Added current position arrow
Windel Bouwman
parents: 236
diff changeset
89
235
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
90 def encode(self):
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
91 return bytes()
249
e41e4109addd Added current position arrow
Windel Bouwman
parents: 236
diff changeset
92
234
Windel Bouwman
parents: 219
diff changeset
93 def __repr__(self):
Windel Bouwman
parents: 219
diff changeset
94 return '; {}'.format(self.txt)
Windel Bouwman
parents: 219
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 Alignment(PseudoInstruction):
Windel Bouwman
parents: 219
diff changeset
98 def __init__(self, a):
Windel Bouwman
parents: 219
diff changeset
99 self.align = a
235
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
100
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
101 def __repr__(self):
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
102 return 'ALIGN({})'.format(self.align)
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
103
234
Windel Bouwman
parents: 219
diff changeset
104 def encode(self):
Windel Bouwman
parents: 219
diff changeset
105 pad = []
335
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
106 # TODO
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
107 address = 0
235
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
108 while (address % self.align) != 0:
234
Windel Bouwman
parents: 219
diff changeset
109 address += 1
Windel Bouwman
parents: 219
diff changeset
110 pad.append(0)
Windel Bouwman
parents: 219
diff changeset
111 return bytes(pad)
Windel Bouwman
parents: 219
diff changeset
112
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
113
249
e41e4109addd Added current position arrow
Windel Bouwman
parents: 236
diff changeset
114 class DebugInfo(PseudoInstruction):
e41e4109addd Added current position arrow
Windel Bouwman
parents: 236
diff changeset
115 def __init__(self, i):
e41e4109addd Added current position arrow
Windel Bouwman
parents: 236
diff changeset
116 self.info = i
e41e4109addd Added current position arrow
Windel Bouwman
parents: 236
diff changeset
117
e41e4109addd Added current position arrow
Windel Bouwman
parents: 236
diff changeset
118 def __repr__(self):
e41e4109addd Added current position arrow
Windel Bouwman
parents: 236
diff changeset
119 return 'DebugInfo: {}'.format(self.info)
e41e4109addd Added current position arrow
Windel Bouwman
parents: 236
diff changeset
120
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
121
199
a690473b79e2 Added msp430 target
Windel Bouwman
parents:
diff changeset
122 class Register(Operand):
a690473b79e2 Added msp430 target
Windel Bouwman
parents:
diff changeset
123 def __init__(self, name):
a690473b79e2 Added msp430 target
Windel Bouwman
parents:
diff changeset
124 self.name = name
a690473b79e2 Added msp430 target
Windel Bouwman
parents:
diff changeset
125
a690473b79e2 Added msp430 target
Windel Bouwman
parents:
diff changeset
126
a690473b79e2 Added msp430 target
Windel Bouwman
parents:
diff changeset
127 class Target:
201
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
128 def __init__(self, name, desc=''):
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
129 self.name = name
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
130 self.desc = desc
200
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
131 self.registers = []
341
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 335
diff changeset
132 self.byte_sizes = {'int' : 4} # For front end!
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 335
diff changeset
133
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 335
diff changeset
134 # For assembler:
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 335
diff changeset
135 self.assembler_rules = []
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 335
diff changeset
136 self.asm_keywords = []
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 335
diff changeset
137
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 335
diff changeset
138 def add_keyword(self, kw):
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 335
diff changeset
139 self.asm_keywords.append(kw)
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 335
diff changeset
140
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 335
diff changeset
141 def add_instruction(self, rhs, f):
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 335
diff changeset
142 self.add_rule('instruction', rhs, f)
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 335
diff changeset
143
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 335
diff changeset
144 def add_rule(self, lhs, rhs, f):
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 335
diff changeset
145 self.assembler_rules.append((lhs, rhs, f))
200
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
146
201
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
147 def instruction(self, cls):
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
148 """ Decorator function that registers an instruction to this target """
202
f22b431f4113 Added arm add instruction
Windel Bouwman
parents: 201
diff changeset
149 self.addInstruction(cls)
201
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
150 return cls
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
151
341
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 335
diff changeset
152 def addInstruction(self, i):
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 335
diff changeset
153 pass
202
f22b431f4113 Added arm add instruction
Windel Bouwman
parents: 201
diff changeset
154