From a6fffdf77084e4ad63ef06bf9f77a6c97ad352b3 Mon Sep 17 00:00:00 2001 From: cuz Date: Mon, 16 Sep 2002 20:13:05 +0000 Subject: [PATCH] Fixed a bug git-svn-id: svn://svn.cc65.org/cc65/trunk@1393 b7a2c559-68d2-44c3-8de9-860c34a00d81 --- src/cc65/swstmt.c | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/src/cc65/swstmt.c b/src/cc65/swstmt.c index fc4d40a63..c7446c020 100644 --- a/src/cc65/swstmt.c +++ b/src/cc65/swstmt.c @@ -73,7 +73,8 @@ void SwitchStatement (void) unsigned ExitLabel; /* Exit label */ unsigned CaseLabel; /* Label for case */ unsigned DefaultLabel; /* Label for the default branch */ - long Val; /* Case label value */ + long Val; /* Case label value */ + int HaveBreak = 0; /* True if the last statement had a break */ /* Eat the "switch" token */ @@ -200,7 +201,7 @@ void SwitchStatement (void) /* Parse statements */ if (CurTok.Tok != TOK_RCURLY) { - Statement (0); + HaveBreak = Statement (0); } } @@ -210,9 +211,22 @@ void SwitchStatement (void) Warning ("No case labels"); } else { + + CodeMark SwitchCodeStart; + + /* If the last statement did not have a break, we may have an open + * label (maybe from an if or similar). Emitting code and then moving + * this code to the top will also move the label to the top which is + * wrong. So if the last statement did not have a break (which would + * carry the label), add a jump to the exit. If it is useless, the + * optimizer will remove it later. + */ + if (!HaveBreak) { + g_jump (ExitLabel); + } /* Remember the current position */ - CodeMark SwitchCodeStart = GetCodePos(); + SwitchCodeStart = GetCodePos(); /* Generate code */ g_switch (Nodes, DefaultLabel? DefaultLabel : ExitLabel, Depth); -- 2.39.5