当在编译时未知参数数量时,在LLVM(haskell绑定)中添加函数 [英] Adding a function in LLVM (haskell bindings) when the number of parameters is not known at compile time

查看:201
本文介绍了当在编译时未知参数数量时,在LLVM(haskell绑定)中添加函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景:我写了一个玩具Lisp解释器,我试图添加LLVM JIT功能。目前,已施加以下限制:




  • 仅允许在函数中使用整数值

  • 变量只能引用形式参数
  •   compile :: [Value]  - 正式参数列表
    - > [Value] - 函数体
    - > CodeGenModule(function a)`

    问题:如何生成函数其中参数数量等于形式参数列表的长度?

    解决方案

    我不知道是否可以做只是EDSL从 LLVM.Core (类型确实很毛茸茸),但如果你看看 LLVM.FFI.Core ,您会发现操纵LLVM的低级功能函数类型创建LLVM函数。这导致了以下计划:




    • 为您的函数创建 TypeRef functionType

    • 使用 addFunction 将此函数添加到模块。 / li>
    • 使用 appendBasicBlock / insertBasicBlock 填充基本块。使用 getParam 来引用基本块中的函数参数。



    可能利用好的一元EDSL语法来构造基本块。另外,请查看 LLVM.Core.Util ,它包含一些用于使用FFI层的帮助函数。


    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?

    解决方案

    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.

    这篇关于当在编译时未知参数数量时,在LLVM(haskell绑定)中添加函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆