From 2eab65ad24b672cf72499ad6002c05a3a173c17a Mon Sep 17 00:00:00 2001 From: cuz Date: Mon, 26 Mar 2001 21:57:07 +0000 Subject: [PATCH] Remove the hardcoded limit from the literal pool. git-svn-id: svn://svn.cc65.org/cc65/trunk@678 b7a2c559-68d2-44c3-8de9-860c34a00d81 --- src/cc65/compile.c | 10 +++--- src/cc65/declare.c | 63 ++++++++++++++++++++++++++----------- src/cc65/declare.h | 46 +++++++++++++++++++++------ src/cc65/expr.c | 6 ++-- src/cc65/function.c | 3 -- src/cc65/litpool.c | 76 ++++++++++++++++++++++++++------------------- src/cc65/litpool.h | 14 ++++++--- src/cc65/locals.c | 8 ++--- src/cc65/parser.c | 2 +- src/cc65/pragma.c | 4 +-- src/cc65/scanner.c | 2 +- src/cc65/scanner.h | 2 +- 12 files changed, 152 insertions(+), 84 deletions(-) diff --git a/src/cc65/compile.c b/src/cc65/compile.c index a532881ce..73ac3700d 100644 --- a/src/cc65/compile.c +++ b/src/cc65/compile.c @@ -253,9 +253,6 @@ void Compile (void) char* Path; - /* Setup variables */ - LiteralLabel = GetLabel (); - /* Add some standard paths to the include search path */ AddIncludePath ("", INC_USER); /* Current directory */ AddIncludePath ("include", INC_SYS); @@ -291,6 +288,9 @@ void Compile (void) } } + /* Initialize the literal pool */ + InitLiteralPool (); + /* Create the base lexical level */ EnterGlobalLevel (); @@ -300,14 +300,14 @@ void Compile (void) /* Ok, start the ball rolling... */ Parse (); - /* Dump literal pool. */ + /* Dump the literal pool. */ DumpLiteralPool (); /* Write imported/exported symbols */ EmitExternals (); if (Debug) { - PrintLiteralStats (stdout); + PrintLiteralPoolStats (stdout); PrintMacroStats (stdout); } diff --git a/src/cc65/declare.c b/src/cc65/declare.c index b37d9088b..b6a92f844 100644 --- a/src/cc65/declare.c +++ b/src/cc65/declare.c @@ -1,8 +1,35 @@ -/* - * declare.c - * - * Ullrich von Bassewitz, 20.06.1998 - */ +/*****************************************************************************/ +/* */ +/* declare.c */ +/* */ +/* Parse variable and function declarations */ +/* */ +/* */ +/* */ +/* (C) 1998-2001 Ullrich von Bassewitz */ +/* Wacholderweg 14 */ +/* D-70597 Stuttgart */ +/* EMail: uz@musoftware.de */ +/* */ +/* */ +/* This software is provided 'as-is', without any expressed or implied */ +/* warranty. In no event will the authors be held liable for any damages */ +/* arising from the use of this software. */ +/* */ +/* Permission is granted to anyone to use this software for any purpose, */ +/* including commercial applications, and to alter it and redistribute it */ +/* freely, subject to the following restrictions: */ +/* */ +/* 1. The origin of this software must not be misrepresented; you must not */ +/* claim that you wrote the original software. If you use this software */ +/* in a product, an acknowledgment in the product documentation would be */ +/* appreciated but is not required. */ +/* 2. Altered source versions must be plainly marked as such, and must not */ +/* be misrepresented as being the original software. */ +/* 3. This notice may not be removed or altered from any source */ +/* distribution. */ +/* */ +/*****************************************************************************/ @@ -1025,11 +1052,11 @@ static void ParseStructInit (type* Type) void ParseInit (type* T) /* Parse initialization of variables. */ { - int count; struct expent lval; type* t; const char* str; - int sz; + int Count; + int Size; switch (UnqualifiedType (*T)) { @@ -1070,32 +1097,32 @@ void ParseInit (type* T) break; case T_ARRAY: - sz = Decode (T + 1); + Size = Decode (T + 1); t = T + DECODE_SIZE + 1; if (IsTypeChar(t) && curtok == TOK_SCONST) { str = GetLiteral (curval); - count = strlen (str) + 1; + Count = strlen (str) + 1; TranslateLiteralPool (curval); /* Translate into target charset */ - g_defbytes (str, count); - ResetLiteralOffs (curval); /* Remove string from pool */ + g_defbytes (str, Count); + ResetLiteralPoolOffs (curval); /* Remove string from pool */ NextToken (); } else { ConsumeLCurly (); - count = 0; + Count = 0; while (curtok != TOK_RCURLY) { ParseInit (T + DECODE_SIZE + 1); - ++count; + ++Count; if (curtok != TOK_COMMA) break; NextToken (); } ConsumeRCurly (); } - if (sz == 0) { - Encode (T + 1, count); - } else if (count < sz) { - g_zerobytes ((sz - count) * SizeOf (T + DECODE_SIZE + 1)); - } else if (count > sz) { + if (Size == 0) { + Encode (T + 1, Count); + } else if (Count < Size) { + g_zerobytes ((Size - Count) * SizeOf (T + DECODE_SIZE + 1)); + } else if (Count > Size) { Error ("Too many initializers"); } break; diff --git a/src/cc65/declare.h b/src/cc65/declare.h index 1edf38e21..c079a1a0d 100644 --- a/src/cc65/declare.h +++ b/src/cc65/declare.h @@ -1,8 +1,35 @@ -/* - * declare.h - * - * Ullrich von Bassewitz, 20.06.1998 - */ +/*****************************************************************************/ +/* */ +/* declare.h */ +/* */ +/* Parse variable and function declarations */ +/* */ +/* */ +/* */ +/* (C) 1998-2001 Ullrich von Bassewitz */ +/* Wacholderweg 14 */ +/* D-70597 Stuttgart */ +/* EMail: uz@musoftware.de */ +/* */ +/* */ +/* This software is provided 'as-is', without any expressed or implied */ +/* warranty. In no event will the authors be held liable for any damages */ +/* arising from the use of this software. */ +/* */ +/* Permission is granted to anyone to use this software for any purpose, */ +/* including commercial applications, and to alter it and redistribute it */ +/* freely, subject to the following restrictions: */ +/* */ +/* 1. The origin of this software must not be misrepresented; you must not */ +/* claim that you wrote the original software. If you use this software */ +/* in a product, an acknowledgment in the product documentation would be */ +/* appreciated but is not required. */ +/* 2. Altered source versions must be plainly marked as such, and must not */ +/* be misrepresented as being the original software. */ +/* 3. This notice may not be removed or altered from any source */ +/* distribution. */ +/* */ +/*****************************************************************************/ @@ -11,13 +38,14 @@ +/* cc65 */ #include "scanner.h" #include "symtab.h" /*****************************************************************************/ -/* Data */ +/* Data */ /*****************************************************************************/ @@ -31,7 +59,7 @@ typedef struct DeclSpec DeclSpec; struct DeclSpec { unsigned StorageClass; /* One of the SC_xxx flags */ - type Type [MAXTYPELEN]; /* Type of the declaration spec */ + type Type [MAXTYPELEN]; /* Type of the declaration spec */ unsigned Flags; /* Bitmapped flags */ }; @@ -42,7 +70,7 @@ struct Declaration { type Type [MAXTYPELEN]; /* The type */ /* Working variables */ - type* T; /* Used to build Type */ + type* T; /* Used to build Type */ }; /* Modes for ParseDecl */ @@ -73,7 +101,7 @@ void CheckEmptyDecl (const DeclSpec* D); * warning if not. */ -void ParseInit (type* tptr); +void ParseInit (type* T); /* Parse initialization of variables. */ diff --git a/src/cc65/expr.c b/src/cc65/expr.c index f517933f4..938f199d0 100644 --- a/src/cc65/expr.c +++ b/src/cc65/expr.c @@ -309,7 +309,7 @@ void DefineData (struct expent* lval) case E_TLIT: /* a literal of some kind */ - g_defdata (CF_STATIC, LiteralLabel, lval->e_const); + g_defdata (CF_STATIC, LiteralPoolLabel, lval->e_const); break; default: @@ -352,7 +352,7 @@ static void lconst (unsigned flags, struct expent* lval) case E_TLIT: /* Literal string */ - g_getimmed (CF_STATIC, LiteralLabel, lval->e_const); + g_getimmed (CF_STATIC, LiteralPoolLabel, lval->e_const); break; default: @@ -756,7 +756,7 @@ void doasm (void) * will fail if the next token is also a string token, but that's a * syntax error anyway, because we expect a right paren. */ - ResetLiteralOffs (curval); + ResetLiteralPoolOffs (curval); } /* Skip the string token */ diff --git a/src/cc65/function.c b/src/cc65/function.c index b4340b85e..f6d64ffab 100644 --- a/src/cc65/function.c +++ b/src/cc65/function.c @@ -333,9 +333,6 @@ void NewFunc (SymEntry* Func) /* Emit references to imports/exports */ EmitExternals (); - /* Dump literal data created by the function */ - DumpLiteralPool (); - /* Cleanup register variables */ DoneRegVars (); diff --git a/src/cc65/litpool.c b/src/cc65/litpool.c index abe07f4b6..e47ae2c94 100644 --- a/src/cc65/litpool.c +++ b/src/cc65/litpool.c @@ -38,6 +38,7 @@ /* common */ #include "check.h" #include "tgttrans.h" +#include "xmalloc.h" /* cc65 */ #include "asmlabel.h" @@ -49,32 +50,39 @@ /*****************************************************************************/ -/* Data */ +/* Data */ /*****************************************************************************/ -#define LITPOOL_SIZE 4096 /* Max strings per function */ -static unsigned char LiteralPool[LITPOOL_SIZE]; /* The literal pool */ -static unsigned LiteralOffs = 0; /* Current pool offset */ -static unsigned LiteralSpace = 0; /* Space used (stats only) */ - -unsigned LiteralLabel = 1; /* Pool asm label */ +static unsigned char* LiteralPoolBuf = 0; /* Pointer to buffer */ +static unsigned LiteralPoolSize = 0; /* Size of pool */ +static unsigned LiteralPoolOffs = 0; /* Current offset into pool */ +unsigned LiteralPoolLabel = 0; /* Pool asm label */ /*****************************************************************************/ -/* Code */ +/* Code */ /*****************************************************************************/ +void InitLiteralPool (void) +/* Initialize the literal pool */ +{ + /* Get the pool label */ + LiteralPoolLabel = GetLabel (); +} + + + void TranslateLiteralPool (unsigned Offs) /* Translate the literals starting from the given offset into the target * charset. */ { - TgtTranslateBuf (LiteralPool + Offs, LiteralOffs - Offs); + TgtTranslateBuf (LiteralPoolBuf + Offs, LiteralPoolOffs - Offs); } @@ -82,8 +90,8 @@ void TranslateLiteralPool (unsigned Offs) void DumpLiteralPool (void) /* Dump the literal pool */ { - /* if nothing there, exit... */ - if (LiteralOffs == 0) { + /* If nothing there, exit... */ + if (LiteralPoolOffs == 0) { return; } @@ -95,40 +103,35 @@ void DumpLiteralPool (void) } /* Define the label */ - g_defloclabel (LiteralLabel); + g_defloclabel (LiteralPoolLabel); /* Translate the buffer contents into the target charset */ TranslateLiteralPool (0); /* Output the buffer data */ - g_defbytes (LiteralPool, LiteralOffs); + g_defbytes (LiteralPoolBuf, LiteralPoolOffs); /* Switch back to the code segment */ g_usecode (); - - /* Reset the buffer */ - LiteralSpace += LiteralOffs; /* Count literal bytes emitted */ - LiteralLabel = GetLabel (); /* Get a new pool label */ - LiteralOffs = 0; } -unsigned GetLiteralOffs (void) +unsigned GetLiteralPoolOffs (void) /* Return the current offset into the literal pool */ { - return LiteralOffs; + return LiteralPoolOffs; } -void ResetLiteralOffs (unsigned Offs) +void ResetLiteralPoolOffs (unsigned Offs) /* Reset the offset into the literal pool to some earlier value, effectively * removing values from the pool. */ { - CHECK (Offs <= LiteralOffs); - LiteralOffs = Offs; + CHECK (Offs <= LiteralPoolOffs); + LiteralPoolOffs = Offs; } @@ -136,10 +139,19 @@ void ResetLiteralOffs (unsigned Offs) void AddLiteralChar (char C) /* Add one character to the literal pool */ { - if (LiteralOffs >= LITPOOL_SIZE) { - Fatal ("Out of literal space"); + /* Grow the buffer if needed */ + if (LiteralPoolOffs >= LiteralPoolSize) { + if (LiteralPoolSize == 0) { + /* First call */ + LiteralPoolSize = 256; + } else { + LiteralPoolSize *= 2; + } + LiteralPoolBuf = xrealloc (LiteralPoolBuf, LiteralPoolSize); } - LiteralPool[LiteralOffs++] = C; + + /* Store the character */ + LiteralPoolBuf[LiteralPoolOffs++] = C; } @@ -150,9 +162,9 @@ unsigned AddLiteral (const char* S) */ { /* Remember the starting offset */ - unsigned Start = LiteralOffs; + unsigned Start = LiteralPoolOffs; - /* Copy the string doing a range check */ + /* Copy the string including the terminator growing the buffer if needed */ do { AddLiteralChar (*S); } while (*S++); @@ -166,16 +178,16 @@ unsigned AddLiteral (const char* S) const char* GetLiteral (unsigned Offs) /* Get a pointer to the literal with the given offset in the pool */ { - CHECK (Offs < LiteralOffs); - return (const char*) &LiteralPool[Offs]; + CHECK (Offs < LiteralPoolOffs); + return (const char*) &LiteralPoolBuf[Offs]; } -void PrintLiteralStats (FILE* F) +void PrintLiteralPoolStats (FILE* F) /* Print statistics about the literal space used */ { - fprintf (F, "Literal space used: %d bytes\n", LiteralSpace); + fprintf (F, "Literal space used: %u bytes\n", LiteralPoolOffs); } diff --git a/src/cc65/litpool.h b/src/cc65/litpool.h index 3cd69846b..cd5cf9031 100644 --- a/src/cc65/litpool.h +++ b/src/cc65/litpool.h @@ -48,16 +48,19 @@ -extern unsigned LiteralLabel; /* Pool asm label */ +extern unsigned LiteralPoolLabel; /* Pool asm label */ /*****************************************************************************/ -/* Code */ +/* Code */ /*****************************************************************************/ +void InitLiteralPool (void); +/* Initialize the literal pool */ + void TranslateLiteralPool (unsigned Offs); /* Translate the literals starting from the given offset into the target * charset. @@ -66,10 +69,10 @@ void TranslateLiteralPool (unsigned Offs); void DumpLiteralPool (void); /* Dump the literal pool */ -unsigned GetLiteralOffs (void); +unsigned GetLiteralPoolOffs (void); /* Return the current offset into the literal pool */ -void ResetLiteralOffs (unsigned Offs); +void ResetLiteralPoolOffs (unsigned Offs); /* Reset the offset into the literal pool to some earlier value, effectively * removing values from the pool. */ @@ -85,7 +88,7 @@ unsigned AddLiteral (const char* S); const char* GetLiteral (unsigned Offs); /* Get a pointer to the literal with the given offset in the pool */ -void PrintLiteralStats (FILE* F); +void PrintLiteralPoolStats (FILE* F); /* Print statistics about the literal space used */ @@ -95,3 +98,4 @@ void PrintLiteralStats (FILE* F); + diff --git a/src/cc65/locals.c b/src/cc65/locals.c index 25fbf81f8..c10083ec4 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@musoftware.de */ /* */ /* */ /* This software is provided 'as-is', without any expressed or implied */ diff --git a/src/cc65/parser.c b/src/cc65/parser.c index 42f732477..1e9536c5a 100644 --- a/src/cc65/parser.c +++ b/src/cc65/parser.c @@ -179,7 +179,7 @@ ExprNode* DoAsm (void) * will fail if the next token is also a string token, but that's a * syntax error anyway, because we expect a right paren. */ - ResetLiteralOffs (CurTok.IVal); + ResetLiteralPoolOffs (CurTok.IVal); } /* Skip the string token */ diff --git a/src/cc65/pragma.c b/src/cc65/pragma.c index 0ef31083d..51a2dbdc4 100644 --- a/src/cc65/pragma.c +++ b/src/cc65/pragma.c @@ -129,7 +129,7 @@ static void StringPragma (void (*Func) (const char*)) Func (Name); /* Reset the string pointer, removing the string from the pool */ - ResetLiteralOffs (curval); + ResetLiteralPoolOffs (curval); } /* Skip the string (or error) token */ @@ -161,7 +161,7 @@ static void SegNamePragma (void (*Func) (const char*)) } /* Reset the string pointer, removing the string from the pool */ - ResetLiteralOffs (curval); + ResetLiteralPoolOffs (curval); } /* Skip the string (or error) token */ diff --git a/src/cc65/scanner.c b/src/cc65/scanner.c index 58df8f76d..598d5d214 100644 --- a/src/cc65/scanner.c +++ b/src/cc65/scanner.c @@ -340,7 +340,7 @@ static void CharConst (void) static void StringConst (void) /* Parse a quoted string */ { - nxtval = GetLiteralOffs (); + nxtval = GetLiteralPoolOffs (); nxttok = TOK_SCONST; /* Be sure to concatenate strings */ diff --git a/src/cc65/scanner.h b/src/cc65/scanner.h index 216c007c9..af424af97 100644 --- a/src/cc65/scanner.h +++ b/src/cc65/scanner.h @@ -153,7 +153,7 @@ struct Token_ { double FVal; /* The float attribute */ ident Ident; /* Identifier if IDENT */ unsigned Pos; /* Source line where the token comes from */ - type* Type; /* Type if integer or float constant */ + type* Type; /* Type if integer or float constant */ }; extern Token CurTok; /* The current token */ -- 2.39.5