def write_push_virtual(segment, location): primitive_ml.write_const_to(location, 'D') primitive_ml.point_to_segment(segment) primitive_ml.write_c_command('D', 'M+D') primitive_ml.point_to_segment(segment, True) primitive_ml.write_c_command('A', 'D') primitive_ml.write_c_command('D', 'M') primitive_ml.write_to_stack('D')
def write_push_command(line_def): if line_def['command_type'] != vm_command_type.Push: return segment = line_def['arg'][0] location = line_def['arg'][1] if segment == 'constant': add_line('@' + location) add_line('D=A') primitive_ml.write_to_stack('D') elif segment == 'temp': write_push_temp(location) elif segment == 'pointer': write_push_pointer(location) elif segment == 'static': write_push_static(location) else: write_push_virtual(segment, location)
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 write_push_static(location): global file_name primitive_ml.point_static(file_name, location) primitive_ml.write_c_command('D', 'M') primitive_ml.write_to_stack('D')
def write_push_pointer(location): primitive_ml.point_to_pointer(int(location)) primitive_ml.write_c_command('D', 'M') primitive_ml.write_to_stack('D')
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 write_function(name, var_count): primitive_ml.add_symbol_link(name) while var_count > 0: primitive_ml.write_to_stack('0') var_count -= 1 return