2001-10-09 09:44:39 +00:00
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
/* */
|
|
|
|
|
/* swstmt.c */
|
|
|
|
|
/* */
|
|
|
|
|
/* Parse the switch statement */
|
|
|
|
|
/* */
|
|
|
|
|
/* */
|
|
|
|
|
/* */
|
2006-02-19 12:29:37 +00:00
|
|
|
|
/* (C) 1998-2006 Ullrich von Bassewitz */
|
2004-06-05 11:35:53 +00:00
|
|
|
|
/* R<>merstra<72>e 52 */
|
|
|
|
|
/* D-70794 Filderstadt */
|
2001-10-09 09:44:39 +00:00
|
|
|
|
/* EMail: uz@cc65.org */
|
|
|
|
|
/* */
|
|
|
|
|
/* */
|
|
|
|
|
/* This software is provided 'as-is', without any expressed or implied */
|
|
|
|
|
/* warranty. In no event will the authors be held liable for any damages */
|
|
|
|
|
/* arising from the use of this software. */
|
|
|
|
|
/* */
|
|
|
|
|
/* Permission is granted to anyone to use this software for any purpose, */
|
|
|
|
|
/* including commercial applications, and to alter it and redistribute it */
|
|
|
|
|
/* freely, subject to the following restrictions: */
|
|
|
|
|
/* */
|
|
|
|
|
/* 1. The origin of this software must not be misrepresented; you must not */
|
|
|
|
|
/* claim that you wrote the original software. If you use this software */
|
|
|
|
|
/* in a product, an acknowledgment in the product documentation would be */
|
|
|
|
|
/* appreciated but is not required. */
|
|
|
|
|
/* 2. Altered source versions must be plainly marked as such, and must not */
|
|
|
|
|
/* be misrepresented as being the original software. */
|
|
|
|
|
/* 3. This notice may not be removed or altered from any source */
|
|
|
|
|
/* distribution. */
|
|
|
|
|
/* */
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2001-10-11 08:02:03 +00:00
|
|
|
|
#include <limits.h>
|
|
|
|
|
|
2001-10-09 09:44:39 +00:00
|
|
|
|
/* common */
|
|
|
|
|
#include "coll.h"
|
|
|
|
|
#include "xmalloc.h"
|
|
|
|
|
|
|
|
|
|
/* cc65 */
|
2001-10-11 08:02:03 +00:00
|
|
|
|
#include "asmcode.h"
|
2001-10-09 09:44:39 +00:00
|
|
|
|
#include "asmlabel.h"
|
2001-10-11 08:02:03 +00:00
|
|
|
|
#include "casenode.h"
|
2001-10-09 09:44:39 +00:00
|
|
|
|
#include "codegen.h"
|
|
|
|
|
#include "datatype.h"
|
|
|
|
|
#include "error.h"
|
|
|
|
|
#include "expr.h"
|
|
|
|
|
#include "global.h"
|
|
|
|
|
#include "loop.h"
|
|
|
|
|
#include "scanner.h"
|
|
|
|
|
#include "stmt.h"
|
|
|
|
|
#include "swstmt.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
2004-07-06 11:30:08 +00:00
|
|
|
|
/* Code */
|
2001-10-09 09:44:39 +00:00
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2001-10-11 08:02:03 +00:00
|
|
|
|
void SwitchStatement (void)
|
2001-10-09 09:44:39 +00:00
|
|
|
|
/* Handle a switch statement for chars with a cmp cascade for the selector */
|
|
|
|
|
{
|
2001-10-11 08:02:03 +00:00
|
|
|
|
Collection* Nodes; /* CaseNode tree */
|
|
|
|
|
ExprDesc SwitchExpr; /* Switch statement expression */
|
|
|
|
|
ExprDesc CaseExpr; /* Case label expression */
|
2006-02-19 12:29:37 +00:00
|
|
|
|
TypeCode SwitchExprType; /* Basic switch expression type */
|
2001-10-11 08:02:03 +00:00
|
|
|
|
CodeMark CaseCodeStart; /* Start of code marker */
|
2004-09-22 17:48:23 +00:00
|
|
|
|
CodeMark SwitchCodeStart; /* Start of switch code */
|
|
|
|
|
CodeMark SwitchCodeEnd; /* End of switch code */
|
2001-10-11 08:02:03 +00:00
|
|
|
|
unsigned Depth; /* Number of bytes the selector type has */
|
|
|
|
|
unsigned ExitLabel; /* Exit label */
|
|
|
|
|
unsigned CaseLabel; /* Label for case */
|
|
|
|
|
unsigned DefaultLabel; /* Label for the default branch */
|
2003-07-26 07:31:15 +00:00
|
|
|
|
unsigned SwitchCodeLabel; /* Label for the switch code */
|
2002-09-16 20:13:05 +00:00
|
|
|
|
long Val; /* Case label value */
|
|
|
|
|
int HaveBreak = 0; /* True if the last statement had a break */
|
2001-10-09 09:44:39 +00:00
|
|
|
|
|
|
|
|
|
|
2001-10-11 08:02:03 +00:00
|
|
|
|
/* Eat the "switch" token */
|
|
|
|
|
NextToken ();
|
|
|
|
|
|
2004-05-31 15:27:33 +00:00
|
|
|
|
/* Read the switch expression and load it into the primary. It must have
|
|
|
|
|
* integer type.
|
|
|
|
|
*/
|
2001-10-11 08:02:03 +00:00
|
|
|
|
ConsumeLParen ();
|
2004-05-31 15:27:33 +00:00
|
|
|
|
Expression0 (&SwitchExpr);
|
|
|
|
|
if (!IsClassInt (SwitchExpr.Type)) {
|
|
|
|
|
Error ("Switch quantity is not an integer");
|
|
|
|
|
/* To avoid any compiler errors, make the expression a valid int */
|
|
|
|
|
ED_MakeConstAbsInt (&SwitchExpr, 1);
|
|
|
|
|
}
|
2001-10-11 08:02:03 +00:00
|
|
|
|
ConsumeRParen ();
|
|
|
|
|
|
2003-07-26 07:31:15 +00:00
|
|
|
|
/* Add a jump to the switch code. This jump is usually unnecessary,
|
|
|
|
|
* because the switch code will moved up just behind the switch
|
|
|
|
|
* expression. However, in rare cases, there's a label at the end of
|
|
|
|
|
* the switch expression. This label will not get moved, so the code
|
|
|
|
|
* jumps around the switch code, and after moving the switch code,
|
|
|
|
|
* things look really weird. If we add a jump here, we will never have
|
|
|
|
|
* a label attached to the current code position, and the jump itself
|
|
|
|
|
* will get removed by the optimizer if it is unnecessary.
|
|
|
|
|
*/
|
|
|
|
|
SwitchCodeLabel = GetLocalLabel ();
|
|
|
|
|
g_jump (SwitchCodeLabel);
|
|
|
|
|
|
|
|
|
|
/* Remember the current code position. We will move the switch code
|
|
|
|
|
* to this position later.
|
|
|
|
|
*/
|
2004-07-06 11:30:08 +00:00
|
|
|
|
GetCodePos (&CaseCodeStart);
|
2003-07-26 07:31:15 +00:00
|
|
|
|
|
2001-10-11 08:02:03 +00:00
|
|
|
|
/* Opening curly brace */
|
|
|
|
|
ConsumeLCurly ();
|
|
|
|
|
|
2001-10-09 09:44:39 +00:00
|
|
|
|
/* Get the unqualified type of the switch expression */
|
2006-02-19 12:29:37 +00:00
|
|
|
|
SwitchExprType = UnqualifiedType (SwitchExpr.Type[0].C);
|
2001-10-11 08:02:03 +00:00
|
|
|
|
|
|
|
|
|
/* Get the number of bytes the selector type has */
|
|
|
|
|
Depth = SizeOf (SwitchExpr.Type);
|
2004-09-22 17:48:23 +00:00
|
|
|
|
CHECK (Depth == SIZEOF_CHAR || Depth == SIZEOF_INT || Depth == SIZEOF_LONG);
|
2001-10-11 08:02:03 +00:00
|
|
|
|
|
|
|
|
|
/* Get the exit label for the switch statement */
|
|
|
|
|
ExitLabel = GetLocalLabel ();
|
|
|
|
|
|
|
|
|
|
/* Create a loop so we may use break. */
|
2004-06-06 10:52:32 +00:00
|
|
|
|
AddLoop (ExitLabel, 0);
|
2001-10-09 09:44:39 +00:00
|
|
|
|
|
2001-10-11 08:02:03 +00:00
|
|
|
|
/* Create the collection for the case node tree */
|
|
|
|
|
Nodes = NewCollection ();
|
2001-10-09 09:44:39 +00:00
|
|
|
|
|
2001-10-11 08:02:03 +00:00
|
|
|
|
/* Clear the label for the default branch */
|
|
|
|
|
DefaultLabel = 0;
|
2001-10-09 09:44:39 +00:00
|
|
|
|
|
|
|
|
|
/* Parse the labels */
|
|
|
|
|
while (CurTok.Tok != TOK_RCURLY) {
|
|
|
|
|
|
2001-10-11 08:02:03 +00:00
|
|
|
|
while (CurTok.Tok == TOK_CASE || CurTok.Tok == TOK_DEFAULT) {
|
2001-10-09 09:44:39 +00:00
|
|
|
|
|
2001-10-11 08:02:03 +00:00
|
|
|
|
/* Parse the selector */
|
|
|
|
|
if (CurTok.Tok == TOK_CASE) {
|
|
|
|
|
|
|
|
|
|
/* Skip the "case" token */
|
|
|
|
|
NextToken ();
|
|
|
|
|
|
|
|
|
|
/* Read the selector expression */
|
2004-05-31 15:27:33 +00:00
|
|
|
|
ConstAbsIntExpr (hie1, &CaseExpr);
|
2001-10-11 08:02:03 +00:00
|
|
|
|
|
|
|
|
|
/* Check the range of the expression */
|
2004-06-06 14:48:59 +00:00
|
|
|
|
Val = CaseExpr.IVal;
|
2001-10-11 08:02:03 +00:00
|
|
|
|
switch (SwitchExprType) {
|
|
|
|
|
|
|
|
|
|
case T_SCHAR:
|
|
|
|
|
/* Signed char */
|
|
|
|
|
if (Val < -128 || Val > 127) {
|
|
|
|
|
Error ("Range error");
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case T_UCHAR:
|
|
|
|
|
if (Val < 0 || Val > 255) {
|
|
|
|
|
Error ("Range error");
|
|
|
|
|
}
|
|
|
|
|
break;
|
2002-02-18 08:40:11 +00:00
|
|
|
|
|
2001-10-11 19:58:16 +00:00
|
|
|
|
case T_SHORT:
|
2001-10-11 08:02:03 +00:00
|
|
|
|
case T_INT:
|
|
|
|
|
if (Val < -32768 || Val > 32767) {
|
|
|
|
|
Error ("Range error");
|
|
|
|
|
}
|
|
|
|
|
break;
|
2002-02-18 08:40:11 +00:00
|
|
|
|
|
2001-10-11 19:58:16 +00:00
|
|
|
|
case T_USHORT:
|
2001-10-11 08:02:03 +00:00
|
|
|
|
case T_UINT:
|
|
|
|
|
if (Val < 0 || Val > 65535) {
|
|
|
|
|
Error ("Range error");
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case T_LONG:
|
|
|
|
|
case T_ULONG:
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
2006-02-19 12:29:37 +00:00
|
|
|
|
Internal ("Invalid type: %06lX", SwitchExprType);
|
2001-10-09 09:44:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
2001-10-11 08:02:03 +00:00
|
|
|
|
/* Insert the case selector into the selector table */
|
2003-07-26 07:31:15 +00:00
|
|
|
|
CaseLabel = InsertCaseValue (Nodes, Val, Depth);
|
2001-10-09 09:44:39 +00:00
|
|
|
|
|
2001-10-11 08:02:03 +00:00
|
|
|
|
/* Define this label */
|
|
|
|
|
g_defcodelabel (CaseLabel);
|
2001-10-09 09:44:39 +00:00
|
|
|
|
|
2001-10-11 08:02:03 +00:00
|
|
|
|
/* Skip the colon */
|
|
|
|
|
ConsumeColon ();
|
2001-10-09 09:44:39 +00:00
|
|
|
|
|
2001-10-11 08:02:03 +00:00
|
|
|
|
} else {
|
2001-10-09 09:44:39 +00:00
|
|
|
|
|
2001-10-11 08:02:03 +00:00
|
|
|
|
/* Default case */
|
|
|
|
|
NextToken ();
|
2001-10-09 09:44:39 +00:00
|
|
|
|
|
2001-10-11 08:02:03 +00:00
|
|
|
|
/* Check if we do already have a default branch */
|
|
|
|
|
if (DefaultLabel == 0) {
|
2001-10-09 09:44:39 +00:00
|
|
|
|
|
2001-10-11 08:02:03 +00:00
|
|
|
|
/* Generate and emit the default label */
|
|
|
|
|
DefaultLabel = GetLocalLabel ();
|
|
|
|
|
g_defcodelabel (DefaultLabel);
|
2001-10-09 09:44:39 +00:00
|
|
|
|
|
2001-10-11 08:02:03 +00:00
|
|
|
|
} else {
|
|
|
|
|
/* We had the default label already */
|
|
|
|
|
Error ("Duplicate `default' case");
|
|
|
|
|
}
|
2001-10-09 09:44:39 +00:00
|
|
|
|
|
2001-10-11 08:02:03 +00:00
|
|
|
|
/* Skip the colon */
|
|
|
|
|
ConsumeColon ();
|
2001-10-09 09:44:39 +00:00
|
|
|
|
|
2001-10-11 08:02:03 +00:00
|
|
|
|
}
|
2001-10-09 09:44:39 +00:00
|
|
|
|
|
2001-10-11 08:02:03 +00:00
|
|
|
|
}
|
2001-10-09 09:44:39 +00:00
|
|
|
|
|
2001-10-11 08:02:03 +00:00
|
|
|
|
/* Parse statements */
|
|
|
|
|
if (CurTok.Tok != TOK_RCURLY) {
|
2002-09-16 20:13:05 +00:00
|
|
|
|
HaveBreak = Statement (0);
|
2001-10-11 08:02:03 +00:00
|
|
|
|
}
|
2001-10-09 09:44:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
2001-10-11 08:02:03 +00:00
|
|
|
|
/* Check if we had any labels */
|
|
|
|
|
if (CollCount (Nodes) == 0 && DefaultLabel == 0) {
|
2001-10-09 09:44:39 +00:00
|
|
|
|
|
2001-10-11 08:02:03 +00:00
|
|
|
|
Warning ("No case labels");
|
2001-10-09 09:44:39 +00:00
|
|
|
|
|
2004-09-22 17:48:23 +00:00
|
|
|
|
}
|
2001-10-09 09:44:39 +00:00
|
|
|
|
|
2004-09-22 17:48:23 +00:00
|
|
|
|
/* 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);
|
|
|
|
|
}
|
2001-10-09 09:44:39 +00:00
|
|
|
|
|
2004-09-22 17:48:23 +00:00
|
|
|
|
/* Remember the current position */
|
|
|
|
|
GetCodePos (&SwitchCodeStart);
|
2003-07-26 07:31:15 +00:00
|
|
|
|
|
2004-09-22 17:48:23 +00:00
|
|
|
|
/* Output the switch code label */
|
|
|
|
|
g_defcodelabel (SwitchCodeLabel);
|
2001-10-09 09:44:39 +00:00
|
|
|
|
|
2004-09-22 17:48:23 +00:00
|
|
|
|
/* Generate code */
|
|
|
|
|
g_switch (Nodes, DefaultLabel? DefaultLabel : ExitLabel, Depth);
|
2001-10-09 09:44:39 +00:00
|
|
|
|
|
2004-09-22 17:48:23 +00:00
|
|
|
|
/* Move the code to the front */
|
|
|
|
|
GetCodePos (&SwitchCodeEnd);
|
|
|
|
|
MoveCode (&SwitchCodeStart, &SwitchCodeEnd, &CaseCodeStart);
|
2001-10-09 09:44:39 +00:00
|
|
|
|
|
2001-10-11 08:02:03 +00:00
|
|
|
|
/* Define the exit label */
|
2001-10-09 09:44:39 +00:00
|
|
|
|
g_defcodelabel (ExitLabel);
|
|
|
|
|
|
2001-10-11 08:02:03 +00:00
|
|
|
|
/* Eat the closing curly brace */
|
2001-10-09 09:44:39 +00:00
|
|
|
|
NextToken ();
|
2001-10-11 19:58:16 +00:00
|
|
|
|
|
2001-10-11 13:39:17 +00:00
|
|
|
|
/* Free the case value tree */
|
|
|
|
|
FreeCaseNodeColl (Nodes);
|
2001-10-09 09:44:39 +00:00
|
|
|
|
|
2001-10-11 08:02:03 +00:00
|
|
|
|
/* End the loop */
|
|
|
|
|
DelLoop ();
|
2001-10-09 09:44:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|