Mercurial > hg-old > index.cgi
annotate lwasm/symbol.c @ 363:d96c30e60ddf
Added pass2 and various supporting logic including symbol lookups
author | lost@starbug |
---|---|
date | Tue, 06 Apr 2010 21:03:19 -0600 |
parents | 7d91ab7ac7d6 |
children | 6b33faa21a0a |
rev | line source |
---|---|
342 | 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 | |
344
0215a0fbf61b
Added assembly error system and additional checks for symbol syntax
lost@starbug
parents:
342
diff
changeset
|
32 struct symtabe *register_symbol(asmstate_t *as, line_t *cl, char *sym, lw_expr_t val, int flags) |
342 | 33 { |
34 struct symtabe *se; | |
35 int islocal = 0; | |
36 int context = -1; | |
37 int version = -1; | |
344
0215a0fbf61b
Added assembly error system and additional checks for symbol syntax
lost@starbug
parents:
342
diff
changeset
|
38 char *cp; |
342 | 39 |
344
0215a0fbf61b
Added assembly error system and additional checks for symbol syntax
lost@starbug
parents:
342
diff
changeset
|
40 if (*sym < 0x80 && !strchr(SSYMCHARS, *sym)) |
0215a0fbf61b
Added assembly error system and additional checks for symbol syntax
lost@starbug
parents:
342
diff
changeset
|
41 { |
0215a0fbf61b
Added assembly error system and additional checks for symbol syntax
lost@starbug
parents:
342
diff
changeset
|
42 lwasm_register_error(as, cl, "Bad symbol (%s)", sym); |
0215a0fbf61b
Added assembly error system and additional checks for symbol syntax
lost@starbug
parents:
342
diff
changeset
|
43 return NULL; |
0215a0fbf61b
Added assembly error system and additional checks for symbol syntax
lost@starbug
parents:
342
diff
changeset
|
44 } |
0215a0fbf61b
Added assembly error system and additional checks for symbol syntax
lost@starbug
parents:
342
diff
changeset
|
45 |
0215a0fbf61b
Added assembly error system and additional checks for symbol syntax
lost@starbug
parents:
342
diff
changeset
|
46 if ((*sym == '$' || *sym == '@') && (sym[1] >= '0' && sym[1] <= '9')) |
0215a0fbf61b
Added assembly error system and additional checks for symbol syntax
lost@starbug
parents:
342
diff
changeset
|
47 { |
0215a0fbf61b
Added assembly error system and additional checks for symbol syntax
lost@starbug
parents:
342
diff
changeset
|
48 lwasm_register_error(as, cl, "Bad symbol (%s)", sym); |
0215a0fbf61b
Added assembly error system and additional checks for symbol syntax
lost@starbug
parents:
342
diff
changeset
|
49 return NULL; |
0215a0fbf61b
Added assembly error system and additional checks for symbol syntax
lost@starbug
parents:
342
diff
changeset
|
50 } |
0215a0fbf61b
Added assembly error system and additional checks for symbol syntax
lost@starbug
parents:
342
diff
changeset
|
51 |
0215a0fbf61b
Added assembly error system and additional checks for symbol syntax
lost@starbug
parents:
342
diff
changeset
|
52 for (cp = sym; *cp; cp++) |
0215a0fbf61b
Added assembly error system and additional checks for symbol syntax
lost@starbug
parents:
342
diff
changeset
|
53 { |
0215a0fbf61b
Added assembly error system and additional checks for symbol syntax
lost@starbug
parents:
342
diff
changeset
|
54 if (*cp == '@' || *cp == '?') |
0215a0fbf61b
Added assembly error system and additional checks for symbol syntax
lost@starbug
parents:
342
diff
changeset
|
55 islocal = 1; |
360
7d91ab7ac7d6
Indexed stage 2; set line structure to track pragmas in effect for that line
lost@starbug
parents:
346
diff
changeset
|
56 if (*cp == '$' && !(CURPRAGMA(cl, PRAGMA_DOLLARNOTLOCAL))) |
344
0215a0fbf61b
Added assembly error system and additional checks for symbol syntax
lost@starbug
parents:
342
diff
changeset
|
57 islocal = 1; |
0215a0fbf61b
Added assembly error system and additional checks for symbol syntax
lost@starbug
parents:
342
diff
changeset
|
58 |
0215a0fbf61b
Added assembly error system and additional checks for symbol syntax
lost@starbug
parents:
342
diff
changeset
|
59 // bad symbol |
0215a0fbf61b
Added assembly error system and additional checks for symbol syntax
lost@starbug
parents:
342
diff
changeset
|
60 if (*cp < 0x80 && !strchr(SYMCHARS, *cp)) |
0215a0fbf61b
Added assembly error system and additional checks for symbol syntax
lost@starbug
parents:
342
diff
changeset
|
61 { |
0215a0fbf61b
Added assembly error system and additional checks for symbol syntax
lost@starbug
parents:
342
diff
changeset
|
62 lwasm_register_error(as, cl, "Bad symbol (%s)", sym); |
0215a0fbf61b
Added assembly error system and additional checks for symbol syntax
lost@starbug
parents:
342
diff
changeset
|
63 return NULL; |
0215a0fbf61b
Added assembly error system and additional checks for symbol syntax
lost@starbug
parents:
342
diff
changeset
|
64 } |
0215a0fbf61b
Added assembly error system and additional checks for symbol syntax
lost@starbug
parents:
342
diff
changeset
|
65 } |
342 | 66 |
67 if (islocal) | |
363
d96c30e60ddf
Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
360
diff
changeset
|
68 context = cl -> context; |
342 | 69 |
70 // first, look up symbol to see if it is already defined | |
71 for (se = as -> symtab.head; se; se = se -> next) | |
72 { | |
73 if (!strcmp(sym, se -> symbol)) | |
74 { | |
75 if (se -> context != context) | |
76 continue; | |
77 if ((flags & symbol_flag_set) && (se -> flags & symbol_flag_set)) | |
78 { | |
79 if (version < se -> version) | |
80 version = se -> version; | |
81 continue; | |
82 } | |
83 break; | |
84 } | |
85 } | |
86 if (se) | |
87 { | |
88 // multiply defined symbol | |
344
0215a0fbf61b
Added assembly error system and additional checks for symbol syntax
lost@starbug
parents:
342
diff
changeset
|
89 lwasm_register_error(as, cl, "Multiply defined symbol (%s)", sym); |
342 | 90 return NULL; |
91 } | |
92 | |
93 if (flags & symbol_flag_set) | |
94 { | |
95 version++; | |
96 } | |
97 | |
363
d96c30e60ddf
Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
360
diff
changeset
|
98 // symplify the symbol expression - replaces "SET" symbols with |
d96c30e60ddf
Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
360
diff
changeset
|
99 // symbol table entries |
d96c30e60ddf
Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
360
diff
changeset
|
100 lwasm_reduce_expr(as, val); |
d96c30e60ddf
Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
360
diff
changeset
|
101 |
342 | 102 se = lw_alloc(sizeof(struct symtabe)); |
103 se -> next = as -> symtab.head; | |
104 as -> symtab.head = se; | |
105 se -> context = context; | |
106 se -> version = version; | |
107 se -> flags = flags; | |
108 se -> value = lw_expr_copy(val); | |
109 return se; | |
110 } | |
111 | |
363
d96c30e60ddf
Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
360
diff
changeset
|
112 // for "SET" symbols, always returns the LAST definition of the |
d96c30e60ddf
Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
360
diff
changeset
|
113 // symbol. This works because the lwasm_reduce_expr() call in |
d96c30e60ddf
Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
360
diff
changeset
|
114 // register_symbol will ensure there are no lingering "var" references |
d96c30e60ddf
Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
360
diff
changeset
|
115 // to the set symbol anywhere in the symbol table; they will all be |
d96c30e60ddf
Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
360
diff
changeset
|
116 // converted to direct references |
d96c30e60ddf
Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
360
diff
changeset
|
117 // NOTE: this means that for a forward reference to a SET symbol, |
d96c30e60ddf
Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
360
diff
changeset
|
118 // the LAST definition will be the one used. |
d96c30e60ddf
Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
360
diff
changeset
|
119 // This arrangement also ensures that any reference to the symbol |
d96c30e60ddf
Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
360
diff
changeset
|
120 // itself inside a "set" definition will refer to the previous version |
d96c30e60ddf
Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
360
diff
changeset
|
121 // of the symbol. |
d96c30e60ddf
Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
360
diff
changeset
|
122 struct symtabe * lookup_symbol(asmstate_t *as, line_t *cl, char *sym) |
342 | 123 { |
363
d96c30e60ddf
Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
360
diff
changeset
|
124 int local = 0; |
d96c30e60ddf
Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
360
diff
changeset
|
125 struct symtabe *s, *s2; |
d96c30e60ddf
Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
360
diff
changeset
|
126 |
d96c30e60ddf
Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
360
diff
changeset
|
127 // check if this is a local symbol |
d96c30e60ddf
Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
360
diff
changeset
|
128 if (strchr(sym, '@') || strchr(sym, '?')) |
d96c30e60ddf
Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
360
diff
changeset
|
129 local = 1; |
d96c30e60ddf
Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
360
diff
changeset
|
130 |
d96c30e60ddf
Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
360
diff
changeset
|
131 if (cl && !CURPRAGMA(cl, PRAGMA_DOLLARNOTLOCAL) && strchr(sym, '$')) |
d96c30e60ddf
Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
360
diff
changeset
|
132 local = 1; |
d96c30e60ddf
Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
360
diff
changeset
|
133 if (!cl && !(as -> pragmas & PRAGMA_DOLLARNOTLOCAL) && strchr(sym, '$')) |
d96c30e60ddf
Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
360
diff
changeset
|
134 local = 1; |
d96c30e60ddf
Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
360
diff
changeset
|
135 |
d96c30e60ddf
Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
360
diff
changeset
|
136 // cannot look up local symbol in global context!!!!! |
d96c30e60ddf
Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
360
diff
changeset
|
137 if (!cl && local) |
d96c30e60ddf
Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
360
diff
changeset
|
138 return NULL; |
d96c30e60ddf
Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
360
diff
changeset
|
139 |
d96c30e60ddf
Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
360
diff
changeset
|
140 for (s = as -> symtab.head, s2 = NULL; s; s = s -> next) |
d96c30e60ddf
Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
360
diff
changeset
|
141 { |
d96c30e60ddf
Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
360
diff
changeset
|
142 if (!strcmp(sym, s -> symbol)) |
d96c30e60ddf
Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
360
diff
changeset
|
143 { |
d96c30e60ddf
Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
360
diff
changeset
|
144 if (local && s -> context != cl -> context) |
d96c30e60ddf
Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
360
diff
changeset
|
145 continue; |
d96c30e60ddf
Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
360
diff
changeset
|
146 |
d96c30e60ddf
Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
360
diff
changeset
|
147 if (s -> flags & symbol_flag_set) |
d96c30e60ddf
Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
360
diff
changeset
|
148 { |
d96c30e60ddf
Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
360
diff
changeset
|
149 // look for highest version of symbol |
d96c30e60ddf
Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
360
diff
changeset
|
150 if (s -> version > s2 -> version) |
d96c30e60ddf
Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
360
diff
changeset
|
151 s2 = s; |
d96c30e60ddf
Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
360
diff
changeset
|
152 continue; |
d96c30e60ddf
Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
360
diff
changeset
|
153 } |
d96c30e60ddf
Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
360
diff
changeset
|
154 break; |
d96c30e60ddf
Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
360
diff
changeset
|
155 } |
d96c30e60ddf
Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
360
diff
changeset
|
156 } |
d96c30e60ddf
Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
360
diff
changeset
|
157 if (!s && s2) |
d96c30e60ddf
Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
360
diff
changeset
|
158 s = s2; |
d96c30e60ddf
Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
360
diff
changeset
|
159 |
d96c30e60ddf
Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents:
360
diff
changeset
|
160 return s; |
342 | 161 } |