annotate python/ppci/outstream.py @ 346:3bb7dcfe5529

expanded arm target
author Windel Bouwman
date Fri, 07 Mar 2014 17:05:32 +0100
parents 86b02c98a717
children 442fb043d149
rev   line source
208
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
1 import binascii
346
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 342
diff changeset
2 from ppci.target import Instruction, 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:
234
Windel Bouwman
parents: 225
diff changeset
30 addr = i.address
Windel Bouwman
parents: 225
diff changeset
31 insword = i.encode()
Windel Bouwman
parents: 225
diff changeset
32 assert type(insword) is bytes
Windel Bouwman
parents: 225
diff changeset
33 insword = binascii.hexlify(bytes(reversed(insword))).decode('ascii')
Windel Bouwman
parents: 225
diff changeset
34 asm = str(i)
235
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
35 if len(insword) == 0:
316
56e6ff84f646 Fixed burn led demo
Windel Bouwman
parents: 296
diff changeset
36 print(' {}'.format(asm), file=f)
235
ff40407c0240 Fix ALabel to Label
Windel Bouwman
parents: 234
diff changeset
37 else:
316
56e6ff84f646 Fixed burn led demo
Windel Bouwman
parents: 296
diff changeset
38 print(' 0x{0:08x} 0x{1} {2}'.format(addr, insword, asm), file=f)
208
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
39
296
9417caea2eb3 Directorized some backend files
Windel Bouwman
parents: 259
diff changeset
40
335
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 316
diff changeset
41 class BinaryOutputStream(OutputStream):
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 316
diff changeset
42 """ Output stream that writes to object file """
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 316
diff changeset
43 def __init__(self, obj_file):
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 316
diff changeset
44 super().__init__()
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 316
diff changeset
45 self.obj_file = obj_file
208
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
46
335
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 316
diff changeset
47 def emit(self, item):
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 316
diff changeset
48 """ Encode instruction and add symbol and relocation information """
341
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 337
diff changeset
49 assert isinstance(item, Instruction), str(item) + str(type(item))
335
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 316
diff changeset
50 assert self.currentSection
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 316
diff changeset
51 section = self.currentSection
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 316
diff changeset
52 address = self.currentSection.Size
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 316
diff changeset
53 b = item.encode()
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 316
diff changeset
54 syms = item.symbols()
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 316
diff changeset
55 relocs = item.relocations()
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 316
diff changeset
56 section.add_data(b)
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 316
diff changeset
57 for sym in syms:
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 316
diff changeset
58 self.obj_file.add_symbol(sym, address, section.name)
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 316
diff changeset
59 for sym, typ in relocs:
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 316
diff changeset
60 self.obj_file.add_relocation(sym, address, typ, section.name)
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 316
diff changeset
61 # Special case for align, TODO do this different?
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 316
diff changeset
62 if type(item) is Alignment:
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 316
diff changeset
63 while section.Size % item.align != 0:
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 316
diff changeset
64 section.add_data(bytes([0]))
236
8786811a5a59 Fix pcrel
Windel Bouwman
parents: 235
diff changeset
65
335
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 316
diff changeset
66 def selectSection(self, sname):
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 316
diff changeset
67 self.currentSection = self.obj_file.get_section(sname)
337
b00219172a42 Added cool lm3s811 qemu project
Windel Bouwman
parents: 335
diff changeset
68
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 class DummyOutputStream(OutputStream):
b00219172a42 Added cool lm3s811 qemu project
Windel Bouwman
parents: 335
diff changeset
71 def emit(self, item):
b00219172a42 Added cool lm3s811 qemu project
Windel Bouwman
parents: 335
diff changeset
72 pass
b00219172a42 Added cool lm3s811 qemu project
Windel Bouwman
parents: 335
diff changeset
73
b00219172a42 Added cool lm3s811 qemu project
Windel Bouwman
parents: 335
diff changeset
74 def selectSection(self, sname):
b00219172a42 Added cool lm3s811 qemu project
Windel Bouwman
parents: 335
diff changeset
75 pass