blob: dc7e075952aceef427f3e899f979903e2930320b [file] [log] [blame]
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001/* labels.c label handling for the Netwide Assembler
2 *
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 */
8
9#include <stdio.h>
10#include <string.h>
11#include <stdlib.h>
12#include "nasm.h"
13#include "nasmlib.h"
14
15/*
16 * A local label is one that begins with exactly one period. Things
17 * that begin with _two_ periods are NASM-specific things.
18 */
19#define islocal(l) ((l)[0] == '.' && (l)[1] != '.')
20
21#define LABEL_BLOCK 320 /* no. of labels/block */
22#define LBLK_SIZE (LABEL_BLOCK*sizeof(union label))
23#define LABEL_HASHES 32 /* no. of hash table entries */
24
25#define END_LIST -3 /* don't clash with NO_SEG! */
26#define END_BLOCK -2
27#define BOGUS_VALUE -4
28
29#define PERMTS_SIZE 4096 /* size of text blocks */
30
31/* values for label.defn.is_global */
H. Peter Anvin76690a12002-04-30 20:52:49 +000032#define DEFINED_BIT 1
33#define GLOBAL_BIT 2
34#define EXTERN_BIT 4
35
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000036#define NOT_DEFINED_YET 0
H. Peter Anvin76690a12002-04-30 20:52:49 +000037#define TYPE_MASK 3
38#define LOCAL_SYMBOL (DEFINED_BIT)
39#define GLOBAL_PLACEHOLDER (GLOBAL_BIT)
40#define GLOBAL_SYMBOL (DEFINED_BIT|GLOBAL_BIT)
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000041
42union label { /* actual label structures */
43 struct {
44 long segment, offset;
H. Peter Anvin76690a12002-04-30 20:52:49 +000045 char *label, *special;
H. Peter Anvineba20a72002-04-30 20:53:55 +000046 int is_global, is_norm;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000047 } defn;
48 struct {
49 long movingon, dummy;
50 union label *next;
51 } admin;
52};
53
54struct 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 */
58};
59
60static union label *ltab[LABEL_HASHES];/* using a hash table */
61static union label *lfree[LABEL_HASHES];/* pointer into the above */
62static struct permts *perm_head; /* start of perm. text storage */
63static struct permts *perm_tail; /* end of perm. text storage */
64
65static void init_block (union label *blk);
66static char *perm_copy (char *string1, char *string2);
67
68static char *prevlabel;
69
H. Peter Anvin76690a12002-04-30 20:52:49 +000070static int initialised = FALSE;
71
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000072/*
73 * Internal routine: finds the `union label' corresponding to the
74 * given label name. Creates a new one, if it isn't found, and if
75 * `create' is TRUE.
76 */
H. Peter Anvineba20a72002-04-30 20:53:55 +000077static union label *find_label (char *label, int create)
78{
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000079 int hash = 0;
80 char *p, *prev;
81 int prevlen;
82 union label *lptr;
83
84 if (islocal(label))
85 prev = prevlabel;
86 else
87 prev = "";
88 prevlen = strlen(prev);
89 p = prev;
90 while (*p) hash += *p++;
91 p = label;
92 while (*p) hash += *p++;
93 hash %= LABEL_HASHES;
94 lptr = ltab[hash];
95 while (lptr->admin.movingon != END_LIST) {
96 if (lptr->admin.movingon == END_BLOCK) {
97 lptr = lptr->admin.next;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +000098 if (!lptr)
99 break;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000100 }
101 if (!strncmp(lptr->defn.label, prev, prevlen) &&
102 !strcmp(lptr->defn.label+prevlen, label))
103 return lptr;
104 lptr++;
105 }
106 if (create) {
107 if (lfree[hash]->admin.movingon == END_BLOCK) {
108 /*
109 * must allocate a new block
110 */
111 lfree[hash]->admin.next = (union label *) nasm_malloc (LBLK_SIZE);
112 lfree[hash] = lfree[hash]->admin.next;
113 init_block(lfree[hash]);
114 }
115
116 lfree[hash]->admin.movingon = BOGUS_VALUE;
117 lfree[hash]->defn.label = perm_copy (prev, label);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000118 lfree[hash]->defn.special = NULL;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000119 lfree[hash]->defn.is_global = NOT_DEFINED_YET;
120 return lfree[hash]++;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000121 }
122 else
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000123 return NULL;
124}
125
H. Peter Anvineba20a72002-04-30 20:53:55 +0000126int lookup_label (char *label, long *segment, long *offset)
127{
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000128 union label *lptr;
129
H. Peter Anvin76690a12002-04-30 20:52:49 +0000130 if (!initialised)
131 return 0;
132
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000133 lptr = find_label (label, 0);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000134 if (lptr && (lptr->defn.is_global & DEFINED_BIT)) {
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000135 *segment = lptr->defn.segment;
136 *offset = lptr->defn.offset;
137 return 1;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000138 }
139 else
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000140 return 0;
141}
142
H. Peter Anvineba20a72002-04-30 20:53:55 +0000143int is_extern (char *label)
144{
H. Peter Anvin76690a12002-04-30 20:52:49 +0000145 union label *lptr;
146
147 if (!initialised)
148 return 0;
149
150 lptr = find_label (label, 0);
151 if (lptr && (lptr->defn.is_global & EXTERN_BIT))
152 return 1;
153 else
154 return 0;
155}
156
H. Peter Anvineba20a72002-04-30 20:53:55 +0000157void redefine_label (char *label, long segment, long offset, char *special,
158 int is_norm, int isextrn, struct ofmt *ofmt, efunc error)
159{
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000160 union label *lptr;
161
H. Peter Anvineba20a72002-04-30 20:53:55 +0000162 /* This routine possibly ought to check for phase errors. Most assemblers
163 * check for phase errors at this point. I don't know whether phase errors
164 * are even possible, nor whether they are checked somewhere else
165 */
166
167 (void) segment; /* Don't warn that this parameter is unused */
168 (void) offset; /* Don't warn that this parameter is unused */
169 (void) special; /* Don't warn that this parameter is unused */
170 (void) is_norm; /* Don't warn that this parameter is unused */
171 (void) isextrn; /* Don't warn that this parameter is unused */
172 (void) ofmt; /* Don't warn that this parameter is unused */
173
174#ifdef DEBUG
175 if (!strncmp(label, "debugdump", 9))
176 fprintf(stderr, "debug: redefine_label (%s, %ld, %08lx, %s, %d, %d)\n",
177 label, segment, offset, special, is_norm, isextrn);
178#endif
179
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000180 if (!islocal(label)) {
181 lptr = find_label (label, 1);
182 if (!lptr)
183 error (ERR_PANIC, "can't find label `%s' on pass two", label);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000184 if (*label != '.' && lptr->defn.is_norm)
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000185 prevlabel = lptr->defn.label;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000186 }
187}
188
H. Peter Anvin76690a12002-04-30 20:52:49 +0000189void define_label (char *label, long segment, long offset, char *special,
H. Peter Anvineba20a72002-04-30 20:53:55 +0000190 int is_norm, int isextrn, struct ofmt *ofmt, efunc error)
191{
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000192 union label *lptr;
193
H. Peter Anvineba20a72002-04-30 20:53:55 +0000194#ifdef DEBUG
195 if (!strncmp(label, "debugdump", 9))
196 fprintf(stderr, "debug: define_label (%s, %ld, %08lx, %s, %d, %d)\n",
197 label, segment, offset, special, is_norm, isextrn);
198#endif
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000199 lptr = find_label (label, 1);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000200 if (lptr->defn.is_global & DEFINED_BIT) {
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000201 error(ERR_NONFATAL, "symbol `%s' redefined", label);
202 return;
203 }
H. Peter Anvin76690a12002-04-30 20:52:49 +0000204 lptr->defn.is_global |= DEFINED_BIT;
205 if (isextrn)
206 lptr->defn.is_global |= EXTERN_BIT;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000207
H. Peter Anvin76690a12002-04-30 20:52:49 +0000208 if (label[0] != '.' && is_norm) /* not local, but not special either */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000209 prevlabel = lptr->defn.label;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000210 else if (label[0] == '.' && label[1] != '.' && !*prevlabel)
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000211 error(ERR_NONFATAL, "attempt to define a local label before any"
212 " non-local labels");
213
214 lptr->defn.segment = segment;
215 lptr->defn.offset = offset;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000216 lptr->defn.is_norm = (label[0] != '.' && is_norm);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000217
218 ofmt->symdef (lptr->defn.label, segment, offset,
H. Peter Anvin76690a12002-04-30 20:52:49 +0000219 !!(lptr->defn.is_global & GLOBAL_BIT),
220 special ? special : lptr->defn.special);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000221 ofmt->current_dfmt->debug_deflabel (label, segment, offset,
222 !!(lptr->defn.is_global & GLOBAL_BIT),
223 special ? special : lptr->defn.special);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000224}
225
H. Peter Anvin76690a12002-04-30 20:52:49 +0000226void define_common (char *label, long segment, long size, char *special,
H. Peter Anvineba20a72002-04-30 20:53:55 +0000227 struct ofmt *ofmt, efunc error)
228{
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000229 union label *lptr;
230
231 lptr = find_label (label, 1);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000232 if (lptr->defn.is_global & DEFINED_BIT) {
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000233 error(ERR_NONFATAL, "symbol `%s' redefined", label);
234 return;
235 }
H. Peter Anvin76690a12002-04-30 20:52:49 +0000236 lptr->defn.is_global |= DEFINED_BIT;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000237
238 if (label[0] != '.') /* not local, but not special either */
239 prevlabel = lptr->defn.label;
240 else
241 error(ERR_NONFATAL, "attempt to define a local label as a "
242 "common variable");
243
244 lptr->defn.segment = segment;
245 lptr->defn.offset = 0;
246
H. Peter Anvin76690a12002-04-30 20:52:49 +0000247 ofmt->symdef (lptr->defn.label, segment, size, 2,
248 special ? special : lptr->defn.special);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000249 ofmt->current_dfmt->debug_deflabel(lptr->defn.label, segment, size, 2,
250 special ? special : lptr->defn.special);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000251}
252
H. Peter Anvineba20a72002-04-30 20:53:55 +0000253void declare_as_global (char *label, char *special, efunc error)
254{
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000255 union label *lptr;
256
257 if (islocal(label)) {
258 error(ERR_NONFATAL, "attempt to declare local symbol `%s' as"
259 " global", label);
260 return;
261 }
262 lptr = find_label (label, 1);
H. Peter Anvin76690a12002-04-30 20:52:49 +0000263 switch (lptr->defn.is_global & TYPE_MASK) {
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000264 case NOT_DEFINED_YET:
265 lptr->defn.is_global = GLOBAL_PLACEHOLDER;
H. Peter Anvin76690a12002-04-30 20:52:49 +0000266 lptr->defn.special = special ? perm_copy(special, "") : NULL;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000267 break;
268 case GLOBAL_PLACEHOLDER: /* already done: silently ignore */
269 case GLOBAL_SYMBOL:
270 break;
271 case LOCAL_SYMBOL:
H. Peter Anvin76690a12002-04-30 20:52:49 +0000272 if (!lptr->defn.is_global & EXTERN_BIT)
273 error(ERR_NONFATAL, "symbol `%s': GLOBAL directive must"
274 " appear before symbol definition", label);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000275 break;
276 }
277}
278
H. Peter Anvineba20a72002-04-30 20:53:55 +0000279int init_labels (void)
280{
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000281 int i;
282
283 for (i=0; i<LABEL_HASHES; i++) {
284 ltab[i] = (union label *) nasm_malloc (LBLK_SIZE);
285 if (!ltab[i])
286 return -1; /* can't initialise, panic */
287 init_block (ltab[i]);
288 lfree[i] = ltab[i];
289 }
290
H. Peter Anvineba20a72002-04-30 20:53:55 +0000291 perm_head =
292 perm_tail = (struct permts *) nasm_malloc (sizeof(struct permts));
293
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000294 if (!perm_head)
295 return -1;
296
297 perm_head->next = NULL;
298 perm_head->size = PERMTS_SIZE;
299 perm_head->usage = 0;
300
301 prevlabel = "";
302
H. Peter Anvin76690a12002-04-30 20:52:49 +0000303 initialised = TRUE;
304
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000305 return 0;
306}
307
H. Peter Anvineba20a72002-04-30 20:53:55 +0000308void cleanup_labels (void)
309{
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000310 int i;
311
H. Peter Anvin76690a12002-04-30 20:52:49 +0000312 initialised = FALSE;
313
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000314 for (i=0; i<LABEL_HASHES; i++) {
315 union label *lptr, *lhold;
316
317 lptr = lhold = ltab[i];
318
319 while (lptr) {
320 while (lptr->admin.movingon != END_BLOCK) lptr++;
321 lptr = lptr->admin.next;
322 nasm_free (lhold);
323 lhold = lptr;
324 }
325 }
326
327 while (perm_head) {
328 perm_tail = perm_head;
329 perm_head = perm_head->next;
330 nasm_free (perm_tail);
331 }
332}
333
H. Peter Anvineba20a72002-04-30 20:53:55 +0000334static void init_block (union label *blk)
335{
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000336 int j;
337
338 for (j=0; j<LABEL_BLOCK-1; j++)
339 blk[j].admin.movingon = END_LIST;
340 blk[LABEL_BLOCK-1].admin.movingon = END_BLOCK;
341 blk[LABEL_BLOCK-1].admin.next = NULL;
342}
343
H. Peter Anvineba20a72002-04-30 20:53:55 +0000344static char *perm_copy (char *string1, char *string2)
345{
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000346 char *p, *q;
347 int len = strlen(string1)+strlen(string2)+1;
348
349 if (perm_tail->size - perm_tail->usage < len) {
350 perm_tail->next = (struct permts *)nasm_malloc(sizeof(struct permts));
351 perm_tail = perm_tail->next;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000352 perm_tail->next = NULL;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000353 perm_tail->size = PERMTS_SIZE;
354 perm_tail->usage = 0;
355 }
356 p = q = perm_tail->data + perm_tail->usage;
357 while ( (*q = *string1++) ) q++;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000358 while ( (*q++ = *string2++) ) ;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000359 perm_tail->usage = q - perm_tail->data;
360
361 return p;
362}
H. Peter Anvineba20a72002-04-30 20:53:55 +0000363
364/*
365 * Notes regarding bug involving redefinition of external segments.
366 *
367 * Up to and including v0.97, the following code didn't work. From 0.97
368 * developers release 2 onwards, it will generate an error.
369 *
370 * EXTERN extlabel
371 * newlabel EQU extlabel + 1
372 *
373 * The results of allowing this code through are that two import records
374 * are generated, one for 'extlabel' and one for 'newlabel'.
375 *
376 * The reason for this is an inadequacy in the defined interface between
377 * the label manager and the output formats. The problem lies in how the
378 * output format driver tells that a label is an external label for which
379 * a label import record must be produced. Most (all except bin?) produce
380 * the record if the segment number of the label is not one of the internal
381 * segments that the output driver is producing.
382 *
383 * A simple fix to this would be to make the output formats keep track of
384 * which symbols they've produced import records for, and make them not
385 * produce import records for segments that are already defined.
386 *
387 * The best way, which is slightly harder but reduces duplication of code
388 * and should therefore make the entire system smaller and more stable is
389 * to change the interface between assembler, define_label(), and
390 * the output module. The changes that are needed are:
391 *
392 * The semantics of the 'isextern' flag passed to define_label() need
393 * examining. This information may or may not tell us what we need to
394 * know (ie should we be generating an import record at this point for this
395 * label). If these aren't the semantics, the semantics should be changed
396 * to this.
397 *
398 * The output module interface needs changing, so that the `isextern' flag
399 * is passed to the module, so that it can be easily tested for.
400 */