Hello;<div><br></div><div>Thanks to Eli for the pointer to the ConstantDataArray::getString() fucntion. Now I am trying to declare a global char array with the content "hi" and call a function "print" (which takes a char pointer and return an insteger.</div>
<div><br></div><div>I am doing the following in the code -<br clear="all"><div>Function Creation:</div><div><br></div><div><div><span class="Apple-tab-span" style="white-space:pre"> </span>PointerType* array = PointerType::get(IntegerType::getInt8Ty(getGlobalContext())</div>
<div><span class="Apple-tab-span" style="white-space:pre"> </span>/*CI->getPointerOperand()->getType()*/, 0);</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>printFunc = M.getOrInsertFunction("print", IntegerType::getInt32Ty(getGlobalContext()), <span class="Apple-tab-span" style="white-space:pre"> </span>array, NULL);</div>
</div><div>Global Declaration:</div><div><br></div><div><div><span class="Apple-tab-span" style="white-space:pre"> </span>Constant *fname = ConstantDataArray::getString(getGlobalContext(),"hi", true);</div>
<div> <span class="Apple-tab-span" style="white-space:pre"> </span>Value *FBloc;</div><div> <span class="Apple-tab-span" style="white-space:pre"> </span>FBloc = new GlobalVariable(M,</div>
<div><span class="Apple-tab-span" style="white-space:pre"> </span> fname->getType(),</div><div> <span class="Apple-tab-span" style="white-space:pre"> </span> true,</div>
<div> <span class="Apple-tab-span" style="white-space:pre"> </span> GlobalValue::InternalLinkage,</div><div> <span class="Apple-tab-span" style="white-space:pre"> </span> fname,</div>
<div> <span class="Apple-tab-span" style="white-space:pre"> </span> "fBloc");</div></div><div>Function Call With the Global:</div><div><br></div><div><div> <span class="Apple-tab-span" style="white-space:pre"> </span>std::vector<Constant*> const_ptr_7_indices;</div>
<div><span class="Apple-tab-span" style="white-space:pre"> </span>ConstantInt* const_int32_8 = ConstantInt::get(getGlobalContext(), </div><div><span class="Apple-tab-span" style="white-space:pre"> </span>APInt(32, StringRef("0"), 10));</div>
<div><span class="Apple-tab-span" style="white-space:pre"> </span>const_ptr_7_indices.push_back(const_int32_8);</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>const_ptr_7_indices.push_back(const_int32_8);</div>
<div><span class="Apple-tab-span" style="white-space:pre"> </span>Constant* const_ptr_7 = ConstantExpr::getGetElementPtr(fname, const_ptr_7_indices);</div><div><span class="Apple-tab-span" style="white-space:pre"> </span> CallInst* call_print1 = CallInst::Create(func_print, const_ptr_7, "");</div>
<div><span class="Apple-tab-span" style="white-space:pre"> </span>F->getInstList().insert((Instruction*)CI, call_print1); //insert the call instruction</div></div><div><br></div><div>But getting the error on the ConstantExpr::getGetElementPtr() function. </div>
<div><br></div><div>opt: /home/arnie/llvm-clang/llvm/include/llvm/Support/Casting.h:194: typename llvm::cast_retty<To, From>::ret_type llvm::cast(const Y&) [with X = llvm::CompositeType, Y = llvm::Type*, typename llvm::cast_retty<To, From>::ret_type = llvm::CompositeType*]: Assertion `isa<X>(Val) && "cast<Ty>() argument of incompatible type!"' failed.</div>
<div><br></div><div>What might I be doing wrong?</div><div><br></div><div>Thanks a lot;</div>-- <br>Arnamoy Bhattacharyya<br>Athabasca Hall 143<br>Department of Computing Science - University of Alberta<br>Edmonton, Alberta, Canada, T6G 2E8<br>
587-710-7073<br>
</div>