Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

First cut of new LFS for initial review #3272

Closed
wants to merge 1 commit into from

Conversation

TerryE
Copy link
Collaborator

@TerryE TerryE commented Sep 6, 2020

Fixes #3206 (part)

  • This PR is for the dev branch rather than for master.
  • This PR is compliant with the other contributing guidelines as well (if not, please describe why).
  • I have thoroughly tested my contribution.
  • The code changes are reflected in the documentation at docs/*.

Notes

  • This is a major upgrade to LFS for implement on-ESP building of LFS regions. I have had to do some major rework of the dump / undump architecture to achieve this, and we can discuss this later.
  • This in a initial tranche for review and comment. I have done basic testing, but still have a lot more use cases to enumerate, so this should be considered alpha code at this stage. I also have documentation changes and some peripheral API stuff (such as Addition of file.savedump(filename, func [, striplevel]) #3145) to implement.
  • I have tagged Johny and Nathaniel as reviews, as they are most familiar with the NodeMCU Lua internals. However, any constructive input is welcome.
  • The basic functionality also works in the luac.cross -e environment. Create a script containing debug.debug() and execute this to make a basic interpreter available. os and io are available and there are (HOST only) base functions lfsreload and lfsindex.

@TerryE TerryE requested review from nwf and jmattsson September 6, 2020 01:17
@TerryE TerryE self-assigned this Sep 6, 2020
@TerryE TerryE changed the title First cut of new LFS for initial reviwe First cut of new LFS for initial review Sep 6, 2020
Copy link
Member

@nwf nwf left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some initial nits; I haven't pulled the code to play with it.

L = luaL_newstate();
if (L == NULL) fatal("not enough memory for state");
if (L == NULL)
fatal("not enough memory for state");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are a great many "stylistic" changes like the above three lines of diff in these commits. Do they increase or decrease the diff between "upstream Lua" and "nodemcu Lua"? Most of them appear to my mind to be improvements in the code readability; if they increase the gap between "upstream" and "nodemcu", can we send them upstream? :)

Copy link
Collaborator Author

@TerryE TerryE Sep 7, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

luac.c needs a lot more work largely because the unification of the LFS and LC formats. Given the volume of change and the previous nasty non-standard formatting of lauc.c which is nothing like the rest of the lua core codebase, I have been considering running it through a pretty print style formatter to make it more readable anyway.

PS. Mindfart. Already done this, but the code still needs a tidy.

@@ -359,256 +269,286 @@ int main(int argc, char *argv[]) {
lua_close(L);
continue; /* and loop around once more simulating restart */
}
char *err = strdup(lua_tostring(L, -1));
char *
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This appears to be some violence. What happened to the rest of the line?

case '\t': printf("\\t"); break;
case '\v': printf("\\v"); break;
default:
printf(isprint(c) ? "%c" : "\\%03d", c);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While "obviously" equivalent, this still takes a moment to check, so I'd advocate either for reverting to match mainline or proposing this change to mainline for their inclusion. Many of the following changes also have the same feel.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will post a general discussion in #3206. Have read and review in that light.

LoadF lf;
(void)(multi); //// TODO
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea, I think I'm content with multi waiting for a later PR. :)

@@ -1153,6 +1154,8 @@ static int errhandler_aux (lua_State *L) {
** to convert into an error message which it handles in a separate posted task.
*/
static int errhandler (lua_State *L) {
if (!strncmp(lua_tostring(L, -1), "!LFS reload!", sizeof("!LFS reload!") - 1))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not really worth using strncmp when one of the parameters is a constant; go ahead and use strcmp without the repetition. The compiler's going to inline it anyway, one imagines.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup memcmp is better. We can't use strcmp because the various reload messages include a qualification.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"include a qualification"?

@TerryE
Copy link
Collaborator Author

TerryE commented Sep 6, 2020

As a codicil to my #3206 comments, I do suggest that reviewers print off and go through the three main rewrites: ldump.c, lundump.c, lnodemcu.c

@TerryE
Copy link
Collaborator Author

TerryE commented Sep 7, 2020

I will be adding #3145 to this PR as this is needed for on-ESP LFS maintenance.

Copy link
Member

@jmattsson jmattsson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Phew! Big stuff!
Overall looks good. See review comments for some qs and comments.
I haven't reviewed the dump/undump logic in depth - I'm not in a headspace to manage that right now, and besides I'm figuring you'll have caught any nasty surprises in your testing anyway :)

#include "lnodemcu.h"
#include "lobject.h"
#include "lstate.h"
#include "lstring.h"
#include "lstring.h" // ???
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess these three can/should be reviewed before commit?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Johny, in terms of this and all of your luac.c comments, I get the impression that the original luac.c was a "throw-in" not developed by the original Lua developers. The coding style and layout is quite different to the core Lua code. We are getting to the stage where this is heavily modified because of our LFS, Absolute LFS and execution environment support. I've pretty much decided to give this a complete hose down starting with a pretty print reformat, and getting rid of the some of the coding nasties. I'll add this to the next push. I will also reflect your general comments.

#define IROM0_SEG 0x40200000ul
#define IROM0_SEGMAX 0x00100000ul
#define IROM_OFFSET(a) (cast(lu_int32, (a)) - IROM0_SEG)

#define IROM_OFFSET(a)(((lu_int32) (a)) - IROM0_SEG)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A space after the first close-bracket would be nice. Weren't you the proponent of using a cast macro for easier greppability in the first place btw?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I personally prefer cast() for readability. I am not sure of the rationale here, but it is defined in limits.h and this is typically pathed in via lobject.h which is included in most of the LUA_CORE modules but not the LUA_LIB so

  • the Lua library modules (l*lib.c) -- i.e.the ones that interface to the core through the lua.h API -- don't use it, but
  • the Lua core itself which access the VM internals, internal data structures, etc., do.

Standard luac.c (even though it includes object.h for the code listing function) treats itself as non-core and therefore doesn't use cast(). Confused?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Confused?

Yup! 😄

" -v show version information\n"
" -- stop handling options\n"
" - stop handling options and process stdin\n", progname, Output);
exit(EXIT_FAILURE);
}

#define IS(s) (strcmp(argv[i],s)==0)
#define IS(s)(strcmp(argv[i], s) == 0)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think the space after the macro name & param should be removed. Personal view, of course.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry pretty print reformatting did this. I'll go through these.

} else if (IS("-m")) { /* specify a maximum image size */
flash = lookup = 1;
} else if (IS("-m")) { /* specify a maximum image size */
// flash = lookup = 1;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm? Looks suspicious! :D

maxSize = strtol(argv[++i], NULL, 0);
if (maxSize & 0xFFF)
if (maxSize &0xFFF)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't help but feel some auto-spacer has gone a bit trigger happy in places

luaH_setint(L, t, 1, L->top - 2);
luaC_barrierback(L, t, L->top - 2);
status = luaU_dump(L, t, writer, data, strip);
L->top--; /* Drop able and ceck GC */
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here, have a t and an h ;)

lu_int32 protoHead; /* offset of linked list of Protos in LFS */
lu_int32 shortTShead; /* offset of linked list of short TStrings in LFS */
lu_int32 longTShead; /* offset of linked list of long TStrings in LFS */
short nFiles;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How come short rather than an explicit 16bit type?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will make these a lu_int16 type. The reason for the 16-bit was that I wanted to keep the header the same size if possible and need to pass and additional two integer parameters from pass 1 to pass 2 but only had 4 bytes reserved. Both are small integers so 2 × lu_int16 is fine.

typedef struct LFSflashState {
lua_State *L;
LoadState *S;
int pass; /* 1 or 2; also 0 implies 1 */
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we be explicit about integer width here too (and in nSave below), for consistency if nothing else?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not really. This an internal state structure (which is private to nodemcu.c. This pattern just follows Lua coding style which uses int in its structures for generic integer. The reason for the lu_int32 declarations is that these must be 32bit.

** The following compression maps are used in Store() ONLY in the case of
** 64-bit hosts to repack structs to adjust for 64-bit->32-bit points in
** absolute mode LFS creation. These must match the definitions in lobject.h
*/
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks Terry! :)

} else if (S->mode == MODE_LFSA && pack) {
/*
* 64-bit hosts have two LFS modes. The default LFS mode is for writing to a
* when writing to native LFS where the address pointers are 64 bit and any
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a line missing here? Or is it just a mangled comment?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mangled will rewrite

@galjonsfigur
Copy link
Member

As I'm not that knowledgeable in Lua internals I can only test this PR. My observations:

  • My GCC (10.1.1) returned some warnings when compiling this code - I don't want to paste the whole log here so I uploaded the important parts to this Gist
  • The -f option from luac.cross could be depreciated first instead of being removed
  • Old way of using LFS with _init.luawon't work even with changes from Makes lfs/_init.lua compatible with Lua 5.3 #3168 - but it's not really needed with new LFS API.

@TerryE
Copy link
Collaborator Author

TerryE commented Sep 9, 2020

@galjonsfigur, thanks I will add these to the TODO list. In terms of flash output, there isn't any difference between an LC file and a Flash Image file. Hence the -f option is redundant. Are you suggesting that this is retained as a deprecated synonym for -o?

@galjonsfigur
Copy link
Member

Are you suggesting that this is retained as a deprecated synonym for -o?

Sorry for late reply - didn't notice it. As now -f option is redundant it could just print deprecation notice - that way it won't interfere with any existing scripts that use luac.cross

@nwf nwf added this to the Next release milestone Sep 29, 2020
@marcelstoer marcelstoer removed this from the Next release milestone Nov 3, 2020
@stale
Copy link

stale bot commented Apr 16, 2022

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale label Apr 16, 2022
@stale stale bot closed this Apr 30, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants