Adding a function in LLVM (haskell bindings) when the number of parameters is not known at compile time

别等时光非礼了梦想. 提交于 2019-12-12 07:37:16

问题


Background: I have written a toy Lisp interpreter that I am trying to add LLVM JIT functionality to. For the moment, have imposed the following limitations:

  • Only integer values are allowed in functions
  • Variables may only reference formal parameters

Given:

compile :: [Value] -- List of Formal Parameters
        -> [Value] -- Body of function
        -> CodeGenModule(Function a)`

Question: How do I generate a function where the number of parameters equals the length of the Formal Parameters list?


回答1:


I don't know if it's possible to do this with just the EDSL from LLVM.Core (the types are indeed quite hairy), but if you look at LLVM.FFI.Core, you'll find the lower-level functionality for manipulating LLVM function types and creating LLVM functions. This leads to the following plan of action:

  • Create a TypeRef for your function with functionType.
  • Add this function to your module using addFunction.
  • Populate it with basic blocks by using appendBasicBlock/insertBasicBlock. Use getParam to reference function arguments inside your basic blocks.

It should be possible to utilise the nice monadic EDSL syntax for constructing the basic blocks. Also, look at LLVM.Core.Util, it contains some helper functions for working with the FFI layer.



来源:https://stackoverflow.com/questions/6448705/adding-a-function-in-llvm-haskell-bindings-when-the-number-of-parameters-is-no

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!