1 /*****************************************************************************/
5 /* Local variable handling for the cc65 C compiler */
9 /* (C) 2000-2002 Ullrich von Bassewitz */
11 /* D-70597 Stuttgart */
12 /* EMail: uz@cc65.org */
15 /* This software is provided 'as-is', without any expressed or implied */
16 /* warranty. In no event will the authors be held liable for any damages */
17 /* arising from the use of this software. */
19 /* Permission is granted to anyone to use this software for any purpose, */
20 /* including commercial applications, and to alter it and redistribute it */
21 /* freely, subject to the following restrictions: */
23 /* 1. The origin of this software must not be misrepresented; you must not */
24 /* claim that you wrote the original software. If you use this software */
25 /* in a product, an acknowledgment in the product documentation would be */
26 /* appreciated but is not required. */
27 /* 2. Altered source versions must be plainly marked as such, and must not */
28 /* be misrepresented as being the original software. */
29 /* 3. This notice may not be removed or altered from any source */
32 /*****************************************************************************/
54 /*****************************************************************************/
56 /*****************************************************************************/
60 /* Register variable management */
61 unsigned MaxRegSpace = 6; /* Maximum space available */
62 static unsigned RegOffs = 0; /* Offset into register space */
63 static const SymEntry** RegSyms = 0; /* The register variables */
64 static unsigned RegSymCount = 0; /* Number of register variables */
68 /*****************************************************************************/
70 /*****************************************************************************/
74 void InitRegVars (void)
75 /* Initialize register variable control data */
77 /* If the register space is zero, bail out */
78 if (MaxRegSpace == 0) {
82 /* The maximum number of register variables is equal to the register
83 * variable space available. So allocate one pointer per byte. This
84 * will usually waste some space but we don't need to dynamically
87 RegSyms = (const SymEntry**) xmalloc (MaxRegSpace * sizeof (RegSyms[0]));
88 RegOffs = MaxRegSpace;
93 void DoneRegVars (void)
94 /* Free the register variables */
98 RegOffs = MaxRegSpace;
104 static int AllocRegVar (const SymEntry* Sym, const type* tarray)
105 /* Allocate a register variable with the given amount of storage. If the
106 * allocation was successful, return the offset of the register variable in
107 * the register bank (zero page storage). If there is no register space left,
111 /* Maybe register variables are disabled... */
114 /* Get the size of the variable */
115 unsigned Size = CheckedSizeOf (tarray);
117 /* Do we have space left? */
118 if (RegOffs >= Size) {
120 /* Space left. We allocate the variables from high to low addresses,
121 * so the adressing is compatible with the saved values on stack.
122 * This allows shorter code when saving/restoring the variables.
125 RegSyms [RegSymCount++] = Sym;
130 /* No space left or no allocation */
136 static unsigned ParseAutoDecl (Declaration* Decl, unsigned Size, unsigned* SC)
137 /* Parse the declaration of an auto variable. The function returns the symbol
138 * data, which is the offset for variables on the stack, and the label for
145 /* Check if this is a variable on the stack or in static memory */
146 if (StaticLocals == 0) {
148 /* Change SC in case it was register */
149 *SC = (*SC & ~SC_REGISTER) | SC_AUTO;
150 if (CurTok.Tok == TOK_ASSIGN) {
154 /* Allocate previously reserved local space */
155 F_AllocLocalSpace (CurrentFunc);
160 /* Setup the type flags for the assignment */
161 Flags = (Size == 1)? CF_FORCECHAR : CF_NONE;
163 /* Get the expression into the primary */
164 if (evalexpr (Flags, hie1, &lval) == 0) {
165 /* Constant expression. Adjust the types */
166 assignadjust (Decl->Type, &lval);
169 /* Expression is not constant and in the primary */
170 assignadjust (Decl->Type, &lval);
174 g_push (Flags | TypeOf (Decl->Type), lval.ConstVal);
176 /* Mark the variable as referenced */
179 /* Variable is located at the current SP */
183 /* Non-initialized local variable. Just keep track of
186 SymData = F_ReserveLocalSpace (CurrentFunc, Size);
191 /* Static local variables. */
192 *SC = (*SC & ~(SC_REGISTER | SC_AUTO)) | SC_STATIC;
194 /* Put them into the BSS */
197 /* Define the variable label */
198 SymData = GetLocalLabel ();
199 g_defdatalabel (SymData);
201 /* Reserve space for the data */
204 /* Allow assignments */
205 if (CurTok.Tok == TOK_ASSIGN) {
212 /* Setup the type flags for the assignment */
213 Flags = (Size == 1)? CF_FORCECHAR : CF_NONE;
215 /* Get the expression into the primary */
216 if (evalexpr (Flags, hie1, &lval) == 0) {
217 /* Constant expression. Adjust the types */
218 assignadjust (Decl->Type, &lval);
220 /* Load it into the primary */
221 exprhs (Flags, 0, &lval);
223 /* Expression is not constant and in the primary */
224 assignadjust (Decl->Type, &lval);
227 /* Store the value into the variable */
228 g_putstatic (Flags | TypeOf (Decl->Type), SymData, 0);
230 /* Mark the variable as referenced */
235 /* Return the symbol data */
241 static unsigned ParseStaticDecl (Declaration* Decl, unsigned Size, unsigned* SC)
242 /* Parse the declaration of a static variable. The function returns the symbol
243 * data, which is the asm label of the variable.
249 if (CurTok.Tok == TOK_ASSIGN) {
251 /* Initialization ahead, switch to data segment */
252 if (IsQualConst (Decl->Type)) {
258 /* Define the variable label */
259 SymData = GetLocalLabel ();
260 g_defdatalabel (SymData);
265 /* Allow initialization of static vars */
266 ParseInit (Decl->Type);
268 /* If the previous size has been unknown, it must be known now */
270 Size = SizeOf (Decl->Type);
273 /* Mark the variable as referenced */
278 /* Uninitialized data, use BSS segment */
281 /* Define the variable label */
282 SymData = GetLocalLabel ();
283 g_defdatalabel (SymData);
285 /* Reserve space for the data */
290 /* Return the symbol data */
296 static void ParseOneDecl (const DeclSpec* Spec)
297 /* Parse one variable declaration */
299 unsigned SC; /* Storage class for symbol */
300 unsigned Size; /* Size of the data object */
301 unsigned SymData = 0; /* Symbol data (offset, label name, ...) */
302 Declaration Decl; /* Declaration data structure */
304 /* Remember the storage class for the new symbol */
305 SC = Spec->StorageClass;
307 /* Read the declaration */
308 ParseDecl (Spec, &Decl, DM_NEED_IDENT);
310 /* Set the correct storage class for functions */
311 if (IsTypeFunc (Decl.Type)) {
312 /* Function prototypes are always external */
313 if ((SC & SC_EXTERN) == 0) {
314 Warning ("Function must be extern");
316 SC |= SC_FUNC | SC_EXTERN;
320 /* If we don't have a name, this was flagged as an error earlier.
321 * To avoid problems later, use an anonymous name here.
323 if (Decl.Ident[0] == '\0') {
324 AnonName (Decl.Ident, "param");
327 /* Handle anything that needs storage (no functions, no typdefs) */
328 if ((SC & SC_FUNC) != SC_FUNC && (SC & SC_TYPEDEF) != SC_TYPEDEF) {
330 /* Get the size of the variable */
331 Size = SizeOf (Decl.Type);
334 if (SC & (SC_AUTO | SC_REGISTER)) {
337 SymData = ParseAutoDecl (&Decl, Size, &SC);
339 } else if ((SC & SC_STATIC) == SC_STATIC) {
341 /* Static variable */
342 SymData = ParseStaticDecl (&Decl, Size, &SC);
346 /* Cannot allocate a variable of zero size */
348 Error ("Variable `%s' has unknown size", Decl.Ident);
353 /* If the symbol is not marked as external, it will be defined */
354 if ((SC & SC_EXTERN) == 0) {
358 /* Add the symbol to the symbol table */
359 AddLocalSym (Decl.Ident, Decl.Type, SC, SymData);
364 void DeclareLocals (void)
365 /* Declare local variables and types. */
367 /* Remember the current stack pointer */
368 int InitialStack = oursp;
370 /* Loop until we don't find any more variables */
373 /* Check variable declarations. We need to distinguish between a
374 * default int type and the end of variable declarations. So we
375 * will do the following: If there is no explicit storage class
376 * specifier *and* no explicit type given, it is assume that we
377 * have reached the end of declarations.
380 ParseDeclSpec (&Spec, SC_AUTO, T_INT);
381 if ((Spec.Flags & DS_DEF_STORAGE) != 0 && (Spec.Flags & DS_DEF_TYPE) != 0) {
385 /* Accept type only declarations */
386 if (CurTok.Tok == TOK_SEMI) {
387 /* Type declaration only */
388 CheckEmptyDecl (&Spec);
393 /* Parse a comma separated variable list */
396 /* Parse one declaration */
397 ParseOneDecl (&Spec);
399 /* Check if there is more */
400 if (CurTok.Tok == TOK_COMMA) {
409 /* A semicolon must follow */
413 /* Be sure to allocate any reserved space for locals */
414 F_AllocLocalSpace (CurrentFunc);
416 /* In case we've allocated local variables in this block, emit a call to
417 * the stack checking routine if stack checks are enabled.
419 if (CheckStack && InitialStack != oursp) {
426 void RestoreRegVars (int HaveResult)
427 /* Restore the register variables for the local function if there are any.
428 * The parameter tells us if there is a return value in ax, in that case,
429 * the accumulator must be saved across the restore.
435 /* If we don't have register variables in this function, bail out early */
436 if (RegSymCount == 0) {
440 /* Save the accumulator if needed */
441 if (!F_HasVoidReturn (CurrentFunc) && HaveResult) {
442 g_save (CF_CHAR | CF_FORCECHAR);
445 /* Walk through all variables. If there are several variables in a row
446 * (that is, with increasing stack offset), restore them in one chunk.
449 while (I < RegSymCount) {
451 /* Check for more than one variable */
452 const SymEntry* Sym = RegSyms[I];
454 Bytes = CheckedSizeOf (Sym->Type);
457 while (J < RegSymCount) {
459 /* Get the next symbol */
460 const SymEntry* NextSym = RegSyms [J];
463 int Size = CheckedSizeOf (NextSym->Type);
465 /* Adjacent variable? */
466 if (NextSym->V.Offs + Size != Offs) {
471 /* Adjacent variable */
478 /* Restore the memory range */
479 g_restore_regvars (Offs, Sym->V.Offs, Bytes);
485 /* Restore the accumulator if needed */
486 if (!F_HasVoidReturn (CurrentFunc) && HaveResult) {
487 g_restore (CF_CHAR | CF_FORCECHAR);