test: add test for cv8 structure bug (BR 3392342 and 3392343)
[nasm.git] / labels.c
blob9f2b76e6c4c11adaa90495001518297726d00a25
1 /* ----------------------------------------------------------------------- *
3 * Copyright 1996-2014 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"
48 #include "labels.h"
51 * A local label is one that begins with exactly one period. Things
52 * that begin with _two_ periods are NASM-specific things.
54 * If TASM compatibility is enabled, a local label can also begin with
55 * @@, so @@local is a TASM compatible local label. Note that we only
56 * check for the first @ symbol, although TASM requires both.
58 #define islocal(l) \
59 (tasm_compatible_mode ? \
60 (((l)[0] == '.' || (l)[0] == '@') && (l)[1] != '.') : \
61 ((l)[0] == '.' && (l)[1] != '.'))
62 #define islocalchar(c) \
63 (tasm_compatible_mode ? \
64 ((c) == '.' || (c) == '@') : \
65 ((c) == '.'))
67 #define LABEL_BLOCK 128 /* no. of labels/block */
68 #define LBLK_SIZE (LABEL_BLOCK * sizeof(union label))
70 #define END_LIST -3 /* don't clash with NO_SEG! */
71 #define END_BLOCK -2
72 #define BOGUS_VALUE -4
74 #define PERMTS_SIZE 16384 /* size of text blocks */
75 #if (PERMTS_SIZE < IDLEN_MAX)
76 #error "IPERMTS_SIZE must be greater than or equal to IDLEN_MAX"
77 #endif
79 /* values for label.defn.is_global */
80 #define DEFINED_BIT 1
81 #define GLOBAL_BIT 2
82 #define EXTERN_BIT 4
83 #define COMMON_BIT 8
85 #define NOT_DEFINED_YET 0
86 #define TYPE_MASK 3
87 #define LOCAL_SYMBOL (DEFINED_BIT)
88 #define GLOBAL_PLACEHOLDER (GLOBAL_BIT)
89 #define GLOBAL_SYMBOL (DEFINED_BIT | GLOBAL_BIT)
91 union label { /* actual label structures */
92 struct {
93 int32_t segment;
94 int64_t offset;
95 char *label, *special;
96 int is_global, is_norm;
97 } defn;
98 struct {
99 int32_t movingon;
100 int64_t dummy;
101 union label *next;
102 } admin;
105 struct permts { /* permanent text storage */
106 struct permts *next; /* for the linked list */
107 int size, usage; /* size and used space in ... */
108 char data[PERMTS_SIZE]; /* ... the data block itself */
111 extern int64_t global_offset_changed; /* defined in nasm.c */
113 static struct hash_table ltab; /* labels hash table */
114 static union label *ldata; /* all label data blocks */
115 static union label *lfree; /* labels free block */
116 static struct permts *perm_head; /* start of perm. text storage */
117 static struct permts *perm_tail; /* end of perm. text storage */
119 static void init_block(union label *blk);
120 static char *perm_copy(const char *string);
122 static char *prevlabel;
124 static bool initialized = false;
126 char lprefix[PREFIX_MAX] = { 0 };
127 char lpostfix[PREFIX_MAX] = { 0 };
130 * Internal routine: finds the `union label' corresponding to the
131 * given label name. Creates a new one, if it isn't found, and if
132 * `create' is true.
134 static union label *find_label(char *label, int create)
136 char *prev;
137 int prevlen, len;
138 union label *lptr, **lpp;
139 char label_str[IDLEN_MAX];
140 struct hash_insert ip;
142 if (islocal(label)) {
143 prev = prevlabel;
144 prevlen = strlen(prev);
145 len = strlen(label);
146 if (prevlen + len >= IDLEN_MAX) {
147 nasm_error(ERR_NONFATAL, "identifier length exceed %i bytes",
148 IDLEN_MAX);
149 return NULL;
151 memcpy(label_str, prev, prevlen);
152 memcpy(label_str+prevlen, label, len+1);
153 label = label_str;
154 } else {
155 prev = "";
156 prevlen = 0;
159 lpp = (union label **) hash_find(&ltab, label, &ip);
160 lptr = lpp ? *lpp : NULL;
162 if (lptr || !create)
163 return lptr;
165 /* Create a new label... */
166 if (lfree->admin.movingon == END_BLOCK) {
168 * must allocate a new block
170 lfree->admin.next = (union label *)nasm_malloc(LBLK_SIZE);
171 lfree = lfree->admin.next;
172 init_block(lfree);
175 lfree->admin.movingon = BOGUS_VALUE;
176 lfree->defn.label = perm_copy(label);
177 lfree->defn.special = NULL;
178 lfree->defn.is_global = NOT_DEFINED_YET;
180 hash_add(&ip, lfree->defn.label, lfree);
181 return lfree++;
184 bool lookup_label(char *label, int32_t *segment, int64_t *offset)
186 union label *lptr;
188 if (!initialized)
189 return false;
191 lptr = find_label(label, 0);
192 if (lptr && (lptr->defn.is_global & DEFINED_BIT)) {
193 *segment = lptr->defn.segment;
194 *offset = lptr->defn.offset;
195 return true;
198 return false;
201 bool is_extern(char *label)
203 union label *lptr;
205 if (!initialized)
206 return false;
208 lptr = find_label(label, 0);
209 return (lptr && (lptr->defn.is_global & EXTERN_BIT));
212 void redefine_label(char *label, int32_t segment, int64_t offset, char *special,
213 bool is_norm, bool isextrn)
215 union label *lptr;
216 int exi;
218 /* This routine possibly ought to check for phase errors. Most assemblers
219 * check for phase errors at this point. I don't know whether phase errors
220 * are even possible, nor whether they are checked somewhere else
223 (void)special; /* Don't warn that this parameter is unused */
224 (void)is_norm; /* Don't warn that this parameter is unused */
225 (void)isextrn; /* Don't warn that this parameter is unused */
227 #ifdef DEBUG
228 #if DEBUG < 3
229 if (!strncmp(label, "debugdump", 9))
230 #endif
231 nasm_error(ERR_DEBUG, "redefine_label (%s, %"PRIx32", %"PRIx64", %s, %d, %d)",
232 label, segment, offset, special, is_norm, isextrn);
233 #endif
235 lptr = find_label(label, 1);
236 if (!lptr)
237 nasm_panic(0, "can't find label `%s' on pass two", label);
239 if (!islocal(label)) {
240 if (!islocalchar(*label) && lptr->defn.is_norm)
241 prevlabel = lptr->defn.label;
244 if (lptr->defn.offset != offset)
245 global_offset_changed++;
247 lptr->defn.offset = offset;
248 lptr->defn.segment = segment;
250 if (pass0 == 1) {
251 exi = !!(lptr->defn.is_global & GLOBAL_BIT);
252 if (exi) {
253 char *xsymbol;
254 int slen;
255 slen = strlen(lprefix);
256 slen += strlen(lptr->defn.label);
257 slen += strlen(lpostfix);
258 slen++; /* room for that null char */
259 xsymbol = nasm_malloc(slen);
260 snprintf(xsymbol, slen, "%s%s%s", lprefix, lptr->defn.label,
261 lpostfix);
263 ofmt->symdef(xsymbol, segment, offset, exi,
264 special ? special : lptr->defn.special);
265 ofmt->current_dfmt->debug_deflabel(xsymbol, segment, offset, exi,
266 special ? special : lptr->defn.special);
267 /** nasm_free(xsymbol); ! outobj.c stores the pointer; ouch!!! **/
268 } else {
269 if ((lptr->defn.is_global & (GLOBAL_BIT | EXTERN_BIT)) != EXTERN_BIT) {
270 ofmt->symdef(lptr->defn.label, segment, offset, exi,
271 special ? special : lptr->defn.special);
272 ofmt->current_dfmt->debug_deflabel(label, segment, offset, exi,
273 special ? special : lptr->defn.special);
276 } /* if (pass0 == 1) */
279 void define_label(char *label, int32_t segment, int64_t offset, char *special,
280 bool is_norm, bool isextrn)
282 union label *lptr;
283 int exi;
285 #ifdef DEBUG
286 #if DEBUG<3
287 if (!strncmp(label, "debugdump", 9))
288 #endif
289 nasm_error(ERR_DEBUG, "define_label (%s, %"PRIx32", %"PRIx64", %s, %d, %d)",
290 label, segment, offset, special, is_norm, isextrn);
291 #endif
292 lptr = find_label(label, 1);
293 if (!lptr)
294 return;
295 if (lptr->defn.is_global & DEFINED_BIT) {
296 nasm_error(ERR_NONFATAL, "symbol `%s' redefined", label);
297 return;
299 lptr->defn.is_global |= DEFINED_BIT;
300 if (isextrn)
301 lptr->defn.is_global |= EXTERN_BIT;
303 if (!islocalchar(label[0]) && is_norm) {
304 /* not local, but not special either */
305 prevlabel = lptr->defn.label;
306 } else if (islocal(label) && !*prevlabel) {
307 nasm_error(ERR_NONFATAL, "attempt to define a local label before any"
308 " non-local labels");
311 lptr->defn.segment = segment;
312 lptr->defn.offset = offset;
313 lptr->defn.is_norm = (!islocalchar(label[0]) && is_norm);
315 if (pass0 == 1 || (!is_norm && !isextrn && (segment > 0) && (segment & 1))) {
316 exi = !!(lptr->defn.is_global & GLOBAL_BIT);
317 if (exi) {
318 char *xsymbol;
319 int slen;
320 slen = strlen(lprefix);
321 slen += strlen(lptr->defn.label);
322 slen += strlen(lpostfix);
323 slen++; /* room for that null char */
324 xsymbol = nasm_malloc(slen);
325 snprintf(xsymbol, slen, "%s%s%s", lprefix, lptr->defn.label,
326 lpostfix);
328 ofmt->symdef(xsymbol, segment, offset, exi,
329 special ? special : lptr->defn.special);
330 ofmt->current_dfmt->debug_deflabel(xsymbol, segment, offset, exi,
331 special ? special : lptr->defn.special);
332 /** nasm_free(xsymbol); ! outobj.c stores the pointer; ouch!!! **/
333 } else {
334 if ((lptr->defn.is_global & (GLOBAL_BIT | EXTERN_BIT)) != EXTERN_BIT) {
335 ofmt->symdef(lptr->defn.label, segment, offset, exi,
336 special ? special : lptr->defn.special);
337 ofmt->current_dfmt->debug_deflabel(label, segment, offset, exi,
338 special ? special : lptr->defn.special);
341 } /* if (pass0 == 1) */
344 void define_common(char *label, int32_t segment, int32_t size, char *special)
346 union label *lptr;
348 lptr = find_label(label, 1);
349 if (!lptr)
350 return;
351 if ((lptr->defn.is_global & DEFINED_BIT) &&
352 (passn == 1 || !(lptr->defn.is_global & COMMON_BIT))) {
353 nasm_error(ERR_NONFATAL, "symbol `%s' redefined", label);
354 return;
356 lptr->defn.is_global |= DEFINED_BIT|COMMON_BIT;
358 if (!islocalchar(label[0])) {
359 prevlabel = lptr->defn.label;
360 } else {
361 nasm_error(ERR_NONFATAL, "attempt to define a local label as a "
362 "common variable");
363 return;
366 lptr->defn.segment = segment;
367 lptr->defn.offset = 0;
369 if (pass0 == 0)
370 return;
372 ofmt->symdef(lptr->defn.label, segment, size, 2,
373 special ? special : lptr->defn.special);
374 ofmt->current_dfmt->debug_deflabel(lptr->defn.label, segment, size, 2,
375 special ? special : lptr->defn.special);
378 void declare_as_global(char *label, char *special)
380 union label *lptr;
382 if (islocal(label)) {
383 nasm_error(ERR_NONFATAL, "attempt to declare local symbol `%s' as"
384 " global", label);
385 return;
387 lptr = find_label(label, 1);
388 if (!lptr)
389 return;
390 switch (lptr->defn.is_global & TYPE_MASK) {
391 case NOT_DEFINED_YET:
392 lptr->defn.is_global = GLOBAL_PLACEHOLDER;
393 lptr->defn.special = special ? perm_copy(special) : NULL;
394 break;
395 case GLOBAL_PLACEHOLDER: /* already done: silently ignore */
396 case GLOBAL_SYMBOL:
397 break;
398 case LOCAL_SYMBOL:
399 if (!(lptr->defn.is_global & EXTERN_BIT)) {
400 nasm_error(ERR_WARNING, "symbol `%s': GLOBAL directive "
401 "after symbol definition is an experimental feature", label);
402 lptr->defn.is_global = GLOBAL_SYMBOL;
404 break;
408 int init_labels(void)
410 hash_init(&ltab, HASH_LARGE);
412 ldata = lfree = (union label *)nasm_malloc(LBLK_SIZE);
413 init_block(lfree);
415 perm_head = perm_tail =
416 (struct permts *)nasm_malloc(sizeof(struct permts));
418 perm_head->next = NULL;
419 perm_head->size = PERMTS_SIZE;
420 perm_head->usage = 0;
422 prevlabel = "";
424 initialized = true;
426 return 0;
429 void cleanup_labels(void)
431 union label *lptr, *lhold;
433 initialized = false;
435 hash_free(&ltab);
437 lptr = lhold = ldata;
438 while (lptr) {
439 lptr = &lptr[LABEL_BLOCK-1];
440 lptr = lptr->admin.next;
441 nasm_free(lhold);
442 lhold = lptr;
445 while (perm_head) {
446 perm_tail = perm_head;
447 perm_head = perm_head->next;
448 nasm_free(perm_tail);
452 static void init_block(union label *blk)
454 int j;
456 for (j = 0; j < LABEL_BLOCK - 1; j++)
457 blk[j].admin.movingon = END_LIST;
458 blk[LABEL_BLOCK - 1].admin.movingon = END_BLOCK;
459 blk[LABEL_BLOCK - 1].admin.next = NULL;
462 static char *perm_copy(const char *string)
464 char *p;
465 int len = strlen(string)+1;
467 nasm_assert(len <= PERMTS_SIZE);
469 if (perm_tail->size - perm_tail->usage < len) {
470 perm_tail->next =
471 (struct permts *)nasm_malloc(sizeof(struct permts));
472 perm_tail = perm_tail->next;
473 perm_tail->next = NULL;
474 perm_tail->size = PERMTS_SIZE;
475 perm_tail->usage = 0;
477 p = perm_tail->data + perm_tail->usage;
478 memcpy(p, string, len);
479 perm_tail->usage += len;
481 return p;
484 char *local_scope(char *label)
486 return islocal(label) ? prevlabel : "";
490 * Notes regarding bug involving redefinition of external segments.
492 * Up to and including v0.97, the following code didn't work. From 0.97
493 * developers release 2 onwards, it will generate an error.
495 * EXTERN extlabel
496 * newlabel EQU extlabel + 1
498 * The results of allowing this code through are that two import records
499 * are generated, one for 'extlabel' and one for 'newlabel'.
501 * The reason for this is an inadequacy in the defined interface between
502 * the label manager and the output formats. The problem lies in how the
503 * output format driver tells that a label is an external label for which
504 * a label import record must be produced. Most (all except bin?) produce
505 * the record if the segment number of the label is not one of the internal
506 * segments that the output driver is producing.
508 * A simple fix to this would be to make the output formats keep track of
509 * which symbols they've produced import records for, and make them not
510 * produce import records for segments that are already defined.
512 * The best way, which is slightly harder but reduces duplication of code
513 * and should therefore make the entire system smaller and more stable is
514 * to change the interface between assembler, define_label(), and
515 * the output module. The changes that are needed are:
517 * The semantics of the 'isextern' flag passed to define_label() need
518 * examining. This information may or may not tell us what we need to
519 * know (ie should we be generating an import record at this point for this
520 * label). If these aren't the semantics, the semantics should be changed
521 * to this.
523 * The output module interface needs changing, so that the `isextern' flag
524 * is passed to the module, so that it can be easily tested for.