Skip to content

Commit

Permalink
Make Refactor not store the runtime.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivorforce committed Jul 16, 2024
1 parent 95012d5 commit 31c73fe
Show file tree
Hide file tree
Showing 11 changed files with 31 additions and 29 deletions.
2 changes: 1 addition & 1 deletion src/interpreter.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
pub mod compiler;
pub mod vm;
pub mod run;
pub mod chunks;
Expand All @@ -9,3 +8,4 @@ pub mod data;
pub mod runtime;
mod tests;
mod data_layout;
mod compile;
2 changes: 1 addition & 1 deletion src/interpreter/builtins/vm.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::rc::Rc;
use std::path::PathBuf;
use crate::error::RResult;
use crate::interpreter::compiler::InlineFunction;
use crate::interpreter::compile::function_compiler::InlineFunction;
use crate::interpreter::opcode::{OpCode, Primitive};
use crate::interpreter::runtime::Runtime;
use crate::program;
Expand Down
2 changes: 2 additions & 0 deletions src/interpreter/compile.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod function_compiler;
mod compile_server;
3 changes: 3 additions & 0 deletions src/interpreter/compile/compile_server.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub struct CompileServer {

}
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ pub fn compile_deep(runtime: &mut Runtime, function: &Rc<FunctionHead>) -> RResu
return Err(RuntimeError::error("main! function was somehow internal.").to_array());
};

let mut refactor = Refactor::new(runtime);
let mut refactor = Refactor::new();
refactor.add(implementation);

let mut simplify = Simplify::new(&mut refactor, &transpiler::Config::default());
simplify.run();
simplify.run(runtime);

let needed_functions = refactor.gather_needed_functions();
let needed_functions = refactor.gather_needed_functions(runtime);
let fn_logic = refactor.fn_logic;

let mut errors = vec![];
Expand Down
2 changes: 1 addition & 1 deletion src/interpreter/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::rc::Rc;
use uuid::Uuid;

use crate::error::{RResult, RuntimeError};
use crate::interpreter::compiler::compile_deep;
use crate::interpreter::compile::function_compiler::compile_deep;
use crate::interpreter::data::Value;
use crate::interpreter::runtime::Runtime;
use crate::interpreter::vm::VM;
Expand Down
2 changes: 1 addition & 1 deletion src/interpreter/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::{ast, parser, program, resolver};
use crate::error::{RResult, RuntimeError};
use crate::interpreter::builtins;
use crate::interpreter::chunks::Chunk;
use crate::interpreter::compiler::InlineFunction;
use crate::interpreter::compile::function_compiler::InlineFunction;
use crate::interpreter::data_layout::{create_data_layout, DataLayout};
use crate::program::functions::FunctionHead;
use crate::program::module::{Module, module_name, ModuleName};
Expand Down
2 changes: 1 addition & 1 deletion src/interpreter/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ mod tests {
use crate::error::RResult;
use crate::interpreter;
use crate::interpreter::chunks::Chunk;
use crate::interpreter::compiler::compile_deep;
use crate::interpreter::compile::function_compiler::compile_deep;
use crate::interpreter::data::Value;
use crate::interpreter::opcode::{OpCode, Primitive};
use crate::interpreter::runtime::Runtime;
Expand Down
17 changes: 7 additions & 10 deletions src/refactor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ pub mod locals;
pub mod analyze;
pub mod call_graph;

pub struct Refactor<'a> {
pub runtime: &'a mut Runtime,

pub struct Refactor {
pub explicit_functions: Vec<Rc<FunctionHead>>,
pub invented_functions: HashSet<Rc<FunctionHead>>,

Expand All @@ -31,10 +29,9 @@ pub struct Refactor<'a> {
pub call_graph: CallGraph,
}

impl<'a> Refactor<'a> {
pub fn new(runtime: &'a mut Runtime) -> Refactor<'a> {
impl Refactor {
pub fn new() -> Refactor {
Refactor {
runtime,
explicit_functions: vec![],
invented_functions: HashSet::new(),
fn_logic: Default::default(),
Expand Down Expand Up @@ -122,12 +119,12 @@ impl<'a> Refactor<'a> {
}
}

pub fn try_monomorphize(&mut self, binding: &Rc<FunctionBinding>) -> Option<Rc<FunctionHead>> {
pub fn try_monomorphize(&mut self, binding: &Rc<FunctionBinding>, runtime: &mut Runtime) -> Option<Rc<FunctionHead>> {
if self.fn_optimizations.contains_key(binding) {
return None // We already have an optimization; we need not monomorphize.
}

let Some(logic) = self.fn_logic.get(&binding.function).or_else(|| self.runtime.source.fn_logic.get(&binding.function)) else {
let Some(logic) = self.fn_logic.get(&binding.function).or_else(|| runtime.source.fn_logic.get(&binding.function)) else {
panic!("Cannot find logic for function {:?}", binding.function);
};

Expand Down Expand Up @@ -197,11 +194,11 @@ impl<'a> Refactor<'a> {
}
}

pub fn gather_needed_functions(&mut self) -> LinkedHashSet<Rc<FunctionHead>> {
pub fn gather_needed_functions(&mut self, runtime: &mut Runtime) -> LinkedHashSet<Rc<FunctionHead>> {
let callees = self.call_graph.deep_callees(self.explicit_functions.iter());
for callee in callees.iter() {
if !self.fn_logic.contains_key(callee) {
self.fn_logic.insert(Rc::clone(callee), self.runtime.source.fn_logic[callee].clone());
self.fn_logic.insert(Rc::clone(callee), runtime.source.fn_logic[callee].clone());
}
}
callees
Expand Down
16 changes: 8 additions & 8 deletions src/refactor/simplify.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
use std::collections::hash_map::RandomState;

use linked_hash_set::LinkedHashSet;

use crate::interpreter::runtime::Runtime;
use crate::program::functions::FunctionLogic;
use crate::refactor::{locals, Refactor};
use crate::transpiler::Config;

pub struct Simplify<'a, 'b> {
pub refactor: &'a mut Refactor<'b>,
pub struct Simplify<'a> {
pub refactor: &'a mut Refactor,
pub inline: bool,
pub trim_locals: bool,
pub monomorphize: bool,
}

impl<'a, 'b> Simplify<'a, 'b> {
pub fn new(refactor: &'a mut Refactor<'b>, config: &Config) -> Simplify<'a, 'b> {
impl<'a> Simplify<'a> {
pub fn new(refactor: &'a mut Refactor, config: &Config) -> Simplify<'a> {
if !config.should_monomorphize {
todo!(); // Lots of reasons non-monomorphization doesn't work right now.
}
Expand All @@ -27,22 +27,22 @@ impl<'a, 'b> Simplify<'a, 'b> {
}
}

pub fn run(&mut self) {
pub fn run(&mut self, runtime: &mut Runtime) {
if self.monomorphize {
// First, monomorphize everything we call
let mut next: LinkedHashSet<_, RandomState> = LinkedHashSet::from_iter(
self.refactor.explicit_functions.iter()
.flat_map(|head| self.refactor.call_graph.callees[head].iter().cloned())
);
while let Some(current) = next.pop_front() {
if let Some(monomorphized) = self.refactor.try_monomorphize(&current) {
if let Some(monomorphized) = self.refactor.try_monomorphize(&current, runtime) {
next.extend(self.refactor.call_graph.callees.get(&monomorphized).unwrap().iter().cloned());
}
}
}

// Make sure refactor has everything that's needed so we can simplify it.
self.refactor.gather_needed_functions();
self.refactor.gather_needed_functions(runtime);

// Now, let's simplify!
let mut next: LinkedHashSet<_, RandomState> = LinkedHashSet::from_iter(self.refactor.fn_logic.keys().cloned());
Expand Down
6 changes: 3 additions & 3 deletions src/transpiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ pub trait LanguageContext {
}

pub fn transpile(transpiler: Box<Transpiler>, runtime: &mut Runtime, context: &dyn LanguageContext, config: &Config, base_filename: &str) -> RResult<HashMap<String, String>>{
let mut refactor = Refactor::new(runtime);
let mut refactor = Refactor::new();
context.register_builtins(&mut refactor);

for artifact in transpiler.exported_artifacts {
Expand All @@ -74,13 +74,13 @@ pub fn transpile(transpiler: Box<Transpiler>, runtime: &mut Runtime, context: &d
}

let mut simplify = Simplify::new(&mut refactor, config);
simplify.run();
simplify.run(runtime);

// --- Reclaim from Refactor and make the ast
context.refactor_code(&mut refactor);

// TODO The call_graph doesn't know about calls made outside the refactor. If there was no monomorphization, some functions may not even be caught by this.
let deep_calls = refactor.gather_needed_functions();
let deep_calls = refactor.gather_needed_functions(runtime);
let mut fn_logic = refactor.fn_logic;

let exported_functions = refactor.explicit_functions.iter()
Expand Down

0 comments on commit 31c73fe

Please sign in to comment.