NASM 0.98.03
[nasm/avx512.git] / labels.c
blobc47d34c42f73822e73ef5f47c85da10b7ba5e8b3
1 /* labels.c label handling for the Netwide Assembler
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 */
9 #include <stdio.h>
10 #include <string.h>
11 #include <stdlib.h>
12 #include "nasm.h"
13 #include "nasmlib.h"
16 * A local label is one that begins with exactly one period. Things
17 * that begin with _two_ periods are NASM-specific things.
19 #define islocal(l) ((l)[0] == '.' && (l)[1] != '.')
21 #define LABEL_BLOCK 32 /* no. of labels/block */
22 #define LBLK_SIZE (LABEL_BLOCK*sizeof(union label))
23 #define LABEL_HASHES 37 /* no. of hash table entries */
25 #define END_LIST -3 /* don't clash with NO_SEG! */
26 #define END_BLOCK -2
27 #define BOGUS_VALUE -4
29 #define PERMTS_SIZE 4096 /* size of text blocks */
31 /* values for label.defn.is_global */
32 #define DEFINED_BIT 1
33 #define GLOBAL_BIT 2
34 #define EXTERN_BIT 4
36 #define NOT_DEFINED_YET 0
37 #define TYPE_MASK 3
38 #define LOCAL_SYMBOL (DEFINED_BIT)
39 #define GLOBAL_PLACEHOLDER (GLOBAL_BIT)
40 #define GLOBAL_SYMBOL (DEFINED_BIT|GLOBAL_BIT)
42 union label { /* actual label structures */
43 struct {
44 long segment, offset;
45 char *label, *special;
46 int is_global, is_norm;
47 } defn;
48 struct {
49 long movingon, dummy;
50 union label *next;
51 } admin;
54 struct permts { /* permanent text storage */
55 struct permts *next; /* for the linked list */
56 int size, usage; /* size and used space in ... */
57 char data[PERMTS_SIZE]; /* ... the data block itself */
60 extern int global_offset_changed; /* defined in nasm.c */
62 static union label *ltab[LABEL_HASHES];/* using a hash table */
63 static union label *lfree[LABEL_HASHES];/* pointer into the above */
64 static struct permts *perm_head; /* start of perm. text storage */
65 static struct permts *perm_tail; /* end of perm. text storage */
67 static void init_block (union label *blk);
68 static char *perm_copy (char *string1, char *string2);
70 static char *prevlabel;
72 static int initialised = FALSE;
75 * Internal routine: finds the `union label' corresponding to the
76 * given label name. Creates a new one, if it isn't found, and if
77 * `create' is TRUE.
79 static union label *find_label (char *label, int create)
81 int hash = 0;
82 char *p, *prev;
83 int prevlen;
84 union label *lptr;
86 if (islocal(label))
87 prev = prevlabel;
88 else
89 prev = "";
90 prevlen = strlen(prev);
91 p = prev;
92 while (*p) hash += *p++;
93 p = label;
94 while (*p) hash += *p++;
95 hash %= LABEL_HASHES;
96 lptr = ltab[hash];
97 while (lptr->admin.movingon != END_LIST) {
98 if (lptr->admin.movingon == END_BLOCK) {
99 lptr = lptr->admin.next;
100 if (!lptr)
101 break;
103 if (!strncmp(lptr->defn.label, prev, prevlen) &&
104 !strcmp(lptr->defn.label+prevlen, label))
105 return lptr;
106 lptr++;
108 if (create) {
109 if (lfree[hash]->admin.movingon == END_BLOCK) {
111 * must allocate a new block
113 lfree[hash]->admin.next = (union label *) nasm_malloc (LBLK_SIZE);
114 lfree[hash] = lfree[hash]->admin.next;
115 init_block(lfree[hash]);
118 lfree[hash]->admin.movingon = BOGUS_VALUE;
119 lfree[hash]->defn.label = perm_copy (prev, label);
120 lfree[hash]->defn.special = NULL;
121 lfree[hash]->defn.is_global = NOT_DEFINED_YET;
122 return lfree[hash]++;
124 else
125 return NULL;
128 int lookup_label (char *label, long *segment, long *offset)
130 union label *lptr;
132 if (!initialised)
133 return 0;
135 lptr = find_label (label, 0);
136 if (lptr && (lptr->defn.is_global & DEFINED_BIT)) {
137 *segment = lptr->defn.segment;
138 *offset = lptr->defn.offset;
139 return 1;
141 else
142 return 0;
145 int is_extern (char *label)
147 union label *lptr;
149 if (!initialised)
150 return 0;
152 lptr = find_label (label, 0);
153 if (lptr && (lptr->defn.is_global & EXTERN_BIT))
154 return 1;
155 else
156 return 0;
159 void redefine_label (char *label, long segment, long offset, char *special,
160 int is_norm, int isextrn, struct ofmt *ofmt, efunc error)
162 union label *lptr;
164 /* This routine possibly ought to check for phase errors. Most assemblers
165 * check for phase errors at this point. I don't know whether phase errors
166 * are even possible, nor whether they are checked somewhere else
169 (void) segment; /* Don't warn that this parameter is unused */
170 (void) special; /* Don't warn that this parameter is unused */
171 (void) is_norm; /* Don't warn that this parameter is unused */
172 (void) isextrn; /* Don't warn that this parameter is unused */
173 (void) ofmt; /* Don't warn that this parameter is unused */
175 #ifdef DEBUG
176 #if DEBUG<3
177 if (!strncmp(label, "debugdump", 9))
178 #endif
179 error(ERR_DEBUG, "redefine_label (%s, %ld, %08lx, %s, %d, %d)",
180 label, segment, offset, special, is_norm, isextrn);
181 #endif
183 lptr = find_label (label, 1);
184 if (!lptr)
185 error (ERR_PANIC, "can't find label `%s' on pass two", label);
187 if (!islocal(label)) {
188 if (*label != '.' && lptr->defn.is_norm)
189 prevlabel = lptr->defn.label;
192 global_offset_changed |= (lptr->defn.offset != offset);
193 lptr->defn.offset = offset;
196 void define_label (char *label, long segment, long offset, char *special,
197 int is_norm, int isextrn, struct ofmt *ofmt, efunc error)
199 union label *lptr;
201 #ifdef DEBUG
202 #if DEBUG<3
203 if (!strncmp(label, "debugdump", 9))
204 #endif
205 error(ERR_DEBUG, "define_label (%s, %ld, %08lx, %s, %d, %d)",
206 label, segment, offset, special, is_norm, isextrn);
207 #endif
208 lptr = find_label (label, 1);
209 if (lptr->defn.is_global & DEFINED_BIT) {
210 error(ERR_NONFATAL, "symbol `%s' redefined", label);
211 return;
213 lptr->defn.is_global |= DEFINED_BIT;
214 if (isextrn)
215 lptr->defn.is_global |= EXTERN_BIT;
217 if (label[0] != '.' && is_norm) /* not local, but not special either */
218 prevlabel = lptr->defn.label;
219 else if (label[0] == '.' && label[1] != '.' && !*prevlabel)
220 error(ERR_NONFATAL, "attempt to define a local label before any"
221 " non-local labels");
223 lptr->defn.segment = segment;
224 lptr->defn.offset = offset;
225 lptr->defn.is_norm = (label[0] != '.' && is_norm);
227 if ( (lptr->defn.is_global & (GLOBAL_BIT|EXTERN_BIT)) != EXTERN_BIT ) {
228 ofmt->symdef (lptr->defn.label, segment, offset,
229 !!(lptr->defn.is_global & GLOBAL_BIT),
230 special ? special : lptr->defn.special);
231 ofmt->current_dfmt->debug_deflabel (label, segment, offset,
232 !!(lptr->defn.is_global & GLOBAL_BIT),
233 special ? special : lptr->defn.special);
237 void define_common (char *label, long segment, long size, char *special,
238 struct ofmt *ofmt, efunc error)
240 union label *lptr;
242 lptr = find_label (label, 1);
243 if (lptr->defn.is_global & DEFINED_BIT) {
244 error(ERR_NONFATAL, "symbol `%s' redefined", label);
245 return;
247 lptr->defn.is_global |= DEFINED_BIT;
249 if (label[0] != '.') /* not local, but not special either */
250 prevlabel = lptr->defn.label;
251 else
252 error(ERR_NONFATAL, "attempt to define a local label as a "
253 "common variable");
255 lptr->defn.segment = segment;
256 lptr->defn.offset = 0;
258 ofmt->symdef (lptr->defn.label, segment, size, 2,
259 special ? special : lptr->defn.special);
260 ofmt->current_dfmt->debug_deflabel(lptr->defn.label, segment, size, 2,
261 special ? special : lptr->defn.special);
264 void declare_as_global (char *label, char *special, efunc error)
266 union label *lptr;
268 if (islocal(label)) {
269 error(ERR_NONFATAL, "attempt to declare local symbol `%s' as"
270 " global", label);
271 return;
273 lptr = find_label (label, 1);
274 switch (lptr->defn.is_global & TYPE_MASK) {
275 case NOT_DEFINED_YET:
276 lptr->defn.is_global = GLOBAL_PLACEHOLDER;
277 lptr->defn.special = special ? perm_copy(special, "") : NULL;
278 break;
279 case GLOBAL_PLACEHOLDER: /* already done: silently ignore */
280 case GLOBAL_SYMBOL:
281 break;
282 case LOCAL_SYMBOL:
283 if (!lptr->defn.is_global & EXTERN_BIT)
284 error(ERR_NONFATAL, "symbol `%s': GLOBAL directive must"
285 " appear before symbol definition", label);
286 break;
290 int init_labels (void)
292 int i;
294 for (i=0; i<LABEL_HASHES; i++) {
295 ltab[i] = (union label *) nasm_malloc (LBLK_SIZE);
296 if (!ltab[i])
297 return -1; /* can't initialise, panic */
298 init_block (ltab[i]);
299 lfree[i] = ltab[i];
302 perm_head =
303 perm_tail = (struct permts *) nasm_malloc (sizeof(struct permts));
305 if (!perm_head)
306 return -1;
308 perm_head->next = NULL;
309 perm_head->size = PERMTS_SIZE;
310 perm_head->usage = 0;
312 prevlabel = "";
314 initialised = TRUE;
316 return 0;
319 void cleanup_labels (void)
321 int i;
323 initialised = FALSE;
325 for (i=0; i<LABEL_HASHES; i++) {
326 union label *lptr, *lhold;
328 lptr = lhold = ltab[i];
330 while (lptr) {
331 while (lptr->admin.movingon != END_BLOCK) lptr++;
332 lptr = lptr->admin.next;
333 nasm_free (lhold);
334 lhold = lptr;
338 while (perm_head) {
339 perm_tail = perm_head;
340 perm_head = perm_head->next;
341 nasm_free (perm_tail);
345 static void init_block (union label *blk)
347 int j;
349 for (j=0; j<LABEL_BLOCK-1; j++)
350 blk[j].admin.movingon = END_LIST;
351 blk[LABEL_BLOCK-1].admin.movingon = END_BLOCK;
352 blk[LABEL_BLOCK-1].admin.next = NULL;
355 static char *perm_copy (char *string1, char *string2)
357 char *p, *q;
358 int len = strlen(string1)+strlen(string2)+1;
360 if (perm_tail->size - perm_tail->usage < len) {
361 perm_tail->next = (struct permts *)nasm_malloc(sizeof(struct permts));
362 perm_tail = perm_tail->next;
363 perm_tail->next = NULL;
364 perm_tail->size = PERMTS_SIZE;
365 perm_tail->usage = 0;
367 p = q = perm_tail->data + perm_tail->usage;
368 while ( (*q = *string1++) ) q++;
369 while ( (*q++ = *string2++) ) ;
370 perm_tail->usage = q - perm_tail->data;
372 return p;
376 * Notes regarding bug involving redefinition of external segments.
378 * Up to and including v0.97, the following code didn't work. From 0.97
379 * developers release 2 onwards, it will generate an error.
381 * EXTERN extlabel
382 * newlabel EQU extlabel + 1
384 * The results of allowing this code through are that two import records
385 * are generated, one for 'extlabel' and one for 'newlabel'.
387 * The reason for this is an inadequacy in the defined interface between
388 * the label manager and the output formats. The problem lies in how the
389 * output format driver tells that a label is an external label for which
390 * a label import record must be produced. Most (all except bin?) produce
391 * the record if the segment number of the label is not one of the internal
392 * segments that the output driver is producing.
394 * A simple fix to this would be to make the output formats keep track of
395 * which symbols they've produced import records for, and make them not
396 * produce import records for segments that are already defined.
398 * The best way, which is slightly harder but reduces duplication of code
399 * and should therefore make the entire system smaller and more stable is
400 * to change the interface between assembler, define_label(), and
401 * the output module. The changes that are needed are:
403 * The semantics of the 'isextern' flag passed to define_label() need
404 * examining. This information may or may not tell us what we need to
405 * know (ie should we be generating an import record at this point for this
406 * label). If these aren't the semantics, the semantics should be changed
407 * to this.
409 * The output module interface needs changing, so that the `isextern' flag
410 * is passed to the module, so that it can be easily tested for.