From 80a12780aeb4fbfe7f9a555993aa4a97fd017467 Mon Sep 17 00:00:00 2001 From: Johny Mattsson Date: Thu, 23 Sep 2021 17:35:28 +1000 Subject: [PATCH] Lua tidy-up to reduce delta against esp32 branch. These should hopefully be completely uncontentious changes. There is still a delta against the esp32 branch, but to harmonise that requires a bit more work. This commit includes: - Some LUA_USE_xxx #ifdef changes, since in many ways the ESP32 is closer to a host build than an ESP8266 build. - A bunch of warnings tidy-ups. - A couple of readability/maintainability improvements (e.g. LROT_END definition using named member initialisation, prototype declarations moved to header file(s)). --- app/lua/lbaselib.c | 2 +- app/lua/ldebug.c | 3 ++- app/lua/lflash.c | 33 +++++++++++++++++++-------------- app/lua/llex.c | 1 + app/lua/lmathlib.c | 1 + app/lua/lnodemcu.c | 1 + app/lua/lnodemcu.h | 11 ++++++----- app/lua/loadlib.c | 1 + app/lua/lobject.c | 5 +---- app/lua/lobject.h | 2 +- app/lua/ltable.c | 2 ++ app/lua/ltablib.c | 1 + app/lua53/lauxlib.h | 9 +++++---- app/lua53/ldblib.c | 1 + app/lua53/ldump.c | 1 + app/lua53/lmathlib.c | 1 + app/lua53/lnodemcu.c | 20 ++++++++++++-------- app/lua53/loadlib.c | 2 +- app/lua53/lobject.c | 10 +++------- app/lua53/lobject.h | 2 +- app/lua53/lstate.c | 5 ++++- app/lua53/ltablib.c | 1 + app/lua53/lua.h | 2 ++ app/lua53/lutf8lib.c | 1 + 24 files changed, 70 insertions(+), 48 deletions(-) diff --git a/app/lua/lbaselib.c b/app/lua/lbaselib.c index 160e9640..8b8eceb5 100644 --- a/app/lua/lbaselib.c +++ b/app/lua/lbaselib.c @@ -24,7 +24,7 @@ ** model but changing `fputs' to put the strings at a proper place ** (a console window or a log file, for instance). */ -#ifdef LUA_CROSS_COMPILER +#if defined(LUA_USE_ESP8266) #undef puts #define puts(s) printf("%s",s) #endif diff --git a/app/lua/ldebug.c b/app/lua/ldebug.c index ca05727e..57bd376b 100644 --- a/app/lua/ldebug.c +++ b/app/lua/ldebug.c @@ -248,6 +248,7 @@ static int stripdebug (lua_State *L, Proto *f, int level) { f->packedlineinfo = luaM_freearray(L, f->packedlineinfo, sizepackedlineinfo, unsigned char); len += sizepackedlineinfo; } + // fall-through case 1: f->locvars = luaM_freearray(L, f->locvars, f->sizelocvars, struct LocVar); f->upvalues = luaM_freearray(L, f->upvalues, f->sizeupvalues, TString *); @@ -501,7 +502,7 @@ static Instruction symbexec (const Proto *pt, int lastpc, int reg) { case OP_FORLOOP: case OP_FORPREP: checkreg(pt, a+3); - /* go through */ + /* fall-through */ case OP_JMP: { int dest = pc+1+b; /* not full check and jump is forward and do not skip `lastpc'? */ diff --git a/app/lua/lflash.c b/app/lua/lflash.c index 6875106d..59c5b964 100644 --- a/app/lua/lflash.c +++ b/app/lua/lflash.c @@ -29,7 +29,7 @@ * process. */ -static char *flashAddr; +static const char *flashAddr; static uint32_t flashSize; static uint32_t flashAddrPhys; static uint32_t flashSector; @@ -106,20 +106,20 @@ LUA_API void dumpStrings(lua_State *L) { * writes are suppressed if the global writeToFlash is false. This is used in * phase I where the pass is used to size the structures in flash. */ -static char *flashPosition(void){ +static const char *flashPosition(void){ return flashAddr + curOffset; } -static char *flashSetPosition(uint32_t offset){ +static const char *flashSetPosition(uint32_t offset){ NODE_DBG("flashSetPosition(%04x)\n", offset); curOffset = offset; return flashPosition(); } -static char *flashBlock(const void* b, size_t size) { - void *cur = flashPosition(); +static const char *flashBlock(const void* b, size_t size) { + const void *cur = flashPosition(); NODE_DBG("flashBlock((%04x),%p,%04x)\n", curOffset,b,size); lua_assert(ALIGN_BITS(b) == 0 && ALIGN_BITS(size) == 0); platform_flash_write(b, flashAddrPhys+curOffset, size); @@ -137,6 +137,10 @@ static void flashErase(uint32_t start, uint32_t end){ platform_flash_erase_sector( flashSector + i ); } +static int loadLFS (lua_State *L); +static int loadLFSgc (lua_State *L); +static void procFirstPass (void); + /* ===================================================================================== * luaN_init() is exported via lflash.h. * The first is the startup hook used in lstate.c and the last two are @@ -171,7 +175,7 @@ LUAI_FUNC void luaN_init (lua_State *L) { } if ((fh->flash_sig & (~FLASH_SIG_ABSOLUTE)) != FLASH_SIG ) { - NODE_ERR("Flash sig not correct: 0x%08x vs 0x%08x\n", + NODE_ERR("LFS sig not correct: 0x%x vs expected 0x%x\n", fh->flash_sig & (~FLASH_SIG_ABSOLUTE), FLASH_SIG); return; } @@ -208,6 +212,7 @@ LUALIB_API void luaL_lfsreload (lua_State *L) { return; } + /* * Do a protected call of loadLFS. * @@ -346,13 +351,13 @@ static void put_byte (uint8_t value) { } -static uint8_t recall_byte (unsigned offset) { +static uint8_t recall_byte (uint32_t offset) { if(offset > DICTIONARY_WINDOW || offset >= out->ndx) flash_error("invalid dictionary offset on inflate"); /* ndx starts at 1. Need relative to 0 */ - unsigned n = out->ndx - offset; - unsigned pos = n % WRITE_BLOCKSIZE; - unsigned blockNo = out->ndx / WRITE_BLOCKSIZE - n / WRITE_BLOCKSIZE; + uint32_t n = out->ndx - offset; + uint32_t pos = n % WRITE_BLOCKSIZE; + uint32_t blockNo = out->ndx / WRITE_BLOCKSIZE - n / WRITE_BLOCKSIZE; return out->block[blockNo]->byte[pos]; } @@ -386,7 +391,7 @@ void procFirstPass (void) { fh->flash_size > flashSize || out->flagsLen != 1 + (out->flashLen/WORDSIZE - 1) / BITS_PER_WORD) flash_error("LFS length mismatch"); - out->flags = luaM_newvector(out->L, out->flagsLen, unsigned); + out->flags = luaM_newvector(out->L, out->flagsLen, uint32_t); } /* update running CRC */ @@ -412,7 +417,7 @@ void procSecondPass (void) { (out->flashLen % WRITE_BLOCKSIZE) / WORDSIZE : WRITE_BLOCKSIZE / WORDSIZE; uint32_t *buf = (uint32_t *) out->buffer.byte; - uint32_t flags = 0; + uint32_t flags = 0; /* * Relocate all the addresses tagged in out->flags. This can't be done in * place because the out->blocks are still in use as dictionary content so @@ -423,7 +428,7 @@ void procSecondPass (void) { if ((i&31)==0) flags = out->flags[out->flagsNdx++]; if (flags&1) - buf[i] = WORDSIZE*buf[i] + cast(uint32_t, flashAddr); + buf[i] = WORDSIZE*buf[i] + cast(uint32_t, flashAddr); // mapped, not phys } /* * On first block, set the flash_sig has the in progress bit set and this @@ -468,7 +473,7 @@ static int loadLFS (lua_State *L) { in->len = vfs_size(in->fd); if (in->len <= 200 || /* size of an empty luac output */ vfs_lseek(in->fd, in->len-4, VFS_SEEK_SET) != in->len-4 || - vfs_read(in->fd, &out->len, sizeof(unsigned)) != sizeof(unsigned)) + vfs_read(in->fd, &out->len, sizeof(uint32_t)) != sizeof(uint32_t)) flash_error("read error on LFS image file"); vfs_lseek(in->fd, 0, VFS_SEEK_SET); diff --git a/app/lua/llex.c b/app/lua/llex.c index 965f5a24..3c978c94 100644 --- a/app/lua/llex.c +++ b/app/lua/llex.c @@ -61,6 +61,7 @@ static void save (LexState *ls, int c) { void luaX_init (lua_State *L) { + (void)L; } diff --git a/app/lua/lmathlib.c b/app/lua/lmathlib.c index cb9586fc..c03d34ec 100644 --- a/app/lua/lmathlib.c +++ b/app/lua/lmathlib.c @@ -359,5 +359,6 @@ LROT_END(math, NULL, 0) ** Open math library */ LUALIB_API int luaopen_math (lua_State *L) { + (void)L; return 0; } diff --git a/app/lua/lnodemcu.c b/app/lua/lnodemcu.c index 0b791bb8..d3d96408 100644 --- a/app/lua/lnodemcu.c +++ b/app/lua/lnodemcu.c @@ -118,6 +118,7 @@ LUALIB_API int luaL_posttask( lua_State* L, int prio ) { // [-1, +0, -] } #else LUALIB_API int luaL_posttask( lua_State* L, int prio ) { + (void)L; (void)prio; return 0; } /* Dummy stub on host */ #endif diff --git a/app/lua/lnodemcu.h b/app/lua/lnodemcu.h index e5fedec6..b6363944 100644 --- a/app/lua/lnodemcu.h +++ b/app/lua/lnodemcu.h @@ -45,9 +45,11 @@ static ROTable_entry LOCK_IN_SECTION(s) rt ## _entries[] = { #define LROT_END(rt,mt,f) {NULL, LRO_NILVAL} }; \ const ROTable rt ## _ROTable = { \ - (GCObject *)1, LUA_TROTABLE, LROT_MARKED, \ - cast(lu_byte, ~(f)), (sizeof(rt ## _entries)/sizeof(ROTable_entry)) - 1, \ - cast(Table *, mt), cast(ROTable_entry *, rt ## _entries) }; + .next = (GCObject *)1, .tt = LUA_TROTABLE, .marked = LROT_MARKED, \ + .flags = cast(lu_byte, ~(f)), \ + .lsizenode = (sizeof(rt ## _entries)/sizeof(ROTable_entry)) - 1, \ + .metatable = cast(Table *, mt), \ + .entry = cast(ROTable_entry *, rt ## _entries) }; #define LROT_BREAK(rt) }; #define LROT_MASK(m) cast(lu_byte, 1<= 256) { l += 8; x >>= 8; } return l + log_2[x]; #else - /* Use Normalization Shift Amount Unsigned: 0x1=>31 up to 0xffffffff =>0 - * See Xtensa Instruction Set Architecture (ISA) Refman P 462 */ - asm volatile ("nsau %0, %1;" :"=r"(x) : "r"(x)); - return 31 - x; + return 31 - __builtin_clz(x); #endif } diff --git a/app/lua/lobject.h b/app/lua/lobject.h index 74680d49..0e8e8478 100644 --- a/app/lua/lobject.h +++ b/app/lua/lobject.h @@ -28,7 +28,7 @@ #define LUA_TUPVAL (LAST_TAG+2) #define LUA_TDEADKEY (LAST_TAG+3) -#ifdef LUA_USE_ESP +#ifdef LUA_USE_ESP8266 /* ** force aligned access to critical fields in Flash-based structures ** wo is the offset of aligned word in bytes 0,4,8,.. diff --git a/app/lua/ltable.c b/app/lua/ltable.c index a9940dce..9bfd0abc 100644 --- a/app/lua/ltable.c +++ b/app/lua/ltable.c @@ -336,6 +336,7 @@ static Node *find_prev_node(Node *mp, Node *next) { ** (colliding node is in its main position), moving node goes to an empty position. */ static int move_node (lua_State *L, Table *t, Node *node) { + (void)L; Node *mp = mainposition(t, key2tval(node)); /* if node is in it's main position, don't need to move node. */ if (mp == node) return 1; @@ -374,6 +375,7 @@ static int move_node (lua_State *L, Table *t, Node *node) { static int move_number (lua_State *L, Table *t, Node *node) { + (void)L; int key; lua_Number n = nvalue(key2tval(node)); lua_number2int(key, n); diff --git a/app/lua/ltablib.c b/app/lua/ltablib.c index 000d9a6c..57e66e24 100644 --- a/app/lua/ltablib.c +++ b/app/lua/ltablib.c @@ -277,5 +277,6 @@ LROT_BEGIN(tab_funcs, NULL, 0) LROT_END(tab_funcs, NULL, 0) LUALIB_API int luaopen_table (lua_State *L) { + (void)L; return 1; } diff --git a/app/lua53/lauxlib.h b/app/lua53/lauxlib.h index 50269ee5..2ef11b30 100644 --- a/app/lua53/lauxlib.h +++ b/app/lua53/lauxlib.h @@ -144,6 +144,9 @@ LUALIB_API void (luaL_requiref) (lua_State *L, const char *modname, #define luaL_loadbuffer(L,s,sz,n) luaL_loadbufferx(L,s,sz,n,NULL) +#define luaL_checkfunction(L,n) luaL_checktype(L, (n), LUA_TFUNCTION); +#define luaL_checktable(L,n) luaL_checktype(L, (n), LUA_TTABLE); + /* ** {====================================================== ** Generic Buffer manipulation @@ -227,7 +230,8 @@ LUALIB_API void (luaL_openlib) (lua_State *L, const char *libname, /* print a string */ #if !defined(lua_writestring) -#ifdef LUA_USE_ESP8266 +#ifdef LUA_USE_ESP +void output_redirect(const char *str, size_t l); #define lua_writestring(s,l) output_redirect((s),(l)) #else #define lua_writestring(s,l) fwrite((s), sizeof(char), (l), stdout) @@ -290,11 +294,8 @@ LUALIB_API int (luaL_pushlfsdts) (lua_State *L); LUALIB_API void (luaL_lfsreload) (lua_State *L); LUALIB_API int (luaL_posttask) (lua_State* L, int prio); LUALIB_API int (luaL_pcallx) (lua_State *L, int narg, int nres); - #define luaL_pushlfsmodule(l) lua_pushlfsfunc(L) /* }============================================================ */ #endif - - diff --git a/app/lua53/ldblib.c b/app/lua53/ldblib.c index 0a36a530..5be2ece2 100644 --- a/app/lua53/ldblib.c +++ b/app/lua53/ldblib.c @@ -464,6 +464,7 @@ LROT_BEGIN(dblib, NULL, 0) LROT_END(dblib, NULL, 0) LUAMOD_API int luaopen_debug (lua_State *L) { + (void)L; return 0; } diff --git a/app/lua53/ldump.c b/app/lua53/ldump.c index 90bbdfaa..3dfee3c6 100644 --- a/app/lua53/ldump.c +++ b/app/lua53/ldump.c @@ -272,6 +272,7 @@ static int stripdebug (lua_State *L, Proto *f, int level) { f->lineinfo = luaM_freearray(L, f->lineinfo, f->sizelineinfo); len += f->sizelineinfo; } + // fall-through case 1: for (i=0; isizeupvalues; i++) f->upvalues[i].name = NULL; diff --git a/app/lua53/lmathlib.c b/app/lua53/lmathlib.c index 46ee1b3b..352249cb 100644 --- a/app/lua53/lmathlib.c +++ b/app/lua53/lmathlib.c @@ -394,6 +394,7 @@ LROT_END(mathlib, NULL, 0) ** Open math library */ LUAMOD_API int luaopen_math (lua_State *L) { + (void)L; return 0; } diff --git a/app/lua53/lnodemcu.c b/app/lua53/lnodemcu.c index e124eb1d..2b1d6e9b 100644 --- a/app/lua53/lnodemcu.c +++ b/app/lua53/lnodemcu.c @@ -296,7 +296,7 @@ LUA_API void lua_getlfsconfig (lua_State *L, int *config) { } } -LUA_API int (lua_pushlfsindex) (lua_State *L) { +LUA_API int lua_pushlfsindex(lua_State *L) { lua_lock(L); setobj2n(L, L->top, &G(L)->LFStable); api_incr_top(L); @@ -309,7 +309,7 @@ LUA_API int (lua_pushlfsindex) (lua_State *L) { * one upvalue that must be the set to the _ENV variable when its closure is * created, and as such this parallels some ldo.c processing. */ -LUA_API int (lua_pushlfsfunc) (lua_State *L) { +LUA_API int lua_pushlfsfunc(lua_State *L) { lua_lock(L); const TValue *t = &G(L)->LFStable; if (ttisstring(L->top-1) && ttistable(t)) { @@ -341,7 +341,7 @@ LUA_API int (lua_pushlfsfunc) (lua_State *L) { /* * Return an array of functions in LFS */ -LUALIB_API int (luaL_pushlfsmodules) (lua_State *L) { +LUALIB_API int luaL_pushlfsmodules(lua_State *L) { int i = 1; if (lua_pushlfsindex(L) == LUA_TNIL) return 0; /* return nil if LFS not loaded */ @@ -357,7 +357,7 @@ LUALIB_API int (luaL_pushlfsmodules) (lua_State *L) { return 1; } -LUALIB_API int (luaL_pushlfsdts) (lua_State *L) { +LUALIB_API int luaL_pushlfsdts(lua_State *L) { int config[5]; lua_getlfsconfig(L, config); lua_pushinteger(L, config[4]); @@ -427,7 +427,9 @@ static const char *readF (lua_State *L, void *ud, size_t *size) { static void eraseLFS(LFSflashState *F) { lu_int32 i; +#ifdef LUA_USE_ESP printf("\nErasing LFS from flash addr 0x%06x", F->addrPhys); +#endif unlockFlashWrite(); for (i = 0; i < F->size; i += FLASH_PAGE_SIZE) { size_t *f = cast(size_t *, F->addr + i/sizeof(*f)); @@ -436,11 +438,13 @@ static void eraseLFS(LFSflashState *F) { #ifdef LUA_USE_ESP if (*f == ~0 && !memcmp(f, f + 1, FLASH_PAGE_SIZE - sizeof(*f))) continue; + printf("."); #endif platform_flash_erase_sector(s); - printf("."); } +#ifdef LUA_USE_ESP printf(" to 0x%06x\n", F->addrPhys + F->size-1); +#endif flush_icache(F); lockFlashWrite(); } @@ -623,7 +627,6 @@ LUALIB_API void luaL_lfsreload (lua_State *L) { #ifdef LUA_USE_ESP -extern void lua_main(void); /* ** Task callback handler. Uses luaN_call to do a protected call with full traceback */ @@ -634,7 +637,7 @@ static void do_task (platform_task_param_t task_fn_ref, uint8_t prio) { return; } if (prio < LUA_TASK_LOW|| prio > LUA_TASK_HIGH) - luaL_error(L, "invalid posk task"); + luaL_error(L, "invalid post task"); /* Pop the CB func from the Reg */ lua_rawgeti(L, LUA_REGISTRYINDEX, (int) task_fn_ref); luaL_checktype(L, -1, LUA_TFUNCTION); @@ -662,7 +665,7 @@ LUALIB_API int luaL_posttask ( lua_State* L, int prio ) { // [-1, +0, -] } return task_fn_ref; } else { - return luaL_error(L, "invalid posk task"); + return luaL_error(L, "invalid post task"); } } #else @@ -670,6 +673,7 @@ LUALIB_API int luaL_posttask ( lua_State* L, int prio ) { // [-1, +0, -] ** Task execution isn't supported on HOST builds so returns a -1 status */ LUALIB_API int luaL_posttask( lua_State* L, int prio ) { // [-1, +0, -] + (void)L; (void)prio; return -1; } #endif diff --git a/app/lua53/loadlib.c b/app/lua53/loadlib.c index 74e69a4f..6c049dcf 100644 --- a/app/lua53/loadlib.c +++ b/app/lua53/loadlib.c @@ -421,7 +421,7 @@ static int ll_loadlib (lua_State *L) { ** ======================================================= */ -#ifdef LUA_USE_ESP8266 +#ifdef LUA_USE_ESP #define file_t int #undef fopen #undef fclose diff --git a/app/lua53/lobject.c b/app/lua53/lobject.c index 384c765c..867720f7 100644 --- a/app/lua53/lobject.c +++ b/app/lua53/lobject.c @@ -63,13 +63,7 @@ int luaO_fb2int (int x) { ** Computes ceil(log2(x)) */ int luaO_ceillog2 (unsigned int x) { -#ifdef LUA_USE_ESP - /* Use Normalization Shift Amount Unsigned: 0x1=>31 up to 0xffffffff =>0 - * See Xtensa Instruction Set Architecture (ISA) Refman P 462 */ - x--; - asm volatile ("nsau %0, %1;" :"=r"(x) : "r"(x)); - return 32 - x; -#else +#ifdef LUA_CROSS_COMPILER static const lu_byte log_2[256] = { /* log_2[i] = ceil(log2(i - 1)) */ 0,1,2,2,3,3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5, 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6, @@ -84,6 +78,8 @@ int luaO_ceillog2 (unsigned int x) { x--; while (x >= 256) { l += 8; x >>= 8; } return l + log_2[x]; +#else + return (x == 1) ? 0 : 32 - __builtin_clz(x - 1); #endif } diff --git a/app/lua53/lobject.h b/app/lua53/lobject.h index 5441f81a..7e01944c 100644 --- a/app/lua53/lobject.h +++ b/app/lua53/lobject.h @@ -80,7 +80,7 @@ ** wo is the offset of aligned word in bytes 0,4,8,.. ** bo is the field within the word in bits 0..31 */ -#ifdef LUA_USE_ESP +#if defined(LUA_USE_ESP8266) #define GET_BYTE_FN(name,t,wo,bo) \ static inline lu_int32 get ## name(const void *o) { \ lu_int32 res; /* extract named field */ \ diff --git a/app/lua53/lstate.c b/app/lua53/lstate.c index a2d68eaf..b524b188 100644 --- a/app/lua53/lstate.c +++ b/app/lua53/lstate.c @@ -42,12 +42,15 @@ ** created; the seed is used to randomize hashes. */ #if !defined(luai_makeseed) -#if defined(LUA_USE_ESP) +#if defined(LUA_USE_ESP8266) static inline unsigned int luai_makeseed(void) { unsigned int r; asm volatile("rsr %0, ccount" : "=r"(r)); return r; } +#elif defined(LUA_USE_ESP) +# include "esp_random.h" +# define luai_makeseed() esp_random() #else #include #define luai_makeseed() cast(unsigned int, time(NULL)) diff --git a/app/lua53/ltablib.c b/app/lua53/ltablib.c index 48d58bbf..77715598 100644 --- a/app/lua53/ltablib.c +++ b/app/lua53/ltablib.c @@ -438,6 +438,7 @@ LROT_BEGIN(tab_funcs, NULL, 0) LROT_END(tab_funcs, NULL, 0) LUAMOD_API int luaopen_table (lua_State *L) { + (void)L; return 0; } diff --git a/app/lua53/lua.h b/app/lua53/lua.h index 58eb5a40..4ca58385 100644 --- a/app/lua53/lua.h +++ b/app/lua53/lua.h @@ -465,6 +465,8 @@ struct lua_Debug { struct CallInfo *i_ci; /* active function */ }; +int lua_main(void); + /* }====================================================================== */ /* NodeMCU extensions to the standard API */ diff --git a/app/lua53/lutf8lib.c b/app/lua53/lutf8lib.c index a139bed9..4fff0f2d 100644 --- a/app/lua53/lutf8lib.c +++ b/app/lua53/lutf8lib.c @@ -258,6 +258,7 @@ LROT_END(utf8, LROT_TABLEREF(utf8_meta), 0) LUAMOD_API int luaopen_utf8 (lua_State *L) { + (void)L; return 0; }