#!/usr/bin/env python """Compiles C into assembly for the practicum processor (PP2) All rights reserved, you may not redistribute or use this program without prior permission from Peter Wu or Xander Houtman. Use of this program is entirely your own risk. In no circumstances can the authors of this program be held responsible for any damage including, but not limited to, financial damage or data loss. Modification of this program is not allowed without prior permission. The generated output (assembly and messages) are not subject to this license. """ __author__ = "Peter Wu" __copyright__ = "Copyright 2011, Peter Wu" __credits__ = ["Peter Wu"] __license__ = "Proprietary" __version__ = "1.0" __maintainer__ = "Peter Wu" __email__ = "uwretep@gmail.com" import re class Asm(object): def __init__(self): self.level = 0 self.re_identifier = re.compile("^[0-9a-zA-Z_][0-9a-zA-Z_]*$") def is_identifier(self, text): return self.re_identifier.match(text) def format_line(self, line, label="", indent_size=-1): """Returns an indented line optionally prefixed with label""" if indent_size < 0: indent_size = 8 + self.level * 4 if label: indent_size -= len(label) + 2 if indent_size >= 0: label += ": " else: label += ":" # whitespace is generated if indent_size > 0 indent = indent_size * " " return indent + label + line def binary_op(self, binop, reg, operand, label=""): # output format: BBBB RR OO... return self.format_line("{:<4} {:<2} {}".format(binop, reg, operand), label) def unary_op(self, unop, operand, label=""): # output format: UUUU OO... return self.format_line("{:<4} {}".format(unop, operand), label) def branch_op(self, brop, branch_label, label=""): # output format: BBB LL... return self.format_line("{:<4} {}".format(brop, branch_label), label) def push(self, register, label=""): return self.format_line("PUSH " + register, label) def pull(self, register, label=""): return self.format_line("PULL " + register, label) def insert_label(self, instruction, label): new_instruction = instruction.lstrip(" ") indent_size = len(instruction) - len(new_instruction) return self.format_line(new_instruction, label, indent_size) def has_label(self, line): """"Returns True if a line appears to contain a label, False otherwise""" return self.is_identifier(line.split(":", 1)[0].strip()) def noop(self, label, register="R0"): """Returns a labelled no operation operator Note that the Zero, Negative and oVerflow flags are modified """ return self.binary_op("LOAD", register, register, label) + " ; NOOP"