4 * (C) Copyright 1989-1995
12 * - lookup: Use StoreString for sym construction
13 * - change symeq() to do length-independent string compare
14 * - change hash() to do length-independent hash calculation
16 * - make mnemonics case insensitive ALWAYS
17 * - make hash() case-insensitive always
18 * - replace symeq() call in mlookup with strcmpi
29 * The module assym.c contains the functions that operate
30 * on the mnemonic/directive and symbol structures.
32 * assym.c contains the following functions:
43 * assym.c contains no local/static variables.
46 /*)Function VOID syminit()
48 * The function syminit() is called early in the game
49 * to set up the hashtables. First all buckets in a
50 * table are cleared. Then a pass is made through
51 * the respective symbol lists, linking them into
52 * their hash buckets. Finally the base area pointer
56 * int h computed hash value
57 * mne * mp pointer to a mne structure
58 * mne ** mpp pointer to an array of
59 * mne structure pointers
60 * sym * sp pointer to a sym structure
61 * sym ** spp pointer to an array of
62 * sym structure pointers
65 * area area[] single elememt area array
66 * area dca defined as area[0]
67 * mne * mnehash[] array of pointers to NHASH
68 * linked mnemonic/directive lists
69 * sym * symhash[] array of pointers to NHASH
76 * (1) The symbol hash tables are initialized,
77 * the only defined symbol is '.'.
78 * (2) The mnemonic/directive hash tables are
79 * initialized with the assembler directives
80 * and mnemonics found in the machine dependent
82 * (3) The area pointer is initialized to dca (area[0]).
88 register struct mne *mp;
90 register struct sym *sp;
95 while (mpp < &mnehash[NHASH])
100 mp->m_mp = mnehash[h];
102 if (mp->m_flag&S_END)
108 while (spp < &symhash[NHASH])
113 sp->s_sp = symhash[h];
115 if (sp->s_flag&S_END)
123 /*)Function area * alookup(id)
125 * char * id area name string
127 * The function alookup() searches the area list for a
128 * match with id. If the area is defined then a pointer
129 * to this area is returned else a NULL is returned.
132 * area * ap pointer to area structure
135 * area * areap pointer to an area structure
138 * int symeq() assym.c
148 register struct area *ap;
152 if (symeq(id, ap->a_id)) {
160 /*)Function mne * mlookup(id)
162 * char * id mnemonic/directive name string
164 * The function mlookup() searches the mnemonic/directive
165 * hash tables for a match returning a pointer to the
166 * mne structure else it returns a NULL.
169 * mne * mp pointer to mne structure
170 * int h calculated hash value
173 * mne * mnehash[] array of pointers to NHASH
174 * linked mnemonic/directive lists
187 register struct mne *mp;
193 if (as_strcmpi(id, mp->m_id) == 0) /* JLH: case insensitive */
200 /*)Function sym * lookup(id)
202 * char * id symbol name string
204 * The function lookup() searches the symbol hash tables for
205 * a symbol name match returning a pointer to the sym structure.
206 * If the symbol is not found then a sym structure is created,
207 * initialized, and linked to the appropriate hash table.
208 * A pointer to this new sym structure is returned.
211 * int h computed hash value
212 * sym * sp pointer to a sym structure
215 * sym * symhash[] array of pointers to NHASH
216 * linked symbol lists
220 * VOID * new() assym.c
221 * int symeq() assym.c
224 * If the function new() fails to allocate space
225 * for the new sym structure the assembly terminates.
232 register struct sym *sp;
238 if (symeq(id, sp->s_id))
242 sp = (struct sym *) new (sizeof(struct sym));
243 sp->s_sp = symhash[h];
246 sp->s_id = StoreString( id ); /* JLH */
255 /*)Function VOID symglob()
257 * The function symglob() will mark all symbols of
258 * type S_NEW as global. Called at the beginning of pass 1
259 * if the assembly option -g was specified.
262 * sym * sp pointer to a sym structure
266 * sym * symhash[] array of pointers to NHASH
267 * linked symbol lists
273 * Symbol types changed.
279 register struct sym *sp;
282 for (i=0; i<NHASH; ++i) {
285 if (sp->s_type == S_NEW)
292 /*)Function VOID allglob()
294 * The function allglob() will mark all symbols of
295 * type S_USER as global. Called at the beginning of pass 1
296 * if the assembly option -a was specified.
299 * sym * sp pointer to a sym structure
303 * sym * symhash[] array of pointers to NHASH
304 * linked symbol lists
310 * Symbol types changed.
316 register struct sym *sp;
319 for (i=0; i<NHASH; ++i) {
322 if (sp != &dot && sp->s_type == S_USER)
329 /*)Function int symeq(p1, p2)
331 * char * p1 name string
332 * char * p2 name string
334 * The function symeq() compares the two name strings for a match.
335 * The return value is 1 for a match and 0 for no match.
341 * char ccase[] an array of characters which
342 * perform the case translation function
354 register char *p1, *p2;
357 return (strcmp( p1, p2 ) == 0);
359 return (as_strcmpi( p1, p2 ) == 0);
363 /*)Function int hash(p)
365 * char * p pointer to string to hash
367 * The function hash() computes a hash code using the sum
368 * of all characters mod table size algorithm.
371 * int h accumulated character sum
375 * char ccase[] an array of characters which
376 * perform the case translation function
393 /* JLH: case insensitive hash: Doesn't much affect
394 * hashing, and allows same function for mnemonics and symbols
396 h += ccase[(int)*p++];
401 /*)Function VOID * new(n)
403 * unsigned int n allocation size in bytes
405 * The function new() allocates n bytes of space and returns
406 * a pointer to this memory. If no space is available the
407 * assembly is terminated.
410 * VOID * p a general pointer
416 * VOID asexit() asmain.c
417 * int fprintf() c_library
418 * VOID * malloc() c_library
421 * Memory is allocated, if allocation fails
422 * the assembly is terminated.
431 if ((p = (VOID *) malloc(n)) == NULL) {
432 fprintf(stderr, "Out of space!\n");