[LLVMdev] [Help] How can we call an object's virtual function inside IR?
Gyounghwa Kim
gyounghwakim at gmail.com
Mon Jan 11 00:21:30 PST 2010
Dear Duncan,
Thank you very much for your answer.
Actually, I was able to call a C function from LLVM IR, now I try to
call a class member function from inside IR and tried to follow your
instructions.
1. I am trying to create a ConstantInt pointer to the class member function.
2. Covert it to a Function Pointer and call it from IRBuilder.
However, I can't figure out what to pass as the value of the pointer
to the member function.
Could you help me with this?
My code parts are shown below.
using namespace llvm;
class Record
{
public:
Record(){value=0;}
Record(int a){value=a;}
int getValue(){return value;}
void setValue(int a){value=a;}
int addOneToValue(){return value+1;}
private:
int value;
};
typedef int (Record::*RecMemFunc)();
int main(int argc, char*args[])
{
Record *rec= new Record(5);
int y = rec->addOneToValue();
printf("Hi y = %d\n", y);
RecMemFunc rp = &Record::addOneToValue;
y = (rec->*rp)();
printf("Hi y = %d\n", y);
Constant* constInt = ConstantInt::get(Type::Int64Ty, (int64)thePointer);
Value* constPtr = ConstantExpr::getIntToPtr(constInt,
PointerType::getUnqual(Type::Int32Ty));
//builder.CreateCall(myFunction, constPtr);
:
:
}
What will be the value to pass in instead of thePointer??????
Thank you very much for your help in advance. :)
- Gyounghwa
On Wed, Jan 6, 2010 at 7:39 PM, Duncan Sands <baldrick at free.fr> wrote:
> 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