comparison src/lwasm.c @ 76:2fe5fd7d65a3

Checkpointing object target implementation
author lost
date Thu, 08 Jan 2009 02:57:24 +0000
parents c8c772ef5df9
children a338d496350e
comparison
equal deleted inserted replaced
75:92eb93bffa28 76:2fe5fd7d65a3
173 { 173 {
174 asmstate_t *as; 174 asmstate_t *as;
175 lwasm_line_t *l; 175 lwasm_line_t *l;
176 }; 176 };
177 177
178 int lwasm_expr_lookup_symbol(char *sym, void *state, int *val) 178 lwasm_expr_stack_t *lwasm_expr_lookup_symbol(char *sym, void *state)
179 { 179 {
180 lwasm_symbol_ent_t *se; 180 lwasm_symbol_ent_t *se;
181 struct symstateinfo *st; 181 struct symstateinfo *st;
182 lwasm_expr_stack_t *rs;
183 lwasm_expr_term_t *t;
184 lwasm_expr_stack_node_t *n;
185
186 int val;
182 187
183 st = state; 188 st = state;
184 debug_message(3, "lwasm_expr_lookup_symbol(): find '%s' (context=%d)", sym, st -> as -> context); 189 debug_message(3, "lwasm_expr_lookup_symbol(): find '%s' (context=%d)", sym, st -> as -> context);
185 190
186 // check for special symbols first... 191 // check for special symbols first...
189 switch (sym[0]) 194 switch (sym[0])
190 { 195 {
191 // current line address 196 // current line address
192 case '*': 197 case '*':
193 case '.': 198 case '.':
194 *val = st -> l -> codeaddr; 199 val = st -> l -> codeaddr;
195 return 0; 200 goto retconst;
196 201
197 case '<': 202 case '<':
198 // previous branch point 203 // previous branch point
199 // not implemented 204 // not implemented
200 break; 205 break;
209 se = lwasm_find_symbol(st -> as, sym, st -> as -> context); 214 se = lwasm_find_symbol(st -> as, sym, st -> as -> context);
210 if (!se) 215 if (!se)
211 se = lwasm_find_symbol(st -> as, sym, -1); 216 se = lwasm_find_symbol(st -> as, sym, -1);
212 debug_message(3, "lwasm_expr_lookup_symbol(): got '%p'", se); 217 debug_message(3, "lwasm_expr_lookup_symbol(): got '%p'", se);
213 if (!se) 218 if (!se)
214 return -1; 219 return NULL;
215 *val = se -> value; 220 if (st -> as -> outformat != OUTPUT_OBJ || se -> sect == NULL || se -> sect == st -> as -> csect)
216 return 0; 221 {
222 // global symbol, intrasegment reference, or not an object target
223 val = se -> value;
224 goto retconst;
225 }
226 // an intersegment reference will return as NULL (to be resolved at output/link time)
227 // if se -> expr is NULL, it has to be an intersegment reference here
228 if (se -> expr == NULL)
229 {
230 return NULL;
231 }
232
233 // duplicate the expression for return
234 rs = lwasm_expr_stack_create();
235 for (n = se -> expr -> head; n; n = n -> next)
236 {
237 lwasm_expr_stack_push(rs, n -> term);
238 }
239 return rs;
240
241 retconst:
242 rs = lwasm_expr_stack_create();
243 t = lwasm_expr_term_create_int(val);
244 lwasm_expr_stack_push(rs, t);
245 lwasm_expr_term_free(t);
246 return rs;
217 } 247 }
218 248
219 lwasm_expr_stack_t *lwasm_evaluate_expr(asmstate_t *as, lwasm_line_t *l, const char *inp, const char **outp) 249 lwasm_expr_stack_t *lwasm_evaluate_expr(asmstate_t *as, lwasm_line_t *l, const char *inp, const char **outp)
220 { 250 {
221 struct symstateinfo st; 251 struct symstateinfo st;
226 debug_message(2, "Evaluate expression: %s", inp); 256 debug_message(2, "Evaluate expression: %s", inp);
227 257
228 return(lwasm_expr_eval(inp, outp, lwasm_expr_lookup_symbol, &st)); 258 return(lwasm_expr_eval(inp, outp, lwasm_expr_lookup_symbol, &st));
229 } 259 }
230 260
261 // return 1 if no undefined symbols (externals and incompletes are okay)
262 // return 0 if there are undefined symbols
263 int lwasm_expr_result_ckconst(asmstate_t *as, lwasm_expr_stack_t *s)
264 {
265 lwasm_expr_stack_node_t *n;
266 lwasm_symbol_ent_t *se;
267
268 for (n = s -> head; n; n = n -> next)
269 {
270 if (n -> term -> term_type == LWASM_TERM_SYM)
271 {
272 se = lwasm_find_symbol(as, n -> term -> symbol, as -> context);
273 if (!se)
274 se = lwasm_find_symbol(as, n -> term -> symbol, -1);
275 if (!se)
276 return 0;
277 }
278 }
279 return 1;
280 }
281
282 /*
283 Evaluate an expression according to the flag value. Return 0 if a constant result was
284 obtained, 1 if an incomplete result was obtained, and -1 if an error was flagged.
285
286 Symbol resolution will be modified for the object target as follows:
287 - a symbol which is not defined within a section will evaluate as a constant
288 - a symbol which is defined within the same section will evaluate as a constant
289 - a symbol defined in another section will remain unresolved
290 - external references will also remain unresolved
291
292 */
231 int lwasm_expr_result(asmstate_t *as, lwasm_line_t *l, char **inp, int flag, int *val) 293 int lwasm_expr_result(asmstate_t *as, lwasm_line_t *l, char **inp, int flag, int *val)
232 { 294 {
233 lwasm_expr_stack_t *s; 295 lwasm_expr_stack_t *s;
234 const char *ep; 296 const char *ep;
235 int rval; 297 int rval;