Fixed signed/unsigned compare in newsym
[fw/sdcc] / as / mcs51 / lksym.c
1 /* lksym.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  *           - lkpsym: Use StoreString for sym construction
13  *           - change symeq() to do length-independent string compare
14  *           - change hash() to do length-independent hash calculation
15  */
16
17 #include <stdio.h>
18 #include <string.h>
19 #if defined(_MSC_VER)
20 #include <malloc.h>
21 #else
22 #include <alloc.h>
23 #endif
24 #include "aslink.h"
25
26 /*)Module       lksym.c
27  *
28  *      The module lksym.c contains the functions that operate
29  *      on the symbol structures.
30  *
31  *      lksym.c contains the following functions:
32  *              int     hash()
33  *              sym *   lkpsym()
34  *              VOID *  new()
35  *              sym *   newsym()
36  *              VOID    symdef()
37  *              int     symeq()
38  *              VOID    syminit()
39  *              VOID    symmod()
40  *              Addr_T  symval()
41  *
42  *      lksym.c contains no local/static variables.
43  */
44
45 /*)Function     VOID    syminit()
46  *
47  *      The function syminit() is called to clear the hashtable.
48  *
49  *      local variables:
50  *              int     h               computed hash value
51  *              sym **  spp             pointer to an array of
52  *                                      sym structure pointers
53  *
54  *      global variables:
55  *              sym * symhash[]         array of pointers to NHASH
56  *                                      linked symbol lists
57  *
58  *      functions called:
59  *              none
60  *
61  *      side effects:
62  *              (1)     The symbol hash tables are cleared
63  */
64
65 VOID
66 syminit()
67 {
68     //  register int h;
69         struct sym **spp;
70
71         spp = &symhash[0];
72         while (spp < &symhash[NHASH])
73                 *spp++ = NULL;
74 }
75
76 /*)Function     sym *   newsym()
77  *
78  *      The function newsym() is called to evaluate the symbol
79  *      definition/reference directive from the .rel file(s).
80  *      If the symbol is not found in the symbol table a new
81  *      symbol structure is created.  Evaluation of the
82  *      directive determines if this is a reference or a definition.
83  *      Multiple definitions of the same variable will be flagged
84  *      as an error if the values are not identical.  A symbol
85  *      definition places the symbol value and area extension
86  *      into the symbols data structure.  And finally, a pointer
87  *      to the symbol structure is placed into the head structure
88  *      symbol list.  Refer to the description of the header, symbol,
89  *      area, and areax structures in lkdata.c for structure and
90  *      linkage details.
91  *
92  *      local variables:
93  *              int     c               character from input text
94  *              int     i               evaluation value
95  *              char    id[]            symbol name
96  *              int     nglob           number of symbols in this header
97  *              sym *   tsp             pointer to symbol structure
98  *              sym **  s               list of pointers to symbol structures
99  *
100  *      global variables:
101  *              areax   *axp            Pointer to the current
102  *                                      areax structure
103  *              head    *headp          The pointer to the first
104  *                                      head structure of a linked list
105  *              int     lkerr           error flag
106  *
107  *      functions called:
108  *              Addr_T  eval()          lkeval.c
109  *              VOID    exit()          c_library
110  *              int     fprintf()       c_library
111  *              char    get()           lklex.c
112  *              char    getnb()         lklex.c
113  *              sym *   lkpsym()        lksym.c
114  *
115  *      side effects:
116  *              A symbol structure is created and/or modified.
117  *              If structure space allocation fails linker will abort.
118  *              Several severe errors (these are internal errors
119  *              indicating a corrupted .rel file or corrupted
120  *              assembler or linker) will terminated the linker.
121  */
122
123 /*
124  * Find/Create a global symbol entry.
125  *
126  * S xxxxxx Defnnnn
127  *   |      |  |
128  *   |      |  `-- sp->s_addr
129  *   |      `----- sp->s_type
130  *   `------------ sp->s_id
131  *
132  */
133 struct sym *
134 newsym()
135 {
136   register unsigned i ;
137   register unsigned nglob ;
138         register int c ;
139         struct sym *tsp;
140         struct sym **s;
141         char id[NCPS];
142
143         getid(id, -1);
144         tsp = lkpsym(id, 1);
145         c = getnb();get();get();
146         if (c == 'R') {
147                 tsp->s_type |= S_REF;
148                 if (eval()) {
149                         fprintf(stderr, "Non zero S_REF\n");
150                         lkerr++;
151                 }
152         } else
153         if (c == 'D') {
154                 i = eval();
155                 if (tsp->s_type & S_DEF && tsp->s_addr != i) {
156                         fprintf(stderr, "Multiple definition of %8s\n", id);
157                         lkerr++;
158                 }
159                 tsp->s_type |= S_DEF;
160                 /*
161                  * Set value and area extension link.
162                  */
163                 tsp->s_addr = i;
164                 tsp->s_axp = axp;
165         } else {
166                 fprintf(stderr, "Invalid symbol type %c for %8s\n", c, id);
167                 lkexit(1);
168         }
169         /*
170          * Place pointer in header symbol list
171          */
172         if (headp == NULL) {
173                 fprintf(stderr, "No header defined\n");
174                 lkexit(1);
175         }
176         nglob = hp->h_nglob;
177         s = hp->s_list;
178         for (i=0; i < nglob ;++i) {
179                 if (s[i] == NULL) {
180                         s[i] = tsp;
181                         return(tsp);
182                 }
183         }
184         fprintf(stderr, "Header symbol list overflow\n");
185         lkexit(1);
186         return(0);
187 }
188
189 /*)Function     sym *   lkpsym(id,f)
190  *
191  *              char *  id              symbol name string
192  *              int     f               f == 0, lookup only
193  *                                      f != 0, create if not found
194  *
195  *      The function lookup() searches the symbol hash tables for
196  *      a symbol name match returning a pointer to the sym structure.
197  *      If the symbol is not found then a sym structure is created,
198  *      initialized, and linked to the appropriate hash table if f != 0.
199  *      A pointer to this new sym structure is returned or a NULL
200  *      pointer is returned if f == 0.
201  *
202  *      local variables:
203  *              int     h               computed hash value
204  *              sym *   sp              pointer to a sym structure
205  *
206  *      global varaibles:
207  *              sym * symhash[]         array of pointers to NHASH
208  *                                      linked symbol lists
209  *
210  *      functions called:
211  *              int     hash()          lksym.c
212  *              VOID *  new()           lksym.c
213  *              int     symeq()         lksym.c
214  *
215  *      side effects:
216  *              If the function new() fails to allocate space
217  *              for the new sym structure the linker terminates.
218  */
219
220 struct sym *
221 lkpsym(id, f)
222 char *id;
223 {
224         register struct sym *sp;
225         register int h;
226
227         h = hash(id);
228         sp = symhash[h];
229         while (sp != NULL) {
230                 if (symeq(id, sp->s_id))
231                         return (sp);
232                 sp = sp->s_sp;
233         }
234         if (f == 0)
235                 return (NULL);
236         sp = (struct sym *) new (sizeof(struct sym));
237         sp->s_sp = symhash[h];
238         symhash[h] = sp;
239         sp->s_id = StoreString( id );   /* JLH */
240         return (sp);
241 }
242
243 /*)Function     Addr_T  symval(tsp)
244  *
245  *              sym *   tsp             pointer to a symbol structure
246  *
247  *      The function symval() returns the value of the
248  *      relocated symbol by adding the variable definition
249  *      value to the areax base address.
250  *
251  *      local variables:
252  *              Addr_T  val             relocated address value
253  *
254  *      global variables:
255  *              none
256  *
257  *      functions called:
258  *              none
259  *
260  *      side effects:
261  *              none
262  */
263
264 Addr_T
265 symval(tsp)
266 register struct sym *tsp;
267 {
268         register Addr_T val;
269
270         val = tsp->s_addr;
271         if (tsp->s_axp) {
272                 val += tsp->s_axp->a_addr;
273         }
274         return(val);
275 }
276
277 /*)Function     VOID    symdef(fp)
278  *
279  *              FILE *  fp              file handle for output
280  *
281  *      The function symdef() scans the hashed symbol table
282  *      searching for variables referenced but not defined.
283  *      Undefined variables are linked to the default
284  *      area "_CODE" and reported as referenced by the
285  *      appropriate module.
286  *
287  *      local variables:
288  *              int     i               hash table index loop variable
289  *              sym *   sp              pointer to linked symbol structure
290  *
291  *      global variables:
292  *              area    *areap          The pointer to the first
293  *                                      area structure of a linked list
294  *              sym *symhash[NHASH]     array of pointers to NHASH
295  *                                      linked symbol lists
296  *
297  *      functions called:
298  *              symmod()                lksym.c
299  *
300  *      side effects:
301  *              Undefined variables have their areas set to "_CODE".
302  */
303
304 VOID
305 symdef(fp)
306 FILE *fp;
307 {
308         register struct sym *sp;
309         register int i;
310
311         for (i=0; i<NHASH; ++i) {
312                 sp = symhash[i];
313                 while (sp) {
314                         if (sp->s_axp == NULL)
315                                 sp->s_axp = areap->a_axp;
316                         if ((sp->s_type & S_DEF) == 0)
317                                 symmod(fp, sp);
318                         sp = sp->s_sp;
319                 }
320         }
321 }
322
323 /*)Function     VOID    symmod(fp,tsp)
324  *
325  *              FILE *  fp              output file handle
326  *              sym *   tsp             pointer to a symbol structure
327  *
328  *      The function symmod() scans the header structures
329  *      searching for a reference to the symbol structure
330  *      pointer to by tsp.  The function then generates an error
331  *      message whichs names the module having referenced the
332  *      undefined variable.
333  *
334  *      local variables:
335  *              int     i               loop counter
336  *              sym **  p               pointer to a list of pointers
337  *                                      to symbol structures
338  *
339  *      global variables:
340  *              head    *headp          The pointer to the first
341  *                                      head structure of a linked list
342  *              head    *hp             Pointer to the current
343  *                                      head structure
344  *              int     lkerr           error flag
345  *
346  *      functions called:
347  *              int     fprintf()       c_library
348  *
349  *      side effects:
350  *              Error output generated.
351  */
352
353 VOID
354 symmod(fp, tsp)
355 FILE *fp;
356 struct sym *tsp;
357 {
358         register int i;
359         struct sym **p;
360
361         if ((hp = headp) != NULL) {
362             while(hp) {
363                 p = hp->s_list;
364                 for (i=0; i<hp->h_nglob; ++i) {
365                     if (p[i] == tsp) {
366                         fprintf(fp, "\n?ASlink-Warning-Undefined Global '%s' ", tsp->s_id);
367                         fprintf(fp, "referenced by module '%s'\n", hp->m_id);
368                         lkerr++;
369                     }
370                 }
371             hp = hp->h_hp;
372             }
373         }
374 }
375
376 /*)Function     int     symeq(p1, p2)
377  *
378  *              char *  p1              name string
379  *              char *  p2              name string
380  *
381  *      The function symeq() compares the two name strings for a match.
382  *      The return value is 1 for a match and 0 for no match.
383  *
384  *      local variables:
385  *              int     h               loop counter
386  *
387  *      global variables:
388  *              char    ccase[]         an array of characters which
389  *                                      perform the case translation function
390  *
391  *      functions called:
392  *              none
393  *
394  *      side effects:
395  *              none
396  *
397  */
398
399 int
400 symeq(p1, p2)
401 register char *p1, *p2;
402 {
403 #if     CASE_SENSITIVE
404                 return (strcmp( p1, p2 ) == 0);
405 #else
406                 return (strcmpi( p1, p2 ) == 0);
407 #endif
408 }
409
410 /*)Function     int     hash(p)
411  *
412  *              char *  p               pointer to string to hash
413  *
414  *      The function hash() computes a hash code using the sum
415  *      of all characters mod table size algorithm.
416  *
417  *      local variables:
418  *              int     h               accumulated character sum
419  *              int     n               loop counter
420  *
421  *      global variables:
422  *              char    ccase[]         an array of characters which
423  *                                      perform the case translation function
424  *
425  *      functions called:
426  *              none
427  *
428  *      side effects:
429  *              none
430  *
431  */
432  
433 int
434 hash(p)
435 register char *p;
436 {
437         register int h;
438
439         h = 0;
440         while (*p) {
441
442 #if     CASE_SENSITIVE
443                 h += *p++;
444 #else
445                 h += ccase[*p++];
446 #endif
447
448         };
449         return (h&HMASK);
450 }
451
452 /*)Function     VOID *  new(n)
453  *
454  *              unsigned int    n       allocation size in bytes
455  *
456  *      The function new() allocates n bytes of space and returns
457  *      a pointer to this memory.  If no space is available the
458  *      linker is terminated.
459  *
460  *      local variables:
461  *              char *  p               a general pointer
462  *              char *  q               a general pointer
463  *
464  *      global variables:
465  *              none
466  *
467  *      functions called:
468  *              int     fprintf()       c_library
469  *              VOID *  malloc()        c_library
470  *
471  *      side effects:
472  *              Memory is allocated, if allocation fails
473  *              the linker is terminated.
474  */
475
476 VOID *
477 new(n)
478 unsigned int n;
479 {
480         register char *p,*q;
481         register unsigned int i;
482
483         if ((p = (char *) malloc(n)) == NULL) {
484                 fprintf(stderr, "Out of space!\n");
485                 lkexit(1);
486         }
487         for (i=0,q=p; i<n; i++) {
488                 *q++ = 0;
489         }
490         return (p);
491 }