wiz-lang / wiz

A high-level assembly language for writing homebrew software and games on retro console platforms.
http://wiz-lang.org/
Other
405 stars 40 forks source link

Fix tail-calls of inline functions #123

Open Bananattack opened 3 years ago

Bananattack commented 3 years ago

Currently it is not possible to tail call an inline function. It will compile but generate incorrect code.

inline func beep() {
    (0xC0 as func)();
}

func call_beep() {
    return beep();
}

In this case, tail-call optimization doesn't apply as there is no stack entry to bypass. However, it should still behave the same as the original call (which is inlined), terminated by a return statement, when no tail-call ois possible.

This should at the very least, generate the following rather than invalid code:

call 0xC0
ret

or generate an error, failing that.

Potentially if tail-calls get optimized at the IR level (which is after inline functions are expanded), then this could generate the more optimal

jp 0xC0

branch instruction instead.