/* */
/* */
/* */
-/* (C) 2000-2002 Ullrich von Bassewitz */
-/* Wacholderweg 14 */
-/* D-70597 Stuttgart */
+/* (C) 2000-2003 Ullrich von Bassewitz */
+/* Roemerstrasse 52 */
+/* D-70794 Filderstadt */
/* EMail: uz@cc65.org */
/* */
/* */
/*****************************************************************************/
-/* Data */
+/* Code */
/*****************************************************************************/
-/* 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 */
+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.
+ */
+{
+ unsigned Flags;
+ unsigned InitLabel;
+ /* Determine if this is a compound variable */
+ int IsCompound = IsClassStruct (Decl->Type) || IsTypeArray (Decl->Type);
+ /* Get the size of the variable */
+ unsigned Size = SizeOf (Decl->Type);
-/*****************************************************************************/
-/* Code */
-/*****************************************************************************/
+ /* Save the current contents of the register variable on stack */
+ F_AllocLocalSpace (CurrentFunc);
+ g_save_regvars (Reg, Size);
+ /* Check for an optional initialization */
+ if (CurTok.Tok == TOK_ASSIGN) {
+ ExprDesc lval;
-void InitRegVars (void)
-/* Initialize register variable control data */
-{
- /* If the register space is zero, bail out */
- if (MaxRegSpace == 0) {
- return;
- }
+ /* Skip the '=' */
+ NextToken ();
- /* 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;
-}
+ /* 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);
-void DoneRegVars (void)
-/* Free the register variables */
-{
- xfree (RegSyms);
- RegSyms = 0;
- RegOffs = MaxRegSpace;
- RegSymCount = 0;
-}
+ /* 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 */
+ g_initregister (InitLabel, Reg, Size);
+ } else {
-static int AllocRegVar (const SymEntry* Sym, const type* tarray)
-/* Allocate a register variable with the given amount of storage. 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.
- */
-{
- /* Maybe register variables are disabled... */
- if (EnableRegVars) {
-
- /* Get the size of the variable */
- unsigned Size = CheckedSizeOf (tarray);
-
- /* 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;
- RegSyms [RegSymCount++] = Sym;
- return RegOffs;
- }
+ /* Setup the type flags for the assignment */
+ Flags = CF_NONE;
+ if (Size == SIZEOF_CHAR) {
+ Flags |= CF_FORCECHAR;
+ }
+
+ /* 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);
+ }
+
+ /* Store the value into the variable */
+ Flags |= CF_REGVAR;
+ g_putstatic (Flags | TypeOf (Decl->Type), Reg, 0);
+
+ }
+
+ /* Mark the variable as referenced */
+ *SC |= SC_REF;
}
- /* No space left or no allocation */
- return -1;
+ /* Cannot allocate a variable of zero size */
+ if (Size == 0) {
+ Error ("Variable `%s' has unknown size", Decl->Ident);
+ }
+
+ /* Return the symbol data */
+ return Reg;
}
-static unsigned ParseAutoDecl (Declaration* Decl, unsigned Size, unsigned* SC)
+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.
/* Determine if this is a compound variable */
int IsCompound = IsClassStruct (Decl->Type) || IsTypeArray (Decl->Type);
+ /* Get the size of the variable */
+ unsigned Size = SizeOf (Decl->Type);
+
/* Check if this is a variable on the stack or in static memory */
if (StaticLocals == 0) {
- /* Change SC in case it was register */
- *SC = (*SC & ~SC_REGISTER) | SC_AUTO;
+ /* Check for an optional initialization */
if (CurTok.Tok == TOK_ASSIGN) {
ExprDesc lval;
F_AllocLocalSpace (CurrentFunc);
/* Setup the type flags for the assignment */
- Flags = (Size == 1)? CF_FORCECHAR : CF_NONE;
+ Flags = (Size == SIZEOF_CHAR)? CF_FORCECHAR : CF_NONE;
/* Get the expression into the primary */
if (evalexpr (Flags, hie1, &lval) == 0) {
} else {
/* Static local variables. */
- *SC = (*SC & ~(SC_REGISTER | SC_AUTO)) | SC_STATIC;
+ *SC = (*SC & ~SC_AUTO) | SC_STATIC;
/* Put them into the BSS */
g_usebss ();
} else {
/* Setup the type flags for the assignment */
- Flags = (Size == 1)? CF_FORCECHAR : CF_NONE;
+ Flags = (Size == SIZEOF_CHAR)? CF_FORCECHAR : CF_NONE;
/* Get the expression into the primary */
if (evalexpr (Flags, hie1, &lval) == 0) {
}
}
+ /* Cannot allocate a variable of zero size */
+ if (Size == 0) {
+ Error ("Variable `%s' has unknown size", Decl->Ident);
+ }
+
/* Return the symbol data */
return SymData;
}
-static unsigned ParseStaticDecl (Declaration* Decl, unsigned Size, unsigned* SC)
+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.
*/
{
unsigned SymData;
+ /* Get the size of the variable */
+ unsigned Size = SizeOf (Decl->Type);
+
/* Static data */
if (CurTok.Tok == TOK_ASSIGN) {
}
+ /* Cannot allocate a variable of zero size */
+ if (Size == 0) {
+ Error ("Variable `%s' has unknown size", Decl->Ident);
+ }
+
/* Return the symbol data */
return SymData;
}
/* Parse one variable declaration */
{
unsigned SC; /* Storage class for symbol */
- unsigned Size; /* Size of the data object */
unsigned SymData = 0; /* Symbol data (offset, label name, ...) */
- Declaration Decl; /* Declaration data structure */
+ Declaration Decl; /* Declaration data structure */
+
/* Remember the storage class for the new symbol */
SC = Spec->StorageClass;
/* Set the correct storage class for functions */
if (IsTypeFunc (Decl.Type)) {
- /* Function prototypes are always external */
- if ((SC & SC_EXTERN) == 0) {
+ /* Function prototypes are always external */
+ if ((SC & SC_EXTERN) == 0) {
Warning ("Function must be extern");
- }
+ }
SC |= SC_FUNC | SC_EXTERN;
}
* To avoid problems later, use an anonymous name here.
*/
if (Decl.Ident[0] == '\0') {
- AnonName (Decl.Ident, "param");
+ AnonName (Decl.Ident, "param");
}
/* Handle anything that needs storage (no functions, no typdefs) */
if ((SC & SC_FUNC) != SC_FUNC && (SC & SC_TYPEDEF) != SC_TYPEDEF) {
- /* Get the size of the variable */
- Size = SizeOf (Decl.Type);
-
- /* */
- if (SC & (SC_AUTO | SC_REGISTER)) {
+ /* If we have a register variable, try to allocate a register and
+ * convert the declaration to "auto" if this is not possible.
+ */
+ int Reg = 0; /* Initialize to avoid gcc complains */
+ if ((SC & 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;
+ }
+ /* Check the variable type */
+ if ((SC & SC_REGISTER) == SC_REGISTER) {
+ /* Register variable */
+ SymData = ParseRegisterDecl (&Decl, &SC, Reg);
+ } else if ((SC & SC_AUTO) == SC_AUTO) {
/* Auto variable */
- SymData = ParseAutoDecl (&Decl, Size, &SC);
-
- } else if ((SC & SC_STATIC) == SC_STATIC) {
-
+ SymData = ParseAutoDecl (&Decl, &SC);
+ } else if ((SC & SC_STATIC) == SC_STATIC) {
/* Static variable */
- SymData = ParseStaticDecl (&Decl, Size, &SC);
-
- }
-
- /* Cannot allocate a variable of zero size */
- if (Size == 0) {
- Error ("Variable `%s' has unknown size", Decl.Ident);
- return;
- }
+ SymData = ParseStaticDecl (&Decl, &SC);
+ } else {
+ Internal ("Invalid storage class in ParseOneDecl: %04X", SC);
+ }
}
- /* If the symbol is not marked as external, it will be defined */
+ /* If the symbol is not marked as external, it will be defined now */
if ((SC & SC_EXTERN) == 0) {
- SC |= SC_DEF;
+ SC |= SC_DEF;
}
/* Add the symbol to the symbol table */
/* 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.
+ * 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 && (Spec.Flags & DS_DEF_TYPE) != 0) {
+ 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;
}
-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;
-
- /* If we don't have register variables in this function, bail out early */
- if (RegSymCount == 0) {
- return;
- }
-
- /* Save the accumulator if needed */
- if (!F_HasVoidReturn (CurrentFunc) && HaveResult) {
- g_save (CF_CHAR | CF_FORCECHAR);
- }
-
- /* 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.Offs;
- 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.Offs + Size != Offs) {
- /* No */
- break;
- }
-
- /* Adjacent variable */
- Bytes += Size;
- Offs -= Size;
- Sym = NextSym;
- ++J;
- }
-
- /* Restore the memory range */
- g_restore_regvars (Offs, Sym->V.Offs, Bytes);
-
- /* Next round */
- I = J;
- }
-
- /* Restore the accumulator if needed */
- if (!F_HasVoidReturn (CurrentFunc) && HaveResult) {
- g_restore (CF_CHAR | CF_FORCECHAR);
- }
-}
-
-
-