]> git.sur5r.net Git - cc65/blob - src/cc65/litpool.c
8886b67159e371bfa82baac0f8a8575350129fff
[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
41 /* cc65 */
42 #include "asmlabel.h"
43 #include "ctrans.h"
44 #include "codegen.h"
45 #include "error.h"
46 #include "global.h"
47 #include "litpool.h"
48
49
50
51 /*****************************************************************************/
52 /*                                   Data                                    */
53 /*****************************************************************************/
54
55
56
57 #define LITPOOL_SIZE    4096                    /* Max strings per function */
58 static unsigned char LiteralPool[LITPOOL_SIZE]; /* The literal pool */
59 static unsigned LiteralOffs     = 0;            /* Current pool offset */
60 static unsigned LiteralSpace    = 0;            /* Space used (stats only) */
61
62 unsigned LiteralLabel           = 1;            /* Pool asm label */
63
64
65
66 /*****************************************************************************/
67 /*                                   Code                                    */
68 /*****************************************************************************/
69
70
71
72 void TranslateLiteralPool (unsigned Offs)
73 /* Translate the literals starting from the given offset into the target
74  * charset.
75  */
76 {
77     while (Offs < LiteralOffs) {
78         LiteralPool[Offs] = ctrans (LiteralPool[Offs]);
79         ++Offs;
80     }
81 }
82
83
84
85 void DumpLiteralPool (void)
86 /* Dump the literal pool */
87 {
88     /* if nothing there, exit... */
89     if (LiteralOffs == 0) {
90         return;
91     }
92
93     /* Switch to the data segment */
94     if (WriteableStrings) {
95         g_usedata ();
96     } else {
97         g_userodata ();
98     }
99
100     /* Define the label */
101     g_defloclabel (LiteralLabel);
102
103     /* Translate the buffer contents into the target charset */
104     TranslateLiteralPool (0);
105
106     /* Output the buffer data */
107     g_defbytes (LiteralPool, LiteralOffs);
108
109     /* Switch back to the code segment */
110     g_usecode ();
111
112     /* Reset the buffer */
113     LiteralSpace += LiteralOffs;        /* Count literal bytes emitted */
114     LiteralLabel  = GetLabel ();        /* Get a new pool label */
115     LiteralOffs   = 0;
116 }
117
118
119
120 unsigned GetLiteralOffs (void)
121 /* Return the current offset into the literal pool */
122 {
123     return LiteralOffs;
124 }
125
126
127
128 void ResetLiteralOffs (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 <= LiteralOffs);
134     LiteralOffs = Offs;
135 }
136
137
138
139 void AddLiteralChar (char C)
140 /* Add one character to the literal pool */
141 {
142     if (LiteralOffs >= LITPOOL_SIZE) {
143         Fatal (FAT_OUT_OF_STRSPACE);
144     }
145     LiteralPool[LiteralOffs++] = C;
146 }
147
148
149
150 unsigned AddLiteral (const char* S)
151 /* Add a literal string to the literal pool. Return the starting offset into
152  * the pool
153  */
154 {
155     /* Remember the starting offset */
156     unsigned Start = LiteralOffs;
157
158     /* Copy the string doing a range check */
159     do {
160         AddLiteralChar (*S);
161     } while (*S++);
162
163     /* Return the starting offset */
164     return Start;
165 }
166
167
168
169 const char* GetLiteral (unsigned Offs)
170 /* Get a pointer to the literal with the given offset in the pool */
171 {
172     CHECK (Offs < LiteralOffs);
173     return (const char*) &LiteralPool[Offs];
174 }
175
176
177
178 void PrintLiteralStats (FILE* F)
179 /* Print statistics about the literal space used */
180 {
181     fprintf (F, "Literal space used: %d bytes\n", LiteralSpace);
182 }
183
184
185