Add copyright verbiage to Perl scripts; update LICENSE
[nasm/sigaren-mirror.git] / labels.c
blob49792d2872ad6aafdae82c247a75b11b8f761840
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 license given in the file "LICENSE"
6 * distributed in the NASM archive.
7 */
9 #include "compiler.h"
11 #include <stdio.h>
12 #include <string.h>
13 #include <stdlib.h>
14 #include <inttypes.h>
16 #include "nasm.h"
17 #include "nasmlib.h"
18 #include "hashtbl.h"
21 * A local label is one that begins with exactly one period. Things
22 * that begin with _two_ periods are NASM-specific things.
24 * If TASM compatibility is enabled, a local label can also begin with
25 * @@, so @@local is a TASM compatible local label. Note that we only
26 * check for the first @ symbol, although TASM requires both.
28 #define islocal(l) \
29 (tasm_compatible_mode ? \
30 (((l)[0] == '.' || (l)[0] == '@') && (l)[1] != '.') : \
31 ((l)[0] == '.' && (l)[1] != '.'))
32 #define islocalchar(c) \
33 (tasm_compatible_mode ? \
34 ((c) == '.' || (c) == '@') : \
35 ((c) == '.'))
37 #define LABEL_BLOCK 128 /* no. of labels/block */
38 #define LBLK_SIZE (LABEL_BLOCK*sizeof(union label))
40 #define END_LIST -3 /* don't clash with NO_SEG! */
41 #define END_BLOCK -2
42 #define BOGUS_VALUE -4
44 #define PERMTS_SIZE 4096 /* size of text blocks */
45 #if (PERMTS_SIZE > IDLEN_MAX)
46 #error "IPERMTS_SIZE must be less than or equal to IDLEN_MAX"
47 #endif
49 /* values for label.defn.is_global */
50 #define DEFINED_BIT 1
51 #define GLOBAL_BIT 2
52 #define EXTERN_BIT 4
53 #define COMMON_BIT 8
55 #define NOT_DEFINED_YET 0
56 #define TYPE_MASK 3
57 #define LOCAL_SYMBOL (DEFINED_BIT)
58 #define GLOBAL_PLACEHOLDER (GLOBAL_BIT)
59 #define GLOBAL_SYMBOL (DEFINED_BIT|GLOBAL_BIT)
61 union label { /* actual label structures */
62 struct {
63 int32_t segment;
64 int64_t offset;
65 char *label, *special;
66 int is_global, is_norm;
67 } defn;
68 struct {
69 int32_t movingon;
70 int64_t dummy;
71 union label *next;
72 } admin;
75 struct permts { /* permanent text storage */
76 struct permts *next; /* for the linked list */
77 int size, usage; /* size and used space in ... */
78 char data[PERMTS_SIZE]; /* ... the data block itself */
81 extern int64_t global_offset_changed; /* defined in nasm.c */
83 static struct hash_table ltab; /* labels hash table */
84 static union label *ldata; /* all label data blocks */
85 static union label *lfree; /* labels free block */
86 static struct permts *perm_head; /* start of perm. text storage */
87 static struct permts *perm_tail; /* end of perm. text storage */
89 static void init_block(union label *blk);
90 static char *perm_copy(const char *string);
92 static char *prevlabel;
94 static bool initialized = false;
96 char lprefix[PREFIX_MAX] = { 0 };
97 char lpostfix[PREFIX_MAX] = { 0 };
100 * Internal routine: finds the `union label' corresponding to the
101 * given label name. Creates a new one, if it isn't found, and if
102 * `create' is true.
104 static union label *find_label(char *label, int create)
106 char *prev;
107 int prevlen, len;
108 union label *lptr, **lpp;
109 char label_str[IDLEN_MAX];
110 struct hash_insert ip;
112 if (islocal(label)) {
113 prev = prevlabel;
114 prevlen = strlen(prev);
115 len = strlen(label);
116 if (prevlen+len >= IDLEN_MAX)
117 return NULL; /* Error... */
118 memcpy(label_str, prev, prevlen);
119 memcpy(label_str+prevlen, label, len+1);
120 label = label_str;
121 } else {
122 prev = "";
123 prevlen = 0;
126 lpp = (union label **) hash_find(&ltab, label, &ip);
127 lptr = lpp ? *lpp : NULL;
129 if (lptr || !create)
130 return lptr;
132 /* Create a new label... */
133 if (lfree->admin.movingon == END_BLOCK) {
135 * must allocate a new block
137 lfree->admin.next =
138 (union label *)nasm_malloc(LBLK_SIZE);
139 lfree = lfree->admin.next;
140 init_block(lfree);
143 lfree->admin.movingon = BOGUS_VALUE;
144 lfree->defn.label = perm_copy(label);
145 lfree->defn.special = NULL;
146 lfree->defn.is_global = NOT_DEFINED_YET;
148 hash_add(&ip, lfree->defn.label, lfree);
149 return lfree++;
152 bool lookup_label(char *label, int32_t *segment, int64_t *offset)
154 union label *lptr;
156 if (!initialized)
157 return false;
159 lptr = find_label(label, 0);
160 if (lptr && (lptr->defn.is_global & DEFINED_BIT)) {
161 *segment = lptr->defn.segment;
162 *offset = lptr->defn.offset;
163 return true;
164 } else
165 return false;
168 bool is_extern(char *label)
170 union label *lptr;
172 if (!initialized)
173 return false;
175 lptr = find_label(label, 0);
176 return (lptr && (lptr->defn.is_global & EXTERN_BIT));
179 void redefine_label(char *label, int32_t segment, int64_t offset, char *special,
180 bool is_norm, bool isextrn, struct ofmt *ofmt,
181 efunc error)
183 union label *lptr;
184 int exi;
186 /* This routine possibly ought to check for phase errors. Most assemblers
187 * check for phase errors at this point. I don't know whether phase errors
188 * are even possible, nor whether they are checked somewhere else
191 (void)special; /* Don't warn that this parameter is unused */
192 (void)is_norm; /* Don't warn that this parameter is unused */
193 (void)isextrn; /* Don't warn that this parameter is unused */
194 (void)ofmt; /* Don't warn that this parameter is unused */
196 #ifdef DEBUG
197 #if DEBUG<3
198 if (!strncmp(label, "debugdump", 9))
199 #endif
200 error(ERR_DEBUG, "redefine_label (%s, %ld, %08lx, %s, %d, %d)",
201 label, segment, offset, special, is_norm, isextrn);
202 #endif
204 lptr = find_label(label, 1);
205 if (!lptr)
206 error(ERR_PANIC, "can't find label `%s' on pass two", label);
208 if (!islocal(label)) {
209 if (!islocalchar(*label) && lptr->defn.is_norm)
210 prevlabel = lptr->defn.label;
213 if (lptr->defn.offset != offset)
214 global_offset_changed++;
216 lptr->defn.offset = offset;
217 lptr->defn.segment = segment;
219 if (pass0 == 1) {
220 exi = !!(lptr->defn.is_global & GLOBAL_BIT);
221 if (exi) {
222 char *xsymbol;
223 int slen;
224 slen = strlen(lprefix);
225 slen += strlen(lptr->defn.label);
226 slen += strlen(lpostfix);
227 slen++; /* room for that null char */
228 xsymbol = nasm_malloc(slen);
229 snprintf(xsymbol, slen, "%s%s%s", lprefix, lptr->defn.label,
230 lpostfix);
232 ofmt->symdef(xsymbol, segment, offset, exi,
233 special ? special : lptr->defn.special);
234 ofmt->current_dfmt->debug_deflabel(xsymbol, segment, offset,
235 exi,
236 special ? special : lptr->
237 defn.special);
238 /** nasm_free(xsymbol); ! outobj.c stores the pointer; ouch!!! **/
239 } else {
240 if ((lptr->defn.is_global & (GLOBAL_BIT | EXTERN_BIT)) !=
241 EXTERN_BIT) {
242 ofmt->symdef(lptr->defn.label, segment, offset, exi,
243 special ? special : lptr->defn.special);
244 ofmt->current_dfmt->debug_deflabel(label, segment, offset,
245 exi,
246 special ? special :
247 lptr->defn.special);
251 /* if (pass0 == 1) */
254 void define_label(char *label, int32_t segment, int64_t offset, char *special,
255 bool is_norm, bool isextrn, struct ofmt *ofmt, efunc error)
257 union label *lptr;
258 int exi;
260 #ifdef DEBUG
261 #if DEBUG<3
262 if (!strncmp(label, "debugdump", 9))
263 #endif
264 error(ERR_DEBUG, "define_label (%s, %ld, %08lx, %s, %d, %d)",
265 label, segment, offset, special, is_norm, isextrn);
266 #endif
267 lptr = find_label(label, 1);
268 if (lptr->defn.is_global & DEFINED_BIT) {
269 error(ERR_NONFATAL, "symbol `%s' redefined", label);
270 return;
272 lptr->defn.is_global |= DEFINED_BIT;
273 if (isextrn)
274 lptr->defn.is_global |= EXTERN_BIT;
276 if (!islocalchar(label[0]) && is_norm) {
277 /* not local, but not special either */
278 prevlabel = lptr->defn.label;
279 } else if (islocal(label) && !*prevlabel) {
280 error(ERR_NONFATAL, "attempt to define a local label before any"
281 " non-local labels");
284 lptr->defn.segment = segment;
285 lptr->defn.offset = offset;
286 lptr->defn.is_norm = (!islocalchar(label[0]) && is_norm);
288 if (pass0 == 1 || (!is_norm && !isextrn && (segment > 0) && (segment & 1))) {
289 exi = !!(lptr->defn.is_global & GLOBAL_BIT);
290 if (exi) {
291 char *xsymbol;
292 int slen;
293 slen = strlen(lprefix);
294 slen += strlen(lptr->defn.label);
295 slen += strlen(lpostfix);
296 slen++; /* room for that null char */
297 xsymbol = nasm_malloc(slen);
298 snprintf(xsymbol, slen, "%s%s%s", lprefix, lptr->defn.label,
299 lpostfix);
301 ofmt->symdef(xsymbol, segment, offset, exi,
302 special ? special : lptr->defn.special);
303 ofmt->current_dfmt->debug_deflabel(xsymbol, segment, offset,
304 exi,
305 special ? special : lptr->
306 defn.special);
307 /** nasm_free(xsymbol); ! outobj.c stores the pointer; ouch!!! **/
308 } else {
309 if ((lptr->defn.is_global & (GLOBAL_BIT | EXTERN_BIT)) !=
310 EXTERN_BIT) {
311 ofmt->symdef(lptr->defn.label, segment, offset, exi,
312 special ? special : lptr->defn.special);
313 ofmt->current_dfmt->debug_deflabel(label, segment, offset,
314 exi,
315 special ? special :
316 lptr->defn.special);
319 } /* if (pass0 == 1) */
322 void define_common(char *label, int32_t segment, int32_t size, char *special,
323 struct ofmt *ofmt, efunc error)
325 union label *lptr;
327 lptr = find_label(label, 1);
328 if ((lptr->defn.is_global & DEFINED_BIT) &&
329 (passn == 1 || !(lptr->defn.is_global & COMMON_BIT))) {
330 error(ERR_NONFATAL, "symbol `%s' redefined", label);
331 return;
333 lptr->defn.is_global |= DEFINED_BIT|COMMON_BIT;
335 if (!islocalchar(label[0])) {
336 prevlabel = lptr->defn.label;
337 } else {
338 error(ERR_NONFATAL, "attempt to define a local label as a "
339 "common variable");
340 return;
343 lptr->defn.segment = segment;
344 lptr->defn.offset = 0;
346 if (pass0 == 0)
347 return;
349 ofmt->symdef(lptr->defn.label, segment, size, 2,
350 special ? special : lptr->defn.special);
351 ofmt->current_dfmt->debug_deflabel(lptr->defn.label, segment, size, 2,
352 special ? special : lptr->defn.
353 special);
356 void declare_as_global(char *label, char *special, efunc error)
358 union label *lptr;
360 if (islocal(label)) {
361 error(ERR_NONFATAL, "attempt to declare local symbol `%s' as"
362 " global", label);
363 return;
365 lptr = find_label(label, 1);
366 switch (lptr->defn.is_global & TYPE_MASK) {
367 case NOT_DEFINED_YET:
368 lptr->defn.is_global = GLOBAL_PLACEHOLDER;
369 lptr->defn.special = special ? perm_copy(special) : NULL;
370 break;
371 case GLOBAL_PLACEHOLDER: /* already done: silently ignore */
372 case GLOBAL_SYMBOL:
373 break;
374 case LOCAL_SYMBOL:
375 if (!(lptr->defn.is_global & EXTERN_BIT)) {
376 error(ERR_WARNING, "symbol `%s': GLOBAL directive "
377 "after symbol definition is an experimental feature", label);
378 lptr->defn.is_global = GLOBAL_SYMBOL;
380 break;
384 int init_labels(void)
386 hash_init(&ltab, HASH_LARGE);
388 ldata = lfree = (union label *)nasm_malloc(LBLK_SIZE);
389 init_block(lfree);
391 perm_head =
392 perm_tail = (struct permts *)nasm_malloc(sizeof(struct permts));
394 perm_head->next = NULL;
395 perm_head->size = PERMTS_SIZE;
396 perm_head->usage = 0;
398 prevlabel = "";
400 initialized = true;
402 return 0;
405 void cleanup_labels(void)
407 union label *lptr, *lhold;
409 initialized = false;
411 hash_free(&ltab);
413 lptr = lhold = ldata;
414 while (lptr) {
415 lptr = &lptr[LABEL_BLOCK-1];
416 lptr = lptr->admin.next;
417 nasm_free(lhold);
418 lhold = lptr;
421 while (perm_head) {
422 perm_tail = perm_head;
423 perm_head = perm_head->next;
424 nasm_free(perm_tail);
428 static void init_block(union label *blk)
430 int j;
432 for (j = 0; j < LABEL_BLOCK - 1; j++)
433 blk[j].admin.movingon = END_LIST;
434 blk[LABEL_BLOCK - 1].admin.movingon = END_BLOCK;
435 blk[LABEL_BLOCK - 1].admin.next = NULL;
438 static char *perm_copy(const char *string)
440 char *p;
441 int len = strlen(string)+1;
443 if (perm_tail->size - perm_tail->usage < len) {
444 perm_tail->next =
445 (struct permts *)nasm_malloc(sizeof(struct permts));
446 perm_tail = perm_tail->next;
447 perm_tail->next = NULL;
448 perm_tail->size = PERMTS_SIZE;
449 perm_tail->usage = 0;
451 p = perm_tail->data + perm_tail->usage;
452 memcpy(p, string, len);
453 perm_tail->usage += len;
455 return p;
458 char *local_scope(char *label)
460 return islocal(label) ? prevlabel : "";
464 * Notes regarding bug involving redefinition of external segments.
466 * Up to and including v0.97, the following code didn't work. From 0.97
467 * developers release 2 onwards, it will generate an error.
469 * EXTERN extlabel
470 * newlabel EQU extlabel + 1
472 * The results of allowing this code through are that two import records
473 * are generated, one for 'extlabel' and one for 'newlabel'.
475 * The reason for this is an inadequacy in the defined interface between
476 * the label manager and the output formats. The problem lies in how the
477 * output format driver tells that a label is an external label for which
478 * a label import record must be produced. Most (all except bin?) produce
479 * the record if the segment number of the label is not one of the internal
480 * segments that the output driver is producing.
482 * A simple fix to this would be to make the output formats keep track of
483 * which symbols they've produced import records for, and make them not
484 * produce import records for segments that are already defined.
486 * The best way, which is slightly harder but reduces duplication of code
487 * and should therefore make the entire system smaller and more stable is
488 * to change the interface between assembler, define_label(), and
489 * the output module. The changes that are needed are:
491 * The semantics of the 'isextern' flag passed to define_label() need
492 * examining. This information may or may not tell us what we need to
493 * know (ie should we be generating an import record at this point for this
494 * label). If these aren't the semantics, the semantics should be changed
495 * to this.
497 * The output module interface needs changing, so that the `isextern' flag
498 * is passed to the module, so that it can be easily tested for.