/*****************************************************************************/
/* */
-/* locals.c */
+/* locals.c */
/* */
-/* Local variable handling for the cc65 C compiler */
+/* Local variable handling for the cc65 C compiler */
/* */
/* */
/* */
-/* (C) 2000-2002 Ullrich von Bassewitz */
-/* Wacholderweg 14 */
-/* D-70597 Stuttgart */
-/* EMail: uz@cc65.org */
+/* (C) 2000-2013, Ullrich von Bassewitz */
+/* Roemerstrasse 52 */
+/* D-70794 Filderstadt */
+/* EMail: uz@cc65.org */
/* */
/* */
/* This software is provided 'as-is', without any expressed or implied */
#include "expr.h"
#include "function.h"
#include "global.h"
-#include "symtab.h"
+#include "loadexpr.h"
#include "locals.h"
+#include "stackptr.h"
+#include "standard.h"
+#include "symtab.h"
+#include "typeconv.h"
+#include "input.h"
/*****************************************************************************/
-/* Data */
-/*****************************************************************************/
-
-
-
-/* Register variable management */
-unsigned MaxRegSpace = 6; /* Maximum space available */
-static unsigned RegOffs = 0; /* Offset into register space */
-static const SymEntry** RegSyms = 0; /* The register variables */
-static unsigned RegSymCount = 0; /* Number of register variables */
-
-
-
-/*****************************************************************************/
-/* Code */
+/* Code */
/*****************************************************************************/
-void InitRegVars (void)
-/* Initialize register variable control data */
+static unsigned AllocLabel (void (*UseSeg) ())
+/* Switch to a segment, define a local label and return it */
{
- /* If the register space is zero, bail out */
- if (MaxRegSpace == 0) {
- return;
- }
-
- /* The maximum number of register variables is equal to the register
- * variable space available. So allocate one pointer per byte. This
- * will usually waste some space but we don't need to dynamically
- * grow the array.
- */
- RegSyms = (const SymEntry**) xmalloc (MaxRegSpace * sizeof (RegSyms[0]));
- RegOffs = MaxRegSpace;
-}
+ unsigned Label;
+ /* Switch to the segment */
+ UseSeg ();
+ /* Define the variable label */
+ Label = GetLocalLabel ();
+ g_defdatalabel (Label);
-void DoneRegVars (void)
-/* Free the register variables */
-{
- xfree (RegSyms);
- RegSyms = 0;
- RegOffs = MaxRegSpace;
- RegSymCount = 0;
+ /* Return the label */
+ return Label;
}
-static int AllocRegVar (const type* Type)
-/* Allocate a register variable for the given variable type. If the allocation
- * was successful, return the offset of the register variable in the register
- * bank (zero page storage). If there is no register space left, return -1.
- */
+static void AllocStorage (unsigned Label, void (*UseSeg) (), unsigned Size)
+/* Reserve Size bytes of BSS storage prefixed by a local label. */
{
- /* Maybe register variables are disabled... */
- if (EnableRegVars) {
-
- /* Get the size of the variable */
- unsigned Size = CheckedSizeOf (Type);
-
- /* Do we have space left? */
- if (RegOffs >= Size) {
- /* Space left. We allocate the variables from high to low addresses,
- * so the adressing is compatible with the saved values on stack.
- * This allows shorter code when saving/restoring the variables.
- */
- RegOffs -= Size;
- return RegOffs;
- }
- }
-
- /* No space left or no allocation */
- return -1;
-}
-
+ /* Switch to the segment */
+ UseSeg ();
+ /* Define the variable label */
+ g_defdatalabel (Label);
-static void RememberRegVar (const SymEntry* Sym)
-/* Remember the given register variable */
-{
- RegSyms[RegSymCount++] = Sym;
+ /* Reserve space for the data */
+ g_res (Size);
}
-static unsigned ParseRegisterDecl (Declaration* Decl, unsigned* SC, int Reg)
-/* Parse the declaration of a register variable. The function returns the
- * symbol data, which is the offset of the variable in the register bank.
- */
+static void ParseRegisterDecl (Declaration* Decl, int Reg)
+/* Parse the declaration of a register variable. Reg is the offset of the
+** variable in the register bank.
+*/
{
- unsigned Flags;
- unsigned InitLabel;
+ SymEntry* Sym;
/* Determine if this is a compound variable */
int IsCompound = IsClassStruct (Decl->Type) || IsTypeArray (Decl->Type);
F_AllocLocalSpace (CurrentFunc);
g_save_regvars (Reg, Size);
+ /* Add the symbol to the symbol table. We do that now, because for register
+ ** variables the current stack pointer is implicitly used as location for
+ ** the save area.
+ */
+ Sym = AddLocalSym (Decl->Ident, Decl->Type, Decl->StorageClass, Reg);
+
/* Check for an optional initialization */
if (CurTok.Tok == TOK_ASSIGN) {
- ExprDesc lval;
+ ExprDesc Expr;
/* Skip the '=' */
NextToken ();
/* Special handling for compound types */
if (IsCompound) {
- /* Switch to read only data */
- g_userodata ();
-
- /* Define a label for the initialization data */
- InitLabel = GetLocalLabel ();
- g_defdatalabel (InitLabel);
+ /* Switch to read only data and define a label for the
+ ** initialization data.
+ */
+ unsigned InitLabel = AllocLabel (g_userodata);
/* Parse the initialization generating a memory image of the
- * data in the RODATA segment.
- */
- ParseInit (Decl->Type);
+ ** data in the RODATA segment. The function does return the size
+ ** of the initialization data, which may be greater than the
+ ** actual size of the type, if the type is a structure with a
+ ** flexible array member that has been initialized. Since we must
+ ** know the size of the data in advance for register variables,
+ ** we cannot allow that here.
+ */
+ if (ParseInit (Sym->Type) != Size) {
+ Error ("Cannot initialize flexible array members of storage class 'register'");
+ }
/* Generate code to copy this data into the variable space */
g_initregister (InitLabel, Reg, Size);
} else {
- /* Setup the type flags for the assignment */
- Flags = CF_REGVAR;
- if (Size == SIZEOF_CHAR) {
- Flags |= CF_FORCECHAR;
- }
+ /* Parse the expression */
+ hie1 (&Expr);
- /* Get the expression into the primary */
- if (evalexpr (Flags, hie1, &lval) == 0) {
- /* Constant expression. Adjust the types */
- assignadjust (Decl->Type, &lval);
- Flags |= CF_CONST;
- } else {
- /* Expression is not constant and in the primary */
- assignadjust (Decl->Type, &lval);
- }
+ /* Convert it to the target type */
+ TypeConversion (&Expr, Sym->Type);
+
+ /* Load the value into the primary */
+ LoadExpr (CF_NONE, &Expr);
/* Store the value into the variable */
- g_putstatic (Flags | TypeOf (Decl->Type), Reg, 0);
+ g_putstatic (CF_REGVAR | TypeOf (Sym->Type), Reg, 0);
}
/* Mark the variable as referenced */
- *SC |= SC_REF;
+ Sym->Flags |= SC_REF;
}
/* Cannot allocate a variable of zero size */
if (Size == 0) {
- Error ("Variable `%s' has unknown size", Decl->Ident);
+ Error ("Variable '%s' has unknown size", Decl->Ident);
}
-
- /* Return the symbol data */
- return Reg;
}
-static unsigned ParseAutoDecl (Declaration* Decl, unsigned* SC)
-/* Parse the declaration of an auto variable. The function returns the symbol
- * data, which is the offset for variables on the stack, and the label for
- * static variables.
- */
+static void ParseAutoDecl (Declaration* Decl)
+/* Parse the declaration of an auto variable. */
{
- unsigned Flags;
- unsigned SymData;
- unsigned InitLabel;
+ unsigned Flags;
+ SymEntry* Sym;
/* Determine if this is a compound variable */
int IsCompound = IsClassStruct (Decl->Type) || IsTypeArray (Decl->Type);
unsigned Size = SizeOf (Decl->Type);
/* Check if this is a variable on the stack or in static memory */
- if (StaticLocals == 0) {
+ if (IS_Get (&StaticLocals) == 0) {
+
+ /* Add the symbol to the symbol table. The stack offset we use here
+ ** may get corrected later.
+ */
+ Sym = AddLocalSym (Decl->Ident, Decl->Type,
+ Decl->StorageClass,
+ F_GetStackPtr (CurrentFunc) - (int) Size);
/* Check for an optional initialization */
if (CurTok.Tok == TOK_ASSIGN) {
- ExprDesc lval;
+ ExprDesc Expr;
/* Skip the '=' */
NextToken ();
/* Special handling for compound types */
if (IsCompound) {
- /* First reserve space for the variable */
- SymData = F_ReserveLocalSpace (CurrentFunc, Size);
+ /* Switch to read only data and define a label for the
+ ** initialization data.
+ */
+ unsigned InitLabel = AllocLabel (g_userodata);
+
+ /* Parse the initialization generating a memory image of the
+ ** data in the RODATA segment. The function will return the
+ ** actual size of the initialization data, which may be
+ ** greater than the size of the variable if it is a struct
+ ** that contains a flexible array member and we're not in
+ ** ANSI mode.
+ */
+ Size = ParseInit (Sym->Type);
+
+ /* Now reserve space for the variable on the stack and correct
+ ** the offset in the symbol table entry.
+ */
+ Sym->V.Offs = F_ReserveLocalSpace (CurrentFunc, Size);
/* Next, allocate the space on the stack. This means that the
- * variable is now located at offset 0 from the current sp.
- */
+ ** variable is now located at offset 0 from the current sp.
+ */
F_AllocLocalSpace (CurrentFunc);
- /* Switch to read only data */
- g_userodata ();
-
- /* Define a label for the initialization data */
- InitLabel = GetLocalLabel ();
- g_defdatalabel (InitLabel);
-
- /* Parse the initialization generating a memory image of the
- * data in the RODATA segment.
- */
- ParseInit (Decl->Type);
-
- /* Generate code to copy this data into the variable space */
+ /* Generate code to copy the initialization data into the
+ ** variable space
+ */
g_initauto (InitLabel, Size);
} else {
/* Setup the type flags for the assignment */
Flags = (Size == SIZEOF_CHAR)? CF_FORCECHAR : CF_NONE;
- /* Get the expression into the primary */
- if (evalexpr (Flags, hie1, &lval) == 0) {
- /* Constant expression. Adjust the types */
- assignadjust (Decl->Type, &lval);
+ /* Parse the expression */
+ hie1 (&Expr);
+
+ /* Convert it to the target type */
+ TypeConversion (&Expr, Sym->Type);
+
+ /* If the value is not const, load it into the primary.
+ ** Otherwise pass the information to the code generator.
+ */
+ if (ED_IsConstAbsInt (&Expr)) {
Flags |= CF_CONST;
} else {
- /* Expression is not constant and in the primary */
- assignadjust (Decl->Type, &lval);
+ LoadExpr (CF_NONE, &Expr);
+ ED_MakeRVal (&Expr);
}
/* Push the value */
- g_push (Flags | TypeOf (Decl->Type), lval.ConstVal);
+ g_push (Flags | TypeOf (Sym->Type), Expr.IVal);
}
/* Mark the variable as referenced */
- *SC |= SC_REF;
+ Sym->Flags |= SC_REF;
- /* Variable is located at the current SP */
- SymData = oursp;
+ /* Make note of auto variables initialized in current block.
+ ** We abuse the Collection somewhat by using it to store line
+ ** numbers.
+ */
+ CollReplace (&CurrentFunc->LocalsBlockStack,
+ (void *)(size_t)GetCurrentLine (),
+ CollCount (&CurrentFunc->LocalsBlockStack) - 1);
} else {
/* Non-initialized local variable. Just keep track of
- * the space needed.
- */
- SymData = F_ReserveLocalSpace (CurrentFunc, Size);
+ ** the space needed.
+ */
+ F_ReserveLocalSpace (CurrentFunc, Size);
}
} else {
- /* Static local variables. */
- *SC = (*SC & ~SC_AUTO) | SC_STATIC;
+ unsigned DataLabel;
+
- /* Put them into the BSS */
- g_usebss ();
+ /* Static local variables. */
+ Decl->StorageClass = (Decl->StorageClass & ~SC_AUTO) | SC_STATIC;
- /* Define the variable label */
- SymData = GetLocalLabel ();
- g_defdatalabel (SymData);
+ /* Generate a label, but don't define it */
+ DataLabel = GetLocalLabel ();
- /* Reserve space for the data */
- g_res (Size);
+ /* Add the symbol to the symbol table. */
+ Sym = AddLocalSym (Decl->Ident, Decl->Type, Decl->StorageClass, DataLabel);
/* Allow assignments */
if (CurTok.Tok == TOK_ASSIGN) {
- ExprDesc lval;
+ ExprDesc Expr;
/* Skip the '=' */
NextToken ();
if (IsCompound) {
- /* Switch to read only data */
- g_userodata ();
-
- /* Define a label for the initialization data */
- InitLabel = GetLocalLabel ();
- g_defdatalabel (InitLabel);
+ /* Switch to read only data and define a label for the
+ ** initialization data.
+ */
+ unsigned InitLabel = AllocLabel (g_userodata);
/* Parse the initialization generating a memory image of the
- * data in the RODATA segment.
- */
- ParseInit (Decl->Type);
+ ** data in the RODATA segment.
+ */
+ Size = ParseInit (Sym->Type);
+
+ /* Allocate space for the variable */
+ AllocStorage (DataLabel, g_usebss, Size);
/* Generate code to copy this data into the variable space */
- g_initstatic (InitLabel, SymData, Size);
+ g_initstatic (InitLabel, DataLabel, Size);
} else {
- /* Setup the type flags for the assignment */
- Flags = (Size == SIZEOF_CHAR)? CF_FORCECHAR : CF_NONE;
+ /* Allocate space for the variable */
+ AllocStorage (DataLabel, g_usebss, Size);
- /* Get the expression into the primary */
- if (evalexpr (Flags, hie1, &lval) == 0) {
- /* Constant expression. Adjust the types */
- assignadjust (Decl->Type, &lval);
- Flags |= CF_CONST;
- /* Load it into the primary */
- exprhs (Flags, 0, &lval);
- } else {
- /* Expression is not constant and in the primary */
- assignadjust (Decl->Type, &lval);
- }
+ /* Parse the expression */
+ hie1 (&Expr);
- /* Store the value into the variable */
- g_putstatic (Flags | TypeOf (Decl->Type), SymData, 0);
+ /* Convert it to the target type */
+ TypeConversion (&Expr, Sym->Type);
+
+ /* Load the value into the primary */
+ LoadExpr (CF_NONE, &Expr);
+ /* Store the value into the variable */
+ g_putstatic (TypeOf (Sym->Type), DataLabel, 0);
}
/* Mark the variable as referenced */
- *SC |= SC_REF;
+ Sym->Flags |= SC_REF;
+
+ } else {
+
+ /* No assignment - allocate a label and space for the variable */
+ AllocStorage (DataLabel, g_usebss, Size);
+
}
}
/* Cannot allocate a variable of zero size */
if (Size == 0) {
- Error ("Variable `%s' has unknown size", Decl->Ident);
+ Error ("Variable '%s' has unknown size", Decl->Ident);
}
-
- /* Return the symbol data */
- return SymData;
}
-static unsigned ParseStaticDecl (Declaration* Decl, unsigned* SC)
-/* Parse the declaration of a static variable. The function returns the symbol
- * data, which is the asm label of the variable.
- */
+static void ParseStaticDecl (Declaration* Decl)
+/* Parse the declaration of a static variable. */
{
- unsigned SymData;
+ unsigned Size;
- /* Get the size of the variable */
- unsigned Size = SizeOf (Decl->Type);
+ /* Generate a label, but don't define it */
+ unsigned DataLabel = GetLocalLabel ();
+
+ /* Add the symbol to the symbol table. */
+ SymEntry* Sym = AddLocalSym (Decl->Ident, Decl->Type,
+ Decl->StorageClass,
+ DataLabel);
/* Static data */
if (CurTok.Tok == TOK_ASSIGN) {
- /* Initialization ahead, switch to data segment */
- if (IsQualConst (Decl->Type)) {
+ /* Initialization ahead, switch to data segment and define the label.
+ ** For arrays, we need to check the elements of the array for
+ ** constness, not the array itself.
+ */
+ if (IsQualConst (GetBaseElementType (Sym->Type))) {
g_userodata ();
} else {
g_usedata ();
}
-
- /* Define the variable label */
- SymData = GetLocalLabel ();
- g_defdatalabel (SymData);
+ g_defdatalabel (DataLabel);
/* Skip the '=' */
NextToken ();
/* Allow initialization of static vars */
- ParseInit (Decl->Type);
-
- /* If the previous size has been unknown, it must be known now */
- if (Size == 0) {
- Size = SizeOf (Decl->Type);
- }
+ Size = ParseInit (Sym->Type);
/* Mark the variable as referenced */
- *SC |= SC_REF;
+ Sym->Flags |= SC_REF;
} else {
- /* Uninitialized data, use BSS segment */
- g_usebss ();
+ /* Get the size of the variable */
+ Size = SizeOf (Decl->Type);
- /* Define the variable label */
- SymData = GetLocalLabel ();
- g_defdatalabel (SymData);
-
- /* Reserve space for the data */
- g_res (Size);
+ /* Allocate a label and space for the variable in the BSS segment */
+ AllocStorage (DataLabel, g_usebss, Size);
}
/* Cannot allocate a variable of zero size */
if (Size == 0) {
- Error ("Variable `%s' has unknown size", Decl->Ident);
+ Error ("Variable '%s' has unknown size", Decl->Ident);
}
-
- /* Return the symbol data */
- return SymData;
}
static void ParseOneDecl (const DeclSpec* Spec)
/* Parse one variable declaration */
{
- unsigned SC; /* Storage class for symbol */
- unsigned SymData = 0; /* Symbol data (offset, label name, ...) */
- Declaration Decl; /* Declaration data structure */
- SymEntry* Sym; /* Symbol declared */
+ Declaration Decl; /* Declaration data structure */
- /* Remember the storage class for the new symbol */
- SC = Spec->StorageClass;
-
/* Read the declaration */
ParseDecl (Spec, &Decl, DM_NEED_IDENT);
/* Set the correct storage class for functions */
- if (IsTypeFunc (Decl.Type)) {
- /* Function prototypes are always external */
- if ((SC & SC_EXTERN) == 0) {
- Warning ("Function must be extern");
- }
- SC |= SC_FUNC | SC_EXTERN;
-
+ if ((Decl.StorageClass & SC_FUNC) == SC_FUNC) {
+ /* Function prototypes are always external */
+ if ((Decl.StorageClass & SC_EXTERN) == 0) {
+ Warning ("Function must be extern");
+ }
+ Decl.StorageClass |= SC_EXTERN;
}
/* If we don't have a name, this was flagged as an error earlier.
- * To avoid problems later, use an anonymous name here.
- */
+ ** To avoid problems later, use an anonymous name here.
+ */
if (Decl.Ident[0] == '\0') {
- AnonName (Decl.Ident, "param");
+ AnonName (Decl.Ident, "param");
+ }
+
+ /* If the symbol is not marked as external, it will be defined now */
+ if ((Decl.StorageClass & SC_EXTERN) == 0) {
+ Decl.StorageClass |= SC_DEF;
}
/* Handle anything that needs storage (no functions, no typdefs) */
- if ((SC & SC_FUNC) != SC_FUNC && (SC & SC_TYPEDEF) != SC_TYPEDEF) {
+ if ((Decl.StorageClass & SC_FUNC) != SC_FUNC &&
+ (Decl.StorageClass & SC_TYPEMASK) != SC_TYPEDEF) {
/* If we have a register variable, try to allocate a register and
- * convert the declaration to "auto" if this is not possible.
- */
+ ** convert the declaration to "auto" if this is not possible.
+ */
int Reg = 0; /* Initialize to avoid gcc complains */
- if ((SC & SC_REGISTER) != 0 && (Reg = AllocRegVar (Decl.Type)) < 0) {
+ if ((Decl.StorageClass & SC_REGISTER) != 0 &&
+ (Reg = F_AllocRegVar (CurrentFunc, Decl.Type)) < 0) {
/* No space for this register variable, convert to auto */
- SC = (SC & ~SC_REGISTER) | SC_AUTO;
+ Decl.StorageClass = (Decl.StorageClass & ~SC_REGISTER) | SC_AUTO;
}
/* Check the variable type */
- if (SC & SC_REGISTER) {
+ if ((Decl.StorageClass & SC_REGISTER) == SC_REGISTER) {
/* Register variable */
- SymData = ParseRegisterDecl (&Decl, &SC, Reg);
- } else if (SC & SC_AUTO) {
+ ParseRegisterDecl (&Decl, Reg);
+ } else if ((Decl.StorageClass & SC_AUTO) == SC_AUTO) {
/* Auto variable */
- SymData = ParseAutoDecl (&Decl, &SC);
- } else if (SC & SC_STATIC) {
+ ParseAutoDecl (&Decl);
+ } else if ((Decl.StorageClass & SC_EXTERN) == SC_EXTERN) {
+ /* External identifier - may not get initialized */
+ if (CurTok.Tok == TOK_ASSIGN) {
+ Error ("Cannot initialize externals");
+ }
+ /* Add the external symbol to the symbol table */
+ AddLocalSym (Decl.Ident, Decl.Type, Decl.StorageClass, 0);
+ } else if ((Decl.StorageClass & SC_STATIC) == SC_STATIC) {
/* Static variable */
- SymData = ParseStaticDecl (&Decl, &SC);
- } else {
- Internal ("Invalid storage class in ParseOneDecl: %04X", SC);
+ ParseStaticDecl (&Decl);
+ } else {
+ Internal ("Invalid storage class in ParseOneDecl: %04X", Decl.StorageClass);
}
- }
- /* If the symbol is not marked as external, it will be defined now */
- if ((SC & SC_EXTERN) == 0) {
- SC |= SC_DEF;
- }
+ } else {
- /* Add the symbol to the symbol table */
- Sym = AddLocalSym (Decl.Ident, Decl.Type, SC, SymData);
+ /* Add the symbol to the symbol table */
+ AddLocalSym (Decl.Ident, Decl.Type, Decl.StorageClass, 0);
- /* If we had declared a register variable, remember it now */
- if (SC & SC_REGISTER) {
- RememberRegVar (Sym);
}
}
/* Declare local variables and types. */
{
/* Remember the current stack pointer */
- int InitialStack = oursp;
+ int InitialStack = StackPtr;
+
+ /* A place to store info about potential initializations of auto variables */
+ CollAppend (&CurrentFunc->LocalsBlockStack, 0);
/* Loop until we don't find any more variables */
while (1) {
- /* Check variable declarations. We need to distinguish between a
- * default int type and the end of variable declarations. So we
- * will do the following: If there is no explicit storage class
- * specifier *and* no explicit type given, it is assume that we
- * have reached the end of declarations.
- */
- DeclSpec Spec;
- ParseDeclSpec (&Spec, SC_AUTO, T_INT);
- if ((Spec.Flags & DS_DEF_STORAGE) != 0 && (Spec.Flags & DS_DEF_TYPE) != 0) {
- break;
- }
-
- /* Accept type only declarations */
- if (CurTok.Tok == TOK_SEMI) {
- /* Type declaration only */
- CheckEmptyDecl (&Spec);
- NextToken ();
- continue;
- }
-
- /* Parse a comma separated variable list */
- while (1) {
-
- /* Parse one declaration */
- ParseOneDecl (&Spec);
-
- /* Check if there is more */
- if (CurTok.Tok == TOK_COMMA) {
- /* More to come */
- NextToken ();
- } else {
- /* Done */
- break;
- }
- }
-
- /* A semicolon must follow */
- ConsumeSemi ();
- }
-
- /* Be sure to allocate any reserved space for locals */
- F_AllocLocalSpace (CurrentFunc);
-
- /* In case we've allocated local variables in this block, emit a call to
- * the stack checking routine if stack checks are enabled.
- */
- if (CheckStack && InitialStack != oursp) {
- g_cstackcheck ();
- }
-}
+ /* Check variable declarations. We need to distinguish between a
+ ** default int type and the end of variable declarations. So we
+ ** will do the following: If there is no explicit storage class
+ ** specifier *and* no explicit type given, *and* no type qualifiers
+ ** have been read, it is assumed that we have reached the end of
+ ** declarations.
+ */
+ DeclSpec Spec;
+ ParseDeclSpec (&Spec, SC_AUTO, T_INT);
+ if ((Spec.Flags & DS_DEF_STORAGE) != 0 && /* No storage spec */
+ (Spec.Flags & DS_DEF_TYPE) != 0 && /* No type given */
+ GetQualifier (Spec.Type) == T_QUAL_NONE) { /* No type qualifier */
+ break;
+ }
+ /* Accept type only declarations */
+ if (CurTok.Tok == TOK_SEMI) {
+ /* Type declaration only */
+ CheckEmptyDecl (&Spec);
+ NextToken ();
+ continue;
+ }
+ /* Parse a comma separated variable list */
+ while (1) {
-void RestoreRegVars (int HaveResult)
-/* Restore the register variables for the local function if there are any.
- * The parameter tells us if there is a return value in ax, in that case,
- * the accumulator must be saved across the restore.
- */
-{
- unsigned I, J;
- int Bytes, Offs;
+ /* Parse one declaration */
+ ParseOneDecl (&Spec);
- /* If we don't have register variables in this function, bail out early */
- if (RegSymCount == 0) {
- return;
- }
+ /* Check if there is more */
+ if (CurTok.Tok == TOK_COMMA) {
+ /* More to come */
+ NextToken ();
+ } else {
+ /* Done */
+ break;
+ }
+ }
- /* Save the accumulator if needed */
- if (!F_HasVoidReturn (CurrentFunc) && HaveResult) {
- g_save (CF_CHAR | CF_FORCECHAR);
+ /* A semicolon must follow */
+ ConsumeSemi ();
}
- /* Walk through all variables. If there are several variables in a row
- * (that is, with increasing stack offset), restore them in one chunk.
- */
- I = 0;
- while (I < RegSymCount) {
-
- /* Check for more than one variable */
- const SymEntry* Sym = RegSyms[I];
- Offs = Sym->V.R.SaveOffs;
- Bytes = CheckedSizeOf (Sym->Type);
- J = I+1;
-
- while (J < RegSymCount) {
-
- /* Get the next symbol */
- const SymEntry* NextSym = RegSyms [J];
-
- /* Get the size */
- int Size = CheckedSizeOf (NextSym->Type);
-
- /* Adjacent variable? */
- if (NextSym->V.R.SaveOffs + Size != Offs) {
- /* No */
- break;
- }
-
- /* Adjacent variable */
- Bytes += Size;
- Offs -= Size;
- Sym = NextSym;
- ++J;
- }
-
- /* Restore the memory range */
- g_restore_regvars (Offs, Sym->V.R.RegOffs, Bytes);
+ /* Be sure to allocate any reserved space for locals */
+ F_AllocLocalSpace (CurrentFunc);
- /* Next round */
- I = J;
+ /* No auto variables were inited. No new block on the stack then. */
+ if (CollLast (&CurrentFunc->LocalsBlockStack) == NULL) {
+ CollPop (&CurrentFunc->LocalsBlockStack);
}
- /* Restore the accumulator if needed */
- if (!F_HasVoidReturn (CurrentFunc) && HaveResult) {
- g_restore (CF_CHAR | CF_FORCECHAR);
+ /* In case we've allocated local variables in this block, emit a call to
+ ** the stack checking routine if stack checks are enabled.
+ */
+ if (IS_Get (&CheckStack) && InitialStack != StackPtr) {
+ g_cstackcheck ();
}
}
-
-
-