annotate python/ppci/outstream.py @ 342:86b02c98a717 devel

Moved target directory
author Windel Bouwman
date Sat, 01 Mar 2014 15:40:31 +0100
parents python/outstream.py@4d204f6f7d4e
children 3bb7dcfe5529
rev   line source
208
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
1 import binascii
342
86b02c98a717 Moved target directory
Windel Bouwman
parents: 341
diff changeset
2 from ppci.target import Instruction, DebugInfo, Alignment
335
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 316
diff changeset
3 from ppci.objectfile import ObjectFile
234
Windel Bouwman
parents: 225
diff changeset
4
208
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
5 """
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
6 The output stream is a stream of instructions that can be output
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
7 to a file or binary or hexfile.
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
8 """
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
9
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 208
diff changeset
10
335
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 316
diff changeset
11 class OutputStream:
237
81752b0f85a5 Added burn led test program
Windel Bouwman
parents: 236
diff changeset
12 def emit(self, item):
335
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 316
diff changeset
13 raise NotImplementedError('Abstract base class')
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 208
diff changeset
14
250
f5fba5b554d7 Removal of obsolete editor
Windel Bouwman
parents: 249
diff changeset
15 def selectSection(self, sname):
335
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 316
diff changeset
16 raise NotImplementedError('Abstract base class')
234
Windel Bouwman
parents: 225
diff changeset
17
208
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
18
316
56e6ff84f646 Fixed burn led demo
Windel Bouwman
parents: 296
diff changeset
19 class OutputStreamWriter:
56e6ff84f646 Fixed burn led demo
Windel Bouwman
parents: 296
diff changeset
20 def __init__(self, extra_indent=''):
56e6ff84f646 Fixed burn led demo
Windel Bouwman
parents: 296
diff changeset
21 self.extra_indent = extra_indent
208
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
22
316
56e6ff84f646 Fixed burn led demo
Windel Bouwman
parents: 296
diff changeset
23 def dump(self, stream, f):
56e6ff84f646 Fixed burn led demo
Windel Bouwman
parents: 296
diff changeset
24 for s in sorted(stream.sections.keys()):
56e6ff84f646 Fixed burn led demo
Windel Bouwman
parents: 296
diff changeset
25 # print('.section '+ s)
56e6ff84f646 Fixed burn led demo
Windel Bouwman
parents: 296
diff changeset
26 self.dumpSection(stream.sections[s], f)
56e6ff84f646 Fixed burn led demo
Windel Bouwman
parents: 296
diff changeset
27
56e6ff84f646 Fixed burn led demo
Windel Bouwman
parents: 296
diff changeset
28 def dumpSection(self, s, f):
56e6ff84f646 Fixed burn led demo
Windel Bouwman
parents: 296
diff changeset
29 for i in s.instructions:
259
ac603eb66b63 Added function call
Windel Bouwman
parents: 258
diff changeset
30 if type(i) is DebugInfo:
ac603eb66b63 Added function call
Windel Bouwman
parents: 258
diff changeset
31 continue
234
Windel Bouwman
parents: 225
diff changeset
32 addr = i.address
Windel Bouwman
parents: 225
diff changeset
33 insword = i.encode()
Windel Bouwman
parents: 225
diff changeset
34 assert type(insword) is bytes
Windel Bouwman
parents: 225
diff changeset
35 insword = binascii.hexlify(bytes(reversed(insword))).decode('ascii')
Windel Bouwman
parents: 225
diff changeset
36 asm = str(i)
235
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
37 if len(insword) == 0:
316
56e6ff84f646 Fixed burn led demo
Windel Bouwman
parents: 296
diff changeset
38 print(' {}'.format(asm), file=f)
235
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
39 else:
316
56e6ff84f646 Fixed burn led demo
Windel Bouwman
parents: 296
diff changeset
40 print(' 0x{0:08x} 0x{1} {2}'.format(addr, insword, asm), file=f)
208
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
41
296
9417caea2eb3 Directorized some backend files
Windel Bouwman
parents: 259
diff changeset
42
335
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 316
diff changeset
43 class BinaryOutputStream(OutputStream):
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 316
diff changeset
44 """ Output stream that writes to object file """
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 316
diff changeset
45 def __init__(self, obj_file):
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 316
diff changeset
46 super().__init__()
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 316
diff changeset
47 self.obj_file = obj_file
208
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
48
335
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 316
diff changeset
49 def emit(self, item):
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 316
diff changeset
50 """ Encode instruction and add symbol and relocation information """
341
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 337
diff changeset
51 assert isinstance(item, Instruction), str(item) + str(type(item))
335
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 316
diff changeset
52 assert self.currentSection
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 316
diff changeset
53 section = self.currentSection
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 316
diff changeset
54 address = self.currentSection.Size
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 316
diff changeset
55 b = item.encode()
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 316
diff changeset
56 syms = item.symbols()
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 316
diff changeset
57 relocs = item.relocations()
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 316
diff changeset
58 section.add_data(b)
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 316
diff changeset
59 for sym in syms:
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 316
diff changeset
60 self.obj_file.add_symbol(sym, address, section.name)
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 316
diff changeset
61 for sym, typ in relocs:
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 316
diff changeset
62 self.obj_file.add_relocation(sym, address, typ, section.name)
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 316
diff changeset
63 # Special case for align, TODO do this different?
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 316
diff changeset
64 if type(item) is Alignment:
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 316
diff changeset
65 while section.Size % item.align != 0:
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 316
diff changeset
66 section.add_data(bytes([0]))
236
8786811a5a59 Fix pcrel
Windel Bouwman
parents: 235
diff changeset
67
335
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 316
diff changeset
68 def selectSection(self, sname):
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 316
diff changeset
69 self.currentSection = self.obj_file.get_section(sname)
337
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
b00219172a42 Added cool lm3s811 qemu project
Windel Bouwman
parents: 335
diff changeset
72 class DummyOutputStream(OutputStream):
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
b00219172a42 Added cool lm3s811 qemu project
Windel Bouwman
parents: 335
diff changeset
76 def selectSection(self, sname):
b00219172a42 Added cool lm3s811 qemu project
Windel Bouwman
parents: 335
diff changeset
77 pass