annotate python/ppci/outstream.py @ 390:b77f3290ac79

Added diagram editor tests
author Windel Bouwman
date Fri, 16 May 2014 10:12:16 +0200
parents 6df89163e114
children
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
381
6df89163e114 Fix section and ldr pseudo instruction
Windel Bouwman
parents: 366
diff changeset
26 self.literal_pool = []
208
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
27
335
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 316
diff changeset
28 def emit(self, item):
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 316
diff changeset
29 """ Encode instruction and add symbol and relocation information """
341
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 337
diff changeset
30 assert isinstance(item, Instruction), str(item) + str(type(item))
335
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 316
diff changeset
31 assert self.currentSection
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 316
diff changeset
32 section = self.currentSection
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 316
diff changeset
33 address = self.currentSection.Size
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 316
diff changeset
34 b = item.encode()
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 316
diff changeset
35 syms = item.symbols()
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 316
diff changeset
36 relocs = item.relocations()
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 316
diff changeset
37 section.add_data(b)
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 316
diff changeset
38 for sym in syms:
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 316
diff changeset
39 self.obj_file.add_symbol(sym, address, section.name)
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 316
diff changeset
40 for sym, typ in relocs:
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 316
diff changeset
41 self.obj_file.add_relocation(sym, address, typ, section.name)
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 316
diff changeset
42 # Special case for align, TODO do this different?
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 316
diff changeset
43 if type(item) is Alignment:
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 316
diff changeset
44 while section.Size % item.align != 0:
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 316
diff changeset
45 section.add_data(bytes([0]))
236
8786811a5a59 Fix pcrel
Windel Bouwman
parents: 235
diff changeset
46
348
442fb043d149 Added log option to zcc
Windel Bouwman
parents: 346
diff changeset
47 def select_section(self, sname):
335
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 316
diff changeset
48 self.currentSection = self.obj_file.get_section(sname)
337
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
b00219172a42 Added cool lm3s811 qemu project
Windel Bouwman
parents: 335
diff changeset
51 class DummyOutputStream(OutputStream):
348
442fb043d149 Added log option to zcc
Windel Bouwman
parents: 346
diff changeset
52 """ Stream that implements the bare minimum and does nothing """
337
b00219172a42 Added cool lm3s811 qemu project
Windel Bouwman
parents: 335
diff changeset
53 def emit(self, item):
b00219172a42 Added cool lm3s811 qemu project
Windel Bouwman
parents: 335
diff changeset
54 pass
b00219172a42 Added cool lm3s811 qemu project
Windel Bouwman
parents: 335
diff changeset
55
348
442fb043d149 Added log option to zcc
Windel Bouwman
parents: 346
diff changeset
56 def select_section(self, sname):
337
b00219172a42 Added cool lm3s811 qemu project
Windel Bouwman
parents: 335
diff changeset
57 pass
348
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
442fb043d149 Added log option to zcc
Windel Bouwman
parents: 346
diff changeset
60 class LoggerOutputStream(OutputStream):
442fb043d149 Added log option to zcc
Windel Bouwman
parents: 346
diff changeset
61 """ Stream that emits instructions as text in the log """
442fb043d149 Added log option to zcc
Windel Bouwman
parents: 346
diff changeset
62 def __init__(self):
442fb043d149 Added log option to zcc
Windel Bouwman
parents: 346
diff changeset
63 self.logger = logging.getLogger('LoggerOutputStream')
442fb043d149 Added log option to zcc
Windel Bouwman
parents: 346
diff changeset
64
442fb043d149 Added log option to zcc
Windel Bouwman
parents: 346
diff changeset
65 def emit(self, item):
442fb043d149 Added log option to zcc
Windel Bouwman
parents: 346
diff changeset
66 self.logger.debug(str(item))
442fb043d149 Added log option to zcc
Windel Bouwman
parents: 346
diff changeset
67
442fb043d149 Added log option to zcc
Windel Bouwman
parents: 346
diff changeset
68 def select_section(self, sname):
442fb043d149 Added log option to zcc
Windel Bouwman
parents: 346
diff changeset
69 self.logger.debug('.section {}'.format(sname))
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
442fb043d149 Added log option to zcc
Windel Bouwman
parents: 346
diff changeset
72 class MasterOutputStream(OutputStream):
442fb043d149 Added log option to zcc
Windel Bouwman
parents: 346
diff changeset
73 """ Stream that emits to multiple sub streams """
366
39bf68bf1891 Fix sample tests and deterministic build
Windel Bouwman
parents: 348
diff changeset
74 def __init__(self, substreams=[]):
39bf68bf1891 Fix sample tests and deterministic build
Windel Bouwman
parents: 348
diff changeset
75 self.substreams = list(substreams) # Use copy constructor!!!
348
442fb043d149 Added log option to zcc
Windel Bouwman
parents: 346
diff changeset
76
442fb043d149 Added log option to zcc
Windel Bouwman
parents: 346
diff changeset
77 def add_substream(self, output_stream):
442fb043d149 Added log option to zcc
Windel Bouwman
parents: 346
diff changeset
78 self.substreams.append(output_stream)
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 def emit(self, item):
442fb043d149 Added log option to zcc
Windel Bouwman
parents: 346
diff changeset
81 for output_stream in self.substreams:
442fb043d149 Added log option to zcc
Windel Bouwman
parents: 346
diff changeset
82 output_stream.emit(item)
442fb043d149 Added log option to zcc
Windel Bouwman
parents: 346
diff changeset
83
442fb043d149 Added log option to zcc
Windel Bouwman
parents: 346
diff changeset
84 def select_section(self, sname):
442fb043d149 Added log option to zcc
Windel Bouwman
parents: 346
diff changeset
85 for output_stream in self.substreams:
442fb043d149 Added log option to zcc
Windel Bouwman
parents: 346
diff changeset
86 output_stream.select_section(sname)
381
6df89163e114 Fix section and ldr pseudo instruction
Windel Bouwman
parents: 366
diff changeset
87
6df89163e114 Fix section and ldr pseudo instruction
Windel Bouwman
parents: 366
diff changeset
88
6df89163e114 Fix section and ldr pseudo instruction
Windel Bouwman
parents: 366
diff changeset
89 def BinaryAndLoggingStream(output):
6df89163e114 Fix section and ldr pseudo instruction
Windel Bouwman
parents: 366
diff changeset
90 """ Create a stream object that both logs and writes to an object file """
6df89163e114 Fix section and ldr pseudo instruction
Windel Bouwman
parents: 366
diff changeset
91 o2 = BinaryOutputStream(output)
6df89163e114 Fix section and ldr pseudo instruction
Windel Bouwman
parents: 366
diff changeset
92 o1 = LoggerOutputStream()
6df89163e114 Fix section and ldr pseudo instruction
Windel Bouwman
parents: 366
diff changeset
93 ostream = MasterOutputStream([o1, o2])
6df89163e114 Fix section and ldr pseudo instruction
Windel Bouwman
parents: 366
diff changeset
94 return ostream