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 /*****************************************************************************/
67 /*****************************************************************************/
68 /* Helper functions */
69 /*****************************************************************************/
73 static void CheckTok (token_t Tok, const char* Msg, int* PendingToken)
74 /* Helper function for Statement. Will check for Tok and print Msg if not
75 * found. If PendingToken is NULL, it will the skip the token, otherwise
76 * it will store one to PendingToken.
79 if (CurTok.Tok != Tok) {
81 } else if (PendingToken) {
90 static void CheckSemi (int* PendingToken)
91 /* Helper function for Statement. Will check for a semicolon and print an
92 * error message if not found (plus some error recovery). If PendingToken is
93 * NULL, it will the skip the token, otherwise it will store one to
95 * This function is a special version of CheckTok with the addition of the
99 int HaveToken = (CurTok.Tok == TOK_SEMI);
101 Error ("`;' expected");
102 /* Try to be smart about errors */
103 if (CurTok.Tok == TOK_COLON || CurTok.Tok == TOK_COMMA) {
118 static void SkipPending (int PendingToken)
119 /* Skip the pending token if we have one */
128 /*****************************************************************************/
130 /*****************************************************************************/
134 static int IfStatement (void)
135 /* Handle an 'if' statement */
144 /* Generate a jump label and parse the condition */
145 Label1 = GetLocalLabel ();
146 TestResult = TestInParens (Label1, 0);
148 /* Parse the if body */
149 GotBreak = Statement (0);
151 /* Else clause present? */
152 if (CurTok.Tok != TOK_ELSE) {
154 g_defcodelabel (Label1);
156 /* Since there's no else clause, we're not sure, if the a break
157 * statement is really executed.
163 /* Generate a jump around the else branch */
164 unsigned Label2 = GetLocalLabel ();
170 /* If the if expression was always true, the code in the else branch
171 * is never executed. Output a warning if this is the case.
173 if (TestResult == TESTEXPR_TRUE) {
174 Warning ("Unreachable code");
177 /* Define the target for the first test */
178 g_defcodelabel (Label1);
180 /* Total break only if both branches had a break. */
181 GotBreak &= Statement (0);
183 /* Generate the label for the else clause */
184 g_defcodelabel (Label2);
193 static void DoStatement (void)
194 /* Handle the 'do' statement */
196 /* Get the loop control labels */
197 unsigned LoopLabel = GetLocalLabel ();
198 unsigned BreakLabel = GetLocalLabel ();
199 unsigned ContinueLabel = GetLocalLabel ();
201 /* Skip the while token */
204 /* Add the loop to the loop stack */
205 AddLoop (StackPtr, BreakLabel, ContinueLabel);
207 /* Define the loop label */
208 g_defcodelabel (LoopLabel);
210 /* Parse the loop body */
213 /* Output the label for a continue */
214 g_defcodelabel (ContinueLabel);
216 /* Parse the end condition */
217 Consume (TOK_WHILE, "`while' expected");
218 TestInParens (LoopLabel, 1);
221 /* Define the break label */
222 g_defcodelabel (BreakLabel);
224 /* Remove the loop from the loop stack */
230 static void WhileStatement (void)
231 /* Handle the 'while' statement */
235 /* Get the loop control labels */
236 unsigned LoopLabel = GetLocalLabel ();
237 unsigned BreakLabel = GetLocalLabel ();
239 /* Skip the while token */
242 /* Add the loop to the loop stack. In case of a while loop, the loop head
243 * label is used for continue statements.
245 AddLoop (StackPtr, BreakLabel, LoopLabel);
247 /* Define the head label */
248 g_defcodelabel (LoopLabel);
250 /* Test the loop condition */
251 TestInParens (BreakLabel, 0);
254 Statement (&PendingToken);
256 /* Jump back to loop top */
260 g_defcodelabel (BreakLabel);
262 /* Eat remaining tokens that were delayed because of line info
265 SkipPending (PendingToken);
267 /* Remove the loop from the loop stack */
273 static void ReturnStatement (void)
274 /* Handle the 'return' statement */
279 if (CurTok.Tok != TOK_SEMI) {
281 /* Check if the function has a return value declared */
282 if (F_HasVoidReturn (CurrentFunc)) {
283 Error ("Returning a value in function with return type void");
286 /* Evaluate the return expression */
289 /* Ignore the return expression if the function returns void */
290 if (!F_HasVoidReturn (CurrentFunc)) {
292 /* Convert the return value to the type of the function result */
293 TypeConversion (&Expr, F_GetReturnType (CurrentFunc));
295 /* Load the value into the primary */
296 ExprLoad (CF_NONE, &Expr);
299 } else if (!F_HasVoidReturn (CurrentFunc) && !F_HasOldStyleIntRet (CurrentFunc)) {
300 Error ("Function `%s' must return a value", F_GetFuncName (CurrentFunc));
303 /* Cleanup the stack in case we're inside a block with locals */
304 g_space (StackPtr - F_GetTopLevelSP (CurrentFunc));
306 /* Output a jump to the function exit code */
307 g_jump (F_GetRetLab (CurrentFunc));
312 static void BreakStatement (void)
313 /* Handle the 'break' statement */
320 /* Get the current loop descriptor */
323 /* Check if we are inside a loop */
325 /* Error: No current loop */
326 Error ("`break' statement not within loop or switch");
330 /* Correct the stack pointer if needed */
331 g_space (StackPtr - L->StackPtr);
333 /* Jump to the exit label of the loop */
334 g_jump (L->BreakLabel);
339 static void ContinueStatement (void)
340 /* Handle the 'continue' statement */
344 /* Skip the continue */
347 /* Get the current loop descriptor */
350 /* Search for a loop that has a continue label. */
352 if (L->ContinueLabel) {
359 /* Did we find it? */
361 Error ("`continue' statement not within a loop");
365 /* Correct the stackpointer if needed */
366 g_space (StackPtr - L->StackPtr);
368 /* Jump to next loop iteration */
369 g_jump (L->ContinueLabel);
374 static void ForStatement (void)
375 /* Handle a 'for' statement */
380 CodeMark IncExprStart;
384 /* Get several local labels needed later */
385 unsigned TestLabel = GetLocalLabel ();
386 unsigned BreakLabel = GetLocalLabel ();
387 unsigned IncLabel = GetLocalLabel ();
388 unsigned BodyLabel = GetLocalLabel ();
390 /* Skip the FOR token */
393 /* Add the loop to the loop stack. A continue jumps to the start of the
394 * the increment condition.
396 AddLoop (StackPtr, BreakLabel, IncLabel);
398 /* Skip the opening paren */
401 /* Parse the initializer expression */
402 if (CurTok.Tok != TOK_SEMI) {
403 Expression0 (&lval1);
407 /* Label for the test expressions */
408 g_defcodelabel (TestLabel);
410 /* Parse the test expression */
411 if (CurTok.Tok != TOK_SEMI) {
419 /* Remember the start of the increment expression */
420 IncExprStart = GetCodePos();
422 /* Label for the increment expression */
423 g_defcodelabel (IncLabel);
425 /* Parse the increment expression */
426 HaveIncExpr = (CurTok.Tok != TOK_RPAREN);
428 Expression0 (&lval3);
431 /* Jump to the test */
434 /* Remember the end of the increment expression */
435 IncExprEnd = GetCodePos();
437 /* Skip the closing paren */
441 g_defcodelabel (BodyLabel);
442 Statement (&PendingToken);
444 /* If we had an increment expression, move the code to the bottom of
445 * the loop. In this case we don't need to jump there at the end of
449 MoveCode (IncExprStart, IncExprEnd, GetCodePos());
451 /* Jump back to the increment expression */
455 /* Skip a pending token if we have one */
456 SkipPending (PendingToken);
458 /* Declare the break label */
459 g_defcodelabel (BreakLabel);
461 /* Remove the loop from the loop stack */
467 static int CompoundStatement (void)
468 /* Compound statement. Allow any number of statements inside braces. The
469 * function returns true if the last statement was a break or return.
474 /* Remember the stack at block entry */
475 int OldStack = StackPtr;
477 /* Enter a new lexical level */
480 /* Parse local variable declarations if any */
483 /* Now process statements in this block */
485 while (CurTok.Tok != TOK_RCURLY) {
486 if (CurTok.Tok != TOK_CEOF) {
487 GotBreak = Statement (0);
493 /* Clean up the stack. */
495 g_space (StackPtr - OldStack);
499 /* Emit references to imports/exports for this block */
502 /* Leave the lexical level */
510 int Statement (int* PendingToken)
511 /* Statement parser. Returns 1 if the statement does a return/break, returns
512 * 0 otherwise. If the PendingToken pointer is not NULL, the function will
513 * not skip the terminating token of the statement (closing brace or
514 * semicolon), but store true if there is a pending token, and false if there
515 * is none. The token is always checked, so there is no need for the caller to
516 * check this token, it must be skipped, however. If the argument pointer is
517 * NULL, the function will skip the token.
524 /* Assume no pending token */
529 /* Check for a label */
530 if (CurTok.Tok == TOK_IDENT && NextTok.Tok == TOK_COLON) {
532 /* Special handling for a label */
537 switch (CurTok.Tok) {
541 GotBreak = CompoundStatement ();
542 CheckTok (TOK_RCURLY, "`{' expected", PendingToken);
546 return IfStatement ();
562 CheckSemi (PendingToken);
567 CheckSemi (PendingToken);
571 ContinueStatement ();
572 CheckSemi (PendingToken);
581 CheckSemi (PendingToken);
586 CheckSemi (PendingToken);
594 /* Remember the current code position */
595 Start = GetCodePos ();
596 /* Actual statement */
597 ExprWithCheck (hie0, &Expr);
598 /* Load the result only if it is an lvalue and the type is
599 * marked as volatile. Otherwise the load is useless.
601 if (ED_IsLVal (&Expr) && IsQualVolatile (Expr.Type)) {
602 ExprLoad (CF_NONE, &Expr);
604 /* If the statement didn't generate code, and is not of type
605 * void, emit a warning
607 if (GetCodePos () == Start && !IsTypeVoid (Expr.Type)) {
608 Warning ("Statement has no effect");
610 CheckSemi (PendingToken);