]> git.sur5r.net Git - cc65/blob - src/cc65/swstmt.c
Fixed the macro versions of several inline functions.
[cc65] / src / cc65 / swstmt.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                 swstmt.c                                  */
4 /*                                                                           */
5 /*                        Parse the switch statement                         */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 1998-2008 Ullrich von Bassewitz                                       */
10 /*               Roemerstrasse 52                                            */
11 /*               D-70794 Filderstadt                                         */
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 /*                                   Data                                    */
60 /*****************************************************************************/
61
62
63
64 typedef struct SwitchCtrl SwitchCtrl;
65 struct 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 */
70
71
72
73 };
74
75 /* Pointer to current switch control struct */
76 static SwitchCtrl* Switch = 0;
77
78
79
80 /*****************************************************************************/
81 /*                                   Code                                    */
82 /*****************************************************************************/
83
84
85
86 void SwitchStatement (void)
87 /* Handle a switch statement for chars with a cmp cascade for the selector */
88 {
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     SwitchCtrl* OldSwitch;      /* Pointer to old switch control data */
97     SwitchCtrl  SwitchData;     /* New switch data */
98
99
100     /* Eat the "switch" token */
101     NextToken ();
102
103     /* Read the switch expression and load it into the primary. It must have
104      * integer type.
105      */
106     ConsumeLParen ();
107     Expression0 (&SwitchExpr);
108     if (!IsClassInt (SwitchExpr.Type))  {
109         Error ("Switch quantity is not an integer");
110         /* To avoid any compiler errors, make the expression a valid int */
111         ED_MakeConstAbsInt (&SwitchExpr, 1);
112     }
113     ConsumeRParen ();
114
115     /* Add a jump to the switch code. This jump is usually unnecessary,
116      * because the switch code will moved up just behind the switch
117      * expression. However, in rare cases, there's a label at the end of
118      * the switch expression. This label will not get moved, so the code
119      * jumps around the switch code, and after moving the switch code,
120      * things look really weird. If we add a jump here, we will never have
121      * a label attached to the current code position, and the jump itself
122      * will get removed by the optimizer if it is unnecessary.
123      */
124     SwitchCodeLabel = GetLocalLabel ();
125     g_jump (SwitchCodeLabel);
126
127     /* Remember the current code position. We will move the switch code
128      * to this position later.
129      */
130     GetCodePos (&CaseCodeStart);
131
132     /* Setup the control structure, save the old and activate the new one */
133     SwitchData.Nodes        = NewCollection ();
134     SwitchData.ExprType     = UnqualifiedType (SwitchExpr.Type[0].C);
135     SwitchData.Depth        = SizeOf (SwitchExpr.Type);
136     SwitchData.DefaultLabel = 0;
137     OldSwitch = Switch;
138     Switch = &SwitchData;
139
140     /* Get the exit label for the switch statement */
141     ExitLabel = GetLocalLabel ();
142
143     /* Create a loop so we may use break. */
144     AddLoop (ExitLabel, 0);
145
146     /* Make sure a curly brace follows */
147     if (CurTok.Tok != TOK_LCURLY) {
148         Error ("`{' expected");
149     }
150
151     /* Parse the following statement, which will actually be a compound
152      * statement because of the curly brace at the current input position
153      */
154     HaveBreak = Statement (0);
155
156     /* Check if we had any labels */
157     if (CollCount (SwitchData.Nodes) == 0 && SwitchData.DefaultLabel == 0) {
158         Warning ("No case labels");
159     }
160
161     /* If the last statement did not have a break, we may have an open
162      * label (maybe from an if or similar). Emitting code and then moving
163      * this code to the top will also move the label to the top which is
164      * wrong. So if the last statement did not have a break (which would
165      * carry the label), add a jump to the exit. If it is useless, the
166      * optimizer will remove it later.
167      */
168     if (!HaveBreak) {
169         g_jump (ExitLabel);
170     }
171
172     /* Remember the current position */
173     GetCodePos (&SwitchCodeStart);
174
175     /* Output the switch code label */
176     g_defcodelabel (SwitchCodeLabel);
177
178     /* Generate code */
179     if (SwitchData.DefaultLabel == 0) {
180         /* No default label, use switch exit */
181         SwitchData.DefaultLabel = ExitLabel;
182     }
183     g_switch (SwitchData.Nodes, SwitchData.DefaultLabel, SwitchData.Depth);
184
185     /* Move the code to the front */
186     GetCodePos (&SwitchCodeEnd);
187     MoveCode (&SwitchCodeStart, &SwitchCodeEnd, &CaseCodeStart);
188
189     /* Define the exit label */
190     g_defcodelabel (ExitLabel);
191                                                                     
192     /* Exit the loop */
193     DelLoop ();
194
195     /* Switch back to the enclosing switch statement if any */
196     Switch = OldSwitch;
197
198     /* Free the case value tree */
199     FreeCaseNodeColl (SwitchData.Nodes);
200 }
201
202
203
204 void CaseLabel (void)
205 /* Handle a case sabel */
206 {
207     ExprDesc CaseExpr;          /* Case label expression */
208     long     Val;               /* Case label value */
209     unsigned CodeLabel;         /* Code label for this case */
210
211
212     /* Skip the "case" token */
213     NextToken ();
214
215     /* Read the selector expression */
216     ConstAbsIntExpr (hie1, &CaseExpr);
217     Val = CaseExpr.IVal;
218
219     /* Now check if we're inside a switch statement */
220     if (Switch != 0) {
221
222         /* Check the range of the expression */
223         switch (Switch->ExprType) {
224
225             case T_SCHAR:
226                 /* Signed char */
227                 if (Val < -128 || Val > 127) {
228                     Error ("Range error");
229                 }
230                 break;
231
232             case T_UCHAR:
233                 if (Val < 0 || Val > 255) {
234                     Error ("Range error");
235                 }
236                 break;
237
238             case T_SHORT:
239             case T_INT:
240                 if (Val < -32768 || Val > 32767) {
241                     Error ("Range error");
242                 }
243                 break;
244
245             case T_USHORT:
246             case T_UINT:
247                 if (Val < 0 || Val > 65535) {
248                     Error ("Range error");
249                 }
250                 break;
251
252             case T_LONG:
253             case T_ULONG:
254                 break;
255
256             default:
257                 Internal ("Invalid type: %06lX", Switch->ExprType);
258         }
259
260         /* Insert the case selector into the selector table */
261         CodeLabel = InsertCaseValue (Switch->Nodes, Val, Switch->Depth);
262
263         /* Define this label */
264         g_defcodelabel (CodeLabel);
265
266     } else {
267
268         /* case keyword outside a switch statement */
269         Error ("Case label not within a switch statement");
270
271     }
272
273     /* Skip the colon */
274     ConsumeColon ();
275 }
276
277
278
279 void DefaultLabel (void)
280 /* Handle a default label */
281 {
282     /* Default case */
283     NextToken ();
284
285     /* Now check if we're inside a switch statement */
286     if (Switch != 0) {
287
288         /* Check if we do already have a default branch */
289         if (Switch->DefaultLabel == 0) {
290
291             /* Generate and emit the default label */
292             Switch->DefaultLabel = GetLocalLabel ();
293             g_defcodelabel (Switch->DefaultLabel);
294
295         } else {
296             /* We had the default label already */
297             Error ("Multiple default labels in one switch");
298         }
299
300     } else {
301
302         /* case keyword outside a switch statement */
303         Error ("`default' label not within a switch statement");
304
305     }
306
307     /* Skip the colon */
308     ConsumeColon ();
309 }
310
311
312