comparison lwasm/output.c @ 195:17bd59f045af

Changed symbol table to use a binary tree. Changed symbol table to use a binary tree. Hopefully this improves table lookups some but the tree really needs to be balanced at some point.
author William Astle <lost@l-w.ca>
date Sun, 11 Mar 2012 16:05:54 -0600
parents fc8386b13399
children 2b784a28428e
comparison
equal deleted inserted replaced
194:f8b33b3a45ac 195:17bd59f045af
365 break; 365 break;
366 } 366 }
367 return 0; 367 return 0;
368 } 368 }
369 369
370 void write_code_obj_auxsym(asmstate_t *as, FILE *of, sectiontab_t *s, struct symtabe *se2)
371 {
372 struct symtabe *se;
373 unsigned char buf[16];
374
375 if (!se2)
376 return;
377 write_code_obj_auxsym(as, of, s, se2 -> left);
378
379 for (se = se2; se; se = se -> nextver)
380 {
381 lw_expr_t te;
382
383 debug_message(as, 200, "Consider symbol %s (%p) for export in section %p", se -> symbol, se -> section, s);
384
385 // ignore symbols not in this section
386 if (se -> section != s)
387 continue;
388
389 debug_message(as, 200, " In section");
390
391 if (se -> flags & symbol_flag_set)
392 continue;
393
394 debug_message(as, 200, " Not symbol_flag_set");
395
396 te = lw_expr_copy(se -> value);
397 debug_message(as, 200, " Value=%s", lw_expr_print(te));
398 as -> exportcheck = 1;
399 as -> csect = s;
400 lwasm_reduce_expr(as, te);
401 as -> exportcheck = 0;
402
403 debug_message(as, 200, " Value2=%s", lw_expr_print(te));
404
405 // don't output non-constant symbols
406 if (!lw_expr_istype(te, lw_expr_type_int))
407 {
408 lw_expr_destroy(te);
409 continue;
410 }
411
412 writebytes(se -> symbol, strlen(se -> symbol), 1, of);
413 if (se -> context >= 0)
414 {
415 writebytes("\x01", 1, 1, of);
416 sprintf((char *)buf, "%d", se -> context);
417 writebytes(buf, strlen((char *)buf), 1, of);
418 }
419 // the "" is NOT an error
420 writebytes("", 1, 1, of);
421
422 // write the address
423 buf[0] = (lw_expr_intval(te) >> 8) & 0xff;
424 buf[1] = lw_expr_intval(te) & 0xff;
425 writebytes(buf, 2, 1, of);
426 lw_expr_destroy(te);
427 }
428 write_code_obj_auxsym(as, of, s, se2 -> right);
429 }
370 430
371 void write_code_obj(asmstate_t *as, FILE *of) 431 void write_code_obj(asmstate_t *as, FILE *of)
372 { 432 {
373 line_t *l; 433 line_t *l;
374 sectiontab_t *s; 434 sectiontab_t *s;
375 reloctab_t *re; 435 reloctab_t *re;
376 exportlist_t *ex; 436 exportlist_t *ex;
377 struct symtabe *se;
378 437
379 int i; 438 int i;
380 unsigned char buf[16]; 439 unsigned char buf[16];
381 440
382 // output the magic number and file header 441 // output the magic number and file header
441 writebytes("\x02", 1, 1, of); 500 writebytes("\x02", 1, 1, of);
442 writebytes(s -> name, strlen(s -> name) + 1, 1, of); 501 writebytes(s -> name, strlen(s -> name) + 1, 1, of);
443 // address 0; "\0" is not an error 502 // address 0; "\0" is not an error
444 writebytes("\0", 2, 1, of); 503 writebytes("\0", 2, 1, of);
445 } 504 }
446 for (se = as -> symtab.head; se; se = se -> next) 505
447 { 506 write_code_obj_auxsym(as, of, s, as -> symtab.head);
448 lw_expr_t te;
449
450 debug_message(as, 200, "Consider symbol %s (%p) for export in section %p", se -> symbol, se -> section, s);
451
452 // ignore symbols not in this section
453 if (se -> section != s)
454 continue;
455
456 debug_message(as, 200, " In section");
457
458 if (se -> flags & symbol_flag_set)
459 continue;
460
461 debug_message(as, 200, " Not symbol_flag_set");
462
463 te = lw_expr_copy(se -> value);
464 debug_message(as, 200, " Value=%s", lw_expr_print(te));
465 as -> exportcheck = 1;
466 as -> csect = s;
467 lwasm_reduce_expr(as, te);
468 as -> exportcheck = 0;
469
470 debug_message(as, 200, " Value2=%s", lw_expr_print(te));
471
472 // don't output non-constant symbols
473 if (!lw_expr_istype(te, lw_expr_type_int))
474 {
475 lw_expr_destroy(te);
476 continue;
477 }
478
479 writebytes(se -> symbol, strlen(se -> symbol), 1, of);
480 if (se -> context >= 0)
481 {
482 writebytes("\x01", 1, 1, of);
483 sprintf((char *)buf, "%d", se -> context);
484 writebytes(buf, strlen((char *)buf), 1, of);
485 }
486 // the "" is NOT an error
487 writebytes("", 1, 1, of);
488
489 // write the address
490 buf[0] = (lw_expr_intval(te) >> 8) & 0xff;
491 buf[1] = lw_expr_intval(te) & 0xff;
492 writebytes(buf, 2, 1, of);
493 lw_expr_destroy(te);
494 }
495 // flag end of local symbol table - "" is NOT an error 507 // flag end of local symbol table - "" is NOT an error
496 writebytes("", 1, 1, of); 508 writebytes("", 1, 1, of);
497 509
498 // now the exports -- FIXME 510 // now the exports -- FIXME
499 for (ex = as -> exportlist; ex; ex = ex -> next) 511 for (ex = as -> exportlist; ex; ex = ex -> next)