我正在关注链接Parsing and Modifying LLVM IR code以读取IR文件并尝试解析它。但我发现无论我在参数(.ll或.bc)中写入什么输入文件,它都不会解析文件并将其保存到变量中。
这是我的代码,
#include <iostream>
#include "llvm/LLVMContext.h"
#include "llvm/Module.h"
#include "llvm/Function.h"
#include "llvm/Support/CFG.h"
#include "llvm/Support/IRReader.h"
#include "llvm/Support/SourceMgr.h"
#include "llvm/Support/raw_ostream.h"
using namespace llvm;
int main(int argc, char** argv)
{
if (argc < 2) {
errs() << "Expected an argument - IR file name\n";
exit(1);
}
LLVMContext &Context = getGlobalContext();
SMDiagnostic Err;
Module *Mod = ParseIRFile(argv[1], Err, Context);
if (Mod) {
std::cout << "Mod is not null" << std::endl;
}
else {
std::cout << "Mod is null" << std::endl;
}
return 0;
}
通过使用ll或bc文件运行此代码,它始终显示Mod为空。
有人可以给我一些关于如何重新解决这个问题的提示吗?
这是我的IR代码
; ModuleID = 'bubble.bc'
target datalayout = "e-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-apple-macosx10.9.0"
@Sort = global [8 x i32] [i32 1, i32 4, i32 2, i32 5, i32 7, i32 3, i32 6, i32 3], align 16
@.str = private unnamed_addr constant [4 x i8] c"%d \00", align 1
@.str1 = private unnamed_addr constant [2 x i8] c"\0A\00", align 1
; Function Attrs: nounwind ssp uwtable
define void @bubble() #0 {
%tmp = alloca i32, align 4
%i = alloca i32, align 4
%j = alloca i32, align 4
store i32 0, i32* %i, align 4
br label %1
; <label>:1 ; preds = %41, %0
%2 = load i32* %i, align 4
%3 = icmp slt i32 %2, 8
br i1 %3, label %4, label %44
; <label>:4 ; preds = %1
%5 = load i32* %i, align 4
%6 = add nsw i32 %5, 1
store i32 %6, i32* %j, align 4
br label %7
; <label>:7 ; preds = %37, %4
%8 = load i32* %j, align 4
%9 = icmp slt i32 %8, 8
br i1 %9, label %10, label %40
; <label>:10 ; preds = %7
%11 = load i32* %i, align 4
%12 = sext i32 %11 to i64
%13 = getelementptr inbounds [8 x i32]* @Sort, i32 0, i64 %12
%14 = load i32* %13, align 4
%15 = load i32* %j, align 4
%16 = sext i32 %15 to i64
%17 = getelementptr inbounds [8 x i32]* @Sort, i32 0, i64 %16
%18 = load i32* %17, align 4
%19 = icmp sge i32 %14, %18
br i1 %19, label %20, label %36
//some similar stuff
!0 = metadata !{metadata !"Apple LLVM version 5.1 (clang-503.0.40) (based on LLVM 3.4svn)"}
错误meg是./IRparser:/home/mingaoIrparser/testcase/bubble.ll:10:23:错误:预期'{'在函数体中 define void @bubble()#0 { ^
还有一件事是这个IR是从LLVM3.4编译的,我现在使用的库是2.9。这有关系吗?
答案 0 :(得分:2)
This repository有一堆使用LLVM和Clang作为库的最新示例。例如,this sample具有您所需要的(以及其他样本):
int main(int argc, char **argv) {
if (argc < 2) {
errs() << "Usage: " << argv[0] << " <IR file>\n";
return 1;
}
// Parse the input LLVM IR file into a module.
SMDiagnostic Err;
Module *Mod = ParseIRFile(argv[1], Err, getGlobalContext());
if (!Mod) {
Err.print(argv[0], errs());
return 1;
}
// ... use module
}