bm
bm copied to clipboard
Instruction handler mapping complexity
Currently, mapping instructions to handlers is done using a switch statement. To my knowledge, because it's basically nested if statements, this will produce an O(log n) complexity mapping at best. You can use an array containing pointers to handlers, using instructions as indices, to achieve O(1) complexity.
I understand this might be beyond the scope of this project, but I still wanted to point this out because of the educational value.
typedef enum
{
INST_NOP = 0,
// ...
} Inst;
Err handle_nop(Bm *bm)
{
bm->ip++;
return ERR_OK;
}
// ...
Err (*inst_map[])(Bm *) = {
handle_nop,
// ...
};
Err bm_execute_inst(Bm *bm)
{
// ...
return inst_map[bm->inst.type](bm);
}