]> git.sur5r.net Git - cc65/blob - src/cc65/litpool.c
Changed TgtTranslateBuf
[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
42 /* cc65 */
43 #include "asmlabel.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     TgtTranslateBuf (LiteralPool + Offs, LiteralOffs - Offs);
78 }
79
80
81
82 void DumpLiteralPool (void)
83 /* Dump the literal pool */
84 {
85     /* if nothing there, exit... */
86     if (LiteralOffs == 0) {
87         return;
88     }
89
90     /* Switch to the data segment */
91     if (WriteableStrings) {
92         g_usedata ();
93     } else {
94         g_userodata ();
95     }
96
97     /* Define the label */
98     g_defloclabel (LiteralLabel);
99
100     /* Translate the buffer contents into the target charset */
101     TranslateLiteralPool (0);
102
103     /* Output the buffer data */
104     g_defbytes (LiteralPool, LiteralOffs);
105
106     /* Switch back to the code segment */
107     g_usecode ();
108
109     /* Reset the buffer */
110     LiteralSpace += LiteralOffs;        /* Count literal bytes emitted */
111     LiteralLabel  = GetLabel ();        /* Get a new pool label */
112     LiteralOffs   = 0;
113 }
114
115
116
117 unsigned GetLiteralOffs (void)
118 /* Return the current offset into the literal pool */
119 {
120     return LiteralOffs;
121 }
122
123
124
125 void ResetLiteralOffs (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 <= LiteralOffs);
131     LiteralOffs = Offs;
132 }
133
134
135
136 void AddLiteralChar (char C)
137 /* Add one character to the literal pool */
138 {
139     if (LiteralOffs >= LITPOOL_SIZE) {
140         Fatal ("Out of literal space");
141     }
142     LiteralPool[LiteralOffs++] = C;
143 }
144
145
146
147 unsigned AddLiteral (const char* S)
148 /* Add a literal string to the literal pool. Return the starting offset into
149  * the pool
150  */
151 {
152     /* Remember the starting offset */
153     unsigned Start = LiteralOffs;
154
155     /* Copy the string doing a range check */
156     do {
157         AddLiteralChar (*S);
158     } while (*S++);
159
160     /* Return the starting offset */
161     return Start;
162 }
163
164
165
166 const char* GetLiteral (unsigned Offs)
167 /* Get a pointer to the literal with the given offset in the pool */
168 {
169     CHECK (Offs < LiteralOffs);
170     return (const char*) &LiteralPool[Offs];
171 }
172
173
174
175 void PrintLiteralStats (FILE* F)
176 /* Print statistics about the literal space used */
177 {
178     fprintf (F, "Literal space used: %d bytes\n", LiteralSpace);
179 }
180
181
182