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)).
This commit is contained in:
Johny Mattsson 2021-09-23 17:35:28 +10:00
parent d2f08f54d2
commit 80a12780ae
24 changed files with 70 additions and 48 deletions

View File

@ -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

View File

@ -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'? */

View File

@ -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);

View File

@ -61,6 +61,7 @@ static void save (LexState *ls, int c) {
void luaX_init (lua_State *L) {
(void)L;
}

View File

@ -359,5 +359,6 @@ LROT_END(math, NULL, 0)
** Open math library
*/
LUALIB_API int luaopen_math (lua_State *L) {
(void)L;
return 0;
}

View File

@ -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

View File

@ -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<<TM_ ## m)
@ -65,10 +67,9 @@
#define LROT_MASK_NEWINDEX LROT_MASK(NEWINDEX)
#define LROT_MASK_GC_INDEX (LROT_MASK_GC | LROT_MASK_INDEX)
/* Maximum length of a rotable name and of a string key*/
#define LUA_MAX_ROTABLE_NAME 32 /* Maximum length of a rotable name and of a string key*/
#ifdef LUA_CORE
#endif
#endif

View File

@ -619,6 +619,7 @@ static int ll_seeall (lua_State *L) {
static void setpath (lua_State *L, const char *fieldname, const char *envname,
const char *def) {
(void)envname;
const char *path = NULL; /* getenv(envname) not used in NodeMCU */;
if (path == NULL) /* no environment variable? */
lua_pushstring(L, def); /* use default */

View File

@ -68,10 +68,7 @@ int luaO_log2 (unsigned int x) {
while (x >= 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
}

View File

@ -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,..

View File

@ -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);

View File

@ -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;
}

View File

@ -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

View File

@ -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;
}

View File

@ -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; i<f->sizeupvalues; i++)
f->upvalues[i].name = NULL;

View File

@ -394,6 +394,7 @@ LROT_END(mathlib, NULL, 0)
** Open math library
*/
LUAMOD_API int luaopen_math (lua_State *L) {
(void)L;
return 0;
}

View File

@ -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

View File

@ -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

View File

@ -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
}

View File

@ -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 */ \

View File

@ -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 <time.h>
#define luai_makeseed() cast(unsigned int, time(NULL))

View File

@ -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;
}

View File

@ -465,6 +465,8 @@ struct lua_Debug {
struct CallInfo *i_ci; /* active function */
};
int lua_main(void);
/* }====================================================================== */
/* NodeMCU extensions to the standard API */

View File

@ -258,6 +258,7 @@ LROT_END(utf8, LROT_TABLEREF(utf8_meta), 0)
LUAMOD_API int luaopen_utf8 (lua_State *L) {
(void)L;
return 0;
}