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