comparison python/outstream.py @ 208:4cb47d80fd1f

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