Skip to content

Commit

Permalink
remove PortDef (#475)
Browse files Browse the repository at this point in the history
  • Loading branch information
UnsignedByte authored Oct 30, 2024
1 parent cb09773 commit b10ea3f
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 177 deletions.
8 changes: 8 additions & 0 deletions crates/ast/src/control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,14 @@ impl Bundle {
..self
}
}

/// Resolve events in the Bundle
pub fn resolve_event(self, binding: &Binding<Time>) -> Self {
Self {
typ: self.typ.resolve_event(binding),
..self
}
}
}

/// A let-bound parameter
Expand Down
2 changes: 1 addition & 1 deletion crates/ast/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ impl FilamentParser {
Ok(Port::Un((name.take(), n)))
},
[bundle_def(bd)] => {
Ok(Port::Pd(Loc::new(bd.into(), sp)))
Ok(Port::Pd(Loc::new(bd, sp)))
},
)
}
Expand Down
86 changes: 2 additions & 84 deletions crates/ast/src/port.rs
Original file line number Diff line number Diff line change
@@ -1,88 +1,6 @@
use super::{Binding, Bundle, Expr, Id, Loc, Range, Time};
use super::{Bundle, Id, Loc};

/// A port definition in a [super::Signature].
#[derive(Clone)]
pub enum PortDef {
Port {
name: Loc<Id>,
/// Liveness condition for the Port
liveness: Loc<Range>,
/// Bitwidth of the port
bitwidth: Loc<Expr>,
},
Bundle(Bundle),
}
impl From<Bundle> for PortDef {
fn from(b: Bundle) -> Self {
Self::Bundle(b)
}
}

impl PortDef {
pub fn port(
name: Loc<Id>,
liveness: Loc<Range>,
bitwidth: Loc<Expr>,
) -> Self {
Self::Port {
name,
liveness,
bitwidth,
}
}

pub fn bundle(bundle: Bundle) -> Self {
Self::Bundle(bundle)
}

/// Name of this Port
pub fn name(&self) -> &Loc<Id> {
match &self {
PortDef::Port { name, .. } => name,
PortDef::Bundle(b) => &b.name,
}
}
}
impl PortDef {
/// Resolves all time expressions in this port definition
pub fn resolve_event(self, bindings: &Binding<Time>) -> Self {
match self {
PortDef::Port {
name,
liveness,
bitwidth,
} => PortDef::Port {
name,
liveness: liveness.map(|l| l.resolve_event(bindings)),
bitwidth,
},
PortDef::Bundle(b) => {
let t = b.typ.resolve_event(bindings);
let bun = Bundle { typ: t, ..b };
PortDef::Bundle(bun)
}
}
}

/// Resolves all width expressions in this port definition.
/// Specifically:
/// - The bitwidth of the port
/// - The liveness condition
pub fn resolve_exprs(self, bindings: &Binding<Expr>) -> Self {
match self {
PortDef::Port {
name,
liveness,
bitwidth,
} => PortDef::Port {
name,
liveness: liveness.map(|l| l.resolve_exprs(bindings)),
bitwidth: bitwidth.map(|b| b.resolve(bindings)),
},
PortDef::Bundle(b) => PortDef::Bundle(b.resolve_exprs(bindings)),
}
}
}
pub type PortDef = Bundle;

#[derive(Clone)]
pub struct InterfaceDef {
Expand Down
123 changes: 42 additions & 81 deletions crates/ir/src/from_ast/astconv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -431,86 +431,48 @@ impl<'prog> BuildCtx<'prog> {
pd: ast::PortDef,
owner: ir::PortOwner,
) -> BuildRes<PortIdx> {
let (name, p) = match pd {
ast::PortDef::Port {
name,
liveness,
bitwidth,
} => {
let info = self.comp().add(ir::Info::port(
name.copy(),
name.pos(),
bitwidth.pos(),
liveness.pos(),
));

// The bundle type uses a fake bundle index and has a length of 1.
// We don't need to push a new scope because this type is does not
// bind any new parameters.
let p_name = self.gen_name();
let live = self.try_with_scope(|ctx| {
Ok(ir::Liveness {
idxs: vec![ctx.param(
p_name.into(),
let ast::Bundle {
name,
typ:
ast::BundleType {
idx,
len,
liveness,
bitwidth,
},
} = pd;

let info = self.comp().add(ir::Info::port(
name.copy(),
name.pos(),
bitwidth.pos(),
liveness.pos(),
));
// Construct the bundle type in a new scope.
let live = self.try_with_scope(|ctx| {
Ok(ir::Liveness {
idxs: idx
.into_iter()
.map(|idx| {
ctx.param(
// Updated after the port is constructed
ir::ParamOwner::bundle(ir::PortIdx::UNKNOWN),
)], // This parameter is unused
lens: vec![ctx.comp().num(1)],
range: ctx.range(liveness.take())?,
})
})?;
let p = ir::Port {
width: self.expr(bitwidth.take())?,
owner,
live,
info,
};
(name, p)
}
ast::PortDef::Bundle(ast::Bundle {
name,
typ:
ast::BundleType {
idx,
len,
liveness,
bitwidth,
},
}) => {
let info = self.comp().add(ir::Info::port(
name.copy(),
name.pos(),
bitwidth.pos(),
liveness.pos(),
));
// Construct the bundle type in a new scope.
let live = self.try_with_scope(|ctx| {
Ok(ir::Liveness {
idxs: idx
.into_iter()
.map(|idx| {
ctx.param(
// Updated after the port is constructed
idx,
ir::ParamOwner::bundle(PortIdx::UNKNOWN),
)
})
.collect_vec(),
lens: len
.into_iter()
.map(|len| ctx.expr(len.take()))
.collect::<BuildRes<Vec<_>>>()?,
range: ctx.range(liveness.take())?,
idx,
ir::ParamOwner::bundle(PortIdx::UNKNOWN),
)
})
})?;
let p = ir::Port {
width: self.expr(bitwidth.take())?,
owner,
live,
info,
};
(name, p)
}
.collect_vec(),
lens: len
.into_iter()
.map(|len| ctx.expr(len.take()))
.collect::<BuildRes<Vec<_>>>()?,
range: ctx.range(liveness.take())?,
})
})?;
let p = ir::Port {
width: self.expr(bitwidth.take())?,
owner,
live,
info,
};

// Defines helper variable here due to lifetime issues
Expand Down Expand Up @@ -832,7 +794,7 @@ impl<'prog> BuildCtx<'prog> {
for ((p, idx), src) in sig.inputs.clone().into_iter().zip(srcs) {
let info = self
.comp()
.add(ir::Info::connect(p.inner().name().pos(), src.pos()));
.add(ir::Info::connect(p.inner().name.pos(), src.pos()));
let resolved = p.map(|p| {
p.resolve_exprs(&param_binding)
.resolve_event(&event_binding)
Expand Down Expand Up @@ -1035,8 +997,7 @@ impl<'prog> BuildCtx<'prog> {
}
ast::Command::Bundle(bun) => {
// Add the bundle to the current scope
let idx =
self.port(ast::PortDef::Bundle(bun), ir::PortOwner::Local)?;
let idx = self.port(bun, ir::PortOwner::Local)?;
vec![ir::Command::from(idx)]
}
};
Expand Down
11 changes: 0 additions & 11 deletions crates/ir/src/from_ast/build_ctx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,6 @@ pub(super) struct BuildCtx<'prog> {
/// The parameter map returns instead of a [ir::ParamIdx] because let-bound
/// parameters are immediately rewritten.
param_map: ScopeMap<ir::ExprIdx, OwnedParam>,

/// Index for generating unique names
name_idx: u32,
}

impl<'prog> BuildCtx<'prog> {
Expand All @@ -122,7 +119,6 @@ impl<'prog> BuildCtx<'prog> {
comp,
sigs,
diag: utils::Diagnostics::default(),
name_idx: 0,
param_map: ScopeMap::new(),
event_map: ScopeMap::new(),
port_map: ScopeMap::new(),
Expand All @@ -148,13 +144,6 @@ impl<'prog> BuildCtx<'prog> {
self.comp
}

/// Generate a unique, new name for unused parameters
pub fn gen_name(&mut self) -> Id {
let name = format!("_{}", self.name_idx);
self.name_idx += 1;
Id::from(name)
}

/// Push a new scope level
#[inline]
fn push(&mut self) {
Expand Down

0 comments on commit b10ea3f

Please sign in to comment.