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

Permit recursive read of type store and gv store #35

Merged
merged 1 commit into from
Sep 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
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
Loading