-
Notifications
You must be signed in to change notification settings - Fork 4
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
Refactor: refactor + tests for opcode enum #13
Conversation
Here is another Idea I quickly stitched together; we should also get @florianhartung feedback on that as he dismissed this idea earlier, IIRC. mod opcodes {
const NOP: u8 = 0x01;
const End: u8 = 0x0B;
const LocalGet: u8 = 0x20;
const LocalSet: u8 = 0x21;
const GlobalGet: u8 = 0x23;
const GlobalSet: u8 = 0x24;
const I32Load: u8 = 0x28;
const I32Store: u8 = 0x36;
const I32Const: u8 = 0x41;
const I32Add: u8 = 0x6A;
const I32Mul: u8 = 0x6C;
const FCInstructions: u8 = 0xF;
}
mod fc_opcodes {
const I32TruncSatF32s: u8 = 0x00;
}
struct Interpreter<'a> {
bytecode: &'a [u8],
pc: usize,
stack: Vec<u8>,
}
impl<'a> Interpreter<'a> {
fn run(&mut self) -> Result<(), Box<dyn std::error::Error>> {
use fc_opcodes::*;
use opcodes::*;
let Self {
pc,
bytecode,
stack,
} = self;
match &bytecode[*pc..] {
[NOP, ..] => {
*pc += 1;
}
[LocalSet, x, ..] => {
stack.push(*x);
*pc += 2;
}
[FCInstructions, I32TruncSatF32s, ..] => {
// do some work
*pc += 2;
}
_ => {
todo!("something left to be implemented");
}
}
Ok(())
}
} |
Interesting way of doing this, I'd refactor it this way if Florian and Alex agree. |
Looks clean to me what you propose @wucke13, the only downside that I can find would be the refactoring of the code for this. |
Thank you for the comments! There is two downsides to my way, it contains a variable amount of decisions (possibly hindering optimization by the Compiler), and the const stuff is rather elaborate. Let's wait for @florianhartung feedback before we continue this approach. |
d26670a
to
c083b95
Compare
cae7140
to
34afe90
Compare
34afe90
to
8251998
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Pull Request Overview
This pull request refactors so that
an enum is used for opcodes instead of their raw byte representationwe use constant values in order to improve the readability of the code. This pull request also adds test cases for the above implementation.Also, the usage ofpanic
is correct, as we would exit out at the validation point when an instruction we haven't implemented yet is encountered. It is also used for correctly assessing the outputs of the tests.Testing Strategy
This pull request was tested locally using the test cases provided.
TODO or Help Wanted
This pull request still needs an opinion if it is the right approach.
Formatting
cargo fmt
cargo check
cargo build
Github Issue
N/A
Author
Signed-off-by: Popescu Adrian adrian.popescu@oxidos.io