diff python/outstream.py @ 234:83781bd10fdb

wip
author Windel Bouwman
date Sun, 14 Jul 2013 19:29:21 +0200
parents 1c7364bd74c7
children ff40407c0240
line wrap: on
line diff
--- a/python/outstream.py	Sun Jul 14 12:28:23 2013 +0200
+++ b/python/outstream.py	Sun Jul 14 19:29:21 2013 +0200
@@ -1,13 +1,11 @@
 import binascii
-from asmnodes import ALabel, AComment
+from target import Instruction, Label
+
 """
  The output stream is a stream of instructions that can be output
  to a file or binary or hexfile.
 """
 
-class Alignment:
-    def __init__(self, a):
-        self.align = a
 
 class OutputStream:
     def __init__(self):
@@ -15,16 +13,26 @@
         self.currentSection = None
 
     def emit(self, item):
+        assert isinstance(item, Instruction)
         self.sections[self.currentSection].append(item)
 
     def align(self, alignment):
         self.emit(Alignment(alignment))
-        
+
     def selectSection(self, s):
         self.currentSection = s
         if not s in self.sections:
             self.sections[s] = []
 
+    def getLabelAddress(self, l):
+        assert isinstance(l, Label)
+        for s in self.sections.values():
+            for i in s:
+                if type(i) is Label:
+                    if i.name == l.name:
+                        return i.address
+        return 0
+
     def backpatch(self):
         """ Fixup references to other parts in the assembler """
         for s in self.sections:
@@ -37,12 +45,7 @@
                 address = 0x0
             for i in self.sections[s]:
                 i.address = address
-                if type(i) in [ALabel, AComment]:
-                    continue
-                if type(i) is Alignment:
-                    while (address % i.align) != 0:
-                        address += 1
-                    continue
+                i.resolve(self.getLabelAddress)
                 bts = i.encode()
                 address += len(bts)
 
@@ -54,17 +57,12 @@
     def dumpSection(self, s):
         print('.section '+s)
         for i in self.sections[s]:
-            if type(i) in [ALabel, AComment]:
-                print(i)
-            elif type(i) is Alignment:
-                pass
-            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))
+            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