コード例 #1
0
ファイル: threaded.py プロジェクト: mossprescott/pynand
    def __init__(self):
        # Parameters controlling how many specialized opcode variants are emitted.
        # More specialization means a larger library, but smaller object code and
        # fewer cycles, generally.
        # May be manually tweaked. A smart translator would inspect the source and choose them
        # to optimize for size/speed.
        self.SPECIALIZED_MAX_PUSH_CONSTANT = 6  # TODO: 12?
        self.SPECIALIZED_MAX_POP_SEGMENT = 6  # TODO: 10?
        self.SPECIALIZED_MAX_PUSH_SEGMENT = 6
        self.SPECIALIZED_MAX_FUNCTION_NUM_LOCALS = 10  # TODO: ?
        self.SPECIALIZED_MAX_CALL_NUM_ARGS = 4  # TODO: ?

        self.asm = AssemblySource()

        self.class_namespace = "_"
        self.function_namespace = "_"

        start = self.asm.next_label("start")
        self.asm.instr(f"@{start}")
        self.asm.instr("0;JMP")

        # "Microcoded" instructions, which for this translator basically includes _all_ opcodes,
        # plus many special-cases:
        # If there's a single argument, it's passed in A. If more than one, additional args are
        # passed in R13-R15. See each implementation for specifics.
        self._library()

        # Early check that the library of opcodes fits in the first half of the ROM, as required.
        # Practically speaking, probably want it to be _much_ smaller than that.
        assert self.asm.instruction_count <= 2**14

        self.asm.label(start)
コード例 #2
0
def compile_class(ast: Class, asm: AssemblySource):
    symbol_table = SymbolTable(ast.name)

    handle_class_var_declarations(ast, symbol_table)

    for decl in ast.subroutineDecs:
        compile_subroutineDec(decl, symbol_table, asm)
        asm.blank()
コード例 #3
0
ファイル: test_11.py プロジェクト: mossprescott/pynand
def test_other_instance_field_access():
    ast = project_10.parse_class("""
        class BoxedInt {
            field int value;

            method boolean compare(BoxedInt other) {
                return value < other.value;
            }
        }
        """)

    asm = AssemblySource()
    project_11.compile_class(ast, asm)

    assert asm.lines == [
        "  function BoxedInt.compare 1",
        "  push argument 0",
        "  pop pointer 0",
        "  push this 0",
        "  push argument 1",
        "  pop pointer 1",
        "  push that 0",
        "  lt",
        "  return",
        "",
    ]
コード例 #4
0
ファイル: test_11.py プロジェクト: mossprescott/pynand
def test_program_seven_opcodes():
    """This program is so simple that there's probably only one reasonable way to compile it,
    so just compare the VM opcodes."""

    with open("examples/project_11/Seven/Main.jack") as f:
        src = f.read()

    ast = project_10.parse_class(src)

    asm = AssemblySource()

    project_11.compile_class(ast, asm)

    expected = """
  function Main.main 1
  push constant 1
  push constant 2
  push constant 3
  call Math.multiply 2
  add
  call Output.printInt 1
  pop temp 0
  push constant 0
  return

"""

    assert list(asm.lines) == expected.split("\n")[1:-1]
コード例 #5
0
ファイル: test_11.py プロジェクト: mossprescott/pynand
def test_program_average_compile():
    # Isolate the compiler by using the included solution for everything else:
    platform = BUNDLED_PLATFORM
    simulator = "codegen"

    with open("examples/project_11/Average/Main.jack") as f:
        src = f.read()

    ast = platform.parser(src)

    asm = AssemblySource()
    project_11.compile_class(ast, asm)

    # If it fails, you probably want to see the opcodes it wrote:
    for l in asm.lines:
        print(l)

    ops = [
        platform.parse_line(l) for l in asm.lines
        if platform.parse_line(l) is not None
    ]

    translator = platform.translator()
    translator.preamble()

    for op in ops:
        translator.handle(op)

    translate_library(translator, platform)

    translator.finish()
    translator.check_references()
コード例 #6
0
def test_compile_string_lib(string_class=project_12.STRING_CLASS,
                            platform=BUNDLED_PLATFORM):
    """First, just make sure this particular class makes it through the compiler."""

    asm = AssemblySource()
    platform.compiler(string_class, asm)

    assert len(asm.lines) > 0
コード例 #7
0
ファイル: project_07.py プロジェクト: mossprescott/pynand
    def __init__(self):
        # AssemblySource deals with keeping track of emitted instructions, and provides a nice
        # log of execution which makes debugging a lot easier. In any case, the tests assume you use
        # it, so you might as well not fight it.
        self.asm = AssemblySource()

        # SOLVERS: remove this when all the method bodies are filled in
        self.solved = solved_07.Translator(self.asm)
コード例 #8
0
ファイル: test_11.py プロジェクト: mossprescott/pynand
def test_program_complex_arrays():
    # Isolate the compiler by using the included solution for everything else:
    platform = BUNDLED_PLATFORM
    simulator = "codegen"

    with open("examples/project_11/ComplexArrays/Main.jack") as f:
        src = f.read()

    ast = platform.parser(src)

    asm = AssemblySource()
    project_11.compile_class(ast, asm)

    # If it fails, you probably want to see the opcodes it wrote:
    for l in asm.lines:
        print(l)

    ops = [
        platform.parse_line(l) for l in asm.lines
        if platform.parse_line(l) is not None
    ]

    translator = platform.translator()
    translator.preamble()

    for op in ops:
        translator.handle(op)

    # Note: using the full OS implementation is simpler then the fancy tricks done in test_12
    # to isolate individual OS classes, but it also means that this test might need millions of
    # cycles to run, including writing all the results to the screen buffer.
    translate_library(translator, platform)

    translator.finish()
    translator.check_references()

    computer = run(platform.chip, simulator=simulator)

    output_stream = StringWriter()
    translator.asm.run(platform.assemble,
                       computer,
                       stop_cycles=5_000_000,
                       debug=True,
                       tty=output_stream)

    output_lines = "".join(output_stream.strs).split("\n")
    assert output_lines == [
        "Test 1: expected result: 5; actual result: 5",
        "Test 2: expected result: 40; actual result: 40",
        "Test 3: expected result: 0; actual result: 0",
        "Test 4: expected result: 77; actual result: 77",
        "Test 5: expected result: 110; actual result: 110",
        "",
    ]
コード例 #9
0
ファイル: test_11.py プロジェクト: mossprescott/pynand
def test_trivial_expression():
    ast = project_10.ExpressionP.parse(project_10.lex("1 + 2"))

    symbol_table = project_11.SymbolTable("Main")
    asm = AssemblySource()
    project_11.compile_expression(ast, symbol_table, asm)

    assert asm.lines == [
        "  push constant 1",
        "  push constant 2",
        "  add",
    ]
コード例 #10
0
ファイル: test_11.py プロジェクト: mossprescott/pynand
def test_missing_return():
    """Another very common error."""

    ast = project_10.parse_class("""
        class Foo {
            function void noReturn() {
                // oops, forgot to `return;` here
            }
        }
        """)

    with pytest.raises(Exception) as exc_info:
        asm = AssemblySource()
        project_11.compile_class(ast, asm)
    assert exc_info.value.args == ('Missing "return" in Foo.noReturn', )
コード例 #11
0
ファイル: test_11.py プロジェクト: mossprescott/pynand
def test_no_this():
    """This could be confusing, so make it an error."""

    ast = project_10.parse_class("""
        class Foo {
            function void run() {
                return this;
            }
        }
        """)

    with pytest.raises(Exception) as exc_info:
        asm = AssemblySource()
        project_11.compile_class(ast, asm)
    assert exc_info.value.args == (
        'Undefined "this" in static context: function Foo.run', )
コード例 #12
0
ファイル: test_11.py プロジェクト: mossprescott/pynand
def test_constructor_void_return():
    """Constructor must return "this", or the caller will be surprised."""

    ast = project_10.parse_class("""
        class Foo {
            constructor Foo new() {
                return;  // meaning "null"
            }
        }
        """)

    with pytest.raises(Exception) as exc_info:
        asm = AssemblySource()
        project_11.compile_class(ast, asm)
    assert exc_info.value.args == (
        'Does not return "this": constructor Foo.new', )
コード例 #13
0
ファイル: test_11.py プロジェクト: mossprescott/pynand
def test_constructor_bad_result_type():
    """This could be confusing, so make it an error."""

    ast = project_10.parse_class("""
        class Foo {
            constructor Bar new() {
                return this;
            }
        }
        """)

    with pytest.raises(Exception) as exc_info:
        asm = AssemblySource()
        project_11.compile_class(ast, asm)
    assert exc_info.value.args == (
        'Result type does not match: constructor Foo.new', )
コード例 #14
0
ファイル: test_11.py プロジェクト: mossprescott/pynand
def test_constructor_wrong_name():
    """This could be confusing, so make it an error."""

    ast = project_10.parse_class("""
        class Foo {
            constructor Foo notNew() {
                return this;
            }
        }
        """)

    with pytest.raises(Exception) as exc_info:
        asm = AssemblySource()
        project_11.compile_class(ast, asm)
    assert exc_info.value.args == (
        'Must be named "new": constructor Foo.notNew', )
コード例 #15
0
ファイル: test_11.py プロジェクト: mossprescott/pynand
def test_program_convert_to_bin():
    # Isolate the compiler by using the included solution for everything else:
    platform = BUNDLED_PLATFORM
    simulator = "codegen"

    with open("examples/project_11/ConvertToBin/Main.jack") as f:
        src = f.read()

    ast = platform.parser(src)

    asm = AssemblySource()
    project_11.compile_class(ast, asm)

    # If it fails, you probably want to see the opcodes it wrote:
    for l in asm.lines:
        print(l)

    ops = [
        platform.parse_line(l) for l in asm.lines
        if platform.parse_line(l) is not None
    ]

    translator = platform.translator()
    translator.preamble()

    for op in ops:
        translator.handle(op)

    # Note: using the full OS implementation is simpler then the fancy tricks done in test_12
    # to isolate individual OS classes, but it also means that this test might need 100s
    # of thousands of cycles to run (mainly initializing the OS unnecessarily.)
    translate_library(translator, platform)

    translator.finish()
    translator.check_references()

    computer = run(platform.chip, simulator=simulator)

    computer.poke(8000, 0xBEEF)

    translator.asm.run(platform.assemble,
                       computer,
                       stop_cycles=200_000,
                       debug=True)

    for b in range(16):
        assert computer.peek(8001 + b) == bool(0xBEEF & (1 << b))
コード例 #16
0
ファイル: test_11.py プロジェクト: mossprescott/pynand
def test_call_method_from_function_context():
    """A common error case; referring to a function using the method-call syntax."""

    ast = project_10.parse_class("""
        class Foo {
            function void run() {
                do go();  // Probably meant `Foo.go()`
                return;
            }
        }
        """)

    with pytest.raises(Exception) as exc_info:
        asm = AssemblySource()
        project_11.compile_class(ast, asm)
    assert exc_info.value.args == (
        'Tried to use implicit "this" in static (function) context: Foo.run', )
コード例 #17
0
ファイル: test_11.py プロジェクト: mossprescott/pynand
def test_return_on_both_branches():
    """Accept this common pattern."""

    ast = project_10.parse_class("""
        class Foo {
            function void toInt(boolean x) {
                if (x) {
                    return 1;
                }
                else {
                    return 0;
                }
            }
        }
        """)

    asm = AssemblySource()
    project_11.compile_class(ast, asm)
コード例 #18
0
ファイル: test_11.py プロジェクト: mossprescott/pynand
def test_field_in_function_context():
    """A common error case; referring to an instance member within a function."""

    ast = project_10.parse_class("""
        class Foo {
            field int x;

            function int run() {
                return x;
            }
        }
        """)

    with pytest.raises(Exception) as exc_info:
        asm = AssemblySource()
        project_11.compile_class(ast, asm)
    assert exc_info.value.args == (
        'Tried to use field "x" in static context: function Foo.run', )
コード例 #19
0
ファイル: test_11.py プロジェクト: mossprescott/pynand
def test_call_function_from_function_context():
    ast = project_10.parse_class("""
        class Foo {
            function void run() {
                do Bar.go();
                return;
            }
        }
        """)

    asm = AssemblySource()
    project_11.compile_class(ast, asm)

    assert asm.lines == [
        "  function Foo.run 1",
        "  call Bar.go 0",
        "  pop temp 0",
        "  push constant 0",
        "  return",
        "",
    ]
コード例 #20
0
    def __init__(self, asm=None):
        self.asm = asm if asm else AssemblySource()
        self.class_namespace = "static"
        self.function_namespace = "_"

        self.defined_functions = []
        self.referenced_functions = []
        self.last_function_start = None

        # HACK: some code that's always required, even when preamble is not used.

        start = self.asm.next_label("start")
        self.asm.instr(f"@{start}")
        self.asm.instr("0;JMP")

        # "Microcoded" instructions:
        self.eq_label = self._compare("EQ")
        self.lt_label = self._compare("LT")
        self.gt_label = self._compare("GT")
        self.return_label = self._return()
        self.call_label = self._call()

        self.asm.label(start)
コード例 #21
0
ファイル: test_11.py プロジェクト: mossprescott/pynand
def test_call_method_from_method_context():
    ast = project_10.parse_class("""
        class Foo {
            method void run() {
                do go();
                return;
            }
        }
        """)

    asm = AssemblySource()
    project_11.compile_class(ast, asm)

    assert asm.lines == [
        "  function Foo.run 1",
        "  push argument 0",
        "  pop pointer 0",
        "  push pointer 0",  # implicit `this` for self call
        "  call Foo.go 1",
        "  pop temp 0",
        "  push constant 0",
        "  return",
        "",
    ]
コード例 #22
0
ファイル: sp.py プロジェクト: mossprescott/pynand
class Translator(solved_07.Translator):
    """Re-use most of the solution's translations, but strategically override most of the
    access to SP.
    """
    def __init__(self):
        self.asm = AssemblySource()
        solved_07.Translator.__init__(self, self.asm)

    def push_constant(self, value):
        self.asm.start(f"push constant {value}")
        if value <= 1:
            self.asm.instr(f"SP++={value}")
        else:
            self.asm.instr(f"@{value}")
            self.asm.instr(f"SP++=A")

    def _pop_segment(self, segment_name, segment_ptr, index):
        self.asm.start(f"pop {segment_name} {index}")
        # Since pop doesn't overwrite A, a much simpler sequence works:
        if index == 0:
            self.asm.instr(f"@{segment_ptr}")
            self.asm.instr("A=M")
        elif index == 1:
            self.asm.instr(f"@{segment_ptr}")
            self.asm.instr("A=M+1")
        else:
            self.asm.instr(f"@{index}")
            self.asm.instr("D=A")
            self.asm.instr(f"@{segment_ptr}")
            self.asm.instr("A=D+M")
        self.asm.instr("D=--SP")
        self.asm.instr("M=D")

    def _push_d(self):
        # TODO: no need for this as soon as everything's switched to use SP++ directly
        self.asm.instr("SP++=D")

    def _pop_d(self):
        # TODO: no need for this as soon as everything's switched to use --SP directly?
        self.asm.instr("D=--SP")

    def _binary(self, opcode, op):
        self.asm.start(opcode)
        self.asm.instr("D=--SP")
        self.asm.instr("A=--SP")
        self.asm.instr(f"SP++={op.replace('M', 'A')}")

    def _unary(self, opcode, op):
        self.asm.start(opcode)
        self.asm.instr("D=--SP")
        self.asm.instr(f"SP++={op.replace('M', 'D')}")

    def function(self, class_name, function_name, num_vars):
        """Pushing zeros is a lot simpler now, saving a few instructions."""

        self.class_namespace = class_name.lower()
        self.function_namespace = f"{class_name.lower()}.{function_name}"

        self.asm.start(f"function {class_name}.{function_name} {num_vars}")
        self.asm.label(f"{self.function_namespace}")

        if num_vars == 0:
            # Tricky: this instruction has no effect; it's just here to take up space in the ROM and ensure that the
            # "function" op has a unique address assigned to it, so that it can appear in tracing and profiling. Yes,
            # that is dumb.
            self.asm.instr("0")
        else:
            for _ in range(num_vars):
                self.asm.instr("SP++=0")

    def _compare(self, op):
        # Saves about 4 instuctions each time, or a few % at runtime.

        label = self.asm.next_label(f"{op.lower()}_common")
        end_label = self.asm.next_label(f"{op.lower()}_common$end")
        self.asm.start(f"{op.lower()}_common")
        self.asm.label(label)
        self.asm.instr("@R15")  # R15 = D (the return address)
        self.asm.instr("M=D")

        # D = top, M = second from top
        self.asm.instr("D=--SP")
        self.asm.instr("A=--SP")

        # Compare
        self.asm.instr("D=A-D")

        # Set result True, optimistically
        self.asm.instr("SP++=-1")

        self.asm.instr(f"@{end_label}")
        self.asm.instr(f"D;J{op}")

        # Set result False
        self.asm.instr("D=--SP")  # Drop speculative result
        self.asm.instr("SP++=0")

        self.asm.label(end_label)
        self.asm.instr("@R15")  # JMP to R15
        self.asm.instr("A=M")
        self.asm.instr("0;JMP")
        return label

    def _call(self):
        """Common sequence for all calls.

        D = num_args
        R14 = callee address
        stack: return address already pushed

        Note: this is about 16 instructions better in all, by reducing each push to a single instruction
        and keeping the new ARG address in D while the segment pointers are pushed. The total is now 24,
        not to mention the 10 or so at each point of use that then jumps here. That's still frustratingly
        many.

        Possible improvements:
        - pass the callee address on the stack, now that it's cheaper?
        - with an immediate load instruction (e.g. A=@LCL), save 5 cycles
        - with an immediate store (e.g. @LCL=A), save another 3

        Possibly bigger return from a smarter compiler that avoids saving a full frame when calling
        functions that won't use/clobber everything. This is the familiar "leaf function" optimization.
        """

        label = self.asm.next_label("call_common")

        self.asm.start(f"call_common")
        self.asm.label(label)

        # D = SP - (D + 1) (which will be the new ARG)
        self.asm.instr("@SP")
        self.asm.instr("D=M-D")
        self.asm.instr("D=D-1")

        # push four segment pointers:
        self.asm.instr("@LCL")
        self.asm.instr("A=M")
        self.asm.instr("SP++=A")
        self.asm.instr("@ARG")
        self.asm.instr("A=M")
        self.asm.instr("SP++=A")
        self.asm.instr("@THIS")
        self.asm.instr("A=M")
        self.asm.instr("SP++=A")
        self.asm.instr("@THAT")
        self.asm.instr("A=M")
        self.asm.instr("SP++=A")

        # ARG = D
        self.asm.instr("@ARG")
        self.asm.instr("M=D")

        # LCL = SP
        # Note: setting LCL here (as opposed to in "function") feels wrong, but it makes the
        # state of the segment pointers consistent after each opcode, so it's easier to debug.
        self.asm.instr("@SP")
        self.asm.instr("D=M")
        self.asm.instr("@LCL")
        self.asm.instr("M=D")

        # JMP to R14 (the callee)
        self.asm.instr("@R14")
        self.asm.instr("A=M")
        self.asm.instr("0;JMP")
        return label

    # TODO: improve the common sequence for `return`.

    def finish(self):
        pass
コード例 #23
0
ファイル: threaded.py プロジェクト: mossprescott/pynand
class Translator:
    """Can't really re-use anything from the standard translator.
    """
    def __init__(self):
        # Parameters controlling how many specialized opcode variants are emitted.
        # More specialization means a larger library, but smaller object code and
        # fewer cycles, generally.
        # May be manually tweaked. A smart translator would inspect the source and choose them
        # to optimize for size/speed.
        self.SPECIALIZED_MAX_PUSH_CONSTANT = 6  # TODO: 12?
        self.SPECIALIZED_MAX_POP_SEGMENT = 6  # TODO: 10?
        self.SPECIALIZED_MAX_PUSH_SEGMENT = 6
        self.SPECIALIZED_MAX_FUNCTION_NUM_LOCALS = 10  # TODO: ?
        self.SPECIALIZED_MAX_CALL_NUM_ARGS = 4  # TODO: ?

        self.asm = AssemblySource()

        self.class_namespace = "_"
        self.function_namespace = "_"

        start = self.asm.next_label("start")
        self.asm.instr(f"@{start}")
        self.asm.instr("0;JMP")

        # "Microcoded" instructions, which for this translator basically includes _all_ opcodes,
        # plus many special-cases:
        # If there's a single argument, it's passed in A. If more than one, additional args are
        # passed in R13-R15. See each implementation for specifics.
        self._library()

        # Early check that the library of opcodes fits in the first half of the ROM, as required.
        # Practically speaking, probably want it to be _much_ smaller than that.
        assert self.asm.instruction_count <= 2**14

        self.asm.label(start)

    def preamble(self):
        self.asm.start("VM initialization")
        self.asm.instr("@256")
        self.asm.instr("D=A")
        self.asm.instr("@SP")
        self.asm.instr("M=D")

        self.call("Sys", "init", 0)

    def push_constant(self, value):
        """Value to push in A if not specialized.
        """

        assert 0 <= value < 2**15

        self.asm.start(f"push constant {value}")

        if value <= self.SPECIALIZED_MAX_PUSH_CONSTANT:
            self.asm.instr(f"CALL VM.push_constant_{value}")
        else:
            self.asm.instr(f"@{value}")
            self.asm.instr(f"CALL VM.push_constant")

    def add(self):
        self.asm.start(f"add")
        self.asm.instr(f"CALL VM.add")

    def sub(self):
        self.asm.start(f"sub")
        self.asm.instr(f"CALL VM.sub")

    def neg(self):
        self.asm.start(f"neg")
        self.asm.instr(f"CALL VM.neg")

    def and_op(self):
        self.asm.start(f"and")
        self.asm.instr(f"CALL VM.and")

    def or_op(self):
        self.asm.start(f"or")
        self.asm.instr(f"CALL VM.or")

    def not_op(self):
        self.asm.start(f"not")
        self.asm.instr(f"CALL VM.not")

    def eq(self):
        self.asm.start(f"eq")
        self.asm.instr(f"CALL VM.eq")

    def lt(self):
        self.asm.start(f"lt")
        self.asm.instr(f"CALL VM.lt")

    def gt(self):
        self.asm.start(f"gt")
        self.asm.instr(f"CALL VM.gt")

    def pop_local(self, index):
        self.asm.start(f"pop local {index}")
        if index <= self.SPECIALIZED_MAX_POP_SEGMENT:
            self.asm.instr(f"CALL VM.pop_local_{index}")
        else:
            self.asm.instr(f"@{index}")
            self.asm.instr(f"CALL VM.pop_local")

    def pop_argument(self, index):
        self.asm.start(f"pop argument {index}")
        if index <= self.SPECIALIZED_MAX_POP_SEGMENT:
            self.asm.instr(f"CALL VM.pop_argument_{index}")
        else:
            self.asm.instr(f"@{index}")
            self.asm.instr(f"CALL VM.pop_argument")

    def pop_this(self, index):
        self.asm.start(f"pop this {index}")
        if index <= self.SPECIALIZED_MAX_POP_SEGMENT:
            self.asm.instr(f"CALL VM.pop_this_{index}")
        else:
            self.asm.instr(f"@{index}")
            self.asm.instr(f"CALL VM.pop_this")

    def pop_that(self, index):
        self.asm.start(f"pop that {index}")
        if index <= self.SPECIALIZED_MAX_POP_SEGMENT:
            self.asm.instr(f"CALL VM.pop_that_{index}")
        else:
            self.asm.instr(f"@{index}")
            self.asm.instr(f"CALL VM.pop_that")

    def pop_temp(self, index):
        self.asm.start(f"pop temp {index}")
        self.asm.instr(f"CALL VM.pop_temp_{index}")

    def pop_pointer(self, index):
        assert 0 <= index <= 1
        self.asm.start(f"pop pointer {index}")
        self.asm.instr(f"CALL VM.pop_pointer_{index}")

    def push_local(self, index):
        self.asm.start(f"push local {index}")
        if index <= self.SPECIALIZED_MAX_PUSH_SEGMENT:
            self.asm.instr(f"CALL VM.push_local_{index}")
        else:
            self.asm.instr(f"@{index}")
            self.asm.instr(f"CALL VM.push_local")

    def push_argument(self, index):
        self.asm.start(f"push argument {index}")
        if index <= self.SPECIALIZED_MAX_PUSH_SEGMENT:
            self.asm.instr(f"CALL VM.push_argument_{index}")
        else:
            self.asm.instr(f"@{index}")
            self.asm.instr(f"CALL VM.push_argument")

    def push_this(self, index):
        self.asm.start(f"push this {index}")
        if index <= self.SPECIALIZED_MAX_PUSH_SEGMENT:
            self.asm.instr(f"CALL VM.push_this_{index}")
        else:
            self.asm.instr(f"@{index}")
            self.asm.instr(f"CALL VM.push_this")

    def push_that(self, index):
        self.asm.start(f"push that {index}")
        if index <= self.SPECIALIZED_MAX_PUSH_SEGMENT:
            self.asm.instr(f"CALL VM.push_that_{index}")
        else:
            self.asm.instr(f"@{index}")
            self.asm.instr(f"CALL VM.push_that")

    def push_temp(self, index):
        assert 0 <= index < 8
        self.asm.start(f"push temp {index}")
        self.asm.instr(f"CALL VM.push_temp_{index}")

    def push_pointer(self, index):
        assert 0 <= index <= 1
        self.asm.start(f"push pointer {index}")
        self.asm.instr(f"CALL VM.push_pointer_{index}")

    def pop_static(self, index):
        self.asm.start(f"push static {index}")
        self.asm.instr(f"@{self.class_namespace}.static{index}")
        self.asm.instr(f"CALL VM.pop_static")

    def push_static(self, index):
        self.asm.start(f"pop static {index}")
        self.asm.instr(f"@{self.class_namespace}.static{index}")
        self.asm.instr(f"CALL VM.push_static")

    def label(self, name):
        self.asm.start(f"label {name}")
        self.asm.label(f"{self.function_namespace}${name}")

    def if_goto(self, name):
        self.asm.start(f"if-goto {name}")
        self.asm.instr(f"@{self.function_namespace}${name}")
        self.asm.instr(f"CALL VM.if_goto")

    def goto(self, name):
        self.asm.start(f"goto {name}")
        self.asm.instr(f"@{self.function_namespace}${name}")
        self.asm.instr("0;JMP")

    def function(self, class_name, function_name, num_vars):
        self.class_namespace = class_name.lower()
        self.function_namespace = f"{class_name.lower()}.{function_name}"

        self.asm.start(f"function {class_name}.{function_name} {num_vars}")
        self.asm.label(f"{self.function_namespace}")
        if num_vars <= self.SPECIALIZED_MAX_FUNCTION_NUM_LOCALS:
            self.asm.instr(f"CALL VM.function_{num_vars}")
        else:
            self.asm.instr(f"@{num_vars}")
            self.asm.instr(f"CALL VM.function")

    def return_op(self):
        # Note: not actually going to RTN from this, but using CALL still saves a word.
        self.asm.start("return")
        self.asm.instr("CALL VM.return")

    def call(self, class_name, function_name, num_args):
        """Callee address in A. num_args in R13 if not specialized.
        """

        return_label = self.asm.next_label("RET_ADDRESS_CALL")

        self.asm.start(f"call {class_name}.{function_name} {num_args}")

        self.asm.instr(f"@{return_label}")
        self.asm.instr("CALL VM._push_a")

        if num_args <= self.SPECIALIZED_MAX_CALL_NUM_ARGS:
            self.asm.instr(f"@{class_name.lower()}.{function_name}")
            self.asm.instr(f"CALL VM.call_{num_args}")
        else:
            self.asm.instr(f"@{num_args}")
            self.asm.instr(f"D=A")
            self.asm.instr(f"@R13")
            self.asm.instr(f"M=D")

            self.asm.instr(f"@{class_name.lower()}.{function_name}")
            self.asm.instr(f"CALL VM.call")

        self.asm.label(return_label)

    def rewrite_ops(self, ops):
        return ops

    def finish(self):
        pass

    def handle(self, op):
        op_name, args = op
        self.__getattribute__(op_name)(*args)

    def _library(self):

        # Push from D:
        def push_d():
            self.asm.instr("@SP")
            self.asm.instr("M=M+1")
            self.asm.instr("A=M-1")
            self.asm.instr("M=D")

        # pop to D; has to be generated inline each time because it's never a tail call:
        def pop_d():
            self.asm.instr("@SP")
            self.asm.instr("AM=M-1")
            self.asm.instr("D=M")

        # push constant
        for value in (0, 1):
            self.asm.label(f"VM.push_constant_{value}")
            self.asm.instr("@SP")
            self.asm.instr("M=M+1")
            self.asm.instr("A=M-1")
            self.asm.instr(f"M={value}")
            self.asm.instr("RTN")

        for value in range(2, self.SPECIALIZED_MAX_PUSH_CONSTANT + 1):
            self.asm.label(f"VM.push_constant_{value}")
            self.asm.instr(f"@{value}")
            self.asm.instr("D=A")
            push_d()
            self.asm.instr("RTN")

        self.asm.label("VM.push_constant")
        self.asm.instr("D=A")
        push_d()
        self.asm.instr("RTN")

        # Push from one of the memory segments:
        def push_segment(segment_ptr, index):
            if index == 0:
                self.asm.instr(f"@{segment_ptr}")
                self.asm.instr("A=M")
                self.asm.instr("D=M")
                push_d()
                self.asm.instr("RTN")
            elif index == 1:
                self.asm.instr(f"@{segment_ptr}")
                self.asm.instr("A=M+1")
                self.asm.instr("D=M")
                push_d()
                self.asm.instr("RTN")
            else:
                self.asm.instr(f"@{index}")
                self.asm.instr("D=A")
                self.asm.instr(f"@{segment_ptr}")
                self.asm.instr("A=D+M")
                self.asm.instr("D=M")
                push_d()
                self.asm.instr("RTN")

        def push_segment_a(segment_ptr):
            self.asm.instr("D=A")
            self.asm.instr(f"@{segment_ptr}")
            self.asm.instr("A=D+M")
            self.asm.instr("D=M")
            push_d()
            self.asm.instr("RTN")

        for index in range(self.SPECIALIZED_MAX_PUSH_SEGMENT + 1):
            self.asm.label(f"VM.push_local_{index}")
            push_segment("LCL", index)
            self.asm.label(f"VM.push_argument_{index}")
            push_segment("ARG", index)
            self.asm.label(f"VM.push_this_{index}")
            push_segment("THIS", index)
            self.asm.label(f"VM.push_that_{index}")
            push_segment("THAT", index)

        self.asm.label("VM.push_local")
        push_segment_a("LCL")
        self.asm.label("VM.push_argument")
        push_segment_a("ARG")
        self.asm.label("VM.push_this")
        push_segment_a("THIS")
        self.asm.label("VM.push_that")
        push_segment_a("THAT")

        # Pop to one of the memory segments:
        def pop_segment(segment_ptr, index):
            # TODO: specialize 0 and 1 to save two instr.
            self.asm.instr(f"@{index}")
            pop_segment_a(segment_ptr)

        def pop_segment_a(segment_ptr):
            # R15 = ptr + index
            self.asm.instr("D=A")
            self.asm.instr(f"@{segment_ptr}")
            self.asm.instr("D=D+M")
            self.asm.instr("@R15")
            self.asm.instr("M=D")
            # D = RAM[SP--]
            pop_d()
            # RAM[R15] = D
            self.asm.instr("@R15")
            self.asm.instr("A=M")
            self.asm.instr("M=D")
            self.asm.instr("RTN")

        for index in range(self.SPECIALIZED_MAX_POP_SEGMENT + 1):
            self.asm.label(f"VM.pop_local_{index}")
            pop_segment("LCL", index)
            self.asm.label(f"VM.pop_argument_{index}")
            pop_segment("ARG", index)
            self.asm.label(f"VM.pop_this_{index}")
            pop_segment("THIS", index)
            self.asm.label(f"VM.pop_that_{index}")
            pop_segment("THAT", index)

        self.asm.label("VM.pop_local")
        pop_segment_a("LCL")
        self.asm.label("VM.pop_argument")
        pop_segment_a("ARG")
        self.asm.label("VM.pop_this")
        pop_segment_a("THIS")
        self.asm.label("VM.pop_that")
        pop_segment_a("THAT")

        # Push/pop temp:

        for index in range(8):
            self.asm.label(f"VM.push_temp_{index}")
            self.asm.instr(f"@R{5+index}")
            self.asm.instr("D=M")
            push_d()
            self.asm.instr("RTN")

        for index in range(8):
            self.asm.label(f"VM.pop_temp_{index}")
            pop_d()
            self.asm.instr(f"@R{5+index}")
            self.asm.instr("M=D")
            self.asm.instr("RTN")

        # Push/pop pointer:

        self.asm.label("VM.push_pointer_0")
        self.asm.instr("@THIS")
        self.asm.instr("D=M")
        push_d()
        self.asm.instr("RTN")

        self.asm.label("VM.push_pointer_1")
        self.asm.instr("@THAT")
        self.asm.instr("D=M")
        push_d()
        self.asm.instr("RTN")

        self.asm.label("VM.pop_pointer_0")
        pop_d()
        self.asm.instr("@THIS")
        self.asm.instr("M=D")
        self.asm.instr("RTN")

        self.asm.label("VM.pop_pointer_1")
        pop_d()
        self.asm.instr("@THAT")
        self.asm.instr("M=D")
        self.asm.instr("RTN")

        # Push/pop static:

        self.asm.label("VM.push_static")
        self.asm.instr("D=M")
        push_d()
        self.asm.instr("RTN")

        self.asm.label("VM.pop_static")
        self.asm.instr("D=A")
        self.asm.instr("@R15")  # R15 = target address
        self.asm.instr("M=D")
        pop_d()
        self.asm.instr("@R15")
        self.asm.instr("A=M")
        self.asm.instr("M=D")
        self.asm.instr("RTN")

        # Binary ops:

        def binary(op):
            self.asm.instr("@SP")
            self.asm.instr("AM=M-1")  # update SP
            self.asm.instr("D=M")  # D = top
            self.asm.instr("A=A-1")  # Don't update SP again
            self.asm.instr(f"M={op}")
            self.asm.instr("RTN")

        self.asm.label("VM.add")
        binary("D+M")
        self.asm.label("VM.sub")
        binary("M-D")
        self.asm.label("VM.and")
        binary("D&M")
        self.asm.label("VM.or")
        binary("D|M")

        # Unary ops:

        def unary(op):
            self.asm.instr("@SP")
            self.asm.instr("A=M-1")
            self.asm.instr(f"M={op}")
            self.asm.instr("RTN")

        self.asm.label("VM.neg")
        unary("-M")
        self.asm.label("VM.not")
        unary("!M")

        # comparisons:

        def compare(op):
            label = self.asm.next_label(f"VM._{op.lower()}")
            end_label = self.asm.next_label(f"VM._{op.lower()}$end")

            # D = top, M = second from top, SP -= 1 (not 2!)
            self.asm.instr("@SP")
            self.asm.instr("AM=M-1")
            self.asm.instr("D=M")
            self.asm.instr("A=A-1")

            # Compare
            self.asm.instr("D=M-D")

            # Set result True, optimistically (since A is already loaded with the destination)
            self.asm.instr("M=-1")

            self.asm.instr(f"@{end_label}")
            self.asm.instr(f"D;J{op}")

            # Set result False
            self.asm.instr("@SP")
            self.asm.instr("A=M-1")
            self.asm.instr("M=0")

            self.asm.label(end_label)
            self.asm.instr("RTN")

        self.asm.label(f"VM.eq")
        compare("EQ")
        self.asm.label(f"VM.lt")
        compare("LT")
        self.asm.label(f"VM.gt")
        compare("GT")

        # if-goto:
        not_taken_label = "VM.if_goto$not_taken"
        self.asm.label("VM.if_goto")
        self.asm.instr("D=A")
        self.asm.instr("@R15")  # R15 = target address
        self.asm.instr("M=D")
        pop_d()
        self.asm.instr(f"@{not_taken_label}")
        self.asm.instr("D;JEQ")

        self.asm.instr("@R15")
        self.asm.instr("A=M")
        self.asm.instr("0;JMP")

        self.asm.label(not_taken_label)
        self.asm.instr("RTN")

        # function:

        for num_vars in range(self.SPECIALIZED_MAX_FUNCTION_NUM_LOCALS + 1):
            self.asm.label(f"VM.function_{num_vars}")
            self.asm.instr("@SP")
            self.asm.instr("A=M")
            for _ in range(num_vars):
                self.asm.instr("M=0")
                self.asm.instr("A=A+1")
            self.asm.instr("D=A")
            self.asm.instr("@SP")
            self.asm.instr("M=D")
            self.asm.instr("RTN")

        test_label = "VM.function$test"
        loop_label = "VM.function$loop"
        self.asm.label("VM.function")
        self.asm.instr("D=A")
        self.asm.instr(f"@{test_label}")
        self.asm.instr("0;JMP")

        self.asm.label(loop_label)
        self.asm.instr("@SP")
        self.asm.instr("M=M+1")
        self.asm.instr(
            "A=M-1")  # TODO: save a few instr. by updating RAM[SP] after
        self.asm.instr("M=0")
        self.asm.instr("D=D-1")
        self.asm.label(test_label)
        self.asm.instr(f"@{loop_label}")
        self.asm.instr("D;JGT")

        self.asm.instr("RTN")

        # return:

        self.asm.label("VM.return")

        # R13 = result
        pop_d()
        self.asm.instr("@R13")
        self.asm.instr("M=D")

        # SP = LCL
        self.asm.instr("@LCL")
        self.asm.instr("D=M")
        self.asm.instr("@SP")
        self.asm.instr("M=D")
        # R15 = ARG
        self.asm.instr("@ARG")
        self.asm.instr("D=M")
        self.asm.instr("@R15")
        self.asm.instr("M=D")
        # restore segment pointers from stack:
        pop_d()
        self.asm.instr("@THAT")
        self.asm.instr("M=D")
        pop_d()
        self.asm.instr("@THIS")
        self.asm.instr("M=D")
        pop_d()
        self.asm.instr("@ARG")
        self.asm.instr("M=D")
        pop_d()
        self.asm.instr("@LCL")
        self.asm.instr("M=D")
        # R14 = return address
        pop_d()
        self.asm.instr("@R14")
        self.asm.instr("M=D")
        # SP = R15
        self.asm.instr("@R15")
        self.asm.instr("D=M")
        self.asm.instr("@SP")
        self.asm.instr("M=D")
        # Push R13 (result)
        self.asm.instr("@R13")
        self.asm.instr("D=M")
        push_d()
        # jmp to R14
        self.asm.instr("@R14")
        self.asm.instr("A=M")
        self.asm.instr("0;JMP")

        # call:

        for num_args in range(self.SPECIALIZED_MAX_CALL_NUM_ARGS + 1):
            self.asm.label(f"VM.call_{num_args}")
            self.asm.instr(f"D=A")
            self.asm.instr(f"@R14")
            self.asm.instr(f"M=D")

            if num_args <= 1:
                self.asm.instr(f"@R13")
                self.asm.instr(f"M={num_args}")
            else:
                self.asm.instr(f"@{num_args}")
                self.asm.instr(f"D=A")
                self.asm.instr(f"@R13")
                self.asm.instr(f"M=D")

            self.asm.instr(f"@VM._call_common")
            self.asm.instr(f"0;JMP")

        self.asm.label(f"VM.call")
        # R14 = callee address
        self.asm.instr(f"D=A")
        self.asm.instr(f"@R14")
        self.asm.instr(f"M=D")
        # fall through to the common impl...

        self.asm.label(f"VM._call_common")

        # R15 = SP - (R13 + 1) (which will be the new ARG)
        self.asm.instr("@R13")
        self.asm.instr("D=M")
        self.asm.instr("@SP")
        self.asm.instr("D=M-D")
        self.asm.instr("D=D-1")
        self.asm.instr("@R15")
        self.asm.instr("M=D")

        # push four segment pointers:
        self.asm.instr("@LCL")
        self.asm.instr("D=M")
        push_d()
        self.asm.instr("@ARG")
        self.asm.instr("D=M")
        push_d()
        self.asm.instr("@THIS")
        self.asm.instr("D=M")
        push_d()
        self.asm.instr("@THAT")
        self.asm.instr("D=M")
        push_d()

        # LCL = SP
        # Note: setting LCL here (as opposed to in "function") feels wrong, but it makes the
        # state of the segment pointers consistent after each opcode, so it's easier to debug.
        self.asm.instr("@SP")
        self.asm.instr("D=M")
        self.asm.instr("@LCL")
        self.asm.instr("M=D")

        # ARG = R15
        self.asm.instr("@R15")
        self.asm.instr("D=M")
        self.asm.instr("@ARG")
        self.asm.instr("M=D")

        # JMP to R14 (the callee)
        self.asm.instr("@R14")
        self.asm.instr("A=M")
        self.asm.instr("0;JMP")

        # Used to push the return address in call ops:

        self.asm.label("VM._push_a")
        self.asm.instr("D=A")
        self.asm.instr("@SP")
        self.asm.instr("M=M+1")
        self.asm.instr("A=M-1")
        self.asm.instr("M=D")
        self.asm.instr("RTN")
コード例 #24
0
def compile_op(ast: Op, asm: AssemblySource):
    if ast.symbol == "+":
        asm.instr("add")
    elif ast.symbol == "-":
        asm.instr("sub")
    elif ast.symbol == "*":
        asm.instr("call Math.multiply 2")
    elif ast.symbol == "/":
        asm.instr("call Math.divide 2")
    elif ast.symbol == "&":
        asm.instr("and")
    elif ast.symbol == "|":
        asm.instr("or")
    elif ast.symbol == "<":
        asm.instr("lt")
    elif ast.symbol == ">":
        asm.instr("gt")
    elif ast.symbol == "=":
        asm.instr("eq")
    else:
        raise Exception(f"Unknown op: {ast.symbol}")
コード例 #25
0
def compile_expression(ast: ExpressionRec, symbol_table: SymbolTable,
                       asm: AssemblySource):
    if isinstance(ast, IntegerConstant):
        # Note: the standard parser won't emit these, but they can appear in a transformed AST.
        if ast.value < 0:
            asm.instr(f"push constant {-ast.value}")
            asm.instr("neg")
        else:
            asm.instr(f"push constant {ast.value}")

    elif isinstance(ast, StringConstant):
        # Yikes: this is 2 instructions per character, and it leaks the string.
        # What could the CPU do to make this less painful?
        # Should the compiler hoist these constants to the top of the function
        # and/or delete them for you?
        asm.instr(f"push constant {len(ast.value)}")
        asm.instr("call String.new 1")
        for c in ast.value:
            asm.instr(f"push constant {ord(c)}")
            asm.instr("call String.appendChar 2")

    elif isinstance(ast, KeywordConstant):
        if ast.value == True:
            # All bits set: ~0 == -1. The ALU can do M=-1 in one instruction, but the
            # VM doesn't let you say that.
            asm.instr("push constant 1")
            asm.instr("neg")
        elif ast.value == False:
            asm.instr("push constant 0")
        elif ast.value is None:
            asm.instr("push constant 0")
        elif ast.value == "this":
            if symbol_table.subroutine_kind not in ("constructor", "method"):
                raise Exception(
                    f'Undefined "this" in static context: {symbol_table.context()}'
                )
            asm.instr("push pointer 0")
        else:
            raise Exception(f"Unrecognized constant: {ast}")

    elif isinstance(ast, VarRef):
        asm.instr(
            f"push {symbol_table.kind_of(ast.name)} {symbol_table.index_of(ast.name)}"
        )

    elif isinstance(ast, ArrayRef):
        compile_expression(ast.array_index, symbol_table, asm)
        asm.instr(
            f"push {symbol_table.kind_of(ast.name)} {symbol_table.index_of(ast.name)}"
        )
        asm.instr("add")
        asm.instr("pop pointer 1")
        asm.instr("push that 0")

    elif isinstance(ast, SubroutineCall):
        # Static call (e.g. Sys.error):
        if ast.class_name is not None:
            for arg in ast.args:
                compile_expression(arg, symbol_table, asm)
            asm.instr(f"call {ast.class_name}.{ast.sub_name} {len(ast.args)}")

        # Method call, on a named object (e.g. str.charAt()):
        elif ast.var_name is not None:
            target_class = symbol_table.type_of(ast.var_name)
            compile_expression(VarRef(ast.var_name), symbol_table, asm)
            for arg in ast.args:
                compile_expression(arg, symbol_table, asm)
            asm.instr(f"call {target_class}.{ast.sub_name} {len(ast.args)+1}")

        # Method call on the implicit `this` (e.g. draw()):
        else:
            if symbol_table.subroutine_kind not in ("constructor", "method"):
                raise Exception(
                    f'Tried to use implicit "this" in static (function) context: {symbol_table.class_name}.{symbol_table.subroutine_name}'
                )
            target_class = symbol_table.class_name
            compile_expression(KeywordConstant("this"), symbol_table, asm)
            for arg in ast.args:
                compile_expression(arg, symbol_table, asm)
            asm.instr(f"call {target_class}.{ast.sub_name} {len(ast.args)+1}")

    elif isinstance(ast, BinaryExpression):
        compile_expression(ast.left, symbol_table, asm)
        compile_expression(ast.right, symbol_table, asm)
        compile_op(ast.op, asm)

    elif isinstance(ast, UnaryExpression):
        compile_expression(ast.expr, symbol_table, asm)
        if ast.op.symbol == "-":
            asm.instr("neg")  # Note: same symbol, different vm op.
        elif ast.op.symbol == "~":
            asm.instr("not")
        else:
            raise Exception(f"Unknown unary op: {ast.op}")

    else:
        raise Exception(f"Unexpected expression: {ast}")
コード例 #26
0
def compile_subroutineDec(ast: SubroutineDec, symbol_table: SymbolTable,
                          asm: AssemblySource):
    if not _is_terminal_function(ast, symbol_table) and not _has_final_return(
            ast.body.statements):
        raise Exception(
            f'Missing "return" in {symbol_table.class_name}.{ast.name}')

    handle_subroutine_var_declarations(ast, symbol_table)

    # print(symbol_table)

    if ast.kind == "constructor":
        instance_word_count = symbol_table.count("this")
        num_vars = symbol_table.count("local")

        if ast.name != "new":
            raise Exception(f'Must be named "new": {symbol_table.context()}')
        elif ast.result != symbol_table.class_name:
            raise Exception(
                f'Result type does not match: {symbol_table.context()}')
        elif ast.body.statements[-1] != ReturnStatement(
                KeywordConstant("this")):
            raise Exception(
                f'Does not return "this": {symbol_table.context()}')

        asm.instr(f"function {symbol_table.class_name}.{ast.name} {num_vars}")

        asm.instr(f"push constant {instance_word_count}")
        asm.instr(f"call Memory.alloc 1")
        asm.instr("pop pointer 0")

        for stmt in ast.body.statements:
            compile_statement(stmt, symbol_table, asm)

    elif ast.kind == "function":
        num_vars = max(1,
                       symbol_table.count("local"))  # space for return values?

        asm.instr(f"function {symbol_table.class_name}.{ast.name} {num_vars}")

        for stmt in ast.body.statements:
            compile_statement(stmt, symbol_table, asm)

    elif ast.kind == "method":
        num_vars = max(1,
                       symbol_table.count("local"))  # space for return values?

        asm.instr(f"function {symbol_table.class_name}.{ast.name} {num_vars}")

        # Stash the (implicit) `this` argument:
        asm.instr(f"push argument 0")
        asm.instr(f"pop pointer 0")

        for stmt in ast.body.statements:
            compile_statement(stmt, symbol_table, asm)

    else:
        raise Exception(f"Unexpected subroutine kind: {ast.kind}")
コード例 #27
0
ファイル: lazy.py プロジェクト: mossprescott/pynand
    def __init__(self):
        self.asm = AssemblySource()

        solved_07.Translator.__init__(self, self.asm)

        self.top_in_d = False
コード例 #28
0
ファイル: lazy.py プロジェクト: mossprescott/pynand
class Translator(solved_07.Translator):
    def __init__(self):
        self.asm = AssemblySource()

        solved_07.Translator.__init__(self, self.asm)

        self.top_in_d = False

    def finish(self):
        self._fix_stack()

    def push_constant(self, value):
        self._fix_stack()

        # TODO: this is _costing_ one instruction when the following instr. can't take it
        # from D, by doing D=0/1 ... M=D instead of folding the constant in.
        self.asm.start(f"push constant {value}")
        if value <= 1:
            self.asm.instr(f"D={value}")
        else:
            self.asm.instr(f"@{value}")
            self.asm.instr(f"D=A")
        self.top_in_d = True

    def lt(self):
        self._fix_stack()
        solved_07.Translator.lt(self)

    def eq(self):
        self._fix_stack()
        solved_07.Translator.eq(self)

    def gt(self):
        self._fix_stack()
        solved_07.Translator.gt(self)

    def _binary(self, opcode, op):
        if self.top_in_d:
            self.asm.start(opcode + " (from D)")
            self.asm.instr("@SP")
            self.asm.instr("AM=M-1")
            self.asm.instr(f"D={op}")
            # Note: this isn't really a win if the next instruction is going to just push.
        else:
            solved_07.Translator._binary(self, opcode, op)

    def _unary(self, opcode, op):
        if self.top_in_d:
            self.asm.start(opcode + " (from D)")
            self.asm.instr(f"D={op.replace('M', 'D')}")
            # Note: and it stays in D
        else:
            solved_07.Translator._unary(self, opcode, op)

    def _pop_segment(self, segment_name, segment_ptr, index):
        # Believe it or not, using this unrolled loop for indexes all the way to 13
        # makes the code smaller overall (empirically determined.)
        if self.top_in_d and index <= 13:
            self.asm.start(f"pop {segment_name} {index} (from D)")
            self.asm.instr(f"@{segment_ptr}")
            if index == 0:
                self.asm.instr("A=M")
            else:
                self.asm.instr("A=M+1")
                for _ in range(index - 1):
                    self.asm.instr("A=A+1")
            self.asm.instr("M=D")
            self.top_in_d = False
        else:
            self._fix_stack()
            solved_07.Translator._pop_segment(self, segment_name, segment_ptr,
                                              index)

    def pop_temp(self, index):
        assert 0 <= index < 8
        if not self.top_in_d:  # Beautiful: only this conditional is added
            self.asm.start(f"pop temp {index}")
            self._pop_d()
        else:
            self.asm.start(f"pop temp {index} (from D)")
            self.top_in_d = False
        self.asm.instr(f"@R{5+index}")
        self.asm.instr("M=D")

    def _push_segment(self, segment_name, segment_ptr, index):
        self._fix_stack()

        self.asm.start(f"push {segment_name} {index}")
        if index == 0:
            self.asm.instr(f"@{segment_ptr}")
            self.asm.instr("A=M")
            self.asm.instr("D=M")
        elif index == 1:
            self.asm.instr(f"@{segment_ptr}")
            self.asm.instr("A=M+1")
            self.asm.instr("D=M")
        elif index == 2:
            self.asm.instr(f"@{segment_ptr}")
            self.asm.instr("A=M+1")
            self.asm.instr("A=A+1")
            self.asm.instr("D=M")
        else:
            self.asm.instr(f"@{index}")
            self.asm.instr("D=A")
            self.asm.instr(f"@{segment_ptr}")
            self.asm.instr("A=D+M")
            self.asm.instr("D=M")
        self.top_in_d = True

    def push_temp(self, index):
        assert 0 <= index < 8

        self._fix_stack()

        self.asm.start(f"push temp {index}")
        self.asm.instr(f"@R{5+index}")
        self.asm.instr("D=M")
        self.top_in_d = True

    def pop_pointer(self, index):
        if self.top_in_d:
            self.asm.start(f"pop pointer {index} (from D)")
            segment_ptr = ("THIS", "THAT")[index]
            self.asm.instr(f"@{segment_ptr}")
            self.asm.instr("M=D")
            self.top_in_d = False
        else:
            solved_07.Translator.pop_pointer(self, index)

    def push_pointer(self, index):
        self._fix_stack()

        self.asm.start(f"push pointer {index}")
        segment_ptr = ("THIS", "THAT")[index]
        self.asm.instr(f"@{segment_ptr}")
        self.asm.instr("D=M")
        self.top_in_d = True

    def pop_static(self, index):
        if self.top_in_d:
            self.asm.start(f"push static {index} (from D)")
            self.asm.instr(f"@{self.class_namespace}.static{index}")
            self.asm.instr("M=D")
            self.top_in_d = False
        else:
            solved_07.Translator.pop_static(self, index)

    def push_static(self, index):
        self._fix_stack()

        self.asm.start(f"push static {index}")
        self.asm.instr(f"@{self.class_namespace}.static{index}")
        self.asm.instr("D=M")
        self.top_in_d = True

    def label(self, name):
        self._fix_stack()
        solved_07.Translator.label(self, name)

    def if_goto(self, name):
        if self.top_in_d:
            self.asm.start(f"if-goto {name} (from D)")
            self.asm.instr(f"@{self.function_namespace}${name}")
            self.asm.instr("D;JNE")
            self.top_in_d = False
        else:
            solved_07.Translator.if_goto(self, name)

    def goto(self, name):
        self._fix_stack()
        solved_07.Translator.goto(self, name)

    def function(self, class_name, function_name, num_vars):
        assert not self.top_in_d
        solved_07.Translator.function(self, class_name, function_name,
                                      num_vars)

    def return_op(self):
        # TODO: an alt. return handler?
        self._fix_stack()
        solved_07.Translator.return_op(self)

    def call(self, class_name, function_name, num_args):
        self._fix_stack()
        solved_07.Translator.call(self, class_name, function_name, num_args)

    def _fix_stack(self):
        if self.top_in_d:
            self._push_d()
            self.top_in_d = False