代码之家  ›  专栏  ›  技术社区  ›  rpjohnst

llvm:将指针传递给结构(该结构包含指向函数的指针)和jit函数

  •  2
  • rpjohnst  · 技术社区  · 16 年前

    我有一个LLVM(2.7版)模块,它的函数接受一个指向结构的指针。该结构包含一个指向C++函数的函数指针。模块函数将被JIT编译,并且我需要使用LLVM API在C++中构建该结构。我似乎无法将指向函数的指针作为llvm值,更不用说将指针传递给我无法构建的constantstruct。

    我不确定我是否在赛道上,但这就是我目前为止所拥有的:

    void print(char*);
    
    vector<Constant*> functions;
    functions.push_back(ConstantExpr::getIntToPtr(
        ConstantInt::get(Type::getInt32Ty(context), (int)print),
        /* function pointer type here, FunctionType::get(...) doesn't seem to work */
    ));
    ConstantStruct* struct = cast<ConstantStruct>(ConstantStruct::get(
        cast<StructType>(m->getTypeByName("printer")),
        functions
    ));
    
    Function* main = m->getFunction("main");
    vector<GenericValue> args;
    args[0].PointerVal = /* not sure what goes here */
    ee->runFunction(main, args);
    
    1 回复  |  直到 16 年前
        1
  •  1
  •   rpjohnst    16 年前

    事实上,永远不会。我不会使用LLVM API,只需传递一个与LLVM结构类型的布局相匹配的C++结构。忽略该代码的第一位,并将args[0].pointerval设置为指向该结构的指针。

    推荐文章