This unifies bytecode offset mapping interface of statements
and expressions which leads to cleaner code.

Signed-off-by: Tomek Grabiec <tgrab...@gmail.com>
---
 Makefile                      |    3 +-
 arch/x86/insn-selector_32.brg |    2 +-
 include/jit/statement.h       |    1 +
 include/jit/tree-node.h       |    5 ++-
 jit/bc-offset-mapping.c       |   34 ++++-----------------
 jit/bytecode-to-ir.c          |   29 +-----------------
 jit/expression.c              |    2 +-
 jit/statement.c               |   65 +++++++++++++++++++---------------------
 test/arch-x86/Makefile        |    1 +
 test/jit/Makefile             |    1 +
 10 files changed, 51 insertions(+), 92 deletions(-)

diff --git a/Makefile b/Makefile
index 18a9365..8d1d56c 100644
--- a/Makefile
+++ b/Makefile
@@ -82,7 +82,8 @@ JIT_OBJS = \
        jit/bc-offset-mapping.o \
        jit/cu-mapping.o        \
        jit/method.o            \
-       jit/nop-bc.o
+       jit/nop-bc.o            \
+       jit/tree-node.o
 
 VM_OBJS = \
        vm/bitset.o             \
diff --git a/arch/x86/insn-selector_32.brg b/arch/x86/insn-selector_32.brg
index 9a698fa..7b9a933 100644
--- a/arch/x86/insn-selector_32.brg
+++ b/arch/x86/insn-selector_32.brg
@@ -1273,7 +1273,7 @@ stmt:     STMT_CHECKCAST(reg)
 static void select_insn(struct basic_block *bb, struct tree_node *tree,
                        struct insn *instruction)
 {
-       instruction->bytecode_offset = tree_bytecode_offset(tree);
+       instruction->bytecode_offset = tree->bytecode_offset;
        bb_add_insn(bb, instruction);
 }
 
diff --git a/include/jit/statement.h b/include/jit/statement.h
index b04737a..93350da 100644
--- a/include/jit/statement.h
+++ b/include/jit/statement.h
@@ -72,6 +72,7 @@ static inline enum statement_type stmt_type(struct statement 
*stmt)
 
 struct statement *alloc_statement(enum statement_type);
 void free_statement(struct statement *);
+int stmt_nr_kids(struct statement *);
 
 #define for_each_stmt(stmt, stmt_list) list_for_each_entry(stmt, stmt_list, 
stmt_list_node)
        
diff --git a/include/jit/tree-node.h b/include/jit/tree-node.h
index 314a107..4c944d4 100644
--- a/include/jit/tree-node.h
+++ b/include/jit/tree-node.h
@@ -10,10 +10,11 @@
 #define OP_SHIFT       8UL
 #define STMT_TYPE_MASK 0xFF000000UL
 #define STMT_TYPE_SHIFT        24UL
-               
+
 struct tree_node {
        struct tree_node *kids[2];
        unsigned long op;
+       unsigned long bytecode_offset;
 };
 
 static inline int node_is_stmt(struct tree_node *node)
@@ -36,4 +37,6 @@ static inline int tree_op(struct tree_node *node)
        return (node->op & EXPR_TYPE_MASK) >> EXPR_TYPE_SHIFT;
 }
 
+int node_nr_kids(struct tree_node *node);
+
 #endif
diff --git a/jit/bc-offset-mapping.c b/jit/bc-offset-mapping.c
index 14e2f46..be3756d 100644
--- a/jit/bc-offset-mapping.c
+++ b/jit/bc-offset-mapping.c
@@ -33,34 +33,22 @@
 #include <vm/buffer.h>
 
 /**
- * tree_patch_bc_offset - sets bytecode_offset field of a tree node container
+ * tree_patch_bc_offset - sets bytecode_offset field of a tree node
  *                        unless it is already set.
  * @node: a tree node
  * @offset: bytecode offset to set.
  */
 void tree_patch_bc_offset(struct tree_node *node, unsigned long bc_offset)
 {
-       if (node_is_stmt(node)) {
-               struct statement *stmt = to_stmt(node);
+       int i;
 
-               if (stmt->bytecode_offset != BC_OFFSET_UNKNOWN)
-                       return;
+       if (node->bytecode_offset != BC_OFFSET_UNKNOWN)
+               return;
 
-               stmt->bytecode_offset = bc_offset;
-       } else {
-               struct expression *expr = to_expr(node);
-               int i;
+       node->bytecode_offset = bc_offset;
 
-               if (expr->bytecode_offset != BC_OFFSET_UNKNOWN)
-                       return;
-
-               expr->bytecode_offset = bc_offset;
-
-               /* We should propagate bytecode offset to expressions
-                  contained by this one. */
-               for (i = 0; i < expr_nr_kids(expr); i++)
-                       tree_patch_bc_offset(expr->node.kids[i], bc_offset);
-       }
+       for (i = 0; i < node_nr_kids(node); i++)
+               tree_patch_bc_offset(node->kids[i], bc_offset);
 }
 
 /**
@@ -114,14 +102,6 @@ void print_bytecode_offset(unsigned long bytecode_offset, 
struct string *str)
        }
 }
 
-unsigned long tree_bytecode_offset(struct tree_node *node)
-{
-       if (node_is_stmt(node))
-               return to_stmt(node)->bytecode_offset;
-
-       return to_expr(node)->bytecode_offset;
-}
-
 bool all_insn_have_bytecode_offset(struct compilation_unit *cu)
 {
        struct basic_block *bb;
diff --git a/jit/bytecode-to-ir.c b/jit/bytecode-to-ir.c
index 7ddb7fa..76fd5a4 100644
--- a/jit/bytecode-to-ir.c
+++ b/jit/bytecode-to-ir.c
@@ -89,34 +89,9 @@ void convert_expression(struct parse_context *ctx, struct 
expression *expr)
 }
 
 static void
-do_convert_statement(struct basic_block *bb, struct statement *stmt, unsigned 
short bc_offset)
+do_convert_statement(struct basic_block *bb, struct statement *stmt, unsigned 
long bc_offset)
 {
-       /*
-        * Some expressions do not go through convert_expression()
-        * so we need to set their bytecode_offset here if it is not set.
-        */
-       switch (stmt_type(stmt)) {
-       case STMT_STORE:
-               tree_patch_bc_offset(stmt->store_dest, bc_offset);
-               tree_patch_bc_offset(stmt->store_src, bc_offset);
-               break;
-       case STMT_IF:
-               tree_patch_bc_offset(stmt->if_conditional, bc_offset);
-               break;
-       case STMT_RETURN:
-               tree_patch_bc_offset(stmt->return_value, bc_offset);
-               break;
-       case STMT_EXPRESSION:
-       case STMT_ARRAY_CHECK:
-               tree_patch_bc_offset(stmt->expression, bc_offset);
-               break;
-       case STMT_ATHROW:
-               tree_patch_bc_offset(stmt->exception_ref, bc_offset);
-               break;
-       default: ;
-       }
-
-       stmt->bytecode_offset = bc_offset;
+       tree_patch_bc_offset(&stmt->node, bc_offset);
        bb_add_stmt(bb, stmt);
 }
 
diff --git a/jit/expression.c b/jit/expression.c
index 9f2e252..4905924 100644
--- a/jit/expression.c
+++ b/jit/expression.c
@@ -57,7 +57,7 @@ struct expression *alloc_expression(enum expression_type type,
                expr->node.op = type << EXPR_TYPE_SHIFT;
                expr->vm_type = vm_type;
                expr->refcount = 1;
-               expr->bytecode_offset = BC_OFFSET_UNKNOWN;
+               expr->node.bytecode_offset = BC_OFFSET_UNKNOWN;
        }
        return expr;
 }
diff --git a/jit/statement.c b/jit/statement.c
index 75b92be..3c9bd1e 100644
--- a/jit/statement.c
+++ b/jit/statement.c
@@ -13,6 +13,30 @@
 #include <stdlib.h>
 #include <string.h>
 
+/* How many child expression nodes are used by each type of statement. */
+int stmt_nr_kids(struct statement *stmt)
+{
+       switch (stmt_type(stmt)) {
+       case STMT_STORE:
+       case STMT_ARRAY_STORE_CHECK:
+               return 2;
+       case STMT_IF:
+       case STMT_RETURN:
+       case STMT_EXPRESSION:
+       case STMT_ARRAY_CHECK:
+       case STMT_ATHROW:
+       case STMT_MONITOR_ENTER:
+       case STMT_MONITOR_EXIT:
+       case STMT_CHECKCAST:
+               return 1;
+       case STMT_GOTO:
+       case STMT_VOID_RETURN:
+               return 0;
+       default:
+               assert(!"Invalid statement type");
+       }
+}
+
 struct statement *alloc_statement(enum statement_type type)
 {
        struct statement *stmt = malloc(sizeof *stmt);
@@ -20,7 +44,7 @@ struct statement *alloc_statement(enum statement_type type)
                memset(stmt, 0, sizeof *stmt);
                INIT_LIST_HEAD(&stmt->stmt_list_node);
                stmt->node.op = type << STMT_TYPE_SHIFT;
-               stmt->bytecode_offset = BC_OFFSET_UNKNOWN;
+               stmt->node.bytecode_offset = BC_OFFSET_UNKNOWN;
        }
 
        return stmt;
@@ -28,41 +52,14 @@ struct statement *alloc_statement(enum statement_type type)
 
 void free_statement(struct statement *stmt)
 {
+       int i;
+
        if (!stmt)
                return;
 
-       switch (stmt_type(stmt)) {
-       case STMT_STORE:
-       case STMT_ARRAY_STORE_CHECK:
-               expr_put(to_expr(stmt->store_dest));
-               expr_put(to_expr(stmt->store_src));
-               break;
-       case STMT_IF:
-               expr_put(to_expr(stmt->if_conditional));
-               break;
-       case STMT_GOTO:
-               break;
-       case STMT_RETURN:
-               if (stmt->return_value)
-                       expr_put(to_expr(stmt->return_value));
-               break;
-       case STMT_ATHROW:
-       case STMT_VOID_RETURN:
-               break;
-       case STMT_EXPRESSION:
-       case STMT_ARRAY_CHECK:
-       case STMT_MONITOR_ENTER:
-       case STMT_MONITOR_EXIT:
-               if (stmt->expression)
-                       expr_put(to_expr(stmt->expression));
-               break;
-       case STMT_CHECKCAST:
-               if (stmt->checkcast_ref)
-                       expr_put(to_expr(stmt->checkcast_ref));
-               break;
-       case STMT_LAST:
-               assert(!"STMT_LAST is not a real type. Don't use it!");
-               break;
-       };
+       for (i = 0; i < stmt_nr_kids(stmt); i++)
+               if (stmt->node.kids[i])
+                       expr_put(to_expr(stmt->node.kids[i]));
+
        free(stmt);
 }
diff --git a/test/arch-x86/Makefile b/test/arch-x86/Makefile
index cdbcbe0..aae7593 100644
--- a/test/arch-x86/Makefile
+++ b/test/arch-x86/Makefile
@@ -52,6 +52,7 @@ OBJS = \
        ../../jit/exception.o \
        ../../jit/cu-mapping.o \
        ../../jit/method.o \
+       ../../jit/tree-node.o \
        ../../arch/x86/emit-code$(ARCH_POSTFIX).o \
        ../../arch/x86/instruction.o \
        ../../arch/x86/insn-selector$(ARCH_POSTFIX).o \
diff --git a/test/jit/Makefile b/test/jit/Makefile
index bed9cf7..7f35a33 100644
--- a/test/jit/Makefile
+++ b/test/jit/Makefile
@@ -41,6 +41,7 @@ OBJS = \
        ../../jit/bc-offset-mapping.o \
        ../../jit/cu-mapping.o \
        ../../jit/nop-bc.o \
+       ../../jit/tree-node.o \
        ../libharness/libharness.o \
        ../jamvm/alloc-stub.o \
        ../jamvm/resolve-stub.o \
-- 
1.6.0.6


------------------------------------------------------------------------------
Crystal Reports - New Free Runtime and 30 Day Trial
Check out the new simplified licensing option that enables unlimited
royalty-free distribution of the report engine for externally facing 
server and web deployment.
http://p.sf.net/sfu/businessobjects
_______________________________________________
Jatovm-devel mailing list
Jatovm-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/jatovm-devel

Reply via email to