Ejemplo n.º 1
0
    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))
Ejemplo n.º 2
0
    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))
Ejemplo n.º 3
0
    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
Ejemplo n.º 4
0
 def emit(self, context):
     return asm.Jump(
         asm.DataLoc(asm.LocType.CONST,
                     asm.RefWord(self.labelname))).emit()
Ejemplo n.º 5
0
 def emit_with_dest(self, context):
     return ('', asm.DataLoc(asm.LocType.TAPE, asm.ConstWord(self.letter)))
Ejemplo n.º 6
0
 def emit_with_dest(self, context):
     return ('', asm.DataLoc(asm.LocType.IO))
Ejemplo n.º 7
0
 def emit_assign_to(self, src, context):
     return asm.Instruction(
         asm.Opcode.MOVE,
         [src,
          asm.DataLoc(asm.LocType.TAPE, asm.ConstWord(self.letter))
          ]).emit()
Ejemplo n.º 8
0
 def emit_with_dest(self, context):
     size = context.get_type(self.type_name).len_words()
     return ('', asm.DataLoc(asm.LocType.CONST, asm.ConstWord(size)))
Ejemplo n.º 9
0
 def emit_assign_to(self, src, context):
     return asm.Instruction(asm.Opcode.MOVE,
                            [src, asm.DataLoc(asm.LocType.IO)]).emit()
Ejemplo n.º 10
0
 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))
Ejemplo n.º 11
0
 def emit_with_dest(self, context):
     reg = context.get_reg_for_var(self.varname)
     return ('', asm.DataLoc(asm.LocType.REG, asm.ConstWord(reg)))
Ejemplo n.º 12
0
 def emit_with_dest(self, context):
     return ('', asm.DataLoc(asm.LocType.CONST, asm.RefWord(self.name)))
Ejemplo n.º 13
0
 def emit_with_dest(self, context):
     return ('', asm.DataLoc(asm.LocType.CONST, self.val))
Ejemplo n.º 14
0
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 = {}