Skip to content

Commit

Permalink
make MIR display more readable
Browse files Browse the repository at this point in the history
  • Loading branch information
jay3332 committed Jul 1, 2023
1 parent 2d2b00d commit bddde70
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 15 deletions.
11 changes: 11 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions mir/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ edition = "2021"
common = { path = "../common" }
diagnostics = { path = "../diagnostics" }
hir = { path = "../hir" }
indexmap = "^1.9"
4 changes: 2 additions & 2 deletions mir/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ use hir::{
typed::{BinaryIntIntrinsic, LocalEnv, Ty, UnaryIntIntrinsic},
FloatWidth, Ident, IntSign, IntWidth, ItemId, ScopeId,
};
use indexmap::IndexMap;
use std::{
collections::HashMap,
fmt::{self, Display, Formatter},
Expand All @@ -46,6 +47,7 @@ fn write_comma_sep<T: Display>(f: &mut Formatter, iter: impl Iterator<Item = T>)
}

pub type TypedHir = hir::Hir<hir::infer::InferMetadata>;
pub type BlockMap = IndexMap<BlockId, Vec<Spanned<Node>>>;

#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub struct BlockId(pub Ident);
Expand All @@ -71,8 +73,6 @@ impl Display for LocalId {
}
}

pub type BlockMap = HashMap<BlockId, Vec<Spanned<Node>>>;

/// The MIR representation of a Terbium program.
#[derive(Clone, Debug, Default)]
pub struct Mir {
Expand Down
23 changes: 10 additions & 13 deletions mir/src/lower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,7 @@ pub struct Ctx<'a> {
impl<'a> Ctx<'a> {
/// Move the context to start writing in the given block.
pub(crate) fn move_to(&mut self, block_id: BlockId) {
self.current = unsafe { &mut *self.blocks }
.try_insert(block_id, Vec::new())
.expect("block already exists");
self.current = unsafe { &mut *self.blocks }.entry(block_id).or_default();
self.track = block_id;
}
}
Expand Down Expand Up @@ -164,7 +162,8 @@ impl Lowerer {
let block_id = BlockId::from(scope);
let cont_id = BlockId(format!("_bb{}.after", scope.0).into());
// Create the temporary result local
let result_local = LocalId(format!("result{}", scope.0).into(), LocalEnv::Internal);
let result_local =
LocalId(format!("result.{}", scope.0).into(), LocalEnv::Internal);
ctx.current.extend([
Node::Local(result_local, ty).spanned(span),
// Jump to the block that will assign the result
Expand Down Expand Up @@ -213,12 +212,11 @@ impl Lowerer {
}
// Since if-else-expressions may diverge, this will also store a temporary result local
HirExpr::If(cond, then, Some(els)) => {
let then_id = BlockId::from(then);
let else_id = BlockId::from(els);
let cont_id = BlockId(format!("_bb{}.after", then.0).into());
let then_id = BlockId(format!("if.{}.then", then.0).into());
let else_id = BlockId(format!("if.{}.else", then.0).into());
let cont_id = BlockId(format!("if.{}.after", then.0).into());
// Create the temporary result local
let result_local =
LocalId(format!("result{}", then_id.0).into(), LocalEnv::Internal);
let result_local = LocalId(format!("result.{}", then.0).into(), LocalEnv::Internal);

let cond = self.lower_expr(ctx, *cond);
ctx.current.extend([
Expand Down Expand Up @@ -260,8 +258,7 @@ impl Lowerer {
let body_id = BlockId::from(body);
let cont_id = BlockId(format!("_bb{}.after", body.0).into());
// Create the temporary result local
let result_local =
LocalId(format!("result{}", body_id.0).into(), LocalEnv::Internal);
let result_local = LocalId(format!("result.{}", body.0).into(), LocalEnv::Internal);

ctx.current.extend([
Node::Local(result_local, ty).spanned(span),
Expand Down Expand Up @@ -305,8 +302,8 @@ impl Lowerer {

// SAFETY: the blocks pointer is valid for the lifetime of the function
let entry = unsafe { &mut *blocks }
.try_insert(entry_id, Vec::with_capacity(scope.children.value().len()))
.expect("block already exists");
.entry(entry_id)
.or_insert_with(|| Vec::with_capacity(scope.children.value().len()));

let mut ctx = Ctx {
blocks,
Expand Down

0 comments on commit bddde70

Please sign in to comment.