7323c45cfce6326253ff2c772ec85793b44da9b1
[platform/upstream/nasm.git] / labels.c
1 /* labels.c  label handling for the Netwide Assembler
2  *
3  * The Netwide Assembler is copyright (C) 1996 Simon Tatham and
4  * Julian Hall. All rights reserved. The software is
5  * redistributable under the licence given in the file "Licence"
6  * distributed in the NASM archive.
7  */
8
9 #include <stdio.h>
10 #include <string.h>
11 #include <stdlib.h>
12 #include "nasm.h"
13 #include "nasmlib.h"
14
15 /*
16  * A local label is one that begins with exactly one period. Things
17  * that begin with _two_ periods are NASM-specific things.
18  */
19 #define islocal(l) ((l)[0] == '.' && (l)[1] != '.')
20
21 #define LABEL_BLOCK  320               /* no. of labels/block */
22 #define LBLK_SIZE    (LABEL_BLOCK*sizeof(union label))
23 #define LABEL_HASHES 32                /* no. of hash table entries */
24
25 #define END_LIST -3                    /* don't clash with NO_SEG! */
26 #define END_BLOCK -2
27 #define BOGUS_VALUE -4
28
29 #define PERMTS_SIZE  4096              /* size of text blocks */
30
31 /* values for label.defn.is_global */
32 #define NOT_DEFINED_YET 0
33 #define LOCAL_SYMBOL 1
34 #define GLOBAL_SYMBOL 2
35 #define GLOBAL_PLACEHOLDER 3
36
37 union label {                          /* actual label structures */
38     struct {
39         long segment, offset;
40         char *label;
41         int is_global;
42     } defn;
43     struct {
44         long movingon, dummy;
45         union label *next;
46     } admin;
47 };
48
49 struct permts {                        /* permanent text storage */
50     struct permts *next;               /* for the linked list */
51     int size, usage;                   /* size and used space in ... */
52     char data[PERMTS_SIZE];            /* ... the data block itself */
53 };
54
55 static union label *ltab[LABEL_HASHES];/* using a hash table */
56 static union label *lfree[LABEL_HASHES];/* pointer into the above */
57 static struct permts *perm_head;      /* start of perm. text storage */
58 static struct permts *perm_tail;      /* end of perm. text storage */
59
60 static void init_block (union label *blk);
61 static char *perm_copy (char *string1, char *string2);
62
63 static char *prevlabel;
64
65 /*
66  * Internal routine: finds the `union label' corresponding to the
67  * given label name. Creates a new one, if it isn't found, and if
68  * `create' is TRUE.
69  */
70 static union label *find_label (char *label, int create) {
71     int hash = 0;
72     char *p, *prev;
73     int prevlen;
74     union label *lptr;
75
76     if (islocal(label))
77         prev = prevlabel;
78     else
79         prev = "";
80     prevlen = strlen(prev);
81     p = prev;
82     while (*p) hash += *p++;
83     p = label;
84     while (*p) hash += *p++;
85     hash %= LABEL_HASHES;
86     lptr = ltab[hash];
87     while (lptr->admin.movingon != END_LIST) {
88         if (lptr->admin.movingon == END_BLOCK) {
89             lptr = lptr->admin.next;
90             if (!lptr)
91                 break;
92         }
93         if (!strncmp(lptr->defn.label, prev, prevlen) &&
94             !strcmp(lptr->defn.label+prevlen, label))
95             return lptr;
96         lptr++;
97     }
98     if (create) {
99         if (lfree[hash]->admin.movingon == END_BLOCK) {
100             /*
101              * must allocate a new block
102              */
103             lfree[hash]->admin.next = (union label *) nasm_malloc (LBLK_SIZE);
104             lfree[hash] = lfree[hash]->admin.next;
105             init_block(lfree[hash]);
106         }
107
108         lfree[hash]->admin.movingon = BOGUS_VALUE;
109         lfree[hash]->defn.label = perm_copy (prev, label);
110         lfree[hash]->defn.is_global = NOT_DEFINED_YET;
111         return lfree[hash]++;
112     } else
113         return NULL;
114 }
115
116 int lookup_label (char *label, long *segment, long *offset) {
117     union label *lptr;
118
119     lptr = find_label (label, 0);
120     if (lptr && (lptr->defn.is_global == LOCAL_SYMBOL ||
121                  lptr->defn.is_global == GLOBAL_SYMBOL)) {
122         *segment = lptr->defn.segment;
123         *offset = lptr->defn.offset;
124         return 1;
125     } else
126         return 0;
127 }
128
129 void define_label_stub (char *label, efunc error) {
130     union label *lptr;
131
132     if (!islocal(label)) {
133         lptr = find_label (label, 1);
134         if (!lptr)
135             error (ERR_PANIC, "can't find label `%s' on pass two", label);
136         prevlabel = lptr->defn.label;
137     }
138 }
139
140 void define_label (char *label, long segment, long offset,
141                    struct ofmt *ofmt, efunc error) {
142     union label *lptr;
143
144     lptr = find_label (label, 1);
145     switch (lptr->defn.is_global) {
146       case NOT_DEFINED_YET:
147         lptr->defn.is_global = LOCAL_SYMBOL;
148         break;
149       case GLOBAL_PLACEHOLDER:
150         lptr->defn.is_global = GLOBAL_SYMBOL;
151         break;
152       default:
153         error(ERR_NONFATAL, "symbol `%s' redefined", label);
154         return;
155     }
156
157     if (label[0] != '.')               /* not local, but not special either */
158         prevlabel = lptr->defn.label;
159     else if (!*prevlabel)
160         error(ERR_NONFATAL, "attempt to define a local label before any"
161               " non-local labels");
162
163     lptr->defn.segment = segment;
164     lptr->defn.offset = offset;
165
166     ofmt->symdef (lptr->defn.label, segment, offset,
167                   lptr->defn.is_global == GLOBAL_SYMBOL);
168 }
169
170 void define_common (char *label, long segment, long size,
171                     struct ofmt *ofmt, efunc error) {
172     union label *lptr;
173
174     lptr = find_label (label, 1);
175     switch (lptr->defn.is_global) {
176       case NOT_DEFINED_YET:
177         lptr->defn.is_global = LOCAL_SYMBOL;
178         break;
179       case GLOBAL_PLACEHOLDER:
180         lptr->defn.is_global = GLOBAL_SYMBOL;
181         break;
182       default:
183         error(ERR_NONFATAL, "symbol `%s' redefined", label);
184         return;
185     }
186
187     if (label[0] != '.')               /* not local, but not special either */
188         prevlabel = lptr->defn.label;
189     else
190         error(ERR_NONFATAL, "attempt to define a local label as a "
191               "common variable");
192
193     lptr->defn.segment = segment;
194     lptr->defn.offset = 0;
195
196     ofmt->symdef (lptr->defn.label, segment, size, 2);
197 }
198
199 void declare_as_global (char *label, efunc error) {
200     union label *lptr;
201
202     if (islocal(label)) {
203         error(ERR_NONFATAL, "attempt to declare local symbol `%s' as"
204               " global", label);
205         return;
206     }
207     lptr = find_label (label, 1);
208     switch (lptr->defn.is_global) {
209       case NOT_DEFINED_YET:
210         lptr->defn.is_global = GLOBAL_PLACEHOLDER;
211         break;
212       case GLOBAL_PLACEHOLDER:         /* already done: silently ignore */
213       case GLOBAL_SYMBOL:
214         break;
215       case LOCAL_SYMBOL:
216         error(ERR_NONFATAL, "symbol `%s': [GLOBAL] directive must"
217               " appear before symbol definition", label);
218         break;
219     }
220 }
221
222 int init_labels (void) {
223     int i;
224
225     for (i=0; i<LABEL_HASHES; i++) {
226         ltab[i] = (union label *) nasm_malloc (LBLK_SIZE);
227         if (!ltab[i])
228             return -1;                 /* can't initialise, panic */
229         init_block (ltab[i]);
230         lfree[i] = ltab[i];
231     }
232
233     perm_head = perm_tail = (struct permts *) nasm_malloc (sizeof(struct permts));
234     if (!perm_head)
235         return -1;
236
237     perm_head->next = NULL;
238     perm_head->size = PERMTS_SIZE;
239     perm_head->usage = 0;
240
241     prevlabel = "";
242
243     return 0;
244 }
245
246 void cleanup_labels (void) {
247     int i;
248
249     for (i=0; i<LABEL_HASHES; i++) {
250         union label *lptr, *lhold;
251
252         lptr = lhold = ltab[i];
253
254         while (lptr) {
255             while (lptr->admin.movingon != END_BLOCK) lptr++;
256             lptr = lptr->admin.next;
257             nasm_free (lhold);
258             lhold = lptr;
259         }
260     }
261
262     while (perm_head) {
263         perm_tail = perm_head;
264         perm_head = perm_head->next;
265         nasm_free (perm_tail);
266     }
267 }
268
269 static void init_block (union label *blk) {
270     int j;
271
272     for (j=0; j<LABEL_BLOCK-1; j++)
273         blk[j].admin.movingon = END_LIST;
274     blk[LABEL_BLOCK-1].admin.movingon = END_BLOCK;
275     blk[LABEL_BLOCK-1].admin.next = NULL;
276 }
277
278 static char *perm_copy (char *string1, char *string2) {
279     char *p, *q;
280     int len = strlen(string1)+strlen(string2)+1;
281
282     if (perm_tail->size - perm_tail->usage < len) {
283         perm_tail->next = (struct permts *)nasm_malloc(sizeof(struct permts));
284         perm_tail = perm_tail->next;
285         perm_tail->size = PERMTS_SIZE;
286         perm_tail->usage = 0;
287     }
288     p = q = perm_tail->data + perm_tail->usage;
289     while ( (*q = *string1++) ) q++;
290     while ( (*q++ = *string2++) );
291     perm_tail->usage = q - perm_tail->data;
292
293     return p;
294 }