1 /*****************************************************************************/
5 /* Parse the switch statement */
9 /* (C) 1998-2002 Ullrich von Bassewitz */
11 /* D-70597 Stuttgart */
12 /* EMail: uz@cc65.org */
15 /* This software is provided 'as-is', without any expressed or implied */
16 /* warranty. In no event will the authors be held liable for any damages */
17 /* arising from the use of this software. */
19 /* Permission is granted to anyone to use this software for any purpose, */
20 /* including commercial applications, and to alter it and redistribute it */
21 /* freely, subject to the following restrictions: */
23 /* 1. The origin of this software must not be misrepresented; you must not */
24 /* claim that you wrote the original software. If you use this software */
25 /* in a product, an acknowledgment in the product documentation would be */
26 /* appreciated but is not required. */
27 /* 2. Altered source versions must be plainly marked as such, and must not */
28 /* be misrepresented as being the original software. */
29 /* 3. This notice may not be removed or altered from any source */
32 /*****************************************************************************/
58 /*****************************************************************************/
60 /*****************************************************************************/
64 void SwitchStatement (void)
65 /* Handle a switch statement for chars with a cmp cascade for the selector */
67 Collection* Nodes; /* CaseNode tree */
68 ExprDesc SwitchExpr; /* Switch statement expression */
69 ExprDesc CaseExpr; /* Case label expression */
70 type SwitchExprType; /* Basic switch expression type */
71 CodeMark CaseCodeStart; /* Start of code marker */
72 unsigned Depth; /* Number of bytes the selector type has */
73 unsigned ExitLabel; /* Exit label */
74 unsigned CaseLabel; /* Label for case */
75 unsigned DefaultLabel; /* Label for the default branch */
76 long Val; /* Case label value */
79 /* Eat the "switch" token */
82 /* Read the switch expression */
84 intexpr (&SwitchExpr);
87 /* Opening curly brace */
90 /* Remember the current code position */
91 CaseCodeStart = GetCodePos();
93 /* Get the unqualified type of the switch expression */
94 SwitchExprType = UnqualifiedType (SwitchExpr.Type[0]);
96 /* Get the number of bytes the selector type has */
97 Depth = SizeOf (SwitchExpr.Type);
98 CHECK (Depth == 1 || Depth == 2 || Depth == 4);
100 /* Get the exit label for the switch statement */
101 ExitLabel = GetLocalLabel ();
103 /* Create a loop so we may use break. */
104 AddLoop (oursp, 0, ExitLabel, 0, 0);
106 /* Create the collection for the case node tree */
107 Nodes = NewCollection ();
109 /* Clear the label for the default branch */
112 /* Parse the labels */
113 while (CurTok.Tok != TOK_RCURLY) {
115 while (CurTok.Tok == TOK_CASE || CurTok.Tok == TOK_DEFAULT) {
117 /* Parse the selector */
118 if (CurTok.Tok == TOK_CASE) {
120 /* Skip the "case" token */
123 /* Read the selector expression */
124 ConstExpr (&CaseExpr);
125 if (!IsClassInt (CaseExpr.Type)) {
126 Error ("Switch quantity not an integer");
129 /* Check the range of the expression */
130 Val = CaseExpr.ConstVal;
131 switch (SwitchExprType) {
135 if (Val < -128 || Val > 127) {
136 Error ("Range error");
141 if (Val < 0 || Val > 255) {
142 Error ("Range error");
148 if (Val < -32768 || Val > 32767) {
149 Error ("Range error");
155 if (Val < 0 || Val > 65535) {
156 Error ("Range error");
165 Internal ("Invalid type: %04X", SwitchExprType);
168 /* Insert the case selector into the selector table */
169 CaseLabel = InsertCaseValue (Nodes, Val, Depth);
171 /* Define this label */
172 g_defcodelabel (CaseLabel);
182 /* Check if we do already have a default branch */
183 if (DefaultLabel == 0) {
185 /* Generate and emit the default label */
186 DefaultLabel = GetLocalLabel ();
187 g_defcodelabel (DefaultLabel);
190 /* We had the default label already */
191 Error ("Duplicate `default' case");
201 /* Parse statements */
202 if (CurTok.Tok != TOK_RCURLY) {
207 /* Check if we had any labels */
208 if (CollCount (Nodes) == 0 && DefaultLabel == 0) {
210 Warning ("No case labels");
214 /* Remember the current position */
215 CodeMark SwitchCodeStart = GetCodePos();
218 g_switch (Nodes, DefaultLabel? DefaultLabel : ExitLabel, Depth);
220 /* Move the code to the front */
221 MoveCode (SwitchCodeStart, GetCodePos(), CaseCodeStart);
225 /* Define the exit label */
226 g_defcodelabel (ExitLabel);
228 /* Eat the closing curly brace */
231 /* Free the case value tree */
232 FreeCaseNodeColl (Nodes);