diff --git a/lab6/llvm-pass.so.cc b/lab6/llvm-pass.so.cc index 6c6e17e..c5a7aea 100644 --- a/lab6/llvm-pass.so.cc +++ b/lab6/llvm-pass.so.cc @@ -11,12 +11,30 @@ 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); 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(); }