- /* Output the loop code */
- if (L->linc) {
- g_jump (L->linc);
- } else {
- g_jump (L->Loop);
- }
-}
-
-
-
-static void CascadeSwitch (struct expent* eval)
-/* Handle a switch statement for chars with a cmp cascade for the selector */
-{
- unsigned ExitLab; /* Exit label */
- unsigned NextLab; /* Next case label */
- unsigned CodeLab; /* Label that starts the actual selector code */
- int HaveBreak; /* Remember if we exited with break */
- int HaveDefault; /* Remember if we had a default label */
- int lcount; /* Label count */
- unsigned Flags; /* Code generator flags */
- struct expent lval; /* Case label expression */
- long Val; /* Case label value */
-
-
- /* Create a loop so we may break out, init labels */
- ExitLab = GetLocalLabel ();
- AddLoop (oursp, 0, ExitLab, 0, 0);
-
- /* Setup some variables needed in the loop below */
- Flags = TypeOf (eval->e_tptr) | CF_CONST | CF_FORCECHAR;
- CodeLab = NextLab = 0;
- HaveBreak = 1;
- HaveDefault = 0;
-
- /* Parse the labels */
- lcount = 0;
- while (CurTok.Tok != TOK_RCURLY) {
-
- if (CurTok.Tok == TOK_CASE || CurTok.Tok == TOK_DEFAULT) {
-
- /* If the code for the previous selector did not end with a
- * break statement, we must jump over the next selector test.
- */
- if (!HaveBreak) {
- /* Define a label for the code */
- if (CodeLab == 0) {
- CodeLab = GetLocalLabel ();
- }
- g_jump (CodeLab);
- }
-
- /* If we have a cascade label, emit it */
- if (NextLab) {
- g_defcodelabel (NextLab);
- NextLab = 0;
- }
-
- while (CurTok.Tok == TOK_CASE || CurTok.Tok == TOK_DEFAULT) {
-
- /* Parse the selector */
- if (CurTok.Tok == TOK_CASE) {
-
- /* Count labels */
- ++lcount;
-
- /* Skip the "case" token */
- NextToken ();
-
- /* Read the selector expression */
- constexpr (&lval);
- if (!IsClassInt (lval.e_tptr)) {
- Error ("Switch quantity not an integer");
- }
-
- /* Check the range of the expression */
- Val = lval.e_const;
- switch (*eval->e_tptr) {
-
- case T_SCHAR:
- /* Signed char */
- if (Val < -128 || Val > 127) {
- Error ("Range error");
- }
- break;
-
- case T_UCHAR:
- if (Val < 0 || Val > 255) {
- Error ("Range error");
- }
- break;
-
- case T_INT:
- if (Val < -32768 || Val > 32767) {
- Error ("Range error");
- }
- break;
-
- case T_UINT:
- if (Val < 0 || Val > 65535) {
- Error ("Range error");
- }
- break;
-
- default:
- Internal ("Invalid type: %02X", *eval->e_tptr & 0xFF);
- }
-
- /* Emit a compare */
- g_cmp (Flags, Val);
-
- /* If another case follows, we will jump to the code if
- * the condition is true.
- */
- if (CurTok.Tok == TOK_CASE) {
- /* Create a code label if needed */
- if (CodeLab == 0) {
- CodeLab = GetLocalLabel ();
- }
- g_falsejump (CF_NONE, CodeLab);
- } else if (CurTok.Tok != TOK_DEFAULT) {
- /* No case follows, jump to next selector */
- if (NextLab == 0) {
- NextLab = GetLocalLabel ();
- }
- g_truejump (CF_NONE, NextLab);
- }
-
- /* Skip the colon */
- ConsumeColon ();
-
- } else {
-
- /* Default case */
- NextToken ();
-
- /* Handle the pathologic case: DEFAULT followed by CASE */
- if (CurTok.Tok == TOK_CASE) {
- if (CodeLab == 0) {
- CodeLab = GetLocalLabel ();
- }
- g_jump (CodeLab);
- }
-
- /* Skip the colon */
- ConsumeColon ();
-
- /* Remember that we had a default label */
- HaveDefault = 1;
- }
-
- }
-
- }
-
- /* Emit a code label if we have one */
- if (CodeLab) {
- g_defcodelabel (CodeLab);
- CodeLab = 0;
- }
-
- /* Parse statements */
- if (CurTok.Tok != TOK_RCURLY) {
- HaveBreak = Statement (0);
- }
- }
-
- /* Check if we have any labels */
- if (lcount == 0 && !HaveDefault) {
- Warning ("No case labels");
- }
-
- /* 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 (0);
- }
- }
-
- /* 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);
- }