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