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 /*****************************************************************************/
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 == TOK_SEMI) {
159 /* Type declaration only */
160 CheckEmptyDecl (&Spec);
165 /* Parse a comma separated variable list */
170 /* Remember the storage class for the new symbol */
171 SymbolSC = Spec.StorageClass;
173 /* Read the declaration */
174 ParseDecl (&Spec, &Decl, DM_NEED_IDENT);
176 /* If we don't have a name, this was flagged as an error earlier.
177 * To avoid problems later, use an anonymous name here.
179 if (Decl.Ident[0] == '\0') {
180 AnonName (Decl.Ident, "param");
183 if (!IsFunc (Decl.Type) && (SymbolSC & SC_TYPEDEF) != SC_TYPEDEF) {
185 /* Get the size of the variable */
186 Size = SizeOf (Decl.Type);
189 /* Check the storage class */
190 if ((SymbolSC & SC_REGISTER) && (Reg = AllocRegVar (psym, tarray)) >= 0) {
192 /* We will store the current value of the register onto the
193 * stack, thus making functions with register variables
194 * reentrant. If we have pending auto variables, emit them
202 /* Remember the register bank offset */
205 /* Save the current register value onto the stack */
206 g_save_regvars (Reg, Size);
208 /* Allow variable initialization */
209 if (curtok == TOK_ASSIGN) {
216 /* Get the expression into the primary */
219 /* Make type adjustments if needed */
220 assignadjust (tarray, &lval);
222 /* Setup the type flags for the assignment */
223 flags = TypeOf (tarray) | CF_REGVAR;
225 flags |= CF_FORCECHAR;
228 /* Store the value into the register */
229 g_putstatic (flags, Reg, 0);
231 /* Mark the variable as referenced */
236 /* Account for the stack space needed and remember the
237 * stack offset of the save area.
240 psym->h_lattr = offs;
242 } else if (SymbolSC & (SC_AUTO | SC_REGISTER)) {
244 if (SymbolSC & (SC_AUTO | SC_REGISTER)) {
247 if (LocalsAreStatic == 0) {
249 /* Change SC in case it was register */
250 SymbolSC = (SymbolSC & ~SC_REGISTER) | SC_AUTO;
251 if (curtok == TOK_ASSIGN) {
255 /* Switch to the code segment, allocate space for
256 * uninitialized variables.
266 /* Setup the type flags for the assignment */
267 flags = Size == 1? CF_FORCECHAR : CF_NONE;
269 /* Get the expression into the primary */
270 if (evalexpr (flags, hie1, &lval) == 0) {
271 /* Constant expression. Adjust the types */
272 assignadjust (Decl.Type, &lval);
275 /* Expression is not constant and in the primary */
276 assignadjust (Decl.Type, &lval);
280 g_push (flags | TypeOf (Decl.Type), lval.e_const);
282 /* Mark the variable as referenced */
286 /* Non-initialized local variable. Just keep track of
292 /* Allocate space on the stack, assign the offset */
298 /* Static local variables. */
299 SymbolSC = (SymbolSC & ~(SC_REGISTER | SC_AUTO)) | SC_STATIC;
301 /* Put them into the BSS */
304 /* Define the variable label */
305 g_defloclabel (ldata = GetLabel ());
307 /* Reserve space for the data */
310 /* Allow assignments */
311 if (curtok == TOK_ASSIGN) {
315 /* Switch to the code segment. */
321 /* Get the expression into the primary */
324 /* Make type adjustments if needed */
325 assignadjust (Decl.Type, &lval);
327 /* Setup the type flags for the assignment */
328 flags = TypeOf (Decl.Type);
330 flags |= CF_FORCECHAR;
333 /* Store the value into the variable */
334 g_putstatic (flags, ldata, 0);
336 /* Mark the variable as referenced */
341 } else if ((SymbolSC & SC_STATIC) == SC_STATIC) {
344 if (curtok == TOK_ASSIGN) {
346 /* Initialization ahead, switch to data segment */
349 /* Define the variable label */
350 g_defloclabel (ldata = GetLabel ());
355 /* Allow initialization of static vars */
356 ParseInit (Decl.Type);
358 /* Mark the variable as referenced */
363 /* Uninitialized data, use BSS segment */
366 /* Define the variable label */
367 g_defloclabel (ldata = GetLabel ());
369 /* Reserve space for the data */
377 /* If the symbol is not marked as external, it will be defined */
378 if ((SymbolSC & SC_EXTERN) == 0) {
382 /* Add the symbol to the symbol table */
383 AddLocalSym (Decl.Ident, Decl.Type, SymbolSC, ldata);
385 if (curtok != TOK_COMMA) {
390 if (curtok == TOK_SEMI) {
395 /* In case we switched away from code segment, switch back now */
398 /* Create space for locals */
405 void RestoreRegVars (int HaveResult)
406 /* Restore the register variables for the local function if there are any.
407 * The parameter tells us if there is a return value in ax, in that case,
408 * the accumulator must be saved across the restore.
414 /* If we don't have register variables in this function, bail out early */
415 if (RegSymCount == 0) {
419 /* Save the accumulator if needed */
420 if (!HasVoidReturn (CurrentFunc) && HaveResult) {
421 g_save (CF_CHAR | CF_FORCECHAR);
424 /* Walk through all variables. If there are several variables in a row
425 * (that is, with increasing stack offset), restore them in one chunk.
428 while (I < RegSymCount) {
430 /* Check for more than one variable */
431 const SymEntry* Sym = RegSyms[I];
433 Bytes = SizeOf (Sym->Type);
436 while (J < RegSymCount) {
438 /* Get the next symbol */
439 const SymEntry* NextSym = RegSyms [J];
442 int Size = SizeOf (NextSym->Type);
444 /* Adjacent variable? */
445 if (NextSym->V.Offs + Size != Offs) {
450 /* Adjacent variable */
457 /* Restore the memory range */
458 g_restore_regvars (Offs, Sym->V.Offs, Bytes);
464 /* Restore the accumulator if needed */
465 if (!HasVoidReturn (CurrentFunc) && HaveResult) {
466 g_restore (CF_CHAR | CF_FORCECHAR);