c: Fix some bugs in code generation
authorDavid Schleef <ds@schleef.org>
Thu, 9 Jul 2009 19:07:36 +0000 (12:07 -0700)
committerDavid Schleef <ds@schleef.org>
Thu, 9 Jul 2009 19:07:36 +0000 (12:07 -0700)
orc/opcodes.h
orc/orcprogram-c.c

index 2e0e738..6f6ffba 100644 (file)
@@ -3,19 +3,19 @@ BINARY_SB(addb, "%s + %s")
 BINARY_SB(addssb, "ORC_CLAMP_SB(%s + %s)")
 BINARY_UB(addusb, "ORC_CLAMP_UB((uint8_t)%s + (uint8_t)%s)")
 BINARY_SB(andb, "%s & %s")
-BINARY_SB(andnb, "%s & (~%s)")
+BINARY_SB(andnb, "(~%s) & %s")
 BINARY_SB(avgsb, "(%s + %s + 1)>>1")
 BINARY_UB(avgub, "((uint8_t)%s + (uint8_t)%s + 1)>>1")
 BINARY_SB(cmpeqb, "(%s == %s) ? (~0) : 0")
 BINARY_SB(cmpgtsb, "(%s > %s) ? (~0) : 0")
 UNARY_SB(copyb, "%s")
 BINARY_SB(maxsb, "ORC_MAX(%s, %s)")
-BINARY_UB(maxub, "ORC_MAX(%s, %s)")
+BINARY_UB(maxub, "ORC_MAX((uint8_t)%s, (uint8_t)%s)")
 BINARY_SB(minsb, "ORC_MIN(%s, %s)")
-BINARY_UB(minub, "ORC_MIN(%s, %s)")
+BINARY_UB(minub, "ORC_MIN((uint8_t)%s, (uint8_t)%s)")
 BINARY_SB(mullb, "(%s * %s) & 0xff")
 BINARY_SB(mulhsb, "(%s * %s) >> 8")
-BINARY_UB(mulhub, "(%s * %s) >> 8")
+BINARY_UB(mulhub, "((uint32_t)(uint8_t)%s * (uint32_t)(uint8_t)%s) >> 8")
 BINARY_SB(orb, "%s | %s")
 BINARY_SB(shlb, "%s << %s")
 BINARY_SB(shrsb, "%s >> %s")
@@ -31,16 +31,16 @@ BINARY_SW(addw, "%s + %s")
 BINARY_SW(addssw, "ORC_CLAMP_SW(%s + %s)")
 BINARY_UW(addusw, "ORC_CLAMP_UW((uint16_t)%s + (uint16_t)%s)")
 BINARY_SW(andw, "%s & %s")
-BINARY_SW(andnw, "%s & (~%s)")
+BINARY_SW(andnw, "(~%s) & %s")
 BINARY_SW(avgsw, "(%s + %s + 1)>>1")
 BINARY_UW(avguw, "((uint16_t)%s + (uint16_t)%s + 1)>>1")
 BINARY_SW(cmpeqw, "(%s == %s) ? (~0) : 0")
 BINARY_SW(cmpgtsw, "(%s > %s) ? (~0) : 0")
 UNARY_SW(copyw, "%s")
 BINARY_SW(maxsw, "ORC_MAX(%s, %s)")
-BINARY_UW(maxuw, "ORC_MAX(%s, %s)")
+BINARY_UW(maxuw, "ORC_MAX((uint16_t)%s, (uint16_t)%s)")
 BINARY_SW(minsw, "ORC_MIN(%s, %s)")
-BINARY_UW(minuw, "ORC_MIN(%s, %s)")
+BINARY_UW(minuw, "ORC_MIN((uint16_t)%s, (uint16_t)%s)")
 BINARY_SW(mullw, "(%s * %s) & 0xffff")
 BINARY_SW(mulhsw, "(%s * %s) >> 16")
 BINARY_UW(mulhuw, "((uint32_t)((uint16_t)%s) * (uint32_t)((uint16_t)%s)) >> 16")
@@ -57,18 +57,18 @@ BINARY_SW(xorw, "%s ^ %s")
 UNARY_SL(absl, "ORC_ABS(%s)")
 BINARY_SL(addl, "%s + %s")
 BINARY_SL(addssl, "ORC_CLAMP_SL((int64_t)%s + (int64_t)%s)")
-BINARY_UL(addusl, "ORC_CLAMP_UL((uint64_t)%s + (uint64_t)%s)")
+BINARY_UL(addusl, "ORC_CLAMP_UL((int64_t)(uint32_t)%s + (int64_t)(uint32_t)%s)")
 BINARY_SL(andl, "%s & %s")
-BINARY_SL(andnl, "%s & (~%s)")
-BINARY_SL(avgsl, "(%s + %s + 1)>>1")
-BINARY_UL(avgul, "((uint32_t)%s + (uint32_t)%s + 1)>>1")
+BINARY_SL(andnl, "(~%s) & %s")
+BINARY_SL(avgsl, "((int64_t)%s + (int64_t)%s + 1)>>1")
+BINARY_UL(avgul, "((uint64_t)(uint32_t)%s + (uint64_t)(uint32_t)%s + 1)>>1")
 BINARY_SL(cmpeql, "(%s == %s) ? (~0) : 0")
 BINARY_SL(cmpgtsl, "(%s > %s) ? (~0) : 0")
 UNARY_SL(copyl, "%s")
 BINARY_SL(maxsl, "ORC_MAX(%s, %s)")
-BINARY_UL(maxul, "ORC_MAX(%s, %s)")
+BINARY_UL(maxul, "ORC_MAX((uint32_t)%s, (uint32_t)%s)")
 BINARY_SL(minsl, "ORC_MIN(%s, %s)")
-BINARY_UL(minul, "ORC_MIN(%s, %s)")
+BINARY_UL(minul, "ORC_MIN((uint32_t)%s, (uint32_t)%s)")
 BINARY_SL(mulll, "(%s * %s) & 0xffffffff")
 BINARY_SL(mulhsl, "((int64_t)%s * (int64_t)%s) >> 32")
 BINARY_UL(mulhul, "((uint64_t)%s * (uint64_t)%s) >> 32")
@@ -79,7 +79,7 @@ BINARY_UL(shrul, "((uint32_t)%s) >> %s")
 UNARY_SL(signl, "ORC_CLAMP(%s,-1,1)")
 BINARY_SL(subl, "%s - %s")
 BINARY_SL(subssl, "ORC_CLAMP_SL((int64_t)%s - (int64_t)%s)")
-BINARY_UL(subusl, "ORC_CLAMP_UL((uint64_t)%s - (uint64_t)%s)")
+BINARY_UL(subusl, "ORC_CLAMP_UL((int64_t)(uint32_t)%s - (int64_t)(uint32_t)%s)")
 BINARY_SL(xorl, "%s ^ %s")
 
 UNARY_BW(convsbw, "%s")
@@ -89,13 +89,13 @@ UNARY_WL(convuwl, "(uint16_t)%s")
 UNARY_WB(convwb, "%s")
 UNARY_WB(convssswb, "ORC_CLAMP_SB(%s)")
 UNARY_WB(convsuswb, "ORC_CLAMP_UB(%s)")
-UNARY_WB(convusswb, "ORC_CLAMP_SB(%s)")
-UNARY_WB(convuuswb, "ORC_CLAMP_UB(%s)")
+UNARY_WB(convusswb, "ORC_CLAMP_SB((uint16_t)%s)")
+UNARY_WB(convuuswb, "ORC_CLAMP_UB((uint16_t)%s)")
 UNARY_LW(convlw, "%s")
 UNARY_LW(convssslw, "ORC_CLAMP_SW(%s)")
 UNARY_LW(convsuslw, "ORC_CLAMP_UW(%s)")
-UNARY_LW(convusslw, "ORC_CLAMP_SW(%s)")
-UNARY_LW(convuuslw, "ORC_CLAMP_UW(%s)")
+UNARY_LW(convusslw, "ORC_CLAMP_SW((uint32_t)%s)")
+UNARY_LW(convuuslw, "ORC_CLAMP_UW((uint32_t)%s)")
 
 BINARY_BW(mulsbw, "%s * %s")
 BINARY_BW(mulubw, "(uint8_t)%s * (uint8_t)%s")
index 9c6234d..bc66f6d 100644 (file)
@@ -54,7 +54,7 @@ orc_target_get_asm_preamble (const char *target)
     "  //OrcVariable *args[ORC_OPCODE_N_ARGS];\n"
     "};\n"
     "#define ORC_CLAMP(x,a,b) ((x)<(a) ? (a) : ((x)>(b) ? (b) : (x)))\n"
-    "#define ORC_ABS(a) ((a)<0 ? (-a) : (a))\n"
+    "#define ORC_ABS(a) ((a)<0 ? -(a) : (a))\n"
     "#define ORC_MIN(a,b) ((a)<(b) ? (a) : (b))\n"
     "#define ORC_MAX(a,b) ((a)>(b) ? (a) : (b))\n"
     "#define ORC_SB_MAX 127\n"
@@ -163,6 +163,7 @@ orc_compiler_c_assemble (OrcCompiler *compiler)
       compiler->error = TRUE;
     }
   }
+  ORC_ASM_CODE(compiler,"  }\n");
 
   for(i=0;i<ORC_N_VARIABLES;i++){
     OrcVariable *var = compiler->vars + i;
@@ -177,7 +178,6 @@ orc_compiler_c_assemble (OrcCompiler *compiler)
     }
   }
 
-  ORC_ASM_CODE(compiler,"  }\n");
   if (!(compiler->target_flags & ORC_TARGET_C_BARE)) {
     ORC_ASM_CODE(compiler,"}\n");
     ORC_ASM_CODE(compiler,"\n");
@@ -326,7 +326,9 @@ c_rule_accsadubl (OrcCompiler *p, void *user, OrcInstruction *insn)
   c_get_name (src1, p, insn->src_args[0]);
   c_get_name (src2, p, insn->src_args[1]);
 
-  ORC_ASM_CODE(p,"    %s = %s + ORC_ABS(%s - %s);\n", dest, dest, src1, src2);
+  ORC_ASM_CODE(p,
+      "    %s = %s + ORC_ABS((int32_t)(uint8_t)%s - (int32_t)(uint8_t)%s);\n",
+      dest, dest, src1, src2);
 }
 
 static OrcTarget c_target = {