Initial revision
[fw/sdcc] / link / z80 / lkhead.c
1 /* lkhead.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 <alloc.h>
15 #include "aslink.h"
16
17 /*Module        lkhead.c
18  *
19  *      The module lkhead.c contains the function newhead() which
20  *      creates a head structure and the function module() which
21  *      loads the module name into the current head structure.
22  *
23  *      lkhead.c contains the following functions:
24  *              VOID    newhead()
25  *              VOID    module()
26  *
27  *      lkhead.c contains no local variables.
28  */
29
30 /*)Function     VOID    newhead()
31  *
32  *      The function newhead() creates a head structure.  All head
33  *      structures are linked to form a linked list of head structures
34  *      with the current head structure at the tail of the list.
35  *
36  *      local variables:
37  *              int     i               evaluation value
38  *              head *  thp             temporary pointer
39  *                                      to a header structure
40  *
41  *      global variables:
42  *              area    *ap             Pointer to the current
43  *                                      area structure
44  *              lfile   *cfp            The pointer *cfp points to the
45  *                                      current lfile structure
46  *              head    *headp          The pointer to the first
47  *                                      head structure of a linked list
48  *              head    *hp             Pointer to the current
49  *                                      head structure
50  *
51  *      functions called:
52  *              addr_t  expr()          lkeval.c
53  *              VOID *  new()           lksym.c
54  *              VOID    lkparea()       lkarea.c
55  *
56  *      side effects:
57  *              A new head structure is created and linked to any
58  *              existing linked head structure.  The head structure
59  *              parameters of file handle, number of areas, and number
60  *              of global symbols are loaded into the structure.
61  *              The default area "_abs_" is created when the first
62  *              head structure is created and an areax structure is
63  *              created for every head structure called.
64  */
65
66 /*
67  * Create a new header entry.
68  *
69  * H n areas n global symbols
70  *   |       |
71  *   |       `---- hp->h_nglob
72  *   `------------ hp->h_narea
73  *
74  */
75 VOID
76 newhead()
77 {
78         register i;
79         struct head *thp;
80
81         hp = (struct head *) new (sizeof(struct head));
82         if (headp == NULL) {
83                 headp = hp;
84         } else {
85                 thp = headp;
86                 while (thp->h_hp)
87                         thp = thp->h_hp;
88                 thp->h_hp = hp;
89         }
90         /*
91          * Set file pointer
92          */
93         hp->h_lfile = cfp;
94         /*
95          * Evaluate and build Area pointer list
96          */
97         i = hp->h_narea = eval();
98         if (i)
99                 hp->a_list = (struct areax **) new (i*sizeof(struct areax *));
100         /*
101          * Evaluate and build Global symbol pointer list
102          */
103         skip(-1);
104         i = hp->h_nglob = eval();
105         if (i)
106                 hp->s_list = (struct sym **) new (i*sizeof(struct sym *));
107         /*
108          * Setup Absolute DEF linkage.
109          */
110         lkparea(_abs_);
111         ap->a_flag = A_ABS|A_OVR;
112 }
113
114 /*)Function     VOID    module()
115  *
116  *      The function module() copies the module name into
117  *      the current head structure.
118  *
119  *      local variables:
120  *              char    id[]            module id string
121  *
122  *      global variables:
123  *              head    *headp          The pointer to the first
124  *                                      head structure of a linked list
125  *              head    *hp             Pointer to the current
126  *                                      head structure
127  *              int     lkerr           error flag
128  *              FILE *  stderr          c_library
129  *
130  *      functions called:
131  *              int     fprintf()       c_library
132  *              VOID    getid()         lklex.c
133  *              char *  strncpy()       c_library
134  *
135  *      side effects:
136  *              The module name is copied into the head structure.
137  */
138
139 /*
140  * Module Name
141  */
142 VOID
143 module()
144 {
145         char id[NCPS];
146
147         if (headp) {
148                 getid(id, -1);
149                 strncpy(hp->m_id, id, NCPS);
150         } else {
151                 fprintf(stderr, "No header defined\n");
152                 lkerr++;
153         }
154 }