annotate python/ppci/target/basetarget.py @ 348:442fb043d149

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