Skip to content
Myr

Reimplement block_drop_unreferenced in linear time

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Muh Muhten · committed by Nicolas Williams · parent 9fa2e51 · Browse files · 1 file+29 added−21 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+29 added−21 removed

  • other:+29 added−21 removed

1 file+29 added−21 removed

modified:src/compile.c+29 added-21 removed
51 unmodified lines
52 // body which are unbound and refer to "definition" by name.53 struct inst* bound_by;54 char* symbol;55 int any_unbound;56 int referenced;5758 int nformals;59 int nactuals;60
14 unmodified lines
75 i->bytecode_pos = -1;76 i->bound_by = 0;77 i->symbol = 0;78 i->any_unbound = 0;79 i->referenced = 0;80 i->nformals = -1;81 i->nactuals = -1;82 i->subfn = gen_noop();83 i->arglist = gen_noop();
382 unmodified lines
466 block_free(unrefd);467 return block_join(refd, body);468}469470static void block_mark_referenced(block body) {471 int saw_top = 0;472 for (inst* i = body.last; i; i = i->prev) {473 if (saw_top && i->bound_by == i && !i->referenced)474 continue;475 if (i->op == TOP) {476 saw_top = 1;477 }478 if (i->bound_by) {479 i->bound_by->referenced = 1;480 }481482 block_mark_referenced(i->arglist);483 block_mark_referenced(i->subfn);484 }485}486487block block_drop_unreferenced(block body) {469 inst* curr;488 block_mark_referenced(body);489490 block refd = gen_noop();471 block unrefd = gen_noop();472 int drop;473 do {474 drop = 0;475 while ((curr = block_take(&body)) && curr->op != TOP) {476 block b = inst_block(curr);477 if (block_count_refs(b,refd) + block_count_refs(b,body) == 0) {478 unrefd = BLOCK(unrefd, b);479 drop++;480 } else {481 refd = BLOCK(refd, b);482 }491 inst* curr;492 while ((curr = block_take(&body))) {493 if (curr->bound_by == curr && !curr->referenced) {494 inst_free(curr);495 } else {496 refd = BLOCK(inst_block(curr), refd);497 }484 if (curr && curr->op == TOP) {485 body = BLOCK(inst_block(curr),body);486 }487 body = BLOCK(refd, body);498 }499 return refd;488 refd = gen_noop();489 } while (drop != 0);490 block_free(unrefd);491 return body;500}501502jv block_take_imports(block* body) {503 jv imports = jv_array();
912 unmodified lines