1 /*****************************************************************************/
5 /* Parse the switch statement */
9 /* (C) 1998-2008 Ullrich von Bassewitz */
10 /* Roemerstrasse 52 */
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 /*****************************************************************************/
58 /*****************************************************************************/
60 /*****************************************************************************/
64 typedef struct SwitchCtrl SwitchCtrl;
66 Collection* Nodes; /* CaseNode tree */
67 TypeCode ExprType; /* Basic switch expression type */
68 unsigned Depth; /* Number of bytes the selector type has */
69 unsigned DefaultLabel; /* Label for the default branch */
75 /* Pointer to current switch control struct */
76 static SwitchCtrl* Switch = 0;
80 /*****************************************************************************/
82 /*****************************************************************************/
86 void SwitchStatement (void)
87 /* Handle a switch statement for chars with a cmp cascade for the selector */
89 ExprDesc SwitchExpr; /* Switch statement expression */
90 CodeMark CaseCodeStart; /* Start of code marker */
91 CodeMark SwitchCodeStart;/* Start of switch code */
92 CodeMark SwitchCodeEnd; /* End of switch code */
93 unsigned ExitLabel; /* Exit label */
94 unsigned SwitchCodeLabel;/* Label for the switch code */
95 int HaveBreak = 0; /* True if the last statement had a break */
96 int RCurlyBrace; /* True if last token is right curly brace */
97 SwitchCtrl* OldSwitch; /* Pointer to old switch control data */
98 SwitchCtrl SwitchData; /* New switch data */
101 /* Eat the "switch" token */
104 /* Read the switch expression and load it into the primary. It must have
108 Expression0 (&SwitchExpr);
109 if (!IsClassInt (SwitchExpr.Type)) {
110 Error ("Switch quantity is not an integer");
111 /* To avoid any compiler errors, make the expression a valid int */
112 ED_MakeConstAbsInt (&SwitchExpr, 1);
116 /* Add a jump to the switch code. This jump is usually unnecessary,
117 ** because the switch code will moved up just behind the switch
118 ** expression. However, in rare cases, there's a label at the end of
119 ** the switch expression. This label will not get moved, so the code
120 ** jumps around the switch code, and after moving the switch code,
121 ** things look really weird. If we add a jump here, we will never have
122 ** a label attached to the current code position, and the jump itself
123 ** will get removed by the optimizer if it is unnecessary.
125 SwitchCodeLabel = GetLocalLabel ();
126 g_jump (SwitchCodeLabel);
128 /* Remember the current code position. We will move the switch code
129 ** to this position later.
131 GetCodePos (&CaseCodeStart);
133 /* Setup the control structure, save the old and activate the new one */
134 SwitchData.Nodes = NewCollection ();
135 SwitchData.ExprType = UnqualifiedType (SwitchExpr.Type[0].C);
136 SwitchData.Depth = SizeOf (SwitchExpr.Type);
137 SwitchData.DefaultLabel = 0;
139 Switch = &SwitchData;
141 /* Get the exit label for the switch statement */
142 ExitLabel = GetLocalLabel ();
144 /* Create a loop so we may use break. */
145 AddLoop (ExitLabel, 0);
147 /* Parse the following statement, which may actually be a compound
148 ** statement if there is a curly brace at the current input position
150 HaveBreak = Statement (&RCurlyBrace);
152 /* Check if we had any labels */
153 if (CollCount (SwitchData.Nodes) == 0 && SwitchData.DefaultLabel == 0) {
154 Warning ("No case labels");
157 /* If the last statement did not have a break, we may have an open
158 ** label (maybe from an if or similar). Emitting code and then moving
159 ** this code to the top will also move the label to the top which is
160 ** wrong. So if the last statement did not have a break (which would
161 ** carry the label), add a jump to the exit. If it is useless, the
162 ** optimizer will remove it later.
168 /* Remember the current position */
169 GetCodePos (&SwitchCodeStart);
171 /* Output the switch code label */
172 g_defcodelabel (SwitchCodeLabel);
175 if (SwitchData.DefaultLabel == 0) {
176 /* No default label, use switch exit */
177 SwitchData.DefaultLabel = ExitLabel;
179 g_switch (SwitchData.Nodes, SwitchData.DefaultLabel, SwitchData.Depth);
181 /* Move the code to the front */
182 GetCodePos (&SwitchCodeEnd);
183 MoveCode (&SwitchCodeStart, &SwitchCodeEnd, &CaseCodeStart);
185 /* Define the exit label */
186 g_defcodelabel (ExitLabel);
191 /* Switch back to the enclosing switch statement if any */
194 /* Free the case value tree */
195 FreeCaseNodeColl (SwitchData.Nodes);
197 /* If the case statement was terminated by a closing curly
198 ** brace, skip it now.
207 void CaseLabel (void)
208 /* Handle a case sabel */
210 ExprDesc CaseExpr; /* Case label expression */
211 long Val; /* Case label value */
212 unsigned CodeLabel; /* Code label for this case */
215 /* Skip the "case" token */
218 /* Read the selector expression */
219 ConstAbsIntExpr (hie1, &CaseExpr);
222 /* Now check if we're inside a switch statement */
225 /* Check the range of the expression */
226 switch (Switch->ExprType) {
230 if (Val < -128 || Val > 127) {
231 Error ("Range error");
236 if (Val < 0 || Val > 255) {
237 Error ("Range error");
243 if (Val < -32768 || Val > 32767) {
244 Error ("Range error");
250 if (Val < 0 || Val > 65535) {
251 Error ("Range error");
260 Internal ("Invalid type: %06lX", Switch->ExprType);
263 /* Insert the case selector into the selector table */
264 CodeLabel = InsertCaseValue (Switch->Nodes, Val, Switch->Depth);
266 /* Define this label */
267 g_defcodelabel (CodeLabel);
271 /* case keyword outside a switch statement */
272 Error ("Case label not within a switch statement");
282 void DefaultLabel (void)
283 /* Handle a default label */
288 /* Now check if we're inside a switch statement */
291 /* Check if we do already have a default branch */
292 if (Switch->DefaultLabel == 0) {
294 /* Generate and emit the default label */
295 Switch->DefaultLabel = GetLocalLabel ();
296 g_defcodelabel (Switch->DefaultLabel);
299 /* We had the default label already */
300 Error ("Multiple default labels in one switch");
305 /* case keyword outside a switch statement */
306 Error ("`default' label not within a switch statement");