示例#1
0
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")
示例#2
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