Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion benchmarks/turnt_brilirs.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
command = "bril2json < {filename} | cargo +nightly run --manifest-path ../brilirs/Cargo.toml --quiet -- -p {args}"
command = "bril2json < {filename} | cargo run --manifest-path ../brilirs/Cargo.toml --quiet -- -p {args}"
output.out = "-"
output.prof = "2"
15 changes: 11 additions & 4 deletions bril-rs/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
#![warn(clippy::all, clippy::pedantic, clippy::nursery, clippy::cargo)]
// todo these are allowed to appease clippy but should be addressed some day
#![allow(clippy::missing_errors_doc)]
#![allow(clippy::missing_panics_doc)]
#![allow(clippy::must_use_candidate)]
#![allow(clippy::cargo_common_metadata)]

use std::fmt::{self, Display, Formatter};
use std::io::{self, Write};

Expand All @@ -10,7 +17,7 @@ pub struct Program {

impl Display for Program {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
for func in self.functions.iter() {
for func in &self.functions {
writeln!(f, "{}", func)?;
}
Ok(())
Expand Down Expand Up @@ -46,7 +53,7 @@ impl Display for Function {
write!(f, ": {}", tpe)?;
}
writeln!(f, " {{")?;
for instr in self.instrs.iter() {
for instr in &self.instrs {
writeln!(f, "{}", instr)?;
}
write!(f, "}}")?;
Expand Down Expand Up @@ -331,7 +338,7 @@ pub enum Type {
Float,
#[cfg(feature = "memory")]
#[serde(rename = "ptr")]
Pointer(Box<Type>),
Pointer(Box<Self>),
}

impl Display for Type {
Expand Down Expand Up @@ -368,7 +375,7 @@ impl Display for Literal {
}

impl Literal {
pub fn get_type(&self) -> Type {
pub const fn get_type(&self) -> Type {
match self {
Literal::Int(_) => Type::Int,
Literal::Bool(_) => Type::Bool,
Expand Down
4 changes: 2 additions & 2 deletions brilirs/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ benchmark:

.PHONY: release
release:
RUSTFLAGS="-C target-cpu=native" cargo +nightly build --release
RUSTFLAGS="-C target-cpu=native" cargo build --release

.PHONY: compare
compare: release
Expand All @@ -26,4 +26,4 @@ compare: release
# This is primarily used for running examples and debuging a bril program
.PHONY: example
example:
bril2json < ../benchmarks/sqrt.bril | cargo +nightly run
bril2json < ../benchmarks/sqrt.bril | cargo run
22 changes: 11 additions & 11 deletions brilirs/src/basic_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ pub struct BBProgram {
}

impl BBProgram {
pub fn new(prog: Program) -> Result<BBProgram, InterpError> {
pub fn new(prog: Program) -> Result<Self, InterpError> {
let num_funcs = prog.functions.len();
let bb = BBProgram {
let bb = Self {
func_index: prog
.functions
.into_iter()
Expand Down Expand Up @@ -44,8 +44,8 @@ pub struct BasicBlock {
}

impl BasicBlock {
fn new() -> BasicBlock {
BasicBlock {
const fn new() -> Self {
Self {
label: None,
instrs: Vec::new(),
numified_instrs: Vec::new(),
Expand Down Expand Up @@ -83,18 +83,18 @@ impl NumifiedInstruction {
num_var_map: &mut FxHashMap<String, u32>,
) -> Self {
match instr {
Instruction::Constant { dest, .. } => NumifiedInstruction {
Instruction::Constant { dest, .. } => Self {
dest: Some(get_num_from_map(dest, num_of_vars, num_var_map)),
args: Vec::new(),
},
Instruction::Value { dest, args, .. } => NumifiedInstruction {
Instruction::Value { dest, args, .. } => Self {
dest: Some(get_num_from_map(dest, num_of_vars, num_var_map)),
args: args
.iter()
.map(|v| get_num_from_map(v, num_of_vars, num_var_map))
.collect(),
},
Instruction::Effect { args, .. } => NumifiedInstruction {
Instruction::Effect { args, .. } => Self {
dest: None,
args: args
.iter()
Expand All @@ -119,13 +119,13 @@ pub struct BBFunction {
}

impl BBFunction {
pub fn new(f: Function) -> BBFunction {
let (mut func, label_map) = BBFunction::find_basic_blocks(f);
pub fn new(f: Function) -> Self {
let (mut func, label_map) = Self::find_basic_blocks(f);
func.build_cfg(label_map);
func
}

fn find_basic_blocks(func: bril_rs::Function) -> (BBFunction, FxHashMap<String, usize>) {
fn find_basic_blocks(func: bril_rs::Function) -> (Self, FxHashMap<String, usize>) {
let mut blocks = Vec::new();
let mut label_map = FxHashMap::default();

Expand Down Expand Up @@ -197,7 +197,7 @@ impl BBFunction {
}

(
BBFunction {
Self {
name: func.name,
args: func.args,
return_type: func.return_type,
Expand Down
18 changes: 9 additions & 9 deletions brilirs/src/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::error::InterpError;
use fxhash::FxHashMap;

#[inline(always)]
fn check_num_args(expected: usize, args: &[String]) -> Result<(), InterpError> {
const fn check_num_args(expected: usize, args: &[String]) -> Result<(), InterpError> {
if expected != args.len() {
Err(InterpError::BadNumArgs(expected, args.len()))
} else {
Expand All @@ -17,7 +17,7 @@ fn check_num_args(expected: usize, args: &[String]) -> Result<(), InterpError> {
}

#[inline(always)]
fn check_num_funcs(expected: usize, funcs: &[String]) -> Result<(), InterpError> {
const fn check_num_funcs(expected: usize, funcs: &[String]) -> Result<(), InterpError> {
if expected != funcs.len() {
Err(InterpError::BadNumFuncs(expected, funcs.len()))
} else {
Expand All @@ -26,7 +26,7 @@ fn check_num_funcs(expected: usize, funcs: &[String]) -> Result<(), InterpError>
}

#[inline(always)]
fn check_num_labels(expected: usize, labels: &[String]) -> Result<(), InterpError> {
const fn check_num_labels(expected: usize, labels: &[String]) -> Result<(), InterpError> {
if expected != labels.len() {
Err(InterpError::BadNumLabels(expected, labels.len()))
} else {
Expand All @@ -50,7 +50,7 @@ fn update_env<'a>(
typ: &'a Type,
) -> Result<(), InterpError> {
match env.get(dest) {
Some(current_typ) => check_asmt_type(current_typ, &typ),
Some(current_typ) => check_asmt_type(current_typ, typ),
None => {
env.insert(dest, typ);
Ok(())
Expand All @@ -76,7 +76,7 @@ fn get_type<'a>(
#[inline(always)]
fn get_ptr_type(typ: &bril_rs::Type) -> Result<&bril_rs::Type, InterpError> {
match typ {
bril_rs::Type::Pointer(ptr_type) => Ok(&ptr_type),
bril_rs::Type::Pointer(ptr_type) => Ok(ptr_type),
_ => Err(InterpError::ExpectedPointerType(typ.clone())),
}
}
Expand Down Expand Up @@ -231,15 +231,15 @@ fn type_check_instruction<'a>(
.zip(callee_func.args.iter())
.try_for_each(|(arg_name, expected_arg)| {
let ty = env
.get(&arg_name as &str)
.get(arg_name as &str)
.ok_or_else(|| InterpError::VarUndefined(arg_name.to_string()))?;

check_asmt_type(&ty, &expected_arg.arg_type)
check_asmt_type(ty, &expected_arg.arg_type)
})?;

match &callee_func.return_type {
None => Err(InterpError::NonEmptyRetForfunc(callee_func.name.clone())),
Some(t) => check_asmt_type(op_type, &t),
Some(t) => check_asmt_type(op_type, t),
}?;
update_env(env, dest, op_type)
}
Expand Down Expand Up @@ -399,7 +399,7 @@ fn type_check_instruction<'a>(
.zip(callee_func.args.iter())
.try_for_each(|(arg_name, expected_arg)| {
let ty = env
.get(&arg_name as &str)
.get(arg_name as &str)
.ok_or_else(|| InterpError::VarUndefined(arg_name.to_string()))?;

check_asmt_type(ty, &expected_arg.arg_type)
Expand Down
50 changes: 25 additions & 25 deletions brilirs/src/interp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ struct Environment {
impl Environment {
#[inline(always)]
pub fn new(size: u32) -> Self {
Environment {
Self {
env: vec![Value::default(); size as usize],
}
}
Expand All @@ -43,7 +43,7 @@ struct Heap {

impl Default for Heap {
fn default() -> Self {
Heap {
Self {
memory: FxHashMap::with_capacity_and_hasher(20, fxhash::FxBuildHasher::default()),
base_num_counter: 0,
}
Expand Down Expand Up @@ -127,7 +127,7 @@ pub enum Value {

impl Default for Value {
fn default() -> Self {
Value::Uninitialized
Self::Uninitialized
}
}

Expand All @@ -138,8 +138,8 @@ pub struct Pointer {
}

impl Pointer {
fn add(&self, offset: i64) -> Pointer {
Pointer {
const fn add(&self, offset: i64) -> Self {
Self {
base: self.base,
offset: self.offset + offset,
}
Expand All @@ -161,22 +161,22 @@ impl fmt::Display for Value {

impl From<&bril_rs::Literal> for Value {
#[inline(always)]
fn from(l: &bril_rs::Literal) -> Value {
fn from(l: &bril_rs::Literal) -> Self {
match l {
bril_rs::Literal::Int(i) => Value::Int(*i),
bril_rs::Literal::Bool(b) => Value::Bool(*b),
bril_rs::Literal::Float(f) => Value::Float(*f),
bril_rs::Literal::Int(i) => Self::Int(*i),
bril_rs::Literal::Bool(b) => Self::Bool(*b),
bril_rs::Literal::Float(f) => Self::Float(*f),
}
}
}

impl From<bril_rs::Literal> for Value {
#[inline(always)]
fn from(l: bril_rs::Literal) -> Value {
fn from(l: bril_rs::Literal) -> Self {
match l {
bril_rs::Literal::Int(i) => Value::Int(i),
bril_rs::Literal::Bool(b) => Value::Bool(b),
bril_rs::Literal::Float(f) => Value::Float(f),
bril_rs::Literal::Int(i) => Self::Int(i),
bril_rs::Literal::Bool(b) => Self::Bool(b),
bril_rs::Literal::Float(f) => Self::Float(f),
}
}
}
Expand Down Expand Up @@ -242,7 +242,7 @@ fn execute_value_op<'a, T: std::io::Write>(
out: &mut T,
value_store: &mut Environment,
heap: &mut Heap,
last_label: &Option<&String>,
last_label: Option<&String>,
instruction_count: &mut u32,
) -> Result<(), InterpError> {
use bril_rs::ValueOps::*;
Expand Down Expand Up @@ -536,7 +536,7 @@ fn execute<'a, T: std::io::Write>(
value_store.set(numified_code.dest.unwrap(), Value::Float(*f))
}
// this is safe because we type check this beforehand
_ => unsafe { unreachable_unchecked() },
bril_rs::Literal::Bool(_) => unsafe { unreachable_unchecked() },
}
} else {
value_store.set(numified_code.dest.unwrap(), Value::from(value));
Expand All @@ -560,7 +560,7 @@ fn execute<'a, T: std::io::Write>(
out,
&mut value_store,
heap,
&last_label,
last_label,
instruction_count,
)?;
}
Expand All @@ -576,7 +576,7 @@ fn execute<'a, T: std::io::Write>(
op,
&numified_code.args,
funcs,
&curr_block,
curr_block,
out,
&value_store,
heap,
Expand All @@ -598,7 +598,7 @@ fn parse_args(
mut env: Environment,
args: &[bril_rs::Argument],
args_as_nums: &[u32],
inputs: Vec<&str>,
inputs: &[&str],
) -> Result<Environment, InterpError> {
if args.is_empty() && inputs.is_empty() {
Ok(env)
Expand All @@ -615,7 +615,7 @@ fn parse_args(
Err(_) => {
return Err(InterpError::BadFuncArgType(
bril_rs::Type::Bool,
inputs.get(index).unwrap().to_string(),
(*inputs.get(index).unwrap()).to_string(),
))
}
Ok(b) => env.set(*arg_as_num, Value::Bool(b)),
Expand All @@ -627,7 +627,7 @@ fn parse_args(
Err(_) => {
return Err(InterpError::BadFuncArgType(
bril_rs::Type::Int,
inputs.get(index).unwrap().to_string(),
(*inputs.get(index).unwrap()).to_string(),
))
}
Ok(i) => env.set(*arg_as_num, Value::Int(i)),
Expand All @@ -639,7 +639,7 @@ fn parse_args(
Err(_) => {
return Err(InterpError::BadFuncArgType(
bril_rs::Type::Float,
inputs.get(index).unwrap().to_string(),
(*inputs.get(index).unwrap()).to_string(),
))
}
Ok(f) => env.set(*arg_as_num, Value::Float(f)),
Expand All @@ -654,9 +654,9 @@ fn parse_args(
}

pub fn execute_main<T: std::io::Write>(
prog: BBProgram,
prog: &BBProgram,
mut out: T,
input_args: Vec<&str>,
input_args: &[&str],
profiling: bool,
) -> Result<(), InterpError> {
let main_func = prog.get("main").ok_or(InterpError::NoMainFunction)?;
Expand All @@ -673,8 +673,8 @@ pub fn execute_main<T: std::io::Write>(
let mut instruction_count = 0;

execute(
&prog,
&main_func,
prog,
main_func,
&mut out,
value_store,
&mut heap,
Expand Down
Loading