summaryrefslogtreecommitdiff
path: root/libyasm/symrec.c
blob: 8427ff4e54efd0dc900c2a7f9336bb738a74cafc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
/*
 * Symbol table handling
 *
 *  Copyright (C) 2001  Michael Urman, Peter Johnson
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND OTHER CONTRIBUTORS ``AS IS''
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR OTHER CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */
#define YASM_LIB_INTERNAL
#include "util.h"
/*@unused@*/ RCSID("$IdPath: yasm/libyasm/symrec.c,v 1.60 2003/03/15 05:07:48 peter Exp $");

#ifdef STDC_HEADERS
# include <limits.h>
#endif

#include "coretype.h"
#include "hamt.h"

#include "errwarn.h"
#include "floatnum.h"
#include "expr.h"
#include "symrec.h"

#include "bytecode.h"
#include "section.h"
#include "objfmt.h"


/* DEFINED is set with EXTERN and COMMON below */
typedef enum {
    SYM_NOSTATUS = 0,
    SYM_USED = 1 << 0,		/* for using variables before definition */
    SYM_DEFINED = 1 << 1,	/* once it's been defined in the file */
    SYM_VALUED = 1 << 2,	/* once its value has been determined */
    SYM_NOTINTABLE = 1 << 3	/* if it's not in sym_table (ex. '$') */
} sym_status;

typedef enum {
    SYM_UNKNOWN,		/* for unknown type (COMMON/EXTERN) */
    SYM_EQU,			/* for EQU defined symbols (expressions) */
    SYM_LABEL			/* for labels */
} sym_type;

struct yasm_symrec {
    char *name;
    sym_type type;
    sym_status status;
    yasm_sym_vis visibility;
    unsigned long line;		/*  symbol was first declared or used on */
    union {
	yasm_expr *expn;	/* equ value */
	struct label_s {	/* bytecode immediately preceding a label */
	    /*@dependent@*/ /*@null@*/ yasm_section *sect;
	    /*@dependent@*/ /*@null@*/ yasm_bytecode *bc;
	} label;
    } value;

    /* objfmt-specific data */
    /*@null@*/ /*@dependent@*/ yasm_objfmt *of;
    /*@null@*/ /*@owned@*/ void *of_data;

    /* storage for optimizer flags */
    unsigned long opt_flags;
};

/* The symbol table: a hash array mapped trie (HAMT). */
static /*@only@*/ HAMT *sym_table;

/* Linked list of symbols not in the symbol table. */
typedef struct non_table_symrec_s {
     /*@reldef@*/ SLIST_ENTRY(non_table_symrec_s) link;
     /*@owned@*/ yasm_symrec *rec;
} non_table_symrec;
typedef /*@reldef@*/ SLIST_HEAD(nontablesymhead_s, non_table_symrec_s)
	nontablesymhead;
static /*@only@*/ nontablesymhead *non_table_syms;


void
yasm_symrec_initialize(void)
{
    sym_table = HAMT_new(yasm_internal_error_);
    non_table_syms = yasm_xmalloc(sizeof(nontablesymhead));
    SLIST_INIT(non_table_syms);
}

static void
symrec_delete_one(/*@only@*/ void *d)
{
    yasm_symrec *sym = d;
    yasm_xfree(sym->name);
    if (sym->type == SYM_EQU)
	yasm_expr_delete(sym->value.expn);
    if (sym->of_data && sym->of) {
	if (sym->of->symrec_data_delete)
	    sym->of->symrec_data_delete(sym->of_data);
	else
	    yasm_internal_error(
		N_("don't know how to delete objfmt-specific data"));
    }
    yasm_xfree(sym);
}

static /*@partial@*/ yasm_symrec *
symrec_new_common(/*@keep@*/ char *name)
{
    yasm_symrec *rec = yasm_xmalloc(sizeof(yasm_symrec));
    rec->name = name;
    rec->type = SYM_UNKNOWN;
    rec->line = 0;
    rec->visibility = YASM_SYM_LOCAL;
    rec->of = NULL;
    rec->of_data = NULL;
    rec->opt_flags = 0;
    return rec;
}

static /*@partial@*/ /*@dependent@*/ yasm_symrec *
symrec_get_or_new_in_table(/*@only@*/ char *name)
{
    yasm_symrec *rec = symrec_new_common(name);
    int replace = 0;

    rec->status = SYM_NOSTATUS;

    return HAMT_insert(sym_table, name, rec, &replace, symrec_delete_one);
}

static /*@partial@*/ /*@dependent@*/ yasm_symrec *
symrec_get_or_new_not_in_table(/*@only@*/ char *name)
{
    non_table_symrec *sym = yasm_xmalloc(sizeof(non_table_symrec));
    sym->rec = symrec_new_common(name);

    sym->rec->status = SYM_NOTINTABLE;

    SLIST_INSERT_HEAD(non_table_syms, sym, link);

    return sym->rec;
}

/* create a new symrec */
/*@-freshtrans -mustfree@*/
static /*@partial@*/ /*@dependent@*/ yasm_symrec *
symrec_get_or_new(const char *name, int in_table)
{
    char *symname = yasm__xstrdup(name);

    if (in_table)
	return symrec_get_or_new_in_table(symname);
    else
	return symrec_get_or_new_not_in_table(symname);
}
/*@=freshtrans =mustfree@*/

/* Call a function with each symrec.  Stops early if 0 returned by func.
   Returns 0 if stopped early. */
int
yasm_symrec_traverse(void *d, int (*func) (yasm_symrec *sym, void *d))
{
    return HAMT_traverse(sym_table, d, (int (*) (void *, void *))func);
}

yasm_symrec *
yasm_symrec_use(const char *name, unsigned long lindex)
{
    yasm_symrec *rec = symrec_get_or_new(name, 1);
    if (rec->line == 0)
	rec->line = lindex;	/* set line number of first use */
    rec->status |= SYM_USED;
    return rec;
}

static /*@dependent@*/ yasm_symrec *
symrec_define(const char *name, sym_type type, int in_table,
	      unsigned long lindex)
{
    yasm_symrec *rec = symrec_get_or_new(name, in_table);

    /* Has it been defined before (either by DEFINED or COMMON/EXTERN)? */
    if ((rec->status & SYM_DEFINED) ||
	(rec->visibility & (YASM_SYM_COMMON | YASM_SYM_EXTERN))) {
	yasm__error(lindex,
	    N_("duplicate definition of `%s'; first defined on line %lu"),
	    name, rec->line);
    } else {
	rec->line = lindex;	/* set line number of definition */
	rec->type = type;
	rec->status |= SYM_DEFINED;
    }
    return rec;
}

yasm_symrec *
yasm_symrec_define_equ(const char *name, yasm_expr *e, unsigned long lindex)
{
    yasm_symrec *rec = symrec_define(name, SYM_EQU, 1, lindex);
    rec->value.expn = e;
    rec->status |= SYM_VALUED;
    return rec;
}

yasm_symrec *
yasm_symrec_define_label(const char *name, yasm_section *sect,
			 yasm_bytecode *precbc, int in_table,
			 unsigned long lindex)
{
    yasm_symrec *rec = symrec_define(name, SYM_LABEL, in_table, lindex);
    rec->value.label.sect = sect;
    rec->value.label.bc = precbc;
    return rec;
}

yasm_symrec *
yasm_symrec_declare(const char *name, yasm_sym_vis vis, unsigned long lindex)
{
    yasm_symrec *rec = symrec_get_or_new(name, 1);

    /* Allowable combinations:
     *  Existing State--------------  vis  New State-------------------
     *  DEFINED GLOBAL COMMON EXTERN  GCE  DEFINED GLOBAL COMMON EXTERN
     *     0      -      0      0     GCE     0      G      C      E
     *     0      -      0      1     GE      0      G      0      E
     *     0      -      1      0     GC      0      G      C      0
     * X   0      -      1      1
     *     1      -      0      0      G      1      G      0      0
     * X   1      -      -      1
     * X   1      -      1      -
     */
    if ((vis == YASM_SYM_GLOBAL) ||
	(!(rec->status & SYM_DEFINED) &&
	 (!(rec->visibility & (YASM_SYM_COMMON | YASM_SYM_EXTERN)) ||
	  ((rec->visibility & YASM_SYM_COMMON) && (vis == YASM_SYM_COMMON)) ||
	  ((rec->visibility & YASM_SYM_EXTERN) && (vis == YASM_SYM_EXTERN)))))
	rec->visibility |= vis;
    else
	yasm__error(lindex,
	    N_("duplicate definition of `%s'; first defined on line %lu"),
	    name, rec->line);
    return rec;
}

const char *
yasm_symrec_get_name(const yasm_symrec *sym)
{
    return sym->name;
}

yasm_sym_vis
yasm_symrec_get_visibility(const yasm_symrec *sym)
{
    return sym->visibility;
}

const yasm_expr *
yasm_symrec_get_equ(const yasm_symrec *sym)
{
    if (sym->type == SYM_EQU)
	return sym->value.expn;
    return (const yasm_expr *)NULL;
}

int
yasm_symrec_get_label(const yasm_symrec *sym,
		      yasm_symrec_get_label_sectionp *sect,
		      yasm_symrec_get_label_bytecodep *precbc)
{
    if (sym->type != SYM_LABEL) {
	*sect = (yasm_symrec_get_label_sectionp)0xDEADBEEF;
	*precbc = (yasm_symrec_get_label_bytecodep)0xDEADBEEF;
	return 0;
    }
    *sect = sym->value.label.sect;
    *precbc = sym->value.label.bc;
    return 1;
}

unsigned long
yasm_symrec_get_opt_flags(const yasm_symrec *sym)
{
    return sym->opt_flags;
}

void
yasm_symrec_set_opt_flags(yasm_symrec *sym, unsigned long opt_flags)
{
    sym->opt_flags = opt_flags;
}

void *
yasm_symrec_get_of_data(yasm_symrec *sym)
{
    return sym->of_data;
}

void
yasm_symrec_set_of_data(yasm_symrec *sym, yasm_objfmt *of, void *of_data)
{
    if (sym->of_data && sym->of) {
	if (sym->of->symrec_data_delete)
	    sym->of->symrec_data_delete(sym->of_data);
	else
	    yasm_internal_error(
		N_("don't know how to delete objfmt-specific data"));
    }
    sym->of = of;
    sym->of_data = of_data;
}

static unsigned long firstundef_line;
static int
symrec_parser_finalize_checksym(yasm_symrec *sym,
				/*@unused@*/ /*@null@*/ void *d)
{
    /* error if a symbol is used but never defined or extern/common declared */
    if ((sym->status & SYM_USED) && !(sym->status & SYM_DEFINED) &&
	!(sym->visibility & (YASM_SYM_EXTERN | YASM_SYM_COMMON))) {
	yasm__error(sym->line, N_("undefined symbol `%s' (first use)"),
		    sym->name);
	if (sym->line < firstundef_line)
	    firstundef_line = sym->line;
    }

    return 1;
}

void
yasm_symrec_parser_finalize(void)
{
    firstundef_line = ULONG_MAX;
    yasm_symrec_traverse(NULL, symrec_parser_finalize_checksym);
    if (firstundef_line < ULONG_MAX)
	yasm__error(firstundef_line,
		    N_(" (Each undefined symbol is reported only once.)"));
}

void
yasm_symrec_cleanup(void)
{
    HAMT_delete(sym_table, symrec_delete_one);

    while (!SLIST_EMPTY(non_table_syms)) {
	non_table_symrec *sym = SLIST_FIRST(non_table_syms);
	SLIST_REMOVE_HEAD(non_table_syms, link);
	symrec_delete_one(sym->rec);
	yasm_xfree(sym);
    }
    yasm_xfree(non_table_syms);
}

typedef struct symrec_print_data {
    FILE *f;
    int indent_level;
} symrec_print_data;

/*@+voidabstract@*/
static int
symrec_print_wrapper(yasm_symrec *sym, /*@null@*/ void *d)
{
    symrec_print_data *data = (symrec_print_data *)d;
    assert(data != NULL);
    fprintf(data->f, "%*sSymbol `%s'\n", data->indent_level, "", sym->name);
    yasm_symrec_print(data->f, data->indent_level+1, sym);
    return 1;
}

void
yasm_symrec_print_all(FILE *f, int indent_level)
{
    symrec_print_data data;
    data.f = f;
    data.indent_level = indent_level;
    yasm_symrec_traverse(&data, symrec_print_wrapper);
}
/*@=voidabstract@*/

void
yasm_symrec_print(FILE *f, int indent_level, const yasm_symrec *sym)
{
    switch (sym->type) {
	case SYM_UNKNOWN:
	    fprintf(f, "%*s-Unknown (Common/Extern)-\n", indent_level, "");
	    break;
	case SYM_EQU:
	    fprintf(f, "%*s_EQU_\n", indent_level, "");
	    fprintf(f, "%*sExpn=", indent_level, "");
	    yasm_expr_print(f, sym->value.expn);
	    fprintf(f, "\n");
	    break;
	case SYM_LABEL:
	    fprintf(f, "%*s_Label_\n%*sSection:\n", indent_level, "",
		    indent_level, "");
	    yasm_section_print(f, indent_level+1, sym->value.label.sect, 0);
	    if (!sym->value.label.bc)
		fprintf(f, "%*sFirst bytecode\n", indent_level, "");
	    else {
		fprintf(f, "%*sPreceding bytecode:\n", indent_level, "");
		yasm_bc_print(f, indent_level+1, sym->value.label.bc);
	    }
	    break;
    }

    fprintf(f, "%*sStatus=", indent_level, "");
    if (sym->status == SYM_NOSTATUS)
	fprintf(f, "None\n");
    else {
	if (sym->status & SYM_USED)
	    fprintf(f, "Used,");
	if (sym->status & SYM_DEFINED)
	    fprintf(f, "Defined,");
	if (sym->status & SYM_VALUED)
	    fprintf(f, "Valued,");
	if (sym->status & SYM_NOTINTABLE)
	    fprintf(f, "Not in Table,");
	fprintf(f, "\n");
    }

    fprintf(f, "%*sVisibility=", indent_level, "");
    if (sym->visibility == YASM_SYM_LOCAL)
	fprintf(f, "Local\n");
    else {
	if (sym->visibility & YASM_SYM_GLOBAL)
	    fprintf(f, "Global,");
	if (sym->visibility & YASM_SYM_COMMON)
	    fprintf(f, "Common,");
	if (sym->visibility & YASM_SYM_EXTERN)
	    fprintf(f, "Extern,");
	fprintf(f, "\n");
    }

    if (sym->of_data && sym->of) {
	fprintf(f, "%*sObject format-specific data:\n", indent_level, "");
	if (sym->of->symrec_data_print)
	    sym->of->symrec_data_print(f, indent_level+1, sym->of_data);
	else
	    fprintf(f, "%*sUNKNOWN\n", indent_level+1, "");
    }

    fprintf(f, "%*sLine Index=%lu\n", indent_level, "", sym->line);
}