2006-01-03 Zoltan Varga <vargaz@gmail.com>
[mono.git] / mono / mini / inssel.brg
index 1d9470dfefae53f096e97ae924161bca36ab4ba9..ddeb206fb2deee101d2e13675f6618a7c0419b10 100644 (file)
@@ -64,7 +64,7 @@
                 (inst)->opcode = op; \
                 (inst)->dreg = dr; \
                 (inst)->sreg1 = sr; \
-                (inst)->inst_p1 = GINT_TO_POINTER (imm); \
+                (inst)->inst_p1 = (gpointer)(gssize)(imm); \
                mono_bblock_add_inst (cfg->cbb, inst); \
        } while (0)
 
                 inst->opcode = op; \
                 inst->dreg = dr; \
                 inst->sreg1 = sr; \
-                inst->inst_p1 = GINT_TO_POINTER (imm); \
+                inst->inst_p1 = (gpointer)(gssize)(imm); \
                mono_bblock_add_inst (cfg->cbb, inst); \
        } while (0)
 
+#define MONO_EMIT_BIALU_MEMBASE(cfg,tree,op,dr,sr,basereg,offset) do { \
+                               tree->opcode = op; \
+                               tree->dreg = dr; \
+                               tree->sreg1 = sr; \
+                               tree->sreg2 = basereg; \
+                               tree->inst_offset = offset; \
+                               mono_bblock_add_inst (s->cbb, tree); \
+       } while (0)
+
 #define MONO_EMIT_LOAD_MEMBASE(cfg,inst,dr,base,offset) do { \
                 (inst)->opcode = OP_LOAD_MEMBASE; \
                 (inst)->dreg = dr; \
@@ -396,6 +405,13 @@ stmt: OP_DUMMY_USE (CEE_LDIND_REF (OP_REGOFFSET)) {
 stmt: OP_DUMMY_STORE {
 }
 
+stmt: OP_NOT_REACHED {
+}
+
+stmt: OP_MEMORY_BARRIER {
+       mono_bblock_add_inst (s->cbb, tree);
+}
+
 #
 # load/store operations
 #
@@ -734,7 +750,7 @@ reg: OP_TLS_GET {
        mono_bblock_add_inst (s->cbb, tree);
 }
 
-stmt: CEE_SWITCH (reg) {
+stmt: CEE_SWITCH (reg) "2" {
        MonoInst *label;
        int offset_reg = mono_regstate_next_int (s->rs);
        int target_reg = mono_regstate_next_int (s->rs);
@@ -919,8 +935,13 @@ stmt: OP_THROW_OR_NULL (reg, reg) {
 
        MONO_NEW_LABEL (s, dont_throw);
 
-       MONO_EMIT_NEW_BIALU (s, OP_COMPARE, 0, state->left->reg1, state->right->reg1);
-       MONO_EMIT_NEW_BRANCH_LABEL (s, CEE_BNE_UN, dont_throw);
+       /*
+        * Currently, we allways rethrow the abort exception, despite the fact
+        * that this is not correct. See thread6.cs for an example. But propagating
+        * the abort exception is more important than getting the sematics right.
+     */
+       MONO_EMIT_NEW_BIALU_IMM (s, OP_COMPARE_IMM, -1, state->left->reg1, 0);
+       MONO_EMIT_NEW_BRANCH_LABEL (s, CEE_BEQ, dont_throw);
        MONO_EMIT_UNALU (s, tree, CEE_THROW, -1, state->left->reg1);
        mono_bblock_add_inst (s->cbb, dont_throw);
 }
@@ -986,7 +1007,7 @@ reg: CEE_ISINST (reg) {
                        MONO_EMIT_NEW_LOAD_MEMBASE (s, eclass_reg, klass_reg, G_STRUCT_OFFSET (MonoClass, cast_class));
                        if (klass->cast_class == mono_defaults.object_class) {
                                int parent_reg = mono_regstate_next_int (s->rs);
-                               int const_reg;
+                               int const_reg = -1;
                                MONO_EMIT_NEW_LOAD_MEMBASE (s, parent_reg, eclass_reg, G_STRUCT_OFFSET (MonoClass, parent));
                                if (s->compile_aot) {
                                        const_reg = mono_regstate_next_int (s->rs);
@@ -1005,7 +1026,7 @@ reg: CEE_ISINST (reg) {
                                MONO_EMIT_NEW_BRANCH_LABEL (s, CEE_BEQ, object_is_null);
                                MONO_EMIT_NEW_BRANCH_LABEL (s, CEE_BR, false_label);
                        } else if (klass->cast_class == mono_defaults.enum_class->parent) {
-                               int const_reg;
+                               int const_reg = -1;
                                
                                if (s->compile_aot) {
                                        const_reg = mono_regstate_next_int (s->rs);
@@ -1047,6 +1068,10 @@ reg: CEE_ISINST (reg) {
                                /* the object_is_null target simply copies the input register to the output */
                                mini_emit_isninst_cast (s, eclass_reg, klass->cast_class, false_label, object_is_null);
                        }
+               } else if (mono_class_is_nullable (klass)) {
+                       MONO_EMIT_NEW_LOAD_MEMBASE (s, klass_reg, vtable_reg, G_STRUCT_OFFSET (MonoVTable, klass));
+                       /* the object_is_null target simply copies the input register to the output */
+                       mini_emit_isninst_cast (s, klass_reg, klass->cast_class, false_label, object_is_null);
                } else {
                        if (!s->compile_aot && !(s->opt & MONO_OPT_SHARED) && (klass->flags & TYPE_ATTRIBUTE_SEALED)) {
                                /* the remoting code is broken, access the class for now */
@@ -1219,7 +1244,7 @@ reg: CEE_CASTCLASS (reg) {
                        MONO_EMIT_NEW_LOAD_MEMBASE (s, eclass_reg, klass_reg, G_STRUCT_OFFSET (MonoClass, cast_class));
                        if (klass->cast_class == mono_defaults.object_class) {
                                int parent_reg = mono_regstate_next_int (s->rs);
-                               int const_reg;
+                               int const_reg = -1;
                                MONO_EMIT_NEW_LOAD_MEMBASE (s, parent_reg, eclass_reg, G_STRUCT_OFFSET (MonoClass, parent));
                                if (s->compile_aot) {
                                        const_reg = mono_regstate_next_int (s->rs);
@@ -1598,7 +1623,6 @@ mini_emit_load_intf_reg (MonoCompile *s, int intf_reg, int ioffset_reg, MonoClas
        if (s->compile_aot) {
                int iid_reg = mono_regstate_next_int (s->rs);
                MONO_EMIT_NEW_AOTCONST (s, iid_reg, klass, MONO_PATCH_INFO_IID);
-               /* FIXME: Do a shift instead */
 #if SIZEOF_VOID_P == 8
                MONO_EMIT_NEW_BIALU_IMM (s, OP_SHL_IMM, iid_reg, iid_reg, 3);
 #else
@@ -1628,7 +1652,16 @@ mini_emit_load_intf_reg_class (MonoCompile *s, int intf_reg, int klass_reg, Mono
        int ioffset_reg = mono_regstate_next_int (s->rs);
 
        MONO_EMIT_NEW_LOAD_MEMBASE (s, ioffset_reg, klass_reg, G_STRUCT_OFFSET (MonoClass, interface_offsets));
-       mini_emit_load_intf_reg (s, intf_reg, ioffset_reg, klass);
+
+       if (s->compile_aot) {
+               int iid_reg = mono_regstate_next_int (s->rs);
+               MONO_EMIT_NEW_AOTCONST (s, iid_reg, klass, MONO_PATCH_INFO_IID);
+               MONO_EMIT_NEW_BIALU_IMM (s, OP_SHL_IMM, iid_reg, iid_reg, 2);
+               MONO_EMIT_NEW_BIALU (s, CEE_ADD, ioffset_reg, ioffset_reg, iid_reg);
+               MONO_EMIT_NEW_LOAD_MEMBASE_OP (s, OP_LOADI4_MEMBASE, intf_reg, ioffset_reg, 0);
+       }
+       else
+               MONO_EMIT_NEW_LOAD_MEMBASE_OP (s, OP_LOADI4_MEMBASE, intf_reg, ioffset_reg, klass->interface_id * sizeof (int));
 }
 
 void 
@@ -1637,13 +1670,22 @@ mini_emit_virtual_call (MonoCompile *cfg, void *st, MonoInst *tree, int novirtop
        MonoMethod *method = ((MonoCallInst*)tree)->method;
        int vtable_reg, slot_reg, this_reg;
        MBState *state = st;
+       gboolean enable_for_aot = TRUE;
 
        this_reg = state->left->reg1;
        mono_arch_emit_this_vret_args (cfg, (MonoCallInst*)tree, this_reg, state->left->tree->type, novirtop == OP_VCALL? state->right->reg1: -1);
 
-       if (!(method->flags & METHOD_ATTRIBUTE_VIRTUAL) || 
-           ((method->flags & METHOD_ATTRIBUTE_FINAL) && 
-            method->wrapper_type != MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK)) {
+#ifdef MONO_ARCH_NEED_GOT_VAR
+       /* Can't create a got var at this point */
+       enable_for_aot = cfg->got_var != NULL;
+#else
+       enable_for_aot = TRUE;
+#endif
+
+       if ((!cfg->compile_aot || enable_for_aot) && 
+               (!(method->flags & METHOD_ATTRIBUTE_VIRTUAL) || 
+            ((method->flags & METHOD_ATTRIBUTE_FINAL) && 
+            method->wrapper_type != MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK))) {
                /* 
                 * the method is not virtual, we just need to ensure this is not null
                 * and then we can call the method directly.
@@ -1661,6 +1703,9 @@ mini_emit_virtual_call (MonoCompile *cfg, void *st, MonoInst *tree, int novirtop
                return;
        }
 
+       /* Initialize method->slot */
+       mono_class_setup_vtable (method->klass);
+
        vtable_reg = mono_regstate_next_int (cfg->rs);
        MONO_EMIT_NEW_LOAD_MEMBASE (cfg, vtable_reg, this_reg, G_STRUCT_OFFSET (MonoObject, vtable));
        if (method->klass->flags & TYPE_ATTRIBUTE_INTERFACE) {
@@ -1827,7 +1872,7 @@ mini_emit_memcpy (MonoCompile *s, int destreg, int doffset, int srcreg, int soff
 }
 
 void 
-mini_emit_memset (MonoCompile *s, int destreg, int offset, int size, int val, int alignl)
+mini_emit_memset (MonoCompile *s, int destreg, int offset, int size, int val, int align)
 {
        int val_reg = mono_regstate_next_int (s->rs);
 
@@ -1836,13 +1881,21 @@ mini_emit_memset (MonoCompile *s, int destreg, int offset, int size, int val, in
        else
                MONO_EMIT_NEW_ICONST (s, val_reg, val);
 
+       /* FIXME: consider alignment for archs that need it. */
+#if !NO_UNALIGNED_ACCESS
        if (sizeof (gpointer) == 8) {
+               if (offset % 8) {
+                       MONO_EMIT_NEW_STORE_MEMBASE (s, OP_STOREI4_MEMBASE_REG, destreg, offset, val_reg);
+                       offset += 4;
+                       size -= 4;
+               }
                while (size >= 8) {
                        MONO_EMIT_NEW_STORE_MEMBASE (s, OP_STOREI8_MEMBASE_REG, destreg, offset, val_reg);
                        offset += 8;
                        size -= 8;
                }
        }       
+#endif
 
        while (size >= 4) {
                MONO_EMIT_NEW_STORE_MEMBASE (s, OP_STOREI4_MEMBASE_REG, destreg, offset, val_reg);