]> git.sur5r.net Git - cc65/blobdiff - src/cc65/litpool.c
Fixed a bug
[cc65] / src / cc65 / litpool.c
index 6243e38b5dbd50018a070ad91751420385c242c3..e5bc9b737a29ca5bb652a02d6379a76057ac2315 100644 (file)
 
 #include <stdio.h>
 
-#include "asmlabel.h"
+/* common */
 #include "check.h"
-#include "ctrans.h"
+#include "tgttrans.h"
+
+/* cc65 */
+#include "asmlabel.h"
 #include "codegen.h"
 #include "error.h"
 #include "global.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 */
+unsigned             LiteralPoolLabel  = 0;    /* Pool asm label */
+static StrBuf         LiteralPool       = STATIC_STRBUF_INITIALIZER;
 
 
 
 /*****************************************************************************/
-/*                                  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] = ctrans (LiteralPool[Offs]);
-       ++Offs;
-    }
+    TgtTranslateBuf (SB_GetBuf (&LiteralPool) + Offs, SB_GetLen (&LiteralPool) - Offs);
 }
 
 
@@ -82,8 +87,8 @@ void TranslateLiteralPool (unsigned Offs)
 void DumpLiteralPool (void)
 /* Dump the literal pool */
 {
-    /* if nothing there, exit... */
-    if (LiteralOffs == 0) {
+    /* If nothing there, exit... */
+    if (SB_GetLen (&LiteralPool) == 0) {
        return;
     }
 
@@ -95,40 +100,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 (SB_GetConstBuf (&LiteralPool), SB_GetLen (&LiteralPool));
 }
 
 
 
-unsigned GetLiteralOffs (void)
+unsigned GetLiteralPoolOffs (void)
 /* Return the current offset into the literal pool */
 {
-    return LiteralOffs;
+    return SB_GetLen (&LiteralPool);
 }
 
 
 
-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 <= SB_GetLen (&LiteralPool));
+    SB_Cut (&LiteralPool, Offs);
 }
 
 
@@ -136,10 +133,7 @@ void ResetLiteralOffs (unsigned Offs)
 void AddLiteralChar (char C)
 /* Add one character to the literal pool */
 {
-    if (LiteralOffs >= LITPOOL_SIZE) {
-       Fatal (FAT_OUT_OF_STRSPACE);
-    }
-    LiteralPool[LiteralOffs++] = C;
+    SB_AppendChar (&LiteralPool, C);
 }
 
 
@@ -150,12 +144,10 @@ unsigned AddLiteral (const char* S)
  */
 {
     /* Remember the starting offset */
-    unsigned Start = LiteralOffs;
+    unsigned Start = SB_GetLen (&LiteralPool);
 
-    /* Copy the string doing a range check */
-    do {
-       AddLiteralChar (*S);
-    } while (*S++);
+    /* Copy the string including the terminator growing the buffer if needed */
+    SB_AppendBuf (&LiteralPool, S, strlen (S) + 1);
 
     /* Return the starting offset */
     return Start;
@@ -166,16 +158,27 @@ 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 < SB_GetLen (&LiteralPool));
+    return SB_GetConstBuf (&LiteralPool) + Offs;
+}
+
+
+
+void GetLiteralStrBuf (StrBuf* Target, unsigned Offs)
+/* Copy the string starting at Offs and lasting to the end of the buffer
+ * into Target.
+ */
+{                                        
+    CHECK (Offs <= SB_GetLen (&LiteralPool));
+    SB_Slice (Target, &LiteralPool, Offs, SB_GetLen (&LiteralPool) - 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", SB_GetLen (&LiteralPool));
 }