* src/SDCCast.c (decorateType): fixed bug #898889, cast result of a literal complemen...
[fw/sdcc] / src / SDCCast.c
index 979dcfe225868fb1da8d4f06c920330caf1c503f..f44ae1cc744590b239bde7a01f464f7c1e3c8462 100644 (file)
@@ -22,6 +22,8 @@
    what you give them.   Help stamp out software-hoarding!
 -------------------------------------------------------------------------*/
 
+#define DEBUG_CF(x) /* puts(x); */
+
 #include "common.h"
 
 int currLineno = 0;
@@ -644,89 +646,115 @@ reverseParms (ast * ptree)
 /* processParms  - makes sure the parameters are okay and do some  */
 /*                 processing with them                            */
 /*-----------------------------------------------------------------*/
-int 
-processParms (ast * func,
+static int
+processParms (ast *func,
              value *defParm,
-             ast * actParm,
-             int *parmNumber, // unused, although updated
+             ast **actParm,
+             int *parmNumber, /* unused, although updated */
              bool rightmost)
 {
+  RESULT_TYPE resultType;
+  sym_link *functype;
+  
   /* if none of them exist */
-  if (!defParm && !actParm)
+  if (!defParm && !*actParm)
     return 0;
 
-  if (defParm) {
-    if (getenv("DEBUG_SANITY")) {
-      fprintf (stderr, "processParms: %s ", defParm->name);
+  if (defParm)
+    {
+      if (getenv("DEBUG_SANITY"))
+        {
+          fprintf (stderr, "processParms: %s ", defParm->name);
+        }
+      /* make sure the type is complete and sane */
+      checkTypeSanity(defParm->etype, defParm->name);
     }
-    /* make sure the type is complete and sane */
-    checkTypeSanity(defParm->etype, defParm->name);
-  }
 
+  if (IS_CODEPTR (func->ftype))
+    functype = func->ftype->next;
+  else
+    functype = func->ftype;
+    
   /* if the function is being called via a pointer &   */
   /* it has not been defined a reentrant then we cannot */
   /* have parameters                                   */
-  if (func->type != EX_VALUE && !IFFUNC_ISREENT (func->ftype) && !options.stackAuto)
+  if (func->type != EX_VALUE && !IFFUNC_ISREENT (functype) && !options.stackAuto)
     {
       werror (W_NONRENT_ARGS);
+      fatalError++;
       return 1;
     }
 
   /* if defined parameters ended but actual parameters */
   /* exist and this is not defined as a variable arg   */
-  if (!defParm && actParm && !IFFUNC_HASVARARGS(func->ftype))
+  if (!defParm && *actParm && !IFFUNC_HASVARARGS(functype))
     {
-      //if (func->type==EX_VALUE && func->opval.val->sym->undefined)
-      //  return 1; /* Already gave them an undefined function error */
       werror (E_TOO_MANY_PARMS);
       return 1;
     }
 
   /* if defined parameters present but no actual parameters */
-  if (defParm && !actParm)
+  if (defParm && !*actParm)
     {
       werror (E_TOO_FEW_PARMS);
       return 1;
     }
 
-  if (IS_VOID(actParm->ftype)) {
-    werror (E_VOID_VALUE_USED);
-    return 1;
-  }
+  /* if this is a PARAM node then match left & right */
+  if ((*actParm)->type == EX_OP && (*actParm)->opval.op == PARAM)
+    {
+      (*actParm)->decorated = 1;
+      return (processParms (func, defParm,
+                           &(*actParm)->left,  parmNumber, FALSE) ||
+             processParms (func, defParm ? defParm->next : NULL,
+                           &(*actParm)->right, parmNumber, rightmost));
+    }
+  else if (defParm) /* not vararg */
+    {
+      /* If we have found a value node by following only right-hand links,
+       * then we know that there are no more values after us.
+       *
+       * Therefore, if there are more defined parameters, the caller didn't
+       * supply enough.
+       */
+      if (rightmost && defParm->next)
+       {
+         werror (E_TOO_FEW_PARMS);
+         return 1;
+       }
+    }
+
+  /* decorate parameter */
+  resultType = defParm ? getResultTypeFromType (defParm->etype) :
+                         RESULT_TYPE_NONE;
+  *actParm = decorateType (*actParm, resultType);
+
+  if (IS_VOID((*actParm)->ftype))
+    {
+      werror (E_VOID_VALUE_USED);
+      return 1;
+    }
 
   /* If this is a varargs function... */
-  if (!defParm && actParm && IFFUNC_HASVARARGS(func->ftype))
+  if (!defParm && *actParm && IFFUNC_HASVARARGS(functype))
     {
       ast *newType = NULL;
       sym_link *ftype;
 
-      if (IS_CAST_OP (actParm)
-         || (IS_AST_LIT_VALUE (actParm) && actParm->values.literalFromCast))
+      if (IS_CAST_OP (*actParm)
+         || (IS_AST_LIT_VALUE (*actParm) && (*actParm)->values.literalFromCast))
        {
          /* Parameter was explicitly typecast; don't touch it. */
          return 0;
        }
 
-      ftype = actParm->ftype;
-          
-      /* If it's a small integer, upcast to int. */
+      ftype = (*actParm)->ftype;
+
+      /* If it's a char, upcast to int. */
       if (IS_INTEGRAL (ftype)
          && (getSize (ftype) < (unsigned) INTSIZE))
        {
-         if (IS_AST_OP(actParm) &&
-             (actParm->opval.op == LEFT_OP ||
-              actParm->opval.op == '*' ||
-              actParm->opval.op == '+' ||
-              actParm->opval.op == '-') &&
-             actParm->right) {
-           // we should cast an operand instead of the result
-           actParm->decorated = 0;
-           actParm->left = newNode( CAST, newAst_LINK(newIntLink()),
-                                        actParm->left);
-           actParm = decorateType(actParm, RESULT_CHECK);
-         } else {
-           newType = newAst_LINK(INTTYPE);
-         }
+         newType = newAst_LINK(INTTYPE);  
        }
 
       if (IS_PTR(ftype) && !IS_GENPTR(ftype))
@@ -740,83 +768,60 @@ processParms (ast * func,
          newType = newAst_LINK (copyLinkChain (ftype));
          DCL_TYPE (newType->opval.lnk) = port->unqualified_pointer;
        }
+      
       if (newType)
-       {
-         /* cast required; change this op to a cast. */
-         ast *parmCopy = decorateType(resolveSymbols (copyAst (actParm)), RESULT_CHECK);
-
-         actParm->type = EX_OP;
-         actParm->opval.op = CAST;
-         actParm->left = newType;
-         actParm->right = parmCopy;
-         decorateType (actParm, RESULT_CHECK);
-       }
-      else if (actParm->type == EX_OP && actParm->opval.op == PARAM)
-       {
-         return (processParms (func, NULL, actParm->left, parmNumber, FALSE) ||
-         processParms (func, NULL, actParm->right, parmNumber, rightmost));
-       }
+        {
+          /* cast required; change this op to a cast. */
+          (*actParm)->decorated = 0;
+           *actParm = newNode (CAST, newType, *actParm);
+          (*actParm)->lineno = (*actParm)->right->lineno;
+          
+          decorateType (*actParm, RESULT_TYPE_NONE);
+        }
       return 0;
-    }
+    } /* vararg */
 
   /* if defined parameters ended but actual has not & */
   /* reentrant */
-  if (!defParm && actParm &&
-      (options.stackAuto || IFFUNC_ISREENT (func->ftype)))
+  if (!defParm && *actParm &&
+      (options.stackAuto || IFFUNC_ISREENT (functype)))
     return 0;
 
-  resolveSymbols (actParm);
-  /* if this is a PARAM node then match left & right */
-  if (actParm->type == EX_OP && actParm->opval.op == PARAM)
-    {
-      return (processParms (func, defParm, actParm->left, parmNumber, FALSE) ||
-             processParms (func, defParm->next, actParm->right, parmNumber, rightmost));
-    }
-  else
+  resolveSymbols (*actParm);
+  
+  /* the parameter type must be at least castable */
+  if (compareType (defParm->type, (*actParm)->ftype) == 0)
     {
-      /* If we have found a value node by following only right-hand links,
-       * then we know that there are no more values after us.
-       *
-       * Therefore, if there are more defined parameters, the caller didn't
-       * supply enough.
-       */
-      if (rightmost && defParm->next)
-       {
-         werror (E_TOO_FEW_PARMS);
-         return 1;
-       }
+      werror (E_INCOMPAT_TYPES);
+      printFromToType ((*actParm)->ftype, defParm->type);
+      return 1;
     }
 
-  /* the parameter type must be at least castable */
-  if (compareType (defParm->type, actParm->ftype) == 0) {
-    werror (E_INCOMPAT_TYPES);
-    printFromToType (actParm->ftype, defParm->type);
-    return 1;
-  }
-
   /* if the parameter is castable then add the cast */
-  if (compareType (defParm->type, actParm->ftype) < 0)
+  if (compareType (defParm->type, (*actParm)->ftype) < 0)
     {
-      ast *pTree = decorateType(resolveSymbols (copyAst (actParm)), RESULT_CHECK);
+      ast *pTree;
 
+      resultType = getResultTypeFromType (defParm->etype);
+      pTree = resolveSymbols (copyAst (*actParm));
+      
       /* now change the current one to a cast */
-      actParm->type = EX_OP;
-      actParm->opval.op = CAST;
-      actParm->left = newAst_LINK (defParm->type);
-      actParm->right = pTree;
-      actParm->etype = defParm->etype;
-      actParm->ftype = defParm->type;
-      actParm->decorated=0; /* force typechecking */
-      decorateType (actParm, RESULT_CHECK);
+      (*actParm)->type = EX_OP;
+      (*actParm)->opval.op = CAST;
+      (*actParm)->left = newAst_LINK (defParm->type);
+      (*actParm)->right = pTree;
+      (*actParm)->decorated = 0; /* force typechecking */
+      decorateType (*actParm, resultType);
     }
 
   /* make a copy and change the regparm type to the defined parm */
-  actParm->etype = getSpec (actParm->ftype = copyLinkChain (actParm->ftype));
-  SPEC_REGPARM (actParm->etype) = SPEC_REGPARM (defParm->etype);
-  SPEC_ARGREG  (actParm->etype) = SPEC_ARGREG (defParm->etype);
+  (*actParm)->etype = getSpec ((*actParm)->ftype = copyLinkChain ((*actParm)->ftype));
+  SPEC_REGPARM ((*actParm)->etype) = SPEC_REGPARM (defParm->etype);
+  SPEC_ARGREG  ((*actParm)->etype) = SPEC_ARGREG (defParm->etype);
   (*parmNumber)++;
   return 0;
 }
+
 /*-----------------------------------------------------------------*/
 /* createIvalType - generates ival for basic types                 */
 /*-----------------------------------------------------------------*/
@@ -865,7 +870,7 @@ createIvalStruct (ast * sym, sym_link * type, initList * ilist)
     }
 
   if (iloop) {
-    werrorfl (filename, sym->opval.val->sym->lineDef,
+    werrorfl (sym->opval.val->sym->fileDef, sym->opval.val->sym->lineDef,
              W_EXCESS_INITIALIZERS, "struct", 
              sym->opval.val->sym->name);
   }
@@ -926,6 +931,7 @@ createIvalArray (ast * sym, sym_link * type, initList * ilist)
            // Array size was specified, and we have more initializers than needed.
            char *name=sym->opval.val->sym->name;
            int lineno=sym->opval.val->sym->lineDef;
+           char *filename=sym->opval.val->sym->fileDef;
            
            werrorfl (filename, lineno, W_EXCESS_INITIALIZERS, "array", name);
        }
@@ -952,6 +958,7 @@ createIvalArray (ast * sym, sym_link * type, initList * ilist)
                // there has to be a better way
                char *name=sym->opval.val->sym->name;
                int lineno=sym->opval.val->sym->lineDef;
+               char *filename=sym->opval.val->sym->fileDef;
                werrorfl (filename, lineno, W_EXCESS_INITIALIZERS, "array", name);
                
                break;
@@ -1105,7 +1112,7 @@ gatherAutoInit (symbol * autoChain)
 
       /* resolve the symbols in the ival */
       if (sym->ival)
-       resolveIvalSym (sym->ival);
+       resolveIvalSym (sym->ival, sym->type);
 
       /* if this is a static variable & has an */
       /* initial value the code needs to be lifted */
@@ -1126,7 +1133,7 @@ gatherAutoInit (symbol * autoChain)
            work = initAggregates (sym, sym->ival, NULL);
          } else {
            if (getNelements(sym->type, sym->ival)>1) {
-             werrorfl (filename, sym->lineDef,
+             werrorfl (sym->fileDef, sym->lineDef,
                        W_EXCESS_INITIALIZERS, "scalar", 
                        sym->name);
            }
@@ -1162,7 +1169,7 @@ gatherAutoInit (symbol * autoChain)
            work = initAggregates (sym, sym->ival, NULL);
          } else {
            if (getNelements(sym->type, sym->ival)>1) {
-             werrorfl (filename, sym->lineDef,
+             werrorfl (sym->fileDef, sym->lineDef,
                        W_EXCESS_INITIALIZERS, "scalar", 
                        sym->name);
            }
@@ -1971,7 +1978,7 @@ searchLitOp (ast *tree, ast **parent, const char *ops)
           tree->right->right &&
           (tree->right->opval.op == ops[0] || tree->right->opval.op == ops[1]))
        {
-         if (IS_LITERAL (RTYPE (tree->right)) ^
+         if (IS_LITERAL (RTYPE (tree->right)) !=
              IS_LITERAL (LTYPE (tree->right)))
            {
              tree->right->decorated = 0;
@@ -1988,7 +1995,7 @@ searchLitOp (ast *tree, ast **parent, const char *ops)
           tree->left->right &&
          (tree->left->opval.op == ops[0] || tree->left->opval.op == ops[1]))
        {
-         if (IS_LITERAL (RTYPE (tree->left)) ^
+         if (IS_LITERAL (RTYPE (tree->left)) !=
              IS_LITERAL (LTYPE (tree->left)))
            {
              tree->left->decorated = 0;
@@ -2007,14 +2014,22 @@ searchLitOp (ast *tree, ast **parent, const char *ops)
 /*-----------------------------------------------------------------*/
 /* getResultFromType                                               */
 /*-----------------------------------------------------------------*/
-static RESULT_TYPE
+RESULT_TYPE
 getResultTypeFromType (sym_link *type)
 {
   /* type = getSpec (type); */
-  if (IS_BITVAR (type))
+  if (IS_BIT (type))
     return RESULT_TYPE_BIT;
   if (IS_BITFIELD (type))
-    return RESULT_TYPE_CHAR;
+    {
+      int blen = SPEC_BLEN (type);
+      
+      if (blen <= 1)
+        return RESULT_TYPE_BIT;
+      if (blen <= 8)
+        return RESULT_TYPE_CHAR;
+      return RESULT_TYPE_INT;
+    }
   if (IS_CHAR (type))
     return RESULT_TYPE_CHAR;
   if (   IS_INT (type)
@@ -2026,21 +2041,31 @@ getResultTypeFromType (sym_link *type)
 /*-----------------------------------------------------------------*/
 /* addCast - adds casts to a type specified by RESULT_TYPE         */
 /*-----------------------------------------------------------------*/
-static void
-addCast (ast **tree, RESULT_TYPE resultType, bool upcast)
+static ast *
+addCast (ast *tree, RESULT_TYPE resultType, bool upcast)
 {
   sym_link *newLink;
+  bool upCasted = FALSE;
   
   switch (resultType)
     {
+      case RESULT_TYPE_NONE:
+       /* char: promote to int */
+       if (!upcast ||
+           getSize (tree->etype) >= INTSIZE)
+         return tree;
+       newLink = newIntLink();
+       upCasted = TRUE;
+       break;
       case RESULT_TYPE_CHAR:
-       if (getSize ((*tree)->etype) <= 1)
-         return;
+       if (IS_CHAR (tree->etype) ||
+           IS_FLOAT(tree->etype))
+         return tree;
        newLink = newCharLink();
        break;
       case RESULT_TYPE_INT:
 #if 0
-       if (getSize ((*tree)->etype) > INTSIZE)
+       if (getSize (tree->etype) > INTSIZE)
           {
             /* warn ("Loosing significant digits"); */
            return;
@@ -2048,26 +2073,31 @@ addCast (ast **tree, RESULT_TYPE resultType, bool upcast)
 #endif
        /* char: promote to int */
        if (!upcast ||
-           getSize ((*tree)->etype) >= INTSIZE)
-         return;
+           getSize (tree->etype) >= INTSIZE)
+         return tree;
        newLink = newIntLink();
+       upCasted = TRUE;
        break;
       case RESULT_TYPE_OTHER:
        if (!upcast)
-         return;
-        /* long, float: promote to int */
-       if (getSize ((*tree)->etype) >= INTSIZE)
-         return;
+         return tree;
+        /* return type is long, float: promote char to int */
+       if (getSize (tree->etype) >= INTSIZE)
+         return tree;
        newLink = newIntLink();
+       upCasted = TRUE;
        break;
       default:
-       return;
+       return tree;
     }
-  (*tree)->decorated = 0;
-  *tree = newNode (CAST, newAst_LINK (newLink), *tree);
-  /* keep unsigned type */
-  SPEC_USIGN ((*tree)->left->opval.lnk) = IS_UNSIGNED ((*tree)->right->etype) ? 1 : 0;
-  *tree = decorateType (*tree, resultType);
+  tree->decorated = 0;
+  tree = newNode (CAST, newAst_LINK (newLink), tree);
+  tree->lineno = tree->right->lineno;
+  /* keep unsigned type during cast to smaller type,
+     but not when promoting from char to int */
+  if (!upCasted)
+    SPEC_USIGN (tree->left->opval.lnk) = IS_UNSIGNED (tree->right->etype) ? 1 : 0;
+  return decorateType (tree, resultType);
 }
 
 /*-----------------------------------------------------------------*/
@@ -2083,6 +2113,11 @@ resultTypePropagate (ast *tree, RESULT_TYPE resultType)
       case ':':
       case '|':
       case '^':
+      case '~':
+      case '*':
+      case '+':
+      case '-':
+      case LABEL:
        return resultType;
       case '&':
        if (!tree->right)
@@ -2090,6 +2125,8 @@ resultTypePropagate (ast *tree, RESULT_TYPE resultType)
          return RESULT_TYPE_NONE;
        else
          return resultType;
+      case IFX:
+       return RESULT_TYPE_IFX;
       default:
        return RESULT_TYPE_NONE;
     }
@@ -2105,15 +2142,21 @@ getLeftResultType (ast *tree, RESULT_TYPE resultType)
     {
       case '=':
       case CAST:
-       if (IS_PTR (tree->left->ftype))
+       if (IS_PTR (LTYPE (tree)))
          return RESULT_TYPE_NONE;
        else
-         return getResultTypeFromType (tree->left->etype);
+         return getResultTypeFromType (LETYPE (tree));
       case RETURN:
        if (IS_PTR (currFunc->type->next))
          return RESULT_TYPE_NONE;
        else
          return getResultTypeFromType (currFunc->type->next);
+      case '[':
+       if (!IS_ARRAY (LTYPE (tree)))
+         return resultType;
+       if (DCL_ELEM (LTYPE (tree)) > 0 && DCL_ELEM (LTYPE (tree)) <= 256)
+         return RESULT_TYPE_CHAR;
+       return resultType;
       default:
        return resultType;
     }
@@ -2251,23 +2294,47 @@ decorateType (ast * tree, RESULT_TYPE resultType)
 
     /* Before decorating the left branch we've to decide in dependence
        upon tree->opval.op, if resultType can be propagated */
-    if (getenv ("SDCC_NEWTYPEFLOW"))
-      resultTypeProp = resultTypePropagate (tree, resultType);
+    resultTypeProp = resultTypePropagate (tree, resultType);
+
+    if (tree->opval.op == '?')
+      dtl = decorateType (tree->left, RESULT_TYPE_IFX);
     else
-      resultTypeProp = RESULT_TYPE_NONE; /* provide initialization */
+      dtl = decorateType (tree->left, resultTypeProp);
 
-    dtl = decorateType (tree->left, resultTypeProp);
+    /* if an array node, we may need to swap branches */
+    if (tree->opval.op == '[')
+      {
+        /* determine which is the array & which the index */
+        if ((IS_ARRAY (RTYPE (tree)) || IS_PTR (RTYPE (tree))) &&
+           IS_INTEGRAL (LTYPE (tree)))
+         {
+           ast *tempTree = tree->left;
+           tree->left = tree->right;
+           tree->right = tempTree;
+         }
+      }
 
     /* After decorating the left branch there's type information available
        in tree->left->?type. If the op is e.g. '=' we extract the type
        information from there and propagate it to the right branch. */
-    if (getenv ("SDCC_NEWTYPEFLOW"))
-      resultTypeProp = getLeftResultType (tree, resultTypeProp);
+    resultTypeProp = getLeftResultType (tree, resultTypeProp);
     
-    /* delay right side for '?' operator since conditional macro expansions
-       might rely on this */
-    dtr = tree->opval.op == '?' ? tree->right :
-                                  decorateType (tree->right, resultTypeProp);
+    switch (tree->opval.op)
+      {
+        case '?':
+         /* delay right side for '?' operator since conditional macro
+            expansions might rely on this */
+         dtr = tree->right;
+         break;
+       case CALL: 
+         /* decorate right side for CALL (parameter list) in processParms();
+            there is resultType available */
+         dtr = tree->right;
+         break;
+       default:     
+         dtr = decorateType (tree->right, resultTypeProp);
+         break;
+      }
 
     /* this is to take care of situations
        when the tree gets rewritten */
@@ -2277,29 +2344,6 @@ decorateType (ast * tree, RESULT_TYPE resultType)
       tree->right = dtr;
     if ((dtl && dtl->isError) || (dtr && dtr->isError))
       return tree;
-
-    if (!getenv ("SDCC_NEWTYPEFLOW"))
-      {
-        if (IS_AST_OP(tree) &&
-           (tree->opval.op == CAST || tree->opval.op == '=') &&
-           (getSize(LTYPE(tree)) > getSize(RTYPE(tree))) &&
-           (getSize(RTYPE(tree)) < (unsigned) INTSIZE)) {
-          /* this is a cast/assign to a bigger type */
-         if (IS_AST_OP(tree->right) &&
-             IS_INTEGRAL(tree->right->ftype) &&
-             (tree->right->opval.op == LEFT_OP ||
-              tree->right->opval.op == '*' ||
-              tree->right->opval.op == '+' ||
-              tree->right->opval.op == '-') &&
-              tree->right->right) {
-           /* we should cast an operand instead of the result */
-           tree->right->decorated = 0;
-           tree->right->left = newNode( CAST, newAst_LINK(newIntLink()),
-                                        tree->right->left);
-           tree->right = decorateType(tree->right, RESULT_CHECK);
-          }
-        }
-      }
   }
 
   /* depending on type of operator do */
@@ -2312,15 +2356,6 @@ decorateType (ast * tree, RESULT_TYPE resultType)
        /*----------------------------*/
     case '[':
 
-      /* determine which is the array & which the index */
-      if ((IS_ARRAY (RTYPE (tree)) || IS_PTR (RTYPE (tree))) && IS_INTEGRAL (LTYPE (tree)))
-       {
-
-         ast *tempTree = tree->left;
-         tree->left = tree->right;
-         tree->right = tempTree;
-       }
-
       /* first check if this is a array or a pointer */
       if ((!IS_ARRAY (LTYPE (tree))) && (!IS_PTR (LTYPE (tree))))
        {
@@ -2341,6 +2376,17 @@ decorateType (ast * tree, RESULT_TYPE resultType)
          werror (E_LVALUE_REQUIRED, "array access");
          goto errorTreeReturn;
        }
+
+      if (IS_LITERAL (RTYPE (tree)))
+       {
+         int arrayIndex = (int) floatFromVal (valFromType (RETYPE (tree)));
+         int arraySize = DCL_ELEM (LTYPE (tree));
+         if (arraySize && arrayIndex >= arraySize)
+           {
+             werror (W_IDX_OUT_OF_BOUNDS, arrayIndex, arraySize);
+           }
+       }
+
       RRVAL (tree) = 1;
       COPYTYPE (TTYPE (tree), TETYPE (tree), LTYPE (tree)->next);
       return tree;
@@ -2517,12 +2563,10 @@ decorateType (ast * tree, RESULT_TYPE resultType)
              return decorateType (otree, RESULT_CHECK);
          }
 
-         if (getenv ("SDCC_NEWTYPEFLOW"))
-           {
-             addCast (&tree->left,  resultType, FALSE);
-             addCast (&tree->right, resultType, FALSE);
-           }
-         TTYPE (tree) = computeType (LTYPE (tree), RTYPE (tree), FALSE);
+         TTYPE (tree) = computeType (LTYPE (tree),
+                                     RTYPE (tree),
+                                     resultType,
+                                     tree->opval.op);
          TETYPE (tree) = getSpec (TTYPE (tree));
 
           /* if left is a literal exchange left & right */
@@ -2542,11 +2586,12 @@ decorateType (ast * tree, RESULT_TYPE resultType)
              ast *litTree = searchLitOp (tree, &parent, "&");
              if (litTree)
                {
-                 ast *tTree = litTree->left;
+                 DEBUG_CF("&")
+                 ast *tTree = litTree->left;
                  litTree->left = tree->right;
                  tree->right = tTree;
                  /* both operands in tTree are literal now */
-                 decorateType (parent, RESULT_CHECK);
+                 decorateType (parent, resultType);
                }
            }
 
@@ -2669,11 +2714,12 @@ decorateType (ast * tree, RESULT_TYPE resultType)
          ast *litTree = searchLitOp (tree, &parent, "|");
          if (litTree)
            {
+             DEBUG_CF("|")
              ast *tTree = litTree->left;
              litTree->left = tree->right;
              tree->right = tTree;
              /* both operands in tTree are literal now */
-             decorateType (parent, RESULT_CHECK);
+             decorateType (parent, resultType);
            }
         }
       /* fall through */
@@ -2726,24 +2772,21 @@ decorateType (ast * tree, RESULT_TYPE resultType)
          ast *litTree = searchLitOp (tree, &parent, "^");
          if (litTree)
            {
+             DEBUG_CF("^")
              ast *tTree = litTree->left;
              litTree->left = tree->right;
              tree->right = tTree;
              /* both operands in litTree are literal now */
-             decorateType (parent, RESULT_CHECK);
+             decorateType (parent, resultType);
            }
         }
 
       LRVAL (tree) = RRVAL (tree) = 1;
-      if (getenv ("SDCC_NEWTYPEFLOW"))
-        {
-          addCast (&tree->left,  resultType, FALSE);
-          addCast (&tree->right, resultType, FALSE);
-        }
       TETYPE (tree) = getSpec (TTYPE (tree) =
                               computeType (LTYPE (tree),
                                            RTYPE (tree),
-                                           FALSE));
+                                           resultType,
+                                           tree->opval.op));
 
       return tree;
 
@@ -2771,10 +2814,12 @@ decorateType (ast * tree, RESULT_TYPE resultType)
        }
 
       LRVAL (tree) = RRVAL (tree) = 1;
+
       TETYPE (tree) = getSpec (TTYPE (tree) =
                               computeType (LTYPE (tree),
                                            RTYPE (tree),
-               ! (IS_UNSIGNED (LTYPE (tree)) && IS_UNSIGNED (RTYPE (tree)))));
+                                           resultType,
+                                           tree->opval.op));
 
       /* if right is a literal and */
       /* left is also a division by a literal then */
@@ -2790,11 +2835,14 @@ decorateType (ast * tree, RESULT_TYPE resultType)
              if (IS_LITERAL (RTYPE (litTree)))
                {
                  /* foo_div */
-                 litTree->right = newNode ('*', litTree->right, tree->right);
+                 DEBUG_CF("div r")
+                 litTree->right = newNode ('*',
+                                           litTree->right,
+                                           copyAst (tree->right));
                  litTree->right->lineno = tree->lineno;
 
                  tree->right->opval.val = constVal ("1");
-                 decorateType (parent, RESULT_CHECK);
+                 decorateType (parent, resultType);
                }
              else
                {
@@ -2802,7 +2850,7 @@ decorateType (ast * tree, RESULT_TYPE resultType)
                     We can't call decorateType(parent, RESULT_CHECK), because
                     this would cause an infinit loop. */
                  parent->decorated = 1;
-                 decorateType (litTree, RESULT_CHECK);
+                 decorateType (litTree, resultType);
                }
            }
        }
@@ -2840,7 +2888,8 @@ decorateType (ast * tree, RESULT_TYPE resultType)
       TETYPE (tree) = getSpec (TTYPE (tree) =
                               computeType (LTYPE (tree),
                                            RTYPE (tree),
-               ! (IS_UNSIGNED (LTYPE (tree)) && IS_UNSIGNED (RTYPE (tree)))));
+                                           resultType,
+                                           tree->opval.op));
       return tree;
 
       /*------------------------------------------------------------------*/
@@ -2941,30 +2990,24 @@ decorateType (ast * tree, RESULT_TYPE resultType)
          ast *litTree = searchLitOp (tree, &parent, "*");
          if (litTree)
            {
+             DEBUG_CF("mul")
              ast *tTree = litTree->left;
              litTree->left = tree->right;
              tree->right = tTree;
              /* both operands in litTree are literal now */
-             decorateType (parent, RESULT_CHECK);
+             decorateType (parent, resultType);
            }
         }
 
       LRVAL (tree) = RRVAL (tree) = 1;
-      if (!getenv ("SDCC_NEWTYPEFLOW"))
-        TETYPE (tree) = getSpec (TTYPE (tree) =
-                                computeType (LTYPE (tree),
-                                             RTYPE (tree),
-                                             TRUE));
-      else
-       {
-          addCast (&tree->left,  resultType, FALSE);
-          addCast (&tree->right, resultType, FALSE);
-         TETYPE (tree) = getSpec (TTYPE (tree) =
+      tree->left  = addCast (tree->left,  resultType, FALSE);
+      tree->right = addCast (tree->right, resultType, FALSE);
+      TETYPE (tree) = getSpec (TTYPE (tree) =
                                   computeType (LTYPE (tree),
                                                RTYPE (tree),
-                              resultType == RESULT_TYPE_CHAR ? FALSE : TRUE));
-       }
-      
+                                               resultType,
+                                               tree->opval.op));
+
       return tree;
 
       /*------------------------------------------------------------------*/
@@ -3027,6 +3070,8 @@ decorateType (ast * tree, RESULT_TYPE resultType)
       if (IS_LITERAL (RTYPE (tree)) && IS_LITERAL (LTYPE (tree)))
        {
          tree->type = EX_VALUE;
+         tree->left  = addCast (tree->left,  resultType, TRUE);
+          tree->right = addCast (tree->right, resultType, TRUE);
          tree->opval.val = valPlus (valFromType (LETYPE (tree)),
                                     valFromType (RETYPE (tree)));
          tree->right = tree->left = NULL;
@@ -3058,6 +3103,7 @@ decorateType (ast * tree, RESULT_TYPE resultType)
              if (litTree->opval.op == '+')
                {
                  /* foo_aa */
+                 DEBUG_CF("+ 1 AA")
                  ast *tTree = litTree->left;
                  litTree->left = tree->right;
                  tree->right = tree->left;
@@ -3067,6 +3113,7 @@ decorateType (ast * tree, RESULT_TYPE resultType)
                {
                  if (IS_LITERAL (RTYPE (litTree)))
                    {
+                     DEBUG_CF("+ 2 ASR")
                      /* foo_asr */
                      ast *tTree = litTree->left;
                      litTree->left = tree->right;
@@ -3074,6 +3121,7 @@ decorateType (ast * tree, RESULT_TYPE resultType)
                    }
                  else
                    {
+                     DEBUG_CF("+ 3 ASL")
                      /* foo_asl */
                      ast *tTree = litTree->right;
                      litTree->right = tree->right;
@@ -3082,7 +3130,7 @@ decorateType (ast * tree, RESULT_TYPE resultType)
                      tree->opval.op = '-';
                    }
                }
-             decorateType (parent, RESULT_CHECK);
+             decorateType (parent, resultType);
            }
        }
 
@@ -3092,20 +3140,15 @@ decorateType (ast * tree, RESULT_TYPE resultType)
        TETYPE (tree) = getSpec (TTYPE (tree) =
                                 LTYPE (tree));
       else
-        if (!getenv ("SDCC_NEWTYPEFLOW"))
+       {
+         tree->left  = addCast (tree->left,  resultType, TRUE);
+          tree->right = addCast (tree->right, resultType, TRUE);
           TETYPE (tree) = getSpec (TTYPE (tree) =
-                                  computeType (LTYPE (tree),
-                                               RTYPE (tree),
-                                               FALSE));
-       else
-          {
-           addCast (&tree->left,  resultType, TRUE);
-            addCast (&tree->right, resultType, TRUE);
-            TETYPE (tree) = getSpec (TTYPE (tree) =
                                     computeType (LTYPE (tree),
-                                                 RTYPE (tree),
-                              resultType == RESULT_TYPE_CHAR ? FALSE : TRUE));
-          }
+                                                 RTYPE (tree),
+                                                 resultType,
+                                                 tree->opval.op));
+       }
        
       return tree;
 
@@ -3173,6 +3216,8 @@ decorateType (ast * tree, RESULT_TYPE resultType)
       if (IS_LITERAL (RTYPE (tree)) && IS_LITERAL (LTYPE (tree)))
        {
          tree->type = EX_VALUE;
+         tree->left  = addCast (tree->left,  resultType, TRUE);
+         tree->right = addCast (tree->right, resultType, TRUE);
          tree->opval.val = valMinus (valFromType (LETYPE (tree)),
                                      valFromType (RETYPE (tree)));
          tree->right = tree->left = NULL;
@@ -3203,20 +3248,16 @@ decorateType (ast * tree, RESULT_TYPE resultType)
        TETYPE (tree) = getSpec (TTYPE (tree) =
                                 LTYPE (tree));
       else
-        if (!getenv ("SDCC_NEWTYPEFLOW"))
-          TETYPE (tree) = getSpec (TTYPE (tree) =
-                                  computeType (LTYPE (tree),
-                                               RTYPE (tree),
-                                               FALSE));
-       else
-         {
-           addCast (&tree->left,  resultType, TRUE);
-           addCast (&tree->right, resultType, TRUE);
-           TETYPE (tree) = getSpec (TTYPE (tree) =
+       {
+         tree->left  = addCast (tree->left,  resultType, TRUE);
+         tree->right = addCast (tree->right, resultType, TRUE);
+
+         TETYPE (tree) = getSpec (TTYPE (tree) =
                                     computeType (LTYPE (tree),
-                                                 RTYPE (tree),
-                              resultType == RESULT_TYPE_CHAR ? FALSE : TRUE));
-         }
+                                                 RTYPE (tree),
+                                                 resultType,
+                                                 tree->opval.op));
+       }
 
       LRVAL (tree) = RRVAL (tree) = 1;
 
@@ -3234,30 +3275,39 @@ decorateType (ast * tree, RESULT_TYPE resultType)
              if (litTree->opval.op == '+')
                {
                  /* foo_sa */
-                 litTree->right = newNode ('-', litTree->right, tree->right);
-                 litTree->right->lineno = tree->lineno;
-
-                 tree->right->opval.val = constVal ("0");
+                 DEBUG_CF("- 1 SA")
+                 ast *tTree = litTree->left;
+                 litTree->left = litTree->right;
+                 litTree->right = tree->right;
+                 tree->right = tTree;
+                 tree->opval.op = '+';
+                 litTree->opval.op = '-';
                }
              else if (litTree->opval.op == '-')
                {
                  if (IS_LITERAL (RTYPE (litTree)))
                    {
                      /* foo_ssr */
-                     litTree->right = newNode ('+', tree->right, litTree->right);
-                     litTree->right->lineno = tree->lineno;
-
-                     tree->right->opval.val = constVal ("0");
+                     DEBUG_CF("- 2 SSR")
+                     ast *tTree = litTree->left;
+                     litTree->left = tree->right;
+                     tree->right = litParent->left;
+                     litParent->left = tTree;
+                     litTree->opval.op = '+';
+                     
+                     tree->decorated = 0;
+                     decorateType (tree, resultType);
                    }
                  else
                    {
                      /* foo_ssl */
+                     DEBUG_CF("- 3 SSL")
                      ast *tTree = litTree->right;
                      litTree->right = tree->right;
                      tree->right = tTree;
                    }
                }
-             decorateType (litParent, RESULT_CHECK);
+             decorateType (litParent, resultType);
            }
        }
       return tree;
@@ -3276,13 +3326,14 @@ decorateType (ast * tree, RESULT_TYPE resultType)
 
       /* if left is a literal then do it */
       if (IS_LITERAL (LTYPE (tree)))
-       {
-         tree->type = EX_VALUE;
-         tree->opval.val = valComplement (valFromType (LETYPE (tree)));
-         tree->left = NULL;
-         TETYPE (tree) = TTYPE (tree) = tree->opval.val->type;
-         return tree;
-       }
+        {
+          tree->type = EX_VALUE;
+          tree->opval.val = valComplement (valFromType (LETYPE (tree)));
+          tree->left = NULL;
+          TETYPE (tree) = TTYPE (tree) = tree->opval.val->type;
+          return addCast (tree, resultType, TRUE);
+        }
+      tree->left = addCast (tree->left, resultType, TRUE);
       LRVAL (tree) = 1;
       COPYTYPE (TTYPE (tree), TETYPE (tree), LTYPE (tree));
       return tree;
@@ -3342,6 +3393,10 @@ decorateType (ast * tree, RESULT_TYPE resultType)
          goto errorTreeReturn;
        }
 
+      /* make smaller type only if it's a LEFT_OP */
+      if (tree->opval.op == LEFT_OP)
+        tree->left = addCast (tree->left, resultType, TRUE);
+      
       /* if they are both literal then */
       /* rewrite the tree */
       if (IS_LITERAL (RTYPE (tree)) && IS_LITERAL (LTYPE (tree)))
@@ -3359,20 +3414,11 @@ decorateType (ast * tree, RESULT_TYPE resultType)
       LRVAL (tree) = RRVAL (tree) = 1;
       if (tree->opval.op == LEFT_OP)
        {
-         if (!getenv ("SDCC_NEWTYPEFLOW"))
-           /* promote char to int */
-           TETYPE (tree) = getSpec (TTYPE (tree) =
-                                    computeType (LTYPE (tree),
-                                                 LTYPE (tree), /* no, not RTYPE! */
-                                                 TRUE));
-         else
-           {
-             addCast (&tree->left,  resultType, TRUE);
-             TETYPE (tree) = getSpec (TTYPE (tree) =
+         TETYPE (tree) = getSpec (TTYPE (tree) =
                                       computeType (LTYPE (tree),
-                                                   RTYPE (tree),
-                              resultType == RESULT_TYPE_CHAR ? FALSE : TRUE));
-           }
+                                                   NULL,
+                                                   resultType,
+                                                   tree->opval.op));
        }
       else /* RIGHT_OP */
        {
@@ -3591,7 +3637,7 @@ decorateType (ast * tree, RESULT_TYPE resultType)
       /*----------------------------*/
     case AND_OP:
     case OR_OP:
-      /* each must me arithmetic type or be a pointer */
+      /* each must be arithmetic type or be a pointer */
       if (!IS_PTR (LTYPE (tree)) &&
          !IS_ARRAY (LTYPE (tree)) &&
          !IS_INTEGRAL (LTYPE (tree)))
@@ -3693,8 +3739,14 @@ decorateType (ast * tree, RESULT_TYPE resultType)
            }
          if (tree->opval.op == '>')
            {
-             /* if the parent is an ifx, then we could do */
-             /* return tree->left; */
+             if (resultType == RESULT_TYPE_IFX)
+               {
+                 /* the parent is an ifx: */
+                 /* if (unsigned value) */
+                 return tree->left;
+               }
+             
+             /* (unsigned value) ? 1 : 0 */
              tree->opval.op = '?';
              tree->right = newNode (':',
                                     newAst_VALUE (constVal ("1")),
@@ -3728,8 +3780,13 @@ decorateType (ast * tree, RESULT_TYPE resultType)
       /*----------------------------*/
     case SIZEOF:               /* evaluate wihout code generation */
       /* change the type to a integer */
+      {
+       int size = getSize (tree->right->ftype);
+       SNPRINTF(buffer, sizeof(buffer), "%d", size);
+       if (!size && !IS_VOID(tree->right->ftype))
+         werrorfl (tree->filename, tree->lineno, E_SIZEOF_INCOMPLETE_TYPE);
+      }
       tree->type = EX_VALUE;
-      SNPRINTF(buffer, sizeof(buffer), "%d", (getSize (tree->right->ftype)));
       tree->opval.val = constVal (buffer);
       tree->right = tree->left = NULL;
       TETYPE (tree) = getSpec (TTYPE (tree) =
@@ -3844,7 +3901,8 @@ decorateType (ast * tree, RESULT_TYPE resultType)
          goto errorTreeReturn;
        }
 
-      TTYPE (tree) = computeType (LTYPE (tree), RTYPE (tree), FALSE);
+      TTYPE (tree) = computeType (LTYPE (tree), RTYPE (tree),
+                                  resultType, tree->opval.op);
       TETYPE (tree) = getSpec (TTYPE (tree));
       return tree;
 
@@ -3927,7 +3985,8 @@ decorateType (ast * tree, RESULT_TYPE resultType)
       TETYPE (tree) = getSpec (TTYPE (tree) =
                               computeType (LTYPE (tree),
                                            RTYPE (tree),
-                                           FALSE));
+                                           RESULT_TYPE_NOPROM,
+                                           tree->opval.op));
 
       if (!tree->initMode && IS_CONSTANT (LETYPE (tree)))
        werror (E_CODE_WRITE, "-=");
@@ -3969,7 +4028,8 @@ decorateType (ast * tree, RESULT_TYPE resultType)
       TETYPE (tree) = getSpec (TTYPE (tree) =
                               computeType (LTYPE (tree),
                                            RTYPE (tree),
-                                           FALSE));
+                                           RESULT_TYPE_NOPROM,
+                                           tree->opval.op));
 
       if (!tree->initMode && IS_CONSTANT (LETYPE (tree)))
        werror (E_CODE_WRITE, "+=");
@@ -4042,26 +4102,50 @@ decorateType (ast * tree, RESULT_TYPE resultType)
       /*       function call        */
       /*----------------------------*/
     case CALL:
-      parmNumber = 1;
-
-      if (processParms (tree->left,
-                       FUNC_ARGS(tree->left->ftype),
-                       tree->right, &parmNumber, TRUE)) {
-       goto errorTreeReturn;
-      }
+      
+      /* undo any explicit pointer derefernce; PCALL will handle it instead */
+      if (IS_FUNC (LTYPE (tree)) && tree->left->type == EX_OP)
+        {
+         if (tree->left->opval.op == '*' && !tree->left->right)
+           tree->left = tree->left->left;
+       }
 
-      if ((options.stackAuto || IFFUNC_ISREENT (LTYPE (tree))) && 
-         !IFFUNC_ISBUILTIN(LTYPE(tree)))
+      /* require a function or pointer to function */
+      if (!IS_FUNC (LTYPE (tree))
+          && !(IS_CODEPTR (LTYPE (tree)) && IS_FUNC (LTYPE (tree)->next)))
        {
-         reverseParms (tree->right);
+         werrorfl (tree->filename, tree->lineno, E_FUNCTION_EXPECTED);
+         goto errorTreeReturn;
        }
 
-      if (IS_CODEPTR(LTYPE(tree))) {
-       TTYPE(tree) = LTYPE(tree)->next->next;
-      } else {
-       TTYPE(tree) = LTYPE(tree)->next;
-      }
-      TETYPE (tree) = getSpec (TTYPE (tree));
+      /* if there are parms, make sure that
+         parms are decorate / process / reverse only once */
+      if (!tree->right ||
+          !tree->right->decorated)
+        {
+          sym_link *functype;      
+          parmNumber = 1;
+
+          if (IS_CODEPTR(LTYPE(tree)))
+            functype = LTYPE (tree)->next;
+          else
+            functype = LTYPE (tree);
+
+          if (processParms (tree->left, FUNC_ARGS(functype),
+                            &tree->right, &parmNumber, TRUE))
+            {
+              goto errorTreeReturn;
+            }
+        
+          if ((options.stackAuto || IFFUNC_ISREENT (functype)) && 
+             !IFFUNC_ISBUILTIN(functype))
+            {
+              reverseParms (tree->right);
+            }
+
+           TTYPE (tree) = functype->next;
+           TETYPE (tree) = getSpec (TTYPE (tree));
+        }
       return tree;
 
       /*------------------------------------------------------------------*/
@@ -4166,6 +4250,11 @@ decorateType (ast * tree, RESULT_TYPE resultType)
                                          AST_FOR (tree, loopExpr),
                                          tree->left), RESULT_CHECK);
       }
+    case PARAM:
+      werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
+             "node PARAM shouldn't be processed here");
+             /* but in processParams() */
+      return tree;
     default:
       TTYPE (tree) = TETYPE (tree) = NULL;
       return tree;
@@ -4187,12 +4276,15 @@ value *
 sizeofOp (sym_link * type)
 {
   char buff[10];
+  int size;
 
   /* make sure the type is complete and sane */
   checkTypeSanity(type, "(sizeof)");
 
   /* get the size and convert it to character  */
-  SNPRINTF (buff, sizeof(buff), "%d", getSize (type));
+  SNPRINTF (buff, sizeof(buff), "%d", size = getSize (type));
+  if (!size && !IS_VOID(type))
+    werror (E_SIZEOF_INCOMPLETE_TYPE);
 
   /* now convert into value  */
   return constVal (buff);