comparison 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
comparison
equal deleted inserted replaced
365:98ff43cfdd36 366:39bf68bf1891
1 import logging 1 import logging
2 import binascii 2 import binascii
3 from ppci.target import Instruction, Alignment 3 from .target import Instruction, Alignment
4 from ppci.objectfile import ObjectFile 4 from .objectfile import ObjectFile
5 5
6 """ 6 """
7 The output stream is a stream of instructions that can be output 7 The output stream is a stream of instructions that can be output
8 to a file or binary or hexfile. 8 to a file or binary or hexfile.
9 """ 9 """
10 10
11 11
12 class OutputStream: 12 class OutputStream:
13 """ Interface to generator code with. """
13 def emit(self, item): 14 def emit(self, item):
14 raise NotImplementedError('Abstract base class') 15 raise NotImplementedError('Abstract base class')
15 16
16 def select_section(self, sname): 17 def select_section(self, sname):
17 raise NotImplementedError('Abstract base class') 18 raise NotImplementedError('Abstract base class')
18
19
20 class OutputStreamWriter:
21 def __init__(self, extra_indent=''):
22 self.extra_indent = extra_indent
23
24 def dump(self, stream, f):
25 for s in sorted(stream.sections.keys()):
26 # print('.section '+ s)
27 self.dumpSection(stream.sections[s], f)
28
29 def dumpSection(self, s, f):
30 for i in s.instructions:
31 addr = i.address
32 insword = i.encode()
33 assert type(insword) is bytes
34 insword = binascii.hexlify(bytes(reversed(insword))).decode('ascii')
35 asm = str(i)
36 if len(insword) == 0:
37 print(' {}'.format(asm), file=f)
38 else:
39 print(' 0x{0:08x} 0x{1} {2}'.format(addr, insword, asm), file=f)
40 19
41 20
42 class BinaryOutputStream(OutputStream): 21 class BinaryOutputStream(OutputStream):
43 """ Output stream that writes to object file """ 22 """ Output stream that writes to object file """
44 def __init__(self, obj_file): 23 def __init__(self, obj_file):
89 self.logger.debug('.section {}'.format(sname)) 68 self.logger.debug('.section {}'.format(sname))
90 69
91 70
92 class MasterOutputStream(OutputStream): 71 class MasterOutputStream(OutputStream):
93 """ Stream that emits to multiple sub streams """ 72 """ Stream that emits to multiple sub streams """
94 def __init__(self): 73 def __init__(self, substreams=[]):
95 self.substreams = [] 74 self.substreams = list(substreams) # Use copy constructor!!!
96 75
97 def add_substream(self, output_stream): 76 def add_substream(self, output_stream):
98 self.substreams.append(output_stream) 77 self.substreams.append(output_stream)
99 78
100 def emit(self, item): 79 def emit(self, item):