1 /*****************************************************************************/
5 /* Local variable handling for the cc65 C compiler */
9 /* (C) 2000-2001 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 = SizeOf (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 void ParseOneDecl (const DeclSpec* Spec)
137 /* Parse one variable declaration */
139 int Size; /* Size of an auto variable */
140 int SC; /* Storage class for symbol */
141 int SymData = 0; /* Symbol data (offset, label name, ...) */
142 unsigned flags = 0; /* Code generator flags */
143 Declaration Decl; /* Declaration data structure */
145 /* Remember the storage class for the new symbol */
146 SC = Spec->StorageClass;
148 /* Read the declaration */
149 ParseDecl (Spec, &Decl, DM_NEED_IDENT);
151 /* Set the correct storage class for functions */
152 if (IsTypeFunc (Decl.Type)) {
153 /* Function prototypes are always external */
154 if ((SC & SC_EXTERN) == 0) {
155 Warning ("Function must be extern");
157 SC |= SC_FUNC | SC_EXTERN;
161 /* If we don't have a name, this was flagged as an error earlier.
162 * To avoid problems later, use an anonymous name here.
164 if (Decl.Ident[0] == '\0') {
165 AnonName (Decl.Ident, "param");
168 /* Handle anything that needs storage (no functions, no typdefs) */
169 if ((SC & SC_FUNC) != SC_FUNC && (SC & SC_TYPEDEF) != SC_TYPEDEF) {
171 /* Get the size of the variable */
172 Size = SizeOf (Decl.Type);
174 if (SC & (SC_AUTO | SC_REGISTER)) {
177 if (StaticLocals == 0) {
179 /* Change SC in case it was register */
180 SC = (SC & ~SC_REGISTER) | SC_AUTO;
181 if (curtok == TOK_ASSIGN) {
185 /* Allocate previously reserved local space */
186 AllocLocalSpace (CurrentFunc);
191 /* Setup the type flags for the assignment */
192 flags = Size == 1? CF_FORCECHAR : CF_NONE;
194 /* Get the expression into the primary */
195 if (evalexpr (flags, hie1, &lval) == 0) {
196 /* Constant expression. Adjust the types */
197 assignadjust (Decl.Type, &lval);
200 /* Expression is not constant and in the primary */
201 assignadjust (Decl.Type, &lval);
205 g_push (flags | TypeOf (Decl.Type), lval.e_const);
207 /* Mark the variable as referenced */
210 /* Variable is located at the current SP */
214 /* Non-initialized local variable. Just keep track of
217 SymData = ReserveLocalSpace (CurrentFunc, Size);
222 /* Static local variables. */
223 SC = (SC & ~(SC_REGISTER | SC_AUTO)) | SC_STATIC;
225 /* Put them into the BSS */
228 /* Define the variable label */
229 SymData = GetLocalLabel ();
230 g_defdatalabel (SymData);
232 /* Reserve space for the data */
235 /* Allow assignments */
236 if (curtok == TOK_ASSIGN) {
243 /* Get the expression into the primary */
246 /* Make type adjustments if needed */
247 assignadjust (Decl.Type, &lval);
249 /* Setup the type flags for the assignment */
250 flags = TypeOf (Decl.Type);
252 flags |= CF_FORCECHAR;
255 /* Store the value into the variable */
256 g_putstatic (flags, SymData, 0);
258 /* Mark the variable as referenced */
263 } else if ((SC & SC_STATIC) == SC_STATIC) {
266 if (curtok == TOK_ASSIGN) {
268 /* Initialization ahead, switch to data segment */
269 if (IsQualConst (Decl.Type)) {
275 /* Define the variable label */
276 SymData = GetLocalLabel ();
277 g_defdatalabel (SymData);
282 /* Allow initialization of static vars */
283 ParseInit (Decl.Type);
285 /* Mark the variable as referenced */
290 /* Uninitialized data, use BSS segment */
293 /* Define the variable label */
294 SymData = GetLocalLabel ();
295 g_defdatalabel (SymData);
297 /* Reserve space for the data */
305 /* If the symbol is not marked as external, it will be defined */
306 if ((SC & SC_EXTERN) == 0) {
310 /* Add the symbol to the symbol table */
311 AddLocalSym (Decl.Ident, Decl.Type, SC, SymData);
316 void DeclareLocals (void)
317 /* Declare local variables and types. */
319 /* Remember the current stack pointer */
320 int InitialStack = oursp;
322 /* Loop until we don't find any more variables */
325 /* Check variable declarations. We need to distinguish between a
326 * default int type and the end of variable declarations. So we
327 * will do the following: If there is no explicit storage class
328 * specifier *and* no explicit type given, it is assume that we
329 * have reached the end of declarations.
332 ParseDeclSpec (&Spec, SC_AUTO, T_INT);
333 if ((Spec.Flags & DS_DEF_STORAGE) != 0 && (Spec.Flags & DS_DEF_TYPE) != 0) {
337 /* Accept type only declarations */
338 if (curtok == TOK_SEMI) {
339 /* Type declaration only */
340 CheckEmptyDecl (&Spec);
345 /* Parse a comma separated variable list */
348 /* Parse one declaration */
349 ParseOneDecl (&Spec);
351 /* Check if there is more */
352 if (curtok == TOK_COMMA) {
361 /* A semicolon must follow */
365 /* Be sure to allocate any reserved space for locals */
366 AllocLocalSpace (CurrentFunc);
368 /* In case we've allocated local variables in this block, emit a call to
369 * the stack checking routine if stack checks are enabled.
371 if (CheckStack && InitialStack != oursp) {
378 void RestoreRegVars (int HaveResult)
379 /* Restore the register variables for the local function if there are any.
380 * The parameter tells us if there is a return value in ax, in that case,
381 * the accumulator must be saved across the restore.
387 /* If we don't have register variables in this function, bail out early */
388 if (RegSymCount == 0) {
392 /* Save the accumulator if needed */
393 if (!HasVoidReturn (CurrentFunc) && HaveResult) {
394 g_save (CF_CHAR | CF_FORCECHAR);
397 /* Walk through all variables. If there are several variables in a row
398 * (that is, with increasing stack offset), restore them in one chunk.
401 while (I < RegSymCount) {
403 /* Check for more than one variable */
404 const SymEntry* Sym = RegSyms[I];
406 Bytes = SizeOf (Sym->Type);
409 while (J < RegSymCount) {
411 /* Get the next symbol */
412 const SymEntry* NextSym = RegSyms [J];
415 int Size = SizeOf (NextSym->Type);
417 /* Adjacent variable? */
418 if (NextSym->V.Offs + Size != Offs) {
423 /* Adjacent variable */
430 /* Restore the memory range */
431 g_restore_regvars (Offs, Sym->V.Offs, Bytes);
437 /* Restore the accumulator if needed */
438 if (!HasVoidReturn (CurrentFunc) && HaveResult) {
439 g_restore (CF_CHAR | CF_FORCECHAR);