]> git.sur5r.net Git - cc65/blob - src/cc65/litpool.c
Added .dbg statement generation for the assembler
[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 = GetLocalLabel ();
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_defdatalabel (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
115
116
117 unsigned GetLiteralPoolOffs (void)
118 /* Return the current offset into the literal pool */
119 {
120     return LiteralPoolOffs;
121 }
122
123
124
125 void ResetLiteralPoolOffs (unsigned Offs)
126 /* Reset the offset into the literal pool to some earlier value, effectively
127  * removing values from the pool.
128  */
129 {
130     CHECK (Offs <= LiteralPoolOffs);
131     LiteralPoolOffs = Offs;
132 }
133
134
135
136 void AddLiteralChar (char C)
137 /* Add one character to the literal pool */
138 {
139     /* Grow the buffer if needed */
140     if (LiteralPoolOffs >= LiteralPoolSize) {
141         if (LiteralPoolSize == 0) {
142             /* First call */
143             LiteralPoolSize = 256;
144         } else {
145             LiteralPoolSize *= 2;
146         }
147         LiteralPoolBuf = xrealloc (LiteralPoolBuf, LiteralPoolSize);
148     }
149
150     /* Store the character */
151     LiteralPoolBuf[LiteralPoolOffs++] = C;
152 }
153
154
155
156 unsigned AddLiteral (const char* S)
157 /* Add a literal string to the literal pool. Return the starting offset into
158  * the pool
159  */
160 {
161     /* Remember the starting offset */
162     unsigned Start = LiteralPoolOffs;
163
164     /* Copy the string including the terminator growing the buffer if needed */
165     do {
166         AddLiteralChar (*S);
167     } while (*S++);
168
169     /* Return the starting offset */
170     return Start;
171 }
172
173
174
175 const char* GetLiteral (unsigned Offs)
176 /* Get a pointer to the literal with the given offset in the pool */
177 {
178     CHECK (Offs < LiteralPoolOffs);
179     return (const char*) &LiteralPoolBuf[Offs];
180 }
181
182
183
184 void PrintLiteralPoolStats (FILE* F)
185 /* Print statistics about the literal space used */
186 {
187     fprintf (F, "Literal space used: %u bytes\n", LiteralPoolOffs);
188 }
189
190
191