diff python/asm.py @ 236:8786811a5a59

Fix pcrel
author Windel Bouwman
date Mon, 15 Jul 2013 20:15:31 +0200
parents ff40407c0240
children 1c7c1e619be8
line wrap: on
line diff
--- a/python/asm.py	Mon Jul 15 17:20:37 2013 +0200
+++ b/python/asm.py	Mon Jul 15 20:15:31 2013 +0200
@@ -1,7 +1,7 @@
 import re, argparse
 import pyyacc
 from ppci import Token, CompilerError, SourceLocation
-from target import Target
+from target import Target, Label
 from asmnodes import ALabel, AInstruction, ABinop, AUnop, ASymbol, ANumber
 
 def tokenize(s):
@@ -141,22 +141,19 @@
 asmParser = Parser()
 
 class Assembler:
-    def __init__(self, target=None):
+    def __init__(self, target=None, stream=None):
         self.target = target
+        self.stream = stream
         self.restart()
         self.p = asmParser
 
     # Top level interface:
     def restart(self):
-        self.output = []
-        self.binout = bytearray()
-        self.current_section = '.text'
+        self.stack = []
 
     def emit(self, a):
         """ Emit a parsed instruction """
-        self.output.append(a)
-        # Determine the bit pattern from a lookup table:
-        # TODO
+        self.stack.append(a)
 
     def parse_line(self, line):
         """ Parse line into asm AST """
@@ -167,7 +164,6 @@
         """ Assemble this source snippet """
         for line in asmsrc.split('\n'):
             self.assemble_line(line)
-        self.back_patch()
 
     def assemble_line(self, line):
         """ 
@@ -182,18 +178,16 @@
         # TODO
         if not self.target:
             raise CompilerError('Cannot assemble without target')
-        while self.output:
-            vi = self.output.pop(0)
+        while self.stack:
+            vi = self.stack.pop(0)
             if type(vi) is AInstruction:
-                ri = self.target.mapInstruction(vi)
-                b = ri.encode()
-                assert type(b) is bytes
-                self.binout.extend(b)
-
-    def back_patch(self):
-        """ Fix references to earlier labels """
-        pass
-        #self.output.backpatch()
+                mi = self.target.mapInstruction(vi)
+            elif type(vi) is ALabel:
+                mi = Label(vi.name)
+            else:
+                raise NotImplementedError('{}'.format(vi))
+            if self.stream:
+                self.stream.emit(mi)
 
 
 if __name__ == '__main__':