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 /*****************************************************************************/
41 #include "function.h" /* ## */
49 /*****************************************************************************/
51 /*****************************************************************************/
55 /* Register variable management */
56 unsigned MaxRegSpace = 6; /* Maximum space available */
57 static unsigned RegOffs = 0; /* Offset into register space */
58 static const SymEntry** RegSyms = 0; /* The register variables */
59 static unsigned RegSymCount = 0; /* Number of register variables */
63 /*****************************************************************************/
65 /*****************************************************************************/
69 void InitRegVars (void)
70 /* Initialize register variable control data */
72 /* If the register space is zero, bail out */
73 if (MaxRegSpace == 0) {
77 /* The maximum number of register variables is equal to the register
78 * variable space available. So allocate one pointer per byte. This
79 * will usually waste some space but we don't need to dynamically
82 RegSyms = xmalloc (MaxRegSpace * sizeof (RegSyms[0]));
83 RegOffs = MaxRegSpace;
88 void DoneRegVars (void)
89 /* Free the register variables */
93 RegOffs = MaxRegSpace;
99 static int AllocRegVar (const SymEntry* Sym, const type* tarray)
100 /* Allocate a register variable with the given amount of storage. If the
101 * allocation was successful, return the offset of the register variable in
102 * the register bank (zero page storage). If there is no register space left,
106 /* Maybe register variables are disabled... */
109 /* Get the size of the variable */
110 unsigned Size = SizeOf (tarray);
112 /* Do we have space left? */
113 if (RegOffs >= Size) {
115 /* Space left. We allocate the variables from high to low addresses,
116 * so the adressing is compatible with the saved values on stack.
117 * This allows shorter code when saving/restoring the variables.
120 RegSyms [RegSymCount++] = Sym;
125 /* No space left or no allocation */
131 void DeclareLocals (void)
132 /* Declare local variables and types. */
134 int offs = oursp; /* Current stack offset for variable */
135 int AutoSpace = 0; /* Unallocated space on the stack */
136 int Size; /* Size of an auto variable */
137 int Reg; /* Register variable offset */
138 unsigned flags = 0; /* Code generator flags */
139 int SymbolSC; /* Storage class for symbol */
140 int ldata = 0; /* Local symbol data temp storage */
142 /* Loop until we don't find any more variables */
145 /* Check variable declarations. We need to distinguish between a
146 * default int type and the end of variable declarations. So we
147 * will do the following: If there is no explicit storage class
148 * specifier *and* no explicit type given, it is assume that we
149 * have reached the end of declarations.
152 ParseDeclSpec (&Spec, SC_AUTO, T_INT);
153 if ((Spec.Flags & DS_DEF_STORAGE) != 0 && (Spec.Flags & DS_DEF_TYPE) != 0) {
157 /* Accept type only declarations */
158 if (curtok == SEMI) {
159 /* Type declaration only ### Check struct/union here */
164 /* Parse a comma separated variable list */
169 /* Remember the storage class for the new symbol */
170 SymbolSC = Spec.StorageClass;
172 /* Read the declaration */
173 ParseDecl (&Spec, &Decl, DM_NEED_IDENT);
175 /* If we don't have a name, this was flagged as an error earlier.
176 * To avoid problems later, use an anonymous name here.
178 if (Decl.Ident[0] == '\0') {
179 AnonName (Decl.Ident, "param");
182 if (!IsFunc (Decl.Type) && (SymbolSC & SC_TYPEDEF) != SC_TYPEDEF) {
184 /* Get the size of the variable */
185 Size = SizeOf (Decl.Type);
188 /* Check the storage class */
189 if ((SymbolSC & SC_REGISTER) && (Reg = AllocRegVar (psym, tarray)) >= 0) {
191 /* We will store the current value of the register onto the
192 * stack, thus making functions with register variables
193 * reentrant. If we have pending auto variables, emit them
201 /* Remember the register bank offset */
204 /* Save the current register value onto the stack */
205 g_save_regvars (Reg, Size);
207 /* Allow variable initialization */
208 if (curtok == ASGN) {
215 /* Get the expression into the primary */
218 /* Make type adjustments if needed */
219 assignadjust (tarray, &lval);
221 /* Setup the type flags for the assignment */
222 flags = TypeOf (tarray) | CF_REGVAR;
224 flags |= CF_FORCECHAR;
227 /* Store the value into the register */
228 g_putstatic (flags, Reg, 0);
230 /* Mark the variable as referenced */
235 /* Account for the stack space needed and remember the
236 * stack offset of the save area.
239 psym->h_lattr = offs;
241 } else if (SymbolSC & (SC_AUTO | SC_REGISTER)) {
243 if (SymbolSC & (SC_AUTO | SC_REGISTER)) {
246 if (LocalsAreStatic == 0) {
248 /* Change SC in case it was register */
249 SymbolSC = (SymbolSC & ~SC_REGISTER) | SC_AUTO;
250 if (curtok == ASGN) {
254 /* Switch to the code segment, allocate space for
255 * uninitialized variables.
265 /* Setup the type flags for the assignment */
266 flags = Size == 1? CF_FORCECHAR : CF_NONE;
268 /* Get the expression into the primary */
269 if (evalexpr (flags, hie1, &lval) == 0) {
270 /* Constant expression. Adjust the types */
271 assignadjust (Decl.Type, &lval);
274 /* Expression is not constant and in the primary */
275 assignadjust (Decl.Type, &lval);
279 g_push (flags | TypeOf (Decl.Type), lval.e_const);
281 /* Mark the variable as referenced */
285 /* Non-initialized local variable. Just keep track of
291 /* Allocate space on the stack, assign the offset */
297 /* Static local variables. */
298 SymbolSC = (SymbolSC & ~(SC_REGISTER | SC_AUTO)) | SC_STATIC;
300 /* Put them into the BSS */
303 /* Define the variable label */
304 g_defloclabel (ldata = GetLabel ());
306 /* Reserve space for the data */
309 /* Allow assignments */
310 if (curtok == ASGN) {
314 /* Switch to the code segment. */
320 /* Get the expression into the primary */
323 /* Make type adjustments if needed */
324 assignadjust (Decl.Type, &lval);
326 /* Setup the type flags for the assignment */
327 flags = TypeOf (Decl.Type);
329 flags |= CF_FORCECHAR;
332 /* Store the value into the variable */
333 g_putstatic (flags, ldata, 0);
335 /* Mark the variable as referenced */
340 } else if ((SymbolSC & SC_STATIC) == SC_STATIC) {
343 if (curtok == ASGN) {
345 /* Initialization ahead, switch to data segment */
348 /* Define the variable label */
349 g_defloclabel (ldata = GetLabel ());
354 /* Allow initialization of static vars */
355 ParseInit (Decl.Type);
357 /* Mark the variable as referenced */
362 /* Uninitialized data, use BSS segment */
365 /* Define the variable label */
366 g_defloclabel (ldata = GetLabel ());
368 /* Reserve space for the data */
376 /* If the symbol is not marked as external, it will be defined */
377 if ((SymbolSC & SC_EXTERN) == 0) {
381 /* Add the symbol to the symbol table */
382 AddLocalSym (Decl.Ident, Decl.Type, SymbolSC, ldata);
384 if (curtok != COMMA) {
389 if (curtok == SEMI) {
394 /* In case we switched away from code segment, switch back now */
397 /* Create space for locals */
404 void RestoreRegVars (int HaveResult)
405 /* Restore the register variables for the local function if there are any.
406 * The parameter tells us if there is a return value in ax, in that case,
407 * the accumulator must be saved across the restore.
413 /* If we don't have register variables in this function, bail out early */
414 if (RegSymCount == 0) {
418 /* Save the accumulator if needed */
419 if (!HasVoidReturn (CurrentFunc) && HaveResult) {
420 g_save (CF_CHAR | CF_FORCECHAR);
423 /* Walk through all variables. If there are several variables in a row
424 * (that is, with increasing stack offset), restore them in one chunk.
427 while (I < RegSymCount) {
429 /* Check for more than one variable */
430 const SymEntry* Sym = RegSyms[I];
432 Bytes = SizeOf (Sym->Type);
435 while (J < RegSymCount) {
437 /* Get the next symbol */
438 const SymEntry* NextSym = RegSyms [J];
441 int Size = SizeOf (NextSym->Type);
443 /* Adjacent variable? */
444 if (NextSym->V.Offs + Size != Offs) {
449 /* Adjacent variable */
456 /* Restore the memory range */
457 g_restore_regvars (Offs, Sym->V.Offs, Bytes);
463 /* Restore the accumulator if needed */
464 if (!HasVoidReturn (CurrentFunc) && HaveResult) {
465 g_restore (CF_CHAR | CF_FORCECHAR);