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

Refactor: refactor + tests for opcode enum #13

Merged
merged 1 commit into from
Jul 16, 2024
Merged

Conversation

nerodesu017
Copy link
Collaborator

@nerodesu017 nerodesu017 commented Jun 27, 2024

Pull Request Overview

This pull request refactors so that an enum is used for opcodes instead of their raw byte representation we 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 of panic 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

  • Ran cargo fmt
  • Ran cargo check
  • Ran cargo build

Github Issue

N/A

Author

Signed-off-by: Popescu Adrian adrian.popescu@oxidos.io

src/execution/mod.rs Outdated Show resolved Hide resolved
@wucke13
Copy link
Collaborator

wucke13 commented Jun 27, 2024

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(())
    }
}

@nerodesu017
Copy link
Collaborator Author

Interesting way of doing this, I'd refactor it this way if Florian and Alex agree.

@valexandru
Copy link
Collaborator

valexandru commented Jun 28, 2024

Looks clean to me what you propose @wucke13, the only downside that I can find would be the refactoring of the code for this.

@wucke13
Copy link
Collaborator

wucke13 commented Jun 28, 2024

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.

@nerodesu017 nerodesu017 force-pushed the refactor/opcode_enum branch 2 times, most recently from d26670a to c083b95 Compare July 11, 2024 09:09
src/execution/mod.rs Outdated Show resolved Hide resolved
src/validation/code.rs Outdated Show resolved Hide resolved
@nerodesu017 nerodesu017 force-pushed the refactor/opcode_enum branch 2 times, most recently from cae7140 to 34afe90 Compare July 15, 2024 08:51
@nerodesu017 nerodesu017 force-pushed the refactor/opcode_enum branch from 34afe90 to 8251998 Compare July 15, 2024 13:59
Copy link
Collaborator

@florianhartung florianhartung left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@nerodesu017 nerodesu017 added this pull request to the merge queue Jul 16, 2024
Merged via the queue into main with commit 761bd78 Jul 16, 2024
4 checks passed
@nerodesu017 nerodesu017 deleted the refactor/opcode_enum branch December 16, 2024 09:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants