X-Git-Url: https://git.sur5r.net/?a=blobdiff_plain;f=src%2Fcc65%2Flitpool.c;h=f57e9441330e4b4ca5d7891fb70ebd5ce2d57bc9;hb=916a0879d53d72b28638f4849b384e6567b6d807;hp=801a5ab43b470167f0f21c3ffe156e36083e55ae;hpb=03357652232b72f44fb44b1ba3507875c1d5d862;p=cc65 diff --git a/src/cc65/litpool.c b/src/cc65/litpool.c index 801a5ab43..f57e94413 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,35 +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 = GetLocalLabel (); +} + + + void TranslateLiteralPool (unsigned Offs) /* Translate the literals starting from the given offset into the target * charset. */ { - while (Offs < LiteralOffs) { - LiteralPool[Offs] = TgtTranslateChar (LiteralPool[Offs]); - ++Offs; - } + TgtTranslateBuf (LiteralPoolBuf + Offs, LiteralPoolOffs - Offs); } @@ -85,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; } @@ -98,40 +103,32 @@ void DumpLiteralPool (void) } /* Define the label */ - g_defloclabel (LiteralLabel); + g_defdatalabel (LiteralPoolLabel); /* Translate the buffer contents into the target charset */ TranslateLiteralPool (0); /* Output the buffer data */ - g_defbytes (LiteralPool, LiteralOffs); - - /* 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; + g_defbytes (LiteralPoolBuf, LiteralPoolOffs); } -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; } @@ -139,10 +136,19 @@ void ResetLiteralOffs (unsigned Offs) void AddLiteralChar (char C) /* Add one character to the literal pool */ { - if (LiteralOffs >= LITPOOL_SIZE) { - Fatal (FAT_OUT_OF_STRSPACE); + /* 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; } @@ -153,9 +159,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++); @@ -169,16 +175,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); }