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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 2 additions & 1 deletion
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

Lines changed: 19 additions & 14 deletions
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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 6 additions & 5 deletions
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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 1 addition & 4 deletions
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

Lines changed: 1 addition & 1 deletion
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,..

0 commit comments

Comments
 (0)