def register_string(self, string): maybename = self.maybe_get_string_name(string) if maybename: # This string has already been registered, we don't have to do it again. return asm.DataLoc(asm.LocType.CONST, asm.RefWord(maybename)) name = asm.ident('str') self.strings.append((string, name)) return asm.DataLoc(asm.LocType.CONST, asm.RefWord(name))
def emit_with_dest(self, context): type = context.get_type(self.type_name) if type is None: raise Exception("Type {} does not exist in context {}".format( self.type_name, context)) offset = type.get_field_offset(self.field_name) out = '' basecalc, base_dest = self.base.emit_with_dest(context) out += basecalc loc = None if offset == 0: loc = base_dest elif offset == 4: loc = base_dest.with_flag(asm.DataFlag.PLUSFOUR) else: out += asm.Instruction(asm.Opcode.ADD, [ base_dest, asm.DataLoc(asm.LocType.CONST, asm.ConstWord(offset)), TEMPSTACK ]).emit() loc = TEMPSTACK #out += asm.Instruction(asm.Opcode.ADD, [base_dest, asm.DataLoc(asm.LocType.CONST, asm.ConstWord(offset)), TEMPSTACK]).emit() return (out, loc.with_flag(asm.DataFlag.MEM))
def emit_assign_to(self, src, context): out = '' type = context.get_type(self.type_name) offset = type.get_field_offset(self.field_name) basecalc, base_dest = self.base.emit_with_dest(context) out += basecalc loc = None if offset == 0: loc = base_dest elif offset == 4: loc = base_dest.with_flag(asm.DataFlag.PLUSFOUR) else: out += asm.Instruction(asm.Opcode.ADD, [ base_dest, asm.DataLoc(asm.LocType.CONST, asm.ConstWord(offset)), TEMPSTACK ]).emit() loc = TEMPSTACK out += asm.Instruction(asm.Opcode.MOVE, [src, loc.with_flag(asm.DataFlag.MEM)]).emit() return out
def emit(self, context): return asm.Jump( asm.DataLoc(asm.LocType.CONST, asm.RefWord(self.labelname))).emit()
def emit_with_dest(self, context): return ('', asm.DataLoc(asm.LocType.TAPE, asm.ConstWord(self.letter)))
def emit_with_dest(self, context): return ('', asm.DataLoc(asm.LocType.IO))
def emit_assign_to(self, src, context): return asm.Instruction( asm.Opcode.MOVE, [src, asm.DataLoc(asm.LocType.TAPE, asm.ConstWord(self.letter)) ]).emit()
def emit_with_dest(self, context): size = context.get_type(self.type_name).len_words() return ('', asm.DataLoc(asm.LocType.CONST, asm.ConstWord(size)))
def emit_assign_to(self, src, context): return asm.Instruction(asm.Opcode.MOVE, [src, asm.DataLoc(asm.LocType.IO)]).emit()
def get_loc_for_name(self, name): if self.var_exists(name): return asm.DataLoc(asm.LocType.REG, asm.ConstWord(self.get_reg_for_var(name))) else: return asm.DataLoc(asm.LocType.CONST, asm.RefWord(name))
def emit_with_dest(self, context): reg = context.get_reg_for_var(self.varname) return ('', asm.DataLoc(asm.LocType.REG, asm.ConstWord(reg)))
def emit_with_dest(self, context): return ('', asm.DataLoc(asm.LocType.CONST, asm.RefWord(self.name)))
def emit_with_dest(self, context): return ('', asm.DataLoc(asm.LocType.CONST, self.val))
import asmwrite as asm import abc import os import sys import var_types as types import traceback TEMPSTACK = asm.DataLoc(asm.LocType.STACK, asm.ConstWord('_')) ARGSTACK = asm.DataLoc(asm.LocType.STACK, asm.ConstWord(asm.Stacks.ARGS.value)) RESULT_STACK = asm.DataLoc(asm.LocType.STACK, asm.ConstWord(asm.Stacks.RESULT.value)) MAX_WORD = asm.DataLoc(asm.LocType.CONST, asm.ConstWord("ZZZZ")) ZERO_WORD = asm.DataLoc(asm.LocType.CONST, asm.ConstWord(0)) RETURN_LOC = asm.DataLoc(asm.LocType.CONST, asm.RefWord('@return')) def log(s): print(s, file=sys.stderr) class Context: def __init__(self, parent=None, name=None): self.available_registers = [ 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K' ] self.available_registers.reverse() self.vars = {} self.strings = [] self.parent = parent self.functions = {} self.reserved_mem = {}