Fix check for when stack frame is needed

This commit is contained in:
Bananymous 2024-04-30 00:05:52 +03:00
parent c37e48e8b2
commit 3fa0e5f655
1 changed files with 14 additions and 7 deletions

View File

@ -586,14 +586,21 @@ class CompileData:
break break
saved_registers = list(saved_registers) saved_registers = list(saved_registers)
does_call = False # Stack frame is needed for builtin functions, functions with local variables
# and functions that call other functions
needs_stack_frame = False
if name.startswith('__builtin'):
needs_stack_frame = True
elif name in self.sem_data.callables and len(self.sem_data.callables[name].children_formals) > 0:
needs_stack_frame = True
else:
for instruction in code: for instruction in code:
if instruction.opcode == 'call': if instruction.opcode == 'call':
does_call = True needs_stack_frame = True
break break
code_str += name + ':\n' code_str += name + ':\n'
if does_call: if needs_stack_frame:
code_str += ' pushq %rbp\n' code_str += ' pushq %rbp\n'
code_str += ' movq %rsp, %rbp\n' code_str += ' movq %rsp, %rbp\n'
for reg in saved_registers: for reg in saved_registers:
@ -609,7 +616,7 @@ class CompileData:
code_str += ' addq $8, %rsp\n' code_str += ' addq $8, %rsp\n'
for reg in reversed(saved_registers): for reg in reversed(saved_registers):
code_str += f' popq {reg}\n' code_str += f' popq {reg}\n'
if does_call: if needs_stack_frame:
code_str += ' leave\n' code_str += ' leave\n'
code_str += ' ret\n' code_str += ' ret\n'
code_str += '\n' code_str += '\n'