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 #define islocal(l) ((l)[0] == '.' && (l)[1] != '.')
21 #define LABEL_BLOCK 32 /* no. of labels/block */
22 #define LBLK_SIZE (LABEL_BLOCK*sizeof(union label))
23 #define LABEL_HASHES 37 /* no. of hash table entries */
25 #define END_LIST -3 /* don't clash with NO_SEG! */
27 #define BOGUS_VALUE -4
29 #define PERMTS_SIZE 4096 /* size of text blocks */
31 /* values for label.defn.is_global */
36 #define NOT_DEFINED_YET 0
38 #define LOCAL_SYMBOL (DEFINED_BIT)
39 #define GLOBAL_PLACEHOLDER (GLOBAL_BIT)
40 #define GLOBAL_SYMBOL (DEFINED_BIT|GLOBAL_BIT)
42 union label
{ /* actual label structures */
45 char *label
, *special
;
46 int is_global
, is_norm
;
54 struct permts
{ /* permanent text storage */
55 struct permts
*next
; /* for the linked list */
56 int size
, usage
; /* size and used space in ... */
57 char data
[PERMTS_SIZE
]; /* ... the data block itself */
60 extern int global_offset_changed
; /* defined in nasm.c */
62 static union label
*ltab
[LABEL_HASHES
];/* using a hash table */
63 static union label
*lfree
[LABEL_HASHES
];/* pointer into the above */
64 static struct permts
*perm_head
; /* start of perm. text storage */
65 static struct permts
*perm_tail
; /* end of perm. text storage */
67 static void init_block (union label
*blk
);
68 static char *perm_copy (char *string1
, char *string2
);
70 static char *prevlabel
;
72 static int initialised
= FALSE
;
75 * Internal routine: finds the `union label' corresponding to the
76 * given label name. Creates a new one, if it isn't found, and if
79 static union label
*find_label (char *label
, int create
)
90 prevlen
= strlen(prev
);
92 while (*p
) hash
+= *p
++;
94 while (*p
) hash
+= *p
++;
97 while (lptr
->admin
.movingon
!= END_LIST
) {
98 if (lptr
->admin
.movingon
== END_BLOCK
) {
99 lptr
= lptr
->admin
.next
;
103 if (!strncmp(lptr
->defn
.label
, prev
, prevlen
) &&
104 !strcmp(lptr
->defn
.label
+prevlen
, label
))
109 if (lfree
[hash
]->admin
.movingon
== END_BLOCK
) {
111 * must allocate a new block
113 lfree
[hash
]->admin
.next
= (union label
*) nasm_malloc (LBLK_SIZE
);
114 lfree
[hash
] = lfree
[hash
]->admin
.next
;
115 init_block(lfree
[hash
]);
118 lfree
[hash
]->admin
.movingon
= BOGUS_VALUE
;
119 lfree
[hash
]->defn
.label
= perm_copy (prev
, label
);
120 lfree
[hash
]->defn
.special
= NULL
;
121 lfree
[hash
]->defn
.is_global
= NOT_DEFINED_YET
;
122 return lfree
[hash
]++;
128 int lookup_label (char *label
, long *segment
, long *offset
)
135 lptr
= find_label (label
, 0);
136 if (lptr
&& (lptr
->defn
.is_global
& DEFINED_BIT
)) {
137 *segment
= lptr
->defn
.segment
;
138 *offset
= lptr
->defn
.offset
;
145 int is_extern (char *label
)
152 lptr
= find_label (label
, 0);
153 if (lptr
&& (lptr
->defn
.is_global
& EXTERN_BIT
))
159 void redefine_label (char *label
, long segment
, long offset
, char *special
,
160 int is_norm
, int isextrn
, struct ofmt
*ofmt
, efunc error
)
164 /* This routine possibly ought to check for phase errors. Most assemblers
165 * check for phase errors at this point. I don't know whether phase errors
166 * are even possible, nor whether they are checked somewhere else
169 (void) segment
; /* Don't warn that this parameter is unused */
170 (void) special
; /* Don't warn that this parameter is unused */
171 (void) is_norm
; /* Don't warn that this parameter is unused */
172 (void) isextrn
; /* Don't warn that this parameter is unused */
173 (void) ofmt
; /* Don't warn that this parameter is unused */
177 if (!strncmp(label
, "debugdump", 9))
179 error(ERR_DEBUG
, "redefine_label (%s, %ld, %08lx, %s, %d, %d)",
180 label
, segment
, offset
, special
, is_norm
, isextrn
);
183 lptr
= find_label (label
, 1);
185 error (ERR_PANIC
, "can't find label `%s' on pass two", label
);
187 if (!islocal(label
)) {
188 if (*label
!= '.' && lptr
->defn
.is_norm
)
189 prevlabel
= lptr
->defn
.label
;
192 global_offset_changed
|= (lptr
->defn
.offset
!= offset
);
193 lptr
->defn
.offset
= offset
;
196 void define_label (char *label
, long segment
, long offset
, char *special
,
197 int is_norm
, int isextrn
, struct ofmt
*ofmt
, efunc error
)
203 if (!strncmp(label
, "debugdump", 9))
205 error(ERR_DEBUG
, "define_label (%s, %ld, %08lx, %s, %d, %d)",
206 label
, segment
, offset
, special
, is_norm
, isextrn
);
208 lptr
= find_label (label
, 1);
209 if (lptr
->defn
.is_global
& DEFINED_BIT
) {
210 error(ERR_NONFATAL
, "symbol `%s' redefined", label
);
213 lptr
->defn
.is_global
|= DEFINED_BIT
;
215 lptr
->defn
.is_global
|= EXTERN_BIT
;
217 if (label
[0] != '.' && is_norm
) /* not local, but not special either */
218 prevlabel
= lptr
->defn
.label
;
219 else if (label
[0] == '.' && label
[1] != '.' && !*prevlabel
)
220 error(ERR_NONFATAL
, "attempt to define a local label before any"
221 " non-local labels");
223 lptr
->defn
.segment
= segment
;
224 lptr
->defn
.offset
= offset
;
225 lptr
->defn
.is_norm
= (label
[0] != '.' && is_norm
);
227 if ( (lptr
->defn
.is_global
& (GLOBAL_BIT
|EXTERN_BIT
)) != EXTERN_BIT
) {
228 ofmt
->symdef (lptr
->defn
.label
, segment
, offset
,
229 !!(lptr
->defn
.is_global
& GLOBAL_BIT
),
230 special
? special
: lptr
->defn
.special
);
231 ofmt
->current_dfmt
->debug_deflabel (label
, segment
, offset
,
232 !!(lptr
->defn
.is_global
& GLOBAL_BIT
),
233 special
? special
: lptr
->defn
.special
);
237 void define_common (char *label
, long segment
, long size
, char *special
,
238 struct ofmt
*ofmt
, efunc error
)
242 lptr
= find_label (label
, 1);
243 if (lptr
->defn
.is_global
& DEFINED_BIT
) {
244 error(ERR_NONFATAL
, "symbol `%s' redefined", label
);
247 lptr
->defn
.is_global
|= DEFINED_BIT
;
249 if (label
[0] != '.') /* not local, but not special either */
250 prevlabel
= lptr
->defn
.label
;
252 error(ERR_NONFATAL
, "attempt to define a local label as a "
255 lptr
->defn
.segment
= segment
;
256 lptr
->defn
.offset
= 0;
258 ofmt
->symdef (lptr
->defn
.label
, segment
, size
, 2,
259 special
? special
: lptr
->defn
.special
);
260 ofmt
->current_dfmt
->debug_deflabel(lptr
->defn
.label
, segment
, size
, 2,
261 special
? special
: lptr
->defn
.special
);
264 void declare_as_global (char *label
, char *special
, efunc error
)
268 if (islocal(label
)) {
269 error(ERR_NONFATAL
, "attempt to declare local symbol `%s' as"
273 lptr
= find_label (label
, 1);
274 switch (lptr
->defn
.is_global
& TYPE_MASK
) {
275 case NOT_DEFINED_YET
:
276 lptr
->defn
.is_global
= GLOBAL_PLACEHOLDER
;
277 lptr
->defn
.special
= special
? perm_copy(special
, "") : NULL
;
279 case GLOBAL_PLACEHOLDER
: /* already done: silently ignore */
283 if (!lptr
->defn
.is_global
& EXTERN_BIT
)
284 error(ERR_NONFATAL
, "symbol `%s': GLOBAL directive must"
285 " appear before symbol definition", label
);
290 int init_labels (void)
294 for (i
=0; i
<LABEL_HASHES
; i
++) {
295 ltab
[i
] = (union label
*) nasm_malloc (LBLK_SIZE
);
297 return -1; /* can't initialise, panic */
298 init_block (ltab
[i
]);
303 perm_tail
= (struct permts
*) nasm_malloc (sizeof(struct permts
));
308 perm_head
->next
= NULL
;
309 perm_head
->size
= PERMTS_SIZE
;
310 perm_head
->usage
= 0;
319 void cleanup_labels (void)
325 for (i
=0; i
<LABEL_HASHES
; i
++) {
326 union label
*lptr
, *lhold
;
328 lptr
= lhold
= ltab
[i
];
331 while (lptr
->admin
.movingon
!= END_BLOCK
) lptr
++;
332 lptr
= lptr
->admin
.next
;
339 perm_tail
= perm_head
;
340 perm_head
= perm_head
->next
;
341 nasm_free (perm_tail
);
345 static void init_block (union label
*blk
)
349 for (j
=0; j
<LABEL_BLOCK
-1; j
++)
350 blk
[j
].admin
.movingon
= END_LIST
;
351 blk
[LABEL_BLOCK
-1].admin
.movingon
= END_BLOCK
;
352 blk
[LABEL_BLOCK
-1].admin
.next
= NULL
;
355 static char *perm_copy (char *string1
, char *string2
)
358 int len
= strlen(string1
)+strlen(string2
)+1;
360 if (perm_tail
->size
- perm_tail
->usage
< len
) {
361 perm_tail
->next
= (struct permts
*)nasm_malloc(sizeof(struct permts
));
362 perm_tail
= perm_tail
->next
;
363 perm_tail
->next
= NULL
;
364 perm_tail
->size
= PERMTS_SIZE
;
365 perm_tail
->usage
= 0;
367 p
= q
= perm_tail
->data
+ perm_tail
->usage
;
368 while ( (*q
= *string1
++) ) q
++;
369 while ( (*q
++ = *string2
++) ) ;
370 perm_tail
->usage
= q
- perm_tail
->data
;
376 * Notes regarding bug involving redefinition of external segments.
378 * Up to and including v0.97, the following code didn't work. From 0.97
379 * developers release 2 onwards, it will generate an error.
382 * newlabel EQU extlabel + 1
384 * The results of allowing this code through are that two import records
385 * are generated, one for 'extlabel' and one for 'newlabel'.
387 * The reason for this is an inadequacy in the defined interface between
388 * the label manager and the output formats. The problem lies in how the
389 * output format driver tells that a label is an external label for which
390 * a label import record must be produced. Most (all except bin?) produce
391 * the record if the segment number of the label is not one of the internal
392 * segments that the output driver is producing.
394 * A simple fix to this would be to make the output formats keep track of
395 * which symbols they've produced import records for, and make them not
396 * produce import records for segments that are already defined.
398 * The best way, which is slightly harder but reduces duplication of code
399 * and should therefore make the entire system smaller and more stable is
400 * to change the interface between assembler, define_label(), and
401 * the output module. The changes that are needed are:
403 * The semantics of the 'isextern' flag passed to define_label() need
404 * examining. This information may or may not tell us what we need to
405 * know (ie should we be generating an import record at this point for this
406 * label). If these aren't the semantics, the semantics should be changed
409 * The output module interface needs changing, so that the `isextern' flag
410 * is passed to the module, so that it can be easily tested for.