* Exception.cs: Cosmetic change to default exception message on
[mono.git] / mono / mini / cfold.c
index 092c7d7ba13ffd093598045f8981de0f18630605..ff187c3fa68a3532a670f634b578c42b94d5fc94 100644 (file)
@@ -29,7 +29,7 @@ mono_is_power_of_two (guint32 val)
                        return; \
                if (inst->inst_i1->opcode == OP_ICONST) {       \
                        inst->opcode = OP_ICONST;       \
-                       inst->inst_c0 = inst->inst_i0->inst_c0 op inst->inst_i1->inst_c0;       \
+                       inst->inst_c0 = (gint32)(inst->inst_i0->inst_c0 op inst->inst_i1->inst_c0);     \
                } \
                 return;
 
@@ -43,7 +43,7 @@ mono_is_power_of_two (guint32 val)
                if (inst->inst_i0->opcode == OP_ICONST) {\
                        if (inst->inst_i1->opcode == OP_ICONST) {       \
                                inst->opcode = OP_ICONST;       \
-                               inst->inst_c0 = inst->inst_i0->inst_c0 op inst->inst_i1->inst_c0;       \
+                               inst->inst_c0 = (gint32)(inst->inst_i0->inst_c0 op inst->inst_i1->inst_c0);     \
                                 return; \
                        } else { \
                                MonoInst *tmp = inst->inst_i0;  \
@@ -150,9 +150,9 @@ mono_is_power_of_two (guint32 val)
                        return; \
                if (inst->inst_i0->inst_i1->opcode == OP_ICONST) {      \
                        if ((cast)inst->inst_i0->inst_i0->inst_c0 op (cast)inst->inst_i0->inst_i1->inst_c0)     \
-                               inst->opcode = CEE_BR;  \
+                               inst->opcode = OP_BR;   \
                        else    \
-                               inst->opcode = CEE_NOP; \
+                               inst->opcode = OP_NOP;  \
                } \
                 return;
 
@@ -254,3 +254,67 @@ mono_constant_fold (MonoCompile *cfg)
        }
 }
 
+/*
+ * If the arguments to the cond branch are constants, eval and
+ * return BRANCH_NOT_TAKEN for not taken, BRANCH_TAKEN for taken,
+ * BRANCH_UNDEF otherwise.
+ * If this code is changed to handle also non-const values, make sure
+ * side effects are handled in optimize_branches() in mini.c, by
+ * inserting pop instructions.
+ */
+int
+mono_eval_cond_branch (MonoInst *ins)
+{
+       MonoInst *left, *right;
+       /* FIXME: handle also 64 bit ints */
+       left = ins->inst_left->inst_left;
+       if (left->opcode != OP_ICONST && left->opcode != OP_PCONST)
+               return BRANCH_UNDEF;
+       right = ins->inst_left->inst_right;
+       if (right->opcode != OP_ICONST && right->opcode != OP_PCONST)
+               return BRANCH_UNDEF;
+       switch (ins->opcode) {
+       case CEE_BEQ:
+               if (left->inst_c0 == right->inst_c0)
+                       return BRANCH_TAKEN;
+               return BRANCH_NOT_TAKEN;
+       case CEE_BGE:
+               if (left->inst_c0 >= right->inst_c0)
+                       return BRANCH_TAKEN;
+               return BRANCH_NOT_TAKEN;
+       case CEE_BGT:
+               if (left->inst_c0 > right->inst_c0)
+                       return BRANCH_TAKEN;
+               return BRANCH_NOT_TAKEN;
+       case CEE_BLE:
+               if (left->inst_c0 <= right->inst_c0)
+                       return BRANCH_TAKEN;
+               return BRANCH_NOT_TAKEN;
+       case CEE_BLT:
+               if (left->inst_c0 < right->inst_c0)
+                       return BRANCH_TAKEN;
+               return BRANCH_NOT_TAKEN;
+       case CEE_BNE_UN:
+               if ((gsize)left->inst_c0 != (gsize)right->inst_c0)
+                       return BRANCH_TAKEN;
+               return BRANCH_NOT_TAKEN;
+       case CEE_BGE_UN:
+               if ((gsize)left->inst_c0 >= (gsize)right->inst_c0)
+                       return BRANCH_TAKEN;
+               return BRANCH_NOT_TAKEN;
+       case CEE_BGT_UN:
+               if ((gsize)left->inst_c0 > (gsize)right->inst_c0)
+                       return BRANCH_TAKEN;
+               return BRANCH_NOT_TAKEN;
+       case CEE_BLE_UN:
+               if ((gsize)left->inst_c0 <= (gsize)right->inst_c0)
+                       return BRANCH_TAKEN;
+               return BRANCH_NOT_TAKEN;
+       case CEE_BLT_UN:
+               if ((gsize)left->inst_c0 < (gsize)right->inst_c0)
+                       return BRANCH_TAKEN;
+               return BRANCH_NOT_TAKEN;
+       }
+       return BRANCH_UNDEF;
+}
+