Beruflich Dokumente
Kultur Dokumente
0
Roberto Ierusalimschy Luiz Henrique de Figueiredo Waldemar Celes
e uag g an
the l
Lua
1
M AIN G OALS
Portability
ANSI C and C++ avoid dark corners
Simplicity
small size
Efciency
2
O BJECTS
Pointed by eld GCObject *gc in values Union with common head:
S TRINGS
Internalized
save space save time for comparison/hashing more expensive when creating strings
I MPLEMENTATION OF TABLES
Each table may have two parts, a hash part and an array part Example:
{n = 3; 100, 200, 300}
n nil
Header
6
0
key value link
val
nil nil
0 nil
val
insert key 4
val
Avoid secondary collisions, moving old elements when inserting new ones
key value link key value link
0 nil nil 4
val
0 nil 4
val
val val
val
insert key 3
10,000 elements
n nil 5 nil
500
300 nil
Header
The array part has size N , where N satises 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 ]
Basic algorithm: to build an array where ai is the number of integer keys in the interval (2i1, 2i ]
array needs only 32 entries
12
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
13
V IRTUAL M ACHINE
while a<lim do a=a+1 end 3 4 5 6 7 8 9 GETLOCAL GETLOCAL JMPGE GETLOCAL ADDI SETLOCAL JMP 0 1 4 0 1 0 -7 ; ; ; ; a lim to 10 a
; a ; to 3
14
Overhead to decode more complex instruction is compensated by fewer instructions registers for each function are allocated on the execution stack at activation time
large number of registers (up to 256) simplies code generation
15
I NSTRUCTION F ORMATS
Three-argument format, used for most operators
31 23 22 14 13 6 5 0
OP
All instructions have a 6-bit opcode Operand A refers to a register Operands B and C can refer to a register or a constant
a constant can be any Lua value, stored in an array of constants
I NSTRUCTION E XAMPLES
0 0 0 0
259
assuming that the variable a is in register 0, t is in register 1, the number 1 is at index 3 in the array of constants, and the string "x" is at index 4.
17
I NSTRUCTION F ORMATS
There is an alternative format for instructions that do not need three arguments or with arguments that do not t in 9 bits
used for jumps, access to global variables, access to constants
31
14 13
6 5
Bx
OP
18
I NSTRUCTION E XAMPLES
; a = x ; x = t ; a < 1 ?
assuming that the variable a is in register 0, t is in register 1, the number 1 is at index 3 in the array of constants, and the string "x" is at index 4. conceptually, LT skips the next instruction (always a jump) if the test fails. In the current implementation, it does the jump if the test succeeds. jumps interpret the Bx eld as a signed offset (in excess-217)
19
C ODE E XAMPLE
(all variables are local)
while i<lim do a[i] = 0 end -- Lua 4.0 2 3 4 5 6 7 8 9 GETLOCAL GETLOCAL JMPGE GETLOCAL GETLOCAL PUSHINT SETTABLE JMP 2 1 5 0 2 0 -8 ; ; ; ; ; i lim to 10 a i -- Lua 5.0 2 3 4 5 JMP SETTABLE LT JMP * 0 * * 1 2 256 2 1 -3 ; ; ; ; to 4 a[i] = 0 i < lim? to 3
; to 2
20
C LOSURES
Lua has rst-class functions with lexical scoping Variables in each function may have different scopes
local a = local x = 0 for i = 1, N do local y = i a[i] = function (z) y=z; return x+y end end
Small implementation rules out complex algorithms One-pass compiler cannot know in advance whether a variable is used by a inner function
How to assign variables to the stack?
21
When a Closure is created, it searchs for an upvalue for each of its external variables
if upvalue not found, create a new one search is fast, because lists are typically very short
22
stack
stack
x y
new closure
x y
new closure
23
When an upvalue in the stack goes out of scope, the upvalue is closed
upvalue is removed from list the value is copied to an area in the upvalue itself the upvalue points to itself
24
y
top
stack
stack
x y
closure
x y
closure
25
Traverses them
29
30
Step size
how much to do at each step? how to compare ?step size? across different phases?
Collector speed
stops between steps and between collections
31
F INAL R EMARKS
Lexical scoping: no overhead for non users Virtual machine: good performance gains
plus potential gains with CSE optimizations compiler for register-based machine is more complex