[LLVMdev] Function Insertion Error

rohit bhat rohit_bhat39002 at yahoo.co.uk
Wed Feb 1 00:35:39 PST 2012


Hi,
I am trying to insert a function into the LLVM IR. But i get a stack dump exception. My code is as follows.

 

#include "llvm/Pass.h" 
#include "llvm/Function.h" 
#include "llvm/Module.h" 
#include "llvm/Support/raw_ostream.h" 
#include "llvm/ADT/Statistic.h" 
#include "llvm/Support/IRBuilder.h" 
using namespace llvm; 
 

namespace { 
  struct FunctionInsert : public ModulePass { 
    static char ID;
    FunctionInsert() : ModulePass(ID) {} 

  }; 
    virtual bool runOnModule(Module &M) { 
     

     FunctionType* ty=FunctionType::get(Type::getInt32Ty(M.getContext()),false); 

       Function *func =  cast<Function>(M.getOrInsertFunction("func", ty,Type::getInt32Ty(M.getContext()),  (Type *)0));  
      BasicBlock *BB = BasicBlock::Create(M.getContext(), "bb1", func); 
      IRBuilder<> builder(BB); 
       Value *One = builder.getInt32(1); 
      Argument *ArgX = func->arg_begin();    
      ArgX->setName("firstarg");             
     Value *Add = builder.CreateAdd(One, ArgX); 
     builder.CreateRet(Add); 
    return true; 
    } 
  }; 
} 
 
char FunctionInsert::ID = 0; 
static RegisterPass<FunctionInsert> X("FunctionInsert", "Function Insertion Pass"); 

Most of the above code is from the file examples/HowToUseJIT/HowToUseJIT.cpp.
Thanks
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20120201/9ea28d52/attachment.html>


More information about the llvm-dev mailing list