b9c5541a8c6fd0f247e1cb81604c4c6676530a26
[fw/sdcc] / as / link / 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 "aslink.h"
15
16 /*Module        lkhead.c
17  *
18  *      The module lkhead.c contains the function newhead() which
19  *      creates a head structure and the function module() which
20  *      loads the module name into the current head structure.
21  *
22  *      lkhead.c contains the following functions:
23  *              VOID    newhead()
24  *              VOID    module()
25  *
26  *      lkhead.c contains no local variables.
27  */
28
29 /*)Function     VOID    newhead()
30  *
31  *      The function newhead() creates a head structure.  All head
32  *      structures are linked to form a linked list of head structures
33  *      with the current head structure at the tail of the list.
34  *
35  *      local variables:
36  *              int     i               evaluation value
37  *              head *  thp             temporary pointer
38  *                                      to a header structure
39  *
40  *      global variables:
41  *              area    *ap             Pointer to the current
42  *                                      area structure
43  *              lfile   *cfp            The pointer *cfp points to the
44  *                                      current lfile structure
45  *              head    *headp          The pointer to the first
46  *                                      head structure of a linked list
47  *              head    *hp             Pointer to the current
48  *                                      head structure
49  *
50  *      functions called:
51  *              Addr_T  expr()          lkeval.c
52  *              VOID *  new()           lksym.c
53  *              VOID    lkparea()       lkarea.c
54  *
55  *      side effects:
56  *              A new head structure is created and linked to any
57  *              existing linked head structure.  The head structure
58  *              parameters of file handle, number of areas, and number
59  *              of global symbols are loaded into the structure.
60  *              The default area "_abs_" is created when the first
61  *              head structure is created and an areax structure is
62  *              created for every head structure called.
63  */
64
65 /*
66  * Create a new header entry.
67  *
68  * H n areas n global symbols
69  *   |       |
70  *   |       `---- hp->h_nglob
71  *   `------------ hp->h_narea
72  *
73  */
74 VOID
75 newhead()
76 {
77         register int i;
78         struct head *thp;
79
80         hp = (struct head *) new (sizeof(struct head));
81         if (headp == NULL) {
82                 headp = hp;
83         } else {
84                 thp = headp;
85                 while (thp->h_hp)
86                         thp = thp->h_hp;
87                 thp->h_hp = hp;
88         }
89         /*
90          * Set file pointer
91          */
92         hp->h_lfile = cfp;
93         /*
94          * Evaluate and build Area pointer list
95          */
96         i = hp->h_narea = eval();
97         if (i)
98                 hp->a_list = (struct areax **) new (i*sizeof(struct areax *));
99         /*
100          * Evaluate and build Global symbol pointer list
101          */
102         skip(-1);
103         i = hp->h_nglob = eval();
104         if (i)
105                 hp->s_list = (struct sym **) new (i*sizeof(struct sym *));
106         /*
107          * Setup Absolute DEF linkage.
108          */
109         lkparea(_abs_);
110         ap->a_flag = A_ABS;
111     axp->a_addr = 0;
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 }