This commit is contained in:
Přemysl Eric Janouch 2016-12-24 00:48:27 +01:00
parent b4b3ffef24
commit 290e7087bb
Signed by: p
GPG Key ID: B715679E3A361BE6
1 changed files with 17 additions and 20 deletions

View File

@ -163,6 +163,19 @@ optimize_assignment (struct instruction *irb, size_t irb_len)
return out; return out;
} }
// Add the offset of the LEFT/RIGHT instruction to the accumulator
static bool
add_direction_offset (struct instruction *irb, intptr_t *acc)
{
if (irb->cmd == RIGHT)
*acc += irb->arg;
else if (irb->cmd == LEFT)
*acc -= (intptr_t) irb->arg;
else
return false;
return true;
}
// Add offsets to INC/DEC/SET stuck between LEFT/RIGHT // Add offsets to INC/DEC/SET stuck between LEFT/RIGHT
// and compress the LEFT/RIGHT sequences // and compress the LEFT/RIGHT sequences
static size_t static size_t
@ -172,11 +185,7 @@ optimize_offseted_inc_dec (struct instruction *irb, size_t irb_len)
for (in = 0, out = 0; in < irb_len; in++, out++) for (in = 0, out = 0; in < irb_len; in++, out++)
{ {
intptr_t dir = 0; intptr_t dir = 0;
if (irb[in].cmd == RIGHT) if (!add_direction_offset (&irb[in], &dir))
dir = irb[in].arg;
else if (irb[in].cmd == LEFT)
dir = -(intptr_t) irb[in].arg;
else
{ {
irb[out] = irb[in]; irb[out] = irb[in];
continue; continue;
@ -187,15 +196,9 @@ optimize_offseted_inc_dec (struct instruction *irb, size_t irb_len)
// An immediate offset has its limits on x86-64 // An immediate offset has its limits on x86-64
if (dir < INT8_MIN || dir > INT8_MAX) if (dir < INT8_MIN || dir > INT8_MAX)
break; break;
intptr_t diff = 0;
intptr_t diff; if (!add_direction_offset (&irb[in + 2], &diff))
if (irb[in + 2].cmd == RIGHT)
diff = irb[in + 2].arg;
else if (irb[in + 2].cmd == LEFT)
diff = -(intptr_t) irb[in + 2].arg;
else
break; break;
int cmd = irb[in + 1].cmd; int cmd = irb[in + 1].cmd;
if (cmd != INC && cmd != DEC && cmd != SET) if (cmd != INC && cmd != DEC && cmd != SET)
break; break;
@ -209,14 +212,8 @@ optimize_offseted_inc_dec (struct instruction *irb, size_t irb_len)
} }
for (; in + 1 < irb_len; in++) for (; in + 1 < irb_len; in++)
{ if (!add_direction_offset (&irb[in + 1], &dir))
if (irb[in + 1].cmd == RIGHT)
dir += irb[in + 1].arg;
else if (irb[in + 1].cmd == LEFT)
dir -= (intptr_t) irb[in + 1].arg;
else
break; break;
}
if (!dir) if (!dir)
out--; out--;