Apply Nindent to all .c and .h files
[nasm/avx512.git] / labels.c
blob4d2c3470cdde0a80532ae11f8bbc0a957dcd77a9
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 * If TASM compatibility is enabled, a local label can also begin with
20 * @@, so @@local is a TASM compatible local label. Note that we only
21 * check for the first @ symbol, although TASM requires both.
23 #define islocal(l) \
24 (tasm_compatible_mode ? \
25 (((l)[0] == '.' || (l)[0] == '@') && (l)[1] != '.') : \
26 ((l)[0] == '.' && (l)[1] != '.'))
27 #define islocalchar(c) \
28 (tasm_compatible_mode ? \
29 ((c) == '.' || (c) == '@') : \
30 ((c) == '.'))
32 #define LABEL_BLOCK 32 /* no. of labels/block */
33 #define LBLK_SIZE (LABEL_BLOCK*sizeof(union label))
34 #define LABEL_HASHES 37 /* no. of hash table entries */
36 #define END_LIST -3 /* don't clash with NO_SEG! */
37 #define END_BLOCK -2
38 #define BOGUS_VALUE -4
40 #define PERMTS_SIZE 4096 /* size of text blocks */
41 #if (PERMTS_SIZE > IDLEN_MAX)
42 #error "IPERMTS_SIZE must be less than or equal to IDLEN_MAX"
43 #endif
45 /* values for label.defn.is_global */
46 #define DEFINED_BIT 1
47 #define GLOBAL_BIT 2
48 #define EXTERN_BIT 4
50 #define NOT_DEFINED_YET 0
51 #define TYPE_MASK 3
52 #define LOCAL_SYMBOL (DEFINED_BIT)
53 #define GLOBAL_PLACEHOLDER (GLOBAL_BIT)
54 #define GLOBAL_SYMBOL (DEFINED_BIT|GLOBAL_BIT)
56 union label { /* actual label structures */
57 struct {
58 long segment, offset;
59 char *label, *special;
60 int is_global, is_norm;
61 } defn;
62 struct {
63 long movingon, dummy;
64 union label *next;
65 } admin;
68 struct permts { /* permanent text storage */
69 struct permts *next; /* for the linked list */
70 int size, usage; /* size and used space in ... */
71 char data[PERMTS_SIZE]; /* ... the data block itself */
74 extern int global_offset_changed; /* defined in nasm.c */
76 static union label *ltab[LABEL_HASHES]; /* using a hash table */
77 static union label *lfree[LABEL_HASHES]; /* pointer into the above */
78 static struct permts *perm_head; /* start of perm. text storage */
79 static struct permts *perm_tail; /* end of perm. text storage */
81 static void init_block(union label *blk);
82 static char *perm_copy(char *string1, char *string2);
84 static char *prevlabel;
86 static int initialised = FALSE;
88 char lprefix[PREFIX_MAX] = { 0 };
89 char lpostfix[PREFIX_MAX] = { 0 };
92 * Internal routine: finds the `union label' corresponding to the
93 * given label name. Creates a new one, if it isn't found, and if
94 * `create' is TRUE.
96 static union label *find_label(char *label, int create)
98 int hash = 0;
99 char *p, *prev;
100 int prevlen;
101 union label *lptr;
103 if (islocal(label))
104 prev = prevlabel;
105 else
106 prev = "";
107 prevlen = strlen(prev);
108 p = prev;
109 while (*p)
110 hash += *p++;
111 p = label;
112 while (*p)
113 hash += *p++;
114 hash %= LABEL_HASHES;
115 lptr = ltab[hash];
116 while (lptr->admin.movingon != END_LIST) {
117 if (lptr->admin.movingon == END_BLOCK) {
118 lptr = lptr->admin.next;
119 if (!lptr)
120 break;
122 if (!strncmp(lptr->defn.label, prev, prevlen) &&
123 !strcmp(lptr->defn.label + prevlen, label))
124 return lptr;
125 lptr++;
127 if (create) {
128 if (lfree[hash]->admin.movingon == END_BLOCK) {
130 * must allocate a new block
132 lfree[hash]->admin.next =
133 (union label *)nasm_malloc(LBLK_SIZE);
134 lfree[hash] = lfree[hash]->admin.next;
135 init_block(lfree[hash]);
138 lfree[hash]->admin.movingon = BOGUS_VALUE;
139 lfree[hash]->defn.label = perm_copy(prev, label);
140 lfree[hash]->defn.special = NULL;
141 lfree[hash]->defn.is_global = NOT_DEFINED_YET;
142 return lfree[hash]++;
143 } else
144 return NULL;
147 int lookup_label(char *label, long *segment, long *offset)
149 union label *lptr;
151 if (!initialised)
152 return 0;
154 lptr = find_label(label, 0);
155 if (lptr && (lptr->defn.is_global & DEFINED_BIT)) {
156 *segment = lptr->defn.segment;
157 *offset = lptr->defn.offset;
158 return 1;
159 } else
160 return 0;
163 int is_extern(char *label)
165 union label *lptr;
167 if (!initialised)
168 return 0;
170 lptr = find_label(label, 0);
171 if (lptr && (lptr->defn.is_global & EXTERN_BIT))
172 return 1;
173 else
174 return 0;
177 void redefine_label(char *label, long segment, long offset, char *special,
178 int is_norm, int isextrn, struct ofmt *ofmt,
179 efunc error)
181 union label *lptr;
182 int exi;
184 /* This routine possibly ought to check for phase errors. Most assemblers
185 * check for phase errors at this point. I don't know whether phase errors
186 * are even possible, nor whether they are checked somewhere else
189 (void)segment; /* Don't warn that this parameter is unused */
190 (void)special; /* Don't warn that this parameter is unused */
191 (void)is_norm; /* Don't warn that this parameter is unused */
192 (void)isextrn; /* Don't warn that this parameter is unused */
193 (void)ofmt; /* Don't warn that this parameter is unused */
195 #ifdef DEBUG
196 #if DEBUG<3
197 if (!strncmp(label, "debugdump", 9))
198 #endif
199 error(ERR_DEBUG, "redefine_label (%s, %ld, %08lx, %s, %d, %d)",
200 label, segment, offset, special, is_norm, isextrn);
201 #endif
203 lptr = find_label(label, 1);
204 if (!lptr)
205 error(ERR_PANIC, "can't find label `%s' on pass two", label);
207 if (!islocal(label)) {
208 if (!islocalchar(*label) && lptr->defn.is_norm)
209 prevlabel = lptr->defn.label;
212 global_offset_changed |= (lptr->defn.offset != offset);
213 lptr->defn.offset = offset;
215 if (pass0 == 1) {
216 exi = !!(lptr->defn.is_global & GLOBAL_BIT);
217 if (exi) {
218 char *xsymbol;
219 int slen;
220 slen = strlen(lprefix);
221 slen += strlen(lptr->defn.label);
222 slen += strlen(lpostfix);
223 slen++; /* room for that null char */
224 xsymbol = nasm_malloc(slen);
225 snprintf(xsymbol, slen, "%s%s%s", lprefix, lptr->defn.label,
226 lpostfix);
228 ofmt->symdef(xsymbol, segment, offset, exi,
229 special ? special : lptr->defn.special);
230 ofmt->current_dfmt->debug_deflabel(xsymbol, segment, offset,
231 exi,
232 special ? special : lptr->
233 defn.special);
234 /** nasm_free(xsymbol); ! outobj.c stores the pointer; ouch!!! **/
235 } else {
236 if ((lptr->defn.is_global & (GLOBAL_BIT | EXTERN_BIT)) !=
237 EXTERN_BIT) {
238 ofmt->symdef(lptr->defn.label, segment, offset, exi,
239 special ? special : lptr->defn.special);
240 ofmt->current_dfmt->debug_deflabel(label, segment, offset,
241 exi,
242 special ? special :
243 lptr->defn.special);
247 /* if (pass0 == 1) */
250 void define_label(char *label, long segment, long offset, char *special,
251 int is_norm, int isextrn, struct ofmt *ofmt, efunc error)
253 union label *lptr;
254 int exi;
256 #ifdef DEBUG
257 #if DEBUG<3
258 if (!strncmp(label, "debugdump", 9))
259 #endif
260 error(ERR_DEBUG, "define_label (%s, %ld, %08lx, %s, %d, %d)",
261 label, segment, offset, special, is_norm, isextrn);
262 #endif
263 lptr = find_label(label, 1);
264 if (lptr->defn.is_global & DEFINED_BIT) {
265 error(ERR_NONFATAL, "symbol `%s' redefined", label);
266 return;
268 lptr->defn.is_global |= DEFINED_BIT;
269 if (isextrn)
270 lptr->defn.is_global |= EXTERN_BIT;
272 if (!islocalchar(label[0]) && is_norm) /* not local, but not special either */
273 prevlabel = lptr->defn.label;
274 else if (islocal(label) && !*prevlabel) {
275 error(ERR_NONFATAL, "attempt to define a local label before any"
276 " non-local labels");
279 lptr->defn.segment = segment;
280 lptr->defn.offset = offset;
281 lptr->defn.is_norm = (!islocalchar(label[0]) && is_norm);
283 if (pass0 == 1 || (!is_norm && !isextrn && (segment & 1))) {
284 exi = !!(lptr->defn.is_global & GLOBAL_BIT);
285 if (exi) {
286 char *xsymbol;
287 int slen;
288 slen = strlen(lprefix);
289 slen += strlen(lptr->defn.label);
290 slen += strlen(lpostfix);
291 slen++; /* room for that null char */
292 xsymbol = nasm_malloc(slen);
293 snprintf(xsymbol, slen, "%s%s%s", lprefix, lptr->defn.label,
294 lpostfix);
296 ofmt->symdef(xsymbol, segment, offset, exi,
297 special ? special : lptr->defn.special);
298 ofmt->current_dfmt->debug_deflabel(xsymbol, segment, offset,
299 exi,
300 special ? special : lptr->
301 defn.special);
302 /** nasm_free(xsymbol); ! outobj.c stores the pointer; ouch!!! **/
303 } else {
304 if ((lptr->defn.is_global & (GLOBAL_BIT | EXTERN_BIT)) !=
305 EXTERN_BIT) {
306 ofmt->symdef(lptr->defn.label, segment, offset, exi,
307 special ? special : lptr->defn.special);
308 ofmt->current_dfmt->debug_deflabel(label, segment, offset,
309 exi,
310 special ? special :
311 lptr->defn.special);
314 } /* if (pass0 == 1) */
317 void define_common(char *label, long segment, long size, char *special,
318 struct ofmt *ofmt, efunc error)
320 union label *lptr;
322 lptr = find_label(label, 1);
323 if (lptr->defn.is_global & DEFINED_BIT) {
324 error(ERR_NONFATAL, "symbol `%s' redefined", label);
325 return;
327 lptr->defn.is_global |= DEFINED_BIT;
329 if (!islocalchar(label[0])) /* not local, but not special either */
330 prevlabel = lptr->defn.label;
331 else
332 error(ERR_NONFATAL, "attempt to define a local label as a "
333 "common variable");
335 lptr->defn.segment = segment;
336 lptr->defn.offset = 0;
338 ofmt->symdef(lptr->defn.label, segment, size, 2,
339 special ? special : lptr->defn.special);
340 ofmt->current_dfmt->debug_deflabel(lptr->defn.label, segment, size, 2,
341 special ? special : lptr->defn.
342 special);
345 void declare_as_global(char *label, char *special, efunc error)
347 union label *lptr;
349 if (islocal(label)) {
350 error(ERR_NONFATAL, "attempt to declare local symbol `%s' as"
351 " global", label);
352 return;
354 lptr = find_label(label, 1);
355 switch (lptr->defn.is_global & TYPE_MASK) {
356 case NOT_DEFINED_YET:
357 lptr->defn.is_global = GLOBAL_PLACEHOLDER;
358 lptr->defn.special = special ? perm_copy(special, "") : NULL;
359 break;
360 case GLOBAL_PLACEHOLDER: /* already done: silently ignore */
361 case GLOBAL_SYMBOL:
362 break;
363 case LOCAL_SYMBOL:
364 if (!lptr->defn.is_global & EXTERN_BIT)
365 error(ERR_NONFATAL, "symbol `%s': GLOBAL directive must"
366 " appear before symbol definition", label);
367 break;
371 int init_labels(void)
373 int i;
375 for (i = 0; i < LABEL_HASHES; i++) {
376 ltab[i] = (union label *)nasm_malloc(LBLK_SIZE);
377 if (!ltab[i])
378 return -1; /* can't initialise, panic */
379 init_block(ltab[i]);
380 lfree[i] = ltab[i];
383 perm_head =
384 perm_tail = (struct permts *)nasm_malloc(sizeof(struct permts));
386 if (!perm_head)
387 return -1;
389 perm_head->next = NULL;
390 perm_head->size = PERMTS_SIZE;
391 perm_head->usage = 0;
393 prevlabel = "";
395 initialised = TRUE;
397 return 0;
400 void cleanup_labels(void)
402 int i;
404 initialised = FALSE;
406 for (i = 0; i < LABEL_HASHES; i++) {
407 union label *lptr, *lhold;
409 lptr = lhold = ltab[i];
411 while (lptr) {
412 while (lptr->admin.movingon != END_BLOCK)
413 lptr++;
414 lptr = lptr->admin.next;
415 nasm_free(lhold);
416 lhold = lptr;
420 while (perm_head) {
421 perm_tail = perm_head;
422 perm_head = perm_head->next;
423 nasm_free(perm_tail);
427 static void init_block(union label *blk)
429 int j;
431 for (j = 0; j < LABEL_BLOCK - 1; j++)
432 blk[j].admin.movingon = END_LIST;
433 blk[LABEL_BLOCK - 1].admin.movingon = END_BLOCK;
434 blk[LABEL_BLOCK - 1].admin.next = NULL;
437 static char *perm_copy(char *string1, char *string2)
439 char *p, *q;
440 int len = strlen(string1) + strlen(string2) + 1;
442 if (perm_tail->size - perm_tail->usage < len) {
443 perm_tail->next =
444 (struct permts *)nasm_malloc(sizeof(struct permts));
445 perm_tail = perm_tail->next;
446 perm_tail->next = NULL;
447 perm_tail->size = PERMTS_SIZE;
448 perm_tail->usage = 0;
450 p = q = perm_tail->data + perm_tail->usage;
451 while ((*q = *string1++))
452 q++;
453 while ((*q++ = *string2++)) ;
454 perm_tail->usage = q - perm_tail->data;
456 return p;
460 * Notes regarding bug involving redefinition of external segments.
462 * Up to and including v0.97, the following code didn't work. From 0.97
463 * developers release 2 onwards, it will generate an error.
465 * EXTERN extlabel
466 * newlabel EQU extlabel + 1
468 * The results of allowing this code through are that two import records
469 * are generated, one for 'extlabel' and one for 'newlabel'.
471 * The reason for this is an inadequacy in the defined interface between
472 * the label manager and the output formats. The problem lies in how the
473 * output format driver tells that a label is an external label for which
474 * a label import record must be produced. Most (all except bin?) produce
475 * the record if the segment number of the label is not one of the internal
476 * segments that the output driver is producing.
478 * A simple fix to this would be to make the output formats keep track of
479 * which symbols they've produced import records for, and make them not
480 * produce import records for segments that are already defined.
482 * The best way, which is slightly harder but reduces duplication of code
483 * and should therefore make the entire system smaller and more stable is
484 * to change the interface between assembler, define_label(), and
485 * the output module. The changes that are needed are:
487 * The semantics of the 'isextern' flag passed to define_label() need
488 * examining. This information may or may not tell us what we need to
489 * know (ie should we be generating an import record at this point for this
490 * label). If these aren't the semantics, the semantics should be changed
491 * to this.
493 * The output module interface needs changing, so that the `isextern' flag
494 * is passed to the module, so that it can be easily tested for.