annotate python/outstream.py @ 208:4cb47d80fd1f

Added zcc test
author Windel Bouwman
date Sat, 29 Jun 2013 10:06:58 +0200
parents
children 1fa3e0050b49
rev   line source
208
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
1 import binascii
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
2 from asmnodes import ALabel
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
3 """
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
4 The output stream is a stream of instructions that can be output
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
5 to a file or binary or hexfile.
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
6 """
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
7
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
8 class OutputStream:
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
9 def __init__(self):
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
10 self.sections = {}
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
11 self.currentSection = None
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
12 def emit(self, item):
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
13 self.sections[self.currentSection].append(item)
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
14 def selectSection(self, s):
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
15 self.currentSection = s
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
16 if not s in self.sections:
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
17 self.sections[s] = []
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
18
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
19 def backpatch(self):
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
20 """ Fixup references to other parts in the assembler """
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
21 for s in self.sections:
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
22 # TODO parameterize this:
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
23 if s == 'code':
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
24 address = 0x08000000
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
25 elif s == 'data':
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
26 address = 0x02000000
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
27 else:
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
28 address = 0x0
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
29 for i in self.sections[s]:
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
30 i.address = address
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
31 if type(i) is ALabel:
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
32 continue
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
33 bts = i.encode()
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
34 address += len(bts)
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
35
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
36 def dump(self):
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
37 self.backpatch()
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
38 for s in sorted(self.sections.keys()):
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
39 self.dumpSection(s)
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
40
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
41 def dumpSection(self, s):
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
42 print('.section '+s)
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
43 for i in self.sections[s]:
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
44 if type(i) is ALabel:
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
45 print(i)
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
46 else:
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
47 addr = i.address
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
48 insword = i.encode()
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
49 assert type(insword) is bytes
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
50 insword = binascii.hexlify(bytes(reversed(insword))).decode('ascii')
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
51 asm = str(i)
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
52 print(' 0x{0:08x} 0x{1} {2}'.format(addr, insword, asm))
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
53
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
54 class TextOutputStream(OutputStream):
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
55 pass
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
56
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
57 class BinOutputStream(OutputStream):
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
58 def dump(self):
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
59 pass
4cb47d80fd1f Added zcc test
Windel Bouwman
parents:
diff changeset
60