annotate python/outstream.py @ 234:83781bd10fdb

wip
author Windel Bouwman
date Sun, 14 Jul 2013 19:29:21 +0200
parents 1c7364bd74c7
children ff40407c0240
rev   line source
208
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
1 import binascii
234
Windel Bouwman
parents: 225
diff changeset
2 from target import Instruction, Label
Windel Bouwman
parents: 225
diff changeset
3
208
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
4 """
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
5 The output stream is a stream of instructions that can be output
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
6 to a file or binary or hexfile.
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
7 """
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
8
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 208
diff changeset
9
208
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
10 class OutputStream:
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
11 def __init__(self):
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
12 self.sections = {}
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
13 self.currentSection = None
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 219
diff changeset
14
208
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
15 def emit(self, item):
234
Windel Bouwman
parents: 225
diff changeset
16 assert isinstance(item, Instruction)
208
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
17 self.sections[self.currentSection].append(item)
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 208
diff changeset
18
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 208
diff changeset
19 def align(self, alignment):
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 208
diff changeset
20 self.emit(Alignment(alignment))
234
Windel Bouwman
parents: 225
diff changeset
21
208
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
22 def selectSection(self, s):
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
23 self.currentSection = s
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
24 if not s in self.sections:
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
25 self.sections[s] = []
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
26
234
Windel Bouwman
parents: 225
diff changeset
27 def getLabelAddress(self, l):
Windel Bouwman
parents: 225
diff changeset
28 assert isinstance(l, Label)
Windel Bouwman
parents: 225
diff changeset
29 for s in self.sections.values():
Windel Bouwman
parents: 225
diff changeset
30 for i in s:
Windel Bouwman
parents: 225
diff changeset
31 if type(i) is Label:
Windel Bouwman
parents: 225
diff changeset
32 if i.name == l.name:
Windel Bouwman
parents: 225
diff changeset
33 return i.address
Windel Bouwman
parents: 225
diff changeset
34 return 0
Windel Bouwman
parents: 225
diff changeset
35
208
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
36 def backpatch(self):
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
37 """ Fixup references to other parts in the assembler """
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
38 for s in self.sections:
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
39 # TODO parameterize this:
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
40 if s == 'code':
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
41 address = 0x08000000
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
42 elif s == 'data':
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
43 address = 0x02000000
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
44 else:
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
45 address = 0x0
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
46 for i in self.sections[s]:
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
47 i.address = address
234
Windel Bouwman
parents: 225
diff changeset
48 i.resolve(self.getLabelAddress)
208
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
49 bts = i.encode()
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
50 address += len(bts)
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
51
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
52 def dump(self):
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
53 self.backpatch()
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
54 for s in sorted(self.sections.keys()):
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
55 self.dumpSection(s)
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
56
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
57 def dumpSection(self, s):
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
58 print('.section '+s)
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
59 for i in self.sections[s]:
234
Windel Bouwman
parents: 225
diff changeset
60 addr = i.address
Windel Bouwman
parents: 225
diff changeset
61 insword = i.encode()
Windel Bouwman
parents: 225
diff changeset
62 assert type(insword) is bytes
Windel Bouwman
parents: 225
diff changeset
63 insword = binascii.hexlify(bytes(reversed(insword))).decode('ascii')
Windel Bouwman
parents: 225
diff changeset
64 asm = str(i)
Windel Bouwman
parents: 225
diff changeset
65 print(' 0x{0:08x} 0x{1} {2}'.format(addr, insword, asm))
208
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
66
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
67 class TextOutputStream(OutputStream):
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
68 pass
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
69
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
70 class BinOutputStream(OutputStream):
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
71 def dump(self):
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
72 pass
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
73