GNU header update.
[cacao.git] / src / vm / jit / parse.c
index abd7517767bd3e8330fd0dbcdd4f454c42aaf130..807c20accdbcaf21152986e38ccc6cd46a509b01 100644 (file)
@@ -1,9 +1,9 @@
-/* jit/parse.c - parser for JavaVM to intermediate code translation
+/* vm/jit/parse.c - parser for JavaVM to intermediate code translation
 
-   Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
-   R. Grafl, A. Krall, C. Kruegel, C. Oates, R. Obermaisser,
-   M. Probst, S. Ring, E. Steiner, C. Thalinger, D. Thuernbeck,
-   P. Tomsich, J. Wenninger
+   Copyright (C) 1996-2005 R. Grafl, A. Krall, C. Kruegel, C. Oates,
+   R. Obermaisser, M. Platter, M. Probst, S. Ring, E. Steiner,
+   C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich, J. Wenninger,
+   Institut f. Computersprachen - TU Wien
 
    This file is part of CACAO.
 
 
    Changes: Carolyn Oates
             Edwin Steiner
+            Joseph Wenninger
 
-   $Id: parse.c 1432 2004-11-03 12:14:50Z jowenn $
+   $Id: parse.c 1735 2004-12-07 14:33:27Z twisti $
 
 */
 
 
 #include <string.h>
-#include "parse.h"
-#include "global.h"
-#include "jit.h"
-#include "parseRT.h"
-#include "inline.h"
-#include "loop/loop.h"
+
+#include "config.h"
 #include "types.h"
-#include "builtin.h"
-#include "exceptions.h"
-#include "tables.h"
-#include "native.h"
-#include "loader.h"
-#include "options.h"
-#include "statistics.h"
-#include "toolbox/memory.h"
+#include "mm/memory.h"
+#include "native/native.h"
 #include "toolbox/logging.h"
+#include "vm/builtin.h"
+#include "vm/exceptions.h"
+#include "vm/global.h"
+#include "vm/loader.h"
+#include "vm/options.h"
+#include "vm/statistics.h"
+#include "vm/tables.h"
+#include "vm/jit/jit.h"
+#include "vm/jit/parse.h"
+#include "vm/jit/inline/parseRT.h"
+#include "vm/jit/inline/inline.h"
+#include "vm/jit/loop/loop.h"
+
 
 #define METHINFO(mm) \
         { \
@@ -61,7 +65,7 @@
                 fflush(stdout); \
         }
 #define DEBUGMETH(mm) \
-if (DEBUG == true) \
+if (DEBUG4 == true) \
         { \
                 printf("PARSE method name ="); \
                 utf_display(mm->class->name); \
@@ -79,7 +83,7 @@ bool DEBUG3 = false;
 bool DEBUG4 = false;  /*opcodes*/
 
 /*INLINING*/
-#define debug_writebranch if (DEBUG2==true) printf("op:: %s i: %d label_index[i]: %d label_index=%p\n",opcode_names[opcode], i, label_index[i],label_index);
+#define debug_writebranch if (DEBUG2==true) printf("op:: %s i: %d label_index[i]: %d label_index=0x%x\n",opcode_names[opcode], i, label_index[i], label_index);
 #define debug_writebranch1
 
 
@@ -361,59 +365,66 @@ static exceptiontable* fillextable(methodinfo *m,
                int *label_index, int *block_count, 
                t_inlining_globals *inline_env)
 {
-       int b_count, i, p;
+       int b_count, i, p, src, insertBlock;
        
        if (exceptiontablelength == 0) 
                return extable;
+
        
-//if (m->exceptiontablelength > 0) {
-//   DEBUGMETH(m);
-//   printf("m->exceptiontablelength=%i\n",m->exceptiontablelength);
-//   panic("exceptiontablelength > 0");
-//   }
+       /*if (m->exceptiontablelength > 0) {
+         DEBUGMETH(m);
+         printf("m->exceptiontablelength=%i\n",m->exceptiontablelength);
+         panic("exceptiontablelength > 0");
+         }*/
 
        b_count = *block_count;
 
-       for (i = 0; i < exceptiontablelength; i++) {
+       for (src = exceptiontablelength-1; src >=0; src--) {
                /* printf("Excepiont table index: %d\n",i); */
-               p = raw_extable[i].startpc;
+               p = raw_extable[src].startpc;
                if (label_index != NULL) p = label_index[p];
-               extable[i].startpc = p;
+               extable->startpc = p;
                bound_check(p);
                block_insert(p);
                
 /*** if (DEBUG==true){printf("---------------------block_inserted:b_count=%i m->basicblockindex[(p=%i)]=%i=%p\n",b_count,p,m->basicblockindex[(p)],m->basicblockindex[(p)]); 
   fflush(stdout); } ***/   
-               p = raw_extable[i].endpc;
-               if (p <= raw_extable[i].startpc)
+               p = raw_extable[src].endpc; /* see JVM Spec 4.7.3 */
+               if (p <= raw_extable[src].startpc)
                        panic("Invalid exception handler range");
-               if (p >=m->jcodelength) {
+
+               if (p >inline_env->method->jcodelength) {
                        panic("Invalid exception handler end is after code end");
                }
+               if (p<inline_env->method->jcodelength) insertBlock=1; else insertBlock=0;
+                /*if (label_index !=NULL) printf("%s:translating endpc:%ld to %ld, label_index:%p\n",m->name->text,p,label_index[p],label_index); else
+                       printf("%s:fillextab: endpc:%ld\n",m->name->text,p);*/
                if (label_index != NULL) p = label_index[p];
-               extable[i].endpc = p;
+               extable->endpc = p;
                bound_check1(p);
-               if (p < m->jcodelength) {
-                       block_insert(p); }
+               /*if (p < inline_env->method->jcodelength) {
+                       block_insert(p); }*/
+                if (insertBlock) block_insert(p);
 
-               p = raw_extable[i].handlerpc;
+               p = raw_extable[src].handlerpc;
                if (label_index != NULL) p = label_index[p];
-               extable[i].handlerpc = p;
+               extable->handlerpc = p;
                bound_check(p);
                block_insert(p);
 
-               extable[i].catchtype  = raw_extable[i].catchtype;
-               extable[i].next = NULL;
-               extable[i].down = &extable[i + 1];
+               extable->catchtype  = raw_extable[src].catchtype;
+               extable->next = NULL;
+               extable->down = &extable[1];
+               extable--;
        }
 
        *block_count = b_count;
-       return &extable[i];  /* return the next free xtable* */
+       return extable; /*&extable[i];*/  /* return the next free xtable* */
 }
 
 
 
-methodinfo *parse(methodinfo *m, t_inlining_globals *inline_env) 
+methodinfo *parse(methodinfo *m, codegendata *cd, t_inlining_globals *inline_env)
 {
        int  p;                     /* java instruction counter           */
        int  nextp;                 /* start of next java instruction     */
@@ -436,19 +447,18 @@ methodinfo *parse(methodinfo *m, t_inlining_globals *inline_env)
        exceptiontable* nextex;     /* points next free entry in extable  */
        u1 *instructionstart;       /* 1 for pcs which are valid instr. starts    */
 
-       u2 lineindex=0;
-       u2 currentline=0;
-       u2 linepcchange=0;
-       codegendata *cd=m->codegendata;
+       u2 lineindex = 0;
+       u2 currentline = 0;
+       u2 linepcchange = 0;
 
        u2 skipBasicBlockChange;
 
-if (DEBUG==true) {printf("PARSING: "); fflush(stdout);
+if (DEBUG4==true) {printf("\nPARSING: "); fflush(stdout);
 DEBUGMETH(m);
 }
 if (opt_rt) {
   if (m->methodUsed != USED) {
-    if (verbose) {
+    if (opt_verbose) {
       printf(" rta missed: "); fflush(stdout);
       METHINFO(m);
       }
@@ -482,11 +492,11 @@ if (opt_rt) {
         which has to be called before parse (or ???)
         will check if method being parsed was analysed here
        ****/ 
-       if ((opt_xta) && (verbose)) { 
+       if (opt_xta && opt_verbose) { 
                /**RT_jit_parse(m);**/
                printf("XTA requested, not available\n");
                }
-       if ((opt_vta) && (verbose))  
+       if (opt_vta && opt_verbose)  
                    printf("VTA requested, not yet implemented\n");
 
        /* allocate instruction array and block index table */
@@ -510,24 +520,27 @@ if (opt_rt) {
        memset(iptr, 0, sizeof(instruction) * (inline_env->cumjcodelength + 5));
        
        /* compute branch targets of exception table */
-//if (m->exceptiontable == NULL) {
-//  printf("m->exceptiontable=NULL\n");fflush(stdout);
-//  }
-//else {
-//  printf("m->exceptiontable != NULL\n");fflush(stdout);
-//  }
-//printf("m->exceptiontablelength=%i, inline_env->method->exceptiontablelength=%i,inline_env->cumextablelength=%i\n",
-//m->exceptiontablelength, inline_env->method->exceptiontablelength,inline_env->cumextablelength);
-
-//if (m->exceptiontablelength > 0)
-//     m->exceptiontable = DMNEW(exceptiontable, m->exceptiontablelength + 1); 
+       /*
+if (m->exceptiontable == NULL) {
+  printf("m->exceptiontable=NULL\n");fflush(stdout);
+  }
+else {
+  printf("m->exceptiontable != NULL\n");fflush(stdout);
+  }
+printf("m->exceptiontablelength=%i, inline_env->method->exceptiontablelength=%i,inline_env->cumextablelength=%i\n",
+m->exceptiontablelength, inline_env->method->exceptiontablelength,inline_env->cumextablelength);
+       */
+       /*
+if (m->exceptiontablelength > 0)
+       m->exceptiontable = DMNEW(exceptiontable, m->exceptiontablelength + 1); 
+       */
 
        nextex = fillextable(m, 
-         cd->exceptiontable, m->exceptiontable, m->exceptiontablelength, 
+         &(cd->exceptiontable[cd->exceptiontablelength-1]), m->exceptiontable, m->exceptiontablelength, 
           label_index, &b_count, inline_env);
        s_count = 1 + m->exceptiontablelength; /* initialize stack element counter   */
 
-#ifdef USE_THREADS
+#if defined(USE_THREADS)
        if (checksync && (m->flags & ACC_SYNCHRONIZED)) {
                m->isleafmethod = false;
                inline_env->method->isleafmethod = false;
@@ -571,7 +584,7 @@ if (opt_rt) {
                        bool *readonly = NULL;
                        int argBlockIdx=0;
 
-                       block_insert(gp);               //JJJJJJJJJJ
+                       block_insert(gp);               /* JJJJJJJJJJ */
                        blockend=false;
                        instructionstart[gp] = 1;
                        m->basicblockindex[gp] |= (ipc << 1);  /*FIXME: necessary ? */
@@ -615,8 +628,8 @@ if (opt_rt) {
                                        argBlockIdx--;
 
                                OP1(op, firstlocal + argBlockIdx);
-                               //OP1(op, firstlocal + tmpinlinf->method->paramcount - 1 - i);
-                       //printf("inline argument load operation for local: %ld\n",firstlocal + tmpinlinf->method->paramcount - 1 - i);
+                               /* OP1(op, firstlocal + tmpinlinf->method->paramcount - 1 - i); */
+                               /* printf("inline argument load operation for local: %ld\n",firstlocal + tmpinlinf->method->paramcount - 1 - i); */
                        }
                        skipBasicBlockChange=1;
 if (DEBUG==true) {
@@ -640,6 +653,8 @@ DEBUGMETH(inline_env->method);
                        }
 
 
+                        OP1(ICMD_INLINE_START,tmpinlinf->level);
+
                        if (inlinfo->inlinedmethods == NULL) {
                                gp = -1;
                        } else {
@@ -658,20 +673,24 @@ DEBUGMETH(inline_env->method);
                {
                        printf("Parse p=%i<%i<%i<   opcode=<%i> %s\n",
                           p, gp, inline_env->jcodelength, opcode, opcode_names[opcode]);
+                       if (label_index)
+                               printf("label_index[%d]=%d\n",p,label_index[p]);
                }
-         
-//printf("basicblockindex[gp=%i]=%i=%p ipc=%i=%p shifted ipc=%i=%p\n",
-//gp,m->basicblockindex[gp],m->basicblockindex[gp],ipc,ipc,(ipc<<1),(ipc<<1));
-//fflush(stdout);
+        /*
+printf("basicblockindex[gp=%i]=%i=%p ipc=%i=%p shifted ipc=%i=%p\n",
+gp,m->basicblockindex[gp],m->basicblockindex[gp],ipc,ipc,(ipc<<1),(ipc<<1));
+fflush(stdout);
+        */
                if (!skipBasicBlockChange) {
                        m->basicblockindex[gp] |= (ipc << 1); /*store intermed cnt*/
                } else skipBasicBlockChange=0;
-//printf("basicblockindex[gp=%i]=%i=%p \n",
-//gp,m->basicblockindex[gp],m->basicblockindex[gp]);
-//fflush(stdout);
+               /*
+printf("basicblockindex[gp=%i]=%i=%p \n",
+gp,m->basicblockindex[gp],m->basicblockindex[gp]);
+fflush(stdout);
+               */
 
                if (blockend) {
-//printf("B4 BEND\t"); fflush(stdout);
                        block_insert(gp);               /* start new block                */
                        blockend = false;
                        /*printf("blockend was set: new blockcount: %ld at:%ld\n",b_count,gp);*/
@@ -1011,7 +1030,7 @@ SHOWOPCODE
                                i = label_index[i];
                        }
                        bound_check(i);
-//printf("B6 JSR_W\t"); fflush(stdout);
+                       /*printf("B6 JSR_W\t"); fflush(stdout);*/
                        block_insert(i);
                        blockend = true;
                        OP1(opcode, i);
@@ -1048,12 +1067,13 @@ SHOWOPCODE
                                /*                                              break; */
                                /*                                      } */
                                if (nextp>inline_env->method->jcodelength-1) {
-                                  //OP1(ICMD_GOTO, inlinfo->stopgp);
-                                   //OP(ICMD_NOP);
-                                   //OP(ICMD_NOP);
-                                   blockend=true;
-                                   break;
-                                }//JJJJJJJ
+                                       /* OP1(ICMD_GOTO, inlinfo->stopgp);
+                                          OP(ICMD_NOP);
+                                          OP(ICMD_NOP);
+                                       */
+                                       blockend=true;
+                                       break;
+                               } /* JJJJJJJ */
                                blockend = true;
                                OP1(ICMD_GOTO, inlinfo->stopgp);
                                break;
@@ -1100,7 +1120,6 @@ SHOWOPCODE
                                tablep++;
                                nextp += 4;
                                bound_check(j);
-//printf("B7 LOOKUP1\t"); fflush(stdout);
                                block_insert(j);
 
                                /* number of pairs */
@@ -1136,7 +1155,6 @@ SHOWOPCODE
                                        tablep++;
                                        nextp += 4;
                                        bound_check(j);
-//printf("B8 LOOKUP2\t"); fflush(stdout);
                                        block_insert(j);
                                }
 
@@ -1172,7 +1190,6 @@ SHOWOPCODE
                                tablep++;
                                nextp += 4;
                                bound_check(j);
-//printf("B9 TABLESWITCH1\t"); fflush(stdout);
                                block_insert(j);
 
                                /* lower bound */
@@ -1206,7 +1223,6 @@ SHOWOPCODE
                                        tablep++;
                                        nextp += 4;
                                        bound_check(j);
-//printf("B10 TABLESWITCH2\t"); fflush(stdout);
                                        block_insert(j);
                                        /*printf("TABLESWITCH: block_insert(%ld)\n",j);*/
                                }
@@ -1308,12 +1324,10 @@ SHOWOPCODE
                                if (!mi)
                                        return NULL;
 
-                               /*RTAprint*/// if (((pOpcodes == 2) || (pOpcodes == 3)) && opt_rt)
-if (DEBUG4==true) 
-                                       /*RTAprint*/    {printf(" method name =");
-                                       /*RTAprint*/    utf_display(mi->class->name); printf(".");
-                                       /*RTAprint*/    utf_display(mi->name);printf("\tINVOKE STATIC\n");
-                                       /*RTAprint*/    fflush(stdout);}
+if (DEBUG4==true) { 
+       method_display_w_class(mi); 
+       printf("\tINVOKE STATIC\n");
+        fflush(stdout);}
 
                                if (!(mi->flags & ACC_STATIC)) {
                                        *exceptionptr =
@@ -1352,13 +1366,10 @@ if (DEBUG4==true)
                                if (!mi)
                                        return NULL;
 
-                               /*RTAprint*/ // if (((pOpcodes == 2) || (pOpcodes == 3)) && opt_rt)
-if (DEBUG4==true)
-                                       /*RTAprint*/    {printf(" method name =");
-                                       method_display(mi);
-                                       /*RTAprint*/    utf_display(mi->class->name); printf(".");
-                                       /*RTAprint*/    utf_display(mi->name);printf("\tINVOKE SPECIAL/VIRTUAL\n");
-                                       /*RTAprint*/    fflush(stdout);}
+if (DEBUG4==true) { 
+       method_display_w_class(mi); 
+       printf("\tINVOKE SPEC/VIRT\n");
+        fflush(stdout);}
 
                                if (mi->flags & ACC_STATIC) {
                                        *exceptionptr =
@@ -1401,6 +1412,10 @@ if (DEBUG4==true)
                                        return NULL;
                                }
 
+if (DEBUG4==true) { 
+       method_display_w_class(mi); 
+       printf("\tINVOKE INTERFACE\n");
+        fflush(stdout);}
                                descriptor2types(mi);
                                OP2A(opcode, mi->paramcount, mi, currentline);
                        }
@@ -1479,7 +1494,7 @@ if (DEBUG4==true)
                        break;
 
                case JAVA_MONITORENTER:
-#ifdef USE_THREADS
+#if defined(USE_THREADS)
                        if (checksync) {
                                BUILTIN1(BUILTIN_monitorenter, TYPE_VOID,currentline);
                        } else
@@ -1490,7 +1505,7 @@ if (DEBUG4==true)
                        break;
 
                case JAVA_MONITOREXIT:
-#ifdef USE_THREADS
+#if defined(USE_THREADS)
                        if (checksync) {
                                BUILTIN1(BUILTIN_monitorexit, TYPE_VOID,currentline);
                                OP(ICMD_CHECKEXCEPTION);
@@ -1652,18 +1667,20 @@ if (DEBUG4==true)
                
                /* INLINING */
                  
-//             if (inline_env->isinlinedmethod && p == inline_env->method->jcodelength - 1) { /* end of an inlined method */
+               /* if (inline_env->isinlinedmethod && p == inline_env->method->jcodelength - 1) { */ /* end of an inlined method */
                if (inline_env->isinlinedmethod && (nextp >= inline_env->method->jcodelength) ) { /* end of an inlined method */
                        /*                printf("setting gp from %d to %d\n",gp, inlinfo->stopgp); */
                        gp = inlinfo->stopgp; 
                        inlining_restore_compiler_variables();
-//label_index = inlinfo->label_index;
+                       OP(ICMD_INLINE_END);
+/*label_index = inlinfo->label_index;*/
+
 if (DEBUG==true) {
 printf("AFTER RESTORE : "); fflush(stdout);
 DEBUGMETH(inline_env->method);
 }
                        list_remove(inlinfo->inlinedmethods, list_first(inlinfo->inlinedmethods));
-                       if (inlinfo->inlinedmethods == NULL) { //JJJJ
+                       if (inlinfo->inlinedmethods == NULL) { /* JJJJ */
                                nextgp = -1;
                        } else {
                                tmpinlinf = list_first(inlinfo->inlinedmethods);
@@ -1678,7 +1695,7 @@ DEBUGMETH(inline_env->method);
 
 
        if (p != m->jcodelength) {
-               printf("p (%ld) != m->jcodelength (%ld)\n",p,m->jcodelength);
+               printf("p (%d) != m->jcodelength (%d)\n",p,m->jcodelength);
                panic("Command-sequence crosses code-boundary");
        }
        if (!blockend) {
@@ -1707,7 +1724,7 @@ DEBUGMETH(inline_env->method);
                bptr = m->basicblocks = DMNEW(basicblock, b_count + 1);    /* one more for end ipc */
 
                b_count = 0;
-               c_debug_nr = 0;
+               m->c_debug_nr = 0;
        
                /* additional block if target 0 is not first intermediate instruction */
 
@@ -1718,7 +1735,7 @@ DEBUGMETH(inline_env->method);
                        bptr->type = BBTYPE_STD;
                        bptr->branchrefs = NULL;
                        bptr->pre_count = 0;
-                       bptr->debug_nr = c_debug_nr++;
+                       bptr->debug_nr = m->c_debug_nr++;
                        bptr++;
                        b_count++;
                        (bptr - 1)->next = bptr;
@@ -1727,16 +1744,16 @@ DEBUGMETH(inline_env->method);
                /* allocate blocks */
 
                for (p = 0; p < inline_env->cumjcodelength; p++) { 
-//             for (p = 0; p < m->jcodelength; p++) { 
+               /* for (p = 0; p < m->jcodelength; p++) { */
                        if (m->basicblockindex[p] & 1) {
                                /* check if this block starts at the beginning of an instruction */
                                if (!instructionstart[p]) {
-                                       printf("Basic Block beginn: %ld\n",p);
+                                       printf("Basic Block beginn: %d\n",p);
                                        panic("Branch into middle of instruction");
                                }
                                /* allocate the block */
                                bptr->iinstr = m->instructions + (m->basicblockindex[p] >> 1);
-                               bptr->debug_nr = c_debug_nr++;
+                               bptr->debug_nr = m->c_debug_nr++;
                                if (b_count != 0)
                                        (bptr - 1)->icount = bptr->iinstr - (bptr - 1)->iinstr;
                                bptr->mpc = -1;
@@ -1765,7 +1782,7 @@ DEBUGMETH(inline_env->method);
                bptr->type = BBTYPE_STD;
                bptr->branchrefs = NULL;
                bptr->pre_count = 0;
-               bptr->debug_nr = c_debug_nr++;
+               bptr->debug_nr = m->c_debug_nr++;
                (bptr - 1)->next = bptr;
                bptr->next = NULL;
 
@@ -1778,7 +1795,7 @@ DEBUGMETH(inline_env->method);
                        cd->exceptiontable[i].start = m->basicblocks + m->basicblockindex[p];
 
                        p = cd->exceptiontable[i].endpc;
-                       cd->exceptiontable[i].end = (p == m->jcodelength) ? (m->basicblocks + m->basicblockcount + 1) : (m->basicblocks + m->basicblockindex[p]);
+                       cd->exceptiontable[i].end = (p == inline_env->method->jcodelength) ? (m->basicblocks + m->basicblockcount /*+ 1*/) : (m->basicblocks + m->basicblockindex[p]);
 
                        p = cd->exceptiontable[i].handlerpc;
                        cd->exceptiontable[i].handler = m->basicblocks + m->basicblockindex[p];