* src/SDCCast.c (decorateType),
[fw/sdcc] / src / SDCCicode.c
index 9136a4cbee5684c6113adebe30121fe38fed1e61..2c0b61eab1d9943fa53dd57be9171ea27749e97b 100644 (file)
@@ -335,10 +335,10 @@ PRINTFUNC (picJumpTable)
   dbuf_append_char (dbuf, '\t');
   dbuf_printf (dbuf, "%s\t", s);
   dbuf_printOperand (IC_JTCOND (ic), dbuf);
-  dbuf_append_char (dbuf, '\n');
   for (sym = setFirstItem (IC_JTLABELS (ic)); sym;
        sym = setNextItem (IC_JTLABELS (ic)))
-    dbuf_printf (dbuf, "\t\t\t%s\n", sym->name);
+    dbuf_printf (dbuf, "; %s", sym->name);
+  dbuf_append_char (dbuf, '\n');
 }
 
 PRINTFUNC (picGeneric)
@@ -429,9 +429,10 @@ PRINTFUNC (picIfx)
     dbuf_printf (dbuf, " == 0 goto %s($%d)\n", IC_FALSE (ic)->name, IC_FALSE (ic)->key);
   else
     {
-      dbuf_printf (dbuf, " != 0 goto %s($%d)\n", IC_TRUE (ic)->name, IC_TRUE (ic)->key);
+      dbuf_printf (dbuf, " != 0 goto %s($%d)", IC_TRUE (ic)->name, IC_TRUE (ic)->key);
       if (IC_FALSE (ic))
-        dbuf_printf (dbuf, "\tzzgoto %s\n", IC_FALSE (ic)->name);
+        dbuf_printf (dbuf, "; zzgoto %s\n", IC_FALSE (ic)->name);
+      dbuf_append_char (dbuf, '\n');
     }
 }
 
@@ -528,6 +529,8 @@ printiCChain (iCode * icChain, FILE * of)
           dbuf_init(&dbuf, 1024);
           icTab->iCodePrint (&dbuf, loop, icTab->printName);
           dbuf_write_and_destroy (&dbuf, of);
+          ////
+          fflush(of);
         }
     }
 }
@@ -2035,7 +2038,7 @@ geniCodeCast (sym_link * type, operand * op, bool implicit)
 
   /* This seems very dangerous to me, since there are several */
   /* optimizations (for example, gcse) that don't notice the  */
-  /* cast hidden in this assignement and may simplify an      */
+  /* cast hidden in this assignment and may simplify an       */
   /* iCode to use the original (uncasted) operand.            */
   /* Unfortunately, other things break when this cast is      */
   /* made explicit. Need to fix this someday.                 */
@@ -2730,7 +2733,7 @@ geniCodePreDec (operand * op, bool lvalue)
 
 
 /*-----------------------------------------------------------------*/
-/* geniCodeBitwise - gen int code for bitWise  operators           */
+/* geniCodeBitwise - gen int code for bitWise operators            */
 /*-----------------------------------------------------------------*/
 operand *
 geniCodeBitwise (operand * left, operand * right,
@@ -2944,10 +2947,10 @@ geniCodeRightShift (operand * left, operand * right)
 /* geniCodeLogic- logic code                                       */
 /*-----------------------------------------------------------------*/
 static operand *
-geniCodeLogic (operand * left, operand * right, int op)
+geniCodeLogic (operand * left, operand * right, int op, ast *tree)
 {
   iCode *ic;
-  sym_link *ctype;
+  sym_link *ctype, *ttype;
   sym_link *rtype = operandType (right);
   sym_link *ltype = operandType (left);
 
@@ -3014,7 +3017,9 @@ geniCodeLogic (operand * left, operand * right, int op)
   ctype = usualBinaryConversions (&left, &right, RESULT_TYPE_BIT, 0);
 
   ic = newiCode (op, left, right);
-  IC_RESULT (ic) = newiTempOperand (newCharLink (), 1);
+  /* store 0 or 1 in result */
+  ttype = (tree && IS_BIT (tree->ftype)) ? newBoolLink() : newCharLink();
+  IC_RESULT (ic) = newiTempOperand (ttype, 1);
 
   /* if comparing float
      and not a '==' || '!=' || '&&' || '||' (these
@@ -3085,7 +3090,7 @@ geniCodeLogicAndOr (ast *tree, int lvl)
   ADDTOCHAIN (ic);
 
   /* store 0 or 1 in result */
-  type = (SPEC_NOUN(tree->ftype) == V_BIT) ? newBoolLink() : newCharLink();
+  type = (IS_BIT (tree->ftype)) ? newBoolLink() : newCharLink();
   result = newiTempOperand (type, 1);
 
   geniCodeLabel (falseLabel);
@@ -3136,18 +3141,19 @@ geniCodeConditional (ast * tree,int lvl)
   iCode *ic;
   symbol *falseLabel = newiTempLabel (NULL);
   symbol *exitLabel = newiTempLabel (NULL);
-  operand *cond = ast2iCode (tree->left,lvl+1);
-  operand *true, *false, *result;
+  ast *astTrue  = tree->right->left;
+  ast *astFalse = tree->right->right;
+  operand *cond = ast2iCode (tree->left, lvl+1);
+  operand *result = newiTempOperand (tree->right->ftype, 0);
+  operand *opTrue, *opFalse;
 
-  ic = newiCodeCondition (geniCodeRValue (cond, FALSE),
-                          NULL, falseLabel);
+  ic = newiCodeCondition (geniCodeRValue (cond, FALSE), NULL, falseLabel);
   ADDTOCHAIN (ic);
 
-  true = ast2iCode (tree->right->left,lvl+1);
+  opTrue = ast2iCode (astTrue, lvl+1);
 
-  /* move the value to a new Operand */
-  result = newiTempOperand (tree->right->ftype, 0);
-  geniCodeAssign (result, geniCodeRValue (true, FALSE), 0, 0);
+  /* move the value to the new operand */
+  geniCodeAssign (result, geniCodeRValue (opTrue, FALSE), 0, 0);
 
   /* generate an unconditional goto */
   geniCodeGoto (exitLabel);
@@ -3155,8 +3161,8 @@ geniCodeConditional (ast * tree,int lvl)
   /* now for the right side */
   geniCodeLabel (falseLabel);
 
-  false = ast2iCode (tree->right->right,lvl+1);
-  geniCodeAssign (result, geniCodeRValue (false, FALSE), 0, 0);
+  opFalse = ast2iCode (astFalse, lvl+1);
+  geniCodeAssign (result, geniCodeRValue (opFalse, FALSE), 0, 0);
 
   /* create the exit label */
   geniCodeLabel (exitLabel);
@@ -3238,7 +3244,9 @@ geniCodeAssign (operand * left, operand * right, int nosupdate, int strictLval)
     ic->supportRtn = 1;
 
   ic->nosupdate = nosupdate;
-  return left;
+  /* left could be a pointer assignment,
+     return the properly casted right instead */
+  return right;
 }
 
 /*-----------------------------------------------------------------*/
@@ -3756,13 +3764,13 @@ geniCodeJumpTable (operand * cond, value * caseVals, ast * tree)
          the condition is unsigned & minimum value is zero */
       if (!(min == 0 && IS_UNSIGNED (cetype)))
         {
-          boundary = geniCodeLogic (cond, operandFromLit (min), '<');
+          boundary = geniCodeLogic (cond, operandFromLit (min), '<', NULL);
           ic = newiCodeCondition (boundary, falseLabel, NULL);
           ADDTOCHAIN (ic);
         }
 
       /* now for upper bounds */
-      boundary = geniCodeLogic (cond, operandFromLit (max), '>');
+      boundary = geniCodeLogic (cond, operandFromLit (max), '>', NULL);
       ic = newiCodeCondition (boundary, falseLabel, NULL);
       ADDTOCHAIN (ic);
     }
@@ -3843,7 +3851,7 @@ geniCodeSwitch (ast * tree,int lvl)
 
       operand *compare = geniCodeLogic (cond,
                                         operandFromValue (caseVals),
-                                        EQ_OP);
+                                        EQ_OP, NULL);
 
       SNPRINTF (buffer, sizeof(buffer), "_case_%d_%d",
                tree->values.switchVals.swNum,
@@ -4237,17 +4245,33 @@ ast2iCode (ast * tree,int lvl)
     case GETHBIT:
       {
         operand *op = geniCodeUnary (geniCodeRValue (left, FALSE), tree->opval.op);
-        setOperandType (op, UCHARTYPE);
+        if (!IS_BIT (operandType (op)))
+          setOperandType (op, UCHARTYPE);
         return op;
       }
     case GETABIT:
+      {
+        operand *op = geniCodeBinary (geniCodeRValue (left, FALSE),
+                                      geniCodeRValue (right, FALSE),
+                                      tree->opval.op);
+        if (!IS_BIT (operandType (op)))
+          setOperandType (op, UCHARTYPE);
+        return op;
+      }
     case GETBYTE:
+      {
+        operand *op = geniCodeBinary (geniCodeRValue (left, FALSE),
+                                      geniCodeRValue (right, FALSE),
+                                      tree->opval.op);
+        setOperandType (op, UCHARTYPE);
+        return op;
+      }
     case GETWORD:
       {
         operand *op = geniCodeBinary (geniCodeRValue (left, FALSE),
                                       geniCodeRValue (right, FALSE),
                                       tree->opval.op);
-        setOperandType (op, (tree->opval.op == GETWORD) ? UINTTYPE : UCHARTYPE);
+        setOperandType (op, UINTTYPE);
         return op;
       }
     case AND_OP:
@@ -4261,7 +4285,7 @@ ast2iCode (ast * tree,int lvl)
     case NE_OP:
       /* different compilers (even different gccs) evaluate
          the two calls in a different order. to get the same
-         result on all machines we've to specify a clear sequence.
+         result on all machines we have to specify a clear sequence.
       return geniCodeLogic (geniCodeRValue (left, FALSE),
                             geniCodeRValue (right, FALSE),
                             tree->opval.op);
@@ -4272,7 +4296,7 @@ ast2iCode (ast * tree,int lvl)
         leftOp  = geniCodeRValue (left , FALSE);
         rightOp = geniCodeRValue (right, FALSE);
 
-        return geniCodeLogic (leftOp, rightOp, tree->opval.op);
+        return geniCodeLogic (leftOp, rightOp, tree->opval.op, tree);
       }
     case '?':
       return geniCodeConditional (tree,lvl);
@@ -4290,8 +4314,7 @@ ast2iCode (ast * tree,int lvl)
         else
           right = geniCodeRValue (right, FALSE);
 
-        geniCodeAssign (left, right, 0, 1);
-        return right;
+        return geniCodeAssign (left, right, 0, 1);
       }
     case MUL_ASSIGN:
       return