├── doc ├── logo.gif ├── amazon.gif ├── cover.png ├── contents.html ├── manual.css ├── lua.css ├── readme.html ├── luac.1 ├── lua.1 ├── luac.html └── lua.html ├── etc ├── lua.ico ├── lua.hpp ├── all.c ├── lua.pc ├── min.c ├── embed_jit.c ├── embed_jit.cpp ├── strict.lua ├── README ├── luavs.bat └── noparser.c ├── AUTHORS ├── test ├── life.lua ├── echo.lua ├── hello.lua ├── printf.lua ├── env.lua ├── luac.lua ├── fibfor.lua ├── readonly.lua ├── table.lua ├── cf.lua ├── xd.lua ├── globals.lua ├── bisect.lua ├── fib.lua ├── factorial.lua ├── trace-calls.lua ├── sieve.lua ├── trace-globals.lua ├── README └── sort.lua ├── llvm-lua ├── tests │ ├── arg_test.lua │ ├── test4.lua │ ├── test.lua │ ├── add.lua │ ├── nums.lua │ ├── scimark_loop.lua │ ├── NOTES │ ├── test_varg_tail2.lua │ ├── dump.lua │ ├── stress_for.lua │ ├── for.lua │ ├── test2.lua │ ├── test_tail.lua │ ├── test_tail_nil_multret.lua │ ├── loops.lua │ ├── nestedloop2.lua │ ├── nestedloop.lua │ ├── hash2.lua │ ├── local_nil.lua │ ├── test3.lua │ ├── test_lineerror.lua │ ├── scimark_rand.lua │ ├── coroutine.lua │ ├── test_math.lua │ └── loadk.lua ├── llvm_lua_config.h.in ├── lua_interpreter.h ├── lua_compiler.h ├── run_tests.sh ├── TODO ├── lua_interpreter.c ├── compile_all.sh ├── lua_core.h ├── lua_normal.c ├── COPYRIGHT.llvm-lua ├── load_vm_ops.h ├── load_liblua_main.h ├── load_embedded_bc.h ├── llvm_compiler_private.h ├── llvm_dumper.h ├── load_vm_ops.cpp ├── load_liblua_main.cpp ├── llvm_dumper.cpp ├── llvm_compiler.h ├── no_jit.c ├── load_embedded_bc.cpp ├── lua_core.c ├── llvm_compiler.cpp ├── bin2c.c ├── LLVMDumper.h ├── load_jit_proto.h ├── lua-cross-compiler.in ├── hook_parser.c ├── lua-compiler.in ├── load_jit_proto.c ├── LLVMCompiler.h ├── llvm-lua.cpp └── lua_main.c ├── .hgignore ├── gen_changelog.sh ├── .gitignore ├── src ├── lapi.h ├── linit.c ├── lstring.h ├── lundump.h ├── ldebug.h ├── ltm.h ├── lfunc.h ├── lualib.h ├── ltable.h ├── lmem.h ├── lvm.h ├── lzio.h ├── ltm.c ├── lzio.c ├── ldo.h ├── llex.h ├── lmem.c ├── lparser.h ├── lcoco.h ├── llimits.h ├── lcode.h ├── lopcodes.c ├── lstring.c ├── ldump.c ├── lgc.h ├── lfunc.c └── luac.c ├── cmake_uninstall.cmake.in ├── TODO ├── tools └── hg_import_split.lua ├── cmake ├── FindLLVM.cmake └── CustomMacros.cmake ├── README ├── COPYRIGHT ├── README.llvm-lua └── INSTALL /doc/logo.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrcmdr/llvm-lua/HEAD/doc/logo.gif -------------------------------------------------------------------------------- /etc/lua.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrcmdr/llvm-lua/HEAD/etc/lua.ico -------------------------------------------------------------------------------- /AUTHORS: -------------------------------------------------------------------------------- 1 | bobby@neoawareness.com:Robert G. Jakabosky 2 | -------------------------------------------------------------------------------- /doc/amazon.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrcmdr/llvm-lua/HEAD/doc/amazon.gif -------------------------------------------------------------------------------- /doc/cover.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrcmdr/llvm-lua/HEAD/doc/cover.png -------------------------------------------------------------------------------- /test/life.lua: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrcmdr/llvm-lua/HEAD/test/life.lua -------------------------------------------------------------------------------- /doc/contents.html: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andrcmdr/llvm-lua/HEAD/doc/contents.html -------------------------------------------------------------------------------- /test/echo.lua: -------------------------------------------------------------------------------- 1 | -- echo command line arguments 2 | 3 | for i=0,#arg do 4 | print(i,arg[i]) 5 | end 6 | -------------------------------------------------------------------------------- /test/hello.lua: -------------------------------------------------------------------------------- 1 | -- the first program in every language 2 | 3 | io.write("Hello world, from ",_VERSION,"!\n") 4 | -------------------------------------------------------------------------------- /llvm-lua/tests/arg_test.lua: -------------------------------------------------------------------------------- 1 | print(unpack(arg)) 2 | local n = tonumber((arg and arg[1]) or 1) 3 | io.write(n,"\n") 4 | -------------------------------------------------------------------------------- /.hgignore: -------------------------------------------------------------------------------- 1 | # Backup files 2 | .*~$ 3 | # Rejected patches 4 | .*\.orig$ 5 | .*\.rej$ 6 | # Build directory 7 | ^build$ 8 | -------------------------------------------------------------------------------- /gen_changelog.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # 3 | 4 | svn2cl --authors=AUTHORS --break-before-msg --group-by-day -o ChangeLog 5 | 6 | -------------------------------------------------------------------------------- /llvm-lua/tests/test4.lua: -------------------------------------------------------------------------------- 1 | local a = 1 2 | local b = 2 3 | local c 4 | if a > b then 5 | c = a 6 | else 7 | c = b 8 | end 9 | print(c) 10 | -------------------------------------------------------------------------------- /llvm-lua/tests/test.lua: -------------------------------------------------------------------------------- 1 | 2 | local i 3 | local count=0 4 | 5 | for i=1,100000000 do 6 | count = count + 1 7 | end 8 | 9 | print(count) 10 | -------------------------------------------------------------------------------- /llvm-lua/tests/add.lua: -------------------------------------------------------------------------------- 1 | local a = "1" + 2 2 | local b = 1 3 | local c = 2 4 | local d = b + 2 5 | local e = 1 + c 6 | local f = b + c 7 | print(a,b,c,d,e,f) 8 | -------------------------------------------------------------------------------- /llvm-lua/tests/nums.lua: -------------------------------------------------------------------------------- 1 | local a=5 2 | local b=6 3 | local c=7 4 | local d=8 5 | a = a + 9 6 | b = b - 10 7 | c = c * 11 8 | d = d / 12 9 | print(a,b,c,d) 10 | -------------------------------------------------------------------------------- /llvm-lua/tests/scimark_loop.lua: -------------------------------------------------------------------------------- 1 | 2 | local n = 100 3 | local test = function() 4 | for i=1,n*2 do 5 | print(i) 6 | end 7 | end 8 | 9 | test(100) 10 | 11 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | build 2 | llvm-lua/*.bc 3 | llvm-lua/*_bc.h 4 | src/.libs 5 | src/*.o 6 | src/*.lo 7 | src/*.la 8 | src/*.a 9 | src/lua 10 | src/luac 11 | src/lua_test 12 | .*.swp 13 | -------------------------------------------------------------------------------- /doc/manual.css: -------------------------------------------------------------------------------- 1 | h3 code { 2 | font-family: inherit ; 3 | } 4 | 5 | pre { 6 | font-size: 105% ; 7 | } 8 | 9 | span.apii { 10 | float: right ; 11 | font-family: inherit ; 12 | } 13 | 14 | -------------------------------------------------------------------------------- /llvm-lua/tests/NOTES: -------------------------------------------------------------------------------- 1 | Some of these scripts are pieces from other scripts that have exposed bugs 2 | in past versions of llvm-lua. 3 | 4 | These scripts are only used for regression testing. 5 | 6 | -------------------------------------------------------------------------------- /llvm-lua/tests/test_varg_tail2.lua: -------------------------------------------------------------------------------- 1 | function tail_cfunc(...) 2 | local s = string.format(...) 3 | return print(s) 4 | end 5 | 6 | print(tail_cfunc("this is a test: %s %s %s %s","1","2","3","4")) 7 | 8 | -------------------------------------------------------------------------------- /test/printf.lua: -------------------------------------------------------------------------------- 1 | -- an implementation of printf 2 | 3 | function printf(...) 4 | io.write(string.format(...)) 5 | end 6 | 7 | printf("Hello %s from %s on %s\n",os.getenv"USER" or "there",_VERSION,os.date()) 8 | -------------------------------------------------------------------------------- /llvm-lua/llvm_lua_config.h.in: -------------------------------------------------------------------------------- 1 | #ifndef llvm_config_h 2 | #define llvm_config_h 3 | 4 | #define LLVM_LUA_VERSION "@LLVM_LUA_NAME@ @LLVM_LUA_VERSION@" 5 | #define LLVM_LUA_COPYRIGHT "@LLVM_LUA_COPYRIGHT@" 6 | 7 | #endif 8 | -------------------------------------------------------------------------------- /test/env.lua: -------------------------------------------------------------------------------- 1 | -- read environment variables as if they were global variables 2 | 3 | local f=function (t,i) return os.getenv(i) end 4 | setmetatable(getfenv(),{__index=f}) 5 | 6 | -- an example 7 | print(a,USER,PATH) 8 | -------------------------------------------------------------------------------- /llvm-lua/tests/dump.lua: -------------------------------------------------------------------------------- 1 | local function test() 2 | local v="test" 3 | local function test2() 4 | print(v) 5 | end 6 | test2() 7 | return 8 | end 9 | 10 | local d=string.dump(test) 11 | io.write(d) 12 | test() 13 | -------------------------------------------------------------------------------- /etc/lua.hpp: -------------------------------------------------------------------------------- 1 | // lua.hpp 2 | // Lua header files for C++ 3 | // <> not supplied automatically because Lua also compiles as C++ 4 | 5 | extern "C" { 6 | #include "lua.h" 7 | #include "lualib.h" 8 | #include "lauxlib.h" 9 | } 10 | -------------------------------------------------------------------------------- /llvm-lua/tests/stress_for.lua: -------------------------------------------------------------------------------- 1 | local width = 100 2 | local xb = 50 3 | local a = 0 4 | 5 | for x=0, xb < width and 10 or 20 do 6 | a = a + 1 7 | end 8 | 9 | for x=xb < width and 10 or 20,0,-1 do 10 | a = a + 1 11 | end 12 | 13 | print("a=", a) 14 | 15 | -------------------------------------------------------------------------------- /llvm-lua/tests/for.lua: -------------------------------------------------------------------------------- 1 | local n = tonumber((arg and arg[1]) or 1) 2 | local hash1={} 3 | for i=0,10000 do 4 | hash1["foo_"..i] = i 5 | hash1[i] = i 6 | end 7 | for i=0,10000 do 8 | print(i,hash1[i]) 9 | end 10 | print(hash1["foo_1"], hash1["foo_9999"]) 11 | -------------------------------------------------------------------------------- /test/luac.lua: -------------------------------------------------------------------------------- 1 | -- bare-bones luac in Lua 2 | -- usage: lua luac.lua file.lua 3 | 4 | assert(arg[1]~=nil and arg[2]==nil,"usage: lua luac.lua file.lua") 5 | f=assert(io.open("luac.out","wb")) 6 | assert(f:write(string.dump(assert(loadfile(arg[1]))))) 7 | assert(f:close()) 8 | -------------------------------------------------------------------------------- /llvm-lua/lua_interpreter.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef lua_interpreter_h 3 | #define lua_interpreter_h 4 | 5 | #ifdef __cplusplus 6 | extern "C" { 7 | #endif 8 | 9 | extern int lua_main(int argc, char **argv); 10 | 11 | #ifdef __cplusplus 12 | } 13 | #endif 14 | 15 | #endif 16 | 17 | -------------------------------------------------------------------------------- /llvm-lua/lua_compiler.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef lua_compiler_h 3 | #define lua_compiler_h 4 | 5 | #ifdef __cplusplus 6 | extern "C" { 7 | #endif 8 | 9 | #include "lua_core.h" 10 | 11 | extern int luac_main(int argc, char **argv); 12 | 13 | #ifdef __cplusplus 14 | } 15 | #endif 16 | 17 | #endif 18 | 19 | -------------------------------------------------------------------------------- /llvm-lua/tests/test2.lua: -------------------------------------------------------------------------------- 1 | function f(x,y,z) 2 | print("hook",x,y,z) 3 | end 4 | 5 | function g() 6 | print"0" 7 | debug.sethook(f,"l") 8 | print"1" 9 | print"2" 10 | print"3" 11 | print"4" 12 | end 13 | 14 | print("pcall",pcall(g)) 15 | 16 | -------------------------------------------------------------------------------- /llvm-lua/run_tests.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # 3 | 4 | for script in `ls tests/*.lua`; do 5 | echo "run test: $script" 6 | llvm-lua -g -O0 $script >/dev/null || { 7 | echo "Failed to run: $script" 8 | } 9 | #llvm-lua -g -O0 $script 10 | #lua $script 11 | #llvm-lua -O3 $script >/dev/null 12 | done 13 | 14 | -------------------------------------------------------------------------------- /llvm-lua/TODO: -------------------------------------------------------------------------------- 1 | 2 | optimizations: 3 | * When the internal index/limit/step variable of a OP_FORLOOP get moved to the C-Stack the space on the Lua-stack is unused. We should be able to decrease the maxstacksize by re-using those stack slots. 4 | * Try to inline some table/global lookups if they have a constant key. 5 | 6 | -------------------------------------------------------------------------------- /test/fibfor.lua: -------------------------------------------------------------------------------- 1 | -- example of for with generator functions 2 | 3 | function generatefib (n) 4 | return coroutine.wrap(function () 5 | local a,b = 1, 1 6 | while a <= n do 7 | coroutine.yield(a) 8 | a, b = b, a+b 9 | end 10 | end) 11 | end 12 | 13 | for i in generatefib(1000) do print(i) end 14 | -------------------------------------------------------------------------------- /test/readonly.lua: -------------------------------------------------------------------------------- 1 | -- make global variables readonly 2 | 3 | local f=function (t,i) error("cannot redefine global variable `"..i.."'",2) end 4 | local g={} 5 | local G=getfenv() 6 | setmetatable(g,{__index=G,__newindex=f}) 7 | setfenv(1,g) 8 | 9 | -- an example 10 | rawset(g,"x",3) 11 | x=2 12 | y=1 -- cannot redefine `y' 13 | -------------------------------------------------------------------------------- /llvm-lua/tests/test_tail.lua: -------------------------------------------------------------------------------- 1 | local function tail(depth) 2 | if(depth > 0) then 3 | return tail(depth - 1) 4 | end 5 | return 0 6 | end 7 | 8 | local N = tonumber(arg and arg[1]) or 1 9 | print(tail(N)) 10 | 11 | local function tail_math() 12 | -- tail call 13 | return math.sin("a") 14 | end 15 | 16 | tail_math() 17 | 18 | -------------------------------------------------------------------------------- /src/lapi.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lapi.h,v 2.2.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** Auxiliary functions from Lua API 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lapi_h 8 | #define lapi_h 9 | 10 | 11 | #include "lobject.h" 12 | 13 | 14 | LUAI_FUNC void luaA_pushobject (lua_State *L, const TValue *o); 15 | 16 | #endif 17 | -------------------------------------------------------------------------------- /test/table.lua: -------------------------------------------------------------------------------- 1 | -- make table, grouping all data for the same item 2 | -- input is 2 columns (item, data) 3 | 4 | local A 5 | while 1 do 6 | local l=io.read() 7 | if l==nil then break end 8 | local _,_,a,b=string.find(l,'"?([_%w]+)"?%s*(.*)$') 9 | if a~=A then A=a io.write("\n",a,":") end 10 | io.write(" ",b) 11 | end 12 | io.write("\n") 13 | -------------------------------------------------------------------------------- /llvm-lua/tests/test_tail_nil_multret.lua: -------------------------------------------------------------------------------- 1 | do 2 | local f = function(n) 3 | local x = {} 4 | for i=1,n do 5 | x[i] = i 6 | end 7 | return unpack(x) 8 | end 9 | 10 | local a,b,c 11 | a,b,c = 0,5,f(0) 12 | print("a,b,c", a, b, c) 13 | assert(a==0 and b==5 and c==nil) 14 | a={f(0)} 15 | print("#a = ", #a) 16 | print("a = ", unpack(a)) 17 | end 18 | -------------------------------------------------------------------------------- /test/cf.lua: -------------------------------------------------------------------------------- 1 | -- temperature conversion table (celsius to farenheit) 2 | 3 | for c0=-20,50-1,10 do 4 | io.write("C ") 5 | for c=c0,c0+10-1 do 6 | io.write(string.format("%3.0f ",c)) 7 | end 8 | io.write("\n") 9 | 10 | io.write("F ") 11 | for c=c0,c0+10-1 do 12 | f=(9/5)*c+32 13 | io.write(string.format("%3.0f ",f)) 14 | end 15 | io.write("\n\n") 16 | end 17 | -------------------------------------------------------------------------------- /llvm-lua/tests/loops.lua: -------------------------------------------------------------------------------- 1 | 2 | local x = 0 3 | for a=1,25 do 4 | x = x + 1 5 | end 6 | for b=1,25 do 7 | x = x + 1 8 | end 9 | for c=1,25 do 10 | x = x + 1 11 | end 12 | for d=1,25 do 13 | x = x + 1 14 | end 15 | for e=1,25 do 16 | x = x + 1 17 | end 18 | for f=1,25 do 19 | x = x + 1 20 | end 21 | --print(a,",",b,",",c,",",d,",",e,",",f,",",x,"\n") 22 | io.write(x,"\n") 23 | -------------------------------------------------------------------------------- /llvm-lua/lua_interpreter.c: -------------------------------------------------------------------------------- 1 | /* 2 | * lua_interpreter.c -- Lua interpreter 3 | */ 4 | 5 | #ifdef __cplusplus 6 | extern "C" { 7 | #endif 8 | 9 | #include "lua_core.h" 10 | #include "lua_interpreter.h" 11 | 12 | #define ENABLE_PARSER_HOOK 1 13 | #include "hook_parser.c" 14 | 15 | #define main lua_main 16 | #include "lua.c" 17 | #undef main 18 | 19 | #ifdef __cplusplus 20 | } 21 | #endif 22 | 23 | -------------------------------------------------------------------------------- /llvm-lua/compile_all.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | # 3 | 4 | OPTS="" 5 | FILES="" 6 | # parse command line parameters. 7 | for arg in "$@" ; do 8 | case "$arg" in 9 | -*) OPTS="$OPTS $arg" ;; 10 | *) FILES="$FILES $arg" ;; 11 | esac 12 | done 13 | 14 | for script in $FILES; do 15 | echo "Compiling script: $script" 16 | lua-compiler $OPTS $script 17 | #./lua-compiler $OPTS $script 18 | done 19 | 20 | -------------------------------------------------------------------------------- /llvm-lua/tests/nestedloop2.lua: -------------------------------------------------------------------------------- 1 | local x = 0 2 | for a=25,1,-1 do 3 | for b=25,1,-1 do 4 | for c=25,1,-1 do 5 | for d=25,1,-1 do 6 | for e=25,1,-1 do 7 | for f=25,1,-1 do 8 | --io.write(a,",",b,",",c,",",d,",",e,",",f,",",x,"\n") 9 | x = x + 1 10 | end 11 | end 12 | end 13 | end 14 | end 15 | end 16 | print(a,",",b,",",c,",",d,",",e,",",f,",",x,"\n") 17 | io.write(x,"\n") 18 | -------------------------------------------------------------------------------- /test/xd.lua: -------------------------------------------------------------------------------- 1 | -- hex dump 2 | -- usage: lua xd.lua < file 3 | 4 | local offset=0 5 | while true do 6 | local s=io.read(16) 7 | if s==nil then return end 8 | io.write(string.format("%08X ",offset)) 9 | string.gsub(s,"(.)", 10 | function (c) io.write(string.format("%02X ",string.byte(c))) end) 11 | io.write(string.rep(" ",3*(16-string.len(s)))) 12 | io.write(" ",string.gsub(s,"%c","."),"\n") 13 | offset=offset+16 14 | end 15 | -------------------------------------------------------------------------------- /llvm-lua/tests/nestedloop.lua: -------------------------------------------------------------------------------- 1 | local x = 0 2 | local x2 = 0 3 | for a=1,25 do 4 | for b=1,25 do 5 | for c=1,25 do 6 | for d=1,25 do 7 | for e=1,25 do 8 | for f=1,25 do 9 | --io.write(a,",",b,",",c,",",d,",",e,",",f,",",x,"\n") 10 | x = x + 1 11 | x2 = x2 + a + b + c + d + e + f 12 | end 13 | end 14 | end 15 | end 16 | end 17 | end 18 | --print(a,",",b,",",c,",",d,",",e,",",f,",",x,"\n") 19 | io.write(x,"\n") 20 | io.write(x2,"\n") 21 | -------------------------------------------------------------------------------- /test/globals.lua: -------------------------------------------------------------------------------- 1 | -- reads luac listings and reports global variable usage 2 | -- lines where a global is written to are marked with "*" 3 | -- typical usage: luac -p -l file.lua | lua globals.lua | sort | lua table.lua 4 | 5 | while 1 do 6 | local s=io.read() 7 | if s==nil then break end 8 | local ok,_,l,op,g=string.find(s,"%[%-?(%d*)%]%s*([GS])ETGLOBAL.-;%s+(.*)$") 9 | if ok then 10 | if op=="S" then op="*" else op="" end 11 | io.write(g,"\t",l,op,"\n") 12 | end 13 | end 14 | -------------------------------------------------------------------------------- /llvm-lua/tests/hash2.lua: -------------------------------------------------------------------------------- 1 | local n = tonumber((arg and arg[1]) or 1) 2 | 3 | local hash1={} 4 | for i=0,10000 do 5 | hash1["foo_"..i] = i 6 | end 7 | local hash2={} 8 | for i=1,n do 9 | for k,v in pairs(hash1) do 10 | hash2[k] = v + (hash2[k] or 0) 11 | end 12 | end 13 | 14 | print(hash1["foo_1"], hash1["foo_9999"], 15 | hash2["foo_1"], hash2["foo_9999"]) 16 | --io.write(string.format("%d %d %d %d\n", hash1["foo_1"], hash1["foo_9999"], 17 | -- hash2["foo_1"], hash2["foo_9999"])) 18 | -------------------------------------------------------------------------------- /llvm-lua/tests/local_nil.lua: -------------------------------------------------------------------------------- 1 | --[[ 2 | local function min_stack() 3 | end 4 | local function min_stack_params(a,b,c,d,e) 5 | end 6 | local function tail_recursive_nil(depth) 7 | local a,b,c,d,e 8 | print("should be 5 nil values:",a,b,c,d,e) 9 | a,b,c,d,e = 1,2,3,4,5 10 | if(depth == 0) then return a,b,c,d,e end 11 | return tail_recursive_nil(depth - 1) 12 | end 13 | print(tail_recursive_nil(2)) 14 | ]] 15 | local function test_nil() 16 | local a,b,c,d,e 17 | return a,b,c,d,e 18 | end 19 | print(test_nil()) 20 | -------------------------------------------------------------------------------- /llvm-lua/tests/test3.lua: -------------------------------------------------------------------------------- 1 | debug.sethook(function(e,l,x) print(e,l,x) end, "l") 2 | 3 | -- very inefficient fibonacci function 4 | function fib(n) 5 | N=N+1 6 | if n<2 then 7 | return n 8 | else 9 | return fib(n-1)+fib(n-2) 10 | end 11 | end 12 | 13 | -- run and time it 14 | function test(s,f) 15 | N=0 16 | local c=os.clock() 17 | local v=f(n) 18 | local t=os.clock()-c 19 | print(s,n,v,t,N) 20 | end 21 | 22 | n=arg[1] or 24 -- for other values, do lua fib.lua XX 23 | n=tonumber(n) 24 | print("","n","value","time","evals") 25 | test("plain",fib) 26 | 27 | -------------------------------------------------------------------------------- /llvm-lua/lua_core.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef lua_core_h 3 | #define lua_core_h 4 | 5 | #ifdef __cplusplus 6 | extern "C" { 7 | #endif 8 | 9 | #define lua_c 10 | #define loslib_c 11 | #define LUA_CORE 12 | 13 | /* Lua interpreter with LLVM JIT support. */ 14 | #define JIT_SUPPORT 15 | 16 | /* extra variables for global_State */ 17 | #define JIT_COMPILER_STATE \ 18 | void *llvm_compiler; 19 | 20 | /* state */ 21 | #define JIT_PROTO_STATE \ 22 | lua_CFunction jit_func; /* jit compiled function */ \ 23 | void *func_ref; /* Reference to Function class */ 24 | 25 | #include 26 | /* extern all lua core functions. */ 27 | #undef LUAI_FUNC 28 | #define LUAI_FUNC extern 29 | 30 | #ifdef __cplusplus 31 | } 32 | #endif 33 | 34 | #endif 35 | 36 | -------------------------------------------------------------------------------- /test/bisect.lua: -------------------------------------------------------------------------------- 1 | -- bisection method for solving non-linear equations 2 | 3 | delta=1e-6 -- tolerance 4 | 5 | function bisect(f,a,b,fa,fb) 6 | local c=(a+b)/2 7 | io.write(n," c=",c," a=",a," b=",b,"\n") 8 | if c==a or c==b or math.abs(a-b) #CFLAGS=" -ggdb -O3 -fomit-frame-pointer -pipe -Wall " 17 | CFLAGS=" -O3 -fomit-frame-pointer -pipe -Wl,-E " 18 | http://www.tecgraf.puc-rio.br/~lhf/ftp/lua/install.html 19 | 20 | -------------------------------------------------------------------------------- /etc/lua.pc: -------------------------------------------------------------------------------- 1 | # lua.pc -- pkg-config data for Lua 2 | 3 | # vars from install Makefile 4 | 5 | # grep '^V=' ../Makefile 6 | V= 5.1 7 | # grep '^R=' ../Makefile 8 | R= 5.1.4 9 | 10 | # grep '^INSTALL_.*=' ../Makefile | sed 's/INSTALL_TOP/prefix/' 11 | prefix= /usr 12 | INSTALL_BIN= ${prefix}/bin 13 | INSTALL_INC= ${prefix}/include 14 | INSTALL_LIB= ${prefix}/lib 15 | INSTALL_MAN= ${prefix}/man/man1 16 | INSTALL_LMOD= ${prefix}/share/lua/${V} 17 | INSTALL_CMOD= ${prefix}/lib/lua/${V} 18 | 19 | # canonical vars 20 | exec_prefix=${prefix} 21 | libdir=${exec_prefix}/lib 22 | includedir=${prefix}/include 23 | 24 | Name: LLVM-Lua 25 | Description: An Extensible Extension Language with JIT support from LLVM 26 | Version: ${R} 27 | Requires: 28 | Libs: -L${libdir} -llua -lm `llvm-config --ldflags --libs core jit native bitreader bitwriter ipo` 29 | Cflags: -I${includedir} 30 | 31 | # (end of lua.pc) 32 | -------------------------------------------------------------------------------- /test/trace-calls.lua: -------------------------------------------------------------------------------- 1 | -- trace calls 2 | -- example: lua -ltrace-calls bisect.lua 3 | 4 | local level=0 5 | 6 | local function hook(event) 7 | local t=debug.getinfo(3) 8 | io.write(level," >>> ",string.rep(" ",level)) 9 | if t~=nil and t.currentline>=0 then io.write(t.short_src,":",t.currentline," ") end 10 | t=debug.getinfo(2) 11 | if event=="call" then 12 | level=level+1 13 | else 14 | level=level-1 if level<0 then level=0 end 15 | end 16 | if t.what=="main" then 17 | if event=="call" then 18 | io.write("begin ",t.short_src) 19 | else 20 | io.write("end ",t.short_src) 21 | end 22 | elseif t.what=="Lua" then 23 | -- table.foreach(t,print) 24 | io.write(event," ",t.name or "(Lua)"," <",t.linedefined,":",t.short_src,">") 25 | else 26 | io.write(event," ",t.name or "(C)"," [",t.what,"] ") 27 | end 28 | io.write("\n") 29 | end 30 | 31 | debug.sethook(hook,"cr") 32 | level=0 33 | -------------------------------------------------------------------------------- /test/sieve.lua: -------------------------------------------------------------------------------- 1 | -- the sieve of of Eratosthenes programmed with coroutines 2 | -- typical usage: lua -e N=1000 sieve.lua | column 3 | 4 | -- generate all the numbers from 2 to n 5 | function gen (n) 6 | return coroutine.wrap(function () 7 | for i=2,n do coroutine.yield(i) end 8 | end) 9 | end 10 | 11 | -- filter the numbers generated by `g', removing multiples of `p' 12 | function filter (p, g) 13 | return coroutine.wrap(function () 14 | while 1 do 15 | local n = g() 16 | if n == nil then return end 17 | if math.fmod(n, p) ~= 0 then coroutine.yield(n) end 18 | end 19 | end) 20 | end 21 | 22 | N=N or 1000 -- from command line 23 | x = gen(N) -- generate primes up to N 24 | while 1 do 25 | local n = x() -- pick a number until done 26 | if n == nil then break end 27 | print(n) -- must be a prime number 28 | x = filter(n, x) -- now remove its multiples 29 | end 30 | -------------------------------------------------------------------------------- /test/trace-globals.lua: -------------------------------------------------------------------------------- 1 | -- trace assigments to global variables 2 | 3 | do 4 | -- a tostring that quotes strings. note the use of the original tostring. 5 | local _tostring=tostring 6 | local tostring=function(a) 7 | if type(a)=="string" then 8 | return string.format("%q",a) 9 | else 10 | return _tostring(a) 11 | end 12 | end 13 | 14 | local log=function (name,old,new) 15 | local t=debug.getinfo(3,"Sl") 16 | local line=t.currentline 17 | io.write(t.short_src) 18 | if line>=0 then io.write(":",line) end 19 | io.write(": ",name," is now ",tostring(new)," (was ",tostring(old),")","\n") 20 | end 21 | 22 | local g={} 23 | local set=function (t,name,value) 24 | log(name,g[name],value) 25 | g[name]=value 26 | end 27 | setmetatable(getfenv(),{__index=g,__newindex=set}) 28 | end 29 | 30 | -- an example 31 | 32 | a=1 33 | b=2 34 | a=10 35 | b=20 36 | b=nil 37 | b=200 38 | print(a,b,c) 39 | -------------------------------------------------------------------------------- /src/linit.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: linit.c,v 1.14.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** Initialization of libraries for lua.c 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #define linit_c 9 | #define LUA_LIB 10 | 11 | #include "lua.h" 12 | 13 | #include "lualib.h" 14 | #include "lauxlib.h" 15 | 16 | 17 | static const luaL_Reg lualibs[] = { 18 | {"", luaopen_base}, 19 | {LUA_LOADLIBNAME, luaopen_package}, 20 | {LUA_TABLIBNAME, luaopen_table}, 21 | {LUA_IOLIBNAME, luaopen_io}, 22 | {LUA_OSLIBNAME, luaopen_os}, 23 | {LUA_STRLIBNAME, luaopen_string}, 24 | {LUA_MATHLIBNAME, luaopen_math}, 25 | {LUA_DBLIBNAME, luaopen_debug}, 26 | {NULL, NULL} 27 | }; 28 | 29 | 30 | LUALIB_API void luaL_openlibs (lua_State *L) { 31 | const luaL_Reg *lib = lualibs; 32 | for (; lib->func; lib++) { 33 | lua_pushcfunction(L, lib->func); 34 | lua_pushstring(L, lib->name); 35 | lua_call(L, 1, 0); 36 | } 37 | } 38 | 39 | -------------------------------------------------------------------------------- /src/lstring.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lstring.h,v 1.43.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** String table (keep all strings handled by Lua) 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lstring_h 8 | #define lstring_h 9 | 10 | 11 | #include "lgc.h" 12 | #include "lobject.h" 13 | #include "lstate.h" 14 | 15 | 16 | #define sizestring(s) (sizeof(union TString)+((s)->len+1)*sizeof(char)) 17 | 18 | #define sizeudata(u) (sizeof(union Udata)+(u)->len) 19 | 20 | #define luaS_new(L, s) (luaS_newlstr(L, s, strlen(s))) 21 | #define luaS_newliteral(L, s) (luaS_newlstr(L, "" s, \ 22 | (sizeof(s)/sizeof(char))-1)) 23 | 24 | #define luaS_fix(s) l_setbit((s)->tsv.marked, FIXEDBIT) 25 | 26 | LUAI_FUNC void luaS_resize (lua_State *L, int newsize); 27 | LUAI_FUNC Udata *luaS_newudata (lua_State *L, size_t s, Table *e); 28 | LUAI_FUNC TString *luaS_newlstr (lua_State *L, const char *str, size_t l); 29 | 30 | 31 | #endif 32 | -------------------------------------------------------------------------------- /etc/min.c: -------------------------------------------------------------------------------- 1 | /* 2 | * min.c -- a minimal Lua interpreter 3 | * loads stdin only with minimal error handling. 4 | * no interaction, and no standard library, only a "print" function. 5 | */ 6 | 7 | #include 8 | 9 | #include "lua.h" 10 | #include "lauxlib.h" 11 | 12 | static int print(lua_State *L) 13 | { 14 | int n=lua_gettop(L); 15 | int i; 16 | for (i=1; i<=n; i++) 17 | { 18 | if (i>1) printf("\t"); 19 | if (lua_isstring(L,i)) 20 | printf("%s",lua_tostring(L,i)); 21 | else if (lua_isnil(L,i)) 22 | printf("%s","nil"); 23 | else if (lua_isboolean(L,i)) 24 | printf("%s",lua_toboolean(L,i) ? "true" : "false"); 25 | else 26 | printf("%s:%p",luaL_typename(L,i),lua_topointer(L,i)); 27 | } 28 | printf("\n"); 29 | return 0; 30 | } 31 | 32 | int main(void) 33 | { 34 | lua_State *L=lua_open(); 35 | lua_register(L,"print",print); 36 | if (luaL_dofile(L,NULL)!=0) fprintf(stderr,"%s\n",lua_tostring(L,-1)); 37 | lua_close(L); 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /tools/hg_import_split.lua: -------------------------------------------------------------------------------- 1 | 2 | local patch = arg[1] 3 | if not patch then 4 | print("run:", arg[0] .. "") 5 | return 6 | end 7 | 8 | local prefix, ext = patch:match("^(.*)%.(.*)$") 9 | 10 | print("split HG patch set:", prefix .. '.' .. ext) 11 | 12 | local input = io.open(patch, "rb") 13 | 14 | local part = 0 15 | local output 16 | 17 | local function append_line(line) 18 | if output then 19 | output:write(line, '\n') 20 | end 21 | end 22 | 23 | local function open_next_output() 24 | if output then 25 | -- close previous patch. 26 | output:close() 27 | output = nil 28 | end 29 | part = part + 1 30 | local name = string.format("%s_%d.%s", prefix, part, ext) 31 | print("Start patch file:", name) 32 | output = io.open(name, "w+b") 33 | end 34 | 35 | for line in input:lines() do 36 | if line == "# HG changeset patch" then 37 | open_next_output() 38 | end 39 | append_line(line) 40 | end 41 | 42 | if output then 43 | output:close() 44 | end 45 | 46 | -------------------------------------------------------------------------------- /cmake/FindLLVM.cmake: -------------------------------------------------------------------------------- 1 | # - Find libev 2 | # Find the native LLVM includes and library 3 | # 4 | # LLVM_INCLUDE_DIR - where to find ev.h, etc. 5 | # LLVM_LIBRARIES - List of libraries when using libev. 6 | # LLVM_FOUND - True if libev found. 7 | 8 | find_program(LLVM_CONFIG_EXECUTABLE NAMES "${LLVM_PATH}/bin/llvm-config" DOC "llvm-config executable") 9 | 10 | execute_process( 11 | COMMAND ${LLVM_CONFIG_EXECUTABLE} --cppflags 12 | OUTPUT_VARIABLE LLVM_CFLAGS 13 | OUTPUT_STRIP_TRAILING_WHITESPACE 14 | ) 15 | 16 | execute_process( 17 | COMMAND ${LLVM_CONFIG_EXECUTABLE} --ldflags 18 | OUTPUT_VARIABLE LLVM_LFLAGS 19 | OUTPUT_STRIP_TRAILING_WHITESPACE 20 | ) 21 | execute_process( 22 | COMMAND ${LLVM_CONFIG_EXECUTABLE} --libs core jit native linker bitreader bitwriter ipo 23 | OUTPUT_VARIABLE LLVM_JIT_LIBS 24 | OUTPUT_STRIP_TRAILING_WHITESPACE 25 | ) 26 | execute_process( 27 | COMMAND ${LLVM_CONFIG_EXECUTABLE} --libs all 28 | OUTPUT_VARIABLE LLVM_ALL_LIBS 29 | OUTPUT_STRIP_TRAILING_WHITESPACE 30 | ) 31 | 32 | -------------------------------------------------------------------------------- /doc/readme.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | Lua documentation 4 | 5 | 6 | 7 | 8 | 9 |
10 |

11 | Lua 12 | Documentation 13 |

14 | 15 | This is the documentation included in the source distribution of Lua 5.1.4. 16 | 17 | 25 | 26 | Lua's 27 | official web site 28 | contains updated documentation, 29 | especially the 30 | reference manual. 31 |

32 | 33 |


34 | 35 | Last update: 36 | Tue Aug 12 14:46:07 BRT 2008 37 | 38 | 39 | 40 | 41 | -------------------------------------------------------------------------------- /src/lundump.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lundump.h,v 1.37.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** load precompiled Lua chunks 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lundump_h 8 | #define lundump_h 9 | 10 | #include "lobject.h" 11 | #include "lzio.h" 12 | 13 | /* load one chunk; from lundump.c */ 14 | LUAI_FUNC Proto* luaU_undump (lua_State* L, ZIO* Z, Mbuffer* buff, const char* name); 15 | 16 | /* make header; from lundump.c */ 17 | LUAI_FUNC void luaU_header (char* h); 18 | 19 | /* dump one chunk; from ldump.c */ 20 | LUAI_FUNC int luaU_dump (lua_State* L, const Proto* f, lua_Writer w, void* data, int strip); 21 | 22 | #ifdef luac_c 23 | /* print one chunk; from print.c */ 24 | LUAI_FUNC void luaU_print (const Proto* f, int full); 25 | #endif 26 | 27 | /* for header of binary files -- this is Lua 5.1 */ 28 | #define LUAC_VERSION 0x51 29 | 30 | /* for header of binary files -- this is the official format */ 31 | #define LUAC_FORMAT 0 32 | 33 | /* size of header of binary files */ 34 | #define LUAC_HEADERSIZE 12 35 | 36 | #endif 37 | -------------------------------------------------------------------------------- /llvm-lua/lua_normal.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** See Copyright Notice in lua.h 3 | */ 4 | 5 | /* 6 | * lua_normal.c -- Lua core, libraries and interpreter in a single file 7 | */ 8 | 9 | #define lua_normal_c 10 | 11 | #define luaall_c 12 | #define LUA_CORE 13 | #include "lapi.c" 14 | #include "lcode.c" 15 | #include "ldebug.c" 16 | #include "ldump.c" 17 | #include "lfunc.c" 18 | #include "lgc.c" 19 | #include "llex.c" 20 | #include "lmem.c" 21 | #include "lobject.c" 22 | #include "lopcodes.c" 23 | #include "lparser.c" 24 | #include "lstate.c" 25 | #include "lstring.c" 26 | #include "ltable.c" 27 | #include "ltm.c" 28 | #include "lundump.c" 29 | #include "lvm.c" 30 | #include "lzio.c" 31 | 32 | #include "lbaselib.c" 33 | #include "lcoco.c" 34 | #include "ldblib.c" 35 | #include "liolib.c" 36 | #include "linit.c" 37 | #include "lmathlib.c" 38 | #include "loadlib.c" 39 | #include "loslib.c" 40 | #include "lstrlib.c" 41 | #include "ltablib.c" 42 | 43 | #include "lauxlib.c" 44 | 45 | #include "ldo.c" 46 | #include "lua.c" 47 | 48 | #ifdef __cplusplus 49 | } 50 | #endif 51 | 52 | -------------------------------------------------------------------------------- /etc/embed_jit.c: -------------------------------------------------------------------------------- 1 | /* 2 | * min.c -- a minimal Lua interpreter 3 | * loads stdin only with minimal error handling. 4 | * no interaction, and no standard library, only a "print" function. 5 | */ 6 | 7 | #include 8 | 9 | #include "lua.h" 10 | #include "lauxlib.h" 11 | 12 | static int print(lua_State *L) 13 | { 14 | int n=lua_gettop(L); 15 | int i; 16 | for (i=1; i<=n; i++) 17 | { 18 | if (i>1) printf("\t"); 19 | if (lua_isstring(L,i)) 20 | printf("%s",lua_tostring(L,i)); 21 | else if (lua_isnil(L,i)) 22 | printf("%s","nil"); 23 | else if (lua_isboolean(L,i)) 24 | printf("%s",lua_toboolean(L,i) ? "true" : "false"); 25 | else 26 | printf("%s:%p",luaL_typename(L,i),lua_topointer(L,i)); 27 | } 28 | printf("\n"); 29 | return 0; 30 | } 31 | 32 | int main(int argc, char **argv) 33 | { 34 | lua_State *L=lua_open(); 35 | char *file=NULL; 36 | lua_register(L,"print",print); 37 | if (argc > 1) { 38 | file = argv[1]; 39 | } 40 | if (luaL_dofile(L,file)!=0) fprintf(stderr,"%s\n",lua_tostring(L,-1)); 41 | lua_close(L); 42 | return 0; 43 | } 44 | -------------------------------------------------------------------------------- /etc/embed_jit.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * min.c -- a minimal Lua interpreter 3 | * loads stdin only with minimal error handling. 4 | * no interaction, and no standard library, only a "print" function. 5 | */ 6 | 7 | #include 8 | 9 | extern "C" { 10 | #include "lua.h" 11 | #include "lauxlib.h" 12 | } 13 | 14 | static int print(lua_State *L) 15 | { 16 | int n=lua_gettop(L); 17 | int i; 18 | for (i=1; i<=n; i++) 19 | { 20 | if (i>1) printf("\t"); 21 | if (lua_isstring(L,i)) 22 | printf("%s",lua_tostring(L,i)); 23 | else if (lua_isnil(L,i)) 24 | printf("%s","nil"); 25 | else if (lua_isboolean(L,i)) 26 | printf("%s",lua_toboolean(L,i) ? "true" : "false"); 27 | else 28 | printf("%s:%p",luaL_typename(L,i),lua_topointer(L,i)); 29 | } 30 | printf("\n"); 31 | return 0; 32 | } 33 | 34 | int main(int argc, char **argv) 35 | { 36 | lua_State *L=lua_open(); 37 | char *file=NULL; 38 | lua_register(L,"print",print); 39 | if (argc > 1) { 40 | file = argv[1]; 41 | } 42 | if (luaL_dofile(L,file)!=0) fprintf(stderr,"%s\n",lua_tostring(L,-1)); 43 | lua_close(L); 44 | return 0; 45 | } 46 | -------------------------------------------------------------------------------- /etc/strict.lua: -------------------------------------------------------------------------------- 1 | -- 2 | -- strict.lua 3 | -- checks uses of undeclared global variables 4 | -- All global variables must be 'declared' through a regular assignment 5 | -- (even assigning nil will do) in a main chunk before being used 6 | -- anywhere or assigned to inside a function. 7 | -- 8 | 9 | local getinfo, error, rawset, rawget = debug.getinfo, error, rawset, rawget 10 | 11 | local mt = getmetatable(_G) 12 | if mt == nil then 13 | mt = {} 14 | setmetatable(_G, mt) 15 | end 16 | 17 | mt.__declared = {} 18 | 19 | local function what () 20 | local d = getinfo(3, "S") 21 | return d and d.what or "C" 22 | end 23 | 24 | mt.__newindex = function (t, n, v) 25 | if not mt.__declared[n] then 26 | local w = what() 27 | if w ~= "main" and w ~= "C" then 28 | error("assign to undeclared variable '"..n.."'", 2) 29 | end 30 | mt.__declared[n] = true 31 | end 32 | rawset(t, n, v) 33 | end 34 | 35 | mt.__index = function (t, n) 36 | if not mt.__declared[n] and what() ~= "C" then 37 | error("variable '"..n.."' is not declared", 2) 38 | end 39 | return rawget(t, n) 40 | end 41 | 42 | -------------------------------------------------------------------------------- /etc/README: -------------------------------------------------------------------------------- 1 | This directory contains some useful files and code. 2 | Unlike the code in ../src, everything here is in the public domain. 3 | 4 | If any of the makes fail, you're probably not using the same libraries 5 | used to build Lua. Set MYLIBS in Makefile accordingly. 6 | 7 | all.c 8 | Full Lua interpreter in a single file. 9 | Do "make one" for a demo. 10 | 11 | lua.hpp 12 | Lua header files for C++ using 'extern "C"'. 13 | 14 | lua.ico 15 | A Lua icon for Windows (and web sites: save as favicon.ico). 16 | Drawn by hand by Markus Gritsch . 17 | 18 | lua.pc 19 | pkg-config data for Lua 20 | 21 | luavs.bat 22 | Script to build Lua under "Visual Studio .NET Command Prompt". 23 | Run it from the toplevel as etc\luavs.bat. 24 | 25 | min.c 26 | A minimal Lua interpreter. 27 | Good for learning and for starting your own. 28 | Do "make min" for a demo. 29 | 30 | noparser.c 31 | Linking with noparser.o avoids loading the parsing modules in lualib.a. 32 | Do "make noparser" for a demo. 33 | 34 | strict.lua 35 | Traps uses of undeclared global variables. 36 | Do "make strict" for a demo. 37 | 38 | -------------------------------------------------------------------------------- /llvm-lua/tests/coroutine.lua: -------------------------------------------------------------------------------- 1 | -- coroutine tests 2 | 3 | local f 4 | 5 | assert(coroutine.running() == nil) 6 | 7 | 8 | -- tests for global environment 9 | 10 | local function foo (a) 11 | setfenv(0, a) 12 | print('yield') 13 | coroutine.yield(getfenv()) 14 | print('getfenv(0)') 15 | assert(getfenv(0) == a) 16 | print('getfenv(1)') 17 | assert(getfenv(1) == _G) 18 | print('getfenv(loadstring)') 19 | ---------------------------------------------------------- 20 | -- NOTE: The call to 'loadstring' from a coroutine caused 21 | -- a stack overflow when the LLVM JIT tried to codegen 22 | -- the Lua code (in this case an empty function). 23 | -- 24 | -- For now we will disable the JIT compiler from running 25 | -- on a coroutine. 26 | -- TODO: Switch to a separate large cstack when needed. 27 | ---------------------------------------------------------- 28 | assert(getfenv(loadstring"") == a) 29 | print('getfenv()') 30 | return getfenv() 31 | end 32 | 33 | f = coroutine.wrap(foo) 34 | local a = {print = print, tostring=tostring} 35 | assert(f(a) == _G) 36 | local a,b = pcall(f) 37 | print(a,b) 38 | assert(a and b == _G) 39 | 40 | -------------------------------------------------------------------------------- /llvm-lua/tests/test_math.lua: -------------------------------------------------------------------------------- 1 | 2 | local sqrt=math.sqrt 3 | local a 4 | 5 | for n=1,1000000 do 6 | --for n=1,10000 do 7 | a=sqrt(9) 8 | a=sqrt(9) 9 | a=sqrt(9) 10 | a=sqrt(9) 11 | a=sqrt(9) 12 | a=sqrt(9) 13 | a=sqrt(9) 14 | a=sqrt(9) 15 | a=sqrt(9) 16 | a=sqrt(9) 17 | a=sqrt(9) 18 | a=sqrt(9) 19 | a=sqrt(9) 20 | a=sqrt(9) 21 | a=sqrt(9) 22 | a=sqrt(9) 23 | a=sqrt(9) 24 | a=sqrt(9) 25 | a=sqrt(9) 26 | a=sqrt(9) 27 | a=sqrt(9) 28 | a=sqrt(9) 29 | a=sqrt(9) 30 | a=sqrt(9) 31 | a=sqrt(9) 32 | a=sqrt(9) 33 | a=sqrt(9) 34 | a=sqrt(9) 35 | a=sqrt(9) 36 | a=sqrt(9) 37 | a=sqrt(9) 38 | a=sqrt(9) 39 | a=sqrt(9) 40 | a=sqrt(9) 41 | a=sqrt(9) 42 | a=sqrt(9) 43 | a=sqrt(9) 44 | a=sqrt(9) 45 | a=sqrt(9) 46 | a=sqrt(9) 47 | a=sqrt(9) 48 | a=sqrt(9) 49 | a=sqrt(9) 50 | a=sqrt(9) 51 | a=sqrt(9) 52 | a=sqrt(9) 53 | a=sqrt(9) 54 | a=sqrt(9) 55 | a=sqrt(9) 56 | a=sqrt(9) 57 | end 58 | 59 | print("a=",a) 60 | print("pow(3,3)=", math.pow(3,2)) 61 | print("abs(-3.6)=", math.abs(-3.6)) 62 | 63 | -- test tailcall of math.* function 64 | local function floor(num) 65 | return math.floor(num) 66 | end 67 | 68 | print("floor(1.2345)=", floor(1.2345)) 69 | 70 | -------------------------------------------------------------------------------- /etc/luavs.bat: -------------------------------------------------------------------------------- 1 | @rem Script to build Lua under "Visual Studio .NET Command Prompt". 2 | @rem Do not run from this directory; run it from the toplevel: etc\luavs.bat . 3 | @rem It creates lua51.dll, lua51.lib, lua.exe, and luac.exe in src. 4 | @rem (contributed by David Manura and Mike Pall) 5 | 6 | @setlocal 7 | @set MYCOMPILE=cl /nologo /MD /O2 /W3 /c /D_CRT_SECURE_NO_DEPRECATE 8 | @set MYLINK=link /nologo 9 | @set MYMT=mt /nologo 10 | 11 | cd src 12 | %MYCOMPILE% /DLUA_BUILD_AS_DLL l*.c 13 | del lua.obj luac.obj 14 | %MYLINK% /DLL /out:lua51.dll l*.obj 15 | if exist lua51.dll.manifest^ 16 | %MYMT% -manifest lua51.dll.manifest -outputresource:lua51.dll;2 17 | %MYCOMPILE% /DLUA_BUILD_AS_DLL lua.c 18 | %MYLINK% /out:lua.exe lua.obj lua51.lib 19 | if exist lua.exe.manifest^ 20 | %MYMT% -manifest lua.exe.manifest -outputresource:lua.exe 21 | %MYCOMPILE% l*.c print.c 22 | del lua.obj linit.obj lbaselib.obj ldblib.obj liolib.obj lmathlib.obj^ 23 | loslib.obj ltablib.obj lstrlib.obj loadlib.obj 24 | %MYLINK% /out:luac.exe *.obj 25 | if exist luac.exe.manifest^ 26 | %MYMT% -manifest luac.exe.manifest -outputresource:luac.exe 27 | del *.obj *.manifest 28 | cd .. 29 | -------------------------------------------------------------------------------- /src/ldebug.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ldebug.h,v 2.3.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** Auxiliary functions from Debug Interface module 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef ldebug_h 8 | #define ldebug_h 9 | 10 | 11 | #include "lstate.h" 12 | 13 | 14 | #define pcRel(pc, p) (cast(int, (pc) - (p)->code) - 1) 15 | 16 | #define getlinenum(f,pc) (((f)->lineinfo) ? (f)->lineinfo[pc] : 0) 17 | 18 | #define resethookcount(L) (L->hookcount = L->basehookcount) 19 | 20 | 21 | LUAI_FUNC void luaG_typeerror (lua_State *L, const TValue *o, 22 | const char *opname); 23 | LUAI_FUNC void luaG_concaterror (lua_State *L, StkId p1, StkId p2); 24 | LUAI_FUNC void luaG_aritherror (lua_State *L, const TValue *p1, 25 | const TValue *p2); 26 | LUAI_FUNC int luaG_ordererror (lua_State *L, const TValue *p1, 27 | const TValue *p2); 28 | LUAI_FUNC void luaG_runerror (lua_State *L, const char *fmt, ...); 29 | LUAI_FUNC void luaG_errormsg (lua_State *L); 30 | LUAI_FUNC int luaG_checkcode (const Proto *pt); 31 | LUAI_FUNC int luaG_checkopenop (Instruction i); 32 | 33 | #endif 34 | -------------------------------------------------------------------------------- /llvm-lua/COPYRIGHT.llvm-lua: -------------------------------------------------------------------------------- 1 | Copyright (c) 2008 Robert G. Jakabosky 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is 8 | furnished to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in 11 | all copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | THE SOFTWARE. 20 | 21 | MIT License: http://www.opensource.org/licenses/mit-license.php 22 | 23 | -------------------------------------------------------------------------------- /src/ltm.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ltm.h,v 2.6.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** Tag methods 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef ltm_h 8 | #define ltm_h 9 | 10 | 11 | #include "lobject.h" 12 | 13 | 14 | /* 15 | * WARNING: if you change the order of this enumeration, 16 | * grep "ORDER TM" 17 | */ 18 | typedef enum { 19 | TM_INDEX, 20 | TM_NEWINDEX, 21 | TM_GC, 22 | TM_MODE, 23 | TM_EQ, /* last tag method with `fast' access */ 24 | TM_ADD, 25 | TM_SUB, 26 | TM_MUL, 27 | TM_DIV, 28 | TM_MOD, 29 | TM_POW, 30 | TM_UNM, 31 | TM_LEN, 32 | TM_LT, 33 | TM_LE, 34 | TM_CONCAT, 35 | TM_CALL, 36 | TM_N /* number of elements in the enum */ 37 | } TMS; 38 | 39 | 40 | 41 | #define gfasttm(g,et,e) ((et) == NULL ? NULL : \ 42 | ((et)->flags & (1u<<(e))) ? NULL : luaT_gettm(et, e, (g)->tmname[e])) 43 | 44 | #define fasttm(l,et,e) gfasttm(G(l), et, e) 45 | 46 | LUAI_DATA const char *const luaT_typenames[]; 47 | 48 | 49 | LUAI_FUNC const TValue *luaT_gettm (Table *events, TMS event, TString *ename); 50 | LUAI_FUNC const TValue *luaT_gettmbyobj (lua_State *L, const TValue *o, 51 | TMS event); 52 | LUAI_FUNC void luaT_init (lua_State *L); 53 | 54 | #endif 55 | -------------------------------------------------------------------------------- /src/lfunc.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lfunc.h,v 2.4.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** Auxiliary functions to manipulate prototypes and closures 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lfunc_h 8 | #define lfunc_h 9 | 10 | 11 | #include "lobject.h" 12 | 13 | 14 | #define sizeCclosure(n) (cast(int, sizeof(CClosure)) + \ 15 | cast(int, sizeof(TValue)*((n)-1))) 16 | 17 | #define sizeLclosure(n) (cast(int, sizeof(LClosure)) + \ 18 | cast(int, sizeof(TValue *)*((n)-1))) 19 | 20 | 21 | LUAI_FUNC Proto *luaF_newproto (lua_State *L); 22 | LUAI_FUNC Closure *luaF_newCclosure (lua_State *L, int nelems, Table *e); 23 | LUAI_FUNC Closure *luaF_newLclosure (lua_State *L, int nelems, Table *e); 24 | LUAI_FUNC UpVal *luaF_newupval (lua_State *L); 25 | LUAI_FUNC UpVal *luaF_findupval (lua_State *L, StkId level); 26 | LUAI_FUNC void luaF_close (lua_State *L, StkId level); 27 | LUAI_FUNC void luaF_freeproto (lua_State *L, Proto *f); 28 | LUAI_FUNC void luaF_freeclosure (lua_State *L, Closure *c); 29 | LUAI_FUNC void luaF_freeupval (lua_State *L, UpVal *uv); 30 | LUAI_FUNC const char *luaF_getlocalname (const Proto *func, int local_number, 31 | int pc); 32 | 33 | 34 | #endif 35 | -------------------------------------------------------------------------------- /src/lualib.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lualib.h,v 1.36.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** Lua standard libraries 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #ifndef lualib_h 9 | #define lualib_h 10 | 11 | #include "lua.h" 12 | 13 | 14 | /* Key to file-handle type */ 15 | #define LUA_FILEHANDLE "FILE*" 16 | 17 | 18 | #define LUA_COLIBNAME "coroutine" 19 | LUALIB_API int (luaopen_base) (lua_State *L); 20 | 21 | #define LUA_TABLIBNAME "table" 22 | LUALIB_API int (luaopen_table) (lua_State *L); 23 | 24 | #define LUA_IOLIBNAME "io" 25 | LUALIB_API int (luaopen_io) (lua_State *L); 26 | 27 | #define LUA_OSLIBNAME "os" 28 | LUALIB_API int (luaopen_os) (lua_State *L); 29 | 30 | #define LUA_STRLIBNAME "string" 31 | LUALIB_API int (luaopen_string) (lua_State *L); 32 | 33 | #define LUA_MATHLIBNAME "math" 34 | LUALIB_API int (luaopen_math) (lua_State *L); 35 | 36 | #define LUA_DBLIBNAME "debug" 37 | LUALIB_API int (luaopen_debug) (lua_State *L); 38 | 39 | #define LUA_LOADLIBNAME "package" 40 | LUALIB_API int (luaopen_package) (lua_State *L); 41 | 42 | 43 | /* open all previous libraries */ 44 | LUALIB_API void (luaL_openlibs) (lua_State *L); 45 | 46 | 47 | 48 | #ifndef lua_assert 49 | #define lua_assert(x) ((void)0) 50 | #endif 51 | 52 | 53 | #endif 54 | -------------------------------------------------------------------------------- /test/README: -------------------------------------------------------------------------------- 1 | These are simple tests for Lua. Some of them contain useful code. 2 | They are meant to be run to make sure Lua is built correctly and also 3 | to be read, to see how Lua programs look. 4 | 5 | Here is a one-line summary of each program: 6 | 7 | bisect.lua bisection method for solving non-linear equations 8 | cf.lua temperature conversion table (celsius to farenheit) 9 | echo.lua echo command line arguments 10 | env.lua environment variables as automatic global variables 11 | factorial.lua factorial without recursion 12 | fib.lua fibonacci function with cache 13 | fibfor.lua fibonacci numbers with coroutines and generators 14 | globals.lua report global variable usage 15 | hello.lua the first program in every language 16 | life.lua Conway's Game of Life 17 | luac.lua bare-bones luac 18 | printf.lua an implementation of printf 19 | readonly.lua make global variables readonly 20 | sieve.lua the sieve of of Eratosthenes programmed with coroutines 21 | sort.lua two implementations of a sort function 22 | table.lua make table, grouping all data for the same item 23 | trace-calls.lua trace calls 24 | trace-globals.lua trace assigments to global variables 25 | xd.lua hex dump 26 | 27 | -------------------------------------------------------------------------------- /src/ltable.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ltable.h,v 2.10.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** Lua tables (hash) 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef ltable_h 8 | #define ltable_h 9 | 10 | #include "lobject.h" 11 | 12 | 13 | #define gnode(t,i) (&(t)->node[i]) 14 | #define gkey(n) (&(n)->i_key.nk) 15 | #define gval(n) (&(n)->i_val) 16 | #define gnext(n) ((n)->i_key.nk.next) 17 | 18 | #define key2tval(n) (&(n)->i_key.tvk) 19 | 20 | 21 | LUAI_FUNC const TValue *luaH_getnum (Table *t, int key); 22 | LUAI_FUNC TValue *luaH_setnum (lua_State *L, Table *t, int key); 23 | LUAI_FUNC const TValue *luaH_getstr (Table *t, TString *key); 24 | LUAI_FUNC TValue *luaH_setstr (lua_State *L, Table *t, TString *key); 25 | LUAI_FUNC const TValue *luaH_get (Table *t, const TValue *key); 26 | LUAI_FUNC TValue *luaH_set (lua_State *L, Table *t, const TValue *key); 27 | LUAI_FUNC Table *luaH_new (lua_State *L, int narray, int lnhash); 28 | LUAI_FUNC void luaH_resizearray (lua_State *L, Table *t, int nasize); 29 | LUAI_FUNC void luaH_free (lua_State *L, Table *t); 30 | LUAI_FUNC int luaH_next (lua_State *L, Table *t, StkId key); 31 | LUAI_FUNC int luaH_getn (Table *t); 32 | 33 | 34 | #if defined(LUA_DEBUG) 35 | LUAI_FUNC Node *luaH_mainposition (const Table *t, const TValue *key); 36 | LUAI_FUNC int luaH_isdummy (Node *n); 37 | #endif 38 | 39 | 40 | #endif 41 | -------------------------------------------------------------------------------- /llvm-lua/load_vm_ops.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2009 Robert G. Jakabosky 3 | 4 | Permission is hereby granted, free of charge, to any person obtaining a copy 5 | of this software and associated documentation files (the "Software"), to deal 6 | in the Software without restriction, including without limitation the rights 7 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | copies of the Software, and to permit persons to whom the Software is 9 | furnished to do so, subject to the following conditions: 10 | 11 | The above copyright notice and this permission notice shall be included in 12 | all copies or substantial portions of the Software. 13 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 | THE SOFTWARE. 21 | 22 | MIT License: http://www.opensource.org/licenses/mit-license.php 23 | */ 24 | 25 | #ifndef load_vm_ops_h 26 | #define load_vm_ops_h 27 | 28 | extern llvm::Module *load_vm_ops(llvm::LLVMContext &context, bool NoLazyCompilation); 29 | 30 | #endif 31 | 32 | -------------------------------------------------------------------------------- /llvm-lua/load_liblua_main.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2009 Robert G. Jakabosky 3 | 4 | Permission is hereby granted, free of charge, to any person obtaining a copy 5 | of this software and associated documentation files (the "Software"), to deal 6 | in the Software without restriction, including without limitation the rights 7 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | copies of the Software, and to permit persons to whom the Software is 9 | furnished to do so, subject to the following conditions: 10 | 11 | The above copyright notice and this permission notice shall be included in 12 | all copies or substantial portions of the Software. 13 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 | THE SOFTWARE. 21 | 22 | MIT License: http://www.opensource.org/licenses/mit-license.php 23 | */ 24 | 25 | #ifndef load_liblua_main_h 26 | #define load_liblua_main_h 27 | 28 | extern llvm::Module *load_liblua_main(llvm::LLVMContext &context, bool NoLazyCompilation); 29 | 30 | #endif 31 | 32 | -------------------------------------------------------------------------------- /etc/noparser.c: -------------------------------------------------------------------------------- 1 | /* 2 | * The code below can be used to make a Lua core that does not contain the 3 | * parsing modules (lcode, llex, lparser), which represent 35% of the total core. 4 | * You'll only be able to load binary files and strings, precompiled with luac. 5 | * (Of course, you'll have to build luac with the original parsing modules!) 6 | * 7 | * To use this module, simply compile it ("make noparser" does that) and list 8 | * its object file before the Lua libraries. The linker should then not load 9 | * the parsing modules. To try it, do "make luab". 10 | * 11 | * If you also want to avoid the dump module (ldump.o), define NODUMP. 12 | * #define NODUMP 13 | */ 14 | 15 | #define LUA_CORE 16 | 17 | #include "llex.h" 18 | #include "lparser.h" 19 | #include "lzio.h" 20 | 21 | LUAI_FUNC void luaX_init (lua_State *L) { 22 | UNUSED(L); 23 | } 24 | 25 | LUAI_FUNC Proto *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff, const char *name) { 26 | UNUSED(z); 27 | UNUSED(buff); 28 | UNUSED(name); 29 | lua_pushliteral(L,"parser not loaded"); 30 | lua_error(L); 31 | return NULL; 32 | } 33 | 34 | #ifdef NODUMP 35 | #include "lundump.h" 36 | 37 | LUAI_FUNC int luaU_dump (lua_State* L, const Proto* f, lua_Writer w, void* data, int strip) { 38 | UNUSED(f); 39 | UNUSED(w); 40 | UNUSED(data); 41 | UNUSED(strip); 42 | #if 1 43 | UNUSED(L); 44 | return 0; 45 | #else 46 | lua_pushliteral(L,"dumper not loaded"); 47 | lua_error(L); 48 | #endif 49 | } 50 | #endif 51 | -------------------------------------------------------------------------------- /README: -------------------------------------------------------------------------------- 1 | README for Lua 5.1 2 | 3 | See INSTALL for installation instructions. 4 | See HISTORY for a summary of changes since the last released version. 5 | 6 | * What is Lua? 7 | ------------ 8 | Lua is a powerful, light-weight programming language designed for extending 9 | applications. Lua is also frequently used as a general-purpose, stand-alone 10 | language. Lua is free software. 11 | 12 | For complete information, visit Lua's web site at http://www.lua.org/ . 13 | For an executive summary, see http://www.lua.org/about.html . 14 | 15 | Lua has been used in many different projects around the world. 16 | For a short list, see http://www.lua.org/uses.html . 17 | 18 | * Availability 19 | ------------ 20 | Lua is freely available for both academic and commercial purposes. 21 | See COPYRIGHT and http://www.lua.org/license.html for details. 22 | Lua can be downloaded at http://www.lua.org/download.html . 23 | 24 | * Installation 25 | ------------ 26 | Lua is implemented in pure ANSI C, and compiles unmodified in all known 27 | platforms that have an ANSI C compiler. In most Unix-like platforms, simply 28 | do "make" with a suitable target. See INSTALL for detailed instructions. 29 | 30 | * Origin 31 | ------ 32 | Lua is developed at Lua.org, a laboratory of the Department of Computer 33 | Science of PUC-Rio (the Pontifical Catholic University of Rio de Janeiro 34 | in Brazil). 35 | For more information about the authors, see http://www.lua.org/authors.html . 36 | 37 | (end of README) 38 | -------------------------------------------------------------------------------- /llvm-lua/load_embedded_bc.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2009 Robert G. Jakabosky 3 | 4 | Permission is hereby granted, free of charge, to any person obtaining a copy 5 | of this software and associated documentation files (the "Software"), to deal 6 | in the Software without restriction, including without limitation the rights 7 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | copies of the Software, and to permit persons to whom the Software is 9 | furnished to do so, subject to the following conditions: 10 | 11 | The above copyright notice and this permission notice shall be included in 12 | all copies or substantial portions of the Software. 13 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 | THE SOFTWARE. 21 | 22 | MIT License: http://www.opensource.org/licenses/mit-license.php 23 | */ 24 | 25 | #ifndef load_embedded_bc_h 26 | #define load_embedded_bc_h 27 | 28 | extern llvm::Module *load_embedded_bc(llvm::LLVMContext &context, 29 | const char *name, const unsigned char *start, size_t len, bool NoLazyCompilation); 30 | 31 | #endif 32 | 33 | -------------------------------------------------------------------------------- /llvm-lua/llvm_compiler_private.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2009 Robert G. Jakabosky 3 | 4 | Permission is hereby granted, free of charge, to any person obtaining a copy 5 | of this software and associated documentation files (the "Software"), to deal 6 | in the Software without restriction, including without limitation the rights 7 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | copies of the Software, and to permit persons to whom the Software is 9 | furnished to do so, subject to the following conditions: 10 | 11 | The above copyright notice and this permission notice shall be included in 12 | all copies or substantial portions of the Software. 13 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 | THE SOFTWARE. 21 | 22 | MIT License: http://www.opensource.org/licenses/mit-license.php 23 | */ 24 | 25 | #ifndef llvm_compiler_private_h 26 | #define llvm_compiler_private_h 27 | 28 | #ifdef __cplusplus 29 | extern "C" { 30 | #endif 31 | 32 | LLVMCompiler *llvm_get_compiler(lua_State *L); 33 | 34 | #ifdef __cplusplus 35 | } 36 | #endif 37 | 38 | #endif 39 | 40 | -------------------------------------------------------------------------------- /llvm-lua/llvm_dumper.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2009 Robert G. Jakabosky 3 | 4 | Permission is hereby granted, free of charge, to any person obtaining a copy 5 | of this software and associated documentation files (the "Software"), to deal 6 | in the Software without restriction, including without limitation the rights 7 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | copies of the Software, and to permit persons to whom the Software is 9 | furnished to do so, subject to the following conditions: 10 | 11 | The above copyright notice and this permission notice shall be included in 12 | all copies or substantial portions of the Software. 13 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 | THE SOFTWARE. 21 | 22 | MIT License: http://www.opensource.org/licenses/mit-license.php 23 | */ 24 | 25 | #ifndef llvm_dumper_h 26 | #define llvm_dumper_h 27 | 28 | #include "lua_core.h" 29 | 30 | #ifdef __cplusplus 31 | extern "C" { 32 | #endif 33 | 34 | #include "lobject.h" 35 | 36 | void llvm_dumper_dump(const char *output, lua_State *L, Proto *p, int stripping); 37 | 38 | #ifdef __cplusplus 39 | } 40 | #endif 41 | 42 | #endif 43 | 44 | -------------------------------------------------------------------------------- /COPYRIGHT: -------------------------------------------------------------------------------- 1 | Lua License 2 | ----------- 3 | 4 | Lua is licensed under the terms of the MIT license reproduced below. 5 | This means that Lua is free software and can be used for both academic 6 | and commercial purposes at absolutely no cost. 7 | 8 | For details and rationale, see http://www.lua.org/license.html . 9 | 10 | =============================================================================== 11 | 12 | Copyright (C) 1994-2008 Lua.org, PUC-Rio. 13 | 14 | Permission is hereby granted, free of charge, to any person obtaining a copy 15 | of this software and associated documentation files (the "Software"), to deal 16 | in the Software without restriction, including without limitation the rights 17 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 18 | copies of the Software, and to permit persons to whom the Software is 19 | furnished to do so, subject to the following conditions: 20 | 21 | The above copyright notice and this permission notice shall be included in 22 | all copies or substantial portions of the Software. 23 | 24 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 25 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 26 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 27 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 28 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 29 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 30 | THE SOFTWARE. 31 | 32 | =============================================================================== 33 | 34 | (end of COPYRIGHT) 35 | -------------------------------------------------------------------------------- /llvm-lua/load_vm_ops.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2009 Robert G. Jakabosky 3 | 4 | Permission is hereby granted, free of charge, to any person obtaining a copy 5 | of this software and associated documentation files (the "Software"), to deal 6 | in the Software without restriction, including without limitation the rights 7 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | copies of the Software, and to permit persons to whom the Software is 9 | furnished to do so, subject to the following conditions: 10 | 11 | The above copyright notice and this permission notice shall be included in 12 | all copies or substantial portions of the Software. 13 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 | THE SOFTWARE. 21 | 22 | MIT License: http://www.opensource.org/licenses/mit-license.php 23 | */ 24 | 25 | #include 26 | 27 | #include "llvm/LLVMContext.h" 28 | #include "llvm/Module.h" 29 | 30 | #include "load_vm_ops.h" 31 | #include "load_embedded_bc.h" 32 | 33 | #include "lua_vm_ops_bc.h" 34 | 35 | llvm::Module *load_vm_ops(llvm::LLVMContext &context, bool NoLazyCompilation) { 36 | return load_embedded_bc(context, "lua_vm_ops_bc", lua_vm_ops_bc, 37 | sizeof(lua_vm_ops_bc), NoLazyCompilation); 38 | } 39 | 40 | -------------------------------------------------------------------------------- /src/lmem.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lmem.h,v 1.31.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** Interface to Memory Manager 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lmem_h 8 | #define lmem_h 9 | 10 | 11 | #include 12 | 13 | #include "llimits.h" 14 | #include "lua.h" 15 | 16 | #define MEMERRMSG "not enough memory" 17 | 18 | 19 | #define luaM_reallocv(L,b,on,n,e) \ 20 | ((cast(size_t, (n)+1) <= MAX_SIZET/(e)) ? /* +1 to avoid warnings */ \ 21 | luaM_realloc_(L, (b), (on)*(e), (n)*(e)) : \ 22 | luaM_toobig(L)) 23 | 24 | #define luaM_freemem(L, b, s) luaM_realloc_(L, (b), (s), 0) 25 | #define luaM_free(L, b) luaM_realloc_(L, (b), sizeof(*(b)), 0) 26 | #define luaM_freearray(L, b, n, t) luaM_reallocv(L, (b), n, 0, sizeof(t)) 27 | 28 | #define luaM_malloc(L,t) luaM_realloc_(L, NULL, 0, (t)) 29 | #define luaM_new(L,t) cast(t *, luaM_malloc(L, sizeof(t))) 30 | #define luaM_newvector(L,n,t) \ 31 | cast(t *, luaM_reallocv(L, NULL, 0, n, sizeof(t))) 32 | 33 | #define luaM_growvector(L,v,nelems,size,t,limit,e) \ 34 | if ((nelems)+1 > (size)) \ 35 | ((v)=cast(t *, luaM_growaux_(L,v,&(size),sizeof(t),limit,e))) 36 | 37 | #define luaM_reallocvector(L, v,oldn,n,t) \ 38 | ((v)=cast(t *, luaM_reallocv(L, v, oldn, n, sizeof(t)))) 39 | 40 | 41 | LUAI_FUNC void *luaM_realloc_ (lua_State *L, void *block, size_t oldsize, 42 | size_t size); 43 | LUAI_FUNC void *luaM_toobig (lua_State *L); 44 | LUAI_FUNC void *luaM_growaux_ (lua_State *L, void *block, int *size, 45 | size_t size_elem, int limit, 46 | const char *errormsg); 47 | 48 | #endif 49 | 50 | -------------------------------------------------------------------------------- /llvm-lua/load_liblua_main.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2009 Robert G. Jakabosky 3 | 4 | Permission is hereby granted, free of charge, to any person obtaining a copy 5 | of this software and associated documentation files (the "Software"), to deal 6 | in the Software without restriction, including without limitation the rights 7 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | copies of the Software, and to permit persons to whom the Software is 9 | furnished to do so, subject to the following conditions: 10 | 11 | The above copyright notice and this permission notice shall be included in 12 | all copies or substantial portions of the Software. 13 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 | THE SOFTWARE. 21 | 22 | MIT License: http://www.opensource.org/licenses/mit-license.php 23 | */ 24 | 25 | #include 26 | 27 | #include "llvm/Module.h" 28 | #include "llvm/LLVMContext.h" 29 | 30 | #include "load_liblua_main.h" 31 | #include "load_embedded_bc.h" 32 | 33 | #include "liblua_main_bc.h" 34 | 35 | llvm::Module *load_liblua_main(llvm::LLVMContext &context, bool NoLazyCompilation) { 36 | return load_embedded_bc(context, "liblua_main_bc", liblua_main_bc, 37 | sizeof(liblua_main_bc), NoLazyCompilation); 38 | } 39 | 40 | -------------------------------------------------------------------------------- /src/lvm.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lvm.h,v 2.5.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** Lua virtual machine 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lvm_h 8 | #define lvm_h 9 | 10 | 11 | #include "ldo.h" 12 | #include "lobject.h" 13 | #include "ltm.h" 14 | 15 | 16 | #define tostring(L,o) ((ttype(o) == LUA_TSTRING) || (luaV_tostring(L, o))) 17 | 18 | #define tonumber(o,n) (ttype(o) == LUA_TNUMBER || \ 19 | (((o) = luaV_tonumber(o,n)) != NULL)) 20 | 21 | #define equalobj(L,o1,o2) \ 22 | (ttype(o1) == ttype(o2) && luaV_equalval(L, o1, o2)) 23 | 24 | 25 | LUAI_FUNC void luaV_traceexec (lua_State *L, const Instruction *pc); 26 | LUAI_FUNC int luaV_call_binTM (lua_State *L, const TValue *p1, const TValue *p2, 27 | StkId res, TMS event); 28 | LUAI_FUNC int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r); 29 | LUAI_FUNC int luaV_lessequal (lua_State *L, const TValue *l, const TValue *r); 30 | LUAI_FUNC int luaV_equalval (lua_State *L, const TValue *t1, const TValue *t2); 31 | LUAI_FUNC const TValue *luaV_tonumber (const TValue *obj, TValue *n); 32 | LUAI_FUNC int luaV_tostring (lua_State *L, StkId obj); 33 | LUAI_FUNC void luaV_gettable (lua_State *L, const TValue *t, TValue *key, 34 | StkId val); 35 | LUAI_FUNC void luaV_settable (lua_State *L, const TValue *t, TValue *key, 36 | StkId val); 37 | LUAI_FUNC void luaV_arith (lua_State *L, StkId ra, const TValue *rb, 38 | const TValue *rc, TMS op); 39 | LUAI_FUNC void luaV_execute (lua_State *L, int nexeccalls); 40 | LUAI_FUNC void luaV_concat (lua_State *L, int total, int last); 41 | 42 | #endif 43 | -------------------------------------------------------------------------------- /llvm-lua/llvm_dumper.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2009 Robert G. Jakabosky 3 | 4 | Permission is hereby granted, free of charge, to any person obtaining a copy 5 | of this software and associated documentation files (the "Software"), to deal 6 | in the Software without restriction, including without limitation the rights 7 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | copies of the Software, and to permit persons to whom the Software is 9 | furnished to do so, subject to the following conditions: 10 | 11 | The above copyright notice and this permission notice shall be included in 12 | all copies or substantial portions of the Software. 13 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 | THE SOFTWARE. 21 | 22 | MIT License: http://www.opensource.org/licenses/mit-license.php 23 | */ 24 | 25 | #include "LLVMCompiler.h" 26 | #include "LLVMDumper.h" 27 | #include "llvm_compiler.h" 28 | #include "llvm_compiler_private.h" 29 | #include "llvm_dumper.h" 30 | 31 | extern "C" { 32 | 33 | void llvm_dumper_dump(const char *output, lua_State *L, Proto *p, int stripping) { 34 | LLVMCompiler *compiler = llvm_get_compiler(L); 35 | LLVMDumper *dumper = new LLVMDumper(compiler); 36 | dumper->dump(output, L, p, stripping); 37 | delete dumper; 38 | } 39 | 40 | }// end: extern "C" 41 | 42 | -------------------------------------------------------------------------------- /test/sort.lua: -------------------------------------------------------------------------------- 1 | -- two implementations of a sort function 2 | -- this is an example only. Lua has now a built-in function "sort" 3 | 4 | -- extracted from Programming Pearls, page 110 5 | function qsort(x,l,u,f) 6 | if ly end) 58 | show("after reverse selection sort",x) 59 | qsort(x,1,n,function (x,y) return xn--)>0 ? char2int(*(z)->p++) : luaZ_fill(z)) 23 | 24 | typedef struct Mbuffer { 25 | char *buffer; 26 | size_t n; 27 | size_t buffsize; 28 | } Mbuffer; 29 | 30 | #define luaZ_initbuffer(L, buff) ((buff)->buffer = NULL, (buff)->n = 0, (buff)->buffsize = 0) 31 | 32 | #define luaZ_buffer(buff) ((buff)->buffer) 33 | #define luaZ_sizebuffer(buff) ((buff)->buffsize) 34 | #define luaZ_bufflen(buff) ((buff)->n) 35 | 36 | #define luaZ_resetbuffer(buff) ((buff)->n = 0) 37 | 38 | 39 | #define luaZ_resizebuffer(L, buff, size) \ 40 | (luaM_reallocvector(L, (buff)->buffer, (buff)->buffsize, size, char), \ 41 | (buff)->buffsize = size) 42 | 43 | #define luaZ_freebuffer(L, buff) luaZ_resizebuffer(L, buff, 0) 44 | 45 | 46 | LUAI_FUNC char *luaZ_openspace (lua_State *L, Mbuffer *buff, size_t n); 47 | LUAI_FUNC void luaZ_init (lua_State *L, ZIO *z, lua_Reader reader, 48 | void *data); 49 | LUAI_FUNC size_t luaZ_read (ZIO* z, void* b, size_t n); /* read next n bytes */ 50 | LUAI_FUNC int luaZ_lookahead (ZIO *z); 51 | 52 | 53 | 54 | /* --------- Private Part ------------------ */ 55 | 56 | struct Zio { 57 | size_t n; /* bytes still unread */ 58 | const char *p; /* current position in buffer */ 59 | lua_Reader reader; 60 | void* data; /* additional data */ 61 | lua_State *L; /* Lua state (for reader) */ 62 | }; 63 | 64 | 65 | LUAI_FUNC int luaZ_fill (ZIO *z); 66 | 67 | #endif 68 | -------------------------------------------------------------------------------- /src/ltm.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ltm.c,v 2.8.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** Tag methods 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #include 9 | 10 | #define ltm_c 11 | #define LUA_CORE 12 | 13 | #include "lua.h" 14 | 15 | #include "lobject.h" 16 | #include "lstate.h" 17 | #include "lstring.h" 18 | #include "ltable.h" 19 | #include "ltm.h" 20 | 21 | 22 | 23 | const char *const luaT_typenames[] = { 24 | "nil", "boolean", "userdata", "number", 25 | "string", "table", "function", "userdata", "thread", 26 | "proto", "upval" 27 | }; 28 | 29 | 30 | void luaT_init (lua_State *L) { 31 | static const char *const luaT_eventname[] = { /* ORDER TM */ 32 | "__index", "__newindex", 33 | "__gc", "__mode", "__eq", 34 | "__add", "__sub", "__mul", "__div", "__mod", 35 | "__pow", "__unm", "__len", "__lt", "__le", 36 | "__concat", "__call" 37 | }; 38 | int i; 39 | for (i=0; itmname[i] = luaS_new(L, luaT_eventname[i]); 41 | luaS_fix(G(L)->tmname[i]); /* never collect these names */ 42 | } 43 | } 44 | 45 | 46 | /* 47 | ** function to be used with macro "fasttm": optimized for absence of 48 | ** tag methods 49 | */ 50 | const TValue *luaT_gettm (Table *events, TMS event, TString *ename) { 51 | const TValue *tm = luaH_getstr(events, ename); 52 | lua_assert(event <= TM_EQ); 53 | if (ttisnil(tm)) { /* no tag method? */ 54 | events->flags |= cast_byte(1u<metatable; 66 | break; 67 | case LUA_TUSERDATA: 68 | mt = uvalue(o)->metatable; 69 | break; 70 | default: 71 | mt = G(L)->mt[ttype(o)]; 72 | } 73 | return (mt ? luaH_getstr(mt, G(L)->tmname[event]) : luaO_nilobject); 74 | } 75 | 76 | -------------------------------------------------------------------------------- /src/lzio.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lzio.c,v 1.31.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** a generic input stream interface 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #include 9 | 10 | #define lzio_c 11 | #define LUA_CORE 12 | 13 | #include "lua.h" 14 | 15 | #include "llimits.h" 16 | #include "lmem.h" 17 | #include "lstate.h" 18 | #include "lzio.h" 19 | 20 | 21 | int luaZ_fill (ZIO *z) { 22 | size_t size; 23 | lua_State *L = z->L; 24 | const char *buff; 25 | lua_unlock(L); 26 | buff = z->reader(L, z->data, &size); 27 | lua_lock(L); 28 | if (buff == NULL || size == 0) return EOZ; 29 | z->n = size - 1; 30 | z->p = buff; 31 | return char2int(*(z->p++)); 32 | } 33 | 34 | 35 | int luaZ_lookahead (ZIO *z) { 36 | if (z->n == 0) { 37 | if (luaZ_fill(z) == EOZ) 38 | return EOZ; 39 | else { 40 | z->n++; /* luaZ_fill removed first byte; put back it */ 41 | z->p--; 42 | } 43 | } 44 | return char2int(*z->p); 45 | } 46 | 47 | 48 | void luaZ_init (lua_State *L, ZIO *z, lua_Reader reader, void *data) { 49 | z->L = L; 50 | z->reader = reader; 51 | z->data = data; 52 | z->n = 0; 53 | z->p = NULL; 54 | } 55 | 56 | 57 | /* --------------------------------------------------------------- read --- */ 58 | size_t luaZ_read (ZIO *z, void *b, size_t n) { 59 | while (n) { 60 | size_t m; 61 | if (luaZ_lookahead(z) == EOZ) 62 | return n; /* return number of missing bytes */ 63 | m = (n <= z->n) ? n : z->n; /* min. between n and z->n */ 64 | memcpy(b, z->p, m); 65 | z->n -= m; 66 | z->p += m; 67 | b = (char *)b + m; 68 | n -= m; 69 | } 70 | return 0; 71 | } 72 | 73 | /* ------------------------------------------------------------------------ */ 74 | char *luaZ_openspace (lua_State *L, Mbuffer *buff, size_t n) { 75 | if (n > buff->buffsize) { 76 | if (n < LUA_MINBUFFER) n = LUA_MINBUFFER; 77 | luaZ_resizebuffer(L, buff, n); 78 | } 79 | return buff->buffer; 80 | } 81 | 82 | 83 | -------------------------------------------------------------------------------- /llvm-lua/llvm_compiler.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2009 Robert G. Jakabosky 3 | 4 | Permission is hereby granted, free of charge, to any person obtaining a copy 5 | of this software and associated documentation files (the "Software"), to deal 6 | in the Software without restriction, including without limitation the rights 7 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | copies of the Software, and to permit persons to whom the Software is 9 | furnished to do so, subject to the following conditions: 10 | 11 | The above copyright notice and this permission notice shall be included in 12 | all copies or substantial portions of the Software. 13 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 | THE SOFTWARE. 21 | 22 | MIT License: http://www.opensource.org/licenses/mit-license.php 23 | */ 24 | 25 | #ifndef llvm_compiler_h 26 | #define llvm_compiler_h 27 | 28 | #include "lua_core.h" 29 | #include "llvm_lua_config.h" 30 | 31 | #ifdef __cplusplus 32 | extern "C" { 33 | #endif 34 | 35 | #include "lobject.h" 36 | 37 | int llvm_compiler_main(int useJIT); 38 | void llvm_new_compiler(lua_State *L); 39 | void llvm_free_compiler(lua_State *L); 40 | void llvm_compiler_compile(lua_State *L, Proto *p); 41 | void llvm_compiler_compile_all(lua_State *L, Proto *p); 42 | void llvm_compiler_free(lua_State *L, Proto *p); 43 | 44 | extern int llvm_precall_jit (lua_State *L, StkId func, int nresults); 45 | extern int llvm_precall_lua (lua_State *L, StkId func, int nresults); 46 | 47 | 48 | #ifdef __cplusplus 49 | } 50 | #endif 51 | 52 | #endif 53 | 54 | -------------------------------------------------------------------------------- /llvm-lua/no_jit.c: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Copyright (c) 2009 Robert G. Jakabosky 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | 23 | MIT License: http://www.opensource.org/licenses/mit-license.php 24 | */ 25 | 26 | 27 | #ifdef __cplusplus 28 | extern "C" { 29 | #endif 30 | 31 | #include "lua_core.h" 32 | 33 | #include "lua.h" 34 | #include "lobject.h" 35 | 36 | #include "lauxlib.h" 37 | #include "lualib.h" 38 | 39 | #define ENABLE_PARSER_HOOK 1 40 | #include "hook_parser.c" 41 | 42 | #ifndef UNUSED 43 | #define UNUSED(x) ((void)x) 44 | #endif 45 | 46 | /* 47 | * link against this file to use the Lua VM core without LLVM. 48 | * This will disable JIT support, but still allow loading static compiled Lua scripts. 49 | */ 50 | void llvm_new_compiler(lua_State *L) {UNUSED(L);} 51 | void llvm_free_compiler(lua_State *L) {UNUSED(L);} 52 | void llvm_compiler_compile(lua_State *L, Proto *p) {UNUSED(L);UNUSED(p);} 53 | void llvm_compiler_compile_all(lua_State *L, Proto *p) {UNUSED(L);UNUSED(p);} 54 | void llvm_compiler_free(lua_State *L, Proto *p) {UNUSED(L);UNUSED(p);} 55 | 56 | void llvm_dumper_dump(const char *output, lua_State *L, Proto *p, int stripping) {UNUSED(L);UNUSED(p);UNUSED(output);UNUSED(stripping);} 57 | 58 | #ifdef __cplusplus 59 | } 60 | #endif 61 | 62 | -------------------------------------------------------------------------------- /src/ldo.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ldo.h,v 2.7.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** Stack and Call structure of Lua 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef ldo_h 8 | #define ldo_h 9 | 10 | 11 | #include "lobject.h" 12 | #include "lstate.h" 13 | #include "lzio.h" 14 | 15 | 16 | #define luaD_checkstack(L,n) \ 17 | if ((char *)L->stack_last - (char *)L->top <= (n)*(int)sizeof(TValue)) \ 18 | luaD_growstack(L, n); \ 19 | else condhardstacktests(luaD_reallocstack(L, L->stacksize - EXTRA_STACK - 1)); 20 | 21 | 22 | #define incr_top(L) {luaD_checkstack(L,1); L->top++;} 23 | 24 | #define savestack(L,p) ((char *)(p) - (char *)L->stack) 25 | #define restorestack(L,n) ((TValue *)((char *)L->stack + (n))) 26 | 27 | #define saveci(L,p) ((char *)(p) - (char *)L->base_ci) 28 | #define restoreci(L,n) ((CallInfo *)((char *)L->base_ci + (n))) 29 | 30 | 31 | /* results from luaD_precall */ 32 | #define PCRLUA 0 /* initiated a call to a Lua function */ 33 | #define PCRC 1 /* did a call to a C function */ 34 | #define PCRYIELD 2 /* C funtion yielded */ 35 | #define PCRTAILCALL 3 /* JIT function tail-calling into another function. */ 36 | #define PCRTAILRECUR 4 /* JIT function tail-recursive call */ 37 | 38 | 39 | /* type of protected functions, to be ran by `runprotected' */ 40 | typedef void (*Pfunc) (lua_State *L, void *ud); 41 | 42 | LUAI_FUNC int luaD_protectedparser (lua_State *L, ZIO *z, const char *name); 43 | LUAI_FUNC void luaD_callhook (lua_State *L, int event, int line); 44 | LUAI_FUNC StkId luaD_tryfuncTM (lua_State *L, StkId func); 45 | LUAI_FUNC int luaD_precall (lua_State *L, StkId func, int nresults); 46 | LUAI_FUNC int luaD_precall_lua (lua_State *L, StkId func, int nresults); 47 | LUAI_FUNC int luaD_precall_c (lua_State *L, StkId func, int nresults); 48 | LUAI_FUNC void luaD_call (lua_State *L, StkId func, int nResults); 49 | LUAI_FUNC int luaD_pcall (lua_State *L, Pfunc func, void *u, 50 | ptrdiff_t oldtop, ptrdiff_t ef); 51 | LUAI_FUNC int luaD_poscall (lua_State *L, StkId firstResult); 52 | LUAI_FUNC void luaD_reallocCI (lua_State *L, int newsize); 53 | LUAI_FUNC void luaD_reallocstack (lua_State *L, int newsize); 54 | LUAI_FUNC void luaD_growstack (lua_State *L, int n); 55 | 56 | LUAI_FUNC void luaD_throw (lua_State *L, int errcode); 57 | LUAI_FUNC int luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud); 58 | 59 | LUAI_FUNC void luaD_seterrorobj (lua_State *L, int errcode, StkId oldtop); 60 | 61 | #endif 62 | 63 | -------------------------------------------------------------------------------- /src/llex.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: llex.h,v 1.58.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** Lexical Analyzer 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef llex_h 8 | #define llex_h 9 | 10 | #include "lobject.h" 11 | #include "lzio.h" 12 | 13 | 14 | #define FIRST_RESERVED 257 15 | 16 | /* maximum length of a reserved word */ 17 | #define TOKEN_LEN (sizeof("function")/sizeof(char)) 18 | 19 | 20 | /* 21 | * WARNING: if you change the order of this enumeration, 22 | * grep "ORDER RESERVED" 23 | */ 24 | enum RESERVED { 25 | /* terminal symbols denoted by reserved words */ 26 | TK_AND = FIRST_RESERVED, TK_BREAK, 27 | TK_DO, TK_ELSE, TK_ELSEIF, TK_END, TK_FALSE, TK_FOR, TK_FUNCTION, 28 | TK_IF, TK_IN, TK_LOCAL, TK_NIL, TK_NOT, TK_OR, TK_REPEAT, 29 | TK_RETURN, TK_THEN, TK_TRUE, TK_UNTIL, TK_WHILE, 30 | /* other terminal symbols */ 31 | TK_CONCAT, TK_DOTS, TK_EQ, TK_GE, TK_LE, TK_NE, TK_NUMBER, 32 | TK_NAME, TK_STRING, TK_EOS 33 | }; 34 | 35 | /* number of reserved words */ 36 | #define NUM_RESERVED (cast(int, TK_WHILE-FIRST_RESERVED+1)) 37 | 38 | 39 | /* array with token `names' */ 40 | LUAI_DATA const char *const luaX_tokens []; 41 | 42 | 43 | typedef union { 44 | lua_Number r; 45 | TString *ts; 46 | } SemInfo; /* semantics information */ 47 | 48 | 49 | typedef struct Token { 50 | int token; 51 | SemInfo seminfo; 52 | } Token; 53 | 54 | 55 | typedef struct LexState { 56 | int current; /* current character (charint) */ 57 | int linenumber; /* input line counter */ 58 | int lastline; /* line of last token `consumed' */ 59 | Token t; /* current token */ 60 | Token lookahead; /* look ahead token */ 61 | struct FuncState *fs; /* `FuncState' is private to the parser */ 62 | struct lua_State *L; 63 | ZIO *z; /* input stream */ 64 | Mbuffer *buff; /* buffer for tokens */ 65 | TString *source; /* current source name */ 66 | char decpoint; /* locale decimal point */ 67 | } LexState; 68 | 69 | 70 | LUAI_FUNC void luaX_init (lua_State *L); 71 | LUAI_FUNC void luaX_setinput (lua_State *L, LexState *ls, ZIO *z, 72 | TString *source); 73 | LUAI_FUNC TString *luaX_newstring (LexState *ls, const char *str, size_t l); 74 | LUAI_FUNC void luaX_next (LexState *ls); 75 | LUAI_FUNC void luaX_lookahead (LexState *ls); 76 | LUAI_FUNC void luaX_lexerror (LexState *ls, const char *msg, int token); 77 | LUAI_FUNC void luaX_syntaxerror (LexState *ls, const char *s); 78 | LUAI_FUNC const char *luaX_token2str (LexState *ls, int token); 79 | 80 | 81 | #endif 82 | -------------------------------------------------------------------------------- /src/lmem.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lmem.c,v 1.70.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** Interface to Memory Manager 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #include 9 | 10 | #define lmem_c 11 | #define LUA_CORE 12 | 13 | #include "lua.h" 14 | 15 | #include "ldebug.h" 16 | #include "ldo.h" 17 | #include "lmem.h" 18 | #include "lobject.h" 19 | #include "lstate.h" 20 | 21 | 22 | 23 | /* 24 | ** About the realloc function: 25 | ** void * frealloc (void *ud, void *ptr, size_t osize, size_t nsize); 26 | ** (`osize' is the old size, `nsize' is the new size) 27 | ** 28 | ** Lua ensures that (ptr == NULL) iff (osize == 0). 29 | ** 30 | ** * frealloc(ud, NULL, 0, x) creates a new block of size `x' 31 | ** 32 | ** * frealloc(ud, p, x, 0) frees the block `p' 33 | ** (in this specific case, frealloc must return NULL). 34 | ** particularly, frealloc(ud, NULL, 0, 0) does nothing 35 | ** (which is equivalent to free(NULL) in ANSI C) 36 | ** 37 | ** frealloc returns NULL if it cannot create or reallocate the area 38 | ** (any reallocation to an equal or smaller size cannot fail!) 39 | */ 40 | 41 | 42 | 43 | #define MINSIZEARRAY 4 44 | 45 | 46 | void *luaM_growaux_ (lua_State *L, void *block, int *size, size_t size_elems, 47 | int limit, const char *errormsg) { 48 | void *newblock; 49 | int newsize; 50 | if (*size >= limit/2) { /* cannot double it? */ 51 | if (*size >= limit) /* cannot grow even a little? */ 52 | luaG_runerror(L, errormsg); 53 | newsize = limit; /* still have at least one free place */ 54 | } 55 | else { 56 | newsize = (*size)*2; 57 | if (newsize < MINSIZEARRAY) 58 | newsize = MINSIZEARRAY; /* minimum size */ 59 | } 60 | newblock = luaM_reallocv(L, block, *size, newsize, size_elems); 61 | *size = newsize; /* update only when everything else is OK */ 62 | return newblock; 63 | } 64 | 65 | 66 | void *luaM_toobig (lua_State *L) { 67 | luaG_runerror(L, "memory allocation error: block too big"); 68 | return NULL; /* to avoid warnings */ 69 | } 70 | 71 | 72 | 73 | /* 74 | ** generic allocation routine. 75 | */ 76 | void *luaM_realloc_ (lua_State *L, void *block, size_t osize, size_t nsize) { 77 | global_State *g = G(L); 78 | lua_assert((osize == 0) == (block == NULL)); 79 | block = (*g->frealloc)(g->ud, block, osize, nsize); 80 | if (block == NULL && nsize > 0) 81 | luaD_throw(L, LUA_ERRMEM); 82 | lua_assert((nsize == 0) == (block == NULL)); 83 | g->totalbytes = (g->totalbytes - osize) + nsize; 84 | return block; 85 | } 86 | 87 | -------------------------------------------------------------------------------- /llvm-lua/load_embedded_bc.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2009 Robert G. Jakabosky 3 | 4 | Permission is hereby granted, free of charge, to any person obtaining a copy 5 | of this software and associated documentation files (the "Software"), to deal 6 | in the Software without restriction, including without limitation the rights 7 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | copies of the Software, and to permit persons to whom the Software is 9 | furnished to do so, subject to the following conditions: 10 | 11 | The above copyright notice and this permission notice shall be included in 12 | all copies or substantial portions of the Software. 13 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 | THE SOFTWARE. 21 | 22 | MIT License: http://www.opensource.org/licenses/mit-license.php 23 | */ 24 | 25 | #include 26 | #include 27 | 28 | #include "llvm/Module.h" 29 | #include "llvm/LLVMContext.h" 30 | 31 | #include "llvm/Support/MemoryBuffer.h" 32 | #include "llvm/Bitcode/ReaderWriter.h" 33 | 34 | #include 35 | 36 | #include "load_embedded_bc.h" 37 | 38 | llvm::Module *load_embedded_bc(llvm::LLVMContext &context, 39 | const char *name, const unsigned char *start, size_t len, bool NoLazyCompilation) 40 | { 41 | llvm::Module *module = NULL; 42 | llvm::StringRef mem_ref((const char *)start, len - 1); 43 | std::string error; 44 | 45 | // Load in the bitcode file containing the functions for each 46 | // bytecode operation. 47 | 48 | llvm::MemoryBuffer* buffer; 49 | buffer= llvm::MemoryBuffer::getMemBuffer(mem_ref, name); 50 | if(buffer != NULL) { 51 | module = llvm::getLazyBitcodeModule(buffer, context, &error); 52 | if(!module) { 53 | delete buffer; 54 | } 55 | } 56 | if(!module) { 57 | printf("Failed to parse embedded '%s' file: %s\n", name, error.c_str()); 58 | exit(1); 59 | } 60 | // Materialize module 61 | if(NoLazyCompilation) { 62 | if(module->MaterializeAll(&error)) { 63 | printf("Failed to materialize embedded '%s' file: %s\n", name, error.c_str()); 64 | exit(1); 65 | } 66 | } 67 | 68 | return module; 69 | } 70 | 71 | -------------------------------------------------------------------------------- /src/lparser.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lparser.h,v 1.57.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** Lua Parser 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lparser_h 8 | #define lparser_h 9 | 10 | #include "llimits.h" 11 | #include "lobject.h" 12 | #include "lzio.h" 13 | 14 | 15 | /* 16 | ** Expression descriptor 17 | */ 18 | 19 | typedef enum { 20 | VVOID, /* no value */ 21 | VNIL, 22 | VTRUE, 23 | VFALSE, 24 | VK, /* info = index of constant in `k' */ 25 | VKNUM, /* nval = numerical value */ 26 | VLOCAL, /* info = local register */ 27 | VUPVAL, /* info = index of upvalue in `upvalues' */ 28 | VGLOBAL, /* info = index of table; aux = index of global name in `k' */ 29 | VINDEXED, /* info = table register; aux = index register (or `k') */ 30 | VJMP, /* info = instruction pc */ 31 | VRELOCABLE, /* info = instruction pc */ 32 | VNONRELOC, /* info = result register */ 33 | VCALL, /* info = instruction pc */ 34 | VVARARG /* info = instruction pc */ 35 | } expkind; 36 | 37 | typedef struct expdesc { 38 | expkind k; 39 | union { 40 | struct { int info, aux; } s; 41 | lua_Number nval; 42 | } u; 43 | int t; /* patch list of `exit when true' */ 44 | int f; /* patch list of `exit when false' */ 45 | } expdesc; 46 | 47 | 48 | typedef struct upvaldesc { 49 | lu_byte k; 50 | lu_byte info; 51 | } upvaldesc; 52 | 53 | 54 | struct BlockCnt; /* defined in lparser.c */ 55 | 56 | 57 | /* state needed to generate code for a given function */ 58 | typedef struct FuncState { 59 | Proto *f; /* current function header */ 60 | Table *h; /* table to find (and reuse) elements in `k' */ 61 | struct FuncState *prev; /* enclosing function */ 62 | struct LexState *ls; /* lexical state */ 63 | struct lua_State *L; /* copy of the Lua state */ 64 | struct BlockCnt *bl; /* chain of current blocks */ 65 | int pc; /* next position to code (equivalent to `ncode') */ 66 | int lasttarget; /* `pc' of last `jump target' */ 67 | int jpc; /* list of pending jumps to `pc' */ 68 | int freereg; /* first free register */ 69 | int nk; /* number of elements in `k' */ 70 | int np; /* number of elements in `p' */ 71 | short nlocvars; /* number of elements in `locvars' */ 72 | lu_byte nactvar; /* number of active local variables */ 73 | upvaldesc upvalues[LUAI_MAXUPVALUES]; /* upvalues */ 74 | unsigned short actvar[LUAI_MAXVARS]; /* declared-variable stack */ 75 | } FuncState; 76 | 77 | 78 | LUAI_FUNC Proto *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff, 79 | const char *name); 80 | 81 | 82 | #endif 83 | -------------------------------------------------------------------------------- /src/lcoco.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** Lua/Coco glue. 3 | ** Copyright (C) 2004-2012 Mike Pall. See copyright notice in lcoco.c 4 | */ 5 | 6 | #ifndef lcoco_h 7 | #define lcoco_h 8 | 9 | #define LUACOCO_VERSION "Coco 1.1.7" 10 | #define LUACOCO_VERSION_NUM 10107 11 | 12 | /* Exported C API to add a C stack to a coroutine. */ 13 | LUA_API lua_State *lua_newcthread(lua_State *L, int cstacksize); 14 | 15 | /* Internal support routines. */ 16 | LUAI_FUNC void luaCOCO_free(lua_State *L); 17 | LUAI_FUNC int luaCOCO_resume(lua_State *L, int nargs); 18 | LUAI_FUNC int luaCOCO_yield(lua_State *L); 19 | LUAI_FUNC int luaCOCO_cstacksize(int cstacksize); 20 | LUAI_FUNC int luaCOCO_mainthread(lua_State *L); 21 | 22 | /* Forward declaration. */ 23 | typedef struct coco_State coco_State; 24 | 25 | /* These are redefined below. */ 26 | #undef LUAI_EXTRASPACE 27 | #undef luai_userstateopen 28 | /* luai_userstateclose unused */ 29 | #undef luai_userstatethread 30 | #undef luai_userstatefree 31 | #undef luai_userstateresume 32 | #undef luai_userstateyield 33 | 34 | /* Use Windows Fibers (Win98+). */ 35 | #if defined(_WIN32) 36 | 37 | /* Fibers allocate their own stack. The whole Coco state is in front of L. */ 38 | struct coco_State { 39 | void *fib; /* Own fiber (if any). */ 40 | void *back; /* Fiber to switch back to. */ 41 | int nargs; /* Number of arguments to pass. */ 42 | int dummy_align; 43 | }; 44 | 45 | #define L2COCO(L) (&((coco_State *)(L))[-1]) 46 | #define LHASCOCO(L) (L2COCO(L)->fib) 47 | #define LUAI_EXTRASPACE sizeof(coco_State) 48 | #define luai_userstateopen(L) L2COCO(L)->fib = NULL 49 | #define luai_userstatethread(L,L1) L2COCO(L1)->fib = NULL 50 | #define COCO_USE_FIBERS 51 | 52 | #else /* !defined(_WIN32) */ 53 | 54 | #include "llimits.h" 55 | 56 | /* The Coco state depends on the context switch method used. See lcoco.c. */ 57 | /* It's stored at the end of the stack. Only need a pointer in front of L. */ 58 | #define L2COCO(L) (((coco_State **)(L))[-1]) 59 | #define LHASCOCO(L) (L2COCO(L)) 60 | /* This wastes some space on 32 bit systems, but gets better alignment. */ 61 | #define LUAI_EXTRASPACE sizeof(L_Umaxalign) 62 | #define luai_userstateopen(L) L2COCO(L) = NULL 63 | #define luai_userstatethread(L,L1) L2COCO(L1) = NULL 64 | 65 | #endif /* !defined(_WIN32) */ 66 | 67 | #define luai_userstatefree(L) if (LHASCOCO(L)) luaCOCO_free(L) 68 | #define luai_userstateresume(L, nargs) \ 69 | if (LHASCOCO(L)) return luaCOCO_resume(L, nargs) 70 | #define luai_userstateyield(L, nresults) \ 71 | do { if (LHASCOCO(L)) { \ 72 | L->base = L->top - (nresults); /* Protect stack slots below. */ \ 73 | return luaCOCO_yield(L); } } while (0) 74 | 75 | #endif 76 | -------------------------------------------------------------------------------- /llvm-lua/lua_core.c: -------------------------------------------------------------------------------- 1 | /* 2 | lua_core.c -- Lua core, libraries and JIT hooks compiled into a single file 3 | 4 | Copyright (c) 2009 Robert G. Jakabosky 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in 14 | all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | THE SOFTWARE. 23 | 24 | MIT License: http://www.opensource.org/licenses/mit-license.php 25 | */ 26 | 27 | #ifdef __cplusplus 28 | extern "C" { 29 | #endif 30 | 31 | #include "lua_core.h" 32 | #include "lobject.h" 33 | #include "llvm_compiler.h" 34 | 35 | void llvm_newproto (lua_State *L, Proto *f); 36 | void llvm_freeproto (lua_State *L, Proto *f); 37 | 38 | /* functions */ 39 | #define JIT_NEW_STATE(L) llvm_new_compiler(L) 40 | #define JIT_CLOSE_STATE(L) llvm_free_compiler(L) 41 | #define JIT_NEWPROTO(L,f) llvm_newproto(L,f) 42 | #define JIT_FREEPROTO(L,f) llvm_freeproto(L,f) 43 | #define JIT_PRECALL llvm_precall_lua 44 | 45 | #include "lapi.c" 46 | #include "lcode.c" 47 | #include "ldebug.c" 48 | #include "ldump.c" 49 | #include "lfunc.c" 50 | #include "lgc.c" 51 | #include "llex.c" 52 | #include "lmem.c" 53 | #include "lobject.c" 54 | #include "lopcodes.c" 55 | #include "lparser.c" 56 | #include "lstate.c" 57 | #include "lstring.c" 58 | #include "ltable.c" 59 | #include "ltm.c" 60 | #include "lundump.c" 61 | #include "lvm.c" 62 | #include "lzio.c" 63 | 64 | #include "lbaselib.c" 65 | #include "lcoco.c" 66 | #include "ldblib.c" 67 | #include "liolib.c" 68 | #include "linit.c" 69 | #include "llvm_lmathlib.c" 70 | #include "loadlib.c" 71 | #include "loslib.c" 72 | #include "lstrlib.c" 73 | #include "ltablib.c" 74 | 75 | #include "lauxlib.c" 76 | 77 | void llvm_newproto (lua_State *L, Proto *f) { 78 | (void)L; 79 | f->jit_func = NULL; 80 | f->func_ref = NULL; 81 | } 82 | 83 | void llvm_freeproto (lua_State *L, Proto *f) { 84 | llvm_compiler_free(L, f); 85 | } 86 | 87 | #ifdef __cplusplus 88 | } 89 | #endif 90 | 91 | -------------------------------------------------------------------------------- /README.llvm-lua: -------------------------------------------------------------------------------- 1 | README for llvm-lua 2 | 3 | === Requires === 4 | * LLVM 2.8 5 | * Clang or llvm-gcc 4.2.x from llvm.org 6 | 7 | === Compile === 8 | * mkdir build 9 | * cd build 10 | for Release build: 11 | * cmake .. -DLLVM_PATH= -DCMAKE_BUILD_TYPE=Release 12 | for Debug build: 13 | * cmake .. -DLLVM_PATH= -DCMAKE_BUILD_TYPE=Debug 14 | * make 15 | 16 | === Install === 17 | * make install 18 | 19 | === Patches to lua/src === 20 | * Emergency Garbage Collector: http://lua-users.org/wiki/EmergencyGarbageCollector 21 | * LuaCoco-1.1.6: http://luajit.org/coco.html + (x86_64 support added) 22 | * a few hooks where added to support JIT compiled functions. 23 | 24 | === Programs === 25 | * llvm-lua: This command can be used to run Lua script. It uses the LLVM backend to JIT Lua scripts to machine code for faster execution. 26 | * llvm-luac: This command compiles Lua scripts into LLVM bitcode. 27 | * lua-compiler: This is a bash script that wraps llvm-luac to compile Lua scripts into standalone executables or loadable modules. 28 | 29 | === Libraries === 30 | -- Lua core without LLVM JIT support, but compatible with compiled modules. Can be used as drop-in replacements of the normal Lua libraries. 31 | * liblua_static.a & liblua.so 32 | 33 | -- Static library with LLVM JIT support requires static linking with LLVM libraries. 34 | * libllvm-lua_static.a & libllvm-lua.so 35 | 36 | -- Used for compling Lua scripts to standalone executables. 37 | * liblua_main.a 38 | 39 | === Using llvm-lua === 40 | The JIT/interpreter command 'llvm-lua' can be used just like the normal 'lua'. There are a lot of extra command line options that expose some options from LLVM, they are not required for normal use. The JIT will compile Lua code with optimization level 3 by default. 41 | 42 | === Static compiling Lua scripts === 43 | 'llvm-luac' alone can only compile Lua scripts to Lua bytecode or LLVM bitcode. A wrapper script called 'lua-compiler' is provided that wraps 'llvm-luac', the LLVM tools (llc & opt), and gcc. 44 | 45 | Compile standalone Lua script: 46 | lua-compiler script.lua 47 | outputs: ./script 48 | 49 | Compile Lua script as a module: 50 | lua-compiler -lua-module script.lua 51 | outputs: ./script.so 52 | 53 | === Embedding 'llvm-lua' with JIT support === 54 | The Lua C API is unchanged and no extra API functions are exposed by llvm-lua. The only change is how host app. is linked with the 'liblua-llvm.a' library instead of the normal 'liblua.a' library. 55 | 56 | Use the following command to link your C app with llvm-lua: 57 | gcc -o embed.o -c etc/embed_jit.c 58 | g++ -o embed embed.o -lllvm-lua `llvm-config --ldflags --libs core jit native linker` -rdynamic -Wl,-E -lm -ldl 59 | 60 | Use the following command to link your C++ app with llvm-lua: 61 | g++ -o embed.o -c etc/embed_jit.cpp 62 | g++ -o embed embed.o -lllvm-lua `llvm-config --ldflags --libs core jit native linker` -rdynamic -Wl,-E -lm -ldl 63 | 64 | -------------------------------------------------------------------------------- /llvm-lua/llvm_compiler.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2009 Robert G. Jakabosky 3 | 4 | Permission is hereby granted, free of charge, to any person obtaining a copy 5 | of this software and associated documentation files (the "Software"), to deal 6 | in the Software without restriction, including without limitation the rights 7 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | copies of the Software, and to permit persons to whom the Software is 9 | furnished to do so, subject to the following conditions: 10 | 11 | The above copyright notice and this permission notice shall be included in 12 | all copies or substantial portions of the Software. 13 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 | THE SOFTWARE. 21 | 22 | MIT License: http://www.opensource.org/licenses/mit-license.php 23 | */ 24 | 25 | #include "LLVMCompiler.h" 26 | #include "llvm-c/ExecutionEngine.h" 27 | #include "llvm-c/Target.h" 28 | #include "llvm_compiler.h" 29 | #include "llvm_compiler_private.h" 30 | 31 | extern "C" { 32 | 33 | #include "lstate.h" 34 | 35 | /* only used to turn off JIT for static compiler llvm-luac. */ 36 | static int g_useJIT = 1; 37 | static int g_need_init = 1; 38 | 39 | int llvm_compiler_main(int useJIT) { 40 | g_useJIT = useJIT; 41 | return 0; 42 | } 43 | 44 | LLVMCompiler *llvm_get_compiler(lua_State *L) { 45 | global_State *g = G(L); 46 | return (LLVMCompiler *)g->llvm_compiler; 47 | } 48 | 49 | void llvm_new_compiler(lua_State *L) { 50 | global_State *g = G(L); 51 | if(g_need_init) { 52 | LLVMLinkInJIT(); 53 | LLVMInitializeNativeTarget(); 54 | g_need_init = 0; 55 | } 56 | g->llvm_compiler = new LLVMCompiler(g_useJIT); 57 | } 58 | 59 | void llvm_free_compiler(lua_State *L) { 60 | global_State *g = G(L); 61 | LLVMCompiler *compiler = ((LLVMCompiler *)g->llvm_compiler); 62 | g->llvm_compiler = NULL; 63 | delete compiler; 64 | } 65 | 66 | void llvm_compiler_compile(lua_State *L, Proto *p) { 67 | LLVMCompiler *compiler = llvm_get_compiler(L); 68 | if(compiler == NULL) { 69 | llvm_compiler_main(1); 70 | } 71 | compiler->compile(L, p); 72 | } 73 | 74 | void llvm_compiler_compile_all(lua_State *L, Proto *p) { 75 | LLVMCompiler *compiler = llvm_get_compiler(L); 76 | if(compiler == NULL) { 77 | llvm_compiler_main(1); 78 | } 79 | compiler->compileAll(L, p); 80 | } 81 | 82 | void llvm_compiler_free(lua_State *L, Proto *p) { 83 | LLVMCompiler *compiler = llvm_get_compiler(L); 84 | if(compiler != NULL) { 85 | compiler->free(L, p); 86 | } 87 | } 88 | 89 | }// end: extern "C" 90 | 91 | -------------------------------------------------------------------------------- /llvm-lua/bin2c.c: -------------------------------------------------------------------------------- 1 | /* 2 | * bin2c.c 3 | * 4 | * convert a binary file into a C source vector 5 | * 6 | * THE "BEER-WARE LICENSE" (Revision 3.1415): 7 | * sandro AT sigala DOT it wrote this file. As long as you retain this notice you can do 8 | * whatever you want with this stuff. If we meet some day, and you think this stuff is 9 | * worth it, you can buy me a beer in return. Sandro Sigala 10 | * 11 | * syntax: bin2c [-c] [-z] 12 | * 13 | * -c add the "const" keyword to definition 14 | * -z terminate the array with a zero (useful for embedded C strings) 15 | * 16 | * examples: 17 | * bin2c -c myimage.png myimage_png.cpp 18 | * bin2c -z sometext.txt sometext_txt.cpp 19 | */ 20 | 21 | #include 22 | #include 23 | #include 24 | #include 25 | 26 | #ifndef PATH_MAX 27 | #define PATH_MAX 1024 28 | #endif 29 | 30 | int useconst = 0; 31 | int zeroterminated = 0; 32 | 33 | int myfgetc(FILE *f) 34 | { 35 | int c = fgetc(f); 36 | if (c == EOF && zeroterminated) 37 | { 38 | zeroterminated = 0; 39 | return 0; 40 | } 41 | return c; 42 | } 43 | 44 | void process(const char *ifname, const char *ofname) 45 | { 46 | FILE *ifile, *ofile; 47 | ifile = fopen(ifname, "rb"); 48 | if (ifile == NULL) 49 | { 50 | fprintf(stderr, "cannot open %s for reading\n", ifname); 51 | exit(1); 52 | } 53 | ofile = fopen(ofname, "wb"); 54 | if (ofile == NULL) 55 | { 56 | fprintf(stderr, "cannot open %s for writing\n", ofname); 57 | exit(1); 58 | } 59 | char buf[PATH_MAX], *p; 60 | const char *cp; 61 | if ((cp = strrchr(ifname, '/')) != NULL) 62 | { 63 | ++cp; 64 | } else { 65 | if ((cp = strrchr(ifname, '\\')) != NULL) 66 | ++cp; 67 | else 68 | cp = ifname; 69 | } 70 | strcpy(buf, cp); 71 | for (p = buf; *p != '\0'; ++p) 72 | { 73 | if (!isalnum(*p)) 74 | *p = '_'; 75 | } 76 | fprintf(ofile, "static %sunsigned char %s[] = {\n", useconst ? "const " : "", buf); 77 | int c, col = 1; 78 | while ((c = myfgetc(ifile)) != EOF) 79 | { 80 | if (col >= 78 - 6) 81 | { 82 | fputc('\n', ofile); 83 | col = 1; 84 | } 85 | fprintf(ofile, "0x%.2x,", c); 86 | col += 6; 87 | } 88 | fprintf(ofile, "\n};\n"); 89 | 90 | fclose(ifile); 91 | fclose(ofile); 92 | } 93 | 94 | void usage(void) 95 | { 96 | fprintf(stderr, "usage: bin2c [-cz] \n"); 97 | exit(1); 98 | } 99 | 100 | int main(int argc, char **argv) 101 | { 102 | while (argc > 3) 103 | { 104 | if (!strcmp(argv[1], "-c")) 105 | { 106 | useconst = 1; 107 | --argc; 108 | ++argv; 109 | } else if (!strcmp(argv[1], "-z")) 110 | { 111 | zeroterminated = 1; 112 | --argc; 113 | ++argv; 114 | } else { 115 | usage(); 116 | } 117 | } 118 | if (argc != 3) 119 | { 120 | usage(); 121 | } 122 | process(argv[1], argv[2]); 123 | return 0; 124 | } 125 | 126 | -------------------------------------------------------------------------------- /src/llimits.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: llimits.h,v 1.69.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** Limits, basic types, and some other `installation-dependent' definitions 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef llimits_h 8 | #define llimits_h 9 | 10 | 11 | #include 12 | #include 13 | 14 | 15 | #include "lua.h" 16 | 17 | 18 | typedef LUAI_UINT32 lu_int32; 19 | 20 | typedef LUAI_UMEM lu_mem; 21 | 22 | typedef LUAI_MEM l_mem; 23 | 24 | 25 | 26 | /* chars used as small naturals (so that `char' is reserved for characters) */ 27 | typedef unsigned char lu_byte; 28 | 29 | 30 | #define MAX_SIZET ((size_t)(~(size_t)0)-2) 31 | 32 | #define MAX_LUMEM ((lu_mem)(~(lu_mem)0)-2) 33 | 34 | 35 | #define MAX_INT (INT_MAX-2) /* maximum value of an int (-2 for safety) */ 36 | 37 | /* 38 | ** conversion of pointer to integer 39 | ** this is for hashing only; there is no problem if the integer 40 | ** cannot hold the whole pointer value 41 | */ 42 | #define IntPoint(p) ((unsigned int)(lu_mem)(p)) 43 | 44 | 45 | 46 | /* type to ensure maximum alignment */ 47 | typedef LUAI_USER_ALIGNMENT_T L_Umaxalign; 48 | 49 | 50 | /* result of a `usual argument conversion' over lua_Number */ 51 | typedef LUAI_UACNUMBER l_uacNumber; 52 | 53 | 54 | /* internal assertions for in-house debugging */ 55 | #ifdef lua_assert 56 | 57 | #define check_exp(c,e) (lua_assert(c), (e)) 58 | #define api_check(l,e) lua_assert(e) 59 | 60 | #else 61 | 62 | #define lua_assert(c) ((void)0) 63 | #define check_exp(c,e) (e) 64 | #define api_check luai_apicheck 65 | 66 | #endif 67 | 68 | 69 | #ifndef UNUSED 70 | #define UNUSED(x) ((void)(x)) /* to avoid warnings */ 71 | #endif 72 | 73 | 74 | #ifndef cast 75 | #define cast(t, exp) ((t)(exp)) 76 | #endif 77 | 78 | #define cast_byte(i) cast(lu_byte, (i)) 79 | #define cast_num(i) cast(lua_Number, (i)) 80 | #define cast_int(i) cast(int, (i)) 81 | 82 | 83 | 84 | /* 85 | ** type for virtual-machine instructions 86 | ** must be an unsigned with (at least) 4 bytes (see details in lopcodes.h) 87 | */ 88 | typedef lu_int32 Instruction; 89 | 90 | 91 | 92 | /* maximum stack for a Lua function */ 93 | #define MAXSTACK 250 94 | 95 | 96 | 97 | /* minimum size for the string table (must be power of 2) */ 98 | #ifndef MINSTRTABSIZE 99 | #define MINSTRTABSIZE 32 100 | #endif 101 | 102 | 103 | /* minimum size for string buffer */ 104 | #ifndef LUA_MINBUFFER 105 | #define LUA_MINBUFFER 32 106 | #endif 107 | 108 | 109 | #ifndef lua_lock 110 | #define lua_lock(L) ((void) 0) 111 | #define lua_unlock(L) ((void) 0) 112 | #endif 113 | 114 | #ifndef luai_threadyield 115 | #define luai_threadyield(L) {lua_unlock(L); lua_lock(L);} 116 | #endif 117 | 118 | 119 | /* 120 | ** macro to control inclusion of some hard tests on stack reallocation 121 | */ 122 | #ifndef HARDSTACKTESTS 123 | #define condhardstacktests(x) ((void)0) 124 | #else 125 | #define condhardstacktests(x) x 126 | #endif 127 | 128 | #endif 129 | -------------------------------------------------------------------------------- /src/lcode.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lcode.h,v 1.48.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** Code generator for Lua 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lcode_h 8 | #define lcode_h 9 | 10 | #include "llex.h" 11 | #include "lobject.h" 12 | #include "lopcodes.h" 13 | #include "lparser.h" 14 | 15 | 16 | /* 17 | ** Marks the end of a patch list. It is an invalid value both as an absolute 18 | ** address, and as a list link (would link an element to itself). 19 | */ 20 | #define NO_JUMP (-1) 21 | 22 | 23 | /* 24 | ** grep "ORDER OPR" if you change these enums 25 | */ 26 | typedef enum BinOpr { 27 | OPR_ADD, OPR_SUB, OPR_MUL, OPR_DIV, OPR_MOD, OPR_POW, 28 | OPR_CONCAT, 29 | OPR_NE, OPR_EQ, 30 | OPR_LT, OPR_LE, OPR_GT, OPR_GE, 31 | OPR_AND, OPR_OR, 32 | OPR_NOBINOPR 33 | } BinOpr; 34 | 35 | 36 | typedef enum UnOpr { OPR_MINUS, OPR_NOT, OPR_LEN, OPR_NOUNOPR } UnOpr; 37 | 38 | 39 | #define getcode(fs,e) ((fs)->f->code[(e)->u.s.info]) 40 | 41 | #define luaK_codeAsBx(fs,o,A,sBx) luaK_codeABx(fs,o,A,(sBx)+MAXARG_sBx) 42 | 43 | #define luaK_setmultret(fs,e) luaK_setreturns(fs, e, LUA_MULTRET) 44 | 45 | LUAI_FUNC int luaK_codeABx (FuncState *fs, OpCode o, int A, unsigned int Bx); 46 | LUAI_FUNC int luaK_codeABC (FuncState *fs, OpCode o, int A, int B, int C); 47 | LUAI_FUNC void luaK_fixline (FuncState *fs, int line); 48 | LUAI_FUNC void luaK_nil (FuncState *fs, int from, int n); 49 | LUAI_FUNC void luaK_reserveregs (FuncState *fs, int n); 50 | LUAI_FUNC void luaK_checkstack (FuncState *fs, int n); 51 | LUAI_FUNC int luaK_stringK (FuncState *fs, TString *s); 52 | LUAI_FUNC int luaK_numberK (FuncState *fs, lua_Number r); 53 | LUAI_FUNC void luaK_dischargevars (FuncState *fs, expdesc *e); 54 | LUAI_FUNC int luaK_exp2anyreg (FuncState *fs, expdesc *e); 55 | LUAI_FUNC void luaK_exp2nextreg (FuncState *fs, expdesc *e); 56 | LUAI_FUNC void luaK_exp2val (FuncState *fs, expdesc *e); 57 | LUAI_FUNC int luaK_exp2RK (FuncState *fs, expdesc *e); 58 | LUAI_FUNC void luaK_self (FuncState *fs, expdesc *e, expdesc *key); 59 | LUAI_FUNC void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k); 60 | LUAI_FUNC void luaK_goiftrue (FuncState *fs, expdesc *e); 61 | LUAI_FUNC void luaK_storevar (FuncState *fs, expdesc *var, expdesc *e); 62 | LUAI_FUNC void luaK_setreturns (FuncState *fs, expdesc *e, int nresults); 63 | LUAI_FUNC void luaK_setoneret (FuncState *fs, expdesc *e); 64 | LUAI_FUNC int luaK_jump (FuncState *fs); 65 | LUAI_FUNC void luaK_ret (FuncState *fs, int first, int nret); 66 | LUAI_FUNC void luaK_patchlist (FuncState *fs, int list, int target); 67 | LUAI_FUNC void luaK_patchtohere (FuncState *fs, int list); 68 | LUAI_FUNC void luaK_concat (FuncState *fs, int *l1, int l2); 69 | LUAI_FUNC int luaK_getlabel (FuncState *fs); 70 | LUAI_FUNC void luaK_prefix (FuncState *fs, UnOpr op, expdesc *v); 71 | LUAI_FUNC void luaK_infix (FuncState *fs, BinOpr op, expdesc *v); 72 | LUAI_FUNC void luaK_posfix (FuncState *fs, BinOpr op, expdesc *v1, expdesc *v2); 73 | LUAI_FUNC void luaK_setlist (FuncState *fs, int base, int nelems, int tostore); 74 | 75 | 76 | #endif 77 | -------------------------------------------------------------------------------- /llvm-lua/LLVMDumper.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2009 Robert G. Jakabosky 3 | 4 | Permission is hereby granted, free of charge, to any person obtaining a copy 5 | of this software and associated documentation files (the "Software"), to deal 6 | in the Software without restriction, including without limitation the rights 7 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | copies of the Software, and to permit persons to whom the Software is 9 | furnished to do so, subject to the following conditions: 10 | 11 | The above copyright notice and this permission notice shall be included in 12 | all copies or substantial portions of the Software. 13 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 | THE SOFTWARE. 21 | 22 | MIT License: http://www.opensource.org/licenses/mit-license.php 23 | */ 24 | 25 | #ifndef LLVMDUMPER_h 26 | #define LLVMDUMPER_h 27 | 28 | #include "llvm/Module.h" 29 | #include "lua_core.h" 30 | 31 | #ifdef __cplusplus 32 | extern "C" { 33 | #endif 34 | 35 | #include "lobject.h" 36 | 37 | #ifdef __cplusplus 38 | } 39 | #endif 40 | 41 | namespace llvm { 42 | class Module; 43 | class Type; 44 | class StructType; 45 | class FunctionType; 46 | class Constant; 47 | class GlobalVariable; 48 | } 49 | 50 | class LLVMCompiler; 51 | 52 | class LLVMDumper { 53 | private: 54 | LLVMCompiler *compiler; 55 | llvm::Module *M; 56 | 57 | // types. 58 | llvm::Type *Ty_str_ptr; 59 | llvm::StructType *Ty_constant_value; 60 | llvm::StructType *Ty_constant_type; 61 | llvm::Type *Ty_constant_type_ptr; 62 | llvm::StructType *Ty_constant_num_type; 63 | llvm::Constant *num_padding; 64 | llvm::StructType *Ty_constant_bool_type; 65 | llvm::Constant *bool_padding; 66 | llvm::StructType *Ty_constant_str_type; 67 | llvm::Constant *str_padding; 68 | llvm::StructType *Ty_jit_LocVar; 69 | llvm::Type *Ty_jit_LocVar_ptr; 70 | llvm::StructType *Ty_jit_proto; 71 | llvm::Type *Ty_jit_proto_ptr; 72 | llvm::FunctionType *lua_func_type; 73 | llvm::Type *lua_func_type_ptr; 74 | 75 | public: 76 | LLVMDumper(LLVMCompiler *compiler); 77 | 78 | void dump(const char *output, lua_State *L, Proto *p, int stripping); 79 | 80 | llvm::LLVMContext& getCtx() const { 81 | return compiler->getCtx(); 82 | } 83 | 84 | private: 85 | llvm::Constant *get_ptr(llvm::Constant *val); 86 | 87 | llvm::Constant *get_global_str(const char *str); 88 | 89 | llvm::GlobalVariable *dump_constants(Proto *p); 90 | 91 | llvm::GlobalVariable *dump_locvars(Proto *p); 92 | 93 | llvm::GlobalVariable *dump_upvalues(Proto *p); 94 | 95 | llvm::Constant *dump_proto(Proto *p); 96 | 97 | void dump_standalone(Proto *p); 98 | 99 | void dump_lua_module(Proto *p, std::string mod_name); 100 | 101 | }; 102 | #endif 103 | 104 | -------------------------------------------------------------------------------- /llvm-lua/load_jit_proto.h: -------------------------------------------------------------------------------- 1 | /* 2 | load_jit_proto.h -- load jit proto 3 | 4 | Copyright (c) 2009 Robert G. Jakabosky 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in 14 | all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | THE SOFTWARE. 23 | 24 | MIT License: http://www.opensource.org/licenses/mit-license.php 25 | */ 26 | 27 | #ifndef load_jit_proto_h 28 | #define load_jit_proto_h 29 | 30 | #ifdef __cplusplus 31 | extern "C" { 32 | #endif 33 | 34 | #include "lobject.h" 35 | #include 36 | 37 | #define TYPE_NIL 0 38 | #define TYPE_NUMBER 1 39 | #define TYPE_BOOLEAN 2 40 | #define TYPE_STRING 3 41 | 42 | typedef union constant_value { 43 | /* nil doesn't need a value. */ 44 | uint32_t b; /* Lua boolean */ 45 | LUA_NUMBER num; /* Lua numbers */ 46 | char *str; /* Lua string. */ 47 | } constant_value; 48 | 49 | typedef struct constant_type { 50 | uint32_t type; /* constant type. */ 51 | uint32_t length; /* string length */ 52 | constant_value val; /* value of Lua nil/boolean/number/string. */ 53 | } constant_type; 54 | 55 | /* simplified version of LocVar struct. */ 56 | typedef struct jit_LocVar { 57 | char *varname; 58 | uint32_t startpc; 59 | uint32_t endpc; 60 | } jit_LocVar; 61 | 62 | /* simplified version of Proto struct. */ 63 | typedef struct jit_proto jit_proto; 64 | struct jit_proto { 65 | char *name; 66 | lua_CFunction jit_func; 67 | uint32_t linedefined; 68 | uint32_t lastlinedefined; 69 | uint8_t nups; 70 | uint8_t numparams; 71 | uint8_t is_vararg; 72 | uint8_t maxstacksize; 73 | uint16_t sizek; 74 | uint16_t sizelocvars; 75 | uint32_t sizeupvalues; 76 | uint32_t sizep; 77 | uint32_t sizecode; 78 | uint32_t sizelineinfo; 79 | constant_type *k; 80 | jit_LocVar *locvars; 81 | char **upvalues; 82 | jit_proto *p; 83 | uint32_t *code; 84 | uint32_t *lineinfo; 85 | }; 86 | 87 | Proto *load_jit_proto(lua_State *L, jit_proto *p); 88 | 89 | LUALIB_API int load_compiled_protos(lua_State *L, jit_proto *p); 90 | LUALIB_API int load_compiled_module(lua_State *L, jit_proto *p); 91 | 92 | extern jit_proto jit_proto_init; 93 | 94 | #ifdef __cplusplus 95 | } 96 | #endif 97 | 98 | #endif 99 | 100 | -------------------------------------------------------------------------------- /llvm-lua/tests/loadk.lua: -------------------------------------------------------------------------------- 1 | local function test_loadk() 2 | local q = { 3 | 1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0, 4 | 1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0, 5 | 1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0, 6 | 1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0, 7 | 1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0, 8 | 1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0, 9 | 1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0, 10 | 1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0, 11 | 1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0, 12 | 1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0, 13 | 1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0, 14 | 1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0, 15 | 1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0, 16 | 1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0, 17 | 1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0, 18 | 1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0, 19 | 1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0, 20 | 1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0, 21 | 1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0, 22 | 1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0, 23 | 1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0, 24 | 1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0, 25 | 1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0, 26 | 1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0, 27 | 1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0, 28 | 1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0, 29 | 1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0, 30 | 1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0, 31 | 1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0, 32 | 1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0, 33 | } 34 | return q 35 | end 36 | 37 | local N = tonumber(arg and arg[1]) or 1 38 | 39 | for i=0,N do 40 | test_loadk() 41 | end 42 | 43 | local t=test_loadk() 44 | print("table size=",#t) 45 | -------------------------------------------------------------------------------- /src/lopcodes.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lopcodes.c,v 1.37.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** See Copyright Notice in lua.h 4 | */ 5 | 6 | 7 | #define lopcodes_c 8 | #define LUA_CORE 9 | 10 | 11 | #include "lopcodes.h" 12 | 13 | 14 | /* ORDER OP */ 15 | 16 | const char *const luaP_opnames[NUM_OPCODES+1] = { 17 | "MOVE", 18 | "LOADK", 19 | "LOADBOOL", 20 | "LOADNIL", 21 | "GETUPVAL", 22 | "GETGLOBAL", 23 | "GETTABLE", 24 | "SETGLOBAL", 25 | "SETUPVAL", 26 | "SETTABLE", 27 | "NEWTABLE", 28 | "SELF", 29 | "ADD", 30 | "SUB", 31 | "MUL", 32 | "DIV", 33 | "MOD", 34 | "POW", 35 | "UNM", 36 | "NOT", 37 | "LEN", 38 | "CONCAT", 39 | "JMP", 40 | "EQ", 41 | "LT", 42 | "LE", 43 | "TEST", 44 | "TESTSET", 45 | "CALL", 46 | "TAILCALL", 47 | "RETURN", 48 | "FORLOOP", 49 | "FORPREP", 50 | "TFORLOOP", 51 | "SETLIST", 52 | "CLOSE", 53 | "CLOSURE", 54 | "VARARG", 55 | NULL 56 | }; 57 | 58 | 59 | #define opmode(t,a,b,c,m) (((t)<<7) | ((a)<<6) | ((b)<<4) | ((c)<<2) | (m)) 60 | 61 | const lu_byte luaP_opmodes[NUM_OPCODES] = { 62 | /* T A B C mode opcode */ 63 | opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_MOVE */ 64 | ,opmode(0, 1, OpArgK, OpArgN, iABx) /* OP_LOADK */ 65 | ,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_LOADBOOL */ 66 | ,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_LOADNIL */ 67 | ,opmode(0, 1, OpArgU, OpArgN, iABC) /* OP_GETUPVAL */ 68 | ,opmode(0, 1, OpArgK, OpArgN, iABx) /* OP_GETGLOBAL */ 69 | ,opmode(0, 1, OpArgR, OpArgK, iABC) /* OP_GETTABLE */ 70 | ,opmode(0, 0, OpArgK, OpArgN, iABx) /* OP_SETGLOBAL */ 71 | ,opmode(0, 0, OpArgU, OpArgN, iABC) /* OP_SETUPVAL */ 72 | ,opmode(0, 0, OpArgK, OpArgK, iABC) /* OP_SETTABLE */ 73 | ,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_NEWTABLE */ 74 | ,opmode(0, 1, OpArgR, OpArgK, iABC) /* OP_SELF */ 75 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_ADD */ 76 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_SUB */ 77 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_MUL */ 78 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_DIV */ 79 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_MOD */ 80 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_POW */ 81 | ,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_UNM */ 82 | ,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_NOT */ 83 | ,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_LEN */ 84 | ,opmode(0, 1, OpArgR, OpArgR, iABC) /* OP_CONCAT */ 85 | ,opmode(0, 0, OpArgR, OpArgN, iAsBx) /* OP_JMP */ 86 | ,opmode(1, 0, OpArgK, OpArgK, iABC) /* OP_EQ */ 87 | ,opmode(1, 0, OpArgK, OpArgK, iABC) /* OP_LT */ 88 | ,opmode(1, 0, OpArgK, OpArgK, iABC) /* OP_LE */ 89 | ,opmode(1, 1, OpArgR, OpArgU, iABC) /* OP_TEST */ 90 | ,opmode(1, 1, OpArgR, OpArgU, iABC) /* OP_TESTSET */ 91 | ,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_CALL */ 92 | ,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_TAILCALL */ 93 | ,opmode(0, 0, OpArgU, OpArgN, iABC) /* OP_RETURN */ 94 | ,opmode(0, 1, OpArgR, OpArgN, iAsBx) /* OP_FORLOOP */ 95 | ,opmode(0, 1, OpArgR, OpArgN, iAsBx) /* OP_FORPREP */ 96 | ,opmode(1, 0, OpArgN, OpArgU, iABC) /* OP_TFORLOOP */ 97 | ,opmode(0, 0, OpArgU, OpArgU, iABC) /* OP_SETLIST */ 98 | ,opmode(0, 0, OpArgN, OpArgN, iABC) /* OP_CLOSE */ 99 | ,opmode(0, 1, OpArgU, OpArgN, iABx) /* OP_CLOSURE */ 100 | ,opmode(0, 1, OpArgU, OpArgN, iABC) /* OP_VARARG */ 101 | }; 102 | 103 | -------------------------------------------------------------------------------- /src/lstring.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lstring.c,v 2.8.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** String table (keeps all strings handled by Lua) 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #include 9 | 10 | #define lstring_c 11 | #define LUA_CORE 12 | 13 | #include "lua.h" 14 | 15 | #include "lmem.h" 16 | #include "lobject.h" 17 | #include "lstate.h" 18 | #include "lstring.h" 19 | 20 | 21 | 22 | void luaS_resize (lua_State *L, int newsize) { 23 | stringtable *tb; 24 | int i; 25 | tb = &G(L)->strt; 26 | if (G(L)->gcstate == GCSsweepstring || newsize == tb->size) 27 | return; /* cannot resize during GC traverse or doesn't need to be resized */ 28 | if (newsize > tb->size) { 29 | luaM_reallocvector(L, tb->hash, tb->size, newsize, GCObject *); 30 | for (i=tb->size; ihash[i] = NULL; 31 | } 32 | /* rehash */ 33 | for (i=0; isize; i++) { 34 | GCObject *p = tb->hash[i]; 35 | tb->hash[i] = NULL; 36 | while (p) { /* for each node in the list */ 37 | GCObject *next = p->gch.next; /* save next */ 38 | unsigned int h = gco2ts(p)->hash; 39 | int h1 = lmod(h, newsize); /* new position */ 40 | lua_assert(cast_int(h%newsize) == lmod(h, newsize)); 41 | p->gch.next = tb->hash[h1]; /* chain it */ 42 | tb->hash[h1] = p; 43 | p = next; 44 | } 45 | } 46 | if (newsize < tb->size) 47 | luaM_reallocvector(L, tb->hash, tb->size, newsize, GCObject *); 48 | tb->size = newsize; 49 | } 50 | 51 | 52 | static TString *newlstr (lua_State *L, const char *str, size_t l, 53 | unsigned int h) { 54 | TString *ts; 55 | stringtable *tb; 56 | if (l+1 > (MAX_SIZET - sizeof(TString))/sizeof(char)) 57 | luaM_toobig(L); 58 | tb = &G(L)->strt; 59 | if ((tb->nuse + 1) > cast(lu_int32, tb->size) && tb->size <= MAX_INT/2) 60 | luaS_resize(L, tb->size*2); /* too crowded */ 61 | ts = cast(TString *, luaM_malloc(L, (l+1)*sizeof(char)+sizeof(TString))); 62 | ts->tsv.len = l; 63 | ts->tsv.hash = h; 64 | ts->tsv.marked = luaC_white(G(L)); 65 | ts->tsv.tt = LUA_TSTRING; 66 | ts->tsv.reserved = 0; 67 | memcpy(ts+1, str, l*sizeof(char)); 68 | ((char *)(ts+1))[l] = '\0'; /* ending 0 */ 69 | h = lmod(h, tb->size); 70 | ts->tsv.next = tb->hash[h]; /* chain new entry */ 71 | tb->hash[h] = obj2gco(ts); 72 | tb->nuse++; 73 | return ts; 74 | } 75 | 76 | 77 | TString *luaS_newlstr (lua_State *L, const char *str, size_t l) { 78 | GCObject *o; 79 | unsigned int h = cast(unsigned int, l); /* seed */ 80 | size_t step = (l>>5)+1; /* if string is too long, don't hash all its chars */ 81 | size_t l1; 82 | for (l1=l; l1>=step; l1-=step) /* compute hash */ 83 | h = h ^ ((h<<5)+(h>>2)+cast(unsigned char, str[l1-1])); 84 | for (o = G(L)->strt.hash[lmod(h, G(L)->strt.size)]; 85 | o != NULL; 86 | o = o->gch.next) { 87 | TString *ts = rawgco2ts(o); 88 | if (ts->tsv.len == l && (memcmp(str, getstr(ts), l) == 0)) { 89 | /* string may be dead */ 90 | if (isdead(G(L), o)) changewhite(o); 91 | return ts; 92 | } 93 | } 94 | return newlstr(L, str, l, h); /* not found */ 95 | } 96 | 97 | 98 | Udata *luaS_newudata (lua_State *L, size_t s, Table *e) { 99 | Udata *u; 100 | if (s > MAX_SIZET - sizeof(Udata)) 101 | luaM_toobig(L); 102 | u = cast(Udata *, luaM_malloc(L, s + sizeof(Udata))); 103 | u->uv.marked = luaC_white(G(L)); /* is not finalized */ 104 | u->uv.tt = LUA_TUSERDATA; 105 | u->uv.len = s; 106 | u->uv.metatable = NULL; 107 | u->uv.env = e; 108 | /* chain it on udata list (after main thread) */ 109 | u->uv.next = G(L)->mainthread->next; 110 | G(L)->mainthread->next = obj2gco(u); 111 | return u; 112 | } 113 | 114 | -------------------------------------------------------------------------------- /src/ldump.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ldump.c,v 2.8.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** save precompiled Lua chunks 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #include 8 | 9 | #define ldump_c 10 | #define LUA_CORE 11 | 12 | #include "lua.h" 13 | 14 | #include "lobject.h" 15 | #include "lstate.h" 16 | #include "lundump.h" 17 | 18 | typedef struct { 19 | lua_State* L; 20 | lua_Writer writer; 21 | void* data; 22 | int strip; 23 | int status; 24 | } DumpState; 25 | 26 | #define DumpMem(b,n,size,D) DumpBlock(b,(n)*(size),D) 27 | #define DumpVar(x,D) DumpMem(&x,1,sizeof(x),D) 28 | 29 | static void DumpBlock(const void* b, size_t size, DumpState* D) 30 | { 31 | if (D->status==0) 32 | { 33 | lua_unlock(D->L); 34 | D->status=(*D->writer)(D->L,b,size,D->data); 35 | lua_lock(D->L); 36 | } 37 | } 38 | 39 | static void DumpChar(int y, DumpState* D) 40 | { 41 | char x=(char)y; 42 | DumpVar(x,D); 43 | } 44 | 45 | static void DumpInt(int x, DumpState* D) 46 | { 47 | DumpVar(x,D); 48 | } 49 | 50 | static void DumpNumber(lua_Number x, DumpState* D) 51 | { 52 | DumpVar(x,D); 53 | } 54 | 55 | static void DumpVector(const void* b, int n, size_t size, DumpState* D) 56 | { 57 | DumpInt(n,D); 58 | DumpMem(b,n,size,D); 59 | } 60 | 61 | static void DumpString(const TString* s, DumpState* D) 62 | { 63 | if (s==NULL || getstr(s)==NULL) 64 | { 65 | size_t size=0; 66 | DumpVar(size,D); 67 | } 68 | else 69 | { 70 | size_t size=s->tsv.len+1; /* include trailing '\0' */ 71 | DumpVar(size,D); 72 | DumpBlock(getstr(s),size,D); 73 | } 74 | } 75 | 76 | #define DumpCode(f,D) DumpVector(f->code,f->sizecode,sizeof(Instruction),D) 77 | 78 | static void DumpFunction(const Proto* f, const TString* p, DumpState* D); 79 | 80 | static void DumpConstants(const Proto* f, DumpState* D) 81 | { 82 | int i,n=f->sizek; 83 | DumpInt(n,D); 84 | for (i=0; ik[i]; 87 | DumpChar(ttype(o),D); 88 | switch (ttype(o)) 89 | { 90 | case LUA_TNIL: 91 | break; 92 | case LUA_TBOOLEAN: 93 | DumpChar(bvalue(o),D); 94 | break; 95 | case LUA_TNUMBER: 96 | DumpNumber(nvalue(o),D); 97 | break; 98 | case LUA_TSTRING: 99 | DumpString(rawtsvalue(o),D); 100 | break; 101 | default: 102 | lua_assert(0); /* cannot happen */ 103 | break; 104 | } 105 | } 106 | n=f->sizep; 107 | DumpInt(n,D); 108 | for (i=0; ip[i],f->source,D); 109 | } 110 | 111 | static void DumpDebug(const Proto* f, DumpState* D) 112 | { 113 | int i,n; 114 | n= (D->strip) ? 0 : f->sizelineinfo; 115 | DumpVector(f->lineinfo,n,sizeof(int),D); 116 | n= (D->strip) ? 0 : f->sizelocvars; 117 | DumpInt(n,D); 118 | for (i=0; ilocvars[i].varname,D); 121 | DumpInt(f->locvars[i].startpc,D); 122 | DumpInt(f->locvars[i].endpc,D); 123 | } 124 | n= (D->strip) ? 0 : f->sizeupvalues; 125 | DumpInt(n,D); 126 | for (i=0; iupvalues[i],D); 127 | } 128 | 129 | static void DumpFunction(const Proto* f, const TString* p, DumpState* D) 130 | { 131 | DumpString((f->source==p || D->strip) ? NULL : f->source,D); 132 | DumpInt(f->linedefined,D); 133 | DumpInt(f->lastlinedefined,D); 134 | DumpChar(f->nups,D); 135 | DumpChar(f->numparams,D); 136 | DumpChar(f->is_vararg,D); 137 | DumpChar(f->maxstacksize,D); 138 | DumpCode(f,D); 139 | DumpConstants(f,D); 140 | DumpDebug(f,D); 141 | } 142 | 143 | static void DumpHeader(DumpState* D) 144 | { 145 | char h[LUAC_HEADERSIZE]; 146 | luaU_header(h); 147 | DumpBlock(h,LUAC_HEADERSIZE,D); 148 | } 149 | 150 | /* 151 | ** dump Lua function as precompiled chunk 152 | */ 153 | int luaU_dump (lua_State* L, const Proto* f, lua_Writer w, void* data, int strip) 154 | { 155 | DumpState D; 156 | D.L=L; 157 | D.writer=w; 158 | D.data=data; 159 | D.strip=strip; 160 | D.status=0; 161 | DumpHeader(&D); 162 | DumpFunction(f,NULL,&D); 163 | return D.status; 164 | } 165 | -------------------------------------------------------------------------------- /doc/luac.1: -------------------------------------------------------------------------------- 1 | .\" $Id: luac.man,v 1.28 2006/01/06 16:03:34 lhf Exp $ 2 | .TH LUAC 1 "$Date: 2006/01/06 16:03:34 $" 3 | .SH NAME 4 | luac \- Lua compiler 5 | .SH SYNOPSIS 6 | .B luac 7 | [ 8 | .I options 9 | ] [ 10 | .I filenames 11 | ] 12 | .SH DESCRIPTION 13 | .B luac 14 | is the Lua compiler. 15 | It translates programs written in the Lua programming language 16 | into binary files that can be later loaded and executed. 17 | .LP 18 | The main advantages of precompiling chunks are: 19 | faster loading, 20 | protecting source code from accidental user changes, 21 | and 22 | off-line syntax checking. 23 | .LP 24 | Pre-compiling does not imply faster execution 25 | because in Lua chunks are always compiled into bytecodes before being executed. 26 | .B luac 27 | simply allows those bytecodes to be saved in a file for later execution. 28 | .LP 29 | Pre-compiled chunks are not necessarily smaller than the corresponding source. 30 | The main goal in pre-compiling is faster loading. 31 | .LP 32 | The binary files created by 33 | .B luac 34 | are portable only among architectures with the same word size and byte order. 35 | .LP 36 | .B luac 37 | produces a single output file containing the bytecodes 38 | for all source files given. 39 | By default, 40 | the output file is named 41 | .BR luac.out , 42 | but you can change this with the 43 | .B \-o 44 | option. 45 | .LP 46 | In the command line, 47 | you can mix 48 | text files containing Lua source and 49 | binary files containing precompiled chunks. 50 | This is useful to combine several precompiled chunks, 51 | even from different (but compatible) platforms, 52 | into a single precompiled chunk. 53 | .LP 54 | You can use 55 | .B "'\-'" 56 | to indicate the standard input as a source file 57 | and 58 | .B "'\--'" 59 | to signal the end of options 60 | (that is, 61 | all remaining arguments will be treated as files even if they start with 62 | .BR "'\-'" ). 63 | .LP 64 | The internal format of the binary files produced by 65 | .B luac 66 | is likely to change when a new version of Lua is released. 67 | So, 68 | save the source files of all Lua programs that you precompile. 69 | .LP 70 | .SH OPTIONS 71 | Options must be separate. 72 | .TP 73 | .B \-l 74 | produce a listing of the compiled bytecode for Lua's virtual machine. 75 | Listing bytecodes is useful to learn about Lua's virtual machine. 76 | If no files are given, then 77 | .B luac 78 | loads 79 | .B luac.out 80 | and lists its contents. 81 | .TP 82 | .BI \-o " file" 83 | output to 84 | .IR file , 85 | instead of the default 86 | .BR luac.out . 87 | (You can use 88 | .B "'\-'" 89 | for standard output, 90 | but not on platforms that open standard output in text mode.) 91 | The output file may be a source file because 92 | all files are loaded before the output file is written. 93 | Be careful not to overwrite precious files. 94 | .TP 95 | .B \-p 96 | load files but do not generate any output file. 97 | Used mainly for syntax checking and for testing precompiled chunks: 98 | corrupted files will probably generate errors when loaded. 99 | Lua always performs a thorough integrity test on precompiled chunks. 100 | Bytecode that passes this test is completely safe, 101 | in the sense that it will not break the interpreter. 102 | However, 103 | there is no guarantee that such code does anything sensible. 104 | (None can be given, because the halting problem is unsolvable.) 105 | If no files are given, then 106 | .B luac 107 | loads 108 | .B luac.out 109 | and tests its contents. 110 | No messages are displayed if the file passes the integrity test. 111 | .TP 112 | .B \-s 113 | strip debug information before writing the output file. 114 | This saves some space in very large chunks, 115 | but if errors occur when running a stripped chunk, 116 | then the error messages may not contain the full information they usually do. 117 | For instance, 118 | line numbers and names of local variables are lost. 119 | .TP 120 | .B \-v 121 | show version information. 122 | .SH FILES 123 | .TP 15 124 | .B luac.out 125 | default output file 126 | .SH "SEE ALSO" 127 | .BR lua (1) 128 | .br 129 | http://www.lua.org/ 130 | .SH DIAGNOSTICS 131 | Error messages should be self explanatory. 132 | .SH AUTHORS 133 | L. H. de Figueiredo, 134 | R. Ierusalimschy and 135 | W. Celes 136 | .\" EOF 137 | -------------------------------------------------------------------------------- /INSTALL: -------------------------------------------------------------------------------- 1 | INSTALL for Lua 5.1 2 | 3 | * Building Lua 4 | ------------ 5 | Lua is built in the src directory, but the build process can be 6 | controlled from the top-level Makefile. 7 | 8 | Building Lua on Unix systems should be very easy. First do "make" and 9 | see if your platform is listed. If so, just do "make xxx", where xxx 10 | is your platform name. The platforms currently supported are: 11 | aix ansi bsd freebsd generic linux macosx mingw posix solaris 12 | 13 | If your platform is not listed, try the closest one or posix, generic, 14 | ansi, in this order. 15 | 16 | See below for customization instructions and for instructions on how 17 | to build with other Windows compilers. 18 | 19 | If you want to check that Lua has been built correctly, do "make test" 20 | after building Lua. Also, have a look at the example programs in test. 21 | 22 | * Installing Lua 23 | -------------- 24 | Once you have built Lua, you may want to install it in an official 25 | place in your system. In this case, do "make install". The official 26 | place and the way to install files are defined in Makefile. You must 27 | have the right permissions to install files. 28 | 29 | If you want to build and install Lua in one step, do "make xxx install", 30 | where xxx is your platform name. 31 | 32 | If you want to install Lua locally, then do "make local". This will 33 | create directories bin, include, lib, man, and install Lua there as 34 | follows: 35 | 36 | bin: lua luac 37 | include: lua.h luaconf.h lualib.h lauxlib.h lua.hpp 38 | lib: liblua.a 39 | man/man1: lua.1 luac.1 40 | 41 | These are the only directories you need for development. 42 | 43 | There are man pages for lua and luac, in both nroff and html, and a 44 | reference manual in html in doc, some sample code in test, and some 45 | useful stuff in etc. You don't need these directories for development. 46 | 47 | If you want to install Lua locally, but in some other directory, do 48 | "make install INSTALL_TOP=xxx", where xxx is your chosen directory. 49 | 50 | See below for instructions for Windows and other systems. 51 | 52 | * Customization 53 | ------------- 54 | Three things can be customized by editing a file: 55 | - Where and how to install Lua -- edit Makefile. 56 | - How to build Lua -- edit src/Makefile. 57 | - Lua features -- edit src/luaconf.h. 58 | 59 | You don't actually need to edit the Makefiles because you may set the 60 | relevant variables when invoking make. 61 | 62 | On the other hand, if you need to select some Lua features, you'll need 63 | to edit src/luaconf.h. The edited file will be the one installed, and 64 | it will be used by any Lua clients that you build, to ensure consistency. 65 | 66 | We strongly recommend that you enable dynamic loading. This is done 67 | automatically for all platforms listed above that have this feature 68 | (and also Windows). See src/luaconf.h and also src/Makefile. 69 | 70 | * Building Lua on Windows and other systems 71 | ----------------------------------------- 72 | If you're not using the usual Unix tools, then the instructions for 73 | building Lua depend on the compiler you use. You'll need to create 74 | projects (or whatever your compiler uses) for building the library, 75 | the interpreter, and the compiler, as follows: 76 | 77 | library: lapi.c lcode.c ldebug.c ldo.c ldump.c lfunc.c lgc.c llex.c 78 | lmem.c lobject.c lopcodes.c lparser.c lstate.c lstring.c 79 | ltable.c ltm.c lundump.c lvm.c lzio.c 80 | lauxlib.c lbaselib.c ldblib.c liolib.c lmathlib.c loslib.c 81 | ltablib.c lstrlib.c loadlib.c linit.c 82 | 83 | interpreter: library, lua.c 84 | 85 | compiler: library, luac.c print.c 86 | 87 | If you use Visual Studio .NET, you can use etc/luavs.bat in its 88 | "Command Prompt". 89 | 90 | If all you want is to build the Lua interpreter, you may put all .c files 91 | in a single project, except for luac.c and print.c. Or just use etc/all.c. 92 | 93 | To use Lua as a library in your own programs, you'll need to know how to 94 | create and use libraries with your compiler. 95 | 96 | As mentioned above, you may edit luaconf.h to select some features before 97 | building Lua. 98 | 99 | (end of INSTALL) 100 | -------------------------------------------------------------------------------- /llvm-lua/lua-cross-compiler.in: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | # 3 | 4 | # path to lua-cross-compiler 5 | DIR=`realpath $0` 6 | DIR=`dirname $DIR` 7 | 8 | CC=@CROSS_TRIPLE@-gcc 9 | LLVM_LUAC="./@CROSS_TRIPLE@-llvm-luac" 10 | 11 | # find llvm-luac cross-compiler 12 | if [[ ! -x "$LLVM_LUAC" ]]; then 13 | LLVM_LUAC="$DIR/@CROSS_TRIPLE@-llvm-luac" 14 | fi 15 | if [[ ! -x "$LLVM_LUAC" ]]; then 16 | LLVM_LUAC=`which @CROSS_TRIPLE@-llvm-luac` 17 | fi 18 | 19 | ARCH=@CROSS_ARCH@ 20 | CPU=@CROSS_CPU@ 21 | FORCE_CPU="0" 22 | MODULE="0" 23 | NO_ASSEMBLE="0" 24 | FILE=lua-native-out 25 | FILES="" 26 | DEBUG="0" 27 | KEEP_TMPS="0" 28 | EXTRA_ARGS= 29 | 30 | function echo_cmd() { 31 | CMD="$1" 32 | shift 1 33 | echo "$CMD" "$@" 34 | $CMD "$@" 35 | } 36 | 37 | function version() { 38 | $LLVM_LUAC -version 39 | exit 0; 40 | } 41 | 42 | function usage() { 43 | echo "USAGE: $0 [options]