annotate python/ppci/target/basetarget.py @ 381:6df89163e114

Fix section and ldr pseudo instruction
author Windel Bouwman
date Sat, 26 Apr 2014 17:41:56 +0200
parents 39bf68bf1891
children d056b552d3f4
rev   line source
362
c05ab629976a Added CPUID for arm
Windel Bouwman
parents: 354
diff changeset
1 import types
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
234
Windel Bouwman
parents: 219
diff changeset
8 class Instruction:
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
9 """ Base instruction class """
234
Windel Bouwman
parents: 219
diff changeset
10 def encode(self):
335
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
11 return bytes()
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
12
335
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
13 def relocations(self):
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
14 return []
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
15
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
16 def symbols(self):
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
17 return []
234
Windel Bouwman
parents: 219
diff changeset
18
381
6df89163e114 Fix section and ldr pseudo instruction
Windel Bouwman
parents: 366
diff changeset
19 def literals(self, add_literal):
6df89163e114 Fix section and ldr pseudo instruction
Windel Bouwman
parents: 366
diff changeset
20 pass
6df89163e114 Fix section and ldr pseudo instruction
Windel Bouwman
parents: 366
diff changeset
21
234
Windel Bouwman
parents: 219
diff changeset
22
280
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 277
diff changeset
23 class Nop(Instruction):
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 277
diff changeset
24 """ Instruction that does nothing and has zero size """
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 277
diff changeset
25 def encode(self):
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 277
diff changeset
26 return bytes()
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 277
diff changeset
27
354
5477e499b039 Added some sort of string functionality
Windel Bouwman
parents: 346
diff changeset
28 def __repr__(self):
5477e499b039 Added some sort of string functionality
Windel Bouwman
parents: 346
diff changeset
29 return 'NOP'
5477e499b039 Added some sort of string functionality
Windel Bouwman
parents: 346
diff changeset
30
280
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 277
diff changeset
31
234
Windel Bouwman
parents: 219
diff changeset
32 class PseudoInstruction(Instruction):
Windel Bouwman
parents: 219
diff changeset
33 pass
Windel Bouwman
parents: 219
diff changeset
34
Windel Bouwman
parents: 219
diff changeset
35
Windel Bouwman
parents: 219
diff changeset
36 class Label(PseudoInstruction):
206
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
37 def __init__(self, name):
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
38 self.name = name
6c6bf8890d8a Added push and pop encodings
Windel Bouwman
parents: 205
diff changeset
39
235
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
40 def __repr__(self):
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
41 return '{}:'.format(self.name)
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
42
335
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
43 def symbols(self):
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
44 return [self.name]
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 Comment(PseudoInstruction):
Windel Bouwman
parents: 219
diff changeset
48 def __init__(self, txt):
Windel Bouwman
parents: 219
diff changeset
49 self.txt = txt
249
e41e4109addd Added current position arrow
Windel Bouwman
parents: 236
diff changeset
50
235
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
51 def encode(self):
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
52 return bytes()
249
e41e4109addd Added current position arrow
Windel Bouwman
parents: 236
diff changeset
53
234
Windel Bouwman
parents: 219
diff changeset
54 def __repr__(self):
Windel Bouwman
parents: 219
diff changeset
55 return '; {}'.format(self.txt)
Windel Bouwman
parents: 219
diff changeset
56
235
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
57
234
Windel Bouwman
parents: 219
diff changeset
58 class Alignment(PseudoInstruction):
Windel Bouwman
parents: 219
diff changeset
59 def __init__(self, a):
Windel Bouwman
parents: 219
diff changeset
60 self.align = a
235
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
61
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
62 def __repr__(self):
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
63 return 'ALIGN({})'.format(self.align)
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
64
234
Windel Bouwman
parents: 219
diff changeset
65 def encode(self):
Windel Bouwman
parents: 219
diff changeset
66 pad = []
335
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
67 # TODO
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
68 address = 0
235
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
69 while (address % self.align) != 0:
234
Windel Bouwman
parents: 219
diff changeset
70 address += 1
Windel Bouwman
parents: 219
diff changeset
71 pad.append(0)
Windel Bouwman
parents: 219
diff changeset
72 return bytes(pad)
Windel Bouwman
parents: 219
diff changeset
73
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
74
346
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
75 class Register:
199
a690473b79e2 Added msp430 target
Windel Bouwman
parents:
diff changeset
76 def __init__(self, name):
a690473b79e2 Added msp430 target
Windel Bouwman
parents:
diff changeset
77 self.name = name
a690473b79e2 Added msp430 target
Windel Bouwman
parents:
diff changeset
78
366
39bf68bf1891 Fix sample tests and deterministic build
Windel Bouwman
parents: 364
diff changeset
79 def __gt__(self, other):
39bf68bf1891 Fix sample tests and deterministic build
Windel Bouwman
parents: 364
diff changeset
80 return self.num > other.num
39bf68bf1891 Fix sample tests and deterministic build
Windel Bouwman
parents: 364
diff changeset
81
199
a690473b79e2 Added msp430 target
Windel Bouwman
parents:
diff changeset
82
364
c49459768aaa Work on globals
Windel Bouwman
parents: 363
diff changeset
83 class LabelAddress:
c49459768aaa Work on globals
Windel Bouwman
parents: 363
diff changeset
84 def __init__(self, name):
c49459768aaa Work on globals
Windel Bouwman
parents: 363
diff changeset
85 self.name = name
c49459768aaa Work on globals
Windel Bouwman
parents: 363
diff changeset
86
c49459768aaa Work on globals
Windel Bouwman
parents: 363
diff changeset
87
199
a690473b79e2 Added msp430 target
Windel Bouwman
parents:
diff changeset
88 class Target:
201
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
89 def __init__(self, name, desc=''):
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
90 self.name = name
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
91 self.desc = desc
200
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
92 self.registers = []
341
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 335
diff changeset
93 self.byte_sizes = {'int' : 4} # For front end!
354
5477e499b039 Added some sort of string functionality
Windel Bouwman
parents: 346
diff changeset
94 self.byte_sizes['byte'] = 1
341
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 335
diff changeset
95
346
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
96 # For lowering:
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
97 self.lower_functions = {}
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
98
341
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 335
diff changeset
99 # For assembler:
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 335
diff changeset
100 self.assembler_rules = []
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 335
diff changeset
101 self.asm_keywords = []
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 335
diff changeset
102
346
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
103 self.generate_base_rules()
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
104
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
105 def generate_base_rules(self):
345
b4882ff0ed06 Added more arm isa tests
Windel Bouwman
parents: 342
diff changeset
106 # Base rules for constants:
b4882ff0ed06 Added more arm isa tests
Windel Bouwman
parents: 342
diff changeset
107 self.add_rule('imm32', ['val32'], lambda x: x[0].val)
b4882ff0ed06 Added more arm isa tests
Windel Bouwman
parents: 342
diff changeset
108 self.add_rule('imm32', ['imm16'], lambda x: x[0])
b4882ff0ed06 Added more arm isa tests
Windel Bouwman
parents: 342
diff changeset
109
b4882ff0ed06 Added more arm isa tests
Windel Bouwman
parents: 342
diff changeset
110 self.add_rule('imm16', ['val16'], lambda x: x[0].val)
b4882ff0ed06 Added more arm isa tests
Windel Bouwman
parents: 342
diff changeset
111 self.add_rule('imm16', ['imm12'], lambda x: x[0])
b4882ff0ed06 Added more arm isa tests
Windel Bouwman
parents: 342
diff changeset
112
b4882ff0ed06 Added more arm isa tests
Windel Bouwman
parents: 342
diff changeset
113 self.add_rule('imm12', ['val12'], lambda x: x[0].val)
b4882ff0ed06 Added more arm isa tests
Windel Bouwman
parents: 342
diff changeset
114 self.add_rule('imm12', ['imm8'], lambda x: x[0])
b4882ff0ed06 Added more arm isa tests
Windel Bouwman
parents: 342
diff changeset
115
b4882ff0ed06 Added more arm isa tests
Windel Bouwman
parents: 342
diff changeset
116 self.add_rule('imm8', ['val8'], lambda x: x[0].val)
b4882ff0ed06 Added more arm isa tests
Windel Bouwman
parents: 342
diff changeset
117 self.add_rule('imm8', ['imm5'], lambda x: x[0])
b4882ff0ed06 Added more arm isa tests
Windel Bouwman
parents: 342
diff changeset
118
b4882ff0ed06 Added more arm isa tests
Windel Bouwman
parents: 342
diff changeset
119 self.add_rule('imm5', ['val5'], lambda x: x[0].val)
b4882ff0ed06 Added more arm isa tests
Windel Bouwman
parents: 342
diff changeset
120 self.add_rule('imm5', ['imm3'], lambda x: x[0])
b4882ff0ed06 Added more arm isa tests
Windel Bouwman
parents: 342
diff changeset
121
b4882ff0ed06 Added more arm isa tests
Windel Bouwman
parents: 342
diff changeset
122 self.add_rule('imm3', ['val3'], lambda x: x[0].val)
b4882ff0ed06 Added more arm isa tests
Windel Bouwman
parents: 342
diff changeset
123
341
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 335
diff changeset
124 def add_keyword(self, kw):
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 335
diff changeset
125 self.asm_keywords.append(kw)
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 335
diff changeset
126
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 335
diff changeset
127 def add_instruction(self, rhs, f):
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 335
diff changeset
128 self.add_rule('instruction', rhs, f)
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 335
diff changeset
129
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 335
diff changeset
130 def add_rule(self, lhs, rhs, f):
362
c05ab629976a Added CPUID for arm
Windel Bouwman
parents: 354
diff changeset
131 if type(f) is int:
c05ab629976a Added CPUID for arm
Windel Bouwman
parents: 354
diff changeset
132 f2 = lambda x: f
c05ab629976a Added CPUID for arm
Windel Bouwman
parents: 354
diff changeset
133 else:
c05ab629976a Added CPUID for arm
Windel Bouwman
parents: 354
diff changeset
134 f2 = f
c05ab629976a Added CPUID for arm
Windel Bouwman
parents: 354
diff changeset
135 assert type(f2) is types.FunctionType
c05ab629976a Added CPUID for arm
Windel Bouwman
parents: 354
diff changeset
136 self.assembler_rules.append((lhs, rhs, f2))
200
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
137
346
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
138 def lower_frame_to_stream(self, frame, outs):
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
139 """ Lower instructions from frame to output stream """
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
140 for im in frame.instructions:
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
141 if isinstance(im.assem, Instruction):
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
142 outs.emit(im.assem)
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
143 else:
381
6df89163e114 Fix section and ldr pseudo instruction
Windel Bouwman
parents: 366
diff changeset
144 # TODO assert isinstance(Abs
346
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
145 ins = self.lower_functions[im.assem](im)
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
146 outs.emit(ins)
201
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
147
346
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
148 def add_lowering(self, cls, f):
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
149 """ Add a function to the table of lowering options for this target """
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 345
diff changeset
150 self.lower_functions[cls] = f