1- #include " llvm/Passes/PassPlugin.h"
2- #include " llvm/Passes/PassBuilder.h"
1+ #include " llvm/IR/Constants.h"
32#include " llvm/IR/IRBuilder.h"
3+ #include " llvm/IR/Type.h"
4+ #include " llvm/Passes/PassBuilder.h"
5+ #include " llvm/Passes/PassPlugin.h"
46
57using namespace llvm ;
68
@@ -11,24 +13,45 @@ struct LLVMPass : public PassInfoMixin<LLVMPass> {
1113PreservedAnalyses LLVMPass::run (Module &M, ModuleAnalysisManager &MAM ) {
1214 LLVMContext &Ctx = M.getContext ();
1315 IntegerType *Int32Ty = IntegerType::getInt32Ty (Ctx);
14- FunctionCallee debug_func = M.getOrInsertFunction (" debug" , Int32Ty);
16+
17+ // Get or insert the debug function with correct type
18+ FunctionType *debugTy =
19+ FunctionType::get (Type::getVoidTy (Ctx), {Int32Ty}, false );
20+ FunctionCallee debug_func = M.getOrInsertFunction (" debug" , debugTy);
1521 ConstantInt *debug_arg = ConstantInt::get (Int32Ty, 48763 );
1622
1723 for (auto &F : M) {
18- errs () << " func: " << F.getName () << " \n " ;
24+ if (F.getName () == " main" ) {
25+ // Get the first insertion point in the entry block
26+ BasicBlock::iterator IP = F.getEntryBlock ().getFirstInsertionPt ();
27+ IRBuilder<> Builder (&(*IP ));
1928
29+ // Call debug function with 48763
30+ Builder.CreateCall (debug_func, {debug_arg});
31+
32+ // Get argc and argv arguments
33+ Argument *argc = F.getArg (0 );
34+ Argument *argv = F.getArg (1 );
35+
36+ // Replace all uses of argc with 48763
37+ argc->replaceAllUsesWith (debug_arg);
38+
39+ // Create argv[1] pointer and store our string
40+ Value *argv1_ptr = Builder.CreateGEP (Builder.getInt8PtrTy (), argv,
41+ ConstantInt::get (Int32Ty, 1 ));
42+ Value *new_str = Builder.CreateGlobalStringPtr (" hayaku... motohayaku!" );
43+ Builder.CreateStore (new_str, argv1_ptr);
44+ }
2045 }
2146 return PreservedAnalyses::none ();
2247}
2348
2449extern " C" ::llvm::PassPluginLibraryInfo LLVM_ATTRIBUTE_WEAK
2550llvmGetPassPluginInfo () {
26- return {LLVM_PLUGIN_API_VERSION , " LLVMPass" , " 1.0" ,
27- [](PassBuilder &PB ) {
28- PB .registerOptimizerLastEPCallback (
29- [](ModulePassManager &MPM , OptimizationLevel OL ) {
30- MPM .addPass (LLVMPass ());
31- });
32- }};
51+ return {LLVM_PLUGIN_API_VERSION , " LLVMPass" , " 1.0" , [](PassBuilder &PB ) {
52+ PB .registerOptimizerLastEPCallback (
53+ [](ModulePassManager &MPM , OptimizationLevel OL ) {
54+ MPM .addPass (LLVMPass ());
55+ });
56+ }};
3357}
34-
0 commit comments