diff --git a/04_semantics_and_running/main.py b/04_semantics_and_running/main.py index 141fb5d..6689b51 100644 --- a/04_semantics_and_running/main.py +++ b/04_semantics_and_running/main.py @@ -586,9 +586,16 @@ class CompileData: break saved_registers = list(saved_registers) + does_call = False + for instruction in code: + if instruction.opcode == 'call': + does_call = True + break + code_str += name + ':\n' - code_str += ' pushq %rbp\n' - code_str += ' movq %rsp, %rbp\n' + if does_call: + code_str += ' pushq %rbp\n' + code_str += ' movq %rsp, %rbp\n' for reg in saved_registers: code_str += f' pushq {reg}\n' if len(saved_registers) % 2 != 0: @@ -602,7 +609,8 @@ class CompileData: code_str += ' addq $8, %rsp\n' for reg in reversed(saved_registers): code_str += f' popq {reg}\n' - code_str += ' leave\n' + if does_call: + code_str += ' leave\n' code_str += ' ret\n' code_str += '\n'