Skip to content

Commit 8b9e7d4

Browse files
authored
Merge pull request #432 from john03690248/lab6
[LAB6] 313560003
2 parents a0a28d0 + e1b92c5 commit 8b9e7d4

File tree

1 file changed

+55
-18
lines changed

1 file changed

+55
-18
lines changed

lab6/llvm-pass.so.cc

Lines changed: 55 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,71 @@
11
#include "llvm/Passes/PassPlugin.h"
22
#include "llvm/Passes/PassBuilder.h"
33
#include "llvm/IR/IRBuilder.h"
4+
#include "llvm/IR/Instructions.h"
5+
#include "llvm/IR/Constants.h"
6+
#include "llvm/Support/raw_ostream.h"
47

58
using namespace llvm;
69

10+
bool registerCustomPipeline(StringRef, ModulePassManager&, ArrayRef<PassBuilder::PipelineElement>);
11+
void injectAtStart(ModulePassManager&, OptimizationLevel);
12+
13+
namespace {
714
struct LLVMPass : public PassInfoMixin<LLVMPass> {
8-
PreservedAnalyses run(Module &M, ModuleAnalysisManager &MAM);
9-
};
15+
PreservedAnalyses run(Module &Mod, ModuleAnalysisManager &) {
16+
Function *MainFunc = Mod.getFunction("main");
17+
if (!MainFunc) return PreservedAnalyses::all();
18+
19+
LLVMContext &Context = Mod.getContext();
20+
IRBuilder<> Builder(&*MainFunc->getEntryBlock().getFirstInsertionPt());
21+
22+
FunctionCallee DebugCall = Mod.getOrInsertFunction(
23+
"debug", FunctionType::get(Type::getVoidTy(Context), {Type::getInt32Ty(Context)}, false));
24+
Builder.CreateCall(DebugCall, ConstantInt::get(Type::getInt32Ty(Context), 48763));
25+
26+
auto ArgIter = MainFunc->arg_begin();
27+
Argument *ArgCount = &*ArgIter++;
28+
Argument *ArgList = &*ArgIter;
29+
30+
Value *FixedVal = ConstantInt::get(Type::getInt32Ty(Context), 48763);
31+
AllocaInst *FakeArgc = Builder.CreateAlloca(Type::getInt32Ty(Context), nullptr, "argc_temp");
32+
Builder.CreateStore(FixedVal, FakeArgc);
1033

11-
PreservedAnalyses LLVMPass::run(Module &M, ModuleAnalysisManager &MAM) {
12-
LLVMContext &Ctx = M.getContext();
13-
IntegerType *Int32Ty = IntegerType::getInt32Ty(Ctx);
14-
FunctionCallee debug_func = M.getOrInsertFunction("debug", Int32Ty);
15-
ConstantInt *debug_arg = ConstantInt::get(Int32Ty, 48763);
34+
for (auto UI = ArgCount->use_begin(), UE = ArgCount->use_end(); UI != UE;) {
35+
Use &U = *UI++;
36+
U.set(Builder.CreateLoad(Type::getInt32Ty(Context), FakeArgc));
37+
}
1638

17-
for (auto &F : M) {
18-
errs() << "func: " << F.getName() << "\n";
39+
Value *Str = Builder.CreateGlobalStringPtr("hayaku... motohayaku!", "msg_str");
40+
Value *Index = ConstantInt::get(Type::getInt32Ty(Context), 1);
41+
Value *ArgvSlot = Builder.CreateGEP(
42+
ArgList->getType()->getPointerElementType(), ArgList, Index);
43+
Builder.CreateStore(Str, ArgvSlot);
1944

45+
return PreservedAnalyses::none();
2046
}
21-
return PreservedAnalyses::none();
47+
};
2248
}
2349

24-
extern "C" ::llvm::PassPluginLibraryInfo LLVM_ATTRIBUTE_WEAK
25-
llvmGetPassPluginInfo() {
26-
return {LLVM_PLUGIN_API_VERSION, "LLVMPass", "1.0",
50+
extern "C" LLVM_ATTRIBUTE_WEAK ::llvm::PassPluginLibraryInfo llvmGetPassPluginInfo() {
51+
static const PassPluginLibraryInfo Info{
52+
LLVM_PLUGIN_API_VERSION, "LLVMPass", LLVM_VERSION_STRING,
2753
[](PassBuilder &PB) {
28-
PB.registerOptimizerLastEPCallback(
29-
[](ModulePassManager &MPM, OptimizationLevel OL) {
30-
MPM.addPass(LLVMPass());
31-
});
32-
}};
54+
PB.registerPipelineParsingCallback(registerCustomPipeline);
55+
PB.registerPipelineStartEPCallback(injectAtStart);
56+
}
57+
};
58+
return Info;
3359
}
3460

61+
bool registerCustomPipeline(StringRef Name, ModulePassManager &PM, ArrayRef<PassBuilder::PipelineElement>) {
62+
if (Name == "llvm-pass") {
63+
PM.addPass(LLVMPass());
64+
return true;
65+
}
66+
return false;
67+
}
68+
69+
void injectAtStart(ModulePassManager &PM, OptimizationLevel) {
70+
PM.addPass(LLVMPass());
71+
}

0 commit comments

Comments
 (0)