Use headers defined in the C[++] standards:
[fw/sdcc] / as / z80 / assym.c
1 /* assym.c */
2
3 /*
4  * (C) Copyright 1989-1995
5  * All Rights Reserved
6  *
7  * Alan R. Baldwin
8  * 721 Berkeley St.
9  * Kent, Ohio  44240
10  */
11
12 #include <stdio.h>
13 #include <setjmp.h>
14 #include <string.h>
15 #include <stdlib.h>
16 #if defined(__APPLE__) && defined(__MACH__)
17 #include <sys/types.h>
18 #include <sys/malloc.h>
19 #else
20 #include <malloc.h>
21 #endif
22 #include "asm.h"
23
24 /*)Module       assym.c
25  *
26  *      The module assym.c contains the functions that operate
27  *      on the mnemonic/directive and symbol structures.
28  *
29  *      assym.c contains the following functions:
30  *              VOID    allglob()
31  *              area *  alookup()
32  *              int     hash()
33  *              sym *   lookup()
34  *              mne *   mlookup()
35  *              VOID *  new()
36  *              int     symeq()
37  *              VOID    syminit()
38  *              VOID    symglob()
39  *
40  *      assym.c contains no local/static variables.
41  */
42
43 /*)Function     VOID    syminit()
44  *
45  *      The function syminit() is called early in the game
46  *      to set up the hashtables.  First all buckets in a
47  *      table are cleared.  Then a pass is made through
48  *      the respective symbol lists, linking them into
49  *      their hash buckets.  Finally the base area pointer
50  *      is set to 'dca'.
51  *
52  *      local variables:
53  *              int     h               computed hash value
54  *              mne *   mp              pointer to a mne structure
55  *              mne **  mpp             pointer to an array of
56  *                                      mne structure pointers
57  *              sym *   sp              pointer to a sym structure
58  *              sym **  spp             pointer to an array of
59  *                                      sym structure pointers
60  *
61  *      global variables:
62  *              area    area[]          single elememt area array
63  *              area    dca             defined as area[0]
64  *              mne * mnehash[]         array of pointers to NHASH
65  *                                      linked mnemonic/directive lists
66  *              sym * symhash[]         array of pointers to NHASH
67  *                                      linked symbol lists
68  *
69  *      functions called:
70  *              none
71  *
72  *      side effects:
73  *              (1)     The symbol hash tables are initialized,
74  *                      the only defined symbol is '.'.
75  *              (2)     The mnemonic/directive hash tables are
76  *                      initialized with the assembler directives
77  *                      and mnemonics found in the machine dependent
78  *                      file ___pst.c.
79  *              (3)     The area pointer is initialized to dca (area[0]).
80  */
81
82 VOID
83 syminit()
84 {
85         register struct mne  *mp;
86         struct mne **mpp;
87         register struct sym  *sp;
88         struct sym **spp;
89         register int h;
90
91         mpp = &mnehash[0];
92         while (mpp < &mnehash[NHASH])
93                 *mpp++ = NULL;
94         mp = &mne[0];
95         for (;;) {
96                 h = hash(mp->m_id);
97                 mp->m_mp = mnehash[h];
98                 mnehash[h] = mp;
99                 if (mp->m_flag&S_END)
100                         break;
101                 ++mp;
102         }
103
104         spp = &symhash[0];
105         while (spp < &symhash[NHASH])
106                 *spp++ = NULL;
107         sp = &sym[0];
108         for (;;) {
109                 h = hash(sp->s_id);
110                 sp->s_sp = symhash[h];
111                 symhash[h] = sp;
112                 if (sp->s_flag&S_END)
113                         break;
114                 ++sp;
115         }
116
117         areap = &dca;
118 }
119
120 /*)Function     area *  alookup(id)
121  *
122  *              char *  id              area name string
123  *
124  *      The function alookup() searches the area list for a
125  *      match with id.  If the area is defined then a pointer
126  *      to this area is returned else a NULL is returned.
127  *
128  *      local variables:
129  *              area *  ap              pointer to area structure
130  *
131  *      global variables:
132  *              area *  areap           pointer to an area structure
133  *
134  *      functions called:
135  *              int     symeq()         assym.c
136  *
137  *      side effects:
138  *              none
139  */
140
141 struct area *
142 alookup(id)
143 char *id;
144 {
145         register struct area *ap;
146
147         ap = areap;
148         while (ap) {
149                 if (symeq(id, ap->a_id)) {
150                         return (ap);
151                 }
152                 ap = ap->a_ap;
153         }
154         return(NULL);
155 }
156
157 /*)Function     mne *   mlookup(id)
158  *
159  *              char *  id              mnemonic/directive name string
160  *
161  *      The function mlookup() searches the mnemonic/directive
162  *      hash tables for a match returning a pointer to the
163  *      mne structure else it returns a NULL.
164  *
165  *      local variables:
166  *              mne *   mp              pointer to mne structure
167  *              int     h               calculated hash value
168  *
169  *      global variables:
170  *              mne * mnehash[]         array of pointers to NHASH
171  *                                      linked mnemonic/directive lists
172  *
173  *      functions called:
174  *              none
175  *
176  *      side effects:
177  *              none
178  */
179
180 struct mne *
181 mlookup(id)
182 char *id;
183 {
184         register struct mne *mp;
185         register int h;
186
187         h = hash(id);
188         mp = mnehash[h];
189         while (mp) {
190                 if (symeq(id, mp->m_id))
191                         return (mp);
192                 mp = mp->m_mp;
193         }
194         return (NULL);
195 }
196
197 /*)Function     sym *   lookup(id)
198  *
199  *              char *  id              symbol name string
200  *
201  *      The function lookup() searches the symbol hash tables for
202  *      a symbol name match returning a pointer to the sym structure.
203  *      If the symbol is not found then a sym structure is created,
204  *      initialized, and linked to the appropriate hash table.
205  *      A pointer to this new sym structure is returned.
206  *
207  *      local variables:
208  *              int     h               computed hash value
209  *              sym *   sp              pointer to a sym structure
210  *
211  *      global varaibles:
212  *              sym * symhash[]         array of pointers to NHASH
213  *                                      linked symbol lists
214  *
215  *      functions called:
216  *              int     hash()          assym.c
217  *              VOID *  new()           assym.c
218  *              int     symeq()         assym.c
219  *
220  *      side effects:
221  *              If the function new() fails to allocate space
222  *              for the new sym structure the assembly terminates.
223  */
224
225 struct sym *
226 lookup(id)
227 char *id;
228 {
229         register struct sym *sp;
230         register int h;
231
232         h = hash(id);
233         sp = symhash[h];
234         while (sp) {
235                 if (symeq(id, sp->s_id))
236                         return (sp);
237                 sp = sp->s_sp;
238         }
239         sp = (struct sym *) new (sizeof(struct sym));
240         sp->s_sp = symhash[h];
241         symhash[h] = sp;
242         sp->s_tsym = NULL;
243         strncpy(sp->s_id, id, NCPS);
244         sp->s_type = S_NEW;
245         sp->s_flag = 0;
246         sp->s_area = NULL;
247         sp->s_ref = 0;
248         sp->s_addr = 0;
249         return (sp);
250 }
251
252 /*)Function     VOID    symglob()
253  *
254  *      The function symglob() will mark all symbols of
255  *      type S_NEW as global.  Called at the beginning of pass 1
256  *      if the assembly option -g was specified.
257  *
258  *      local variables:
259  *              sym *   sp              pointer to a sym structure
260  *              int     i               loop index
261  *
262  *      global variables:
263  *              sym * symhash[]         array of pointers to NHASH
264  *                                      linked symbol lists
265  *
266  *      functions called:
267  *              none
268  *
269  *      side effects:
270  *              Symbol types changed.
271  */
272
273 VOID
274 symglob()
275 {
276         register struct sym *sp;
277         register int i;
278
279         for (i=0; i<NHASH; ++i) {
280                 sp = symhash[i];
281                 while (sp != NULL) {
282                         if (sp->s_type == S_NEW)
283                                 sp->s_flag |= S_GBL;
284                         sp = sp->s_sp;
285                 }
286         }
287 }
288
289 /*)Function     VOID    allglob()
290  *
291  *      The function allglob() will mark all symbols of
292  *      type S_USER as global.  Called at the beginning of pass 1
293  *      if the assembly option -a was specified.
294  *
295  *      local variables:
296  *              sym *   sp              pointer to a sym structure
297  *              int     i               loop index
298  *
299  *      global variables:
300  *              sym * symhash[]         array of pointers to NHASH
301  *                                      linked symbol lists
302  *
303  *      functions called:
304  *              none
305  *
306  *      side effects:
307  *              Symbol types changed.
308  */
309
310 VOID
311 allglob()
312 {
313         register struct sym *sp;
314         register int i;
315
316         for (i=0; i<NHASH; ++i) {
317                 sp = symhash[i];
318                 while (sp != NULL) {
319                         if (sp != &dot && sp->s_type == S_USER)
320                                 sp->s_flag |= S_GBL;
321                         sp = sp->s_sp;
322                 }
323         }
324 }
325
326 /*)Function     int     symeq(p1, p2)
327  *
328  *              char *  p1              name string
329  *              char *  p2              name string
330  *
331  *      The function symeq() compares the two name strings for a match.
332  *      The return value is 1 for a match and 0 for no match.
333  *
334  *      local variables:
335  *              int     h               loop counter
336  *
337  *      global variables:
338  *              char    ccase[]         an array of characters which
339  *                                      perform the case translation function
340  *
341  *      functions called:
342  *              none
343  *
344  *      side effects:
345  *              none
346  *
347  */
348
349 int
350 symeq(p1, p2)
351 register char *p1, *p2;
352 {
353         register int n;
354
355         n = NCPS;
356         do {
357
358 #if     CASE_SENSITIVE
359                 if (*p1++ != *p2++)
360                         return (0);
361 #else
362                 if (ccase[(unsigned char)(*p1++)] != ccase[(unsigned char)(*p2++)])
363                         return (0);
364 #endif
365
366         } while (--n);
367         return (1);
368 }
369
370 /*)Function     int     hash(p)
371  *
372  *              char *  p               pointer to string to hash
373  *
374  *      The function hash() computes a hash code using the sum
375  *      of all characters mod table size algorithm.
376  *
377  *      local variables:
378  *              int     h               accumulated character sum
379  *              int     n               loop counter
380  *
381  *      global variables:
382  *              char    ccase[]         an array of characters which
383  *                                      perform the case translation function
384  *
385  *      functions called:
386  *              none
387  *
388  *      side effects:
389  *              none
390  */
391  
392 int
393 hash(p)
394 register char *p;
395 {
396         register int h, n;
397
398         h = 0;
399         n = NCPS;
400         do {
401
402 #if     CASE_SENSITIVE
403                 h += *p++;
404 #else
405                 h += ccase[(unsigned char)(*p++)];
406 #endif
407
408         } while (--n);
409         return (h&HMASK);
410 }
411
412 /*)Function     VOID *  new(n)
413  *
414  *              unsigned int    n       allocation size in bytes
415  *
416  *      The function new() allocates n bytes of space and returns
417  *      a pointer to this memory.  If no space is available the
418  *      assembly is terminated.
419  *
420  *      local variables:
421  *              VOID *  p               a general pointer
422  *
423  *      global variables:
424  *              none
425  *
426  *      functions called:
427  *              VOID    asexit()        asmain.c
428  *              int     fprintf()       c_library
429  *              VOID *  malloc()        c_library
430  *
431  *      side effects:
432  *              Memory is allocated, if allocation fails
433  *              the assembly is terminated.
434  */
435
436 VOID *
437 new(n)
438 unsigned int n;
439 {
440         register VOID *p;
441
442         if ((p = (VOID *) malloc(n)) == NULL) {
443                 fprintf(stderr, "Out of space!\n");
444                 asexit(1);
445         }
446         return (p);
447 }