1 /*****************************************************************************/
5 /* Module constructor/destructor support */
9 /* (C) 2000-2008 Ullrich von Bassewitz */
10 /* Roemerstrasse 52 */
11 /* D-70794 Filderstadt */
12 /* EMail: uz@cc65.org */
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. */
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: */
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 */
32 /*****************************************************************************/
54 /*****************************************************************************/
56 /*****************************************************************************/
60 /* Struct describing one condes type */
61 typedef struct ConDesDesc 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) */
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 },
83 /*****************************************************************************/
84 /* Internally used function to create the condes tables */
85 /*****************************************************************************/
89 static int ConDesCompare (void* Data, const void* E1, const void* E2)
90 /* Compare function to sort the exports */
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.
98 ConDesDesc* CD = ((ConDesDesc*) Data);
99 int Type = CD - ConDes;
100 const Export* Exp1 = (const Export*) E1;
101 const Export* Exp2 = (const Export*) E2;
103 /* Get the priorities of the two exports */
104 unsigned Prio1 = Exp1->ConDes[Type];
105 unsigned Prio2 = Exp2->ConDes[Type];
107 /* Compare the priorities for this condes type */
110 } else if (Prio1 > Prio2) {
113 /* Use the name in this case */
114 Cmp = SB_Compare (GetStrBuf (Exp1->Name), GetStrBuf (Exp2->Name));
117 /* Reverse the result for decreasing order */
118 if (CD->Order == cdIncreasing) {
127 static void ConDesCreateOne (ConDesDesc* CD)
128 /* Create one table if requested */
130 Segment* Seg; /* Segment for table */
131 Section* Sec; /* Section for table */
132 unsigned Count; /* Number of exports */
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.
138 if (CD->SegName == INVALID_STRING_ID || CD->Label == INVALID_STRING_ID) {
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
146 if (!IsUnresolved (CD->Label)) {
150 /* Sort the collection of exports according to priority */
151 CollSort (&CD->ExpList, ConDesCompare, CD);
153 /* Get the segment for the table, create it if needed */
154 Seg = GetSegment (CD->SegName, ADDR_SIZE_ABS, 0);
156 /* Create a new section for the table */
157 Sec = NewSection (Seg, 1, ADDR_SIZE_ABS);
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
164 Count = CollCount (&CD->ExpList);
165 for (I = 0; I < Count; ++I) {
168 Export* E = CollAt (&CD->ExpList, I);
170 /* Create the fragment */
171 Fragment* F = NewFragment (FRAG_EXPR, 2, Sec);
173 /* Set the expression pointer */
177 /* Define the table start as an export, offset into section is zero
178 * (the section only contains the table).
180 CreateSectionExport (CD->Label, Sec, 0);
182 /* If we have a CountSym name given AND if it is referenced, define it
183 * with the number of elements in the table.
186 CreateConstExport (CD->CountSym, Count);
192 /*****************************************************************************/
194 /*****************************************************************************/
198 void ConDesAddExport (struct Export* E)
199 /* Add the given export to the list of constructors/destructor */
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);
214 void ConDesSetSegName (unsigned Type, unsigned SegName)
215 /* Set the segment name where the table should go */
217 /* Check the parameters */
218 PRECONDITION (Type <= CD_TYPE_MAX && SegName != 0);
220 /* Setting the segment name twice is bad */
221 CHECK (ConDes[Type].SegName == INVALID_STRING_ID);
224 ConDes[Type].SegName = SegName;
229 void ConDesSetLabel (unsigned Type, unsigned Name)
230 /* Set the label for the given ConDes type */
232 /* Check the parameters */
233 PRECONDITION (Type <= CD_TYPE_MAX && Name != 0);
235 /* Setting the label twice is bad */
236 CHECK (ConDes[Type].Label == INVALID_STRING_ID);
239 ConDes[Type].Label = Name;
244 void ConDesSetCountSym (unsigned Type, unsigned Name)
245 /* Set the name for the given ConDes count symbol */
247 /* Check the parameters */
248 PRECONDITION (Type <= CD_TYPE_MAX && Name != 0);
250 /* Setting the symbol twice is bad */
251 CHECK (ConDes[Type].CountSym == INVALID_STRING_ID);
254 ConDes[Type].CountSym = Name;
259 void ConDesSetOrder (unsigned Type, ConDesOrder Order)
260 /* Set the sorting oder for the given ConDes table */
262 /* Check the parameters */
263 PRECONDITION (Type <= CD_TYPE_MAX);
266 ConDes[Type].Order = Order;
271 int ConDesHasSegName (unsigned Type)
272 /* Return true if a segment name is already defined for this ConDes type */
274 /* Check the parameters */
275 PRECONDITION (Type <= CD_TYPE_MAX);
277 return (ConDes[Type].SegName != INVALID_STRING_ID);
282 int ConDesHasLabel (unsigned Type)
283 /* Return true if a label is already defined for this ConDes type */
285 /* Check the parameters */
286 PRECONDITION (Type <= CD_TYPE_MAX);
288 return (ConDes[Type].Label != INVALID_STRING_ID);
293 void ConDesCreate (void)
294 /* Create the condes tables if requested */
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);
306 void ConDesDump (void)
307 /* Dump ConDes data to stdout for debugging */
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));