annotate python/ppci/outstream.py @ 365:98ff43cfdd36

Nasty bug in adr instruction
author Windel Bouwman
date Wed, 19 Mar 2014 22:32:04 +0100
parents 442fb043d149
children 39bf68bf1891
rev   line source
348
442fb043d149 Added log option to zcc
Windel Bouwman
parents: 346
diff changeset
1 import logging
208
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
2 import binascii
346
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 342
diff changeset
3 from ppci.target import Instruction, Alignment
335
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 316
diff changeset
4 from ppci.objectfile import ObjectFile
234
Windel Bouwman
parents: 225
diff changeset
5
208
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
6 """
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
7 The output stream is a stream of instructions that can be output
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
8 to a file or binary or hexfile.
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
9 """
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
10
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 208
diff changeset
11
335
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 316
diff changeset
12 class OutputStream:
237
81752b0f85a5 Added burn led test program
Windel Bouwman
parents: 236
diff changeset
13 def emit(self, item):
335
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 316
diff changeset
14 raise NotImplementedError('Abstract base class')
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 208
diff changeset
15
348
442fb043d149 Added log option to zcc
Windel Bouwman
parents: 346
diff changeset
16 def select_section(self, sname):
335
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 316
diff changeset
17 raise NotImplementedError('Abstract base class')
234
Windel Bouwman
parents: 225
diff changeset
18
208
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
19
316
56e6ff84f646 Fixed burn led demo
Windel Bouwman
parents: 296
diff changeset
20 class OutputStreamWriter:
56e6ff84f646 Fixed burn led demo
Windel Bouwman
parents: 296
diff changeset
21 def __init__(self, extra_indent=''):
56e6ff84f646 Fixed burn led demo
Windel Bouwman
parents: 296
diff changeset
22 self.extra_indent = extra_indent
208
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
23
316
56e6ff84f646 Fixed burn led demo
Windel Bouwman
parents: 296
diff changeset
24 def dump(self, stream, f):
56e6ff84f646 Fixed burn led demo
Windel Bouwman
parents: 296
diff changeset
25 for s in sorted(stream.sections.keys()):
56e6ff84f646 Fixed burn led demo
Windel Bouwman
parents: 296
diff changeset
26 # print('.section '+ s)
56e6ff84f646 Fixed burn led demo
Windel Bouwman
parents: 296
diff changeset
27 self.dumpSection(stream.sections[s], f)
56e6ff84f646 Fixed burn led demo
Windel Bouwman
parents: 296
diff changeset
28
56e6ff84f646 Fixed burn led demo
Windel Bouwman
parents: 296
diff changeset
29 def dumpSection(self, s, f):
56e6ff84f646 Fixed burn led demo
Windel Bouwman
parents: 296
diff changeset
30 for i in s.instructions:
234
Windel Bouwman
parents: 225
diff changeset
31 addr = i.address
Windel Bouwman
parents: 225
diff changeset
32 insword = i.encode()
Windel Bouwman
parents: 225
diff changeset
33 assert type(insword) is bytes
Windel Bouwman
parents: 225
diff changeset
34 insword = binascii.hexlify(bytes(reversed(insword))).decode('ascii')
Windel Bouwman
parents: 225
diff changeset
35 asm = str(i)
235
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
36 if len(insword) == 0:
316
56e6ff84f646 Fixed burn led demo
Windel Bouwman
parents: 296
diff changeset
37 print(' {}'.format(asm), file=f)
235
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
38 else:
316
56e6ff84f646 Fixed burn led demo
Windel Bouwman
parents: 296
diff changeset
39 print(' 0x{0:08x} 0x{1} {2}'.format(addr, insword, asm), file=f)
208
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
40
296
9417caea2eb3 Directorized some backend files
Windel Bouwman
parents: 259
diff changeset
41
335
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 316
diff changeset
42 class BinaryOutputStream(OutputStream):
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 316
diff changeset
43 """ Output stream that writes to object file """
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 316
diff changeset
44 def __init__(self, obj_file):
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 316
diff changeset
45 super().__init__()
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 316
diff changeset
46 self.obj_file = obj_file
208
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
47
335
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 316
diff changeset
48 def emit(self, item):
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 316
diff changeset
49 """ Encode instruction and add symbol and relocation information """
341
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 337
diff changeset
50 assert isinstance(item, Instruction), str(item) + str(type(item))
335
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 316
diff changeset
51 assert self.currentSection
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 316
diff changeset
52 section = self.currentSection
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 316
diff changeset
53 address = self.currentSection.Size
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 316
diff changeset
54 b = item.encode()
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 316
diff changeset
55 syms = item.symbols()
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 316
diff changeset
56 relocs = item.relocations()
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 316
diff changeset
57 section.add_data(b)
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 316
diff changeset
58 for sym in syms:
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 316
diff changeset
59 self.obj_file.add_symbol(sym, address, section.name)
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 316
diff changeset
60 for sym, typ in relocs:
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 316
diff changeset
61 self.obj_file.add_relocation(sym, address, typ, section.name)
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 316
diff changeset
62 # Special case for align, TODO do this different?
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 316
diff changeset
63 if type(item) is Alignment:
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 316
diff changeset
64 while section.Size % item.align != 0:
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 316
diff changeset
65 section.add_data(bytes([0]))
236
8786811a5a59 Fix pcrel
Windel Bouwman
parents: 235
diff changeset
66
348
442fb043d149 Added log option to zcc
Windel Bouwman
parents: 346
diff changeset
67 def select_section(self, sname):
335
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 316
diff changeset
68 self.currentSection = self.obj_file.get_section(sname)
337
b00219172a42 Added cool lm3s811 qemu project
Windel Bouwman
parents: 335
diff changeset
69
b00219172a42 Added cool lm3s811 qemu project
Windel Bouwman
parents: 335
diff changeset
70
b00219172a42 Added cool lm3s811 qemu project
Windel Bouwman
parents: 335
diff changeset
71 class DummyOutputStream(OutputStream):
348
442fb043d149 Added log option to zcc
Windel Bouwman
parents: 346
diff changeset
72 """ Stream that implements the bare minimum and does nothing """
337
b00219172a42 Added cool lm3s811 qemu project
Windel Bouwman
parents: 335
diff changeset
73 def emit(self, item):
b00219172a42 Added cool lm3s811 qemu project
Windel Bouwman
parents: 335
diff changeset
74 pass
b00219172a42 Added cool lm3s811 qemu project
Windel Bouwman
parents: 335
diff changeset
75
348
442fb043d149 Added log option to zcc
Windel Bouwman
parents: 346
diff changeset
76 def select_section(self, sname):
337
b00219172a42 Added cool lm3s811 qemu project
Windel Bouwman
parents: 335
diff changeset
77 pass
348
442fb043d149 Added log option to zcc
Windel Bouwman
parents: 346
diff changeset
78
442fb043d149 Added log option to zcc
Windel Bouwman
parents: 346
diff changeset
79
442fb043d149 Added log option to zcc
Windel Bouwman
parents: 346
diff changeset
80 class LoggerOutputStream(OutputStream):
442fb043d149 Added log option to zcc
Windel Bouwman
parents: 346
diff changeset
81 """ Stream that emits instructions as text in the log """
442fb043d149 Added log option to zcc
Windel Bouwman
parents: 346
diff changeset
82 def __init__(self):
442fb043d149 Added log option to zcc
Windel Bouwman
parents: 346
diff changeset
83 self.logger = logging.getLogger('LoggerOutputStream')
442fb043d149 Added log option to zcc
Windel Bouwman
parents: 346
diff changeset
84
442fb043d149 Added log option to zcc
Windel Bouwman
parents: 346
diff changeset
85 def emit(self, item):
442fb043d149 Added log option to zcc
Windel Bouwman
parents: 346
diff changeset
86 self.logger.debug(str(item))
442fb043d149 Added log option to zcc
Windel Bouwman
parents: 346
diff changeset
87
442fb043d149 Added log option to zcc
Windel Bouwman
parents: 346
diff changeset
88 def select_section(self, sname):
442fb043d149 Added log option to zcc
Windel Bouwman
parents: 346
diff changeset
89 self.logger.debug('.section {}'.format(sname))
442fb043d149 Added log option to zcc
Windel Bouwman
parents: 346
diff changeset
90
442fb043d149 Added log option to zcc
Windel Bouwman
parents: 346
diff changeset
91
442fb043d149 Added log option to zcc
Windel Bouwman
parents: 346
diff changeset
92 class MasterOutputStream(OutputStream):
442fb043d149 Added log option to zcc
Windel Bouwman
parents: 346
diff changeset
93 """ Stream that emits to multiple sub streams """
442fb043d149 Added log option to zcc
Windel Bouwman
parents: 346
diff changeset
94 def __init__(self):
442fb043d149 Added log option to zcc
Windel Bouwman
parents: 346
diff changeset
95 self.substreams = []
442fb043d149 Added log option to zcc
Windel Bouwman
parents: 346
diff changeset
96
442fb043d149 Added log option to zcc
Windel Bouwman
parents: 346
diff changeset
97 def add_substream(self, output_stream):
442fb043d149 Added log option to zcc
Windel Bouwman
parents: 346
diff changeset
98 self.substreams.append(output_stream)
442fb043d149 Added log option to zcc
Windel Bouwman
parents: 346
diff changeset
99
442fb043d149 Added log option to zcc
Windel Bouwman
parents: 346
diff changeset
100 def emit(self, item):
442fb043d149 Added log option to zcc
Windel Bouwman
parents: 346
diff changeset
101 for output_stream in self.substreams:
442fb043d149 Added log option to zcc
Windel Bouwman
parents: 346
diff changeset
102 output_stream.emit(item)
442fb043d149 Added log option to zcc
Windel Bouwman
parents: 346
diff changeset
103
442fb043d149 Added log option to zcc
Windel Bouwman
parents: 346
diff changeset
104 def select_section(self, sname):
442fb043d149 Added log option to zcc
Windel Bouwman
parents: 346
diff changeset
105 for output_stream in self.substreams:
442fb043d149 Added log option to zcc
Windel Bouwman
parents: 346
diff changeset
106 output_stream.select_section(sname)