【问题标题】:How to call function in LLVM如何在 LLVM 中调用函数
【发布时间】:2015-04-27 18:33:44
【问题描述】:

我想请教一下将自己的 c++ 函数调用/绑定到 LLVM 中的正确方法。

我编写了简单的函数:

void writeSomething() {
    std::cout << "Awesome" << std::endl;
} 

在 LLVM 中,我正在尝试注册该函数。我已经创建了到它的外部链接。

      // Void type
llvm::FunctionType* fccType =
        llvm::FunctionType::get(
            llvm::Type::getVoidTy(getGlobalContext()), false
        );

// External - c++
Function *fcc = (Function*) module->getOrInsertFunction("writeSomething",
        fccType
        );

// Call
std::vector<Value*> emptyArgs;
CallInst::Create(fcc, makeArrayRef(emptyArgs));

仅调用此函数的 LLVM 输出为 ( // cmets 是我的输入,我如何理解输出)

// External linkage
declare void @writeSomething()

define internal void @main() {
entry:
  // Call my function
  call void @writeSomething()
  ret void
}

程序以消息结束:LLVM ERROR: Program used external function 'writeSomething' which could not be resolved!

【问题讨论】:

    标签: c++ compiler-construction llvm


    【解决方案1】:

    由于C++ name mangling,该函数的名称实际上类似于_Z14writeSomethingv - C++ 通过在函数名称中编码类型信息来支持重载。

    您可以通过将函数声明为 extern "C" void writeSomething() { ... } 来禁用此功能,或者找出在编译器的名称修饰方案下应该调用什么并使用它。

    【讨论】:

    • 我添加了 extern "C" 并且它确实有效!非常感谢。
    猜你喜欢
    • 2014-01-13
    • 1970-01-01
    • 2017-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多