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.
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 */
55 #define NOT_DEFINED_YET 0
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 */
65 char *label
, *special
;
66 int is_global
, is_norm
;
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
104 static union label
*find_label(char *label
, int create
)
108 union label
*lptr
, **lpp
;
109 char label_str
[IDLEN_MAX
];
110 struct hash_insert ip
;
112 if (islocal(label
)) {
114 prevlen
= strlen(prev
);
116 if (prevlen
+len
>= IDLEN_MAX
)
117 return NULL
; /* Error... */
118 memcpy(label_str
, prev
, prevlen
);
119 memcpy(label_str
+prevlen
, label
, len
+1);
126 lpp
= (union label
**) hash_find(<ab
, label
, &ip
);
127 lptr
= lpp
? *lpp
: NULL
;
132 /* Create a new label... */
133 if (lfree
->admin
.movingon
== END_BLOCK
) {
135 * must allocate a new block
138 (union label
*)nasm_malloc(LBLK_SIZE
);
139 lfree
= lfree
->admin
.next
;
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
);
152 bool lookup_label(char *label
, int32_t *segment
, int64_t *offset
)
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
;
168 bool is_extern(char *label
)
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
,
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 */
198 if (!strncmp(label
, "debugdump", 9))
200 error(ERR_DEBUG
, "redefine_label (%s, %ld, %08lx, %s, %d, %d)",
201 label
, segment
, offset
, special
, is_norm
, isextrn
);
204 lptr
= find_label(label
, 1);
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
;
220 exi
= !!(lptr
->defn
.is_global
& GLOBAL_BIT
);
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
,
232 ofmt
->symdef(xsymbol
, segment
, offset
, exi
,
233 special
? special
: lptr
->defn
.special
);
234 ofmt
->current_dfmt
->debug_deflabel(xsymbol
, segment
, offset
,
236 special
? special
: lptr
->
238 /** nasm_free(xsymbol); ! outobj.c stores the pointer; ouch!!! **/
240 if ((lptr
->defn
.is_global
& (GLOBAL_BIT
| 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
,
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
)
262 if (!strncmp(label
, "debugdump", 9))
264 error(ERR_DEBUG
, "define_label (%s, %ld, %08lx, %s, %d, %d)",
265 label
, segment
, offset
, special
, is_norm
, isextrn
);
267 lptr
= find_label(label
, 1);
268 if (lptr
->defn
.is_global
& DEFINED_BIT
) {
269 error(ERR_NONFATAL
, "symbol `%s' redefined", label
);
272 lptr
->defn
.is_global
|= DEFINED_BIT
;
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
);
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
,
301 ofmt
->symdef(xsymbol
, segment
, offset
, exi
,
302 special
? special
: lptr
->defn
.special
);
303 ofmt
->current_dfmt
->debug_deflabel(xsymbol
, segment
, offset
,
305 special
? special
: lptr
->
307 /** nasm_free(xsymbol); ! outobj.c stores the pointer; ouch!!! **/
309 if ((lptr
->defn
.is_global
& (GLOBAL_BIT
| 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
,
319 } /* if (pass0 == 1) */
322 void define_common(char *label
, int32_t segment
, int32_t size
, char *special
,
323 struct ofmt
*ofmt
, efunc error
)
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
);
333 lptr
->defn
.is_global
|= DEFINED_BIT
|COMMON_BIT
;
335 if (!islocalchar(label
[0])) {
336 prevlabel
= lptr
->defn
.label
;
338 error(ERR_NONFATAL
, "attempt to define a local label as a "
343 lptr
->defn
.segment
= segment
;
344 lptr
->defn
.offset
= 0;
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
.
356 void declare_as_global(char *label
, char *special
, efunc error
)
360 if (islocal(label
)) {
361 error(ERR_NONFATAL
, "attempt to declare local symbol `%s' as"
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
;
371 case GLOBAL_PLACEHOLDER
: /* already done: silently ignore */
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
;
384 int init_labels(void)
386 hash_init(<ab
, HASH_LARGE
);
388 ldata
= lfree
= (union label
*)nasm_malloc(LBLK_SIZE
);
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;
405 void cleanup_labels(void)
407 union label
*lptr
, *lhold
;
413 lptr
= lhold
= ldata
;
415 lptr
= &lptr
[LABEL_BLOCK
-1];
416 lptr
= lptr
->admin
.next
;
422 perm_tail
= perm_head
;
423 perm_head
= perm_head
->next
;
424 nasm_free(perm_tail
);
428 static void init_block(union label
*blk
)
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
)
441 int len
= strlen(string
)+1;
443 if (perm_tail
->size
- perm_tail
->usage
< len
) {
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
;
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.
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
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.