lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On 15 August 2015 at 23:13, Dibyendu Majumdar <mobile@majumdar.org.uk> wrote:
> I have been pondering on the whole question of FFI interface for Ravi.
> Any solution has to also work for standard Lua 5.3. After some thought
> I decided that rather than creating an FFI solution, it would be more
> powerful and general if I created a Lua centric LLVM binding. I don't
> know yet how this will look like, but I am hoping that by end
> September I will have something to show.
>
> I want to get to a point eventually where one can not only use the
> LLVM bindings to interface with foreign C code without having to write
> C code, but also be able to write a JIT compiler for Lua in Lua!
>
> To start with, for the sake of expediency, I am creating this as a
> component of Ravi; but it will be Lua 5.3 compatible. I need to at
> some point factor it out as a separate library - but I will do this
> once I have something working.
>

Hi,

I am pleased to report that I have added another demo - this time we
have more than a 'hello world!'. The new demo is a fibonacci function.

The source is here:
https://github.com/dibyendumajumdar/ravi/blob/master/llvmbinding/fib.lua

The generated LLVM code looks like this:

define i32 @demofib(%struct.lua_State* %L) {
entry:
  %0 = call i64 @luaL_checkinteger(%struct.lua_State* %L, i32 1)
  %1 = trunc i64 %0 to i32
  %2 = call i32 @fib(i32 %1)
  %3 = zext i32 %2 to i64
  call void @lua_pushinteger(%struct.lua_State* %L, i64 %3)
  ret i32 1
}

define internal i32 @fib(i32) {
EntryBlock:
  %1 = icmp sle i32 %0, 2
  br i1 %1, label %return, label %recurse

return:                                           ; preds = %EntryBlock
  ret i32 1

recurse:                                          ; preds = %EntryBlock
  %2 = sub nsw i32 %0, 1
  %3 = tail call i32 @fib(i32 %2)
  %4 = sub nsw i32 %0, 2
  %5 = tail call i32 @fib(i32 %4)
  %6 = add nsw i32 %3, %5
  ret i32 %6
}

; Function Attrs: nounwind
declare i64 @luaL_checkinteger(%struct.lua_State*, i32) #0

; Function Attrs: nounwind
declare void @lua_pushinteger(%struct.lua_State*, i64) #0