Skip to content

Commit

Permalink
Lua tidy-up to reduce delta against esp32 branch.
Browse files Browse the repository at this point in the history
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)).
  • Loading branch information
Johny Mattsson committed Sep 25, 2021
1 parent d2f08f5 commit 4e56a1b
Show file tree
Hide file tree
Showing 25 changed files with 71 additions and 49 deletions.
2 changes: 1 addition & 1 deletion app/lua/lbaselib.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion app/lua/ldebug.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 *);
Expand Down Expand Up @@ -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'? */
Expand Down
33 changes: 19 additions & 14 deletions app/lua/lflash.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
* process.
*/

static char *flashAddr;
static const char *flashAddr;
static uint32_t flashSize;
static uint32_t flashAddrPhys;
static uint32_t flashSector;
Expand Down Expand Up @@ -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);
Expand All @@ -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
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -208,6 +212,7 @@ LUALIB_API void luaL_lfsreload (lua_State *L) {
return;
}


/*
* Do a protected call of loadLFS.
*
Expand Down Expand Up @@ -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];
}

Expand Down Expand Up @@ -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 */
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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);

Expand Down
1 change: 1 addition & 0 deletions app/lua/llex.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ static void save (LexState *ls, int c) {


void luaX_init (lua_State *L) {
(void)L;
}


Expand Down
1 change: 1 addition & 0 deletions app/lua/lmathlib.c
Original file line number Diff line number Diff line change
Expand Up @@ -359,5 +359,6 @@ LROT_END(math, NULL, 0)
** Open math library
*/
LUALIB_API int luaopen_math (lua_State *L) {
(void)L;
return 0;
}
1 change: 1 addition & 0 deletions app/lua/lnodemcu.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 6 additions & 5 deletions app/lua/lnodemcu.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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

1 change: 1 addition & 0 deletions app/lua/loadlib.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
5 changes: 1 addition & 4 deletions app/lua/lobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion app/lua/lobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,..
Expand Down
2 changes: 2 additions & 0 deletions app/lua/ltable.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions app/lua/ltablib.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
2 changes: 1 addition & 1 deletion app/lua/lundump.c
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ static lua_Number LoadNumber(LoadState* S)
LoadVar(S,y);
x = (lua_Number)y;
} break;
default: lua_assert(0);
default: lua_assert(0); __builtin_unreachable();
}
}
else
Expand Down
9 changes: 5 additions & 4 deletions app/lua53/lauxlib.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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


1 change: 1 addition & 0 deletions app/lua53/ldblib.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

1 change: 1 addition & 0 deletions app/lua53/ldump.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions app/lua53/lmathlib.c
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,7 @@ LROT_END(mathlib, NULL, 0)
** Open math library
*/
LUAMOD_API int luaopen_math (lua_State *L) {
(void)L;
return 0;
}

Loading

0 comments on commit 4e56a1b

Please sign in to comment.