我发现我的llvm pass项目可能存在一些错误。我写了一个小程序,试图打印出所有指令及其相应的操作码。我使用Instruction :: getOpcode()和Instruction :: getOpcodeName()。
%1 = alloca i32, align 4
load
%a = alloca i32, align 4
load
%b = alloca i32, align 4
load
store i32 0, i32* %1
getelementptr
%2 = call i64 @time(i64* null) #3
select
%3 = trunc i64 %2 to i32
zext
call void @srand(i32 %3) #3
select
%4 = call i32 @rand() #3
select
%5 = srem i32 %4, 10
frem
store i32 %5, i32* %a, align 4
getelementptr
%6 = call i32 @rand() #3
select
%7 = srem i32 %6, 10
frem
store i32 %7, i32* %b, align 4
getelementptr
%8 = load i32* %a, align 4
store
%9 = load i32* %b, align 4
store
%10 = icmp sgt i32 %8, %9
fcmp
br i1 %10, label %11, label %14
switch
%12 = load i32* %a, align 4
store
%13 = call i32 (i8*, ...)* @printf(i8* getelementptr inbounds ([4 x i8]* @.str, i32 0, i32 0), i32 %12)
select
br label %17
switch
%15 = load i32* %b, align 4
store
%16 = call i32 (i8*, ...)* @printf(i8* getelementptr inbounds ([4 x i8]* @.str, i32 0, i32 0), i32 %15)
select
br label %17
switch
%18 = load i32* %1
store
ret i32 %18
br
我尝试在指令后打印出每个指令opcodeName,但每条指令都得到一个错误的opcodeName。我还检查了每个opcodeName的操作码,它们也是错误的。我不确切知道在哪里找到指令列表的枚举,但我想我以前找过它,并且错误的opcodeName与操作码相关。看起来他们都从右侧案件转移了2个单位。
你有没有遇到过这样的问题?
我还附上了我的llvm传递的代码,如下所示:
#include "llvm/ADT/Statistic.h"
#include "llvm/IR/Function.h"
#include "llvm/Pass.h"
#include "llvm/Support/raw_ostream.h"
#include "llvmFaultInjector.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/BasicBlock.h"
#include <iostream>
#include <cstdlib>
using namespace llvm;
namespace {
// Hello - The first implementation, without getAnalysisUsage.
struct Hello : public ModulePass {
static char ID; // Pass identification, replacement for typeid
Hello() : ModulePass(ID) {}
bool runOnModule(Module &M) override {
Module::iterator fi, fie;
Function::iterator bi, bie;
BasicBlock::iterator ii, iie;
/* test a example : delete first instruction of test file */
Instruction * inst;
for(fi = M.begin(), fie = M.end(); fi != fie; fi++)
{
for(bi = fi->begin(), bie = fi->end(); bi != bie; bi++)
{
for(ii = bi->begin(), iie = bi->end(); ii != iie; ii++)
{
inst = (Instruction *)(ii);
errs() << *inst << '\n';
errs() << inst->getOpcodeName() << '\n';
}
}
}
/* delete instruction*/
/*llvmFaultInjector::tranBranchDelete(inst);
*/
return true;
}
};
}
char Hello::ID = 0;
static RegisterPass<Hello> X("CFCFaultInject", "llvm CFCFaultInjector");