Fixed bug #621531 (const & volatile confusion in the type chain).
[fw/sdcc] / src / SDCCast.c
index 2c096d017dcc76bf836cbdd7bb3212cc8125e7fe..c531c8987b58a719d58c9c9b0ac7464305e9a501 100644 (file)
@@ -47,7 +47,7 @@ int labelKey = 1;
 
 int noLineno = 0;
 int noAlloc = 0;
-symbol *currFunc;
+symbol *currFunc=NULL;
 static ast *createIval (ast *, sym_link *, initList *, ast *);
 static ast *createIvalCharPtr (ast *, sym_link *, ast *);
 static ast *optimizeCompare (ast *);
@@ -78,7 +78,7 @@ newAst_ (unsigned type)
   ex = Safe_alloc ( sizeof (ast));
 
   ex->type = type;
-  ex->lineno = (noLineno ? oldLineno : yylineno);
+  ex->lineno = (noLineno ? oldLineno : mylineno);
   ex->filename = currFname;
   ex->level = NestLevel;
   ex->block = currBlockno;
@@ -110,14 +110,6 @@ newAst_LINK (sym_link * val)
   return ex;
 }
 
-ast *
-newAst_STMNT (unsigned val)
-{
-  ast *ex = newAst_ (EX_STMNT);
-  ex->opval.stmnt = val;
-  return ex;
-}
-
 /*-----------------------------------------------------------------*/
 /* newNode - creates a new node                                    */
 /*-----------------------------------------------------------------*/
@@ -1287,9 +1279,12 @@ bool constExprTree (ast *cexpr) {
       if (constExprTree(cexpr->left) && constExprTree(cexpr->right)) {
        return TRUE;
       }
+      return FALSE;
+    case EX_OPERAND:
+      return IS_CONSTANT(operandType(cexpr->opval.oprnd));
     }
   return FALSE;
-}  
+}
     
 /*-----------------------------------------------------------------*/
 /* constExprValue - returns the value of a constant expression     */
@@ -1702,7 +1697,7 @@ isConformingBody (ast * pbody, symbol * sym, ast * body)
          return FALSE;
        // if the loopvar is used in another (maybe conditional) block
        if (astHasSymbol (pbody->right, sym) &&
-           (pbody->level > body->level)) {
+           (pbody->level >= body->level)) {
          return FALSE;
        }
       }
@@ -2002,7 +1997,7 @@ decorateType (ast * tree)
     if (IS_AST_OP(tree) &&
        (tree->opval.op == CAST || tree->opval.op == '=') &&
        (getSize(LTYPE(tree)) > getSize(RTYPE(tree))) &&
-       (getSize(RTYPE(tree)) < INTSIZE)) {
+       (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) &&
@@ -2061,9 +2056,6 @@ decorateType (ast * tree)
        }
       RRVAL (tree) = 1;
       COPYTYPE (TTYPE (tree), TETYPE (tree), LTYPE (tree)->next);
-      if (IS_PTR(LTYPE(tree))) {
-       SPEC_CONST (TETYPE (tree)) = DCL_PTR_CONST (LTYPE(tree));
-      }
       return tree;
 
       /*------------------------------------------------------------------*/
@@ -2139,12 +2131,12 @@ decorateType (ast * tree)
       /*----------------------------*/
       /*  ++/-- operation           */
       /*----------------------------*/
-    case INC_OP:               /* incerement operator unary so left only */
+    case INC_OP:               /* increment operator unary so left only */
     case DEC_OP:
       {
        sym_link *ltc = (tree->right ? RTYPE (tree) : LTYPE (tree));
        COPYTYPE (TTYPE (tree), TETYPE (tree), ltc);
-       if (!tree->initMode && IS_CONSTANT(TETYPE(tree)))
+        if (!tree->initMode && IS_CONSTANT(TTYPE(tree)))
          werror (E_CODE_WRITE, tree->opval.op==INC_OP ? "++" : "--");
 
        if (tree->right)
@@ -2240,10 +2232,7 @@ decorateType (ast * tree)
          goto errorTreeReturn;
        }
       if (SPEC_SCLS (tree->left->etype) == S_CODE)
-       {
-         DCL_TYPE (p) = CPOINTER;
-         DCL_PTR_CONST (p) = port->mem.code_ro;
-       }
+       DCL_TYPE (p) = CPOINTER;
       else if (SPEC_SCLS (tree->left->etype) == S_XDATA)
        DCL_TYPE (p) = FPOINTER;
       else if (SPEC_SCLS (tree->left->etype) == S_XSTACK)
@@ -2266,8 +2255,6 @@ decorateType (ast * tree)
       p->next = LTYPE (tree);
       TTYPE (tree) = p;
       TETYPE (tree) = getSpec (TTYPE (tree));
-      DCL_PTR_CONST (p) = SPEC_CONST (TETYPE (tree));   
-      DCL_PTR_VOLATILE (p) = SPEC_VOLATILE (TETYPE (tree)); 
       LLVAL (tree) = 1;
       TLVAL (tree) = 1;
       return tree;
@@ -2398,8 +2385,7 @@ decorateType (ast * tree)
              goto errorTreeReturn;
            }
          TTYPE (tree) = copyLinkChain (LTYPE (tree)->next);
-         TETYPE (tree) = getSpec (TTYPE (tree));
-         SPEC_CONST (TETYPE (tree)) = DCL_PTR_CONST (LTYPE(tree));
+         TETYPE (tree) = getSpec (TTYPE (tree));
          return tree;
        }
 
@@ -2563,7 +2549,7 @@ decorateType (ast * tree)
              return tree;
            }
          LRVAL (tree) = 1;
-         TTYPE (tree) = LTYPE (tree);
+         TETYPE(tree) = getSpec (TTYPE (tree) = LTYPE (tree));
          return tree;
        }
 
@@ -2786,10 +2772,10 @@ decorateType (ast * tree)
                      tree->right = NULL;
                      TTYPE (tree) = tree->opval.val->type;
                      tree->values.literalFromCast = 1;
-             } else if (IS_GENPTR(LTYPE(tree)) && !IS_PTR(RTYPE(tree)) && 
+             } else if (IS_GENPTR(LTYPE(tree)) && !IS_PTR(RTYPE(tree)) &&
                         ((int)floatFromVal(valFromType(RETYPE(tree)))) !=0 ) /* special case of NULL */  {
                      sym_link *rest = LTYPE(tree)->next;
-                     werror(W_LITERAL_GENERIC);                      
+                     werror(W_LITERAL_GENERIC);
                      TTYPE(tree) = newLink(DECLARATOR);
                      DCL_TYPE(TTYPE(tree)) = FPOINTER;
                      TTYPE(tree)->next = rest;
@@ -2808,7 +2794,7 @@ decorateType (ast * tree)
       /* if pointer to struct then check names */
       if (IS_PTR(LTYPE(tree)) && IS_STRUCT(LTYPE(tree)->next) &&
          IS_PTR(RTYPE(tree)) && IS_STRUCT(RTYPE(tree)->next) &&
-         strcmp(SPEC_STRUCT(LETYPE(tree))->tag,SPEC_STRUCT(RETYPE(tree))->tag)) 
+         strcmp(SPEC_STRUCT(LETYPE(tree))->tag,SPEC_STRUCT(RETYPE(tree))->tag))
        {
          werror(W_CAST_STRUCT_PTR,SPEC_STRUCT(RETYPE(tree))->tag,
                 SPEC_STRUCT(LETYPE(tree))->tag);
@@ -2930,7 +2916,7 @@ decorateType (ast * tree)
        }
       /* if unsigned value < 0  then always false */
       /* if (unsigned value) > 0 then (unsigned value) */
-      if (SPEC_USIGN(LETYPE(tree)) && IS_LITERAL(RTYPE(tree)) && 
+      if (SPEC_USIGN(LETYPE(tree)) && IS_LITERAL(RTYPE(tree))  && 
          ((int) floatFromVal (valFromType (RETYPE (tree)))) == 0) {
 
          if (tree->opval.op == '<') {
@@ -2999,6 +2985,9 @@ decorateType (ast * tree)
                case V_STRUCT:
                    typeofv = TYPEOF_STRUCT;
                    break;
+               case V_BITFIELD:
+                   typeofv = TYPEOF_BITFIELD;
+                   break;
                case V_BIT:
                    typeofv = TYPEOF_BIT;
                    break;
@@ -3232,7 +3221,6 @@ decorateType (ast * tree)
        {
          werror (E_TYPE_MISMATCH, "assignment", " ");
          printFromToType(RTYPE(tree),LTYPE(tree));
-         //goto errorTreeReturn;
        }
 
       /* if the left side of the tree is of type void
@@ -3248,7 +3236,7 @@ decorateType (ast * tree)
       RRVAL (tree) = 1;
       LLVAL (tree) = 1;
       if (!tree->initMode ) {
-       if ((IS_SPEC(LETYPE(tree)) && IS_CONSTANT (LETYPE (tree))))
+        if (IS_CONSTANT(LTYPE(tree)))
          werror (E_CODE_WRITE, "=");
       }
       if (LRVAL (tree))
@@ -3283,12 +3271,15 @@ decorateType (ast * tree)
       if ((options.stackAuto || IFFUNC_ISREENT (LTYPE (tree))) && 
          !IFFUNC_ISBUILTIN(LTYPE(tree)))
        {
-         //FUNC_ARGS(tree->left->ftype) = 
-         //reverseVal (FUNC_ARGS(tree->left->ftype));
          reverseParms (tree->right);
        }
 
-      TETYPE (tree) = getSpec (TTYPE (tree) = LTYPE (tree)->next);
+      if (IS_CODEPTR(LTYPE(tree))) {
+       TTYPE(tree) = LTYPE(tree)->next->next;
+      } else {
+       TTYPE(tree) = LTYPE(tree)->next;
+      }
+      TETYPE (tree) = getSpec (TTYPE (tree));
       return tree;
 
       /*------------------------------------------------------------------*/
@@ -3330,7 +3321,7 @@ decorateType (ast * tree)
 
       if (!IS_VOID (currFunc->type->next) && tree->right == NULL)
        {
-         werror (E_VOID_FUNC, currFunc->name);
+         werror (W_VOID_FUNC, currFunc->name);
          goto errorTreeReturn;
        }
 
@@ -4318,7 +4309,7 @@ createFunction (symbol * name, ast * body)
       addSymChain (name);
       allocVariables (name);
     }
-  name->lastLine = yylineno;
+  name->lastLine = mylineno;
   currFunc = name;
 
   /* set the stack pointer */
@@ -4459,7 +4450,7 @@ void ast_print (ast * tree, FILE *outfile, int indent)
                value *args=FUNC_ARGS(tree->left->opval.val->type);
                fprintf(outfile,"FUNCTION (%s=%p) type (", 
                        tree->left->opval.val->name, tree);
-               printTypeChain (tree->ftype,outfile);
+               printTypeChain (tree->left->opval.val->type->next,outfile);
                fprintf(outfile,") args (");
                do {
                  if (arg) {
@@ -4507,11 +4498,14 @@ void ast_print (ast * tree, FILE *outfile, int indent)
        /* just get the type        */
        if (tree->type == EX_VALUE) {
 
-               if (IS_LITERAL (tree->opval.val->etype)) {                      
-                       fprintf(outfile,"CONSTANT (%p) value = %d, 0x%x, %g", tree,
-                               (int) floatFromVal(tree->opval.val),
-                               (int) floatFromVal(tree->opval.val),
-                               floatFromVal(tree->opval.val));
+               if (IS_LITERAL (tree->opval.val->etype)) {
+                       fprintf(outfile,"CONSTANT (%p) value = ", tree);
+                       if (SPEC_USIGN (tree->opval.val->etype))
+                               fprintf(outfile,"%u", (TYPE_UDWORD) floatFromVal(tree->opval.val));
+                       else
+                               fprintf(outfile,"%d", (TYPE_DWORD) floatFromVal(tree->opval.val));
+                       fprintf(outfile,", 0x%x, %g", (TYPE_UDWORD) floatFromVal(tree->opval.val),
+                                                     floatFromVal(tree->opval.val));
                } else if (tree->opval.val->sym) {
                        /* if the undefined flag is set then give error message */
                        if (tree->opval.val->sym->undefined) {