lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Hello, Lua Hackers,

2010/9/19 Tom N Harris <telliamed@whoopdedo.org>
- The script code and compiled binary is shorter than other notation. So
the code will run fast.


Can you give an example of how the bytecode would be shorter? As far as I can tell, the same instructions should be generated as using an explicit if-then-else.

I just compiled following code by luac with my patch:

# cat b.lua
x = if a then b else c

y = a and b or c

if a then z = b else z = c end
# src/luac -l b.lua

main <b.lua:0,0> (24 instructions, 96 bytes at 0x10e94c8)
0+ params, 2 slots, 0 upvalues, 0 locals, 6 constants, 0 functions
1 [1] GETGLOBAL 0 -2 ; a
2 [1] TEST     0 0 0
3 [1] JMP       2 ; to 6
4 [1] GETGLOBAL 0 -3 ; b
5 [1] JMP       1 ; to 7
6 [1] GETGLOBAL 0 -4 ; c
7 [1] SETGLOBAL 0 -1 ; x
8 [3] GETGLOBAL 0 -2 ; a
9 [3] TEST     0 0 0
10 [3] JMP       3 ; to 14
11 [3] GETGLOBAL 0 -3 ; b
12 [3] TEST     0 0 1
13 [3] JMP       1 ; to 15
14 [3] GETGLOBAL 0 -4 ; c
15 [3] SETGLOBAL 0 -5 ; y
16 [5] GETGLOBAL 0 -2 ; a
17 [5] TEST     0 0 0
18 [5] JMP       3 ; to 22
19 [5] GETGLOBAL 0 -3 ; b
20 [5] SETGLOBAL 0 -6 ; z
21 [5] JMP       2 ; to 24
22 [5] GETGLOBAL 0 -4 ; c
23 [5] SETGLOBAL 0 -6 ; z
24 [5] RETURN   0 1

So, source line 1 was compiled to 7 lines binary, line 3 was 8 lines, line 5 was 9 lines.

Thank you,
Ryota Hirose