Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove unused jump table entries #33

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions lib/expr.c
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,28 @@ read_expr_common(const uint8_t **pp, const uint8_t *ep, struct expr *expr,
i, j->pc, j->targetpc);
}
#endif
if (vctx->can_shrink_jump_table) {
assert(ei->jumps != NULL);
assert(ei->njumps > 0);
const struct jump *from = ei->jumps;
struct jump *to = ei->jumps;
const struct jump *ep = ei->jumps + ei->njumps;
while (from < ep) {
if (from->targetpc != JUMP_TABLE_INVALID_PC) {
*to++ = *from;
}
from++;
}
assert(to < from);
xlog_trace("jump table shrinked from %" PRIu32 " to %zu",
ei->njumps, to - ei->jumps);
ei->njumps = to - ei->jumps;
ret = resize_array((void **)&ei->jumps, sizeof(*ei->jumps),
ei->njumps);
if (ret != 0) {
xlog_error("ignoring jump table resize failure");
}
}
*pp = p;
#if defined(TOYWASM_ENABLE_WRITER)
expr->end = p;
Expand Down
6 changes: 4 additions & 2 deletions lib/type.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@
#define WASM_PAGE_SIZE 65536
#define WASM_MAX_PAGES 65536

#define JUMP_TABLE_INVALID_PC 0xffffffff

struct jump {
uint32_t pc;
uint32_t targetpc;
uint32_t pc; /* the address of block instruction (eg. block, loop) */
uint32_t targetpc; /* the jump target addreass */
};

struct type_annotation {
Expand Down
9 changes: 8 additions & 1 deletion lib/validation.c
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ push_ctrlframe(uint32_t pc, enum ctrlframe_op op, uint32_t jumpslot,
cframe->start_types = start_types;
cframe->end_types = end_types;
cframe->unreachable = false;
cframe->seen = false;
cframe->height = ctx->valtypes.lsize;
cframe->height_cell = ctx->ncells;
assert(nsize == ctx->cframes.lsize);
Expand Down Expand Up @@ -224,8 +225,12 @@ pop_ctrlframe(uint32_t pc, bool is_else, struct ctrlframe *cframep,
assert(jump->targetpc == 0);
if (cframe->op == FRAME_OP_LOOP) {
jump->targetpc = jump->pc;
} else {
} else if (cframe->seen || (cframe->op == FRAME_OP_IF ||
cframe->op == FRAME_OP_ELSE)) {
jump->targetpc = pc;
} else {
ctx->can_shrink_jump_table = true;
jump->targetpc = JUMP_TABLE_INVALID_PC;
}
}
ret = pop_valtypes(cframe->end_types, ctx);
Expand Down Expand Up @@ -296,6 +301,7 @@ validation_context_reuse(struct validation_context *ctx)
ctx->valtypes.lsize = 0;
ctx->ncells = 0;
ctx->locals.lsize = 0;
ctx->can_shrink_jump_table = false;
}

void
Expand Down Expand Up @@ -324,6 +330,7 @@ target_label_types(struct validation_context *ctx, uint32_t labelidx,
struct ctrlframe *cframe =
&VEC_ELEM(ctx->cframes, ctx->cframes.lsize - labelidx - 1);
const struct resulttype *rt = label_types(cframe);
cframe->seen = true;
*rtp = rt;
return 0;
}
Expand Down
2 changes: 2 additions & 0 deletions lib/validation.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ struct ctrlframe {
uint32_t height;
uint32_t height_cell;
bool unreachable;
bool seen;
};

struct validation_context {
Expand All @@ -28,6 +29,7 @@ struct validation_context {
VEC(, enum valtype) locals;

bool const_expr;
bool can_shrink_jump_table;

bool has_datacount;
uint32_t ndatas_in_datacount;
Expand Down