Pārlūkot izejas kodu

update: 完善bsp/mini,可以用msvc进行编译了,并添加luac,完工

Wendal Chen 2 gadi atpakaļ
vecāks
revīzija
f52887c6e7

+ 1 - 0
bsp/mini/build_linux_32bit.sh

@@ -3,5 +3,6 @@
 # apt install gcc-multilib apt install g++-multilib
 
 xmake clean -a
+export VM_64bit=0
 xmake f -p linux  -a i386
 xmake -w

+ 2 - 1
bsp/mini/build_linux_64bit.sh

@@ -1,3 +1,4 @@
 xmake clean -a
-xmake f -p linux  -a x86_64
+export VM_64bit=1
+xmake f -p linux  -a i386
 xmake -w

+ 0 - 4
bsp/mini/build_windows_32bit.bat

@@ -1,4 +0,0 @@
-
-xmake clean -a
-xmake f -p mingw --sdk=C:\msys64\mingw32 -a i386
-xmake

+ 5 - 0
bsp/mini/build_windows_32bit_msvc.bat

@@ -0,0 +1,5 @@
+
+xmake clean -a
+set VM_64bit=0
+xmake f -a x86
+xmake -w -v

+ 0 - 4
bsp/mini/build_windows_64bit.bat

@@ -1,4 +0,0 @@
-
-xmake clean -a
-xmake f -p mingw --sdk=C:\msys64\mingw32 -a x86_64
-xmake

+ 5 - 0
bsp/mini/build_windows_64bit_msvc.bat

@@ -0,0 +1,5 @@
+
+xmake clean -a
+set VM_64bit=1
+xmake f -a x86
+xmake -w -v

+ 1 - 1
bsp/mini/include/luat_conf_bsp.h

@@ -7,7 +7,7 @@
 #define LUAT_BSP_VERSION "V1005"
 #define LUAT_USE_CMDLINE_ARGS 1
 // 启用64位虚拟机
-#define LUAT_CONF_VM_64bit
+// #define LUAT_CONF_VM_64bit
 #define LUAT_RTOS_API_NOTOK 1
 #define LUAT_RET int
 #define LUAT_RT_RET_TYPE	void

+ 450 - 0
bsp/mini/src/luac.c

@@ -0,0 +1,450 @@
+/*
+** $Id: luac.c,v 1.76 2018/06/19 01:32:02 lhf Exp $
+** Lua compiler (saves bytecodes to files; also lists bytecodes)
+** See Copyright Notice in lua.h
+*/
+
+#define luac_c
+#define LUA_CORE
+
+#include "lprefix.h"
+
+#include <ctype.h>
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "lua.h"
+#include "lauxlib.h"
+
+#include "lobject.h"
+#include "lstate.h"
+#include "lundump.h"
+
+static void PrintFunction(const Proto* f, int full);
+#define luaU_print	PrintFunction
+
+#define PROGNAME	"luac"		/* default program name */
+#define OUTPUT		PROGNAME ".out"	/* default output file */
+
+static int listing=0;			/* list bytecodes? */
+static int dumping=1;			/* dump bytecodes? */
+static int stripping=0;			/* strip debug information? */
+static char Output[]={ OUTPUT };	/* default output file name */
+static const char* output=Output;	/* actual output file name */
+static const char* progname=PROGNAME;	/* actual program name */
+
+static void fatal(const char* message)
+{
+ fprintf(stderr,"%s: %s\n",progname,message);
+ exit(EXIT_FAILURE);
+}
+
+static void cannot(const char* what)
+{
+ fprintf(stderr,"%s: cannot %s %s: %s\n",progname,what,output,strerror(errno));
+ exit(EXIT_FAILURE);
+}
+
+static void usage(const char* message)
+{
+ if (*message=='-')
+  fprintf(stderr,"%s: unrecognized option '%s'\n",progname,message);
+ else
+  fprintf(stderr,"%s: %s\n",progname,message);
+ fprintf(stderr,
+  "usage: %s [options] [filenames]\n"
+  "Available options are:\n"
+  "  -l       list (use -l -l for full listing)\n"
+  "  -o name  output to file 'name' (default is \"%s\")\n"
+  "  -p       parse only\n"
+  "  -s       strip debug information\n"
+  "  -v       show version information\n"
+  "  --       stop handling options\n"
+  "  -        stop handling options and process stdin\n"
+  ,progname,Output);
+ exit(EXIT_FAILURE);
+}
+
+#define IS(s)	(strcmp(argv[i],s)==0)
+
+static int doargs(int argc, char* argv[])
+{
+ int i;
+ int version=0;
+ if (argv[0]!=NULL && *argv[0]!=0) progname=argv[0];
+ for (i=1; i<argc; i++)
+ {
+  if (*argv[i]!='-')			/* end of options; keep it */
+   break;
+  else if (IS("--"))			/* end of options; skip it */
+  {
+   ++i;
+   if (version) ++version;
+   break;
+  }
+  else if (IS("-"))			/* end of options; use stdin */
+   break;
+  else if (IS("-l"))			/* list */
+   ++listing;
+  else if (IS("-o"))			/* output file */
+  {
+   output=argv[++i];
+   if (output==NULL || *output==0 || (*output=='-' && output[1]!=0))
+    usage("'-o' needs argument");
+   if (IS("-")) output=NULL;
+  }
+  else if (IS("-p"))			/* parse only */
+   dumping=0;
+  else if (IS("-s"))			/* strip debug information */
+   stripping=1;
+  else if (IS("-v"))			/* show version */
+   ++version;
+  else					/* unknown option */
+   usage(argv[i]);
+ }
+ if (i==argc && (listing || !dumping))
+ {
+  dumping=0;
+  argv[--i]=Output;
+ }
+ if (version)
+ {
+  printf("%s\n",LUA_COPYRIGHT);
+  if (version==argc-1) exit(EXIT_SUCCESS);
+ }
+ return i;
+}
+
+#define FUNCTION "(function()end)();"
+
+static const char* reader(lua_State *L, void *ud, size_t *size)
+{
+ UNUSED(L);
+ if ((*(int*)ud)--)
+ {
+  *size=sizeof(FUNCTION)-1;
+  return FUNCTION;
+ }
+ else
+ {
+  *size=0;
+  return NULL;
+ }
+}
+
+#define toproto(L,i) getproto(L->top+(i))
+
+static const Proto* combine(lua_State* L, int n)
+{
+ if (n==1)
+  return toproto(L,-1);
+ else
+ {
+  Proto* f;
+  int i=n;
+  if (lua_load(L,reader,&i,"=(" PROGNAME ")",NULL)!=LUA_OK) fatal(lua_tostring(L,-1));
+  f=toproto(L,-1);
+  for (i=0; i<n; i++)
+  {
+   f->p[i]=toproto(L,i-n-1);
+   if (f->p[i]->sizeupvalues>0) f->p[i]->upvalues[0].instack=0;
+  }
+  f->sizelineinfo=0;
+  return f;
+ }
+}
+
+static int writer(lua_State* L, const void* p, size_t size, void* u)
+{
+ UNUSED(L);
+ return (fwrite(p,size,1,(FILE*)u)!=1) && (size!=0);
+}
+
+static int pmain(lua_State* L)
+{
+ int argc=(int)lua_tointeger(L,1);
+ char** argv=(char**)lua_touserdata(L,2);
+ const Proto* f;
+ int i;
+ if (!lua_checkstack(L,argc)) fatal("too many input files");
+ for (i=0; i<argc; i++)
+ {
+  const char* filename=IS("-") ? NULL : argv[i];
+  if (luaL_loadfile(L,filename)!=LUA_OK) fatal(lua_tostring(L,-1));
+ }
+ f=combine(L,argc);
+ if (listing) luaU_print(f,listing>1);
+ if (dumping)
+ {
+  FILE* D= (output==NULL) ? stdout : fopen(output,"wb");
+  if (D==NULL) cannot("open");
+  lua_lock(L);
+  luaU_dump(L,f,writer,D,stripping);
+  lua_unlock(L);
+  if (ferror(D)) cannot("write");
+  if (fclose(D)) cannot("close");
+ }
+ return 0;
+}
+void* luat_heap_alloc(void *ud, void *ptr, size_t osize, size_t nsize);
+int luac_main(int argc, char* argv[])
+{
+ lua_State* L;
+ int i=doargs(argc,argv);
+ argc-=i; argv+=i;
+ if (argc<=0) usage("no input files given");
+ L = lua_newstate(luat_heap_alloc, NULL);;  /* create state */
+ if (L==NULL) fatal("cannot create state: not enough memory");
+ lua_pushcfunction(L,&pmain);
+ lua_pushinteger(L,argc);
+ lua_pushlightuserdata(L,argv);
+ if (lua_pcall(L,2,0,0)!=LUA_OK) fatal(lua_tostring(L,-1));
+ lua_close(L);
+ return EXIT_SUCCESS;
+}
+
+/*
+** $Id: luac.c,v 1.76 2018/06/19 01:32:02 lhf Exp $
+** print bytecodes
+** See Copyright Notice in lua.h
+*/
+
+#include <ctype.h>
+#include <stdio.h>
+
+#define luac_c
+#define LUA_CORE
+
+#include "ldebug.h"
+#include "lobject.h"
+#include "lopcodes.h"
+
+#define VOID(p)		((const void*)(p))
+
+static void PrintString(const TString* ts)
+{
+ const char* s=getstr(ts);
+ size_t i,n=tsslen(ts);
+ printf("%c",'"');
+ for (i=0; i<n; i++)
+ {
+  int c=(int)(unsigned char)s[i];
+  switch (c)
+  {
+   case '"':  printf("\\\""); break;
+   case '\\': printf("\\\\"); break;
+   case '\a': printf("\\a"); break;
+   case '\b': printf("\\b"); break;
+   case '\f': printf("\\f"); break;
+   case '\n': printf("\\n"); break;
+   case '\r': printf("\\r"); break;
+   case '\t': printf("\\t"); break;
+   case '\v': printf("\\v"); break;
+   default:	if (isprint(c))
+   			printf("%c",c);
+		else
+			printf("\\%03d",c);
+  }
+ }
+ printf("%c",'"');
+}
+
+static void PrintConstant(const Proto* f, int i)
+{
+ const TValue* o=&f->k[i];
+ switch (ttype(o))
+ {
+  case LUA_TNIL:
+	printf("nil");
+	break;
+  case LUA_TBOOLEAN:
+	printf(bvalue(o) ? "true" : "false");
+	break;
+  case LUA_TNUMFLT:
+	{
+	char buff[100];
+	sprintf(buff,LUA_NUMBER_FMT,fltvalue(o));
+	printf("%s",buff);
+	if (buff[strspn(buff,"-0123456789")]=='\0') printf(".0");
+	break;
+	}
+  case LUA_TNUMINT:
+	printf(LUA_INTEGER_FMT,ivalue(o));
+	break;
+  case LUA_TSHRSTR: case LUA_TLNGSTR:
+	PrintString(tsvalue(o));
+	break;
+  default:				/* cannot happen */
+	printf("? type=%d",ttype(o));
+	break;
+ }
+}
+
+#define UPVALNAME(x) ((f->upvalues[x].name) ? getstr(f->upvalues[x].name) : "-")
+#define MYK(x)		(-1-(x))
+
+static void PrintCode(const Proto* f)
+{
+ const Instruction* code=f->code;
+ int pc,n=f->sizecode;
+ for (pc=0; pc<n; pc++)
+ {
+  Instruction i=code[pc];
+  OpCode o=GET_OPCODE(i);
+  int a=GETARG_A(i);
+  int b=GETARG_B(i);
+  int c=GETARG_C(i);
+  int ax=GETARG_Ax(i);
+  int bx=GETARG_Bx(i);
+  int sbx=GETARG_sBx(i);
+  int line=getfuncline(f,pc);
+  printf("\t%d\t",pc+1);
+  if (line>0) printf("[%d]\t",line); else printf("[-]\t");
+  printf("%-9s\t",luaP_opnames[o]);
+  switch (getOpMode(o))
+  {
+   case iABC:
+    printf("%d",a);
+    if (getBMode(o)!=OpArgN) printf(" %d",ISK(b) ? (MYK(INDEXK(b))) : b);
+    if (getCMode(o)!=OpArgN) printf(" %d",ISK(c) ? (MYK(INDEXK(c))) : c);
+    break;
+   case iABx:
+    printf("%d",a);
+    if (getBMode(o)==OpArgK) printf(" %d",MYK(bx));
+    if (getBMode(o)==OpArgU) printf(" %d",bx);
+    break;
+   case iAsBx:
+    printf("%d %d",a,sbx);
+    break;
+   case iAx:
+    printf("%d",MYK(ax));
+    break;
+  }
+  switch (o)
+  {
+   case OP_LOADK:
+    printf("\t; "); PrintConstant(f,bx);
+    break;
+   case OP_GETUPVAL:
+   case OP_SETUPVAL:
+    printf("\t; %s",UPVALNAME(b));
+    break;
+   case OP_GETTABUP:
+    printf("\t; %s",UPVALNAME(b));
+    if (ISK(c)) { printf(" "); PrintConstant(f,INDEXK(c)); }
+    break;
+   case OP_SETTABUP:
+    printf("\t; %s",UPVALNAME(a));
+    if (ISK(b)) { printf(" "); PrintConstant(f,INDEXK(b)); }
+    if (ISK(c)) { printf(" "); PrintConstant(f,INDEXK(c)); }
+    break;
+   case OP_GETTABLE:
+   case OP_SELF:
+    if (ISK(c)) { printf("\t; "); PrintConstant(f,INDEXK(c)); }
+    break;
+   case OP_SETTABLE:
+   case OP_ADD:
+   case OP_SUB:
+   case OP_MUL:
+   case OP_MOD:
+   case OP_POW:
+   case OP_DIV:
+   case OP_IDIV:
+   case OP_BAND:
+   case OP_BOR:
+   case OP_BXOR:
+   case OP_SHL:
+   case OP_SHR:
+   case OP_EQ:
+   case OP_LT:
+   case OP_LE:
+    if (ISK(b) || ISK(c))
+    {
+     printf("\t; ");
+     if (ISK(b)) PrintConstant(f,INDEXK(b)); else printf("-");
+     printf(" ");
+     if (ISK(c)) PrintConstant(f,INDEXK(c)); else printf("-");
+    }
+    break;
+   case OP_JMP:
+   case OP_FORLOOP:
+   case OP_FORPREP:
+   case OP_TFORLOOP:
+    printf("\t; to %d",sbx+pc+2);
+    break;
+   case OP_CLOSURE:
+    printf("\t; %p",VOID(f->p[bx]));
+    break;
+   case OP_SETLIST:
+    if (c==0) printf("\t; %d",(int)code[++pc]); else printf("\t; %d",c);
+    break;
+   case OP_EXTRAARG:
+    printf("\t; "); PrintConstant(f,ax);
+    break;
+   default:
+    break;
+  }
+  printf("\n");
+ }
+}
+
+#define SS(x)	((x==1)?"":"s")
+#define S(x)	(int)(x),SS(x)
+
+static void PrintHeader(const Proto* f)
+{
+ const char* s=f->source ? getstr(f->source) : "=?";
+ if (*s=='@' || *s=='=')
+  s++;
+ else if (*s==LUA_SIGNATURE[0])
+  s="(bstring)";
+ else
+  s="(string)";
+ printf("\n%s <%s:%d,%d> (%d instruction%s at %p)\n",
+ 	(f->linedefined==0)?"main":"function",s,
+	f->linedefined,f->lastlinedefined,
+	S(f->sizecode),VOID(f));
+ printf("%d%s param%s, %d slot%s, %d upvalue%s, ",
+	(int)(f->numparams),f->is_vararg?"+":"",SS(f->numparams),
+	S(f->maxstacksize),S(f->sizeupvalues));
+ printf("%d local%s, %d constant%s, %d function%s\n",
+	S(f->sizelocvars),S(f->sizek),S(f->sizep));
+}
+
+static void PrintDebug(const Proto* f)
+{
+ int i,n;
+ n=f->sizek;
+ printf("constants (%d) for %p:\n",n,VOID(f));
+ for (i=0; i<n; i++)
+ {
+  printf("\t%d\t",i+1);
+  PrintConstant(f,i);
+  printf("\n");
+ }
+ n=f->sizelocvars;
+ printf("locals (%d) for %p:\n",n,VOID(f));
+ for (i=0; i<n; i++)
+ {
+  printf("\t%d\t%s\t%d\t%d\n",
+  i,getstr(f->locvars[i].varname),f->locvars[i].startpc+1,f->locvars[i].endpc+1);
+ }
+ n=f->sizeupvalues;
+ printf("upvalues (%d) for %p:\n",n,VOID(f));
+ for (i=0; i<n; i++)
+ {
+  printf("\t%d\t%s\t%d\t%d\n",
+  i,UPVALNAME(i),f->upvalues[i].instack,f->upvalues[i].idx);
+ }
+}
+
+static void PrintFunction(const Proto* f, int full)
+{
+ int i,n=f->sizep;
+ PrintHeader(f);
+ PrintCode(f);
+ if (full) PrintDebug(f);
+ for (i=0; i<n; i++) PrintFunction(f->p[i],full);
+}

+ 10 - 13
bsp/mini/src/main_mini.c

@@ -23,15 +23,15 @@ int lua_main (int argc, char **argv);
 void luat_log_init_win32(void);
 void luat_uart_initial_win32(void);
 
-static void _luat_main(void* args) {
-    (void)args;
-    luat_fs_init();
-    lua_main(cmdline_argc, cmdline_argv);
-    exit(0);
-}
-
 // boot
-int main(int argc, char** argv) {
+int main(int argc, char** argv) {   
+    luat_log_init_win32();
+    bpool(luavm_heap, LUAT_HEAP_SIZE);
+    luat_fs_init();
+#ifdef LUAT_USE_LUAC
+    extern int luac_main(int argc, char* argv[]);
+    luac_main(argc, argv);
+#else
     cmdline_argc = argc;
     cmdline_argv = argv;
     if (cmdline_argc > 1) {
@@ -43,10 +43,7 @@ int main(int argc, char** argv) {
             }
         }
     }
-    
-    luat_log_init_win32();
-
-    bpool(luavm_heap, LUAT_HEAP_SIZE);
-    _luat_main(NULL);
+    lua_main(cmdline_argc, cmdline_argv);
+#endif
     return 0;
 }

+ 41 - 13
bsp/mini/xmake.lua

@@ -16,34 +16,62 @@ add_defines("__LUATOS__", "__XMAKE_BUILD__")
 add_defines("MBEDTLS_CONFIG_FILE=\"mbedtls_config_mini.h\"")
 
 --add_ldflags("-Wl,-gc-sections")
+if os.getenv("VM_64bit") == "1" then
+    add_defines("LUAT_CONF_VM_64bit")
+end
 
 if is_host("windows") then
-    add_defines("LUA_USE_WINDOWS")
-    add_ldflags("-static")
-elseif is_host("linux") then
-    add_defines("LUA_USE_LINUX")
-elseif is_host("macos") then
-    add_defines("LUA_USE_MACOSX")
+    -- add_defines("LUA_USE_WINDOWS")
+    add_cflags("/utf-8")
+    -- add_ldflags("-static")
+-- elseif is_host("linux") then
+--     add_defines("LUA_USE_LINUX")
+-- elseif is_host("macos") then
+--     add_defines("LUA_USE_MACOSX")
 end
 
-target("luatos")
+
+add_includedirs("include",{public = true})
+add_includedirs(luatos.."lua/include",{public = true})
+add_includedirs(luatos.."luat/include",{public = true})
+
+
+
+target("luatos-lua")
     -- set kind
     set_kind("binary")
     set_targetdir("$(buildir)/out")
-    
-    -- add deps
 
     add_files("src/*.c",{public = true})
+    add_deps("luatos")
+target_end()
+
+
+
+target("luatos-luac")
+    -- set kind
+    set_kind("binary")
+    set_targetdir("$(buildir)/out")
+
+    add_files("src/*.c",{public = true})
+    add_deps("luatos")
+    add_defines("LUAT_USE_LUAC")
+target_end()
+
+
+target("luatos")
+    -- set kind
+    set_kind("static")
+    set_targetdir("$(buildir)/out")
+    
+    -- add deps
     add_files("port/*.c",{public = true})
-    add_includedirs("include",{public = true})
 
-    add_includedirs(luatos.."lua/include",{public = true})
     add_files(luatos.."lua/src/*.c")
     -- printf
     add_includedirs(luatos.."components/printf",{public = true})
     add_files(luatos.."components/printf/*.c")
     
-    add_includedirs(luatos.."luat/include",{public = true})
     -- add_files(luatos.."luat/modules/*.c")
 
     add_files(luatos.."luat/modules/crc.c"
@@ -77,7 +105,7 @@ target("luatos")
     add_files(luatos.."components/mbedtls/library/*.c")
     add_includedirs(luatos.."components/mbedtls/include")
     -- iotauth
-    -- add_files(luatos.."components/iotauth/luat_lib_iotauth.c")
+    add_files(luatos.."components/iotauth/luat_lib_iotauth.c")
     -- crypto
     add_files(luatos.."components/crypto/**.c")
     -- protobuf

+ 1 - 1
components/lua-cjson/lua_cjson.c

@@ -68,7 +68,7 @@ int strncasecmp(const char *string1, const char *string2, size_t count);
 
 static char float_fmt[12];
 
-#ifdef WIN32
+#ifdef LUA_USE_WINDOWS
 #define snprintf _snprintf
 typedef unsigned char u_char;
 

+ 1 - 1
lua/include/ldebug.h

@@ -14,7 +14,7 @@
 #define pcRel(pc, p)	(cast(int, (pc) - (p)->code) - 1)
 
 // #define getfuncline(f,pc)	(((f)->lineinfo) ? (f)->lineinfo[pc] : -1)
-
+int getfuncline(Proto *p, int pc);
 #define resethookcount(L)	(L->hookcount = L->basehookcount)
 
 

+ 1 - 1
lua/src/ldebug.c

@@ -41,7 +41,7 @@
 static const char *funcnamefromcode (lua_State *L, CallInfo *ci,
                                     const char **name);
 
-static int getfuncline(Proto *p, int pc)	{
+int getfuncline(Proto *p, int pc)	{
   int tmp2 = -1;
   if (p->lineinfo) {
     memcpy(&tmp2, &p->lineinfo[pc], sizeof(tmp2));

+ 4 - 0
luat/include/luat_base.h

@@ -12,7 +12,11 @@
 // 调试开关, 预留
 #define LUAT_DEBUG 0
 
+#if !defined(LUA_USE_C89) && defined(_WIN32) && !defined(_WIN32_WCE)
+#define LUAT_WEAK
+#else
 #define LUAT_WEAK                     __attribute__((weak))
+#endif
 
 //-------------------------------
 // 通用头文件

+ 2 - 0
luat/vfs/luat_fs_posix.c

@@ -223,6 +223,8 @@ int luat_vfs_posix_umount(void* userdata, luat_fs_conf_t *conf) {
 #include <sys/types.h>
 #include <dirent.h>
 #include <sys/stat.h>
+#endif
+#if defined(LUA_USE_LINUX) || defined(LUA_USE_MACOSX)
 #include <unistd.h>
 #endif