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

o1vm/riscv32i: define registers #2743

Merged
merged 3 commits into from
Nov 14, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 41 additions & 4 deletions o1vm/src/interpreters/riscv32i/registers.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
use std::ops::{Index, IndexMut};

use serde::{Deserialize, Serialize};

pub const N_GP_REGISTERS: usize = 32;
// FIXME:
pub const REGISTER_CURRENT_IP: usize = 33;
pub const REGISTER_NEXT_IP: usize = 34;
pub const REGISTER_HEAP_POINTER: usize = 35;
pub const REGISTER_CURRENT_IP: usize = N_GP_REGISTERS + 1;
pub const REGISTER_NEXT_IP: usize = N_GP_REGISTERS + 2;
pub const REGISTER_HEAP_POINTER: usize = N_GP_REGISTERS + 3;

/// This represents the internal state of the virtual machine.
#[derive(Clone, Default, Debug, Serialize, Deserialize)]
Expand All @@ -22,8 +25,42 @@ pub struct Registers<T> {
/// - x12-x17: function arguments
/// - x18-x27: saved registers
/// - x28-x31: temporaries
pub general_purpose: [T; 32],
pub general_purpose: [T; N_GP_REGISTERS],
pub current_instruction_pointer: T,
pub next_instruction_pointer: T,
pub heap_pointer: T,
}

impl<T: Clone> Index<usize> for Registers<T> {
type Output = T;

fn index(&self, index: usize) -> &Self::Output {
if index < N_GP_REGISTERS {
&self.general_purpose[index]
} else if index == REGISTER_CURRENT_IP {
&self.current_instruction_pointer
} else if index == REGISTER_NEXT_IP {
&self.next_instruction_pointer
} else if index == REGISTER_HEAP_POINTER {
&self.heap_pointer
} else {
panic!("Index out of bounds");
}
Comment on lines +38 to +48
Copy link
Member

Choose a reason for hiding this comment

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

nbd since ip, nip, and the heap ptr are artificial, but it is a bit awkward that 32 isn't an index in the range.

}
}

impl<T: Clone> IndexMut<usize> for Registers<T> {
fn index_mut(&mut self, index: usize) -> &mut Self::Output {
if index < N_GP_REGISTERS {
&mut self.general_purpose[index]
} else if index == REGISTER_CURRENT_IP {
&mut self.current_instruction_pointer
} else if index == REGISTER_NEXT_IP {
&mut self.next_instruction_pointer
} else if index == REGISTER_HEAP_POINTER {
&mut self.heap_pointer
} else {
panic!("Index out of bounds");
}
}
}
Loading