[LLVMdev] [Help] How can we call an object's virtual function inside IR?

Duncan Sands baldrick at free.fr
Wed Jan 6 02:39:49 PST 2010


Hi Gyounghwa Kim,

> First of all, thank you very much for your answer.
> I tried your sugestion and found out that it is not what I wanted.
> What I have to do is call a native C function from inside this
> generated function.
> Is there any way that we can find and call native C functions not
> created by LLVM IR?

You can insert a declaration of the function into the IR, then call
it.  Of course, for this to work you need to link in the native function
when running.  If you are building a standalone application then this
is no problem.  If you are running the IR using the JIT then it is also
possible, hopefully someone else will explain how.

> I am asking this question because I want to fix this example to get a
> class member variable (ClassA * %record) and call the member function
> of it from inside LLVM IR.

You are allowed to call a pointer, i.e. a function declaration is not
required.  So you can just load the pointer out of %record, bitcast it
to the right function type, and call it.

Ciao,

Duncan.

PS: Please reply to the list and not to me personally.  That way, others
may answer, and the discussion is archived which may help in the future
if someone else has the same question.

If LLVM IR cannot access the member
> function of a class. If it is not supported, we can change class
> member functions like a c function. For example, ClassA->funca () can
> be created as funcb(&ClassA ) -a C style function. Then we need to
> call funcb from inside LLVM IR.
> 
> Will that be possible?
> I tried to search web and documents, but really couldn't find it.
> 
> [[a [10.00]] > [3.00]]
> ; ModuleID = 'ExprF'
> 
> define i1 @expr(double* %record) {
> entry:
>        %0 = getelementptr double* %record, i32 0               ;
> <double*> [#uses=1]
>        %1 = load double* %0            ; <double> [#uses=1]
>        %2 = frem double %1, 1.000000e+01               ; <double> [#uses=1]
>        %3 = fcmp ogt double %2, 3.000000e+00           ; <i1> [#uses=1]
>        ret i1 %3
> }
> 
> On Tue, Jan 5, 2010 at 7:59 PM, Duncan Sands <baldrick at free.fr> wrote:
>> Hi Gyounghwa Kim, try pasting C++ code into http://llvm.org/demo/
>> in order to see the LLVM IR that llvm-g++ turns it into.  That way
>> you will see how this can be done.
>>
>> Best wishes,
>>
>> Duncan.
>>




More information about the llvm-dev mailing list