我正在尝试使用 C API 实现一个小示例。我收到一条错误消息,指出函数上下文与模块上下文不匹配,我无法弄清楚。
这是我的代码:
#include <stdio.h>
#include <llvm-c/Analysis.h>
#include <llvm-c/Core.h>
#include <llvm-c/Target.h>
#include <llvm-c/TargetMachine.h>
int
main() {
LLVMInitializeNativeTarget();
LLVMInitializeNativeAsmPrinter();
char* triple = LLVMGetDefaultTargetTriple();
char* error;
LLVMTargetRef target_ref;
if (LLVMGetTargetFromTriple(triple, &target_ref, &error)) {
printf("Error: %s\n", error);
return 1;
}
LLVMTargetMachineRef tm_ref = LLVMCreateTargetMachine(
target_ref,
triple,
"",
"",
LLVMCodeGenLevelDefault,
LLVMRelocStatic,
LLVMCodeModelJITDefault);
LLVMDisposeMessage(triple);
LLVMContextRef context = LLVMContextCreate();
LLVMModuleRef module = LLVMModuleCreateWithNameInContext("module_name", context);
// LLVMModuleRef module = LLVMModuleCreateWithName("module_name");
LLVMTypeRef param_types[] = {LLVMIntType(32), LLVMIntType(32)};
LLVMTypeRef func_type = LLVMFunctionType(LLVMIntType(32), param_types, 2, 0);
LLVMValueRef func = LLVMAddFunction(module, "function_name", func_type);
LLVMBasicBlockRef entry = LLVMAppendBasicBlock(func, "entry");
LLVMBuilderRef builder = LLVMCreateBuilderInContext(context);
// LLVMBuilderRef builder = LLVMCreateBuilder();
LLVMPositionBuilderAtEnd(builder, entry);
LLVMValueRef tmp = LLVMBuildAdd(builder, LLVMGetParam(func, 0), LLVMGetParam(func, 1), "add");
LLVMBuildRet(builder, tmp);
LLVMVerifyModule(module, LLVMAbortProcessAction, &error);
LLVMDisposeMessage(error);
}
Run Code Online (Sandbox Code Playgroud)
然后我的执行:
$ llvm-config --version
8.0.0
$ clang++ trash.cpp `llvm-config --cflags --ldflags` `llvm-config --libs` `llvm-config --system-libs`
$ ./a.out
Function context does not match Module context!
i32 (i32, i32)* @function_name
LLVM ERROR: Broken module found, compilation aborted!
Run Code Online (Sandbox Code Playgroud)
这不是一个适用于非常小的示例的 API;因此,这里有相当多的代码。
如果我使用当前注释掉的未引用的代码,则context一切正常。我不清楚为什么当我调用 时LLVMAddFunction,它不仅仅从我传入的模块中获取其上下文。
嗯,我找到了答案。而不是LLVMIntType,我应该使用LLVMIntTypeInContext. 而不是LLVMAppendBasicBlock,我应该使用LLVMAppendBasicBlockInContext. 我以前没有意识到存在这样的功能。
| 归档时间: |
|
| 查看次数: |
316 次 |
| 最近记录: |