Mercurial > hg-old > index.cgi
comparison 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 |
comparison
equal
deleted
inserted
replaced
362:4867f18c872f | 363:d96c30e60ddf |
---|---|
63 return NULL; | 63 return NULL; |
64 } | 64 } |
65 } | 65 } |
66 | 66 |
67 if (islocal) | 67 if (islocal) |
68 context = as -> context; | 68 context = cl -> context; |
69 | 69 |
70 // first, look up symbol to see if it is already defined | 70 // first, look up symbol to see if it is already defined |
71 for (se = as -> symtab.head; se; se = se -> next) | 71 for (se = as -> symtab.head; se; se = se -> next) |
72 { | 72 { |
73 if (!strcmp(sym, se -> symbol)) | 73 if (!strcmp(sym, se -> symbol)) |
93 if (flags & symbol_flag_set) | 93 if (flags & symbol_flag_set) |
94 { | 94 { |
95 version++; | 95 version++; |
96 } | 96 } |
97 | 97 |
98 // symplify the symbol expression - replaces "SET" symbols with | |
99 // symbol table entries | |
100 lwasm_reduce_expr(as, val); | |
101 | |
98 se = lw_alloc(sizeof(struct symtabe)); | 102 se = lw_alloc(sizeof(struct symtabe)); |
99 se -> next = as -> symtab.head; | 103 se -> next = as -> symtab.head; |
100 as -> symtab.head = se; | 104 as -> symtab.head = se; |
101 se -> context = context; | 105 se -> context = context; |
102 se -> version = version; | 106 se -> version = version; |
103 se -> flags = flags; | 107 se -> flags = flags; |
104 se -> value = lw_expr_copy(val); | 108 se -> value = lw_expr_copy(val); |
105 return se; | 109 return se; |
106 } | 110 } |
107 | 111 |
108 struct symtabe * lookup_symbol(asmstate_t *as, line_t *cl, char *sym, int context, int version) | 112 // for "SET" symbols, always returns the LAST definition of the |
113 // symbol. This works because the lwasm_reduce_expr() call in | |
114 // register_symbol will ensure there are no lingering "var" references | |
115 // to the set symbol anywhere in the symbol table; they will all be | |
116 // converted to direct references | |
117 // NOTE: this means that for a forward reference to a SET symbol, | |
118 // the LAST definition will be the one used. | |
119 // This arrangement also ensures that any reference to the symbol | |
120 // itself inside a "set" definition will refer to the previous version | |
121 // of the symbol. | |
122 struct symtabe * lookup_symbol(asmstate_t *as, line_t *cl, char *sym) | |
109 { | 123 { |
110 return NULL; | 124 int local = 0; |
125 struct symtabe *s, *s2; | |
126 | |
127 // check if this is a local symbol | |
128 if (strchr(sym, '@') || strchr(sym, '?')) | |
129 local = 1; | |
130 | |
131 if (cl && !CURPRAGMA(cl, PRAGMA_DOLLARNOTLOCAL) && strchr(sym, '$')) | |
132 local = 1; | |
133 if (!cl && !(as -> pragmas & PRAGMA_DOLLARNOTLOCAL) && strchr(sym, '$')) | |
134 local = 1; | |
135 | |
136 // cannot look up local symbol in global context!!!!! | |
137 if (!cl && local) | |
138 return NULL; | |
139 | |
140 for (s = as -> symtab.head, s2 = NULL; s; s = s -> next) | |
141 { | |
142 if (!strcmp(sym, s -> symbol)) | |
143 { | |
144 if (local && s -> context != cl -> context) | |
145 continue; | |
146 | |
147 if (s -> flags & symbol_flag_set) | |
148 { | |
149 // look for highest version of symbol | |
150 if (s -> version > s2 -> version) | |
151 s2 = s; | |
152 continue; | |
153 } | |
154 break; | |
155 } | |
156 } | |
157 if (!s && s2) | |
158 s = s2; | |
159 | |
160 return s; | |
111 } | 161 } |