translate: improve cast simplification
[ia32rtools.git] / tools / translate.c
index 1bdc884..9922f32 100644 (file)
@@ -1087,7 +1087,7 @@ static void parse_op(struct parsed_op *op, char words[16][256], int wordc)
     op->operand_cnt = 2;
     setup_reg_opr(&op->operand[0], xAX, OPLM_BYTE, &op->regmask_src);
     op->regmask_dst = op->regmask_src;
-    setup_reg_opr(&op->operand[1], xDX, OPLM_DWORD, &op->regmask_src);
+    setup_reg_opr(&op->operand[1], xBX, OPLM_DWORD, &op->regmask_src);
     break;
 
   case OP_CDQ:
@@ -1099,8 +1099,6 @@ static void parse_op(struct parsed_op *op, char words[16][256], int wordc)
   case OP_LODS:
   case OP_STOS:
   case OP_SCAS:
-    if (op->operand_cnt != 0)
-      break;
     if      (words[op_w][4] == 'b')
       lmod = OPLM_BYTE;
     else if (words[op_w][4] == 'w')
@@ -1108,20 +1106,21 @@ static void parse_op(struct parsed_op *op, char words[16][256], int wordc)
     else if (words[op_w][4] == 'd')
       lmod = OPLM_DWORD;
     j = 0;
+    op->regmask_src = 0;
     setup_reg_opr(&op->operand[j++], op->op == OP_LODS ? xSI : xDI,
-      lmod, &op->regmask_src);
-    if (op->flags & OPF_REP)
-      setup_reg_opr(&op->operand[j++], xCX, OPLM_DWORD, &op->regmask_src);
+      OPLM_DWORD, &op->regmask_src);
     op->regmask_dst = op->regmask_src;
-    setup_reg_opr(&op->operand[j++], xAX, OPLM_DWORD,
+    setup_reg_opr(&op->operand[j++], xAX, lmod,
       op->op == OP_LODS ? &op->regmask_dst : &op->regmask_src);
+    if (op->flags & OPF_REP) {
+      setup_reg_opr(&op->operand[j++], xCX, OPLM_DWORD, &op->regmask_src);
+      op->regmask_dst |= 1 << xCX;
+    }
     op->operand_cnt = j;
     break;
 
   case OP_MOVS:
   case OP_CMPS:
-    if (op->operand_cnt != 0)
-      break;
     if      (words[op_w][4] == 'b')
       lmod = OPLM_BYTE;
     else if (words[op_w][4] == 'w')
@@ -1129,6 +1128,8 @@ static void parse_op(struct parsed_op *op, char words[16][256], int wordc)
     else if (words[op_w][4] == 'd')
       lmod = OPLM_DWORD;
     j = 0;
+    op->regmask_src = 0;
+    // note: lmod is not correct, don't have where to place it
     setup_reg_opr(&op->operand[j++], xDI, lmod, &op->regmask_src);
     setup_reg_opr(&op->operand[j++], xSI, OPLM_DWORD, &op->regmask_src);
     if (op->flags & OPF_REP)
@@ -1420,10 +1421,45 @@ static const char *opr_reg_p(struct parsed_op *po, struct parsed_opr *popr)
   return regs_r32[popr->reg];
 }
 
+static int check_simple_cast(const char *cast, int *bits, int *is_signed)
+{
+  if      (IS_START(cast, "(s8)") || IS_START(cast, "(u8)"))
+    *bits = 8;
+  else if (IS_START(cast, "(s16)") || IS_START(cast, "(u16)"))
+    *bits = 16;
+  else if (IS_START(cast, "(s32)") || IS_START(cast, "(u32)"))
+    *bits = 32;
+  else if (IS_START(cast, "(s64)") || IS_START(cast, "(u64)"))
+    *bits = 64;
+  else
+    return -1;
+
+  *is_signed = cast[1] == 's' ? 1 : 0;
+  return 0;
+}
+
+static int check_deref_cast(const char *cast, int *bits)
+{
+  if      (IS_START(cast, "*(u8 *)"))
+    *bits = 8;
+  else if (IS_START(cast, "*(u16 *)"))
+    *bits = 16;
+  else if (IS_START(cast, "*(u32 *)"))
+    *bits = 32;
+  else if (IS_START(cast, "*(u64 *)"))
+    *bits = 64;
+  else
+    return -1;
+
+  return 0;
+}
+
 // cast1 is the "final" cast
 static const char *simplify_cast(const char *cast1, const char *cast2)
 {
   static char buf[256];
+  int bits1, bits2;
+  int s1, s2;
 
   if (cast1[0] == 0)
     return cast2;
@@ -1431,14 +1467,22 @@ static const char *simplify_cast(const char *cast1, const char *cast2)
     return cast1;
   if (IS(cast1, cast2))
     return cast1;
-  if (IS(cast1, "(s8)") && IS(cast2, "(u8)"))
-    return cast1;
-  if (IS(cast1, "(s16)") && IS(cast2, "(u16)"))
-    return cast1;
-  if (IS(cast1, "(u8)") && IS_START(cast2, "*(u8 *)"))
-    return cast2;
-  if (IS(cast1, "(u16)") && IS_START(cast2, "*(u16 *)"))
-    return cast2;
+
+  if (check_simple_cast(cast1, &bits1, &s1) == 0
+    && check_simple_cast(cast2, &bits2, &s2) == 0)
+  {
+    if (bits1 <= bits2)
+      return cast1;
+  }
+  if (check_simple_cast(cast1, &bits1, &s1) == 0
+    && check_deref_cast(cast2, &bits2) == 0)
+  {
+    if (bits1 == bits2) {
+      snprintf(buf, sizeof(buf), "*(%c%d *)", s1 ? 's' : 'u', bits1);
+      return buf;
+    }
+  }
+
   if (strchr(cast1, '*') && IS_START(cast2, "(u32)"))
     return cast1;
 
@@ -5298,10 +5342,11 @@ static void gen_func(FILE *fout, FILE *fhdr, const char *funcn, int opcnt)
         }
         else {
           assert_operand_cnt(2);
-          fprintf(fout, "  eax = %sesi; esi %c= %d;",
-            lmod_cast_u_ptr(po, po->operand[0].lmod),
+          fprintf(fout, "  %s = %sesi; esi %c= %d;",
+            out_dst_opr(buf1, sizeof(buf1), po, &po->operand[1]),
+            lmod_cast_u_ptr(po, po->operand[1].lmod),
             (po->flags & OPF_DF) ? '-' : '+',
-            lmod_bytes(po, po->operand[0].lmod));
+            lmod_bytes(po, po->operand[1].lmod));
           strcpy(g_comment, "lods");
         }
         break;
@@ -5311,17 +5356,17 @@ static void gen_func(FILE *fout, FILE *fhdr, const char *funcn, int opcnt)
           assert_operand_cnt(3);
           fprintf(fout, "  for (; ecx != 0; ecx--, edi %c= %d)\n",
             (po->flags & OPF_DF) ? '-' : '+',
-            lmod_bytes(po, po->operand[0].lmod));
+            lmod_bytes(po, po->operand[1].lmod));
           fprintf(fout, "    %sedi = eax;",
-            lmod_cast_u_ptr(po, po->operand[0].lmod));
+            lmod_cast_u_ptr(po, po->operand[1].lmod));
           strcpy(g_comment, "rep stos");
         }
         else {
           assert_operand_cnt(2);
           fprintf(fout, "  %sedi = eax; edi %c= %d;",
-            lmod_cast_u_ptr(po, po->operand[0].lmod),
+            lmod_cast_u_ptr(po, po->operand[1].lmod),
             (po->flags & OPF_DF) ? '-' : '+',
-            lmod_bytes(po, po->operand[0].lmod));
+            lmod_bytes(po, po->operand[1].lmod));
           strcpy(g_comment, "stos");
         }
         break;
@@ -5388,7 +5433,7 @@ static void gen_func(FILE *fout, FILE *fhdr, const char *funcn, int opcnt)
       case OP_SCAS:
         // only does ZF (for now)
         // repe ~ repeat while ZF=1
-        j = lmod_bytes(po, po->operand[0].lmod);
+        j = lmod_bytes(po, po->operand[1].lmod);
         l = (po->flags & OPF_DF) ? '-' : '+';
         if (po->flags & OPF_REP) {
           assert_operand_cnt(3);
@@ -5396,8 +5441,8 @@ static void gen_func(FILE *fout, FILE *fhdr, const char *funcn, int opcnt)
             "  for (; ecx != 0; ecx--) {\n");
           fprintf(fout,
             "    cond_z = (%seax == %sedi); edi %c= %d;\n",
-              lmod_cast_u(po, po->operand[0].lmod),
-              lmod_cast_u_ptr(po, po->operand[0].lmod), l, j);
+              lmod_cast_u(po, po->operand[1].lmod),
+              lmod_cast_u_ptr(po, po->operand[1].lmod), l, j);
           fprintf(fout,
             "    if (cond_z %s 0) break;\n",
               (po->flags & OPF_REPZ) ? "==" : "!=");
@@ -5409,8 +5454,8 @@ static void gen_func(FILE *fout, FILE *fhdr, const char *funcn, int opcnt)
         else {
           assert_operand_cnt(2);
           fprintf(fout, "  cond_z = (%seax == %sedi); edi %c= %d;",
-              lmod_cast_u(po, po->operand[0].lmod),
-              lmod_cast_u_ptr(po, po->operand[0].lmod), l, j);
+              lmod_cast_u(po, po->operand[1].lmod),
+              lmod_cast_u_ptr(po, po->operand[1].lmod), l, j);
           strcpy(g_comment, "scas");
         }
         pfomask &= ~(1 << PFO_Z);
@@ -5851,7 +5896,7 @@ static void gen_func(FILE *fout, FILE *fhdr, const char *funcn, int opcnt)
         break;
 
       case OP_LOOP:
-        fprintf(fout, "  if (--ecx == 0)\n");
+        fprintf(fout, "  if (--ecx != 0)\n");
         fprintf(fout, "    goto %s;", po->operand[0].name);
         strcat(g_comment, "loop");
         break;