- /* Define the exit label and, if there's a next label left, create this
- * one, too.
- */
- if (NextLab) {
- g_defcodelabel (NextLab);
- }
- g_defcodelabel (ExitLab);
-
- /* Eat the closing curly brace */
- NextToken ();
-
- /* End the loop */
- DelLoop ();
-}
-
-
-
-static void TableSwitch (struct expent* eval)
-/* Handle a switch statement via table based selector */
-{
- /* Entry for one case in a switch statement */
- struct swent {
- long sw_const; /* selector value */
- unsigned sw_lab; /* label for this selector */
- };
-
- int dlabel; /* for default */
- int lab; /* exit label */
- int label; /* label for case */
- int lcase; /* label for compares */
- int lcount; /* Label count */
- int HaveBreak; /* Last statement has a break */
- int HaveDefault; /* Remember if we had a default label */
- unsigned Flags; /* Code generator flags */
- struct expent lval; /* Case label expression */
- struct swent *p;
- struct swent *swtab;
-
- /* Allocate memory for the switch table */
- swtab = xmalloc (CASE_MAX * sizeof (struct swent));
-
- /* Create a look so we may break out, init labels */
- HaveBreak = 0; /* Keep gcc silent */
- HaveDefault = 0; /* No default case until now */
- dlabel = 0; /* init */
- lab = GetLocalLabel (); /* get exit */
- p = swtab;
- AddLoop (oursp, 0, lab, 0, 0);
-
- /* Jump behind the code for the CASE labels */
- g_jump (lcase = GetLocalLabel ());
- lcount = 0;
- while (CurTok.Tok != TOK_RCURLY) {
- if (CurTok.Tok == TOK_CASE || CurTok.Tok == TOK_DEFAULT) {
- if (lcount >= CASE_MAX) {
- Fatal ("Too many case labels");
- }
- label = GetLocalLabel ();
- do {
- if (CurTok.Tok == TOK_CASE) {
- NextToken ();
- constexpr (&lval);
- if (!IsClassInt (lval.e_tptr)) {
- Error ("Switch quantity not an integer");
- }
- p->sw_const = lval.e_const;
- p->sw_lab = label;
- ++p;
- ++lcount;
- } else {
- NextToken ();
- dlabel = label;
- HaveDefault = 1;
- }
- ConsumeColon ();
- } while (CurTok.Tok == TOK_CASE || CurTok.Tok == TOK_DEFAULT);
- g_defcodelabel (label);
- HaveBreak = 0;
- }
- if (CurTok.Tok != TOK_RCURLY) {
- HaveBreak = Statement ();
- }
- }
-
- /* Check if we have any labels */
- if (lcount == 0 && !HaveDefault) {
- Warning ("No case labels");
- }
-
- /* Eat the closing curly brace */
- NextToken ();
-
- /* If the last statement doesn't have a break or return, add one */
- if (!HaveBreak) {
- g_jump (lab);
- }
-
- /* Actual selector code goes here */
- g_defcodelabel (lcase);
-
- /* Create the call to the switch subroutine */
- Flags = TypeOf (eval->e_tptr);
- g_switch (Flags);
-
- /* First entry is negative of label count */
- g_defdata (CF_INT | CF_CONST, -((int)lcount)-1, 0);
-
- /* Create the case selector table */
- p = swtab;
- while (lcount) {
- g_case (Flags, p->sw_lab, p->sw_const); /* Create one label */
- --lcount;
- ++p;
- }
-
- if (dlabel) {
- g_jump (dlabel);
- }
- g_defcodelabel (lab);
- DelLoop ();
-
- /* Free the allocated space for the labels */
- xfree (swtab);
-}
-
-
-
-static void SwitchStatement (void)
-/* Handle a 'switch' statement */
-{
- struct expent eval; /* Switch statement expression */
-
- /* Eat the "switch" */
- NextToken ();
-
- /* Read the switch expression */
- ConsumeLParen ();
- intexpr (&eval);
- ConsumeRParen ();
-
- /* result of expr is in P */
- ConsumeLCurly ();
-
- /* Now decide which sort of switch we will create: */
- if (IsTypeChar (eval.e_tptr) || (CodeSizeFactor >= 200 && IsClassInt (eval.e_tptr))) {
- CascadeSwitch (&eval);
- } else {
- TableSwitch (&eval);
- }