Passing String to an external function in llvm

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..............

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?

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.

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.

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.

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.

I have one file named hashtable.cpp whose link is
"http://pastebin.com/Cq2Qy50C&quot;

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

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

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()), ???, ???,);

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.

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.