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"
51 /*****************************************************************************/
53 /*****************************************************************************/
57 /* Register variable management */
58 unsigned MaxRegSpace = 6; /* Maximum space available */
59 static unsigned RegOffs = 0; /* Offset into register space */
60 static const SymEntry** RegSyms = 0; /* The register variables */
61 static unsigned RegSymCount = 0; /* Number of register variables */
65 /*****************************************************************************/
67 /*****************************************************************************/
71 void InitRegVars (void)
72 /* Initialize register variable control data */
74 /* If the register space is zero, bail out */
75 if (MaxRegSpace == 0) {
79 /* The maximum number of register variables is equal to the register
80 * variable space available. So allocate one pointer per byte. This
81 * will usually waste some space but we don't need to dynamically
84 RegSyms = xmalloc (MaxRegSpace * sizeof (RegSyms[0]));
85 RegOffs = MaxRegSpace;
90 void DoneRegVars (void)
91 /* Free the register variables */
95 RegOffs = MaxRegSpace;
101 static int AllocRegVar (const SymEntry* Sym, const type* tarray)
102 /* Allocate a register variable with the given amount of storage. If the
103 * allocation was successful, return the offset of the register variable in
104 * the register bank (zero page storage). If there is no register space left,
108 /* Maybe register variables are disabled... */
111 /* Get the size of the variable */
112 unsigned Size = SizeOf (tarray);
114 /* Do we have space left? */
115 if (RegOffs >= Size) {
117 /* Space left. We allocate the variables from high to low addresses,
118 * so the adressing is compatible with the saved values on stack.
119 * This allows shorter code when saving/restoring the variables.
122 RegSyms [RegSymCount++] = Sym;
127 /* No space left or no allocation */
133 static void ParseOneDecl (const DeclSpec* Spec)
134 /* Parse one variable declaration */
136 int Size; /* Size of an auto variable */
137 int SC; /* Storage class for symbol */
138 int SymData = 0; /* Symbol data (offset, label name, ...) */
139 unsigned flags = 0; /* Code generator flags */
140 Declaration Decl; /* Declaration data structure */
142 /* Remember the storage class for the new symbol */
143 SC = Spec->StorageClass;
145 /* Read the declaration */
146 ParseDecl (Spec, &Decl, DM_NEED_IDENT);
148 /* Set the correct storage class for functions */
149 if (IsFunc (Decl.Type)) {
150 /* Function prototypes are always external */
151 if ((SC & SC_EXTERN) == 0) {
152 Warning (WARN_FUNC_MUST_BE_EXTERN);
154 SC |= SC_FUNC | SC_EXTERN;
158 /* If we don't have a name, this was flagged as an error earlier.
159 * To avoid problems later, use an anonymous name here.
161 if (Decl.Ident[0] == '\0') {
162 AnonName (Decl.Ident, "param");
165 /* Handle anything that needs storage (no functions, no typdefs) */
166 if ((SC & SC_FUNC) != SC_FUNC && (SC & SC_TYPEDEF) != SC_TYPEDEF) {
168 /* Get the size of the variable */
169 Size = SizeOf (Decl.Type);
171 if (SC & (SC_AUTO | SC_REGISTER)) {
174 if (StaticLocals == 0) {
176 /* Change SC in case it was register */
177 SC = (SC & ~SC_REGISTER) | SC_AUTO;
178 if (curtok == TOK_ASSIGN) {
182 /* Allocate previously reserved local space */
183 AllocLocalSpace (CurrentFunc);
188 /* Setup the type flags for the assignment */
189 flags = Size == 1? CF_FORCECHAR : CF_NONE;
191 /* Get the expression into the primary */
192 if (evalexpr (flags, hie1, &lval) == 0) {
193 /* Constant expression. Adjust the types */
194 assignadjust (Decl.Type, &lval);
197 /* Expression is not constant and in the primary */
198 assignadjust (Decl.Type, &lval);
202 g_push (flags | TypeOf (Decl.Type), lval.e_const);
204 /* Mark the variable as referenced */
207 /* Variable is located at the current SP */
211 /* Non-initialized local variable. Just keep track of
214 SymData = ReserveLocalSpace (CurrentFunc, Size);
219 /* Static local variables. */
220 SC = (SC & ~(SC_REGISTER | SC_AUTO)) | SC_STATIC;
222 /* Put them into the BSS */
225 /* Define the variable label */
226 SymData = GetLabel ();
227 g_defloclabel (SymData);
229 /* Reserve space for the data */
232 /* Allow assignments */
233 if (curtok == TOK_ASSIGN) {
237 /* Switch to the code segment. */
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 */
271 /* Define the variable label */
272 SymData = GetLabel ();
273 g_defloclabel (SymData);
278 /* Allow initialization of static vars */
279 ParseInit (Decl.Type);
281 /* Mark the variable as referenced */
286 /* Uninitialized data, use BSS segment */
289 /* Define the variable label */
290 SymData = GetLabel ();
291 g_defloclabel (SymData);
293 /* Reserve space for the data */
301 /* If the symbol is not marked as external, it will be defined */
302 if ((SC & SC_EXTERN) == 0) {
306 /* Add the symbol to the symbol table */
307 AddLocalSym (Decl.Ident, Decl.Type, SC, SymData);
312 void DeclareLocals (void)
313 /* Declare local variables and types. */
315 /* Loop until we don't find any more variables */
318 /* Check variable declarations. We need to distinguish between a
319 * default int type and the end of variable declarations. So we
320 * will do the following: If there is no explicit storage class
321 * specifier *and* no explicit type given, it is assume that we
322 * have reached the end of declarations.
325 ParseDeclSpec (&Spec, SC_AUTO, T_INT);
326 if ((Spec.Flags & DS_DEF_STORAGE) != 0 && (Spec.Flags & DS_DEF_TYPE) != 0) {
330 /* Accept type only declarations */
331 if (curtok == TOK_SEMI) {
332 /* Type declaration only */
333 CheckEmptyDecl (&Spec);
338 /* Parse a comma separated variable list */
341 /* Parse one declaration */
342 ParseOneDecl (&Spec);
344 /* Check if there is more */
345 if (curtok == TOK_COMMA) {
354 /* A semicolon must follow */
358 /* Be sure to allocate any reserved space for locals */
359 AllocLocalSpace (CurrentFunc);
361 /* In case we switched away from code segment, switch back now */
367 void RestoreRegVars (int HaveResult)
368 /* Restore the register variables for the local function if there are any.
369 * The parameter tells us if there is a return value in ax, in that case,
370 * the accumulator must be saved across the restore.
376 /* If we don't have register variables in this function, bail out early */
377 if (RegSymCount == 0) {
381 /* Save the accumulator if needed */
382 if (!HasVoidReturn (CurrentFunc) && HaveResult) {
383 g_save (CF_CHAR | CF_FORCECHAR);
386 /* Walk through all variables. If there are several variables in a row
387 * (that is, with increasing stack offset), restore them in one chunk.
390 while (I < RegSymCount) {
392 /* Check for more than one variable */
393 const SymEntry* Sym = RegSyms[I];
395 Bytes = SizeOf (Sym->Type);
398 while (J < RegSymCount) {
400 /* Get the next symbol */
401 const SymEntry* NextSym = RegSyms [J];
404 int Size = SizeOf (NextSym->Type);
406 /* Adjacent variable? */
407 if (NextSym->V.Offs + Size != Offs) {
412 /* Adjacent variable */
419 /* Restore the memory range */
420 g_restore_regvars (Offs, Sym->V.Offs, Bytes);
426 /* Restore the accumulator if needed */
427 if (!HasVoidReturn (CurrentFunc) && HaveResult) {
428 g_restore (CF_CHAR | CF_FORCECHAR);