[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Behavior of logical operators
- From: GrayFace <sergroj@...>
- Date: Thu, 17 Mar 2011 00:54:56 +0600
On 13.03.2011 19:06, Gavin Wraith wrote:
I use lua5.2-alpha with the following patch (marked by
#if 0 ... #endif) in lcode.c which seems to cure the problem:
It's the one from http://www.lua.org/bugs.html#5.1.4-3
I have it in my 5.1.4 and it doesn't help.
I tried to dig into the problem a little, it's my first experience with
Lua VM instructions. Here's the code generated for this function:
return (1 or 2) and true or false
1 [3] LOADK 0 -1 ; 1 -- R(0) := 1
2 [3] TEST 0 0 1 -- do nothing, go to 3
3 [3] JMP 5 ; to 9 -- go to 9
4 [3] JMP 3 ; to 8
5 [3] LOADBOOL 0 0 0
6 [3] JMP 2 ; to 9
7 [3] LOADBOOL 0 0 1
8 [3] LOADBOOL 0 1 0
9 [3] RETURN 0 2 -- return R(0)
10 [4] RETURN 0 1
constants (1) for 0162CC10:
1 1
To me it looks like the instruction #3 is a leftover from (1 or 2), but
that's just a blind guess.
From my further tests it looks like the patch does more harm than good.
It doesn't fix the problem of and/or optimization, but just disables it
under certain conditions. As a result, this common construction has a
useless comparison in its generated code in case 'aa' is true:
return aa and true or false
1 [2] GETUPVAL 0 0 ; aa
2 [2] TEST 0 0 0
3 [2] JMP 3 ; to 7
4 [2] LOADBOOL 0 1 0
5 [2] TEST 0 0 1 -- <= useless
6 [2] JMP 1 ; to 8
7 [2] LOADBOOL 0 0 0
8 [2] RETURN 0 2
9 [3] RETURN 0 1
upvalues (1) for 0162CC10:
0 aa
Compare it to unpatched 5.1.4:
1 [3] GETUPVAL 0 0 ; aa
2 [3] TEST 0 0 0
3 [3] JMP 1 ; to 5
4 [3] JMP 3 ; to 8
5 [3] LOADBOOL 0 0 0
6 [3] JMP 2 ; to 9
7 [3] LOADBOOL 0 0 1
8 [3] LOADBOOL 0 1 0
9 [3] RETURN 0 2
10 [4] RETURN 0 1
upvalues (1) for 0162CC10:
0 aa
Some useless jumping around here, but no useless TEST instructions.
I also found that "A no-frills introduction to Lua 5 VM instructions"
has wrong description for TEST instruction.
It says:
"if (R(B) <=> C) then R(A) := R(B) else pc++"
and treats "<=>" sign as "~=". In fact, here's how it works in 5.1.4:
"if (R(B) == C) then R(A) := R(B) else pc++".
--
Best regards,
Sergey Rozhenko mailto:sergroj@mail.ru