def restore_vm_symbols(symbol): frame_minus() primitive_ml.add_line('@' + symbol) primitive_ml.add_line('M=D')
def write_if(location): primitive_ml.read_stack_to('D') primitive_ml.add_line('@' + location) primitive_ml.add_line('D;JNE')
def read_symbol_to_stack(symbol): primitive_ml.add_line('@' + symbol) primitive_ml.write_c_command('D', 'M') primitive_ml.write_to_stack('D')
def go_to_return_address(): primitive_ml.add_line('@RET') primitive_ml.add_line('A=M') primitive_ml.add_line('0;JMP')
def frame_descrese_to_d(): primitive_ml.add_line('@FRAME') primitive_ml.add_line('M=M-1') primitive_ml.add_line('A=M') primitive_ml.write_c_command('D', 'M')
def write_call(name, var_count): # push return-address (using label below) global return_counter return_label = 'return.' + str(return_counter) primitive_ml.add_line('@' + return_label) primitive_ml.write_c_command('D', 'A') primitive_ml.write_to_stack('D') # push lcl read_symbol_to_stack('LCL') read_symbol_to_stack('ARG') read_symbol_to_stack('THIS') read_symbol_to_stack('THAT') # reposition arg primitive_ml.add_line('@SP') primitive_ml.write_c_command('D', 'M') primitive_ml.add_line('@ARG') primitive_ml.write_c_command('M', 'D') primitive_ml.add_line('@5') primitive_ml.write_c_command('D', 'A') primitive_ml.add_line('@ARG') primitive_ml.write_c_command('M', 'M-D') primitive_ml.add_line('@' + str(var_count)) primitive_ml.write_c_command('D', 'A') primitive_ml.add_line('@ARG') primitive_ml.write_c_command('M', 'M-D') # reposition lcl primitive_ml.add_line('@SP') primitive_ml.write_c_command('D', 'M') primitive_ml.add_line('@LCL') primitive_ml.write_c_command('M', 'D') # goto function primitive_ml.unconditional_jump(name) # declare return label primitive_ml.add_symbol_link(return_label) return_counter += 1 return
def frame_minus(): primitive_ml.add_line('@FRAME') primitive_ml.add_line('M=M-1') primitive_ml.add_line('A=M') primitive_ml.add_line('D=M')
def save_return_address(): primitive_ml.add_line('@5') primitive_ml.add_line('D=D-A') primitive_ml.add_line('A=D') primitive_ml.add_line('D=M') primitive_ml.add_line('@RET') primitive_ml.add_line('M=D')
def frame(): primitive_ml.add_line('@LCL') primitive_ml.write_c_command('D', 'M') primitive_ml.add_line('@FRAME') d_to_m() primitive_ml.add_line('D=M')
def reposition_return_val(): primitive_ml.read_stack_to('D') primitive_ml.add_line('@ARG') primitive_ml.add_line('A=M') primitive_ml.add_line('M=D')
def restore_sp_of_caller(): primitive_ml.add_line('@ARG') primitive_ml.add_line('D=M') primitive_ml.set_stack_pointer('D+1')