1 /*****************************************************************************/
5 /* Local variable handling for the cc65 C compiler */
9 /* (C) 2000-2004 Ullrich von Bassewitz */
10 /* Römerstrasse 52 */
11 /* D-70794 Filderstadt */
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 /*****************************************************************************/
58 /*****************************************************************************/
60 /*****************************************************************************/
64 static unsigned ParseRegisterDecl (Declaration* Decl, unsigned* SC, int Reg)
65 /* Parse the declaration of a register variable. The function returns the
66 * symbol data, which is the offset of the variable in the register bank.
71 /* Determine if this is a compound variable */
72 int IsCompound = IsClassStruct (Decl->Type) || IsTypeArray (Decl->Type);
74 /* Get the size of the variable */
75 unsigned Size = SizeOf (Decl->Type);
77 /* Save the current contents of the register variable on stack */
78 F_AllocLocalSpace (CurrentFunc);
79 g_save_regvars (Reg, Size);
81 /* Check for an optional initialization */
82 if (CurTok.Tok == TOK_ASSIGN) {
89 /* Special handling for compound types */
92 /* Switch to read only data */
95 /* Define a label for the initialization data */
96 InitLabel = GetLocalLabel ();
97 g_defdatalabel (InitLabel);
99 /* Parse the initialization generating a memory image of the
100 * data in the RODATA segment. The function does return the size
101 * of the initialization data, which may be greater than the
102 * actual size of the type, if the type is a structure with a
103 * flexible array member that has been initialized. Since we must
104 * know the size of the data in advance for register variables,
105 * we cannot allow that here.
107 if (ParseInit (Decl->Type) != Size) {
108 Error ("Cannot initialize flexible array members of storage class `register'");
111 /* Generate code to copy this data into the variable space */
112 g_initregister (InitLabel, Reg, Size);
116 /* Parse the expression */
119 /* Convert it to the target type */
120 TypeConversion (&Expr, Decl->Type);
122 /* Load the value into the primary */
123 LoadExpr (CF_NONE, &Expr);
125 /* Store the value into the variable */
126 g_putstatic (CF_REGVAR | TypeOf (Decl->Type), Reg, 0);
130 /* Mark the variable as referenced */
134 /* Cannot allocate a variable of zero size */
136 Error ("Variable `%s' has unknown size", Decl->Ident);
139 /* Return the symbol data */
145 static unsigned ParseAutoDecl (Declaration* Decl, unsigned* SC)
146 /* Parse the declaration of an auto variable. The function returns the symbol
147 * data, which is the offset for variables on the stack, and the label for
155 /* Determine if this is a compound variable */
156 int IsCompound = IsClassStruct (Decl->Type) || IsTypeArray (Decl->Type);
158 /* Get the size of the variable */
159 unsigned Size = SizeOf (Decl->Type);
161 /* Check if this is a variable on the stack or in static memory */
162 if (IS_Get (&StaticLocals) == 0) {
164 /* Check for an optional initialization */
165 if (CurTok.Tok == TOK_ASSIGN) {
172 /* Special handling for compound types */
175 /* Switch to read only data */
178 /* Define a label for the initialization data */
179 InitLabel = GetLocalLabel ();
180 g_defdatalabel (InitLabel);
182 /* Parse the initialization generating a memory image of the
183 * data in the RODATA segment. The function will return the
184 * actual size of the initialization data, which may be
185 * greater than the size of the variable if it is a struct
186 * that contains a flexible array member and we're not in
189 Size = ParseInit (Decl->Type);
191 /* Now reserve space for the variable on the stack */
192 SymData = F_ReserveLocalSpace (CurrentFunc, Size);
194 /* Next, allocate the space on the stack. This means that the
195 * variable is now located at offset 0 from the current sp.
197 F_AllocLocalSpace (CurrentFunc);
199 /* Generate code to copy the initialization data into the
202 g_initauto (InitLabel, Size);
206 /* Allocate previously reserved local space */
207 F_AllocLocalSpace (CurrentFunc);
209 /* Setup the type flags for the assignment */
210 Flags = (Size == SIZEOF_CHAR)? CF_FORCECHAR : CF_NONE;
212 /* Parse the expression */
215 /* Convert it to the target type */
216 TypeConversion (&Expr, Decl->Type);
218 /* If the value is not const, load it into the primary.
219 * Otherwise pass the information to the code generator.
221 if (ED_IsConstAbsInt (&Expr)) {
224 LoadExpr (CF_NONE, &Expr);
229 g_push (Flags | TypeOf (Decl->Type), Expr.IVal);
233 /* Mark the variable as referenced */
236 /* Variable is located at the current SP */
240 /* Non-initialized local variable. Just keep track of
243 SymData = F_ReserveLocalSpace (CurrentFunc, Size);
248 /* Static local variables. */
249 *SC = (*SC & ~SC_AUTO) | SC_STATIC;
251 /* Put them into the BSS */
254 /* Define the variable label */
255 SymData = GetLocalLabel ();
256 g_defdatalabel (SymData);
258 /* Reserve space for the data */
261 /* Allow assignments */
262 if (CurTok.Tok == TOK_ASSIGN) {
271 /* Switch to read only data */
274 /* Define a label for the initialization data */
275 InitLabel = GetLocalLabel ();
276 g_defdatalabel (InitLabel);
278 /* Parse the initialization generating a memory image of the
279 * data in the RODATA segment.
281 ParseInit (Decl->Type);
283 /* Generate code to copy this data into the variable space */
284 g_initstatic (InitLabel, SymData, Size);
288 /* Parse the expression */
291 /* Convert it to the target type */
292 TypeConversion (&Expr, Decl->Type);
294 /* Load the value into the primary */
295 LoadExpr (CF_NONE, &Expr);
297 /* Store the value into the variable */
298 g_putstatic (TypeOf (Decl->Type), SymData, 0);
301 /* Mark the variable as referenced */
306 /* Cannot allocate a variable of zero size */
308 Error ("Variable `%s' has unknown size", Decl->Ident);
311 /* Return the symbol data */
317 static unsigned ParseStaticDecl (Declaration* Decl, unsigned* SC)
318 /* Parse the declaration of a static variable. The function returns the symbol
319 * data, which is the asm label of the variable.
324 /* Get the size of the variable */
325 unsigned Size = SizeOf (Decl->Type);
328 if (CurTok.Tok == TOK_ASSIGN) {
330 /* Initialization ahead, switch to data segment */
331 if (IsQualConst (Decl->Type)) {
337 /* Define the variable label */
338 SymData = GetLocalLabel ();
339 g_defdatalabel (SymData);
344 /* Allow initialization of static vars */
345 ParseInit (Decl->Type);
347 /* If the previous size has been unknown, it must be known now */
349 Size = SizeOf (Decl->Type);
352 /* Mark the variable as referenced */
357 /* Uninitialized data, use BSS segment */
360 /* Define the variable label */
361 SymData = GetLocalLabel ();
362 g_defdatalabel (SymData);
364 /* Reserve space for the data */
369 /* Cannot allocate a variable of zero size */
371 Error ("Variable `%s' has unknown size", Decl->Ident);
374 /* Return the symbol data */
380 static void ParseOneDecl (const DeclSpec* Spec)
381 /* Parse one variable declaration */
383 unsigned SC; /* Storage class for symbol */
384 unsigned SymData = 0; /* Symbol data (offset, label name, ...) */
385 Declaration Decl; /* Declaration data structure */
388 /* Remember the storage class for the new symbol */
389 SC = Spec->StorageClass;
391 /* Read the declaration */
392 ParseDecl (Spec, &Decl, DM_NEED_IDENT);
394 /* Set the correct storage class for functions */
395 if (IsTypeFunc (Decl.Type)) {
396 /* Function prototypes are always external */
397 if ((SC & SC_EXTERN) == 0) {
398 Warning ("Function must be extern");
400 SC |= SC_FUNC | SC_EXTERN;
404 /* If we don't have a name, this was flagged as an error earlier.
405 * To avoid problems later, use an anonymous name here.
407 if (Decl.Ident[0] == '\0') {
408 AnonName (Decl.Ident, "param");
411 /* Handle anything that needs storage (no functions, no typdefs) */
412 if ((SC & SC_FUNC) != SC_FUNC && (SC & SC_TYPEDEF) != SC_TYPEDEF) {
414 /* If we have a register variable, try to allocate a register and
415 * convert the declaration to "auto" if this is not possible.
417 int Reg = 0; /* Initialize to avoid gcc complains */
418 if ((SC & SC_REGISTER) != 0 && (Reg = F_AllocRegVar (CurrentFunc, Decl.Type)) < 0) {
419 /* No space for this register variable, convert to auto */
420 SC = (SC & ~SC_REGISTER) | SC_AUTO;
423 /* Check the variable type */
424 if ((SC & SC_REGISTER) == SC_REGISTER) {
425 /* Register variable */
426 SymData = ParseRegisterDecl (&Decl, &SC, Reg);
427 } else if ((SC & SC_AUTO) == SC_AUTO) {
429 SymData = ParseAutoDecl (&Decl, &SC);
430 } else if ((SC & SC_STATIC) == SC_STATIC) {
431 /* Static variable */
432 SymData = ParseStaticDecl (&Decl, &SC);
434 Internal ("Invalid storage class in ParseOneDecl: %04X", SC);
437 /* If the standard was not set explicitly to C89, print a warning
438 * for variables with implicit int type.
440 if ((Spec->Flags & DS_DEF_TYPE) != 0 && IS_Get (&Standard) >= STD_C99) {
441 Warning ("Implicit `int' is an obsolete feature");
445 /* If the symbol is not marked as external, it will be defined now */
446 if ((SC & SC_EXTERN) == 0) {
450 /* Add the symbol to the symbol table */
451 AddLocalSym (Decl.Ident, Decl.Type, SC, SymData);
456 void DeclareLocals (void)
457 /* Declare local variables and types. */
459 /* Remember the current stack pointer */
460 int InitialStack = StackPtr;
462 /* Loop until we don't find any more variables */
465 /* Check variable declarations. We need to distinguish between a
466 * default int type and the end of variable declarations. So we
467 * will do the following: If there is no explicit storage class
468 * specifier *and* no explicit type given, *and* no type qualifiers
469 * have been read, it is assumed that we have reached the end of
473 ParseDeclSpec (&Spec, SC_AUTO, T_INT);
474 if ((Spec.Flags & DS_DEF_STORAGE) != 0 && /* No storage spec */
475 (Spec.Flags & DS_DEF_TYPE) != 0 && /* No type given */
476 GetQualifier (Spec.Type) == T_QUAL_NONE) { /* No type qualifier */
480 /* Accept type only declarations */
481 if (CurTok.Tok == TOK_SEMI) {
482 /* Type declaration only */
483 CheckEmptyDecl (&Spec);
488 /* Parse a comma separated variable list */
491 /* Parse one declaration */
492 ParseOneDecl (&Spec);
494 /* Check if there is more */
495 if (CurTok.Tok == TOK_COMMA) {
504 /* A semicolon must follow */
508 /* Be sure to allocate any reserved space for locals */
509 F_AllocLocalSpace (CurrentFunc);
511 /* In case we've allocated local variables in this block, emit a call to
512 * the stack checking routine if stack checks are enabled.
514 if (IS_Get (&CheckStack) && InitialStack != StackPtr) {