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