Passing String to an external function in llvm

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|

Passing String to an external function in llvm

Abhinash Jain
Hi All,

On my llvm pass I have some variable named "expr" which is being declared as :-
string expr;      // or  char  *expr; //

Now I want to pass this "expr" to some external function.
How can I do this??

Similarly, How can I pass variable "var" to an external function which is being decalred as :-
Vector<int> var;

Any help will be appreciated..............
Reply | Threaded
Open this post in threaded view
|

Re: Passing String to an external function in llvm

David Blaikie
On Thu, Jul 25, 2013 at 9:07 AM, Abhinash Jain <[hidden email]> wrote:
> Hi All,
>
> On my llvm pass I have some variable named "expr" which is being declared as
> :-
> string expr;      // or  char  *expr; //

I don't understand this question - these are C++ declarations, not
LLVM declarations (not to mention LLVM IR doesn't have "variables", as
such). So what's the actual LLVM entity you're dealing with?

> Now I want to pass this "expr" to some external function.
> How can I do this??

Have you tried compiling some code with Clang to see what IR it
produces for different types, calls, and passing?

>
> Similarly, How can I pass variable "var" to an external function which is
> being decalred as :-
> Vector<int> var;
>
> Any help will be appreciated..............
>
>
>
> --
> View this message in context: http://llvm.1065342.n5.nabble.com/Passing-String-to-an-external-function-in-llvm-tp59798.html
> Sent from the LLVM - Dev mailing list archive at Nabble.com.
> _______________________________________________
> LLVM Developers mailing list
> [hidden email]         http://llvm.cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
_______________________________________________
LLVM Developers mailing list
[hidden email]         http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
Reply | Threaded
Open this post in threaded view
|

Re: Passing String to an external function in llvm

Abhinash Jain
I did some computation through llvm pass, and store those computed values on string.  eg. :-

stringstream lhs;
lhs << instr->getOperand(1);   //  'instr' is some instruction
string lhsvar=lhs.str();

Now I want to pass this 'lhsvar' to the external function, so how can i do this???

This is just the part of a code to make you understand. if you say I can even provide the link to the code.



Reply | Threaded
Open this post in threaded view
|

Re: Passing String to an external function in llvm

David Blaikie
On Thu, Jul 25, 2013 at 10:12 AM, Abhinash Jain <[hidden email]> wrote:
> I did some computation through llvm pass, and store those computed values on
> string.  eg. :-
>
> stringstream lhs;
> lhs << instr->getOperand(1);   //  'instr' is some instruction
> string lhsvar=lhs.str();
>
> Now I want to pass this 'lhsvar' to the external function, so how can i do
> this???

Why are you storing this in a string at all - just use the operand
itself, it will be the LLVM Value you want to pass to the function.

>
> This is just the part of a code to make you understand. if you say I can
> even provide the link to the code.
>
>
>
>
>
>
>
> --
> View this message in context: http://llvm.1065342.n5.nabble.com/Passing-String-to-an-external-function-in-llvm-tp59798p59803.html
> Sent from the LLVM - Dev mailing list archive at Nabble.com.
> _______________________________________________
> LLVM Developers mailing list
> [hidden email]         http://llvm.cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
_______________________________________________
LLVM Developers mailing list
[hidden email]         http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
Reply | Threaded
Open this post in threaded view
|

Re: Passing String to an external function in llvm

Abhinash Jain
This post was updated on .
@David Blaikie Thanx for the response.

 %x = alloca i32, align 4
 %y = alloca i32, align 4
 %a = alloca i32, align 4
 %t = alloca i32, align 4

1. %10 = load i32* %x, align 4
2. %11 = load i32* %y, align 4
3. %div = sdiv i32 %10, %11
4. %12 = load i32* %a, align 4
5. %mul4 = mul nsw i32 %div, %12

6. store i32 %mul4, i32* %t, align 4
a.  %mul4 = mul nsw i32 %div, %12
b.  %div = sdiv i32 %10, %11
c.  %10 = load i32* %x, align 4
d.  %11 = load i32* %y, align 4
e.  %12 = load i32* %a, align 4

This is a IR of       " t = x/y*a "
The address of the "%x, %y, %a" are "0xbe4ab94, 0xbe4abd4, 0xbe4ac54"  respectively.
and the opcode of "*, /" are "12, 15" respectively.

Point (6.a to 6.e)  are the result of use-def chain, with the help of which I managed to make a string as -

"12_15_0xbe4ab94_0xbe4abd4_0xbe4ac54" (by converting getOpcode() and getOperand() to string and than concatenating it)

Now I want to pass this whole string to some other external function, How can I do this?

If you want I can show you the full code.
Reply | Threaded
Open this post in threaded view
|

Re: Passing String to an external function in llvm

David Blaikie
On Thu, Jul 25, 2013 at 11:07 AM, Abhinash Jain <[hidden email]> wrote:

> Thanx for the response.
>
>  %x = alloca i32, align 4
>  %y = alloca i32, align 4
>  %a = alloca i32, align 4
>  %t = alloca i32, align 4
>
> 1. %10 = load i32* %x, align 4
> 2. %11 = load i32* %y, align 4
> 3. %div = sdiv i32 %10, %11
> 4. %12 = load i32* %a, align 4
> 5. %mul4 = mul nsw i32 %div, %12
>
> 6. store i32 %mul4, i32* %t, align 4
> a.                %mul4 = mul nsw i32 %div, %12
> b.                %div = sdiv i32 %10, %11
> c.                %10 = load i32* %x, align 4
> d.                %11 = load i32* %y, align 4
> e.                %12 = load i32* %a, align 4
>
> This is a IR of       " t = x/y*a "
> The address of the "%x, %y, %a" are "0xbe4ab94, 0xbe4abd4, 0xbe4ac54"
> respectively.
> and the opcode of "*, /" are "12, 15" respectively.
>
> Point (6.a to 6.e)  are the result of use-def chain, with the help of which
> I managed to make a string as -
>
> "12_15_0xbe4ab94_0xbe4abd4_0xbe4ac54" (by converting getOpcode() and
> getOperand() to string and than concatenating it)
>
> Now I want to pass this whole string to some other external function, How
> can I do this?

It's still rather unclear what you're attempting to achieve - are you
trying to pass this string to something else within the compiler? If
so, you'd pass a string the same way you'd pass any other string in
C++ (well, in LLVM:

void func(StringRef Str);
...
std::string foo = "12_15_....";
func(foo);

If you're trying to emit this string into the compiled binary &
produce a function call in that program... that's another story, but
again - looking at simple examples of code compiled by Clang should
give you an idea of what instructions you need to emit.

>
> If you want I can show you the full code.
>
>
>
> --
> View this message in context: http://llvm.1065342.n5.nabble.com/Passing-String-to-an-external-function-in-llvm-tp59798p59807.html
> Sent from the LLVM - Dev mailing list archive at Nabble.com.
> _______________________________________________
> LLVM Developers mailing list
> [hidden email]         http://llvm.cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
_______________________________________________
LLVM Developers mailing list
[hidden email]         http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
Reply | Threaded
Open this post in threaded view
|

Re: Passing String to an external function in llvm

Abhinash Jain
This post was updated on .
I have one file named hashtable.cpp whose link is  "http://pastebin.com/Cq2Qy50C"

and one llvm pass named testing.cpp whose link is "http://pastebin.com/E3RemxLF"

Now on this testing.cpp pass I have computed the string named "expr" (line 43, 50) which I want to pass to the function named hashtable(string) in hashtable.cpp   (on line 106 of testing.cpp)


> looking at simple examples of code compiled by Clang should give you an idea of what instructions you need to emit.

I did try but not able to pass the string.

If you could go through my code it will be of great help or if you have some better idea for doing the same.

Thanx
Reply | Threaded
Open this post in threaded view
|

Re: Passing String to an external function in llvm

David Blaikie
On Thu, Jul 25, 2013 at 11:40 AM, Abhinash Jain <[hidden email]> wrote:

> I have one file named hashtable.cpp whose link is
> "http://pastebin.com/Cq2Qy50C"
>
> and one llvm pass named testing.cpp whose link is
> "http://pastebin.com/E3RemxLF"
>
> Now on this testing.cpp pass I have computed the string named "expr" which I
> want to pass to the function named hashtable(string) in hashtable.cpp   (on
> line 106 of testing.cpp)
>
>
>> looking at simple examples of code compiled by Clang should give you an
>> idea of what instructions you need to emit.
>
> I did try but not able to pass the string.
>
> If you could go through my code it will be of great help. Thanx

OK - seems you might want to take a few steps back & understand how
C++ code is written/structured generally (and/or take a look at other
parts of the compiler). You'll need a header file with the declaration
of your function & you can include that header file in the
hashtable.cpp and testing.cpp - if that sentence doesn't make sense to
you yet, please find some general C++ programming resources to provide
further detail as such a discussion isn't really on-topic here.

- David
_______________________________________________
LLVM Developers mailing list
[hidden email]         http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
Reply | Threaded
Open this post in threaded view
|

Re: Passing String to an external function in llvm

Abhinash Jain
This post was updated on .
> OK - seems you might want to take a few steps back & understand how
> C++ code is written/structured generally (and/or take a look at other
> parts of the compiler). You'll need a header file with the declaration
> of your function & you can include that header file in the
> hashtable.cpp and testing.cpp - if that sentence doesn't make sense to
> you yet, please find some general C++ programming resources to provide
> further detail as such a discussion isn't really on-topic here.

@David Blaikie
I know it could be a bit frustrating for you to answer such foolish question, but am novice to llvm and desperately need some help to resolve this issue.

testing.cpp is an instrumentation file of llvm, whereas hashtable.cpp is simple c++ code.

I want to insert the function named hashtable() (which is already present in hashtable.cpp) after every store instruction.
So I have to first DECLARE it,  which can be done with getOrInsertFunction() in testing.cpp.
Now since declaration of function hashtable() is like

void hashtable(string expr)    //in hashtable.cpp

So in getOrInsertFunction() I have to pass some more details like

getOrInsertFunction (name of function in IR, Return type of function, List of argument for the function, etc.)
i.e. getOrInsertFunction("_Z14printHashTablev", Type::getVoidTy(M.getContext()), ????, ????,);

Now hashtable() function takes string as an argument, so what should I write in place of ???? above.

And than how should I actually CALL that hashtable() function with the help of methods
like CallInst::Create, getInstList().insert etc.
Reply | Threaded
Open this post in threaded view
|

Re: Passing String to an external function in llvm

David Blaikie
On Thu, Jul 25, 2013 at 1:45 PM, Abhinash Jain <[hidden email]> wrote:

>> OK - seems you might want to take a few steps back & understand how
>> C++ code is written/structured generally (and/or take a look at other
>> parts of the compiler). You'll need a header file with the declaration
>> of your function & you can include that header file in the
>> hashtable.cpp and testing.cpp - if that sentence doesn't make sense to
>> you yet, please find some general C++ programming resources to provide
>> further detail as such a discussion isn't really on-topic here.
>
> I know it could be a bit frustrating for you to answer such foolish
> question, but am novice to llvm and desperately need some help to resolve
> this issue.
>
> testing.cpp is an instrumentation file of llvm, whereas hashtable.cpp is
> simple c++ code.
>
> I want to insert the function named hashtable() (which is already present in
> hashtable.cpp) after every store instruction.
> So I have to first DECLARE it,  which can be done with getOrInsertFunction()
> in testing.cpp.
> Now since declaration of function hashtable() is like
>
> void hashtable(string expr)    //in hashtable.cpp
>
> So in getOrInsertFunction() I have to pass some more details like
>
> getOrInsertFunction (name of function in IR, Return type of function, List
> of argument for the function, etc.)
> i.e. getOrInsertFunction("_Z14printHashTablev",
> Type::getVoidTy(M.getContext()), ????, ????,);

I'd suggest you try looking at the IR that Clang generates for a call
to this function & produce similar IR yourself. If you're having
trouble figuring out which APIs to call to produce the same IR there
is a "cpp" backend to LLVM that can spit out the LLVM C++ API calls to
produce the same IR, I believe - I don't recall the specifics of how
to invoke it, though.

>
> Now hashtable() function takes string as an argument, so what should I write
> in place of ???? above.
>
> And than how should I actually CALL that hashtable() function with the help
> of methods
> like CallInst::Create, getInstList().insert etc.
>
>
>
> --
> View this message in context: http://llvm.1065342.n5.nabble.com/Passing-String-to-an-external-function-in-llvm-tp59798p59818.html
> Sent from the LLVM - Dev mailing list archive at Nabble.com.
> _______________________________________________
> LLVM Developers mailing list
> [hidden email]         http://llvm.cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
_______________________________________________
LLVM Developers mailing list
[hidden email]         http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev