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
);
109 while (*p
) hash
+= *p
++;
111 while (*p
) hash
+= *p
++;
112 hash
%= LABEL_HASHES
;
114 while (lptr
->admin
.movingon
!= END_LIST
) {
115 if (lptr
->admin
.movingon
== END_BLOCK
) {
116 lptr
= lptr
->admin
.next
;
120 if (!strncmp(lptr
->defn
.label
, prev
, prevlen
) &&
121 !strcmp(lptr
->defn
.label
+prevlen
, label
))
126 if (lfree
[hash
]->admin
.movingon
== END_BLOCK
) {
128 * must allocate a new block
130 lfree
[hash
]->admin
.next
= (union label
*) nasm_malloc (LBLK_SIZE
);
131 lfree
[hash
] = lfree
[hash
]->admin
.next
;
132 init_block(lfree
[hash
]);
135 lfree
[hash
]->admin
.movingon
= BOGUS_VALUE
;
136 lfree
[hash
]->defn
.label
= perm_copy (prev
, label
);
137 lfree
[hash
]->defn
.special
= NULL
;
138 lfree
[hash
]->defn
.is_global
= NOT_DEFINED_YET
;
139 return lfree
[hash
]++;
145 int lookup_label (char *label
, long *segment
, long *offset
)
152 lptr
= find_label (label
, 0);
153 if (lptr
&& (lptr
->defn
.is_global
& DEFINED_BIT
)) {
154 *segment
= lptr
->defn
.segment
;
155 *offset
= lptr
->defn
.offset
;
162 int is_extern (char *label
)
169 lptr
= find_label (label
, 0);
170 if (lptr
&& (lptr
->defn
.is_global
& EXTERN_BIT
))
176 void redefine_label (char *label
, long segment
, long offset
, char *special
,
177 int is_norm
, int isextrn
, struct ofmt
*ofmt
, efunc error
)
182 /* This routine possibly ought to check for phase errors. Most assemblers
183 * check for phase errors at this point. I don't know whether phase errors
184 * are even possible, nor whether they are checked somewhere else
187 (void) segment
; /* Don't warn that this parameter is unused */
188 (void) special
; /* Don't warn that this parameter is unused */
189 (void) is_norm
; /* Don't warn that this parameter is unused */
190 (void) isextrn
; /* Don't warn that this parameter is unused */
191 (void) ofmt
; /* Don't warn that this parameter is unused */
195 if (!strncmp(label
, "debugdump", 9))
197 error(ERR_DEBUG
, "redefine_label (%s, %ld, %08lx, %s, %d, %d)",
198 label
, segment
, offset
, special
, is_norm
, isextrn
);
201 lptr
= find_label (label
, 1);
203 error (ERR_PANIC
, "can't find label `%s' on pass two", label
);
205 if (!islocal(label
)) {
206 if (!islocalchar(*label
) && lptr
->defn
.is_norm
)
207 prevlabel
= lptr
->defn
.label
;
210 global_offset_changed
|= (lptr
->defn
.offset
!= offset
);
211 lptr
->defn
.offset
= offset
;
214 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 sprintf(xsymbol
,"%s%s%s",lprefix
,lptr
->defn
.label
,lpostfix
);
226 ofmt
->symdef (xsymbol
, segment
, offset
, exi
,
227 special
? special
: lptr
->defn
.special
);
228 ofmt
->current_dfmt
->debug_deflabel (xsymbol
, segment
, offset
, exi
,
229 special
? special
: lptr
->defn
.special
);
230 /** nasm_free(xsymbol); ! outobj.c stores the pointer; ouch!!! **/
234 if ( (lptr
->defn
.is_global
& (GLOBAL_BIT
|EXTERN_BIT
)) != EXTERN_BIT
) {
235 ofmt
->symdef (lptr
->defn
.label
, segment
, offset
, exi
,
236 special
? special
: lptr
->defn
.special
);
237 ofmt
->current_dfmt
->debug_deflabel (label
, segment
, offset
, exi
,
238 special
? special
: lptr
->defn
.special
);
241 } /* if (pass0 == 1) */
245 void define_label (char *label
, long segment
, long offset
, char *special
,
246 int is_norm
, int isextrn
, struct ofmt
*ofmt
, efunc error
)
253 if (!strncmp(label
, "debugdump", 9))
255 error(ERR_DEBUG
, "define_label (%s, %ld, %08lx, %s, %d, %d)",
256 label
, segment
, offset
, special
, is_norm
, isextrn
);
258 lptr
= find_label (label
, 1);
259 if (lptr
->defn
.is_global
& DEFINED_BIT
) {
260 error(ERR_NONFATAL
, "symbol `%s' redefined", label
);
263 lptr
->defn
.is_global
|= DEFINED_BIT
;
265 lptr
->defn
.is_global
|= EXTERN_BIT
;
267 if (!islocalchar(label
[0]) && is_norm
) /* not local, but not special either */
268 prevlabel
= lptr
->defn
.label
;
269 else if (islocal(label
) && !*prevlabel
) {
270 error(ERR_NONFATAL
, "attempt to define a local label before any"
271 " non-local labels");
274 lptr
->defn
.segment
= segment
;
275 lptr
->defn
.offset
= offset
;
276 lptr
->defn
.is_norm
= (!islocalchar(label
[0]) && is_norm
);
278 if (pass0
== 1 || (!is_norm
&& !isextrn
&& (segment
&1))) {
279 exi
= !!(lptr
->defn
.is_global
& GLOBAL_BIT
);
284 slen
= strlen(lprefix
);
285 slen
+= strlen(lptr
->defn
.label
);
286 slen
+= strlen(lpostfix
);
287 slen
++; /* room for that null char */
288 xsymbol
= nasm_malloc(slen
);
289 sprintf(xsymbol
,"%s%s%s",lprefix
,lptr
->defn
.label
,lpostfix
);
291 ofmt
->symdef (xsymbol
, segment
, offset
, exi
,
292 special
? special
: lptr
->defn
.special
);
293 ofmt
->current_dfmt
->debug_deflabel (xsymbol
, segment
, offset
, exi
,
294 special
? special
: lptr
->defn
.special
);
295 /** nasm_free(xsymbol); ! outobj.c stores the pointer; ouch!!! **/
299 if ( (lptr
->defn
.is_global
& (GLOBAL_BIT
|EXTERN_BIT
)) != EXTERN_BIT
) {
300 ofmt
->symdef (lptr
->defn
.label
, segment
, offset
, exi
,
301 special
? special
: lptr
->defn
.special
);
302 ofmt
->current_dfmt
->debug_deflabel (label
, segment
, offset
, exi
,
303 special
? special
: lptr
->defn
.special
);
306 } /* if (pass0 == 1) */
309 void define_common (char *label
, long segment
, long size
, char *special
,
310 struct ofmt
*ofmt
, efunc error
)
314 lptr
= find_label (label
, 1);
315 if (lptr
->defn
.is_global
& DEFINED_BIT
) {
316 error(ERR_NONFATAL
, "symbol `%s' redefined", label
);
319 lptr
->defn
.is_global
|= DEFINED_BIT
;
321 if (!islocalchar(label
[0])) /* not local, but not special either */
322 prevlabel
= lptr
->defn
.label
;
324 error(ERR_NONFATAL
, "attempt to define a local label as a "
327 lptr
->defn
.segment
= segment
;
328 lptr
->defn
.offset
= 0;
330 ofmt
->symdef (lptr
->defn
.label
, segment
, size
, 2,
331 special
? special
: lptr
->defn
.special
);
332 ofmt
->current_dfmt
->debug_deflabel(lptr
->defn
.label
, segment
, size
, 2,
333 special
? special
: lptr
->defn
.special
);
336 void declare_as_global (char *label
, char *special
, efunc error
)
340 if (islocal(label
)) {
341 error(ERR_NONFATAL
, "attempt to declare local symbol `%s' as"
345 lptr
= find_label (label
, 1);
346 switch (lptr
->defn
.is_global
& TYPE_MASK
) {
347 case NOT_DEFINED_YET
:
348 lptr
->defn
.is_global
= GLOBAL_PLACEHOLDER
;
349 lptr
->defn
.special
= special
? perm_copy(special
, "") : NULL
;
351 case GLOBAL_PLACEHOLDER
: /* already done: silently ignore */
355 if (!lptr
->defn
.is_global
& EXTERN_BIT
)
356 error(ERR_NONFATAL
, "symbol `%s': GLOBAL directive must"
357 " appear before symbol definition", label
);
362 int init_labels (void)
366 for (i
=0; i
<LABEL_HASHES
; i
++) {
367 ltab
[i
] = (union label
*) nasm_malloc (LBLK_SIZE
);
369 return -1; /* can't initialise, panic */
370 init_block (ltab
[i
]);
375 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)
397 for (i
=0; i
<LABEL_HASHES
; i
++) {
398 union label
*lptr
, *lhold
;
400 lptr
= lhold
= ltab
[i
];
403 while (lptr
->admin
.movingon
!= END_BLOCK
) lptr
++;
404 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 (char *string1
, char *string2
)
430 int len
= strlen(string1
)+strlen(string2
)+1;
432 if (perm_tail
->size
- perm_tail
->usage
< len
) {
433 perm_tail
->next
= (struct permts
*)nasm_malloc(sizeof(struct permts
));
434 perm_tail
= perm_tail
->next
;
435 perm_tail
->next
= NULL
;
436 perm_tail
->size
= PERMTS_SIZE
;
437 perm_tail
->usage
= 0;
439 p
= q
= perm_tail
->data
+ perm_tail
->usage
;
440 while ( (*q
= *string1
++) ) q
++;
441 while ( (*q
++ = *string2
++) ) ;
442 perm_tail
->usage
= q
- perm_tail
->data
;
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.