]> git.gag.com Git - fw/sdcc/commitdiff
Early optimization of '?' operator if value already known
authorsandeep <sandeep@4a8a32a2-be11-0410-ad9d-d568d2c75423>
Sat, 20 Oct 2001 17:55:50 +0000 (17:55 +0000)
committersandeep <sandeep@4a8a32a2-be11-0410-ad9d-d568d2c75423>
Sat, 20 Oct 2001 17:55:50 +0000 (17:55 +0000)
git-svn-id: https://sdcc.svn.sourceforge.net/svnroot/sdcc/trunk/sdcc@1420 4a8a32a2-be11-0410-ad9d-d568d2c75423

src/SDCCast.c

index 9812f66bbf04b34c3e1c2e11cbc5d0955e8d1a07..1c79c9ea8901a37094e5e69d1473538bc514325b 100644 (file)
@@ -2902,15 +2902,25 @@ decorateType (ast * tree)
                               tree->opval.val->type);
       return tree;
 
-/*------------------------------------------------------------------*/
-/*----------------------------*/
+      /*------------------------------------------------------------------*/
+      /*----------------------------*/
       /* conditional operator  '?'  */
-/*----------------------------*/
+      /*----------------------------*/
     case '?':
       /* the type is value of the colon operator (on the right) */
       assert(IS_COLON_OP(tree->right));
-      TTYPE (tree) = RTYPE(tree); // #HACK LTYPE(tree).
-      TETYPE (tree) = getSpec (TTYPE (tree));
+      /* if already known then replace the tree : optimizer will do it
+        but faster to do it here */
+      if (IS_LITERAL (LTYPE(tree))) {
+         if ( ((int) floatFromVal (valFromType (LETYPE (tree)))) != 0) {
+             return tree->right->left ;
+         } else {
+             return tree->right->right ;
+         }
+      } else {
+         TTYPE (tree) = RTYPE(tree); // #HACK LTYPE(tree).
+         TETYPE (tree) = getSpec (TTYPE (tree));
+      }
       return tree;
 
     case ':':
@@ -4728,6 +4738,7 @@ void ast_print (ast * tree, FILE *outfile, int indent)
                fprintf(outfile,")\n");
                ast_print(tree->left,outfile,indent+4);
                ast_print(tree->right,outfile,indent+4);
+               return;
 
        case ':':
                fprintf(outfile,"COLON(:) (%p) type (",tree);