You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

78 lines
2.6 KiB

  1. From e6676ebbd2ab0a6283d96c797dbe93552c1a222c Mon Sep 17 00:00:00 2001
  2. From: Muh Muhten <muh.muhten@gmail.com>
  3. Date: Mon, 18 Feb 2019 21:00:59 -0500
  4. Subject: [PATCH 3/9] Simplify definition of block_bind_referenced
  5. ---
  6. src/compile.c | 49 ++++++++-----------------------------------------
  7. 1 file changed, 8 insertions(+), 41 deletions(-)
  8. --- a/src/compile.c
  9. +++ b/src/compile.c
  10. @@ -317,20 +317,6 @@ static int block_count_actuals(block b)
  11. return args;
  12. }
  13. -static int block_count_refs(block binder, block body) {
  14. - int nrefs = 0;
  15. - for (inst* i = body.first; i; i = i->next) {
  16. - if (i != binder.first && i->bound_by == binder.first) {
  17. - nrefs++;
  18. - }
  19. - // counting recurses into closures
  20. - nrefs += block_count_refs(binder, i->subfn);
  21. - // counting recurses into argument list
  22. - nrefs += block_count_refs(binder, i->arglist);
  23. - }
  24. - return nrefs;
  25. -}
  26. -
  27. static int block_bind_subblock_inner(int* any_unbound, block binder, block body, int bindflags, int break_distance) {
  28. assert(block_is_single(binder));
  29. assert((opcode_describe(binder.first->op)->flags & bindflags) == (bindflags & ~OP_BIND_WILDCARD));
  30. @@ -434,37 +420,18 @@ block block_bind_library(block binder, b
  31. return body; // We don't return a join because we don't want those sticking around...
  32. }
  33. -// Bind binder to body and throw away any defs in binder not referenced
  34. -// (directly or indirectly) from body.
  35. +// Bind binder to body, then throw it away if not referenced.
  36. block block_bind_referenced(block binder, block body, int bindflags) {
  37. + assert(block_is_single(binder));
  38. assert(block_has_only_binders(binder, bindflags));
  39. bindflags |= OP_HAS_BINDING;
  40. - block refd = gen_noop();
  41. - block unrefd = gen_noop();
  42. - int nrefs;
  43. - for (int last_kept = 0, kept = 0; ; ) {
  44. - for (inst* curr; (curr = block_take(&binder));) {
  45. - block b = inst_block(curr);
  46. - nrefs = block_bind_each(b, body, bindflags);
  47. - // Check if this binder is referenced from any of the ones we
  48. - // already know are referenced by body.
  49. - nrefs += block_count_refs(b, refd);
  50. - nrefs += block_count_refs(b, body);
  51. - if (nrefs) {
  52. - refd = BLOCK(refd, b);
  53. - kept++;
  54. - } else {
  55. - unrefd = BLOCK(unrefd, b);
  56. - }
  57. - }
  58. - if (kept == last_kept)
  59. - break;
  60. - last_kept = kept;
  61. - binder = unrefd;
  62. - unrefd = gen_noop();
  63. +
  64. + if (block_bind_subblock(binder, body, bindflags, 0) == 0) {
  65. + block_free(binder);
  66. + } else {
  67. + body = BLOCK(binder, body);
  68. }
  69. - block_free(unrefd);
  70. - return block_join(refd, body);
  71. + return body;
  72. }
  73. static void block_mark_referenced(block body) {