1 /*****************************************************************************/
5 /* Parse a statement */
9 /* (C) 1998-2004 Ullrich von Bassewitz */
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 CheckTok (token_t Tok, const char* Msg, int* PendingToken)
75 /* Helper function for Statement. Will check for Tok and print Msg if not
76 * found. If PendingToken is NULL, it will the skip the token, otherwise
77 * it will store one to PendingToken.
80 if (CurTok.Tok != Tok) {
82 } else if (PendingToken) {
91 static void CheckSemi (int* PendingToken)
92 /* Helper function for Statement. Will check for a semicolon and print an
93 * error message if not found (plus some error recovery). If PendingToken is
94 * NULL, it will the skip the token, otherwise it will store one to
96 * This function is a special version of CheckTok with the addition of the
100 int HaveToken = (CurTok.Tok == TOK_SEMI);
102 Error ("`;' expected");
103 /* Try to be smart about errors */
104 if (CurTok.Tok == TOK_COLON || CurTok.Tok == TOK_COMMA) {
119 static void SkipPending (int PendingToken)
120 /* Skip the pending token if we have one */
129 /*****************************************************************************/
131 /*****************************************************************************/
135 static int IfStatement (void)
136 /* Handle an 'if' statement */
145 /* Generate a jump label and parse the condition */
146 Label1 = GetLocalLabel ();
147 TestResult = TestInParens (Label1, 0);
149 /* Parse the if body */
150 GotBreak = Statement (0);
152 /* Else clause present? */
153 if (CurTok.Tok != TOK_ELSE) {
155 g_defcodelabel (Label1);
157 /* Since there's no else clause, we're not sure, if the a break
158 * statement is really executed.
164 /* Generate a jump around the else branch */
165 unsigned Label2 = GetLocalLabel ();
171 /* If the if expression was always true, the code in the else branch
172 * is never executed. Output a warning if this is the case.
174 if (TestResult == TESTEXPR_TRUE) {
175 Warning ("Unreachable code");
178 /* Define the target for the first test */
179 g_defcodelabel (Label1);
181 /* Total break only if both branches had a break. */
182 GotBreak &= Statement (0);
184 /* Generate the label for the else clause */
185 g_defcodelabel (Label2);
194 static void DoStatement (void)
195 /* Handle the 'do' statement */
197 /* Get the loop control labels */
198 unsigned LoopLabel = GetLocalLabel ();
199 unsigned BreakLabel = GetLocalLabel ();
200 unsigned ContinueLabel = GetLocalLabel ();
202 /* Skip the while token */
205 /* Add the loop to the loop stack */
206 AddLoop (BreakLabel, ContinueLabel);
208 /* Define the loop label */
209 g_defcodelabel (LoopLabel);
211 /* Parse the loop body */
214 /* Output the label for a continue */
215 g_defcodelabel (ContinueLabel);
217 /* Parse the end condition */
218 Consume (TOK_WHILE, "`while' expected");
219 TestInParens (LoopLabel, 1);
222 /* Define the break label */
223 g_defcodelabel (BreakLabel);
225 /* Remove the loop from the loop stack */
231 static void WhileStatement (void)
232 /* Handle the 'while' statement */
236 /* Get the loop control labels */
237 unsigned LoopLabel = GetLocalLabel ();
238 unsigned BreakLabel = GetLocalLabel ();
240 /* Skip the while token */
243 /* Add the loop to the loop stack. In case of a while loop, the loop head
244 * label is used for continue statements.
246 AddLoop (BreakLabel, LoopLabel);
248 /* Define the head label */
249 g_defcodelabel (LoopLabel);
251 /* Test the loop condition */
252 TestInParens (BreakLabel, 0);
255 Statement (&PendingToken);
257 /* Jump back to loop top */
261 g_defcodelabel (BreakLabel);
263 /* Eat remaining tokens that were delayed because of line info
266 SkipPending (PendingToken);
268 /* Remove the loop from the loop stack */
274 static void ReturnStatement (void)
275 /* Handle the 'return' statement */
280 if (CurTok.Tok != TOK_SEMI) {
282 /* Check if the function has a return value declared */
283 if (F_HasVoidReturn (CurrentFunc)) {
284 Error ("Returning a value in function with return type void");
287 /* Evaluate the return expression */
290 /* Ignore the return expression if the function returns void */
291 if (!F_HasVoidReturn (CurrentFunc)) {
293 /* Convert the return value to the type of the function result */
294 TypeConversion (&Expr, F_GetReturnType (CurrentFunc));
296 /* Load the value into the primary */
297 LoadExpr (CF_NONE, &Expr);
300 } else if (!F_HasVoidReturn (CurrentFunc) && !F_HasOldStyleIntRet (CurrentFunc)) {
301 Error ("Function `%s' must return a value", F_GetFuncName (CurrentFunc));
304 /* Cleanup the stack in case we're inside a block with locals */
305 g_space (StackPtr - F_GetTopLevelSP (CurrentFunc));
307 /* Output a jump to the function exit code */
308 g_jump (F_GetRetLab (CurrentFunc));
313 static void BreakStatement (void)
314 /* Handle the 'break' statement */
321 /* Get the current loop descriptor */
324 /* Check if we are inside a loop */
326 /* Error: No current loop */
327 Error ("`break' statement not within loop or switch");
331 /* Correct the stack pointer if needed */
332 g_space (StackPtr - L->StackPtr);
334 /* Jump to the exit label of the loop */
335 g_jump (L->BreakLabel);
340 static void ContinueStatement (void)
341 /* Handle the 'continue' statement */
345 /* Skip the continue */
348 /* Get the current loop descriptor */
351 /* Search for a loop that has a continue label. */
353 if (L->ContinueLabel) {
360 /* Did we find it? */
362 Error ("`continue' statement not within a loop");
366 /* Correct the stackpointer if needed */
367 g_space (StackPtr - L->StackPtr);
369 /* Jump to next loop iteration */
370 g_jump (L->ContinueLabel);
375 static void ForStatement (void)
376 /* Handle a 'for' statement */
381 CodeMark IncExprStart;
385 /* Get several local labels needed later */
386 unsigned TestLabel = GetLocalLabel ();
387 unsigned BreakLabel = GetLocalLabel ();
388 unsigned IncLabel = GetLocalLabel ();
389 unsigned BodyLabel = GetLocalLabel ();
391 /* Skip the FOR token */
394 /* Add the loop to the loop stack. A continue jumps to the start of the
395 * the increment condition.
397 AddLoop (BreakLabel, IncLabel);
399 /* Skip the opening paren */
402 /* Parse the initializer expression */
403 if (CurTok.Tok != TOK_SEMI) {
404 Expression0 (&lval1);
408 /* Label for the test expressions */
409 g_defcodelabel (TestLabel);
411 /* Parse the test expression */
412 if (CurTok.Tok != TOK_SEMI) {
420 /* Remember the start of the increment expression */
421 GetCodePos (&IncExprStart);
423 /* Label for the increment expression */
424 g_defcodelabel (IncLabel);
426 /* Parse the increment expression */
427 HaveIncExpr = (CurTok.Tok != TOK_RPAREN);
429 Expression0 (&lval3);
432 /* Jump to the test */
435 /* Remember the end of the increment expression */
436 GetCodePos (&IncExprEnd);
438 /* Skip the closing paren */
442 g_defcodelabel (BodyLabel);
443 Statement (&PendingToken);
445 /* If we had an increment expression, move the code to the bottom of
446 * the loop. In this case we don't need to jump there at the end of
452 MoveCode (&IncExprStart, &IncExprEnd, &Here);
454 /* Jump back to the increment expression */
458 /* Skip a pending token if we have one */
459 SkipPending (PendingToken);
461 /* Declare the break label */
462 g_defcodelabel (BreakLabel);
464 /* Remove the loop from the loop stack */
470 static int CompoundStatement (void)
471 /* Compound statement. Allow any number of statements inside braces. The
472 * function returns true if the last statement was a break or return.
477 /* Remember the stack at block entry */
478 int OldStack = StackPtr;
480 /* Enter a new lexical level */
483 /* Parse local variable declarations if any */
486 /* Now process statements in this block */
488 while (CurTok.Tok != TOK_RCURLY) {
489 if (CurTok.Tok != TOK_CEOF) {
490 GotBreak = Statement (0);
496 /* Clean up the stack. */
498 g_space (StackPtr - OldStack);
502 /* Emit references to imports/exports for this block */
505 /* Leave the lexical level */
513 int Statement (int* PendingToken)
514 /* Statement parser. Returns 1 if the statement does a return/break, returns
515 * 0 otherwise. If the PendingToken pointer is not NULL, the function will
516 * not skip the terminating token of the statement (closing brace or
517 * semicolon), but store true if there is a pending token, and false if there
518 * is none. The token is always checked, so there is no need for the caller to
519 * check this token, it must be skipped, however. If the argument pointer is
520 * NULL, the function will skip the token.
527 /* Assume no pending token */
532 /* Check for a label */
533 if (CurTok.Tok == TOK_IDENT && NextTok.Tok == TOK_COLON) {
535 /* Special handling for a label */
540 switch (CurTok.Tok) {
544 GotBreak = CompoundStatement ();
545 CheckTok (TOK_RCURLY, "`{' expected", PendingToken);
549 return IfStatement ();
565 CheckSemi (PendingToken);
570 CheckSemi (PendingToken);
574 ContinueStatement ();
575 CheckSemi (PendingToken);
584 CheckSemi (PendingToken);
589 CheckSemi (PendingToken);
597 /* Remember the current code position */
599 /* Actual statement */
600 ExprWithCheck (hie0, &Expr);
601 /* Load the result only if it is an lvalue and the type is
602 * marked as volatile. Otherwise the load is useless.
604 if (ED_IsLVal (&Expr) && IsQualVolatile (Expr.Type)) {
605 LoadExpr (CF_NONE, &Expr);
607 /* If the statement didn't generate code, and is not of type
608 * void, emit a warning.
611 if (CodeRangeIsEmpty (&Start, &End) && !IsTypeVoid (Expr.Type)) {
612 Warning ("Statement has no effect");
614 CheckSemi (PendingToken);