]> git.sur5r.net Git - cc65/blob - src/ld65/condes.c
Use a string pool to reduce the memory footprint
[cc65] / src / ld65 / condes.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                 condes.h                                  */
4 /*                                                                           */
5 /*                   Module constructor/destructor support                   */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 2000-2003 Ullrich von Bassewitz                                       */
10 /*               Römerstrasse 52                                             */
11 /*               D-70794 Filderstadt                                         */
12 /* EMail:        uz@cc65.org                                                 */
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 <string.h>
37
38 /* common */
39 #include "check.h"
40 #include "coll.h"
41 #include "fragdefs.h"
42 #include "segdefs.h"
43 #include "xmalloc.h"
44
45 /* ld65 */
46 #include "condes.h"
47 #include "exports.h"
48 #include "fragment.h"
49 #include "segments.h"
50 #include "spool.h"
51
52
53
54 /*****************************************************************************/
55 /*                                   Data                                    */
56 /*****************************************************************************/
57
58
59
60 /* Struct describing one condes type */
61 typedef struct ConDesDesc ConDesDesc;
62 struct ConDesDesc {
63     Collection          ExpList;        /* List of exported symbols */
64     unsigned            SegName;        /* Name of segment the table is in */
65     unsigned            Label;          /* Name of table label */
66     unsigned            CountSym;       /* Name of symbol for entry count */
67     unsigned char       Order;          /* Table order (increasing/decreasing) */
68 };
69
70 /* Array for all types */
71 static ConDesDesc ConDes[CD_TYPE_COUNT] = {
72     { STATIC_COLLECTION_INITIALIZER, INVALID_STRING_ID, INVALID_STRING_ID, INVALID_STRING_ID, cdIncreasing },
73     { STATIC_COLLECTION_INITIALIZER, INVALID_STRING_ID, INVALID_STRING_ID, INVALID_STRING_ID, cdIncreasing },
74     { STATIC_COLLECTION_INITIALIZER, INVALID_STRING_ID, INVALID_STRING_ID, INVALID_STRING_ID, cdIncreasing },
75     { STATIC_COLLECTION_INITIALIZER, INVALID_STRING_ID, INVALID_STRING_ID, INVALID_STRING_ID, cdIncreasing },
76     { STATIC_COLLECTION_INITIALIZER, INVALID_STRING_ID, INVALID_STRING_ID, INVALID_STRING_ID, cdIncreasing },
77     { STATIC_COLLECTION_INITIALIZER, INVALID_STRING_ID, INVALID_STRING_ID, INVALID_STRING_ID, cdIncreasing },
78     { STATIC_COLLECTION_INITIALIZER, INVALID_STRING_ID, INVALID_STRING_ID, INVALID_STRING_ID, cdIncreasing },
79 };
80
81
82
83 /*****************************************************************************/
84 /*           Internally used function to create the condes tables            */
85 /*****************************************************************************/
86
87
88
89 static int ConDesCompare (void* Data, const void* E1, const void* E2)
90 /* Compare function to sort the exports */
91 {
92     int Cmp;
93
94     /* Data is actually a pointer to a ConDesDesc from the table, E1 and
95      * E2 are exports from the collection. Get the condes type and cast
96      * the void pointers to object pointers.
97      */
98     ConDesDesc* CD = ((ConDesDesc*) Data);
99     int Type = CD - ConDes;
100     const Export* Exp1 = (const Export*) E1;
101     const Export* Exp2 = (const Export*) E2;
102
103     /* Get the priorities of the two exports */
104     unsigned Prio1 = Exp1->ConDes[Type];
105     unsigned Prio2 = Exp2->ConDes[Type];
106
107     /* Compare the priorities for this condes type */
108     if (Prio1 < Prio2) {
109         Cmp = -1;
110     } else if (Prio1 > Prio2) {
111         Cmp = 1;
112     } else {
113         /* Use the name in this case */
114         Cmp = strcmp (GetString (Exp1->Name), GetString (Exp2->Name));
115     }
116
117     /* Reverse the result for decreasing order */
118     if (CD->Order == cdIncreasing) {
119         return Cmp;
120     } else {
121         return -Cmp;
122     }
123 }
124
125
126
127 static void ConDesCreateOne (ConDesDesc* CD)
128 /* Create one table if requested */
129 {
130     Segment*    Seg;            /* Segment for table */
131     Section*    Sec;            /* Section for table */
132     unsigned    Count;          /* Number of exports */
133     unsigned    I;
134
135     /* Check if this table has a segment and table label defined. If not,
136      * creation was not requested in the config file - ignore it.
137      */
138     if (CD->SegName == INVALID_STRING_ID || CD->Label == INVALID_STRING_ID) {
139         return;
140     }
141
142     /* Check if there is an import for the table label. If not, there is no
143      * reference to the table and we would just waste memory creating the
144      * table.
145      */
146     if (!IsUnresolved (CD->Label)) {
147         return;
148     }
149
150     /* Sort the collection of exports according to priority */
151     CollSort (&CD->ExpList, ConDesCompare, CD);
152
153     /* Get the segment for the table, create it if needed */
154     Seg = GetSegment (CD->SegName, SEGTYPE_ABS, 0);
155
156     /* Create a new section for the table */
157     Sec = NewSection (Seg, 1, SEGTYPE_ABS);
158
159     /* Walk over the exports and create a fragment for each one. We will use
160      * the exported expression without copying it, since it's cheap and there
161      * is currently no place where it gets changed (hope this will not hunt
162      * me later...).
163      */
164     Count = CollCount (&CD->ExpList);
165     for (I = 0; I < Count; ++I) {
166
167         /* Get the export */
168         Export* E = CollAt (&CD->ExpList, I);
169
170         /* Create the fragment */
171         Fragment* F = NewFragment (FRAG_EXPR, 2, Sec);
172
173         /* Set the expression pointer */
174         F->Expr = E->Expr;
175     }
176
177     /* Define the table start as an export, offset into section is zero
178      * (the section only contains the table).
179      */
180     CreateSectionExport (CD->Label, Sec, 0);
181
182     /* If we have a CountSym name given AND if it is referenced, define it
183      * with the number of elements in the table.
184      */
185     if (CD->CountSym) {
186         CreateConstExport (CD->CountSym, Count);
187     }
188 }
189
190
191
192 /*****************************************************************************/
193 /*                                   Code                                    */
194 /*****************************************************************************/
195
196
197
198 void ConDesAddExport (struct Export* E)
199 /* Add the given export to the list of constructors/destructor */
200 {
201     unsigned Type;
202
203     /* Insert the export into all tables for which declarations exist */
204     for (Type = 0; Type < CD_TYPE_COUNT; ++Type) {
205         unsigned Prio = E->ConDes[Type];
206         if (Prio != CD_PRIO_NONE) {
207             CollAppend (&ConDes[Type].ExpList, E);
208         }
209     }
210 }
211
212
213
214 void ConDesSetSegName (unsigned Type, unsigned SegName)
215 /* Set the segment name where the table should go */
216 {
217     /* Check the parameters */
218     PRECONDITION (Type <= CD_TYPE_MAX && SegName != 0);
219
220     /* Setting the segment name twice is bad */
221     CHECK (ConDes[Type].SegName == INVALID_STRING_ID);
222
223     /* Set the name */
224     ConDes[Type].SegName = SegName;
225 }
226
227
228
229 void ConDesSetLabel (unsigned Type, unsigned Name)
230 /* Set the label for the given ConDes type */
231 {
232     /* Check the parameters */
233     PRECONDITION (Type <= CD_TYPE_MAX && Name != 0);
234
235     /* Setting the label twice is bad */
236     CHECK (ConDes[Type].Label == INVALID_STRING_ID);
237
238     /* Set the name */
239     ConDes[Type].Label = Name;
240 }
241
242
243
244 void ConDesSetCountSym (unsigned Type, unsigned Name)
245 /* Set the name for the given ConDes count symbol */
246 {
247     /* Check the parameters */
248     PRECONDITION (Type <= CD_TYPE_MAX && Name != 0);
249
250     /* Setting the symbol twice is bad */
251     CHECK (ConDes[Type].CountSym == INVALID_STRING_ID);
252
253     /* Set the name */
254     ConDes[Type].CountSym = Name;
255 }
256
257
258
259 void ConDesSetOrder (unsigned Type, ConDesOrder Order)
260 /* Set the sorting oder for the given ConDes table */
261 {
262     /* Check the parameters */
263     PRECONDITION (Type <= CD_TYPE_MAX);
264
265     /* Set the order */
266     ConDes[Type].Order = Order;
267 }
268
269
270
271 int ConDesHasSegName (unsigned Type)
272 /* Return true if a segment name is already defined for this ConDes type */
273 {
274     /* Check the parameters */
275     PRECONDITION (Type <= CD_TYPE_MAX);
276
277     return (ConDes[Type].SegName != INVALID_STRING_ID);
278 }
279
280
281
282 int ConDesHasLabel (unsigned Type)
283 /* Return true if a label is already defined for this ConDes type */
284 {
285     /* Check the parameters */
286     PRECONDITION (Type <= CD_TYPE_MAX);
287
288     return (ConDes[Type].Label != INVALID_STRING_ID);
289 }
290
291
292
293 void ConDesCreate (void)
294 /* Create the condes tables if requested */
295 {
296     unsigned Type;
297
298     /* Walk over the descriptor array and create a table for each entry */
299     for (Type = 0; Type < CD_TYPE_COUNT; ++Type) {
300         ConDesCreateOne (ConDes + Type);
301     }
302 }
303
304
305
306 void ConDesDump (void)
307 /* Dump ConDes data to stdout for debugging */
308 {
309     unsigned Type;
310     for (Type = 0; Type < CD_TYPE_COUNT; ++Type) {
311         Collection* ExpList = &ConDes[Type].ExpList;
312         printf ("CONDES(%u): %u symbols\n", Type, CollCount (ExpList));
313     }
314 }
315
316
317