On Apr 18, 2006, at 7:28 PM, sean yang wrote:
I will appreciate your help. Thanks in advance.
Let me give a concrete example of what I want to do (Please
understand I have other reasons to do this after gimplification,
though the example shows that there is a much simpler way to
achieve this): in the example, I want to add a call of instrument
() after each call of foo()
-----------------------
//input source: example.c
main(){
int i=0;
foo();
i++;
bar();
}
-----------------------------
//conceptual source: with my compiler, the output should look like
the output of the following source code
main(){
int i=0;
foo();
instrument(); //i.e, adding a node of CALL_EXPR (instrument)
after each node CALL_EXPR (foo)
i++;
bar();
}
-----------------------------
What I want to do is whenever I find a CALL_EXPR node, I look up
its ID to see if it is "foo". If it is, I add another CALL_EXPR
node with ID of "instrument". I have two questions:
(1) I know "call_node = build (CALL_EXPR, tree_node)" can build a
new node, but how can I fill the field of tree_node properly? i.e.,
make the content of tree_node be a call to "instrument". It seems
there is no API to do so.
You really want build_function_call_expr to build the call itself, it
takes a function decl and the argument list.
To build a function_decl, use build_decl (FUNCTION_DECL,
get_identifier ("name"), type);
If it returns a value, you want to build a modify_expr with the first
argument being the return value to assign to, and the second argument
being the result of build_function_call_expr.
(2) after the node the call_node has been built, how can I added to
the proper place? It seems that tsi_link_after () can't do what I
want under this situation because it's only used for
STATEMENT_LIST, not other type of nodes, like CALL_EXPR.
Actually, it would work fine, but it's not the right thing.
use bsi_from_stmt to get a bsi for the thing you are isnerting before/
after, and use bsi_insert_before/bsi_insert_after.
You really should be looking at tree-nested, tree-profile, omp-low, etc.
They all do this sort of thing