Permalink
Browse files

[bonsai][analysis] Approximate the read/write permissions of variables.

  • Loading branch information...
ptal committed Apr 29, 2017
1 parent 677788d commit 03d733b61e4c82f9e5bb5b8956c2967ccc6fb661
Showing with 86 additions and 12 deletions.
  1. +1 −1 Cargo.toml
  2. +1 −1 manual-installation.md
  3. +1 −1 setup.py
  4. +4 −3 src/ast.rs
  5. +71 −0 src/middle/approximate_permission.rs
  6. +8 −6 src/middle/mod.rs
View
@@ -17,7 +17,7 @@ name = "bonsai"
doc = false
[dependencies]
oak = "^0.5.2"
oak = "^0.5.3"
oak_runtime = "^0.5.5"
partial = "0.1.3"
clap = "^2"
View
@@ -9,7 +9,7 @@ See [README.md](README.md)
## Installing the bonsai compiler
```sh
rustup override set nightly-2017-01-24
rustup override set nightly-2017-04-27
cargo install
```
View
@@ -2,7 +2,7 @@
import platform
import subprocess
rust_nightly_version = "nightly-2017-01-24"
rust_nightly_version = "nightly-2017-04-27"
sugarcubes_jar = "/tmp/SugarCubesv4.0.0a5.jar"
bonsai_runtime_jar = "target/runtime-1.0-SNAPSHOT.jar"
bonsai_runtime_src = "runtime/"
View
@@ -463,17 +463,18 @@ impl Hash for VarPath {
}
}
#[derive(Clone, Debug, Hash, Eq, PartialEq)]
#[derive(Clone, Copy, Debug, Hash, Eq, PartialEq)]
pub enum Permission {
Read,
Write,
ReadWrite
}
#[derive(Clone, Debug, Eq)]
pub struct Variable {
pub path: VarPath,
pub past: usize,
pub perm: Permission,
pub permission: Permission,
pub span: Span
}
@@ -482,7 +483,7 @@ impl Variable {
Variable {
path: path,
past: past,
perm: Permission::ReadWrite,
permission: Permission::ReadWrite,
span: span
}
}
@@ -0,0 +1,71 @@
// Copyright 2017 Pierre Talbot (IRCAM)
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/// For each variable, depending on its context, we approximate its permission.
/// Permissions can either be `Read`, `Write` or `ReadWrite` if the variable is read-only, write-only or both.
/// When calling external method, we do not know how the method is actually modifying the variable so we approximate it with `ReadWrite`.
/// In a tell statement `x <- e`, `x` is only wrote and variables occurring in `e` are supposed to be only read.
/// In an entailment condition `e |= e'`, every variables appearing in `e` or `e'` are supposed to be only read.
use context::*;
pub fn approximate_permission<'a>(context: Context<'a>) -> Partial<Context<'a>> {
let permission = ApproximatePermission::new(context);
permission.compute()
}
struct ApproximatePermission<'a> {
context: Context<'a>,
perm_context: Permission
}
impl<'a> ApproximatePermission<'a> {
pub fn new(context: Context<'a>) -> Self {
ApproximatePermission {
context: context,
perm_context: Permission::ReadWrite,
}
}
fn compute(mut self) -> Partial<Context<'a>> {
let mut bcrate_clone = self.context.clone_ast();
self.visit_crate(&mut bcrate_clone);
self.context.replace_ast(bcrate_clone);
Partial::Value(self.context)
}
}
impl<'a> VisitorMut<JClass> for ApproximatePermission<'a>
{
fn visit_var(&mut self, var: &mut Variable) {
var.permission = self.perm_context;
}
fn visit_tell(&mut self, var: &mut Variable, expr: &mut Expr) {
let old = self.perm_context;
self.perm_context = Permission::Write;
self.visit_var(var);
self.perm_context = Permission::Read;
self.visit_expr(expr);
self.perm_context = old;
}
fn visit_entailment(&mut self, rel: &mut EntailmentRel) {
let old = self.perm_context;
self.perm_context = Permission::Read;
self.visit_var(&mut rel.left);
self.visit_expr(&mut rel.right);
self.perm_context = old;
}
}
View
@@ -17,20 +17,22 @@ mod undeclared;
mod resolve;
mod matching_ref;
mod stream_bound;
mod approximate_permission;
use context::*;
use middle::duplicate::*;
use middle::undeclared::*;
use middle::resolve::*;
use middle::matching_ref::*;
use middle::stream_bound::*;
use middle::approximate_permission::*;
pub fn analyse_bonsai<'a>(context: Context<'a>) -> Partial<Context<'a>> {
Partial::Value(context)
.and_then(|context| duplicate(context))
.and_then(|context| undeclared(context))
.and_then(|context| resolve(context))
.and_then(|context| matching_ref(context))
.and_then(|context| stream_bound(context))
// .and_then(|context| approximate_permission(context))
.and_then(duplicate)
.and_then(undeclared)
.and_then(resolve)
.and_then(matching_ref)
.and_then(stream_bound)
.and_then(approximate_permission)
}

0 comments on commit 03d733b

Please sign in to comment.