1 /*****************************************************************************/
5 /* Literal string handling for the cc65 C compiler */
9 /* (C) 1998 Ullrich von Bassewitz */
11 /* D-70597 Stuttgart */
12 /* EMail: uz@musoftware.de */
15 /* This software is provided 'as-is', without any expressed or implied */
16 /* warranty. In no event will the authors be held liable for any damages */
17 /* arising from the use of this software. */
19 /* Permission is granted to anyone to use this software for any purpose, */
20 /* including commercial applications, and to alter it and redistribute it */
21 /* freely, subject to the following restrictions: */
23 /* 1. The origin of this software must not be misrepresented; you must not */
24 /* claim that you wrote the original software. If you use this software */
25 /* in a product, an acknowledgment in the product documentation would be */
26 /* appreciated but is not required. */
27 /* 2. Altered source versions must be plainly marked as such, and must not */
28 /* be misrepresented as being the original software. */
29 /* 3. This notice may not be removed or altered from any source */
32 /*****************************************************************************/
52 /*****************************************************************************/
54 /*****************************************************************************/
58 static unsigned char* LiteralPoolBuf = 0; /* Pointer to buffer */
59 static unsigned LiteralPoolSize = 0; /* Size of pool */
60 static unsigned LiteralPoolOffs = 0; /* Current offset into pool */
61 unsigned LiteralPoolLabel = 0; /* Pool asm label */
65 /*****************************************************************************/
67 /*****************************************************************************/
71 void InitLiteralPool (void)
72 /* Initialize the literal pool */
74 /* Get the pool label */
75 LiteralPoolLabel = GetLabel ();
80 void TranslateLiteralPool (unsigned Offs)
81 /* Translate the literals starting from the given offset into the target
85 TgtTranslateBuf (LiteralPoolBuf + Offs, LiteralPoolOffs - Offs);
90 void DumpLiteralPool (void)
91 /* Dump the literal pool */
93 /* If nothing there, exit... */
94 if (LiteralPoolOffs == 0) {
98 /* Switch to the data segment */
99 if (WriteableStrings) {
105 /* Define the label */
106 g_defloclabel (LiteralPoolLabel);
108 /* Translate the buffer contents into the target charset */
109 TranslateLiteralPool (0);
111 /* Output the buffer data */
112 g_defbytes (LiteralPoolBuf, LiteralPoolOffs);
114 /* Switch back to the code segment */
120 unsigned GetLiteralPoolOffs (void)
121 /* Return the current offset into the literal pool */
123 return LiteralPoolOffs;
128 void ResetLiteralPoolOffs (unsigned Offs)
129 /* Reset the offset into the literal pool to some earlier value, effectively
130 * removing values from the pool.
133 CHECK (Offs <= LiteralPoolOffs);
134 LiteralPoolOffs = Offs;
139 void AddLiteralChar (char C)
140 /* Add one character to the literal pool */
142 /* Grow the buffer if needed */
143 if (LiteralPoolOffs >= LiteralPoolSize) {
144 if (LiteralPoolSize == 0) {
146 LiteralPoolSize = 256;
148 LiteralPoolSize *= 2;
150 LiteralPoolBuf = xrealloc (LiteralPoolBuf, LiteralPoolSize);
153 /* Store the character */
154 LiteralPoolBuf[LiteralPoolOffs++] = C;
159 unsigned AddLiteral (const char* S)
160 /* Add a literal string to the literal pool. Return the starting offset into
164 /* Remember the starting offset */
165 unsigned Start = LiteralPoolOffs;
167 /* Copy the string including the terminator growing the buffer if needed */
172 /* Return the starting offset */
178 const char* GetLiteral (unsigned Offs)
179 /* Get a pointer to the literal with the given offset in the pool */
181 CHECK (Offs < LiteralPoolOffs);
182 return (const char*) &LiteralPoolBuf[Offs];
187 void PrintLiteralPoolStats (FILE* F)
188 /* Print statistics about the literal space used */
190 fprintf (F, "Literal space used: %u bytes\n", LiteralPoolOffs);