2001-10-09 09:44:39 +00:00
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
/* */
|
|
|
|
|
/* stmt.c */
|
|
|
|
|
/* */
|
|
|
|
|
/* Parse a statement */
|
|
|
|
|
/* */
|
|
|
|
|
/* */
|
|
|
|
|
/* */
|
2003-08-29 09:16:28 +00:00
|
|
|
|
/* (C) 1998-2003 Ullrich von Bassewitz */
|
2003-12-09 16:29:59 +00:00
|
|
|
|
/* R<>merstra<72>e 52 */
|
|
|
|
|
/* D-70794 Filderstadt */
|
2001-10-09 09:44:39 +00:00
|
|
|
|
/* EMail: uz@cc65.org */
|
|
|
|
|
/* */
|
|
|
|
|
/* */
|
|
|
|
|
/* This software is provided 'as-is', without any expressed or implied */
|
|
|
|
|
/* warranty. In no event will the authors be held liable for any damages */
|
|
|
|
|
/* arising from the use of this software. */
|
|
|
|
|
/* */
|
|
|
|
|
/* Permission is granted to anyone to use this software for any purpose, */
|
|
|
|
|
/* including commercial applications, and to alter it and redistribute it */
|
|
|
|
|
/* freely, subject to the following restrictions: */
|
|
|
|
|
/* */
|
|
|
|
|
/* 1. The origin of this software must not be misrepresented; you must not */
|
|
|
|
|
/* claim that you wrote the original software. If you use this software */
|
|
|
|
|
/* in a product, an acknowledgment in the product documentation would be */
|
|
|
|
|
/* appreciated but is not required. */
|
|
|
|
|
/* 2. Altered source versions must be plainly marked as such, and must not */
|
|
|
|
|
/* be misrepresented as being the original software. */
|
|
|
|
|
/* 3. This notice may not be removed or altered from any source */
|
|
|
|
|
/* distribution. */
|
|
|
|
|
/* */
|
|
|
|
|
/*****************************************************************************/
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
2000-08-01 18:45:08 +00:00
|
|
|
|
/* common */
|
2001-07-17 21:53:17 +00:00
|
|
|
|
#include "coll.h"
|
2000-08-01 18:45:08 +00:00
|
|
|
|
#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"
|
2001-10-09 09:44:39 +00:00
|
|
|
|
#include "swstmt.h"
|
2000-05-28 13:40:48 +00:00
|
|
|
|
#include "symtab.h"
|
|
|
|
|
#include "stmt.h"
|
2004-03-02 18:00:08 +00:00
|
|
|
|
#include "testexpr.h"
|
2003-08-11 20:18:30 +00:00
|
|
|
|
#include "typeconv.h"
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2001-05-22 16:54:47 +00:00
|
|
|
|
/*****************************************************************************/
|
2001-05-26 10:58:40 +00:00
|
|
|
|
/* Helper functions */
|
2001-05-22 16:54:47 +00:00
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2001-05-26 10:58:40 +00:00
|
|
|
|
static void CheckTok (token_t Tok, const char* Msg, int* PendingToken)
|
|
|
|
|
/* Helper function for Statement. Will check for Tok and print Msg if not
|
|
|
|
|
* found. If PendingToken is NULL, it will the skip the token, otherwise
|
|
|
|
|
* it will store one to PendingToken.
|
|
|
|
|
*/
|
|
|
|
|
{
|
|
|
|
|
if (CurTok.Tok != Tok) {
|
2003-03-13 12:35:54 +00:00
|
|
|
|
Error (Msg);
|
2001-05-26 10:58:40 +00:00
|
|
|
|
} else if (PendingToken) {
|
2003-03-13 12:35:54 +00:00
|
|
|
|
*PendingToken = 1;
|
2001-05-26 10:58:40 +00:00
|
|
|
|
} else {
|
2003-03-13 12:35:54 +00:00
|
|
|
|
NextToken ();
|
2001-05-26 10:58:40 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void CheckSemi (int* PendingToken)
|
2003-03-13 12:35:54 +00:00
|
|
|
|
/* Helper function for Statement. Will check for a semicolon and print an
|
|
|
|
|
* error message if not found (plus some error recovery). If PendingToken is
|
|
|
|
|
* NULL, it will the skip the token, otherwise it will store one to
|
|
|
|
|
* PendingToken.
|
2003-08-11 20:18:30 +00:00
|
|
|
|
* This function is a special version of CheckTok with the addition of the
|
2003-03-13 12:35:54 +00:00
|
|
|
|
* error recovery.
|
2001-05-26 10:58:40 +00:00
|
|
|
|
*/
|
|
|
|
|
{
|
2003-03-13 12:35:54 +00:00
|
|
|
|
int HaveToken = (CurTok.Tok == TOK_SEMI);
|
|
|
|
|
if (!HaveToken) {
|
2003-08-11 20:18:30 +00:00
|
|
|
|
Error ("`;' expected");
|
2003-03-13 12:35:54 +00:00
|
|
|
|
/* Try to be smart about errors */
|
|
|
|
|
if (CurTok.Tok == TOK_COLON || CurTok.Tok == TOK_COMMA) {
|
|
|
|
|
HaveToken = 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (HaveToken) {
|
|
|
|
|
if (PendingToken) {
|
|
|
|
|
*PendingToken = 1;
|
|
|
|
|
} else {
|
|
|
|
|
NextToken ();
|
|
|
|
|
}
|
|
|
|
|
}
|
2001-05-26 10:58:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void SkipPending (int PendingToken)
|
|
|
|
|
/* Skip the pending token if we have one */
|
|
|
|
|
{
|
2001-07-17 12:50:38 +00:00
|
|
|
|
if (PendingToken) {
|
2001-05-26 10:58:40 +00:00
|
|
|
|
NextToken ();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2000-05-28 13:40:48 +00:00
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
/* Code */
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2001-05-22 16:54:47 +00:00
|
|
|
|
static int IfStatement (void)
|
2001-05-22 13:58:56 +00:00
|
|
|
|
/* Handle an 'if' statement */
|
2000-05-28 13:40:48 +00:00
|
|
|
|
{
|
2001-05-22 16:54:47 +00:00
|
|
|
|
unsigned Label1;
|
2004-03-02 18:00:08 +00:00
|
|
|
|
unsigned TestResult;
|
2001-05-22 16:54:47 +00:00
|
|
|
|
int GotBreak;
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
|
|
/* 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 */
|
2001-05-22 16:54:47 +00:00
|
|
|
|
Label1 = GetLocalLabel ();
|
2004-03-02 18:00:08 +00:00
|
|
|
|
TestResult = TestInParens (Label1, 0);
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
|
|
/* Parse the if body */
|
2001-05-26 10:58:40 +00:00
|
|
|
|
GotBreak = Statement (0);
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
|
|
/* Else clause present? */
|
2001-05-22 09:32:24 +00:00
|
|
|
|
if (CurTok.Tok != TOK_ELSE) {
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
2001-05-22 16:54:47 +00:00
|
|
|
|
g_defcodelabel (Label1);
|
2001-05-26 10:58:40 +00:00
|
|
|
|
|
2000-05-28 13:40:48 +00:00
|
|
|
|
/* Since there's no else clause, we're not sure, if the a break
|
|
|
|
|
* statement is really executed.
|
|
|
|
|
*/
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
2004-03-02 18:00:08 +00:00
|
|
|
|
/* Generate a jump around the else branch */
|
2001-05-22 16:54:47 +00:00
|
|
|
|
unsigned Label2 = GetLocalLabel ();
|
2004-03-02 18:00:08 +00:00
|
|
|
|
g_jump (Label2);
|
2001-05-22 16:54:47 +00:00
|
|
|
|
|
2004-03-02 18:00:08 +00:00
|
|
|
|
/* Skip the else */
|
2000-06-12 18:31:40 +00:00
|
|
|
|
NextToken ();
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
2004-03-02 18:00:08 +00:00
|
|
|
|
/* If the if expression was always true, the code in the else branch
|
|
|
|
|
* is never executed. Output a warning if this is the case.
|
|
|
|
|
*/
|
|
|
|
|
if (TestResult == TESTEXPR_TRUE) {
|
|
|
|
|
Warning ("Unreachable code");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Define the target for the first test */
|
|
|
|
|
g_defcodelabel (Label1);
|
2001-05-22 16:54:47 +00:00
|
|
|
|
|
2004-03-02 18:00:08 +00:00
|
|
|
|
/* Total break only if both branches had a break. */
|
2001-05-26 10:58:40 +00:00
|
|
|
|
GotBreak &= Statement (0);
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
|
|
/* Generate the label for the else clause */
|
2004-03-02 18:00:08 +00:00
|
|
|
|
g_defcodelabel (Label2);
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
|
|
/* Done */
|
2001-05-22 16:54:47 +00:00
|
|
|
|
return GotBreak;
|
2000-05-28 13:40:48 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2001-05-22 16:54:47 +00:00
|
|
|
|
static void DoStatement (void)
|
2001-05-22 13:58:56 +00:00
|
|
|
|
/* Handle the 'do' statement */
|
2000-05-28 13:40:48 +00:00
|
|
|
|
{
|
2001-05-22 13:58:56 +00:00
|
|
|
|
/* Get the loop control labels */
|
2003-12-09 16:29:59 +00:00
|
|
|
|
unsigned LoopLabel = GetLocalLabel ();
|
|
|
|
|
unsigned BreakLabel = GetLocalLabel ();
|
|
|
|
|
unsigned ContinueLabel = GetLocalLabel ();
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
2001-05-22 13:58:56 +00:00
|
|
|
|
/* Skip the while token */
|
2000-06-12 18:31:40 +00:00
|
|
|
|
NextToken ();
|
2001-05-22 13:58:56 +00:00
|
|
|
|
|
|
|
|
|
/* Add the loop to the loop stack */
|
2003-12-09 16:29:59 +00:00
|
|
|
|
AddLoop (oursp, BreakLabel, ContinueLabel);
|
2001-05-22 13:58:56 +00:00
|
|
|
|
|
2003-12-09 16:29:59 +00:00
|
|
|
|
/* Define the loop label */
|
|
|
|
|
g_defcodelabel (LoopLabel);
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
2001-05-22 13:58:56 +00:00
|
|
|
|
/* Parse the loop body */
|
2001-05-26 10:58:40 +00:00
|
|
|
|
Statement (0);
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
2003-12-09 16:29:59 +00:00
|
|
|
|
/* Output the label for a continue */
|
|
|
|
|
g_defcodelabel (ContinueLabel);
|
|
|
|
|
|
2001-05-22 13:58:56 +00:00
|
|
|
|
/* Parse the end condition */
|
|
|
|
|
Consume (TOK_WHILE, "`while' expected");
|
2003-12-09 16:29:59 +00:00
|
|
|
|
TestInParens (LoopLabel, 1);
|
2001-05-22 13:58:56 +00:00
|
|
|
|
ConsumeSemi ();
|
|
|
|
|
|
|
|
|
|
/* Define the break label */
|
2003-12-09 16:29:59 +00:00
|
|
|
|
g_defcodelabel (BreakLabel);
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
2001-05-22 13:58:56 +00:00
|
|
|
|
/* Remove the loop from the loop stack */
|
|
|
|
|
DelLoop ();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2001-05-22 16:54:47 +00:00
|
|
|
|
static void WhileStatement (void)
|
2001-05-22 13:58:56 +00:00
|
|
|
|
/* Handle the 'while' statement */
|
|
|
|
|
{
|
2001-05-26 10:58:40 +00:00
|
|
|
|
int PendingToken;
|
|
|
|
|
|
2001-05-22 13:58:56 +00:00
|
|
|
|
/* Get the loop control labels */
|
2003-12-09 16:29:59 +00:00
|
|
|
|
unsigned LoopLabel = GetLocalLabel ();
|
|
|
|
|
unsigned BreakLabel = GetLocalLabel ();
|
2001-05-22 13:58:56 +00:00
|
|
|
|
|
|
|
|
|
/* Skip the while token */
|
|
|
|
|
NextToken ();
|
|
|
|
|
|
2003-12-09 16:29:59 +00:00
|
|
|
|
/* Add the loop to the loop stack. In case of a while loop, the loop head
|
|
|
|
|
* label is used for continue statements.
|
|
|
|
|
*/
|
|
|
|
|
AddLoop (oursp, BreakLabel, LoopLabel);
|
2001-05-22 13:58:56 +00:00
|
|
|
|
|
|
|
|
|
/* Define the head label */
|
2003-12-09 16:29:59 +00:00
|
|
|
|
g_defcodelabel (LoopLabel);
|
2001-05-22 13:58:56 +00:00
|
|
|
|
|
|
|
|
|
/* Test the loop condition */
|
2003-12-09 16:29:59 +00:00
|
|
|
|
TestInParens (BreakLabel, 0);
|
2001-05-22 13:58:56 +00:00
|
|
|
|
|
2001-07-17 17:18:07 +00:00
|
|
|
|
/* Loop body */
|
|
|
|
|
Statement (&PendingToken);
|
2001-07-17 21:24:19 +00:00
|
|
|
|
|
2001-07-17 17:18:07 +00:00
|
|
|
|
/* Jump back to loop top */
|
2003-12-09 16:29:59 +00:00
|
|
|
|
g_jump (LoopLabel);
|
2001-07-17 21:24:19 +00:00
|
|
|
|
|
2001-07-17 17:18:07 +00:00
|
|
|
|
/* Exit label */
|
2003-12-09 16:29:59 +00:00
|
|
|
|
g_defcodelabel (BreakLabel);
|
2001-07-17 21:24:19 +00:00
|
|
|
|
|
2001-10-11 08:02:03 +00:00
|
|
|
|
/* Eat remaining tokens that were delayed because of line info
|
|
|
|
|
* correctness
|
2001-10-09 09:44:39 +00:00
|
|
|
|
*/
|
2001-07-17 17:18:07 +00:00
|
|
|
|
SkipPending (PendingToken);
|
2001-05-22 13:58:56 +00:00
|
|
|
|
|
|
|
|
|
/* Remove the loop from the loop stack */
|
2000-11-02 21:22:33 +00:00
|
|
|
|
DelLoop ();
|
2000-05-28 13:40:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2001-05-22 16:54:47 +00:00
|
|
|
|
static void ReturnStatement (void)
|
2001-05-22 13:58:56 +00:00
|
|
|
|
/* Handle the 'return' statement */
|
2000-05-28 13:40:48 +00:00
|
|
|
|
{
|
2003-08-11 20:18:30 +00:00
|
|
|
|
ExprDesc Expr;
|
|
|
|
|
int k;
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
2000-06-12 18:31:40 +00:00
|
|
|
|
NextToken ();
|
2001-05-22 09:32:24 +00:00
|
|
|
|
if (CurTok.Tok != TOK_SEMI) {
|
2002-11-02 12:39:10 +00:00
|
|
|
|
|
|
|
|
|
/* Check if the function has a return value declared */
|
2002-06-12 12:12:04 +00:00
|
|
|
|
if (F_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
|
|
|
|
}
|
2001-03-26 20:07:05 +00:00
|
|
|
|
|
2003-08-11 20:18:30 +00:00
|
|
|
|
/* Evaluate the return expression */
|
|
|
|
|
k = hie0 (InitExprDesc (&Expr));
|
2000-12-04 22:28:15 +00:00
|
|
|
|
|
2003-08-11 20:18:30 +00:00
|
|
|
|
/* Ignore the return expression if the function returns void */
|
2002-06-12 12:12:04 +00:00
|
|
|
|
if (!F_HasVoidReturn (CurrentFunc)) {
|
2003-08-11 20:18:30 +00:00
|
|
|
|
|
|
|
|
|
/* Convert the return value to the type of the function result */
|
2003-09-30 14:40:32 +00:00
|
|
|
|
k = TypeConversion (&Expr, k, F_GetReturnType (CurrentFunc));
|
2003-08-11 20:18:30 +00:00
|
|
|
|
|
|
|
|
|
/* Load the value into the primary */
|
2003-08-29 09:16:28 +00:00
|
|
|
|
ExprLoad (CF_NONE, k, &Expr);
|
2000-12-04 22:28:15 +00:00
|
|
|
|
}
|
2002-11-02 12:39:10 +00:00
|
|
|
|
|
2002-06-12 12:12:04 +00:00
|
|
|
|
} else if (!F_HasVoidReturn (CurrentFunc) && !F_HasOldStyleIntRet (CurrentFunc)) {
|
|
|
|
|
Error ("Function `%s' must return a value", F_GetFuncName (CurrentFunc));
|
2000-05-28 13:40:48 +00:00
|
|
|
|
}
|
2001-03-26 20:07:05 +00:00
|
|
|
|
|
|
|
|
|
/* Cleanup the stack in case we're inside a block with locals */
|
2002-06-12 12:12:04 +00:00
|
|
|
|
g_space (oursp - F_GetTopLevelSP (CurrentFunc));
|
2001-03-26 20:07:05 +00:00
|
|
|
|
|
|
|
|
|
/* Output a jump to the function exit code */
|
2002-06-12 12:12:04 +00:00
|
|
|
|
g_jump (F_GetRetLab (CurrentFunc));
|
2000-05-28 13:40:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2001-05-22 16:54:47 +00:00
|
|
|
|
static void BreakStatement (void)
|
2001-05-22 13:58:56 +00:00
|
|
|
|
/* Handle the 'break' statement */
|
2000-05-28 13:40:48 +00:00
|
|
|
|
{
|
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) {
|
2001-10-09 09:44:39 +00:00
|
|
|
|
/* Error: No current loop */
|
|
|
|
|
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 */
|
2003-12-09 16:29:59 +00:00
|
|
|
|
g_jump (L->BreakLabel);
|
2000-05-28 13:40:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2001-05-22 16:54:47 +00:00
|
|
|
|
static void ContinueStatement (void)
|
2001-05-22 13:58:56 +00:00
|
|
|
|
/* Handle the 'continue' statement */
|
2000-05-28 13:40:48 +00:00
|
|
|
|
{
|
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) {
|
2003-12-09 16:29:59 +00:00
|
|
|
|
/* Search for a loop that has a continue label. */
|
2001-10-09 09:44:39 +00:00
|
|
|
|
do {
|
2003-12-09 16:29:59 +00:00
|
|
|
|
if (L->ContinueLabel) {
|
|
|
|
|
break;
|
2001-10-09 09:44:39 +00:00
|
|
|
|
}
|
|
|
|
|
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) {
|
2001-10-09 09:44:39 +00:00
|
|
|
|
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);
|
|
|
|
|
|
2003-12-09 16:29:59 +00:00
|
|
|
|
/* Jump to next loop iteration */
|
|
|
|
|
g_jump (L->ContinueLabel);
|
2000-05-28 13:40:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2001-05-22 16:54:47 +00:00
|
|
|
|
static void ForStatement (void)
|
2001-05-22 13:58:56 +00:00
|
|
|
|
/* Handle a 'for' statement */
|
2000-05-28 13:40:48 +00:00
|
|
|
|
{
|
2001-07-17 21:24:19 +00:00
|
|
|
|
ExprDesc lval1;
|
|
|
|
|
ExprDesc lval3;
|
2001-07-17 17:18:07 +00:00
|
|
|
|
int HaveIncExpr;
|
2001-07-17 12:50:38 +00:00
|
|
|
|
CodeMark IncExprStart;
|
|
|
|
|
CodeMark IncExprEnd;
|
2001-05-26 10:58:40 +00:00
|
|
|
|
int PendingToken;
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
2001-05-22 16:54:47 +00:00
|
|
|
|
/* Get several local labels needed later */
|
2003-12-09 16:29:59 +00:00
|
|
|
|
unsigned TestLabel = GetLocalLabel ();
|
|
|
|
|
unsigned BreakLabel = GetLocalLabel ();
|
|
|
|
|
unsigned IncLabel = GetLocalLabel ();
|
|
|
|
|
unsigned BodyLabel = GetLocalLabel ();
|
2001-05-22 16:54:47 +00:00
|
|
|
|
|
2001-07-17 17:18:07 +00:00
|
|
|
|
/* Skip the FOR token */
|
2000-06-12 18:31:40 +00:00
|
|
|
|
NextToken ();
|
2001-05-22 16:54:47 +00:00
|
|
|
|
|
2003-12-09 16:29:59 +00:00
|
|
|
|
/* Add the loop to the loop stack. A continue jumps to the start of the
|
|
|
|
|
* the increment condition.
|
|
|
|
|
*/
|
|
|
|
|
AddLoop (oursp, BreakLabel, IncLabel);
|
2001-05-22 16:54:47 +00:00
|
|
|
|
|
|
|
|
|
/* Skip the opening paren */
|
2000-05-28 13:40:48 +00:00
|
|
|
|
ConsumeLParen ();
|
2001-05-22 16:54:47 +00:00
|
|
|
|
|
|
|
|
|
/* Parse the initializer expression */
|
|
|
|
|
if (CurTok.Tok != TOK_SEMI) {
|
2003-12-09 16:29:59 +00:00
|
|
|
|
expression (&lval1);
|
2000-05-28 13:40:48 +00:00
|
|
|
|
}
|
|
|
|
|
ConsumeSemi ();
|
2001-05-22 16:54:47 +00:00
|
|
|
|
|
|
|
|
|
/* Label for the test expressions */
|
|
|
|
|
g_defcodelabel (TestLabel);
|
|
|
|
|
|
2001-05-26 10:04:15 +00:00
|
|
|
|
/* Parse the test expression */
|
2001-05-22 16:54:47 +00:00
|
|
|
|
if (CurTok.Tok != TOK_SEMI) {
|
2003-12-09 16:29:59 +00:00
|
|
|
|
Test (BodyLabel, 1);
|
|
|
|
|
g_jump (BreakLabel);
|
2000-05-28 13:40:48 +00:00
|
|
|
|
} else {
|
2003-12-09 16:29:59 +00:00
|
|
|
|
g_jump (BodyLabel);
|
2000-05-28 13:40:48 +00:00
|
|
|
|
}
|
|
|
|
|
ConsumeSemi ();
|
2001-05-22 16:54:47 +00:00
|
|
|
|
|
2001-07-17 12:50:38 +00:00
|
|
|
|
/* Remember the start of the increment expression */
|
|
|
|
|
IncExprStart = GetCodePos();
|
|
|
|
|
|
2001-05-22 16:54:47 +00:00
|
|
|
|
/* Label for the increment expression */
|
|
|
|
|
g_defcodelabel (IncLabel);
|
|
|
|
|
|
|
|
|
|
/* Parse the increment expression */
|
2001-07-17 12:50:38 +00:00
|
|
|
|
HaveIncExpr = (CurTok.Tok != TOK_RPAREN);
|
|
|
|
|
if (HaveIncExpr) {
|
2000-05-28 13:40:48 +00:00
|
|
|
|
expression (&lval3);
|
|
|
|
|
}
|
2001-05-22 16:54:47 +00:00
|
|
|
|
|
|
|
|
|
/* Jump to the test */
|
|
|
|
|
g_jump (TestLabel);
|
|
|
|
|
|
2001-07-17 12:50:38 +00:00
|
|
|
|
/* Remember the end of the increment expression */
|
|
|
|
|
IncExprEnd = GetCodePos();
|
|
|
|
|
|
2001-05-22 16:54:47 +00:00
|
|
|
|
/* Skip the closing paren */
|
2000-05-28 13:40:48 +00:00
|
|
|
|
ConsumeRParen ();
|
2001-05-22 16:54:47 +00:00
|
|
|
|
|
|
|
|
|
/* Loop body */
|
2003-12-09 16:29:59 +00:00
|
|
|
|
g_defcodelabel (BodyLabel);
|
2001-05-26 10:58:40 +00:00
|
|
|
|
Statement (&PendingToken);
|
2001-05-22 16:54:47 +00:00
|
|
|
|
|
2001-07-17 12:50:38 +00:00
|
|
|
|
/* If we had an increment expression, move the code to the bottom of
|
|
|
|
|
* the loop. In this case we don't need to jump there at the end of
|
|
|
|
|
* the loop body.
|
|
|
|
|
*/
|
|
|
|
|
if (HaveIncExpr) {
|
2003-12-09 16:29:59 +00:00
|
|
|
|
MoveCode (IncExprStart, IncExprEnd, GetCodePos());
|
2001-07-17 12:50:38 +00:00
|
|
|
|
} else {
|
2003-12-09 16:29:59 +00:00
|
|
|
|
/* Jump back to the increment expression */
|
|
|
|
|
g_jump (IncLabel);
|
2001-07-17 12:50:38 +00:00
|
|
|
|
}
|
2001-07-17 17:18:07 +00:00
|
|
|
|
|
2001-05-26 10:58:40 +00:00
|
|
|
|
/* Skip a pending token if we have one */
|
|
|
|
|
SkipPending (PendingToken);
|
2001-05-22 16:54:47 +00:00
|
|
|
|
|
|
|
|
|
/* Declare the break label */
|
2003-12-09 16:29:59 +00:00
|
|
|
|
g_defcodelabel (BreakLabel);
|
2001-05-22 16:54:47 +00:00
|
|
|
|
|
|
|
|
|
/* Remove the loop from the loop stack */
|
2000-11-02 21:22:33 +00:00
|
|
|
|
DelLoop ();
|
2000-05-28 13:40:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2001-03-26 20:07:05 +00:00
|
|
|
|
static int CompoundStatement (void)
|
2001-05-22 16:54:47 +00:00
|
|
|
|
/* Compound statement. Allow any number of statements inside braces. The
|
|
|
|
|
* function returns true if the last statement was a break or return.
|
|
|
|
|
*/
|
2001-03-26 20:07:05 +00:00
|
|
|
|
{
|
2001-05-22 16:54:47 +00:00
|
|
|
|
int GotBreak;
|
2001-03-26 20:07:05 +00:00
|
|
|
|
|
|
|
|
|
/* Remember the stack at block entry */
|
2001-05-22 16:54:47 +00:00
|
|
|
|
int OldStack = oursp;
|
2001-03-26 20:07:05 +00:00
|
|
|
|
|
|
|
|
|
/* Enter a new lexical level */
|
|
|
|
|
EnterBlockLevel ();
|
|
|
|
|
|
|
|
|
|
/* Parse local variable declarations if any */
|
|
|
|
|
DeclareLocals ();
|
|
|
|
|
|
|
|
|
|
/* Now process statements in this block */
|
2001-05-22 16:54:47 +00:00
|
|
|
|
GotBreak = 0;
|
2001-05-22 09:32:24 +00:00
|
|
|
|
while (CurTok.Tok != TOK_RCURLY) {
|
|
|
|
|
if (CurTok.Tok != TOK_CEOF) {
|
2001-05-26 10:58:40 +00:00
|
|
|
|
GotBreak = Statement (0);
|
2001-03-26 20:07:05 +00:00
|
|
|
|
} else {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Clean up the stack. */
|
2001-05-22 16:54:47 +00:00
|
|
|
|
if (!GotBreak) {
|
|
|
|
|
g_space (oursp - OldStack);
|
2001-03-26 20:07:05 +00:00
|
|
|
|
}
|
2001-05-22 16:54:47 +00:00
|
|
|
|
oursp = OldStack;
|
|
|
|
|
|
|
|
|
|
/* Emit references to imports/exports for this block */
|
|
|
|
|
EmitExternals ();
|
2001-03-26 20:07:05 +00:00
|
|
|
|
|
|
|
|
|
/* Leave the lexical level */
|
|
|
|
|
LeaveBlockLevel ();
|
|
|
|
|
|
2001-05-22 16:54:47 +00:00
|
|
|
|
return GotBreak;
|
2001-03-26 20:07:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2001-05-26 10:58:40 +00:00
|
|
|
|
int Statement (int* PendingToken)
|
2001-03-26 20:07:05 +00:00
|
|
|
|
/* Statement parser. Returns 1 if the statement does a return/break, returns
|
2001-05-26 10:58:40 +00:00
|
|
|
|
* 0 otherwise. If the PendingToken pointer is not NULL, the function will
|
|
|
|
|
* not skip the terminating token of the statement (closing brace or
|
|
|
|
|
* semicolon), but store true if there is a pending token, and false if there
|
|
|
|
|
* is none. The token is always checked, so there is no need for the caller to
|
|
|
|
|
* check this token, it must be skipped, however. If the argument pointer is
|
|
|
|
|
* NULL, the function will skip the token.
|
2000-05-28 13:40:48 +00:00
|
|
|
|
*/
|
|
|
|
|
{
|
2001-07-17 21:24:19 +00:00
|
|
|
|
ExprDesc lval;
|
2001-05-26 10:58:40 +00:00
|
|
|
|
int GotBreak;
|
|
|
|
|
|
|
|
|
|
/* Assume no pending token */
|
|
|
|
|
if (PendingToken) {
|
|
|
|
|
*PendingToken = 0;
|
|
|
|
|
}
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
2001-05-22 16:54:47 +00:00
|
|
|
|
/* Check for a label */
|
2001-05-22 09:32:24 +00:00
|
|
|
|
if (CurTok.Tok == TOK_IDENT && NextTok.Tok == TOK_COLON) {
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
|
|
/* Special handling for a label */
|
|
|
|
|
DoLabel ();
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
2001-05-22 09:32:24 +00:00
|
|
|
|
switch (CurTok.Tok) {
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
2000-06-12 18:31:40 +00:00
|
|
|
|
case TOK_LCURLY:
|
2001-05-26 10:58:40 +00:00
|
|
|
|
NextToken ();
|
|
|
|
|
GotBreak = CompoundStatement ();
|
|
|
|
|
CheckTok (TOK_RCURLY, "`{' expected", PendingToken);
|
|
|
|
|
return GotBreak;
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
2000-06-12 18:31:40 +00:00
|
|
|
|
case TOK_IF:
|
2001-05-22 16:54:47 +00:00
|
|
|
|
return IfStatement ();
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
2000-06-12 18:31:40 +00:00
|
|
|
|
case TOK_WHILE:
|
2001-05-22 16:54:47 +00:00
|
|
|
|
WhileStatement ();
|
2001-05-22 13:58:56 +00:00
|
|
|
|
break;
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
2000-06-12 18:31:40 +00:00
|
|
|
|
case TOK_DO:
|
2001-05-22 16:54:47 +00:00
|
|
|
|
DoStatement ();
|
2001-05-22 13:58:56 +00:00
|
|
|
|
break;
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
2000-06-12 18:31:40 +00:00
|
|
|
|
case TOK_SWITCH:
|
2001-05-22 16:54:47 +00:00
|
|
|
|
SwitchStatement ();
|
2001-05-22 13:58:56 +00:00
|
|
|
|
break;
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
2000-06-12 18:31:40 +00:00
|
|
|
|
case TOK_RETURN:
|
2001-05-22 16:54:47 +00:00
|
|
|
|
ReturnStatement ();
|
2001-05-26 10:58:40 +00:00
|
|
|
|
CheckSemi (PendingToken);
|
2001-05-22 13:58:56 +00:00
|
|
|
|
return 1;
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
2000-06-12 18:31:40 +00:00
|
|
|
|
case TOK_BREAK:
|
2001-05-22 16:54:47 +00:00
|
|
|
|
BreakStatement ();
|
2001-05-26 10:58:40 +00:00
|
|
|
|
CheckSemi (PendingToken);
|
2000-05-28 13:40:48 +00:00
|
|
|
|
return 1;
|
|
|
|
|
|
2000-06-12 18:31:40 +00:00
|
|
|
|
case TOK_CONTINUE:
|
2001-05-22 16:54:47 +00:00
|
|
|
|
ContinueStatement ();
|
2001-05-26 10:58:40 +00:00
|
|
|
|
CheckSemi (PendingToken);
|
2000-05-28 13:40:48 +00:00
|
|
|
|
return 1;
|
|
|
|
|
|
2000-06-12 18:31:40 +00:00
|
|
|
|
case TOK_FOR:
|
2001-05-22 16:54:47 +00:00
|
|
|
|
ForStatement ();
|
2000-05-28 13:40:48 +00:00
|
|
|
|
break;
|
|
|
|
|
|
2000-06-12 18:31:40 +00:00
|
|
|
|
case TOK_GOTO:
|
2001-05-22 16:54:47 +00:00
|
|
|
|
GotoStatement ();
|
2001-05-26 10:58:40 +00:00
|
|
|
|
CheckSemi (PendingToken);
|
2000-05-28 13:40:48 +00:00
|
|
|
|
return 1;
|
|
|
|
|
|
2000-06-12 18:31:40 +00:00
|
|
|
|
case TOK_SEMI:
|
2001-05-22 13:58:56 +00:00
|
|
|
|
/* Ignore it */
|
2001-10-13 09:20:26 +00:00
|
|
|
|
CheckSemi (PendingToken);
|
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 ();
|
2001-10-09 09:44:39 +00:00
|
|
|
|
break;
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
|
|
default:
|
2001-05-22 13:58:56 +00:00
|
|
|
|
/* Actual statement */
|
2000-05-28 13:40:48 +00:00
|
|
|
|
expression (&lval);
|
2001-05-26 10:58:40 +00:00
|
|
|
|
CheckSemi (PendingToken);
|
2000-05-28 13:40:48 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2001-10-11 08:02:03 +00:00
|
|
|
|
|