5

我正在尝试创建一个可以应用于单个参数的函数“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
}

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

附言。我正在研究这个,因为我正在为一种小型功能语言开发编译器,所以我正在寻找任何关于在编译器设计中实现部分应用程序/currying 的建议。

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

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
}
4

1 回答 1

4

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

clang -S -emit-llvm partial.c

当编译器通过 AST 节点时,会触发编译器发出 LLVM-IR 中的部分机制,以反映括号中包裹的(提供或获取一些额外的细节)表达式。

希望这可以帮助。

于 2017-09-10T13:53:39.220 回答