Skip to content
This repository has been archived by the owner on Jun 11, 2024. It is now read-only.

Commit

Permalink
Add support for register and letbind initialization, fix pow2 seman…
Browse files Browse the repository at this point in the history
…tics, indices on read/write variable statements (tricky bit of code to generate correct sequence of extract and mutate field for nested field writes), and new Tom optimizations
  • Loading branch information
fmckeogh committed May 23, 2024
1 parent 6a76cb2 commit 5d76aac
Show file tree
Hide file tree
Showing 33 changed files with 1,079 additions and 466 deletions.
245 changes: 132 additions & 113 deletions Cargo.lock

Large diffs are not rendered by default.

19 changes: 10 additions & 9 deletions borealis/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,31 @@ common = { path = "../common" }
sailrs = { path = "../sailrs" }

displaydoc = "0.2.4"
thiserror = "1.0.59"
thiserror = "1.0.61"
errctx = "1.0.0"
color-eyre = { version = "0.6.3", default-features = false }
pretty_assertions = "1.4.0"
pretty_env_logger = "0.5.0"
log = "0.4.21"
clap = { version = "4.5.4", features = ["derive"] }
deepsize = "0.2.0"
serde_json = "1.0.116"
num-bigint = { version = "0.4.4", features = ["serde"] }
serde_json = "1.0.117"
num-bigint = { version = "0.4.5", features = ["serde"] }
once_cell = "1.19.0"
regex = "1.10.4"
dot = "0.1.4"
itertools = "0.12.1"
itertools = "0.13.0"
kinded = "0.3.0"
quote = "1.0.36"
syn = { version = "2.0.60", features = ["full", "parsing"] }
prettyplease = "0.2.19"
proc-macro2 = "1.0.81"
syn = { version = "2.0.65", features = ["full", "parsing"] }
prettyplease = "0.2.20"
proc-macro2 = "1.0.83"
memmap2 = "0.9.4"
rkyv = {version = "0.7.44", default-features = false, features = ["std", "alloc", "size_64"] }
parking_lot = "0.12.2"
cargo-util-schemas = "0.3.0"
toml = "0.8.12"
semver = { version = "1.0.22", features = ["serde"] }
toml = "0.8.13"
semver = { version = "1.0.23", features = ["serde"] }
walkdir = "2.5.0"
rayon = "1.10.0"
dashmap = { version = "5.5.3", features = ["rayon"] }
4 changes: 4 additions & 0 deletions borealis/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,7 @@
* [opam](https://opam.ocaml.org)
* [Dune](https://dune.build)
* [`sail` opam package](https://opam.ocaml.org/packages/sail/)

## Usage

$ cargo r --bin borealis -- --dump-ir ./target ../arm-v9.4-a_d43f3f4c.rkyv ~/Documents/Sync/brig-aarch64
14 changes: 12 additions & 2 deletions borealis/src/boom/control_flow/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,28 @@ use {
///
/// Contains state required to build the control flow graph, resolving labels
/// and block terminators
#[derive(Debug)]
pub struct ControlFlowGraphBuilder {
labels: HashMap<InternedString, Shared<MaybeUnresolvedControlFlowBlock>>,
resolved_blocks: HashMap<SharedKey<MaybeUnresolvedControlFlowBlock>, ControlFlowBlock>,
entry_block: Shared<MaybeUnresolvedControlFlowBlock>,
current_block: Shared<MaybeUnresolvedControlFlowBlock>,
allow_unknown_terminators: bool,
}

impl ControlFlowGraphBuilder {
pub fn from_statements(statements: &[Shared<Statement>]) -> ControlFlowBlock {
pub fn from_statements(
statements: &[Shared<Statement>],
allow_unknown_terminators: bool,
) -> ControlFlowBlock {
let entry_block = MaybeUnresolvedControlFlowBlock::new();

let mut celf = Self {
labels: Default::default(),
resolved_blocks: Default::default(),
current_block: entry_block.clone(),
entry_block,
allow_unknown_terminators,
};

celf.process_statements(statements);
Expand Down Expand Up @@ -219,7 +225,11 @@ impl ControlFlowGraphBuilder {
},

MaybeUnresolvedTerminator::Unknown => {
panic!("encountered unknown terminator during resolution");
if self.allow_unknown_terminators {
Terminator::Return(None)
} else {
panic!("encountered unknown terminator during resolution\n{self:#?}")
}
}
};
ControlFlowBlock::set_terminator(&resolved, terminator);
Expand Down
10 changes: 1 addition & 9 deletions borealis/src/boom/control_flow/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

use {
crate::boom::{
control_flow::builder::ControlFlowGraphBuilder,
visitor::{Visitor, Walkable},
Statement, Value,
},
Expand All @@ -27,17 +26,10 @@ use {
},
};

mod builder;
pub mod builder;
pub mod dot;
pub mod util;

/// Creates a control flow graph from a slice of statements.
///
/// This should be called per BOOM function.
pub fn build_graph(statements: &[Shared<Statement>]) -> ControlFlowBlock {
ControlFlowGraphBuilder::from_statements(statements)
}

/// Node in a control flow graph, contains a basic block of statements and a
/// terminator
#[derive(Debug, Clone, Default)]
Expand Down
33 changes: 25 additions & 8 deletions borealis/src/boom/convert.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
//! JIB to BOOM conversion

use rayon::iter::ParallelIterator;
use {
crate::boom::{
self, control_flow::build_graph, Bit, FunctionSignature, NamedType, Parameter, Size,
self, control_flow::builder::ControlFlowGraphBuilder, Bit, FunctionSignature, NamedType,
Parameter, Size,
},
common::{intern::InternedString, shared::Shared, HashMap},
rayon::iter::IntoParallelIterator,
sailrs::{jib_ast, sail_ast},
std::borrow::Borrow,
};
Expand All @@ -29,9 +32,10 @@ impl BoomEmitter {
}

/// Process a sequence of JIB definitions
/// IntoParallelIterator
pub fn process<I: IntoIterator<Item = jib_ast::Definition>>(&mut self, definitions: I) {
definitions
.into_iter()
.into_iter() //.into_par_iter
.for_each(|def| self.process_definition(&def));
}

Expand All @@ -42,10 +46,18 @@ impl BoomEmitter {

fn process_definition(&mut self, definition: &jib_ast::Definition) {
match definition {
jib_ast::Definition::Register(ident, typ, _) => {
self.ast
.registers
.insert(ident.as_interned(), convert_type(typ));
jib_ast::Definition::Register(ident, typ, body) => {
self.ast.registers.insert(
ident.as_interned(),
(
convert_type(typ),
// allow unknown terminators for registers
ControlFlowGraphBuilder::from_statements(
&convert_body(body.as_ref()),
true,
),
),
);
}
jib_ast::Definition::Type(type_def) => {
let def = match type_def {
Expand Down Expand Up @@ -73,7 +85,11 @@ impl BoomEmitter {
typ: convert_type(typ),
})
.collect(),
body: convert_body(body.as_ref()),
// allow unknown terminators for letbinds
body: ControlFlowGraphBuilder::from_statements(
&convert_body(body.as_ref()),
true,
),
});
}
jib_ast::Definition::Val(id, _, parameters, out) => {
Expand Down Expand Up @@ -107,7 +123,8 @@ impl BoomEmitter {
let body = convert_body(body.as_ref());

//debug!("building new control flow graph for {name}");
let control_flow = build_graph(&body);
// do not allow unknown terminators for regular functions
let control_flow = ControlFlowGraphBuilder::from_statements(&body, false);

self.ast.functions.insert(
name,
Expand Down
8 changes: 4 additions & 4 deletions borealis/src/boom/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use {
common::{intern::InternedString, shared::Shared, HashMap},
kinded::Kinded,
num_bigint::BigInt,
rayon::iter::IntoParallelIterator,
sailrs::jib_ast,
std::{fmt::Debug, ops::Add},
};
Expand All @@ -28,7 +29,7 @@ pub struct Ast {
/// Sequence of definitions
pub definitions: Vec<Definition>,
/// Register types by identifier
pub registers: HashMap<InternedString, Shared<Type>>,
pub registers: HashMap<InternedString, (Shared<Type>, ControlFlowBlock)>,
/// Function definitions by identifier
pub functions: HashMap<InternedString, FunctionDefinition>,
}
Expand Down Expand Up @@ -73,7 +74,7 @@ pub enum Definition {

Let {
bindings: Vec<NamedType>,
body: Vec<Shared<Statement>>,
body: ControlFlowBlock,
},
}

Expand All @@ -93,8 +94,7 @@ impl Walkable for Definition {
.iter()
.for_each(|named_type| visitor.visit_named_type(named_type));

body.iter()
.for_each(|statement| visitor.visit_statement(statement.clone()));
visitor.visit_control_flow_block(body);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion borealis/src/boom/passes/monomorphize_vectors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ fn monomorphize_vectors(ast: Shared<Ast>, entry_block: ControlFlowBlock) -> bool
if let Some(reg_type) = ast.get().registers.get(source) {
// assert element_types are the same
// replace original type with that type
*original_type.get_mut() = reg_type.get().clone();
*original_type.get_mut() = reg_type.0.get().clone();
}
}
}
Expand Down
104 changes: 56 additions & 48 deletions borealis/src/boom/pretty_print.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,15 @@ pub fn print_ast<W: Write>(w: &mut W, ast: Shared<Ast>) {
.iter()
.for_each(|def| visitor.visit_definition(def));

registers.iter().for_each(|(name, typ)| {
registers.iter().for_each(|(name, (typ, init))| {
write!(visitor.writer, "register {name}: ").unwrap();
visitor.visit_type(typ.clone());
writeln!(visitor.writer).unwrap();
writeln!(visitor.writer, " = {{").unwrap();
{
let _h = visitor.indent();
visitor.print_control_flow_graph(init.clone());
}
writeln!(visitor.writer, "}}").unwrap();
});

functions
Expand Down Expand Up @@ -91,6 +96,53 @@ impl<'writer, W: Write> PrettyPrinter<'writer, W> {
indent: self.indent.clone(),
}
}

fn print_control_flow_graph(&mut self, entry_block: ControlFlowBlock) {
entry_block.iter().for_each(|b| {
writeln!(self.writer, " {}:", b).unwrap();
{
b.statements().iter().for_each(|stmt| {
write!(self.writer, " ").unwrap();
self.visit_statement(stmt.clone());
writeln!(self.writer).unwrap();
});

match b.terminator() {
Terminator::Return(None) => writeln!(self.writer, " return;").unwrap(),
Terminator::Return(Some(value)) => {
write!(self.writer, " return ").unwrap();
self.visit_value(Shared::new(value));
writeln!(self.writer, ";").unwrap();
}
Terminator::Conditional {
condition,
target,
fallthrough,
} => {
write!(self.writer, " if (").unwrap();
self.visit_value(Shared::new(condition));
writeln!(self.writer, ") {{").unwrap();
writeln!(self.writer, " goto {target};").unwrap();
writeln!(self.writer, " }} else {{").unwrap();
writeln!(self.writer, " goto {fallthrough};").unwrap();
writeln!(self.writer, " }}").unwrap();
}
Terminator::Unconditional { target } => {
writeln!(self.writer, " goto {target};").unwrap();
}
Terminator::Panic(values) => {
write!(self.writer, " panic ").unwrap();
for value in values {
self.visit_value(value);
writeln!(self.writer, ", ").unwrap();
}
writeln!(self.writer, ";").unwrap();
}
}
}
writeln!(self.writer).unwrap();
});
}
}

struct IndentHandle {
Expand Down Expand Up @@ -165,8 +217,7 @@ impl<'writer, W: Write> Visitor for PrettyPrinter<'writer, W> {

{
let _h = self.indent();
body.iter()
.for_each(|statement| self.visit_statement(statement.clone()));
self.print_control_flow_graph(body.clone());
}

writeln!(self.writer, "}}").unwrap();
Expand All @@ -177,50 +228,7 @@ impl<'writer, W: Write> Visitor for PrettyPrinter<'writer, W> {
fn visit_function_definition(&mut self, node: &FunctionDefinition) {
self.visit_function_signature(&node.signature);

node.entry_block.iter().for_each(|b| {
writeln!(self.writer, " {}:", b).unwrap();
{
b.statements().iter().for_each(|stmt| {
write!(self.writer, " ").unwrap();
self.visit_statement(stmt.clone());
writeln!(self.writer).unwrap();
});

match b.terminator() {
Terminator::Return(None) => writeln!(self.writer, " return;").unwrap(),
Terminator::Return(Some(value)) => {
write!(self.writer, " return ").unwrap();
self.visit_value(Shared::new(value));
writeln!(self.writer, ";").unwrap();
}
Terminator::Conditional {
condition,
target,
fallthrough,
} => {
write!(self.writer, " if (").unwrap();
self.visit_value(Shared::new(condition));
writeln!(self.writer, ") {{").unwrap();
writeln!(self.writer, " goto {target};").unwrap();
writeln!(self.writer, " }} else {{").unwrap();
writeln!(self.writer, " goto {fallthrough};").unwrap();
writeln!(self.writer, " }}").unwrap();
}
Terminator::Unconditional { target } => {
writeln!(self.writer, " goto {target};").unwrap();
}
Terminator::Panic(values) => {
write!(self.writer, " panic ").unwrap();
for value in values {
self.visit_value(value);
writeln!(self.writer, ", ").unwrap();
}
writeln!(self.writer, ";").unwrap();
}
}
}
writeln!(self.writer).unwrap();
});
self.print_control_flow_graph(node.entry_block.clone());

writeln!(self.writer, "}}").unwrap();
}
Expand Down
5 changes: 5 additions & 0 deletions borealis/src/brig/codegen/dbt.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
use crate::rudder;

pub fn generate(rudder: &rudder::Context) {
todo!()
}
1 change: 1 addition & 0 deletions borealis/src/brig/codegen/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod dbt;
Loading

0 comments on commit 5d76aac

Please sign in to comment.