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