1 /*****************************************************************************/
5 /* Parse a statement */
9 /* (C) 1998-2001 Ullrich von Bassewitz */
11 /* D-70597 Stuttgart */
12 /* EMail: uz@cc65.org */
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. */
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: */
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 */
32 /*****************************************************************************/
64 /*****************************************************************************/
65 /* Helper functions */
66 /*****************************************************************************/
70 static void CheckTok (token_t Tok, const char* Msg, int* PendingToken)
71 /* Helper function for Statement. Will check for Tok and print Msg if not
72 * found. If PendingToken is NULL, it will the skip the token, otherwise
73 * it will store one to PendingToken.
76 if (CurTok.Tok != Tok) {
78 } else if (PendingToken) {
87 static void CheckSemi (int* PendingToken)
88 /* Helper function for Statement. Will call CheckTok with the parameters
92 CheckTok (TOK_SEMI, "`;' expected", PendingToken);
97 static void SkipPending (int PendingToken)
98 /* Skip the pending token if we have one */
107 /*****************************************************************************/
109 /*****************************************************************************/
113 static int IfStatement (void)
114 /* Handle an 'if' statement */
122 /* Generate a jump label and parse the condition */
123 Label1 = GetLocalLabel ();
124 TestInParens (Label1, 0);
126 /* Parse the if body */
127 GotBreak = Statement (0);
129 /* Else clause present? */
130 if (CurTok.Tok != TOK_ELSE) {
132 g_defcodelabel (Label1);
134 /* Since there's no else clause, we're not sure, if the a break
135 * statement is really executed.
141 /* Generate a jump around the else branch */
142 unsigned Label2 = GetLocalLabel ();
148 /* Define the target for the first test */
149 g_defcodelabel (Label1);
151 /* Total break only if both branches had a break. */
152 GotBreak &= Statement (0);
154 /* Generate the label for the else clause */
155 g_defcodelabel (Label2);
164 static void DoStatement (void)
165 /* Handle the 'do' statement */
167 /* Get the loop control labels */
168 unsigned loop = GetLocalLabel ();
169 unsigned lab = GetLocalLabel ();
171 /* Skip the while token */
174 /* Add the loop to the loop stack */
175 AddLoop (oursp, loop, lab, 0, 0);
177 /* Define the head label */
178 g_defcodelabel (loop);
180 /* Parse the loop body */
183 /* Parse the end condition */
184 Consume (TOK_WHILE, "`while' expected");
185 TestInParens (loop, 1);
188 /* Define the break label */
189 g_defcodelabel (lab);
191 /* Remove the loop from the loop stack */
197 static void WhileStatement (void)
198 /* Handle the 'while' statement */
202 /* Get the loop control labels */
203 unsigned loop = GetLocalLabel ();
204 unsigned lab = GetLocalLabel ();
206 /* Skip the while token */
209 /* Add the loop to the loop stack */
210 AddLoop (oursp, loop, lab, 0, 0);
212 /* Define the head label */
213 g_defcodelabel (loop);
215 /* Test the loop condition */
216 TestInParens (lab, 0);
219 Statement (&PendingToken);
221 /* Jump back to loop top */
225 g_defcodelabel (lab);
227 /* Eat remaining tokens that were delayed because of line info
230 SkipPending (PendingToken);
232 /* Remove the loop from the loop stack */
238 static void ReturnStatement (void)
239 /* Handle the 'return' statement */
244 if (CurTok.Tok != TOK_SEMI) {
246 /* Check if the function has a return value declared */
247 if (F_HasVoidReturn (CurrentFunc)) {
248 Error ("Returning a value in function with return type void");
251 /* Evaluate the return expression. Result will be in primary */
254 /* Convert the return value to the type of the function result */
255 if (!F_HasVoidReturn (CurrentFunc)) {
256 assignadjust (F_GetReturnType (CurrentFunc), &lval);
259 } else if (!F_HasVoidReturn (CurrentFunc) && !F_HasOldStyleIntRet (CurrentFunc)) {
260 Error ("Function `%s' must return a value", F_GetFuncName (CurrentFunc));
263 /* Cleanup the stack in case we're inside a block with locals */
264 g_space (oursp - F_GetTopLevelSP (CurrentFunc));
266 /* Output a jump to the function exit code */
267 g_jump (F_GetRetLab (CurrentFunc));
272 static void BreakStatement (void)
273 /* Handle the 'break' statement */
280 /* Get the current loop descriptor */
283 /* Check if we are inside a loop */
285 /* Error: No current loop */
286 Error ("`break' statement not within loop or switch");
290 /* Correct the stack pointer if needed */
291 g_space (oursp - L->StackPtr);
293 /* Jump to the exit label of the loop */
299 static void ContinueStatement (void)
300 /* Handle the 'continue' statement */
304 /* Skip the continue */
307 /* Get the current loop descriptor */
310 /* Search for the correct loop */
319 /* Did we find it? */
321 Error ("`continue' statement not within a loop");
325 /* Correct the stackpointer if needed */
326 g_space (oursp - L->StackPtr);
328 /* Output the loop code */
338 static void ForStatement (void)
339 /* Handle a 'for' statement */
344 CodeMark IncExprStart;
348 /* Get several local labels needed later */
349 unsigned TestLabel = GetLocalLabel ();
350 unsigned lab = GetLocalLabel ();
351 unsigned IncLabel = GetLocalLabel ();
352 unsigned lstat = GetLocalLabel ();
354 /* Skip the FOR token */
357 /* Add the loop to the loop stack */
358 AddLoop (oursp, TestLabel, lab, IncLabel, lstat);
360 /* Skip the opening paren */
363 /* Parse the initializer expression */
364 if (CurTok.Tok != TOK_SEMI) {
369 /* Label for the test expressions */
370 g_defcodelabel (TestLabel);
372 /* Parse the test expression */
373 if (CurTok.Tok != TOK_SEMI) {
381 /* Remember the start of the increment expression */
382 IncExprStart = GetCodePos();
384 /* Label for the increment expression */
385 g_defcodelabel (IncLabel);
387 /* Parse the increment expression */
388 HaveIncExpr = (CurTok.Tok != TOK_RPAREN);
393 /* Jump to the test */
396 /* Remember the end of the increment expression */
397 IncExprEnd = GetCodePos();
399 /* Skip the closing paren */
403 g_defcodelabel (lstat);
404 Statement (&PendingToken);
406 /* If we had an increment expression, move the code to the bottom of
407 * the loop. In this case we don't need to jump there at the end of
411 MoveCode (IncExprStart, IncExprEnd, GetCodePos());
413 /* Jump back to the increment expression */
417 /* Skip a pending token if we have one */
418 SkipPending (PendingToken);
420 /* Declare the break label */
421 g_defcodelabel (lab);
423 /* Remove the loop from the loop stack */
429 static int CompoundStatement (void)
430 /* Compound statement. Allow any number of statements inside braces. The
431 * function returns true if the last statement was a break or return.
436 /* Remember the stack at block entry */
437 int OldStack = oursp;
439 /* Enter a new lexical level */
442 /* Parse local variable declarations if any */
445 /* Now process statements in this block */
447 while (CurTok.Tok != TOK_RCURLY) {
448 if (CurTok.Tok != TOK_CEOF) {
449 GotBreak = Statement (0);
455 /* Clean up the stack. */
457 g_space (oursp - OldStack);
461 /* Emit references to imports/exports for this block */
464 /* Leave the lexical level */
472 int Statement (int* PendingToken)
473 /* Statement parser. Returns 1 if the statement does a return/break, returns
474 * 0 otherwise. If the PendingToken pointer is not NULL, the function will
475 * not skip the terminating token of the statement (closing brace or
476 * semicolon), but store true if there is a pending token, and false if there
477 * is none. The token is always checked, so there is no need for the caller to
478 * check this token, it must be skipped, however. If the argument pointer is
479 * NULL, the function will skip the token.
485 /* Assume no pending token */
490 /* Check for a label */
491 if (CurTok.Tok == TOK_IDENT && NextTok.Tok == TOK_COLON) {
493 /* Special handling for a label */
498 switch (CurTok.Tok) {
502 GotBreak = CompoundStatement ();
503 CheckTok (TOK_RCURLY, "`{' expected", PendingToken);
507 return IfStatement ();
523 CheckSemi (PendingToken);
528 CheckSemi (PendingToken);
532 ContinueStatement ();
533 CheckSemi (PendingToken);
542 CheckSemi (PendingToken);
547 CheckSemi (PendingToken);
555 /* Actual statement */
557 CheckSemi (PendingToken);