Mercurial > lcfOS
diff python/asm.py @ 199:a690473b79e2
Added msp430 target
author | Windel Bouwman |
---|---|
date | Fri, 07 Jun 2013 18:59:57 +0200 |
parents | 33d50727a23c |
children | 5e391d9a3381 |
line wrap: on
line diff
--- a/python/asm.py Sat Jun 01 13:11:22 2013 +0200 +++ b/python/asm.py Fri Jun 07 18:59:57 2013 +0200 @@ -1,10 +1,7 @@ import re, sys, argparse import pyyacc from ppci import Token, CompilerError, SourceLocation - - -# Generic assembler: -keywords = ['global', 'db'] +from target import Target def tokenize(s): """ @@ -34,13 +31,8 @@ if typ == 'NEWLINE': line_start = pos line += 1 - elif typ == 'COMMENTS': - pass elif typ != 'SKIP': - if typ == 'ID': - if val in keywords: - typ = val - elif typ == 'LEESTEKEN': + if typ == 'LEESTEKEN': typ = val elif typ == 'NUMBER': val = int(val) @@ -122,12 +114,14 @@ class ANumber(AExpression): def __init__(self, n): + assert type(n) is int self.n = n def __repr__(self): return '{0}'.format(self.n) class Assembler: - def __init__(self): + def __init__(self, target=None): + self.target = target self.output = [] # Construct a parser given a grammar: ident = lambda x: x # Identity helper function @@ -174,9 +168,6 @@ return ops def p_mem_op(self, brace_open, exp, brace_close): return AUnop('[]', exp) - def handle_ins(self, id0, operands): - ins = AInstruction(id0) - self.emit(ins) def p_label(self, lname, cn): lab = ALabel(lname) self.emit(lab) @@ -189,6 +180,9 @@ return ANumber(n) # Top level interface: + def restart(self): + pass + def emit(self, a): """ Emit a parsed instruction """ self.output.append(a) @@ -217,7 +211,12 @@ def assemble_aast(self): """ Assemble a parsed asm line """ - pass + # TODO + if not self.target: + raise CompilerError('Cannot assemble without target') + while self.output: + i = self.output.pop(0) + self.target.createInstruction(i) def back_patch(self): """ Fix references to earlier labels """