* as/link/aslink.h,
[fw/sdcc] / as / 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 "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 }
112
113 /*)Function     VOID    module()
114  *
115  *      The function module() copies the module name into
116  *      the current head structure.
117  *
118  *      local variables:
119  *              char    id[]            module id string
120  *
121  *      global variables:
122  *              head    *headp          The pointer to the first
123  *                                      head structure of a linked list
124  *              head    *hp             Pointer to the current
125  *                                      head structure
126  *              int     lkerr           error flag
127  *              FILE *  stderr          c_library
128  *
129  *      functions called:
130  *              int     fprintf()       c_library
131  *              VOID    getid()         lklex.c
132  *              char *  strncpy()       c_library
133  *
134  *      side effects:
135  *              The module name is copied into the head structure.
136  */
137
138 /*
139  * Module Name
140  */
141 VOID
142 module()
143 {
144         char id[NCPS];
145
146         if (headp) {
147                 getid(id, -1);
148                 strncpy(hp->m_id, id, NCPS);
149         } else {
150                 fprintf(stderr, "No header defined\n");
151                 lkerr++;
152         }
153 }