[BUG]Wrong trace of goto

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

[BUG]Wrong trace of goto

actboy168@gmail.com
For this code:
> debug.sethook(function(...) print(...) end, "l")
> if false then
>     goto EXIT
>     A = 1
> end
> ::EXIT::

The output is:
line    3
line    6

I think it should be
line    2
line    6

I got the same result in both lua5.4.1 and lua5.3.6.

-- actboy168
Reply | Threaded
Open this post in threaded view
|

Re: [BUG]Wrong trace of goto

Luiz Henrique de Figueiredo
> > debug.sethook(function(...) print(...) end, "l")
> > if false then
> >     goto EXIT
> >     A = 1
> > end
> > ::EXIT::
>
> The output is:
> line    3
> line    6
>
> I think it should be
> line    2
> line    6

This is not a bug. It's a consequence of an optimization introduced by
the compiler. Replace false by foo and see the difference.
Reply | Threaded
Open this post in threaded view
|

Re: [BUG]Wrong trace of goto

Egor Skriptunoff-2
On Fri, Oct 23, 2020 at 8:56 PM Luiz Henrique de Figueiredo wrote:
> > debug.sethook(function(...) print(...) end, "l")
> > if false then
> >     goto EXIT
> >     A = 1
> > end
> > ::EXIT::
>
> The output is:
> line    3
> line    6
>
> I think it should be
> line    2
> line    6

This is not a bug. It's a consequence of an optimization introduced by
the compiler. Replace false by foo and see the difference.


After "false" is replaced with "foo" we still see the bug:

$ luac -l -p -
if foo then
  goto EXIT
  A=2
end
::EXIT::

main <stdin:0,0> (7 instructions at 0x25f7840)
0+ params, 2 slots, 1 upvalue, 0 locals, 3 constants, 0 functions
    1    [1]    VARARGPREP    0
    2    [2]    GETTABUP     0 0 0    ; _ENV "foo"
    3    [2]    TEST         0 1
    4    [2]    JMP          2    ; to 7
    5    [2]    JMP          1    ; to 7
    6    [3]    SETTABUP     0 1 2k    ; _ENV "A" 2
    7    [5]    RETURN       0 1 1    ; 0 out


In the Lua script: the global "foo" is accessed at the first line.
In the bytecode: "GETTABUP _ENV foo" is marked as line #[2].
Reply | Threaded
Open this post in threaded view
|

Re: [BUG]Wrong trace of goto

Luiz Henrique de Figueiredo
> After "false" is replaced with "foo" we still see the bug:

I was running 5.3. It seems that 5.4 behaves differently.

% nl -ba a
1     debug.sethook(print, "l")
2     if foo then
3       goto EXIT
4       A=2
5     end
6     ::EXIT::

% lua53 -v a
Lua 5.3.6  Copyright (C) 1994-2020 Lua.org, PUC-Rio
line 2
line 3
line 6

% lua54 -v a
Lua 5.4.1  Copyright (C) 1994-2020 Lua.org, PUC-Rio
line 3
line 6