4 * Ullrich von Bassewitz, 06.08.1998
6 * Original by John R. Dunning - see copyleft.jrd
38 /*****************************************************************************/
40 /*****************************************************************************/
44 /* Maximum count of cases */
49 /*****************************************************************************/
50 /* Helper functions */
51 /*****************************************************************************/
55 static void CheckTok (token_t Tok, const char* Msg, int* PendingToken)
56 /* Helper function for Statement. Will check for Tok and print Msg if not
57 * found. If PendingToken is NULL, it will the skip the token, otherwise
58 * it will store one to PendingToken.
61 if (CurTok.Tok != Tok) {
63 } else if (PendingToken) {
72 static void CheckSemi (int* PendingToken)
73 /* Helper function for Statement. Will call CheckTok with the parameters
77 CheckTok (TOK_SEMI, "`;' expected", PendingToken);
82 static void SkipPending (int PendingToken)
83 /* Skip the pending token if we have one */
92 /*****************************************************************************/
94 /*****************************************************************************/
98 static int IfStatement (void)
99 /* Handle an 'if' statement */
107 /* Generate a jump label and parse the condition */
108 Label1 = GetLocalLabel ();
111 /* Parse the if body */
112 GotBreak = Statement (0);
114 /* Else clause present? */
115 if (CurTok.Tok != TOK_ELSE) {
117 g_defcodelabel (Label1);
119 /* Since there's no else clause, we're not sure, if the a break
120 * statement is really executed.
126 /* Generate a jump around the else branch */
127 unsigned Label2 = GetLocalLabel ();
133 /* Define the target for the first test */
134 g_defcodelabel (Label1);
136 /* Total break only if both branches had a break. */
137 GotBreak &= Statement (0);
139 /* Generate the label for the else clause */
140 g_defcodelabel (Label2);
149 static void DoStatement (void)
150 /* Handle the 'do' statement */
152 /* Get the loop control labels */
153 unsigned loop = GetLocalLabel ();
154 unsigned lab = GetLocalLabel ();
156 /* Skip the while token */
159 /* Add the loop to the loop stack */
160 AddLoop (oursp, loop, lab, 0, 0);
162 /* Define the head label */
163 g_defcodelabel (loop);
165 /* Parse the loop body */
168 /* Parse the end condition */
169 Consume (TOK_WHILE, "`while' expected");
173 /* Define the break label */
174 g_defcodelabel (lab);
176 /* Remove the loop from the loop stack */
182 static void WhileStatement (void)
183 /* Handle the 'while' statement */
187 /* Get the loop control labels */
188 unsigned loop = GetLocalLabel ();
189 unsigned lab = GetLocalLabel ();
191 /* Skip the while token */
194 /* Add the loop to the loop stack */
195 AddLoop (oursp, loop, lab, 0, 0);
197 /* Define the head label */
198 g_defcodelabel (loop);
200 /* Test the loop condition */
204 Statement (&PendingToken);
206 /* Jump back to loop top */
210 g_defcodelabel (lab);
212 /* Eat remaining tokens that were delay because of line info correctness */
213 SkipPending (PendingToken);
215 /* Remove the loop from the loop stack */
221 static void ReturnStatement (void)
222 /* Handle the 'return' statement */
227 if (CurTok.Tok != TOK_SEMI) {
228 if (HasVoidReturn (CurrentFunc)) {
229 Error ("Returning a value in function with return type void");
232 /* Evaluate the return expression. Result will be in primary */
235 /* Convert the return value to the type of the function result */
236 if (!HasVoidReturn (CurrentFunc)) {
237 assignadjust (GetReturnType (CurrentFunc), &lval);
239 } else if (!HasVoidReturn (CurrentFunc)) {
240 Error ("Function `%s' must return a value", GetFuncName (CurrentFunc));
243 /* Cleanup the stack in case we're inside a block with locals */
244 g_space (oursp - GetTopLevelSP (CurrentFunc));
246 /* Output a jump to the function exit code */
247 g_jump (GetRetLab (CurrentFunc));
252 static void BreakStatement (void)
253 /* Handle the 'break' statement */
260 /* Get the current loop descriptor */
263 /* Check if we are inside a loop */
265 /* Error: No current loop */
266 Error ("`break' statement not within loop or switch");
270 /* Correct the stack pointer if needed */
271 g_space (oursp - L->StackPtr);
273 /* Jump to the exit label of the loop */
279 static void ContinueStatement (void)
280 /* Handle the 'continue' statement */
284 /* Skip the continue */
287 /* Get the current loop descriptor */
290 /* Search for the correct loop */
299 /* Did we find it? */
301 Error ("`continue' statement not within a loop");
305 /* Correct the stackpointer if needed */
306 g_space (oursp - L->StackPtr);
308 /* Output the loop code */
318 static void CascadeSwitch (ExprDesc* Expr)
319 /* Handle a switch statement for chars with a cmp cascade for the selector */
321 unsigned ExitLab; /* Exit label */
322 unsigned NextLab; /* Next case label */
323 unsigned CodeLab; /* Label that starts the actual selector code */
324 int HaveBreak; /* Remember if we exited with break */
325 int HaveDefault; /* Remember if we had a default label */
326 int lcount; /* Label count */
327 unsigned Flags; /* Code generator flags */
328 ExprDesc lval; /* Case label expression */
329 long Val; /* Case label value */
332 /* Get the unqualified type of the switch expression */
333 type ExprType = UnqualifiedType (Expr->Type[0]);
335 /* Create a loop so we may break out, init labels */
336 ExitLab = GetLocalLabel ();
337 AddLoop (oursp, 0, ExitLab, 0, 0);
339 /* Setup some variables needed in the loop below */
340 Flags = TypeOf (Expr->Type) | CF_CONST | CF_FORCECHAR;
341 CodeLab = NextLab = 0;
345 /* Parse the labels */
347 while (CurTok.Tok != TOK_RCURLY) {
349 if (CurTok.Tok == TOK_CASE || CurTok.Tok == TOK_DEFAULT) {
351 /* If the code for the previous selector did not end with a
352 * break statement, we must jump over the next selector test.
355 /* Define a label for the code */
357 CodeLab = GetLocalLabel ();
362 /* If we have a cascade label, emit it */
364 g_defcodelabel (NextLab);
368 while (CurTok.Tok == TOK_CASE || CurTok.Tok == TOK_DEFAULT) {
370 /* Parse the selector */
371 if (CurTok.Tok == TOK_CASE) {
376 /* Skip the "case" token */
379 /* Read the selector expression */
381 if (!IsClassInt (lval.Type)) {
382 Error ("Switch quantity not an integer");
385 /* Check the range of the expression */
391 if (Val < -128 || Val > 127) {
392 Error ("Range error");
397 if (Val < 0 || Val > 255) {
398 Error ("Range error");
403 if (Val < -32768 || Val > 32767) {
404 Error ("Range error");
409 if (Val < 0 || Val > 65535) {
410 Error ("Range error");
415 Internal ("Invalid type: %04X", ExprType);
421 /* If another case follows after the colon (which is
422 * currently pending and cannot be skipped since otherwise
423 * the debug infos will get wrong), we will jump to the
424 * code if the condition is true.
426 if (NextTok.Tok == TOK_CASE) {
427 /* Create a code label if needed */
429 CodeLab = GetLocalLabel ();
431 g_falsejump (CF_NONE, CodeLab);
432 } else if (NextTok.Tok != TOK_DEFAULT) {
433 /* No case follows, jump to next selector */
435 NextLab = GetLocalLabel ();
437 g_truejump (CF_NONE, NextLab);
448 /* Handle the pathologic case: DEFAULT followed by CASE */
449 if (NextTok.Tok == TOK_CASE) {
451 CodeLab = GetLocalLabel ();
459 /* Remember that we had a default label */
467 /* Emit a code label if we have one */
469 g_defcodelabel (CodeLab);
473 /* Parse statements */
474 if (CurTok.Tok != TOK_RCURLY) {
475 HaveBreak = Statement (0);
479 /* Check if we have any labels */
480 if (lcount == 0 && !HaveDefault) {
481 Warning ("No case labels");
484 /* Define the exit label and, if there's a next label left, create this
488 g_defcodelabel (NextLab);
490 g_defcodelabel (ExitLab);
492 /* Eat the closing curly brace */
501 static void TableSwitch (ExprDesc* Expr)
502 /* Handle a switch statement via table based selector */
504 /* Entry for one case in a switch statement */
506 long Value; /* selector value */
507 unsigned Label; /* label for this selector */
510 unsigned DefaultLabel; /* Label for default case */
511 unsigned ExitLabel; /* exit label */
512 int lcase; /* label for compares */
513 int HaveBreak; /* Last statement has a break */
514 unsigned Flags; /* Code generator flags */
515 ExprDesc lval; /* Case label expression */
518 Collection SwitchTab;
520 /* Initialize the collection for the switch entries */
521 InitCollection (&SwitchTab);
523 /* Create a look so we may break out, init labels */
524 HaveBreak = 0; /* Keep gcc silent */
525 DefaultLabel = 0; /* No default case until now */
526 ExitLabel = GetLocalLabel (); /* get exit */
527 AddLoop (oursp, 0, ExitLabel, 0, 0);
529 /* Jump behind the code for the CASE labels */
530 g_jump (lcase = GetLocalLabel ());
531 while (CurTok.Tok != TOK_RCURLY) {
532 if (CurTok.Tok == TOK_CASE || CurTok.Tok == TOK_DEFAULT) {
534 if (CurTok.Tok == TOK_CASE) {
537 if (!IsClassInt (lval.Type)) {
538 Error ("Switch quantity not an integer");
540 P = xmalloc (sizeof (SwitchEntry));
541 P->Value = lval.ConstVal;
542 P->Label = GetLocalLabel ();
543 CollAppend (&SwitchTab, P);
544 g_defcodelabel (P->Label);
545 } else if (DefaultLabel == 0) {
547 DefaultLabel = GetLocalLabel ();
548 g_defcodelabel (DefaultLabel);
550 /* We already had a default label */
551 Error ("Multiple default labels in one switch");
556 } while (CurTok.Tok == TOK_CASE || CurTok.Tok == TOK_DEFAULT);
559 if (CurTok.Tok != TOK_RCURLY) {
560 HaveBreak = Statement (0);
564 /* Check if we have any labels */
565 if (CollCount(&SwitchTab) == 0 && DefaultLabel == 0) {
566 Warning ("No case labels");
569 /* Eat the closing curly brace */
572 /* If the last statement doesn't have a break or return, add one */
577 /* Actual selector code goes here */
578 g_defcodelabel (lcase);
580 /* Create the call to the switch subroutine */
581 Flags = TypeOf (Expr->Type);
584 /* First entry is negative of label count */
585 g_defdata (CF_INT | CF_CONST, -((int)CollCount(&SwitchTab))-1, 0);
587 /* Create the case selector table */
588 for (I = 0; I < CollCount (&SwitchTab); ++I) {
589 P = CollAt (&SwitchTab, I);
590 g_case (Flags, P->Label, P->Value); /* Create one label */
593 if (DefaultLabel != 0) {
594 g_jump (DefaultLabel);
596 g_defcodelabel (ExitLabel);
599 /* Free the allocated space for the labels */
600 for (I = 0; I < CollCount (&SwitchTab); ++I) {
601 xfree (CollAt (&SwitchTab, I));
604 /* Free the collection itself */
605 DoneCollection (&SwitchTab);
610 static void SwitchStatement (void)
611 /* Handle a 'switch' statement */
613 ExprDesc Expr; /* Switch statement expression */
615 /* Eat the "switch" */
618 /* Read the switch expression */
623 /* result of expr is in P */
626 /* Now decide which sort of switch we will create: */
627 if (IsTypeChar (Expr.Type) || (CodeSizeFactor >= 200 && IsClassInt (Expr.Type))) {
628 CascadeSwitch (&Expr);
636 static void ForStatement (void)
637 /* Handle a 'for' statement */
643 CodeMark IncExprStart;
647 /* Get several local labels needed later */
648 unsigned TestLabel = GetLocalLabel ();
649 unsigned lab = GetLocalLabel ();
650 unsigned IncLabel = GetLocalLabel ();
651 unsigned lstat = GetLocalLabel ();
653 /* Skip the FOR token */
656 /* Add the loop to the loop stack */
657 AddLoop (oursp, TestLabel, lab, IncLabel, lstat);
659 /* Skip the opening paren */
662 /* Parse the initializer expression */
663 if (CurTok.Tok != TOK_SEMI) {
668 /* Label for the test expressions */
669 g_defcodelabel (TestLabel);
671 /* Parse the test expression */
672 if (CurTok.Tok != TOK_SEMI) {
674 g_truejump (CF_NONE, lstat);
681 /* Remember the start of the increment expression */
682 IncExprStart = GetCodePos();
684 /* Label for the increment expression */
685 g_defcodelabel (IncLabel);
687 /* Parse the increment expression */
688 HaveIncExpr = (CurTok.Tok != TOK_RPAREN);
693 /* Jump to the test */
696 /* Remember the end of the increment expression */
697 IncExprEnd = GetCodePos();
699 /* Skip the closing paren */
703 g_defcodelabel (lstat);
704 Statement (&PendingToken);
706 /* If we had an increment expression, move the code to the bottom of
707 * the loop. In this case we don't need to jump there at the end of
711 MoveCode (IncExprStart, IncExprEnd, GetCodePos());
713 /* Jump back to the increment expression */
717 /* Skip a pending token if we have one */
718 SkipPending (PendingToken);
720 /* Declare the break label */
721 g_defcodelabel (lab);
723 /* Remove the loop from the loop stack */
729 static int CompoundStatement (void)
730 /* Compound statement. Allow any number of statements inside braces. The
731 * function returns true if the last statement was a break or return.
736 /* Remember the stack at block entry */
737 int OldStack = oursp;
739 /* Enter a new lexical level */
742 /* Parse local variable declarations if any */
745 /* Now process statements in this block */
747 while (CurTok.Tok != TOK_RCURLY) {
748 if (CurTok.Tok != TOK_CEOF) {
749 GotBreak = Statement (0);
755 /* Clean up the stack. */
757 g_space (oursp - OldStack);
761 /* Emit references to imports/exports for this block */
764 /* Leave the lexical level */
772 int Statement (int* PendingToken)
773 /* Statement parser. Returns 1 if the statement does a return/break, returns
774 * 0 otherwise. If the PendingToken pointer is not NULL, the function will
775 * not skip the terminating token of the statement (closing brace or
776 * semicolon), but store true if there is a pending token, and false if there
777 * is none. The token is always checked, so there is no need for the caller to
778 * check this token, it must be skipped, however. If the argument pointer is
779 * NULL, the function will skip the token.
785 /* Assume no pending token */
790 /* Check for a label */
791 if (CurTok.Tok == TOK_IDENT && NextTok.Tok == TOK_COLON) {
793 /* Special handling for a label */
798 switch (CurTok.Tok) {
802 GotBreak = CompoundStatement ();
803 CheckTok (TOK_RCURLY, "`{' expected", PendingToken);
807 return IfStatement ();
823 CheckSemi (PendingToken);
828 CheckSemi (PendingToken);
832 ContinueStatement ();
833 CheckSemi (PendingToken);
842 CheckSemi (PendingToken);
855 /* Actual statement */
857 CheckSemi (PendingToken);