简体   繁体   中英

How to call function in LLVM

I would like to ask you on the proper way to call/bind own c++ function into LLVM.

I have coded simple function:

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

In LLVM I am trying to register the function. I have created the external linkage to it.

      // 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 Output for just calling this function is ( // comments are mine input how do I understand the output )

// External linkage
declare void @writeSomething()

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

The program ends with message: LLVM ERROR: Program used external function 'writeSomething' which could not be resolved!

Due to C++ name mangling , the name of that function is actually something like _Z14writeSomethingv - C++ supports overloading by encoding type information in the function name.

You can disable this by declaring the function as extern "C" void writeSomething() { ... } , or figure out what it should be called under your compiler's name mangling scheme and use that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM