[LLVMdev] Calling a function with a pointer to a global char array as argument

Arnamoy Bhattacharyya arnamoy at ualberta.ca
Wed Sep 5 13:07:32 PDT 2012


Hello;

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.

I am doing the following in the code -
Function Creation:

PointerType* array =
PointerType::get(IntegerType::getInt8Ty(getGlobalContext())
/*CI->getPointerOperand()->getType()*/, 0);
printFunc = M.getOrInsertFunction("print",
IntegerType::getInt32Ty(getGlobalContext()), array, NULL);
Global Declaration:

Constant *fname = ConstantDataArray::getString(getGlobalContext(),"hi",
true);
                                Value *FBloc;
                                FBloc = new GlobalVariable(M,
                                        fname->getType(),
                                        true,
                                        GlobalValue::InternalLinkage,
                                        fname,
                                        "fBloc");
Function Call With the Global:

  std::vector<Constant*> const_ptr_7_indices;
ConstantInt* const_int32_8 = ConstantInt::get(getGlobalContext(),
APInt(32, StringRef("0"), 10));
const_ptr_7_indices.push_back(const_int32_8);
const_ptr_7_indices.push_back(const_int32_8);
Constant* const_ptr_7 = ConstantExpr::getGetElementPtr(fname,
const_ptr_7_indices);
                                CallInst* call_print1 =
CallInst::Create(func_print, const_ptr_7, "");
F->getInstList().insert((Instruction*)CI, call_print1);   //insert the call
instruction

But getting the error on the ConstantExpr::getGetElementPtr() function.

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.

What might I be doing wrong?

Thanks a lot;
-- 
Arnamoy Bhattacharyya
Athabasca Hall 143
Department of Computing Science - University of Alberta
Edmonton, Alberta, Canada, T6G 2E8
587-710-7073
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20120905/818702a6/attachment.html>


More information about the llvm-dev mailing list