1 /*****************************************************************************/
5 /* Handle program external symbols for relocatable output formats */
9 /* (C) 1999-2003 Ullrich von Bassewitz */
10 /* Römerstrasse 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 /*****************************************************************************/
49 /*****************************************************************************/
51 /*****************************************************************************/
55 /* Structure holding an external symbol */
57 unsigned Name; /* Name index */
58 ExtSym* List; /* Next entry in list of all symbols */
59 ExtSym* Next; /* Next entry in hash list */
60 unsigned Flags; /* Generic flags */
61 unsigned Num; /* Number of external symbol */
64 /* External symbol table structure */
65 #define HASHTAB_MASK 0x3FU
66 #define HASHTAB_SIZE (HASHTAB_MASK + 1)
68 ExtSym* Root; /* List of symbols */
69 ExtSym* Last; /* Pointer to last symbol */
70 unsigned Count; /* Number of symbols */
71 ExtSym* HashTab[HASHTAB_SIZE];
76 /*****************************************************************************/
78 /*****************************************************************************/
82 ExtSym* NewExtSym (ExtSymTab* Tab, unsigned Name)
83 /* Create a new external symbol and insert it into the table */
85 /* Get the hash value of the string */
86 unsigned Hash = (Name & HASHTAB_MASK);
88 /* Check for duplicates */
89 ExtSym* E = GetExtSym (Tab, Name);
91 /* We do already have a symbol with this name */
92 Error ("Duplicate external symbol `%s'", GetString (Name));
95 /* Allocate memory for the structure */
96 E = xmalloc (sizeof (ExtSym));
98 /* Initialize the structure */
104 /* Insert the entry into the list of all symbols */
105 if (Tab->Last == 0) {
115 /* Insert the symbol into the hash table */
116 E->Next = Tab->HashTab[Hash];
117 Tab->HashTab[Hash] = E;
119 /* Done, return the created entry */
125 static void FreeExtSym (ExtSym* E)
126 /* Free an external symbol structure. Will not unlink the entry, so internal
135 ExtSymTab* NewExtSymTab (void)
136 /* Create a new external symbol table */
140 /* Allocate memory */
141 ExtSymTab* Tab = xmalloc (sizeof (ExtSymTab));
143 /* Initialize the fields */
147 for (I = 0; I < HASHTAB_SIZE; ++I) {
148 Tab->HashTab [I] = 0;
151 /* Done, return the hash table */
157 void FreeExtSymTab (ExtSymTab* Tab)
158 /* Free an external symbol structure */
160 /* Free all entries */
162 ExtSym* E = Tab->Root;
167 /* Free the struct itself */
173 ExtSym* GetExtSym (const ExtSymTab* Tab, unsigned Name)
174 /* Return the entry for the external symbol with the given name. Return NULL
175 * if there is no such symbol.
179 unsigned Hash = (Name & HASHTAB_MASK);
181 /* Check the linked list */
182 ExtSym* E = Tab->HashTab[Hash];
184 if (E->Name == Name) {
191 /* Return the symbol we found */
197 unsigned ExtSymCount (const ExtSymTab* Tab)
198 /* Return the number of symbols in the table */
205 const ExtSym* ExtSymList (const ExtSymTab* Tab)
206 /* Return the start of the symbol list sorted by symbol number. Call
207 * ExtSymNext for the next symbol.
215 unsigned ExtSymNum (const ExtSym* E)
216 /* Return the number of an external symbol */
223 unsigned ExtSymName (const ExtSym* E)
224 /* Return the symbol name index */
231 const ExtSym* ExtSymNext (const ExtSym* E)
232 /* Return the next symbol in the list */