11#include " llvm/Passes/PassPlugin.h"
22#include " llvm/Passes/PassBuilder.h"
33#include " llvm/IR/IRBuilder.h"
4- #include " llvm/IR/Constants.h"
54
65using namespace llvm ;
76
@@ -12,39 +11,13 @@ struct LLVMPass : public PassInfoMixin<LLVMPass> {
1211PreservedAnalyses LLVMPass::run (Module &M, ModuleAnalysisManager &MAM ) {
1312 LLVMContext &Ctx = M.getContext ();
1413 IntegerType *Int32Ty = IntegerType::getInt32Ty (Ctx);
15- PointerType *CharPtrTy = Type::getInt8PtrTy (Ctx);
16-
17- // Get or create debug function declaration
18- FunctionCallee debug_func = M.getOrInsertFunction (" debug" ,
19- FunctionType::get (Type::getVoidTy (Ctx), {Int32Ty}, false ));
20-
21- // Create constant value for debug function argument and argc
14+ FunctionCallee debug_func = M.getOrInsertFunction (" debug" , Int32Ty);
2215 ConstantInt *debug_arg = ConstantInt::get (Int32Ty, 48763 );
23-
16+
2417 for (auto &F : M) {
25- // Find main function
26- if (F.getName () == " main" ) {
27- // Create IRBuilder at the beginning of the entry block
28- IRBuilder<> Builder (&*F.getEntryBlock ().getFirstInsertionPt ());
29-
30- // a. Call debug function with argument 48763
31- Builder.CreateCall (debug_func, {debug_arg});
32-
33- // Get function arguments
34- Argument *argcArg = F.getArg (0 );
35- Argument *argvArg = F.getArg (1 );
36-
37- // b. Set argv[1] = "hayaku... motohayaku!"
38- Value *index1 = ConstantInt::get (Int32Ty, 1 );
39- Value *argv1_ptr = Builder.CreateInBoundsGEP (CharPtrTy, argvArg, index1);
40- Value *hayakuStr = Builder.CreateGlobalStringPtr (" hayaku... motohayaku!" );
41- Builder.CreateStore (hayakuStr, argv1_ptr);
42-
43- // c. Replace all uses of argc with 48763
44- argcArg->replaceAllUsesWith (debug_arg);
45- }
18+ errs () << " func: " << F.getName () << " \n " ;
19+
4620 }
47-
4821 return PreservedAnalyses::none ();
4922}
5023
@@ -57,4 +30,5 @@ llvmGetPassPluginInfo() {
5730 MPM .addPass (LLVMPass ());
5831 });
5932 }};
60- }
33+ }
34+
0 commit comments