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