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 licence given in the file "Licence"
6 * distributed in the NASM archive.
19 * A local label is one that begins with exactly one period. Things
20 * that begin with _two_ periods are NASM-specific things.
22 * If TASM compatibility is enabled, a local label can also begin with
23 * @@, so @@local is a TASM compatible local label. Note that we only
24 * check for the first @ symbol, although TASM requires both.
27 (tasm_compatible_mode ? \
28 (((l)[0] == '.' || (l)[0] == '@') && (l)[1] != '.') : \
29 ((l)[0] == '.' && (l)[1] != '.'))
30 #define islocalchar(c) \
31 (tasm_compatible_mode ? \
32 ((c) == '.' || (c) == '@') : \
35 #define LABEL_BLOCK 128 /* no. of labels/block */
36 #define LBLK_SIZE (LABEL_BLOCK*sizeof(union label))
38 #define END_LIST -3 /* don't clash with NO_SEG! */
40 #define BOGUS_VALUE -4
42 #define PERMTS_SIZE 4096 /* size of text blocks */
43 #if (PERMTS_SIZE > IDLEN_MAX)
44 #error "IPERMTS_SIZE must be less than or equal to IDLEN_MAX"
47 /* values for label.defn.is_global */
52 #define NOT_DEFINED_YET 0
54 #define LOCAL_SYMBOL (DEFINED_BIT)
55 #define GLOBAL_PLACEHOLDER (GLOBAL_BIT)
56 #define GLOBAL_SYMBOL (DEFINED_BIT|GLOBAL_BIT)
58 union label { /* actual label structures */
60 int32_t segment, offset;
61 char *label, *special;
62 int is_global, is_norm;
65 int32_t movingon, dummy;
70 struct permts { /* permanent text storage */
71 struct permts *next; /* for the linked list */
72 int size, usage; /* size and used space in ... */
73 char data[PERMTS_SIZE]; /* ... the data block itself */
76 extern int global_offset_changed; /* defined in nasm.c */
78 static struct hash_table *ltab; /* labels hash table */
79 static union label *ldata; /* all label data blocks */
80 static union label *lfree; /* labels free block */
81 static struct permts *perm_head; /* start of perm. text storage */
82 static struct permts *perm_tail; /* end of perm. text storage */
84 static void init_block(union label *blk);
85 static char *perm_copy(const char *string);
87 static char *prevlabel;
89 static int initialized = FALSE;
91 char lprefix[PREFIX_MAX] = { 0 };
92 char lpostfix[PREFIX_MAX] = { 0 };
95 * Internal routine: finds the `union label' corresponding to the
96 * given label name. Creates a new one, if it isn't found, and if
99 static union label *find_label(char *label, int create)
103 union label *lptr, **lpp;
104 char label_str[IDLEN_MAX];
105 struct hash_insert ip;
107 if (islocal(label)) {
109 prevlen = strlen(prev);
111 if (prevlen+len >= IDLEN_MAX)
112 return NULL; /* Error... */
113 memcpy(label_str, prev, prevlen);
114 memcpy(label_str+prevlen, label, len+1);
121 lpp = (union label **) hash_find(ltab, label, &ip);
122 lptr = lpp ? *lpp : NULL;
127 /* Create a new label... */
128 if (lfree->admin.movingon == END_BLOCK) {
130 * must allocate a new block
133 (union label *)nasm_malloc(LBLK_SIZE);
134 lfree = lfree->admin.next;
138 lfree->admin.movingon = BOGUS_VALUE;
139 lfree->defn.label = perm_copy(label);
140 lfree->defn.special = NULL;
141 lfree->defn.is_global = NOT_DEFINED_YET;
143 hash_add(&ip, lfree->defn.label, lfree);
147 int lookup_label(char *label, int32_t *segment, int32_t *offset)
154 lptr = find_label(label, 0);
155 if (lptr && (lptr->defn.is_global & DEFINED_BIT)) {
156 *segment = lptr->defn.segment;
157 *offset = lptr->defn.offset;
163 int is_extern(char *label)
170 lptr = find_label(label, 0);
171 if (lptr && (lptr->defn.is_global & EXTERN_BIT))
177 void redefine_label(char *label, int32_t segment, int32_t offset, char *special,
178 int is_norm, int isextrn, struct ofmt *ofmt,
184 /* This routine possibly ought to check for phase errors. Most assemblers
185 * check for phase errors at this point. I don't know whether phase errors
186 * are even possible, nor whether they are checked somewhere else
189 (void)segment; /* Don't warn that this parameter is unused */
190 (void)special; /* Don't warn that this parameter is unused */
191 (void)is_norm; /* Don't warn that this parameter is unused */
192 (void)isextrn; /* Don't warn that this parameter is unused */
193 (void)ofmt; /* Don't warn that this parameter is unused */
197 if (!strncmp(label, "debugdump", 9))
199 error(ERR_DEBUG, "redefine_label (%s, %ld, %08lx, %s, %d, %d)",
200 label, segment, offset, special, is_norm, isextrn);
203 lptr = find_label(label, 1);
205 error(ERR_PANIC, "can't find label `%s' on pass two", label);
207 if (!islocal(label)) {
208 if (!islocalchar(*label) && lptr->defn.is_norm)
209 prevlabel = lptr->defn.label;
212 global_offset_changed |= (lptr->defn.offset != offset);
213 lptr->defn.offset = offset;
216 exi = !!(lptr->defn.is_global & GLOBAL_BIT);
220 slen = strlen(lprefix);
221 slen += strlen(lptr->defn.label);
222 slen += strlen(lpostfix);
223 slen++; /* room for that null char */
224 xsymbol = nasm_malloc(slen);
225 snprintf(xsymbol, slen, "%s%s%s", lprefix, lptr->defn.label,
228 ofmt->symdef(xsymbol, segment, offset, exi,
229 special ? special : lptr->defn.special);
230 ofmt->current_dfmt->debug_deflabel(xsymbol, segment, offset,
232 special ? special : lptr->
234 /** nasm_free(xsymbol); ! outobj.c stores the pointer; ouch!!! **/
236 if ((lptr->defn.is_global & (GLOBAL_BIT | EXTERN_BIT)) !=
238 ofmt->symdef(lptr->defn.label, segment, offset, exi,
239 special ? special : lptr->defn.special);
240 ofmt->current_dfmt->debug_deflabel(label, segment, offset,
247 /* if (pass0 == 1) */
250 void define_label(char *label, int32_t segment, int32_t offset, char *special,
251 int is_norm, int isextrn, struct ofmt *ofmt, efunc error)
258 if (!strncmp(label, "debugdump", 9))
260 error(ERR_DEBUG, "define_label (%s, %ld, %08lx, %s, %d, %d)",
261 label, segment, offset, special, is_norm, isextrn);
263 lptr = find_label(label, 1);
264 if (lptr->defn.is_global & DEFINED_BIT) {
265 error(ERR_NONFATAL, "symbol `%s' redefined", label);
268 lptr->defn.is_global |= DEFINED_BIT;
270 lptr->defn.is_global |= EXTERN_BIT;
272 if (!islocalchar(label[0]) && is_norm) /* not local, but not special either */
273 prevlabel = lptr->defn.label;
274 else if (islocal(label) && !*prevlabel) {
275 error(ERR_NONFATAL, "attempt to define a local label before any"
276 " non-local labels");
279 lptr->defn.segment = segment;
280 lptr->defn.offset = offset;
281 lptr->defn.is_norm = (!islocalchar(label[0]) && is_norm);
283 if (pass0 == 1 || (!is_norm && !isextrn && (segment & 1))) {
284 exi = !!(lptr->defn.is_global & GLOBAL_BIT);
288 slen = strlen(lprefix);
289 slen += strlen(lptr->defn.label);
290 slen += strlen(lpostfix);
291 slen++; /* room for that null char */
292 xsymbol = nasm_malloc(slen);
293 snprintf(xsymbol, slen, "%s%s%s", lprefix, lptr->defn.label,
296 ofmt->symdef(xsymbol, segment, offset, exi,
297 special ? special : lptr->defn.special);
298 ofmt->current_dfmt->debug_deflabel(xsymbol, segment, offset,
300 special ? special : lptr->
302 /** nasm_free(xsymbol); ! outobj.c stores the pointer; ouch!!! **/
304 if ((lptr->defn.is_global & (GLOBAL_BIT | EXTERN_BIT)) !=
306 ofmt->symdef(lptr->defn.label, segment, offset, exi,
307 special ? special : lptr->defn.special);
308 ofmt->current_dfmt->debug_deflabel(label, segment, offset,
314 } /* if (pass0 == 1) */
317 void define_common(char *label, int32_t segment, int32_t size, char *special,
318 struct ofmt *ofmt, efunc error)
322 lptr = find_label(label, 1);
323 if (lptr->defn.is_global & DEFINED_BIT) {
324 error(ERR_NONFATAL, "symbol `%s' redefined", label);
327 lptr->defn.is_global |= DEFINED_BIT;
329 if (!islocalchar(label[0])) /* not local, but not special either */
330 prevlabel = lptr->defn.label;
332 error(ERR_NONFATAL, "attempt to define a local label as a "
335 lptr->defn.segment = segment;
336 lptr->defn.offset = 0;
338 ofmt->symdef(lptr->defn.label, segment, size, 2,
339 special ? special : lptr->defn.special);
340 ofmt->current_dfmt->debug_deflabel(lptr->defn.label, segment, size, 2,
341 special ? special : lptr->defn.
345 void declare_as_global(char *label, char *special, efunc error)
349 if (islocal(label)) {
350 error(ERR_NONFATAL, "attempt to declare local symbol `%s' as"
354 lptr = find_label(label, 1);
355 switch (lptr->defn.is_global & TYPE_MASK) {
356 case NOT_DEFINED_YET:
357 lptr->defn.is_global = GLOBAL_PLACEHOLDER;
358 lptr->defn.special = special ? perm_copy(special) : NULL;
360 case GLOBAL_PLACEHOLDER: /* already done: silently ignore */
364 if (!lptr->defn.is_global & EXTERN_BIT)
365 error(ERR_NONFATAL, "symbol `%s': GLOBAL directive must"
366 " appear before symbol definition", label);
371 int init_labels(void)
375 ldata = lfree = (union label *)nasm_malloc(LBLK_SIZE);
379 perm_tail = (struct permts *)nasm_malloc(sizeof(struct permts));
381 perm_head->next = NULL;
382 perm_head->size = PERMTS_SIZE;
383 perm_head->usage = 0;
392 void cleanup_labels(void)
394 union label *lptr, *lhold;
400 lptr = lhold = ldata;
402 lptr = &lptr[LABEL_BLOCK-1];
403 lptr = lptr->admin.next;
409 perm_tail = perm_head;
410 perm_head = perm_head->next;
411 nasm_free(perm_tail);
415 static void init_block(union label *blk)
419 for (j = 0; j < LABEL_BLOCK - 1; j++)
420 blk[j].admin.movingon = END_LIST;
421 blk[LABEL_BLOCK - 1].admin.movingon = END_BLOCK;
422 blk[LABEL_BLOCK - 1].admin.next = NULL;
425 static char *perm_copy(const char *string)
428 int len = strlen(string)+1;
430 if (perm_tail->size - perm_tail->usage < len) {
432 (struct permts *)nasm_malloc(sizeof(struct permts));
433 perm_tail = perm_tail->next;
434 perm_tail->next = NULL;
435 perm_tail->size = PERMTS_SIZE;
436 perm_tail->usage = 0;
438 p = perm_tail->data + perm_tail->usage;
439 memcpy(p, string, len);
440 perm_tail->usage += len;
446 * Notes regarding bug involving redefinition of external segments.
448 * Up to and including v0.97, the following code didn't work. From 0.97
449 * developers release 2 onwards, it will generate an error.
452 * newlabel EQU extlabel + 1
454 * The results of allowing this code through are that two import records
455 * are generated, one for 'extlabel' and one for 'newlabel'.
457 * The reason for this is an inadequacy in the defined interface between
458 * the label manager and the output formats. The problem lies in how the
459 * output format driver tells that a label is an external label for which
460 * a label import record must be produced. Most (all except bin?) produce
461 * the record if the segment number of the label is not one of the internal
462 * segments that the output driver is producing.
464 * A simple fix to this would be to make the output formats keep track of
465 * which symbols they've produced import records for, and make them not
466 * produce import records for segments that are already defined.
468 * The best way, which is slightly harder but reduces duplication of code
469 * and should therefore make the entire system smaller and more stable is
470 * to change the interface between assembler, define_label(), and
471 * the output module. The changes that are needed are:
473 * The semantics of the 'isextern' flag passed to define_label() need
474 * examining. This information may or may not tell us what we need to
475 * know (ie should we be generating an import record at this point for this
476 * label). If these aren't the semantics, the semantics should be changed
479 * The output module interface needs changing, so that the `isextern' flag
480 * is passed to the module, so that it can be easily tested for.