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.
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.
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) == '@') : \
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! */
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"
49 /* values for label.defn.is_global */
54 #define NOT_DEFINED_YET 0
56 #define LOCAL_SYMBOL (DEFINED_BIT)
57 #define GLOBAL_PLACEHOLDER (GLOBAL_BIT)
58 #define GLOBAL_SYMBOL (DEFINED_BIT|GLOBAL_BIT)
60 union label { /* actual label structures */
62 int32_t segment, offset;
63 char *label, *special;
64 int is_global, is_norm;
67 int32_t movingon, dummy;
72 struct permts { /* permanent text storage */
73 struct permts *next; /* for the linked list */
74 int size, usage; /* size and used space in ... */
75 char data[PERMTS_SIZE]; /* ... the data block itself */
78 extern int global_offset_changed; /* defined in nasm.c */
80 static struct hash_table *ltab; /* labels hash table */
81 static union label *ldata; /* all label data blocks */
82 static union label *lfree; /* labels free block */
83 static struct permts *perm_head; /* start of perm. text storage */
84 static struct permts *perm_tail; /* end of perm. text storage */
86 static void init_block(union label *blk);
87 static char *perm_copy(const char *string);
89 static char *prevlabel;
91 static int initialized = FALSE;
93 char lprefix[PREFIX_MAX] = { 0 };
94 char lpostfix[PREFIX_MAX] = { 0 };
97 * Internal routine: finds the `union label' corresponding to the
98 * given label name. Creates a new one, if it isn't found, and if
101 static union label *find_label(char *label, int create)
105 union label *lptr, **lpp;
106 char label_str[IDLEN_MAX];
107 struct hash_insert ip;
109 if (islocal(label)) {
111 prevlen = strlen(prev);
113 if (prevlen+len >= IDLEN_MAX)
114 return NULL; /* Error... */
115 memcpy(label_str, prev, prevlen);
116 memcpy(label_str+prevlen, label, len+1);
123 lpp = (union label **) hash_find(ltab, label, &ip);
124 lptr = lpp ? *lpp : NULL;
129 /* Create a new label... */
130 if (lfree->admin.movingon == END_BLOCK) {
132 * must allocate a new block
135 (union label *)nasm_malloc(LBLK_SIZE);
136 lfree = lfree->admin.next;
140 lfree->admin.movingon = BOGUS_VALUE;
141 lfree->defn.label = perm_copy(label);
142 lfree->defn.special = NULL;
143 lfree->defn.is_global = NOT_DEFINED_YET;
145 hash_add(&ip, lfree->defn.label, lfree);
149 int lookup_label(char *label, int32_t *segment, int32_t *offset)
156 lptr = find_label(label, 0);
157 if (lptr && (lptr->defn.is_global & DEFINED_BIT)) {
158 *segment = lptr->defn.segment;
159 *offset = lptr->defn.offset;
165 int is_extern(char *label)
172 lptr = find_label(label, 0);
173 if (lptr && (lptr->defn.is_global & EXTERN_BIT))
179 void redefine_label(char *label, int32_t segment, int32_t offset, char *special,
180 int is_norm, int isextrn, struct ofmt *ofmt,
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)segment; /* Don't warn that this parameter is unused */
192 (void)special; /* Don't warn that this parameter is unused */
193 (void)is_norm; /* Don't warn that this parameter is unused */
194 (void)isextrn; /* Don't warn that this parameter is unused */
195 (void)ofmt; /* Don't warn that this parameter is unused */
199 if (!strncmp(label, "debugdump", 9))
201 error(ERR_DEBUG, "redefine_label (%s, %ld, %08lx, %s, %d, %d)",
202 label, segment, offset, special, is_norm, isextrn);
205 lptr = find_label(label, 1);
207 error(ERR_PANIC, "can't find label `%s' on pass two", label);
209 if (!islocal(label)) {
210 if (!islocalchar(*label) && lptr->defn.is_norm)
211 prevlabel = lptr->defn.label;
214 global_offset_changed |= (lptr->defn.offset != offset);
215 lptr->defn.offset = offset;
218 exi = !!(lptr->defn.is_global & GLOBAL_BIT);
222 slen = strlen(lprefix);
223 slen += strlen(lptr->defn.label);
224 slen += strlen(lpostfix);
225 slen++; /* room for that null char */
226 xsymbol = nasm_malloc(slen);
227 snprintf(xsymbol, slen, "%s%s%s", lprefix, lptr->defn.label,
230 ofmt->symdef(xsymbol, segment, offset, exi,
231 special ? special : lptr->defn.special);
232 ofmt->current_dfmt->debug_deflabel(xsymbol, segment, offset,
234 special ? special : lptr->
236 /** nasm_free(xsymbol); ! outobj.c stores the pointer; ouch!!! **/
238 if ((lptr->defn.is_global & (GLOBAL_BIT | EXTERN_BIT)) !=
240 ofmt->symdef(lptr->defn.label, segment, offset, exi,
241 special ? special : lptr->defn.special);
242 ofmt->current_dfmt->debug_deflabel(label, segment, offset,
249 /* if (pass0 == 1) */
252 void define_label(char *label, int32_t segment, int32_t offset, char *special,
253 int is_norm, int isextrn, struct ofmt *ofmt, efunc error)
260 if (!strncmp(label, "debugdump", 9))
262 error(ERR_DEBUG, "define_label (%s, %ld, %08lx, %s, %d, %d)",
263 label, segment, offset, special, is_norm, isextrn);
265 lptr = find_label(label, 1);
266 if (lptr->defn.is_global & DEFINED_BIT) {
267 error(ERR_NONFATAL, "symbol `%s' redefined", label);
270 lptr->defn.is_global |= DEFINED_BIT;
272 lptr->defn.is_global |= EXTERN_BIT;
274 if (!islocalchar(label[0]) && is_norm) /* not local, but not special either */
275 prevlabel = lptr->defn.label;
276 else if (islocal(label) && !*prevlabel) {
277 error(ERR_NONFATAL, "attempt to define a local label before any"
278 " non-local labels");
281 lptr->defn.segment = segment;
282 lptr->defn.offset = offset;
283 lptr->defn.is_norm = (!islocalchar(label[0]) && is_norm);
285 if (pass0 == 1 || (!is_norm && !isextrn && (segment & 1))) {
286 exi = !!(lptr->defn.is_global & GLOBAL_BIT);
290 slen = strlen(lprefix);
291 slen += strlen(lptr->defn.label);
292 slen += strlen(lpostfix);
293 slen++; /* room for that null char */
294 xsymbol = nasm_malloc(slen);
295 snprintf(xsymbol, slen, "%s%s%s", lprefix, lptr->defn.label,
298 ofmt->symdef(xsymbol, segment, offset, exi,
299 special ? special : lptr->defn.special);
300 ofmt->current_dfmt->debug_deflabel(xsymbol, segment, offset,
302 special ? special : lptr->
304 /** nasm_free(xsymbol); ! outobj.c stores the pointer; ouch!!! **/
306 if ((lptr->defn.is_global & (GLOBAL_BIT | EXTERN_BIT)) !=
308 ofmt->symdef(lptr->defn.label, segment, offset, exi,
309 special ? special : lptr->defn.special);
310 ofmt->current_dfmt->debug_deflabel(label, segment, offset,
316 } /* if (pass0 == 1) */
319 void define_common(char *label, int32_t segment, int32_t size, char *special,
320 struct ofmt *ofmt, efunc error)
324 lptr = find_label(label, 1);
325 if (lptr->defn.is_global & DEFINED_BIT) {
326 error(ERR_NONFATAL, "symbol `%s' redefined", label);
329 lptr->defn.is_global |= DEFINED_BIT;
331 if (!islocalchar(label[0])) /* not local, but not special either */
332 prevlabel = lptr->defn.label;
334 error(ERR_NONFATAL, "attempt to define a local label as a "
337 lptr->defn.segment = segment;
338 lptr->defn.offset = 0;
340 ofmt->symdef(lptr->defn.label, segment, size, 2,
341 special ? special : lptr->defn.special);
342 ofmt->current_dfmt->debug_deflabel(lptr->defn.label, segment, size, 2,
343 special ? special : lptr->defn.
347 void declare_as_global(char *label, char *special, efunc error)
351 if (islocal(label)) {
352 error(ERR_NONFATAL, "attempt to declare local symbol `%s' as"
356 lptr = find_label(label, 1);
357 switch (lptr->defn.is_global & TYPE_MASK) {
358 case NOT_DEFINED_YET:
359 lptr->defn.is_global = GLOBAL_PLACEHOLDER;
360 lptr->defn.special = special ? perm_copy(special) : NULL;
362 case GLOBAL_PLACEHOLDER: /* already done: silently ignore */
366 if (!lptr->defn.is_global & EXTERN_BIT)
367 error(ERR_NONFATAL, "symbol `%s': GLOBAL directive must"
368 " appear before symbol definition", label);
373 int init_labels(void)
377 ldata = lfree = (union label *)nasm_malloc(LBLK_SIZE);
381 perm_tail = (struct permts *)nasm_malloc(sizeof(struct permts));
383 perm_head->next = NULL;
384 perm_head->size = PERMTS_SIZE;
385 perm_head->usage = 0;
394 void cleanup_labels(void)
396 union label *lptr, *lhold;
402 lptr = lhold = ldata;
404 lptr = &lptr[LABEL_BLOCK-1];
405 lptr = lptr->admin.next;
411 perm_tail = perm_head;
412 perm_head = perm_head->next;
413 nasm_free(perm_tail);
417 static void init_block(union label *blk)
421 for (j = 0; j < LABEL_BLOCK - 1; j++)
422 blk[j].admin.movingon = END_LIST;
423 blk[LABEL_BLOCK - 1].admin.movingon = END_BLOCK;
424 blk[LABEL_BLOCK - 1].admin.next = NULL;
427 static char *perm_copy(const char *string)
430 int len = strlen(string)+1;
432 if (perm_tail->size - perm_tail->usage < len) {
434 (struct permts *)nasm_malloc(sizeof(struct permts));
435 perm_tail = perm_tail->next;
436 perm_tail->next = NULL;
437 perm_tail->size = PERMTS_SIZE;
438 perm_tail->usage = 0;
440 p = perm_tail->data + perm_tail->usage;
441 memcpy(p, string, len);
442 perm_tail->usage += len;
448 * Notes regarding bug involving redefinition of external segments.
450 * Up to and including v0.97, the following code didn't work. From 0.97
451 * developers release 2 onwards, it will generate an error.
454 * newlabel EQU extlabel + 1
456 * The results of allowing this code through are that two import records
457 * are generated, one for 'extlabel' and one for 'newlabel'.
459 * The reason for this is an inadequacy in the defined interface between
460 * the label manager and the output formats. The problem lies in how the
461 * output format driver tells that a label is an external label for which
462 * a label import record must be produced. Most (all except bin?) produce
463 * the record if the segment number of the label is not one of the internal
464 * segments that the output driver is producing.
466 * A simple fix to this would be to make the output formats keep track of
467 * which symbols they've produced import records for, and make them not
468 * produce import records for segments that are already defined.
470 * The best way, which is slightly harder but reduces duplication of code
471 * and should therefore make the entire system smaller and more stable is
472 * to change the interface between assembler, define_label(), and
473 * the output module. The changes that are needed are:
475 * The semantics of the 'isextern' flag passed to define_label() need
476 * examining. This information may or may not tell us what we need to
477 * know (ie should we be generating an import record at this point for this
478 * label). If these aren't the semantics, the semantics should be changed
481 * The output module interface needs changing, so that the `isextern' flag
482 * is passed to the module, so that it can be easily tested for.