Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //provided by Pavel
- void printPart(tree node)
- {
- if (node == NULL_TREE) return;
- switch (TREE_CODE (node)) {
- case IDENTIFIER_NODE: printf("%s", IDENTIFIER_POINTER (node)); break; // names of functions (calls)
- case VAR_DECL: printf("%s", IDENTIFIER_POINTER (DECL_NAME (node))); break; // declarations
- case CONST_DECL: printf("%s", IDENTIFIER_POINTER (DECL_NAME (node))); break; // declarations
- case INTEGER_CST: printf("%d", TREE_INT_CST_LOW(node)); break; // ints
- case SSA_NAME: // arguments _x
- if (SSA_NAME_IDENTIFIER (node)) {
- printPart (SSA_NAME_IDENTIFIER (node));
- }
- printf("_%d", SSA_NAME_VERSION (node));
- break;
- default:
- break;
- }
- }
- const char* printSymbolCode (enum tree_code code)
- {
- switch (code)
- {
- case TRUTH_OR_EXPR:
- case TRUTH_ORIF_EXPR: return "||";
- case TRUTH_AND_EXPR:
- case TRUTH_ANDIF_EXPR: return "&&";
- case EQ_EXPR: return "==";
- case NE_EXPR: return "!=";
- case LT_EXPR: return "<";
- case LE_EXPR: return "<=";
- case GT_EXPR: return ">";
- case GE_EXPR: return ">=";
- case PLUS_EXPR: return "+";
- case MINUS_EXPR: return "-";
- case MULT_EXPR: return "*";
- default: return " ";
- }
- }
- void printStmt(gimple stmt)
- {
- tree lhs, rhs1, rhs2;
- switch (gimple_code(stmt))
- {
- case GIMPLE_COND: printf(" GIMPLE_COND:\n\t");
- lhs = gimple_assign_lhs (stmt);
- rhs1 = gimple_assign_rhs1 (stmt);
- printf("if (");
- printPart(lhs);
- printf(" %s ", op_symbol_code (gimple_cond_code (stmt)));
- printPart(rhs1);
- printf(") \n");
- break;
- case GIMPLE_CALL: printf(" GIMPLE_CALL:\n\t");
- lhs = gimple_call_lhs (stmt);
- if (lhs) {
- printPart(lhs);
- printf(" = ");
- }
- printPart(DECL_NAME (TREE_OPERAND(gimple_call_fn (stmt), 0)));
- printf("(");
- int i;
- for (i = 0; i < gimple_call_num_args (stmt); i++){
- printPart(gimple_call_arg (stmt, i));
- if (i < gimple_call_num_args (stmt) - 1) {
- printf(", ");
- }
- }
- printf(")\n");
- break;
- case GIMPLE_ASSIGN:
- printf(" ASSIGN: %d args\n\t", gimple_num_ops (stmt));
- switch (gimple_num_ops (stmt))
- {
- case 0:
- case 1: break;
- case 2:
- lhs = gimple_assign_lhs (stmt);
- rhs1 = gimple_assign_rhs1 (stmt);
- printPart(lhs);
- printf(" = ");
- printPart(rhs1);
- break;
- case 3:
- lhs = gimple_assign_lhs (stmt);
- rhs1 = gimple_assign_rhs1 (stmt);
- rhs2 = gimple_assign_rhs2 (stmt);
- printPart(lhs);
- printf(" = ");
- printPart(rhs1);
- //enum tree_code gimple_assign_rhs_code (gimple g)
- //Return the code of the expression computed on the RHS of assignment statement G.
- printf("%s ", printSymbolCode (gimple_assign_rhs_code (stmt)));
- printPart(rhs2);
- break;
- }
- break;
- case GIMPLE_RETURN: printf(" GIMPLE_RETURN\n\t");
- printPart(gimple_return_retval (stmt));
- printf("\n");
- break;
- default:
- break;
- }
- }
- void print_tree()
- {
- printf("Printing tree:\n");
- printf("Function: %s", function_name(cfun));
- basic_block bb;
- gimple_stmt_iterator gsi;
- gimple stmt;
- int i = 0, j = 0;
- FOR_EACH_BB_FN (bb, cfun)
- {
- j = 0;
- printf("\n BaseBlock:");
- for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
- {
- printf("\n Stmt %d:\n", j++);
- stmt = gsi_stmt (gsi);
- printStmt(stmt);
- }
- }
- }
- opt_gcc/build -> make install
- opt_gcc/install/bin -> ./gcc -O2 -o hello hello.c
- export LD_LIBRARY_PATH=/home/anthony/opt_gcc/externals/mpc_install/lib:${LD_LIBRARY_PATH}
- export LD_LIBRARY_PATH=/usr/lib/x86_64-linux-gnu:${LD_LIBRARY_PATH}
- #include <stdio.h>
- #include <time.h>
- #include <stdlib.h>
- int notUsedFunc(int x) {
- return x * 322;
- }
- int simpleFunc(int a, int b) {
- int d = a + b;
- return d;
- }
- int main (void)
- {
- srand(time(NULL));
- int cond = rand() -2;
- printf("Hello, World!\n");
- int b, c, a;
- a = 322;
- c = 633;
- if (cond > 0) {
- a += simpleFunc(a, b);
- } else {
- a -= simpleFunc(a, b);
- }
- printf("c=%d, a=%d\n", c, a);
- return 0;
- }
- anthony@tokyonyquisd:~/opt_gcc/install/bin$ ./gcc -O2 -o hello hello.cPrinting tree:
- Function: notUsedFunc
- BaseBlock:
- Stmt 0:
- ASSIGN: 3 args
- _2 = x_1* 322
- Stmt 1:
- GIMPLE_RETURN
- _2
- Printing tree:
- Function: simpleFunc
- BaseBlock:
- Stmt 0:
- ASSIGN: 3 args
- d_3 = a_1+ b_2
- Stmt 1:
- GIMPLE_RETURN
- d_3
- Printing tree:
- Function: main
- BaseBlock:
- Stmt 0:
- GIMPLE_CALL:
- _4 = time(0)
- Stmt 1:
- ASSIGN: 2 args
- _5 = _4
- Stmt 2:
- GIMPLE_CALL:
- srand(_5)
- Stmt 3:
- GIMPLE_CALL:
- _8 = rand()
- Stmt 4:
- ASSIGN: 3 args
- _9 = _8 5
- Stmt 5:
- GIMPLE_CALL:
- __builtin_puts()
- Stmt 6:
- GIMPLE_COND:
- if (_9 > 2)
- BaseBlock:
- Stmt 0:
- ASSIGN: 3 args
- d_21 = b_14+ 322
- Stmt 1:
- ASSIGN: 3 args
- a_16 = d_21+ 322
- BaseBlock:
- Stmt 0:
- ASSIGN: 3 args
- d_22 = b_14+ 322
- Stmt 1:
- ASSIGN: 3 args
- a_18 = 322- d_22
- BaseBlock:
- Stmt 0:
- GIMPLE_CALL:
- printf(, 633, a_1)
- Stmt 1:
- GIMPLE_RETURN
- 0
- Printing tree:
- Function: notUsedFunc
- BaseBlock:
- Stmt 0:
- ASSIGN: 3 args
- _2 = x_1* 322
- Stmt 1:
- GIMPLE_RETURN
- _2
- Printing tree:
- Function: simpleFunc
- BaseBlock:
- Stmt 0:
- ASSIGN: 3 args
- d_3 = a_1+ b_2
- Stmt 1:
- GIMPLE_RETURN
- d_3
- Printing tree:
- Function: main
- BaseBlock:
- Stmt 0:
- GIMPLE_CALL:
- _4 = time(0)
- Stmt 1:
- ASSIGN: 2 args
- _5 = _4
- Stmt 2:
- GIMPLE_CALL:
- srand(_5)
- Stmt 3:
- GIMPLE_CALL:
- _8 = rand()
- Stmt 4:
- ASSIGN: 3 args
- _9 = _8 5
- Stmt 5:
- GIMPLE_CALL:
- __builtin_puts()
- Stmt 6:
- GIMPLE_COND:
- if (_9 > 2)
- BaseBlock:
- Stmt 0:
- ASSIGN: 3 args
- a_12 = b_11+ 644
- BaseBlock:
- Stmt 0:
- ASSIGN: 2 args
- a_13 = b_11
- BaseBlock:
- Stmt 0:
- GIMPLE_CALL:
- printf(, 633, a_1)
- Stmt 1:
- GIMPLE_RETURN
- 0
- anthony@tokyonyquisd:~/opt_gcc/install/bin$
Advertisement
Add Comment
Please, Sign In to add comment