nasmlib/file: move memory-mapping functions out of file.c
[nasm.git] / asm / labels.c
blob224d695958c409a377922f2944c3b821509d9936
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>
44 #include "nasm.h"
45 #include "nasmlib.h"
46 #include "hashtbl.h"
47 #include "labels.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 * Emit a symdef to the output and the debug format backends.
131 static void out_symdef(char *name, int32_t segment, int64_t offset,
132 int is_global, char *special)
134 ofmt->symdef(name, segment, offset, is_global, special);
137 * NASM special symbols are not passed to the debug format; none
138 * of the current backends want to see them.
140 if (!(name[0] == '.' && name[1] == '.' && name[2] != '@'))
141 dfmt->debug_deflabel(name, segment, offset, is_global, special);
145 * Internal routine: finds the `union label' corresponding to the
146 * given label name. Creates a new one, if it isn't found, and if
147 * `create' is true.
149 static union label *find_label(char *label, int create, int *created)
151 char *prev;
152 int prevlen, len;
153 union label *lptr, **lpp;
154 char label_str[IDLEN_MAX];
155 struct hash_insert ip;
157 if (islocal(label)) {
158 prev = prevlabel;
159 prevlen = strlen(prev);
160 len = strlen(label);
161 if (prevlen + len >= IDLEN_MAX) {
162 nasm_error(ERR_NONFATAL, "identifier length exceed %i bytes",
163 IDLEN_MAX);
164 return NULL;
166 memcpy(label_str, prev, prevlen);
167 memcpy(label_str+prevlen, label, len+1);
168 label = label_str;
169 } else {
170 prev = "";
171 prevlen = 0;
174 lpp = (union label **) hash_find(&ltab, label, &ip);
175 lptr = lpp ? *lpp : NULL;
177 if (lptr || !create) {
178 if (created)
179 *created = 0;
180 return lptr;
183 /* Create a new label... */
184 if (lfree->admin.movingon == END_BLOCK) {
186 * must allocate a new block
188 lfree->admin.next = (union label *)nasm_malloc(LBLK_SIZE);
189 lfree = lfree->admin.next;
190 init_block(lfree);
193 if (created)
194 *created = 1;
196 lfree->admin.movingon = BOGUS_VALUE;
197 lfree->defn.label = perm_copy(label);
198 lfree->defn.special = NULL;
199 lfree->defn.is_global = NOT_DEFINED_YET;
201 hash_add(&ip, lfree->defn.label, lfree);
202 return lfree++;
205 bool lookup_label(char *label, int32_t *segment, int64_t *offset)
207 union label *lptr;
209 if (!initialized)
210 return false;
212 lptr = find_label(label, 0, NULL);
213 if (lptr && (lptr->defn.is_global & DEFINED_BIT)) {
214 *segment = lptr->defn.segment;
215 *offset = lptr->defn.offset;
216 return true;
219 return false;
222 bool is_extern(char *label)
224 union label *lptr;
226 if (!initialized)
227 return false;
229 lptr = find_label(label, 0, NULL);
230 return (lptr && (lptr->defn.is_global & EXTERN_BIT));
233 void redefine_label(char *label, int32_t segment, int64_t offset, char *special,
234 bool is_norm, bool isextrn)
236 union label *lptr;
237 int exi, created;
239 /* This routine possibly ought to check for phase errors. Most assemblers
240 * check for phase errors at this point. I don't know whether phase errors
241 * are even possible, nor whether they are checked somewhere else
244 (void)special; /* Don't warn that this parameter is unused */
245 (void)is_norm; /* Don't warn that this parameter is unused */
246 (void)isextrn; /* Don't warn that this parameter is unused */
248 #ifdef DEBUG
249 #if DEBUG < 3
250 if (!strncmp(label, "debugdump", 9))
251 #endif
252 nasm_error(ERR_DEBUG, "redefine_label (%s, %"PRIx32", %"PRIx64", %s, %d, %d)",
253 label, segment, offset, special, is_norm, isextrn);
254 #endif
256 lptr = find_label(label, 1, &created);
257 if (!lptr)
258 nasm_panic(0, "can't find label `%s' on pass two", label);
260 if (created)
261 nasm_error(ERR_WARNING, "label `%s' defined on pass two", label);
263 if (!islocal(label)) {
264 if (!islocalchar(*label) && lptr->defn.is_norm)
265 prevlabel = lptr->defn.label;
268 if (lptr->defn.offset != offset)
269 global_offset_changed++;
271 lptr->defn.offset = offset;
272 lptr->defn.segment = segment;
274 if (pass0 == 1) {
275 exi = !!(lptr->defn.is_global & GLOBAL_BIT);
276 if (exi) {
277 char *xsymbol;
278 int slen;
279 slen = strlen(lprefix);
280 slen += strlen(lptr->defn.label);
281 slen += strlen(lpostfix);
282 slen++; /* room for that null char */
283 xsymbol = nasm_malloc(slen);
284 snprintf(xsymbol, slen, "%s%s%s", lprefix, lptr->defn.label,
285 lpostfix);
287 out_symdef(xsymbol, segment, offset, exi,
288 special ? special : lptr->defn.special);
289 /** nasm_free(xsymbol); ! outobj.c stores the pointer; ouch!!! **/
290 } else {
291 if ((lptr->defn.is_global & (GLOBAL_BIT | EXTERN_BIT)) != EXTERN_BIT) {
292 out_symdef(lptr->defn.label, segment, offset, exi,
293 special ? special : lptr->defn.special);
296 } /* if (pass0 == 1) */
299 void define_label(char *label, int32_t segment, int64_t offset, char *special,
300 bool is_norm, bool isextrn)
302 union label *lptr;
303 int exi;
305 #ifdef DEBUG
306 #if DEBUG<3
307 if (!strncmp(label, "debugdump", 9))
308 #endif
309 nasm_error(ERR_DEBUG, "define_label (%s, %"PRIx32", %"PRIx64", %s, %d, %d)",
310 label, segment, offset, special, is_norm, isextrn);
311 #endif
312 lptr = find_label(label, 1, NULL);
313 if (!lptr)
314 return;
315 if (lptr->defn.is_global & DEFINED_BIT) {
316 nasm_error(ERR_NONFATAL, "symbol `%s' redefined", label);
317 return;
319 lptr->defn.is_global |= DEFINED_BIT;
320 if (isextrn)
321 lptr->defn.is_global |= EXTERN_BIT;
323 if (!islocalchar(label[0]) && is_norm) {
324 /* not local, but not special either */
325 prevlabel = lptr->defn.label;
326 } else if (islocal(label) && !*prevlabel) {
327 nasm_error(ERR_NONFATAL, "attempt to define a local label before any"
328 " non-local labels");
331 lptr->defn.segment = segment;
332 lptr->defn.offset = offset;
333 lptr->defn.is_norm = (!islocalchar(label[0]) && is_norm);
335 if (pass0 == 1 || (!is_norm && !isextrn && (segment > 0) && (segment & 1))) {
336 exi = !!(lptr->defn.is_global & GLOBAL_BIT);
337 if (exi) {
338 char *xsymbol;
339 int slen;
340 slen = strlen(lprefix);
341 slen += strlen(lptr->defn.label);
342 slen += strlen(lpostfix);
343 slen++; /* room for that null char */
344 xsymbol = nasm_malloc(slen);
345 snprintf(xsymbol, slen, "%s%s%s", lprefix, lptr->defn.label,
346 lpostfix);
348 out_symdef(xsymbol, segment, offset, exi,
349 special ? special : lptr->defn.special);
350 /** nasm_free(xsymbol); ! outobj.c stores the pointer; ouch!!! **/
351 } else {
352 if ((lptr->defn.is_global & (GLOBAL_BIT | EXTERN_BIT)) != EXTERN_BIT) {
353 out_symdef(lptr->defn.label, segment, offset, exi,
354 special ? special : lptr->defn.special);
357 } /* if (pass0 == 1) */
360 void define_common(char *label, int32_t segment, int32_t size, char *special)
362 union label *lptr;
364 lptr = find_label(label, 1, NULL);
365 if (!lptr)
366 return;
367 if ((lptr->defn.is_global & DEFINED_BIT) &&
368 (passn == 1 || !(lptr->defn.is_global & COMMON_BIT))) {
369 nasm_error(ERR_NONFATAL, "symbol `%s' redefined", label);
370 return;
372 lptr->defn.is_global |= DEFINED_BIT|COMMON_BIT;
374 if (!islocalchar(label[0])) {
375 prevlabel = lptr->defn.label;
376 } else {
377 nasm_error(ERR_NONFATAL, "attempt to define a local label as a "
378 "common variable");
379 return;
382 lptr->defn.segment = segment;
383 lptr->defn.offset = 0;
385 if (pass0 == 0)
386 return;
388 out_symdef(lptr->defn.label, segment, size, 2,
389 special ? special : lptr->defn.special);
392 void declare_as_global(char *label, char *special)
394 union label *lptr;
396 if (islocal(label)) {
397 nasm_error(ERR_NONFATAL, "attempt to declare local symbol `%s' as"
398 " global", label);
399 return;
401 lptr = find_label(label, 1, NULL);
402 if (!lptr)
403 return;
404 switch (lptr->defn.is_global & TYPE_MASK) {
405 case NOT_DEFINED_YET:
406 lptr->defn.is_global = GLOBAL_PLACEHOLDER;
407 lptr->defn.special = special ? perm_copy(special) : NULL;
408 break;
409 case GLOBAL_PLACEHOLDER: /* already done: silently ignore */
410 case GLOBAL_SYMBOL:
411 break;
412 case LOCAL_SYMBOL:
413 if (!(lptr->defn.is_global & EXTERN_BIT)) {
414 nasm_error(ERR_WARNING, "symbol `%s': GLOBAL directive "
415 "after symbol definition is an experimental feature", label);
416 lptr->defn.is_global = GLOBAL_SYMBOL;
418 break;
422 int init_labels(void)
424 hash_init(&ltab, HASH_LARGE);
426 ldata = lfree = (union label *)nasm_malloc(LBLK_SIZE);
427 init_block(lfree);
429 perm_head = perm_tail =
430 (struct permts *)nasm_malloc(sizeof(struct permts));
432 perm_head->next = NULL;
433 perm_head->size = PERMTS_SIZE;
434 perm_head->usage = 0;
436 prevlabel = "";
438 initialized = true;
440 return 0;
443 void cleanup_labels(void)
445 union label *lptr, *lhold;
447 initialized = false;
449 hash_free(&ltab);
451 lptr = lhold = ldata;
452 while (lptr) {
453 lptr = &lptr[LABEL_BLOCK-1];
454 lptr = lptr->admin.next;
455 nasm_free(lhold);
456 lhold = lptr;
459 while (perm_head) {
460 perm_tail = perm_head;
461 perm_head = perm_head->next;
462 nasm_free(perm_tail);
466 static void init_block(union label *blk)
468 int j;
470 for (j = 0; j < LABEL_BLOCK - 1; j++)
471 blk[j].admin.movingon = END_LIST;
472 blk[LABEL_BLOCK - 1].admin.movingon = END_BLOCK;
473 blk[LABEL_BLOCK - 1].admin.next = NULL;
476 static char *perm_copy(const char *string)
478 char *p;
479 int len = strlen(string)+1;
481 nasm_assert(len <= PERMTS_SIZE);
483 if (perm_tail->size - perm_tail->usage < len) {
484 perm_tail->next =
485 (struct permts *)nasm_malloc(sizeof(struct permts));
486 perm_tail = perm_tail->next;
487 perm_tail->next = NULL;
488 perm_tail->size = PERMTS_SIZE;
489 perm_tail->usage = 0;
491 p = perm_tail->data + perm_tail->usage;
492 memcpy(p, string, len);
493 perm_tail->usage += len;
495 return p;
498 char *local_scope(char *label)
500 return islocal(label) ? prevlabel : "";
504 * Notes regarding bug involving redefinition of external segments.
506 * Up to and including v0.97, the following code didn't work. From 0.97
507 * developers release 2 onwards, it will generate an error.
509 * EXTERN extlabel
510 * newlabel EQU extlabel + 1
512 * The results of allowing this code through are that two import records
513 * are generated, one for 'extlabel' and one for 'newlabel'.
515 * The reason for this is an inadequacy in the defined interface between
516 * the label manager and the output formats. The problem lies in how the
517 * output format driver tells that a label is an external label for which
518 * a label import record must be produced. Most (all except bin?) produce
519 * the record if the segment number of the label is not one of the internal
520 * segments that the output driver is producing.
522 * A simple fix to this would be to make the output formats keep track of
523 * which symbols they've produced import records for, and make them not
524 * produce import records for segments that are already defined.
526 * The best way, which is slightly harder but reduces duplication of code
527 * and should therefore make the entire system smaller and more stable is
528 * to change the interface between assembler, define_label(), and
529 * the output module. The changes that are needed are:
531 * The semantics of the 'isextern' flag passed to define_label() need
532 * examining. This information may or may not tell us what we need to
533 * know (ie should we be generating an import record at this point for this
534 * label). If these aren't the semantics, the semantics should be changed
535 * to this.
537 * The output module interface needs changing, so that the `isextern' flag
538 * is passed to the module, so that it can be easily tested for.