1 /*****************************************************************************/
5 /* Module constructor/destructor support */
9 /* (C) 2000-2012, 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 /*****************************************************************************/
55 /*****************************************************************************/
57 /*****************************************************************************/
61 /* Struct describing one condes type */
62 typedef struct ConDesDesc ConDesDesc;
64 Collection ExpList; /* List of exported symbols */
65 unsigned SegName; /* Name of segment the table is in */
66 unsigned Label; /* Name of table label */
67 unsigned CountSym; /* Name of symbol for entry count */
68 unsigned char Order; /* Table order (increasing/decreasing) */
69 ConDesImport Import; /* Forced import if any */
72 /* Array for all types */
73 static ConDesDesc ConDes[CD_TYPE_COUNT] = {
75 STATIC_COLLECTION_INITIALIZER,
80 { INVALID_STRING_ID, STATIC_FILEPOS_INITIALIZER, ADDR_SIZE_DEFAULT },
82 STATIC_COLLECTION_INITIALIZER,
87 { INVALID_STRING_ID, STATIC_FILEPOS_INITIALIZER, ADDR_SIZE_DEFAULT },
89 STATIC_COLLECTION_INITIALIZER,
94 { INVALID_STRING_ID, STATIC_FILEPOS_INITIALIZER, ADDR_SIZE_DEFAULT },
96 STATIC_COLLECTION_INITIALIZER,
101 { INVALID_STRING_ID, STATIC_FILEPOS_INITIALIZER, ADDR_SIZE_DEFAULT },
103 STATIC_COLLECTION_INITIALIZER,
108 { INVALID_STRING_ID, STATIC_FILEPOS_INITIALIZER, ADDR_SIZE_DEFAULT },
110 STATIC_COLLECTION_INITIALIZER,
115 { INVALID_STRING_ID, STATIC_FILEPOS_INITIALIZER, ADDR_SIZE_DEFAULT },
117 STATIC_COLLECTION_INITIALIZER,
122 { INVALID_STRING_ID, STATIC_FILEPOS_INITIALIZER, ADDR_SIZE_DEFAULT },
128 /*****************************************************************************/
129 /* Internally used function to create the condes tables */
130 /*****************************************************************************/
134 static int ConDesCompare (void* Data, const void* E1, const void* E2)
135 /* Compare function to sort the exports */
139 /* Data is actually a pointer to a ConDesDesc from the table, E1 and
140 ** E2 are exports from the collection. Get the condes type and cast
141 ** the void pointers to object pointers.
143 ConDesDesc* CD = ((ConDesDesc*) Data);
144 int Type = CD - ConDes;
145 const Export* Exp1 = (const Export*) E1;
146 const Export* Exp2 = (const Export*) E2;
148 /* Get the priorities of the two exports */
149 unsigned Prio1 = Exp1->ConDes[Type];
150 unsigned Prio2 = Exp2->ConDes[Type];
152 /* Compare the priorities for this condes type */
155 } else if (Prio1 > Prio2) {
158 /* Use the name in this case */
159 Cmp = SB_Compare (GetStrBuf (Exp1->Name), GetStrBuf (Exp2->Name));
162 /* Reverse the result for decreasing order */
163 if (CD->Order == cdIncreasing) {
172 static void ConDesCreateOne (ConDesDesc* CD)
173 /* Create one table if requested */
175 Segment* Seg; /* Segment for table */
176 Section* Sec; /* Section for table */
177 unsigned Count; /* Number of exports */
180 /* Check if this table has a segment and table label defined. If not,
181 ** creation was not requested in the config file - ignore it.
183 if (CD->SegName == INVALID_STRING_ID || CD->Label == INVALID_STRING_ID) {
187 /* Check if there is an import for the table label. If not, there is no
188 ** reference to the table and we would just waste memory creating the
191 if (!IsUnresolved (CD->Label)) {
195 /* Sort the collection of exports according to priority */
196 CollSort (&CD->ExpList, ConDesCompare, CD);
198 /* Get the segment for the table, create it if needed */
199 Seg = GetSegment (CD->SegName, ADDR_SIZE_ABS, 0);
201 /* Create a new section for the table */
202 Sec = NewSection (Seg, 1, ADDR_SIZE_ABS);
204 /* Walk over the exports and create a fragment for each one. We will use
205 ** the exported expression without copying it, since it's cheap and there
206 ** is currently no place where it gets changed (hope this will not hunt
209 Count = CollCount (&CD->ExpList);
210 for (I = 0; I < Count; ++I) {
213 Export* E = CollAt (&CD->ExpList, I);
215 /* Create the fragment */
216 Fragment* F = NewFragment (FRAG_EXPR, 2, Sec);
218 /* Set the expression pointer */
222 /* Define the table start as an export, offset into section is zero
223 ** (the section only contains the table).
225 CreateSectionExport (CD->Label, Sec, 0);
227 /* If we have a CountSym name given AND if it is referenced, define it
228 ** with the number of elements in the table.
231 CreateConstExport (CD->CountSym, Count);
237 /*****************************************************************************/
239 /*****************************************************************************/
243 void ConDesAddExport (struct Export* E)
244 /* Add the given export to the list of constructors/destructor */
248 /* Insert the export into all tables for which declarations exist */
249 for (Type = 0; Type < CD_TYPE_COUNT; ++Type) {
250 unsigned Prio = E->ConDes[Type];
251 if (Prio != CD_PRIO_NONE) {
252 CollAppend (&ConDes[Type].ExpList, E);
259 void ConDesSetSegName (unsigned Type, unsigned SegName)
260 /* Set the segment name where the table should go */
262 /* Check the parameters */
263 PRECONDITION (Type <= CD_TYPE_MAX && SegName != 0);
265 /* Setting the segment name twice is bad */
266 CHECK (ConDes[Type].SegName == INVALID_STRING_ID);
269 ConDes[Type].SegName = SegName;
274 const ConDesImport* ConDesGetImport (unsigned Type)
275 /* Get the forced import for the given ConDes type. Returns NULL if there is
276 ** no forced import for this type.
279 const ConDesImport* Import;
281 /* Check the parameters */
282 PRECONDITION (Type <= CD_TYPE_MAX);
284 /* Return the import */
285 Import = &ConDes[Type].Import;
286 return (Import->Name != INVALID_STRING_ID)? Import : 0;
291 void ConDesSetImport (unsigned Type, const ConDesImport* Import)
292 /* Set the forced import for the given ConDes type */
294 /* Check the parameters */
295 PRECONDITION (Type <= CD_TYPE_MAX && Import != 0);
297 /* Setting the import twice is bad */
298 CHECK (ConDes[Type].Import.Name == INVALID_STRING_ID);
300 /* Set the import and its position */
301 ConDes[Type].Import = *Import;
306 void ConDesSetLabel (unsigned Type, unsigned Name)
307 /* Set the label for the given ConDes type */
309 /* Check the parameters */
310 PRECONDITION (Type <= CD_TYPE_MAX && Name != 0);
312 /* Setting the label twice is bad */
313 CHECK (ConDes[Type].Label == INVALID_STRING_ID);
316 ConDes[Type].Label = Name;
321 void ConDesSetCountSym (unsigned Type, unsigned Name)
322 /* Set the name for the given ConDes count symbol */
324 /* Check the parameters */
325 PRECONDITION (Type <= CD_TYPE_MAX && Name != 0);
327 /* Setting the symbol twice is bad */
328 CHECK (ConDes[Type].CountSym == INVALID_STRING_ID);
331 ConDes[Type].CountSym = Name;
336 void ConDesSetOrder (unsigned Type, ConDesOrder Order)
337 /* Set the sorting oder for the given ConDes table */
339 /* Check the parameters */
340 PRECONDITION (Type <= CD_TYPE_MAX);
343 ConDes[Type].Order = Order;
348 int ConDesHasSegName (unsigned Type)
349 /* Return true if a segment name is already defined for this ConDes type */
351 /* Check the parameters */
352 PRECONDITION (Type <= CD_TYPE_MAX);
354 return (ConDes[Type].SegName != INVALID_STRING_ID);
359 int ConDesHasLabel (unsigned Type)
360 /* Return true if a label is already defined for this ConDes type */
362 /* Check the parameters */
363 PRECONDITION (Type <= CD_TYPE_MAX);
365 return (ConDes[Type].Label != INVALID_STRING_ID);
370 void ConDesCreate (void)
371 /* Create the condes tables if requested */
375 /* Walk over the descriptor array and create a table for each entry */
376 for (Type = 0; Type < CD_TYPE_COUNT; ++Type) {
377 ConDesCreateOne (ConDes + Type);
383 void ConDesDump (void)
384 /* Dump ConDes data to stdout for debugging */
387 for (Type = 0; Type < CD_TYPE_COUNT; ++Type) {
388 Collection* ExpList = &ConDes[Type].ExpList;
389 printf ("CONDES(%u): %u symbols\n", Type, CollCount (ExpList));