preproc.c: fix assignment incorrectly coded as ==
[nasm.git] / labels.c
blobd7cdb8c33f29603b1b372e15d7a3794e734e48ee
1 /* ----------------------------------------------------------------------- *
3 * Copyright 1996-2016 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 * Emit a symdef to the output and the debug format backends.
132 static void out_symdef(char *name, int32_t segment, int64_t offset,
133 int is_global, char *special)
135 ofmt->symdef(name, segment, offset, is_global, special);
138 * NASM special symbols are not passed to the debug format; none
139 * of the current backends want to see them.
141 if (!(name[0] == '.' && name[1] == '.' && name[2] != '@'))
142 dfmt->debug_deflabel(name, segment, offset, is_global, special);
146 * Internal routine: finds the `union label' corresponding to the
147 * given label name. Creates a new one, if it isn't found, and if
148 * `create' is true.
150 static union label *find_label(char *label, int create)
152 char *prev;
153 int prevlen, len;
154 union label *lptr, **lpp;
155 char label_str[IDLEN_MAX];
156 struct hash_insert ip;
158 if (islocal(label)) {
159 prev = prevlabel;
160 prevlen = strlen(prev);
161 len = strlen(label);
162 if (prevlen + len >= IDLEN_MAX) {
163 nasm_error(ERR_NONFATAL, "identifier length exceed %i bytes",
164 IDLEN_MAX);
165 return NULL;
167 memcpy(label_str, prev, prevlen);
168 memcpy(label_str+prevlen, label, len+1);
169 label = label_str;
170 } else {
171 prev = "";
172 prevlen = 0;
175 lpp = (union label **) hash_find(&ltab, label, &ip);
176 lptr = lpp ? *lpp : NULL;
178 if (lptr || !create)
179 return lptr;
181 /* Create a new label... */
182 if (lfree->admin.movingon == END_BLOCK) {
184 * must allocate a new block
186 lfree->admin.next = (union label *)nasm_malloc(LBLK_SIZE);
187 lfree = lfree->admin.next;
188 init_block(lfree);
191 lfree->admin.movingon = BOGUS_VALUE;
192 lfree->defn.label = perm_copy(label);
193 lfree->defn.special = NULL;
194 lfree->defn.is_global = NOT_DEFINED_YET;
196 hash_add(&ip, lfree->defn.label, lfree);
197 return lfree++;
200 bool lookup_label(char *label, int32_t *segment, int64_t *offset)
202 union label *lptr;
204 if (!initialized)
205 return false;
207 lptr = find_label(label, 0);
208 if (lptr && (lptr->defn.is_global & DEFINED_BIT)) {
209 *segment = lptr->defn.segment;
210 *offset = lptr->defn.offset;
211 return true;
214 return false;
217 bool is_extern(char *label)
219 union label *lptr;
221 if (!initialized)
222 return false;
224 lptr = find_label(label, 0);
225 return (lptr && (lptr->defn.is_global & EXTERN_BIT));
228 void redefine_label(char *label, int32_t segment, int64_t offset, char *special,
229 bool is_norm, bool isextrn)
231 union label *lptr;
232 int exi;
234 /* This routine possibly ought to check for phase errors. Most assemblers
235 * check for phase errors at this point. I don't know whether phase errors
236 * are even possible, nor whether they are checked somewhere else
239 (void)special; /* Don't warn that this parameter is unused */
240 (void)is_norm; /* Don't warn that this parameter is unused */
241 (void)isextrn; /* Don't warn that this parameter is unused */
243 #ifdef DEBUG
244 #if DEBUG < 3
245 if (!strncmp(label, "debugdump", 9))
246 #endif
247 nasm_error(ERR_DEBUG, "redefine_label (%s, %"PRIx32", %"PRIx64", %s, %d, %d)",
248 label, segment, offset, special, is_norm, isextrn);
249 #endif
251 lptr = find_label(label, 1);
252 if (!lptr)
253 nasm_panic(0, "can't find label `%s' on pass two", label);
255 if (!islocal(label)) {
256 if (!islocalchar(*label) && lptr->defn.is_norm)
257 prevlabel = lptr->defn.label;
260 if (lptr->defn.offset != offset)
261 global_offset_changed++;
263 lptr->defn.offset = offset;
264 lptr->defn.segment = segment;
266 if (pass0 == 1) {
267 exi = !!(lptr->defn.is_global & GLOBAL_BIT);
268 if (exi) {
269 char *xsymbol;
270 int slen;
271 slen = strlen(lprefix);
272 slen += strlen(lptr->defn.label);
273 slen += strlen(lpostfix);
274 slen++; /* room for that null char */
275 xsymbol = nasm_malloc(slen);
276 snprintf(xsymbol, slen, "%s%s%s", lprefix, lptr->defn.label,
277 lpostfix);
279 out_symdef(xsymbol, segment, offset, exi,
280 special ? special : lptr->defn.special);
281 /** nasm_free(xsymbol); ! outobj.c stores the pointer; ouch!!! **/
282 } else {
283 if ((lptr->defn.is_global & (GLOBAL_BIT | EXTERN_BIT)) != EXTERN_BIT) {
284 out_symdef(lptr->defn.label, segment, offset, exi,
285 special ? special : lptr->defn.special);
288 } /* if (pass0 == 1) */
291 void define_label(char *label, int32_t segment, int64_t offset, char *special,
292 bool is_norm, bool isextrn)
294 union label *lptr;
295 int exi;
297 #ifdef DEBUG
298 #if DEBUG<3
299 if (!strncmp(label, "debugdump", 9))
300 #endif
301 nasm_error(ERR_DEBUG, "define_label (%s, %"PRIx32", %"PRIx64", %s, %d, %d)",
302 label, segment, offset, special, is_norm, isextrn);
303 #endif
304 lptr = find_label(label, 1);
305 if (!lptr)
306 return;
307 if (lptr->defn.is_global & DEFINED_BIT) {
308 nasm_error(ERR_NONFATAL, "symbol `%s' redefined", label);
309 return;
311 lptr->defn.is_global |= DEFINED_BIT;
312 if (isextrn)
313 lptr->defn.is_global |= EXTERN_BIT;
315 if (!islocalchar(label[0]) && is_norm) {
316 /* not local, but not special either */
317 prevlabel = lptr->defn.label;
318 } else if (islocal(label) && !*prevlabel) {
319 nasm_error(ERR_NONFATAL, "attempt to define a local label before any"
320 " non-local labels");
323 lptr->defn.segment = segment;
324 lptr->defn.offset = offset;
325 lptr->defn.is_norm = (!islocalchar(label[0]) && is_norm);
327 if (pass0 == 1 || (!is_norm && !isextrn && (segment > 0) && (segment & 1))) {
328 exi = !!(lptr->defn.is_global & GLOBAL_BIT);
329 if (exi) {
330 char *xsymbol;
331 int slen;
332 slen = strlen(lprefix);
333 slen += strlen(lptr->defn.label);
334 slen += strlen(lpostfix);
335 slen++; /* room for that null char */
336 xsymbol = nasm_malloc(slen);
337 snprintf(xsymbol, slen, "%s%s%s", lprefix, lptr->defn.label,
338 lpostfix);
340 out_symdef(xsymbol, segment, offset, exi,
341 special ? special : lptr->defn.special);
342 /** nasm_free(xsymbol); ! outobj.c stores the pointer; ouch!!! **/
343 } else {
344 if ((lptr->defn.is_global & (GLOBAL_BIT | EXTERN_BIT)) != EXTERN_BIT) {
345 out_symdef(lptr->defn.label, segment, offset, exi,
346 special ? special : lptr->defn.special);
349 } /* if (pass0 == 1) */
352 void define_common(char *label, int32_t segment, int32_t size, char *special)
354 union label *lptr;
356 lptr = find_label(label, 1);
357 if (!lptr)
358 return;
359 if ((lptr->defn.is_global & DEFINED_BIT) &&
360 (passn == 1 || !(lptr->defn.is_global & COMMON_BIT))) {
361 nasm_error(ERR_NONFATAL, "symbol `%s' redefined", label);
362 return;
364 lptr->defn.is_global |= DEFINED_BIT|COMMON_BIT;
366 if (!islocalchar(label[0])) {
367 prevlabel = lptr->defn.label;
368 } else {
369 nasm_error(ERR_NONFATAL, "attempt to define a local label as a "
370 "common variable");
371 return;
374 lptr->defn.segment = segment;
375 lptr->defn.offset = 0;
377 if (pass0 == 0)
378 return;
380 out_symdef(lptr->defn.label, segment, size, 2,
381 special ? special : lptr->defn.special);
384 void declare_as_global(char *label, char *special)
386 union label *lptr;
388 if (islocal(label)) {
389 nasm_error(ERR_NONFATAL, "attempt to declare local symbol `%s' as"
390 " global", label);
391 return;
393 lptr = find_label(label, 1);
394 if (!lptr)
395 return;
396 switch (lptr->defn.is_global & TYPE_MASK) {
397 case NOT_DEFINED_YET:
398 lptr->defn.is_global = GLOBAL_PLACEHOLDER;
399 lptr->defn.special = special ? perm_copy(special) : NULL;
400 break;
401 case GLOBAL_PLACEHOLDER: /* already done: silently ignore */
402 case GLOBAL_SYMBOL:
403 break;
404 case LOCAL_SYMBOL:
405 if (!(lptr->defn.is_global & EXTERN_BIT)) {
406 nasm_error(ERR_WARNING, "symbol `%s': GLOBAL directive "
407 "after symbol definition is an experimental feature", label);
408 lptr->defn.is_global = GLOBAL_SYMBOL;
410 break;
414 int init_labels(void)
416 hash_init(&ltab, HASH_LARGE);
418 ldata = lfree = (union label *)nasm_malloc(LBLK_SIZE);
419 init_block(lfree);
421 perm_head = perm_tail =
422 (struct permts *)nasm_malloc(sizeof(struct permts));
424 perm_head->next = NULL;
425 perm_head->size = PERMTS_SIZE;
426 perm_head->usage = 0;
428 prevlabel = "";
430 initialized = true;
432 return 0;
435 void cleanup_labels(void)
437 union label *lptr, *lhold;
439 initialized = false;
441 hash_free(&ltab);
443 lptr = lhold = ldata;
444 while (lptr) {
445 lptr = &lptr[LABEL_BLOCK-1];
446 lptr = lptr->admin.next;
447 nasm_free(lhold);
448 lhold = lptr;
451 while (perm_head) {
452 perm_tail = perm_head;
453 perm_head = perm_head->next;
454 nasm_free(perm_tail);
458 static void init_block(union label *blk)
460 int j;
462 for (j = 0; j < LABEL_BLOCK - 1; j++)
463 blk[j].admin.movingon = END_LIST;
464 blk[LABEL_BLOCK - 1].admin.movingon = END_BLOCK;
465 blk[LABEL_BLOCK - 1].admin.next = NULL;
468 static char *perm_copy(const char *string)
470 char *p;
471 int len = strlen(string)+1;
473 nasm_assert(len <= PERMTS_SIZE);
475 if (perm_tail->size - perm_tail->usage < len) {
476 perm_tail->next =
477 (struct permts *)nasm_malloc(sizeof(struct permts));
478 perm_tail = perm_tail->next;
479 perm_tail->next = NULL;
480 perm_tail->size = PERMTS_SIZE;
481 perm_tail->usage = 0;
483 p = perm_tail->data + perm_tail->usage;
484 memcpy(p, string, len);
485 perm_tail->usage += len;
487 return p;
490 char *local_scope(char *label)
492 return islocal(label) ? prevlabel : "";
496 * Notes regarding bug involving redefinition of external segments.
498 * Up to and including v0.97, the following code didn't work. From 0.97
499 * developers release 2 onwards, it will generate an error.
501 * EXTERN extlabel
502 * newlabel EQU extlabel + 1
504 * The results of allowing this code through are that two import records
505 * are generated, one for 'extlabel' and one for 'newlabel'.
507 * The reason for this is an inadequacy in the defined interface between
508 * the label manager and the output formats. The problem lies in how the
509 * output format driver tells that a label is an external label for which
510 * a label import record must be produced. Most (all except bin?) produce
511 * the record if the segment number of the label is not one of the internal
512 * segments that the output driver is producing.
514 * A simple fix to this would be to make the output formats keep track of
515 * which symbols they've produced import records for, and make them not
516 * produce import records for segments that are already defined.
518 * The best way, which is slightly harder but reduces duplication of code
519 * and should therefore make the entire system smaller and more stable is
520 * to change the interface between assembler, define_label(), and
521 * the output module. The changes that are needed are:
523 * The semantics of the 'isextern' flag passed to define_label() need
524 * examining. This information may or may not tell us what we need to
525 * know (ie should we be generating an import record at this point for this
526 * label). If these aren't the semantics, the semantics should be changed
527 * to this.
529 * The output module interface needs changing, so that the `isextern' flag
530 * is passed to the module, so that it can be easily tested for.