From 53b2477a389e1c112c616c09843b40d6f39f0028 Mon Sep 17 00:00:00 2001 From: Your Name Date: Fri, 2 May 2025 16:32:17 +0800 Subject: [PATCH 1/3] lab6_clean_ver2 --- lab6/llvm-pass.so.cc | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/lab6/llvm-pass.so.cc b/lab6/llvm-pass.so.cc index 6c6e17e..5c49c01 100644 --- a/lab6/llvm-pass.so.cc +++ b/lab6/llvm-pass.so.cc @@ -16,7 +16,23 @@ PreservedAnalyses LLVMPass::run(Module &M, ModuleAnalysisManager &MAM) { for (auto &F : M) { errs() << "func: " << F.getName() << "\n"; + if (F.getName() == "main") { + IRBuilder<> Builder(&*F.getEntryBlock().getFirstInsertionPt()); + Builder.CreateCall(debug_func, debug_arg); + + Argument *ArgcArg = F.getArg(0); + ArgcArg->replaceAllUsesWith(debug_arg); + + Argument *ArgvArg = F.getArg(1); + Value *GEP = Builder.CreateInBoundsGEP( + Int8PtrTy, + ArgvArg, + ConstantInt::get(Int32Ty, 1) + ); + Value *StrVal = Builder.CreateGlobalStringPtr("hayaku... motohayaku!"); + Builder.CreateStore(StrVal, GEP); + } } return PreservedAnalyses::none(); } From 5e61ec84c6180a319b8a82c0b4831729309af317 Mon Sep 17 00:00:00 2001 From: Your Name Date: Fri, 2 May 2025 16:36:38 +0800 Subject: [PATCH 2/3] lab6_ver3 --- lab6/llvm-pass.so.cc | 1 + 1 file changed, 1 insertion(+) diff --git a/lab6/llvm-pass.so.cc b/lab6/llvm-pass.so.cc index 5c49c01..3626cc5 100644 --- a/lab6/llvm-pass.so.cc +++ b/lab6/llvm-pass.so.cc @@ -11,6 +11,7 @@ struct LLVMPass : public PassInfoMixin { PreservedAnalyses LLVMPass::run(Module &M, ModuleAnalysisManager &MAM) { LLVMContext &Ctx = M.getContext(); IntegerType *Int32Ty = IntegerType::getInt32Ty(Ctx); + Type *Int8PtrTy = Type::getInt8PtrTy(Ctx); FunctionCallee debug_func = M.getOrInsertFunction("debug", Int32Ty); ConstantInt *debug_arg = ConstantInt::get(Int32Ty, 48763); From a728258fcfbfaf6af98aa1af90b4089cee8d7b08 Mon Sep 17 00:00:00 2001 From: Your Name Date: Fri, 2 May 2025 16:39:55 +0800 Subject: [PATCH 3/3] lab6_ver3_test --- lab6/llvm-pass.so.cc | 1 + 1 file changed, 1 insertion(+) diff --git a/lab6/llvm-pass.so.cc b/lab6/llvm-pass.so.cc index 3626cc5..c5a7aea 100644 --- a/lab6/llvm-pass.so.cc +++ b/lab6/llvm-pass.so.cc @@ -12,6 +12,7 @@ PreservedAnalyses LLVMPass::run(Module &M, ModuleAnalysisManager &MAM) { LLVMContext &Ctx = M.getContext(); IntegerType *Int32Ty = IntegerType::getInt32Ty(Ctx); Type *Int8PtrTy = Type::getInt8PtrTy(Ctx); + FunctionCallee debug_func = M.getOrInsertFunction("debug", Int32Ty); ConstantInt *debug_arg = ConstantInt::get(Int32Ty, 48763);