예제 #1
0
def restore_vm_symbols(symbol):
    frame_minus()
    primitive_ml.add_line('@' + symbol)
    primitive_ml.add_line('M=D')
예제 #2
0
def write_if(location):
    primitive_ml.read_stack_to('D')
    primitive_ml.add_line('@' + location)
    primitive_ml.add_line('D;JNE')
예제 #3
0
def read_symbol_to_stack(symbol):
    primitive_ml.add_line('@' + symbol)
    primitive_ml.write_c_command('D', 'M')
    primitive_ml.write_to_stack('D')
예제 #4
0
def go_to_return_address():
    primitive_ml.add_line('@RET')
    primitive_ml.add_line('A=M')
    primitive_ml.add_line('0;JMP')
예제 #5
0
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')
예제 #6
0
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
예제 #7
0
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')
예제 #8
0
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')
예제 #9
0
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')
예제 #10
0
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')
예제 #11
0
def restore_sp_of_caller():
    primitive_ml.add_line('@ARG')
    primitive_ml.add_line('D=M')
    primitive_ml.set_stack_pointer('D+1')