On 3/28/11 1:42 AM, netra at cse.iitb.ac.in wrote:> Hi,
> I am a Mtech student at IIT,Bombay and presently studying llvm as a part
> of my Seminar work.Currently,i am writing a simple pass that would detect
> function pointer call,use and declarations in the src program.
> I could detect function pointer calls using the getCalledFunction().
>
> However,i am stuck in identifying function pointer declaration and use.
> is there a way one can distinguish between data and function pointers
> in llvm?
>
> I tried the following,
> I->getType()->getElementType()->getTypeID()
>
> but this always returns me 12 i.e pointer type id (for pointers)
> irrespective if the pointer is a data or a function pointer.
What you want to do is first determine if the value is of pointer type
and then, if it is, determine if it is pointing to a function.
You can use dyn_cast<Type> and isa<Type> to determine if a
particular
type is of the class PointerType. You can then use PointerType's
getElementType() method to determine if it is pointing to a function type.
To get you started, here's how to determine if Type * Ty is a Pointer type:
Type * Ty = I->getType();
if (PointerType * PT = dyn_cast<PointerType>(Ty)) {
...
}
Inside the block, you can now use PT's getElementType() method to see
what the pointer is pointing to.
You should read up on dyn_cast<> and isa<> in the Programmer's
Manual
and use the doxygen information on the web site to understand the class
hierarchy of llvm::Type and its subclasses.
One final note: the LLVM type system is not type safe in that it allows
arbitrary type casts like C does. For example, I can take a function
pointer, cast it to an int, store it in memory, and then have some other
part of the program load the integer from memory, cast it back to a
function pointer, and use it. The fact that an LLVM value has an
integer type doesn't mean that it's holding an integer.
For some applications, this is not a problem. For others, it can be,
and you'll need an analysis like DSA to give you more accurate (but more
conservative) information.
-- John T.
> Kindly let me know where i am going wrong.
>
> Regards,
> Netra
> Mtech,CSE,IIT BOMBAY
>
> _______________________________________________
> LLVM Developers mailing list
> LLVMdev at cs.uiuc.edu http://llvm.cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev