Permalink
Browse files

[ir] commit last stage of IR before extracting this code into a branch.

  • Loading branch information...
ptal committed Sep 22, 2018
1 parent 684f9c9 commit dec96f5f7239235994935387cb04103e7ed79d41
@@ -9,7 +9,7 @@ See [README.md](README.md)
## Installing the bonsai compiler
```sh
rustup override set nightly-2018-04-18
rustup override set nightly-2018-08-18
cargo install
```
@@ -2,7 +2,7 @@
import platform
import subprocess
rust_nightly_version = "nightly-2018-04-18"
rust_nightly_version = "nightly-2018-08-18"
sugarcubes_jar = "/tmp/SugarCubesv4.0.0a5.jar"
bonsai_runtime_jar = "target/runtime-1.0-SNAPSHOT.jar"
bonsai_runtime_src = "runtime/"
@@ -192,9 +192,9 @@ impl SymbolicExecution
let (session, data) = env.decompose();
fake = fake || data.is_fake();
match data {
Partial::Value((context, schedule_paths))
| Partial::Fake((context, schedule_paths)) => {
instant.schedule_paths = schedule_paths;
Partial::Value((context, path_schedules))
| Partial::Fake((context, path_schedules)) => {
instant.path_schedules = path_schedules;
self.all_instants.push(instant);
self.context = context;
self.session = session;
@@ -17,16 +17,59 @@ use session::*;
use middle::causality::symbolic_execution::State;
use middle::ir::guarded_command::*;
use middle::ir::scheduling::*;
use std::cmp;
use std::collections::HashMap;
trait Continuation {
fn call(&self, this: &Compiler, guard: Box<Expr>) -> Vec<GuardedProgram>;
fn bclone(&self) -> Box<Continuation>;
}
type Cont = Box<Continuation>;
#[derive(Clone)]
struct IdentityCont;
impl Continuation for IdentityCont {
fn call(&self, _this: &Compiler, guard: Box<Expr>) -> Vec<GuardedProgram> {
// vec![model]
vec![]
}
fn bclone(&self) -> Cont { Box::new(self.clone()) }
}
struct SequenceCont {
children: Vec<Stmt>,
continuation: Cont
}
impl SequenceCont {
pub fn new(children: Vec<Stmt>, continuation: Cont) -> Self {
SequenceCont { children, continuation }
}
}
impl Continuation for SequenceCont {
fn call(&self, this: &Compiler, guard: Box<Expr>) -> Vec<GuardedProgram> {
// this.visit_seq(self.children.clone(), model, self.continuation.bclone())
vec![]
}
fn bclone(&self) -> Cont {
Box::new(SequenceCont::new(self.children.clone(), self.continuation.bclone()))
}
}
pub type AllInstants = HashMap<ProcessUID, Vec<Instant>>;
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Instant {
pub locations: State,
pub program: Stmt,
pub schedule_paths: Vec<Scheduling>,
pub path_schedules: Vec<Scheduling>,
}
impl Instant
@@ -35,47 +78,185 @@ impl Instant
Self::new(locations, program, vec![])
}
pub fn new(locations: State, program: Stmt, schedule_paths: Vec<Scheduling>) -> Self {
Instant { locations, program, schedule_paths }
pub fn new(locations: State, program: Stmt, path_schedules: Vec<Scheduling>) -> Self {
Instant { locations, program, path_schedules }
}
}
pub struct Compiler {
session: Session,
context: Context,
instants: AllInstants,
factory: GuardedCommandFactory,
ir: IR,
location_names: Vec<Ident>,
}
impl Compiler {
pub fn new(session: Session, context: Context, instants: AllInstants) -> Self {
pub fn new(session: Session, context: Context, instants: &AllInstants) -> Self {
let factory = GuardedCommandFactory::new(&instants);
Compiler {
session, context, instants,
session, context,
ir: IR::new(),
location_names: vec![]
factory
}
}
pub fn compile(mut self) -> Env<(Context, IR)> {
// self.initialize_locations();
// let instants = self.instants;
// self.instants = vec![];
// for instant in instants {
// self.compile_instant(instant);
// }
pub fn compile(mut self, instants: AllInstants) -> Env<(Context, IR)> {
for instants_per_process in instants {
self.compile_process_instants(instants_per_process);
}
Env::value(self.session, (self.context, self.ir))
}
// fn initialize_locations(&mut self) {
// let max_loc = self.instants.iter()
// .flat_map(|i| i.locations.iter())
// .fold(0, cmp::max);
// for loc in 0..max_loc+1
// self.ir.
fn compile_process_instants(&mut self, (process, instants): (ProcessUID, Vec<Instant>)) {
for instant in instants {
let execution_paths = self.compile_instant(instant.locations, instant.program);
let program = self.schedule_paths(execution_paths, instant.path_schedules);
self.ir.processes.insert(process.clone(), program);
}
}
fn compile_instant(&mut self, locations: State, program: Stmt) -> Vec<GuardedProgram> {
let guard = self.factory.create_locations_guard(locations);
self.compile_stmt(program, guard, Box::new(IdentityCont))
}
fn compile_stmt(&self, program: Stmt, guard: Box<Expr>,
continuation: Cont) -> Vec<GuardedProgram>
{
// use ast::StmtKind::*;
match program.node {
// DelayStmt(delay) => self.compile_delay(delay, guard, continuation),
// Space(p) => self.compile_space(p, guard, continuation),
// Prune => self.compile_prune(guard, continuation),
// LocalDrop(binding) => self.compile_local_drop(binding, guard, continuation),
// Nothing => continuation.call(self, guard),
// Seq(branches) => self.compile_seq(branches, guard, continuation),
// Let(stmt) => self.compile_let(stmt, guard, continuation),
// Tell(var, expr) => self.compile_tell(var, expr, guard, continuation),
// When(cond, then_branch, else_branch) =>
// self.compile_when(cond, *then_branch, *else_branch, guard, continuation),
// ExprStmt(expr) => self.compile_expr_stmt(expr, guard, continuation),
// OrPar(branches) => self.compile_or_par(branches, guard, continuation),
// AndPar(branches) => self.compile_and_par(branches, guard, continuation),
_ => vec![]
// Suspend(cond, body) => self.compile_suspend(cond, *body, guard, continuation),
// Abort(cond, body) => self.compile_abort(cond, *body, guard, continuation),
// Loop(body) => self.compile_loop(*body),
// ProcCall(var, process, args) => self.compile_proc_call(var, process, args),
// Universe(body) => self.compile_universe(*body),
}
}
// fn compile_delay(&self, delay: Delay, guard: Box<Expr>,
// _continuation: Cont) -> Vec<GuardedProgram>
// {
// let delay_command = self.factory.create_delay(delay, guard);
// vec![vec![delay_command]]
// }
// fn compile_seq(&self, mut children: Vec<Stmt>,
// model: CausalModel, continuation: Cont) -> Vec<CausalModel>
// {
// match children.len() {
// 0 => continuation.call(self, model),
// 1 => self.compile_stmt(children.remove(0), model, continuation),
// _ => {
// let stmt = children.remove(0);
// self.compile_stmt(stmt, model,
// Box::new(SequenceCont::new(children, continuation)))
// }
// }
// }
// fn compile_let(&self, let_stmt: LetStmt,
// model: CausalModel, continuation: Cont) -> Vec<CausalModel>
// {
// let model = match let_stmt.binding.expr {
// None => model,
// Some(expr) => self.deps.compile_expr(expr, None, model)
// };
// self.compile_stmt(*(let_stmt.body), model, continuation)
// }
// fn compile_tell(&self, var: Variable, expr: Expr,
// model: CausalModel, continuation: Cont) -> Vec<CausalModel>
// {
// let m1 = self.deps.compile_expr(expr, None, model);
// let m2 = self.deps.compile_var(var, None, m1);
// continuation.call(self, m2)
// }
// fn compile_when(&self, condition: Expr, then_branch: Stmt, else_branch: Stmt,
// model: CausalModel, continuation: Cont) -> Vec<CausalModel>
// {
// let then_m = self.deps.compile_expr(condition.clone(), Some(true), model.clone());
// let mut m1 = self.compile_stmt(then_branch, then_m, continuation.bclone());
// let else_m = self.deps.compile_expr(condition, Some(false), model);
// let mut m2 = self.compile_stmt(else_branch, else_m, continuation);
// m1.append(&mut m2);
// m1
// }
// fn compile_expr_stmt(&self, expr: Expr,
// model: CausalModel, continuation: Cont) -> Vec<CausalModel>
// {
// let m = self.deps.compile_expr(expr, None, model);
// continuation.call(self, m)
// }
// fn compile_instant(&mut self, instant: Instant) -> Vec<GuardedProgram> {
// fn compile_par<F>(&self, children: Vec<Stmt>, model: CausalModel,
// continuation: Cont, join_termination: F) -> Vec<CausalModel>
// where F: Clone + Fn(bool, bool) -> bool
// {
// // We create the model of every branch without calling the current continuation.
// let mut models = vec![];
// for child in children {
// models.push(self.compile_stmt(child, model.clone(), Box::new(IdentityCont)));
// }
// // We merge by Cartesian product the models created by every branch.
// let first = models.remove(0);
// let models = models.into_iter().fold(first, |accu, m| {
// CausalModel::cartesian_product(accu, m, join_termination.clone())
// });
// // We call the continuation on models that are instantaneous.
// let mut result = vec![];
// for model in models {
// if model.instantaneous {
// let mut next = continuation.call(self, model);
// result.append(&mut next);
// }
// else {
// result.push(model);
// }
// }
// result
// }
// fn compile_or_par(&self, children: Vec<Stmt>, model: CausalModel,
// continuation: Cont) -> Vec<CausalModel>
// {
// self.compile_par(children, model, continuation, CausalModel::term_or)
// }
// fn compile_and_par(&self, children: Vec<Stmt>, model: CausalModel,
// continuation: Cont) -> Vec<CausalModel>
// {
// self.compile_par(children, model, continuation, CausalModel::term_and)
// }
fn schedule_paths(&mut self, execution_paths: Vec<GuardedProgram>,
schedules: Vec<Scheduling>) -> GuardedProgram
{
let mut program = GuardedProgram::new();
for (path, schedule) in execution_paths.into_iter().zip(schedules.into_iter()) {
let mut path = self.schedule_path(path, schedule);
program.append(&mut path);
}
program
}
fn schedule_path(&mut self, path: GuardedProgram, schedule: Scheduling) -> GuardedProgram {
GuardedProgram::new()
}
}
@@ -13,7 +13,10 @@
// limitations under the License.
use context::*;
use middle::causality::symbolic_execution::State;
use middle::ir::compiler::AllInstants;
use std::collections::HashMap;
use std::cmp;
pub type GuardedProgram = Vec<GuardedCommand>;
@@ -44,3 +47,42 @@ pub enum Action {
Push(VarPath),
Prune(VarPath),
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct GuardedCommandFactory {
locations: Vec<Variable>,
}
impl GuardedCommandFactory
{
pub fn new(all_instants: &AllInstants) -> Self {
let mut factory = GuardedCommandFactory{ locations: vec![] };
factory.initialize_locations_names(all_instants);
factory
}
fn initialize_locations_names(&mut self, all_instants: &AllInstants) {
let max_loc = all_instants.values()
.flat_map(|p| p.iter())
.flat_map(|i| i.locations.iter())
.cloned()
.fold(0, cmp::max);
for loc in 0..max_loc+1 {
let loc_path = VarPath::gen(&format!("loc{}",loc));
let var = Variable::access(DUMMY_SP, loc_path, None);
self.locations.push(var);
}
}
pub fn create_locations_guard(&self, locations: State) -> Box<Expr> {
let guard = self.make_expr(ExprKind::Trilean(SKleene::True));
locations.into_iter().fold(guard, |a, l| {
let loc_var = self.locations[l].clone();
self.make_expr(ExprKind::And(a,self.make_expr(ExprKind::Var(loc_var))))
})
}
fn make_expr(&self, expr_kind: ExprKind) -> Box<Expr> {
Box::new(Expr::new(DUMMY_SP, expr_kind))
}
}
@@ -22,6 +22,6 @@ pub mod compiler;
pub mod scheduling;
pub fn compile_to_guarded_commands(session: Session, (context, instants): (Context, AllInstants)) -> Env<(Context, IR)> {
let compiler = Compiler::new(session, context, instants);
compiler.compile()
let compiler = Compiler::new(session, context, &instants);
compiler.compile(instants)
}

0 comments on commit dec96f5

Please sign in to comment.