[LLVMdev] How to create arguments CallInst

913 views
Skip to first unread message

Rafael Baldiati Parizi

unread,
Oct 6, 2011, 12:48:00 PM10/6/11
to llv...@cs.uiuc.edu
Hello,
I need create a CallInst to this function

define i32 @function(i32 %n, i8 %m){ ... }

I now how get argument's type but I do not know how to create arguments that meet these types.
For example, if the argument is long, accurate pass CallInst an integer argument, however, if a Char, Char must pass an argument.
How to get the type of the argument of the function definition and create the same type to pass the Callinst???


--
Rafael Parizi



John Criswell

unread,
Oct 6, 2011, 12:54:25 PM10/6/11
to Rafael Baldiati Parizi, llv...@cs.uiuc.edu
If you want to pass a value to a function that doesn't match the type, you will need to insert a cast instruction to cast the value to the correct type.  Which cast instruction you insert will depend on the value's type and the type you want it to be.

SAFECode has a utility function castTo() that takes a Value * and a desired Type * and inserts a cast instruction if Value * is not of the desired type.  It's designed to minimize the number of cast instructions it inserts so that the resulting LLVM IR is more readable for debugging:

http://llvm.org/viewvc/llvm-project/safecode/trunk/include/safecode/Utility.h?view=markup

-- John T.



--
Rafael Parizi





_______________________________________________
LLVM Developers mailing list
LLV...@cs.uiuc.edu         http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev

Rafael Baldiati Parizi

unread,
Oct 6, 2011, 1:40:47 PM10/6/11
to John Criswell, llv...@cs.uiuc.edu
virtual std::vector<Value *> getESetArgumentosFunc(Function *F){
std::vector<Value *> varg_list;
varg_list.clear();
for(Function::arg_iterator arg_iti = F->getArgumentList().begin(), arg_ite = F->getArgumentList().end(); arg_iti != arg_ite; ++arg_iti){
Value *para = ConstantInt::get(IntegerType::get(getGlobalContext(),32), 0);
Value *val2 = cast< arg_iti->getType()>(para);                                                 <--------------------------
para->setName("test");
varg_list.push_back(para);
return varg_list;
}

How can I make this cast??


2011/10/6 John Criswell <cris...@illinois.edu>



--
Rafael Parizi



John Criswell

unread,
Oct 6, 2011, 2:26:52 PM10/6/11
to Rafael Baldiati Parizi, llv...@cs.uiuc.edu
On 10/6/11 12:40 PM, Rafael Baldiati Parizi wrote:
virtual std::vector<Value *> getESetArgumentosFunc(Function *F){
std::vector<Value *> varg_list;
varg_list.clear();
for(Function::arg_iterator arg_iti = F->getArgumentList().begin(), arg_ite = F->getArgumentList().end(); arg_iti != arg_ite; ++arg_iti){
Value *para = ConstantInt::get(IntegerType::get(getGlobalContext(),32), 0);
Value *val2 = cast< arg_iti->getType()>(para);                                                 <--------------------------
para->setName("test");
varg_list.push_back(para);
return varg_list;
}

How can I make this cast??

I think you misunderstood me.  You need to *insert* an LLVM cast instruction.  For example, you might insert the following code:


Value *para = ConstantInt::get(IntegerType::get(getGlobalContext(),32), 0);
Value * Val2 = CastInst::CreateZExtOrBitCast (para, arg_iti->getType(), "test", InsertPt)

... where InsertPt is an Instruction * specifying where to insert the cast instruction.

Having said that, if all you're doing is inserting zero values of the appropriate type, it might be easier to use:

Value * Val2 = Constant::getNullValue (arg_iti->getType());

-- John T.

Rafael Baldiati Parizi

unread,
Oct 6, 2011, 2:33:12 PM10/6/11
to John Criswell, llv...@cs.uiuc.edu
John,
I needed just this: Value * Val2 = Constant::getNullValue (arg_iti->getType());

Thanks.
--
Rafael Parizi



Reply all
Reply to author
Forward
0 new messages