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

Optimize instruction dispatch #7

Closed
jprochazk opened this issue Feb 20, 2023 · 1 comment
Closed

Optimize instruction dispatch #7

jprochazk opened this issue Feb 20, 2023 · 1 comment

Comments

@jprochazk
Copy link
Owner

jprochazk commented Feb 20, 2023

Currently, the VM uses a simple dispatch loop, which boils down to:

match next_opcode() {
  op::LoadReg => vm.op_load_reg(decode_args::<LoadReg>()),
  op::StoreReg => vm.op_store_reg(decode_args::<StoreReg>()),
  // ... etc.
}

It could be replaced by a less portable direct-threaded dispatch written in architecture-specific assembly for a few platforms (most likely only x86_64 and arm64), similar to this one (note that this code is heavily outdated, as it used the super unstable LLVM assembly macro, and asm has since been stabilised). The concept boils down to:

// create a jump table where `jump_table[opcode]` is the label address of the dispatch handler for that opcode
let jump_table = [label_addr!("dispatch_load_reg"), label_addr!("dispatch_store_reg"), ...];

// setup `pc` and `op` registers, jump to the first instruction
trampoline!(vm, pc, op, jump_table);

// all opcodes would be defined similar to this:
dispatch_handler!(load_reg, (vm, pc, op, jump_table) {
  // call VM's opcode handler
  vm.op_load_reg(decode_args::<LoadReg>());
  // load + dispatch next instruction
  dispatch!(vm, pc, op, jump_table);
})
@jprochazk jprochazk added this to Mu v1 Feb 20, 2023
@jprochazk
Copy link
Owner Author

This is unfortunately not better than a switch in most cases https://github.com/jprochazk/vm-perf

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
No open projects
Status: No status
Development

No branches or pull requests

1 participant