comparison src/symbol.c @ 37:538e15927776

Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
author lost
date Sat, 03 Jan 2009 04:20:49 +0000
parents
children d2cee0c335e7
comparison
equal deleted inserted replaced
36:99e3b3310bac 37:538e15927776
1 /*
2 symbol.c
3 Copyright © 2009 William Astle
4
5 This file is part of LWASM.
6
7 LWASM is free software: you can redistribute it and/or modify it under the
8 terms of the GNU General Public License as published by the Free Software
9 Foundation, either version 3 of the License, or (at your option) any later
10 version.
11
12 This program is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 more details.
16
17 You should have received a copy of the GNU General Public License along with
18 this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 /*
22 for handling the symbol table
23 */
24
25 #define __symbol_c_seen__
26
27 #include <string.h>
28
29 #include "lwasm.h"
30 #include "util.h"
31
32 int lwasm_register_symbol(asmstate_t *as, lwasm_line_t *l, char *sym, int val)
33 {
34 lwasm_symbol_ent_t *se, *se2;
35 char *p;
36
37 int scontext = -1;
38
39 // first check if the symbol is valid
40 // the following characters are allowed in a symbol:
41 // [a-zA-Z0-9._$?@] and any byte value larger than 0x7F
42 // although symbols should be restricted to the 7 bit range
43 // symbols must start with [a-zA-Z._]
44 if (!strchr("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz._@?", *sym))
45 {
46 register_error(as, l, 1, "Bad symbol: %s", sym);
47 return -1;
48 }
49
50 for (p = sym; *p; p++)
51 {
52 if (!strchr("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz._$?@0123456789", *sym) && (unsigned char)*sym < 0x80)
53 {
54 register_error(as, l, 1, "Bad symbol: %s", sym);
55 return -1;
56 }
57 // flag local symbols while we're at it...
58 if (*p == '?' || *p == '@')
59 scontext = as -> context;
60 }
61
62 // now look it for to see if it is a duplicate
63 se = lwasm_find_symbol(as, sym, scontext);
64 if (se)
65 {
66 register_error(as, l, 1, "Mulitply defined symbol: %s", sym);
67 return -1;
68 }
69
70 // if not a duplicate, register it with the value
71 se = lwasm_alloc(sizeof(lwasm_symbol_ent_t));
72 if (as -> symhead)
73 {
74 se -> prev = NULL;
75 se -> next = as -> symhead;
76 as -> symhead -> prev = se;
77 as -> symhead = se;
78 }
79 else
80 {
81 se -> next = NULL;
82 se -> prev = NULL;
83 as -> symhead = se;
84 as -> symtail = se;
85 }
86 se -> value = val;
87 se -> sym = lwasm_strdup(sym);
88 se -> context = scontext;
89
90 return 0;
91 }
92
93 lwasm_symbol_ent_t *lwasm_find_symbol(asmstate_t *as, char *sym, int scontext)
94 {
95 lwasm_symbol_ent_t *se;
96
97 for (se = as -> symhead; se; se = se -> next)
98 {
99 if (scontext == se -> context && !strcmp(sym, se -> sym))
100 {
101 return se;
102 }
103 }
104 return NULL;
105 }
106
107 // reset the value of a symbol - should not be used normally
108 // it is intended for use by such operations as EQU
109 // returns -1 if the symbol is not registered
110 int lwasm_set_symbol(asmstate_t *as, char *sym, int scontext, int val)
111 {
112 lwasm_symbol_ent_t *se;
113
114 se = lwasm_find_symbol(as, sym, scontext);
115 if (!se)
116 return -1;
117
118 se -> value = val;
119 return 0;
120 }