An Overview of the C/Lua Interface. Many programming languages are distributed in the form of a language-specific compiler or interpreter. Lua, on the other hand, is essentially distributed as a C API.
lua2c is a Lua module and utility to convert Lua 5.1 source code to C API code.
This utility converts a given Lua source file into an equivalent C source file written in terms of Lua C API calls. At least, this works for a large subset of the Lua language (see limitations below).
The compiler is written entirely in Lua, and no build/install is needed. This project reuses Metalua's gg/mlp parser to convert Lua source to a Metalua-style  AST over which the compiler then operates. lua2c does not require Metalua itself though since gg/mlp is bundled in the distribution (patched metalua-0.4.1-rc1) and is written in pure Lua.
which generates a C file similar to as shown here: [bisect.c].
You may also use the shell script 'clua' to compile Lua->C->machine code and execute all in one step. However, you may need to edit the variables in the file to match your system since this utility invokes the C compiler.
lua2c can even compile itself! (Note: the -c option compiles only without running.)
- luac2c - This related effort by Hisham Muhammad  converts Lua bytecodes to C source, whereas this project converts Lua source to C source. luac2c runs into a few similar limitations as given below. luac2c has remained experimental but superseded Luiz Henrique De Figueiredo's very basic but similarly named lua2c for Lua 4.0 .
- luagen++  uses C++ expression templates to translate Lua-like C++ statements to C API calls. Some of the code generation structure is actually fairly similar to lua2c.
- Python Pyrex  does something similar in Python but has the added goal of lowering the boundary between C and Python code. Something like that could be done with lua2c, especially since lua2c uses the extensible gg parser.
- Clue by David Given  does the opposite: convert C source to Lua source.
- luac + BinToCee  allow compilation of Lua source to bytecodes and/or embedding in a C file.
- LuaInterpreterInLua  - is the Lua interpreter (lua.c) reimplemented in Lua. It can be compiled to C via lua2c (e.g. for scaffolding).
I think this project not only is theoretically nice to have but has a number of potential uses:
- Provide another approach of compiling Lua to machine code (rather than luac + BinToCee).
- Streamline the interface between C and Lua code and allow efficient access to C datatypes from Lua (see Pyrex above).
- Compile Lua to optimized C. For example, by statically determining that certain variables are used in a restricted way (e.g. by decorating the Lua source file with pragmas or determining this implicitly by inference), certain code constructs might be simplified to use plain C rather that the Lua C API. This could allow certain Lua code written with sufficient care to run at native speeds. Since it compiles to C, it will even work on CPUs where LuaJIT is not available.
WARNING: This code passes much of the Lua 5.1 test suite  and can compile itself, but the code is new and there can still be errors. In particular, a few language features (e.g. coroutines) are not implemented. See comments in lua2c.lua for details. Please report bugs/patches on the wiki.
lua2c does not currently support coroutines, functions that normally reject C functions (e.g. setfenv), and possibly tail call optimizations. Not all of these have the exact analogue in C. Coroutines might not ever be supported. However, some solutions might be explored , including possibly generating code that maintains the coroutine context in Lua tables.
Closures and upvalues are implemented, but creating and accessing upvalues is somewhat slow due to the implementation (see implementation notes below) and hopefully can be improved.
Now that the code is fairly complete/robust, more attention can be given to optimizing the code generation. Performance was 25%-75% of regular Lua when running a few tests , but hopefully future optimizations will improve that.
Lua 5.2 Notes
- LuaFiveTwo (as of 5.2.0-work4) deprecates getfenv and setfenv, which eliminates one of the limitations above.
- LuaFiveTwo has new lua_arith and lua_compare C API functions, which eliminate the need for lua2c to reimplement these functions.
- LuaFiveTwo also has new lua_yieldk, lua_callk, and lua_pcallk functions for coroutines and might help to implement coroutines in lua2c.
- lua2c might implement upvalues as C function upvalues rather than tables, given LuaFiveTwo implements lua_upvaluejoin/lua_setupvalue/lua_setupvalue, so C functions can share upvalues directly.
- Concerning tail calls, see .
- Latest development source (recommended): http://github.com/davidm/lua2c/ . From here you can browse the source, download a tar/zip snapshot, or checkout with git by running '
git clone git://github.com/davidm/lua2c.git'.
- Last release distribution: [lua2c-0.2.1-20080802.tar.gz]; Older versions: [achive]
The project page is currently http://lua-users.org/wiki/LuaToCee .
(c) 2008 DavidManura. Licensed under the same terms as Lua (MIT license). See included LICENSE file for full licensing details. Please post any patches/improvements.
Some implementation notes will likely go here.
Important topics include
- Implementing closures with upvalues in C. The current approach is to store local variables that are used as upvalues in a Lua table scoped to the block (lc_newclosuretable). Utility functions lc_getupvalue/lc_setupvalue access it.
- Supporting vararg (...) functions. Mainly, this just requires allowing the stack to be shifted by an arbitrary amount (lc_nextra).
- The transformation process: Lua code string -> Lua AST -> C AST -> C code string
- Implementation of utility functions not in the Lua C API (e.g. arithmetic metamethod handlers, such as lc_add to implement __add to add two numbers while handling metamethods).
- Keeping track of lexical scope (currentscope table)
- Keeping track of the stack (idxtop)
- Handling expressions that evaluate to 0, 1, or multiple return values
- Handling expressions not on the top of the stack (where is 'onstack' v.s. 'anywhere')
- Constant folding
- Possible optimization tricks (not yet implemented):
- Removing metamethods calls when it can be inferred they won't be triggered.
- Other code elimination.
- Using internal interfaces rather than the C API?
- Building constant tables?
This project could be significantly helped by the addition of a global (per-file) code optimizer. Most importantly, some type of interprocedural data flow analysis could infer the basic data types of many expressions in code (e.g. this value is a number, has no metatable, is a constant with a certain value, or is positive), especially when that code makes extensive use of local (lexical) variables. Alternately, special comments ('--!' like in LuaInspect) could be supported to allow the programmer to inject this information. If the AST is decorated with this information, the code generator could replace generic code with more specific code. For example, the Lua code 'a+b' normally translates to values a and b on the Lua stack, with __add metamethod lookups. However, if a and b are known to be numbers, this can be translated directly to the C code 'a+b' where a and b are C doubles on the C stack. There is no reason we shouldn't be able to achieve this type of transformation:
There may be some overlap with LuaJIT and other projects here . For initial work in this area, see SourceOptimizer and LuaInspect.
Some syntax for embedding C expressions in-line to Lua code should also be high on the to-do list (as in Pyrex).
Coroutines should be supported, particularly given the enhancements in 5.2 (e.g. lua_callk).
Update to Lua 5.2 is recommended (see 5.2 notes above).
Unfortunately, due to time and priorities, I'm not currently actively maintaining this, besides perhaps the occasional simple bug fix, or perhaps if paid to do so. If you want to take over maintenance of this and correct the above limitations, please do so.
This package possibly would be better renamed 'lua2capi' to emphasize the fact that it generates C code in the form of Lua C API calls. Another option would be to generate C code in the form of Lua internals.
-  Metalua AST - http://metalua.luaforge.net/manual006.html#toc17
-  luac2c (previously named luatoc) - LuaList:2006-07/msg00144.html
-  LHF's lua2c for Lua 4.0 - LuaList:2002-01/msg00296.html
-  luagen++ - LuaGenPlusPlus
-  Pyrex - http://www.cosc.canterbury.ac.nz/greg.ewing/python/Pyrex/
-  Clue - http://cluecc.sourceforge.net/
-  Lua 5.1 test suite - http://www.inf.puc-rio.br/~roberto/lua/lua5.1-tests.tar.gz
-  [Wikipedia:Coroutine - Implementations for C]
-  [Coco] - True C coroutine semantics (used in LuaJIT 1.x)
-  BinToCee
-  [The Computer Language Benchmarks Game]
-  LuaInterpreterInLua - Lua interpreter (lua.c) reimplemented in Lua.
- LuaImplementations - other source translators and Lua reimplementations
Lua To C Compiler Downloadedit · history
Last edited September 1, 2010 7:05 am GMT (diff)
LuaJIT is a Just-In-Time Compiler (JIT) for theLua programming language.Lua is a powerful, dynamic and light-weight programming language.It may be embedded or used as a general-purpose, stand-alone language.
LuaJIT is Copyright © 2005-2021 Mike Pall, released under theMIT open source license.
|PS3||PS4||PS Vita||Xbox 360|
|+ JIT||+ BitOp||+ FFI||Drop-in|
Lua To C Compiler Free
LuaJIT has been successfully used as a scripting middleware ingames, appliances, network and graphics apps, numerical simulations,trading platforms and many other specialty applications. It scales fromembedded devices, smartphones, desktops up to server farms. It combineshigh flexibility with high performanceand an unmatched low memory footprint.
LuaJIT has been in continuous development since 2005. It's widelyconsidered to be one of the fastest dynamic languageimplementations. It has outperformed other dynamic languages on manycross-language benchmarks since its first release — often by asubstantial margin.
For LuaJIT 2.0, the whole VM has been rewritten from the ground upand relentlessly optimized for performance. It combines a high-speedinterpreter, written in assembler, with a state-of-the-art JITcompiler.
An innovative trace compiler is integrated with advanced,SSA-based optimizations and highly tuned code generation backends.A substantial reduction of the overhead associated with dynamic languagesallows it to break into the performance range traditionally reserved foroffline, static language compilers.
Please select a sub-topic in the navigation bar to learn more about LuaJIT.