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 bool 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 bool 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 bool 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 bool is_extern(char *label
)
172 lptr
= find_label(label
, 0);
173 return (lptr
&& (lptr
->defn
.is_global
& EXTERN_BIT
));
176 void redefine_label(char *label
, int32_t segment
, int32_t offset
, char *special
,
177 bool is_norm
, bool isextrn
, struct ofmt
*ofmt
,
183 /* This routine possibly ought to check for phase errors. Most assemblers
184 * check for phase errors at this point. I don't know whether phase errors
185 * are even possible, nor whether they are checked somewhere else
188 (void)segment
; /* Don't warn that this parameter is unused */
189 (void)special
; /* Don't warn that this parameter is unused */
190 (void)is_norm
; /* Don't warn that this parameter is unused */
191 (void)isextrn
; /* Don't warn that this parameter is unused */
192 (void)ofmt
; /* Don't warn that this parameter is unused */
196 if (!strncmp(label
, "debugdump", 9))
198 error(ERR_DEBUG
, "redefine_label (%s, %ld, %08lx, %s, %d, %d)",
199 label
, segment
, offset
, special
, is_norm
, isextrn
);
202 lptr
= find_label(label
, 1);
204 error(ERR_PANIC
, "can't find label `%s' on pass two", label
);
206 if (!islocal(label
)) {
207 if (!islocalchar(*label
) && lptr
->defn
.is_norm
)
208 prevlabel
= lptr
->defn
.label
;
211 global_offset_changed
|= (lptr
->defn
.offset
!= offset
);
212 lptr
->defn
.offset
= offset
;
215 exi
= !!(lptr
->defn
.is_global
& GLOBAL_BIT
);
219 slen
= strlen(lprefix
);
220 slen
+= strlen(lptr
->defn
.label
);
221 slen
+= strlen(lpostfix
);
222 slen
++; /* room for that null char */
223 xsymbol
= nasm_malloc(slen
);
224 snprintf(xsymbol
, slen
, "%s%s%s", lprefix
, lptr
->defn
.label
,
227 ofmt
->symdef(xsymbol
, segment
, offset
, exi
,
228 special
? special
: lptr
->defn
.special
);
229 ofmt
->current_dfmt
->debug_deflabel(xsymbol
, segment
, offset
,
231 special
? special
: lptr
->
233 /** nasm_free(xsymbol); ! outobj.c stores the pointer; ouch!!! **/
235 if ((lptr
->defn
.is_global
& (GLOBAL_BIT
| EXTERN_BIT
)) !=
237 ofmt
->symdef(lptr
->defn
.label
, segment
, offset
, exi
,
238 special
? special
: lptr
->defn
.special
);
239 ofmt
->current_dfmt
->debug_deflabel(label
, segment
, offset
,
246 /* if (pass0 == 1) */
249 void define_label(char *label
, int32_t segment
, int32_t offset
, char *special
,
250 bool is_norm
, bool isextrn
, struct ofmt
*ofmt
, efunc error
)
257 if (!strncmp(label
, "debugdump", 9))
259 error(ERR_DEBUG
, "define_label (%s, %ld, %08lx, %s, %d, %d)",
260 label
, segment
, offset
, special
, is_norm
, isextrn
);
262 lptr
= find_label(label
, 1);
263 if (lptr
->defn
.is_global
& DEFINED_BIT
) {
264 error(ERR_NONFATAL
, "symbol `%s' redefined", label
);
267 lptr
->defn
.is_global
|= DEFINED_BIT
;
269 lptr
->defn
.is_global
|= EXTERN_BIT
;
271 if (!islocalchar(label
[0]) && is_norm
) /* not local, but not special either */
272 prevlabel
= lptr
->defn
.label
;
273 else if (islocal(label
) && !*prevlabel
) {
274 error(ERR_NONFATAL
, "attempt to define a local label before any"
275 " non-local labels");
278 lptr
->defn
.segment
= segment
;
279 lptr
->defn
.offset
= offset
;
280 lptr
->defn
.is_norm
= (!islocalchar(label
[0]) && is_norm
);
282 if (pass0
== 1 || (!is_norm
&& !isextrn
&& (segment
& 1))) {
283 exi
= !!(lptr
->defn
.is_global
& GLOBAL_BIT
);
287 slen
= strlen(lprefix
);
288 slen
+= strlen(lptr
->defn
.label
);
289 slen
+= strlen(lpostfix
);
290 slen
++; /* room for that null char */
291 xsymbol
= nasm_malloc(slen
);
292 snprintf(xsymbol
, slen
, "%s%s%s", lprefix
, lptr
->defn
.label
,
295 ofmt
->symdef(xsymbol
, segment
, offset
, exi
,
296 special
? special
: lptr
->defn
.special
);
297 ofmt
->current_dfmt
->debug_deflabel(xsymbol
, segment
, offset
,
299 special
? special
: lptr
->
301 /** nasm_free(xsymbol); ! outobj.c stores the pointer; ouch!!! **/
303 if ((lptr
->defn
.is_global
& (GLOBAL_BIT
| EXTERN_BIT
)) !=
305 ofmt
->symdef(lptr
->defn
.label
, segment
, offset
, exi
,
306 special
? special
: lptr
->defn
.special
);
307 ofmt
->current_dfmt
->debug_deflabel(label
, segment
, offset
,
313 } /* if (pass0 == 1) */
316 void define_common(char *label
, int32_t segment
, int32_t size
, char *special
,
317 struct ofmt
*ofmt
, efunc error
)
321 lptr
= find_label(label
, 1);
322 if (lptr
->defn
.is_global
& DEFINED_BIT
) {
323 error(ERR_NONFATAL
, "symbol `%s' redefined", label
);
326 lptr
->defn
.is_global
|= DEFINED_BIT
;
328 if (!islocalchar(label
[0])) /* not local, but not special either */
329 prevlabel
= lptr
->defn
.label
;
331 error(ERR_NONFATAL
, "attempt to define a local label as a "
334 lptr
->defn
.segment
= segment
;
335 lptr
->defn
.offset
= 0;
337 ofmt
->symdef(lptr
->defn
.label
, segment
, size
, 2,
338 special
? special
: lptr
->defn
.special
);
339 ofmt
->current_dfmt
->debug_deflabel(lptr
->defn
.label
, segment
, size
, 2,
340 special
? special
: lptr
->defn
.
344 void declare_as_global(char *label
, char *special
, efunc error
)
348 if (islocal(label
)) {
349 error(ERR_NONFATAL
, "attempt to declare local symbol `%s' as"
353 lptr
= find_label(label
, 1);
354 switch (lptr
->defn
.is_global
& TYPE_MASK
) {
355 case NOT_DEFINED_YET
:
356 lptr
->defn
.is_global
= GLOBAL_PLACEHOLDER
;
357 lptr
->defn
.special
= special
? perm_copy(special
) : NULL
;
359 case GLOBAL_PLACEHOLDER
: /* already done: silently ignore */
363 if (!lptr
->defn
.is_global
& EXTERN_BIT
)
364 error(ERR_NONFATAL
, "symbol `%s': GLOBAL directive must"
365 " appear before symbol definition", label
);
370 int init_labels(void)
374 ldata
= lfree
= (union label
*)nasm_malloc(LBLK_SIZE
);
378 perm_tail
= (struct permts
*)nasm_malloc(sizeof(struct permts
));
380 perm_head
->next
= NULL
;
381 perm_head
->size
= PERMTS_SIZE
;
382 perm_head
->usage
= 0;
391 void cleanup_labels(void)
393 union label
*lptr
, *lhold
;
399 lptr
= lhold
= ldata
;
401 lptr
= &lptr
[LABEL_BLOCK
-1];
402 lptr
= lptr
->admin
.next
;
408 perm_tail
= perm_head
;
409 perm_head
= perm_head
->next
;
410 nasm_free(perm_tail
);
414 static void init_block(union label
*blk
)
418 for (j
= 0; j
< LABEL_BLOCK
- 1; j
++)
419 blk
[j
].admin
.movingon
= END_LIST
;
420 blk
[LABEL_BLOCK
- 1].admin
.movingon
= END_BLOCK
;
421 blk
[LABEL_BLOCK
- 1].admin
.next
= NULL
;
424 static char *perm_copy(const char *string
)
427 int len
= strlen(string
)+1;
429 if (perm_tail
->size
- perm_tail
->usage
< len
) {
431 (struct permts
*)nasm_malloc(sizeof(struct permts
));
432 perm_tail
= perm_tail
->next
;
433 perm_tail
->next
= NULL
;
434 perm_tail
->size
= PERMTS_SIZE
;
435 perm_tail
->usage
= 0;
437 p
= perm_tail
->data
+ perm_tail
->usage
;
438 memcpy(p
, string
, len
);
439 perm_tail
->usage
+= len
;
445 * Notes regarding bug involving redefinition of external segments.
447 * Up to and including v0.97, the following code didn't work. From 0.97
448 * developers release 2 onwards, it will generate an error.
451 * newlabel EQU extlabel + 1
453 * The results of allowing this code through are that two import records
454 * are generated, one for 'extlabel' and one for 'newlabel'.
456 * The reason for this is an inadequacy in the defined interface between
457 * the label manager and the output formats. The problem lies in how the
458 * output format driver tells that a label is an external label for which
459 * a label import record must be produced. Most (all except bin?) produce
460 * the record if the segment number of the label is not one of the internal
461 * segments that the output driver is producing.
463 * A simple fix to this would be to make the output formats keep track of
464 * which symbols they've produced import records for, and make them not
465 * produce import records for segments that are already defined.
467 * The best way, which is slightly harder but reduces duplication of code
468 * and should therefore make the entire system smaller and more stable is
469 * to change the interface between assembler, define_label(), and
470 * the output module. The changes that are needed are:
472 * The semantics of the 'isextern' flag passed to define_label() need
473 * examining. This information may or may not tell us what we need to
474 * know (ie should we be generating an import record at this point for this
475 * label). If these aren't the semantics, the semantics should be changed
478 * The output module interface needs changing, so that the `isextern' flag
479 * is passed to the module, so that it can be easily tested for.