SlideShare ist ein Scribd-Unternehmen logo
1 von 61
Downloaden Sie, um offline zu lesen
LuaVM
                1 2       L u a V M




2010   2   24
blog:
                         hatena: Isoparametric
                         twitter: isoparametric
                            name:
                  (http://d.hatena.ne.jp/Isoparametric/)




2010   2   24
Lua?


                Lua




                 LL    ANSI C

2010   2   24
LuaVM
                                      Lua

                Lua C
                          if    for
                               Lua

                    Lua


                                VM

2010   2   24
Lua

                      VM   VM




2010   2   24
I think that one cannot completely grok a
                scripting language, or any complex system for
                that matter, without slitting the animal open
                and examining the entrails, organs and other
                yucky stuff that isn’t normally seen.




2010   2   24
LuaVM
                          VM(5.0   )




                Windows




2010   2   24
LuaVM




2010   2   24
VM              CPU
                        ……CPU
                        ……



                       ……




                PC……
2010   2   24
VM              CPU
                        ……CPU
                        ……



                       ……




                PC……
2010   2   24
/*----------------------------------------------------------------------

   38           (5.1.4     )   name	 	      args	 description
                               ------------------------------------------------------------------------*/
                               OP_MOVE,/*	 A B	 R(A) := R(B)		         	     	      	     */
                               OP_LOADK,/*	 A Bx	 R(A) := Kst(Bx)	     	     	      	     	     */
                               OP_LOADBOOL,/*	     A B C	 R(A) := (Bool)B; if (C) pc++	   	     	      */

       R(X)     X              OP_LOADNIL,/*	      A B	 R(A) := ... := R(B) := nil	       	     	      */
                               OP_GETUPVAL,/*	     A B	 R(A) := UpValue[B]	 	       	     	     */
                               OP_GETGLOBAL,/*	    A Bx	 R(A) := Gbl[Kst(Bx)]	      	     	     	      */
                               OP_GETTABLE,/*	     A B C	 R(A) := R(B)[RK(C)]	
                                                                             	      	     	     */
                               OP_SETGLOBAL,/*	    A Bx	 Gbl[Kst(Bx)] := R(A)	      	     	     	      */
                               OP_SETUPVAL,/*	     A B	 UpValue[B] := R(A)	 	       	     	     */
                               OP_SETTABLE,/*	     A B C	 R(A)[RK(B)] := RK(C)	     	     	     	      */
                               OP_NEWTABLE,/*	     A B C	 R(A) := {} (size = B,C)	 	      	     	      */
                               OP_SELF,/*	 A B C	 R(A+1) := R(B); R(A) := R(B)[RK(C)]	 	        */
                               OP_ADD,/*	   A B C	 R(A) := RK(B) + RK(C)	    	      	     	     */
       K(X)     X              OP_SUB,/*	
                               OP_MUL,/*	
                                            A B C	 R(A) := RK(B) - RK(C)	
                                            A B C	 R(A) := RK(B) * RK(C)	
                                                                             	
                                                                             	
                                                                                    	
                                                                                    	
                                                                                          	
                                                                                          	
                                                                                                */
                                                                                                */
                               OP_DIV,/*	   A B C	 R(A) := RK(B) / RK(C)	    	      	     	     */
                               OP_MOD,/*	   A B C	 R(A) := RK(B) % RK(C)	    	      	     	     */
                               OP_POW,/*	   A B C	 R(A) := RK(B) ^ RK(C)	    	      	     	     */
                               OP_UNM,/*	   A B	 R(A) := -R(B)	        	     	      	     	     */
                               OP_NOT,/*	   A B	 R(A) := not R(B)	 	         	      	     */
                               OP_LEN,/*	   A B	 R(A) := length of R(B)	     	      	     	     */
                               OP_CONCAT,/*	A B C	 R(A) := R(B).. ... ..R(C)		      	     */

       RK(X)        R(X)       OP_JMP,/*	
                               OP_EQ,/*	
                               OP_LT,/*	
                                            sBx	 pc+=sBx	       	      	     	      	
                                            A B C	 if ((RK(B) == RK(C)) ~= A) then pc++	 	
                                            A B C	 if ((RK(B) < RK(C)) ~= A) then pc++ 	
                                                                                          */

                                                                                          	
                                                                                                */
                                                                                                */
                               OP_LE,/*	    A B C	 if ((RK(B) <= RK(C)) ~= A) then pc++ 	 	     */

       K(X-k)                  OP_TEST,/*	 A C	 if not (R(A) <=> C) then pc++	 	
                               OP_TESTSET,/*	
                                                                                          	     */
                                                   A B C	 if (R(B) <=> C) then R(A) := R(B) else pc++	 */
                               OP_CALL,/*	 A B C	 R(A), ... ,R(A+C-2) := R(A)(R(A+1), ... ,R(A+B-1)) */
                               OP_TAILCALL,/*	     A B C	 return R(A)(R(A+1), ... ,R(A+B-1))	   	      */
                               OP_RETURN,/*	A B	 return R(A), ... ,R(A+B-2)	        (see note)	 */
                               OP_FORLOOP,/*	      A sBx	 R(A)+=R(A+2);
                               	     	      	      if R(A) <?= R(A+1) then { pc+=sBx; R(A+3)=R(A) }*/
                               OP_FORPREP,/*	      A sBx	 R(A)-=R(A+2); pc+=sBx	    	     	     	      */
                               OP_TFORLOOP,/*	     A C	 R(A+3), ... ,R(A+2+C) := R(A)(R(A+1), R(A+2));
                                                        if R(A+3) ~= nil then R(A+2)=R(A+3) else pc++	 */
                               OP_SETLIST,/*	      A B C	 R(A)[(C-1)*FPF+i] := R(A+i), 1 <= i <= B	    */
                               OP_CLOSE,/*	 A 	    close all variables in the stack up to (>=) R(A)*/
                               OP_CLOSURE,/*	      A Bx	 R(A) := closure(KPROTO[Bx], R(A), ... ,R(A+n))	    */
                               OP_VARARG/*	 A B	 R(A), R(A+1), ..., R(A+B-1) = vararg	 	        */

2010   2   24
/*----------------------------------------------------------------------

   38           (5.1.4     )   name	 	      args	 description
                               ------------------------------------------------------------------------*/
                               OP_MOVE,/*	 A B	 R(A) := R(B)		         	     	      	     */
                               OP_LOADK,/*	 A Bx	 R(A) := Kst(Bx)	     	     	      	     	     */
                               OP_LOADBOOL,/*	     A B C	 R(A) := (Bool)B; if (C) pc++	   	     	      */

       R(X)     X              OP_LOADNIL,/*	      A B	 R(A) := ... := R(B) := nil	       	     	      */
                               OP_GETUPVAL,/*	     A B	 R(A) := UpValue[B]	 	       	     	     */
                               OP_GETGLOBAL,/*	    A Bx	 R(A) := Gbl[Kst(Bx)]	      	     	     	      */
                               OP_GETTABLE,/*	     A B C	 R(A) := R(B)[RK(C)]	
                                                                             	      	     	     */
                               OP_SETGLOBAL,/*	    A Bx	 Gbl[Kst(Bx)] := R(A)	      	     	     	      */
                               OP_SETUPVAL,/*	     A B	 UpValue[B] := R(A)	 	       	     	     */
                               OP_SETTABLE,/*	     A B C	 R(A)[RK(B)] := RK(C)	     	     	     	      */
                               OP_NEWTABLE,/*	     A B C	 R(A) := {} (size = B,C)	 	      	     	      */
                               OP_SELF,/*	 A B C	 R(A+1) := R(B); R(A) := R(B)[RK(C)]	 	        */
                               OP_ADD,/*	   A B C	 R(A) := RK(B) + RK(C)	    	      	     	     */
       K(X)     X              OP_SUB,/*	
                               OP_MUL,/*	
                                            A B C	 R(A) := RK(B) - RK(C)	
                                            A B C	 R(A) := RK(B) * RK(C)	
                                                                             	
                                                                             	
                                                                                    	
                                                                                    	
                                                                                          	
                                                                                          	
                                                                                                */
                                                                                                */
                               OP_DIV,/*	   A B C	 R(A) := RK(B) / RK(C)	    	      	     	     */
                               OP_MOD,/*	   A B C	 R(A) := RK(B) % RK(C)	    	      	     	     */
                               OP_POW,/*	   A B C	 R(A) := RK(B) ^ RK(C)	    	      	     	     */
                               OP_UNM,/*	   A B	 R(A) := -R(B)	        	     	      	     	     */
                               OP_NOT,/*	   A B	 R(A) := not R(B)	 	         	      	     */
                               OP_LEN,/*	   A B	 R(A) := length of R(B)	     	      	     	     */
                               OP_CONCAT,/*	A B C	 R(A) := R(B).. ... ..R(C)		      	     */

       RK(X)        R(X)       OP_JMP,/*	
                               OP_EQ,/*	
                               OP_LT,/*	
                                            sBx	 pc+=sBx	       	      	     	      	
                                            A B C	 if ((RK(B) == RK(C)) ~= A) then pc++	 	
                                            A B C	 if ((RK(B) < RK(C)) ~= A) then pc++ 	
                                                                                          */

                                                                                          	
                                                                                                */
                                                                                                */
                               OP_LE,/*	    A B C	 if ((RK(B) <= RK(C)) ~= A) then pc++ 	 	     */

       K(X-k)                  OP_TEST,/*	 A C	 if not (R(A) <=> C) then pc++	 	
                               OP_TESTSET,/*	
                                                                                          	     */
                                                   A B C	 if (R(B) <=> C) then R(A) := R(B) else pc++	 */
                               OP_CALL,/*	 A B C	 R(A), ... ,R(A+C-2) := R(A)(R(A+1), ... ,R(A+B-1)) */
                               OP_TAILCALL,/*	     A B C	 return R(A)(R(A+1), ... ,R(A+B-1))	   	      */
                               OP_RETURN,/*	A B	 return R(A), ... ,R(A+B-2)	        (see note)	 */
                               OP_FORLOOP,/*	      A sBx	 R(A)+=R(A+2);
                               	     	      	      if R(A) <?= R(A+1) then { pc+=sBx; R(A+3)=R(A) }*/
                               OP_FORPREP,/*	      A sBx	 R(A)-=R(A+2); pc+=sBx	    	     	     	      */
                               OP_TFORLOOP,/*	     A C	 R(A+3), ... ,R(A+2+C) := R(A)(R(A+1), R(A+2));
                                                        if R(A+3) ~= nil then R(A+2)=R(A+3) else pc++	 */
                               OP_SETLIST,/*	      A B C	 R(A)[(C-1)*FPF+i] := R(A+i), 1 <= i <= B	    */
                               OP_CLOSE,/*	 A 	    close all variables in the stack up to (>=) R(A)*/
                               OP_CLOSURE,/*	      A Bx	 R(A) := closure(KPROTO[Bx], R(A), ... ,R(A+n))	    */
                               OP_VARARG/*	 A B	 R(A), R(A+1), ..., R(A+B-1) = vararg	 	        */

2010   2   24
MOVE

                 LOADK

                LOADBOOL

                 LOADNIL    nil

                GETUPVAL

                GETGLOBAL

                GETTABLE

                SETGLOBAL

                SETUPVAL

                SETTABLE
2010   2   24
NEWTABLE

                  SELF

                  ADD

                  SUB

                  MUL

                  DIV

                  MOD

                  POW

                  UNM

                  NOT
2010   2   24
LEN

                CONCAT

                  JMP

                  EQ

                   LT

                  LE

                 TEST

                TESTSET

                 CALL

                TAILCALL
2010   2   24
RETURN

                FORLOOP    for         FORPREP

                FORPREP          for

                TFORLOOP                  for

                 SETLIST

                 CLOSE

                CLOSURE

                 VARARG




2010   2   24
0   1   2   3   4   5   6   7   8    9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31

                                 OP                            A                         B                         C
                                 OP                            A                                    Bx
                                 OP                            A                                   sBx

                                                             Figure 6: Instruction layout
                                                         OP                                  6bit
                function max (a,b)
                  local m = a                                      1   MOVE          2   0 0        ;   R(2) = R(0)
                  if b > a then                                    2   LT            0   0 1        ;   R(0) < R(1) ?
                    m = b
                  end
                                                                   3
                                                                   4
                                                                        A
                                                                       JMP
                                                                       MOVE
                                                                                     1
                                                                                     2   1 0
                                                                                                    ;
                                                                                                    ;
                                                                                                               8bit
                                                                                                        to 5 (4+1)
                                                                                                        R(2) = R(1)
                  return m                                         5   RETURN        2   2 0        ;   return R(2)
                end                                                6    B C 9bit
                                                                       RETURN        0   1 0        ;   return

                                         18bit 7: Bytecode for a Lua function
                                           Figure

                                     Bx(unsigned) or sBx(signed)
                a register or a constant (using the representation RK(X) explained above). With
                this format, several typical operations in Lua can be coded in a single instruction.
2010   2   24
OP             A                          Bx
                             OP             A                          sBx

                                          Figure 6: Instruction layout

                function max (a,b)
                  local m = a                   1   MOVE     2   0 0   ;   R(2) = R(0)
                  if b > a then                 2   LT       0   0 1   ;   R(0) < R(1) ?
                    m = b                       3   JMP      1         ;   to 5 (4+1)
                  end                           4   MOVE     2   1 0   ;   R(2) = R(1)
                  return m                      5   RETURN   2   2 0   ;   return R(2)
                end                             6   RETURN   0   1 0   ;   return


                                     Figure 7: Bytecode for a Lua function


                a register or a constant (using the representation RK(X) explained above). With
                this format, several typical operations in Lua can be coded in a single instruction.
                For instance, the increment of a local variable, such as a = a + 1, is coded
                as ADD x x y, where x represents the register holding the local variable and y
                represents the constant 1. An assignment like a = b.f, when both a and b are
                local variables, is also coded as the single instruction GETTABLE x y z, where x
                is the register for a, y is the register for b, and z is the index of the string
                constant "f". (In Lua, the syntax b.f is syntactic sugar for b["f"], that is, b
2010   2   24   indexed by the string "f".)
OP             A                          Bx
                             OP             A                          sBx

                                          Figure 6: Instruction layout

                function max (a,b)
                  local m = a                   1   MOVE     2   0 0   ;   R(2) = R(0)
                  if b > a then                 2   LT       0   0 1   ;   R(0) < R(1) ?
                    m = b                       3   JMP      1         ;   to 5 (4+1)
                  end                           4   MOVE     2   1 0   ;   R(2) = R(1)
                  return m                      5   RETURN   2   2 0   ;   return R(2)
                end                             6   RETURN   0   1 0   ;   return


                                     Figure 7: Bytecode for a Lua function



                                                    R(0) == a, R(1) == b
                a register or a constant (using the representation RK(X) explained above). With
                this format, several typical operations in Lua can be coded in a single instruction.
       LT       For instance, the increment of a local variable, such as a = a + 1, is coded
                as ADD x x y, where x represents the register holding the local variable and y
                           PC++                                   JMP
                represents the constant 1. An assignment like a = b.f, when both a and b are
                local variables, is also coded as the single instruction GETTABLE x y z, where x

       OP_LT,/* A B C if ((RK(B) < RK(C)) ~= A) then pc++
                is the register for a, y is the register for b, and z is the index of the string
                constant "f". (In Lua, the syntax b.f is syntactic sugar for b["f"], that is, b
                                                                                                   */
2010   2   24   indexed by the string "f".)
256
                        B/C 9bit==1bit

                Lua


                18bit               (131071   )
2010   2   24
VM         Lua


                     VM
                          lua -l
2010   2   24
Lua
                                                   1.0   1.1   2.1   2.2   2.4   2.5   3.0    3.1   3.2   4.0   5.0   5.1
                constructors                        •     •     •     •     •     •     •     •     •     •     •     •
                garbage collection                  •     •     •     •     •     •     •     •     •     •     •     •
                extensible semantics                ◦     ◦     •     •     •     •     •     •     •     •     •     •
                support for OOP                     ◦     ◦     •     •     •     •     •     •     •     •     •     •
                long strings                        ◦     ◦     ◦     •     •     •     •     •     •     •     •     •
                debug API                           ◦     ◦     ◦     •     •     •     •     •     •     •     •     •
                external compiler                   ◦     ◦     ◦     ◦     •     •     •     •     •     •     •     •
                vararg functions                    ◦     ◦     ◦     ◦     ◦     •     •     •     •     •     •     •
                pattern matching                    ◦     ◦     ◦     ◦     ◦     •     •     •     •     •     •     •
                conditional compilation             ◦     ◦     ◦     ◦     ◦     ◦     •     •     •     ◦     ◦     ◦
                anonymous functions, closures       ◦     ◦     ◦     ◦     ◦     ◦     ◦     •     •     •     •     •
                debug library                       ◦     ◦     ◦     ◦     ◦     ◦     ◦     ◦     •     •     •     •
                multi-state API                     ◦     ◦     ◦     ◦     ◦     ◦     ◦     ◦     ◦     •     •     •
                for statement                       ◦     ◦     ◦     ◦     ◦     ◦     ◦     ◦     ◦     •     •     •
                long comments                       ◦     ◦     ◦     ◦     ◦     ◦     ◦     ◦     ◦     ◦     •     •
                full lexical scoping                ◦     ◦     ◦     ◦     ◦     ◦     ◦     ◦     ◦     ◦     •     •
                booleans                            ◦     ◦     ◦     ◦     ◦     ◦     ◦     ◦     ◦     ◦     •     •
                coroutines                          ◦     ◦     ◦     ◦     ◦     ◦     ◦     ◦     ◦     ◦     •     •
                incremental garbage collection      ◦     ◦     ◦     ◦     ◦     ◦     ◦     ◦     ◦     ◦     ◦     •
                module system                       ◦     ◦     ◦     ◦     ◦     ◦     ◦     ◦     ◦     ◦     ◦     •
                                                   1.0   1.1   2.1   2.2   2.4   2.5   3.0    3.1   3.2   4.0   5.0   5.1
                libraries                           4     4     4     4     4     4     4       4    5     6     8     9
                built-in functions                  5     7    11    11    13    14    25      27   35     0     0     0
                API functions                      30    30    30    30    32    32    33      47   41    60    76    79
                vm type (stack × register)          S     S     S     S     S     S     S      S     S     S     R     R
                vm instructions                    64    65    69    67    67    68    69     128   64    49    35    38
                keywords                           16    16    16    16    16    16    16      16   16    18    21    21
                other tokens                       21    21    23    23    23    23    24      25   25    25    24    26
                                                 Table 1. The evolution of features in Lua.
2010   2   24
Lua
                                                   1.0   1.1   2.1   2.2   2.4   2.5   3.0    3.1   3.2   4.0   5.0   5.1
                constructors                        •     •     •     •     •     •     •     •     •     •     •     •
                garbage collection                  •     •     •     •     •     •     •     •     •     •     •     •
                extensible semantics                ◦     ◦     •     •     •     •     •     •     •     •     •     •
                support for OOP                     ◦     ◦     •     •     •     •     •     •     •     •     •     •
                long strings                        ◦     ◦     ◦     •     •     •     •     •     •     •     •     •
                debug API                           ◦     ◦     ◦     •     •     •     •     •     •     •     •     •
                external compiler                   ◦     ◦     ◦     ◦     •     •     •     •     •     •     •     •
                vararg functions                    ◦     ◦     ◦     ◦     ◦     •     •     •     •     •     •     •
                pattern matching                    ◦     ◦     ◦     ◦     ◦     •     •     •     •     •     •     •
                conditional compilation             ◦     ◦     ◦     ◦     ◦     ◦     •     •     •     ◦     ◦     ◦
                anonymous functions, closures       ◦     ◦     ◦     ◦     ◦     ◦     ◦     •     •     •     •     •
                debug library                       ◦     ◦     ◦     ◦     ◦     ◦     ◦     ◦     •     •     •     •
                multi-state API                     ◦     ◦     ◦     ◦     ◦     ◦     ◦     ◦     ◦     •     •     •
                for statement                       ◦     ◦     ◦     ◦     ◦     ◦     ◦     ◦     ◦     •     •     •
                long comments                       ◦     ◦     ◦     ◦     ◦     ◦     ◦     ◦     ◦     ◦     •     •
                full lexical scoping                ◦     ◦     ◦     ◦     ◦     ◦     ◦     ◦     ◦     ◦     •     •
                booleans                            ◦     ◦     ◦     ◦     ◦     ◦     ◦     ◦     ◦     ◦     •     •
                coroutines                          ◦     ◦     ◦     ◦     ◦     ◦     ◦     ◦     ◦     ◦     •     •
                incremental garbage collection      ◦     ◦     ◦     ◦     ◦     ◦     ◦     ◦     ◦     ◦     ◦     •
                module system                       ◦     ◦     ◦     ◦     ◦     ◦     ◦     ◦     ◦     ◦     ◦     •
                                                   1.0   1.1   2.1   2.2   2.4   2.5   3.0    3.1   3.2   4.0   5.0   5.1
                libraries                           4     4     4     4     4     4     4       4    5     6     8     9
                built-in functions                  5     7    11    11    13    14    25      27   35     0     0     0
                API functions                      30    30    30    30    32    32    33      47   41    60    76    79
                vm type (stack × register)          S     S     S     S     S     S     S      S     S     S     R     R
                vm instructions                    64    65    69    67    67    68    69     128   64    49    35    38
                keywords                           16    16    16    16    16    16    16      16   16    18    21    21
                other tokens                       21    21    23    23    23    23    24      25   25    25    24    26
                                                 Table 1. The evolution of features in Lua.
2010   2   24
Lua
                                                   1.0   1.1   2.1   2.2   2.4   2.5   3.0    3.1   3.2   4.0   5.0   5.1
                constructors                        •     •     •     •     •     •     •     •     •     •     •     •
                garbage collection                  •     •     •     •     •     •     •     •     •     •     •     •
                extensible semantics                ◦     ◦     •     •     •     •     •     •     •     •     •     •
                support for OOP                     ◦     ◦     •     •     •     •     •     •     •     •     •     •
                long strings                        ◦     ◦     ◦     •     •     •     •     •     •     •     •     •
                debug API                           ◦     ◦     ◦     •     •     •     •     •     •     •     •     •
                external compiler                   ◦     ◦     ◦     ◦     •     •     •     •     •     •     •     •
                vararg functions                    ◦     ◦     ◦     ◦     ◦     •     •     •     •     •     •     •
                pattern matching                    ◦     ◦     ◦     ◦     ◦     •     •     •     •     •     •     •
                conditional compilation             ◦     ◦     ◦     ◦     ◦     ◦     •     •     •     ◦     ◦     ◦
                anonymous functions, closures       ◦     ◦     ◦     ◦     ◦     ◦     ◦     •     •     •     •     •
                debug library                       ◦     ◦     ◦     ◦     ◦     ◦     ◦     ◦     •     •     •     •
                multi-state API                     ◦     ◦     ◦     ◦     ◦     ◦     ◦     ◦     ◦     •     •     •
                for statement                       ◦     ◦     ◦     ◦     ◦     ◦     ◦     ◦     ◦     •     •     •
                long comments                       ◦     ◦     ◦     ◦     ◦     ◦     ◦     ◦     ◦     ◦     •     •
                full lexical scoping                ◦     ◦     ◦     ◦     ◦     ◦     ◦     ◦     ◦     ◦     •     •
                booleans                            ◦     ◦     ◦     ◦     ◦     ◦     ◦     ◦     ◦     ◦     •     •
                coroutines                          ◦     ◦     ◦     ◦     ◦     ◦     ◦     ◦     ◦     ◦     •     •
                incremental garbage collection      ◦     ◦     ◦     ◦     ◦     ◦     ◦     ◦     ◦     ◦     ◦     •
                module system                       ◦     ◦     ◦     ◦     ◦     ◦     ◦     ◦     ◦     ◦     ◦     •
                                                   1.0   1.1   2.1   2.2   2.4   2.5   3.0    3.1   3.2   4.0   5.0   5.1
                libraries                           4     4     4     4     4     4     4       4    5     6     8     9
                built-in functions                  5     7    11    11    13    14    25      27   35     0     0     0
                API functions                      30    30    30    30    32    32    33      47   41    60    76    79
                vm type (stack × register)          S     S     S     S     S     S     S      S     S     S     R     R
                vm instructions                    64    65    69    67    67    68    69     128   64    49    35    38
                keywords                           16    16    16    16    16    16    16      16   16    18    21    21
                other tokens                       21    21    23    23    23    23    24      25   25    25    24    26
                                                 Table 1. The evolution of features in Lua.
2010   2   24
VM
                V S   V M




2010   2   24
VM
                        VM
                 push




                VM            VM
2010   2   24
VM
                Lua
                VM Lua


                            local



                 push/pop


2010   2   24
LuaVM
                Lua




2010   2   24
==   ==




2010   2   24
==   ==
                Lua        VM        ……




                                VM
2010   2   24
2010   2   24
LuaVM
                         4byte           Lua
                1byte   2byte




2010   2   24
ADD
                ADD
                          ADD




                ADD
                      +
2010   2   24
JavaVM   goto   2byte)




                2byte
2010   2   24
Lua   4byte   Lua   1 or 2byte




2010   2   24
local a,t,i
                a=a+i
                a=a+100       YO

                a=t[i]




2010   2   24
local a,t,i
                a=a+i
                a=a+100                      YO

                a=t[i]



             1	
              [1]	
                 PUSHNIL      	   3
             2	
              [2]	
                 GETLOCAL     	   0	 ; a
             3	
              [2]	
                 GETLOCAL     	   2	 ; i
             4	
              [2]	
                 ADD          	
             5	
              [2]	
                 SETLOCAL     	   0	 ;   a
             6	
              [3]	
                 GETLOCAL     	   0	 ;   a
             7	
              [3]	
                 ADDI         	   100
             8	
              [3]	
                 SETLOCAL     	   0	 ;   a
             9	
              [4]	
                 GETLOCAL     	   1	 ;   t
            10	
              [4]	
                 GETINDEXED   	   2	 ;   i
            11	
              [4]	
                 SETLOCAL     	   0	 ;   a
            12	
              [4]	
                 END          	
2010   2   24
local a,t,i
                a=a+i
                a=a+100                                                   YO

                a=t[i]



             1	
              [1]	
                 PUSHNIL      	   3          1   [2]ADD        0   0 2
             2	
              [2]	
                 GETLOCAL     	   0	 ; a     2   [3]ADD        0   0 -1   ; - 100
             3	
              [2]	
                 GETLOCAL     	   2	 ; i     3   [4]GETTABLE   0   1 2
             4	
              [2]	
                 ADD          	              4   [4]RETURN     0   1
             5	
              [2]	
                 SETLOCAL     	   0	 ;   a
             6	
              [3]	
                 GETLOCAL     	   0	 ;   a
             7	
              [3]	
                 ADDI         	   100
             8	
              [3]	
                 SETLOCAL     	   0	 ;   a
             9	
              [4]	
                 GETLOCAL     	   1	 ;   t
            10	
              [4]	
                 GETINDEXED   	   2	 ;   i
            11	
              [4]	
                 SETLOCAL     	   0	 ;   a
            12	
              [4]	
                 END          	
2010   2   24
Lua 4.0.1

main <0:@test.lua> (12 instructions/96 bytes at 0x100100650)
0 params, 5 stacks, 3 locals, 0 strings, 0 numbers, 0 functions, 5
lines
      1	
       [1]	
          PUSHNIL    	 3
      2	
       [2]	
          GETLOCAL   	 0	 ; a
      3	
       [2]	
          GETLOCAL   	 2	 ; i
      4	
       [2]	
          ADD        	
      5	
       [2]	
          SETLOCAL   	 0	 ; a
      6	
       [3]	
          GETLOCAL   	 0	 ; a
      7	
       [3]	
          ADDI       	 100
      8	
       [3]	
          SETLOCAL   	 0	 ; a
      9	
       [4]	
          GETLOCAL   	 1	 ; t
    10	[4]	
          GETINDEXED 	 2	 ; i
    11	[4]	
          SETLOCAL   	 0	 ; a
    12	[4]	
          END        	




2010   2   24
Lua 4.0.1

main <0:@test.lua> (12 instructions/96 bytes at 0x100100650)
0 params, 5 stacks, 3 locals, 0 strings, 0 numbers, 0 functions, 5
lines
      1	
       [1]	
          PUSHNIL    	 3        local a,t,i
      2	
       [2]	
          GETLOCAL   	 0	 ; a
      3	
       [2]	
          GETLOCAL   	 2	 ; i
      4	
       [2]	
          ADD        	
      5	
       [2]	
          SETLOCAL   	 0	 ; a
      6	
       [3]	
          GETLOCAL   	 0	 ; a
      7	
       [3]	
          ADDI       	 100
      8	
       [3]	
          SETLOCAL   	 0	 ; a
      9	
       [4]	
          GETLOCAL   	 1	 ; t
    10	[4]	
          GETINDEXED 	 2	 ; i
    11	[4]	
          SETLOCAL   	 0	 ; a
    12	[4]	
          END        	




2010   2   24
Lua 4.0.1

main <0:@test.lua> (12 instructions/96 bytes at 0x100100650)
0 params, 5 stacks, 3 locals, 0 strings, 0 numbers, 0 functions, 5
lines
      1	
       [1]	
          PUSHNIL    	 3        local a,t,i
      2	
       [2]	
          GETLOCAL   	 0	 ; a
      3	
       [2]	
          GETLOCAL   	 2	 ; i   a=a+i
      4	
       [2]	
          ADD        	
      5	
       [2]	
          SETLOCAL   	 0	 ; a
      6	
       [3]	
          GETLOCAL   	 0	 ; a
      7	
       [3]	
          ADDI       	 100
      8	
       [3]	
          SETLOCAL   	 0	 ; a
      9	
       [4]	
          GETLOCAL   	 1	 ; t
    10	[4]	
          GETINDEXED 	 2	 ; i
    11	[4]	
          SETLOCAL   	 0	 ; a
    12	[4]	
          END        	




2010   2   24
Lua 4.0.1

main <0:@test.lua> (12 instructions/96 bytes at 0x100100650)
0 params, 5 stacks, 3 locals, 0 strings, 0 numbers, 0 functions, 5
lines
      1	
       [1]	
          PUSHNIL    	 3        local a,t,i
      2	
       [2]	
          GETLOCAL   	 0	 ; a
      3	
       [2]	
          GETLOCAL   	 2	 ; i   a=a+i
      4	
       [2]	
          ADD        	
      5	
       [2]	
          SETLOCAL   	 0	 ; a
      6	
       [3]	
          GETLOCAL   	 0	 ; a
      7	
       [3]	
          ADDI       	 100
                                a=a+100
      8	
       [3]	
          SETLOCAL   	 0	 ; a
      9	
       [4]	
          GETLOCAL   	 1	 ; t
    10	[4]	
          GETINDEXED 	 2	 ; i
    11	[4]	
          SETLOCAL   	 0	 ; a
    12	[4]	
          END        	




2010   2   24
Lua 4.0.1

main <0:@test.lua> (12 instructions/96 bytes at 0x100100650)
0 params, 5 stacks, 3 locals, 0 strings, 0 numbers, 0 functions, 5
lines
      1	
       [1]	
          PUSHNIL    	 3        local a,t,i
      2	
       [2]	
          GETLOCAL   	 0	 ; a
      3	
       [2]	
          GETLOCAL   	 2	 ; i   a=a+i
      4	
       [2]	
          ADD        	
      5	
       [2]	
          SETLOCAL   	 0	 ; a
      6	
       [3]	
          GETLOCAL   	 0	 ; a
      7	
       [3]	
          ADDI       	 100
                                a=a+100
      8	
       [3]	
          SETLOCAL   	 0	 ; a
      9	
       [4]	
          GETLOCAL   	 1	 ; t
    10	[4]	
          GETINDEXED 	 2	 ; i   a=t[i]
    11	[4]	
          SETLOCAL   	 0	 ; a
    12	[4]	
          END        	




2010   2   24
Lua 4.0.1

main <0:@test.lua> (12 instructions/96 bytes at 0x100100650)
0 params, 5 stacks, 3 locals, 0 strings, 0 numbers, 0 functions, 5
lines
      1	
       [1]	
          PUSHNIL    	 3        local a,t,i
      2	
       [2]	
          GETLOCAL   	 0	 ; a
      3	
       [2]	
          GETLOCAL   	 2	 ; i   a=a+i
      4	
       [2]	
          ADD        	
      5	
       [2]	
          SETLOCAL   	 0	 ; a
      6	
       [3]	
          GETLOCAL   	 0	 ; a
      7	
       [3]	
          ADDI       	 100
                                a=a+100
      8	
       [3]	
          SETLOCAL   	 0	 ; a
      9	
       [4]	
          GETLOCAL   	 1	 ; t
    10	[4]	
          GETINDEXED 	 2	 ; i   a=t[i]
    11	[4]	
          SETLOCAL   	 0	 ; a
    12	[4]	
          END        	
                                                      YO!
                ADD        ADDI
                                             ……
2010   2   24
Lua 5.1.4

  main <test.lua:0,0> (4 instructions, 16 bytes at 0x100101050)
  0+ params, 3 slots, 0 upvalues, 3 locals, 1 constant, 0 functions
                1     [2]       ADD        0 0 2
                2     [3]       ADD        0 0 -1   ; - 100
                3     [4]       GETTABLE   0 1 2
                4     [4]       RETURN     0 1




2010   2   24
Lua 5.1.4

  main <test.lua:0,0> (4 instructions, 16 bytes at 0x100101050)
  0+ params, 3 slots, 0 upvalues, 3 locals, 1 constant, 0 functions
                1     [2]       ADD        0 0 2              a=a+i
                2     [3]       ADD        0 0 -1   ; - 100
                3     [4]       GETTABLE   0 1 2
                4     [4]       RETURN     0 1




2010   2   24
Lua 5.1.4

  main <test.lua:0,0> (4 instructions, 16 bytes at 0x100101050)
  0+ params, 3 slots, 0 upvalues, 3 locals, 1 constant, 0 functions
                1     [2]       ADD        0 0 2              a=a+i
                2     [3]       ADD        0 0 -1   ; - 100   a=a+100
                3     [4]       GETTABLE   0 1 2
                4     [4]       RETURN     0 1




2010   2   24
Lua 5.1.4

  main <test.lua:0,0> (4 instructions, 16 bytes at 0x100101050)
  0+ params, 3 slots, 0 upvalues, 3 locals, 1 constant, 0 functions
                1     [2]       ADD        0 0 2              a=a+i
                2     [3]       ADD        0 0 -1   ; - 100   a=a+100
                3     [4]       GETTABLE   0 1 2              a=t[i]
                4     [4]       RETURN     0 1




2010   2   24
Lua 5.1.4

  main <test.lua:0,0> (4 instructions, 16 bytes at 0x100101050)
  0+ params, 3 slots, 0 upvalues, 3 locals, 1 constant, 0 functions
                1         [2]     ADD        0 0 2              a=a+i
                2         [3]     ADD        0 0 -1   ; - 100   a=a+100
                3         [4]     GETTABLE   0 1 2              a=t[i]
                4         [4]     RETURN     0 1




                    local a,t,i                       ……



2010   2   24
2010   2   24
Lua   :-p




                            ……
2010   2   24
Lua


                Lua     12byte   tag+
                union




2010   2   24
ual machine with the new optimization for arrays can reduce the runnin
 up to 40%.
The complete code of Lua 5.0 is available for browsing at Lua’s web site
p://www.lua.org/source/5.0/.


                 program          Lua 4.0     Lua 5’       Lua 5.0
                 sum (2e7)         1.23     0.54   (44%) 0.54   (44%)
                 fibo (30)          0.95     0.68   (72%) 0.69   (73%)
                 ack (8)           1.00     0.86   (86%) 0.88   (88%)
                 random (1e6)      1.04     0.96   (92%) 0.96   (92%)
                 sieve (100)       0.93     0.82   (88%) 0.57   (61%)
                 heapsort (5e4)    1.08     1.05   (97%) 0.70   (65%)
                 matrix (50)       0.84     0.82   (98%) 0.59   (70%)

 re 10: Benchmarks (times in seconds; percentages are relative to Lua 4.0)
                      Lua


 2010   2   24
L u a V M




2010   2   24
Lua
                Lua



                              Lua
                      local




2010   2   24
Lua

2010   2   24
LuaVM
                Lua-Alchemy(Lua on Flash)

                kahlua(Lua on Java[J2ME])

                Yueliang(Lua on Lua)

                LuaCLR(Lua on .NET)

                Lua2js(Lua on JavaScript)

                        http://lua-users.org/wiki/
                LuaImplementations
2010   2   24
The Implementation of Lua 5.0

                The Evolution of Lua

                A No-Frills Introduction to Lua 5.1 VM
                Instructions

                Lua           lvm.c    lopcodes.h



2010   2   24
Lua




                Lua




2010   2   24
Lua
                              C



                Lua     JIT
                                  C



                VM
2010   2   24
2010   2   24

Weitere ähnliche Inhalte

Was ist angesagt?

BoostAsioで可読性を求めるのは間違っているだろうか
BoostAsioで可読性を求めるのは間違っているだろうかBoostAsioで可読性を求めるのは間違っているだろうか
BoostAsioで可読性を求めるのは間違っているだろうかYuki Miyatake
 
ネットワーク ゲームにおけるTCPとUDPの使い分け
ネットワーク ゲームにおけるTCPとUDPの使い分けネットワーク ゲームにおけるTCPとUDPの使い分け
ネットワーク ゲームにおけるTCPとUDPの使い分けモノビット エンジン
 
エンジニアの個人ブランディングと技術組織
エンジニアの個人ブランディングと技術組織エンジニアの個人ブランディングと技術組織
エンジニアの個人ブランディングと技術組織Takafumi ONAKA
 
Linuxのsemaphoreとmutexを見る 
Linuxのsemaphoreとmutexを見る Linuxのsemaphoreとmutexを見る 
Linuxのsemaphoreとmutexを見る wata2ki
 
MagicOnion入門
MagicOnion入門MagicOnion入門
MagicOnion入門torisoup
 
すごい constexpr たのしくレイトレ!
すごい constexpr たのしくレイトレ!すごい constexpr たのしくレイトレ!
すごい constexpr たのしくレイトレ!Genya Murakami
 
Redisの特徴と活用方法について
Redisの特徴と活用方法についてRedisの特徴と活用方法について
Redisの特徴と活用方法についてYuji Otani
 
【CEDEC2018】一歩先のUnityでのパフォーマンス/メモリ計測、デバッグ術
【CEDEC2018】一歩先のUnityでのパフォーマンス/メモリ計測、デバッグ術【CEDEC2018】一歩先のUnityでのパフォーマンス/メモリ計測、デバッグ術
【CEDEC2018】一歩先のUnityでのパフォーマンス/メモリ計測、デバッグ術Unity Technologies Japan K.K.
 
SQLアンチパターン 幻の第26章「とりあえず削除フラグ」
SQLアンチパターン 幻の第26章「とりあえず削除フラグ」SQLアンチパターン 幻の第26章「とりあえず削除フラグ」
SQLアンチパターン 幻の第26章「とりあえず削除フラグ」Takuto Wada
 
いまさら聞けないarmを使ったNEONの基礎と活用事例
いまさら聞けないarmを使ったNEONの基礎と活用事例いまさら聞けないarmを使ったNEONの基礎と活用事例
いまさら聞けないarmを使ったNEONの基礎と活用事例Fixstars Corporation
 
Pythonによる黒魔術入門
Pythonによる黒魔術入門Pythonによる黒魔術入門
Pythonによる黒魔術入門大樹 小倉
 
世界一わかりやすいClean Architecture
世界一わかりやすいClean Architecture世界一わかりやすいClean Architecture
世界一わかりやすいClean ArchitectureAtsushi Nakamura
 
async/await のしくみ
async/await のしくみasync/await のしくみ
async/await のしくみ信之 岩永
 
FINAL FANTASY Record Keeperのマスターデータを支える技術
FINAL FANTASY Record Keeperのマスターデータを支える技術FINAL FANTASY Record Keeperのマスターデータを支える技術
FINAL FANTASY Record Keeperのマスターデータを支える技術dena_study
 
PlaySQLAlchemy: SQLAlchemy入門
PlaySQLAlchemy: SQLAlchemy入門PlaySQLAlchemy: SQLAlchemy入門
PlaySQLAlchemy: SQLAlchemy入門泰 増田
 
tcpdumpとtcpreplayとtcprewriteと他。
tcpdumpとtcpreplayとtcprewriteと他。tcpdumpとtcpreplayとtcprewriteと他。
tcpdumpとtcpreplayとtcprewriteと他。(^-^) togakushi
 
C#とILとネイティブと
C#とILとネイティブとC#とILとネイティブと
C#とILとネイティブと信之 岩永
 
serviceクラスをやめようサブクラスを使おう
serviceクラスをやめようサブクラスを使おうserviceクラスをやめようサブクラスを使おう
serviceクラスをやめようサブクラスを使おうよしだ あつし
 
新たなgitのブランチモデル「Git Feature Flow」!Git Flow,Git Hub Flow,Git Lab Flowを超えれるか?
新たなgitのブランチモデル「Git Feature Flow」!Git Flow,Git Hub Flow,Git Lab Flowを超えれるか?新たなgitのブランチモデル「Git Feature Flow」!Git Flow,Git Hub Flow,Git Lab Flowを超えれるか?
新たなgitのブランチモデル「Git Feature Flow」!Git Flow,Git Hub Flow,Git Lab Flowを超えれるか?naoki koyama
 

Was ist angesagt? (20)

BoostAsioで可読性を求めるのは間違っているだろうか
BoostAsioで可読性を求めるのは間違っているだろうかBoostAsioで可読性を求めるのは間違っているだろうか
BoostAsioで可読性を求めるのは間違っているだろうか
 
ネットワーク ゲームにおけるTCPとUDPの使い分け
ネットワーク ゲームにおけるTCPとUDPの使い分けネットワーク ゲームにおけるTCPとUDPの使い分け
ネットワーク ゲームにおけるTCPとUDPの使い分け
 
エンジニアの個人ブランディングと技術組織
エンジニアの個人ブランディングと技術組織エンジニアの個人ブランディングと技術組織
エンジニアの個人ブランディングと技術組織
 
Linuxのsemaphoreとmutexを見る 
Linuxのsemaphoreとmutexを見る Linuxのsemaphoreとmutexを見る 
Linuxのsemaphoreとmutexを見る 
 
プログラムを高速化する話
プログラムを高速化する話プログラムを高速化する話
プログラムを高速化する話
 
MagicOnion入門
MagicOnion入門MagicOnion入門
MagicOnion入門
 
すごい constexpr たのしくレイトレ!
すごい constexpr たのしくレイトレ!すごい constexpr たのしくレイトレ!
すごい constexpr たのしくレイトレ!
 
Redisの特徴と活用方法について
Redisの特徴と活用方法についてRedisの特徴と活用方法について
Redisの特徴と活用方法について
 
【CEDEC2018】一歩先のUnityでのパフォーマンス/メモリ計測、デバッグ術
【CEDEC2018】一歩先のUnityでのパフォーマンス/メモリ計測、デバッグ術【CEDEC2018】一歩先のUnityでのパフォーマンス/メモリ計測、デバッグ術
【CEDEC2018】一歩先のUnityでのパフォーマンス/メモリ計測、デバッグ術
 
SQLアンチパターン 幻の第26章「とりあえず削除フラグ」
SQLアンチパターン 幻の第26章「とりあえず削除フラグ」SQLアンチパターン 幻の第26章「とりあえず削除フラグ」
SQLアンチパターン 幻の第26章「とりあえず削除フラグ」
 
いまさら聞けないarmを使ったNEONの基礎と活用事例
いまさら聞けないarmを使ったNEONの基礎と活用事例いまさら聞けないarmを使ったNEONの基礎と活用事例
いまさら聞けないarmを使ったNEONの基礎と活用事例
 
Pythonによる黒魔術入門
Pythonによる黒魔術入門Pythonによる黒魔術入門
Pythonによる黒魔術入門
 
世界一わかりやすいClean Architecture
世界一わかりやすいClean Architecture世界一わかりやすいClean Architecture
世界一わかりやすいClean Architecture
 
async/await のしくみ
async/await のしくみasync/await のしくみ
async/await のしくみ
 
FINAL FANTASY Record Keeperのマスターデータを支える技術
FINAL FANTASY Record Keeperのマスターデータを支える技術FINAL FANTASY Record Keeperのマスターデータを支える技術
FINAL FANTASY Record Keeperのマスターデータを支える技術
 
PlaySQLAlchemy: SQLAlchemy入門
PlaySQLAlchemy: SQLAlchemy入門PlaySQLAlchemy: SQLAlchemy入門
PlaySQLAlchemy: SQLAlchemy入門
 
tcpdumpとtcpreplayとtcprewriteと他。
tcpdumpとtcpreplayとtcprewriteと他。tcpdumpとtcpreplayとtcprewriteと他。
tcpdumpとtcpreplayとtcprewriteと他。
 
C#とILとネイティブと
C#とILとネイティブとC#とILとネイティブと
C#とILとネイティブと
 
serviceクラスをやめようサブクラスを使おう
serviceクラスをやめようサブクラスを使おうserviceクラスをやめようサブクラスを使おう
serviceクラスをやめようサブクラスを使おう
 
新たなgitのブランチモデル「Git Feature Flow」!Git Flow,Git Hub Flow,Git Lab Flowを超えれるか?
新たなgitのブランチモデル「Git Feature Flow」!Git Flow,Git Hub Flow,Git Lab Flowを超えれるか?新たなgitのブランチモデル「Git Feature Flow」!Git Flow,Git Hub Flow,Git Lab Flowを超えれるか?
新たなgitのブランチモデル「Git Feature Flow」!Git Flow,Git Hub Flow,Git Lab Flowを超えれるか?
 

Ähnlich wie 12 分くらいで知るLuaVM

Data structuresUsing java language and develop a prot.pdf
Data structuresUsing java language and develop a prot.pdfData structuresUsing java language and develop a prot.pdf
Data structuresUsing java language and develop a prot.pdfarmyshoes
 
(1)Objective Binary Search Tree traversal (2 points)Use traversal.pdf
(1)Objective Binary Search Tree traversal (2 points)Use traversal.pdf(1)Objective Binary Search Tree traversal (2 points)Use traversal.pdf
(1)Objective Binary Search Tree traversal (2 points)Use traversal.pdfarihantmobileselepun
 
C++ Advanced
C++ AdvancedC++ Advanced
C++ AdvancedVivek Das
 
Michal Malohlava presents: Open Source H2O and Scala
Michal Malohlava presents: Open Source H2O and Scala Michal Malohlava presents: Open Source H2O and Scala
Michal Malohlava presents: Open Source H2O and Scala Sri Ambati
 
Codigo fuente
Codigo fuenteCodigo fuente
Codigo fuenteBlackD10
 
Please fix my errors class Iterator public Construc.pdf
Please fix my errors   class Iterator  public  Construc.pdfPlease fix my errors   class Iterator  public  Construc.pdf
Please fix my errors class Iterator public Construc.pdfkitty811
 

Ähnlich wie 12 分くらいで知るLuaVM (11)

Data structuresUsing java language and develop a prot.pdf
Data structuresUsing java language and develop a prot.pdfData structuresUsing java language and develop a prot.pdf
Data structuresUsing java language and develop a prot.pdf
 
(1)Objective Binary Search Tree traversal (2 points)Use traversal.pdf
(1)Objective Binary Search Tree traversal (2 points)Use traversal.pdf(1)Objective Binary Search Tree traversal (2 points)Use traversal.pdf
(1)Objective Binary Search Tree traversal (2 points)Use traversal.pdf
 
Permute
PermutePermute
Permute
 
C++ Advanced
C++ AdvancedC++ Advanced
C++ Advanced
 
Permute
PermutePermute
Permute
 
Quiz using C++
Quiz using C++Quiz using C++
Quiz using C++
 
About Go
About GoAbout Go
About Go
 
Michal Malohlava presents: Open Source H2O and Scala
Michal Malohlava presents: Open Source H2O and Scala Michal Malohlava presents: Open Source H2O and Scala
Michal Malohlava presents: Open Source H2O and Scala
 
Codigo fuente
Codigo fuenteCodigo fuente
Codigo fuente
 
Please fix my errors class Iterator public Construc.pdf
Please fix my errors   class Iterator  public  Construc.pdfPlease fix my errors   class Iterator  public  Construc.pdf
Please fix my errors class Iterator public Construc.pdf
 
Lecture5
Lecture5Lecture5
Lecture5
 

Mehr von Yuki Tamura

失敗から学ぶゲーム開発(ドラゴンジェネシス〜聖戦の絆〜の場合)
失敗から学ぶゲーム開発(ドラゴンジェネシス〜聖戦の絆〜の場合)失敗から学ぶゲーム開発(ドラゴンジェネシス〜聖戦の絆〜の場合)
失敗から学ぶゲーム開発(ドラゴンジェネシス〜聖戦の絆〜の場合)Yuki Tamura
 
ネイティブ開発アンチパターン
ネイティブ開発アンチパターンネイティブ開発アンチパターン
ネイティブ開発アンチパターンYuki Tamura
 
デザインパターンとは何か?
デザインパターンとは何か?デザインパターンとは何か?
デザインパターンとは何か?Yuki Tamura
 
ソーシャルアプリ開発におけるチーム開発戦略後半
ソーシャルアプリ開発におけるチーム開発戦略後半ソーシャルアプリ開発におけるチーム開発戦略後半
ソーシャルアプリ開発におけるチーム開発戦略後半Yuki Tamura
 
ソーシャルアプリ開発におけるチーム開発戦略前半
ソーシャルアプリ開発におけるチーム開発戦略前半ソーシャルアプリ開発におけるチーム開発戦略前半
ソーシャルアプリ開発におけるチーム開発戦略前半Yuki Tamura
 
C++の話(本当にあった怖い話)
C++の話(本当にあった怖い話)C++の話(本当にあった怖い話)
C++の話(本当にあった怖い話)Yuki Tamura
 
最速の言語Lua ~Python Hack-a-thon #3~
最速の言語Lua ~Python Hack-a-thon #3~最速の言語Lua ~Python Hack-a-thon #3~
最速の言語Lua ~Python Hack-a-thon #3~Yuki Tamura
 

Mehr von Yuki Tamura (9)

失敗から学ぶゲーム開発(ドラゴンジェネシス〜聖戦の絆〜の場合)
失敗から学ぶゲーム開発(ドラゴンジェネシス〜聖戦の絆〜の場合)失敗から学ぶゲーム開発(ドラゴンジェネシス〜聖戦の絆〜の場合)
失敗から学ぶゲーム開発(ドラゴンジェネシス〜聖戦の絆〜の場合)
 
ネイティブ開発アンチパターン
ネイティブ開発アンチパターンネイティブ開発アンチパターン
ネイティブ開発アンチパターン
 
デザインパターンとは何か?
デザインパターンとは何か?デザインパターンとは何か?
デザインパターンとは何か?
 
ソーシャルアプリ開発におけるチーム開発戦略後半
ソーシャルアプリ開発におけるチーム開発戦略後半ソーシャルアプリ開発におけるチーム開発戦略後半
ソーシャルアプリ開発におけるチーム開発戦略後半
 
ソーシャルアプリ開発におけるチーム開発戦略前半
ソーシャルアプリ開発におけるチーム開発戦略前半ソーシャルアプリ開発におけるチーム開発戦略前半
ソーシャルアプリ開発におけるチーム開発戦略前半
 
Boost study#4
Boost study#4Boost study#4
Boost study#4
 
Djangoの話
Djangoの話Djangoの話
Djangoの話
 
C++の話(本当にあった怖い話)
C++の話(本当にあった怖い話)C++の話(本当にあった怖い話)
C++の話(本当にあった怖い話)
 
最速の言語Lua ~Python Hack-a-thon #3~
最速の言語Lua ~Python Hack-a-thon #3~最速の言語Lua ~Python Hack-a-thon #3~
最速の言語Lua ~Python Hack-a-thon #3~
 

Kürzlich hochgeladen

TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...Fwdays
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningLars Bell
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piececharlottematthew16
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionDilum Bandara
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyAlfredo García Lavilla
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo DayH2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo DaySri Ambati
 

Kürzlich hochgeladen (20)

TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine Tuning
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piece
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An Introduction
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easy
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo DayH2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
 

12 分くらいで知るLuaVM

  • 1. LuaVM 1 2 L u a V M 2010 2 24
  • 2. blog: hatena: Isoparametric twitter: isoparametric name: (http://d.hatena.ne.jp/Isoparametric/) 2010 2 24
  • 3. Lua? Lua LL ANSI C 2010 2 24
  • 4. LuaVM Lua Lua C if for Lua Lua VM 2010 2 24
  • 5. Lua VM VM 2010 2 24
  • 6. I think that one cannot completely grok a scripting language, or any complex system for that matter, without slitting the animal open and examining the entrails, organs and other yucky stuff that isn’t normally seen. 2010 2 24
  • 7. LuaVM VM(5.0 ) Windows 2010 2 24
  • 8. LuaVM 2010 2 24
  • 9. VM CPU ……CPU …… …… PC…… 2010 2 24
  • 10. VM CPU ……CPU …… …… PC…… 2010 2 24
  • 11. /*---------------------------------------------------------------------- 38 (5.1.4 ) name args description ------------------------------------------------------------------------*/ OP_MOVE,/* A B R(A) := R(B) */ OP_LOADK,/* A Bx R(A) := Kst(Bx) */ OP_LOADBOOL,/* A B C R(A) := (Bool)B; if (C) pc++ */ R(X) X OP_LOADNIL,/* A B R(A) := ... := R(B) := nil */ OP_GETUPVAL,/* A B R(A) := UpValue[B] */ OP_GETGLOBAL,/* A Bx R(A) := Gbl[Kst(Bx)] */ OP_GETTABLE,/* A B C R(A) := R(B)[RK(C)] */ OP_SETGLOBAL,/* A Bx Gbl[Kst(Bx)] := R(A) */ OP_SETUPVAL,/* A B UpValue[B] := R(A) */ OP_SETTABLE,/* A B C R(A)[RK(B)] := RK(C) */ OP_NEWTABLE,/* A B C R(A) := {} (size = B,C) */ OP_SELF,/* A B C R(A+1) := R(B); R(A) := R(B)[RK(C)] */ OP_ADD,/* A B C R(A) := RK(B) + RK(C) */ K(X) X OP_SUB,/* OP_MUL,/* A B C R(A) := RK(B) - RK(C) A B C R(A) := RK(B) * RK(C) */ */ OP_DIV,/* A B C R(A) := RK(B) / RK(C) */ OP_MOD,/* A B C R(A) := RK(B) % RK(C) */ OP_POW,/* A B C R(A) := RK(B) ^ RK(C) */ OP_UNM,/* A B R(A) := -R(B) */ OP_NOT,/* A B R(A) := not R(B) */ OP_LEN,/* A B R(A) := length of R(B) */ OP_CONCAT,/* A B C R(A) := R(B).. ... ..R(C) */ RK(X) R(X) OP_JMP,/* OP_EQ,/* OP_LT,/* sBx pc+=sBx A B C if ((RK(B) == RK(C)) ~= A) then pc++ A B C if ((RK(B) < RK(C)) ~= A) then pc++ */ */ */ OP_LE,/* A B C if ((RK(B) <= RK(C)) ~= A) then pc++ */ K(X-k) OP_TEST,/* A C if not (R(A) <=> C) then pc++ OP_TESTSET,/* */ A B C if (R(B) <=> C) then R(A) := R(B) else pc++ */ OP_CALL,/* A B C R(A), ... ,R(A+C-2) := R(A)(R(A+1), ... ,R(A+B-1)) */ OP_TAILCALL,/* A B C return R(A)(R(A+1), ... ,R(A+B-1)) */ OP_RETURN,/* A B return R(A), ... ,R(A+B-2) (see note) */ OP_FORLOOP,/* A sBx R(A)+=R(A+2); if R(A) <?= R(A+1) then { pc+=sBx; R(A+3)=R(A) }*/ OP_FORPREP,/* A sBx R(A)-=R(A+2); pc+=sBx */ OP_TFORLOOP,/* A C R(A+3), ... ,R(A+2+C) := R(A)(R(A+1), R(A+2)); if R(A+3) ~= nil then R(A+2)=R(A+3) else pc++ */ OP_SETLIST,/* A B C R(A)[(C-1)*FPF+i] := R(A+i), 1 <= i <= B */ OP_CLOSE,/* A close all variables in the stack up to (>=) R(A)*/ OP_CLOSURE,/* A Bx R(A) := closure(KPROTO[Bx], R(A), ... ,R(A+n)) */ OP_VARARG/* A B R(A), R(A+1), ..., R(A+B-1) = vararg */ 2010 2 24
  • 12. /*---------------------------------------------------------------------- 38 (5.1.4 ) name args description ------------------------------------------------------------------------*/ OP_MOVE,/* A B R(A) := R(B) */ OP_LOADK,/* A Bx R(A) := Kst(Bx) */ OP_LOADBOOL,/* A B C R(A) := (Bool)B; if (C) pc++ */ R(X) X OP_LOADNIL,/* A B R(A) := ... := R(B) := nil */ OP_GETUPVAL,/* A B R(A) := UpValue[B] */ OP_GETGLOBAL,/* A Bx R(A) := Gbl[Kst(Bx)] */ OP_GETTABLE,/* A B C R(A) := R(B)[RK(C)] */ OP_SETGLOBAL,/* A Bx Gbl[Kst(Bx)] := R(A) */ OP_SETUPVAL,/* A B UpValue[B] := R(A) */ OP_SETTABLE,/* A B C R(A)[RK(B)] := RK(C) */ OP_NEWTABLE,/* A B C R(A) := {} (size = B,C) */ OP_SELF,/* A B C R(A+1) := R(B); R(A) := R(B)[RK(C)] */ OP_ADD,/* A B C R(A) := RK(B) + RK(C) */ K(X) X OP_SUB,/* OP_MUL,/* A B C R(A) := RK(B) - RK(C) A B C R(A) := RK(B) * RK(C) */ */ OP_DIV,/* A B C R(A) := RK(B) / RK(C) */ OP_MOD,/* A B C R(A) := RK(B) % RK(C) */ OP_POW,/* A B C R(A) := RK(B) ^ RK(C) */ OP_UNM,/* A B R(A) := -R(B) */ OP_NOT,/* A B R(A) := not R(B) */ OP_LEN,/* A B R(A) := length of R(B) */ OP_CONCAT,/* A B C R(A) := R(B).. ... ..R(C) */ RK(X) R(X) OP_JMP,/* OP_EQ,/* OP_LT,/* sBx pc+=sBx A B C if ((RK(B) == RK(C)) ~= A) then pc++ A B C if ((RK(B) < RK(C)) ~= A) then pc++ */ */ */ OP_LE,/* A B C if ((RK(B) <= RK(C)) ~= A) then pc++ */ K(X-k) OP_TEST,/* A C if not (R(A) <=> C) then pc++ OP_TESTSET,/* */ A B C if (R(B) <=> C) then R(A) := R(B) else pc++ */ OP_CALL,/* A B C R(A), ... ,R(A+C-2) := R(A)(R(A+1), ... ,R(A+B-1)) */ OP_TAILCALL,/* A B C return R(A)(R(A+1), ... ,R(A+B-1)) */ OP_RETURN,/* A B return R(A), ... ,R(A+B-2) (see note) */ OP_FORLOOP,/* A sBx R(A)+=R(A+2); if R(A) <?= R(A+1) then { pc+=sBx; R(A+3)=R(A) }*/ OP_FORPREP,/* A sBx R(A)-=R(A+2); pc+=sBx */ OP_TFORLOOP,/* A C R(A+3), ... ,R(A+2+C) := R(A)(R(A+1), R(A+2)); if R(A+3) ~= nil then R(A+2)=R(A+3) else pc++ */ OP_SETLIST,/* A B C R(A)[(C-1)*FPF+i] := R(A+i), 1 <= i <= B */ OP_CLOSE,/* A close all variables in the stack up to (>=) R(A)*/ OP_CLOSURE,/* A Bx R(A) := closure(KPROTO[Bx], R(A), ... ,R(A+n)) */ OP_VARARG/* A B R(A), R(A+1), ..., R(A+B-1) = vararg */ 2010 2 24
  • 13. MOVE LOADK LOADBOOL LOADNIL nil GETUPVAL GETGLOBAL GETTABLE SETGLOBAL SETUPVAL SETTABLE 2010 2 24
  • 14. NEWTABLE SELF ADD SUB MUL DIV MOD POW UNM NOT 2010 2 24
  • 15. LEN CONCAT JMP EQ LT LE TEST TESTSET CALL TAILCALL 2010 2 24
  • 16. RETURN FORLOOP for FORPREP FORPREP for TFORLOOP for SETLIST CLOSE CLOSURE VARARG 2010 2 24
  • 17. 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 OP A B C OP A Bx OP A sBx Figure 6: Instruction layout OP 6bit function max (a,b) local m = a 1 MOVE 2 0 0 ; R(2) = R(0) if b > a then 2 LT 0 0 1 ; R(0) < R(1) ? m = b end 3 4 A JMP MOVE 1 2 1 0 ; ; 8bit to 5 (4+1) R(2) = R(1) return m 5 RETURN 2 2 0 ; return R(2) end 6 B C 9bit RETURN 0 1 0 ; return 18bit 7: Bytecode for a Lua function Figure Bx(unsigned) or sBx(signed) a register or a constant (using the representation RK(X) explained above). With this format, several typical operations in Lua can be coded in a single instruction. 2010 2 24
  • 18. OP A Bx OP A sBx Figure 6: Instruction layout function max (a,b) local m = a 1 MOVE 2 0 0 ; R(2) = R(0) if b > a then 2 LT 0 0 1 ; R(0) < R(1) ? m = b 3 JMP 1 ; to 5 (4+1) end 4 MOVE 2 1 0 ; R(2) = R(1) return m 5 RETURN 2 2 0 ; return R(2) end 6 RETURN 0 1 0 ; return Figure 7: Bytecode for a Lua function a register or a constant (using the representation RK(X) explained above). With this format, several typical operations in Lua can be coded in a single instruction. For instance, the increment of a local variable, such as a = a + 1, is coded as ADD x x y, where x represents the register holding the local variable and y represents the constant 1. An assignment like a = b.f, when both a and b are local variables, is also coded as the single instruction GETTABLE x y z, where x is the register for a, y is the register for b, and z is the index of the string constant "f". (In Lua, the syntax b.f is syntactic sugar for b["f"], that is, b 2010 2 24 indexed by the string "f".)
  • 19. OP A Bx OP A sBx Figure 6: Instruction layout function max (a,b) local m = a 1 MOVE 2 0 0 ; R(2) = R(0) if b > a then 2 LT 0 0 1 ; R(0) < R(1) ? m = b 3 JMP 1 ; to 5 (4+1) end 4 MOVE 2 1 0 ; R(2) = R(1) return m 5 RETURN 2 2 0 ; return R(2) end 6 RETURN 0 1 0 ; return Figure 7: Bytecode for a Lua function R(0) == a, R(1) == b a register or a constant (using the representation RK(X) explained above). With this format, several typical operations in Lua can be coded in a single instruction. LT For instance, the increment of a local variable, such as a = a + 1, is coded as ADD x x y, where x represents the register holding the local variable and y PC++ JMP represents the constant 1. An assignment like a = b.f, when both a and b are local variables, is also coded as the single instruction GETTABLE x y z, where x OP_LT,/* A B C if ((RK(B) < RK(C)) ~= A) then pc++ is the register for a, y is the register for b, and z is the index of the string constant "f". (In Lua, the syntax b.f is syntactic sugar for b["f"], that is, b */ 2010 2 24 indexed by the string "f".)
  • 20. 256 B/C 9bit==1bit Lua 18bit (131071 ) 2010 2 24
  • 21. VM Lua VM lua -l 2010 2 24
  • 22. Lua 1.0 1.1 2.1 2.2 2.4 2.5 3.0 3.1 3.2 4.0 5.0 5.1 constructors • • • • • • • • • • • • garbage collection • • • • • • • • • • • • extensible semantics ◦ ◦ • • • • • • • • • • support for OOP ◦ ◦ • • • • • • • • • • long strings ◦ ◦ ◦ • • • • • • • • • debug API ◦ ◦ ◦ • • • • • • • • • external compiler ◦ ◦ ◦ ◦ • • • • • • • • vararg functions ◦ ◦ ◦ ◦ ◦ • • • • • • • pattern matching ◦ ◦ ◦ ◦ ◦ • • • • • • • conditional compilation ◦ ◦ ◦ ◦ ◦ ◦ • • • ◦ ◦ ◦ anonymous functions, closures ◦ ◦ ◦ ◦ ◦ ◦ ◦ • • • • • debug library ◦ ◦ ◦ ◦ ◦ ◦ ◦ ◦ • • • • multi-state API ◦ ◦ ◦ ◦ ◦ ◦ ◦ ◦ ◦ • • • for statement ◦ ◦ ◦ ◦ ◦ ◦ ◦ ◦ ◦ • • • long comments ◦ ◦ ◦ ◦ ◦ ◦ ◦ ◦ ◦ ◦ • • full lexical scoping ◦ ◦ ◦ ◦ ◦ ◦ ◦ ◦ ◦ ◦ • • booleans ◦ ◦ ◦ ◦ ◦ ◦ ◦ ◦ ◦ ◦ • • coroutines ◦ ◦ ◦ ◦ ◦ ◦ ◦ ◦ ◦ ◦ • • incremental garbage collection ◦ ◦ ◦ ◦ ◦ ◦ ◦ ◦ ◦ ◦ ◦ • module system ◦ ◦ ◦ ◦ ◦ ◦ ◦ ◦ ◦ ◦ ◦ • 1.0 1.1 2.1 2.2 2.4 2.5 3.0 3.1 3.2 4.0 5.0 5.1 libraries 4 4 4 4 4 4 4 4 5 6 8 9 built-in functions 5 7 11 11 13 14 25 27 35 0 0 0 API functions 30 30 30 30 32 32 33 47 41 60 76 79 vm type (stack × register) S S S S S S S S S S R R vm instructions 64 65 69 67 67 68 69 128 64 49 35 38 keywords 16 16 16 16 16 16 16 16 16 18 21 21 other tokens 21 21 23 23 23 23 24 25 25 25 24 26 Table 1. The evolution of features in Lua. 2010 2 24
  • 23. Lua 1.0 1.1 2.1 2.2 2.4 2.5 3.0 3.1 3.2 4.0 5.0 5.1 constructors • • • • • • • • • • • • garbage collection • • • • • • • • • • • • extensible semantics ◦ ◦ • • • • • • • • • • support for OOP ◦ ◦ • • • • • • • • • • long strings ◦ ◦ ◦ • • • • • • • • • debug API ◦ ◦ ◦ • • • • • • • • • external compiler ◦ ◦ ◦ ◦ • • • • • • • • vararg functions ◦ ◦ ◦ ◦ ◦ • • • • • • • pattern matching ◦ ◦ ◦ ◦ ◦ • • • • • • • conditional compilation ◦ ◦ ◦ ◦ ◦ ◦ • • • ◦ ◦ ◦ anonymous functions, closures ◦ ◦ ◦ ◦ ◦ ◦ ◦ • • • • • debug library ◦ ◦ ◦ ◦ ◦ ◦ ◦ ◦ • • • • multi-state API ◦ ◦ ◦ ◦ ◦ ◦ ◦ ◦ ◦ • • • for statement ◦ ◦ ◦ ◦ ◦ ◦ ◦ ◦ ◦ • • • long comments ◦ ◦ ◦ ◦ ◦ ◦ ◦ ◦ ◦ ◦ • • full lexical scoping ◦ ◦ ◦ ◦ ◦ ◦ ◦ ◦ ◦ ◦ • • booleans ◦ ◦ ◦ ◦ ◦ ◦ ◦ ◦ ◦ ◦ • • coroutines ◦ ◦ ◦ ◦ ◦ ◦ ◦ ◦ ◦ ◦ • • incremental garbage collection ◦ ◦ ◦ ◦ ◦ ◦ ◦ ◦ ◦ ◦ ◦ • module system ◦ ◦ ◦ ◦ ◦ ◦ ◦ ◦ ◦ ◦ ◦ • 1.0 1.1 2.1 2.2 2.4 2.5 3.0 3.1 3.2 4.0 5.0 5.1 libraries 4 4 4 4 4 4 4 4 5 6 8 9 built-in functions 5 7 11 11 13 14 25 27 35 0 0 0 API functions 30 30 30 30 32 32 33 47 41 60 76 79 vm type (stack × register) S S S S S S S S S S R R vm instructions 64 65 69 67 67 68 69 128 64 49 35 38 keywords 16 16 16 16 16 16 16 16 16 18 21 21 other tokens 21 21 23 23 23 23 24 25 25 25 24 26 Table 1. The evolution of features in Lua. 2010 2 24
  • 24. Lua 1.0 1.1 2.1 2.2 2.4 2.5 3.0 3.1 3.2 4.0 5.0 5.1 constructors • • • • • • • • • • • • garbage collection • • • • • • • • • • • • extensible semantics ◦ ◦ • • • • • • • • • • support for OOP ◦ ◦ • • • • • • • • • • long strings ◦ ◦ ◦ • • • • • • • • • debug API ◦ ◦ ◦ • • • • • • • • • external compiler ◦ ◦ ◦ ◦ • • • • • • • • vararg functions ◦ ◦ ◦ ◦ ◦ • • • • • • • pattern matching ◦ ◦ ◦ ◦ ◦ • • • • • • • conditional compilation ◦ ◦ ◦ ◦ ◦ ◦ • • • ◦ ◦ ◦ anonymous functions, closures ◦ ◦ ◦ ◦ ◦ ◦ ◦ • • • • • debug library ◦ ◦ ◦ ◦ ◦ ◦ ◦ ◦ • • • • multi-state API ◦ ◦ ◦ ◦ ◦ ◦ ◦ ◦ ◦ • • • for statement ◦ ◦ ◦ ◦ ◦ ◦ ◦ ◦ ◦ • • • long comments ◦ ◦ ◦ ◦ ◦ ◦ ◦ ◦ ◦ ◦ • • full lexical scoping ◦ ◦ ◦ ◦ ◦ ◦ ◦ ◦ ◦ ◦ • • booleans ◦ ◦ ◦ ◦ ◦ ◦ ◦ ◦ ◦ ◦ • • coroutines ◦ ◦ ◦ ◦ ◦ ◦ ◦ ◦ ◦ ◦ • • incremental garbage collection ◦ ◦ ◦ ◦ ◦ ◦ ◦ ◦ ◦ ◦ ◦ • module system ◦ ◦ ◦ ◦ ◦ ◦ ◦ ◦ ◦ ◦ ◦ • 1.0 1.1 2.1 2.2 2.4 2.5 3.0 3.1 3.2 4.0 5.0 5.1 libraries 4 4 4 4 4 4 4 4 5 6 8 9 built-in functions 5 7 11 11 13 14 25 27 35 0 0 0 API functions 30 30 30 30 32 32 33 47 41 60 76 79 vm type (stack × register) S S S S S S S S S S R R vm instructions 64 65 69 67 67 68 69 128 64 49 35 38 keywords 16 16 16 16 16 16 16 16 16 18 21 21 other tokens 21 21 23 23 23 23 24 25 25 25 24 26 Table 1. The evolution of features in Lua. 2010 2 24
  • 25. VM V S V M 2010 2 24
  • 26. VM VM push VM VM 2010 2 24
  • 27. VM Lua VM Lua local push/pop 2010 2 24
  • 28. LuaVM Lua 2010 2 24
  • 29. == == 2010 2 24
  • 30. == == Lua VM …… VM 2010 2 24
  • 31. 2010 2 24
  • 32. LuaVM 4byte Lua 1byte 2byte 2010 2 24
  • 33. ADD ADD ADD ADD + 2010 2 24
  • 34. JavaVM goto 2byte) 2byte 2010 2 24
  • 35. Lua 4byte Lua 1 or 2byte 2010 2 24
  • 36. local a,t,i a=a+i a=a+100 YO a=t[i] 2010 2 24
  • 37. local a,t,i a=a+i a=a+100 YO a=t[i] 1 [1] PUSHNIL 3 2 [2] GETLOCAL 0 ; a 3 [2] GETLOCAL 2 ; i 4 [2] ADD 5 [2] SETLOCAL 0 ; a 6 [3] GETLOCAL 0 ; a 7 [3] ADDI 100 8 [3] SETLOCAL 0 ; a 9 [4] GETLOCAL 1 ; t 10 [4] GETINDEXED 2 ; i 11 [4] SETLOCAL 0 ; a 12 [4] END 2010 2 24
  • 38. local a,t,i a=a+i a=a+100 YO a=t[i] 1 [1] PUSHNIL 3 1 [2]ADD 0 0 2 2 [2] GETLOCAL 0 ; a 2 [3]ADD 0 0 -1 ; - 100 3 [2] GETLOCAL 2 ; i 3 [4]GETTABLE 0 1 2 4 [2] ADD 4 [4]RETURN 0 1 5 [2] SETLOCAL 0 ; a 6 [3] GETLOCAL 0 ; a 7 [3] ADDI 100 8 [3] SETLOCAL 0 ; a 9 [4] GETLOCAL 1 ; t 10 [4] GETINDEXED 2 ; i 11 [4] SETLOCAL 0 ; a 12 [4] END 2010 2 24
  • 39. Lua 4.0.1 main <0:@test.lua> (12 instructions/96 bytes at 0x100100650) 0 params, 5 stacks, 3 locals, 0 strings, 0 numbers, 0 functions, 5 lines 1 [1] PUSHNIL 3 2 [2] GETLOCAL 0 ; a 3 [2] GETLOCAL 2 ; i 4 [2] ADD 5 [2] SETLOCAL 0 ; a 6 [3] GETLOCAL 0 ; a 7 [3] ADDI 100 8 [3] SETLOCAL 0 ; a 9 [4] GETLOCAL 1 ; t 10 [4] GETINDEXED 2 ; i 11 [4] SETLOCAL 0 ; a 12 [4] END 2010 2 24
  • 40. Lua 4.0.1 main <0:@test.lua> (12 instructions/96 bytes at 0x100100650) 0 params, 5 stacks, 3 locals, 0 strings, 0 numbers, 0 functions, 5 lines 1 [1] PUSHNIL 3 local a,t,i 2 [2] GETLOCAL 0 ; a 3 [2] GETLOCAL 2 ; i 4 [2] ADD 5 [2] SETLOCAL 0 ; a 6 [3] GETLOCAL 0 ; a 7 [3] ADDI 100 8 [3] SETLOCAL 0 ; a 9 [4] GETLOCAL 1 ; t 10 [4] GETINDEXED 2 ; i 11 [4] SETLOCAL 0 ; a 12 [4] END 2010 2 24
  • 41. Lua 4.0.1 main <0:@test.lua> (12 instructions/96 bytes at 0x100100650) 0 params, 5 stacks, 3 locals, 0 strings, 0 numbers, 0 functions, 5 lines 1 [1] PUSHNIL 3 local a,t,i 2 [2] GETLOCAL 0 ; a 3 [2] GETLOCAL 2 ; i a=a+i 4 [2] ADD 5 [2] SETLOCAL 0 ; a 6 [3] GETLOCAL 0 ; a 7 [3] ADDI 100 8 [3] SETLOCAL 0 ; a 9 [4] GETLOCAL 1 ; t 10 [4] GETINDEXED 2 ; i 11 [4] SETLOCAL 0 ; a 12 [4] END 2010 2 24
  • 42. Lua 4.0.1 main <0:@test.lua> (12 instructions/96 bytes at 0x100100650) 0 params, 5 stacks, 3 locals, 0 strings, 0 numbers, 0 functions, 5 lines 1 [1] PUSHNIL 3 local a,t,i 2 [2] GETLOCAL 0 ; a 3 [2] GETLOCAL 2 ; i a=a+i 4 [2] ADD 5 [2] SETLOCAL 0 ; a 6 [3] GETLOCAL 0 ; a 7 [3] ADDI 100 a=a+100 8 [3] SETLOCAL 0 ; a 9 [4] GETLOCAL 1 ; t 10 [4] GETINDEXED 2 ; i 11 [4] SETLOCAL 0 ; a 12 [4] END 2010 2 24
  • 43. Lua 4.0.1 main <0:@test.lua> (12 instructions/96 bytes at 0x100100650) 0 params, 5 stacks, 3 locals, 0 strings, 0 numbers, 0 functions, 5 lines 1 [1] PUSHNIL 3 local a,t,i 2 [2] GETLOCAL 0 ; a 3 [2] GETLOCAL 2 ; i a=a+i 4 [2] ADD 5 [2] SETLOCAL 0 ; a 6 [3] GETLOCAL 0 ; a 7 [3] ADDI 100 a=a+100 8 [3] SETLOCAL 0 ; a 9 [4] GETLOCAL 1 ; t 10 [4] GETINDEXED 2 ; i a=t[i] 11 [4] SETLOCAL 0 ; a 12 [4] END 2010 2 24
  • 44. Lua 4.0.1 main <0:@test.lua> (12 instructions/96 bytes at 0x100100650) 0 params, 5 stacks, 3 locals, 0 strings, 0 numbers, 0 functions, 5 lines 1 [1] PUSHNIL 3 local a,t,i 2 [2] GETLOCAL 0 ; a 3 [2] GETLOCAL 2 ; i a=a+i 4 [2] ADD 5 [2] SETLOCAL 0 ; a 6 [3] GETLOCAL 0 ; a 7 [3] ADDI 100 a=a+100 8 [3] SETLOCAL 0 ; a 9 [4] GETLOCAL 1 ; t 10 [4] GETINDEXED 2 ; i a=t[i] 11 [4] SETLOCAL 0 ; a 12 [4] END YO! ADD ADDI …… 2010 2 24
  • 45. Lua 5.1.4 main <test.lua:0,0> (4 instructions, 16 bytes at 0x100101050) 0+ params, 3 slots, 0 upvalues, 3 locals, 1 constant, 0 functions 1 [2] ADD 0 0 2 2 [3] ADD 0 0 -1 ; - 100 3 [4] GETTABLE 0 1 2 4 [4] RETURN 0 1 2010 2 24
  • 46. Lua 5.1.4 main <test.lua:0,0> (4 instructions, 16 bytes at 0x100101050) 0+ params, 3 slots, 0 upvalues, 3 locals, 1 constant, 0 functions 1 [2] ADD 0 0 2 a=a+i 2 [3] ADD 0 0 -1 ; - 100 3 [4] GETTABLE 0 1 2 4 [4] RETURN 0 1 2010 2 24
  • 47. Lua 5.1.4 main <test.lua:0,0> (4 instructions, 16 bytes at 0x100101050) 0+ params, 3 slots, 0 upvalues, 3 locals, 1 constant, 0 functions 1 [2] ADD 0 0 2 a=a+i 2 [3] ADD 0 0 -1 ; - 100 a=a+100 3 [4] GETTABLE 0 1 2 4 [4] RETURN 0 1 2010 2 24
  • 48. Lua 5.1.4 main <test.lua:0,0> (4 instructions, 16 bytes at 0x100101050) 0+ params, 3 slots, 0 upvalues, 3 locals, 1 constant, 0 functions 1 [2] ADD 0 0 2 a=a+i 2 [3] ADD 0 0 -1 ; - 100 a=a+100 3 [4] GETTABLE 0 1 2 a=t[i] 4 [4] RETURN 0 1 2010 2 24
  • 49. Lua 5.1.4 main <test.lua:0,0> (4 instructions, 16 bytes at 0x100101050) 0+ params, 3 slots, 0 upvalues, 3 locals, 1 constant, 0 functions 1 [2] ADD 0 0 2 a=a+i 2 [3] ADD 0 0 -1 ; - 100 a=a+100 3 [4] GETTABLE 0 1 2 a=t[i] 4 [4] RETURN 0 1 local a,t,i …… 2010 2 24
  • 50. 2010 2 24
  • 51. Lua :-p …… 2010 2 24
  • 52. Lua Lua 12byte tag+ union 2010 2 24
  • 53. ual machine with the new optimization for arrays can reduce the runnin up to 40%. The complete code of Lua 5.0 is available for browsing at Lua’s web site p://www.lua.org/source/5.0/. program Lua 4.0 Lua 5’ Lua 5.0 sum (2e7) 1.23 0.54 (44%) 0.54 (44%) fibo (30) 0.95 0.68 (72%) 0.69 (73%) ack (8) 1.00 0.86 (86%) 0.88 (88%) random (1e6) 1.04 0.96 (92%) 0.96 (92%) sieve (100) 0.93 0.82 (88%) 0.57 (61%) heapsort (5e4) 1.08 1.05 (97%) 0.70 (65%) matrix (50) 0.84 0.82 (98%) 0.59 (70%) re 10: Benchmarks (times in seconds; percentages are relative to Lua 4.0) Lua 2010 2 24
  • 54. L u a V M 2010 2 24
  • 55. Lua Lua Lua local 2010 2 24
  • 56. Lua 2010 2 24
  • 57. LuaVM Lua-Alchemy(Lua on Flash) kahlua(Lua on Java[J2ME]) Yueliang(Lua on Lua) LuaCLR(Lua on .NET) Lua2js(Lua on JavaScript) http://lua-users.org/wiki/ LuaImplementations 2010 2 24
  • 58. The Implementation of Lua 5.0 The Evolution of Lua A No-Frills Introduction to Lua 5.1 VM Instructions Lua lvm.c lopcodes.h 2010 2 24
  • 59. Lua Lua 2010 2 24
  • 60. Lua C Lua JIT C VM 2010 2 24
  • 61. 2010 2 24