예제 #1
0
파일: ast.py 프로젝트: thestuckster/4LW
    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))
예제 #2
0
파일: ast.py 프로젝트: thestuckster/4LW
    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
예제 #3
0
파일: ast.py 프로젝트: thestuckster/4LW
 def emit_with_dest(self, context):
     return ('', asm.DataLoc(asm.LocType.TAPE, asm.ConstWord(self.letter)))
예제 #4
0
파일: ast.py 프로젝트: thestuckster/4LW
 def emit_with_dest(self, context):
     size = context.get_type(self.type_name).len_words()
     return ('', asm.DataLoc(asm.LocType.CONST, asm.ConstWord(size)))
예제 #5
0
파일: ast.py 프로젝트: thestuckster/4LW
 def emit_assign_to(self, src, context):
     return asm.Instruction(
         asm.Opcode.MOVE,
         [src,
          asm.DataLoc(asm.LocType.TAPE, asm.ConstWord(self.letter))
          ]).emit()
예제 #6
0
파일: ast.py 프로젝트: thestuckster/4LW
 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))
예제 #7
0
파일: ast.py 프로젝트: thestuckster/4LW
 def emit_with_dest(self, context):
     reg = context.get_reg_for_var(self.varname)
     return ('', asm.DataLoc(asm.LocType.REG, asm.ConstWord(reg)))
예제 #8
0
파일: ast.py 프로젝트: thestuckster/4LW
 def has_value(self, val):
     return self.val == asm.ConstWord(val)
예제 #9
0
파일: ast.py 프로젝트: thestuckster/4LW
 def __init__(self, val):
     self.val = asm.ConstWord(val)
예제 #10
0
파일: ast.py 프로젝트: thestuckster/4LW
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 = {}