fixed bug #524209
[fw/sdcc] / src / SDCCast.c
index 4e92814703d6836911229db21266e698fb73ab4f..b6f7cbfbd3a48a4511d631a1aba1463b46dcc1f4 100644 (file)
@@ -477,8 +477,7 @@ resolveChildren:
 /*-----------------------------------------------------------------*/
 /* setAstLineno - walks a ast tree & sets the line number          */
 /*-----------------------------------------------------------------*/
-int 
-setAstLineno (ast * tree, int lineno)
+int setAstLineno (ast * tree, int lineno)
 {
   if (!tree)
     return 0;
@@ -1520,8 +1519,7 @@ isConformingBody (ast * pbody, symbol * sym, ast * body)
   /* if we reach the end or a leaf then true */
   if (!pbody || IS_AST_LINK (pbody) || IS_AST_VALUE (pbody))
     return TRUE;
-
-
+  
   /* if anything else is "volatile" */
   if (IS_VOLATILE (TETYPE (pbody)))
     return FALSE;
@@ -1532,6 +1530,10 @@ isConformingBody (ast * pbody, symbol * sym, ast * body)
     {
 /*------------------------------------------------------------------*/
     case '[':
+      // if the loopvar is used as an index
+      if (astHasSymbol(pbody->right, sym)) {
+       return FALSE;
+      }
       return isConformingBody (pbody->right, sym, body);
 
 /*------------------------------------------------------------------*/
@@ -1800,14 +1802,15 @@ reverseLoop (ast * loop, symbol * sym, ast * init, ast * end)
   rloop = newNode (NULLOP,
                   createIf (newAst_VALUE (symbolVal (sym)),
                             newNode (GOTO,
-                  newAst_VALUE (symbolVal (AST_FOR (loop, continueLabel))),
+                                     newAst_VALUE (symbolVal (AST_FOR (loop, continueLabel))),
                                      NULL), NULL),
                   newNode ('=',
                            newAst_VALUE (symbolVal (sym)),
                            end));
-
+  
   replLoopSym (loop->left, sym);
-
+  setAstLineno (rloop, init->lineno);
+  
   rloop = newNode (NULLOP,
                   newNode ('=',
                            newAst_VALUE (symbolVal (sym)),
@@ -1817,12 +1820,13 @@ reverseLoop (ast * loop, symbol * sym, ast * init, ast * end)
                                         loop->left,
                                         newNode (NULLOP,
                                                  newNode (SUB_ASSIGN,
-                                            newAst_VALUE (symbolVal (sym)),
-                                            newAst_VALUE (constVal ("1"))),
+                                                          newAst_VALUE (symbolVal (sym)),
+                                                          newAst_VALUE (constVal ("1"))),
                                                  rloop))));
-
+  
+  rloop->lineno=init->lineno;
   return decorateType (rloop);
-
+  
 }
 
 /*-----------------------------------------------------------------*/
@@ -2144,8 +2148,8 @@ decorateType (ast * tree)
 
       if (IS_FUNC (LTYPE (tree)))
        {
-         werror (E_ILLEGAL_ADDR, "address of function");
-         goto errorTreeReturn;
+         // this ought to be ignored
+         return (tree->left);
        }
 
       if (IS_LITERAL(LTYPE(tree)))
@@ -2651,6 +2655,15 @@ decorateType (ast * tree)
                                   tree->opval.val->type);
          return tree;
        }
+      /* a left shift must be done with at least 16bits */
+      if ((tree->opval.op==LEFT_OP) && (getSize(LTYPE(tree))<2)) {
+       // insert a cast
+       tree->left = 
+         decorateType (newNode (CAST,
+                                newAst_LINK(copyLinkChain(LTYPE(tree))),
+                                tree->left));
+       SPEC_NOUN(tree->left->left->ftype)=V_INT;
+      }
       /* 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)) &&
@@ -2721,12 +2734,16 @@ decorateType (ast * tree)
              LRVAL (tree) = 1;
       }
 #else
+#if 0 // this is already checked, now this could be explicit
       /* 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)) {
-             werror(W_CAST_STRUCT_PTR,SPEC_STRUCT(RETYPE(tree))->tag,SPEC_STRUCT(LETYPE(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);
+       }
+#endif
       /* if the right is a literal replace the tree */
       if (IS_LITERAL (RETYPE (tree)) && !IS_PTR (LTYPE (tree))) {
        tree->type = EX_VALUE;