CallInst constructor interface

Hi,

Inserting a call instruction is a bit of a pain. The only way I know how to do it is to write a bunch of code like the following:

      std::vector<const Type*> formalArgs;
      formalArgs.push_back(arg1->getType());
      formalArgs.push_back(arg2->getType());
  ...
  formalArgs.push_back(argn->getType());
  std::vector<Value*> args;
  args.push_back(arg1);
  args.push_back(arg2);
  ...
  args.push_back(argn);
  FunctionType *FType = FunctionType::get(RetTy, formalArgs, false);
  Module *M = before->getParent()->getParent()->getParent();
  Function *F = M->getOrInsertFunction(FName, FType);
  CallInst *call = new CallInst(F, args, "func", before);

It seems that for the common case you should just be able to write this:

  std::vector<Value*> args;
  args.push_back(arg1);
  args.push_back(arg2);
  ...
  args.push_back(argn);
  CallInst *call = new CallInst(RetTy, FName, args, "func", before);

or even (for a small number of arguments)

  CallInst *call = new CallInst(RetTy, FName, arg1, arg2, ..., argn, "func", before);

All the other stuff can be inferred from these parameters, so the extra rigamarole is redundant.

Does any interface like this exist? If not, would it be acceptable to add one? This would make my life easier.

Rob

Robert L. Bocchino Jr.
Ph.D. Student, Computer Science
University of Illinois, Urbana-Champaign

Inserting a call instruction is a bit of a pain. The only way I know how to do it is to write a bunch of code like the following:

You're doing two seperate things here: creating a function and inserting a call to it. For the first, check out Module::getOrInsertFunction.

This allows you write something like this:

   // get 'void foo(int)'
   F = M.getOrInsertFunction("foo", Type::VoidTy, Type::IntTy, 0);

To insert a call, you do need a vector if you want to pass more than two args. Up to two can be passed directly in. You might also find make_vector in VectorExtras.h useful, it lets you say stuff like this:

   make_vector(V1, V2, V3, 0)

to create a vector of three things, in-line.

-Chris

    std::vector<const Type*> formalArgs;
    formalArgs.push_back(arg1->getType());
    formalArgs.push_back(arg2->getType());
  ...
  formalArgs.push_back(argn->getType());
  std::vector<Value*> args;
  args.push_back(arg1);
  args.push_back(arg2);
  ...
  args.push_back(argn);
  FunctionType *FType = FunctionType::get(RetTy, formalArgs, false);
  Module *M = before->getParent()->getParent()->getParent();
  Function *F = M->getOrInsertFunction(FName, FType);
  CallInst *call = new CallInst(F, args, "func", before);

It seems that for the common case you should just be able to write this:

  std::vector<Value*> args;
  args.push_back(arg1);
  args.push_back(arg2);
  ...
  args.push_back(argn);
  CallInst *call = new CallInst(RetTy, FName, args, "func", before);

or even (for a small number of arguments)

  CallInst *call = new CallInst(RetTy, FName, arg1, arg2, ..., argn, "func", before);

All the other stuff can be inferred from these parameters, so the extra rigamarole is redundant.

Does any interface like this exist? If not, would it be acceptable to add one? This would make my life easier.

Rob

Robert L. Bocchino Jr.
Ph.D. Student, Computer Science
University of Illinois, Urbana-Champaign

-Chris