]> git.sur5r.net Git - cc65/blobdiff - src/cc65/litpool.c
More optimizations
[cc65] / src / cc65 / litpool.c
index abe07f4b6ee0a93de343483b996942accfe8c487..f57e9441330e4b4ca5d7891fb70ebd5ce2d57bc9 100644 (file)
@@ -38,6 +38,7 @@
 /* common */
 #include "check.h"
 #include "tgttrans.h"
+#include "xmalloc.h"
 
 /* cc65 */
 #include "asmlabel.h"
 
 
 /*****************************************************************************/
-/*                                  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.
  */
 {
-    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,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;
 }
 
 
@@ -136,10 +136,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 +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++);
@@ -166,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);
 }