Skip to content

Commit

Permalink
🎨 Use RegSetImm for switches
Browse files Browse the repository at this point in the history
  • Loading branch information
fennecdjay committed Feb 21, 2019
1 parent a06ee3e commit 3b3eae4
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 14 deletions.
3 changes: 1 addition & 2 deletions examples/dyn_switch.gw
@@ -1,5 +1,4 @@
3 =>
const int i;
3 => const int i;
2 => const int y;
switch(i) {
case 1: <<<1>>>;break;
Expand Down
4 changes: 2 additions & 2 deletions src/emit/emit.c
Expand Up @@ -1130,7 +1130,7 @@ ANN static m_bool emit_switch_instr(const Emitter emit, Instr *instr) {
CHECK_BB(emit_exp(emit, e, 0))
*instr = emit_add_instr(emit, SwitchIni);
} else {
const Instr instr = emit_add_instr(emit, RegPushImm);
const Instr instr = emit_add_instr(emit, RegSetImm);
instr->m_val = (m_uint)switch_map(emit->env);
}
return GW_OK;
Expand All @@ -1146,9 +1146,9 @@ ANN static void emit_switch_map(const Instr instr, const Map map) {
ANN static m_bool emit_stmt_switch(const Emitter emit, const Stmt_Switch stmt) { GWDEBUG_EXE
switch_get(emit->env, stmt);
Instr push = NULL;
CHECK_BB(emit_exp(emit, stmt->val, 0))
CHECK_BB(emit_switch_instr(emit, &push))
vector_add(&emit->code->stack_break, (vtype)NULL);
CHECK_BB(emit_exp(emit, stmt->val, 0))
const Instr instr = emit_add_instr(emit, BranchSwitch);
instr->m_val2 = (m_uint)switch_map(emit->env);
CHECK_BB(emit_stmt(emit, stmt->stmt, 1))
Expand Down
12 changes: 6 additions & 6 deletions src/lib/instr.c
Expand Up @@ -36,16 +36,16 @@ INSTR(SwitchIni) {
const Vector v = (Vector)instr->m_val;
const m_uint size = vector_size(v);
const Map m = (Map)instr->m_val2;
POP_REG(shred, SZ_INT * (size-1));
POP_REG(shred, SZ_INT * (size));
for(m_uint i = 0; i < size; ++i)
map_set(m, *(vtype*)REG((i-1) * SZ_INT), vector_at(v, i));
*(Map*)REG(-SZ_INT) = m;
map_set(m, *(vtype*)REG((i) * SZ_INT), vector_at(v, i));
*(Map*)REG(0) = m;
}

INSTR(BranchSwitch) { GWDEBUG_EXE
POP_REG(shred, SZ_INT*2);
const Map map = *(Map*)REG(0);
shred->pc = map_get(map, *(m_uint*)REG(SZ_INT)) ?: instr->m_val;
POP_REG(shred, SZ_INT);
const Map map = *(Map*)REG(SZ_INT);
shred->pc = map_get(map, *(m_uint*)REG(0)) ?: instr->m_val;
}

INSTR(AutoLoopStart) { GWDEBUG_EXE
Expand Down
6 changes: 2 additions & 4 deletions src/vm/vm.c
Expand Up @@ -532,11 +532,11 @@ firmul: FI_R(*)
firdiv: FI_R(/)

itof:
reg -=SZ_INT - SZ_FLOAT;
reg -= SZ_INT - SZ_FLOAT;
*(m_float*)(reg-SZ_FLOAT) = (m_float)*(m_int*)(reg-SZ_FLOAT);
DISPATCH()
ftoi:
reg -= SZ_FLOAT -SZ_INT;
reg -= SZ_FLOAT - SZ_INT;
*(m_int*)(reg-SZ_INT) = (m_int)*(m_float*)(reg-SZ_INT);
DISPATCH()

Expand Down Expand Up @@ -756,11 +756,9 @@ DISPATCH();
DISPATCH()
dotfunc:
assert(a.obj);
// *(VM_Code*)(reg) = ((Func)vector_at(&a.obj->type_ref->nspc->vtable, instr->m_val))->code;
*(VM_Code*)(reg) = ((Func)vector_at(a.obj->vtable, instr->m_val))->code;
reg += SZ_INT;
DISPATCH()
//dottemplate:
staticcode:
(*(VM_Code*)reg = ((Func)instr->m_val)->code);
reg += SZ_INT;
Expand Down

0 comments on commit 3b3eae4

Please sign in to comment.