From a51d6d40de4a6a60d27c472358cd4e1ece5f5986 Mon Sep 17 00:00:00 2001 From: acqn Date: Sun, 14 Mar 2021 02:39:40 +0800 Subject: [PATCH] Ternary fix for some obscure cases. --- src/cc65/expr.c | 66 +++++++++++++++++++++++++++---------------------- 1 file changed, 36 insertions(+), 30 deletions(-) diff --git a/src/cc65/expr.c b/src/cc65/expr.c index 4f579b7ae..422159c4b 100644 --- a/src/cc65/expr.c +++ b/src/cc65/expr.c @@ -3987,24 +3987,24 @@ static void hieQuest (ExprDesc* Expr) */ ExprWithCheck (hie1, &Expr2); Expr2IsNULL = ED_IsNullPtr (&Expr2); - if (!IsTypeVoid (Expr2.Type)) { - if (!ConstantCond || !ED_IsConst (&Expr2)) { - /* Load it into the primary */ - LoadExpr (CF_NONE, &Expr2); + if (!IsTypeVoid (Expr2.Type) && + ED_YetToLoad (&Expr2) && + (!ConstantCond || !ED_IsConst (&Expr2))) { + /* Load it into the primary */ + LoadExpr (CF_NONE, &Expr2); - /* Append deferred inc/dec at sequence point */ - DoDeferred (SQP_KEEP_EXPR, &Expr2); + /* Append deferred inc/dec at sequence point */ + DoDeferred (SQP_KEEP_EXPR, &Expr2); - ED_FinalizeRValLoad (&Expr2); - } else { - /* Constant boolean subexpression could still have deferred inc/ - ** dec operations, so just flush their side-effects at this - ** sequence point. - */ - DoDeferred (SQP_KEEP_NONE, &Expr2); - } - Expr2.Type = PtrConversion (Expr2.Type); + ED_FinalizeRValLoad (&Expr2); + } else { + /* Constant boolean subexpression could still have deferred inc/ + ** dec operations, so just flush their side-effects at this + ** sequence point. + */ + DoDeferred (SQP_KEEP_NONE, &Expr2); } + Expr2.Type = PtrConversion (Expr2.Type); if (!ConstantCond) { /* Remember the current code position */ @@ -4021,6 +4021,9 @@ static void hieQuest (ExprDesc* Expr) g_defcodelabel (FalseLab); } else { if (Expr->IVal == 0) { + /* Expr2 is unevaluated when the condition is false */ + Expr2.Flags |= E_EVAL_UNEVAL; + /* Remove the load code of Expr2 */ RemoveCode (&SkippedBranch); } else { @@ -4035,26 +4038,29 @@ static void hieQuest (ExprDesc* Expr) */ ExprWithCheck (hie1, &Expr3); Expr3IsNULL = ED_IsNullPtr (&Expr3); - if (!IsTypeVoid (Expr3.Type)) { - if (!ConstantCond || !ED_IsConst (&Expr3)) { - /* Load it into the primary */ - LoadExpr (CF_NONE, &Expr3); + if (!IsTypeVoid (Expr3.Type) && + ED_YetToLoad (&Expr3) && + (!ConstantCond || !ED_IsConst (&Expr3))) { + /* Load it into the primary */ + LoadExpr (CF_NONE, &Expr3); - /* Append deferred inc/dec at sequence point */ - DoDeferred (SQP_KEEP_EXPR, &Expr3); + /* Append deferred inc/dec at sequence point */ + DoDeferred (SQP_KEEP_EXPR, &Expr3); - ED_FinalizeRValLoad (&Expr3); - } else { - /* Constant boolean subexpression could still have deferred inc/ - ** dec operations, so just flush their side-effects at this - ** sequence point. - */ - DoDeferred (SQP_KEEP_NONE, &Expr3); - } - Expr3.Type = PtrConversion (Expr3.Type); + ED_FinalizeRValLoad (&Expr3); + } else { + /* Constant boolean subexpression could still have deferred inc/ + ** dec operations, so just flush their side-effects at this + ** sequence point. + */ + DoDeferred (SQP_KEEP_NONE, &Expr3); } + Expr3.Type = PtrConversion (Expr3.Type); if (ConstantCond && Expr->IVal != 0) { + /* Expr3 is unevaluated when the condition is true */ + Expr3.Flags |= E_EVAL_UNEVAL; + /* Remove the load code of Expr3 */ RemoveCode (&SkippedBranch); }