annotate lwasm/symbol.c @ 426:652eee8f0c82

Fixed lw_expr_destroy() to not crash on NULL
author lost@l-w.ca
date Sun, 19 Sep 2010 10:40:37 -0600
parents cac204676434
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
342
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
1 /*
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
2 symbol.c
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
3
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
4 Copyright © 2010 William Astle
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
5
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
6 This file is part of LWTOOLS.
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
7
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
8 LWTOOLS is free software: you can redistribute it and/or modify it under the
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
9 terms of the GNU General Public License as published by the Free Software
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
10 Foundation, either version 3 of the License, or (at your option) any later
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
11 version.
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
12
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
13 This program is distributed in the hope that it will be useful, but WITHOUT
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
16 more details.
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
17
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
18 You should have received a copy of the GNU General Public License along with
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
19 this program. If not, see <http://www.gnu.org/licenses/>.
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
20 */
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
21
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
22 #include <config.h>
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
23
390
027d7fbcdcfc Basic symbol table output; needs work for non-constant symbols
lost@l-w.ca
parents: 389
diff changeset
24 #include <stdio.h>
342
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
25 #include <stdlib.h>
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
26 #include <string.h>
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
27
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
28 #include <lw_alloc.h>
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
29 #include <lw_expr.h>
370
6b33faa21a0a Debugging output and bugfixing pass 0
lost@starbug
parents: 363
diff changeset
30 #include <lw_string.h>
342
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
31
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
32 #include "lwasm.h"
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
33
406
502fbc37ff4e Symbol table is now sorted in lwasm
lost@l-w.ca
parents: 405
diff changeset
34 struct symtabe *symbol_findprev(asmstate_t *as, struct symtabe *se)
502fbc37ff4e Symbol table is now sorted in lwasm
lost@l-w.ca
parents: 405
diff changeset
35 {
502fbc37ff4e Symbol table is now sorted in lwasm
lost@l-w.ca
parents: 405
diff changeset
36 struct symtabe *se1, *se2;
502fbc37ff4e Symbol table is now sorted in lwasm
lost@l-w.ca
parents: 405
diff changeset
37 int i;
502fbc37ff4e Symbol table is now sorted in lwasm
lost@l-w.ca
parents: 405
diff changeset
38
502fbc37ff4e Symbol table is now sorted in lwasm
lost@l-w.ca
parents: 405
diff changeset
39 for (se2 = NULL, se1 = as -> symtab.head; se1; se1 = se1 -> next)
502fbc37ff4e Symbol table is now sorted in lwasm
lost@l-w.ca
parents: 405
diff changeset
40 {
502fbc37ff4e Symbol table is now sorted in lwasm
lost@l-w.ca
parents: 405
diff changeset
41 debug_message(as, 200, "Sorting; looking at symbol %s (%p) for %s", se1 -> symbol, se1, se -> symbol);
502fbc37ff4e Symbol table is now sorted in lwasm
lost@l-w.ca
parents: 405
diff changeset
42 /* compare se with se1 */
502fbc37ff4e Symbol table is now sorted in lwasm
lost@l-w.ca
parents: 405
diff changeset
43 i = strcasecmp(se -> symbol, se1 -> symbol);
502fbc37ff4e Symbol table is now sorted in lwasm
lost@l-w.ca
parents: 405
diff changeset
44
502fbc37ff4e Symbol table is now sorted in lwasm
lost@l-w.ca
parents: 405
diff changeset
45 /* if the symbol sorts before se1, we just need to return */
502fbc37ff4e Symbol table is now sorted in lwasm
lost@l-w.ca
parents: 405
diff changeset
46 if (i < 0)
502fbc37ff4e Symbol table is now sorted in lwasm
lost@l-w.ca
parents: 405
diff changeset
47 return se2;
502fbc37ff4e Symbol table is now sorted in lwasm
lost@l-w.ca
parents: 405
diff changeset
48
502fbc37ff4e Symbol table is now sorted in lwasm
lost@l-w.ca
parents: 405
diff changeset
49 if (i == 0)
502fbc37ff4e Symbol table is now sorted in lwasm
lost@l-w.ca
parents: 405
diff changeset
50 {
502fbc37ff4e Symbol table is now sorted in lwasm
lost@l-w.ca
parents: 405
diff changeset
51 /* symbol name matches; compare other things */
502fbc37ff4e Symbol table is now sorted in lwasm
lost@l-w.ca
parents: 405
diff changeset
52
502fbc37ff4e Symbol table is now sorted in lwasm
lost@l-w.ca
parents: 405
diff changeset
53 /*if next version is greater than this one, return */
502fbc37ff4e Symbol table is now sorted in lwasm
lost@l-w.ca
parents: 405
diff changeset
54 if (se -> version > se1 -> version)
502fbc37ff4e Symbol table is now sorted in lwasm
lost@l-w.ca
parents: 405
diff changeset
55 return se2;
502fbc37ff4e Symbol table is now sorted in lwasm
lost@l-w.ca
parents: 405
diff changeset
56 /* if next context is great than this one, return */
502fbc37ff4e Symbol table is now sorted in lwasm
lost@l-w.ca
parents: 405
diff changeset
57 if (se -> context > se1 -> context)
502fbc37ff4e Symbol table is now sorted in lwasm
lost@l-w.ca
parents: 405
diff changeset
58 return se2;
502fbc37ff4e Symbol table is now sorted in lwasm
lost@l-w.ca
parents: 405
diff changeset
59
502fbc37ff4e Symbol table is now sorted in lwasm
lost@l-w.ca
parents: 405
diff changeset
60 /* if section name is greater, return */
502fbc37ff4e Symbol table is now sorted in lwasm
lost@l-w.ca
parents: 405
diff changeset
61 /* if se has no section but se1 does, we go first */
502fbc37ff4e Symbol table is now sorted in lwasm
lost@l-w.ca
parents: 405
diff changeset
62 if (se -> section == NULL && se1 -> section != NULL)
502fbc37ff4e Symbol table is now sorted in lwasm
lost@l-w.ca
parents: 405
diff changeset
63 return se2;
502fbc37ff4e Symbol table is now sorted in lwasm
lost@l-w.ca
parents: 405
diff changeset
64 if (se -> section != NULL && se -> section != NULL)
502fbc37ff4e Symbol table is now sorted in lwasm
lost@l-w.ca
parents: 405
diff changeset
65 {
502fbc37ff4e Symbol table is now sorted in lwasm
lost@l-w.ca
parents: 405
diff changeset
66 /* compare section names and if se < se1, return */
502fbc37ff4e Symbol table is now sorted in lwasm
lost@l-w.ca
parents: 405
diff changeset
67 i = strcasecmp(se -> section -> name, se1 -> section -> name);
502fbc37ff4e Symbol table is now sorted in lwasm
lost@l-w.ca
parents: 405
diff changeset
68 if (i < 0)
502fbc37ff4e Symbol table is now sorted in lwasm
lost@l-w.ca
parents: 405
diff changeset
69 return se2;
502fbc37ff4e Symbol table is now sorted in lwasm
lost@l-w.ca
parents: 405
diff changeset
70 }
502fbc37ff4e Symbol table is now sorted in lwasm
lost@l-w.ca
parents: 405
diff changeset
71 }
502fbc37ff4e Symbol table is now sorted in lwasm
lost@l-w.ca
parents: 405
diff changeset
72
502fbc37ff4e Symbol table is now sorted in lwasm
lost@l-w.ca
parents: 405
diff changeset
73 se2 = se1;
502fbc37ff4e Symbol table is now sorted in lwasm
lost@l-w.ca
parents: 405
diff changeset
74 }
502fbc37ff4e Symbol table is now sorted in lwasm
lost@l-w.ca
parents: 405
diff changeset
75 return se2;
502fbc37ff4e Symbol table is now sorted in lwasm
lost@l-w.ca
parents: 405
diff changeset
76 }
502fbc37ff4e Symbol table is now sorted in lwasm
lost@l-w.ca
parents: 405
diff changeset
77
344
0215a0fbf61b Added assembly error system and additional checks for symbol syntax
lost@starbug
parents: 342
diff changeset
78 struct symtabe *register_symbol(asmstate_t *as, line_t *cl, char *sym, lw_expr_t val, int flags)
342
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
79 {
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
80 struct symtabe *se;
406
502fbc37ff4e Symbol table is now sorted in lwasm
lost@l-w.ca
parents: 405
diff changeset
81 struct symtabe *sprev;
342
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
82 int islocal = 0;
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
83 int context = -1;
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
84 int version = -1;
344
0215a0fbf61b Added assembly error system and additional checks for symbol syntax
lost@starbug
parents: 342
diff changeset
85 char *cp;
342
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
86
406
502fbc37ff4e Symbol table is now sorted in lwasm
lost@l-w.ca
parents: 405
diff changeset
87 debug_message(as, 200, "Register symbol %s (%02X), %s", sym, flags, lw_expr_print(val));
502fbc37ff4e Symbol table is now sorted in lwasm
lost@l-w.ca
parents: 405
diff changeset
88
389
fbb7bfed8076 Added in structure support and fixed up some warts in the listing code (by adding more warts)
lost@l-w.ca
parents: 374
diff changeset
89 if (!(flags & symbol_flag_nocheck))
344
0215a0fbf61b Added assembly error system and additional checks for symbol syntax
lost@starbug
parents: 342
diff changeset
90 {
411
cac204676434 Allow symbols to start with digits if they contain $, ?, or @; numbered locals
lost@l-w.ca
parents: 406
diff changeset
91 if (!sym || !*sym)
cac204676434 Allow symbols to start with digits if they contain $, ?, or @; numbered locals
lost@l-w.ca
parents: 406
diff changeset
92 {
cac204676434 Allow symbols to start with digits if they contain $, ?, or @; numbered locals
lost@l-w.ca
parents: 406
diff changeset
93 lwasm_register_error(as, cl, "Bad symbol (%s)", sym);
cac204676434 Allow symbols to start with digits if they contain $, ?, or @; numbered locals
lost@l-w.ca
parents: 406
diff changeset
94 return NULL;
cac204676434 Allow symbols to start with digits if they contain $, ?, or @; numbered locals
lost@l-w.ca
parents: 406
diff changeset
95 }
cac204676434 Allow symbols to start with digits if they contain $, ?, or @; numbered locals
lost@l-w.ca
parents: 406
diff changeset
96 if (*sym < 0x80 && (!strchr(SSYMCHARS, *sym) && !strchr(sym + 1, '$') && !strchr(sym + 1, '@') && !strchr(sym + 1, '?')))
389
fbb7bfed8076 Added in structure support and fixed up some warts in the listing code (by adding more warts)
lost@l-w.ca
parents: 374
diff changeset
97 {
fbb7bfed8076 Added in structure support and fixed up some warts in the listing code (by adding more warts)
lost@l-w.ca
parents: 374
diff changeset
98 lwasm_register_error(as, cl, "Bad symbol (%s)", sym);
fbb7bfed8076 Added in structure support and fixed up some warts in the listing code (by adding more warts)
lost@l-w.ca
parents: 374
diff changeset
99 return NULL;
fbb7bfed8076 Added in structure support and fixed up some warts in the listing code (by adding more warts)
lost@l-w.ca
parents: 374
diff changeset
100 }
344
0215a0fbf61b Added assembly error system and additional checks for symbol syntax
lost@starbug
parents: 342
diff changeset
101
389
fbb7bfed8076 Added in structure support and fixed up some warts in the listing code (by adding more warts)
lost@l-w.ca
parents: 374
diff changeset
102 if ((*sym == '$' || *sym == '@') && (sym[1] >= '0' && sym[1] <= '9'))
fbb7bfed8076 Added in structure support and fixed up some warts in the listing code (by adding more warts)
lost@l-w.ca
parents: 374
diff changeset
103 {
fbb7bfed8076 Added in structure support and fixed up some warts in the listing code (by adding more warts)
lost@l-w.ca
parents: 374
diff changeset
104 lwasm_register_error(as, cl, "Bad symbol (%s)", sym);
fbb7bfed8076 Added in structure support and fixed up some warts in the listing code (by adding more warts)
lost@l-w.ca
parents: 374
diff changeset
105 return NULL;
fbb7bfed8076 Added in structure support and fixed up some warts in the listing code (by adding more warts)
lost@l-w.ca
parents: 374
diff changeset
106 }
344
0215a0fbf61b Added assembly error system and additional checks for symbol syntax
lost@starbug
parents: 342
diff changeset
107 }
0215a0fbf61b Added assembly error system and additional checks for symbol syntax
lost@starbug
parents: 342
diff changeset
108
0215a0fbf61b Added assembly error system and additional checks for symbol syntax
lost@starbug
parents: 342
diff changeset
109 for (cp = sym; *cp; cp++)
0215a0fbf61b Added assembly error system and additional checks for symbol syntax
lost@starbug
parents: 342
diff changeset
110 {
0215a0fbf61b Added assembly error system and additional checks for symbol syntax
lost@starbug
parents: 342
diff changeset
111 if (*cp == '@' || *cp == '?')
0215a0fbf61b Added assembly error system and additional checks for symbol syntax
lost@starbug
parents: 342
diff changeset
112 islocal = 1;
360
7d91ab7ac7d6 Indexed stage 2; set line structure to track pragmas in effect for that line
lost@starbug
parents: 346
diff changeset
113 if (*cp == '$' && !(CURPRAGMA(cl, PRAGMA_DOLLARNOTLOCAL)))
344
0215a0fbf61b Added assembly error system and additional checks for symbol syntax
lost@starbug
parents: 342
diff changeset
114 islocal = 1;
0215a0fbf61b Added assembly error system and additional checks for symbol syntax
lost@starbug
parents: 342
diff changeset
115
0215a0fbf61b Added assembly error system and additional checks for symbol syntax
lost@starbug
parents: 342
diff changeset
116 // bad symbol
389
fbb7bfed8076 Added in structure support and fixed up some warts in the listing code (by adding more warts)
lost@l-w.ca
parents: 374
diff changeset
117 if (!(flags & symbol_flag_nocheck) && *cp < 0x80 && !strchr(SYMCHARS, *cp))
344
0215a0fbf61b Added assembly error system and additional checks for symbol syntax
lost@starbug
parents: 342
diff changeset
118 {
0215a0fbf61b Added assembly error system and additional checks for symbol syntax
lost@starbug
parents: 342
diff changeset
119 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
120 return NULL;
0215a0fbf61b Added assembly error system and additional checks for symbol syntax
lost@starbug
parents: 342
diff changeset
121 }
0215a0fbf61b Added assembly error system and additional checks for symbol syntax
lost@starbug
parents: 342
diff changeset
122 }
342
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
123
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
124 if (islocal)
363
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents: 360
diff changeset
125 context = cl -> context;
342
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
126
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
127 // first, look up symbol to see if it is already defined
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
128 for (se = as -> symtab.head; se; se = se -> next)
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
129 {
406
502fbc37ff4e Symbol table is now sorted in lwasm
lost@l-w.ca
parents: 405
diff changeset
130 debug_message(as, 300, "Symbol add lookup: %p, %p", se, se -> next);
342
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
131 if (!strcmp(sym, se -> symbol))
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
132 {
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
133 if (se -> context != context)
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
134 continue;
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
135 if ((flags & symbol_flag_set) && (se -> flags & symbol_flag_set))
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
136 {
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
137 if (version < se -> version)
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
138 version = se -> version;
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
139 continue;
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
140 }
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
141 break;
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
142 }
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
143 }
389
fbb7bfed8076 Added in structure support and fixed up some warts in the listing code (by adding more warts)
lost@l-w.ca
parents: 374
diff changeset
144
342
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
145 if (se)
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
146 {
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
147 // multiply defined symbol
344
0215a0fbf61b Added assembly error system and additional checks for symbol syntax
lost@starbug
parents: 342
diff changeset
148 lwasm_register_error(as, cl, "Multiply defined symbol (%s)", sym);
342
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
149 return NULL;
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
150 }
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
151
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
152 if (flags & symbol_flag_set)
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
153 {
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
154 version++;
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
155 }
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
156
363
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents: 360
diff changeset
157 // symplify the symbol expression - replaces "SET" symbols with
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents: 360
diff changeset
158 // symbol table entries
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents: 360
diff changeset
159 lwasm_reduce_expr(as, val);
406
502fbc37ff4e Symbol table is now sorted in lwasm
lost@l-w.ca
parents: 405
diff changeset
160
342
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
161 se = lw_alloc(sizeof(struct symtabe));
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
162 se -> context = context;
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
163 se -> version = version;
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
164 se -> flags = flags;
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
165 se -> value = lw_expr_copy(val);
370
6b33faa21a0a Debugging output and bugfixing pass 0
lost@starbug
parents: 363
diff changeset
166 se -> symbol = lw_strdup(sym);
374
d99322ef6f21 Stage 1: actually do output
lost@starbug
parents: 370
diff changeset
167 se -> section = cl -> csect;
406
502fbc37ff4e Symbol table is now sorted in lwasm
lost@l-w.ca
parents: 405
diff changeset
168 sprev = symbol_findprev(as, se);
502fbc37ff4e Symbol table is now sorted in lwasm
lost@l-w.ca
parents: 405
diff changeset
169 if (!sprev)
502fbc37ff4e Symbol table is now sorted in lwasm
lost@l-w.ca
parents: 405
diff changeset
170 {
502fbc37ff4e Symbol table is now sorted in lwasm
lost@l-w.ca
parents: 405
diff changeset
171 debug_message(as, 200, "Adding symbol at head of symbol table");
502fbc37ff4e Symbol table is now sorted in lwasm
lost@l-w.ca
parents: 405
diff changeset
172 se -> next = as -> symtab.head;
502fbc37ff4e Symbol table is now sorted in lwasm
lost@l-w.ca
parents: 405
diff changeset
173 as -> symtab.head = se;
502fbc37ff4e Symbol table is now sorted in lwasm
lost@l-w.ca
parents: 405
diff changeset
174 }
502fbc37ff4e Symbol table is now sorted in lwasm
lost@l-w.ca
parents: 405
diff changeset
175 else
502fbc37ff4e Symbol table is now sorted in lwasm
lost@l-w.ca
parents: 405
diff changeset
176 {
502fbc37ff4e Symbol table is now sorted in lwasm
lost@l-w.ca
parents: 405
diff changeset
177 debug_message(as, 200, "Adding symbol in middle of symbol table");
502fbc37ff4e Symbol table is now sorted in lwasm
lost@l-w.ca
parents: 405
diff changeset
178 se -> next = sprev -> next;
502fbc37ff4e Symbol table is now sorted in lwasm
lost@l-w.ca
parents: 405
diff changeset
179 sprev -> next = se;
502fbc37ff4e Symbol table is now sorted in lwasm
lost@l-w.ca
parents: 405
diff changeset
180 }
342
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
181 return se;
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
182 }
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
183
363
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents: 360
diff changeset
184 // 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
185 // 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
186 // 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
187 // 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
188 // converted to direct references
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents: 360
diff changeset
189 // 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
190 // the LAST definition will be the one used.
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents: 360
diff changeset
191 // 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
192 // 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
193 // of the symbol.
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents: 360
diff changeset
194 struct symtabe * lookup_symbol(asmstate_t *as, line_t *cl, char *sym)
342
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
195 {
363
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents: 360
diff changeset
196 int local = 0;
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents: 360
diff changeset
197 struct symtabe *s, *s2;
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents: 360
diff changeset
198
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents: 360
diff changeset
199 // check if this is a local symbol
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents: 360
diff changeset
200 if (strchr(sym, '@') || strchr(sym, '?'))
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents: 360
diff changeset
201 local = 1;
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents: 360
diff changeset
202
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents: 360
diff changeset
203 if (cl && !CURPRAGMA(cl, PRAGMA_DOLLARNOTLOCAL) && strchr(sym, '$'))
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents: 360
diff changeset
204 local = 1;
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents: 360
diff changeset
205 if (!cl && !(as -> pragmas & PRAGMA_DOLLARNOTLOCAL) && strchr(sym, '$'))
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents: 360
diff changeset
206 local = 1;
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents: 360
diff changeset
207
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents: 360
diff changeset
208 // cannot look up local symbol in global context!!!!!
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents: 360
diff changeset
209 if (!cl && local)
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents: 360
diff changeset
210 return NULL;
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents: 360
diff changeset
211
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents: 360
diff changeset
212 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
213 {
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents: 360
diff changeset
214 if (!strcmp(sym, s -> symbol))
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents: 360
diff changeset
215 {
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents: 360
diff changeset
216 if (local && s -> context != cl -> context)
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents: 360
diff changeset
217 continue;
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents: 360
diff changeset
218
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents: 360
diff changeset
219 if (s -> flags & symbol_flag_set)
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents: 360
diff changeset
220 {
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents: 360
diff changeset
221 // look for highest version of symbol
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents: 360
diff changeset
222 if (s -> version > s2 -> version)
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents: 360
diff changeset
223 s2 = s;
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents: 360
diff changeset
224 continue;
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents: 360
diff changeset
225 }
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents: 360
diff changeset
226 break;
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents: 360
diff changeset
227 }
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents: 360
diff changeset
228 }
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents: 360
diff changeset
229 if (!s && s2)
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents: 360
diff changeset
230 s = s2;
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents: 360
diff changeset
231
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents: 360
diff changeset
232 return s;
342
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
233 }
390
027d7fbcdcfc Basic symbol table output; needs work for non-constant symbols
lost@l-w.ca
parents: 389
diff changeset
234
405
7bcc50e828ff Fixed up symbol table list to be more clear
lost@l-w.ca
parents: 390
diff changeset
235 struct listinfo
7bcc50e828ff Fixed up symbol table list to be more clear
lost@l-w.ca
parents: 390
diff changeset
236 {
7bcc50e828ff Fixed up symbol table list to be more clear
lost@l-w.ca
parents: 390
diff changeset
237 sectiontab_t *sect;
7bcc50e828ff Fixed up symbol table list to be more clear
lost@l-w.ca
parents: 390
diff changeset
238 asmstate_t *as;
7bcc50e828ff Fixed up symbol table list to be more clear
lost@l-w.ca
parents: 390
diff changeset
239 int complex;
7bcc50e828ff Fixed up symbol table list to be more clear
lost@l-w.ca
parents: 390
diff changeset
240 };
7bcc50e828ff Fixed up symbol table list to be more clear
lost@l-w.ca
parents: 390
diff changeset
241
7bcc50e828ff Fixed up symbol table list to be more clear
lost@l-w.ca
parents: 390
diff changeset
242 int list_symbols_test(lw_expr_t e, void *p)
7bcc50e828ff Fixed up symbol table list to be more clear
lost@l-w.ca
parents: 390
diff changeset
243 {
7bcc50e828ff Fixed up symbol table list to be more clear
lost@l-w.ca
parents: 390
diff changeset
244 struct listinfo *li = p;
7bcc50e828ff Fixed up symbol table list to be more clear
lost@l-w.ca
parents: 390
diff changeset
245
7bcc50e828ff Fixed up symbol table list to be more clear
lost@l-w.ca
parents: 390
diff changeset
246 if (li -> complex)
7bcc50e828ff Fixed up symbol table list to be more clear
lost@l-w.ca
parents: 390
diff changeset
247 return 0;
7bcc50e828ff Fixed up symbol table list to be more clear
lost@l-w.ca
parents: 390
diff changeset
248
7bcc50e828ff Fixed up symbol table list to be more clear
lost@l-w.ca
parents: 390
diff changeset
249 if (lw_expr_istype(e, lw_expr_type_special))
7bcc50e828ff Fixed up symbol table list to be more clear
lost@l-w.ca
parents: 390
diff changeset
250 {
7bcc50e828ff Fixed up symbol table list to be more clear
lost@l-w.ca
parents: 390
diff changeset
251 if (lw_expr_specint(e) == lwasm_expr_secbase)
7bcc50e828ff Fixed up symbol table list to be more clear
lost@l-w.ca
parents: 390
diff changeset
252 {
7bcc50e828ff Fixed up symbol table list to be more clear
lost@l-w.ca
parents: 390
diff changeset
253 if (li -> sect)
7bcc50e828ff Fixed up symbol table list to be more clear
lost@l-w.ca
parents: 390
diff changeset
254 {
7bcc50e828ff Fixed up symbol table list to be more clear
lost@l-w.ca
parents: 390
diff changeset
255 li -> complex = 1;
7bcc50e828ff Fixed up symbol table list to be more clear
lost@l-w.ca
parents: 390
diff changeset
256 }
7bcc50e828ff Fixed up symbol table list to be more clear
lost@l-w.ca
parents: 390
diff changeset
257 else
7bcc50e828ff Fixed up symbol table list to be more clear
lost@l-w.ca
parents: 390
diff changeset
258 {
7bcc50e828ff Fixed up symbol table list to be more clear
lost@l-w.ca
parents: 390
diff changeset
259 li -> sect = lw_expr_specptr(e);
7bcc50e828ff Fixed up symbol table list to be more clear
lost@l-w.ca
parents: 390
diff changeset
260 }
7bcc50e828ff Fixed up symbol table list to be more clear
lost@l-w.ca
parents: 390
diff changeset
261 }
7bcc50e828ff Fixed up symbol table list to be more clear
lost@l-w.ca
parents: 390
diff changeset
262 }
7bcc50e828ff Fixed up symbol table list to be more clear
lost@l-w.ca
parents: 390
diff changeset
263 return 0;
7bcc50e828ff Fixed up symbol table list to be more clear
lost@l-w.ca
parents: 390
diff changeset
264 }
7bcc50e828ff Fixed up symbol table list to be more clear
lost@l-w.ca
parents: 390
diff changeset
265
390
027d7fbcdcfc Basic symbol table output; needs work for non-constant symbols
lost@l-w.ca
parents: 389
diff changeset
266 void list_symbols(asmstate_t *as, FILE *of)
027d7fbcdcfc Basic symbol table output; needs work for non-constant symbols
lost@l-w.ca
parents: 389
diff changeset
267 {
027d7fbcdcfc Basic symbol table output; needs work for non-constant symbols
lost@l-w.ca
parents: 389
diff changeset
268 struct symtabe *s;
405
7bcc50e828ff Fixed up symbol table list to be more clear
lost@l-w.ca
parents: 390
diff changeset
269 lw_expr_t te;
7bcc50e828ff Fixed up symbol table list to be more clear
lost@l-w.ca
parents: 390
diff changeset
270 struct listinfo li;
7bcc50e828ff Fixed up symbol table list to be more clear
lost@l-w.ca
parents: 390
diff changeset
271
7bcc50e828ff Fixed up symbol table list to be more clear
lost@l-w.ca
parents: 390
diff changeset
272 li.as = as;
7bcc50e828ff Fixed up symbol table list to be more clear
lost@l-w.ca
parents: 390
diff changeset
273
390
027d7fbcdcfc Basic symbol table output; needs work for non-constant symbols
lost@l-w.ca
parents: 389
diff changeset
274 fprintf(of, "\nSymbol Table:\n");
027d7fbcdcfc Basic symbol table output; needs work for non-constant symbols
lost@l-w.ca
parents: 389
diff changeset
275
027d7fbcdcfc Basic symbol table output; needs work for non-constant symbols
lost@l-w.ca
parents: 389
diff changeset
276 for (s = as -> symtab.head; s; s = s -> next)
027d7fbcdcfc Basic symbol table output; needs work for non-constant symbols
lost@l-w.ca
parents: 389
diff changeset
277 {
405
7bcc50e828ff Fixed up symbol table list to be more clear
lost@l-w.ca
parents: 390
diff changeset
278 lwasm_reduce_expr(as, s -> value);
390
027d7fbcdcfc Basic symbol table output; needs work for non-constant symbols
lost@l-w.ca
parents: 389
diff changeset
279 fputc('[', of);
027d7fbcdcfc Basic symbol table output; needs work for non-constant symbols
lost@l-w.ca
parents: 389
diff changeset
280 if (s -> flags & symbol_flag_set)
027d7fbcdcfc Basic symbol table output; needs work for non-constant symbols
lost@l-w.ca
parents: 389
diff changeset
281 fputc('S', of);
027d7fbcdcfc Basic symbol table output; needs work for non-constant symbols
lost@l-w.ca
parents: 389
diff changeset
282 else
027d7fbcdcfc Basic symbol table output; needs work for non-constant symbols
lost@l-w.ca
parents: 389
diff changeset
283 fputc(' ', of);
405
7bcc50e828ff Fixed up symbol table list to be more clear
lost@l-w.ca
parents: 390
diff changeset
284 if (as -> output_format == OUTPUT_OBJ)
7bcc50e828ff Fixed up symbol table list to be more clear
lost@l-w.ca
parents: 390
diff changeset
285 {
7bcc50e828ff Fixed up symbol table list to be more clear
lost@l-w.ca
parents: 390
diff changeset
286 if (lw_expr_istype(s -> value, lw_expr_type_int))
7bcc50e828ff Fixed up symbol table list to be more clear
lost@l-w.ca
parents: 390
diff changeset
287 fputc('c', of);
7bcc50e828ff Fixed up symbol table list to be more clear
lost@l-w.ca
parents: 390
diff changeset
288 else
7bcc50e828ff Fixed up symbol table list to be more clear
lost@l-w.ca
parents: 390
diff changeset
289 fputc('s', of);
7bcc50e828ff Fixed up symbol table list to be more clear
lost@l-w.ca
parents: 390
diff changeset
290 }
7bcc50e828ff Fixed up symbol table list to be more clear
lost@l-w.ca
parents: 390
diff changeset
291 if (s -> context < 0)
390
027d7fbcdcfc Basic symbol table output; needs work for non-constant symbols
lost@l-w.ca
parents: 389
diff changeset
292 fputc('G', of);
027d7fbcdcfc Basic symbol table output; needs work for non-constant symbols
lost@l-w.ca
parents: 389
diff changeset
293 else
405
7bcc50e828ff Fixed up symbol table list to be more clear
lost@l-w.ca
parents: 390
diff changeset
294 fputc('L', of);
7bcc50e828ff Fixed up symbol table list to be more clear
lost@l-w.ca
parents: 390
diff changeset
295
390
027d7fbcdcfc Basic symbol table output; needs work for non-constant symbols
lost@l-w.ca
parents: 389
diff changeset
296 fputc(']', of);
027d7fbcdcfc Basic symbol table output; needs work for non-constant symbols
lost@l-w.ca
parents: 389
diff changeset
297 fputc(' ', of);
027d7fbcdcfc Basic symbol table output; needs work for non-constant symbols
lost@l-w.ca
parents: 389
diff changeset
298 fprintf(of, "%-32s ", s -> symbol);
405
7bcc50e828ff Fixed up symbol table list to be more clear
lost@l-w.ca
parents: 390
diff changeset
299
7bcc50e828ff Fixed up symbol table list to be more clear
lost@l-w.ca
parents: 390
diff changeset
300 te = lw_expr_copy(s -> value);
7bcc50e828ff Fixed up symbol table list to be more clear
lost@l-w.ca
parents: 390
diff changeset
301 li.complex = 0;
7bcc50e828ff Fixed up symbol table list to be more clear
lost@l-w.ca
parents: 390
diff changeset
302 li.sect = NULL;
7bcc50e828ff Fixed up symbol table list to be more clear
lost@l-w.ca
parents: 390
diff changeset
303 lw_expr_testterms(te, list_symbols_test, &li);
7bcc50e828ff Fixed up symbol table list to be more clear
lost@l-w.ca
parents: 390
diff changeset
304 if (li.sect)
7bcc50e828ff Fixed up symbol table list to be more clear
lost@l-w.ca
parents: 390
diff changeset
305 {
7bcc50e828ff Fixed up symbol table list to be more clear
lost@l-w.ca
parents: 390
diff changeset
306 as -> exportcheck = 1;
7bcc50e828ff Fixed up symbol table list to be more clear
lost@l-w.ca
parents: 390
diff changeset
307 as -> csect = li.sect;
7bcc50e828ff Fixed up symbol table list to be more clear
lost@l-w.ca
parents: 390
diff changeset
308 lwasm_reduce_expr(as, te);
7bcc50e828ff Fixed up symbol table list to be more clear
lost@l-w.ca
parents: 390
diff changeset
309 as -> exportcheck = 0;
7bcc50e828ff Fixed up symbol table list to be more clear
lost@l-w.ca
parents: 390
diff changeset
310 }
7bcc50e828ff Fixed up symbol table list to be more clear
lost@l-w.ca
parents: 390
diff changeset
311
7bcc50e828ff Fixed up symbol table list to be more clear
lost@l-w.ca
parents: 390
diff changeset
312 if (lw_expr_istype(te, lw_expr_type_int))
7bcc50e828ff Fixed up symbol table list to be more clear
lost@l-w.ca
parents: 390
diff changeset
313 {
7bcc50e828ff Fixed up symbol table list to be more clear
lost@l-w.ca
parents: 390
diff changeset
314 fprintf(of, "%04X", lw_expr_intval(te));
7bcc50e828ff Fixed up symbol table list to be more clear
lost@l-w.ca
parents: 390
diff changeset
315 if (li.sect)
7bcc50e828ff Fixed up symbol table list to be more clear
lost@l-w.ca
parents: 390
diff changeset
316 {
7bcc50e828ff Fixed up symbol table list to be more clear
lost@l-w.ca
parents: 390
diff changeset
317 fprintf(of, " (%s)", li.sect -> name);
7bcc50e828ff Fixed up symbol table list to be more clear
lost@l-w.ca
parents: 390
diff changeset
318 }
7bcc50e828ff Fixed up symbol table list to be more clear
lost@l-w.ca
parents: 390
diff changeset
319 fprintf(of, "\n");
7bcc50e828ff Fixed up symbol table list to be more clear
lost@l-w.ca
parents: 390
diff changeset
320 }
390
027d7fbcdcfc Basic symbol table output; needs work for non-constant symbols
lost@l-w.ca
parents: 389
diff changeset
321 else
405
7bcc50e828ff Fixed up symbol table list to be more clear
lost@l-w.ca
parents: 390
diff changeset
322 {
7bcc50e828ff Fixed up symbol table list to be more clear
lost@l-w.ca
parents: 390
diff changeset
323 fprintf(of, "<<incomplete>>\n");
7bcc50e828ff Fixed up symbol table list to be more clear
lost@l-w.ca
parents: 390
diff changeset
324 // fprintf(of, "%s\n", lw_expr_print(s -> value));
7bcc50e828ff Fixed up symbol table list to be more clear
lost@l-w.ca
parents: 390
diff changeset
325 }
7bcc50e828ff Fixed up symbol table list to be more clear
lost@l-w.ca
parents: 390
diff changeset
326 lw_expr_destroy(te);
390
027d7fbcdcfc Basic symbol table output; needs work for non-constant symbols
lost@l-w.ca
parents: 389
diff changeset
327 }
027d7fbcdcfc Basic symbol table output; needs work for non-constant symbols
lost@l-w.ca
parents: 389
diff changeset
328 }