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.
16 * A local label is one that begins with exactly one period. Things
17 * that begin with _two_ periods are NASM-specific things.
19 * If TASM compatibility is enabled, a local label can also begin with
20 * @@, so @@local is a TASM compatible local label. Note that we only
21 * check for the first @ symbol, although TASM requires both.
24 (tasm_compatible_mode ? \
25 (((l)[0] == '.' || (l)[0] == '@') && (l)[1] != '.') : \
26 ((l)[0] == '.' && (l)[1] != '.'))
27 #define islocalchar(c) \
28 (tasm_compatible_mode ? \
29 ((c) == '.' || (c) == '@') : \
32 #define LABEL_BLOCK 32 /* no. of labels/block */
33 #define LBLK_SIZE (LABEL_BLOCK*sizeof(union label))
34 #define LABEL_HASHES 37 /* no. of hash table entries */
36 #define END_LIST -3 /* don't clash with NO_SEG! */
38 #define BOGUS_VALUE -4
40 #define PERMTS_SIZE 4096 /* size of text blocks */
41 #if (PERMTS_SIZE > IDLEN_MAX)
42 #error "IPERMTS_SIZE must be less than or equal to IDLEN_MAX"
45 /* values for label.defn.is_global */
50 #define NOT_DEFINED_YET 0
52 #define LOCAL_SYMBOL (DEFINED_BIT)
53 #define GLOBAL_PLACEHOLDER (GLOBAL_BIT)
54 #define GLOBAL_SYMBOL (DEFINED_BIT|GLOBAL_BIT)
56 union label
{ /* actual label structures */
59 char *label
, *special
;
60 int is_global
, is_norm
;
68 struct permts
{ /* permanent text storage */
69 struct permts
*next
; /* for the linked list */
70 int size
, usage
; /* size and used space in ... */
71 char data
[PERMTS_SIZE
]; /* ... the data block itself */
74 extern int global_offset_changed
; /* defined in nasm.c */
76 static union label
*ltab
[LABEL_HASHES
]; /* using a hash table */
77 static union label
*lfree
[LABEL_HASHES
]; /* pointer into the above */
78 static struct permts
*perm_head
; /* start of perm. text storage */
79 static struct permts
*perm_tail
; /* end of perm. text storage */
81 static void init_block(union label
*blk
);
82 static char *perm_copy(char *string1
, char *string2
);
84 static char *prevlabel
;
86 static int initialised
= FALSE
;
88 char lprefix
[PREFIX_MAX
] = { 0 };
89 char lpostfix
[PREFIX_MAX
] = { 0 };
92 * Internal routine: finds the `union label' corresponding to the
93 * given label name. Creates a new one, if it isn't found, and if
96 static union label
*find_label(char *label
, int create
)
107 prevlen
= strlen(prev
);
114 hash
%= LABEL_HASHES
;
116 while (lptr
->admin
.movingon
!= END_LIST
) {
117 if (lptr
->admin
.movingon
== END_BLOCK
) {
118 lptr
= lptr
->admin
.next
;
122 if (!strncmp(lptr
->defn
.label
, prev
, prevlen
) &&
123 !strcmp(lptr
->defn
.label
+ prevlen
, label
))
128 if (lfree
[hash
]->admin
.movingon
== END_BLOCK
) {
130 * must allocate a new block
132 lfree
[hash
]->admin
.next
=
133 (union label
*)nasm_malloc(LBLK_SIZE
);
134 lfree
[hash
] = lfree
[hash
]->admin
.next
;
135 init_block(lfree
[hash
]);
138 lfree
[hash
]->admin
.movingon
= BOGUS_VALUE
;
139 lfree
[hash
]->defn
.label
= perm_copy(prev
, label
);
140 lfree
[hash
]->defn
.special
= NULL
;
141 lfree
[hash
]->defn
.is_global
= NOT_DEFINED_YET
;
142 return lfree
[hash
]++;
147 int lookup_label(char *label
, long *segment
, long *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
, long segment
, long 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
, long segment
, long 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
, long segment
, long 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 for (i
= 0; i
< LABEL_HASHES
; i
++) {
376 ltab
[i
] = (union label
*)nasm_malloc(LBLK_SIZE
);
378 return -1; /* can't initialise, panic */
384 perm_tail
= (struct permts
*)nasm_malloc(sizeof(struct permts
));
389 perm_head
->next
= NULL
;
390 perm_head
->size
= PERMTS_SIZE
;
391 perm_head
->usage
= 0;
400 void cleanup_labels(void)
406 for (i
= 0; i
< LABEL_HASHES
; i
++) {
407 union label
*lptr
, *lhold
;
409 lptr
= lhold
= ltab
[i
];
412 while (lptr
->admin
.movingon
!= END_BLOCK
)
414 lptr
= lptr
->admin
.next
;
421 perm_tail
= perm_head
;
422 perm_head
= perm_head
->next
;
423 nasm_free(perm_tail
);
427 static void init_block(union label
*blk
)
431 for (j
= 0; j
< LABEL_BLOCK
- 1; j
++)
432 blk
[j
].admin
.movingon
= END_LIST
;
433 blk
[LABEL_BLOCK
- 1].admin
.movingon
= END_BLOCK
;
434 blk
[LABEL_BLOCK
- 1].admin
.next
= NULL
;
437 static char *perm_copy(char *string1
, char *string2
)
440 int len
= strlen(string1
) + strlen(string2
) + 1;
442 if (perm_tail
->size
- perm_tail
->usage
< len
) {
444 (struct permts
*)nasm_malloc(sizeof(struct permts
));
445 perm_tail
= perm_tail
->next
;
446 perm_tail
->next
= NULL
;
447 perm_tail
->size
= PERMTS_SIZE
;
448 perm_tail
->usage
= 0;
450 p
= q
= perm_tail
->data
+ perm_tail
->usage
;
451 while ((*q
= *string1
++))
453 while ((*q
++ = *string2
++)) ;
454 perm_tail
->usage
= q
- perm_tail
->data
;
460 * Notes regarding bug involving redefinition of external segments.
462 * Up to and including v0.97, the following code didn't work. From 0.97
463 * developers release 2 onwards, it will generate an error.
466 * newlabel EQU extlabel + 1
468 * The results of allowing this code through are that two import records
469 * are generated, one for 'extlabel' and one for 'newlabel'.
471 * The reason for this is an inadequacy in the defined interface between
472 * the label manager and the output formats. The problem lies in how the
473 * output format driver tells that a label is an external label for which
474 * a label import record must be produced. Most (all except bin?) produce
475 * the record if the segment number of the label is not one of the internal
476 * segments that the output driver is producing.
478 * A simple fix to this would be to make the output formats keep track of
479 * which symbols they've produced import records for, and make them not
480 * produce import records for segments that are already defined.
482 * The best way, which is slightly harder but reduces duplication of code
483 * and should therefore make the entire system smaller and more stable is
484 * to change the interface between assembler, define_label(), and
485 * the output module. The changes that are needed are:
487 * The semantics of the 'isextern' flag passed to define_label() need
488 * examining. This information may or may not tell us what we need to
489 * know (ie should we be generating an import record at this point for this
490 * label). If these aren't the semantics, the semantics should be changed
493 * The output module interface needs changing, so that the `isextern' flag
494 * is passed to the module, so that it can be easily tested for.