diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/host/genminilua.lua | 66 | ||||
| -rw-r--r-- | src/host/minilua.c | 63 |
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) | |||
| 58 | local REMOVE_EXTINC = { ["<assert.h>"] = true, ["<locale.h>"] = true, } | 58 | local REMOVE_EXTINC = { ["<assert.h>"] = true, ["<locale.h>"] = true, } |
| 59 | 59 | ||
| 60 | local CUSTOM_MAIN = [[ | 60 | local CUSTOM_MAIN = [[ |
| 61 | typedef unsigned int UB; | ||
| 62 | static UB barg(lua_State *L,int idx){ | ||
| 63 | union{lua_Number n;U64 b;}bn; | ||
| 64 | bn.n=lua_tonumber(L,idx)+6755399441055744.0; | ||
| 65 | if (bn.n==0.0&&!lua_isnumber(L,idx))luaL_typerror(L,idx,"number"); | ||
| 66 | return(UB)bn.b; | ||
| 67 | } | ||
| 68 | #define BRET(b) lua_pushnumber(L,(lua_Number)(int)(b));return 1; | ||
| 69 | static int tobit(lua_State *L){ | ||
| 70 | BRET(barg(L,1))} | ||
| 71 | static int bnot(lua_State *L){ | ||
| 72 | BRET(~barg(L,1))} | ||
| 73 | static int band(lua_State *L){ | ||
| 74 | int i;UB b=barg(L,1);for(i=lua_gettop(L);i>1;i--)b&=barg(L,i);BRET(b)} | ||
| 75 | static int bor(lua_State *L){ | ||
| 76 | int i;UB b=barg(L,1);for(i=lua_gettop(L);i>1;i--)b|=barg(L,i);BRET(b)} | ||
| 77 | static int bxor(lua_State *L){ | ||
| 78 | int i;UB b=barg(L,1);for(i=lua_gettop(L);i>1;i--)b^=barg(L,i);BRET(b)} | ||
| 79 | static int lshift(lua_State *L){ | ||
| 80 | UB b=barg(L,1),n=barg(L,2)&31;BRET(b<<n)} | ||
| 81 | static int rshift(lua_State *L){ | ||
| 82 | UB b=barg(L,1),n=barg(L,2)&31;BRET(b>>n)} | ||
| 83 | static int arshift(lua_State *L){ | ||
| 84 | UB b=barg(L,1),n=barg(L,2)&31;BRET((int)b>>n)} | ||
| 85 | static int rol(lua_State *L){ | ||
| 86 | UB b=barg(L,1),n=barg(L,2)&31;BRET((b<<n)|(b>>(32-n)))} | ||
| 87 | static int ror(lua_State *L){ | ||
| 88 | UB b=barg(L,1),n=barg(L,2)&31;BRET((b>>n)|(b<<(32-n)))} | ||
| 89 | static int bswap(lua_State *L){ | ||
| 90 | UB b=barg(L,1);b=(b>>24)|((b>>8)&0xff00)|((b&0xff00)<<8)|(b<<24);BRET(b)} | ||
| 91 | static int tohex(lua_State *L){ | ||
| 92 | UB b=barg(L,1); | ||
| 93 | int n=lua_isnone(L,2)?8:(int)barg(L,2); | ||
| 94 | const char *hexdigits="0123456789abcdef"; | ||
| 95 | char buf[8]; | ||
| 96 | int i; | ||
| 97 | if(n<0){n=-n;hexdigits="0123456789ABCDEF";} | ||
| 98 | if(n>8)n=8; | ||
| 99 | for(i=(int)n;--i>=0;){buf[i]=hexdigits[b&15];b>>=4;} | ||
| 100 | lua_pushlstring(L,buf,(size_t)n); | ||
| 101 | return 1; | ||
| 102 | } | ||
| 103 | static 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 | }; | ||
| 61 | int main(int argc, char **argv){ | 118 | int 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" |
| 147 | end | 205 | end |
| 148 | 206 | ||
| 149 | local head, defs = {}, {} | 207 | local head, defs = {[[ |
| 208 | #ifdef _MSC_VER | ||
| 209 | typedef unsigned __int64 U64; | ||
| 210 | #else | ||
| 211 | typedef unsigned long long U64; | ||
| 212 | #endif | ||
| 213 | ]]}, {} | ||
| 150 | 214 | ||
| 151 | local function preprocess(src) | 215 | local 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 | ||
| 26 | typedef unsigned __int64 U64; | ||
| 27 | #else | ||
| 28 | typedef 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); | |||
| 7683 | lua_call(L,1,0); | 7688 | lua_call(L,1,0); |
| 7684 | } | 7689 | } |
| 7685 | } | 7690 | } |
| 7691 | typedef unsigned int UB; | ||
| 7692 | static UB barg(lua_State*L,int idx){ | ||
| 7693 | union{lua_Number n;U64 b;}bn; | ||
| 7694 | bn.n=lua_tonumber(L,idx)+6755399441055744.0; | ||
| 7695 | if(bn.n==0.0&&!lua_isnumber(L,idx))luaL_typerror(L,idx,"number"); | ||
| 7696 | return(UB)bn.b; | ||
| 7697 | } | ||
| 7698 | #define BRET(b)lua_pushnumber(L,(lua_Number)(int)(b));return 1; | ||
| 7699 | static int tobit(lua_State*L){ | ||
| 7700 | BRET(barg(L,1))} | ||
| 7701 | static int bnot(lua_State*L){ | ||
| 7702 | BRET(~barg(L,1))} | ||
| 7703 | static int band(lua_State*L){ | ||
| 7704 | int i;UB b=barg(L,1);for(i=lua_gettop(L);i>1;i--)b&=barg(L,i);BRET(b)} | ||
| 7705 | static int bor(lua_State*L){ | ||
| 7706 | int i;UB b=barg(L,1);for(i=lua_gettop(L);i>1;i--)b|=barg(L,i);BRET(b)} | ||
| 7707 | static int bxor(lua_State*L){ | ||
| 7708 | int i;UB b=barg(L,1);for(i=lua_gettop(L);i>1;i--)b^=barg(L,i);BRET(b)} | ||
| 7709 | static int lshift(lua_State*L){ | ||
| 7710 | UB b=barg(L,1),n=barg(L,2)&31;BRET(b<<n)} | ||
| 7711 | static int rshift(lua_State*L){ | ||
| 7712 | UB b=barg(L,1),n=barg(L,2)&31;BRET(b>>n)} | ||
| 7713 | static int arshift(lua_State*L){ | ||
| 7714 | UB b=barg(L,1),n=barg(L,2)&31;BRET((int)b>>n)} | ||
| 7715 | static int rol(lua_State*L){ | ||
| 7716 | UB b=barg(L,1),n=barg(L,2)&31;BRET((b<<n)|(b>>(32-n)))} | ||
| 7717 | static int ror(lua_State*L){ | ||
| 7718 | UB b=barg(L,1),n=barg(L,2)&31;BRET((b>>n)|(b<<(32-n)))} | ||
| 7719 | static int bswap(lua_State*L){ | ||
| 7720 | UB b=barg(L,1);b=(b>>24)|((b>>8)&0xff00)|((b&0xff00)<<8)|(b<<24);BRET(b)} | ||
| 7721 | static int tohex(lua_State*L){ | ||
| 7722 | UB b=barg(L,1); | ||
| 7723 | int n=lua_isnone(L,2)?8:(int)barg(L,2); | ||
| 7724 | const char*hexdigits="0123456789abcdef"; | ||
| 7725 | char buf[8]; | ||
| 7726 | int i; | ||
| 7727 | if(n<0){n=-n;hexdigits="0123456789ABCDEF";} | ||
| 7728 | if(n>8)n=8; | ||
| 7729 | for(i=(int)n;--i>=0;){buf[i]=hexdigits[b&15];b>>=4;} | ||
| 7730 | lua_pushlstring(L,buf,(size_t)n); | ||
| 7731 | return 1; | ||
| 7732 | } | ||
| 7733 | static 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 | }; | ||
| 7686 | int main(int argc,char**argv){ | 7748 | int main(int argc,char**argv){ |
| 7687 | lua_State*L=luaL_newstate(); | 7749 | lua_State*L=luaL_newstate(); |
| 7688 | int i; | 7750 | int i; |
| 7689 | luaL_openlibs(L); | 7751 | luaL_openlibs(L); |
| 7752 | luaL_register(L,"bit",bitlib); | ||
| 7690 | if(argc<2)return sizeof(void*); | 7753 | if(argc<2)return sizeof(void*); |
| 7691 | lua_createtable(L,0,1); | 7754 | lua_createtable(L,0,1); |
| 7692 | lua_pushstring(L,argv[1]); | 7755 | lua_pushstring(L,argv[1]); |
