annotate python/ppci/target/basetarget.py @ 362:c05ab629976a

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