aboutsummaryrefslogtreecommitdiff
path: root/src/host/genminilua.lua
diff options
context:
space:
mode:
Diffstat (limited to 'src/host/genminilua.lua')
-rw-r--r--src/host/genminilua.lua66
1 files changed, 65 insertions, 1 deletions
diff --git a/src/host/genminilua.lua b/src/host/genminilua.lua
index 587d5a8d..2a8a5336 100644
--- a/src/host/genminilua.lua
+++ b/src/host/genminilua.lua
@@ -58,10 +58,68 @@ end)
58local REMOVE_EXTINC = { ["<assert.h>"] = true, ["<locale.h>"] = true, } 58local REMOVE_EXTINC = { ["<assert.h>"] = true, ["<locale.h>"] = true, }
59 59
60local CUSTOM_MAIN = [[ 60local CUSTOM_MAIN = [[
61typedef unsigned int UB;
62static UB barg(lua_State *L,int idx){
63union{lua_Number n;U64 b;}bn;
64bn.n=lua_tonumber(L,idx)+6755399441055744.0;
65if (bn.n==0.0&&!lua_isnumber(L,idx))luaL_typerror(L,idx,"number");
66return(UB)bn.b;
67}
68#define BRET(b) lua_pushnumber(L,(lua_Number)(int)(b));return 1;
69static int tobit(lua_State *L){
70BRET(barg(L,1))}
71static int bnot(lua_State *L){
72BRET(~barg(L,1))}
73static int band(lua_State *L){
74int i;UB b=barg(L,1);for(i=lua_gettop(L);i>1;i--)b&=barg(L,i);BRET(b)}
75static int bor(lua_State *L){
76int i;UB b=barg(L,1);for(i=lua_gettop(L);i>1;i--)b|=barg(L,i);BRET(b)}
77static int bxor(lua_State *L){
78int i;UB b=barg(L,1);for(i=lua_gettop(L);i>1;i--)b^=barg(L,i);BRET(b)}
79static int lshift(lua_State *L){
80UB b=barg(L,1),n=barg(L,2)&31;BRET(b<<n)}
81static int rshift(lua_State *L){
82UB b=barg(L,1),n=barg(L,2)&31;BRET(b>>n)}
83static int arshift(lua_State *L){
84UB b=barg(L,1),n=barg(L,2)&31;BRET((int)b>>n)}
85static int rol(lua_State *L){
86UB b=barg(L,1),n=barg(L,2)&31;BRET((b<<n)|(b>>(32-n)))}
87static int ror(lua_State *L){
88UB b=barg(L,1),n=barg(L,2)&31;BRET((b>>n)|(b<<(32-n)))}
89static int bswap(lua_State *L){
90UB b=barg(L,1);b=(b>>24)|((b>>8)&0xff00)|((b&0xff00)<<8)|(b<<24);BRET(b)}
91static int tohex(lua_State *L){
92UB b=barg(L,1);
93int n=lua_isnone(L,2)?8:(int)barg(L,2);
94const char *hexdigits="0123456789abcdef";
95char buf[8];
96int i;
97if(n<0){n=-n;hexdigits="0123456789ABCDEF";}
98if(n>8)n=8;
99for(i=(int)n;--i>=0;){buf[i]=hexdigits[b&15];b>>=4;}
100lua_pushlstring(L,buf,(size_t)n);
101return 1;
102}
103static const struct luaL_Reg bitlib[] = {
104{"tobit",tobit},
105{"bnot",bnot},
106{"band",band},
107{"bor",bor},
108{"bxor",bxor},
109{"lshift",lshift},
110{"rshift",rshift},
111{"arshift",arshift},
112{"rol",rol},
113{"ror",ror},
114{"bswap",bswap},
115{"tohex",tohex},
116{NULL,NULL}
117};
61int main(int argc, char **argv){ 118int main(int argc, char **argv){
62 lua_State *L = luaL_newstate(); 119 lua_State *L = luaL_newstate();
63 int i; 120 int i;
64 luaL_openlibs(L); 121 luaL_openlibs(L);
122 luaL_register(L, "bit", bitlib);
65 if (argc < 2) return sizeof(void *); 123 if (argc < 2) return sizeof(void *);
66 lua_createtable(L, 0, 1); 124 lua_createtable(L, 0, 1);
67 lua_pushstring(L, argv[1]); 125 lua_pushstring(L, argv[1]);
@@ -146,7 +204,13 @@ local function def_istrue(def)
146 def == "UNUSED" 204 def == "UNUSED"
147end 205end
148 206
149local head, defs = {}, {} 207local head, defs = {[[
208#ifdef _MSC_VER
209typedef unsigned __int64 U64;
210#else
211typedef unsigned long long U64;
212#endif
213]]}, {}
150 214
151local function preprocess(src) 215local function preprocess(src)
152 local t = { match(src, "^(.-)#") } 216 local t = { match(src, "^(.-)#") }