Skip to content

Commit

Permalink
Merge pull request sampsyo#95 from Pat-Lafon/bril-rust-extension
Browse files Browse the repository at this point in the history
Bril rust extension
  • Loading branch information
sampsyo authored Dec 9, 2020
2 parents 8042ac1 + 492eea3 commit 701a02e
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 3 deletions.
6 changes: 6 additions & 0 deletions bril-rs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,9 @@ edition = "2018"
[dependencies]
serde_json = "1.0"
serde = { version = "1.0", features = ["derive"] }

[features]
float = []
memory = []
ssa = []
speculate = []
30 changes: 29 additions & 1 deletion bril-rs/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use serde::{Deserialize, Serialize};
use std::io::{self, Read};
use std::io::{self, Read, Write};

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Program {
Expand Down Expand Up @@ -81,8 +81,16 @@ pub enum EffectOps {
Return,
Print,
Nop,
#[cfg(feature = "memory")]
Store,
#[cfg(feature = "memory")]
Free,
#[cfg(feature = "speculate")]
Speculate,
#[cfg(feature = "speculate")]
Commit,
#[cfg(feature = "speculate")]
Guard,
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
Expand All @@ -102,18 +110,31 @@ pub enum ValueOps {
Or,
Call,
Id,
#[cfg(feature = "ssa")]
Phi,
#[cfg(feature = "float")]
Fadd,
#[cfg(feature = "float")]
Fsub,
#[cfg(feature = "float")]
Fmul,
#[cfg(feature = "float")]
Fdiv,
#[cfg(feature = "float")]
Feq,
#[cfg(feature = "float")]
Flt,
#[cfg(feature = "float")]
Fgt,
#[cfg(feature = "float")]
Fle,
#[cfg(feature = "float")]
Fge,
#[cfg(feature = "memory")]
Alloc,
#[cfg(feature = "memory")]
Load,
#[cfg(feature = "memory")]
PtrAdd,
}

Expand All @@ -123,6 +144,7 @@ pub enum Type {
Int,
Bool,
Float,
#[cfg(feature = "memory")]
#[serde(rename = "ptr")]
Pointer(Box<Type>),
}
Expand All @@ -140,3 +162,9 @@ pub fn load_program() -> Program {
io::stdin().read_to_string(&mut buffer).unwrap();
serde_json::from_str(&buffer).unwrap()
}

pub fn output_program(p: &Program) {
io::stdout()
.write_all(serde_json::to_string(p).unwrap().as_bytes())
.unwrap();
}
8 changes: 6 additions & 2 deletions docs/tools/rust.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Rust Library
============

This is a no-frills interface between Bril's JSON and your [Rust][] code. It supports the [Bril core][core] along with the [SSA][], [memory][], and [floating point][float] extensions.
This is a no-frills interface between Bril's JSON and your [Rust][] code. It supports the [Bril core][core] along with the [SSA][], [memory][], [floating point][float], and [speculative execution][spec] extensions.

Use
---
Expand All @@ -12,9 +12,12 @@ Include this by adding the following to your `Cargo.toml`:
[dependencies.bril-rs]
version = "0.1.0"
path = "../bril-rs"
features = ["ssa", "memory", "float", "speculate"]
```

There is one helper function, `load_program`, which will read a valid Bril program from stdin and return it as a Rust struct. Otherwise, this library can be treated like any other [serde][] JSON representation.
Each of the extensions to [Bril core][core] is feature gated. To ignore an extension, remove its corresponding string from the `features` list.

There are two helper functions, `load_program`, which will read a valid Bril program from stdin and and `output_program` which writes your Bril program to stdout. Otherwise, this library can be treated like any other [serde][] JSON representation.

Development
-----------
Expand All @@ -32,3 +35,4 @@ cargo clippy
[ssa]: ../lang/ssa.md
[memory]: ../lang/memory.md
[float]: ../lang/float.md
[spec]: ../lang/spec.md

0 comments on commit 701a02e

Please sign in to comment.