Discussion:
Crazy idea: compiling C to Lua
Rena
2013-03-13 11:31:31 UTC
Permalink
Just had an interesting thought: could you write a compiler in Lua,
that compiles C into Lua bytecode or source code?

I think you'd need a C module (compiled to native code) that provides
Lua the ability to do some of the things C can do, such as call
syscalls and allocate raw memory blocks (char*s), but once you have
that framework in place, it could be possible to compile C programs
into Lua code, that would then only rely on Lua and that "framework"
module.

Or have I finally gone off the deep end?
--
Sent from my Game Boy.
steve donovan
2013-03-13 11:37:58 UTC
Permalink
Post by Rena
Or have I finally gone off the deep end?
Teetering on the edge, but not quite off ;)

What about generating LuaJIT source/bytecode? Then you can use the
FFI to do the C-like things we all hate and love.

Although, it's not a direction we usually think of!
Nikolay Zapolnov
2013-03-13 11:46:52 UTC
Permalink
Hi!

Maybe this is not exactly what you want, but there is a CLUE:
http://cluecc.sourceforge.net/

It can compile C to Lua, Python, Perl, Java and Javascript.

The project seems abandoned, but it can help to understand how the
translation could be made.
Post by Rena
Just had an interesting thought: could you write a compiler in Lua,
that compiles C into Lua bytecode or source code?
I think you'd need a C module (compiled to native code) that provides
Lua the ability to do some of the things C can do, such as call
syscalls and allocate raw memory blocks (char*s), but once you have
that framework in place, it could be possible to compile C programs
into Lua code, that would then only rely on Lua and that "framework"
module.
Or have I finally gone off the deep end?
--
Sent from my Game Boy.
--
ó Õ×ÁÖÅÎÉÅÍ,
ôÅÈÎÉÞÅÓËÉÊ ÄÉÒÅËÔÏÒ ËÏÍÐÁÎÉÉ my-apps.com
úÁÐÏÌØÎÏ× îÉËÏÌÁÊ

http://www.my-apps.com/
mail: ***@gmail.com
skype: nzapps
http://www.facebook.com/nzapps
http://vk.com/nikolayz
David Given
2013-03-13 11:54:00 UTC
Permalink
Nikolay Zapolnov wrote:
[...]
Post by Nikolay Zapolnov
http://cluecc.sourceforge.net/
It can compile C to Lua, Python, Perl, Java and Javascript.
The project seems abandoned, but it can help to understand how the
translation could be made.
Not quite. I was finally prodded into getting it working again just the
other day, and I updated the Lua backend so that it uses the new Lua 5.2
goto statement. (It's not checked in yet.) Here are the new results of
running the whetstone benchmark:

gcc directly: 2582
clue -> LuaJIT: 2482
clue -> C -> gcc: 2361
clue -> Java: 784
clue -> Javascript: 105
clue -> Perl: 2.7

The top one is what you get if you compile the benchmark directly with
gcc. I'm afraid I don't have a Lua interpreter value because I don't
have a luasocket for Debian Lua 5.2 yet. The Javascript interpreter is V8.

That's all very preliminary and may in fact be wrong --- it's all work
in progress. Assuming the results are correct, the LuaJIT score is
remarkable. Adding some more LuaJIT-specific optimisations --- using FFI
types, for example --- ought to speed it up some more.
--
┌───  ───── http://www.cowlark.com ─────
│
│ 𝕻𝖍'𝖓𝖌𝖑𝖚𝖎 𝖒𝖌𝖑𝖜'𝖓𝖆𝖋𝖍 𝕮𝖙𝖍𝖚𝖑𝖍𝖚 𝕜'𝖑𝖞𝖊𝖍
𝖜𝖌𝖆𝖍'𝖓𝖆𝖌𝖑 𝖋𝖍𝖙𝖆𝖌𝖓.
│
sushil kumar
2013-03-13 11:55:54 UTC
Permalink
Emscripten compiles llvm byte code to javascript. May want to look at that
to get an idea.

Sushil
Post by Nikolay Zapolnov
Hi!
http://cluecc.sourceforge.net/
It can compile C to Lua, Python, Perl, Java and Javascript.
The project seems abandoned, but it can help to understand how the
translation could be made.
Post by Rena
Just had an interesting thought: could you write a compiler in Lua,
that compiles C into Lua bytecode or source code?
I think you'd need a C module (compiled to native code) that provides
Lua the ability to do some of the things C can do, such as call
syscalls and allocate raw memory blocks (char*s), but once you have
that framework in place, it could be possible to compile C programs
into Lua code, that would then only rely on Lua and that "framework"
module.
Or have I finally gone off the deep end?
--
Sent from my Game Boy.
--
ó Õ×ÁÖÅÎÉÅÍ,
ôÅÈÎÉÞÅÓËÉÊ ÄÉÒÅËÔÏÒ ËÏÍÐÁÎÉÉ my-apps.com
úÁÐÏÌØÎÏ× îÉËÏÌÁÊ
http://www.my-apps.com/
skype: nzapps
http://www.facebook.com/nzapps
http://vk.com/nikolayz
Sean Conner
2013-03-13 15:41:25 UTC
Permalink
Post by Rena
Just had an interesting thought: could you write a compiler in Lua,
that compiles C into Lua bytecode or source code?
I think you'd need a C module (compiled to native code) that provides
Lua the ability to do some of the things C can do, such as call
syscalls and allocate raw memory blocks (char*s), but once you have
that framework in place, it could be possible to compile C programs
into Lua code, that would then only rely on Lua and that "framework"
module.
I wrote Lua bindings to TCC [1][2] so now I can compile C code from within
Lua. I then wrote another module [3] to provide a better interface over the
TCC module, as well as extend require() to allow the loading of Lua modules
from C source (it compiles them directly into memory). It's not quite what
you are asking for, but ...

-spc (Sigh ... I really need to talk about these modules a bit more ... )

[1] http://en.wikipedia.org/wiki/Tiny_C_Compiler
It's a C compiler that's a library.

[2] https://github.com/spc476/lua-conmanorg/blob/master/src/tcc.c

[3] https://github.com/spc476/lua-conmanorg/blob/master/lua/cc.lua
Sean Conner
2013-03-13 16:02:12 UTC
Permalink
Post by Sean Conner
I wrote Lua bindings to TCC [1][2] so now I can compile C code from within
Lua. I then wrote another module [3] to provide a better interface over the
TCC module, as well as extend require() to allow the loading of Lua modules
from C source (it compiles them directly into memory). It's not quite what
you are asking for, but ...
And you can see it used in my JSON parser:
https://github.com/spc476/LPeg-Parsers/blob/master/json.lua
Post by Sean Conner
[1] http://en.wikipedia.org/wiki/Tiny_C_Compiler
It's a C compiler that's a library.
[2] https://github.com/spc476/lua-conmanorg/blob/master/src/tcc.c
[3] https://github.com/spc476/lua-conmanorg/blob/master/lua/cc.lua
-spc
steve donovan
2013-03-14 07:45:55 UTC
Permalink
Post by Sean Conner
-spc (Sigh ... I really need to talk about these modules a bit more ... )
I know the feeling ;) But David's results show that a C->LuaJIT
translator would probably be faster than TCC.

I have an old C++ interpreter project, and the bytecode performance
sucks. LuaJIT would be a great target, but whenever I look at the
compiler backend I get an instant headache (the front-end is not a
work of beauty either ;)) I'm so relieved that the clang people have
an interative C++ compiler project.

BTW, Peter D's batteries distribution has the luadist executable, and
one of the available binary packages is .. tcc. Having that stuff
available (especially on systems without package managers) is so nice.

steve d.
Sean Conner
2013-03-14 15:40:15 UTC
Permalink
Post by steve donovan
Post by Sean Conner
-spc (Sigh ... I really need to talk about these modules a bit more ... )
I know the feeling ;) But David's results show that a C->LuaJIT
translator would probably be faster than TCC.
Well, TCC was used in a project to boot Linux, from source, in under ten
seconds. It's a fast compiler ...

-spc (Okay, so the code it produces is a bit sub-optimal 8-P

Continue reading on narkive:
Loading...