2000-05-28 13:40:48 +00:00
|
|
|
/*
|
|
|
|
* stmt.c
|
|
|
|
*
|
|
|
|
* Ullrich von Bassewitz, 06.08.1998
|
|
|
|
*
|
|
|
|
* Original by John R. Dunning - see copyleft.jrd
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2000-08-01 18:45:08 +00:00
|
|
|
/* common */
|
|
|
|
#include "xmalloc.h"
|
2000-10-15 19:52:01 +00:00
|
|
|
|
2000-08-01 18:45:08 +00:00
|
|
|
/* cc65 */
|
2000-05-28 13:40:48 +00:00
|
|
|
#include "asmcode.h"
|
|
|
|
#include "asmlabel.h"
|
|
|
|
#include "codegen.h"
|
|
|
|
#include "datatype.h"
|
|
|
|
#include "error.h"
|
|
|
|
#include "expr.h"
|
|
|
|
#include "function.h"
|
|
|
|
#include "global.h"
|
|
|
|
#include "goto.h"
|
|
|
|
#include "litpool.h"
|
|
|
|
#include "locals.h"
|
|
|
|
#include "loop.h"
|
|
|
|
#include "pragma.h"
|
|
|
|
#include "scanner.h"
|
|
|
|
#include "symtab.h"
|
|
|
|
#include "stmt.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
/* Data */
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Maximum count of cases */
|
|
|
|
#define CASE_MAX 257
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
/* Code */
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static int statement (void);
|
|
|
|
/* Forward decl */
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static int doif (void)
|
|
|
|
/* Handle 'if' statement here */
|
|
|
|
{
|
|
|
|
int flab1;
|
|
|
|
int flab2;
|
|
|
|
int gotbreak;
|
|
|
|
|
|
|
|
/* Skip the if */
|
2000-06-12 18:31:40 +00:00
|
|
|
NextToken ();
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
/* Generate a jump label and parse the condition */
|
|
|
|
flab1 = GetLabel ();
|
|
|
|
test (flab1, 0);
|
|
|
|
|
|
|
|
/* Parse the if body */
|
|
|
|
gotbreak = statement ();
|
|
|
|
|
|
|
|
/* Else clause present? */
|
2000-06-12 18:31:40 +00:00
|
|
|
if (curtok != TOK_ELSE) {
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
g_defloclabel (flab1);
|
|
|
|
/* Since there's no else clause, we're not sure, if the a break
|
|
|
|
* statement is really executed.
|
|
|
|
*/
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
/* Skip the else */
|
2000-06-12 18:31:40 +00:00
|
|
|
NextToken ();
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
/* If we had some sort of break statement at the end of the if clause,
|
|
|
|
* there's no need to generate an additional jump around the else
|
|
|
|
* clause, since the jump is never reached.
|
|
|
|
*/
|
|
|
|
if (!gotbreak) {
|
|
|
|
flab2 = GetLabel ();
|
|
|
|
g_jump (flab2);
|
|
|
|
} else {
|
|
|
|
/* Mark the label as unused */
|
|
|
|
flab2 = 0;
|
|
|
|
}
|
|
|
|
g_defloclabel (flab1);
|
|
|
|
gotbreak &= statement ();
|
|
|
|
|
|
|
|
/* Generate the label for the else clause */
|
|
|
|
if (flab2) {
|
|
|
|
g_defloclabel (flab2);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Done */
|
|
|
|
return gotbreak;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void dowhile (char wtype)
|
|
|
|
/* Handle 'while' statement here */
|
|
|
|
{
|
|
|
|
int loop;
|
|
|
|
int lab;
|
|
|
|
|
2000-06-12 18:31:40 +00:00
|
|
|
NextToken ();
|
2000-05-28 13:40:48 +00:00
|
|
|
loop = GetLabel ();
|
|
|
|
lab = GetLabel ();
|
2000-11-02 21:22:33 +00:00
|
|
|
AddLoop (oursp, loop, lab, 0, 0);
|
2000-05-28 13:40:48 +00:00
|
|
|
g_defloclabel (loop);
|
|
|
|
if (wtype == 'w') {
|
|
|
|
|
|
|
|
/* While loop */
|
|
|
|
test (lab, 0);
|
|
|
|
|
|
|
|
/* If the statement following the while loop is empty, that is, we have
|
|
|
|
* something like "while (1) ;", the test function ommitted the jump as
|
|
|
|
* an optimization. Since we know, the condition codes are set, we can
|
|
|
|
* do another small optimization here, and use a conditional jump
|
|
|
|
* instead an absolute one.
|
|
|
|
*/
|
2000-06-12 18:31:40 +00:00
|
|
|
if (curtok == TOK_SEMI) {
|
2000-05-28 13:40:48 +00:00
|
|
|
/* Shortcut */
|
2000-06-12 18:31:40 +00:00
|
|
|
NextToken ();
|
2000-05-28 13:40:48 +00:00
|
|
|
/* Use a conditional jump */
|
|
|
|
g_truejump (CF_NONE, loop);
|
|
|
|
} else {
|
|
|
|
/* There is code inside the while loop */
|
|
|
|
statement ();
|
|
|
|
g_jump (loop);
|
|
|
|
g_defloclabel (lab);
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
/* Do loop */
|
|
|
|
statement ();
|
2000-11-02 21:22:33 +00:00
|
|
|
Consume (TOK_WHILE, "`while' expected");
|
2000-05-28 13:40:48 +00:00
|
|
|
test (loop, 1);
|
|
|
|
ConsumeSemi ();
|
|
|
|
g_defloclabel (lab);
|
|
|
|
|
|
|
|
}
|
2000-11-02 21:22:33 +00:00
|
|
|
DelLoop ();
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void doreturn (void)
|
|
|
|
/* Handle 'return' statement here */
|
|
|
|
{
|
|
|
|
struct expent lval;
|
|
|
|
unsigned etype = 0; /* Type of return expression */
|
|
|
|
int HaveVal = 0; /* Do we have a return value in ax? */
|
|
|
|
|
|
|
|
|
2000-06-12 18:31:40 +00:00
|
|
|
NextToken ();
|
|
|
|
if (curtok != TOK_SEMI) {
|
2000-05-28 13:40:48 +00:00
|
|
|
if (HasVoidReturn (CurrentFunc)) {
|
2000-11-02 21:22:33 +00:00
|
|
|
Error ("Returning a value in function with return type void");
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
if (evalexpr (CF_NONE, hie0, &lval) == 0) {
|
|
|
|
/* Constant value */
|
|
|
|
etype = CF_CONST;
|
|
|
|
} else {
|
|
|
|
/* Value in the primary register */
|
|
|
|
HaveVal = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Convert the return value to the type of the function result */
|
|
|
|
if (!HasVoidReturn (CurrentFunc)) {
|
|
|
|
etype |= assignadjust (GetReturnType (CurrentFunc), &lval) & ~CF_CONST;
|
|
|
|
}
|
|
|
|
} else if (!HasVoidReturn (CurrentFunc)) {
|
2000-11-02 21:22:33 +00:00
|
|
|
Error ("Function `%s' must return a value", GetFuncName (CurrentFunc));
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
RestoreRegVars (HaveVal);
|
|
|
|
g_leave (etype, lval.e_const);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void dobreak (void)
|
|
|
|
/* Handle 'break' statement here */
|
|
|
|
{
|
2000-11-02 21:22:33 +00:00
|
|
|
LoopDesc* L;
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2000-11-02 21:22:33 +00:00
|
|
|
/* Skip the break */
|
2000-06-12 18:31:40 +00:00
|
|
|
NextToken ();
|
2000-11-02 21:22:33 +00:00
|
|
|
|
|
|
|
/* Get the current loop descriptor */
|
|
|
|
L = CurrentLoop ();
|
|
|
|
|
|
|
|
/* Check if we are inside a loop */
|
|
|
|
if (L == 0) {
|
2000-05-28 13:40:48 +00:00
|
|
|
/* Error: No current loop */
|
2000-11-02 21:22:33 +00:00
|
|
|
Error ("`break' statement not within loop or switch");
|
|
|
|
return;
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
2000-11-02 21:22:33 +00:00
|
|
|
|
|
|
|
/* Correct the stack pointer if needed */
|
|
|
|
g_space (oursp - L->StackPtr);
|
|
|
|
|
|
|
|
/* Jump to the exit label of the loop */
|
|
|
|
g_jump (L->Label);
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void docontinue (void)
|
|
|
|
/* Handle 'continue' statement here */
|
|
|
|
{
|
2000-11-02 21:22:33 +00:00
|
|
|
LoopDesc* L;
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2000-11-02 21:22:33 +00:00
|
|
|
/* Skip the continue */
|
2000-06-12 18:31:40 +00:00
|
|
|
NextToken ();
|
2000-11-02 21:22:33 +00:00
|
|
|
|
|
|
|
/* Get the current loop descriptor */
|
|
|
|
L = CurrentLoop ();
|
|
|
|
if (L) {
|
|
|
|
/* Search for the correct loop */
|
|
|
|
do {
|
|
|
|
if (L->Loop) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
L = L->Next;
|
|
|
|
} while (L);
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
2000-11-02 21:22:33 +00:00
|
|
|
|
|
|
|
/* Did we find it? */
|
|
|
|
if (L == 0) {
|
|
|
|
Error ("`continue' statement not within a loop");
|
|
|
|
return;
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
2000-11-02 21:22:33 +00:00
|
|
|
|
|
|
|
/* Correct the stackpointer if needed */
|
|
|
|
g_space (oursp - L->StackPtr);
|
|
|
|
|
|
|
|
/* Output the loop code */
|
|
|
|
if (L->linc) {
|
|
|
|
g_jump (L->linc);
|
2000-05-28 13:40:48 +00:00
|
|
|
} else {
|
2000-11-02 21:22:33 +00:00
|
|
|
g_jump (L->Loop);
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void cascadeswitch (struct expent* eval)
|
|
|
|
/* Handle a switch statement for chars with a cmp cascade for the selector */
|
|
|
|
{
|
2000-11-22 12:02:15 +00:00
|
|
|
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 */
|
2000-05-28 13:40:48 +00:00
|
|
|
int lcount; /* Label count */
|
2000-11-22 12:02:15 +00:00
|
|
|
unsigned Flags; /* Code generator flags */
|
2000-05-28 13:40:48 +00:00
|
|
|
struct expent lval; /* Case label expression */
|
2000-11-22 12:02:15 +00:00
|
|
|
long Val; /* Case label value */
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* Create a loop so we may break out, init labels */
|
2000-11-22 12:02:15 +00:00
|
|
|
ExitLab = GetLabel ();
|
|
|
|
AddLoop (oursp, 0, ExitLab, 0, 0);
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
/* Setup some variables needed in the loop below */
|
2000-11-22 12:02:15 +00:00
|
|
|
Flags = TypeOf (eval->e_tptr) | CF_CONST | CF_FORCECHAR;
|
|
|
|
CodeLab = NextLab = 0;
|
|
|
|
HaveBreak = 1;
|
|
|
|
HaveDefault = 0;
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
/* Parse the labels */
|
|
|
|
lcount = 0;
|
2000-06-12 18:31:40 +00:00
|
|
|
while (curtok != TOK_RCURLY) {
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2000-06-12 18:31:40 +00:00
|
|
|
if (curtok == TOK_CASE || curtok == TOK_DEFAULT) {
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
/* If the code for the previous selector did not end with a
|
|
|
|
* break statement, we must jump over the next selector test.
|
|
|
|
*/
|
2000-11-22 12:02:15 +00:00
|
|
|
if (!HaveBreak) {
|
2000-05-28 13:40:48 +00:00
|
|
|
/* Define a label for the code */
|
2000-11-22 12:02:15 +00:00
|
|
|
if (CodeLab == 0) {
|
|
|
|
CodeLab = GetLabel ();
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
2000-11-22 12:02:15 +00:00
|
|
|
g_jump (CodeLab);
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* If we have a cascade label, emit it */
|
2000-11-22 12:02:15 +00:00
|
|
|
if (NextLab) {
|
|
|
|
g_defloclabel (NextLab);
|
|
|
|
NextLab = 0;
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
2000-06-12 18:31:40 +00:00
|
|
|
while (curtok == TOK_CASE || curtok == TOK_DEFAULT) {
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
/* Parse the selector */
|
2000-06-12 18:31:40 +00:00
|
|
|
if (curtok == TOK_CASE) {
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
/* Count labels */
|
|
|
|
++lcount;
|
|
|
|
|
|
|
|
/* Skip the "case" token */
|
2000-06-12 18:31:40 +00:00
|
|
|
NextToken ();
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
/* Read the selector expression */
|
|
|
|
constexpr (&lval);
|
2000-08-01 18:45:08 +00:00
|
|
|
if (!IsClassInt (lval.e_tptr)) {
|
2000-11-02 21:22:33 +00:00
|
|
|
Error ("Switch quantity not an integer");
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Check the range of the expression */
|
2000-11-22 12:02:15 +00:00
|
|
|
Val = lval.e_const;
|
2000-05-28 13:40:48 +00:00
|
|
|
switch (*eval->e_tptr) {
|
|
|
|
|
2000-08-01 15:04:35 +00:00
|
|
|
case T_SCHAR:
|
2000-05-28 13:40:48 +00:00
|
|
|
/* Signed char */
|
2000-11-22 12:02:15 +00:00
|
|
|
if (Val < -128 || Val > 127) {
|
2000-11-02 21:22:33 +00:00
|
|
|
Error ("Range error");
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case T_UCHAR:
|
2000-11-22 12:02:15 +00:00
|
|
|
if (Val < 0 || Val > 255) {
|
2000-11-02 21:22:33 +00:00
|
|
|
Error ("Range error");
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case T_INT:
|
2000-11-22 12:02:15 +00:00
|
|
|
if (Val < -32768 || Val > 32767) {
|
2000-11-02 21:22:33 +00:00
|
|
|
Error ("Range error");
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2000-11-22 12:02:15 +00:00
|
|
|
case T_UINT:
|
|
|
|
if (Val < 0 || Val > 65535) {
|
2000-11-02 21:22:33 +00:00
|
|
|
Error ("Range error");
|
2000-11-22 12:02:15 +00:00
|
|
|
}
|
2000-05-28 13:40:48 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2000-11-22 12:02:15 +00:00
|
|
|
Internal ("Invalid type: %02X", *eval->e_tptr & 0xFF);
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Skip the colon */
|
|
|
|
ConsumeColon ();
|
|
|
|
|
|
|
|
/* Emit a compare */
|
2000-11-22 12:02:15 +00:00
|
|
|
g_cmp (Flags, Val);
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
/* If another case follows, we will jump to the code if
|
|
|
|
* the condition is true.
|
|
|
|
*/
|
2000-06-12 18:31:40 +00:00
|
|
|
if (curtok == TOK_CASE) {
|
2000-05-28 13:40:48 +00:00
|
|
|
/* Create a code label if needed */
|
2000-11-22 12:02:15 +00:00
|
|
|
if (CodeLab == 0) {
|
|
|
|
CodeLab = GetLabel ();
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
2000-11-22 12:02:15 +00:00
|
|
|
g_falsejump (CF_NONE, CodeLab);
|
2000-06-12 18:31:40 +00:00
|
|
|
} else if (curtok != TOK_DEFAULT) {
|
2000-11-22 12:02:15 +00:00
|
|
|
/* No case follows, jump to next selector */
|
|
|
|
if (NextLab == 0) {
|
|
|
|
NextLab = GetLabel ();
|
|
|
|
}
|
|
|
|
g_truejump (CF_NONE, NextLab);
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
/* Default case */
|
2000-06-12 18:31:40 +00:00
|
|
|
NextToken ();
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
/* Skip the colon */
|
|
|
|
ConsumeColon ();
|
|
|
|
|
|
|
|
/* Handle the pathologic case: DEFAULT followed by CASE */
|
2000-06-12 18:31:40 +00:00
|
|
|
if (curtok == TOK_CASE) {
|
2000-11-22 12:02:15 +00:00
|
|
|
if (CodeLab == 0) {
|
|
|
|
CodeLab = GetLabel ();
|
|
|
|
}
|
|
|
|
g_jump (CodeLab);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Remember that we had a default label */
|
|
|
|
HaveDefault = 1;
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Emit a code label if we have one */
|
2000-11-22 12:02:15 +00:00
|
|
|
if (CodeLab) {
|
|
|
|
g_defloclabel (CodeLab);
|
|
|
|
CodeLab = 0;
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Parse statements */
|
2000-06-12 18:31:40 +00:00
|
|
|
if (curtok != TOK_RCURLY) {
|
2000-11-22 12:02:15 +00:00
|
|
|
HaveBreak = statement ();
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if we have any labels */
|
2000-11-22 12:02:15 +00:00
|
|
|
if (lcount == 0 && !HaveDefault) {
|
2000-10-15 19:52:01 +00:00
|
|
|
Warning ("No case labels");
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Eat the closing curly brace */
|
2000-06-12 18:31:40 +00:00
|
|
|
NextToken ();
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
/* Define the exit label and, if there's a next label left, create this
|
|
|
|
* one, too.
|
|
|
|
*/
|
2000-11-22 12:02:15 +00:00
|
|
|
if (NextLab) {
|
|
|
|
g_defloclabel (NextLab);
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
2000-11-22 12:02:15 +00:00
|
|
|
g_defloclabel (ExitLab);
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
/* End the loop */
|
2000-11-02 21:22:33 +00:00
|
|
|
DelLoop ();
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 */
|
2000-11-22 12:02:15 +00:00
|
|
|
int HaveBreak; /* Last statement has a break */
|
|
|
|
int HaveDefault; /* Remember if we had a default label */
|
|
|
|
unsigned Flags; /* Code generator flags */
|
2000-05-28 13:40:48 +00:00
|
|
|
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 */
|
2000-11-22 12:02:15 +00:00
|
|
|
HaveBreak = 0; /* Keep gcc silent */
|
|
|
|
HaveDefault = 0; /* No default case until now */
|
2000-05-28 13:40:48 +00:00
|
|
|
dlabel = 0; /* init */
|
|
|
|
lab = GetLabel (); /* get exit */
|
|
|
|
p = swtab;
|
2000-11-02 21:22:33 +00:00
|
|
|
AddLoop (oursp, 0, lab, 0, 0);
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
/* Jump behind the code for the CASE labels */
|
|
|
|
g_jump (lcase = GetLabel ());
|
|
|
|
lcount = 0;
|
2000-06-12 18:31:40 +00:00
|
|
|
while (curtok != TOK_RCURLY) {
|
|
|
|
if (curtok == TOK_CASE || curtok == TOK_DEFAULT) {
|
2000-05-28 13:40:48 +00:00
|
|
|
if (lcount >= CASE_MAX) {
|
2000-10-15 19:52:01 +00:00
|
|
|
Fatal ("Too many case labels");
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
label = GetLabel ();
|
|
|
|
do {
|
2000-06-12 18:31:40 +00:00
|
|
|
if (curtok == TOK_CASE) {
|
|
|
|
NextToken ();
|
2000-05-28 13:40:48 +00:00
|
|
|
constexpr (&lval);
|
2000-08-01 18:45:08 +00:00
|
|
|
if (!IsClassInt (lval.e_tptr)) {
|
2000-11-02 21:22:33 +00:00
|
|
|
Error ("Switch quantity not an integer");
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
p->sw_const = lval.e_const;
|
|
|
|
p->sw_lab = label;
|
|
|
|
++p;
|
|
|
|
++lcount;
|
|
|
|
} else {
|
2000-06-12 18:31:40 +00:00
|
|
|
NextToken ();
|
2000-05-28 13:40:48 +00:00
|
|
|
dlabel = label;
|
2000-11-22 12:02:15 +00:00
|
|
|
HaveDefault = 1;
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
ConsumeColon ();
|
2000-06-12 18:31:40 +00:00
|
|
|
} while (curtok == TOK_CASE || curtok == TOK_DEFAULT);
|
2000-05-28 13:40:48 +00:00
|
|
|
g_defloclabel (label);
|
2000-11-22 12:02:15 +00:00
|
|
|
HaveBreak = 0;
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
2000-06-12 18:31:40 +00:00
|
|
|
if (curtok != TOK_RCURLY) {
|
2000-11-22 12:02:15 +00:00
|
|
|
HaveBreak = statement ();
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if we have any labels */
|
2000-11-22 12:02:15 +00:00
|
|
|
if (lcount == 0 && !HaveDefault) {
|
2000-10-15 19:52:01 +00:00
|
|
|
Warning ("No case labels");
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Eat the closing curly brace */
|
2000-06-12 18:31:40 +00:00
|
|
|
NextToken ();
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
/* If the last statement doesn't have a break or return, add one */
|
2000-11-22 12:02:15 +00:00
|
|
|
if (!HaveBreak) {
|
2000-05-28 13:40:48 +00:00
|
|
|
g_jump (lab);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Actual selector code goes here */
|
|
|
|
g_defloclabel (lcase);
|
|
|
|
|
|
|
|
/* Create the call to the switch subroutine */
|
2000-11-22 12:02:15 +00:00
|
|
|
Flags = TypeOf (eval->e_tptr);
|
|
|
|
g_switch (Flags);
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
/* First entry is negative of label count */
|
2000-05-30 17:40:38 +00:00
|
|
|
g_defdata (CF_INT | CF_CONST, -((int)lcount)-1, 0);
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
/* Create the case selector table */
|
|
|
|
AddCodeHint ("casetable");
|
|
|
|
p = swtab;
|
|
|
|
while (lcount) {
|
2000-11-22 12:02:15 +00:00
|
|
|
g_case (Flags, p->sw_lab, p->sw_const); /* Create one label */
|
2000-05-28 13:40:48 +00:00
|
|
|
--lcount;
|
|
|
|
++p;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dlabel) {
|
|
|
|
g_jump (dlabel);
|
|
|
|
}
|
|
|
|
g_defloclabel (lab);
|
2000-11-02 21:22:33 +00:00
|
|
|
DelLoop ();
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
/* Free the allocated space for the labels */
|
|
|
|
xfree (swtab);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void doswitch (void)
|
|
|
|
/* Handle 'switch' statement here */
|
|
|
|
{
|
|
|
|
struct expent eval; /* Switch statement expression */
|
|
|
|
|
|
|
|
/* Eat the "switch" */
|
2000-06-12 18:31:40 +00:00
|
|
|
NextToken ();
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
/* Read the switch expression */
|
|
|
|
ConsumeLParen ();
|
|
|
|
intexpr (&eval);
|
|
|
|
ConsumeRParen ();
|
|
|
|
|
|
|
|
/* result of expr is in P */
|
|
|
|
ConsumeLCurly ();
|
|
|
|
|
|
|
|
/* Now decide which sort of switch we will create: */
|
2000-08-01 18:45:08 +00:00
|
|
|
if (IsTypeChar (eval.e_tptr) || (FavourSize == 0 && IsClassInt (eval.e_tptr))) {
|
2000-05-28 13:40:48 +00:00
|
|
|
cascadeswitch (&eval);
|
|
|
|
} else {
|
|
|
|
tableswitch (&eval);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void dofor (void)
|
|
|
|
/* Handle 'for' statement here */
|
|
|
|
{
|
|
|
|
int loop;
|
|
|
|
int lab;
|
|
|
|
int linc;
|
|
|
|
int lstat;
|
|
|
|
struct expent lval1;
|
|
|
|
struct expent lval2;
|
|
|
|
struct expent lval3;
|
|
|
|
|
2000-06-12 18:31:40 +00:00
|
|
|
NextToken ();
|
2000-05-28 13:40:48 +00:00
|
|
|
loop = GetLabel ();
|
|
|
|
lab = GetLabel ();
|
|
|
|
linc = GetLabel ();
|
|
|
|
lstat = GetLabel ();
|
2000-11-02 21:22:33 +00:00
|
|
|
AddLoop (oursp, loop, lab, linc, lstat);
|
2000-05-28 13:40:48 +00:00
|
|
|
ConsumeLParen ();
|
2000-06-12 18:31:40 +00:00
|
|
|
if (curtok != TOK_SEMI) { /* exp1 */
|
2000-05-28 13:40:48 +00:00
|
|
|
expression (&lval1);
|
|
|
|
}
|
|
|
|
ConsumeSemi ();
|
|
|
|
g_defloclabel (loop);
|
2000-06-12 18:31:40 +00:00
|
|
|
if (curtok != TOK_SEMI) { /* exp2 */
|
2000-05-28 13:40:48 +00:00
|
|
|
boolexpr (&lval2);
|
|
|
|
g_truejump (CF_NONE, lstat);
|
|
|
|
g_jump (lab);
|
|
|
|
} else {
|
|
|
|
g_jump (lstat);
|
|
|
|
}
|
|
|
|
ConsumeSemi ();
|
|
|
|
g_defloclabel (linc);
|
2000-06-12 18:31:40 +00:00
|
|
|
if (curtok != TOK_RPAREN) { /* exp3 */
|
2000-05-28 13:40:48 +00:00
|
|
|
expression (&lval3);
|
|
|
|
}
|
|
|
|
ConsumeRParen ();
|
|
|
|
g_jump (loop);
|
|
|
|
g_defloclabel (lstat);
|
|
|
|
statement ();
|
|
|
|
g_jump (linc);
|
|
|
|
g_defloclabel (lab);
|
2000-11-02 21:22:33 +00:00
|
|
|
DelLoop ();
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static int statement (void)
|
|
|
|
/* Statement parser. Called whenever syntax requires a statement.
|
|
|
|
* This routine performs that statement and returns 1 if it is a branch,
|
|
|
|
* 0 otherwise
|
|
|
|
*/
|
|
|
|
{
|
|
|
|
struct expent lval;
|
|
|
|
|
|
|
|
/* */
|
2000-06-12 18:31:40 +00:00
|
|
|
if (curtok == TOK_IDENT && nxttok == TOK_COLON) {
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
/* Special handling for a label */
|
|
|
|
DoLabel ();
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
switch (curtok) {
|
|
|
|
|
2000-06-12 18:31:40 +00:00
|
|
|
case TOK_LCURLY:
|
2000-05-28 13:40:48 +00:00
|
|
|
return compound ();
|
|
|
|
|
2000-06-12 18:31:40 +00:00
|
|
|
case TOK_IF:
|
2000-05-28 13:40:48 +00:00
|
|
|
return doif ();
|
|
|
|
|
2000-06-12 18:31:40 +00:00
|
|
|
case TOK_WHILE:
|
2000-05-28 13:40:48 +00:00
|
|
|
dowhile ('w');
|
|
|
|
break;
|
|
|
|
|
2000-06-12 18:31:40 +00:00
|
|
|
case TOK_DO:
|
2000-05-28 13:40:48 +00:00
|
|
|
dowhile ('d');
|
|
|
|
break;
|
|
|
|
|
2000-06-12 18:31:40 +00:00
|
|
|
case TOK_SWITCH:
|
2000-05-28 13:40:48 +00:00
|
|
|
doswitch ();
|
|
|
|
break;
|
|
|
|
|
2000-06-12 18:31:40 +00:00
|
|
|
case TOK_RETURN:
|
2000-05-28 13:40:48 +00:00
|
|
|
doreturn ();
|
|
|
|
ConsumeSemi ();
|
|
|
|
return 1;
|
|
|
|
|
2000-06-12 18:31:40 +00:00
|
|
|
case TOK_BREAK:
|
2000-05-28 13:40:48 +00:00
|
|
|
dobreak ();
|
|
|
|
ConsumeSemi ();
|
|
|
|
return 1;
|
|
|
|
|
2000-06-12 18:31:40 +00:00
|
|
|
case TOK_CONTINUE:
|
2000-05-28 13:40:48 +00:00
|
|
|
docontinue ();
|
|
|
|
ConsumeSemi ();
|
|
|
|
return 1;
|
|
|
|
|
2000-06-12 18:31:40 +00:00
|
|
|
case TOK_FOR:
|
2000-05-28 13:40:48 +00:00
|
|
|
dofor ();
|
|
|
|
break;
|
|
|
|
|
2000-06-12 18:31:40 +00:00
|
|
|
case TOK_GOTO:
|
2000-05-28 13:40:48 +00:00
|
|
|
DoGoto ();
|
|
|
|
ConsumeSemi ();
|
|
|
|
return 1;
|
|
|
|
|
2000-06-12 18:31:40 +00:00
|
|
|
case TOK_SEMI:
|
2000-05-28 13:40:48 +00:00
|
|
|
/* ignore it. */
|
2000-06-12 18:31:40 +00:00
|
|
|
NextToken ();
|
2000-05-28 13:40:48 +00:00
|
|
|
break;
|
|
|
|
|
2000-06-12 18:31:40 +00:00
|
|
|
case TOK_PRAGMA:
|
2000-05-28 13:40:48 +00:00
|
|
|
DoPragma ();
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
AddCodeHint ("stmt:start");
|
|
|
|
expression (&lval);
|
|
|
|
AddCodeHint ("stmt:end");
|
|
|
|
ConsumeSemi ();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int compound (void)
|
|
|
|
/* Compound statement. Allow any number of statements, inside braces. */
|
|
|
|
{
|
|
|
|
static unsigned CurrentLevel = 0;
|
|
|
|
|
|
|
|
int isbrk;
|
|
|
|
int oldsp;
|
|
|
|
|
|
|
|
/* eat LCURLY */
|
2000-06-12 18:31:40 +00:00
|
|
|
NextToken ();
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
/* Remember the stack at block entry */
|
|
|
|
oldsp = oursp;
|
|
|
|
|
|
|
|
/* If we're not on function level, enter a new lexical level */
|
|
|
|
if (CurrentLevel++ > 0) {
|
|
|
|
/* A nested block */
|
|
|
|
EnterBlockLevel ();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Parse local variable declarations if any */
|
|
|
|
DeclareLocals ();
|
|
|
|
|
|
|
|
/* Now process statements in the function body */
|
|
|
|
isbrk = 0;
|
2000-06-12 18:31:40 +00:00
|
|
|
while (curtok != TOK_RCURLY) {
|
|
|
|
if (curtok == TOK_CEOF)
|
2000-05-28 13:40:48 +00:00
|
|
|
break;
|
|
|
|
else {
|
|
|
|
isbrk = statement ();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Emit references to imports/exports for this block */
|
|
|
|
EmitExternals ();
|
|
|
|
|
|
|
|
/* If this is not the top level compound statement, clean up the stack.
|
|
|
|
* For a top level statement this will be done by the function exit code.
|
|
|
|
*/
|
|
|
|
if (--CurrentLevel != 0) {
|
|
|
|
/* Some sort of nested block */
|
|
|
|
LeaveBlockLevel ();
|
|
|
|
if (isbrk) {
|
|
|
|
oursp = oldsp;
|
|
|
|
} else {
|
|
|
|
g_space (oursp - oldsp);
|
|
|
|
oursp = oldsp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Eat closing brace */
|
|
|
|
ConsumeRCurly ();
|
|
|
|
|
|
|
|
return isbrk;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|