3 Copyright (C) 1989-1995 Alan R. Baldwin
4 721 Berkeley St., Kent, Ohio 44240
6 This program is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 3, or (at your option) any
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21 * - use strsto instead StoreString and include it in assym.c
22 * for compatibility with the original asxxxx
23 * - applied changes from 28-Oct-97 JLH:
24 * - lookup: Use StoreString for sym construction
25 * - change symeq() to do length-independent string compare
26 * - change hash() to do length-independent hash calculation
27 * - applied changes from 29-Oct-97 JLH:
28 * - make mnemonics case insensitive ALWAYS
29 * - make hash() case-insensitive always
30 * - replace symeq() call in mlookup with strcmpi
41 * The module assym.c contains the functions that operate
42 * on the mnemonic/directive and symbol structures.
44 * assym.c contains the following functions:
55 * assym.c contains no local/static variables.
58 /*)Function VOID syminit()
60 * The function syminit() is called early in the game
61 * to set up the hashtables. First all buckets in a
62 * table are cleared. Then a pass is made through
63 * the respective symbol lists, linking them into
64 * their hash buckets. Finally the base area pointer
68 * int h computed hash value
69 * mne * mp pointer to a mne structure
70 * mne ** mpp pointer to an array of
71 * mne structure pointers
72 * sym * sp pointer to a sym structure
73 * sym ** spp pointer to an array of
74 * sym structure pointers
77 * area area[] single elememt area array
78 * area dca defined as area[0]
79 * mne * mnehash[] array of pointers to NHASH
80 * linked mnemonic/directive lists
81 * sym * symhash[] array of pointers to NHASH
88 * (1) The symbol hash tables are initialized,
89 * the only defined symbol is '.'.
90 * (2) The mnemonic/directive hash tables are
91 * initialized with the assembler directives
92 * and mnemonics found in the machine dependent
94 * (3) The area pointer is initialized to dca (area[0]).
100 register struct mne *mp;
102 register struct sym *sp;
107 while (mpp < &mnehash[NHASH])
112 mp->m_mp = mnehash[h];
114 if (mp->m_flag&S_END)
120 while (spp < &symhash[NHASH])
125 sp->s_sp = symhash[h];
127 if (sp->s_flag&S_END)
135 /*)Function area * alookup(id)
137 * char * id area name string
139 * The function alookup() searches the area list for a
140 * match with id. If the area is defined then a pointer
141 * to this area is returned else a NULL is returned.
144 * area * ap pointer to area structure
147 * area * areap pointer to an area structure
150 * int symeq() assym.c
159 register struct area *ap;
163 if (symeq(id, ap->a_id)) {
171 /*)Function mne * mlookup(id)
173 * char * id mnemonic/directive name string
175 * The function mlookup() searches the mnemonic/directive
176 * hash tables for a match returning a pointer to the
177 * mne structure else it returns a NULL.
180 * mne * mp pointer to mne structure
181 * int h calculated hash value
184 * mne * mnehash[] array of pointers to NHASH
185 * linked mnemonic/directive lists
197 register struct mne *mp;
203 if (as_strcmpi(id, mp->m_id) == 0) /* JLH: case insensitive */
210 /*)Function sym * lookup(id)
212 * char * id symbol name string
214 * The function lookup() searches the symbol hash tables for
215 * a symbol name match returning a pointer to the sym structure.
216 * If the symbol is not found then a sym structure is created,
217 * initialized, and linked to the appropriate hash table.
218 * A pointer to this new sym structure is returned.
221 * int h computed hash value
222 * sym * sp pointer to a sym structure
225 * sym * symhash[] array of pointers to NHASH
226 * linked symbol lists
230 * VOID * new() assym.c
231 * int symeq() assym.c
234 * If the function new() fails to allocate space
235 * for the new sym structure the assembly terminates.
241 register struct sym *sp;
247 if (symeq(id, sp->s_id))
251 sp = (struct sym *) new (sizeof(struct sym));
252 sp->s_sp = symhash[h];
255 sp->s_id = strsto(id);
264 /*)Function VOID symglob()
266 * The function symglob() will mark all symbols of
267 * type S_NEW as global. Called at the beginning of pass 1
268 * if the assembly option -g was specified.
271 * sym * sp pointer to a sym structure
275 * sym * symhash[] array of pointers to NHASH
276 * linked symbol lists
282 * Symbol types changed.
288 register struct sym *sp;
291 for (i=0; i<NHASH; ++i) {
294 if (sp->s_type == S_NEW)
301 /*)Function VOID allglob()
303 * The function allglob() will mark all symbols of
304 * type S_USER as global. Called at the beginning of pass 1
305 * if the assembly option -a was specified.
308 * sym * sp pointer to a sym structure
312 * sym * symhash[] array of pointers to NHASH
313 * linked symbol lists
319 * Symbol types changed.
325 register struct sym *sp;
328 for (i=0; i<NHASH; ++i) {
331 if (sp != &dot && sp->s_type == S_USER)
338 /*)Function int symeq(p1, p2)
340 * char * p1 name string
341 * char * p2 name string
343 * The function symeq() compares the two name strings for a match.
344 * The return value is 1 for a match and 0 for no match.
350 * char ccase[] an array of characters which
351 * perform the case translation function
362 symeq(char *p1, char *p2)
365 return (strcmp( p1, p2 ) == 0);
367 return (as_strcmpi( p1, p2 ) == 0);
371 /*)Function int hash(p)
373 * char * p pointer to string to hash
375 * The function hash() computes a hash code using the sum
376 * of all characters mod table size algorithm.
379 * int h accumulated character sum
383 * char ccase[] an array of characters which
384 * perform the case translation function
400 /* JLH: case insensitive hash: Doesn't much affect
401 * hashing, and allows same function for mnemonics and symbols
403 h += ccase[(int)*p++];
408 /*)Function char * strsto(str)
410 * char * str pointer to string to save
412 * Allocate space for "str", copy str into new space.
413 * Return a pointer to the allocated string.
415 * This function based on code by
417 * jhartman at compuserve dot com
420 * int l string length + 1
421 * int bytes bytes remaining in buffer area
422 * char * p pointer to head of copied string
423 * char * pnext next location in buffer area
429 * VOID * new() assym.c
430 * char * strncpy() c_library
433 * Space allocated for string, string copied
434 * to space. Out of Space terminates assembler.
438 * To avoid wasting memory headers on small allocations, we
439 * allocate a big chunk and parcel it out as required.
440 * These static variables remember our hunk
444 static char * pnext = NULL;
445 static int bytes = 0;
454 * What we need, including a null.
460 * No space. Allocate a new hunk.
461 * We lose the pointer to any old hunk.
462 * We don't care, as the names are never deleted.
464 pnext = (char *) new (STR_SPC);
469 * Copy the name and terminating null.
480 /*)Function VOID * new(n)
482 * unsigned int n allocation size in bytes
484 * The function new() allocates n bytes of space and returns
485 * a pointer to this memory. If no space is available the
486 * assembly is terminated.
489 * VOID * p a general pointer
495 * VOID asexit() asmain.c
496 * int fprintf() c_library
497 * VOID * malloc() c_library
500 * Memory is allocated, if allocation fails
501 * the assembly is terminated.
509 if ((p = (VOID *) malloc(n)) == NULL) {
510 fprintf(stderr, "Out of space!\n");