From a4ad212316bce9ae9a2d54fa35999a2b5c15ba96 Mon Sep 17 00:00:00 2001 From: acqn Date: Mon, 22 Feb 2021 13:47:32 +0800 Subject: [PATCH] Now the compiler removes code that has no observable effect. --- src/cc65/expr.c | 22 ++++++++++++++-------- src/cc65/stmt.c | 23 ++++++++++++++--------- 2 files changed, 28 insertions(+), 17 deletions(-) diff --git a/src/cc65/expr.c b/src/cc65/expr.c index ef3c4ec38..eb0afb38b 100644 --- a/src/cc65/expr.c +++ b/src/cc65/expr.c @@ -4188,19 +4188,25 @@ void hie0 (ExprDesc *Expr) /* Append deferred inc/dec at sequence point */ DoDeferred (SQP_KEEP_NONE, Expr); - /* If the expression didn't generate code or isn't cast to type void, - ** emit a warning. + /* If the expression has no observable effect and isn't cast to type + ** void, emit a warning and remove useless code if any. */ GetCodePos (&End); - if (!ED_MayHaveNoEffect (Expr) && - (CodeRangeIsEmpty (&Start, &End) || - (Expr->Flags & E_SIDE_EFFECTS) == 0) && - IS_Get (&WarnNoEffect) && - PrevErrorCount == ErrorCount) { - Warning ("Left-hand operand of comma expression has no effect"); + if (CodeRangeIsEmpty (&Start, &End) || + (Expr->Flags & E_SIDE_EFFECTS) == 0) { + + if (!ED_MayHaveNoEffect (Expr) && + IS_Get (&WarnNoEffect) && + PrevErrorCount == ErrorCount) { + Warning ("Left-hand operand of comma expression has no effect"); + } + + /* Remove code with no effect */ + RemoveCode (&Start); } PrevErrorCount = ErrorCount; + /* Remember the current code position */ GetCodePos (&Start); diff --git a/src/cc65/stmt.c b/src/cc65/stmt.c index a08197c09..7355e88a8 100644 --- a/src/cc65/stmt.c +++ b/src/cc65/stmt.c @@ -609,18 +609,23 @@ static void Statement (int* PendingToken) Expr.Flags |= E_NEED_NONE; Expression0 (&Expr); - /* If the statement didn't generate code, and is not of type - ** void, emit a warning. + /* If the statement has no observable effect and isn't cast to type + ** void, emit a warning and remove useless code if any. */ GetCodePos (&End); - if (!ED_YetToLoad (&Expr) && - !ED_MayHaveNoEffect (&Expr) && - (CodeRangeIsEmpty (&Start, &End) || - (Expr.Flags & E_SIDE_EFFECTS) == 0) && - IS_Get (&WarnNoEffect) && - PrevErrorCount == ErrorCount) { - Warning ("Statement has no effect"); + if (CodeRangeIsEmpty (&Start, &End) || + (Expr.Flags & E_SIDE_EFFECTS) == 0) { + + if (!ED_MayHaveNoEffect (&Expr) && + IS_Get (&WarnNoEffect) && + PrevErrorCount == ErrorCount) { + Warning ("Statement has no effect"); + } + + /* Remove code with no effect */ + RemoveCode (&Start); } + CheckSemi (PendingToken); }