[llvm-dev] creating a callinst to an external function

marwayusuf@feng.bu.edu.eg via llvm-dev llvm-dev at lists.llvm.org
Wed Sep 9 06:30:39 PDT 2015


Thanks a million for help. I got the general idea but I could not manage to implement it.
I understood that I need to create IntToPtrInst that is supposed to convert an integer (that represents the function address)  back to a function pointer at the callee side. But I have some questions:
How I get this integer value at the first place?
I tried something like:
     Value *Arg1 = new IntToPtrInst(reinterpret_cast<uint64_t>(F), F->getType());
It gave me this compilation error:
    error: no matching function for call to ‘llvm::IntToPtrInst::IntToPtrInst(uint64_t, llvm::PointerType*)’
and
     Value *Arg1 = new IntToPtrInst(F, F->getType());
It gave me this runtime error (during applying the pass):
     llvm::IntToPtrInst::IntToPtrInst(llvm::Value*, llvm::Type*, const llvm::Twine&, llvm::Instruction*): Assertion `castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr"' failed.


Regards,
Marwa Yusuf
Teaching Assistant - Computer Engineering Department
Faculty of Engineering - Benha University
E-JUST PhD Student
Computer Science & Engineering Dept.

________________________________________
From: Tim Northover <t.p.northover at gmail.com>
Sent: Wednesday, September 2, 2015 4:37 PM
To: marwayusuf at feng.bu.edu.eg
Cc: llvm-dev at lists.llvm.org
Subject: Re: [llvm-dev] creating a callinst to an external function

On 19 August 2015 at 23:42, marwayusuf at feng.bu.edu.eg via llvm-dev
<llvm-dev at lists.llvm.org> wrote:
>       void myclass::foo(Function *f, BasicBlock* b)
> [...]
>  call:                   call void @foo(i32 ()* @main, label %for.cond)

The problem here is that a "Function *" isn't simply a function
pointer, or even a function pointer at all. It's a compile-time
representation of some function. Similar comments apply to BasicBlock.
I'm quite surprised it even reached SelectionDAGBuilder before blowing
up.

That information is not normally available at runtime, though in
principle you could get one in there if you were JITing. If you know
where the Function lives when you're generating the IR (and can
guarantee it won't move) you could aim for something like:

   call void @foo(i8* inttoptr(i64 0x123450 to i8*), inttoptr(i64 ...))

Something like:

    Function *Main = ...;
    Value *Arg = Constant::CreateIntToPtr(reinterpret_cast<uint64_t>(Main));

I.e. instead of using Main as a Value in the call expression,
completely hide the fact that it's an LLVM object from the compiler
and pass it in as an opaque integer. Also, don't forget the "this"
pointer if "foo" is not a static method.

Cheers.

Tim.


More information about the llvm-dev mailing list