* asranlib/asranlib.c, link/lkar.h, link/lkar.c:
[fw/sdcc] / as / link / lkhead.c
1 /* lkhead.c
2
3    Copyright (C) 1989-1995 Alan R. Baldwin
4    721 Berkeley St., Kent, Ohio 44240
5
6 This program is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 3, or (at your option) any
9 later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program.  If not, see <http://www.gnu.org/licenses/>. */
18
19 #include <stdio.h>
20 #include <string.h>
21 #include "aslink.h"
22
23 /*Module        lkhead.c
24  *
25  *      The module lkhead.c contains the function newhead() which
26  *      creates a head structure and the function module() which
27  *      loads the module name into the current head structure.
28  *
29  *      lkhead.c contains the following functions:
30  *              VOID    newhead()
31  *              VOID    module()
32  *
33  *      lkhead.c contains no local variables.
34  */
35
36 /*)Function     VOID    newhead()
37  *
38  *      The function newhead() creates a head structure.  All head
39  *      structures are linked to form a linked list of head structures
40  *      with the current head structure at the tail of the list.
41  *
42  *      local variables:
43  *              int     i               evaluation value
44  *              head *  thp             temporary pointer
45  *                                      to a header structure
46  *
47  *      global variables:
48  *              area    *ap             Pointer to the current
49  *                                      area structure
50  *              lfile   *cfp            The pointer *cfp points to the
51  *                                      current lfile structure
52  *              head    *headp          The pointer to the first
53  *                                      head structure of a linked list
54  *              head    *hp             Pointer to the current
55  *                                      head structure
56  *
57  *      functions called:
58  *              Addr_T  expr()          lkeval.c
59  *              VOID *  new()           lksym.c
60  *              VOID    lkparea()       lkarea.c
61  *
62  *      side effects:
63  *              A new head structure is created and linked to any
64  *              existing linked head structure.  The head structure
65  *              parameters of file handle, number of areas, and number
66  *              of global symbols are loaded into the structure.
67  *              The default area "_abs_" is created when the first
68  *              head structure is created and an areax structure is
69  *              created for every head structure called.
70  */
71
72 /*
73  * Create a new header entry.
74  *
75  * H n areas n global symbols
76  *   |       |
77  *   |       `---- hp->h_nglob
78  *   `------------ hp->h_narea
79  *
80  */
81 VOID
82 newhead()
83 {
84         register int i;
85         struct head *thp;
86
87         hp = (struct head *) new (sizeof(struct head));
88         if (headp == NULL) {
89                 headp = hp;
90         } else {
91                 thp = headp;
92                 while (thp->h_hp)
93                         thp = thp->h_hp;
94                 thp->h_hp = hp;
95         }
96         /*
97          * Set file pointer
98          */
99         hp->h_lfile = cfp;
100         /*
101          * Evaluate and build Area pointer list
102          */
103         i = hp->h_narea = eval();
104         if (i)
105                 hp->a_list = (struct areax **) new (i*sizeof(struct areax *));
106         /*
107          * Evaluate and build Global symbol pointer list
108          */
109         skip(-1);
110         i = hp->h_nglob = eval();
111         if (i)
112                 hp->s_list = (struct sym **) new (i*sizeof(struct sym *));
113         /*
114          * Setup Absolute DEF linkage.
115          */
116         lkparea(_abs_);
117         ap->a_flag = A_ABS;
118     axp->a_addr = 0;
119 }
120
121 /*)Function     VOID    module()
122  *
123  *      The function module() copies the module name into
124  *      the current head structure.
125  *
126  *      local variables:
127  *              char    id[]            module id string
128  *
129  *      global variables:
130  *              head    *headp          The pointer to the first
131  *                                      head structure of a linked list
132  *              head    *hp             Pointer to the current
133  *                                      head structure
134  *              int     lkerr           error flag
135  *              FILE *  stderr          c_library
136  *
137  *      functions called:
138  *              int     fprintf()       c_library
139  *              VOID    getid()         lklex.c
140  *              char *  strncpy()       c_library
141  *
142  *      side effects:
143  *              The module name is copied into the head structure.
144  */
145
146 /*
147  * Module Name
148  */
149 VOID
150 module()
151 {
152         char id[NCPS];
153
154         if (headp) {
155                 getid(id, -1);
156                 strncpy(hp->m_id, id, NCPS);
157         } else {
158                 fprintf(stderr, "No header defined\n");
159                 lkerr++;
160         }
161 }