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