Skip to content

Tail-call optimization for self-recursive functions #38

Description

@NightmarePog

Deep self-recursive functions will stack overflow in Lua because each call adds a frame. Lazarus has no way to avoid this right now.

The fix is to detect tail calls — calls that are the last thing a function does before returning — and lower them to a `goto` loop in Lua instead of an actual call. Lua's `goto` makes this clean to implement and produces the same semantics with no stack growth.

This is mostly a codegen change. A tail call `return f(args)` where `f` is the current function gets replaced with an argument reassignment and a `goto` back to the top of the function body.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions