]> 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-2002 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     long     Val;               /* Case label value */
77     int      HaveBreak = 0;     /* True if the last statement had a break */
78
79
80     /* Eat the "switch" token */
81     NextToken ();
82
83     /* Read the switch expression */
84     ConsumeLParen ();
85     intexpr (&SwitchExpr);
86     ConsumeRParen ();
87
88     /* Opening curly brace */
89     ConsumeLCurly ();
90
91     /* Remember the current code position */
92     CaseCodeStart = GetCodePos();
93
94     /* Get the unqualified type of the switch expression */
95     SwitchExprType = UnqualifiedType (SwitchExpr.Type[0]);
96
97     /* Get the number of bytes the selector type has */
98     Depth = SizeOf (SwitchExpr.Type);
99     CHECK (Depth == 1 || Depth == 2 || Depth == 4);
100
101     /* Get the exit label for the switch statement */
102     ExitLabel = GetLocalLabel ();
103
104     /* Create a loop so we may use break. */
105     AddLoop (oursp, 0, ExitLabel, 0, 0);
106
107     /* Create the collection for the case node tree */
108     Nodes = NewCollection ();
109
110     /* Clear the label for the default branch */
111     DefaultLabel = 0;
112
113     /* Parse the labels */
114     while (CurTok.Tok != TOK_RCURLY) {
115
116         while (CurTok.Tok == TOK_CASE || CurTok.Tok == TOK_DEFAULT) {
117
118             /* Parse the selector */
119             if (CurTok.Tok == TOK_CASE) {
120
121                 /* Skip the "case" token */
122                 NextToken ();
123
124                 /* Read the selector expression */
125                 ConstExpr (&CaseExpr);
126                 if (!IsClassInt (CaseExpr.Type)) {
127                     Error ("Switch quantity not an integer");
128                 }
129
130                 /* Check the range of the expression */
131                 Val = CaseExpr.ConstVal;
132                 switch (SwitchExprType) {
133
134                     case T_SCHAR:
135                         /* Signed char */
136                         if (Val < -128 || Val > 127) {
137                             Error ("Range error");
138                         }
139                         break;
140
141                     case T_UCHAR:
142                         if (Val < 0 || Val > 255) {
143                             Error ("Range error");
144                         }
145                         break;
146
147                     case T_SHORT:
148                     case T_INT:
149                         if (Val < -32768 || Val > 32767) {
150                             Error ("Range error");
151                         }
152                         break;
153
154                     case T_USHORT:
155                     case T_UINT:
156                         if (Val < 0 || Val > 65535) {
157                             Error ("Range error");
158                         }
159                         break;
160
161                     case T_LONG:
162                     case T_ULONG:
163                         break;
164
165                     default:
166                         Internal ("Invalid type: %04X", SwitchExprType);
167                 }
168
169                 /* Insert the case selector into the selector table */
170                 CaseLabel = InsertCaseValue (Nodes, Val, Depth);
171
172                 /* Define this label */
173                 g_defcodelabel (CaseLabel);
174
175                 /* Skip the colon */
176                 ConsumeColon ();
177
178             } else {
179
180                 /* Default case */
181                 NextToken ();
182
183                 /* Check if we do already have a default branch */
184                 if (DefaultLabel == 0) {
185
186                     /* Generate and emit the default label */
187                     DefaultLabel = GetLocalLabel ();
188                     g_defcodelabel (DefaultLabel);
189
190                 } else {
191                     /* We had the default label already */
192                     Error ("Duplicate `default' case");
193                 }
194
195                 /* Skip the colon */
196                 ConsumeColon ();
197
198             }
199
200         }
201
202         /* Parse statements */
203         if (CurTok.Tok != TOK_RCURLY) {
204             HaveBreak = Statement (0);
205         }
206     }
207
208     /* Check if we had any labels */
209     if (CollCount (Nodes) == 0 && DefaultLabel == 0) {
210
211         Warning ("No case labels");
212
213     } else {
214                                    
215         CodeMark SwitchCodeStart;
216
217         /* If the last statement did not have a break, we may have an open
218          * label (maybe from an if or similar). Emitting code and then moving
219          * this code to the top will also move the label to the top which is
220          * wrong. So if the last statement did not have a break (which would
221          * carry the label), add a jump to the exit. If it is useless, the
222          * optimizer will remove it later.
223          */
224         if (!HaveBreak) {
225             g_jump (ExitLabel);
226         }
227
228         /* Remember the current position */
229         SwitchCodeStart = GetCodePos();
230
231         /* Generate code */
232         g_switch (Nodes, DefaultLabel? DefaultLabel : ExitLabel, Depth);
233
234         /* Move the code to the front */
235         MoveCode (SwitchCodeStart, GetCodePos(), CaseCodeStart);
236
237     }
238
239     /* Define the exit label */
240     g_defcodelabel (ExitLabel);
241
242     /* Eat the closing curly brace */
243     NextToken ();
244
245     /* Free the case value tree */
246     FreeCaseNodeColl (Nodes);
247
248     /* End the loop */
249     DelLoop ();
250 }
251
252
253