Skip to content

Commit

Permalink
Merge pull request sampsyo#89 from Pat-Lafon/bril-rust-extension
Browse files Browse the repository at this point in the history
Bril<->Rust extension
  • Loading branch information
sampsyo committed Nov 27, 2020
2 parents e8d7004 + fe716c0 commit 3d501ea
Show file tree
Hide file tree
Showing 5 changed files with 183 additions and 0 deletions.
2 changes: 2 additions & 0 deletions bril-rs/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
**/target
Cargo.lock
11 changes: 11 additions & 0 deletions bril-rs/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[package]
name = "bril-rs"
version = "0.1.0"
authors = ["Patrick LaFontaine <[email protected]>"]
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
serde_json = "1.0"
serde = { version = "1.0", features = ["derive"] }
142 changes: 142 additions & 0 deletions bril-rs/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
use serde::{Deserialize, Serialize};
use std::io::{self, Read};

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Program {
pub functions: Vec<Function>,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Function {
pub name: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub args: Option<Vec<Argument>>,
#[serde(rename = "type")]
#[serde(skip_serializing_if = "Option::is_none")]
pub return_type: Option<Type>,
pub instrs: Vec<Code>,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Argument {
pub name: String,
#[serde(rename = "type")]
pub arg_type: Type,
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
#[serde(untagged)]
pub enum Code {
Label { label: String },
Instruction(Instruction),
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
#[serde(untagged)]
pub enum Instruction {
Constant {
op: ConstOps,
dest: String,
#[serde(rename = "type")]
const_type: Type,
value: Literal,
},
Value {
op: ValueOps,
dest: String,
#[serde(rename = "type")]
op_type: Type,
#[serde(skip_serializing_if = "Option::is_none")]
args: Option<Vec<String>>,
#[serde(skip_serializing_if = "Option::is_none")]
funcs: Option<Vec<String>>,
#[serde(skip_serializing_if = "Option::is_none")]
labels: Option<Vec<String>>,
},
Effect {
op: EffectOps,
#[serde(skip_serializing_if = "Option::is_none")]
args: Option<Vec<String>>,
#[serde(skip_serializing_if = "Option::is_none")]
funcs: Option<Vec<String>>,
#[serde(skip_serializing_if = "Option::is_none")]
labels: Option<Vec<String>>,
},
}
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
pub enum ConstOps {
#[serde(rename = "const")]
Const,
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
#[serde(rename_all = "lowercase")]
pub enum EffectOps {
#[serde(rename = "jmp")]
Jump,
#[serde(rename = "br")]
Branch,
Call,
#[serde(rename = "ret")]
Return,
Print,
Nop,
Store,
Free,
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
#[serde(rename_all = "lowercase")]
pub enum ValueOps {
Add,
Sub,
Mul,
Div,
Eq,
Lt,
Gt,
Le,
Ge,
Not,
And,
Or,
Call,
Id,
Phi,
Fadd,
Fsub,
Fmul,
Fdiv,
Feq,
Flt,
Fgt,
Fle,
Fge,
Alloc,
Load,
PtrAdd,
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
#[serde(rename_all = "lowercase")]
pub enum Type {
Int,
Bool,
Float,
#[serde(rename = "ptr")]
Pointer(Box<Type>),
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
#[serde(untagged)]
pub enum Literal {
Int(i64),
Bool(bool),
Float(f64),
}

pub fn load_program() -> Program {
let mut buffer = String::new();
io::stdin().read_to_string(&mut buffer).unwrap();
serde_json::from_str(&buffer).unwrap()
}
1 change: 1 addition & 0 deletions docs/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@
- [Type Inference](tools/infer.md)
- [Benchmarks](tools/bench.md)
- [OCaml Library](tools/ocaml.md)
- [Rust Library](tools/rust.md)
- [Benchmark Runner](tools/brench.md)
27 changes: 27 additions & 0 deletions docs/tools/rust.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
Rust Library
============

This is a no-frills interface between bril's json and your rust code. It supports the bril core syntax along with SSA, Memory, and Floating Point extensions.

Use
---

Include this by adding the following to your Cargo.toml

```toml
[dependencies.bril-rs]
version = "0.1.0"
path = "../bril-rs"
```

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](https://github.com/serde-rs/serde) json representation.

Development
-----------

To maintain consistency and cleanliness, run:

```bash
cargo fmt
cargo clippy
```

0 comments on commit 3d501ea

Please sign in to comment.