Skip to content

Commit 80a1278

Browse files
author
Johny Mattsson
committed
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)).
1 parent d2f08f5 commit 80a1278

24 files changed

+70
-48
lines changed

app/lua/lbaselib.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
** model but changing `fputs' to put the strings at a proper place
2525
** (a console window or a log file, for instance).
2626
*/
27-
#ifdef LUA_CROSS_COMPILER
27+
#if defined(LUA_USE_ESP8266)
2828
#undef puts
2929
#define puts(s) printf("%s",s)
3030
#endif

app/lua/ldebug.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,7 @@ static int stripdebug (lua_State *L, Proto *f, int level) {
248248
f->packedlineinfo = luaM_freearray(L, f->packedlineinfo, sizepackedlineinfo, unsigned char);
249249
len += sizepackedlineinfo;
250250
}
251+
// fall-through
251252
case 1:
252253
f->locvars = luaM_freearray(L, f->locvars, f->sizelocvars, struct LocVar);
253254
f->upvalues = luaM_freearray(L, f->upvalues, f->sizeupvalues, TString *);
@@ -501,7 +502,7 @@ static Instruction symbexec (const Proto *pt, int lastpc, int reg) {
501502
case OP_FORLOOP:
502503
case OP_FORPREP:
503504
checkreg(pt, a+3);
504-
/* go through */
505+
/* fall-through */
505506
case OP_JMP: {
506507
int dest = pc+1+b;
507508
/* not full check and jump is forward and do not skip `lastpc'? */

app/lua/lflash.c

+19-14
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
* process.
3030
*/
3131

32-
static char *flashAddr;
32+
static const char *flashAddr;
3333
static uint32_t flashSize;
3434
static uint32_t flashAddrPhys;
3535
static uint32_t flashSector;
@@ -106,20 +106,20 @@ LUA_API void dumpStrings(lua_State *L) {
106106
* writes are suppressed if the global writeToFlash is false. This is used in
107107
* phase I where the pass is used to size the structures in flash.
108108
*/
109-
static char *flashPosition(void){
109+
static const char *flashPosition(void){
110110
return flashAddr + curOffset;
111111
}
112112

113113

114-
static char *flashSetPosition(uint32_t offset){
114+
static const char *flashSetPosition(uint32_t offset){
115115
NODE_DBG("flashSetPosition(%04x)\n", offset);
116116
curOffset = offset;
117117
return flashPosition();
118118
}
119119

120120

121-
static char *flashBlock(const void* b, size_t size) {
122-
void *cur = flashPosition();
121+
static const char *flashBlock(const void* b, size_t size) {
122+
const void *cur = flashPosition();
123123
NODE_DBG("flashBlock((%04x),%p,%04x)\n", curOffset,b,size);
124124
lua_assert(ALIGN_BITS(b) == 0 && ALIGN_BITS(size) == 0);
125125
platform_flash_write(b, flashAddrPhys+curOffset, size);
@@ -137,6 +137,10 @@ static void flashErase(uint32_t start, uint32_t end){
137137
platform_flash_erase_sector( flashSector + i );
138138
}
139139

140+
static int loadLFS (lua_State *L);
141+
static int loadLFSgc (lua_State *L);
142+
static void procFirstPass (void);
143+
140144
/* =====================================================================================
141145
* luaN_init() is exported via lflash.h.
142146
* 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) {
171175
}
172176

173177
if ((fh->flash_sig & (~FLASH_SIG_ABSOLUTE)) != FLASH_SIG ) {
174-
NODE_ERR("Flash sig not correct: 0x%08x vs 0x%08x\n",
178+
NODE_ERR("LFS sig not correct: 0x%x vs expected 0x%x\n",
175179
fh->flash_sig & (~FLASH_SIG_ABSOLUTE), FLASH_SIG);
176180
return;
177181
}
@@ -208,6 +212,7 @@ LUALIB_API void luaL_lfsreload (lua_State *L) {
208212
return;
209213
}
210214

215+
211216
/*
212217
* Do a protected call of loadLFS.
213218
*
@@ -346,13 +351,13 @@ static void put_byte (uint8_t value) {
346351
}
347352

348353

349-
static uint8_t recall_byte (unsigned offset) {
354+
static uint8_t recall_byte (uint32_t offset) {
350355
if(offset > DICTIONARY_WINDOW || offset >= out->ndx)
351356
flash_error("invalid dictionary offset on inflate");
352357
/* ndx starts at 1. Need relative to 0 */
353-
unsigned n = out->ndx - offset;
354-
unsigned pos = n % WRITE_BLOCKSIZE;
355-
unsigned blockNo = out->ndx / WRITE_BLOCKSIZE - n / WRITE_BLOCKSIZE;
358+
uint32_t n = out->ndx - offset;
359+
uint32_t pos = n % WRITE_BLOCKSIZE;
360+
uint32_t blockNo = out->ndx / WRITE_BLOCKSIZE - n / WRITE_BLOCKSIZE;
356361
return out->block[blockNo]->byte[pos];
357362
}
358363

@@ -386,7 +391,7 @@ void procFirstPass (void) {
386391
fh->flash_size > flashSize ||
387392
out->flagsLen != 1 + (out->flashLen/WORDSIZE - 1) / BITS_PER_WORD)
388393
flash_error("LFS length mismatch");
389-
out->flags = luaM_newvector(out->L, out->flagsLen, unsigned);
394+
out->flags = luaM_newvector(out->L, out->flagsLen, uint32_t);
390395
}
391396

392397
/* update running CRC */
@@ -412,7 +417,7 @@ void procSecondPass (void) {
412417
(out->flashLen % WRITE_BLOCKSIZE) / WORDSIZE :
413418
WRITE_BLOCKSIZE / WORDSIZE;
414419
uint32_t *buf = (uint32_t *) out->buffer.byte;
415-
uint32_t flags = 0;
420+
uint32_t flags = 0;
416421
/*
417422
* Relocate all the addresses tagged in out->flags. This can't be done in
418423
* place because the out->blocks are still in use as dictionary content so
@@ -423,7 +428,7 @@ void procSecondPass (void) {
423428
if ((i&31)==0)
424429
flags = out->flags[out->flagsNdx++];
425430
if (flags&1)
426-
buf[i] = WORDSIZE*buf[i] + cast(uint32_t, flashAddr);
431+
buf[i] = WORDSIZE*buf[i] + cast(uint32_t, flashAddr); // mapped, not phys
427432
}
428433
/*
429434
* 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) {
468473
in->len = vfs_size(in->fd);
469474
if (in->len <= 200 || /* size of an empty luac output */
470475
vfs_lseek(in->fd, in->len-4, VFS_SEEK_SET) != in->len-4 ||
471-
vfs_read(in->fd, &out->len, sizeof(unsigned)) != sizeof(unsigned))
476+
vfs_read(in->fd, &out->len, sizeof(uint32_t)) != sizeof(uint32_t))
472477
flash_error("read error on LFS image file");
473478
vfs_lseek(in->fd, 0, VFS_SEEK_SET);
474479

app/lua/llex.c

+1
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ static void save (LexState *ls, int c) {
6161

6262

6363
void luaX_init (lua_State *L) {
64+
(void)L;
6465
}
6566

6667

app/lua/lmathlib.c

+1
Original file line numberDiff line numberDiff line change
@@ -359,5 +359,6 @@ LROT_END(math, NULL, 0)
359359
** Open math library
360360
*/
361361
LUALIB_API int luaopen_math (lua_State *L) {
362+
(void)L;
362363
return 0;
363364
}

app/lua/lnodemcu.c

+1
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ LUALIB_API int luaL_posttask( lua_State* L, int prio ) { // [-1, +0, -]
118118
}
119119
#else
120120
LUALIB_API int luaL_posttask( lua_State* L, int prio ) {
121+
(void)L; (void)prio;
121122
return 0;
122123
} /* Dummy stub on host */
123124
#endif

app/lua/lnodemcu.h

+6-5
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,11 @@
4545
static ROTable_entry LOCK_IN_SECTION(s) rt ## _entries[] = {
4646
#define LROT_END(rt,mt,f) {NULL, LRO_NILVAL} }; \
4747
const ROTable rt ## _ROTable = { \
48-
(GCObject *)1, LUA_TROTABLE, LROT_MARKED, \
49-
cast(lu_byte, ~(f)), (sizeof(rt ## _entries)/sizeof(ROTable_entry)) - 1, \
50-
cast(Table *, mt), cast(ROTable_entry *, rt ## _entries) };
48+
.next = (GCObject *)1, .tt = LUA_TROTABLE, .marked = LROT_MARKED, \
49+
.flags = cast(lu_byte, ~(f)), \
50+
.lsizenode = (sizeof(rt ## _entries)/sizeof(ROTable_entry)) - 1, \
51+
.metatable = cast(Table *, mt), \
52+
.entry = cast(ROTable_entry *, rt ## _entries) };
5153
#define LROT_BREAK(rt) };
5254

5355
#define LROT_MASK(m) cast(lu_byte, 1<<TM_ ## m)
@@ -65,10 +67,9 @@
6567
#define LROT_MASK_NEWINDEX LROT_MASK(NEWINDEX)
6668
#define LROT_MASK_GC_INDEX (LROT_MASK_GC | LROT_MASK_INDEX)
6769

68-
/* Maximum length of a rotable name and of a string key*/
70+
#define LUA_MAX_ROTABLE_NAME 32 /* Maximum length of a rotable name and of a string key*/
6971

7072
#ifdef LUA_CORE
7173

7274
#endif
7375
#endif
74-

app/lua/loadlib.c

+1
Original file line numberDiff line numberDiff line change
@@ -619,6 +619,7 @@ static int ll_seeall (lua_State *L) {
619619

620620
static void setpath (lua_State *L, const char *fieldname, const char *envname,
621621
const char *def) {
622+
(void)envname;
622623
const char *path = NULL; /* getenv(envname) not used in NodeMCU */;
623624
if (path == NULL) /* no environment variable? */
624625
lua_pushstring(L, def); /* use default */

app/lua/lobject.c

+1-4
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,7 @@ int luaO_log2 (unsigned int x) {
6868
while (x >= 256) { l += 8; x >>= 8; }
6969
return l + log_2[x];
7070
#else
71-
/* Use Normalization Shift Amount Unsigned: 0x1=>31 up to 0xffffffff =>0
72-
* See Xtensa Instruction Set Architecture (ISA) Refman P 462 */
73-
asm volatile ("nsau %0, %1;" :"=r"(x) : "r"(x));
74-
return 31 - x;
71+
return 31 - __builtin_clz(x);
7572
#endif
7673
}
7774

app/lua/lobject.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
#define LUA_TUPVAL (LAST_TAG+2)
2929
#define LUA_TDEADKEY (LAST_TAG+3)
3030

31-
#ifdef LUA_USE_ESP
31+
#ifdef LUA_USE_ESP8266
3232
/*
3333
** force aligned access to critical fields in Flash-based structures
3434
** wo is the offset of aligned word in bytes 0,4,8,..

app/lua/ltable.c

+2
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,7 @@ static Node *find_prev_node(Node *mp, Node *next) {
336336
** (colliding node is in its main position), moving node goes to an empty position.
337337
*/
338338
static int move_node (lua_State *L, Table *t, Node *node) {
339+
(void)L;
339340
Node *mp = mainposition(t, key2tval(node));
340341
/* if node is in it's main position, don't need to move node. */
341342
if (mp == node) return 1;
@@ -374,6 +375,7 @@ static int move_node (lua_State *L, Table *t, Node *node) {
374375

375376

376377
static int move_number (lua_State *L, Table *t, Node *node) {
378+
(void)L;
377379
int key;
378380
lua_Number n = nvalue(key2tval(node));
379381
lua_number2int(key, n);

app/lua/ltablib.c

+1
Original file line numberDiff line numberDiff line change
@@ -277,5 +277,6 @@ LROT_BEGIN(tab_funcs, NULL, 0)
277277
LROT_END(tab_funcs, NULL, 0)
278278

279279
LUALIB_API int luaopen_table (lua_State *L) {
280+
(void)L;
280281
return 1;
281282
}

app/lua53/lauxlib.h

+5-4
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,9 @@ LUALIB_API void (luaL_requiref) (lua_State *L, const char *modname,
144144
#define luaL_loadbuffer(L,s,sz,n) luaL_loadbufferx(L,s,sz,n,NULL)
145145

146146

147+
#define luaL_checkfunction(L,n) luaL_checktype(L, (n), LUA_TFUNCTION);
148+
#define luaL_checktable(L,n) luaL_checktype(L, (n), LUA_TTABLE);
149+
147150
/*
148151
** {======================================================
149152
** Generic Buffer manipulation
@@ -227,7 +230,8 @@ LUALIB_API void (luaL_openlib) (lua_State *L, const char *libname,
227230

228231
/* print a string */
229232
#if !defined(lua_writestring)
230-
#ifdef LUA_USE_ESP8266
233+
#ifdef LUA_USE_ESP
234+
void output_redirect(const char *str, size_t l);
231235
#define lua_writestring(s,l) output_redirect((s),(l))
232236
#else
233237
#define lua_writestring(s,l) fwrite((s), sizeof(char), (l), stdout)
@@ -290,11 +294,8 @@ LUALIB_API int (luaL_pushlfsdts) (lua_State *L);
290294
LUALIB_API void (luaL_lfsreload) (lua_State *L);
291295
LUALIB_API int (luaL_posttask) (lua_State* L, int prio);
292296
LUALIB_API int (luaL_pcallx) (lua_State *L, int narg, int nres);
293-
294297
#define luaL_pushlfsmodule(l) lua_pushlfsfunc(L)
295298

296299
/* }============================================================ */
297300

298301
#endif
299-
300-

app/lua53/ldblib.c

+1
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,7 @@ LROT_BEGIN(dblib, NULL, 0)
464464
LROT_END(dblib, NULL, 0)
465465

466466
LUAMOD_API int luaopen_debug (lua_State *L) {
467+
(void)L;
467468
return 0;
468469
}
469470

app/lua53/ldump.c

+1
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,7 @@ static int stripdebug (lua_State *L, Proto *f, int level) {
272272
f->lineinfo = luaM_freearray(L, f->lineinfo, f->sizelineinfo);
273273
len += f->sizelineinfo;
274274
}
275+
// fall-through
275276
case 1:
276277
for (i=0; i<f->sizeupvalues; i++)
277278
f->upvalues[i].name = NULL;

app/lua53/lmathlib.c

+1
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,7 @@ LROT_END(mathlib, NULL, 0)
394394
** Open math library
395395
*/
396396
LUAMOD_API int luaopen_math (lua_State *L) {
397+
(void)L;
397398
return 0;
398399
}
399400

app/lua53/lnodemcu.c

+12-8
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ LUA_API void lua_getlfsconfig (lua_State *L, int *config) {
296296
}
297297
}
298298

299-
LUA_API int (lua_pushlfsindex) (lua_State *L) {
299+
LUA_API int lua_pushlfsindex(lua_State *L) {
300300
lua_lock(L);
301301
setobj2n(L, L->top, &G(L)->LFStable);
302302
api_incr_top(L);
@@ -309,7 +309,7 @@ LUA_API int (lua_pushlfsindex) (lua_State *L) {
309309
* one upvalue that must be the set to the _ENV variable when its closure is
310310
* created, and as such this parallels some ldo.c processing.
311311
*/
312-
LUA_API int (lua_pushlfsfunc) (lua_State *L) {
312+
LUA_API int lua_pushlfsfunc(lua_State *L) {
313313
lua_lock(L);
314314
const TValue *t = &G(L)->LFStable;
315315
if (ttisstring(L->top-1) && ttistable(t)) {
@@ -341,7 +341,7 @@ LUA_API int (lua_pushlfsfunc) (lua_State *L) {
341341
/*
342342
* Return an array of functions in LFS
343343
*/
344-
LUALIB_API int (luaL_pushlfsmodules) (lua_State *L) {
344+
LUALIB_API int luaL_pushlfsmodules(lua_State *L) {
345345
int i = 1;
346346
if (lua_pushlfsindex(L) == LUA_TNIL)
347347
return 0; /* return nil if LFS not loaded */
@@ -357,7 +357,7 @@ LUALIB_API int (luaL_pushlfsmodules) (lua_State *L) {
357357
return 1;
358358
}
359359

360-
LUALIB_API int (luaL_pushlfsdts) (lua_State *L) {
360+
LUALIB_API int luaL_pushlfsdts(lua_State *L) {
361361
int config[5];
362362
lua_getlfsconfig(L, config);
363363
lua_pushinteger(L, config[4]);
@@ -427,7 +427,9 @@ static const char *readF (lua_State *L, void *ud, size_t *size) {
427427

428428
static void eraseLFS(LFSflashState *F) {
429429
lu_int32 i;
430+
#ifdef LUA_USE_ESP
430431
printf("\nErasing LFS from flash addr 0x%06x", F->addrPhys);
432+
#endif
431433
unlockFlashWrite();
432434
for (i = 0; i < F->size; i += FLASH_PAGE_SIZE) {
433435
size_t *f = cast(size_t *, F->addr + i/sizeof(*f));
@@ -436,11 +438,13 @@ static void eraseLFS(LFSflashState *F) {
436438
#ifdef LUA_USE_ESP
437439
if (*f == ~0 && !memcmp(f, f + 1, FLASH_PAGE_SIZE - sizeof(*f)))
438440
continue;
441+
printf(".");
439442
#endif
440443
platform_flash_erase_sector(s);
441-
printf(".");
442444
}
445+
#ifdef LUA_USE_ESP
443446
printf(" to 0x%06x\n", F->addrPhys + F->size-1);
447+
#endif
444448
flush_icache(F);
445449
lockFlashWrite();
446450
}
@@ -623,7 +627,6 @@ LUALIB_API void luaL_lfsreload (lua_State *L) {
623627

624628

625629
#ifdef LUA_USE_ESP
626-
extern void lua_main(void);
627630
/*
628631
** Task callback handler. Uses luaN_call to do a protected call with full traceback
629632
*/
@@ -634,7 +637,7 @@ static void do_task (platform_task_param_t task_fn_ref, uint8_t prio) {
634637
return;
635638
}
636639
if (prio < LUA_TASK_LOW|| prio > LUA_TASK_HIGH)
637-
luaL_error(L, "invalid posk task");
640+
luaL_error(L, "invalid post task");
638641
/* Pop the CB func from the Reg */
639642
lua_rawgeti(L, LUA_REGISTRYINDEX, (int) task_fn_ref);
640643
luaL_checktype(L, -1, LUA_TFUNCTION);
@@ -662,14 +665,15 @@ LUALIB_API int luaL_posttask ( lua_State* L, int prio ) { // [-1, +0, -]
662665
}
663666
return task_fn_ref;
664667
} else {
665-
return luaL_error(L, "invalid posk task");
668+
return luaL_error(L, "invalid post task");
666669
}
667670
}
668671
#else
669672
/*
670673
** Task execution isn't supported on HOST builds so returns a -1 status
671674
*/
672675
LUALIB_API int luaL_posttask( lua_State* L, int prio ) { // [-1, +0, -]
676+
(void)L; (void)prio;
673677
return -1;
674678
}
675679
#endif

0 commit comments

Comments
 (0)