1 /*****************************************************************************/
5 /* Parse a statement */
9 /* (C) 1998-2009, Ullrich von Bassewitz */
10 /* Roemerstrasse 52 */
11 /* D-70794 Filderstadt */
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 /*****************************************************************************/
68 /*****************************************************************************/
69 /* Helper functions */
70 /*****************************************************************************/
74 static void CheckLabelWithoutStatement (void)
75 /* Called from Statement() after a label definition. Will check for a
76 * following closing curly brace. This means that a label is not followed
77 * by a statement which is required by the standard. Output an error if so.
80 if (CurTok.Tok == TOK_RCURLY) {
81 Error ("Label at end of compound statement");
87 static void CheckTok (token_t Tok, const char* Msg, int* PendingToken)
88 /* Helper function for Statement. Will check for Tok and print Msg if not
89 * found. If PendingToken is NULL, it will the skip the token, otherwise
90 * it will store one to PendingToken.
93 if (CurTok.Tok != Tok) {
95 } else if (PendingToken) {
104 static void CheckSemi (int* PendingToken)
105 /* Helper function for Statement. Will check for a semicolon and print an
106 * error message if not found (plus some error recovery). If PendingToken is
107 * NULL, it will the skip the token, otherwise it will store one to
109 * This function is a special version of CheckTok with the addition of the
113 int HaveToken = (CurTok.Tok == TOK_SEMI);
115 Error ("`;' expected");
116 /* Try to be smart about errors */
117 if (CurTok.Tok == TOK_COLON || CurTok.Tok == TOK_COMMA) {
132 static void SkipPending (int PendingToken)
133 /* Skip the pending token if we have one */
142 /*****************************************************************************/
144 /*****************************************************************************/
148 static int IfStatement (void)
149 /* Handle an 'if' statement */
158 /* Generate a jump label and parse the condition */
159 Label1 = GetLocalLabel ();
160 TestResult = TestInParens (Label1, 0);
162 /* Parse the if body */
163 GotBreak = Statement (0);
165 /* Else clause present? */
166 if (CurTok.Tok != TOK_ELSE) {
168 g_defcodelabel (Label1);
170 /* Since there's no else clause, we're not sure, if the a break
171 * statement is really executed.
177 /* Generate a jump around the else branch */
178 unsigned Label2 = GetLocalLabel ();
184 /* If the if expression was always true, the code in the else branch
185 * is never executed. Output a warning if this is the case.
187 if (TestResult == TESTEXPR_TRUE) {
188 Warning ("Unreachable code");
191 /* Define the target for the first test */
192 g_defcodelabel (Label1);
194 /* Total break only if both branches had a break. */
195 GotBreak &= Statement (0);
197 /* Generate the label for the else clause */
198 g_defcodelabel (Label2);
207 static void DoStatement (void)
208 /* Handle the 'do' statement */
210 /* Get the loop control labels */
211 unsigned LoopLabel = GetLocalLabel ();
212 unsigned BreakLabel = GetLocalLabel ();
213 unsigned ContinueLabel = GetLocalLabel ();
215 /* Skip the while token */
218 /* Add the loop to the loop stack */
219 AddLoop (BreakLabel, ContinueLabel);
221 /* Define the loop label */
222 g_defcodelabel (LoopLabel);
224 /* Parse the loop body */
227 /* Output the label for a continue */
228 g_defcodelabel (ContinueLabel);
230 /* Parse the end condition */
231 Consume (TOK_WHILE, "`while' expected");
232 TestInParens (LoopLabel, 1);
235 /* Define the break label */
236 g_defcodelabel (BreakLabel);
238 /* Remove the loop from the loop stack */
244 static void WhileStatement (void)
245 /* Handle the 'while' statement */
249 /* Get the loop control labels */
250 unsigned LoopLabel = GetLocalLabel ();
251 unsigned BreakLabel = GetLocalLabel ();
253 /* Skip the while token */
256 /* Add the loop to the loop stack. In case of a while loop, the loop head
257 * label is used for continue statements.
259 AddLoop (BreakLabel, LoopLabel);
261 /* Define the head label */
262 g_defcodelabel (LoopLabel);
264 /* Test the loop condition */
265 TestInParens (BreakLabel, 0);
268 Statement (&PendingToken);
270 /* Jump back to loop top */
274 g_defcodelabel (BreakLabel);
276 /* Eat remaining tokens that were delayed because of line info
279 SkipPending (PendingToken);
281 /* Remove the loop from the loop stack */
287 static void ReturnStatement (void)
288 /* Handle the 'return' statement */
293 if (CurTok.Tok != TOK_SEMI) {
295 /* Evaluate the return expression */
298 /* If we return something in a void function, print an error and
299 * ignore the value. Otherwise convert the value to the type of the
302 if (F_HasVoidReturn (CurrentFunc)) {
303 Error ("Returning a value in function with return type void");
305 /* Convert the return value to the type of the function result */
306 TypeConversion (&Expr, F_GetReturnType (CurrentFunc));
308 /* Load the value into the primary */
309 LoadExpr (CF_NONE, &Expr);
312 } else if (!F_HasVoidReturn (CurrentFunc) && !F_HasOldStyleIntRet (CurrentFunc)) {
313 Error ("Function `%s' must return a value", F_GetFuncName (CurrentFunc));
316 /* Mark the function as having a return statement */
317 F_HasReturn (CurrentFunc);
319 /* Cleanup the stack in case we're inside a block with locals */
320 g_space (StackPtr - F_GetTopLevelSP (CurrentFunc));
322 /* Output a jump to the function exit code */
323 g_jump (F_GetRetLab (CurrentFunc));
328 static void BreakStatement (void)
329 /* Handle the 'break' statement */
336 /* Get the current loop descriptor */
339 /* Check if we are inside a loop */
341 /* Error: No current loop */
342 Error ("`break' statement not within loop or switch");
346 /* Correct the stack pointer if needed */
347 g_space (StackPtr - L->StackPtr);
349 /* Jump to the exit label of the loop */
350 g_jump (L->BreakLabel);
355 static void ContinueStatement (void)
356 /* Handle the 'continue' statement */
360 /* Skip the continue */
363 /* Get the current loop descriptor */
366 /* Search for a loop that has a continue label. */
368 if (L->ContinueLabel) {
375 /* Did we find it? */
377 Error ("`continue' statement not within a loop");
381 /* Correct the stackpointer if needed */
382 g_space (StackPtr - L->StackPtr);
384 /* Jump to next loop iteration */
385 g_jump (L->ContinueLabel);
390 static void ForStatement (void)
391 /* Handle a 'for' statement */
396 CodeMark IncExprStart;
400 /* Get several local labels needed later */
401 unsigned TestLabel = GetLocalLabel ();
402 unsigned BreakLabel = GetLocalLabel ();
403 unsigned IncLabel = GetLocalLabel ();
404 unsigned BodyLabel = GetLocalLabel ();
406 /* Skip the FOR token */
409 /* Add the loop to the loop stack. A continue jumps to the start of the
410 * the increment condition.
412 AddLoop (BreakLabel, IncLabel);
414 /* Skip the opening paren */
417 /* Parse the initializer expression */
418 if (CurTok.Tok != TOK_SEMI) {
419 Expression0 (&lval1);
423 /* Label for the test expressions */
424 g_defcodelabel (TestLabel);
426 /* Parse the test expression */
427 if (CurTok.Tok != TOK_SEMI) {
435 /* Remember the start of the increment expression */
436 GetCodePos (&IncExprStart);
438 /* Label for the increment expression */
439 g_defcodelabel (IncLabel);
441 /* Parse the increment expression */
442 HaveIncExpr = (CurTok.Tok != TOK_RPAREN);
444 Expression0 (&lval3);
447 /* Jump to the test */
450 /* Remember the end of the increment expression */
451 GetCodePos (&IncExprEnd);
453 /* Skip the closing paren */
457 g_defcodelabel (BodyLabel);
458 Statement (&PendingToken);
460 /* If we had an increment expression, move the code to the bottom of
461 * the loop. In this case we don't need to jump there at the end of
467 MoveCode (&IncExprStart, &IncExprEnd, &Here);
469 /* Jump back to the increment expression */
473 /* Skip a pending token if we have one */
474 SkipPending (PendingToken);
476 /* Declare the break label */
477 g_defcodelabel (BreakLabel);
479 /* Remove the loop from the loop stack */
485 static int CompoundStatement (void)
486 /* Compound statement. Allow any number of statements inside braces. The
487 * function returns true if the last statement was a break or return.
492 /* Remember the stack at block entry */
493 int OldStack = StackPtr;
495 /* Enter a new lexical level */
498 /* Parse local variable declarations if any */
501 /* Now process statements in this block */
503 while (CurTok.Tok != TOK_RCURLY) {
504 if (CurTok.Tok != TOK_CEOF) {
505 GotBreak = Statement (0);
511 /* Clean up the stack. */
513 g_space (StackPtr - OldStack);
517 /* Emit references to imports/exports for this block */
520 /* Leave the lexical level */
528 int Statement (int* PendingToken)
529 /* Statement parser. Returns 1 if the statement does a return/break, returns
530 * 0 otherwise. If the PendingToken pointer is not NULL, the function will
531 * not skip the terminating token of the statement (closing brace or
532 * semicolon), but store true if there is a pending token, and false if there
533 * is none. The token is always checked, so there is no need for the caller to
534 * check this token, it must be skipped, however. If the argument pointer is
535 * NULL, the function will skip the token.
542 /* Assume no pending token */
547 /* Check for a label */
548 if (CurTok.Tok == TOK_IDENT && NextTok.Tok == TOK_COLON) {
550 /* Special handling for a label */
552 CheckLabelWithoutStatement ();
556 switch (CurTok.Tok) {
560 GotBreak = CompoundStatement ();
561 CheckTok (TOK_RCURLY, "`{' expected", PendingToken);
565 return IfStatement ();
581 CheckSemi (PendingToken);
586 CheckSemi (PendingToken);
590 ContinueStatement ();
591 CheckSemi (PendingToken);
600 CheckSemi (PendingToken);
605 CheckSemi (PendingToken);
614 CheckLabelWithoutStatement ();
619 CheckLabelWithoutStatement ();
623 /* Remember the current code position */
625 /* Actual statement */
626 ExprWithCheck (hie0, &Expr);
627 /* Load the result only if it is an lvalue and the type is
628 * marked as volatile. Otherwise the load is useless.
630 if (ED_IsLVal (&Expr) && IsQualVolatile (Expr.Type)) {
631 LoadExpr (CF_NONE, &Expr);
633 /* If the statement didn't generate code, and is not of type
634 * void, emit a warning.
637 if (CodeRangeIsEmpty (&Start, &End) && !IsTypeVoid (Expr.Type)) {
638 Warning ("Statement has no effect");
640 CheckSemi (PendingToken);