Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add attributes for components and implement counter_fsm attribute #453

Merged
merged 13 commits into from
Sep 4, 2024
Prev Previous commit
Next Next commit
transfer attribute changes
  • Loading branch information
UnsignedByte committed Sep 4, 2024
commit ff65552abb5c909741a695c3d2a7fdf496fcbd21
5 changes: 0 additions & 5 deletions apps/fft/test/rand.json

This file was deleted.

18 changes: 12 additions & 6 deletions crates/ast/src/attribute.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use enum_map::{Enum, EnumMap};
use fil_utils::GPosIdx;
use strum_macros::EnumString;

/// An attribute that accepts a numeric value
Expand All @@ -21,7 +22,7 @@ pub enum BoolAttr {

/// Represents a single attribute. This is a private enum that is used during
/// parsing to collect all attributes before creating the [Attributes] struct.
#[derive(Enum)]
#[derive(Enum, Clone, Copy)]
pub enum Attr {
Bool(BoolAttr),
Num(NumAttr),
Expand All @@ -42,24 +43,29 @@ impl From<NumAttr> for Attr {
/// A set of attributes attached to a component
#[derive(Default, Clone)]
pub struct Attributes {
attrs: EnumMap<Attr, Option<u64>>,
attrs: EnumMap<Attr, Option<(u64, GPosIdx)>>,
}

impl Attributes {
pub fn new(attrs: impl Iterator<Item = (Attr, u64)>) -> Self {
pub fn new(attrs: impl Iterator<Item = (Attr, GPosIdx, u64)>) -> Self {
Self {
attrs: attrs.map(|(attr, v)| (attr, Some(v))).collect(),
attrs: attrs.map(|(attr, l, v)| (attr, Some((v, l)))).collect(),
}
}

/// Get the value of an attribute.
pub fn get(&self, attr: impl Into<Attr>) -> Option<u64> {
self.attrs[attr.into()]
self.attrs[attr.into()].map(|(v, _)| v)
}

/// Get the location of an attribute.
pub fn get_loc(&self, attr: impl Into<Attr>) -> Option<GPosIdx> {
self.attrs[attr.into()].map(|(_, l)| l)
}

/// Set the value of an attribute
pub fn set(&mut self, attr: impl Into<Attr>, value: u64) {
self.attrs[attr.into()] = Some(value);
self.attrs[attr.into()] = Some((value, GPosIdx::UNKNOWN));
}

/// Remove an attribute
Expand Down
8 changes: 3 additions & 5 deletions crates/ast/src/component.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::{Attributes, Command, Id, Signature};
use super::{Command, Id, Signature};
use fil_gen as gen;
use gen::GenConfig;
use std::path::PathBuf;
Expand Down Expand Up @@ -36,13 +36,11 @@ pub struct Component {
pub sig: Signature,
/// Model for this component
pub body: Vec<Command>,
/// Attributes for this component
pub attrs: Attributes,
}

impl Component {
pub fn new(sig: Signature, body: Vec<Command>, attrs: Attributes) -> Self {
Self { sig, body, attrs }
pub fn new(sig: Signature, body: Vec<Command>) -> Self {
Self { sig, body }
}
}

Expand Down
15 changes: 9 additions & 6 deletions crates/ast/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -627,6 +627,7 @@ impl FilamentParser {
Ok(match_nodes!(
input.into_children();
[
attributes(attributes),
identifier(name),
params(params),
abstract_var(abstract_vars),
Expand All @@ -637,6 +638,7 @@ impl FilamentParser {
let (inputs, outputs, interface_signals, unannotated_ports) = io;
ast::Signature::new(
name,
attributes,
params,
abstract_vars,
unannotated_ports,
Expand All @@ -649,6 +651,7 @@ impl FilamentParser {
)
},
[
attributes(attributes),
identifier(name),
params(params),
io(io),
Expand All @@ -658,6 +661,7 @@ impl FilamentParser {
let (inputs, outputs, interface_signals, unannotated_ports) = io;
ast::Signature::new(
name,
attributes,
params,
vec![],
unannotated_ports,
Expand Down Expand Up @@ -825,20 +829,20 @@ impl FilamentParser {
Ok(())
}

fn attr_bind(input: Node) -> ParseResult<Vec<(ast::Attr, u64)>> {
fn attr_bind(input: Node) -> ParseResult<Vec<(ast::Attr, GPosIdx, u64)>> {
match_nodes!(
input.clone().into_children();
[identifier(name)] =>
ast::BoolAttr::from_str(name.as_ref()).map(
|attr| vec![(ast::Attr::Bool(attr), 1)]).map_err(
|attr| vec![(ast::Attr::Bool(attr), name.pos(), 1)]).map_err(
|_| input.error(format!("Found unknown attribute flag \"{name}\""))),
[not(_), identifier(name)] =>
ast::BoolAttr::from_str(name.as_ref()).map(
|attr| vec![(ast::Attr::Bool(attr), 0)]).map_err(
|attr| vec![(ast::Attr::Bool(attr), name.pos(), 0)]).map_err(
|_| input.error(format!("Found unknown attribute flag \"{name}\""))),
[identifier(name), bitwidth(val)] =>
ast::NumAttr::from_str(name.as_ref()).map(
|attr| vec![(ast::Attr::Num(attr), val)]).map_err(
|attr| vec![(ast::Attr::Num(attr), name.pos(), val)]).map_err(
|_| input.error(format!("Found unknown numeric attribute \"{name}\""))),
)
}
Expand All @@ -854,11 +858,10 @@ impl FilamentParser {
match_nodes!(
input.into_children();
[
attributes(attributes),
signature(sig),
command(body)..
] => {
Ok(ast::Component::new(sig, body.into_iter().flatten().collect(), attributes))
Ok(ast::Component::new(sig, body.into_iter().flatten().collect()))
}
)
}
Expand Down
5 changes: 5 additions & 0 deletions crates/ast/src/signature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use super::{
Binding, Expr, Id, InterfaceDef, Loc, OrderConstraint, PortDef, Time,
TimeSub,
};
use crate::Attributes;
use fil_utils::GPosIdx;

#[derive(Clone)]
Expand Down Expand Up @@ -111,6 +112,8 @@ impl SigBind {
pub struct Signature {
/// Name of the component
pub name: Loc<Id>,
/// Attributes associated with this component
pub attributes: Attributes,
/// Parameters for the Signature
pub params: Vec<Loc<ParamBind>>,
/// Parameters bound in the signature binding. These always have a default value.
Expand All @@ -136,6 +139,7 @@ impl Signature {
#[allow(clippy::too_many_arguments)]
pub fn new(
name: Loc<Id>,
attributes: Attributes,
params: Vec<Loc<ParamBind>>,
events: Vec<Loc<EventBind>>,
unannotated_ports: Vec<(Id, u64)>,
Expand All @@ -150,6 +154,7 @@ impl Signature {
inputs.append(&mut outputs);
Self {
name,
attributes,
params,
sig_bindings,
events,
Expand Down
9 changes: 5 additions & 4 deletions crates/ast/src/syntax.pest
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ param_bind = {
}

signature = {
identifier ~ params ~ abstract_var? ~ io ~ sig_bindings ~ constraints
attributes ~ "comp" ~ identifier ~ params ~ abstract_var? ~ io ~ sig_bindings ~ constraints
}

attributes = {
Expand All @@ -78,13 +78,14 @@ attr_bind = {
}

component = {
attributes ~ "comp" ~ signature ~ "{" ~ command* ~ "}"
signature ~ "{" ~ command* ~ "}"
}

external = {
"extern" ~ string_lit ~ "{" ~ ("comp" ~ signature ~ ";")* ~ "}"
"extern" ~ string_lit ~ "{" ~ (signature ~ ";")* ~ "}"
}
generate = {
"generate" ~ "(" ~ identifier ~ ")" ~ "using" ~ string_lit ~ "{" ~ ("comp" ~ signature ~ ";")* ~ "}"
"generate" ~ "(" ~ identifier ~ ")" ~ "using" ~ string_lit ~ "{" ~ (signature ~ ";")* ~ "}"
}

comp_or_ext = {
Expand Down
12 changes: 2 additions & 10 deletions crates/ir/src/from_ast/astconv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1085,8 +1085,6 @@ struct ComponentTransform {
typ: TypeInfo,
/// Signature of the component
sig: ast::Signature,
/// Attributes of the component
attrs: ast::Attributes,
}

/// Convert an [ast::Component] into a source [ComponentTransform]
Expand All @@ -1095,7 +1093,6 @@ impl From<ast::Component> for ComponentTransform {
Self {
typ: TypeInfo::Source(comp.body),
sig: comp.sig,
attrs: comp.attrs,
}
}
}
Expand All @@ -1111,7 +1108,6 @@ impl From<ast::Extern> for ComponentTransform {
Self {
typ,
sig: ast::Signature::default(),
attrs: ast::Attributes::default(),
}
}
}
Expand Down Expand Up @@ -1150,11 +1146,7 @@ fn try_transform(ns: ast::Namespace) -> BuildRes<ir::Context> {
} else {
TypeInfo::External(path.clone())
};
ComponentTransform {
typ,
sig: comp,
attrs: Default::default(),
}
ComponentTransform { typ, sig: comp }
})
})
// add signatures of components as well as their command bodies
Expand All @@ -1180,7 +1172,7 @@ fn try_transform(ns: ast::Namespace) -> BuildRes<ir::Context> {
TypeInfo::Source(_) => ir::CompType::Source,
TypeInfo::External(_) => ir::CompType::External,
TypeInfo::Generated(_) => ir::CompType::Generated,
}, comp_ctx.attrs), &sig_map);
}, comp_ctx.sig.attributes.clone()), &sig_map);

// enable source information saving if this is main
if ctx.is_main(idx) {
Expand Down