jilc-patches
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Jilc-patches] CVS: jilc/src jdasm.c,1.3,1.4 jdasm.h,1.4,1.5


From: Gopal.V <address@hidden>
Subject: [Jilc-patches] CVS: jilc/src jdasm.c,1.3,1.4 jdasm.h,1.4,1.5
Date: Mon, 25 Feb 2002 10:31:14 -0500

Update of /cvsroot/jilc/jilc/src
In directory subversions:/tmp/cvs-serv565

Modified Files:
        jdasm.c jdasm.h 
Log Message:
 implement label management, clean up code using READ_INT32 & READ_INT16,
fix SWITCH opcode parameter


Index: jdasm.c
===================================================================
RCS file: /cvsroot/jilc/jilc/src/jdasm.c,v
retrieving revision 1.3
retrieving revision 1.4
diff -C2 -r1.3 -r1.4
*** jdasm.c     22 Feb 2002 07:01:32 -0000      1.3
--- jdasm.c     25 Feb 2002 15:31:10 -0000      1.4
***************
*** 64,69 ****
        }
  }
  
! void print_byte_code(byte *code, int *start_index, int code_len, JVCPEntry 
*cp_entry)
  {
        int   start = *start_index;
--- 64,163 ----
        }
  }
+ /** 
+  * collect JUMP offset, 
+  * param @code the bytecode
+  * param @codelen code length
+  */
+ UInt32 * get_jump_points(byte *code,int codelen)
+ {
+       Int32 *jump_points;
+       jopcode_map found;
+       int start=0;
+       int i,count,j;
+       Int32 high,low;
+       Int32 dest;
+       Int32 insn_offset;
+       jump_points=(UInt32*)calloc(codelen >> 5,sizeof(UInt32));
+       //a bit for each byte in code
+       while(start<codelen)
+       {
+                       i=search_jopcode_map(code[start]);
+                       found=get_jopcode_map(i);
+                       insn_offset=start;
+                       switch(found.args)
+                       {
+                               /*case NONE: 
+                                               start++; // default: already 
does this
+                                               break;*/
+                               case CONSTANT:
+                               case I:
+                                               start+=2;
+                                               break;
+                               case FIELD:
+                               case METHOD:
+                               case CLASS:
+                               case BIGCONSTANT:
+                               case II:                                
+                                               start+=3;
+                                               break;
+                               case SWITCH:                                    
                
+                                    start++;
+                                    while( (start % 4) != 0)
+                                    {
+                                                        start++;
+                                            }
+                                                
dest=insn_offset+READ_INT32(code,start);
+                                                if(dest<codelen) // default 
offset
+                                                       
jump_points[dest/32]|=(UInt32)(1L << (dest%32));
+                                                else fprintf(stderr,"illegal 
jump offset %ld\n",dest);
+                                            start += 4;
+                                                if(found.opcode == 
JAVA_LOOKUPSWITCH)
+                                                {
+                                               count = READ_INT32(code,start);
+                                                   start += 4;
+                                                       for(j=0;j<count;j++)
+                                                       {
+                                                               start+=4;//skip 
key
+                                                               
dest=insn_offset+READ_INT32(code,start);
+                                                               if(dest<codelen)
+                                                                       
jump_points[dest/32]|=(UInt32)(1L <<\
+                                                                               
                        (dest%32));
+                                                               else 
fprintf(stderr,"illegal jump offset");
+                                                               start+=4;
+                                                       }
+                                                }
+                                                else if (found.opcode == 
JAVA_TABLESWITCH)
+                                                {
+                                                       low = 
READ_INT32(code,start);
+                                                       start+= 4;
+                                                       high = 
READ_INT32(code,start);
+                                                       start+= 4;
+                                                       for(j=low;j<=high;j++)
+                                                       {
+                                                               
dest=insn_offset+READ_INT32(code,start);
+                                                               if(dest<codelen)
+                                                                       
jump_points[dest/32]|=(UInt32)(1L <<\
+                                                                               
                        (dest%32));                                             
                
+                                                               else 
fprintf(stderr,"illegal jump offset");
+                                                               start+=4;
+                                                       }
+                                                 }
+                                               break;
+                               case LABEL:
+                                               start++;
+                                               
dest=insn_offset+READ_INT16(code,start);
+                                               if(dest<codelen)
+                                                       
jump_points[dest/32]|=(UInt32)(1L << (dest%32));
+                                               else fprintf(stderr,"illegal 
jump offset %ld\n",dest);
+                                               start+=2;
+                               default:
+                                               start++;
+                                               break;
+                       }
+       }
+       return jump_points;
+ }
  
! void print_byte_code(byte *code, int *start_index, int code_len, UInt32 
*jump_points,JVCPEntry *cp_entry)
  {
        int   start = *start_index;
***************
*** 72,77 ****
--- 166,173 ----
        Int32 default_offset;
        Int32 count;
+       Int32 low,high;
        int   insn_offset = start;
        int   j;
+       Int16 tmp;//to solve glib's printf signing problem ! 
  
        int   i = search_jopcode_map(code[start]);
***************
*** 81,84 ****
--- 177,184 ----
        {
                found = get_jopcode_map(i);
+               if((jump_points[insn_offset/32] & (UInt32)(1L << (insn_offset 
%32)))!=0)
+                       printf("j_%04d :",insn_offset);
+               else
+                       printf("\t");
                switch(found.args)
                {
***************
*** 87,96 ****
                                     break;
                        case I:      PRINT_OP_NAME(found);
                                     printf(" %d ", code[start + 1]);
!                                    start += 2;
                                     break;
                        case II:     PRINT_OP_NAME(found);
                                     printf(" %d %d", (code[start + 1]), 
(code[start + 2]));
!                                    start += 3;
                                     break;
                        case FIELD:
--- 187,198 ----
                                     break;
                        case I:      PRINT_OP_NAME(found);
+                                                start++;
                                     printf(" %d ", code[start + 1]);
!                                    start += 1;
                                     break;
                        case II:     PRINT_OP_NAME(found);
+                                                start++;
                                     printf(" %d %d", (code[start + 1]), 
(code[start + 2]));
!                                    start += 2;
                                     break;
                        case FIELD:
***************
*** 99,109 ****
                        case BIGCONSTANT:
                                     PRINT_OP_NAME(found);
!                                    print_string_cp(cp_entry, (code[start + 1] 
<< 0x08) | code[start + 2]);
!                                    start += 3;
                                     break;
                        case CONSTANT:
                                     PRINT_OP_NAME(found);
                                     print_string_cp(cp_entry, code[start + 1]);
!                                    start += 2;
                                     break;
                        case SWITCH: PRINT_OP_NAME(found);
--- 201,213 ----
                        case BIGCONSTANT:
                                     PRINT_OP_NAME(found);
!                                                start++;
!                                    print_string_cp(cp_entry, 
READ_INT16(code,start));
!                                    start += 2;
                                     break;
                        case CONSTANT:
                                     PRINT_OP_NAME(found);
+                                                start++;
                                     print_string_cp(cp_entry, code[start + 1]);
!                                    start += 1;
                                     break;
                        case SWITCH: PRINT_OP_NAME(found);
***************
*** 113,149 ****
                                                         start++;
                                             }
!                                            default_offset = 
(((Int32)code[start]) << 0x18) | \
!                                                             
(((Int32)code[start + 1]) << 0x10) | \
!                                                             
(((Int32)code[start + 2]) << 0x08) | \
!                                                             
((Int32)code[start + 3]);
                                             start += 4;
!                                            count = (((Int32)code[start]) << 
0x18) | \
!                                                    (((Int32)code[start + 1]) 
<< 0x10) | \
!                                                    (((Int32)code[start + 2]) 
<< 0x08) | \
!                                                    ((Int32)code[start + 3]);
!                                            start += 4;
!                                            printf(" %ld key-values\n", count);
!                                            for(j = 0; j < count; j++)
!                                            {
!                                                    key = 
(((Int32)code[start]) << 0x18) | \
!                                                      (((Int32)code[start + 
1]) << 0x10) | \
!                                                      (((Int32)code[start + 
2]) << 0x08) | \
!                                                      ((Int32)code[start + 3]);
                                                 start += 4;
!                                                offset = (((Int32)code[start]) 
<< 0x18) | \
!                                                         (((Int32)code[start + 
1]) << 0x10) | \
!                                                         (((Int32)code[start + 
2]) << 0x08) | \
!                                                         ((Int32)code[start + 
3]);
                                                 start += 4;
!                                                printf("\t\t%ld\t: %ld\n", 
key, insn_offset + offset);
!                                            }
!                                            printf("\t\tdefault: %ld\n", 
insn_offset + default_offset);
                                             break;
                        case LABEL:  PRINT_OP_NAME(found);
!                                    printf("\t %d \t\t//code offset ", 
insn_offset + \
!                                           (Int16)(code[start + 1] << 0x08 | 
code[start + 2]) );
! //                              ( ((Int16)code[start + 1]) << 0x08 | \
! //                              ((Int16)code[start + 2]) ) );
!                                    start += 3;
                                     break;
                        default:     printf(": Unknown instruction %d", 
code[start]);
--- 217,267 ----
                                                         start++;
                                             }
!                                            default_offset = 
READ_INT32(code,start);
                                             start += 4;
!                                                if(found.opcode == 
JAVA_LOOKUPSWITCH)
!                                                {
!                                                count = READ_INT32(code,start);
!                                                    start += 4;
!                                                    printf(" %ld 
key-values\n", count);
!                                                    for(j = 0; j < count; j++)
!                                                    {
!                                                    key = 
READ_INT32(code,start);
                                                 start += 4;
!                                                offset = 
READ_INT32(code,start); 
                                                 start += 4;
!                                                printf("\t\t%ld\t: j_%04d\n", 
key, insn_offset + \
!                                                                               
         offset);
!                                                    }
!                                                printf("\t\tdefault\t: 
j_%04d", insn_offset + \
!                                                                               
         default_offset);
!                                                }
!                                                else if (found.opcode == 
JAVA_TABLESWITCH)
!                                                {
!                                                                low = 
READ_INT32(code,start);
!                                                                start+= 4;
!                                                                printf(" %d; 
low value\n",low);
!                                                                high = 
READ_INT32(code,start);
!                                                                start+= 4;
!                                                                if(low>high)
!                                                                {
!                                                                       
fprintf(stderr,"Error !\n");
!                                                                       return;
!                                                                }
!                                                                
for(j=low;j<=high;j++)
!                                                                {
!                                                               offset = 
READ_INT32(code,start); 
!                                                               start += 4;
!                                                               printf("\t\t 
j_%04d; offset for %d\n",\
!                                                                               
                        insn_offset + offset,j);
!                                                                }
!                                               printf("\t default:j_%04d", 
insn_offset + \
!                                                                               
                default_offset);
!                                                }
                                             break;
                        case LABEL:  PRINT_OP_NAME(found);
!                                                start++;
!                                                tmp= insn_offset + 
READ_INT16(code,start);
!                                                printf(" j_%04d",tmp);
!                                    start += 2;
                                     break;
                        default:     printf(": Unknown instruction %d", 
code[start]);
***************
*** 161,164 ****
--- 279,283 ----
        byte *code    = 
method->attr_entries[method->code_index].attributes.code.code;
        int   codelen = 
method->attr_entries[method->code_index].attributes.code.code_count;
+       UInt32 *jump_points;
  
        printf(".limit stack %d\n", 
method->attr_entries[method->code_index].attributes.code.max_stack);
***************
*** 169,178 ****
                return;
        }
! 
        while(start < codelen)
        {
!               print_byte_code(code, &start, codelen, cp_entry);
        }
!       printf(": TODO-> label management\n");
        printf(": TODO-> wide opcode not supported\n");
  }
--- 288,297 ----
                return;
        }
!       jump_points=get_jump_points(code,codelen);
        while(start < codelen)
        {
!               print_byte_code(code, &start, codelen, jump_points, cp_entry);
        }
!       //printf(": TODO-> label management\n"); worked out
        printf(": TODO-> wide opcode not supported\n");
  }
***************
*** 260,262 ****
        print_class(klass, klass->cp_entries);
        return 0;
! }
\ No newline at end of file
--- 379,381 ----
        print_class(klass, klass->cp_entries);
        return 0;
! }

Index: jdasm.h
===================================================================
RCS file: /cvsroot/jilc/jilc/src/jdasm.h,v
retrieving revision 1.4
retrieving revision 1.5
diff -C2 -r1.4 -r1.5
*** jdasm.h     21 Feb 2002 13:25:20 -0000      1.4
--- jdasm.h     25 Feb 2002 15:31:10 -0000      1.5
***************
*** 13,18 ****
  #include <jparser.h>
  
! #define PRINT_OP_NAME(x)  printf("\t %s ", (x).mnemonic)
  
  /**
   * Methods to help printing the deassmbled code.
--- 13,24 ----
  #include <jparser.h>
  
! #define PRINT_OP_NAME(x)  printf(" %s ", (x).mnemonic)
  
+ #define READ_INT32(code,start) ( (((Int32)code[start]) << 0x18) | \
+                                                    (((Int32)code[start + 1]) 
<< 0x10) | \
+                                                    (((Int32)code[start + 2]) 
<< 0x08) | \
+                                                     ((Int32)code[start + 3]) )
+ #define READ_INT16(code,start) ( (((Int16)code[start]) << 0x08 | \
+                                    (Int16)code[start+1]) )
  /**
   * Methods to help printing the deassmbled code.
***************
*** 22,26 ****
  void print_fields(JVClass *klass, JVCPEntry *cp_entry);
  void print_string_cp(JVCPEntry *cp_entry, int i);
! void print_byte_code(byte *code, int *start_index, int code_len, JVCPEntry 
*cp_entry);
  void print_method_code(JVMethodEntry *method, JVCPEntry *cp_entry);
  void print_methods(JVClass *klass, JVCPEntry *cp_entry);
--- 28,32 ----
  void print_fields(JVClass *klass, JVCPEntry *cp_entry);
  void print_string_cp(JVCPEntry *cp_entry, int i);
! void print_byte_code(byte *code, int *start_index, int code_len, UInt32 * 
jump_points, JVCPEntry *cp_entry);
  void print_method_code(JVMethodEntry *method, JVCPEntry *cp_entry);
  void print_methods(JVClass *klass, JVCPEntry *cp_entry);




reply via email to

[Prev in Thread] Current Thread [Next in Thread]