annotate lwasm/symbol.c @ 406:502fbc37ff4e

Symbol table is now sorted in lwasm
author lost@l-w.ca
date Fri, 23 Jul 2010 19:23:17 -0600
parents 7bcc50e828ff
children cac204676434
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 {
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
91 if (*sym < 0x80 && !strchr(SSYMCHARS, *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
92 {
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
93 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
94 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
95 }
344
0215a0fbf61b Added assembly error system and additional checks for symbol syntax
lost@starbug
parents: 342
diff changeset
96
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 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
98 {
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 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
100 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
101 }
344
0215a0fbf61b Added assembly error system and additional checks for symbol syntax
lost@starbug
parents: 342
diff changeset
102 }
0215a0fbf61b Added assembly error system and additional checks for symbol syntax
lost@starbug
parents: 342
diff changeset
103
0215a0fbf61b Added assembly error system and additional checks for symbol syntax
lost@starbug
parents: 342
diff changeset
104 for (cp = sym; *cp; cp++)
0215a0fbf61b Added assembly error system and additional checks for symbol syntax
lost@starbug
parents: 342
diff changeset
105 {
0215a0fbf61b Added assembly error system and additional checks for symbol syntax
lost@starbug
parents: 342
diff changeset
106 if (*cp == '@' || *cp == '?')
0215a0fbf61b Added assembly error system and additional checks for symbol syntax
lost@starbug
parents: 342
diff changeset
107 islocal = 1;
360
7d91ab7ac7d6 Indexed stage 2; set line structure to track pragmas in effect for that line
lost@starbug
parents: 346
diff changeset
108 if (*cp == '$' && !(CURPRAGMA(cl, PRAGMA_DOLLARNOTLOCAL)))
344
0215a0fbf61b Added assembly error system and additional checks for symbol syntax
lost@starbug
parents: 342
diff changeset
109 islocal = 1;
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 // 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
112 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
113 {
0215a0fbf61b Added assembly error system and additional checks for symbol syntax
lost@starbug
parents: 342
diff changeset
114 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
115 return NULL;
0215a0fbf61b Added assembly error system and additional checks for symbol syntax
lost@starbug
parents: 342
diff changeset
116 }
0215a0fbf61b Added assembly error system and additional checks for symbol syntax
lost@starbug
parents: 342
diff changeset
117 }
342
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
118
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
119 if (islocal)
363
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents: 360
diff changeset
120 context = cl -> context;
342
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
121
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
122 // first, look up symbol to see if it is already defined
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
123 for (se = as -> symtab.head; se; se = se -> next)
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
124 {
406
502fbc37ff4e Symbol table is now sorted in lwasm
lost@l-w.ca
parents: 405
diff changeset
125 debug_message(as, 300, "Symbol add lookup: %p, %p", se, se -> next);
342
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
126 if (!strcmp(sym, se -> symbol))
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
127 {
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
128 if (se -> context != context)
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
129 continue;
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
130 if ((flags & symbol_flag_set) && (se -> flags & symbol_flag_set))
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
131 {
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
132 if (version < se -> version)
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
133 version = se -> version;
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
134 continue;
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
135 }
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
136 break;
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
137 }
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
138 }
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
139
342
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
140 if (se)
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
141 {
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
142 // multiply defined symbol
344
0215a0fbf61b Added assembly error system and additional checks for symbol syntax
lost@starbug
parents: 342
diff changeset
143 lwasm_register_error(as, cl, "Multiply defined symbol (%s)", sym);
342
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
144 return NULL;
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
145 }
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
146
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
147 if (flags & symbol_flag_set)
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
148 {
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
149 version++;
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
150 }
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
151
363
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents: 360
diff changeset
152 // symplify the symbol expression - replaces "SET" symbols with
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents: 360
diff changeset
153 // symbol table entries
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents: 360
diff changeset
154 lwasm_reduce_expr(as, val);
406
502fbc37ff4e Symbol table is now sorted in lwasm
lost@l-w.ca
parents: 405
diff changeset
155
342
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
156 se = lw_alloc(sizeof(struct symtabe));
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
157 se -> context = context;
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
158 se -> version = version;
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
159 se -> flags = flags;
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
160 se -> value = lw_expr_copy(val);
370
6b33faa21a0a Debugging output and bugfixing pass 0
lost@starbug
parents: 363
diff changeset
161 se -> symbol = lw_strdup(sym);
374
d99322ef6f21 Stage 1: actually do output
lost@starbug
parents: 370
diff changeset
162 se -> section = cl -> csect;
406
502fbc37ff4e Symbol table is now sorted in lwasm
lost@l-w.ca
parents: 405
diff changeset
163 sprev = symbol_findprev(as, se);
502fbc37ff4e Symbol table is now sorted in lwasm
lost@l-w.ca
parents: 405
diff changeset
164 if (!sprev)
502fbc37ff4e Symbol table is now sorted in lwasm
lost@l-w.ca
parents: 405
diff changeset
165 {
502fbc37ff4e Symbol table is now sorted in lwasm
lost@l-w.ca
parents: 405
diff changeset
166 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
167 se -> next = as -> symtab.head;
502fbc37ff4e Symbol table is now sorted in lwasm
lost@l-w.ca
parents: 405
diff changeset
168 as -> symtab.head = se;
502fbc37ff4e Symbol table is now sorted in lwasm
lost@l-w.ca
parents: 405
diff changeset
169 }
502fbc37ff4e Symbol table is now sorted in lwasm
lost@l-w.ca
parents: 405
diff changeset
170 else
502fbc37ff4e Symbol table is now sorted in lwasm
lost@l-w.ca
parents: 405
diff changeset
171 {
502fbc37ff4e Symbol table is now sorted in lwasm
lost@l-w.ca
parents: 405
diff changeset
172 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
173 se -> next = sprev -> next;
502fbc37ff4e Symbol table is now sorted in lwasm
lost@l-w.ca
parents: 405
diff changeset
174 sprev -> next = se;
502fbc37ff4e Symbol table is now sorted in lwasm
lost@l-w.ca
parents: 405
diff changeset
175 }
342
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
176 return se;
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
177 }
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
178
363
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents: 360
diff changeset
179 // 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
180 // 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
181 // 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
182 // 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
183 // converted to direct references
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents: 360
diff changeset
184 // 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
185 // the LAST definition will be the one used.
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents: 360
diff changeset
186 // 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
187 // 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
188 // of the symbol.
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents: 360
diff changeset
189 struct symtabe * lookup_symbol(asmstate_t *as, line_t *cl, char *sym)
342
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
190 {
363
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents: 360
diff changeset
191 int local = 0;
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents: 360
diff changeset
192 struct symtabe *s, *s2;
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents: 360
diff changeset
193
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents: 360
diff changeset
194 // check if this is a local symbol
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents: 360
diff changeset
195 if (strchr(sym, '@') || strchr(sym, '?'))
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents: 360
diff changeset
196 local = 1;
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents: 360
diff changeset
197
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents: 360
diff changeset
198 if (cl && !CURPRAGMA(cl, PRAGMA_DOLLARNOTLOCAL) && strchr(sym, '$'))
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents: 360
diff changeset
199 local = 1;
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents: 360
diff changeset
200 if (!cl && !(as -> pragmas & PRAGMA_DOLLARNOTLOCAL) && 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 // cannot look up local symbol in global context!!!!!
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents: 360
diff changeset
204 if (!cl && local)
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents: 360
diff changeset
205 return NULL;
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents: 360
diff changeset
206
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents: 360
diff changeset
207 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
208 {
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents: 360
diff changeset
209 if (!strcmp(sym, s -> symbol))
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents: 360
diff changeset
210 {
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents: 360
diff changeset
211 if (local && s -> context != cl -> context)
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents: 360
diff changeset
212 continue;
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 (s -> flags & symbol_flag_set)
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 // look for highest version of symbol
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents: 360
diff changeset
217 if (s -> version > s2 -> version)
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents: 360
diff changeset
218 s2 = s;
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents: 360
diff changeset
219 continue;
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 break;
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents: 360
diff changeset
222 }
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents: 360
diff changeset
223 }
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents: 360
diff changeset
224 if (!s && s2)
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents: 360
diff changeset
225 s = s2;
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents: 360
diff changeset
226
d96c30e60ddf Added pass2 and various supporting logic including symbol lookups
lost@starbug
parents: 360
diff changeset
227 return s;
342
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
228 }
390
027d7fbcdcfc Basic symbol table output; needs work for non-constant symbols
lost@l-w.ca
parents: 389
diff changeset
229
405
7bcc50e828ff Fixed up symbol table list to be more clear
lost@l-w.ca
parents: 390
diff changeset
230 struct listinfo
7bcc50e828ff Fixed up symbol table list to be more clear
lost@l-w.ca
parents: 390
diff changeset
231 {
7bcc50e828ff Fixed up symbol table list to be more clear
lost@l-w.ca
parents: 390
diff changeset
232 sectiontab_t *sect;
7bcc50e828ff Fixed up symbol table list to be more clear
lost@l-w.ca
parents: 390
diff changeset
233 asmstate_t *as;
7bcc50e828ff Fixed up symbol table list to be more clear
lost@l-w.ca
parents: 390
diff changeset
234 int complex;
7bcc50e828ff Fixed up symbol table list to be more clear
lost@l-w.ca
parents: 390
diff changeset
235 };
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 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
238 {
7bcc50e828ff Fixed up symbol table list to be more clear
lost@l-w.ca
parents: 390
diff changeset
239 struct listinfo *li = p;
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 if (li -> complex)
7bcc50e828ff Fixed up symbol table list to be more clear
lost@l-w.ca
parents: 390
diff changeset
242 return 0;
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 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
245 {
7bcc50e828ff Fixed up symbol table list to be more clear
lost@l-w.ca
parents: 390
diff changeset
246 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
247 {
7bcc50e828ff Fixed up symbol table list to be more clear
lost@l-w.ca
parents: 390
diff changeset
248 if (li -> sect)
7bcc50e828ff Fixed up symbol table list to be more clear
lost@l-w.ca
parents: 390
diff changeset
249 {
7bcc50e828ff Fixed up symbol table list to be more clear
lost@l-w.ca
parents: 390
diff changeset
250 li -> complex = 1;
7bcc50e828ff Fixed up symbol table list to be more clear
lost@l-w.ca
parents: 390
diff changeset
251 }
7bcc50e828ff Fixed up symbol table list to be more clear
lost@l-w.ca
parents: 390
diff changeset
252 else
7bcc50e828ff Fixed up symbol table list to be more clear
lost@l-w.ca
parents: 390
diff changeset
253 {
7bcc50e828ff Fixed up symbol table list to be more clear
lost@l-w.ca
parents: 390
diff changeset
254 li -> sect = lw_expr_specptr(e);
7bcc50e828ff Fixed up symbol table list to be more clear
lost@l-w.ca
parents: 390
diff changeset
255 }
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 }
7bcc50e828ff Fixed up symbol table list to be more clear
lost@l-w.ca
parents: 390
diff changeset
258 return 0;
7bcc50e828ff Fixed up symbol table list to be more clear
lost@l-w.ca
parents: 390
diff changeset
259 }
7bcc50e828ff Fixed up symbol table list to be more clear
lost@l-w.ca
parents: 390
diff changeset
260
390
027d7fbcdcfc Basic symbol table output; needs work for non-constant symbols
lost@l-w.ca
parents: 389
diff changeset
261 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
262 {
027d7fbcdcfc Basic symbol table output; needs work for non-constant symbols
lost@l-w.ca
parents: 389
diff changeset
263 struct symtabe *s;
405
7bcc50e828ff Fixed up symbol table list to be more clear
lost@l-w.ca
parents: 390
diff changeset
264 lw_expr_t te;
7bcc50e828ff Fixed up symbol table list to be more clear
lost@l-w.ca
parents: 390
diff changeset
265 struct listinfo li;
7bcc50e828ff Fixed up symbol table list to be more clear
lost@l-w.ca
parents: 390
diff changeset
266
7bcc50e828ff Fixed up symbol table list to be more clear
lost@l-w.ca
parents: 390
diff changeset
267 li.as = as;
7bcc50e828ff Fixed up symbol table list to be more clear
lost@l-w.ca
parents: 390
diff changeset
268
390
027d7fbcdcfc Basic symbol table output; needs work for non-constant symbols
lost@l-w.ca
parents: 389
diff changeset
269 fprintf(of, "\nSymbol Table:\n");
027d7fbcdcfc Basic symbol table output; needs work for non-constant symbols
lost@l-w.ca
parents: 389
diff changeset
270
027d7fbcdcfc Basic symbol table output; needs work for non-constant symbols
lost@l-w.ca
parents: 389
diff changeset
271 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
272 {
405
7bcc50e828ff Fixed up symbol table list to be more clear
lost@l-w.ca
parents: 390
diff changeset
273 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
274 fputc('[', of);
027d7fbcdcfc Basic symbol table output; needs work for non-constant symbols
lost@l-w.ca
parents: 389
diff changeset
275 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
276 fputc('S', of);
027d7fbcdcfc Basic symbol table output; needs work for non-constant symbols
lost@l-w.ca
parents: 389
diff changeset
277 else
027d7fbcdcfc Basic symbol table output; needs work for non-constant symbols
lost@l-w.ca
parents: 389
diff changeset
278 fputc(' ', of);
405
7bcc50e828ff Fixed up symbol table list to be more clear
lost@l-w.ca
parents: 390
diff changeset
279 if (as -> output_format == OUTPUT_OBJ)
7bcc50e828ff Fixed up symbol table list to be more clear
lost@l-w.ca
parents: 390
diff changeset
280 {
7bcc50e828ff Fixed up symbol table list to be more clear
lost@l-w.ca
parents: 390
diff changeset
281 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
282 fputc('c', of);
7bcc50e828ff Fixed up symbol table list to be more clear
lost@l-w.ca
parents: 390
diff changeset
283 else
7bcc50e828ff Fixed up symbol table list to be more clear
lost@l-w.ca
parents: 390
diff changeset
284 fputc('s', of);
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 (s -> context < 0)
390
027d7fbcdcfc Basic symbol table output; needs work for non-constant symbols
lost@l-w.ca
parents: 389
diff changeset
287 fputc('G', of);
027d7fbcdcfc Basic symbol table output; needs work for non-constant symbols
lost@l-w.ca
parents: 389
diff changeset
288 else
405
7bcc50e828ff Fixed up symbol table list to be more clear
lost@l-w.ca
parents: 390
diff changeset
289 fputc('L', of);
7bcc50e828ff Fixed up symbol table list to be more clear
lost@l-w.ca
parents: 390
diff changeset
290
390
027d7fbcdcfc Basic symbol table output; needs work for non-constant symbols
lost@l-w.ca
parents: 389
diff changeset
291 fputc(']', of);
027d7fbcdcfc Basic symbol table output; needs work for non-constant symbols
lost@l-w.ca
parents: 389
diff changeset
292 fputc(' ', of);
027d7fbcdcfc Basic symbol table output; needs work for non-constant symbols
lost@l-w.ca
parents: 389
diff changeset
293 fprintf(of, "%-32s ", s -> symbol);
405
7bcc50e828ff Fixed up symbol table list to be more clear
lost@l-w.ca
parents: 390
diff changeset
294
7bcc50e828ff Fixed up symbol table list to be more clear
lost@l-w.ca
parents: 390
diff changeset
295 te = lw_expr_copy(s -> value);
7bcc50e828ff Fixed up symbol table list to be more clear
lost@l-w.ca
parents: 390
diff changeset
296 li.complex = 0;
7bcc50e828ff Fixed up symbol table list to be more clear
lost@l-w.ca
parents: 390
diff changeset
297 li.sect = NULL;
7bcc50e828ff Fixed up symbol table list to be more clear
lost@l-w.ca
parents: 390
diff changeset
298 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
299 if (li.sect)
7bcc50e828ff Fixed up symbol table list to be more clear
lost@l-w.ca
parents: 390
diff changeset
300 {
7bcc50e828ff Fixed up symbol table list to be more clear
lost@l-w.ca
parents: 390
diff changeset
301 as -> exportcheck = 1;
7bcc50e828ff Fixed up symbol table list to be more clear
lost@l-w.ca
parents: 390
diff changeset
302 as -> csect = li.sect;
7bcc50e828ff Fixed up symbol table list to be more clear
lost@l-w.ca
parents: 390
diff changeset
303 lwasm_reduce_expr(as, te);
7bcc50e828ff Fixed up symbol table list to be more clear
lost@l-w.ca
parents: 390
diff changeset
304 as -> exportcheck = 0;
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
7bcc50e828ff Fixed up symbol table list to be more clear
lost@l-w.ca
parents: 390
diff changeset
307 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
308 {
7bcc50e828ff Fixed up symbol table list to be more clear
lost@l-w.ca
parents: 390
diff changeset
309 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
310 if (li.sect)
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 fprintf(of, " (%s)", li.sect -> name);
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, "\n");
7bcc50e828ff Fixed up symbol table list to be more clear
lost@l-w.ca
parents: 390
diff changeset
315 }
390
027d7fbcdcfc Basic symbol table output; needs work for non-constant symbols
lost@l-w.ca
parents: 389
diff changeset
316 else
405
7bcc50e828ff Fixed up symbol table list to be more clear
lost@l-w.ca
parents: 390
diff changeset
317 {
7bcc50e828ff Fixed up symbol table list to be more clear
lost@l-w.ca
parents: 390
diff changeset
318 fprintf(of, "<<incomplete>>\n");
7bcc50e828ff Fixed up symbol table list to be more clear
lost@l-w.ca
parents: 390
diff changeset
319 // 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
320 }
7bcc50e828ff Fixed up symbol table list to be more clear
lost@l-w.ca
parents: 390
diff changeset
321 lw_expr_destroy(te);
390
027d7fbcdcfc Basic symbol table output; needs work for non-constant symbols
lost@l-w.ca
parents: 389
diff changeset
322 }
027d7fbcdcfc Basic symbol table output; needs work for non-constant symbols
lost@l-w.ca
parents: 389
diff changeset
323 }