1 /*****************************************************************************/
5 /* Parse a statement */
9 /* (C) 1998-2003 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 /*****************************************************************************/
66 /*****************************************************************************/
67 /* Helper functions */
68 /*****************************************************************************/
72 static void CheckTok (token_t Tok, const char* Msg, int* PendingToken)
73 /* Helper function for Statement. Will check for Tok and print Msg if not
74 * found. If PendingToken is NULL, it will the skip the token, otherwise
75 * it will store one to PendingToken.
78 if (CurTok.Tok != Tok) {
80 } else if (PendingToken) {
89 static void CheckSemi (int* PendingToken)
90 /* Helper function for Statement. Will check for a semicolon and print an
91 * error message if not found (plus some error recovery). If PendingToken is
92 * NULL, it will the skip the token, otherwise it will store one to
94 * This function is a special version of CheckTok with the addition of the
98 int HaveToken = (CurTok.Tok == TOK_SEMI);
100 Error ("`;' expected");
101 /* Try to be smart about errors */
102 if (CurTok.Tok == TOK_COLON || CurTok.Tok == TOK_COMMA) {
117 static void SkipPending (int PendingToken)
118 /* Skip the pending token if we have one */
127 /*****************************************************************************/
129 /*****************************************************************************/
133 static int IfStatement (void)
134 /* Handle an 'if' statement */
143 /* Generate a jump label and parse the condition */
144 Label1 = GetLocalLabel ();
145 TestResult = TestInParens (Label1, 0);
147 /* Parse the if body */
148 GotBreak = Statement (0);
150 /* Else clause present? */
151 if (CurTok.Tok != TOK_ELSE) {
153 g_defcodelabel (Label1);
155 /* Since there's no else clause, we're not sure, if the a break
156 * statement is really executed.
162 /* Generate a jump around the else branch */
163 unsigned Label2 = GetLocalLabel ();
169 /* If the if expression was always true, the code in the else branch
170 * is never executed. Output a warning if this is the case.
172 if (TestResult == TESTEXPR_TRUE) {
173 Warning ("Unreachable code");
176 /* Define the target for the first test */
177 g_defcodelabel (Label1);
179 /* Total break only if both branches had a break. */
180 GotBreak &= Statement (0);
182 /* Generate the label for the else clause */
183 g_defcodelabel (Label2);
192 static void DoStatement (void)
193 /* Handle the 'do' statement */
195 /* Get the loop control labels */
196 unsigned LoopLabel = GetLocalLabel ();
197 unsigned BreakLabel = GetLocalLabel ();
198 unsigned ContinueLabel = GetLocalLabel ();
200 /* Skip the while token */
203 /* Add the loop to the loop stack */
204 AddLoop (oursp, BreakLabel, ContinueLabel);
206 /* Define the loop label */
207 g_defcodelabel (LoopLabel);
209 /* Parse the loop body */
212 /* Output the label for a continue */
213 g_defcodelabel (ContinueLabel);
215 /* Parse the end condition */
216 Consume (TOK_WHILE, "`while' expected");
217 TestInParens (LoopLabel, 1);
220 /* Define the break label */
221 g_defcodelabel (BreakLabel);
223 /* Remove the loop from the loop stack */
229 static void WhileStatement (void)
230 /* Handle the 'while' statement */
234 /* Get the loop control labels */
235 unsigned LoopLabel = GetLocalLabel ();
236 unsigned BreakLabel = GetLocalLabel ();
238 /* Skip the while token */
241 /* Add the loop to the loop stack. In case of a while loop, the loop head
242 * label is used for continue statements.
244 AddLoop (oursp, BreakLabel, LoopLabel);
246 /* Define the head label */
247 g_defcodelabel (LoopLabel);
249 /* Test the loop condition */
250 TestInParens (BreakLabel, 0);
253 Statement (&PendingToken);
255 /* Jump back to loop top */
259 g_defcodelabel (BreakLabel);
261 /* Eat remaining tokens that were delayed because of line info
264 SkipPending (PendingToken);
266 /* Remove the loop from the loop stack */
272 static void ReturnStatement (void)
273 /* Handle the 'return' statement */
278 if (CurTok.Tok != TOK_SEMI) {
280 /* Check if the function has a return value declared */
281 if (F_HasVoidReturn (CurrentFunc)) {
282 Error ("Returning a value in function with return type void");
285 /* Evaluate the return expression */
286 hie0 (InitExprDesc (&Expr));
288 /* Ignore the return expression if the function returns void */
289 if (!F_HasVoidReturn (CurrentFunc)) {
291 /* Convert the return value to the type of the function result */
292 TypeConversion (&Expr, F_GetReturnType (CurrentFunc));
294 /* Load the value into the primary */
295 ExprLoad (CF_NONE, &Expr);
298 } else if (!F_HasVoidReturn (CurrentFunc) && !F_HasOldStyleIntRet (CurrentFunc)) {
299 Error ("Function `%s' must return a value", F_GetFuncName (CurrentFunc));
302 /* Cleanup the stack in case we're inside a block with locals */
303 g_space (oursp - F_GetTopLevelSP (CurrentFunc));
305 /* Output a jump to the function exit code */
306 g_jump (F_GetRetLab (CurrentFunc));
311 static void BreakStatement (void)
312 /* Handle the 'break' statement */
319 /* Get the current loop descriptor */
322 /* Check if we are inside a loop */
324 /* Error: No current loop */
325 Error ("`break' statement not within loop or switch");
329 /* Correct the stack pointer if needed */
330 g_space (oursp - L->StackPtr);
332 /* Jump to the exit label of the loop */
333 g_jump (L->BreakLabel);
338 static void ContinueStatement (void)
339 /* Handle the 'continue' statement */
343 /* Skip the continue */
346 /* Get the current loop descriptor */
349 /* Search for a loop that has a continue label. */
351 if (L->ContinueLabel) {
358 /* Did we find it? */
360 Error ("`continue' statement not within a loop");
364 /* Correct the stackpointer if needed */
365 g_space (oursp - L->StackPtr);
367 /* Jump to next loop iteration */
368 g_jump (L->ContinueLabel);
373 static void ForStatement (void)
374 /* Handle a 'for' statement */
379 CodeMark IncExprStart;
383 /* Get several local labels needed later */
384 unsigned TestLabel = GetLocalLabel ();
385 unsigned BreakLabel = GetLocalLabel ();
386 unsigned IncLabel = GetLocalLabel ();
387 unsigned BodyLabel = GetLocalLabel ();
389 /* Skip the FOR token */
392 /* Add the loop to the loop stack. A continue jumps to the start of the
393 * the increment condition.
395 AddLoop (oursp, BreakLabel, IncLabel);
397 /* Skip the opening paren */
400 /* Parse the initializer expression */
401 if (CurTok.Tok != TOK_SEMI) {
402 expression0 (&lval1);
406 /* Label for the test expressions */
407 g_defcodelabel (TestLabel);
409 /* Parse the test expression */
410 if (CurTok.Tok != TOK_SEMI) {
418 /* Remember the start of the increment expression */
419 IncExprStart = GetCodePos();
421 /* Label for the increment expression */
422 g_defcodelabel (IncLabel);
424 /* Parse the increment expression */
425 HaveIncExpr = (CurTok.Tok != TOK_RPAREN);
427 expression0 (&lval3);
430 /* Jump to the test */
433 /* Remember the end of the increment expression */
434 IncExprEnd = GetCodePos();
436 /* Skip the closing paren */
440 g_defcodelabel (BodyLabel);
441 Statement (&PendingToken);
443 /* If we had an increment expression, move the code to the bottom of
444 * the loop. In this case we don't need to jump there at the end of
448 MoveCode (IncExprStart, IncExprEnd, GetCodePos());
450 /* Jump back to the increment expression */
454 /* Skip a pending token if we have one */
455 SkipPending (PendingToken);
457 /* Declare the break label */
458 g_defcodelabel (BreakLabel);
460 /* Remove the loop from the loop stack */
466 static int CompoundStatement (void)
467 /* Compound statement. Allow any number of statements inside braces. The
468 * function returns true if the last statement was a break or return.
473 /* Remember the stack at block entry */
474 int OldStack = oursp;
476 /* Enter a new lexical level */
479 /* Parse local variable declarations if any */
482 /* Now process statements in this block */
484 while (CurTok.Tok != TOK_RCURLY) {
485 if (CurTok.Tok != TOK_CEOF) {
486 GotBreak = Statement (0);
492 /* Clean up the stack. */
494 g_space (oursp - OldStack);
498 /* Emit references to imports/exports for this block */
501 /* Leave the lexical level */
509 int Statement (int* PendingToken)
510 /* Statement parser. Returns 1 if the statement does a return/break, returns
511 * 0 otherwise. If the PendingToken pointer is not NULL, the function will
512 * not skip the terminating token of the statement (closing brace or
513 * semicolon), but store true if there is a pending token, and false if there
514 * is none. The token is always checked, so there is no need for the caller to
515 * check this token, it must be skipped, however. If the argument pointer is
516 * NULL, the function will skip the token.
522 /* Assume no pending token */
527 /* Check for a label */
528 if (CurTok.Tok == TOK_IDENT && NextTok.Tok == TOK_COLON) {
530 /* Special handling for a label */
535 switch (CurTok.Tok) {
539 GotBreak = CompoundStatement ();
540 CheckTok (TOK_RCURLY, "`{' expected", PendingToken);
544 return IfStatement ();
560 CheckSemi (PendingToken);
565 CheckSemi (PendingToken);
569 ContinueStatement ();
570 CheckSemi (PendingToken);
579 CheckSemi (PendingToken);
584 CheckSemi (PendingToken);
592 /* Actual statement */
594 CheckSemi (PendingToken);