Passing function value as a function argument

I am building a compiler for Scheme using LLVM with the Rust programming language. I would like to know if I can pass “Function Value” type as a function parameter? This is required for me to pass a function object as a parameter to a function call.

Here is the link to the relevant code.

I got confused with function value and object, but there is a guide for mapping higher-level constructs to LLVM IR:
https://mapping-high-level-constructs-to-llvm-ir.readthedocs.io/en/latest/advanced-constructs/lambda-functions.html

LLVM, similarily to C, doesn’t directly support passing function as arguments, but rather allows passing function pointers and calling function pointers.
So you’d essentially have a ptr parameter and use the call instruction with the ptr parameter as callee argument.

That said, I am guessing that for Scheme you’ll not only need indirect calls but also fully fledged closures. These are more involved to generate, as you’ll likely have to generate a struct type and instance containing the environment + the function pointer.

Either way, by searching for closure or lambda compilation (specifically closure conversion) you ought to find some more resources for that.
I found a LLVM dev talk from 2017 that on first glance illustrates closure conversion very well (how it translates them to structs), even if using C and C++ “pseudo” code: https://llvm.org/devmtg/2017-03//assets/slides/effective_compilation_of_higher_order_programs.pdf
You ought to be able to map it to LLVM relatively nicely however.

1 Like