ams-hackers / gbforth

👾 A Forth-based Game Boy development kit
https://gbforth.org
MIT License
128 stars 24 forks source link

Memory leak on unreachable IR #281

Closed davazp closed 4 years ago

davazp commented 6 years ago

Right now, some words like again produces IR components that are unreachable

https://github.com/ams-hackers/gbforth/blob/ad56fcac4e1f77d5edf7efb74ac3577ef1542f28/src/compiler/cross.fs#L221

That is to say, an IR component is created, code is emitted to it, but it will never be actually emitted, as no other IR component references it.

For example,

: f begin ." hello" again drop ;

the last drop will never be executed.

The problem is that IR components are released by traversing the graph of IRs. Being unreachable means that they will never be released.

This can be solved by having a special value or special IR where all unreachable code can be emitted and ensuring that this is released at some point.

A warning could also be emitted if some code is compiled into the unreachable IR.