Ladies_Man

OPTCODE Lab1 Gimple COMPLETE

Jun 13th, 2016
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.10 KB | None | 0 0
  1. //original idea by fedor
  2.  
  3. void print_tree_obj(tree node)
  4. {
  5.   if (node == NULL_TREE) return;
  6.  
  7.   switch (TREE_CODE (node)) {
  8.     case IDENTIFIER_NODE:
  9.         printf("%s", IDENTIFIER_POINTER (node));              
  10.         break;
  11.     case VAR_DECL:        
  12.         printf("%s", IDENTIFIER_POINTER (DECL_NAME (node)));  
  13.         break;
  14.     case CONST_DECL:      
  15.         printf("%s", IDENTIFIER_POINTER (DECL_NAME (node)));
  16.         break;
  17.     case INTEGER_CST:    
  18.         printf("%d", TREE_INT_CST_LOW(node));                
  19.         break;
  20.     case STRING_CST:      
  21.         printf("\"%s\"", TREE_STRING_POINTER (node));        
  22.         break;
  23.     case SSA_NAME:
  24.         if (SSA_NAME_IDENTIFIER (node)) {
  25.             print_tree_obj (SSA_NAME_IDENTIFIER (node));
  26.         }
  27.         printf("_%d", SSA_NAME_VERSION (node));
  28.         break;
  29.     default:
  30.         break;
  31.   }
  32. }
  33.  
  34.  
  35. #define PRINT_CASE(c) case c:printf(#c); break
  36.  
  37.  
  38. void print_stmt(gimple stmt)
  39. {
  40.     int i;
  41.     switch (gimple_code(stmt)) {
  42.      
  43.     case GIMPLE_ASM:
  44.       printf("asm code\n");
  45.       break;
  46.      
  47.     case GIMPLE_CALL: {
  48.       printf("-------CALL-------\n");
  49.       tree callee = gimple_call_fndecl (stmt);
  50.  
  51.       for (i = 0; i < gimple_call_num_args(stmt); i++) {
  52.         tree arg = gimple_call_arg(stmt, i);
  53.         printf(" [%d]: ", i);
  54.         print_tree_obj(arg);
  55.  
  56.       }
  57.    
  58.     } break;
  59.    
  60.     case GIMPLE_ASSIGN: {
  61.       tree lhs = gimple_assign_lhs(stmt);
  62.       tree rhs1, rhs2;
  63.    
  64.       int count = gimple_num_ops(stmt);
  65.  
  66.       printf("------ASSIGN------\n");
  67.       printf("assign with %d argument(s)\n", count-1);
  68.  
  69.       switch (gimple_num_ops (stmt))
  70.       {
  71.         case 0:
  72.         case 1: break;
  73.         case 2:
  74.           rhs1 = gimple_assign_rhs1 (stmt);
  75.           print_tree_obj(lhs);
  76.           printf(" = ");
  77.           print_tree_obj(rhs1);
  78.           break;
  79.         case 3:
  80.           rhs1 = gimple_assign_rhs1 (stmt);
  81.           rhs2 = gimple_assign_rhs2 (stmt);
  82.           print_tree_obj(lhs);
  83.           printf(" = ");
  84.           print_tree_obj(rhs1);
  85.           printf(", ");
  86.           print_tree_obj(rhs2);
  87.           break;
  88.       }
  89.   } break;
  90.    
  91.   case GIMPLE_RESX: {
  92.       printf("-------RESX-------\n");
  93.     } break;
  94.   case GIMPLE_RETURN: {
  95.       printf("------RETURN------\n");
  96.      
  97.     } break;
  98.   case GIMPLE_SWITCH: {
  99.       printf("------SWITCH------\n");
  100.     } break;
  101.   case GIMPLE_TRY: {
  102.       printf("-------TRY--------\n");
  103.     } break;
  104.   case GIMPLE_GOTO: {
  105.       printf("-------GOTO-------\n");
  106.     } break;
  107.   case GIMPLE_COND: {
  108.       printf("-------COND-------\n");
  109.    
  110.     } break;
  111.   default: {
  112.       printf("default case %d\n", gimple_code(stmt));
  113.     }
  114.    }
  115.    printf("\n");
  116.  
  117. }
  118.  
  119.  
  120. void print_tree()
  121. {
  122.     basic_block bb;
  123.     gimple_stmt_iterator gsi;
  124.     edge e;
  125.     gimple phi, stmt;
  126.     int flags;
  127.  
  128.     printf("===%s===\n", function_name(cfun));
  129.  
  130.     FOR_EACH_BB_FN (bb, cfun)
  131.     {
  132.     for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
  133.     {
  134.       stmt = gsi_stmt (gsi);
  135.       print_stmt(stmt);
  136.     }
  137.       }
  138.  
  139.     printf("=====================\n\n\n");
  140.  
  141. }
Advertisement
Add Comment
Please, Sign In to add comment