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

[optimizer] Fix in-block jump offsets resolved wrong inside blocks with NOP statements. #1042

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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
48 changes: 13 additions & 35 deletions optimize.c
Original file line number Diff line number Diff line change
Expand Up @@ -2688,7 +2688,6 @@ convert_code_r(conv_state_t *conv_state, struct icode *ic, struct block *p)
struct slist *src;
u_int slen;
u_int off;
struct slist **offset = NULL;

if (p == 0 || isMarked(ic, p))
return (1);
Expand All @@ -2705,23 +2704,6 @@ convert_code_r(conv_state_t *conv_state, struct icode *ic, struct block *p)

p->offset = (int)(dst - conv_state->fstart);

/* generate offset[] for convenience */
if (slen) {
offset = (struct slist **)calloc(slen, sizeof(struct slist *));
if (!offset) {
conv_error(conv_state, "not enough core");
/*NOTREACHED*/
}
}
src = p->stmts;
for (off = 0; off < slen && src; off++) {
#if 0
printf("off=%d src=%x\n", off, src);
#endif
offset[off] = src;
src = src->next;
}

off = 0;
for (src = p->stmts; src; src = src->next) {
if (src->s.code == NOP)
Expand All @@ -2744,8 +2726,9 @@ convert_code_r(conv_state_t *conv_state, struct icode *ic, struct block *p)
goto filled;

{
u_int i;
u_int target_off;
int jt, jf;
struct slist *target;
const char ljerr[] = "%s for block-local relative jump: off=%d";

#if 0
Expand All @@ -2754,45 +2737,42 @@ convert_code_r(conv_state_t *conv_state, struct icode *ic, struct block *p)
#endif

if (!src->s.jt || !src->s.jf) {
free(offset);
conv_error(conv_state, ljerr, "no jmp destination", off);
/*NOTREACHED*/
}

jt = jf = 0;
for (i = 0; i < slen; i++) {
if (offset[i] == src->s.jt) {
jt = jf = target_off = 0;
for (target = p->stmts; target; target = target->next) {
if (target == src->s.jt) {
if (jt) {
free(offset);
conv_error(conv_state, ljerr, "multiple matches", off);
/*NOTREACHED*/
}

if (i - off - 1 >= 256) {
free(offset);
if (target_off - off - 1 >= 256) {
conv_error(conv_state, ljerr, "out-of-range jump", off);
/*NOTREACHED*/
}
dst->jt = (u_char)(i - off - 1);
dst->jt = (u_char)(target_off - off - 1);
jt++;
}
if (offset[i] == src->s.jf) {
if (target == src->s.jf) {
if (jf) {
free(offset);
conv_error(conv_state, ljerr, "multiple matches", off);
/*NOTREACHED*/
}
if (i - off - 1 >= 256) {
free(offset);
if (target_off - off - 1 >= 256) {
conv_error(conv_state, ljerr, "out-of-range jump", off);
/*NOTREACHED*/
}
dst->jf = (u_char)(i - off - 1);
dst->jf = (u_char)(target_off - off - 1);
jf++;
}
if (target->s.code != NOP) {
target_off++;
}
}
if (!jt || !jf) {
free(offset);
conv_error(conv_state, ljerr, "no destination found", off);
/*NOTREACHED*/
}
Expand All @@ -2801,8 +2781,6 @@ convert_code_r(conv_state_t *conv_state, struct icode *ic, struct block *p)
++dst;
++off;
}
if (offset)
free(offset);

#ifdef BDEBUG
if (dst - conv_state->fstart < NBIDS)
Expand Down