]> git.sur5r.net Git - cc65/blob - src/cc65/swstmt.c
Fixed a bug
[cc65] / src / cc65 / swstmt.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                 swstmt.c                                  */
4 /*                                                                           */
5 /*                        Parse the switch statement                         */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 1998-2003 Ullrich von Bassewitz                                       */
10 /*               Wacholderweg 14                                             */
11 /*               D-70597 Stuttgart                                           */
12 /* EMail:        uz@cc65.org                                                 */
13 /*                                                                           */
14 /*                                                                           */
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.                                    */
18 /*                                                                           */
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:                            */
22 /*                                                                           */
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              */
30 /*    distribution.                                                          */
31 /*                                                                           */
32 /*****************************************************************************/
33
34
35
36 #include <limits.h>
37
38 /* common */
39 #include "coll.h"
40 #include "xmalloc.h"
41
42 /* cc65 */
43 #include "asmcode.h"
44 #include "asmlabel.h"
45 #include "casenode.h"
46 #include "codegen.h"
47 #include "datatype.h"
48 #include "error.h"
49 #include "expr.h"
50 #include "global.h"
51 #include "loop.h"
52 #include "scanner.h"
53 #include "stmt.h"
54 #include "swstmt.h"
55
56
57
58 /*****************************************************************************/
59 /*                                   Code                                    */
60 /*****************************************************************************/
61
62
63
64 void SwitchStatement (void)
65 /* Handle a switch statement for chars with a cmp cascade for the selector */
66 {
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     unsigned SwitchCodeLabel;   /* Label for the switch code */
77     long     Val;               /* Case label value */
78     int      HaveBreak = 0;     /* True if the last statement had a break */
79
80
81     /* Eat the "switch" token */
82     NextToken ();
83
84     /* Read the switch expression */
85     ConsumeLParen ();
86     intexpr (&SwitchExpr);
87     ConsumeRParen ();
88
89     /* Add a jump to the switch code. This jump is usually unnecessary,
90      * because the switch code will moved up just behind the switch
91      * expression. However, in rare cases, there's a label at the end of
92      * the switch expression. This label will not get moved, so the code
93      * jumps around the switch code, and after moving the switch code,
94      * things look really weird. If we add a jump here, we will never have
95      * a label attached to the current code position, and the jump itself
96      * will get removed by the optimizer if it is unnecessary.
97      */
98     SwitchCodeLabel = GetLocalLabel ();
99     g_jump (SwitchCodeLabel);
100
101     /* Remember the current code position. We will move the switch code
102      * to this position later.
103      */
104     CaseCodeStart = GetCodePos();
105
106     /* Opening curly brace */
107     ConsumeLCurly ();
108
109     /* Get the unqualified type of the switch expression */
110     SwitchExprType = UnqualifiedType (SwitchExpr.Type[0]);
111
112     /* Get the number of bytes the selector type has */
113     Depth = SizeOf (SwitchExpr.Type);
114     CHECK (Depth == 1 || Depth == 2 || Depth == 4);
115
116     /* Get the exit label for the switch statement */
117     ExitLabel = GetLocalLabel ();
118
119     /* Create a loop so we may use break. */
120     AddLoop (oursp, ExitLabel, 0);
121
122     /* Create the collection for the case node tree */
123     Nodes = NewCollection ();
124
125     /* Clear the label for the default branch */
126     DefaultLabel = 0;
127
128     /* Parse the labels */
129     while (CurTok.Tok != TOK_RCURLY) {
130
131         while (CurTok.Tok == TOK_CASE || CurTok.Tok == TOK_DEFAULT) {
132
133             /* Parse the selector */
134             if (CurTok.Tok == TOK_CASE) {
135
136                 /* Skip the "case" token */
137                 NextToken ();
138
139                 /* Read the selector expression */
140                 ConstExpr (&CaseExpr);
141                 if (!IsClassInt (CaseExpr.Type)) {
142                     Error ("Switch quantity not an integer");
143                 }
144
145                 /* Check the range of the expression */
146                 Val = CaseExpr.ConstVal;
147                 switch (SwitchExprType) {
148
149                     case T_SCHAR:
150                         /* Signed char */
151                         if (Val < -128 || Val > 127) {
152                             Error ("Range error");
153                         }
154                         break;
155
156                     case T_UCHAR:
157                         if (Val < 0 || Val > 255) {
158                             Error ("Range error");
159                         }
160                         break;
161
162                     case T_SHORT:
163                     case T_INT:
164                         if (Val < -32768 || Val > 32767) {
165                             Error ("Range error");
166                         }
167                         break;
168
169                     case T_USHORT:
170                     case T_UINT:
171                         if (Val < 0 || Val > 65535) {
172                             Error ("Range error");
173                         }
174                         break;
175
176                     case T_LONG:
177                     case T_ULONG:
178                         break;
179
180                     default:
181                         Internal ("Invalid type: %04X", SwitchExprType);
182                 }
183
184                 /* Insert the case selector into the selector table */
185                 CaseLabel = InsertCaseValue (Nodes, Val, Depth);
186
187                 /* Define this label */
188                 g_defcodelabel (CaseLabel);
189
190                 /* Skip the colon */
191                 ConsumeColon ();
192
193             } else {
194
195                 /* Default case */
196                 NextToken ();
197
198                 /* Check if we do already have a default branch */
199                 if (DefaultLabel == 0) {
200
201                     /* Generate and emit the default label */
202                     DefaultLabel = GetLocalLabel ();
203                     g_defcodelabel (DefaultLabel);
204
205                 } else {
206                     /* We had the default label already */
207                     Error ("Duplicate `default' case");
208                 }
209
210                 /* Skip the colon */
211                 ConsumeColon ();
212
213             }
214
215         }
216
217         /* Parse statements */
218         if (CurTok.Tok != TOK_RCURLY) {
219             HaveBreak = Statement (0);
220         }
221     }
222
223     /* Check if we had any labels */
224     if (CollCount (Nodes) == 0 && DefaultLabel == 0) {
225
226         Warning ("No case labels");
227
228     } else {
229
230         CodeMark SwitchCodeStart;
231
232         /* If the last statement did not have a break, we may have an open
233          * label (maybe from an if or similar). Emitting code and then moving
234          * this code to the top will also move the label to the top which is
235          * wrong. So if the last statement did not have a break (which would
236          * carry the label), add a jump to the exit. If it is useless, the
237          * optimizer will remove it later.
238          */
239         if (!HaveBreak) {
240             g_jump (ExitLabel);
241         }
242
243         /* Remember the current position */
244         SwitchCodeStart = GetCodePos();
245
246         /* Output the switch code label */
247         g_defcodelabel (SwitchCodeLabel);
248
249         /* Generate code */
250         g_switch (Nodes, DefaultLabel? DefaultLabel : ExitLabel, Depth);
251
252         /* Move the code to the front */
253         MoveCode (SwitchCodeStart, GetCodePos(), CaseCodeStart);
254
255     }
256
257     /* Define the exit label */
258     g_defcodelabel (ExitLabel);
259
260     /* Eat the closing curly brace */
261     NextToken ();
262
263     /* Free the case value tree */
264     FreeCaseNodeColl (Nodes);
265
266     /* End the loop */
267     DelLoop ();
268 }
269
270
271