promote operand instead of result for +-*
[fw/sdcc] / src / SDCCast.c
index a647dcddb0dc05aab8a07db8107a84087ae28554..10ee1ea0c9a1058e022e06656b1f1a7fbbc5714c 100644 (file)
@@ -367,6 +367,7 @@ resolveSymbols (ast * tree)
   if (tree == NULL)
     return tree;
 
+#if 0
   /* print the line          */
   /* if not block & function */
   if (tree->type == EX_OP &&
@@ -377,6 +378,7 @@ resolveSymbols (ast * tree)
       filename = tree->filename;
       lineno = tree->lineno;
     }
+#endif
 
   /* make sure we resolve the true & false labels for ifx */
   if (tree->type == EX_OP && tree->opval.op == IFX)
@@ -1085,6 +1087,16 @@ gatherAutoInit (symbol * autoChain)
       /* if there is an initial value */
       if (sym->ival && SPEC_SCLS (sym->etype) != S_CODE)
        {
+         initList *ilist=sym->ival;
+         
+         while (ilist->type == INIT_DEEP) {
+           ilist = ilist->init.deep;
+         }
+
+         /* update lineno for error msg */
+         lineno=sym->lineDef;
+         setAstLineno (ilist->init.node, lineno);
+         
          if (IS_AGGREGATE (sym->type)) {
            work = initAggregates (sym, sym->ival, NULL);
          } else {
@@ -1095,8 +1107,10 @@ gatherAutoInit (symbol * autoChain)
            work = newNode ('=', newAst_VALUE (symbolVal (sym)),
                            list2expr (sym->ival));
          }
-
+         
+         // just to be sure
          setAstLineno (work, sym->lineDef);
+
          sym->ival = NULL;
          if (init)
            init = newNode (NULLOP, init, work);
@@ -1849,6 +1863,7 @@ decorateType (ast * tree)
 
   tree->decorated = 1;
 
+#if 0
   /* print the line          */
   /* if not block & function */
   if (tree->type == EX_OP &&
@@ -1859,6 +1874,7 @@ decorateType (ast * tree)
       filename = tree->filename;
       lineno = tree->lineno;
     }
+#endif
 
   /* if any child is an error | this one is an error do nothing */
   if (tree->isError ||
@@ -1942,6 +1958,27 @@ decorateType (ast * tree)
       tree->left = dtl;
     if (dtr != tree->right)
       tree->right = dtr;
+
+    /* special case for some operations: cast up right->left if type of left
+       has greater size than right */
+    if (tree->left && tree->right && IS_AST_OP(tree->right) &&
+       (tree->right->opval.op == LEFT_OP ||
+        (tree->right->opval.op == '*' /* for int -> long only */ &&
+         tree->right->right /* but not for deref */ ) ||
+        tree->right->opval.op == '+' ||
+        tree->right->opval.op == '-')) {
+       int lsize = getSize(LTYPE(tree));
+       int rsize = getSize(RTYPE(tree));
+
+       if (lsize > rsize) {
+           tree->right->decorated = 0;
+           tree->right->left = newNode( CAST, (lsize == 2 ? 
+                                              newAst_LINK(newIntLink()) : 
+                                              newAst_LINK(newLongLink())),
+                                       tree->right->left);
+           tree->right = decorateType(tree->right);
+       }
+    }
   }
 
   /* depending on type of operator do */
@@ -2013,7 +2050,7 @@ decorateType (ast * tree)
       /*----------------------------*/
     case PTR_OP:
       /* if not pointer to a structure */
-      if (!IS_PTR (LTYPE (tree)))
+      if (!IS_PTR (LTYPE (tree)) && !IS_ARRAY (LTYPE(tree)))
        {
          werror (E_PTR_REQD);
          goto errorTreeReturn;
@@ -2054,6 +2091,7 @@ decorateType (ast * tree)
       case UPOINTER:
       case ARRAY:
       case FUNCTION:
+       break;
       }
 
       return tree;
@@ -2359,17 +2397,13 @@ decorateType (ast * tree)
        }
 
       LRVAL (tree) = RRVAL (tree) = 1;
+      TETYPE (tree) = getSpec (TTYPE (tree) =
+                              computeType (LTYPE (tree),
+                                           RTYPE (tree)));
       /* promote result to int if left & right are char
         this will facilitate hardware multiplies 8bit x 8bit = 16bit */
       if (IS_CHAR(LETYPE(tree)) && IS_CHAR(RETYPE(tree))) {
-       TETYPE (tree) = getSpec (TTYPE (tree) =
-                                computeType (LTYPE (tree),
-                                             RTYPE (tree)));
        SPEC_NOUN(TETYPE(tree)) = V_INT;
-      } else {
-       TETYPE (tree) = getSpec (TTYPE (tree) =
-                                computeType (LTYPE (tree),
-                                             RTYPE (tree)));
       }
       return tree;
 
@@ -2454,7 +2488,7 @@ decorateType (ast * tree)
 
       LRVAL (tree) = RRVAL (tree) = 1;
       /* if the left is a pointer */
-      if (IS_PTR (LTYPE (tree)))
+      if (IS_PTR (LTYPE (tree)) || IS_ARRAY (LTYPE (tree)))
        TETYPE (tree) = getSpec (TTYPE (tree) =
                                 LTYPE (tree));
       else
@@ -2655,6 +2689,7 @@ decorateType (ast * tree)
                                   tree->opval.val->type);
          return tree;
        }
+#if 0
       /* a left shift must be done with at least 16bits */
       if ((tree->opval.op==LEFT_OP) && (getSize(LTYPE(tree))<2)) {
        // insert a cast
@@ -2669,19 +2704,31 @@ decorateType (ast * tree)
          SPEC_NOUN(tree->left->opval.val->type)=V_INT;
        }
       }
+#endif
       /* if only the right side is a literal & we are
          shifting more than size of the left operand then zero */
       if (IS_LITERAL (RTYPE (tree)) &&
          ((unsigned) floatFromVal (valFromType (RETYPE (tree)))) >=
          (getSize (LTYPE (tree)) * 8))
        {
-         werror (W_SHIFT_CHANGED,
-                 (tree->opval.op == LEFT_OP ? "left" : "right"));
-         tree->type = EX_VALUE;
-         tree->left = tree->right = NULL;
-         tree->opval.val = constVal ("0");
-         TETYPE (tree) = TTYPE (tree) = tree->opval.val->type;
-         return tree;
+           /* if left shift then cast up */
+           if (tree->opval.op==LEFT_OP) {
+               int size = getSize(LTYPE(tree));
+               tree->left = 
+                   decorateType (newNode (CAST,
+                                          (size == 1 ? newAst_LINK(newIntLink()) : 
+                                           (size == 2 ? newAst_LINK(newLongLink()) : 
+                                            newAst_LINK(newIntLink()))),
+                                          tree->left));
+           } else {
+               werror (W_SHIFT_CHANGED,
+                       (tree->opval.op == LEFT_OP ? "left" : "right"));
+               tree->type = EX_VALUE;
+               tree->left = tree->right = NULL;
+               tree->opval.val = constVal ("0");
+               TETYPE (tree) = TTYPE (tree) = tree->opval.val->type;
+               return tree;
+           }
        }
       LRVAL (tree) = RRVAL (tree) = 1;
       if (IS_LITERAL (LTYPE (tree)) && !IS_LITERAL (RTYPE (tree)))