From 61a73ce50b1c779879bffcd8235e9a529f3b1949 Mon Sep 17 00:00:00 2001 From: Peter Wu Date: Sun, 4 Dec 2011 14:37:59 +0000 Subject: Support CONS and lonely labels when parsing .asm files --- AsmLine.py | 3 +++ AsmParser.py | 23 +++++++++++++++++++---- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/AsmLine.py b/AsmLine.py index 5a5249f..0b99119 100644 --- a/AsmLine.py +++ b/AsmLine.py @@ -64,6 +64,9 @@ class AsmLine(object): elif self.instruction in Asm.operators_misc_noreg: # no args pass + elif self.instruction == "CONS": + # pseudo instruction + self.setOperand(parts[1]) else: raise RuntimeError("Unknown instruction '{}'".format(self.instruction)) def setOperand(self, str): diff --git a/AsmParser.py b/AsmParser.py index 24755f6..c3354dd 100644 --- a/AsmParser.py +++ b/AsmParser.py @@ -24,6 +24,7 @@ import re class AsmParser(object): def __init__(self, filename, parent=None): + self.filename = filename if parent: self.parent = parent self.defined_names = parent.defined_names @@ -45,11 +46,21 @@ class AsmParser(object): self.re_whitespace = re.compile("\s+") file = open(filename, "rU") + line_no = 0 + buffered_line = "" while True: + line_no += 1 line = file.readline() # line is empty if EoF, otherwise a string including newline if line: - self.parseLine(line.split(";", 1)[0].strip()) + # strip comments and leading/trailing whitespace + line = line.split(";")[0].strip() + # assume that lines ending with ":" are incomplete and that + # the instruction contains on a next line + buffered_line += line + if buffered_line and not buffered_line.endswith(":"): + self.parseLine(buffered_line, line_no) + buffered_line = "" else: break file.close() @@ -58,7 +69,7 @@ class AsmParser(object): if name in self.defined_names: self.defined_names[name].rename(value) del self.defined_names[name] - def parseLine(self, line): + def parseLine(self, line, line_no): """Processes the a line from assembly""" if line.startswith("@"): cmd, opts = re.split("\s+", line + " ", 1) @@ -107,7 +118,11 @@ class AsmParser(object): if self.in_section == "DATA": raise RuntimeError("Found non-definition data in @DATA, " "namely: " + line) - lineobj = AsmLine(line, id_dict=self.defined_names) + try: + lineobj = AsmLine(line, id_dict=self.defined_names) + except RuntimeError as errmsg: + raise RuntimeError("{} on line {} in {}" + .format(errmsg, line_no, self.filename)) if lineobj.label: self.labels.append(lineobj.label) self.addCode(lineobj) @@ -119,7 +134,7 @@ class AsmParser(object): """ if name in self.constants: raise RuntimeError("Redefinition of constant '{}'".format(name)) - self.constants[name] = self.evaluateConstant(data) + self.constants[name] = self.evaluateConstant(value) def addCode(self, line): """Add a line to the @CODE section""" if self.parent: -- cgit v1.2.1