X-Git-Url: https://git.sur5r.net/?a=blobdiff_plain;f=src%2Fcc65%2Flocals.c;h=fccc8040e94429ffd5f36384ce1efbcba9f7b146;hb=08443d5e7aca1686a7b69dd23ff496516dfdf569;hp=3cf6c61276de5dff567003145b51c89d748105e9;hpb=77e8bffa81d947d2d726c9ee2efffaf1e945a16d;p=cc65 diff --git a/src/cc65/locals.c b/src/cc65/locals.c index 3cf6c6127..fccc8040e 100644 --- a/src/cc65/locals.c +++ b/src/cc65/locals.c @@ -6,10 +6,10 @@ /* */ /* */ /* */ -/* (C) 2000 Ullrich von Bassewitz */ -/* Wacholderweg 14 */ -/* D-70597 Stuttgart */ -/* EMail: uz@musoftware.de */ +/* (C) 2000-2001 Ullrich von Bassewitz */ +/* Wacholderweg 14 */ +/* D-70597 Stuttgart */ +/* EMail: uz@cc65.org */ /* */ /* */ /* This software is provided 'as-is', without any expressed or implied */ @@ -33,8 +33,11 @@ -#include "../common/xmalloc.h" +/* common */ +#include "xmalloc.h" +#include "xsprintf.h" +/* cc65 */ #include "anonname.h" #include "asmlabel.h" #include "codegen.h" @@ -81,7 +84,7 @@ void InitRegVars (void) * will usually waste some space but we don't need to dynamically * grow the array. */ - RegSyms = xmalloc (MaxRegSpace * sizeof (RegSyms[0])); + RegSyms = (const SymEntry**) xmalloc (MaxRegSpace * sizeof (RegSyms[0])); RegOffs = MaxRegSpace; } @@ -146,10 +149,10 @@ static void ParseOneDecl (const DeclSpec* Spec) ParseDecl (Spec, &Decl, DM_NEED_IDENT); /* Set the correct storage class for functions */ - if (IsFunc (Decl.Type)) { + if (IsTypeFunc (Decl.Type)) { /* Function prototypes are always external */ if ((SC & SC_EXTERN) == 0) { - Warning (WARN_FUNC_MUST_BE_EXTERN); + Warning ("Function must be extern"); } SC |= SC_FUNC | SC_EXTERN; @@ -175,23 +178,20 @@ static void ParseOneDecl (const DeclSpec* Spec) /* Change SC in case it was register */ SC = (SC & ~SC_REGISTER) | SC_AUTO; - if (curtok == TOK_ASSIGN) { + if (CurTok.Tok == TOK_ASSIGN) { - struct expent lval; + ExprDesc lval; /* Allocate previously reserved local space */ AllocLocalSpace (CurrentFunc); - /* Switch to the code segment. */ - g_usecode (); - /* Skip the '=' */ NextToken (); /* Setup the type flags for the assignment */ flags = Size == 1? CF_FORCECHAR : CF_NONE; - /* Get the expression into the primary */ + /* Get the expression into the primary */ if (evalexpr (flags, hie1, &lval) == 0) { /* Constant expression. Adjust the types */ assignadjust (Decl.Type, &lval); @@ -202,7 +202,7 @@ static void ParseOneDecl (const DeclSpec* Spec) } /* Push the value */ - g_push (flags | TypeOf (Decl.Type), lval.e_const); + g_push (flags | TypeOf (Decl.Type), lval.ConstVal); /* Mark the variable as referenced */ SC |= SC_REF; @@ -226,37 +226,37 @@ static void ParseOneDecl (const DeclSpec* Spec) g_usebss (); /* Define the variable label */ - SymData = GetLabel (); - g_defloclabel (SymData); + SymData = GetLocalLabel (); + g_defdatalabel (SymData); /* Reserve space for the data */ g_res (Size); /* Allow assignments */ - if (curtok == TOK_ASSIGN) { - - struct expent lval; + if (CurTok.Tok == TOK_ASSIGN) { - /* Switch to the code segment. */ - g_usecode (); + ExprDesc lval; /* Skip the '=' */ NextToken (); - /* Get the expression into the primary */ - expression1 (&lval); - - /* Make type adjustments if needed */ - assignadjust (Decl.Type, &lval); - /* Setup the type flags for the assignment */ - flags = TypeOf (Decl.Type); - if (Size == 1) { - flags |= CF_FORCECHAR; + flags = Size == 1? CF_FORCECHAR : CF_NONE; + + /* Get the expression into the primary */ + if (evalexpr (flags, hie1, &lval) == 0) { + /* Constant expression. Adjust the types */ + assignadjust (Decl.Type, &lval); + flags |= CF_CONST; + /* Load it into the primary */ + exprhs (flags, 0, &lval); + } else { + /* Expression is not constant and in the primary */ + assignadjust (Decl.Type, &lval); } /* Store the value into the variable */ - g_putstatic (flags, SymData, 0); + g_putstatic (flags | TypeOf (Decl.Type), SymData, 0); /* Mark the variable as referenced */ SC |= SC_REF; @@ -266,22 +266,26 @@ static void ParseOneDecl (const DeclSpec* Spec) } else if ((SC & SC_STATIC) == SC_STATIC) { /* Static data */ - if (curtok == TOK_ASSIGN) { + if (CurTok.Tok == TOK_ASSIGN) { - /* Initialization ahead, switch to data segment */ - g_usedata (); + /* Initialization ahead, switch to data segment */ + if (IsQualConst (Decl.Type)) { + g_userodata (); + } else { + g_usedata (); + } - /* Define the variable label */ - SymData = GetLabel (); - g_defloclabel (SymData); + /* Define the variable label */ + SymData = GetLocalLabel (); + g_defdatalabel (SymData); - /* Skip the '=' */ - NextToken (); + /* Skip the '=' */ + NextToken (); - /* Allow initialization of static vars */ - ParseInit (Decl.Type); + /* Allow initialization of static vars */ + ParseInit (Decl.Type); - /* Mark the variable as referenced */ + /* Mark the variable as referenced */ SC |= SC_REF; } else { @@ -290,8 +294,8 @@ static void ParseOneDecl (const DeclSpec* Spec) g_usebss (); /* Define the variable label */ - SymData = GetLabel (); - g_defloclabel (SymData); + SymData = GetLocalLabel (); + g_defdatalabel (SymData); /* Reserve space for the data */ g_res (Size); @@ -315,6 +319,9 @@ static void ParseOneDecl (const DeclSpec* Spec) void DeclareLocals (void) /* Declare local variables and types. */ { + /* Remember the current stack pointer */ + int InitialStack = oursp; + /* Loop until we don't find any more variables */ while (1) { @@ -331,7 +338,7 @@ void DeclareLocals (void) } /* Accept type only declarations */ - if (curtok == TOK_SEMI) { + if (CurTok.Tok == TOK_SEMI) { /* Type declaration only */ CheckEmptyDecl (&Spec); NextToken (); @@ -345,7 +352,7 @@ void DeclareLocals (void) ParseOneDecl (&Spec); /* Check if there is more */ - if (curtok == TOK_COMMA) { + if (CurTok.Tok == TOK_COMMA) { /* More to come */ NextToken (); } else { @@ -361,8 +368,12 @@ void DeclareLocals (void) /* Be sure to allocate any reserved space for locals */ AllocLocalSpace (CurrentFunc); - /* In case we switched away from code segment, switch back now */ - g_usecode (); + /* In case we've allocated local variables in this block, emit a call to + * the stack checking routine if stack checks are enabled. + */ + if (CheckStack && InitialStack != oursp) { + g_cstackcheck (); + } }