Ladies_Man

#Codegen Lab1 Gimple COMPLETE

Oct 24th, 2016
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.00 KB | None | 0 0
  1. //provided by Pavel
  2.  
  3.  
  4. void printPart(tree node)
  5. {
  6.   if (node == NULL_TREE) return;
  7.  
  8.   switch (TREE_CODE (node)) {
  9.       case IDENTIFIER_NODE: printf("%s", IDENTIFIER_POINTER (node));              break; // names of functions (calls)
  10.       case VAR_DECL:        printf("%s", IDENTIFIER_POINTER (DECL_NAME (node)));  break; // declarations
  11.       case CONST_DECL:      printf("%s", IDENTIFIER_POINTER (DECL_NAME (node)));  break; // declarations
  12.       case INTEGER_CST:     printf("%d", TREE_INT_CST_LOW(node));                 break; // ints
  13.       case SSA_NAME:  // arguments _x
  14.           if (SSA_NAME_IDENTIFIER (node)) {
  15.               printPart (SSA_NAME_IDENTIFIER (node));
  16.           }
  17.           printf("_%d", SSA_NAME_VERSION (node));
  18.           break;
  19.       default:
  20.           break;
  21.   }
  22. }
  23.  
  24. const char* printSymbolCode (enum tree_code code)
  25. {
  26.     switch (code)
  27.     {
  28.         case TRUTH_OR_EXPR:
  29.         case TRUTH_ORIF_EXPR:   return "||";
  30.         case TRUTH_AND_EXPR:
  31.         case TRUTH_ANDIF_EXPR:  return "&&";
  32.         case EQ_EXPR:           return "==";
  33.         case NE_EXPR:           return "!=";
  34.         case LT_EXPR:           return "<";
  35.         case LE_EXPR:           return "<=";
  36.         case GT_EXPR:           return ">";
  37.         case GE_EXPR:           return ">=";
  38.         case PLUS_EXPR:         return "+";
  39.         case MINUS_EXPR:        return "-";
  40.         case MULT_EXPR:         return "*";
  41.         default:                return " ";
  42.     }  
  43. }
  44. void printStmt(gimple stmt)
  45. {
  46.     tree lhs, rhs1, rhs2;
  47.     switch (gimple_code(stmt))
  48.     {
  49.       case GIMPLE_COND: printf("      GIMPLE_COND:\n\t");
  50.           lhs = gimple_assign_lhs (stmt);
  51.           rhs1 = gimple_assign_rhs1 (stmt);
  52.           printf("if (");
  53.           printPart(lhs);
  54.           printf(" %s ", op_symbol_code (gimple_cond_code (stmt)));
  55.           printPart(rhs1);
  56.           printf(") \n");
  57.           break;
  58.  
  59.       case GIMPLE_CALL: printf("      GIMPLE_CALL:\n\t");
  60.           lhs = gimple_call_lhs (stmt);
  61.           if (lhs) {
  62.               printPart(lhs);
  63.               printf(" = ");
  64.           }
  65.           printPart(DECL_NAME (TREE_OPERAND(gimple_call_fn (stmt), 0)));
  66.           printf("(");
  67.           int i;
  68.           for (i = 0; i < gimple_call_num_args (stmt); i++){
  69.               printPart(gimple_call_arg (stmt, i));
  70.               if (i < gimple_call_num_args (stmt) - 1) {
  71.                   printf(", ");
  72.               }
  73.           }
  74.           printf(")\n");
  75.           break;
  76.  
  77.       case GIMPLE_ASSIGN:
  78.         printf("      ASSIGN: %d args\n\t", gimple_num_ops (stmt));
  79.    
  80.         switch (gimple_num_ops (stmt))
  81.         {
  82.             case 0:
  83.             case 1: break;
  84.             case 2:
  85.                 lhs = gimple_assign_lhs (stmt);
  86.                 rhs1 = gimple_assign_rhs1 (stmt);
  87.                 printPart(lhs);
  88.                 printf(" = ");
  89.                 printPart(rhs1);
  90.                 break;
  91.             case 3:
  92.                 lhs = gimple_assign_lhs (stmt);
  93.                 rhs1 = gimple_assign_rhs1 (stmt);
  94.                 rhs2 = gimple_assign_rhs2 (stmt);
  95.                 printPart(lhs);
  96.                 printf(" = ");
  97.                 printPart(rhs1);
  98.                 //enum tree_code gimple_assign_rhs_code (gimple g)
  99.                 //Return the code of the expression computed on the RHS of assignment statement G.
  100.                 printf("%s ", printSymbolCode (gimple_assign_rhs_code (stmt)));
  101.                 printPart(rhs2);
  102.                 break;
  103.           }
  104.       break;
  105.       case GIMPLE_RETURN: printf("      GIMPLE_RETURN\n\t");
  106.           printPart(gimple_return_retval (stmt));  
  107.           printf("\n");
  108.           break;
  109.       default:
  110.           break;
  111.     }
  112. }
  113.  
  114.  
  115. void print_tree()
  116. {
  117.     printf("Printing tree:\n");
  118.  
  119.     printf("Function: %s", function_name(cfun));
  120.    
  121.     basic_block bb;
  122.     gimple_stmt_iterator gsi;
  123.     gimple stmt;
  124.    
  125.     int i = 0, j = 0;
  126.     FOR_EACH_BB_FN (bb, cfun)
  127.     {
  128.         j = 0;
  129.         printf("\n  BaseBlock:");
  130.         for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
  131.         {
  132.             printf("\n    Stmt %d:\n", j++);
  133.             stmt = gsi_stmt (gsi);
  134.             printStmt(stmt);
  135.         }
  136.     }
  137.  
  138. }
  139.  
  140.  
  141.  
  142.  
  143. opt_gcc/build -> make install
  144. opt_gcc/install/bin -> ./gcc -O2 -o hello hello.c
  145. export LD_LIBRARY_PATH=/home/anthony/opt_gcc/externals/mpc_install/lib:${LD_LIBRARY_PATH}
  146. export LD_LIBRARY_PATH=/usr/lib/x86_64-linux-gnu:${LD_LIBRARY_PATH}
  147.  
  148.  
  149.  
  150. #include <stdio.h>
  151. #include <time.h>
  152. #include <stdlib.h>
  153.  
  154. int notUsedFunc(int x) {
  155.     return x * 322;
  156. }
  157.  
  158. int simpleFunc(int a, int b) {
  159.     int d = a + b;
  160.     return d;
  161. }
  162.  
  163. int main (void)
  164. {
  165.     srand(time(NULL));
  166.     int cond = rand() -2;
  167.  
  168.     printf("Hello, World!\n");
  169.  
  170.     int b, c, a;
  171.     a = 322;
  172.     c = 633;
  173.  
  174.     if (cond > 0) {
  175.         a += simpleFunc(a, b);
  176.     } else {
  177.         a -= simpleFunc(a, b);
  178.     }
  179.  
  180.    
  181.     printf("c=%d, a=%d\n", c, a);
  182.     return 0;
  183. }
  184.  
  185.  
  186.  
  187.  
  188. anthony@tokyonyquisd:~/opt_gcc/install/bin$ ./gcc -O2 -o hello hello.cPrinting tree:
  189. Function: notUsedFunc
  190.   BaseBlock:
  191.     Stmt 0:
  192.       ASSIGN: 3 args
  193.     _2 = x_1* 322
  194.     Stmt 1:
  195.       GIMPLE_RETURN
  196.     _2
  197. Printing tree:
  198. Function: simpleFunc
  199.   BaseBlock:
  200.     Stmt 0:
  201.       ASSIGN: 3 args
  202.     d_3 = a_1+ b_2
  203.     Stmt 1:
  204.       GIMPLE_RETURN
  205.     d_3
  206. Printing tree:
  207. Function: main
  208.   BaseBlock:
  209.     Stmt 0:
  210.       GIMPLE_CALL:
  211.     _4 = time(0)
  212.  
  213.     Stmt 1:
  214.       ASSIGN: 2 args
  215.     _5 = _4
  216.     Stmt 2:
  217.       GIMPLE_CALL:
  218.     srand(_5)
  219.  
  220.     Stmt 3:
  221.       GIMPLE_CALL:
  222.     _8 = rand()
  223.  
  224.     Stmt 4:
  225.       ASSIGN: 3 args
  226.     _9 = _8  5
  227.     Stmt 5:
  228.       GIMPLE_CALL:
  229.     __builtin_puts()
  230.  
  231.     Stmt 6:
  232.       GIMPLE_COND:
  233.     if (_9 > 2)
  234.  
  235.   BaseBlock:
  236.     Stmt 0:
  237.       ASSIGN: 3 args
  238.     d_21 = b_14+ 322
  239.     Stmt 1:
  240.       ASSIGN: 3 args
  241.     a_16 = d_21+ 322
  242.   BaseBlock:
  243.     Stmt 0:
  244.       ASSIGN: 3 args
  245.     d_22 = b_14+ 322
  246.     Stmt 1:
  247.       ASSIGN: 3 args
  248.     a_18 = 322- d_22
  249.   BaseBlock:
  250.     Stmt 0:
  251.       GIMPLE_CALL:
  252.     printf(, 633, a_1)
  253.  
  254.     Stmt 1:
  255.       GIMPLE_RETURN
  256.     0
  257. Printing tree:
  258. Function: notUsedFunc
  259.   BaseBlock:
  260.     Stmt 0:
  261.       ASSIGN: 3 args
  262.     _2 = x_1* 322
  263.     Stmt 1:
  264.       GIMPLE_RETURN
  265.     _2
  266. Printing tree:
  267. Function: simpleFunc
  268.   BaseBlock:
  269.     Stmt 0:
  270.       ASSIGN: 3 args
  271.     d_3 = a_1+ b_2
  272.     Stmt 1:
  273.       GIMPLE_RETURN
  274.     d_3
  275. Printing tree:
  276. Function: main
  277.   BaseBlock:
  278.     Stmt 0:
  279.       GIMPLE_CALL:
  280.     _4 = time(0)
  281.  
  282.     Stmt 1:
  283.       ASSIGN: 2 args
  284.     _5 = _4
  285.     Stmt 2:
  286.       GIMPLE_CALL:
  287.     srand(_5)
  288.  
  289.     Stmt 3:
  290.       GIMPLE_CALL:
  291.     _8 = rand()
  292.  
  293.     Stmt 4:
  294.       ASSIGN: 3 args
  295.     _9 = _8  5
  296.     Stmt 5:
  297.       GIMPLE_CALL:
  298.     __builtin_puts()
  299.  
  300.     Stmt 6:
  301.       GIMPLE_COND:
  302.     if (_9 > 2)
  303.  
  304.   BaseBlock:
  305.     Stmt 0:
  306.       ASSIGN: 3 args
  307.     a_12 = b_11+ 644
  308.   BaseBlock:
  309.     Stmt 0:
  310.       ASSIGN: 2 args
  311.     a_13 = b_11
  312.   BaseBlock:
  313.     Stmt 0:
  314.       GIMPLE_CALL:
  315.     printf(, 633, a_1)
  316.  
  317.     Stmt 1:
  318.       GIMPLE_RETURN
  319.     0
  320. anthony@tokyonyquisd:~/opt_gcc/install/bin$
Advertisement
Add Comment
Please, Sign In to add comment