annotate python/ppci/outstream.py @ 366:39bf68bf1891

Fix sample tests and deterministic build
author Windel Bouwman
date Fri, 21 Mar 2014 09:43:01 +0100
parents 442fb043d149
children 6df89163e114
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
366
39bf68bf1891 Fix sample tests and deterministic build
Windel Bouwman
parents: 348
diff changeset
3 from .target import Instruction, Alignment
39bf68bf1891 Fix sample tests and deterministic build
Windel Bouwman
parents: 348
diff changeset
4 from .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:
366
39bf68bf1891 Fix sample tests and deterministic build
Windel Bouwman
parents: 348
diff changeset
13 """ Interface to generator code with. """
237
81752b0f85a5 Added burn led test program
Windel Bouwman
parents: 236
diff changeset
14 def emit(self, item):
335
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 316
diff changeset
15 raise NotImplementedError('Abstract base class')
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 208
diff changeset
16
348
442fb043d149 Added log option to zcc
Windel Bouwman
parents: 346
diff changeset
17 def select_section(self, sname):
335
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 316
diff changeset
18 raise NotImplementedError('Abstract base class')
234
Windel Bouwman
parents: 225
diff changeset
19
208
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
20
335
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 316
diff changeset
21 class BinaryOutputStream(OutputStream):
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 316
diff changeset
22 """ Output stream that writes to object file """
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 316
diff changeset
23 def __init__(self, obj_file):
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 316
diff changeset
24 super().__init__()
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 316
diff changeset
25 self.obj_file = obj_file
208
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
26
335
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 316
diff changeset
27 def emit(self, item):
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 316
diff changeset
28 """ Encode instruction and add symbol and relocation information """
341
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 337
diff changeset
29 assert isinstance(item, Instruction), str(item) + str(type(item))
335
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 316
diff changeset
30 assert self.currentSection
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 316
diff changeset
31 section = self.currentSection
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 316
diff changeset
32 address = self.currentSection.Size
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 316
diff changeset
33 b = item.encode()
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 316
diff changeset
34 syms = item.symbols()
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 316
diff changeset
35 relocs = item.relocations()
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 316
diff changeset
36 section.add_data(b)
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 316
diff changeset
37 for sym in syms:
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 316
diff changeset
38 self.obj_file.add_symbol(sym, address, section.name)
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 316
diff changeset
39 for sym, typ in relocs:
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 316
diff changeset
40 self.obj_file.add_relocation(sym, address, typ, section.name)
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 316
diff changeset
41 # Special case for align, TODO do this different?
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 316
diff changeset
42 if type(item) is Alignment:
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 316
diff changeset
43 while section.Size % item.align != 0:
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 316
diff changeset
44 section.add_data(bytes([0]))
236
8786811a5a59 Fix pcrel
Windel Bouwman
parents: 235
diff changeset
45
348
442fb043d149 Added log option to zcc
Windel Bouwman
parents: 346
diff changeset
46 def select_section(self, sname):
335
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 316
diff changeset
47 self.currentSection = self.obj_file.get_section(sname)
337
b00219172a42 Added cool lm3s811 qemu project
Windel Bouwman
parents: 335
diff changeset
48
b00219172a42 Added cool lm3s811 qemu project
Windel Bouwman
parents: 335
diff changeset
49
b00219172a42 Added cool lm3s811 qemu project
Windel Bouwman
parents: 335
diff changeset
50 class DummyOutputStream(OutputStream):
348
442fb043d149 Added log option to zcc
Windel Bouwman
parents: 346
diff changeset
51 """ Stream that implements the bare minimum and does nothing """
337
b00219172a42 Added cool lm3s811 qemu project
Windel Bouwman
parents: 335
diff changeset
52 def emit(self, item):
b00219172a42 Added cool lm3s811 qemu project
Windel Bouwman
parents: 335
diff changeset
53 pass
b00219172a42 Added cool lm3s811 qemu project
Windel Bouwman
parents: 335
diff changeset
54
348
442fb043d149 Added log option to zcc
Windel Bouwman
parents: 346
diff changeset
55 def select_section(self, sname):
337
b00219172a42 Added cool lm3s811 qemu project
Windel Bouwman
parents: 335
diff changeset
56 pass
348
442fb043d149 Added log option to zcc
Windel Bouwman
parents: 346
diff changeset
57
442fb043d149 Added log option to zcc
Windel Bouwman
parents: 346
diff changeset
58
442fb043d149 Added log option to zcc
Windel Bouwman
parents: 346
diff changeset
59 class LoggerOutputStream(OutputStream):
442fb043d149 Added log option to zcc
Windel Bouwman
parents: 346
diff changeset
60 """ Stream that emits instructions as text in the log """
442fb043d149 Added log option to zcc
Windel Bouwman
parents: 346
diff changeset
61 def __init__(self):
442fb043d149 Added log option to zcc
Windel Bouwman
parents: 346
diff changeset
62 self.logger = logging.getLogger('LoggerOutputStream')
442fb043d149 Added log option to zcc
Windel Bouwman
parents: 346
diff changeset
63
442fb043d149 Added log option to zcc
Windel Bouwman
parents: 346
diff changeset
64 def emit(self, item):
442fb043d149 Added log option to zcc
Windel Bouwman
parents: 346
diff changeset
65 self.logger.debug(str(item))
442fb043d149 Added log option to zcc
Windel Bouwman
parents: 346
diff changeset
66
442fb043d149 Added log option to zcc
Windel Bouwman
parents: 346
diff changeset
67 def select_section(self, sname):
442fb043d149 Added log option to zcc
Windel Bouwman
parents: 346
diff changeset
68 self.logger.debug('.section {}'.format(sname))
442fb043d149 Added log option to zcc
Windel Bouwman
parents: 346
diff changeset
69
442fb043d149 Added log option to zcc
Windel Bouwman
parents: 346
diff changeset
70
442fb043d149 Added log option to zcc
Windel Bouwman
parents: 346
diff changeset
71 class MasterOutputStream(OutputStream):
442fb043d149 Added log option to zcc
Windel Bouwman
parents: 346
diff changeset
72 """ Stream that emits to multiple sub streams """
366
39bf68bf1891 Fix sample tests and deterministic build
Windel Bouwman
parents: 348
diff changeset
73 def __init__(self, substreams=[]):
39bf68bf1891 Fix sample tests and deterministic build
Windel Bouwman
parents: 348
diff changeset
74 self.substreams = list(substreams) # Use copy constructor!!!
348
442fb043d149 Added log option to zcc
Windel Bouwman
parents: 346
diff changeset
75
442fb043d149 Added log option to zcc
Windel Bouwman
parents: 346
diff changeset
76 def add_substream(self, output_stream):
442fb043d149 Added log option to zcc
Windel Bouwman
parents: 346
diff changeset
77 self.substreams.append(output_stream)
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 def emit(self, item):
442fb043d149 Added log option to zcc
Windel Bouwman
parents: 346
diff changeset
80 for output_stream in self.substreams:
442fb043d149 Added log option to zcc
Windel Bouwman
parents: 346
diff changeset
81 output_stream.emit(item)
442fb043d149 Added log option to zcc
Windel Bouwman
parents: 346
diff changeset
82
442fb043d149 Added log option to zcc
Windel Bouwman
parents: 346
diff changeset
83 def select_section(self, sname):
442fb043d149 Added log option to zcc
Windel Bouwman
parents: 346
diff changeset
84 for output_stream in self.substreams:
442fb043d149 Added log option to zcc
Windel Bouwman
parents: 346
diff changeset
85 output_stream.select_section(sname)