Skip to content

Commit

Permalink
Use MonoVec for FuncInfo.
Browse files Browse the repository at this point in the history
  • Loading branch information
sisshiki1969 committed Dec 22, 2023
1 parent dcdf8f0 commit 0e53b4d
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 29 deletions.
6 changes: 3 additions & 3 deletions monoruby/src/compiler/jitgen/asmir/method_call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,12 +224,12 @@ impl Codegen {
movq rdx, [rdi + (RVALUE_OFFSET_VAR as i32)];
testq rdx, rdx;
jz exit;
movq rdi, [rdx + (IVAR_TABLE_CAPA)]; // capa
movq rdi, [rdx + (MONOVEC_CAPA)]; // capa
testq rdi, rdi;
jz exit;
movq rdi, [rdx + (IVAR_TABLE_LEN)]; // len
movq rdi, [rdx + (MONOVEC_LEN)]; // len
cmpq rdi, rsi;
movq rdi, [rdx + (IVAR_TABLE_PTR)]; // ptr
movq rdi, [rdx + (MONOVEC_PTR)]; // ptr
cmovgtq rax, [rdi + rsi * 8];
exit:
);
Expand Down
12 changes: 6 additions & 6 deletions monoruby/src/compiler/jitgen/asmir/variables.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,12 @@ impl Codegen {
movq rsi, [rdi + (RVALUE_OFFSET_VAR as i32)];
testq rsi, rsi;
jz exit;
movq rdi, [rsi + (IVAR_TABLE_CAPA)]; // capa
movq rdi, [rsi + (MONOVEC_CAPA)]; // capa
testq rdi, rdi;
jz exit;
movq rdi, [rsi + (IVAR_TABLE_LEN)]; // len
movq rdi, [rsi + (MONOVEC_LEN)]; // len
cmpq rdi, (idx);
movq rdi, [rsi + (IVAR_TABLE_PTR)]; // ptr
movq rdi, [rsi + (MONOVEC_PTR)]; // ptr
cmovgtq rax, [rdi + (idx * 8)];
exit:
);
Expand Down Expand Up @@ -181,15 +181,15 @@ impl Codegen {
movq rsi, [rdi + (RVALUE_OFFSET_VAR as i32)];
testq rsi, rsi;
jz generic;
movq rdi, [rsi + (IVAR_TABLE_CAPA)]; // capa
movq rdi, [rsi + (MONOVEC_CAPA)]; // capa
testq rdi, rdi;
jz generic;
movq rdi, [rsi + (IVAR_TABLE_LEN)]; // len
movq rdi, [rsi + (MONOVEC_LEN)]; // len
cmpq rdi, (idx);
jle generic;
}
monoasm! { &mut self.jit,
movq rdi, [rsi + (IVAR_TABLE_PTR)]; // ptr
movq rdi, [rsi + (MONOVEC_PTR)]; // ptr
movq [rdi + (idx * 8)], rax;
exit:
}
Expand Down
3 changes: 2 additions & 1 deletion monoruby/src/globals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ impl MethodTableEntry {
}
}

pub const GLOBALS_FUNCINFO: usize = std::mem::offset_of!(Globals, store.functions.info) + 8/* offset_of!(Vec, ptr) */;
pub const GLOBALS_FUNCINFO: usize =
std::mem::offset_of!(Globals, store.functions.info) + MONOVEC_PTR;

///
/// Global state.
Expand Down
5 changes: 3 additions & 2 deletions monoruby/src/globals/store/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ mod test {
}

pub(crate) struct Funcs {
pub info: Vec<FuncInfo>,
pub info: MonoVec<FuncInfo>,
compile_info: Vec<CompileInfo>,
}

Expand All @@ -228,7 +228,8 @@ impl std::ops::IndexMut<FuncId> for Funcs {

impl std::default::Default for Funcs {
fn default() -> Self {
let info = vec![FuncInfo::default()];
let mut info = MonoVec::with_capacity(256);
info.push(FuncInfo::default());
Self {
info,
compile_info: vec![],
Expand Down
4 changes: 2 additions & 2 deletions monoruby/src/value/rvalue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ pub struct RValue {
header: Header,
/// instance variable table. 8 bytes
#[allow(clippy::box_collection)]
var_table: Option<Box<IvarTable<Option<Value>>>>,
var_table: Option<Box<MonoVec<Option<Value>>>>,
/// object data. 48 bytes.
pub kind: ObjKind,
}
Expand Down Expand Up @@ -320,7 +320,7 @@ impl RValue {
v[i] = Some(val);
}
None => {
let mut v = IvarTable::with_capacity(i + 1);
let mut v = MonoVec::with_capacity(i + 1);
v.resize(i + 1);
v[i] = Some(val);
self.var_table = Some(Box::new(v));
Expand Down
47 changes: 32 additions & 15 deletions monoruby/src/value/rvalue/ivar_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ use std::slice;

//type T = Option<Value>;

pub const IVAR_TABLE_PTR: usize = std::mem::offset_of!(IvarTable<Option<Value>>, buf.ptr);
pub const IVAR_TABLE_CAPA: usize = std::mem::offset_of!(IvarTable<Option<Value>>, buf.cap);
pub const IVAR_TABLE_LEN: usize = std::mem::offset_of!(IvarTable<Option<Value>>, len);
pub const MONOVEC_PTR: usize = std::mem::offset_of!(MonoVec<Option<Value>>, buf.ptr);
pub const MONOVEC_CAPA: usize = std::mem::offset_of!(MonoVec<Option<Value>>, buf.cap);
pub const MONOVEC_LEN: usize = std::mem::offset_of!(MonoVec<Option<Value>>, len);

///
/// A table of instant variables in the field `ivar_table` of RValue.
Expand All @@ -20,28 +20,28 @@ pub const IVAR_TABLE_LEN: usize = std::mem::offset_of!(IvarTable<Option<Value>>,
///
#[derive(Clone)]
#[repr(C)]
pub struct IvarTable<T> {
buf: RawTable<T>,
pub struct MonoVec<T> {
buf: RawVec<T>,
len: usize,
}

impl<T> Deref for IvarTable<T> {
impl<T> Deref for MonoVec<T> {
type Target = [T];
fn deref(&self) -> &[T] {
unsafe { slice::from_raw_parts(self.ptr(), self.len) }
}
}

impl<T> DerefMut for IvarTable<T> {
impl<T> DerefMut for MonoVec<T> {
fn deref_mut(&mut self) -> &mut [T] {
unsafe { slice::from_raw_parts_mut(self.ptr(), self.len) }
}
}

impl<T> IvarTable<T> {
impl<T> MonoVec<T> {
pub fn with_capacity(capacity: usize) -> Self {
Self {
buf: RawTable::with_capacity(capacity),
buf: RawVec::with_capacity(capacity),
len: 0,
}
}
Expand All @@ -50,6 +50,19 @@ impl<T> IvarTable<T> {
self.buf.ptr.as_ptr()
}

pub fn push(&mut self, value: T) {
// This will panic or abort if we would allocate > isize::MAX bytes
// or if the length increment would overflow for zero-sized types.
if self.len == self.buf.capacity() {
self.buf.grow(self.len + 1);
}
unsafe {
let end = self.as_mut_ptr().add(self.len);
ptr::write(end, value);
self.len += 1;
}
}

pub fn reserve(&mut self, additional: usize) {
self.buf.reserve(self.len, additional);
}
Expand Down Expand Up @@ -87,7 +100,7 @@ impl<T> IvarTable<T> {
}

pub struct IvarTableIntoIter<T> {
_buf: RawTable<T>, // we don't actually care about this. Just need it to live.
_buf: RawVec<T>, // we don't actually care about this. Just need it to live.
iter: RawIter<T>,
}

Expand All @@ -110,12 +123,12 @@ impl<T> Drop for IvarTableIntoIter<T> {
}

#[repr(C)]
struct RawTable<T> {
struct RawVec<T> {
ptr: std::ptr::NonNull<T>,
cap: usize,
}

impl<T> Clone for RawTable<T> {
impl<T> Clone for RawVec<T> {
fn clone(&self) -> Self {
unsafe {
let ptr = alloc(self.cap);
Expand All @@ -128,7 +141,7 @@ impl<T> Clone for RawTable<T> {
}
}

impl<T> Drop for RawTable<T> {
impl<T> Drop for RawVec<T> {
fn drop(&mut self) {
let elem_size = mem::size_of::<T>();
if self.cap != 0 {
Expand All @@ -142,7 +155,7 @@ impl<T> Drop for RawTable<T> {
}
}

impl<T> RawTable<T> {
impl<T> RawVec<T> {
/*fn new() -> Self {
Self::with_capacity(0)
}*/
Expand All @@ -158,7 +171,11 @@ impl<T> RawTable<T> {
let ptr = alloc(cap).into();
(ptr, cap)
};
RawTable { ptr, cap }
RawVec { ptr, cap }
}

fn capacity(&self) -> usize {
self.cap
}

fn reserve(&mut self, len: usize, additional: usize) {
Expand Down

0 comments on commit 0e53b4d

Please sign in to comment.