diff python/outstream.py @ 208:4cb47d80fd1f

Added zcc test
author Windel Bouwman
date Sat, 29 Jun 2013 10:06:58 +0200
parents
children 1fa3e0050b49
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/python/outstream.py	Sat Jun 29 10:06:58 2013 +0200
@@ -0,0 +1,60 @@
+import binascii
+from asmnodes import ALabel
+"""
+ The output stream is a stream of instructions that can be output
+ to a file or binary or hexfile.
+"""
+
+class OutputStream:
+    def __init__(self):
+        self.sections = {}
+        self.currentSection = None
+    def emit(self, item):
+        self.sections[self.currentSection].append(item)
+    def selectSection(self, s):
+        self.currentSection = s
+        if not s in self.sections:
+            self.sections[s] = []
+
+    def backpatch(self):
+        """ Fixup references to other parts in the assembler """
+        for s in self.sections:
+            # TODO parameterize this:
+            if s == 'code':
+                address = 0x08000000
+            elif s == 'data':
+                address = 0x02000000
+            else:
+                address = 0x0
+            for i in self.sections[s]:
+                i.address = address
+                if type(i) is ALabel:
+                    continue
+                bts = i.encode()
+                address += len(bts)
+
+    def dump(self):
+        self.backpatch()
+        for s in sorted(self.sections.keys()):
+            self.dumpSection(s)
+
+    def dumpSection(self, s):
+        print('.section '+s)
+        for i in self.sections[s]:
+            if type(i) is ALabel:
+                print(i)
+            else:
+                addr = i.address
+                insword = i.encode()
+                assert type(insword) is bytes
+                insword = binascii.hexlify(bytes(reversed(insword))).decode('ascii')
+                asm = str(i)
+                print('    0x{0:08x} 0x{1} {2}'.format(addr, insword, asm))
+
+class TextOutputStream(OutputStream):
+    pass
+
+class BinOutputStream(OutputStream):
+    def dump(self):
+        pass
+