If we don't specify -g, actually suppress debugging output
[nasm.git] / labels.c
blob74222f528ef0d33b69114404ce4a267c8ee2e661
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.
7 */
9 #include <stdio.h>
10 #include <string.h>
11 #include <stdlib.h>
12 #include "nasm.h"
13 #include "nasmlib.h"
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.
23 #define islocal(l) \
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) == '@') : \
30 ((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! */
37 #define END_BLOCK -2
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"
43 #endif
45 /* values for label.defn.is_global */
46 #define DEFINED_BIT 1
47 #define GLOBAL_BIT 2
48 #define EXTERN_BIT 4
50 #define NOT_DEFINED_YET 0
51 #define TYPE_MASK 3
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 */
57 struct {
58 long segment, offset;
59 char *label, *special;
60 int is_global, is_norm;
61 } defn;
62 struct {
63 long movingon, dummy;
64 union label *next;
65 } admin;
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
94 * `create' is TRUE.
96 static union label *find_label (char *label, int create)
98 int hash = 0;
99 char *p, *prev;
100 int prevlen;
101 union label *lptr;
103 if (islocal(label))
104 prev = prevlabel;
105 else
106 prev = "";
107 prevlen = strlen(prev);
108 p = prev;
109 while (*p) hash += *p++;
110 p = label;
111 while (*p) hash += *p++;
112 hash %= LABEL_HASHES;
113 lptr = ltab[hash];
114 while (lptr->admin.movingon != END_LIST) {
115 if (lptr->admin.movingon == END_BLOCK) {
116 lptr = lptr->admin.next;
117 if (!lptr)
118 break;
120 if (!strncmp(lptr->defn.label, prev, prevlen) &&
121 !strcmp(lptr->defn.label+prevlen, label))
122 return lptr;
123 lptr++;
125 if (create) {
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]++;
141 else
142 return NULL;
145 int lookup_label (char *label, long *segment, long *offset)
147 union label *lptr;
149 if (!initialised)
150 return 0;
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;
156 return 1;
158 else
159 return 0;
162 int is_extern (char *label)
164 union label *lptr;
166 if (!initialised)
167 return 0;
169 lptr = find_label (label, 0);
170 if (lptr && (lptr->defn.is_global & EXTERN_BIT))
171 return 1;
172 else
173 return 0;
176 void redefine_label (char *label, long segment, long offset, char *special,
177 int is_norm, int isextrn, struct ofmt *ofmt, efunc error)
179 union label *lptr;
180 int exi;
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 */
193 #ifdef DEBUG
194 #if DEBUG<3
195 if (!strncmp(label, "debugdump", 9))
196 #endif
197 error(ERR_DEBUG, "redefine_label (%s, %ld, %08lx, %s, %d, %d)",
198 label, segment, offset, special, is_norm, isextrn);
199 #endif
201 lptr = find_label (label, 1);
202 if (!lptr)
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;
213 if (pass0 == 1) {
214 exi = !!(lptr->defn.is_global & GLOBAL_BIT);
215 if (exi)
217 char *xsymbol;
218 int slen;
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!!! **/
232 else
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)
248 union label *lptr;
249 int exi;
251 #ifdef DEBUG
252 #if DEBUG<3
253 if (!strncmp(label, "debugdump", 9))
254 #endif
255 error(ERR_DEBUG, "define_label (%s, %ld, %08lx, %s, %d, %d)",
256 label, segment, offset, special, is_norm, isextrn);
257 #endif
258 lptr = find_label (label, 1);
259 if (lptr->defn.is_global & DEFINED_BIT) {
260 error(ERR_NONFATAL, "symbol `%s' redefined", label);
261 return;
263 lptr->defn.is_global |= DEFINED_BIT;
264 if (isextrn)
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);
280 if (exi)
282 char *xsymbol;
283 int slen;
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!!! **/
297 else
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)
312 union label *lptr;
314 lptr = find_label (label, 1);
315 if (lptr->defn.is_global & DEFINED_BIT) {
316 error(ERR_NONFATAL, "symbol `%s' redefined", label);
317 return;
319 lptr->defn.is_global |= DEFINED_BIT;
321 if (!islocalchar(label[0])) /* not local, but not special either */
322 prevlabel = lptr->defn.label;
323 else
324 error(ERR_NONFATAL, "attempt to define a local label as a "
325 "common variable");
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)
338 union label *lptr;
340 if (islocal(label)) {
341 error(ERR_NONFATAL, "attempt to declare local symbol `%s' as"
342 " global", label);
343 return;
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;
350 break;
351 case GLOBAL_PLACEHOLDER: /* already done: silently ignore */
352 case GLOBAL_SYMBOL:
353 break;
354 case LOCAL_SYMBOL:
355 if (!lptr->defn.is_global & EXTERN_BIT)
356 error(ERR_NONFATAL, "symbol `%s': GLOBAL directive must"
357 " appear before symbol definition", label);
358 break;
362 int init_labels (void)
364 int i;
366 for (i=0; i<LABEL_HASHES; i++) {
367 ltab[i] = (union label *) nasm_malloc (LBLK_SIZE);
368 if (!ltab[i])
369 return -1; /* can't initialise, panic */
370 init_block (ltab[i]);
371 lfree[i] = ltab[i];
374 perm_head =
375 perm_tail = (struct permts *) nasm_malloc (sizeof(struct permts));
377 if (!perm_head)
378 return -1;
380 perm_head->next = NULL;
381 perm_head->size = PERMTS_SIZE;
382 perm_head->usage = 0;
384 prevlabel = "";
386 initialised = TRUE;
388 return 0;
391 void cleanup_labels (void)
393 int i;
395 initialised = FALSE;
397 for (i=0; i<LABEL_HASHES; i++) {
398 union label *lptr, *lhold;
400 lptr = lhold = ltab[i];
402 while (lptr) {
403 while (lptr->admin.movingon != END_BLOCK) lptr++;
404 lptr = lptr->admin.next;
405 nasm_free (lhold);
406 lhold = lptr;
410 while (perm_head) {
411 perm_tail = perm_head;
412 perm_head = perm_head->next;
413 nasm_free (perm_tail);
417 static void init_block (union label *blk)
419 int j;
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)
429 char *p, *q;
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;
444 return p;
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.
453 * EXTERN extlabel
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
479 * to this.
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.