annotate python/ppci/target/basetarget.py @ 360:42343d189e14

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