[LLVMdev] How to cast an integer array to an integer pointer? (user question)

Yuri yuri at tsoft.com
Wed May 5 16:05:29 PDT 2010


I am new to LLVM and couldn't find any llvm-user list, so I am posting 
my user question here, sorry.

I am trying to create a simple "puts" call accepting the static string, 
with the code below.
The last line (CallInst::Create) fails with an assert: "Calling a 
function with a bad signature!"
Because the type of function is void(u8*) and the argument supplied is: 
getelementptr inbounds [4 x i8]* @0, i32 0
How to convert the pointer to the array to u8* ? I tried BitCastInst but 
it inserts an explicit bitcast. I don't think this is right.

Yuri


  // assume BasicBlock *BB, Module *M
  // global string
  std::vector<llvm::Constant*> Ivars;
  Ivars.push_back(llvm::ConstantInt::get(Type::getInt8Ty(Context), 0x41));
  Ivars.push_back(llvm::ConstantInt::get(Type::getInt8Ty(Context), 0x42));
  Ivars.push_back(llvm::ConstantInt::get(Type::getInt8Ty(Context), 0x43));
  Ivars.push_back(llvm::ConstantInt::get(Type::getInt8Ty(Context), 0));
  GlobalVariable *GGV = new GlobalVariable(
    *M,
    ArrayType::get(Type::getInt8Ty(Context), 4),
    true, GlobalValue::PrivateLinkage, 
llvm::ConstantArray::get(ArrayType::get(Type::getInt8Ty(Context), 4), 
Ivars),
    "g_value_mine", 0, false, 0/*AddrSpace*/);
  // ptr
  GetElementPtrInst *gepi = GetElementPtrInst::CreateInBounds(
    GGV,
    ConstantInt::get(Type::getInt32Ty(Context), 0),
    "zzz",
    BB
    );
  // func call
  Function *PutsF =
    cast<Function>(M->getOrInsertFunction("puts", Type::getVoidTy(Context),
                                          
Type::getInt8Ty(Context)->getPointerTo(),
                                          (Type *)0));
  CallInst::Create(PutsF, (Value* const*)&gepi, (Value* 
const*)(&gepi)+1, "", BB);





More information about the llvm-dev mailing list