diff python/ppci/assembler.py @ 347:742588fb8cd6 devel

Merge into devel branch
author Windel Bouwman
date Fri, 07 Mar 2014 17:10:21 +0100
parents 3bb7dcfe5529
children 19eacf4f7270
line wrap: on
line diff
--- a/python/ppci/assembler.py	Sat Mar 01 16:27:52 2014 +0100
+++ b/python/ppci/assembler.py	Fri Mar 07 17:10:21 2014 +0100
@@ -3,14 +3,13 @@
 import pyyacc
 from . import Token, CompilerError, SourceLocation
 from .target import Target, Label
-from .asmnodes import ALabel, AInstruction, ABinop, AUnop, ASymbol, ANumber
 
 
 def bit_type(value):
-    assert value < (2**31)
+    assert value < (2**32)
     assert value >= 0
     t = 'val32'
-    for n in [16, 8, 5, 3]:
+    for n in [16, 12, 8, 5, 3]:
         if value < (2**n):
             t = 'val{}'.format(n)
     return t
@@ -100,7 +99,7 @@
         # Construct a parser given a grammar:
         tokens2 = ['ID', 'NUMBER', ',', '[', ']', ':', '+', '-', '*',
                    pyyacc.EPS, 'COMMENT', '{', '}',
-                   pyyacc.EOF, 'val32', 'val16', 'val8', 'val5', 'val3']
+                   pyyacc.EOF, 'val32', 'val16', 'val12', 'val8', 'val5', 'val3']
         tokens2.extend(kws)
         self.kws = kws
         g = pyyacc.Grammar(tokens2)
@@ -176,11 +175,13 @@
 
 
 class Assembler:
-    def __init__(self, target, stream):
+    def __init__(self, target):
         self.target = target
         assert isinstance(target, Target)
-        self.stream = stream
-        self.parser = Parser(target.asm_keywords, target.assembler_rules, self.stream.emit)
+        self.parser = Parser(target.asm_keywords, target.assembler_rules, self.emit)
+
+    def emit(self, *args):
+        self.stream.emit(*args)
 
     # Top level interface:
     def parse_line(self, line):
@@ -188,7 +189,7 @@
         tokens = Lexer(line, self.target.asm_keywords)
         self.parser.parse(tokens)
 
-    def assemble(self, asmsrc):
+    def assemble(self, asmsrc, stream):
         """ Assemble this source snippet """
         if hasattr(asmsrc, 'read'):
             asmsrc2 = asmsrc.read()
@@ -196,9 +197,8 @@
             asmsrc = asmsrc2
         # TODO: use generic newline??
         # TODO: the bothersome newline ...
+        self.stream = stream
         for line in asmsrc.split('\n'):
             self.parse_line(line)
+        self.stream = None
 
-    def assemble_line(self, line):
-        """ Assemble a single assembly line. """
-        self.parse_line(line)