aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMike Pall <mike>2012-07-08 16:24:53 +0200
committerMike Pall <mike>2012-07-08 16:24:53 +0200
commite3dec0438d50dbdf0184078d4e0233e399725787 (patch)
treec6bb83ac81679b5904a909f301852f36a873d63d /src
parent6a67fa8a4b2b038e0dca676303545da47f2b9773 (diff)
downloadluajit-e3dec0438d50dbdf0184078d4e0233e399725787.tar.gz
luajit-e3dec0438d50dbdf0184078d4e0233e399725787.tar.bz2
luajit-e3dec0438d50dbdf0184078d4e0233e399725787.zip
Add Lua BitOp to minilua.
Diffstat (limited to 'src')
-rw-r--r--src/host/genminilua.lua66
-rw-r--r--src/host/minilua.c63
2 files changed, 128 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, "^(.-)#") }
diff --git a/src/host/minilua.c b/src/host/minilua.c
index 77e6349c..93e9273e 100644
--- a/src/host/minilua.c
+++ b/src/host/minilua.c
@@ -22,6 +22,11 @@
22* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 22* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 23* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24******************************************************************************/ 24******************************************************************************/
25#ifdef _MSC_VER
26typedef unsigned __int64 U64;
27#else
28typedef unsigned long long U64;
29#endif
25#include <stddef.h> 30#include <stddef.h>
26#include <stdarg.h> 31#include <stdarg.h>
27#include <limits.h> 32#include <limits.h>
@@ -7683,10 +7688,68 @@ lua_pushstring(L,lib->name);
7683lua_call(L,1,0); 7688lua_call(L,1,0);
7684} 7689}
7685} 7690}
7691typedef unsigned int UB;
7692static UB barg(lua_State*L,int idx){
7693union{lua_Number n;U64 b;}bn;
7694bn.n=lua_tonumber(L,idx)+6755399441055744.0;
7695if(bn.n==0.0&&!lua_isnumber(L,idx))luaL_typerror(L,idx,"number");
7696return(UB)bn.b;
7697}
7698#define BRET(b)lua_pushnumber(L,(lua_Number)(int)(b));return 1;
7699static int tobit(lua_State*L){
7700BRET(barg(L,1))}
7701static int bnot(lua_State*L){
7702BRET(~barg(L,1))}
7703static int band(lua_State*L){
7704int i;UB b=barg(L,1);for(i=lua_gettop(L);i>1;i--)b&=barg(L,i);BRET(b)}
7705static int bor(lua_State*L){
7706int i;UB b=barg(L,1);for(i=lua_gettop(L);i>1;i--)b|=barg(L,i);BRET(b)}
7707static int bxor(lua_State*L){
7708int i;UB b=barg(L,1);for(i=lua_gettop(L);i>1;i--)b^=barg(L,i);BRET(b)}
7709static int lshift(lua_State*L){
7710UB b=barg(L,1),n=barg(L,2)&31;BRET(b<<n)}
7711static int rshift(lua_State*L){
7712UB b=barg(L,1),n=barg(L,2)&31;BRET(b>>n)}
7713static int arshift(lua_State*L){
7714UB b=barg(L,1),n=barg(L,2)&31;BRET((int)b>>n)}
7715static int rol(lua_State*L){
7716UB b=barg(L,1),n=barg(L,2)&31;BRET((b<<n)|(b>>(32-n)))}
7717static int ror(lua_State*L){
7718UB b=barg(L,1),n=barg(L,2)&31;BRET((b>>n)|(b<<(32-n)))}
7719static int bswap(lua_State*L){
7720UB b=barg(L,1);b=(b>>24)|((b>>8)&0xff00)|((b&0xff00)<<8)|(b<<24);BRET(b)}
7721static int tohex(lua_State*L){
7722UB b=barg(L,1);
7723int n=lua_isnone(L,2)?8:(int)barg(L,2);
7724const char*hexdigits="0123456789abcdef";
7725char buf[8];
7726int i;
7727if(n<0){n=-n;hexdigits="0123456789ABCDEF";}
7728if(n>8)n=8;
7729for(i=(int)n;--i>=0;){buf[i]=hexdigits[b&15];b>>=4;}
7730lua_pushlstring(L,buf,(size_t)n);
7731return 1;
7732}
7733static const struct luaL_Reg bitlib[]={
7734{"tobit",tobit},
7735{"bnot",bnot},
7736{"band",band},
7737{"bor",bor},
7738{"bxor",bxor},
7739{"lshift",lshift},
7740{"rshift",rshift},
7741{"arshift",arshift},
7742{"rol",rol},
7743{"ror",ror},
7744{"bswap",bswap},
7745{"tohex",tohex},
7746{NULL,NULL}
7747};
7686int main(int argc,char**argv){ 7748int main(int argc,char**argv){
7687lua_State*L=luaL_newstate(); 7749lua_State*L=luaL_newstate();
7688int i; 7750int i;
7689luaL_openlibs(L); 7751luaL_openlibs(L);
7752luaL_register(L,"bit",bitlib);
7690if(argc<2)return sizeof(void*); 7753if(argc<2)return sizeof(void*);
7691lua_createtable(L,0,1); 7754lua_createtable(L,0,1);
7692lua_pushstring(L,argv[1]); 7755lua_pushstring(L,argv[1]);