1 /*****************************************************************************/
5 /* Local variable handling for the cc65 C compiler */
9 /* (C) 2000 Ullrich von Bassewitz */
11 /* D-70597 Stuttgart */
12 /* EMail: uz@musoftware.de */
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 /*****************************************************************************/
36 #include "../common/xmalloc.h"
50 /*****************************************************************************/
52 /*****************************************************************************/
56 /* Register variable management */
57 unsigned MaxRegSpace = 6; /* Maximum space available */
58 static unsigned RegOffs = 0; /* Offset into register space */
59 static const SymEntry** RegSyms = 0; /* The register variables */
60 static unsigned RegSymCount = 0; /* Number of register variables */
64 /*****************************************************************************/
66 /*****************************************************************************/
70 void InitRegVars (void)
71 /* Initialize register variable control data */
73 /* If the register space is zero, bail out */
74 if (MaxRegSpace == 0) {
78 /* The maximum number of register variables is equal to the register
79 * variable space available. So allocate one pointer per byte. This
80 * will usually waste some space but we don't need to dynamically
83 RegSyms = xmalloc (MaxRegSpace * sizeof (RegSyms[0]));
84 RegOffs = MaxRegSpace;
89 void DoneRegVars (void)
90 /* Free the register variables */
94 RegOffs = MaxRegSpace;
100 static int AllocRegVar (const SymEntry* Sym, const type* tarray)
101 /* Allocate a register variable with the given amount of storage. If the
102 * allocation was successful, return the offset of the register variable in
103 * the register bank (zero page storage). If there is no register space left,
107 /* Maybe register variables are disabled... */
110 /* Get the size of the variable */
111 unsigned Size = SizeOf (tarray);
113 /* Do we have space left? */
114 if (RegOffs >= Size) {
116 /* Space left. We allocate the variables from high to low addresses,
117 * so the adressing is compatible with the saved values on stack.
118 * This allows shorter code when saving/restoring the variables.
121 RegSyms [RegSymCount++] = Sym;
126 /* No space left or no allocation */
132 void DeclareLocals (void)
133 /* Declare local variables and types. */
135 int offs = oursp; /* Current stack offset for variable */
136 int AutoSpace = 0; /* Unallocated space on the stack */
137 int Size; /* Size of an auto variable */
138 int Reg; /* Register variable offset */
139 unsigned flags = 0; /* Code generator flags */
140 int SymbolSC; /* Storage class for symbol */
141 int ldata = 0; /* Local symbol data temp storage */
143 /* Loop until we don't find any more variables */
146 /* Check variable declarations. We need to distinguish between a
147 * default int type and the end of variable declarations. So we
148 * will do the following: If there is no explicit storage class
149 * specifier *and* no explicit type given, it is assume that we
150 * have reached the end of declarations.
153 ParseDeclSpec (&Spec, SC_AUTO, T_INT);
154 if ((Spec.Flags & DS_DEF_STORAGE) != 0 && (Spec.Flags & DS_DEF_TYPE) != 0) {
158 /* Accept type only declarations */
159 if (curtok == TOK_SEMI) {
160 /* Type declaration only */
161 CheckEmptyDecl (&Spec);
166 /* Parse a comma separated variable list */
171 /* Remember the storage class for the new symbol */
172 SymbolSC = Spec.StorageClass;
174 /* Read the declaration */
175 ParseDecl (&Spec, &Decl, DM_NEED_IDENT);
177 /* If we don't have a name, this was flagged as an error earlier.
178 * To avoid problems later, use an anonymous name here.
180 if (Decl.Ident[0] == '\0') {
181 AnonName (Decl.Ident, "param");
184 if (!IsFunc (Decl.Type) && (SymbolSC & SC_TYPEDEF) != SC_TYPEDEF) {
186 /* Get the size of the variable */
187 Size = SizeOf (Decl.Type);
190 /* Check the storage class */
191 if ((SymbolSC & SC_REGISTER) && (Reg = AllocRegVar (psym, tarray)) >= 0) {
193 /* We will store the current value of the register onto the
194 * stack, thus making functions with register variables
195 * reentrant. If we have pending auto variables, emit them
203 /* Remember the register bank offset */
206 /* Save the current register value onto the stack */
207 g_save_regvars (Reg, Size);
209 /* Allow variable initialization */
210 if (curtok == TOK_ASSIGN) {
217 /* Get the expression into the primary */
220 /* Make type adjustments if needed */
221 assignadjust (tarray, &lval);
223 /* Setup the type flags for the assignment */
224 flags = TypeOf (tarray) | CF_REGVAR;
226 flags |= CF_FORCECHAR;
229 /* Store the value into the register */
230 g_putstatic (flags, Reg, 0);
232 /* Mark the variable as referenced */
237 /* Account for the stack space needed and remember the
238 * stack offset of the save area.
241 psym->h_lattr = offs;
243 } else if (SymbolSC & (SC_AUTO | SC_REGISTER)) {
245 if (SymbolSC & (SC_AUTO | SC_REGISTER)) {
248 if (StaticLocals == 0) {
250 /* Change SC in case it was register */
251 SymbolSC = (SymbolSC & ~SC_REGISTER) | SC_AUTO;
252 if (curtok == TOK_ASSIGN) {
256 /* Switch to the code segment, allocate space for
257 * uninitialized variables.
267 /* Setup the type flags for the assignment */
268 flags = Size == 1? CF_FORCECHAR : CF_NONE;
270 /* Get the expression into the primary */
271 if (evalexpr (flags, hie1, &lval) == 0) {
272 /* Constant expression. Adjust the types */
273 assignadjust (Decl.Type, &lval);
276 /* Expression is not constant and in the primary */
277 assignadjust (Decl.Type, &lval);
281 g_push (flags | TypeOf (Decl.Type), lval.e_const);
283 /* Mark the variable as referenced */
287 /* Non-initialized local variable. Just keep track of
293 /* Allocate space on the stack, assign the offset */
299 /* Static local variables. */
300 SymbolSC = (SymbolSC & ~(SC_REGISTER | SC_AUTO)) | SC_STATIC;
302 /* Put them into the BSS */
305 /* Define the variable label */
306 g_defloclabel (ldata = GetLabel ());
308 /* Reserve space for the data */
311 /* Allow assignments */
312 if (curtok == TOK_ASSIGN) {
316 /* Switch to the code segment. */
322 /* Get the expression into the primary */
325 /* Make type adjustments if needed */
326 assignadjust (Decl.Type, &lval);
328 /* Setup the type flags for the assignment */
329 flags = TypeOf (Decl.Type);
331 flags |= CF_FORCECHAR;
334 /* Store the value into the variable */
335 g_putstatic (flags, ldata, 0);
337 /* Mark the variable as referenced */
342 } else if ((SymbolSC & SC_STATIC) == SC_STATIC) {
345 if (curtok == TOK_ASSIGN) {
347 /* Initialization ahead, switch to data segment */
350 /* Define the variable label */
351 g_defloclabel (ldata = GetLabel ());
356 /* Allow initialization of static vars */
357 ParseInit (Decl.Type);
359 /* Mark the variable as referenced */
364 /* Uninitialized data, use BSS segment */
367 /* Define the variable label */
368 g_defloclabel (ldata = GetLabel ());
370 /* Reserve space for the data */
378 /* If the symbol is not marked as external, it will be defined */
379 if ((SymbolSC & SC_EXTERN) == 0) {
383 /* Add the symbol to the symbol table */
384 AddLocalSym (Decl.Ident, Decl.Type, SymbolSC, ldata);
386 if (curtok != TOK_COMMA) {
391 if (curtok == TOK_SEMI) {
396 /* In case we switched away from code segment, switch back now */
399 /* Create space for locals */
406 void RestoreRegVars (int HaveResult)
407 /* Restore the register variables for the local function if there are any.
408 * The parameter tells us if there is a return value in ax, in that case,
409 * the accumulator must be saved across the restore.
415 /* If we don't have register variables in this function, bail out early */
416 if (RegSymCount == 0) {
420 /* Save the accumulator if needed */
421 if (!HasVoidReturn (CurrentFunc) && HaveResult) {
422 g_save (CF_CHAR | CF_FORCECHAR);
425 /* Walk through all variables. If there are several variables in a row
426 * (that is, with increasing stack offset), restore them in one chunk.
429 while (I < RegSymCount) {
431 /* Check for more than one variable */
432 const SymEntry* Sym = RegSyms[I];
434 Bytes = SizeOf (Sym->Type);
437 while (J < RegSymCount) {
439 /* Get the next symbol */
440 const SymEntry* NextSym = RegSyms [J];
443 int Size = SizeOf (NextSym->Type);
445 /* Adjacent variable? */
446 if (NextSym->V.Offs + Size != Offs) {
451 /* Adjacent variable */
458 /* Restore the memory range */
459 g_restore_regvars (Offs, Sym->V.Offs, Bytes);
465 /* Restore the accumulator if needed */
466 if (!HasVoidReturn (CurrentFunc) && HaveResult) {
467 g_restore (CF_CHAR | CF_FORCECHAR);