some small fixes
[fw/sdcc] / src / SDCCast.c
index 69e8b02696df0304435334cbf921c18d1fa8419f..15f59285a84add354e7611937d698e1eadc1f75c 100644 (file)
@@ -50,6 +50,7 @@ int noAlloc = 0;
 symbol *currFunc;
 static ast *createIval (ast *, sym_link *, initList *, ast *);
 static ast *createIvalCharPtr (ast *, sym_link *, ast *);
+static ast *optimizeCompare (ast *);
 ast *optimizeRRCRLC (ast *);
 ast *optimizeGetHbit (ast *);
 ast *backPatchLabels (ast *, symbol *, symbol *);
@@ -1161,12 +1162,34 @@ bool constExprTree (ast *cexpr) {
   switch (cexpr->type) 
     {
     case EX_VALUE:
-      return (IS_AST_LIT_VALUE(cexpr));
+      if (IS_AST_LIT_VALUE(cexpr)) {
+       // this is a literal
+       return TRUE;
+      }
+      if (IS_AST_SYM_VALUE(cexpr) && IS_FUNC(AST_SYMBOL(cexpr)->type)) {
+       // a function's address will never change
+       return TRUE;
+      }
+      if (IS_AST_SYM_VALUE(cexpr) && 
+         IN_CODESPACE(SPEC_OCLS(AST_SYMBOL(cexpr)->etype))) {
+       // a symbol in code space will never change
+       // This is only for the 'char *s="hallo"' case and will have to leave
+       return TRUE;
+      }
+      return FALSE;
     case EX_LINK:
       werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
              "unexpected link in expression tree\n");
       return FALSE;
     case EX_OP:
+      if (cexpr->opval.op==ARRAYINIT) {
+       fprintf (stderr, "skipping arrayinit\n");
+       // this is a list of literals
+       return TRUE;
+      }
+      if (cexpr->opval.op=='=') {
+       return constExprTree(cexpr->right);
+      }
       if (cexpr->opval.op==CAST) {
        // jwk: cast ignored, maybe we should throw a warning here
        return constExprTree(cexpr->right);
@@ -3942,7 +3965,7 @@ tryNext2:
 /*-----------------------------------------------------------------*/
 /* optimizeCompare - otimizes compares for bit variables     */
 /*-----------------------------------------------------------------*/
-ast *
+static ast *
 optimizeCompare (ast * root)
 {
   ast *optExpr = NULL;