comparison lwasm/symbol.c @ 342:7b4123dce741

Added basic symbol registration
author lost@starbug
date Wed, 24 Mar 2010 21:30:31 -0600
parents
children 0215a0fbf61b
comparison
equal deleted inserted replaced
341:4e1cff60c293 342:7b4123dce741
1 /*
2 symbol.c
3
4 Copyright © 2010 William Astle
5
6 This file is part of LWTOOLS.
7
8 LWTOOLS is free software: you can redistribute it and/or modify it under the
9 terms of the GNU General Public License as published by the Free Software
10 Foundation, either version 3 of the License, or (at your option) any later
11 version.
12
13 This program is distributed in the hope that it will be useful, but WITHOUT
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 more details.
17
18 You should have received a copy of the GNU General Public License along with
19 this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include <config.h>
23
24 #include <stdlib.h>
25 #include <string.h>
26
27 #include <lw_alloc.h>
28 #include <lw_expr.h>
29
30 #include "lwasm.h"
31
32 struct symtabe *register_symbol(asmstate_t *as, char *sym, lw_expr_t val, int flags)
33 {
34 struct symtabe *se;
35 int islocal = 0;
36 int context = -1;
37 int version = -1;
38
39 if (strchr(sym, '@') || strchr(sym, '?'))
40 islocal = 1;
41 if (!(as -> pragmas & PRAGMA_DOLLARNOTLOCAL) && strchr(sym, '$'))
42 islocal = 1;
43
44 if (islocal)
45 context = as -> context;
46
47 // first, look up symbol to see if it is already defined
48 for (se = as -> symtab.head; se; se = se -> next)
49 {
50 if (!strcmp(sym, se -> symbol))
51 {
52 if (se -> context != context)
53 continue;
54 if ((flags & symbol_flag_set) && (se -> flags & symbol_flag_set))
55 {
56 if (version < se -> version)
57 version = se -> version;
58 continue;
59 }
60 break;
61 }
62 }
63 if (se)
64 {
65 // multiply defined symbol
66 return NULL;
67 }
68
69 if (flags & symbol_flag_set)
70 {
71 version++;
72 }
73
74 se = lw_alloc(sizeof(struct symtabe));
75 se -> next = as -> symtab.head;
76 as -> symtab.head = se;
77 se -> context = context;
78 se -> version = version;
79 se -> flags = flags;
80 se -> value = lw_expr_copy(val);
81 return se;
82 }
83
84 struct symtabe * lookup_symbol(asmstate_t *as, char *sym, int context, int version)
85 {
86 return NULL;
87 }