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 */
42 /* values for label.defn.is_global */
47 #define NOT_DEFINED_YET 0
49 #define LOCAL_SYMBOL (DEFINED_BIT)
50 #define GLOBAL_PLACEHOLDER (GLOBAL_BIT)
51 #define GLOBAL_SYMBOL (DEFINED_BIT|GLOBAL_BIT)
53 union label
{ /* actual label structures */
56 char *label
, *special
;
57 int is_global
, is_norm
;
65 struct permts
{ /* permanent text storage */
66 struct permts
*next
; /* for the linked list */
67 int size
, usage
; /* size and used space in ... */
68 char data
[PERMTS_SIZE
]; /* ... the data block itself */
71 extern int global_offset_changed
; /* defined in nasm.c */
73 static union label
*ltab
[LABEL_HASHES
];/* using a hash table */
74 static union label
*lfree
[LABEL_HASHES
];/* pointer into the above */
75 static struct permts
*perm_head
; /* start of perm. text storage */
76 static struct permts
*perm_tail
; /* end of perm. text storage */
78 static void init_block (union label
*blk
);
79 static char *perm_copy (char *string1
, char *string2
);
81 static char *prevlabel
;
83 static int initialised
= FALSE
;
85 char lprefix
[PREFIX_MAX
] = {0};
86 char lpostfix
[PREFIX_MAX
] = {0};
89 * Internal routine: finds the `union label' corresponding to the
90 * given label name. Creates a new one, if it isn't found, and if
93 static union label
*find_label (char *label
, int create
)
104 prevlen
= strlen(prev
);
106 while (*p
) hash
+= *p
++;
108 while (*p
) hash
+= *p
++;
109 hash
%= LABEL_HASHES
;
111 while (lptr
->admin
.movingon
!= END_LIST
) {
112 if (lptr
->admin
.movingon
== END_BLOCK
) {
113 lptr
= lptr
->admin
.next
;
117 if (!strncmp(lptr
->defn
.label
, prev
, prevlen
) &&
118 !strcmp(lptr
->defn
.label
+prevlen
, label
))
123 if (lfree
[hash
]->admin
.movingon
== END_BLOCK
) {
125 * must allocate a new block
127 lfree
[hash
]->admin
.next
= (union label
*) nasm_malloc (LBLK_SIZE
);
128 lfree
[hash
] = lfree
[hash
]->admin
.next
;
129 init_block(lfree
[hash
]);
132 lfree
[hash
]->admin
.movingon
= BOGUS_VALUE
;
133 lfree
[hash
]->defn
.label
= perm_copy (prev
, label
);
134 lfree
[hash
]->defn
.special
= NULL
;
135 lfree
[hash
]->defn
.is_global
= NOT_DEFINED_YET
;
136 return lfree
[hash
]++;
142 int lookup_label (char *label
, long *segment
, long *offset
)
149 lptr
= find_label (label
, 0);
150 if (lptr
&& (lptr
->defn
.is_global
& DEFINED_BIT
)) {
151 *segment
= lptr
->defn
.segment
;
152 *offset
= lptr
->defn
.offset
;
159 int is_extern (char *label
)
166 lptr
= find_label (label
, 0);
167 if (lptr
&& (lptr
->defn
.is_global
& EXTERN_BIT
))
173 void redefine_label (char *label
, long segment
, long offset
, char *special
,
174 int is_norm
, int isextrn
, struct ofmt
*ofmt
, efunc error
)
179 /* This routine possibly ought to check for phase errors. Most assemblers
180 * check for phase errors at this point. I don't know whether phase errors
181 * are even possible, nor whether they are checked somewhere else
184 (void) segment
; /* Don't warn that this parameter is unused */
185 (void) special
; /* Don't warn that this parameter is unused */
186 (void) is_norm
; /* Don't warn that this parameter is unused */
187 (void) isextrn
; /* Don't warn that this parameter is unused */
188 (void) ofmt
; /* Don't warn that this parameter is unused */
192 if (!strncmp(label
, "debugdump", 9))
194 error(ERR_DEBUG
, "redefine_label (%s, %ld, %08lx, %s, %d, %d)",
195 label
, segment
, offset
, special
, is_norm
, isextrn
);
198 lptr
= find_label (label
, 1);
200 error (ERR_PANIC
, "can't find label `%s' on pass two", label
);
202 if (!islocal(label
)) {
203 if (!islocalchar(*label
) && lptr
->defn
.is_norm
)
204 prevlabel
= lptr
->defn
.label
;
207 global_offset_changed
|= (lptr
->defn
.offset
!= offset
);
208 lptr
->defn
.offset
= offset
;
211 exi
= !!(lptr
->defn
.is_global
& GLOBAL_BIT
);
216 slen
= strlen(lprefix
);
217 slen
+= strlen(lptr
->defn
.label
);
218 slen
+= strlen(lpostfix
);
219 slen
++; /* room for that null char */
220 xsymbol
= nasm_malloc(slen
);
221 sprintf(xsymbol
,"%s%s%s",lprefix
,lptr
->defn
.label
,lpostfix
);
223 ofmt
->symdef (xsymbol
, segment
, offset
, exi
,
224 special
? special
: lptr
->defn
.special
);
225 ofmt
->current_dfmt
->debug_deflabel (xsymbol
, segment
, offset
, exi
,
226 special
? special
: lptr
->defn
.special
);
227 /** nasm_free(xsymbol); ! outobj.c stores the pointer; ouch!!! **/
231 if ( (lptr
->defn
.is_global
& (GLOBAL_BIT
|EXTERN_BIT
)) != EXTERN_BIT
) {
232 ofmt
->symdef (lptr
->defn
.label
, segment
, offset
, exi
,
233 special
? special
: lptr
->defn
.special
);
234 ofmt
->current_dfmt
->debug_deflabel (label
, segment
, offset
, exi
,
235 special
? special
: lptr
->defn
.special
);
238 } /* if (pass0 == 1) */
242 void define_label (char *label
, long segment
, long offset
, char *special
,
243 int is_norm
, int isextrn
, struct ofmt
*ofmt
, efunc error
)
250 if (!strncmp(label
, "debugdump", 9))
252 error(ERR_DEBUG
, "define_label (%s, %ld, %08lx, %s, %d, %d)",
253 label
, segment
, offset
, special
, is_norm
, isextrn
);
255 lptr
= find_label (label
, 1);
256 if (lptr
->defn
.is_global
& DEFINED_BIT
) {
257 error(ERR_NONFATAL
, "symbol `%s' redefined", label
);
260 lptr
->defn
.is_global
|= DEFINED_BIT
;
262 lptr
->defn
.is_global
|= EXTERN_BIT
;
264 if (!islocalchar(label
[0]) && is_norm
) /* not local, but not special either */
265 prevlabel
= lptr
->defn
.label
;
266 else if (islocal(label
) && !*prevlabel
) {
267 error(ERR_NONFATAL
, "attempt to define a local label before any"
268 " non-local labels");
271 lptr
->defn
.segment
= segment
;
272 lptr
->defn
.offset
= offset
;
273 lptr
->defn
.is_norm
= (!islocalchar(label
[0]) && is_norm
);
275 if (pass0
== 1 || (!is_norm
&& !isextrn
&& (segment
&1))) {
276 exi
= !!(lptr
->defn
.is_global
& GLOBAL_BIT
);
281 slen
= strlen(lprefix
);
282 slen
+= strlen(lptr
->defn
.label
);
283 slen
+= strlen(lpostfix
);
284 slen
++; /* room for that null char */
285 xsymbol
= nasm_malloc(slen
);
286 sprintf(xsymbol
,"%s%s%s",lprefix
,lptr
->defn
.label
,lpostfix
);
288 ofmt
->symdef (xsymbol
, segment
, offset
, exi
,
289 special
? special
: lptr
->defn
.special
);
290 ofmt
->current_dfmt
->debug_deflabel (xsymbol
, segment
, offset
, exi
,
291 special
? special
: lptr
->defn
.special
);
292 /** nasm_free(xsymbol); ! outobj.c stores the pointer; ouch!!! **/
296 if ( (lptr
->defn
.is_global
& (GLOBAL_BIT
|EXTERN_BIT
)) != EXTERN_BIT
) {
297 ofmt
->symdef (lptr
->defn
.label
, segment
, offset
, exi
,
298 special
? special
: lptr
->defn
.special
);
299 ofmt
->current_dfmt
->debug_deflabel (label
, segment
, offset
, exi
,
300 special
? special
: lptr
->defn
.special
);
303 } /* if (pass0 == 1) */
306 void define_common (char *label
, long segment
, long size
, char *special
,
307 struct ofmt
*ofmt
, efunc error
)
311 lptr
= find_label (label
, 1);
312 if (lptr
->defn
.is_global
& DEFINED_BIT
) {
313 error(ERR_NONFATAL
, "symbol `%s' redefined", label
);
316 lptr
->defn
.is_global
|= DEFINED_BIT
;
318 if (!islocalchar(label
[0])) /* not local, but not special either */
319 prevlabel
= lptr
->defn
.label
;
321 error(ERR_NONFATAL
, "attempt to define a local label as a "
324 lptr
->defn
.segment
= segment
;
325 lptr
->defn
.offset
= 0;
327 ofmt
->symdef (lptr
->defn
.label
, segment
, size
, 2,
328 special
? special
: lptr
->defn
.special
);
329 ofmt
->current_dfmt
->debug_deflabel(lptr
->defn
.label
, segment
, size
, 2,
330 special
? special
: lptr
->defn
.special
);
333 void declare_as_global (char *label
, char *special
, efunc error
)
337 if (islocal(label
)) {
338 error(ERR_NONFATAL
, "attempt to declare local symbol `%s' as"
342 lptr
= find_label (label
, 1);
343 switch (lptr
->defn
.is_global
& TYPE_MASK
) {
344 case NOT_DEFINED_YET
:
345 lptr
->defn
.is_global
= GLOBAL_PLACEHOLDER
;
346 lptr
->defn
.special
= special
? perm_copy(special
, "") : NULL
;
348 case GLOBAL_PLACEHOLDER
: /* already done: silently ignore */
352 if (!lptr
->defn
.is_global
& EXTERN_BIT
)
353 error(ERR_NONFATAL
, "symbol `%s': GLOBAL directive must"
354 " appear before symbol definition", label
);
359 int init_labels (void)
363 for (i
=0; i
<LABEL_HASHES
; i
++) {
364 ltab
[i
] = (union label
*) nasm_malloc (LBLK_SIZE
);
366 return -1; /* can't initialise, panic */
367 init_block (ltab
[i
]);
372 perm_tail
= (struct permts
*) nasm_malloc (sizeof(struct permts
));
377 perm_head
->next
= NULL
;
378 perm_head
->size
= PERMTS_SIZE
;
379 perm_head
->usage
= 0;
388 void cleanup_labels (void)
394 for (i
=0; i
<LABEL_HASHES
; i
++) {
395 union label
*lptr
, *lhold
;
397 lptr
= lhold
= ltab
[i
];
400 while (lptr
->admin
.movingon
!= END_BLOCK
) lptr
++;
401 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 (char *string1
, char *string2
)
427 int len
= strlen(string1
)+strlen(string2
)+1;
429 if (perm_tail
->size
- perm_tail
->usage
< len
) {
430 perm_tail
->next
= (struct permts
*)nasm_malloc(sizeof(struct permts
));
431 perm_tail
= perm_tail
->next
;
432 perm_tail
->next
= NULL
;
433 perm_tail
->size
= PERMTS_SIZE
;
434 perm_tail
->usage
= 0;
436 p
= q
= perm_tail
->data
+ perm_tail
->usage
;
437 while ( (*q
= *string1
++) ) q
++;
438 while ( (*q
++ = *string2
++) ) ;
439 perm_tail
->usage
= q
- perm_tail
->data
;
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.