Cross-compiling Lua byte code for specific target hardware

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

Cross-compiling Lua byte code for specific target hardware

Matthias
Hi,

in search of an example on how to cross-compile Lua byte code for a specific target architecture, I came across this post: http://lua-users.org/lists/lua-l/2006-06/msg00205.html. We use Lua 5.3.4 in a project which relies on Yocto in order to build a customized Linux operating system image for our target hardware – an ARM Cortex A7 32bit processor. On that hardware, we run some applications that are implemented in Lua. During the build process, we want to compile our Lua application script files into byte code for the mentioned target machine. That is, luac shall generate byte code which can run on the Cortex A7 processor. The whole build process, especially luac, is automatically executed on a 64bit-Ubuntu-based build server backend in which we cannot integrate the target hardware for compilation. This is why we need to have a cross-compilation capable version of luac in our Yocto build system.

In order to realize that, I would like to patch the Lua sources accordingly. Unfortunately, I was not able to find any concrete example which demonstrates on how to change the existing code base for a specific target architecture. I already had a look at the ldump.c file as mentioned in the post above. As far as I understand I (only) have to adapt DumpBlock() since any data produced by the higher-layer functions finally go through this function. However, I am not sure how to exactly do that. Is there any concrete example available that demonstrates the required steps?

Thank you very much in advance.

With kind regards,
Matthias
Reply | Threaded
Open this post in threaded view
|

Re: Cross-compiling Lua byte code for specific target hardware

Luiz Henrique de Figueiredo
> We use Lua 5.3.4 in a project which relies on Yocto in order to build a customized Linux operating system image for our target hardware – an ARM Cortex A7 32bit processor. On that hardware, we run some applications that are implemented in Lua. During the build process, we want to compile our Lua application script files into byte code for the mentioned target machine. That is, luac shall generate byte code which can run on the Cortex A7 processor. The whole build process, especially luac, is automatically executed on a 64bit-Ubuntu-based build server backend in which we cannot integrate the target hardware for compilation. This is why we need to have a cross-compilation capable version of luac in our Yocto build system.

One route is to change ldump.c in Lua 5.3 as follows:
- Save the correct ARM values in DumpHeader, especially the sizeof ones.
- Write DumpVector as a function to match the endianness of your ARM processor:

      #define DumpVector(v,n,D)       DumpVectorARM(v,n,sizeof((v)[0]),D)

where DumpVectorARM is a loop that copies each item to a local buffer
in the correct endianness of your ARM processor and calls DumpBlock on
that buffer.

See http://lua-users.org/lists/lua-l/2006-02/msg00507.html for similar
code for loading, instead of dumping.

You'll need to know how floating point numbers are represented in your
ARM processor. Beware that it may use a strange endianness, I don't
know.

Perhaps there are people here in lua-l that know all these details
about that ARM processor.

Reply | Threaded
Open this post in threaded view
|

Re: Cross-compiling Lua byte code for specific target hardware

BogdanM
In reply to this post by Matthias


On Mon, Apr 1, 2019 at 7:03 AM Matthias <[hidden email]> wrote:
Hi,

in search of an example on how to cross-compile Lua byte code for a specific target architecture, I came across this post: http://lua-users.org/lists/lua-l/2006-06/msg00205.html. We use Lua 5.3.4 in a project which relies on Yocto in order to build a customized Linux operating system image for our target hardware – an ARM Cortex A7 32bit processor. On that hardware, we run some applications that are implemented in Lua. During the build process, we want to compile our Lua application script files into byte code for the mentioned target machine. That is, luac shall generate byte code which can run on the Cortex A7 processor. The whole build process, especially luac, is automatically executed on a 64bit-Ubuntu-based build server backend in which we cannot integrate the target hardware for compilation. This is why we need to have a cross-compilation capable version of luac in our Yocto build system.

In order to realize that, I would like to patch the Lua sources accordingly. Unfortunately, I was not able to find any concrete example which demonstrates on how to change the existing code base for a specific target architecture. I already had a look at the ldump.c file as mentioned in the post above. As far as I understand I (only) have to adapt DumpBlock() since any data produced by the higher-layer functions finally go through this function. However, I am not sure how to exactly do that. Is there any concrete example available that demonstrates the required steps?

Thank you very much in advance.

With kind regards,
Matthias

Hi,

Take a look at https://github.com/elua/elua/commit/810b787ec319f8823b7e4741f50d30eef0b02646. That was made for Lua 5.1 with the intent of cross-compiling Lua code to different architectures. Most of that commit is not my code, but I can't find the original Lua patch anymore. It shouldn't be too difficult to make that work for Lua 5.3.

HTH,
Bogdan

Reply | Threaded
Open this post in threaded view
|

Re: Cross-compiling Lua byte code for specific target hardware

Russell Haley
In reply to this post by Matthias
Hi,

Sorry for the top post. Is there a reason you don't just cross compile luac then run it in qemu to generate the Arm byte code?

https://www.qemu.org/

Cheers
Russ

Sent from my BlackBerry 10 smartphone on the Virgin Mobile network.
From: Matthias
Sent: Sunday, March 31, 2019 11:03 PM
Reply To: Lua mailing list
Subject: Cross-compiling Lua byte code for specific target hardware

Hi,

in search of an example on how to cross-compile Lua byte code for a specific target architecture, I came across this post: http://lua-users.org/lists/lua-l/2006-06/msg00205.html. We use Lua 5.3.4 in a project which relies on Yocto in order to build a customized Linux operating system image for our target hardware – an ARM Cortex A7 32bit processor. On that hardware, we run some applications that are implemented in Lua. During the build process, we want to compile our Lua application script files into byte code for the mentioned target machine. That is, luac shall generate byte code which can run on the Cortex A7 processor. The whole build process, especially luac, is automatically executed on a 64bit-Ubuntu-based build server backend in which we cannot integrate the target hardware for compilation. This is why we need to have a cross-compilation capable version of luac in our Yocto build system.

In order to realize that, I would like to patch the Lua sources accordingly. Unfortunately, I was not able to find any concrete example which demonstrates on how to change the existing code base for a specific target architecture. I already had a look at the ldump.c file as mentioned in the post above. As far as I understand I (only) have to adapt DumpBlock() since any data produced by the higher-layer functions finally go through this function. However, I am not sure how to exactly do that. Is there any concrete example available that demonstrates the required steps?

Thank you very much in advance.

With kind regards,
Matthias

Reply | Threaded
Open this post in threaded view
|

Re: Cross-compiling Lua byte code for specific target hardware

Egor Skriptunoff-2
In reply to this post by Matthias
On Mon, Apr 1, 2019 at 9:03 AM Matthias wrote:

how to cross-compile Lua byte code for a specific target architecture


There is a tool to convert Lua 5.3 bytecode files from one platform to another.
https://lua-bytecode.github.io/
The converter itself is written in pure Lua (and runs under Fengari),
so you can use it as a stand-alone application after a small modification:
extract .lua file from this web-page
and add few lines of Lua code (to read input file and write output file).
 
 
The converter has the following limitations:
 
1. On each platform sizeof(int) must be either 4 or 8
(this size might be different on source and destination platforms)
 
2. On each platform sizeof(size_t) must be either 4 or 8
(this size might be different on source and destination platforms)
 
3. On each platform Lua 5.3 floating point numbers must be IEEE-754-compatible:
either 32-bit float or 64-bit double
(float size might be different on source and destination platforms)
 
4. On each platform Lua 5.3 integers must be either int32 or int64
(integer size might be different on source and destination platforms)
 
5. Mixed endian platforms are not supported:
each platform must be either little-endian or big-endian
(endianness might be different on source and destination platforms)
 
6. Exotic platforms are not supported:
on each platform endianness of integers must be the same as endianness of floats
 
 
Prior to convert a bytecode file you must determine the "EnBi" (endianness + bitness)
of the destination platform.

How to determine EnBi:
Just open (in the bytecode viewer) any bytecode file created on the destination platform:
the EnBi (string of length 5) will be displayed on the second line.
Even the simplest bytecode would be enough:
run "luac -" with empty stdin and take "luac.out"