Skip to content

Commit

Permalink
Moved shared block info behind a reference-count
Browse files Browse the repository at this point in the history
  • Loading branch information
Hirevo committed Feb 2, 2023
1 parent a2c63fb commit 040696b
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 21 deletions.
17 changes: 11 additions & 6 deletions som-interpreter-bc/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,21 @@ use crate::universe::Universe;
use crate::value::Value;
use crate::SOMRef;

/// Represents an executable block.
#[derive(Clone)]
pub struct Block {
/// Reference to the captured stack frame.
pub frame: Option<SOMRef<Frame>>,
pub struct BlockInfo {
pub locals: Vec<Value>,
pub literals: Vec<Literal>,
pub body: Vec<Bytecode>,
pub nb_params: usize,
pub inline_cache: Rc<RefCell<Vec<Option<(*const Class, Rc<Method>)>>>>,
pub inline_cache: RefCell<Vec<Option<(*const Class, Rc<Method>)>>>,
}

/// Represents an executable block.
#[derive(Clone)]
pub struct Block {
/// Reference to the captured stack frame.
pub frame: Option<SOMRef<Frame>>,
pub blk_info: Rc<BlockInfo>,
}

impl Block {
Expand All @@ -37,7 +42,7 @@ impl Block {

/// Retrieve the number of parameters this block accepts.
pub fn nb_parameters(&self) -> usize {
self.nb_params
self.blk_info.nb_params
}
}

Expand Down
16 changes: 9 additions & 7 deletions som-interpreter-bc/src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use num_bigint::BigInt;
use som_core::ast;
use som_core::bytecode::Bytecode;

use crate::block::Block;
use crate::block::{Block, BlockInfo};
use crate::class::{Class, MaybeWeak};
use crate::interner::{Interned, Interner};
use crate::method::{Method, MethodEnv, MethodKind};
Expand Down Expand Up @@ -459,15 +459,17 @@ fn compile_block(outer: &mut dyn GenCtxt, defn: &ast::Block) -> Option<Block> {
let literals = ctxt.literals.into_iter().collect();
let body = ctxt.body.unwrap_or_default();
let nb_params = ctxt.args.len();
let inline_cache = Rc::new(RefCell::new(vec![None; body.len()]));
let inline_cache = RefCell::new(vec![None; body.len()]);

let block = Block {
frame,
locals,
literals,
body,
nb_params,
inline_cache,
blk_info: Rc::new(BlockInfo {
locals,
literals,
body,
nb_params,
inline_cache,
}),
};

// println!("(system) compiled block !");
Expand Down
6 changes: 3 additions & 3 deletions som-interpreter-bc/src/frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ impl Frame {
pub fn from_kind(kind: FrameKind) -> Self {
match &kind {
FrameKind::Block { block } => {
let locals = block.locals.iter().map(|_| Value::Nil).collect();
let locals = block.blk_info.locals.iter().map(|_| Value::Nil).collect();
Self {
kind,
locals,
Expand Down Expand Up @@ -113,7 +113,7 @@ impl Frame {
MethodKind::Primitive(_) => None,
MethodKind::NotImplemented(_) => None,
},
FrameKind::Block { block, .. } => block.body.get(idx).copied(),
FrameKind::Block { block, .. } => block.blk_info.body.get(idx).copied(),
}
}

Expand All @@ -124,7 +124,7 @@ impl Frame {

pub fn lookup_constant(&self, idx: usize) -> Option<Literal> {
match self.kind() {
FrameKind::Block { block } => block.literals.get(idx).cloned(),
FrameKind::Block { block } => block.blk_info.literals.get(idx).cloned(),
FrameKind::Method { method, .. } => match method.kind() {
MethodKind::Defined(env) => env.literals.get(idx).cloned(),
MethodKind::Primitive(_) => None,
Expand Down
8 changes: 4 additions & 4 deletions som-interpreter-bc/src/hashcode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,10 @@ impl Hash for Instance {

impl Hash for Block {
fn hash<H: Hasher>(&self, hasher: &mut H) {
self.literals.iter().for_each(|it| it.hash(hasher));
self.locals.iter().for_each(|it| it.hash(hasher));
self.nb_params.hash(hasher);
self.body.iter().for_each(|it| it.hash(hasher));
self.blk_info.literals.iter().for_each(|it| it.hash(hasher));
self.blk_info.locals.iter().for_each(|it| it.hash(hasher));
self.blk_info.nb_params.hash(hasher);
self.blk_info.body.iter().for_each(|it| it.hash(hasher));
}
}

Expand Down
2 changes: 1 addition & 1 deletion som-interpreter-bc/src/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ impl Interpreter {
) -> Option<Rc<Method>> {
match frame.borrow().kind() {
FrameKind::Block { block } => {
let mut inline_cache = block.inline_cache.borrow_mut();
let mut inline_cache = block.blk_info.inline_cache.borrow_mut();

// SAFETY: this access is actually safe because the bytecode compiler
// makes sure the cache has as many entries as there are bytecode instructions,
Expand Down

0 comments on commit 040696b

Please sign in to comment.