diff python/outstream.py @ 237:81752b0f85a5

Added burn led test program
author Windel Bouwman
date Wed, 17 Jul 2013 22:31:54 +0200
parents 8786811a5a59
children e41e4109addd
line wrap: on
line diff
--- a/python/outstream.py	Mon Jul 15 20:15:31 2013 +0200
+++ b/python/outstream.py	Wed Jul 17 22:31:54 2013 +0200
@@ -7,24 +7,41 @@
 """
 
 
+class Section:
+    def __init__(self):
+        self.instructions = []
+
+    def emit(self, item):
+        assert isinstance(item, Instruction)
+        self.instructions.append(item)
+
+    def to_bytes(self):
+        d = bytearray()
+        for i in self.instructions:
+            addr = i.address
+            insword = i.encode()
+            assert type(insword) is bytes
+            d.extend(insword)
+        return bytes(d)
+
 class OutputStream:
     def __init__(self):
         self.sections = {}
         self.currentSection = None
 
     def emit(self, item):
-        assert isinstance(item, Instruction)
-        self.sections[self.currentSection].append(item)
+        assert self.currentSection
+        self.sections[self.currentSection].emit(item)
 
     def selectSection(self, s):
         self.currentSection = s
         if not s in self.sections:
-            self.sections[s] = []
+            self.sections[s] = Section()
 
     def getLabelAddress(self, lname):
         assert isinstance(lname, str)
         for s in self.sections.values():
-            for i in s:
+            for i in s.instructions:
                 if type(i) is Label:
                     if i.name == lname:
                         return i.address
@@ -40,7 +57,8 @@
                 address = 0x02000000
             else:
                 address = 0x0
-            for i in self.sections[s]:
+
+            for i in self.sections[s].instructions:
                 i.address = address
                 i.resolve(self.getLabelAddress)
                 bts = i.encode()
@@ -53,8 +71,8 @@
             self.dumpSection(s)
 
     def dumpSection(self, s):
-        print('.section '+s)
-        for i in self.sections[s]:
+        print('.section '+ s)
+        for i in self.sections[s].instructions:
             addr = i.address
             insword = i.encode()
             assert type(insword) is bytes
@@ -69,8 +87,6 @@
     pass
 
 class BinOutputStream(OutputStream):
-    def dump(self):
-        pass
 
     @property
     def Data(self):
@@ -80,15 +96,7 @@
     def dump(self):
         self.backpatch()
         self.backpatch()
-        section = list(self.sections.keys())[0]
-        return self.dumpSection(section)
+        section = self.sections[list(self.sections.keys())[0]]
+        return section.to_bytes()
 
-    def dumpSection(self, s):
-        d = bytearray()
-        for i in self.sections[s]:
-            addr = i.address
-            insword = i.encode()
-            assert type(insword) is bytes
-            d.extend(insword)
-        return d