]> git.sur5r.net Git - cc65/blob - src/cc65/swstmt.c
add gotox, gotoy, and gotoxy
[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     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 */
99
100
101     /* Eat the "switch" token */
102     NextToken ();
103
104     /* Read the switch expression and load it into the primary. It must have
105      * integer type.
106      */
107     ConsumeLParen ();
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);
113     }
114     ConsumeRParen ();
115
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.
124      */
125     SwitchCodeLabel = GetLocalLabel ();
126     g_jump (SwitchCodeLabel);
127
128     /* Remember the current code position. We will move the switch code
129      * to this position later.
130      */
131     GetCodePos (&CaseCodeStart);
132
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;
138     OldSwitch = Switch;
139     Switch = &SwitchData;
140
141     /* Get the exit label for the switch statement */
142     ExitLabel = GetLocalLabel ();
143
144     /* Create a loop so we may use break. */
145     AddLoop (ExitLabel, 0);
146
147     /* Make sure a curly brace follows */
148     if (CurTok.Tok != TOK_LCURLY) {
149         Error ("`{' expected");
150     }
151
152     /* Parse the following statement, which will actually be a compound
153      * statement because of the curly brace at the current input position
154      */
155     HaveBreak = Statement (&RCurlyBrace);
156
157     /* Check if we had any labels */
158     if (CollCount (SwitchData.Nodes) == 0 && SwitchData.DefaultLabel == 0) {
159         Warning ("No case labels");
160     }
161
162     /* If the last statement did not have a break, we may have an open
163      * label (maybe from an if or similar). Emitting code and then moving
164      * this code to the top will also move the label to the top which is
165      * wrong. So if the last statement did not have a break (which would
166      * carry the label), add a jump to the exit. If it is useless, the
167      * optimizer will remove it later.
168      */
169     if (!HaveBreak) {
170         g_jump (ExitLabel);
171     }
172
173     /* Remember the current position */
174     GetCodePos (&SwitchCodeStart);
175
176     /* Output the switch code label */
177     g_defcodelabel (SwitchCodeLabel);
178
179     /* Generate code */
180     if (SwitchData.DefaultLabel == 0) {
181         /* No default label, use switch exit */
182         SwitchData.DefaultLabel = ExitLabel;
183     }
184     g_switch (SwitchData.Nodes, SwitchData.DefaultLabel, SwitchData.Depth);
185
186     /* Move the code to the front */
187     GetCodePos (&SwitchCodeEnd);
188     MoveCode (&SwitchCodeStart, &SwitchCodeEnd, &CaseCodeStart);
189
190     /* Define the exit label */
191     g_defcodelabel (ExitLabel);
192
193     /* Exit the loop */
194     DelLoop ();
195
196     /* Switch back to the enclosing switch statement if any */
197     Switch = OldSwitch;
198
199     /* Free the case value tree */
200     FreeCaseNodeColl (SwitchData.Nodes);
201
202     /* If the case statement was (correctly) terminated by a closing curly
203      * brace, skip it now.
204      */
205     if (RCurlyBrace) {
206         NextToken ();
207     }
208 }
209
210
211
212 void CaseLabel (void)
213 /* Handle a case sabel */
214 {
215     ExprDesc CaseExpr;          /* Case label expression */
216     long     Val;               /* Case label value */
217     unsigned CodeLabel;         /* Code label for this case */
218
219
220     /* Skip the "case" token */
221     NextToken ();
222
223     /* Read the selector expression */
224     ConstAbsIntExpr (hie1, &CaseExpr);
225     Val = CaseExpr.IVal;
226
227     /* Now check if we're inside a switch statement */
228     if (Switch != 0) {
229
230         /* Check the range of the expression */
231         switch (Switch->ExprType) {
232
233             case T_SCHAR:
234                 /* Signed char */
235                 if (Val < -128 || Val > 127) {
236                     Error ("Range error");
237                 }
238                 break;
239
240             case T_UCHAR:
241                 if (Val < 0 || Val > 255) {
242                     Error ("Range error");
243                 }
244                 break;
245
246             case T_SHORT:
247             case T_INT:
248                 if (Val < -32768 || Val > 32767) {
249                     Error ("Range error");
250                 }
251                 break;
252
253             case T_USHORT:
254             case T_UINT:
255                 if (Val < 0 || Val > 65535) {
256                     Error ("Range error");
257                 }
258                 break;
259
260             case T_LONG:
261             case T_ULONG:
262                 break;
263
264             default:
265                 Internal ("Invalid type: %06lX", Switch->ExprType);
266         }
267
268         /* Insert the case selector into the selector table */
269         CodeLabel = InsertCaseValue (Switch->Nodes, Val, Switch->Depth);
270
271         /* Define this label */
272         g_defcodelabel (CodeLabel);
273
274     } else {
275
276         /* case keyword outside a switch statement */
277         Error ("Case label not within a switch statement");
278
279     }
280
281     /* Skip the colon */
282     ConsumeColon ();
283 }
284
285
286
287 void DefaultLabel (void)
288 /* Handle a default label */
289 {
290     /* Default case */
291     NextToken ();
292
293     /* Now check if we're inside a switch statement */
294     if (Switch != 0) {
295
296         /* Check if we do already have a default branch */
297         if (Switch->DefaultLabel == 0) {
298
299             /* Generate and emit the default label */
300             Switch->DefaultLabel = GetLocalLabel ();
301             g_defcodelabel (Switch->DefaultLabel);
302
303         } else {
304             /* We had the default label already */
305             Error ("Multiple default labels in one switch");
306         }
307
308     } else {
309
310         /* case keyword outside a switch statement */
311         Error ("`default' label not within a switch statement");
312
313     }
314
315     /* Skip the colon */
316     ConsumeColon ();
317 }
318
319
320