summaryrefslogtreecommitdiff
path: root/src/symrec.c
blob: b1871689034af0fa1f0427d6ef632c45244973f9 (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
/*
 * Symbol table handling
 *
 *  Copyright (C) 2001  Michael Urman, Peter Johnson
 *
 *  This file is part of YASM.
 *
 *  YASM is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  YASM is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
#include "util.h"
/*@unused@*/ RCSID("$IdPath$");

#include "ternary.h"

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

#include "bytecode.h"
#include "section.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. '$') */
} SymStatus;

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

struct symrec {
    char *name;
    SymType type;
    SymStatus status;
    SymVisibility visibility;
    /*@dependent@*/ /*@null@*/ const char *filename;	/* file and line */
    unsigned long line;		/*  symbol was first declared or used on */
    union {
	expr *expn;		/* equ value */
	struct label_s {	/* bytecode immediately preceding a label */
	    /*@dependent@*/ /*@null@*/ section *sect;
	    /*@dependent@*/ /*@null@*/ bytecode *bc;
	} label;
    } value;
};

/* The symbol table: a ternary tree. */
static /*@only@*/ /*@null@*/ ternary_tree sym_table = (ternary_tree)NULL;

/* create a new symrec */
static /*@partial@*/ /*@dependent@*/ symrec *
symrec_get_or_new(const char *name, int in_table)
{
    symrec *rec, *rec2;

    rec = xmalloc(sizeof(symrec));
    if (in_table) {
	rec2 = ternary_insert(&sym_table, name, rec, 0);

	if (rec2 != rec) {
	    xfree(rec);
	    return rec2;
	}
	rec->status = SYM_NOSTATUS;
    } else
	rec->status = SYM_NOTINTABLE;

    rec->name = xstrdup(name);
    rec->type = SYM_UNKNOWN;
    rec->filename = in_filename;
    rec->line = line_number;
    rec->visibility = SYM_LOCAL;

    /*@-freshtrans -mustfree@*/
    return rec;
    /*@=freshtrans =mustfree@*/
}

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

symrec *
symrec_use(const char *name)
{
    symrec *rec = symrec_get_or_new(name, 1);

    rec->status |= SYM_USED;
    return rec;
}

static /*@dependent@*/ symrec *
symrec_define(const char *name, SymType type, int in_table)
{
    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) {
	Error(_("duplicate definition of `%s'; first defined on line %d"),
	      name, rec->line);
    } else {
	rec->line = line_number;	/* set line number of definition */
	rec->type = type;
	rec->status |= SYM_DEFINED;
    }
    return rec;
}

symrec *
symrec_define_equ(const char *name, expr *e)
{
    symrec *rec = symrec_define(name, SYM_EQU, 1);
    rec->value.expn = e;
    rec->status |= SYM_VALUED;
    return rec;
}

symrec *
symrec_define_label(const char *name, section *sect, bytecode *precbc,
		    int in_table)
{
    symrec *rec = symrec_define(name, SYM_LABEL, in_table);
    rec->value.label.sect = sect;
    rec->value.label.bc = precbc;
    return rec;
}

symrec *
symrec_declare(const char *name, SymVisibility vis)
{
    symrec *rec = symrec_get_or_new(name, 1);

    /* Don't allow EXTERN and COMMON if symbol has already been DEFINED. */
    /* Also, EXTERN and COMMON are mutually exclusive. */
    if ((rec->status & SYM_DEFINED) ||
	((rec->visibility & SYM_COMMON) && (vis == SYM_EXTERN)) ||
	((rec->visibility & SYM_EXTERN) && (vis == SYM_COMMON))) {
	Error(_("duplicate definition of `%s'; first defined on line %d"),
	      name, rec->line);
    } else {
	rec->line = line_number;	/* set line number of declaration */
	rec->visibility |= vis;

	/* If declared as COMMON or EXTERN, set as DEFINED. */
	if ((vis == SYM_COMMON) || (vis == SYM_EXTERN))
	    rec->status |= SYM_DEFINED;
    }
    return rec;
}
#if 0
int
symrec_get_int_value(const symrec *sym, unsigned long *ret_val,
		     int resolve_label)
{
    /* If we already know the value, just return it. */
    if (sym->status & SYM_VALUED) {
	switch (sym->type) {
	    case SYM_CONSTANT_INT:
		*ret_val = sym->value.int_val;
		break;
	    case SYM_CONSTANT_FLOAT:
		/* FIXME: Line number on this error will be incorrect. */
		if (floatnum_get_int(ret_val, sym->value.flt))
		    Error(_("Floating point value cannot fit in 32-bit single precision"));
		break;
	    case SYM_LABEL:
		if (!bytecode_get_offset(sym->value.label.sect,
					 sym->value.label.bc, ret_val))
		    InternalError(__LINE__, __FILE__,
				  _("Label symbol is valued but cannot get offset"));
	    case SYM_UNKNOWN:
		InternalError(__LINE__, __FILE__,
			      _("Have a valued symbol but of unknown type"));
	}
	return 1;
    }

    /* Try to get offset of unvalued label */
    if (resolve_label && sym->type == SYM_LABEL)
	return bytecode_get_offset(sym->value.label.sect, sym->value.label.bc,
				   ret_val);

    /* We can't get the value right now. */
    return 0;
}
#endif
const char *
symrec_get_name(const symrec *sym)
{
    return sym->name;
}

SymVisibility
symrec_get_visibility(const symrec *sym)
{
    return sym->visibility;
}

const expr *
symrec_get_equ(const symrec *sym)
{
    if (sym->type == SYM_EQU)
	return sym->value.expn;
    return (const expr *)NULL;
}

static int
symrec_parser_finalize_checksym(symrec *sym)
{
    /* error if a symbol is used but never defined */
    if ((sym->status & SYM_USED) && !(sym->status & SYM_DEFINED)) {
	ErrorAt(sym->filename, sym->line,
		_("undefined symbol `%s' (first use)"), sym->name);
	ErrorAt(sym->filename, sym->line,
		_("(Each undefined symbol is reported only once.)"));
	return 0;
    } else
	return 1;
}

void
symrec_parser_finalize(void)
{
    symrec_foreach(symrec_parser_finalize_checksym);
}

static void
symrec_delete_one(/*@only@*/ void *d)
{
    symrec *sym = d;
    xfree(sym->name);
    if (sym->type == SYM_EQU)
	expr_delete(sym->value.expn);
    xfree(sym);
}

void
symrec_delete_all(void)
{
    ternary_cleanup(sym_table, symrec_delete_one);
    sym_table = NULL;
}

void
symrec_delete(symrec *sym)
{
    /*@-branchstate@*/
    if (sym->status & SYM_NOTINTABLE)
	symrec_delete_one(sym);
    /*@=branchstate@*/
}

void
symrec_print(const symrec *sym)
{
    printf("---Symbol `%s'---\n", sym->name);

    switch (sym->type) {
	case SYM_UNKNOWN:
	    printf("-Unknown (Common/Extern)-\n");
	    break;
	case SYM_EQU:
	    printf("_EQU_\n");
	    printf("Expn=");
	    expr_print(sym->value.expn);
	    printf("\n");
	    break;
	case SYM_LABEL:
	    printf("_Label_\n");
	    printf("Section=`%s'\n", sym->value.label.sect?
		   section_get_name(sym->value.label.sect):"(nil)");
	    if (!sym->value.label.bc)
		printf("[First bytecode]\n");
	    else {
		printf("[Preceding bytecode]\n");
		bc_print(sym->value.label.bc);
	    }
	    break;
    }

    printf("Status=");
    if (sym->status == SYM_NOSTATUS)
	printf("None\n");
    else {
	if (sym->status & SYM_USED)
	    printf("Used,");
	if (sym->status & SYM_DEFINED)
	    printf("Defined,");
	if (sym->status & SYM_VALUED)
	    printf("Valued,");
	if (sym->status & SYM_NOTINTABLE)
	    printf("Not in Table,");
	printf("\n");
    }

    printf("Visibility=");
    if (sym->visibility == SYM_LOCAL)
	printf("Local\n");
    else {
	if (sym->visibility & SYM_GLOBAL)
	    printf("Global,");
	if (sym->visibility & SYM_COMMON)
	    printf("Common,");
	if (sym->visibility & SYM_EXTERN)
	    printf("Extern,");
	printf("\n");
    }

    printf("Filename=\"%s\" Line Number=%lu\n",
	   sym->filename?sym->filename:"(NULL)", sym->line);
}