* src/vm/jit/parse.h (code_get_u1, code_get_s1, code_get_u2)
[cacao.git] / src / vm / jit / parse.h
index b6450e3aee11a4abc3dfdc2ccdf9d1a4c852da2e..4c22d260f28ff0431af3ba6db92cfefc5268d99b 100644 (file)
@@ -1,9 +1,9 @@
-/* jit/parse.h - parser header
+/* src/vm/jit/parse.h - parser header
 
-   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, 2006 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.
 
 
    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
-   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
-   02111-1307, USA.
+   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+   02110-1301, USA.
 
-   Contact: cacao@complang.tuwien.ac.at
+   Contact: cacao@cacaojvm.org
 
-   Author: Christian Thalinger
+   Author:  Christian Thalinger
 
-   $Id: parse.h 1414 2004-10-04 12:55:33Z carolyn $
+   Changes: Edwin Steiner
+
+   $Id: parse.h 5181 2006-07-26 13:27:54Z twisti $
 
 */
 
 #ifndef _PARSE_H
 #define _PARSE_H
 
-#include "global.h"
-#include "inline.h"
-
-
-/* intermediate code generating macros */
-
-#define PINC           iptr++;ipc++
-
-#define LOADCONST_I(v) \
-    iptr->opc = ICMD_ICONST; \
-    /*iptr->op1=0;*/ \
-    iptr->val.i = (v); \
-    iptr->line = currentline; \
-    iptr->method = inline_env->method; \
-    PINC
+#include "config.h"
+#include "vm/types.h"
 
-#define LOADCONST_L(v) iptr->opc=ICMD_LCONST;/*iptr->op1=0*/;iptr->val.l=(v);iptr->line=currentline;iptr->method=inline_env->method;PINC
-#define LOADCONST_F(v) iptr->opc=ICMD_FCONST;/*iptr->op1=0*/;iptr->val.f=(v);iptr->line=currentline;iptr->method=inline_env->method;PINC
-#define LOADCONST_D(v) iptr->opc=ICMD_DCONST;/*iptr->op1=0*/;iptr->val.d=(v);iptr->line=currentline;iptr->method=inline_env->method;PINC
-#define LOADCONST_A(v) iptr->opc=ICMD_ACONST;/*iptr->op1=0*/;iptr->val.a=(v);iptr->line=currentline;iptr->method=inline_env->method;PINC
+#include "vm/global.h"
+#include "vm/jit/codegen-common.h"
 
-/* ACONST instructions generated as arguments for builtin functions
- * have op1 set to non-zero. This is used for stack overflow checking
- * in stack.c. */
-#define LOADCONST_A_BUILTIN(v) \
-                       iptr->opc=ICMD_ACONST;iptr->op1=1;iptr->val.a=(v);iptr->line=currentline;iptr->method=inline_env->method;PINC
 
-#define OP(o)          iptr->opc=(o);/*iptr->op1=0*/;/*iptr->val.l=0*/;iptr->line=currentline;iptr->method=inline_env->method;PINC
-#define OP1(o,o1)      iptr->opc=(o);iptr->op1=(o1);/*iptr->val.l=(0)*/;iptr->line=currentline;iptr->method=inline_env->method;PINC
-#define OP2I(o,o1,v)   iptr->opc=(o);iptr->op1=(o1);iptr->val.i=(v);iptr->line=currentline;iptr->method=inline_env->method;PINC
+/* macros for verifier checks during parsing **********************************/
 
-#define OP2A(o,o1,v,l) \
-    iptr->opc = (o); \
-    iptr->op1 = (o1); \
-    iptr->val.a = (v); \
-    iptr->line = l; \
-    iptr->method = inline_env->method; \
-    PINC
+#if defined(ENABLE_VERIFIER)
 
-#define BUILTIN1(v,t,l) \
-    inline_env->method->isleafmethod = false; \
-    iptr->opc = ICMD_BUILTIN1; \
-    iptr->val.a = (v); \
-    iptr->op1 = (t); \
-    iptr->line = (l); \
-    iptr->method = inline_env->method; \
-    PINC
+/* We have to check local variables indices here because they are             */
+/* used in stack.c to index the locals array.                                 */
 
-#define BUILTIN2(v,t,l)  inline_env->method->isleafmethod=false;iptr->opc=ICMD_BUILTIN2;iptr->op1=t;\
-                       iptr->val.a=(v);iptr->line=l;iptr->method=inline_env->method;PINC
-#define BUILTIN3(v,t,l)  inline_env->method->isleafmethod=false;iptr->opc=ICMD_BUILTIN3;iptr->op1=t;\
-                       iptr->val.a=(v);iptr->line=l;iptr->method=inline_env->method;PINC
+#define INDEX_ONEWORD(num) \
+    do { \
+        if (((num) < 0) || ((num) >= m->maxlocals)) \
+            goto throw_illegal_local_variable_number; \
+    } while (0)
 
+#define INDEX_TWOWORD(num) \
+    do { \
+        if (((num) < 0) || (((num) + 1) >= m->maxlocals)) \
+            goto throw_illegal_local_variable_number; \
+    } while (0)
 
-/* We have to check local variables indices here because they are
- * used in stack.c to index the locals array. */
+/* CHECK_BYTECODE_INDEX(i) checks whether i is a valid bytecode index.        */
+/* The end of the bytecode (i == m->jcodelength) is considered valid.         */
 
-#define INDEX_ONEWORD(num) \
+#define CHECK_BYTECODE_INDEX(i) \
     do { \
-        if ((num) < 0 || (num) >= inline_env->cumlocals) { \
-            *exceptionptr = \
-                new_verifyerror(inline_env->method, "Illegal local variable number"); \
-            return NULL; \
-        } \
+        if (((i) < 0) || ((i) >= m->jcodelength)) \
+                       goto throw_invalid_bytecode_index; \
     } while (0)
 
-#define INDEX_TWOWORD(num) \
+/* CHECK_BYTECODE_INDEX_EXCLUSIVE is used for the exclusive ends               */
+/* of exception handler ranges.                                                */
+#define CHECK_BYTECODE_INDEX_EXCLUSIVE(i) \
     do { \
-        if ((num) < 0 || ((num) + 1) >= inline_env->cumlocals) { \
-            *exceptionptr = \
-                new_verifyerror(inline_env->method, "Illegal local variable number"); \
-            return NULL; \
-        } \
+        if ((i) < 0 || (i) > m->jcodelength) \
+                       goto throw_invalid_bytecode_index; \
     } while (0)
 
-#define OP1LOAD(o,o1)                                                  \
-       do {if (o == ICMD_LLOAD || o == ICMD_DLOAD)     \
-                       INDEX_TWOWORD(o1);                                      \
-               else                                                                    \
-                       INDEX_ONEWORD(o1);                                      \
-               OP1(o,o1);} while(0)
+#else /* !define(ENABLE_VERIFIER) */
+
+#define INDEX_ONEWORD(num)
+#define INDEX_TWOWORD(num)
+#define CHECK_BYTECODE_INDEX(i)
+#define CHECK_BYTECODE_INDEX_EXCLUSIVE(i)
+
+#endif /* define(ENABLE_VERIFIER) */
 
-#define OP1STORE(o,o1)                                                         \
-       do {if (o == ICMD_LSTORE || o == ICMD_DSTORE)   \
-                       INDEX_TWOWORD(o1);                                              \
-               else                                                                            \
-                       INDEX_ONEWORD(o1);                                              \
-               OP1(o,o1);} while(0)
 
-/* block generating and checking macros */
+/* basic block generating macro ***********************************************/
+
+#define new_block_insert(i) \
+    do { \
+        if (!(jd->new_basicblockindex[(i)] & 1)) { \
+            b_count++; \
+            jd->new_basicblockindex[(i)] |= 1; \
+        } \
+    } while (0)
 
 #define block_insert(i) \
     do { \
         if (!(m->basicblockindex[(i)] & 1)) { \
             b_count++; \
             m->basicblockindex[(i)] |= 1; \
- if (DEBUG==true){printf("---------------------block_inserted:b_count=%i m->basicblockindex[(i=%i)]=%i=%p\n",b_count,i,m->basicblockindex[(i)],m->basicblockindex[(i)]); \
-  fflush(stdout); }   \
         } \
     } while (0)
 
 
-/* FIXME really use cumjcodelength for the bound_checkers ? */
+/* intermediate code generating macros ****************************************/
 
-#define bound_check(i) \
-    do { \
-        if (i < 0 || i >= inline_env->cumjcodelength) { \
- printf("bound_check i=%i >= %i=cum\n",i,inline_env->cumjcodelength); \
- fflush(stdout); \
-       /*  if (i < 0 || i >= m->jcodelength) { */ \
-            *exceptionptr = \
-                new_verifyerror(inline_env->method, "Illegal target of jump or branch"); \
-            return NULL; \
-        } \
+/* These macros ALWAYS set the following fields of *iptr to valid values:     */
+/*     iptr->opc                                                              */
+/*     iptr->flags                                                            */
+/*     iptr->line                                                             */
+
+/* These macros do NOT touch the following fields of *iptr, unless a value is */
+/* given for them:                                                            */
+/*     iptr->s1                                                               */
+/*     iptr->sx                                                               */
+/*     iptr->dst                                                              */
+
+/* The _PREPARE macros omit the PINC, so you can set additional fields        */
+/* afterwards.                                                                */
+/* CAUTION: Some of the _PREPARE macros don't set iptr->flags!                */
+
+#define PINC                                                           \
+    iptr++; ipc++
+
+/* CAUTION: You must set iptr->flags yourself when using this!                */
+#define NEW_OP_PREPARE(o)                                              \
+    iptr->opc                = (o);                                    \
+    iptr->line               = currentline;
+
+#define NEW_OP_PREPARE_ZEROFLAGS(o)                                    \
+    iptr->opc                = (o);                                    \
+    iptr->line               = currentline;                            \
+    iptr->flags.bits         = 0;
+
+#define NEW_OP(o)                                                      \
+       NEW_OP_PREPARE_ZEROFLAGS(o);                                       \
+    PINC
+
+#define NEW_OP_LOADCONST_I(v)                                          \
+       NEW_OP_PREPARE_ZEROFLAGS(ICMD_ICONST);                             \
+    iptr->sx.val.i           = (v);                                    \
+    PINC
+
+#define NEW_OP_LOADCONST_L(v)                                          \
+       NEW_OP_PREPARE_ZEROFLAGS(ICMD_LCONST);                             \
+    iptr->sx.val.l           = (v);                                    \
+    PINC
+
+#define NEW_OP_LOADCONST_F(v)                                          \
+       NEW_OP_PREPARE_ZEROFLAGS(ICMD_FCONST);                             \
+    iptr->sx.val.f           = (v);                                    \
+    PINC
+
+#define NEW_OP_LOADCONST_D(v)                                          \
+       NEW_OP_PREPARE_ZEROFLAGS(ICMD_DCONST);                             \
+    iptr->sx.val.d           = (v);                                    \
+    PINC
+
+#define NEW_OP_LOADCONST_NULL()                                        \
+       NEW_OP_PREPARE_ZEROFLAGS(ICMD_ACONST);                             \
+    iptr->sx.val.anyptr      = NULL;                                   \
+    PINC
+
+#define NEW_OP_LOADCONST_STRING(v)                                     \
+       NEW_OP_PREPARE_ZEROFLAGS(ICMD_ACONST);                             \
+    iptr->sx.val.stringconst = (v);                                    \
+    PINC
+
+#define NEW_OP_LOADCONST_CLASSINFO_OR_CLASSREF(c, cr, extraflags)      \
+       NEW_OP_PREPARE(ICMD_ACONST);                                       \
+    if (c) {                                                           \
+        iptr->sx.val.c.cls   = (c);                                    \
+        iptr->flags.bits     = INS_FLAG_CLASS | (extraflags);          \
+    }                                                                  \
+    else {                                                             \
+        iptr->sx.val.c.ref   = (cr);                                   \
+        iptr->flags.bits     = INS_FLAG_CLASS | INS_FLAG_UNRESOLVED    \
+                             | (extraflags);                           \
+    }                                                                  \
+    PINC
+
+#define NEW_OP_S3_CLASSINFO_OR_CLASSREF(o, c, cr, extraflags)          \
+       NEW_OP_PREPARE(o);                                                 \
+    if (c) {                                                           \
+        iptr->sx.s23.s3.c.cls= (c);                                    \
+        iptr->flags.bits     = (extraflags);                           \
+    }                                                                  \
+    else {                                                             \
+        iptr->sx.s23.s3.c.ref= (cr);                                   \
+        iptr->flags.bits     = INS_FLAG_UNRESOLVED | (extraflags);     \
+    }                                                                  \
+    PINC
+
+#define NEW_OP_INSINDEX(o, iindex)                                     \
+       NEW_OP_PREPARE_ZEROFLAGS(o);                                       \
+    iptr->dst.insindex       = (iindex);                               \
+    PINC
+
+#define NEW_OP_LOCALINDEX(o,index)                                     \
+       NEW_OP_PREPARE_ZEROFLAGS(o);                                       \
+    iptr->s1.localindex      = (index);                                \
+    PINC
+
+#define NEW_OP_LOCALINDEX_I(o,index,v)                                 \
+       NEW_OP_PREPARE_ZEROFLAGS(o);                                       \
+    iptr->s1.localindex      = (index);                                \
+    iptr->sx.val.i           = (v);                                    \
+    PINC
+
+#define NEW_OP_LOAD_ONEWORD(o,index)                                   \
+    do {                                                               \
+        INDEX_ONEWORD(index);                                          \
+        NEW_OP_LOCALINDEX(o,index);                                    \
     } while (0)
 
-/* bound_check1 is used for the inclusive ends of exception handler ranges */
-#define bound_check1(i) \
-    do { \
-        if (i < 0 || i > inline_env->cumjcodelength) { \
-/*        if (i < 0 || i > m->jcodelength) { */ \
-            *exceptionptr = \
-                new_verifyerror(inline_env->method, "Illegal target of jump or branch"); \
-            return NULL; \
-        } \
+#define NEW_OP_LOAD_TWOWORD(o,index)                                   \
+    do {                                                               \
+        INDEX_TWOWORD(index);                                          \
+        NEW_OP_LOCALINDEX(o,index);                                    \
+    } while (0)
+
+#define NEW_OP_STORE_ONEWORD(o,index)                                  \
+    do {                                                               \
+        INDEX_ONEWORD(index);                                          \
+        NEW_OP_PREPARE_ZEROFLAGS(o);                                   \
+        iptr->dst.localindex = (index);                                \
+        PINC;                                                          \
     } while (0)
 
+#define NEW_OP_STORE_TWOWORD(o,index)                                  \
+    do {                                                               \
+        INDEX_TWOWORD(index);                                          \
+        NEW_OP_PREPARE_ZEROFLAGS(o);                                   \
+        iptr->dst.localindex = (index);                                \
+        PINC;                                                          \
+    } while (0)
+
+#define NEW_OP_BUILTIN_CHECK_EXCEPTION(bte)                            \
+    jd->isleafmethod         = false;                                  \
+       NEW_OP_PREPARE_ZEROFLAGS(ICMD_BUILTIN);                            \
+    iptr->sx.s23.s3.bte      = (bte);                                  \
+    PINC
+
+#define NEW_OP_BUILTIN_NO_EXCEPTION(bte)                               \
+    jd->isleafmethod         = false;                                  \
+       NEW_OP_PREPARE(ICMD_BUILTIN);                                      \
+    iptr->sx.s23.s3.bte      = (bte);                                  \
+    iptr->flags.bits         = INS_FLAG_NOCHECK;                       \
+    PINC
+
+#define NEW_OP_BUILTIN_ARITHMETIC(opcode, bte)                         \
+    jd->isleafmethod         = false;                                  \
+       NEW_OP_PREPARE_ZEROFLAGS(opcode);                                  \
+    iptr->sx.s23.s3.bte      = (bte);                                  \
+    PINC
+
+/* CAUTION: You must set iptr->flags yourself when using this!                */
+#define NEW_OP_FMIREF_PREPARE(o, fmiref)                               \
+       NEW_OP_PREPARE(o);                                                 \
+    iptr->sx.s23.s3.fmiref   = (fmiref);
+
+/* old macros for intermediate code generation ********************************/
+
+#define LOADCONST_I(v) \
+    iptr->opc    = ICMD_ICONST; \
+    iptr->val.i  = (v); \
+    iptr->line   = currentline; \
+    PINC
+
+#define LOADCONST_L(v) \
+    iptr->opc    = ICMD_LCONST; \
+    iptr->val.l  = (v); \
+    iptr->line   = currentline; \
+    PINC
+
+#define LOADCONST_F(v) \
+    iptr->opc    = ICMD_FCONST; \
+    iptr->val.f  = (v); \
+    iptr->line   = currentline; \
+    PINC
+
+#define LOADCONST_D(v) \
+    iptr->opc    = ICMD_DCONST; \
+    iptr->val.d  = (v); \
+    iptr->line   = currentline; \
+    PINC
+
+#define LOADCONST_A(v) \
+    iptr->opc    = ICMD_ACONST; \
+    iptr->val.a  = (v); \
+    iptr->line   = currentline; \
+    PINC
+
+/* ACONST instructions generated as arguments for builtin functions
+ * have op1 set to non-zero. This is used for stack overflow checking
+ * in stack.c. */
+/* XXX in the new instruction format use a flag for this */
+/* XXX target used temporarily as flag */
+#define LOADCONST_A_BUILTIN(c,cr) \
+    iptr->opc    = ICMD_ACONST; \
+    iptr->op1    = 1; \
+    iptr->line   = currentline; \
+       if (c) { \
+               iptr->val.a = c; iptr->target = (void*) 0x02; \
+       } \
+       else { \
+               iptr->val.a = cr; iptr->target = (void*) 0x03; \
+       } \
+    PINC
+
+#define OP(o) \
+    iptr->opc    = (o); \
+    iptr->line   = currentline; \
+    PINC
+
+#define OP1(o,o1) \
+    iptr->opc    = (o); \
+    iptr->op1    = (o1); \
+    iptr->line   = currentline; \
+    PINC
+
+#define OP2I(o,o1,v) \
+    iptr->opc    = (o); \
+    iptr->op1    = (o1); \
+    iptr->val.i  = (v); \
+    iptr->line   = currentline; \
+    PINC
+
+#define OP2A_NOINC(o,o1,v,l) \
+    iptr->opc    = (o); \
+    iptr->op1    = (o1); \
+    iptr->val.a  = (v); \
+    iptr->line   = (l);
 
-/* macros for byte code fetching ***********************************************
+#define OP2A(o,o1,v,l) \
+    OP2A_NOINC(o,o1,v,l); \
+    PINC
 
-       fetch a byte code of given size from position p in code array jcode
+#define OP2AT(o,o1,v,t,l) \
+    OP2A_NOINC(o,o1,v,l); \
+    iptr->target = (t); \
+    PINC
 
-*******************************************************************************/
+#define BUILTIN(v,o1,t,l) \
+    jd->isleafmethod = false; \
+    iptr->opc        = ICMD_BUILTIN; \
+    iptr->op1        = (o1); \
+    iptr->val.a      = (v); \
+    iptr->target     = (t); \
+    iptr->line       = (l); \
+    PINC
+
+#define OP1LOAD_ONEWORD(o,o1) \
+    do { \
+               INDEX_ONEWORD(o1); \
+        OP1(o,o1); \
+    } while (0)
 
-#define code_get_u1(p,m)  m->jcode[p]
-#define code_get_s1(p,m)  ((s1)m->jcode[p])
-#define code_get_u2(p,m)  ((((u2)m->jcode[p]) << 8) + m->jcode[p + 1])
-#define code_get_s2(p,m)  ((s2)((((u2)m->jcode[p]) << 8) + m->jcode[p + 1]))
-#define code_get_u4(p,m)  ((((u4)m->jcode[p]) << 24) + (((u4)m->jcode[p + 1]) << 16) \
-                        +(((u4)m->jcode[p + 2]) << 8) + m->jcode[p + 3])
-#define code_get_s4(p,m)  ((s4)((((u4)m->jcode[p]) << 24) + (((u4)m->jcode[p + 1]) << 16) \
-                             +(((u4)m->jcode[p + 2]) << 8) + m->jcode[p + 3]))
+#define OP1LOAD_TWOWORD(o,o1) \
+    do { \
+               INDEX_TWOWORD(o1); \
+        OP1(o,o1); \
+    } while (0)
 
+#define OP1STORE_ONEWORD(o,o1) \
+    do { \
+               INDEX_ONEWORD(o1); \
+        OP1(o,o1); \
+    } while (0)
 
-extern classinfo  *rt_class;
-extern methodinfo *rt_method;
-extern utf *rt_descriptor;
-extern int rt_jcodelength;
-extern u1  *rt_jcode;
+#define OP1STORE_TWOWORD(o,o1) \
+    do { \
+               INDEX_TWOWORD(o1); \
+        OP1(o,o1); \
+    } while (0)
 
 
-/* function prototypes */
+/* function prototypes ********************************************************/
 
-void compiler_addinitclass(classinfo *c);
-classSetNode * descriptor2typesL(methodinfo *m);
-void descriptor2types(methodinfo *m);
-methodinfo *parse(methodinfo *m,t_inlining_globals *inline_env);
+bool parse(jitdata *jd);
 
 #endif /* _PARSE_H */
 
@@ -206,6 +399,6 @@ methodinfo *parse(methodinfo *m,t_inlining_globals *inline_env);
  * c-basic-offset: 4
  * tab-width: 4
  * End:
+ * vim:noexpandtab:sw=4:ts=4:
  */
 
-