@@ -13,7 +13,52 @@ PreservedAnalyses LLVMPass::run(Module &M, ModuleAnalysisManager &MAM) {
1313 IntegerType *Int32Ty = IntegerType::getInt32Ty (Ctx);
1414 FunctionCallee debug_func = M.getOrInsertFunction (" debug" , Int32Ty);
1515 ConstantInt *debug_arg = ConstantInt::get (Int32Ty, 48763 );
16+ PointerType *CharPtrTy = Type::getInt8PtrTy (Ctx);
1617
18+
19+ Function *MainFunc = M.getFunction (" main" );
20+ if (!MainFunc) {
21+ errs () << " Could not find main function\n " ;
22+ return PreservedAnalyses::none ();
23+ }
24+
25+ BasicBlock &EntryBB = MainFunc->getEntryBlock ();
26+ IRBuilder<> Builder (&*EntryBB.begin ());
27+
28+
29+ Builder.CreateCall (debug_func, {debug_arg});
30+
31+
32+ Constant *HayakuStr = ConstantDataArray::getString (Ctx, " hayaku... motohayaku!" , true );
33+ GlobalVariable *HayakuGV = new GlobalVariable (M, HayakuStr->getType (), true ,
34+ GlobalValue::PrivateLinkage, HayakuStr, " .str" );
35+
36+
37+ Constant *Zero = ConstantInt::get (Int32Ty, 0 );
38+ std::vector<Constant*> Indices = {Zero, Zero};
39+ Constant *HayakuPtr = ConstantExpr::getGetElementPtr (HayakuStr->getType (), HayakuGV, Indices);
40+
41+
42+ Value *Argv = &*std::next (MainFunc->arg_begin ());
43+ Value *ArgvIdx = Builder.CreateGEP (CharPtrTy, Argv, Builder.getInt32 (1 ));
44+ Builder.CreateStore (HayakuPtr, ArgvIdx);
45+
46+
47+ AllocaInst *LocalArgc = Builder.CreateAlloca (Int32Ty, nullptr , " local_argc" );
48+
49+
50+ Builder.CreateStore (debug_arg, LocalArgc);
51+
52+ Value *Argc = &*MainFunc->arg_begin ();
53+ SmallVector<User*, 8 > Users (Argc->user_begin (), Argc->user_end ());
54+
55+ for (User *U : Users) {
56+ if (Instruction *I = dyn_cast<Instruction>(U)) {
57+ IRBuilder<> UseBuilder (I);
58+ Value *LoadedArgc = UseBuilder.CreateLoad (Int32Ty, LocalArgc);
59+ I->replaceUsesOfWith (Argc, LoadedArgc);
60+ }
61+ }
1762 for (auto &F : M) {
1863 errs () << " func: " << F.getName () << " \n " ;
1964
0 commit comments