]> git.sur5r.net Git - cc65/blob - src/cc65/swstmt.c
Amiga install files by Stefan Haubenthal.
[cc65] / src / cc65 / swstmt.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                 swstmt.c                                  */
4 /*                                                                           */
5 /*                        Parse the switch statement                         */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 1998-2006 Ullrich von Bassewitz                                       */
10 /*               Römerstraße 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 /*                                   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     TypeCode SwitchExprType;    /* Basic switch expression type */
71     CodeMark CaseCodeStart;     /* Start of code marker */
72     CodeMark SwitchCodeStart;   /* Start of switch code */
73     CodeMark SwitchCodeEnd;     /* End of switch code */
74     unsigned Depth;             /* Number of bytes the selector type has */
75     unsigned ExitLabel;         /* Exit label */
76     unsigned CaseLabel;         /* Label for case */
77     unsigned DefaultLabel;      /* Label for the default branch */
78     unsigned SwitchCodeLabel;   /* Label for the switch code */
79     long     Val;               /* Case label value */
80     int      HaveBreak = 0;     /* True if the last statement had a break */
81
82
83     /* Eat the "switch" token */
84     NextToken ();
85
86     /* Read the switch expression and load it into the primary. It must have
87      * integer type.
88      */
89     ConsumeLParen ();
90     Expression0 (&SwitchExpr);
91     if (!IsClassInt (SwitchExpr.Type))  {
92         Error ("Switch quantity is not an integer");
93         /* To avoid any compiler errors, make the expression a valid int */
94         ED_MakeConstAbsInt (&SwitchExpr, 1);
95     }
96     ConsumeRParen ();
97
98     /* Add a jump to the switch code. This jump is usually unnecessary,
99      * because the switch code will moved up just behind the switch
100      * expression. However, in rare cases, there's a label at the end of
101      * the switch expression. This label will not get moved, so the code
102      * jumps around the switch code, and after moving the switch code,
103      * things look really weird. If we add a jump here, we will never have
104      * a label attached to the current code position, and the jump itself
105      * will get removed by the optimizer if it is unnecessary.
106      */
107     SwitchCodeLabel = GetLocalLabel ();
108     g_jump (SwitchCodeLabel);
109
110     /* Remember the current code position. We will move the switch code
111      * to this position later.
112      */
113     GetCodePos (&CaseCodeStart);
114
115     /* Opening curly brace */
116     ConsumeLCurly ();
117
118     /* Get the unqualified type of the switch expression */
119     SwitchExprType = UnqualifiedType (SwitchExpr.Type[0].C);
120
121     /* Get the number of bytes the selector type has */
122     Depth = SizeOf (SwitchExpr.Type);
123     CHECK (Depth == SIZEOF_CHAR || Depth == SIZEOF_INT || Depth == SIZEOF_LONG);
124
125     /* Get the exit label for the switch statement */
126     ExitLabel = GetLocalLabel ();
127
128     /* Create a loop so we may use break. */
129     AddLoop (ExitLabel, 0);
130
131     /* Create the collection for the case node tree */
132     Nodes = NewCollection ();
133
134     /* Clear the label for the default branch */
135     DefaultLabel = 0;
136
137     /* Parse the labels */
138     while (CurTok.Tok != TOK_RCURLY) {
139
140         while (CurTok.Tok == TOK_CASE || CurTok.Tok == TOK_DEFAULT) {
141
142             /* Parse the selector */
143             if (CurTok.Tok == TOK_CASE) {
144
145                 /* Skip the "case" token */
146                 NextToken ();
147
148                 /* Read the selector expression */
149                 ConstAbsIntExpr (hie1, &CaseExpr);
150
151                 /* Check the range of the expression */
152                 Val = CaseExpr.IVal;
153                 switch (SwitchExprType) {
154
155                     case T_SCHAR:
156                         /* Signed char */
157                         if (Val < -128 || Val > 127) {
158                             Error ("Range error");
159                         }
160                         break;
161
162                     case T_UCHAR:
163                         if (Val < 0 || Val > 255) {
164                             Error ("Range error");
165                         }
166                         break;
167
168                     case T_SHORT:
169                     case T_INT:
170                         if (Val < -32768 || Val > 32767) {
171                             Error ("Range error");
172                         }
173                         break;
174
175                     case T_USHORT:
176                     case T_UINT:
177                         if (Val < 0 || Val > 65535) {
178                             Error ("Range error");
179                         }
180                         break;
181
182                     case T_LONG:
183                     case T_ULONG:
184                         break;
185
186                     default:
187                         Internal ("Invalid type: %06lX", SwitchExprType);
188                 }
189
190                 /* Insert the case selector into the selector table */
191                 CaseLabel = InsertCaseValue (Nodes, Val, Depth);
192
193                 /* Define this label */
194                 g_defcodelabel (CaseLabel);
195
196                 /* Skip the colon */
197                 ConsumeColon ();
198
199             } else {
200
201                 /* Default case */
202                 NextToken ();
203
204                 /* Check if we do already have a default branch */
205                 if (DefaultLabel == 0) {
206
207                     /* Generate and emit the default label */
208                     DefaultLabel = GetLocalLabel ();
209                     g_defcodelabel (DefaultLabel);
210
211                 } else {
212                     /* We had the default label already */
213                     Error ("Duplicate `default' case");
214                 }
215
216                 /* Skip the colon */
217                 ConsumeColon ();
218
219             }
220
221         }
222
223         /* Parse statements */
224         if (CurTok.Tok != TOK_RCURLY) {
225             HaveBreak = Statement (0);
226         }
227     }
228
229     /* Check if we had any labels */
230     if (CollCount (Nodes) == 0 && DefaultLabel == 0) {
231
232         Warning ("No case labels");
233
234     }
235
236     /* If the last statement did not have a break, we may have an open
237      * label (maybe from an if or similar). Emitting code and then moving
238      * this code to the top will also move the label to the top which is
239      * wrong. So if the last statement did not have a break (which would
240      * carry the label), add a jump to the exit. If it is useless, the
241      * optimizer will remove it later.
242      */
243     if (!HaveBreak) {
244         g_jump (ExitLabel);
245     }
246
247     /* Remember the current position */
248     GetCodePos (&SwitchCodeStart);
249
250     /* Output the switch code label */
251     g_defcodelabel (SwitchCodeLabel);
252
253     /* Generate code */
254     g_switch (Nodes, DefaultLabel? DefaultLabel : ExitLabel, Depth);
255
256     /* Move the code to the front */
257     GetCodePos (&SwitchCodeEnd);
258     MoveCode (&SwitchCodeStart, &SwitchCodeEnd, &CaseCodeStart);
259
260     /* Define the exit label */
261     g_defcodelabel (ExitLabel);
262
263     /* Eat the closing curly brace */
264     NextToken ();
265
266     /* Free the case value tree */
267     FreeCaseNodeColl (Nodes);
268
269     /* End the loop */
270     DelLoop ();
271 }
272
273
274