1
0
mirror of https://github.com/cc65/cc65.git synced 2025-01-11 11:30:13 +00:00

Now the compiler removes code that has no observable effect.

This commit is contained in:
acqn 2021-02-22 13:47:32 +08:00 committed by mrdudz
parent 4cb5063e9c
commit a4ad212316
2 changed files with 28 additions and 17 deletions

View File

@ -4188,19 +4188,25 @@ void hie0 (ExprDesc *Expr)
/* Append deferred inc/dec at sequence point */ /* Append deferred inc/dec at sequence point */
DoDeferred (SQP_KEEP_NONE, Expr); DoDeferred (SQP_KEEP_NONE, Expr);
/* If the expression didn't generate code or isn't cast to type void, /* If the expression has no observable effect and isn't cast to type
** emit a warning. ** void, emit a warning and remove useless code if any.
*/ */
GetCodePos (&End); GetCodePos (&End);
if (!ED_MayHaveNoEffect (Expr) && if (CodeRangeIsEmpty (&Start, &End) ||
(CodeRangeIsEmpty (&Start, &End) || (Expr->Flags & E_SIDE_EFFECTS) == 0) {
(Expr->Flags & E_SIDE_EFFECTS) == 0) &&
IS_Get (&WarnNoEffect) && if (!ED_MayHaveNoEffect (Expr) &&
PrevErrorCount == ErrorCount) { IS_Get (&WarnNoEffect) &&
Warning ("Left-hand operand of comma expression has no effect"); PrevErrorCount == ErrorCount) {
Warning ("Left-hand operand of comma expression has no effect");
}
/* Remove code with no effect */
RemoveCode (&Start);
} }
PrevErrorCount = ErrorCount; PrevErrorCount = ErrorCount;
/* Remember the current code position */ /* Remember the current code position */
GetCodePos (&Start); GetCodePos (&Start);

View File

@ -609,18 +609,23 @@ static void Statement (int* PendingToken)
Expr.Flags |= E_NEED_NONE; Expr.Flags |= E_NEED_NONE;
Expression0 (&Expr); Expression0 (&Expr);
/* If the statement didn't generate code, and is not of type /* If the statement has no observable effect and isn't cast to type
** void, emit a warning. ** void, emit a warning and remove useless code if any.
*/ */
GetCodePos (&End); GetCodePos (&End);
if (!ED_YetToLoad (&Expr) && if (CodeRangeIsEmpty (&Start, &End) ||
!ED_MayHaveNoEffect (&Expr) && (Expr.Flags & E_SIDE_EFFECTS) == 0) {
(CodeRangeIsEmpty (&Start, &End) ||
(Expr.Flags & E_SIDE_EFFECTS) == 0) && if (!ED_MayHaveNoEffect (&Expr) &&
IS_Get (&WarnNoEffect) && IS_Get (&WarnNoEffect) &&
PrevErrorCount == ErrorCount) { PrevErrorCount == ErrorCount) {
Warning ("Statement has no effect"); Warning ("Statement has no effect");
}
/* Remove code with no effect */
RemoveCode (&Start);
} }
CheckSemi (PendingToken); CheckSemi (PendingToken);
} }