Lua ll3 VM PDF
Lua ll3 VM PDF
uage
g
an
l
the
Lua
1
W HAT IS L UA ?
• Conventional syntax:
2
W HAT IS L UA ? ( CONT.)
3
W HY L UA ?
• Light
◦ simple and small language, with few concepts
◦ core with approximately 60K, complete executable with 140K
• Portable
◦ written in “clean C”
◦ runs in PalmOS, EPOC (Symbian), Brew (Qualcomm),
Playstation II, XBox, embedded systems, mainframes, etc.
• Efficient
◦ see benchmarks
• Easy to embed
◦ C/C++, Java, Fortran, Ruby, OPL (EPOC), C#
4
S OME A PPLICATIONS
• Games
◦ LucasArts, BioWare, Microsoft, Relic Entertainment, Absolute
Studios, Monkeystone Games, etc.
• Other Uses
◦ tomsrtbt - ”The most Linux on one floppy disk”
◦ Crazy Ivan Robot (champion of RoboCup 2000/2001 in Denmark)
◦ chip layouts (Intel)
◦ APT-RPM (Conectiva & United Linux)
◦ Space Shuttle Hazardous Gas Detection System
(ASRC Aerospace)
5
P OLL FROM GAMEDEV. NET
6
V IRTUAL M ACHINE
3 GETLOCAL 0 ; a
4 GETLOCAL 1 ; lim
5 JMPGE 4 ; to 10
• Example in Lua 4.0: 6 GETLOCAL 0 ; a
7 ADDI 1
8 SETLOCAL 0 ; a
9 JMP -7 ; to 3
8
A NOTHER M ODEL FOR V IRTUAL M ACHINES
9
I NSTRUCTION F ORMATS
• Three-argument format, used for most operators
◦ binary operators & indexing
31 23 22 14 13 6 5 0
C B A OP
11
I NSTRUCTION F ORMATS
31 14 13 6 5 0
Bx A OP
12
I NSTRUCTION E XAMPLES
GETGLOBAL 0 260 ; a = x
SETGLOBAL 1 260 ; x = t
LT 0 259 ; a < 1 ?
JMP * 13
◦ conceptually, LT skips the next instruction (always a jump) if the test fails. In the
current implementation, it does the jump if the test succeed.
13
C ODE E XAMPLE
(all variables are local)
while i<lim do a[i] = 0 end
-- Lua 4.0
-- Lua 5.0
2 GETLOCAL 2 ; i
3 GETLOCAL 1 ; lim 2 JMP * 1 ; to 4
4 JMPGE 5 ; to 10 3 SETTABLE 0 2 256 ; a[i] = 0
5 GETLOCAL 0 ; a 4 LT * 2 1 ; i < lim?
6 GETLOCAL 2 ; i 5 JMP * -3 ; to 3
7 PUSHINT 0
8 SETTABLE
9 JMP -8 ; to 2
14
I MPLEMENTATION OF TABLES
• Each table may have two parts, a “hash” part and an “array” part
n 3 100
nil 200
300
nil
Header
15
TABLES : H ASH PART
16
TABLES : A RRAY PART
n 3 100
100
nil 200
n 3 200
5 500 300
nil 300
nil nil
nil
500
nil
Header nil
Header nil
17
C OMPUTING THE SIZE OF A TABLE
• The array part has size N , where N satisfies the following rules:
◦ N is a power of 2
◦ the table contains at least N/2 integer keys in the interval [1, N ]
◦ the table has at least one integer key in the interval [N/2 + 1, N ]
18
C OMPUTING THE SIZE OF A TABLE ( CONT.)
19
C OMPUTING THE SIZE OF A TABLE ( CONT.)
total = 0
bestsize = 0
for i=0,32 do
if a[i] > 0 then
total += a[i]
if total >= 2^(i-1) then
bestsize = i
end
end
end
20
P ERFORMANCE
◦ all test code copied from The Great Computer Language Shootout
◦ Lua 5’ is Lua 5.0 without table-array optimization, tail calls, and dynamic stacks
(related to coroutines).
21
F INAL R EMARKS
• Requirements
◦ no more than 256 local variables and temporaries
• Main gains:
◦ avoid moves of local variables and constants
◦ fewer instructions per task
◦ potential gain with CSE optimizations
22