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 = GetLocalLabel ();
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_defdatalabel (LiteralPoolLabel);
108 /* Translate the buffer contents into the target charset */
109 TranslateLiteralPool (0);
111 /* Output the buffer data */
112 g_defbytes (LiteralPoolBuf, LiteralPoolOffs);
117 unsigned GetLiteralPoolOffs (void)
118 /* Return the current offset into the literal pool */
120 return LiteralPoolOffs;
125 void ResetLiteralPoolOffs (unsigned Offs)
126 /* Reset the offset into the literal pool to some earlier value, effectively
127 * removing values from the pool.
130 CHECK (Offs <= LiteralPoolOffs);
131 LiteralPoolOffs = Offs;
136 void AddLiteralChar (char C)
137 /* Add one character to the literal pool */
139 /* Grow the buffer if needed */
140 if (LiteralPoolOffs >= LiteralPoolSize) {
141 if (LiteralPoolSize == 0) {
143 LiteralPoolSize = 256;
145 LiteralPoolSize *= 2;
147 LiteralPoolBuf = xrealloc (LiteralPoolBuf, LiteralPoolSize);
150 /* Store the character */
151 LiteralPoolBuf[LiteralPoolOffs++] = C;
156 unsigned AddLiteral (const char* S)
157 /* Add a literal string to the literal pool. Return the starting offset into
161 /* Remember the starting offset */
162 unsigned Start = LiteralPoolOffs;
164 /* Copy the string including the terminator growing the buffer if needed */
169 /* Return the starting offset */
175 const char* GetLiteral (unsigned Offs)
176 /* Get a pointer to the literal with the given offset in the pool */
178 CHECK (Offs < LiteralPoolOffs);
179 return (const char*) &LiteralPoolBuf[Offs];
184 void PrintLiteralPoolStats (FILE* F)
185 /* Print statistics about the literal space used */
187 fprintf (F, "Literal space used: %u bytes\n", LiteralPoolOffs);