]> git.sur5r.net Git - cc65/blobdiff - src/cc65/litpool.c
Cleanup. Added a few general purpose functions.
[cc65] / src / cc65 / litpool.c
index abe07f4b6ee0a93de343483b996942accfe8c487..e026db0d49af72c85687d75068e9ef46c7bfa785 100644 (file)
@@ -6,10 +6,10 @@
 /*                                                                           */
 /*                                                                           */
 /*                                                                           */
-/* (C) 1998     Ullrich von Bassewitz                                        */
-/*              Wacholderweg 14                                              */
-/*              D-70597 Stuttgart                                            */
-/* EMail:       uz@musoftware.de                                             */
+/* (C) 1998-2004 Ullrich von Bassewitz                                        */
+/*               Römerstraße 52                                              */
+/*               D-70794 Filderstadt                                         */
+/* EMail:        uz@cc65.org                                                 */
 /*                                                                           */
 /*                                                                           */
 /* This software is provided 'as-is', without any expressed or implied       */
 
 
 /*****************************************************************************/
-/*                                  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.
  */
 {
-    TgtTranslateBuf (LiteralPool + Offs, LiteralOffs - Offs);
+    TgtTranslateBuf (SB_GetBuf (&LiteralPool) + Offs, SB_GetLen (&LiteralPool) - Offs);
 }
 
 
@@ -82,53 +87,45 @@ 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;
     }
 
     /* Switch to the data segment */
-    if (WriteableStrings) {
+    if (IS_Get (&WritableStrings)) {
        g_usedata ();
     } else {
                g_userodata ();
     }
 
     /* 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 ("Out of literal space");
-    }
-    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));
 }