Hi, I have this call instruction to printf inserted which is causing an assertion failure. Any pointers to where I am wrong : Code Dump : Function *printFn=M.getNamedFunction(std::string("printf")); Constant *str=ConstantArray::get("Value : %d\n"); std::vector<Value *> Args(2); std::vector<Constant *> GEPIndices(2); GEPIndices[0]=Constant::getNullValue(Type::LongTy); GEPIndices[1]=Constant::getNullValue(Type::LongTy); Args[0]=ConstantExpr::getGetElementPtr(strcon,GEPIndices); Args[1]=ConstantInt::get(Type::IntTy, id); Instruction * cI= new CallInst(printFn, Args, "retprintf",instr); Thanks
On Tue, Dec 21, 2004 at 03:45:33PM -0700, Sriraman Tallam wrote:> I have this call instruction to printf inserted which is causing > an assertion failure. Any pointers to where I am wrong : > > Function *printFn=M.getNamedFunction(std::string("printf"));std::string() is unnecessary here as it's implicit.> Constant *str=ConstantArray::get("Value : %d\n"); > std::vector<Value *> Args(2); > std::vector<Constant *> GEPIndices(2); > GEPIndices[0]=Constant::getNullValue(Type::LongTy); > GEPIndices[1]=Constant::getNullValue(Type::LongTy); > Args[0]=ConstantExpr::getGetElementPtr(strcon,GEPIndices);You are using 'strcon', but you defined 'str' above. Where is 'strcon' coming from?> Args[1]=ConstantInt::get(Type::IntTy, id); > Instruction * cI= new CallInst(printFn, Args, "retprintf",instr);-- Misha Brukman :: http://misha.brukman.net :: http://llvm.cs.uiuc.edu
Constant *strcon==ConstantArray::get("Value : %d\n"); Sorry Typo. On Tue, 21 Dec 2004, Misha Brukman wrote:> On Tue, Dec 21, 2004 at 03:45:33PM -0700, Sriraman Tallam wrote: > > I have this call instruction to printf inserted which is causing > > an assertion failure. Any pointers to where I am wrong : > > > > Function *printFn=M.getNamedFunction(std::string("printf")); > > std::string() is unnecessary here as it's implicit. > > > Constant *str=ConstantArray::get("Value : %d\n"); > > std::vector<Value *> Args(2); > > std::vector<Constant *> GEPIndices(2); > > GEPIndices[0]=Constant::getNullValue(Type::LongTy); > > GEPIndices[1]=Constant::getNullValue(Type::LongTy); > > Args[0]=ConstantExpr::getGetElementPtr(strcon,GEPIndices); > > You are using 'strcon', but you defined 'str' above. Where is 'strcon' > coming from? > > > Args[1]=ConstantInt::get(Type::IntTy, id); > > Instruction * cI= new CallInst(printFn, Args, "retprintf",instr); > > -- > Misha Brukman :: http://misha.brukman.net :: http://llvm.cs.uiuc.edu > > _______________________________________________ > LLVM Developers mailing list > LLVMdev at cs.uiuc.edu http://llvm.cs.uiuc.edu > http://mail.cs.uiuc.edu/mailman/listinfo/llvmdev >
On Tue, 21 Dec 2004, Sriraman Tallam wrote:> Hi, > > I have this call instruction to printf inserted which is causing > an assertion failure. Any pointers to where I am wrong : > > Code Dump : > > Function *printFn=M.getNamedFunction(std::string("printf")); > Constant *str=ConstantArray::get("Value : %d\n"); > std::vector<Value *> Args(2); > std::vector<Constant *> GEPIndices(2); > GEPIndices[0]=Constant::getNullValue(Type::LongTy); > GEPIndices[1]=Constant::getNullValue(Type::LongTy); > Args[0]=ConstantExpr::getGetElementPtr(strcon,GEPIndices);The problem here is that you are forming a getelementptr instruction where the first operand is an array, not a pointer. In particular, given a pointer to an aggregate, GEP returns a pointer to one of its elements. To solve this problem, you need to create a new GlobalVariable object, initializing it with your constant array. Instead of using strcon, you use the global variable as the address of the string constant. If you add this line above the ConstantExpr::getGetElementPtr line, things should work better for you (untested, but it should show the idea): strcon = new GlobalVariable(strcon->getType(), true, GlobalVariable::InternalLinkage, strcon, "", &M); -Chris -- http://nondot.org/sabre/ http://llvm.cs.uiuc.edu/