[LLVMdev] Scheme on LLVM IR

Tim Northover t.p.northover at gmail.com
Wed Aug 7 03:59:05 PDT 2013


> One especially problematic function seems to be APPLY. It takes a
> function and a list of values as parameters, and applies the function to
> the values [as in (apply + '(1 2 3)) -> (+ 1 2 3)]. So if varargs adhere
> to the standard ABI, I could just cast the function to varargs
> parameters, then call it, and (with the arity checking you described
> above), things should work correctly?

Probably not, unfortunately. The problem is that there's no LLVM
CallInst with a variable number of arguments at runtime. A couple of
ugly solutions spring to mind, but nothing very elegant.

You could have a maximum number of arguments and implement @apply
something like:

define %whatever @apply(i32 %numArgs, %valType %func, %valType %argList) {
  Check %numArgs = 2
  if (length %argList) = 0
    %callee = bitcast %valType %func to %whatever(i32)*
    call %whatever(i32)* %callee(i32 0)
  else if (length %argList) = 1
    %callee = bitcast %valType %func to %whatever(i32, %valType)*
    %arg1 = (extract from %argList)
    call %whatever(i32, %valType)* %callee(i32 1, %valType %arg1)
  ...
}

Another possibility would be to implement apply in assembly (that has
the advantage that you could put a loop in once you reach stack
arguments to support as many as are needed).

The only neat LLVM solution I can think of would be to use a less
efficient calling convention where arguments are really pointers off
somewhere else and every function has the prototype %whatever(i32
%numArgs, valType* %args).

But I've not actually implemented a dynamic language like Scheme.
Others around here have, and they may have better ideas. Fingers
crossed they'll pipe up.

Tim.



More information about the llvm-dev mailing list