lua without OS calls for embedded systems

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

lua without OS calls for embedded systems

Marc Scherer
Dear all,

I am trying to compile lua with the Renesas gcc 4.8.4 toolchain in
e2Studio (eclipse) for RX600 controllers and it fails with optilib
instead of newlib as standard library due to missing functions.

Also with newlib in debug mode I am getting linker errors about missing
symbols.

I only need math functions to do some scriptable operations, where the
script would be passed to lua as C-string.

My understanding is that for math scripting I would not need any
operating system related functions.

Is there documentation describing the changes needed for lua to take
away the OS calls, i.e. to make it independent from any operating system?

Or how to strip unused code parts/modules from lua in general?

Thanks,
Marc








Reply | Threaded
Open this post in threaded view
|

Re: lua without OS calls for embedded systems

Luiz Henrique de Figueiredo
> I am trying to compile lua with the Renesas gcc 4.8.4 toolchain in
> e2Studio (eclipse) for RX600 controllers and it fails with optilib
> instead of newlib as standard library due to missing functions.
>
> Also with newlib in debug mode I am getting linker errors about missing
> symbols.

Which missing functions?


Reply | Threaded
Open this post in threaded view
|

Re: lua without OS calls for embedded systems

Viacheslav Usov
In reply to this post by Marc Scherer
On Wed, Apr 12, 2017 at 12:01 PM, Marc Scherer <[hidden email]> wrote:

> I only need math functions to do some scriptable operations, where the script would be passed to lua as C-string.

Lua consists of the "core" language plus "standard" libraries. Bundled with it are a standalone compiler (luac.c) and a standalone interpreter (lua.c). Basic math operations are part of the language, plus there are some more in the math library, which you may or may not need. All the library code resides in *lib.c/h files, so at a minimum you can remove those and the compiler/interpreter from your build. That will eliminate some of the problems that you are facing.

The only other problematic stuff I can think of would then be the built-in loader, but that should be trivial to disable.

Cheers,
V.
Reply | Threaded
Open this post in threaded view
|

RE: lua without OS calls for embedded systems

benjamin.steinwender
In reply to this post by Marc Scherer
Hi,

we have successfully compiled Lua 5.3 for an embedded ARM Cortex-M4F with the following hints:

Remove lua.c and luac.c (no need for interpreter and compiler) and just use the C-API (described in the later chapters of the book).

Your base entry-point is luaconf.h.
However, a few items can (should?) be modified directly in the source:

If you are not requiring all libraries - you just have to comment out the respective lines in (linit.c).
You can also remove / uncomment several functions not needed from specific libraries (just follow the variables from here).

For the unresolved symbols:
To use math, you have to link the math library ("-lm" with gcc).
Probably you encounter a missing symbol for _gettimeofday - which is weakly defined in time.h.
It is used by the os_lib and by "luai_makeseed" in (lstate.c).

Either you provide your own version of this function (see below) or you remove os_lib (which you usually don't need on embedded controllers).
We have a RTC so we just provide the current time to Lua:

int _gettimeofday(struct timeval *tv, struct timezone *tz) {
    tv->tv_sec = RTC_seconds();
    tv->tv_usec = RTC_us();
}

-Benjamin


-----Original Message-----
From: [hidden email] [mailto:[hidden email]] On Behalf Of Marc Scherer
Sent: Wednesday, April 12, 2017 12:01 PM
To: [hidden email]
Subject: lua without OS calls for embedded systems

Dear all,

I am trying to compile lua with the Renesas gcc 4.8.4 toolchain in e2Studio (eclipse) for RX600 controllers and it fails with optilib instead of newlib as standard library due to missing functions.

Also with newlib in debug mode I am getting linker errors about missing symbols.

I only need math functions to do some scriptable operations, where the script would be passed to lua as C-string.

My understanding is that for math scripting I would not need any operating system related functions.

Is there documentation describing the changes needed for lua to take away the OS calls, i.e. to make it independent from any operating system?

Or how to strip unused code parts/modules from lua in general?

Thanks,
Marc








Reply | Threaded
Open this post in threaded view
|

Re: lua without OS calls for embedded systems

Luiz Henrique de Figueiredo
In reply to this post by Luiz Henrique de Figueiredo
For the record, here are the libc functions that the Lua core uses:

setjmp frexp pow strcpy time
abort localeconv snprintf strlen
abs longjmp strchr strpbrk
floor memcmp strcmp strspn
fmod memcpy strcoll strtod

The libraries need additional functions from libc.

See also this thread:
        http://lua-users.org/lists/lua-l/2015-10/msg00255.html

Reply | Threaded
Open this post in threaded view
|

Re: lua without OS calls for embedded systems

Russell Haley
In reply to this post by benjamin.steinwender
My Kenetis board thanks you for this information. :)

Has anybody experimented with eLua successfully? Building a Board Support Package seemed to be quite a daunting task...

Russ

Sent from my BlackBerry 10 smartphone on the Virgin Mobile network.
  Original Message  
From: [hidden email]
Sent: Wednesday, April 12, 2017 4:14 AM
To: [hidden email]
Reply To: Lua mailing list
Subject: RE: lua without OS calls for embedded systems

Hi,

we have successfully compiled Lua 5.3 for an embedded ARM Cortex-M4F with the following hints:

Remove lua.c and luac.c (no need for interpreter and compiler) and just use the C-API (described in the later chapters of the book).

Your base entry-point is luaconf.h.
However, a few items can (should?) be modified directly in the source:

If you are not requiring all libraries - you just have to comment out the respective lines in (linit.c).
You can also remove / uncomment several functions not needed from specific libraries (just follow the variables from here).

For the unresolved symbols:
To use math, you have to link the math library ("-lm" with gcc).
Probably you encounter a missing symbol for _gettimeofday - which is weakly defined in time.h.
It is used by the os_lib and by "luai_makeseed" in (lstate.c).

Either you provide your own version of this function (see below) or you remove os_lib (which you usually don't need on embedded controllers).
We have a RTC so we just provide the current time to Lua:

int _gettimeofday(struct timeval *tv, struct timezone *tz) {
tv->tv_sec = RTC_seconds();
tv->tv_usec = RTC_us();
}

-Benjamin


-----Original Message-----
From: [hidden email] [mailto:[hidden email]] On Behalf Of Marc Scherer
Sent: Wednesday, April 12, 2017 12:01 PM
To: [hidden email]
Subject: lua without OS calls for embedded systems

Dear all,

I am trying to compile lua with the Renesas gcc 4.8.4 toolchain in e2Studio (eclipse) for RX600 controllers and it fails with optilib instead of newlib as standard library due to missing functions.

Also with newlib in debug mode I am getting linker errors about missing symbols.

I only need math functions to do some scriptable operations, where the script would be passed to lua as C-string.

My understanding is that for math scripting I would not need any operating system related functions.

Is there documentation describing the changes needed for lua to take away the OS calls, i.e. to make it independent from any operating system?

Or how to strip unused code parts/modules from lua in general?

Thanks,
Marc









Reply | Threaded
Open this post in threaded view
|

Re: lua without OS calls for embedded systems

Roberto Ierusalimschy
In reply to this post by Luiz Henrique de Figueiredo
> For the record, here are the libc functions that the Lua core uses:
>
> setjmp frexp pow strcpy time
> abort localeconv snprintf strlen
> abs longjmp strchr strpbrk
> floor memcmp strcmp strspn
> fmod memcpy strcoll strtod

Also for the record, I have done a small "guide" about how to avoid
(or not) the above functions in the core.

* The following functions are easy to implement in ISO C:

- abs
- memcmp
- memcpy
- strchr
- strcmp
- strcoll   (can be replaced by strcmp if we ignore locales)
- strcpy
- strlen
- strpbrk
- strspn


* The following functions can be replaced by something else through macros:

- fmod (used only in 'luai_nummod' for the '%' operator)
- frexp (used only in 'l_hashfloat' to produce a hash for a floating-point
  for the hashing of strings)
- localeconv (used only in 'lua_getlocaledecpoint', which could evaluate
  always to some fixed decimal point)
- pow (used only in 'luai_numpow' for the '^' operator)
- time (used only in 'luai_makeseed' to produce an initial "random" seed
number)


* The following functions probably can be implemented in assembler:

- abort
- longjmp/setjmp (essential!)


* The following functions should be provided somehow (and may be
difficult to implement correctly):

- floor
- snprintf: used in very controlled ways, with exactly one format
  item in each use; the kernel needs '%p' (easy), '%d' (easy), and
  '%g' (not trivial)
- strtod

-- Roberto


Reply | Threaded
Open this post in threaded view
|

Re: lua without OS calls for embedded systems

Jay Carlson
In reply to this post by Russell Haley
On Apr 13, 2017, at 5:02 PM, Russell Haley <[hidden email]> wrote:

>
> Has anybody experimented with eLua successfully? Building a Board Support Package seemed to be quite a daunting task...

I gave up and worked on other things, and stopped looking at it. So the following is based on when I stopped looking, which appears to be 2015-06-19. The last github commit on master is before that, so I assume things have not changed in terms of the public project.

eLua is doing some important stuff, so I regret that they first hear my complaint broadcast to lua-l instead of privately. In my defense, I was very frustrated at the time--this probably says more about me than eLua. :-)

I wish I could write a null BSP (me writing it, not upstream writing it) and have it work with newlib without further knowledge of the hardware; then I could refine incrementally. But eLua is not my project; if you know the hardware and plan to support a lot of features, you might as well do all of it at once.

It might be worthwhile to delegate toolchains to platformio.org. I love the build system for simple stuff and for other tooling: someone has resolved “the tyranny of choice” for me.

Jay