LLVM中的部分应用

alt*_*ler 5 compiler-construction llvm compiler-development llvm-ir

我正在尝试创建一个函数“add”,它可以应用于单个参数,然后是另一个。我不知道如何用 LLVM IR 表示这一点,因为我不明白如何使用单个值调用函数,然后将该值保存在内存中的某处并返回另一个应用于该 val 的函数。我需要在 LLVM 中使用某种关闭机制。

我已经在 C 中搜索了这个实现,以便我可以通过 clang 查看发出的 LLVM,但是我发现的解决方案非常复杂,所以我想我可以直接调查 LLVM。

这将是未经处理的版本

define i8 @add(i8 %a, i8 %b) {
entry:
  %res = add i8 %a, %b
  ret i8 %res
}
Run Code Online (Sandbox Code Playgroud)

不知何故,我想add(1)返回一个i8 (i8)类型。我想我必须以某种方式拆分该功能。

附:我正在研究这个,因为我正在研究一个小型函数式语言的编译器,所以我正在寻找任何关于编译器设计中部分应用程序/柯里化的实现的建议。

更新:我现在有以下代码工作,但它非常复杂,我认为自动生成并不容易

declare i32 @printf(i8* noalias nocapture, ...)

define { i8, i8 (i8, i8) * } @add1(i8 %a) {
  ; allocate the struct containing the supplied argument 
  ; and a function ptr to the actual function
  %nextPtr = alloca { i8, i8 (i8, i8) * }
  store { i8, i8 (i8, i8) * } { i8 undef, i8 (i8, i8) * @add2 }, { i8, i8 (i8, i8) * } * %nextPtr
  %next0 = load { i8, i8 (i8, i8) * } * %nextPtr

  ; insert the supplied arg into the struct
  %next1 = insertvalue { i8, i8 (i8, i8) * } %next0, i8 %a, 0
  ret { i8, i8 (i8, i8) * } %next1
}

define i8 @add2(i8 %a, i8 %b) {
  %res = add i8 %a, %b
  ret i8 %res
}

define i8 @main() {
  ; call add(35) resulting in 'fn' of type {35, &add2}
  %res1 = call { i8, i8 (i8, i8) * } @add1(i8 35)

  ; get the arg of the first call, ie element 0 of the resulting struct
  %arg = extractvalue { i8, i8 (i8, i8) * } %res1, 0
  ; similarily get the function ptr
  %fn = extractvalue { i8, i8 (i8, i8) * } %res1, 1

  ; apply the argument to the function
  %res = call i8 %fn(i8 %arg, i8 30)

  ; print result  
  %ptr = alloca i8
  store i8 %res, i8* %ptr
  call i32 (i8*, ...)* @printf(i8* %ptr)

  ret i8 0
}
Run Code Online (Sandbox Code Playgroud)

Fra*_* C. 4

我编写了这个示例 C 代码来模拟我的函数语言编译器支持的“部分应用程序”(lambda 演算)。我不发出“C”代码,而是直接发出到 LLVM-IR。您只需从示例源发出即可看到 LLVM-IR 视角:

clang -S -emit-llvm partial.c
Run Code Online (Sandbox Code Playgroud)

当编译器传递反映括号内的(给出或获取一些附加细节)表达式的 AST 节点时,编译器被触发以发出 LLVM-IR 中的部分机制。

希望这可以帮助。