Mercurial > hg-old > index.cgi
annotate lwasm/lwasm.c @ 236:a58f49a77441
Added os9 target, pragma to control whether $ localizes a symbol, and fixed some condition nesting bugs
author | lost |
---|---|
date | Fri, 14 Aug 2009 03:22:26 +0000 |
parents | bae1e3ecdce1 |
children | a9a14e6b4bc8 |
rev | line source |
---|---|
0 | 1 /* |
4
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
2 lwasm.c |
55 | 3 Copyright © 2009 William Astle |
4
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
4 |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
5 This file is part of LWASM. |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
6 |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
7 LWASM is free software: you can redistribute it and/or modify it under the |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
8 terms of the GNU General Public License as published by the Free Software |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
9 Foundation, either version 3 of the License, or (at your option) any later |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
10 version. |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
11 |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
12 This program is distributed in the hope that it will be useful, but WITHOUT |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
15 more details. |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
16 |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
17 You should have received a copy of the GNU General Public License along with |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
18 this program. If not, see <http://www.gnu.org/licenses/>. |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
19 |
26 | 20 |
21 Contains random functions used by the assembler | |
4
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
22 */ |
0 | 23 |
26 | 24 #define __lwasm_c_seen__ |
212 | 25 #include <config.h> |
26 | 26 |
27 #include <stdarg.h> | |
0 | 28 #include <stdlib.h> |
26 | 29 #include <stdio.h> |
30 | |
0 | 31 #include "lwasm.h" |
26 | 32 #include "util.h" |
37
538e15927776
Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents:
32
diff
changeset
|
33 #include "expr.h" |
0 | 34 |
38 | 35 int debug_level = 0; |
36 | |
26 | 37 int register_error(asmstate_t *as, lwasm_line_t *l, int pass, const char *fmt, ...) |
0 | 38 { |
26 | 39 lwasm_error_t *e; |
40 va_list args; | |
41 char errbuff[1024]; | |
42 int r; | |
0 | 43 |
143
0ee5f65bccf9
Added pragma to allow all undefined symbols to be considered external and also added a --pragma command line option
lost
parents:
101
diff
changeset
|
44 if (!l) |
0ee5f65bccf9
Added pragma to allow all undefined symbols to be considered external and also added a --pragma command line option
lost
parents:
101
diff
changeset
|
45 return; |
0ee5f65bccf9
Added pragma to allow all undefined symbols to be considered external and also added a --pragma command line option
lost
parents:
101
diff
changeset
|
46 |
26 | 47 if (as -> passnum != pass) |
48 return; | |
49 | |
50 va_start(args, fmt); | |
0 | 51 |
26 | 52 e = lwasm_alloc(sizeof(lwasm_error_t)); |
53 | |
54 e -> next = l -> err; | |
55 l -> err = e; | |
0 | 56 |
57 as -> errorcount++; | |
58 | |
26 | 59 r = vsnprintf(errbuff, 1024, fmt, args); |
60 e -> mess = lwasm_strdup(errbuff); | |
0 | 61 |
26 | 62 va_end(args); |
0 | 63 |
26 | 64 return r; |
0 | 65 } |
27
f736579569b4
Added handlers for inherent and register to register instructions
lost
parents:
26
diff
changeset
|
66 |
f736579569b4
Added handlers for inherent and register to register instructions
lost
parents:
26
diff
changeset
|
67 void lwasm_emit(asmstate_t *as, lwasm_line_t *l, int b) |
f736579569b4
Added handlers for inherent and register to register instructions
lost
parents:
26
diff
changeset
|
68 { |
f736579569b4
Added handlers for inherent and register to register instructions
lost
parents:
26
diff
changeset
|
69 as -> addr += 1; |
58 | 70 as -> addr &= 0xffff; |
71 | |
74 | 72 if (as -> outformat == OUTPUT_OBJ && !(as -> csect)) |
73 { | |
74 register_error(as, l, 1, "Output not allowed outside sections with obj target"); | |
75 return; | |
76 } | |
77 if (as -> outformat == OUTPUT_OBJ && as -> csect -> flags & SECTION_BSS) | |
78 { | |
79 register_error(as, l, 1, "Output not allowed inside BSS sections"); | |
80 return; | |
81 } | |
236
a58f49a77441
Added os9 target, pragma to control whether $ localizes a symbol, and fixed some condition nesting bugs
lost
parents:
212
diff
changeset
|
82 |
a58f49a77441
Added os9 target, pragma to control whether $ localizes a symbol, and fixed some condition nesting bugs
lost
parents:
212
diff
changeset
|
83 if (as -> outformat == OUTPUT_OS9 && as -> inmod == 0) |
a58f49a77441
Added os9 target, pragma to control whether $ localizes a symbol, and fixed some condition nesting bugs
lost
parents:
212
diff
changeset
|
84 { |
a58f49a77441
Added os9 target, pragma to control whether $ localizes a symbol, and fixed some condition nesting bugs
lost
parents:
212
diff
changeset
|
85 register_error(as, l, 1, "Output not allowed outside of module in OS9 mode"); |
a58f49a77441
Added os9 target, pragma to control whether $ localizes a symbol, and fixed some condition nesting bugs
lost
parents:
212
diff
changeset
|
86 return; |
a58f49a77441
Added os9 target, pragma to control whether $ localizes a symbol, and fixed some condition nesting bugs
lost
parents:
212
diff
changeset
|
87 } |
a58f49a77441
Added os9 target, pragma to control whether $ localizes a symbol, and fixed some condition nesting bugs
lost
parents:
212
diff
changeset
|
88 |
27
f736579569b4
Added handlers for inherent and register to register instructions
lost
parents:
26
diff
changeset
|
89 if (as -> passnum == 1) |
f736579569b4
Added handlers for inherent and register to register instructions
lost
parents:
26
diff
changeset
|
90 return; |
f736579569b4
Added handlers for inherent and register to register instructions
lost
parents:
26
diff
changeset
|
91 |
42 | 92 |
93 if (l -> codelen >= l -> codesize) | |
94 { | |
95 l -> bytes = realloc(l -> bytes, l -> codesize + 16); | |
96 l -> codesize += 16; | |
97 } | |
98 l -> bytes[l -> codelen] = b & 0xff; | |
99 l -> codelen += 1; | |
236
a58f49a77441
Added os9 target, pragma to control whether $ localizes a symbol, and fixed some condition nesting bugs
lost
parents:
212
diff
changeset
|
100 |
a58f49a77441
Added os9 target, pragma to control whether $ localizes a symbol, and fixed some condition nesting bugs
lost
parents:
212
diff
changeset
|
101 if (as -> outformat == OUTPUT_OS9 && as -> inmod) |
a58f49a77441
Added os9 target, pragma to control whether $ localizes a symbol, and fixed some condition nesting bugs
lost
parents:
212
diff
changeset
|
102 { |
a58f49a77441
Added os9 target, pragma to control whether $ localizes a symbol, and fixed some condition nesting bugs
lost
parents:
212
diff
changeset
|
103 // calc the CRC |
a58f49a77441
Added os9 target, pragma to control whether $ localizes a symbol, and fixed some condition nesting bugs
lost
parents:
212
diff
changeset
|
104 b &= 0xff; |
a58f49a77441
Added os9 target, pragma to control whether $ localizes a symbol, and fixed some condition nesting bugs
lost
parents:
212
diff
changeset
|
105 |
a58f49a77441
Added os9 target, pragma to control whether $ localizes a symbol, and fixed some condition nesting bugs
lost
parents:
212
diff
changeset
|
106 b ^= (as -> crc) >> 16; |
a58f49a77441
Added os9 target, pragma to control whether $ localizes a symbol, and fixed some condition nesting bugs
lost
parents:
212
diff
changeset
|
107 as -> crc <<= 8; |
a58f49a77441
Added os9 target, pragma to control whether $ localizes a symbol, and fixed some condition nesting bugs
lost
parents:
212
diff
changeset
|
108 as -> crc ^= b << 1; |
a58f49a77441
Added os9 target, pragma to control whether $ localizes a symbol, and fixed some condition nesting bugs
lost
parents:
212
diff
changeset
|
109 as -> crc ^= b << 6; |
a58f49a77441
Added os9 target, pragma to control whether $ localizes a symbol, and fixed some condition nesting bugs
lost
parents:
212
diff
changeset
|
110 b ^= b << 1; |
a58f49a77441
Added os9 target, pragma to control whether $ localizes a symbol, and fixed some condition nesting bugs
lost
parents:
212
diff
changeset
|
111 b ^= b << 2; |
a58f49a77441
Added os9 target, pragma to control whether $ localizes a symbol, and fixed some condition nesting bugs
lost
parents:
212
diff
changeset
|
112 b ^= b << 4; |
a58f49a77441
Added os9 target, pragma to control whether $ localizes a symbol, and fixed some condition nesting bugs
lost
parents:
212
diff
changeset
|
113 if (b & 0x80) |
a58f49a77441
Added os9 target, pragma to control whether $ localizes a symbol, and fixed some condition nesting bugs
lost
parents:
212
diff
changeset
|
114 as -> crc ^= 0x800021; |
a58f49a77441
Added os9 target, pragma to control whether $ localizes a symbol, and fixed some condition nesting bugs
lost
parents:
212
diff
changeset
|
115 as -> crc &= 0xffffff; |
a58f49a77441
Added os9 target, pragma to control whether $ localizes a symbol, and fixed some condition nesting bugs
lost
parents:
212
diff
changeset
|
116 } |
27
f736579569b4
Added handlers for inherent and register to register instructions
lost
parents:
26
diff
changeset
|
117 } |
f736579569b4
Added handlers for inherent and register to register instructions
lost
parents:
26
diff
changeset
|
118 |
f736579569b4
Added handlers for inherent and register to register instructions
lost
parents:
26
diff
changeset
|
119 void lwasm_emitop(asmstate_t *as, lwasm_line_t *l, int o) |
f736579569b4
Added handlers for inherent and register to register instructions
lost
parents:
26
diff
changeset
|
120 { |
f736579569b4
Added handlers for inherent and register to register instructions
lost
parents:
26
diff
changeset
|
121 if (o >= 0x100) |
f736579569b4
Added handlers for inherent and register to register instructions
lost
parents:
26
diff
changeset
|
122 lwasm_emit(as, l, o >> 8); |
f736579569b4
Added handlers for inherent and register to register instructions
lost
parents:
26
diff
changeset
|
123 lwasm_emit(as, l, o & 0xff); |
f736579569b4
Added handlers for inherent and register to register instructions
lost
parents:
26
diff
changeset
|
124 } |
f736579569b4
Added handlers for inherent and register to register instructions
lost
parents:
26
diff
changeset
|
125 |
f736579569b4
Added handlers for inherent and register to register instructions
lost
parents:
26
diff
changeset
|
126 int lwasm_lookupreg2(const char *reglist, char **str) |
f736579569b4
Added handlers for inherent and register to register instructions
lost
parents:
26
diff
changeset
|
127 { |
f736579569b4
Added handlers for inherent and register to register instructions
lost
parents:
26
diff
changeset
|
128 int rval = 0; |
f736579569b4
Added handlers for inherent and register to register instructions
lost
parents:
26
diff
changeset
|
129 |
f736579569b4
Added handlers for inherent and register to register instructions
lost
parents:
26
diff
changeset
|
130 while (*reglist) |
f736579569b4
Added handlers for inherent and register to register instructions
lost
parents:
26
diff
changeset
|
131 { |
f736579569b4
Added handlers for inherent and register to register instructions
lost
parents:
26
diff
changeset
|
132 if (toupper(**str) == *reglist) |
f736579569b4
Added handlers for inherent and register to register instructions
lost
parents:
26
diff
changeset
|
133 { |
f736579569b4
Added handlers for inherent and register to register instructions
lost
parents:
26
diff
changeset
|
134 // first char matches |
f736579569b4
Added handlers for inherent and register to register instructions
lost
parents:
26
diff
changeset
|
135 if (reglist[1] == ' ' && !isalpha(*(*str + 1))) |
f736579569b4
Added handlers for inherent and register to register instructions
lost
parents:
26
diff
changeset
|
136 break; |
f736579569b4
Added handlers for inherent and register to register instructions
lost
parents:
26
diff
changeset
|
137 if (toupper(*(*str + 1)) == reglist[1]) |
f736579569b4
Added handlers for inherent and register to register instructions
lost
parents:
26
diff
changeset
|
138 break; |
f736579569b4
Added handlers for inherent and register to register instructions
lost
parents:
26
diff
changeset
|
139 } |
f736579569b4
Added handlers for inherent and register to register instructions
lost
parents:
26
diff
changeset
|
140 reglist += 2; |
f736579569b4
Added handlers for inherent and register to register instructions
lost
parents:
26
diff
changeset
|
141 rval++; |
f736579569b4
Added handlers for inherent and register to register instructions
lost
parents:
26
diff
changeset
|
142 } |
f736579569b4
Added handlers for inherent and register to register instructions
lost
parents:
26
diff
changeset
|
143 if (!*reglist) |
f736579569b4
Added handlers for inherent and register to register instructions
lost
parents:
26
diff
changeset
|
144 return -1; |
f736579569b4
Added handlers for inherent and register to register instructions
lost
parents:
26
diff
changeset
|
145 if (reglist[1] == ' ') |
f736579569b4
Added handlers for inherent and register to register instructions
lost
parents:
26
diff
changeset
|
146 (*str)++; |
f736579569b4
Added handlers for inherent and register to register instructions
lost
parents:
26
diff
changeset
|
147 else |
f736579569b4
Added handlers for inherent and register to register instructions
lost
parents:
26
diff
changeset
|
148 (*str) += 2; |
f736579569b4
Added handlers for inherent and register to register instructions
lost
parents:
26
diff
changeset
|
149 return rval; |
f736579569b4
Added handlers for inherent and register to register instructions
lost
parents:
26
diff
changeset
|
150 } |
32 | 151 |
152 int lwasm_lookupreg3(const char *rlist, const char **str) | |
153 { | |
154 int rval = 0; | |
155 int f = 0; | |
156 const char *reglist = rlist; | |
157 | |
158 while (*reglist) | |
159 { | |
160 if (toupper(**str) == *reglist) | |
161 { | |
162 // first char matches | |
163 if (reglist[1] == ' ') | |
164 { | |
165 f = 1; | |
166 break; | |
167 } | |
168 if (toupper(*(*str + 1)) == reglist[1]) | |
169 { | |
170 // second char matches | |
171 if (reglist[2] == ' ') | |
172 { | |
173 f = 1; | |
174 break; | |
175 } | |
176 if (toupper(*(*str + 2)) == reglist[2]) | |
177 { | |
178 f = 1; | |
179 break; | |
180 } | |
181 } | |
182 } | |
183 reglist += 3; | |
184 rval++; | |
185 } | |
186 if (f == 0) | |
187 return -1; | |
188 | |
189 | |
190 reglist = rval * 3 + rlist; | |
191 if (reglist[1] == ' ') | |
192 (*str) += 1; | |
193 else if (reglist[2] == ' ') | |
194 (*str) += 2; | |
195 else | |
196 (*str)+=3; | |
197 return rval; | |
198 } | |
37
538e15927776
Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents:
32
diff
changeset
|
199 |
538e15927776
Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents:
32
diff
changeset
|
200 struct symstateinfo |
538e15927776
Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents:
32
diff
changeset
|
201 { |
538e15927776
Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents:
32
diff
changeset
|
202 asmstate_t *as; |
538e15927776
Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents:
32
diff
changeset
|
203 lwasm_line_t *l; |
101
f59c0916753d
Fixed relative branches and PCR addressing to handle constant intra-section references properly
lost
parents:
98
diff
changeset
|
204 int flags; |
37
538e15927776
Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents:
32
diff
changeset
|
205 }; |
538e15927776
Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents:
32
diff
changeset
|
206 |
76 | 207 lwasm_expr_stack_t *lwasm_expr_lookup_symbol(char *sym, void *state) |
37
538e15927776
Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents:
32
diff
changeset
|
208 { |
538e15927776
Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents:
32
diff
changeset
|
209 lwasm_symbol_ent_t *se; |
538e15927776
Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents:
32
diff
changeset
|
210 struct symstateinfo *st; |
76 | 211 lwasm_expr_stack_t *rs; |
212 lwasm_expr_term_t *t; | |
213 lwasm_expr_stack_node_t *n; | |
214 | |
215 int val; | |
37
538e15927776
Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents:
32
diff
changeset
|
216 |
538e15927776
Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents:
32
diff
changeset
|
217 st = state; |
52 | 218 debug_message(3, "lwasm_expr_lookup_symbol(): find '%s' (context=%d)", sym, st -> as -> context); |
219 | |
60 | 220 // check for special symbols first... |
221 if (sym[1] == '\0') | |
222 { | |
223 switch (sym[0]) | |
224 { | |
225 // current line address | |
226 case '*': | |
227 case '.': | |
76 | 228 val = st -> l -> codeaddr; |
229 goto retconst; | |
60 | 230 |
231 case '<': | |
232 // previous branch point | |
233 // not implemented | |
234 break; | |
235 case '>': | |
236 // next branch point | |
237 // not implemented | |
238 break; | |
239 } | |
240 } | |
241 | |
37
538e15927776
Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents:
32
diff
changeset
|
242 // look for local symbol first then global symbol |
538e15927776
Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents:
32
diff
changeset
|
243 se = lwasm_find_symbol(st -> as, sym, st -> as -> context); |
538e15927776
Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents:
32
diff
changeset
|
244 if (!se) |
538e15927776
Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents:
32
diff
changeset
|
245 se = lwasm_find_symbol(st -> as, sym, -1); |
52 | 246 debug_message(3, "lwasm_expr_lookup_symbol(): got '%p'", se); |
37
538e15927776
Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents:
32
diff
changeset
|
247 if (!se) |
77 | 248 { |
249 register_error(st -> as, st -> l, 2, "Undefined symbol '%s'", sym); | |
76 | 250 return NULL; |
77 | 251 } |
92
ea2cfebef5d0
Make external symbols remain unresolved in expressions and also flag them in the symbol list
lost
parents:
84
diff
changeset
|
252 // external reference - can not resolve it |
ea2cfebef5d0
Make external symbols remain unresolved in expressions and also flag them in the symbol list
lost
parents:
84
diff
changeset
|
253 if (se -> flags & SYMBOL_EXTERN) |
ea2cfebef5d0
Make external symbols remain unresolved in expressions and also flag them in the symbol list
lost
parents:
84
diff
changeset
|
254 { |
ea2cfebef5d0
Make external symbols remain unresolved in expressions and also flag them in the symbol list
lost
parents:
84
diff
changeset
|
255 return NULL; |
ea2cfebef5d0
Make external symbols remain unresolved in expressions and also flag them in the symbol list
lost
parents:
84
diff
changeset
|
256 } |
101
f59c0916753d
Fixed relative branches and PCR addressing to handle constant intra-section references properly
lost
parents:
98
diff
changeset
|
257 if (st -> flags & EXPR_SECTCONST) |
f59c0916753d
Fixed relative branches and PCR addressing to handle constant intra-section references properly
lost
parents:
98
diff
changeset
|
258 { |
f59c0916753d
Fixed relative branches and PCR addressing to handle constant intra-section references properly
lost
parents:
98
diff
changeset
|
259 if (se -> sect == st -> l -> sect) |
f59c0916753d
Fixed relative branches and PCR addressing to handle constant intra-section references properly
lost
parents:
98
diff
changeset
|
260 { |
f59c0916753d
Fixed relative branches and PCR addressing to handle constant intra-section references properly
lost
parents:
98
diff
changeset
|
261 if (se -> expr) |
f59c0916753d
Fixed relative branches and PCR addressing to handle constant intra-section references properly
lost
parents:
98
diff
changeset
|
262 goto retsym; |
f59c0916753d
Fixed relative branches and PCR addressing to handle constant intra-section references properly
lost
parents:
98
diff
changeset
|
263 val = se -> value; |
f59c0916753d
Fixed relative branches and PCR addressing to handle constant intra-section references properly
lost
parents:
98
diff
changeset
|
264 goto retconst; |
f59c0916753d
Fixed relative branches and PCR addressing to handle constant intra-section references properly
lost
parents:
98
diff
changeset
|
265 } |
f59c0916753d
Fixed relative branches and PCR addressing to handle constant intra-section references properly
lost
parents:
98
diff
changeset
|
266 } |
93
34ca1c6e9550
Fixed symbol resolution to not resolve intra-section references to constants by default
lost
parents:
92
diff
changeset
|
267 if (st -> as -> outformat == OUTPUT_OBJ && se -> sect != NULL) |
34ca1c6e9550
Fixed symbol resolution to not resolve intra-section references to constants by default
lost
parents:
92
diff
changeset
|
268 { |
34ca1c6e9550
Fixed symbol resolution to not resolve intra-section references to constants by default
lost
parents:
92
diff
changeset
|
269 return NULL; |
34ca1c6e9550
Fixed symbol resolution to not resolve intra-section references to constants by default
lost
parents:
92
diff
changeset
|
270 } |
34ca1c6e9550
Fixed symbol resolution to not resolve intra-section references to constants by default
lost
parents:
92
diff
changeset
|
271 if (st -> as -> outformat != OUTPUT_OBJ || se -> sect == NULL) |
76 | 272 { |
273 // global symbol, intrasegment reference, or not an object target | |
274 val = se -> value; | |
275 goto retconst; | |
276 } | |
101
f59c0916753d
Fixed relative branches and PCR addressing to handle constant intra-section references properly
lost
parents:
98
diff
changeset
|
277 |
76 | 278 // an intersegment reference will return as NULL (to be resolved at output/link time) |
279 // if se -> expr is NULL, it has to be an intersegment reference here | |
280 if (se -> expr == NULL) | |
281 { | |
282 return NULL; | |
283 } | |
284 | |
101
f59c0916753d
Fixed relative branches and PCR addressing to handle constant intra-section references properly
lost
parents:
98
diff
changeset
|
285 retsym: |
76 | 286 // duplicate the expression for return |
287 rs = lwasm_expr_stack_create(); | |
288 for (n = se -> expr -> head; n; n = n -> next) | |
289 { | |
290 lwasm_expr_stack_push(rs, n -> term); | |
291 } | |
292 return rs; | |
293 | |
294 retconst: | |
295 rs = lwasm_expr_stack_create(); | |
296 t = lwasm_expr_term_create_int(val); | |
297 lwasm_expr_stack_push(rs, t); | |
298 lwasm_expr_term_free(t); | |
299 return rs; | |
37
538e15927776
Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents:
32
diff
changeset
|
300 } |
538e15927776
Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents:
32
diff
changeset
|
301 |
101
f59c0916753d
Fixed relative branches and PCR addressing to handle constant intra-section references properly
lost
parents:
98
diff
changeset
|
302 lwasm_expr_stack_t *lwasm_evaluate_expr(asmstate_t *as, lwasm_line_t *l, const char *inp, const char **outp, int flags) |
37
538e15927776
Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents:
32
diff
changeset
|
303 { |
538e15927776
Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents:
32
diff
changeset
|
304 struct symstateinfo st; |
538e15927776
Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents:
32
diff
changeset
|
305 |
538e15927776
Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents:
32
diff
changeset
|
306 st.as = as; |
538e15927776
Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents:
32
diff
changeset
|
307 st.l = l; |
101
f59c0916753d
Fixed relative branches and PCR addressing to handle constant intra-section references properly
lost
parents:
98
diff
changeset
|
308 st.flags = flags; |
37
538e15927776
Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents:
32
diff
changeset
|
309 |
38 | 310 debug_message(2, "Evaluate expression: %s", inp); |
311 | |
37
538e15927776
Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents:
32
diff
changeset
|
312 return(lwasm_expr_eval(inp, outp, lwasm_expr_lookup_symbol, &st)); |
538e15927776
Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents:
32
diff
changeset
|
313 } |
38 | 314 |
77 | 315 |
101
f59c0916753d
Fixed relative branches and PCR addressing to handle constant intra-section references properly
lost
parents:
98
diff
changeset
|
316 int lwasm_reevaluate_expr(asmstate_t *as, lwasm_line_t *l, lwasm_expr_stack_t *s, int flags) |
77 | 317 { |
318 struct symstateinfo st; | |
319 | |
320 st.as = as; | |
321 st.l = l; | |
101
f59c0916753d
Fixed relative branches and PCR addressing to handle constant intra-section references properly
lost
parents:
98
diff
changeset
|
322 st.flags = flags; |
77 | 323 return(lwasm_expr_reval(s, lwasm_expr_lookup_symbol, &st)); |
324 } | |
325 | |
76 | 326 // return 1 if no undefined symbols (externals and incompletes are okay) |
327 // return 0 if there are undefined symbols | |
328 int lwasm_expr_result_ckconst(asmstate_t *as, lwasm_expr_stack_t *s) | |
329 { | |
330 lwasm_expr_stack_node_t *n; | |
331 lwasm_symbol_ent_t *se; | |
332 | |
77 | 333 if (as -> outformat != OUTPUT_OBJ) |
334 { | |
335 if (lwasm_expr_is_constant(s)) | |
336 return 1; | |
337 else | |
338 return 0; | |
339 } | |
340 | |
76 | 341 for (n = s -> head; n; n = n -> next) |
342 { | |
343 if (n -> term -> term_type == LWASM_TERM_SYM) | |
344 { | |
345 se = lwasm_find_symbol(as, n -> term -> symbol, as -> context); | |
346 if (!se) | |
347 se = lwasm_find_symbol(as, n -> term -> symbol, -1); | |
348 if (!se) | |
349 return 0; | |
350 } | |
351 } | |
352 return 1; | |
353 } | |
354 | |
355 /* | |
356 Evaluate an expression according to the flag value. Return 0 if a constant result was | |
357 obtained, 1 if an incomplete result was obtained, and -1 if an error was flagged. | |
358 | |
77 | 359 */ |
360 int lwasm_expr_result2(asmstate_t *as, lwasm_line_t *l, char **inp, int flag, int *val, int slot) | |
361 { | |
79
d0ce3f5f6797
Checkpointing deployment of non-constant expression handling
lost
parents:
78
diff
changeset
|
362 lwasm_expr_stack_t *s = NULL; |
77 | 363 const char *ep; |
364 int rval; | |
365 | |
101
f59c0916753d
Fixed relative branches and PCR addressing to handle constant intra-section references properly
lost
parents:
98
diff
changeset
|
366 if ((as -> passnum == 1 && !(flag & EXPR_REEVAL)) || slot < 0) |
77 | 367 { |
101
f59c0916753d
Fixed relative branches and PCR addressing to handle constant intra-section references properly
lost
parents:
98
diff
changeset
|
368 s = lwasm_evaluate_expr(as, l, *inp, &ep, flag); |
79
d0ce3f5f6797
Checkpointing deployment of non-constant expression handling
lost
parents:
78
diff
changeset
|
369 if (slot >= 0) |
d0ce3f5f6797
Checkpointing deployment of non-constant expression handling
lost
parents:
78
diff
changeset
|
370 l -> exprs[slot] = s; |
77 | 371 if (!s) |
372 { | |
373 register_error(as, l, 1, "Bad expression"); | |
374 *val = 0; | |
375 return -1; | |
376 } | |
377 *inp = (char *)ep; | |
79
d0ce3f5f6797
Checkpointing deployment of non-constant expression handling
lost
parents:
78
diff
changeset
|
378 if (slot >= 0) |
d0ce3f5f6797
Checkpointing deployment of non-constant expression handling
lost
parents:
78
diff
changeset
|
379 { |
94
83ba34ed11b3
Fixed problem with constant expressions evaluating to 0 when they shouldn't
lost
parents:
93
diff
changeset
|
380 l -> exprends[slot] = (char *)ep; |
79
d0ce3f5f6797
Checkpointing deployment of non-constant expression handling
lost
parents:
78
diff
changeset
|
381 l -> exprvals[slot] = lwasm_expr_get_value(s); |
d0ce3f5f6797
Checkpointing deployment of non-constant expression handling
lost
parents:
78
diff
changeset
|
382 } |
77 | 383 } |
384 else if (l -> exprs[slot]) | |
385 { | |
386 s = l -> exprs[slot]; | |
101
f59c0916753d
Fixed relative branches and PCR addressing to handle constant intra-section references properly
lost
parents:
98
diff
changeset
|
387 lwasm_reevaluate_expr(as, l, s, flag); |
77 | 388 l -> exprvals[slot] = lwasm_expr_get_value(s); |
389 } | |
84
e12edcfbebd5
Fixed problem with expression evaluation infrastructure not advancing input pointer on pass 2
lost
parents:
79
diff
changeset
|
390 if (as -> passnum == 2 && slot >= 0) |
e12edcfbebd5
Fixed problem with expression evaluation infrastructure not advancing input pointer on pass 2
lost
parents:
79
diff
changeset
|
391 *inp = l -> exprends[slot]; |
77 | 392 |
393 if (s && lwasm_expr_is_constant(s)) | |
394 { | |
94
83ba34ed11b3
Fixed problem with constant expressions evaluating to 0 when they shouldn't
lost
parents:
93
diff
changeset
|
395 *val = lwasm_expr_get_value(s); |
77 | 396 lwasm_expr_stack_free(s); |
397 l -> exprs[slot] = NULL; | |
398 s = NULL; | |
94
83ba34ed11b3
Fixed problem with constant expressions evaluating to 0 when they shouldn't
lost
parents:
93
diff
changeset
|
399 return 0; |
77 | 400 } |
401 | |
79
d0ce3f5f6797
Checkpointing deployment of non-constant expression handling
lost
parents:
78
diff
changeset
|
402 if (!s && slot >= 0) |
77 | 403 { |
404 *val = l -> exprvals[slot]; | |
405 return 0; | |
406 } | |
79
d0ce3f5f6797
Checkpointing deployment of non-constant expression handling
lost
parents:
78
diff
changeset
|
407 else if (!s) |
d0ce3f5f6797
Checkpointing deployment of non-constant expression handling
lost
parents:
78
diff
changeset
|
408 { |
d0ce3f5f6797
Checkpointing deployment of non-constant expression handling
lost
parents:
78
diff
changeset
|
409 *val = 0; |
d0ce3f5f6797
Checkpointing deployment of non-constant expression handling
lost
parents:
78
diff
changeset
|
410 return 0; |
d0ce3f5f6797
Checkpointing deployment of non-constant expression handling
lost
parents:
78
diff
changeset
|
411 } |
78
121bf4a588ea
Checkpointing deployment of non-constant expression handling
lost
parents:
77
diff
changeset
|
412 |
121bf4a588ea
Checkpointing deployment of non-constant expression handling
lost
parents:
77
diff
changeset
|
413 // was a constant result on pass 1 requested? |
121bf4a588ea
Checkpointing deployment of non-constant expression handling
lost
parents:
77
diff
changeset
|
414 // that means we must have a constant on either pass |
121bf4a588ea
Checkpointing deployment of non-constant expression handling
lost
parents:
77
diff
changeset
|
415 if (flag & EXPR_PASS1CONST) |
121bf4a588ea
Checkpointing deployment of non-constant expression handling
lost
parents:
77
diff
changeset
|
416 { |
121bf4a588ea
Checkpointing deployment of non-constant expression handling
lost
parents:
77
diff
changeset
|
417 *val = 0; |
79
d0ce3f5f6797
Checkpointing deployment of non-constant expression handling
lost
parents:
78
diff
changeset
|
418 if (slot >= 0) |
d0ce3f5f6797
Checkpointing deployment of non-constant expression handling
lost
parents:
78
diff
changeset
|
419 l -> exprvals[slot] = 0; |
78
121bf4a588ea
Checkpointing deployment of non-constant expression handling
lost
parents:
77
diff
changeset
|
420 register_error(as, l, 1, "Illegal forward, external, or inter-section reference"); |
121bf4a588ea
Checkpointing deployment of non-constant expression handling
lost
parents:
77
diff
changeset
|
421 lwasm_expr_stack_free(s); |
79
d0ce3f5f6797
Checkpointing deployment of non-constant expression handling
lost
parents:
78
diff
changeset
|
422 if (slot >= 0) |
d0ce3f5f6797
Checkpointing deployment of non-constant expression handling
lost
parents:
78
diff
changeset
|
423 l -> exprs[slot] = NULL; |
78
121bf4a588ea
Checkpointing deployment of non-constant expression handling
lost
parents:
77
diff
changeset
|
424 return -1; |
121bf4a588ea
Checkpointing deployment of non-constant expression handling
lost
parents:
77
diff
changeset
|
425 } |
77 | 426 |
427 return 1; | |
428 } | |
55 | 429 |
38 | 430 void debug_message(int level, const char *fmt, ...) |
431 { | |
432 va_list args; | |
433 | |
434 va_start(args, fmt); | |
435 if (debug_level >= level) | |
436 { | |
39 | 437 if (level > 0) |
438 fprintf(stderr, "DEBUG %d: ", level); | |
38 | 439 vfprintf(stderr, fmt, args); |
440 fputc('\n', stderr); | |
441 } | |
442 va_end(args); | |
443 } | |
57 | 444 |
445 int lwasm_next_context(asmstate_t *as) | |
446 { | |
58 | 447 int r; |
448 r = as -> nextcontext; | |
449 as -> nextcontext += 1; | |
450 debug_message(3, "lwasm_next_context(): %d (%d) pass %d", r, as -> nextcontext, as -> passnum); | |
451 return r; | |
57 | 452 } |
453 |