1 /*****************************************************************************/
5 /* Parse the switch statement */
9 /* (C) 1998-2004 Ullrich von Bassewitz */
11 /* D-70794 Filderstadt */
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 /*****************************************************************************/
59 /*****************************************************************************/
61 /*****************************************************************************/
65 void SwitchStatement (void)
66 /* Handle a switch statement for chars with a cmp cascade for the selector */
68 Collection* Nodes; /* CaseNode tree */
69 ExprDesc SwitchExpr; /* Switch statement expression */
70 ExprDesc CaseExpr; /* Case label expression */
71 type SwitchExprType; /* Basic switch expression type */
72 CodeMark CaseCodeStart; /* Start of code marker */
73 unsigned Depth; /* Number of bytes the selector type has */
74 unsigned ExitLabel; /* Exit label */
75 unsigned CaseLabel; /* Label for case */
76 unsigned DefaultLabel; /* Label for the default branch */
77 unsigned SwitchCodeLabel; /* Label for the switch code */
78 long Val; /* Case label value */
79 int HaveBreak = 0; /* True if the last statement had a break */
82 /* Eat the "switch" token */
85 /* Read the switch expression and load it into the primary. It must have
89 Expression0 (&SwitchExpr);
90 if (!IsClassInt (SwitchExpr.Type)) {
91 Error ("Switch quantity is not an integer");
92 /* To avoid any compiler errors, make the expression a valid int */
93 ED_MakeConstAbsInt (&SwitchExpr, 1);
95 /* Load the expression into the primary register */
98 /* Add a jump to the switch code. This jump is usually unnecessary,
99 * because the switch code will moved up just behind the switch
100 * expression. However, in rare cases, there's a label at the end of
101 * the switch expression. This label will not get moved, so the code
102 * jumps around the switch code, and after moving the switch code,
103 * things look really weird. If we add a jump here, we will never have
104 * a label attached to the current code position, and the jump itself
105 * will get removed by the optimizer if it is unnecessary.
107 SwitchCodeLabel = GetLocalLabel ();
108 g_jump (SwitchCodeLabel);
110 /* Remember the current code position. We will move the switch code
111 * to this position later.
113 CaseCodeStart = GetCodePos();
115 /* Opening curly brace */
118 /* Get the unqualified type of the switch expression */
119 SwitchExprType = UnqualifiedType (SwitchExpr.Type[0]);
121 /* Get the number of bytes the selector type has */
122 Depth = SizeOf (SwitchExpr.Type);
123 CHECK (Depth == 1 || Depth == 2 || Depth == 4);
125 /* Get the exit label for the switch statement */
126 ExitLabel = GetLocalLabel ();
128 /* Create a loop so we may use break. */
129 AddLoop (StackPtr, ExitLabel, 0);
131 /* Create the collection for the case node tree */
132 Nodes = NewCollection ();
134 /* Clear the label for the default branch */
137 /* Parse the labels */
138 while (CurTok.Tok != TOK_RCURLY) {
140 while (CurTok.Tok == TOK_CASE || CurTok.Tok == TOK_DEFAULT) {
142 /* Parse the selector */
143 if (CurTok.Tok == TOK_CASE) {
145 /* Skip the "case" token */
148 /* Read the selector expression */
149 ConstAbsIntExpr (hie1, &CaseExpr);
151 /* Check the range of the expression */
153 switch (SwitchExprType) {
157 if (Val < -128 || Val > 127) {
158 Error ("Range error");
163 if (Val < 0 || Val > 255) {
164 Error ("Range error");
170 if (Val < -32768 || Val > 32767) {
171 Error ("Range error");
177 if (Val < 0 || Val > 65535) {
178 Error ("Range error");
187 Internal ("Invalid type: %04X", SwitchExprType);
190 /* Insert the case selector into the selector table */
191 CaseLabel = InsertCaseValue (Nodes, Val, Depth);
193 /* Define this label */
194 g_defcodelabel (CaseLabel);
204 /* Check if we do already have a default branch */
205 if (DefaultLabel == 0) {
207 /* Generate and emit the default label */
208 DefaultLabel = GetLocalLabel ();
209 g_defcodelabel (DefaultLabel);
212 /* We had the default label already */
213 Error ("Duplicate `default' case");
223 /* Parse statements */
224 if (CurTok.Tok != TOK_RCURLY) {
225 HaveBreak = Statement (0);
229 /* Check if we had any labels */
230 if (CollCount (Nodes) == 0 && DefaultLabel == 0) {
232 Warning ("No case labels");
236 CodeMark SwitchCodeStart;
238 /* If the last statement did not have a break, we may have an open
239 * label (maybe from an if or similar). Emitting code and then moving
240 * this code to the top will also move the label to the top which is
241 * wrong. So if the last statement did not have a break (which would
242 * carry the label), add a jump to the exit. If it is useless, the
243 * optimizer will remove it later.
249 /* Remember the current position */
250 SwitchCodeStart = GetCodePos();
252 /* Output the switch code label */
253 g_defcodelabel (SwitchCodeLabel);
256 g_switch (Nodes, DefaultLabel? DefaultLabel : ExitLabel, Depth);
258 /* Move the code to the front */
259 MoveCode (SwitchCodeStart, GetCodePos(), CaseCodeStart);
263 /* Define the exit label */
264 g_defcodelabel (ExitLabel);
266 /* Eat the closing curly brace */
269 /* Free the case value tree */
270 FreeCaseNodeColl (Nodes);