codegen.c: a new function `get_int_operand`.

parent 5066e237
...@@ -562,6 +562,33 @@ gen_return(codegen_scope *s, uint8_t op, uint16_t src) ...@@ -562,6 +562,33 @@ gen_return(codegen_scope *s, uint8_t op, uint16_t src)
} }
} }
static mrb_bool
get_int_operand(struct mrb_insn_data *data, int32_t *n)
{
switch (data->insn) {
case OP_LOADI__1:
*n = -1;
return TRUE;
case OP_LOADI_0: case OP_LOADI_1: case OP_LOADI_2: case OP_LOADI_3:
case OP_LOADI_4: case OP_LOADI_5: case OP_LOADI_6: case OP_LOADI_7:
*n = data->insn - OP_LOADI_0;
return TRUE;
case OP_LOADI:
case OP_LOADI16:
*n = data->b;
return TRUE;
case OP_LOADI32:
*n = (int32_t)((uint32_t)data->b<<16)+data->c;
return TRUE;
default:
return FALSE;
}
}
static void static void
gen_addsub(codegen_scope *s, uint8_t op, uint16_t dst) gen_addsub(codegen_scope *s, uint8_t op, uint16_t dst)
{ {
...@@ -572,30 +599,20 @@ gen_addsub(codegen_scope *s, uint8_t op, uint16_t dst) ...@@ -572,30 +599,20 @@ gen_addsub(codegen_scope *s, uint8_t op, uint16_t dst)
} }
else { else {
struct mrb_insn_data data = mrb_last_insn(s); struct mrb_insn_data data = mrb_last_insn(s);
int32_t n;
switch (data.insn) { if (!get_int_operand(&data, &n)) {
case OP_LOADI__1: /* not integer immediate */
if (op == OP_ADD) op = OP_SUB; goto normal;
else op = OP_ADD; }
data.b = 1; /* OP_ADDI/OP_SUBI takes upto 16bits */
goto replace; if (n > INT16_MAX) goto normal;
case OP_LOADI_0: case OP_LOADI_1: case OP_LOADI_2: case OP_LOADI_3:
case OP_LOADI_4: case OP_LOADI_5: case OP_LOADI_6: case OP_LOADI_7:
data.b = data.insn - OP_LOADI_0;
/* fall through */
case OP_LOADI:
case OP_LOADI16:
replace:
s->pc = s->lastpc; s->pc = s->lastpc;
if (op == OP_ADD) { if (op == OP_ADD) {
genop_2(s, OP_ADDI, dst, data.b); genop_2(s, OP_ADDI, dst, (uint16_t)n);
} }
else { else {
genop_2(s, OP_SUBI, dst, data.b); genop_2(s, OP_SUBI, dst, (uint16_t)n);
}
break;
default:
goto normal;
} }
} }
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment