]> git.sur5r.net Git - cc65/blob - src/cc65/litpool.c
e47ae2c9483f7a8a34c082feb756a3c1e4ca003e
[cc65] / src / cc65 / litpool.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                 litpool.c                                 */
4 /*                                                                           */
5 /*              Literal string handling for the cc65 C compiler              */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 1998     Ullrich von Bassewitz                                        */
10 /*              Wacholderweg 14                                              */
11 /*              D-70597 Stuttgart                                            */
12 /* EMail:       uz@musoftware.de                                             */
13 /*                                                                           */
14 /*                                                                           */
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.                                    */
18 /*                                                                           */
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:                            */
22 /*                                                                           */
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              */
30 /*    distribution.                                                          */
31 /*                                                                           */
32 /*****************************************************************************/
33
34
35
36 #include <stdio.h>
37
38 /* common */
39 #include "check.h"
40 #include "tgttrans.h"
41 #include "xmalloc.h"
42
43 /* cc65 */
44 #include "asmlabel.h"
45 #include "codegen.h"
46 #include "error.h"
47 #include "global.h"
48 #include "litpool.h"
49
50
51
52 /*****************************************************************************/
53 /*                                   Data                                    */
54 /*****************************************************************************/
55
56
57
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 */
62
63
64
65 /*****************************************************************************/
66 /*                                   Code                                    */
67 /*****************************************************************************/
68
69
70
71 void InitLiteralPool (void)
72 /* Initialize the literal pool */
73 {
74     /* Get the pool label */
75     LiteralPoolLabel = GetLabel ();
76 }
77
78
79
80 void TranslateLiteralPool (unsigned Offs)
81 /* Translate the literals starting from the given offset into the target
82  * charset.
83  */
84 {
85     TgtTranslateBuf (LiteralPoolBuf + Offs, LiteralPoolOffs - Offs);
86 }
87
88
89
90 void DumpLiteralPool (void)
91 /* Dump the literal pool */
92 {
93     /* If nothing there, exit... */
94     if (LiteralPoolOffs == 0) {
95         return;
96     }
97
98     /* Switch to the data segment */
99     if (WriteableStrings) {
100         g_usedata ();
101     } else {
102         g_userodata ();
103     }
104
105     /* Define the label */
106     g_defloclabel (LiteralPoolLabel);
107
108     /* Translate the buffer contents into the target charset */
109     TranslateLiteralPool (0);
110
111     /* Output the buffer data */
112     g_defbytes (LiteralPoolBuf, LiteralPoolOffs);
113
114     /* Switch back to the code segment */
115     g_usecode ();
116 }
117
118
119
120 unsigned GetLiteralPoolOffs (void)
121 /* Return the current offset into the literal pool */
122 {
123     return LiteralPoolOffs;
124 }
125
126
127
128 void ResetLiteralPoolOffs (unsigned Offs)
129 /* Reset the offset into the literal pool to some earlier value, effectively
130  * removing values from the pool.
131  */
132 {
133     CHECK (Offs <= LiteralPoolOffs);
134     LiteralPoolOffs = Offs;
135 }
136
137
138
139 void AddLiteralChar (char C)
140 /* Add one character to the literal pool */
141 {
142     /* Grow the buffer if needed */
143     if (LiteralPoolOffs >= LiteralPoolSize) {
144         if (LiteralPoolSize == 0) {
145             /* First call */
146             LiteralPoolSize = 256;
147         } else {
148             LiteralPoolSize *= 2;
149         }
150         LiteralPoolBuf = xrealloc (LiteralPoolBuf, LiteralPoolSize);
151     }
152
153     /* Store the character */
154     LiteralPoolBuf[LiteralPoolOffs++] = C;
155 }
156
157
158
159 unsigned AddLiteral (const char* S)
160 /* Add a literal string to the literal pool. Return the starting offset into
161  * the pool
162  */
163 {
164     /* Remember the starting offset */
165     unsigned Start = LiteralPoolOffs;
166
167     /* Copy the string including the terminator growing the buffer if needed */
168     do {
169         AddLiteralChar (*S);
170     } while (*S++);
171
172     /* Return the starting offset */
173     return Start;
174 }
175
176
177
178 const char* GetLiteral (unsigned Offs)
179 /* Get a pointer to the literal with the given offset in the pool */
180 {
181     CHECK (Offs < LiteralPoolOffs);
182     return (const char*) &LiteralPoolBuf[Offs];
183 }
184
185
186
187 void PrintLiteralPoolStats (FILE* F)
188 /* Print statistics about the literal space used */
189 {
190     fprintf (F, "Literal space used: %u bytes\n", LiteralPoolOffs);
191 }
192
193
194