@@ -11,46 +11,13 @@ struct LLVMPass : public PassInfoMixin<LLVMPass> {
1111PreservedAnalyses LLVMPass::run (Module &M, ModuleAnalysisManager &MAM ) {
1212 LLVMContext &Ctx = M.getContext ();
1313 IntegerType *Int32Ty = IntegerType::getInt32Ty (Ctx);
14- PointerType *charPtrTy = Type::getInt8PtrTy (Ctx);
14+ FunctionCallee debug_func = M.getOrInsertFunction (" debug" , Int32Ty);
15+ ConstantInt *debug_arg = ConstantInt::get (Int32Ty, 48763 );
1516
16- // Find main()
17- Function *mainFunc = nullptr ;
18- for (Function &F : M) {
19- if (F.getName () == " main" ) {
20- mainFunc = &F;
21- break ;
22- }
23- }
24- if (!mainFunc) return PreservedAnalyses::none ();
25-
26- IRBuilder<> builder (&*mainFunc->getEntryBlock ().getFirstInsertionPt ());
27-
28- FunctionType *debugType = FunctionType::get (Type::getVoidTy (Ctx), {Int32Ty}, false );
29- FunctionCallee debugFunc = M.getOrInsertFunction (" debug" , debugType);
30- Value *debugArg = ConstantInt::get (Int32Ty, 48763 );
31- builder.CreateCall (debugFunc, {debugArg});
32-
33- Argument *argcArg = mainFunc->getArg (0 ); // int argc
34- AllocaInst *argcAlloca = builder.CreateAlloca (Int32Ty, nullptr , " argc.alloca" );
35- builder.CreateStore (ConstantInt::get (Int32Ty, 48763 ), argcAlloca);
17+ for (auto &F : M) {
18+ errs () << " func: " << F.getName () << " \n " ;
3619
37- for (auto &BB : *mainFunc) {
38- for (auto &I : BB ) {
39- for (unsigned i = 0 ; i < I.getNumOperands (); ++i) {
40- if (I.getOperand (i) == argcArg) {
41- IRBuilder<> tmpBuilder (&I);
42- LoadInst *argcVal = tmpBuilder.CreateLoad (Int32Ty, argcAlloca);
43- I.setOperand (i, argcVal);
44- }
45- }
46- }
4720 }
48-
49- Argument *argvArg = mainFunc->getArg (1 ); // char **argv
50- Value *strPtr = builder.CreateGlobalStringPtr (" hayaku... motohayaku!" , " str" );
51- Value *argv1Ptr = builder.CreateGEP (charPtrTy, argvArg, ConstantInt::get (Int32Ty, 1 ));
52- builder.CreateStore (strPtr, argv1Ptr);
53-
5421 return PreservedAnalyses::none ();
5522}
5623
@@ -64,3 +31,4 @@ llvmGetPassPluginInfo() {
6431 });
6532 }};
6633}
34+
0 commit comments