[LLVMdev] Passing a pointer to a function

Scott Ricketts sricketts at maxentric.com
Wed May 20 19:31:30 PDT 2009


I recently began hacking around with my first LLVM pass. The big
picture is that I would like to insert function calls for each
instruction type, and pass some parameters based on the instruction
type. Then I will link the output to some C file that implements those
functions.

Things were going well until I started trying to make function calls
with a pointer as a parameter. For example, I would like to do
something like:

void
visitLoadInst(LoadInst &I) {
  Value *P;
  P = I.getPointerOperand();

  CallInst::Create(load_fcall, P, "", &I);
}

Where load_fcall is build using a call to Module::getOrInsertFunction
as in the example here:

http://wiki.llvm.org/HowTo:_Insert_a_function_call

My question is, what do I pass as the argument type for P above? The
following seems to work, as long as there are no floating point ops:

PointerType::getUnqual(IntegerType::get(32))

So I tried using just a void pointer type, as in:
PointerType::getUnqual(Type::VoidTy)

But then Type.cpp throws this assertion.
Assertion `ValueType != Type::VoidTy && "Pointer to void is not valid,
use sbyte* instead!"' failed.

I can try checking the type before building the function, as in:

void
visitLoadInst(LoadInst &I) {
  Value *P;
  P = I.getPointerOperand();

  PointerType* PtrTy = (PointerType*)(P);

  Constant* fcall;

  if (PtrTy->getElementType()->isInteger()) {
    fcall = buildFcallMem(*theModule, "load_int", Type::Int32Ty);
    CallInst::Create(fcall, P, "", &I);
  } else if (PtrTy->getElementType()->isInteger()){
    fcall = buildFcallMem(*theModule, "load_float", Type::FloatTy);
    CallInst::Create(fcall, P, "", &I);
  }
}

But then I get segfaults or assertions fail in Type.h (it's a huge
mess). I could be more specific, but I have tried several different
approaches and am trying to be brief here.

Perhaps I am overlooking an obvious approach. I will continue snooping
through the code to attempt to learn what is going on, but if anyone
could nudge me in the right direction or point out where I am
completely off, that would probably be helpful.

Thanks,
Scott



More information about the llvm-dev mailing list