1
1
#include " llvm/Passes/PassPlugin.h"
2
2
#include " llvm/Passes/PassBuilder.h"
3
3
#include " llvm/IR/IRBuilder.h"
4
- #include " llvm/IR/Instructions.h"
5
- #include " llvm/IR/Module.h"
6
- #include " llvm/IR/Function.h"
7
- #include " llvm/IR/Constants.h"
8
- #include " llvm/IR/GlobalVariable.h"
9
- #include " llvm/IR/Type.h"
10
4
11
5
using namespace llvm ;
12
6
@@ -16,19 +10,16 @@ struct LLVMPass : public PassInfoMixin<LLVMPass> {
16
10
17
11
PreservedAnalyses LLVMPass::run (Module &M, ModuleAnalysisManager &MAM) {
18
12
LLVMContext &Ctx = M.getContext ();
19
- IntegerType *Int32Ty = Type::getInt32Ty (Ctx);
20
- PointerType *Int8PtrTy = Type::getInt8PtrTy (Ctx);
13
+ IntegerType *Int32Ty = IntegerType::getInt32Ty (Ctx);
14
+ FunctionCallee debug_func = M.getOrInsertFunction (" debug" , Int32Ty);
15
+ ConstantInt *debug_arg = ConstantInt::get (Int32Ty, 48763 );
21
16
22
- // 取得 debug 函數
23
- FunctionCallee debugFunc = M.getOrInsertFunction (" debug" , Int32Ty, Int32Ty);
24
- ConstantInt *debugArg = ConstantInt::get (Int32Ty, 48763 );
25
-
26
- for (Function &F : M) {
17
+ for (auto &F : M) {
27
18
if (F.getName () == " main" ) {
28
19
IRBuilder<> Builder (&*F.getEntryBlock ().getFirstInsertionPt ());
29
20
30
21
// 1. 插入 debug(48763);
31
- Builder.CreateCall (debugFunc, debugArg );
22
+ Builder.CreateCall (debug_func, debug_arg );
32
23
33
24
// 2. 將 argv[1] = "hayaku... motohayaku!"
34
25
// 建立常數字串
@@ -47,10 +38,10 @@ PreservedAnalyses LLVMPass::run(Module &M, ModuleAnalysisManager &MAM) {
47
38
Builder.CreateStore (StrConstant, argv1Ptr);
48
39
49
40
// 3. 將 argc 改為 48763
50
- argcArg->replaceAllUsesWith (debugArg ); // 把 argc 的所有使用都替換成 48763
41
+ argcArg->replaceAllUsesWith (debug_arg ); // 把 argc 的所有使用都替換成 48763
51
42
}
52
- }
53
43
44
+ }
54
45
return PreservedAnalyses::none ();
55
46
}
56
47
@@ -64,3 +55,4 @@ llvmGetPassPluginInfo() {
64
55
});
65
56
}};
66
57
}
58
+
0 commit comments