* src/pic16/genarith.c (pic16_genPlus, pic16_genMinus): take special
[fw/sdcc] / as / hc08 / 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  * 28-Oct-97 JLH:
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
15  * 29-Oct-97 JLH:
16  *           - make mnemonics case insensitive ALWAYS
17  *           - make hash() case-insensitive always
18  *           - replace symeq() call in mlookup with strcmpi
19  */
20
21 #include <stdio.h>
22 #include <setjmp.h>
23 #include <string.h>
24 #include <stdlib.h>
25 #include "asm.h"
26
27 /*)Module       assym.c
28  *
29  *      The module assym.c contains the functions that operate
30  *      on the mnemonic/directive and symbol structures.
31  *
32  *      assym.c contains the following functions:
33  *              VOID    allglob()
34  *              area *  alookup()
35  *              int     hash()
36  *              sym *   lookup()
37  *              mne *   mlookup()
38  *              VOID *  new()
39  *              int     symeq()
40  *              VOID    syminit()
41  *              VOID    symglob()
42  *
43  *      assym.c contains no local/static variables.
44  */
45
46 /*)Function     VOID    syminit()
47  *
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
53  *      is set to 'dca'.
54  *
55  *      local variables:
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
63  *
64  *      global variables:
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
70  *                                      linked symbol lists
71  *
72  *      functions called:
73  *              none
74  *
75  *      side effects:
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
81  *                      file ___pst.c.
82  *              (3)     The area pointer is initialized to dca (area[0]).
83  */
84
85 VOID
86 syminit()
87 {
88         register struct mne  *mp;
89         struct mne **mpp;
90         register struct sym  *sp;
91         struct sym **spp;
92         register int h;
93
94         mpp = &mnehash[0];
95         while (mpp < &mnehash[NHASH])
96                 *mpp++ = NULL;
97         mp = &mne[0];
98         for (;;) {
99                 h = hash(mp->m_id);
100                 mp->m_mp = mnehash[h];
101                 mnehash[h] = mp;
102                 if (mp->m_flag&S_END)
103                         break;
104                 ++mp;
105         }
106
107         spp = &symhash[0];
108         while (spp < &symhash[NHASH])
109                 *spp++ = NULL;
110         sp = &sym[0];
111         for (;;) {
112                 h = hash(sp->s_id);
113                 sp->s_sp = symhash[h];
114                 symhash[h] = sp;
115                 if (sp->s_flag&S_END)
116                         break;
117                 ++sp;
118         }
119
120         areap = &dca;
121 }
122
123 /*)Function     area *  alookup(id)
124  *
125  *              char *  id              area name string
126  *
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.
130  *
131  *      local variables:
132  *              area *  ap              pointer to area structure
133  *
134  *      global variables:
135  *              area *  areap           pointer to an area structure
136  *
137  *      functions called:
138  *              int     symeq()         assym.c
139  *
140  *      side effects:
141  *              none
142  */
143
144 struct area *
145 alookup(id)
146 char *id;
147 {
148         register struct area *ap;
149
150         ap = areap;
151         while (ap) {
152                 if (symeq(id, ap->a_id)) {
153                         return (ap);
154                 }
155                 ap = ap->a_ap;
156         }
157         return(NULL);
158 }
159
160 /*)Function     mne *   mlookup(id)
161  *
162  *              char *  id              mnemonic/directive name string
163  *
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.
167  *
168  *      local variables:
169  *              mne *   mp              pointer to mne structure
170  *              int     h               calculated hash value
171  *
172  *      global variables:
173  *              mne * mnehash[]         array of pointers to NHASH
174  *                                      linked mnemonic/directive lists
175  *
176  *      functions called:
177  *              none
178  *
179  *      side effects:
180  *              none
181  */
182
183 struct mne *
184 mlookup(id)
185 char *id;
186 {
187         register struct mne *mp;
188         register int h;
189
190         h = hash(id);
191         mp = mnehash[h];
192         while (mp) {
193                 if (as_strcmpi(id, mp->m_id) == 0)      /* JLH: case insensitive */
194                         return (mp);
195                 mp = mp->m_mp;
196         }
197         return (NULL);
198 }
199
200 /*)Function     sym *   lookup(id)
201  *
202  *              char *  id              symbol name string
203  *
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.
209  *
210  *      local variables:
211  *              int     h               computed hash value
212  *              sym *   sp              pointer to a sym structure
213  *
214  *      global varaibles:
215  *              sym * symhash[]         array of pointers to NHASH
216  *                                      linked symbol lists
217  *
218  *      functions called:
219  *              int     hash()          assym.c
220  *              VOID *  new()           assym.c
221  *              int     symeq()         assym.c
222  *
223  *      side effects:
224  *              If the function new() fails to allocate space
225  *              for the new sym structure the assembly terminates.
226  */
227
228 struct sym *
229 lookup(id)
230 char *id;
231 {
232         register struct sym *sp;
233         register int h;
234
235         h = hash(id);
236         sp = symhash[h];
237         while (sp) {
238                 if (symeq(id, sp->s_id))
239                         return (sp);
240                 sp = sp->s_sp;
241         }
242         sp = (struct sym *) new (sizeof(struct sym));
243         sp->s_sp = symhash[h];
244         symhash[h] = sp;
245         sp->s_tsym = NULL;
246         sp->s_id = StoreString( id ); /* JLH */
247         sp->s_type = S_NEW;
248         sp->s_flag = 0;
249         sp->s_area = NULL;
250         sp->s_ref = 0;
251         sp->s_addr = 0;
252         return (sp);
253 }
254
255 /*)Function     VOID    symglob()
256  *
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.
260  *
261  *      local variables:
262  *              sym *   sp              pointer to a sym structure
263  *              int     i               loop index
264  *
265  *      global variables:
266  *              sym * symhash[]         array of pointers to NHASH
267  *                                      linked symbol lists
268  *
269  *      functions called:
270  *              none
271  *
272  *      side effects:
273  *              Symbol types changed.
274  */
275
276 VOID
277 symglob()
278 {
279         register struct sym *sp;
280         register int i;
281
282         for (i=0; i<NHASH; ++i) {
283                 sp = symhash[i];
284                 while (sp != NULL) {
285                         if (sp->s_type == S_NEW)
286                                 sp->s_flag |= S_GBL;
287                         sp = sp->s_sp;
288                 }
289         }
290 }
291
292 /*)Function     VOID    allglob()
293  *
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.
297  *
298  *      local variables:
299  *              sym *   sp              pointer to a sym structure
300  *              int     i               loop index
301  *
302  *      global variables:
303  *              sym * symhash[]         array of pointers to NHASH
304  *                                      linked symbol lists
305  *
306  *      functions called:
307  *              none
308  *
309  *      side effects:
310  *              Symbol types changed.
311  */
312
313 VOID
314 allglob()
315 {
316         register struct sym *sp;
317         register int i;
318
319         for (i=0; i<NHASH; ++i) {
320                 sp = symhash[i];
321                 while (sp != NULL) {
322                         if (sp != &dot && sp->s_type == S_USER)
323                                 sp->s_flag |= S_GBL;
324                         sp = sp->s_sp;
325                 }
326         }
327 }
328
329 /*)Function     int     symeq(p1, p2)
330  *
331  *              char *  p1              name string
332  *              char *  p2              name string
333  *
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.
336  *
337  *      local variables:
338  *              int     h               loop counter
339  *
340  *      global variables:
341  *              char    ccase[]         an array of characters which
342  *                                      perform the case translation function
343  *
344  *      functions called:
345  *              none
346  *
347  *      side effects:
348  *              none
349  *
350  */
351
352 int
353 symeq(p1, p2)
354 register char *p1, *p2;
355 {
356 #if     CASE_SENSITIVE
357                 return (strcmp( p1, p2 ) == 0);
358 #else
359                 return (as_strcmpi( p1, p2 ) == 0);
360 #endif
361 }
362
363 /*)Function     int     hash(p)
364  *
365  *              char *  p               pointer to string to hash
366  *
367  *      The function hash() computes a hash code using the sum
368  *      of all characters mod table size algorithm.
369  *
370  *      local variables:
371  *              int     h               accumulated character sum
372  *              int     n               loop counter
373  *
374  *      global variables:
375  *              char    ccase[]         an array of characters which
376  *                                      perform the case translation function
377  *
378  *      functions called:
379  *              none
380  *
381  *      side effects:
382  *              none
383  */
384
385 int
386 hash(p)
387 register char *p;
388 {
389         register int h;
390
391         h = 0;
392         while (*p) {
393                 /* JLH: case insensitive hash:  Doesn't much affect
394                  * hashing, and allows same function for mnemonics and symbols
395                  */
396                 h += ccase[(int)*p++];
397         }
398         return (h&HMASK);
399 }
400
401 /*)Function     VOID *  new(n)
402  *
403  *              unsigned int    n       allocation size in bytes
404  *
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.
408  *
409  *      local variables:
410  *              VOID *  p               a general pointer
411  *
412  *      global variables:
413  *              none
414  *
415  *      functions called:
416  *              VOID    asexit()        asmain.c
417  *              int     fprintf()       c_library
418  *              VOID *  malloc()        c_library
419  *
420  *      side effects:
421  *              Memory is allocated, if allocation fails
422  *              the assembly is terminated.
423  */
424
425 VOID *
426 new(n)
427 unsigned int n;
428 {
429         register VOID *p;
430
431         if ((p = (VOID *) malloc(n)) == NULL) {
432                 fprintf(stderr, "Out of space!\n");
433                 asexit(1);
434         }
435         return (p);
436 }