Skip to content

Commit

Permalink
Permit recursive read of type store and gv store
Browse files Browse the repository at this point in the history
  • Loading branch information
k0aki committed Sep 30, 2023
1 parent f2f58ca commit 6d7fa0d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 13 deletions.
7 changes: 3 additions & 4 deletions crates/interpreter/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,13 @@ pub fn size_of_ty_data(ctx: &ModuleCtx, ty: Type) -> usize {
Type::I256 => 32,
Type::Compound(cmpd_ty) => {
use CompoundTypeData::*;
let cmpd_ty_data = ctx.with_ty_store(|s| s.resolve_compound(cmpd_ty).clone());
match cmpd_ty_data {
Array { len, elem } => len * size_of_ty_data(ctx, elem),
ctx.with_ty_store(|s| match s.resolve_compound(cmpd_ty) {
Array { len, elem } => len * size_of_ty_data(ctx, *elem),
Ptr(_) => mem::size_of::<usize>(),
Struct(data) => data.fields.iter().fold(0usize, |acc, field_ty| {
acc + size_of_ty_data(ctx, *field_ty)
}),
}
})
}
Type::Void => mem::size_of::<()>(),
}
Expand Down
18 changes: 9 additions & 9 deletions crates/ir/src/module.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{
fmt,
sync::{Arc, Mutex},
sync::{Arc, RwLock},
};

use cranelift_entity::{entity_impl, PrimaryMap};
Expand Down Expand Up @@ -43,46 +43,46 @@ impl Module {
pub struct ModuleCtx {
pub isa: TargetIsa,
// TODO: Consider using `RwLock` instead of `Mutex`.
type_store: Arc<Mutex<TypeStore>>,
type_store: Arc<RwLock<TypeStore>>,
// TODO: Consider using `RwLock` instead of `Mutex`.
gv_store: Arc<Mutex<GlobalVariableStore>>,
gv_store: Arc<RwLock<GlobalVariableStore>>,
}

impl ModuleCtx {
pub fn new(isa: TargetIsa) -> Self {
Self {
isa,
type_store: Arc::new(Mutex::new(TypeStore::default())),
gv_store: Arc::new(Mutex::new(GlobalVariableStore::default())),
type_store: Arc::new(RwLock::new(TypeStore::default())),
gv_store: Arc::new(RwLock::new(GlobalVariableStore::default())),
}
}

pub fn with_ty_store<F, R>(&self, f: F) -> R
where
F: FnOnce(&TypeStore) -> R,
{
f(&self.type_store.lock().unwrap())
f(&self.type_store.read().unwrap())
}

pub fn with_ty_store_mut<F, R>(&self, f: F) -> R
where
F: FnOnce(&mut TypeStore) -> R,
{
f(&mut self.type_store.lock().unwrap())
f(&mut self.type_store.write().unwrap())
}

pub fn with_gv_store<F, R>(&self, f: F) -> R
where
F: FnOnce(&GlobalVariableStore) -> R,
{
f(&self.gv_store.lock().unwrap())
f(&self.gv_store.read().unwrap())
}

pub fn with_gv_store_mut<F, R>(&self, f: F) -> R
where
F: FnOnce(&mut GlobalVariableStore) -> R,
{
f(&mut self.gv_store.lock().unwrap())
f(&mut self.gv_store.write().unwrap())
}
}

Expand Down

0 comments on commit 6d7fa0d

Please sign in to comment.