* src/vm/jit/jit.h (stackelement): Added creator field.
[cacao.git] / src / vm / jit / stack.h
index 53e0c88f2b0f841e94cf4f7bf385ad19ab40d65f..bcaa7905f633157b99e09d608b27ce1ea59eb62e 100644 (file)
@@ -1,9 +1,9 @@
-/* jit/stack.h - stack analysis header
+/* vm/jit/stack.h - stack analysis 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
 
    Authors: Christian Thalinger
 
-   $Id: stack.h 1203 2004-06-22 23:14:55Z twisti $
+   Changes: Christian Ullrich
+
+   $Id: stack.h 5436 2006-09-08 19:48:27Z edwin $
 
 */
 
 #ifndef _STACK_H
 #define _STACK_H
 
+#include "config.h"
+
+#include "vm/types.h"
+
+#include "vm/exceptions.h"
+#include "vm/global.h"
+#include "vm/jit/jit.h"
+#include "vm/jit/reg.h"
+
+
+/* macros used internally by analyse_stack ************************************/
+
+/* convenient abbreviations */
+#define CURKIND    curstack->varkind
+#define CURTYPE    curstack->type
+
+
+/*--------------------------------------------------*/
+/* STACK DEPTH CHECKING                             */
+/*--------------------------------------------------*/
+
+#if defined(ENABLE_VERIFIER)
+#define CHECK_STACK_DEPTH(depthA,depthB)                             \
+    do {                                                             \
+        if ((depthA) != (depthB))                                    \
+            goto throw_stack_depth_error;                            \
+    } while (0)
+#else /* !ENABLE_VERIFIER */
+#define CHECK_STACK_DEPTH(depthA,depthB)
+#endif /* ENABLE_VERIFIER */
 
-#include "global.h"
 
+/*--------------------------------------------------*/
+/* BASIC TYPE CHECKING                              */
+/*--------------------------------------------------*/
 
-/* function prototypes */
+/* XXX would be nice if we did not have to pass the expected type */
 
-methodinfo *analyse_stack(methodinfo *m);
+#if defined(ENABLE_VERIFIER)
+#define CHECK_BASIC_TYPE(expected,actual)                            \
+    do {                                                             \
+        if ((actual) != (expected)) {                                \
+            expectedtype = (expected);                               \
+            goto throw_stack_type_error;                             \
+        }                                                            \
+    } while (0)
+#else /* !ENABLE_VERIFIER */
+#define CHECK_BASIC_TYPE(expected,actual)
+#endif /* ENABLE_VERIFIER */
 
-void icmd_print_stack(methodinfo *m, stackptr s);
-char *icmd_builtin_name(functionptr bptr);
-void show_icmd_method(methodinfo *m);
-void show_icmd_block(methodinfo *m, basicblock *bptr);
-void show_icmd(instruction *iptr, bool deadcode);
+/*--------------------------------------------------*/
+/* STACK UNDERFLOW/OVERFLOW CHECKS                  */
+/*--------------------------------------------------*/
+
+/* underflow checks */
+
+#if defined(ENABLE_VERIFIER)
+#define REQUIRE(num)                                                 \
+    do {                                                             \
+        if (stackdepth < (num))                                      \
+            goto throw_stack_underflow;                              \
+    } while (0)
+#else /* !ENABLE_VERIFIER */
+#define REQUIRE(num)
+#endif /* ENABLE_VERIFIER */
+
+#define REQUIRE_1     REQUIRE(1)
+#define REQUIRE_2     REQUIRE(2)
+#define REQUIRE_3     REQUIRE(3)
+#define REQUIRE_4     REQUIRE(4)
+
+
+/* overflow check */
+/* We allow ACONST instructions inserted as arguments to builtin
+ * functions to exceed the maximum stack depth.  Maybe we should check
+ * against maximum stack depth only at block boundaries?
+ */
+
+/* XXX we should find a way to remove the opc/op1 check */
+#if defined(ENABLE_VERIFIER)
+#define CHECKOVERFLOW                                                \
+    do {                                                             \
+        if (stackdepth > m->maxstack)                                \
+            if ((iptr->opc != ICMD_ACONST) || INSTRUCTION_MUST_CHECK(iptr))\
+                goto throw_stack_overflow;                           \
+    } while(0)
+#else /* !ENABLE_VERIFIER */
+#define CHECKOVERFLOW
+#endif /* ENABLE_VERIFIER */
+
+/*--------------------------------------------------*/
+/* ALLOCATING STACK SLOTS                           */
+/*--------------------------------------------------*/
+
+#define NEWSTACK(s,v,n)                                              \
+    do {                                                             \
+        sd.new->prev = curstack;                                     \
+        sd.new->type = (s);                                          \
+        sd.new->flags = 0;                                           \
+        sd.new->varkind = (v);                                       \
+        sd.new->varnum = (n);                                        \
+        curstack = sd.new;                                           \
+        sd.var[(n)].type = (s);                                      \
+        sd.var[(n)].flags = 0;                                       \
+        sd.new++;                                                    \
+    } while (0)
+
+/* Initialize regoff, so -sia can show regnames even before reg.inc */
+/* regs[rd->intregargnum] has to be set for this                    */
+/* new->regoff = (IS_FLT_DBL_TYPE(s))?-1:rd->intreg_argnum; }       */
+
+#define NEWSTACKn(s,n)  NEWSTACK(s,UNDEFVAR,n)
+#define NEWSTACK0(s)    NEWSTACK(s,UNDEFVAR,0)
+
+
+/*--------------------------------------------------*/
+/* MACROS FOR HANDLING BASIC BLOCKS                 */
+/*--------------------------------------------------*/
+
+/* COPYCURSTACK makes a copy of the current operand stack (curstack)
+ * and returns it in the variable copy.
+ *
+ * This macro is used to propagate the operand stack from one basic
+ * block to another. The destination block receives the copy as its
+ * input stack.
+ */
+#define COPYCURSTACK(sd, copy) {                                     \
+    stackptr s;                                                      \
+    if (curstack) {                                                  \
+        s = curstack;                                                \
+        (sd).new += stackdepth;                                      \
+        copy = (sd).new;                                             \
+        while (s) {                                                  \
+            copy--;                                                  \
+            copy->prev = copy-1;                                     \
+            copy->creator = NULL;                                    \
+            copy->type = s->type;                                    \
+            copy->flags = 0;                                         \
+            copy->varkind = STACKVAR;                                \
+            copy->varnum = s->varnum;                                \
+            SET_OUTVAR((sd), s);                                     \
+            s = s->prev;                                             \
+        }                                                            \
+        copy->prev = NULL;                                           \
+        copy = (sd).new-1;                                           \
+    }                                                                \
+    else                                                             \
+        copy = NULL;                                                 \
+}
+
+
+/* external macros ************************************************************/
+
+#define BLOCK_OF(index)                                              \
+    (jd->new_basicblocks + jd->new_basicblockindex[index])
+
+
+/* function prototypes ********************************************************/
+
+bool stack_init(void);
+
+bool new_stack_analyse(jitdata *jd);
 
 #endif /* _STACK_H */
 
@@ -62,4 +213,5 @@ void show_icmd(instruction *iptr, bool deadcode);
  * c-basic-offset: 4
  * tab-width: 4
  * End:
+ * vim:noexpandtab:sw=4:ts=4:
  */