listing: preserve list file on error, include errors
[nasm/avx512.git] / labels.c
blob7e7b7f315098b89f50ae119561fdc4df33db73dd
1 /* ----------------------------------------------------------------------- *
2 *
3 * Copyright 1996-2009 The NASM Authors - All Rights Reserved
4 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
9 * conditions are met:
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 * ----------------------------------------------------------------------- */
35 * labels.c label handling for the Netwide Assembler
38 #include "compiler.h"
40 #include <stdio.h>
41 #include <string.h>
42 #include <stdlib.h>
43 #include <inttypes.h>
45 #include "nasm.h"
46 #include "nasmlib.h"
47 #include "hashtbl.h"
50 * A local label is one that begins with exactly one period. Things
51 * that begin with _two_ periods are NASM-specific things.
53 * If TASM compatibility is enabled, a local label can also begin with
54 * @@, so @@local is a TASM compatible local label. Note that we only
55 * check for the first @ symbol, although TASM requires both.
57 #define islocal(l) \
58 (tasm_compatible_mode ? \
59 (((l)[0] == '.' || (l)[0] == '@') && (l)[1] != '.') : \
60 ((l)[0] == '.' && (l)[1] != '.'))
61 #define islocalchar(c) \
62 (tasm_compatible_mode ? \
63 ((c) == '.' || (c) == '@') : \
64 ((c) == '.'))
66 #define LABEL_BLOCK 128 /* no. of labels/block */
67 #define LBLK_SIZE (LABEL_BLOCK*sizeof(union label))
69 #define END_LIST -3 /* don't clash with NO_SEG! */
70 #define END_BLOCK -2
71 #define BOGUS_VALUE -4
73 #define PERMTS_SIZE 16384 /* size of text blocks */
74 #if (PERMTS_SIZE < IDLEN_MAX)
75 #error "IPERMTS_SIZE must be greater than or equal to IDLEN_MAX"
76 #endif
78 /* values for label.defn.is_global */
79 #define DEFINED_BIT 1
80 #define GLOBAL_BIT 2
81 #define EXTERN_BIT 4
82 #define COMMON_BIT 8
84 #define NOT_DEFINED_YET 0
85 #define TYPE_MASK 3
86 #define LOCAL_SYMBOL (DEFINED_BIT)
87 #define GLOBAL_PLACEHOLDER (GLOBAL_BIT)
88 #define GLOBAL_SYMBOL (DEFINED_BIT|GLOBAL_BIT)
90 union label { /* actual label structures */
91 struct {
92 int32_t segment;
93 int64_t offset;
94 char *label, *special;
95 int is_global, is_norm;
96 } defn;
97 struct {
98 int32_t movingon;
99 int64_t dummy;
100 union label *next;
101 } admin;
104 struct permts { /* permanent text storage */
105 struct permts *next; /* for the linked list */
106 int size, usage; /* size and used space in ... */
107 char data[PERMTS_SIZE]; /* ... the data block itself */
110 extern int64_t global_offset_changed; /* defined in nasm.c */
112 static struct hash_table ltab; /* labels hash table */
113 static union label *ldata; /* all label data blocks */
114 static union label *lfree; /* labels free block */
115 static struct permts *perm_head; /* start of perm. text storage */
116 static struct permts *perm_tail; /* end of perm. text storage */
118 static void init_block(union label *blk);
119 static char *perm_copy(const char *string);
121 static char *prevlabel;
123 static bool initialized = false;
125 char lprefix[PREFIX_MAX] = { 0 };
126 char lpostfix[PREFIX_MAX] = { 0 };
129 * Internal routine: finds the `union label' corresponding to the
130 * given label name. Creates a new one, if it isn't found, and if
131 * `create' is true.
133 static union label *find_label(char *label, int create)
135 char *prev;
136 int prevlen, len;
137 union label *lptr, **lpp;
138 char label_str[IDLEN_MAX];
139 struct hash_insert ip;
141 if (islocal(label)) {
142 prev = prevlabel;
143 prevlen = strlen(prev);
144 len = strlen(label);
145 if (prevlen+len >= IDLEN_MAX)
146 return NULL; /* Error... */
147 memcpy(label_str, prev, prevlen);
148 memcpy(label_str+prevlen, label, len+1);
149 label = label_str;
150 } else {
151 prev = "";
152 prevlen = 0;
155 lpp = (union label **) hash_find(&ltab, label, &ip);
156 lptr = lpp ? *lpp : NULL;
158 if (lptr || !create)
159 return lptr;
161 /* Create a new label... */
162 if (lfree->admin.movingon == END_BLOCK) {
164 * must allocate a new block
166 lfree->admin.next =
167 (union label *)nasm_malloc(LBLK_SIZE);
168 lfree = lfree->admin.next;
169 init_block(lfree);
172 lfree->admin.movingon = BOGUS_VALUE;
173 lfree->defn.label = perm_copy(label);
174 lfree->defn.special = NULL;
175 lfree->defn.is_global = NOT_DEFINED_YET;
177 hash_add(&ip, lfree->defn.label, lfree);
178 return lfree++;
181 bool lookup_label(char *label, int32_t *segment, int64_t *offset)
183 union label *lptr;
185 if (!initialized)
186 return false;
188 lptr = find_label(label, 0);
189 if (lptr && (lptr->defn.is_global & DEFINED_BIT)) {
190 *segment = lptr->defn.segment;
191 *offset = lptr->defn.offset;
192 return true;
193 } else
194 return false;
197 bool is_extern(char *label)
199 union label *lptr;
201 if (!initialized)
202 return false;
204 lptr = find_label(label, 0);
205 return (lptr && (lptr->defn.is_global & EXTERN_BIT));
208 void redefine_label(char *label, int32_t segment, int64_t offset, char *special,
209 bool is_norm, bool isextrn, struct ofmt *ofmt,
210 efunc error)
212 union label *lptr;
213 int exi;
215 /* This routine possibly ought to check for phase errors. Most assemblers
216 * check for phase errors at this point. I don't know whether phase errors
217 * are even possible, nor whether they are checked somewhere else
220 (void)special; /* Don't warn that this parameter is unused */
221 (void)is_norm; /* Don't warn that this parameter is unused */
222 (void)isextrn; /* Don't warn that this parameter is unused */
223 (void)ofmt; /* Don't warn that this parameter is unused */
225 #ifdef DEBUG
226 #if DEBUG<3
227 if (!strncmp(label, "debugdump", 9))
228 #endif
229 error(ERR_DEBUG, "redefine_label (%s, %ld, %08lx, %s, %d, %d)",
230 label, segment, offset, special, is_norm, isextrn);
231 #endif
233 lptr = find_label(label, 1);
234 if (!lptr)
235 error(ERR_PANIC, "can't find label `%s' on pass two", label);
237 if (!islocal(label)) {
238 if (!islocalchar(*label) && lptr->defn.is_norm)
239 prevlabel = lptr->defn.label;
242 if (lptr->defn.offset != offset)
243 global_offset_changed++;
245 lptr->defn.offset = offset;
246 lptr->defn.segment = segment;
248 if (pass0 == 1) {
249 exi = !!(lptr->defn.is_global & GLOBAL_BIT);
250 if (exi) {
251 char *xsymbol;
252 int slen;
253 slen = strlen(lprefix);
254 slen += strlen(lptr->defn.label);
255 slen += strlen(lpostfix);
256 slen++; /* room for that null char */
257 xsymbol = nasm_malloc(slen);
258 snprintf(xsymbol, slen, "%s%s%s", lprefix, lptr->defn.label,
259 lpostfix);
261 ofmt->symdef(xsymbol, segment, offset, exi,
262 special ? special : lptr->defn.special);
263 ofmt->current_dfmt->debug_deflabel(xsymbol, segment, offset,
264 exi,
265 special ? special : lptr->
266 defn.special);
267 /** nasm_free(xsymbol); ! outobj.c stores the pointer; ouch!!! **/
268 } else {
269 if ((lptr->defn.is_global & (GLOBAL_BIT | EXTERN_BIT)) !=
270 EXTERN_BIT) {
271 ofmt->symdef(lptr->defn.label, segment, offset, exi,
272 special ? special : lptr->defn.special);
273 ofmt->current_dfmt->debug_deflabel(label, segment, offset,
274 exi,
275 special ? special :
276 lptr->defn.special);
280 /* if (pass0 == 1) */
283 void define_label(char *label, int32_t segment, int64_t offset, char *special,
284 bool is_norm, bool isextrn, struct ofmt *ofmt, efunc error)
286 union label *lptr;
287 int exi;
289 #ifdef DEBUG
290 #if DEBUG<3
291 if (!strncmp(label, "debugdump", 9))
292 #endif
293 error(ERR_DEBUG, "define_label (%s, %ld, %08lx, %s, %d, %d)",
294 label, segment, offset, special, is_norm, isextrn);
295 #endif
296 lptr = find_label(label, 1);
297 if (lptr->defn.is_global & DEFINED_BIT) {
298 error(ERR_NONFATAL, "symbol `%s' redefined", label);
299 return;
301 lptr->defn.is_global |= DEFINED_BIT;
302 if (isextrn)
303 lptr->defn.is_global |= EXTERN_BIT;
305 if (!islocalchar(label[0]) && is_norm) {
306 /* not local, but not special either */
307 prevlabel = lptr->defn.label;
308 } else if (islocal(label) && !*prevlabel) {
309 error(ERR_NONFATAL, "attempt to define a local label before any"
310 " non-local labels");
313 lptr->defn.segment = segment;
314 lptr->defn.offset = offset;
315 lptr->defn.is_norm = (!islocalchar(label[0]) && is_norm);
317 if (pass0 == 1 || (!is_norm && !isextrn && (segment > 0) && (segment & 1))) {
318 exi = !!(lptr->defn.is_global & GLOBAL_BIT);
319 if (exi) {
320 char *xsymbol;
321 int slen;
322 slen = strlen(lprefix);
323 slen += strlen(lptr->defn.label);
324 slen += strlen(lpostfix);
325 slen++; /* room for that null char */
326 xsymbol = nasm_malloc(slen);
327 snprintf(xsymbol, slen, "%s%s%s", lprefix, lptr->defn.label,
328 lpostfix);
330 ofmt->symdef(xsymbol, segment, offset, exi,
331 special ? special : lptr->defn.special);
332 ofmt->current_dfmt->debug_deflabel(xsymbol, segment, offset,
333 exi,
334 special ? special : lptr->
335 defn.special);
336 /** nasm_free(xsymbol); ! outobj.c stores the pointer; ouch!!! **/
337 } else {
338 if ((lptr->defn.is_global & (GLOBAL_BIT | EXTERN_BIT)) !=
339 EXTERN_BIT) {
340 ofmt->symdef(lptr->defn.label, segment, offset, exi,
341 special ? special : lptr->defn.special);
342 ofmt->current_dfmt->debug_deflabel(label, segment, offset,
343 exi,
344 special ? special :
345 lptr->defn.special);
348 } /* if (pass0 == 1) */
351 void define_common(char *label, int32_t segment, int32_t size, char *special,
352 struct ofmt *ofmt, efunc error)
354 union label *lptr;
356 lptr = find_label(label, 1);
357 if ((lptr->defn.is_global & DEFINED_BIT) &&
358 (passn == 1 || !(lptr->defn.is_global & COMMON_BIT))) {
359 error(ERR_NONFATAL, "symbol `%s' redefined", label);
360 return;
362 lptr->defn.is_global |= DEFINED_BIT|COMMON_BIT;
364 if (!islocalchar(label[0])) {
365 prevlabel = lptr->defn.label;
366 } else {
367 error(ERR_NONFATAL, "attempt to define a local label as a "
368 "common variable");
369 return;
372 lptr->defn.segment = segment;
373 lptr->defn.offset = 0;
375 if (pass0 == 0)
376 return;
378 ofmt->symdef(lptr->defn.label, segment, size, 2,
379 special ? special : lptr->defn.special);
380 ofmt->current_dfmt->debug_deflabel(lptr->defn.label, segment, size, 2,
381 special ? special : lptr->defn.
382 special);
385 void declare_as_global(char *label, char *special, efunc error)
387 union label *lptr;
389 if (islocal(label)) {
390 error(ERR_NONFATAL, "attempt to declare local symbol `%s' as"
391 " global", label);
392 return;
394 lptr = find_label(label, 1);
395 switch (lptr->defn.is_global & TYPE_MASK) {
396 case NOT_DEFINED_YET:
397 lptr->defn.is_global = GLOBAL_PLACEHOLDER;
398 lptr->defn.special = special ? perm_copy(special) : NULL;
399 break;
400 case GLOBAL_PLACEHOLDER: /* already done: silently ignore */
401 case GLOBAL_SYMBOL:
402 break;
403 case LOCAL_SYMBOL:
404 if (!(lptr->defn.is_global & EXTERN_BIT)) {
405 error(ERR_WARNING, "symbol `%s': GLOBAL directive "
406 "after symbol definition is an experimental feature", label);
407 lptr->defn.is_global = GLOBAL_SYMBOL;
409 break;
413 int init_labels(void)
415 hash_init(&ltab, HASH_LARGE);
417 ldata = lfree = (union label *)nasm_malloc(LBLK_SIZE);
418 init_block(lfree);
420 perm_head =
421 perm_tail = (struct permts *)nasm_malloc(sizeof(struct permts));
423 perm_head->next = NULL;
424 perm_head->size = PERMTS_SIZE;
425 perm_head->usage = 0;
427 prevlabel = "";
429 initialized = true;
431 return 0;
434 void cleanup_labels(void)
436 union label *lptr, *lhold;
438 initialized = false;
440 hash_free(&ltab);
442 lptr = lhold = ldata;
443 while (lptr) {
444 lptr = &lptr[LABEL_BLOCK-1];
445 lptr = lptr->admin.next;
446 nasm_free(lhold);
447 lhold = lptr;
450 while (perm_head) {
451 perm_tail = perm_head;
452 perm_head = perm_head->next;
453 nasm_free(perm_tail);
457 static void init_block(union label *blk)
459 int j;
461 for (j = 0; j < LABEL_BLOCK - 1; j++)
462 blk[j].admin.movingon = END_LIST;
463 blk[LABEL_BLOCK - 1].admin.movingon = END_BLOCK;
464 blk[LABEL_BLOCK - 1].admin.next = NULL;
467 static char *perm_copy(const char *string)
469 char *p;
470 int len = strlen(string)+1;
472 nasm_assert(len <= PERMTS_SIZE);
474 if (perm_tail->size - perm_tail->usage < len) {
475 perm_tail->next =
476 (struct permts *)nasm_malloc(sizeof(struct permts));
477 perm_tail = perm_tail->next;
478 perm_tail->next = NULL;
479 perm_tail->size = PERMTS_SIZE;
480 perm_tail->usage = 0;
482 p = perm_tail->data + perm_tail->usage;
483 memcpy(p, string, len);
484 perm_tail->usage += len;
486 return p;
489 char *local_scope(char *label)
491 return islocal(label) ? prevlabel : "";
495 * Notes regarding bug involving redefinition of external segments.
497 * Up to and including v0.97, the following code didn't work. From 0.97
498 * developers release 2 onwards, it will generate an error.
500 * EXTERN extlabel
501 * newlabel EQU extlabel + 1
503 * The results of allowing this code through are that two import records
504 * are generated, one for 'extlabel' and one for 'newlabel'.
506 * The reason for this is an inadequacy in the defined interface between
507 * the label manager and the output formats. The problem lies in how the
508 * output format driver tells that a label is an external label for which
509 * a label import record must be produced. Most (all except bin?) produce
510 * the record if the segment number of the label is not one of the internal
511 * segments that the output driver is producing.
513 * A simple fix to this would be to make the output formats keep track of
514 * which symbols they've produced import records for, and make them not
515 * produce import records for segments that are already defined.
517 * The best way, which is slightly harder but reduces duplication of code
518 * and should therefore make the entire system smaller and more stable is
519 * to change the interface between assembler, define_label(), and
520 * the output module. The changes that are needed are:
522 * The semantics of the 'isextern' flag passed to define_label() need
523 * examining. This information may or may not tell us what we need to
524 * know (ie should we be generating an import record at this point for this
525 * label). If these aren't the semantics, the semantics should be changed
526 * to this.
528 * The output module interface needs changing, so that the `isextern' flag
529 * is passed to the module, so that it can be easily tested for.