X-Git-Url: https://git.sur5r.net/?a=blobdiff_plain;f=src%2Fcc65%2Flocals.c;h=9f4c05088740af5ca6dc55dc8d7d1909b58ea1c6;hb=73dfa23c987d8a7f1154801b85c171f9e01dcd58;hp=31519429549ea44559c3891bf310098f909c3bad;hpb=f24375b241ab9580226e299d5e5b65fe44e4aad9;p=cc65 diff --git a/src/cc65/locals.c b/src/cc65/locals.c index 315194295..9f4c05088 100644 --- a/src/cc65/locals.c +++ b/src/cc65/locals.c @@ -6,10 +6,10 @@ /* */ /* */ /* */ -/* (C) 2000 Ullrich von Bassewitz */ -/* Wacholderweg 14 */ -/* D-70597 Stuttgart */ -/* EMail: uz@musoftware.de */ +/* (C) 2000-2003 Ullrich von Bassewitz */ +/* Römerstrasse 52 */ +/* D-70794 Filderstadt */ +/* EMail: uz@cc65.org */ /* */ /* */ /* This software is provided 'as-is', without any expressed or implied */ @@ -33,438 +33,489 @@ +/* common */ +#include "xmalloc.h" +#include "xsprintf.h" + +/* cc65 */ #include "anonname.h" #include "asmlabel.h" #include "codegen.h" #include "declare.h" +#include "error.h" #include "expr.h" -#include "function.h" /* ## */ +#include "function.h" #include "global.h" -#include "mem.h" #include "symtab.h" #include "locals.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 */ /*****************************************************************************/ -void InitRegVars (void) -/* Initialize register variable control data */ +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. + */ { - /* If the register space is zero, bail out */ - if (MaxRegSpace == 0) { - return; + 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); + + /* 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; + + /* 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); + + /* Parse the initialization generating a memory image of the + * 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 (Decl->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_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; } - /* 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 = xmalloc (MaxRegSpace * sizeof (RegSyms[0])); - RegOffs = MaxRegSpace; -} - - + /* Cannot allocate a variable of zero size */ + if (Size == 0) { + Error ("Variable `%s' has unknown size", Decl->Ident); + } -void DoneRegVars (void) -/* Free the register variables */ -{ - xfree (RegSyms); - RegSyms = 0; - RegOffs = MaxRegSpace; - RegSymCount = 0; + /* Return the symbol data */ + return Reg; } -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. +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. */ { - /* Maybe register variables are disabled... */ - if (EnableRegVars) { - - /* Get the size of the variable */ - unsigned Size = SizeOf (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; - } - } + unsigned Flags; + unsigned SymData; + unsigned InitLabel; - /* No space left or no allocation */ - return -1; -} + /* 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) { -void DeclareLocals (void) -/* Declare local variables and types. */ -{ - int offs = oursp; /* Current stack offset for variable */ - int AutoSpace = 0; /* Unallocated space on the stack */ - int Size; /* Size of an auto variable */ - int Reg; /* Register variable offset */ - unsigned flags = 0; /* Code generator flags */ - int SymbolSC; /* Storage class for symbol */ - int ldata = 0; /* Local symbol data temp storage */ + /* Check for an optional initialization */ + if (CurTok.Tok == TOK_ASSIGN) { - /* Loop until we don't find any more variables */ - while (1) { + ExprDesc lval; - /* 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; - } + /* Skip the '=' */ + NextToken (); - /* Accept type only declarations */ - if (curtok == SEMI) { - /* Type declaration only ### Check struct/union here */ - gettok (); - continue; - } + /* Special handling for compound types */ + if (IsCompound) { - /* Parse a comma separated variable list */ - while (1) { + /* Switch to read only data */ + g_userodata (); - Declaration Decl; + /* Define a label for the initialization data */ + InitLabel = GetLocalLabel (); + g_defdatalabel (InitLabel); - /* Remember the storage class for the new symbol */ - SymbolSC = Spec.StorageClass; + /* 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 (Decl->Type); - /* Read the declaration */ - ParseDecl (&Spec, &Decl, DM_NEED_IDENT); + /* Now reserve space for the variable on the stack */ + SymData = F_ReserveLocalSpace (CurrentFunc, Size); - /* If we don't have a name, this was flagged as an error earlier. - * To avoid problems later, use an anonymous name here. - */ - if (Decl.Ident[0] == '\0') { - AnonName (Decl.Ident, "param"); - } + /* Next, allocate the space on the stack. This means that the + * variable is now located at offset 0 from the current sp. + */ + F_AllocLocalSpace (CurrentFunc); - if (!IsFunc (Decl.Type) && (SymbolSC & SC_TYPEDEF) != SC_TYPEDEF) { + /* Generate code to copy the initialization data into the + * variable space + */ + g_initauto (InitLabel, Size); - /* Get the size of the variable */ - Size = SizeOf (Decl.Type); + } else { -#if 0 - /* Check the storage class */ - if ((SymbolSC & SC_REGISTER) && (Reg = AllocRegVar (psym, tarray)) >= 0) { + /* Allocate previously reserved local space */ + F_AllocLocalSpace (CurrentFunc); - /* We will store the current value of the register onto the - * stack, thus making functions with register variables - * reentrant. If we have pending auto variables, emit them - * now. - */ - g_usecode (); - g_space (AutoSpace); - oursp -= AutoSpace; - AutoSpace = 0; + /* Setup the type flags for the assignment */ + Flags = (Size == SIZEOF_CHAR)? CF_FORCECHAR : CF_NONE; - /* Remember the register bank offset */ - ldata = Reg; + /* 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); + } - /* Save the current register value onto the stack */ - g_save_regvars (Reg, Size); + /* Push the value */ + g_push (Flags | TypeOf (Decl->Type), lval.ConstVal); - /* Allow variable initialization */ - if (curtok == ASGN) { + } - struct expent lval; + /* Mark the variable as referenced */ + *SC |= SC_REF; - /* Skip the '=' */ - gettok (); + /* Variable is located at the current SP */ + SymData = oursp; - /* Get the expression into the primary */ - expression1 (&lval); + } else { + /* Non-initialized local variable. Just keep track of + * the space needed. + */ + SymData = F_ReserveLocalSpace (CurrentFunc, Size); + } - /* Make type adjustments if needed */ - assignadjust (tarray, &lval); + } else { - /* Setup the type flags for the assignment */ - flags = TypeOf (tarray) | CF_REGVAR; - if (Size == 1) { - flags |= CF_FORCECHAR; - } + /* Static local variables. */ + *SC = (*SC & ~SC_AUTO) | SC_STATIC; - /* Store the value into the register */ - g_putstatic (flags, Reg, 0); + /* Put them into the BSS */ + g_usebss (); - /* Mark the variable as referenced */ - SymbolSC |= SC_REF; + /* Define the variable label */ + SymData = GetLocalLabel (); + g_defdatalabel (SymData); - } + /* Reserve space for the data */ + g_res (Size); - /* Account for the stack space needed and remember the - * stack offset of the save area. - */ - offs -= Size; - psym->h_lattr = offs; + /* Allow assignments */ + if (CurTok.Tok == TOK_ASSIGN) { - } else if (SymbolSC & (SC_AUTO | SC_REGISTER)) { -#endif - if (SymbolSC & (SC_AUTO | SC_REGISTER)) { + ExprDesc lval; - /* Auto variable */ - if (LocalsAreStatic == 0) { + /* Skip the '=' */ + NextToken (); - /* Change SC in case it was register */ - SymbolSC = (SymbolSC & ~SC_REGISTER) | SC_AUTO; - if (curtok == ASGN) { + if (IsCompound) { - struct expent lval; + /* Switch to read only data */ + g_userodata (); - /* Switch to the code segment, allocate space for - * uninitialized variables. - */ - g_usecode (); - g_space (AutoSpace); - oursp -= AutoSpace; - AutoSpace = 0; + /* Define a label for the initialization data */ + InitLabel = GetLocalLabel (); + g_defdatalabel (InitLabel); - /* Skip the '=' */ - gettok (); + /* Parse the initialization generating a memory image of the + * data in the RODATA segment. + */ + ParseInit (Decl->Type); - /* Setup the type flags for the assignment */ - flags = Size == 1? CF_FORCECHAR : CF_NONE; + /* Generate code to copy this data into the variable space */ + g_initstatic (InitLabel, SymData, 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; - } else { - /* Expression is not constant and in the primary */ - assignadjust (Decl.Type, &lval); - } + } else { - /* Push the value */ - g_push (flags | TypeOf (Decl.Type), lval.e_const); + /* Setup the type flags for the assignment */ + Flags = (Size == SIZEOF_CHAR)? CF_FORCECHAR : CF_NONE; - /* Mark the variable as referenced */ - SymbolSC |= SC_REF; + /* 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); + } - } else { - /* Non-initialized local variable. Just keep track of - * the space needed. - */ - AutoSpace += Size; - } + /* Store the value into the variable */ + g_putstatic (Flags | TypeOf (Decl->Type), SymData, 0); - /* Allocate space on the stack, assign the offset */ - offs -= Size; - ldata = offs; + } - } else { + /* Mark the variable as referenced */ + *SC |= SC_REF; + } + } - /* Static local variables. */ - SymbolSC = (SymbolSC & ~(SC_REGISTER | SC_AUTO)) | SC_STATIC; + /* Cannot allocate a variable of zero size */ + if (Size == 0) { + Error ("Variable `%s' has unknown size", Decl->Ident); + } - /* Put them into the BSS */ - g_usebss (); + /* Return the symbol data */ + return SymData; +} - /* Define the variable label */ - g_defloclabel (ldata = GetLabel ()); - /* Reserve space for the data */ - g_res (Size); - /* Allow assignments */ - if (curtok == ASGN) { +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; - struct expent lval; + /* Get the size of the variable */ + unsigned Size = SizeOf (Decl->Type); - /* Switch to the code segment. */ - g_usecode (); + /* Static data */ + if (CurTok.Tok == TOK_ASSIGN) { - /* Skip the '=' */ - gettok (); + /* Initialization ahead, switch to data segment */ + if (IsQualConst (Decl->Type)) { + g_userodata (); + } else { + g_usedata (); + } - /* Get the expression into the primary */ - expression1 (&lval); + /* Define the variable label */ + SymData = GetLocalLabel (); + g_defdatalabel (SymData); - /* Make type adjustments if needed */ - assignadjust (Decl.Type, &lval); + /* Skip the '=' */ + NextToken (); - /* Setup the type flags for the assignment */ - flags = TypeOf (Decl.Type); - if (Size == 1) { - flags |= CF_FORCECHAR; - } + /* Allow initialization of static vars */ + ParseInit (Decl->Type); - /* Store the value into the variable */ - g_putstatic (flags, ldata, 0); + /* If the previous size has been unknown, it must be known now */ + if (Size == 0) { + Size = SizeOf (Decl->Type); + } - /* Mark the variable as referenced */ - SymbolSC |= SC_REF; - } - } + /* Mark the variable as referenced */ + *SC |= SC_REF; - } else if ((SymbolSC & SC_STATIC) == SC_STATIC) { + } else { - /* Static data */ - if (curtok == ASGN) { + /* Uninitialized data, use BSS segment */ + g_usebss (); - /* Initialization ahead, switch to data segment */ - g_usedata (); + /* Define the variable label */ + SymData = GetLocalLabel (); + g_defdatalabel (SymData); - /* Define the variable label */ - g_defloclabel (ldata = GetLabel ()); + /* Reserve space for the data */ + g_res (Size); - /* Skip the '=' */ - gettok (); + } - /* Allow initialization of static vars */ - ParseInit (Decl.Type); + /* Cannot allocate a variable of zero size */ + if (Size == 0) { + Error ("Variable `%s' has unknown size", Decl->Ident); + } - /* Mark the variable as referenced */ - SymbolSC |= SC_REF; + /* Return the symbol data */ + return SymData; +} - } else { - /* Uninitialized data, use BSS segment */ - g_usebss (); - /* Define the variable label */ - g_defloclabel (ldata = GetLabel ()); +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 */ - /* Reserve space for the data */ - g_res (Size); - } - } + /* Remember the storage class for the new symbol */ + SC = Spec->StorageClass; - } + /* Read the declaration */ + ParseDecl (Spec, &Decl, DM_NEED_IDENT); - /* If the symbol is not marked as external, it will be defined */ - if ((SymbolSC & SC_EXTERN) == 0) { - SymbolSC |= SC_DEF; - } + /* 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; - /* Add the symbol to the symbol table */ - AddLocalSym (Decl.Ident, Decl.Type, SymbolSC, ldata); + } - if (curtok != COMMA) { - break; - } - gettok (); - } - if (curtok == SEMI) { - gettok (); - } + /* If we don't have a name, this was flagged as an error earlier. + * To avoid problems later, use an anonymous name here. + */ + if (Decl.Ident[0] == '\0') { + AnonName (Decl.Ident, "param"); } - /* In case we switched away from code segment, switch back now */ - g_usecode (); + /* Handle anything that needs storage (no functions, no typdefs) */ + if ((SC & SC_FUNC) != SC_FUNC && (SC & SC_TYPEDEF) != SC_TYPEDEF) { + + /* 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, &SC); + } else if ((SC & SC_STATIC) == SC_STATIC) { + /* Static variable */ + SymData = ParseStaticDecl (&Decl, &SC); + } else { + Internal ("Invalid storage class in ParseOneDecl: %04X", SC); + } + } - /* Create space for locals */ - g_space (AutoSpace); - oursp -= AutoSpace; + /* If the symbol is not marked as external, it will be defined now */ + if ((SC & SC_EXTERN) == 0) { + SC |= SC_DEF; + } + + /* Add the symbol to the symbol table */ + AddLocalSym (Decl.Ident, Decl.Type, SC, SymData); } -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. - */ +void DeclareLocals (void) +/* Declare local variables and types. */ { - 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 (!HasVoidReturn (CurrentFunc) && HaveResult) { - g_save (CF_CHAR | CF_FORCECHAR); - } + /* Remember the current stack pointer */ + int InitialStack = oursp; - /* 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) { + /* Loop until we don't find any more variables */ + while (1) { - /* Check for more than one variable */ - const SymEntry* Sym = RegSyms[I]; - Offs = Sym->V.Offs; - Bytes = SizeOf (Sym->Type); - J = I+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, *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; + } - while (J < RegSymCount) { + /* Accept type only declarations */ + if (CurTok.Tok == TOK_SEMI) { + /* Type declaration only */ + CheckEmptyDecl (&Spec); + NextToken (); + continue; + } - /* Get the next symbol */ - const SymEntry* NextSym = RegSyms [J]; + /* Parse a comma separated variable list */ + while (1) { - /* Get the size */ - int Size = SizeOf (NextSym->Type); + /* Parse one declaration */ + ParseOneDecl (&Spec); - /* Adjacent variable? */ - if (NextSym->V.Offs + Size != Offs) { - /* No */ - break; + /* Check if there is more */ + if (CurTok.Tok == TOK_COMMA) { + /* More to come */ + NextToken (); + } else { + /* Done */ + 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; + /* A semicolon must follow */ + ConsumeSemi (); } - /* Restore the accumulator if needed */ - if (!HasVoidReturn (CurrentFunc) && HaveResult) { - g_restore (CF_CHAR | CF_FORCECHAR); + /* 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 (); } } - +