Support for goto and labels added

  • From: Mike Pall <mike-1209@xxxxxxxxxx>
  • To: luajit@xxxxxxxxxxxxx
  • Date: Sun, 16 Sep 2012 19:42:58 +0200

LuaJIT git HEAD now contains support for goto and labels,
following the Lua 5.2 syntax and semantics. I.e.:

  goto name

  ::name::

[In the default Lua 5.1-compatible mode, "goto" is accepted as a
statement *or* a variable name, to avoid breaking compatibility.]

As has been pointed out on the Lua mailing list: goto is *not* a
substitute for regular loops and control-flow constructs. But it's
useful to implement "continue", to break out of deep loop nestings,
to implement state machines or simulators and when using Lua code
as a target language for cross-language translators.

There's a small caveat: the region selection algorithm of the JIT
compiler relies on detecting "natural" loops and compiling them
first. Since that would be pretty expensive to detect for gotos,
I've decided to use a simple heuristic instead: only a backwards
goto in the same scope is treated like a loop header and can
trigger JIT-compilation. OTOH if recording of an outer loop hits
a backwards goto, it's unrolled (assuming this is not an actual
loop). Any other gotos do not trigger JIT-compilation, but may be
compiled (i.e. ignored/followed) as part of some other loop.

I'm open to suggestions on improving these heuristics, based on
actual use cases.

So, everyone who has asked for goto support, please test and
report back if it actually does what you expected it to do.

--Mike

Other related posts: