Skip to content

Commit 7e8ed87

Browse files
authored
Reworking attributes to match Ctx (#474)
* rewrite the attribute system * clean up parser code * macro-ize ports * remove port attrs * cleanup unused imports * more unused imports * rename exports * move to macros rs
1 parent bd39fe7 commit 7e8ed87

File tree

21 files changed

+406
-145
lines changed

21 files changed

+406
-145
lines changed

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ atty = "0.2"
3333
lazy_static = "1.4"
3434
easy-smt = { version = "0.2.1" }
3535
struct-variant = "1.0"
36+
strum = "0.26.3"
37+
strum_macros = "0.26.3"
3638

3739
fil-utils = { version = "0.1.0", path = "crates/utils" }
3840
fil-ast = { version = "0.1.0", path = "crates/ast" }

crates/ast/Cargo.toml

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,8 @@ lazy_static.workspace = true
2222
struct-variant.workspace = true
2323
pest.workspace = true
2424
pest_consume.workspace = true
25+
strum_macros.workspace = true
26+
strum.workspace = true
2527

2628
fil-utils.workspace = true
27-
fil-gen.workspace = true
28-
strum = "0.26.3"
29-
strum_macros = "0.26.3"
30-
enumflags2 = "0.7.10"
31-
enum-map = "2.7.3"
29+
fil-gen.workspace = true

crates/ast/src/attribute.rs

Lines changed: 0 additions & 82 deletions
This file was deleted.

crates/ast/src/component.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
use crate::BoolAttr;
2-
31
use super::{Command, Id, Signature};
42
use fil_gen as gen;
3+
use fil_utils::{self as utils, AttrCtx};
54
use gen::GenConfig;
65
use std::path::PathBuf;
76

@@ -96,7 +95,7 @@ impl Namespace {
9695
pub fn main_idx(&self) -> Option<usize> {
9796
self.components
9897
.iter()
99-
.position(|c| c.sig.attributes.has(BoolAttr::TopLevel))
98+
.position(|c| c.sig.attributes.has(utils::CompBool::TopLevel))
10099
}
101100

102101
/// Get the toplevel component name

crates/ast/src/lib.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
mod attribute;
21
mod bind_map;
32
mod component;
43
mod constraint;
@@ -12,7 +11,6 @@ mod port;
1211
mod signature;
1312
mod time;
1413

15-
pub use attribute::{Attr, Attributes, BoolAttr, NumAttr};
1614
pub use bind_map::Binding;
1715
pub use component::{Component, Extern, Namespace};
1816
pub use constraint::{Constraint, OrderConstraint, OrderOp};

crates/ast/src/parser.rs

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@
22
#![allow(clippy::type_complexity)]
33

44
//! Parser for Filament programs.
5-
use crate::{self as ast, Attributes, Loc, TimeSub};
6-
use fil_utils::{self as utils, FilamentResult};
7-
use fil_utils::{FileIdx, GPosIdx, GlobalPositionTable};
5+
use crate::{self as ast, Loc, TimeSub};
6+
use fil_utils::{
7+
self as utils, AttrCtx, FilamentResult, FileIdx, GPosIdx,
8+
GlobalPositionTable,
9+
};
810
use itertools::Itertools;
911
use pest::pratt_parser::{Assoc, Op, PrattParser};
1012
use pest_consume::{match_nodes, Error, Parser};
1113
use std::fs;
14+
use std::hash::Hash;
1215
use std::path::Path;
1316
use std::str::FromStr;
1417

@@ -830,29 +833,30 @@ impl FilamentParser {
830833
Ok(())
831834
}
832835

833-
fn attr_bind(input: Node) -> ParseResult<Vec<(ast::Attr, GPosIdx, u64)>> {
834-
match_nodes!(
835-
input.clone().into_children();
836-
[identifier(name)] =>
837-
ast::BoolAttr::from_str(name.as_ref()).map(
838-
|attr| vec![(ast::Attr::Bool(attr), name.pos(), 1)]).map_err(
839-
|_| input.error(format!("Found unknown attribute flag \"{name}\""))),
840-
[not(_), identifier(name)] =>
841-
ast::BoolAttr::from_str(name.as_ref()).map(
842-
|attr| vec![(ast::Attr::Bool(attr), name.pos(), 0)]).map_err(
843-
|_| input.error(format!("Found unknown attribute flag \"{name}\""))),
844-
[identifier(name), bitwidth(val)] =>
845-
ast::NumAttr::from_str(name.as_ref()).map(
846-
|attr| vec![(ast::Attr::Num(attr), name.pos(), val)]).map_err(
847-
|_| input.error(format!("Found unknown numeric attribute \"{name}\""))),
848-
)
849-
}
836+
fn attributes<Bool, Num>(
837+
input: Node,
838+
) -> ParseResult<utils::Attributes<Bool, Num>>
839+
where
840+
Bool: FromStr + Hash + Eq + Copy,
841+
Num: FromStr + Hash + Eq + Copy,
842+
{
843+
let mut attrs = utils::Attributes::default();
844+
for attr in input.into_children() {
845+
match_nodes!(
846+
attr.clone().into_children();
847+
[identifier(name)] => Bool::from_str(name.as_ref()).map(
848+
|attr| attrs.set(attr, true, name.pos())).map_err(
849+
|_| attr.error(format!("Found unknown attribute flag \"{name}\""))),
850+
[not(_), identifier(name)] => Bool::from_str(name.as_ref()).map(
851+
|attr| attrs.set(attr, false, name.pos())).map_err(
852+
|_| attr.error(format!("Found unknown attribute flag \"{name}\""))),
853+
[identifier(name), bitwidth(val)] => Num::from_str(name.as_ref()).map(
854+
|attr| attrs.set(attr, val, name.pos())).map_err(
855+
|_| attr.error(format!("Found unknown numeric attribute \"{name}\""))),
856+
)?;
857+
}
850858

851-
fn attributes(input: Node) -> ParseResult<Attributes> {
852-
Ok(match_nodes!(
853-
input.into_children();
854-
[attr_bind(attr)..] => ast::Attributes::new(attr.into_iter().flatten())
855-
))
859+
Ok(attrs)
856860
}
857861

858862
fn component(input: Node) -> ParseResult<ast::Component> {

crates/ast/src/signature.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ use super::{
22
Binding, Expr, Id, InterfaceDef, Loc, OrderConstraint, PortDef, Time,
33
TimeSub,
44
};
5-
use crate::Attributes;
6-
use fil_utils::GPosIdx;
5+
use fil_utils::{CompAttrs, GPosIdx};
76

87
#[derive(Clone)]
98
/// An event variable bound in the signature
@@ -113,7 +112,7 @@ pub struct Signature {
113112
/// Name of the component
114113
pub name: Loc<Id>,
115114
/// Attributes associated with this component
116-
pub attributes: Attributes,
115+
pub attributes: CompAttrs,
117116
/// Parameters for the Signature
118117
pub params: Vec<Loc<ParamBind>>,
119118
/// Parameters bound in the signature binding. These always have a default value.
@@ -139,7 +138,7 @@ impl Signature {
139138
#[allow(clippy::too_many_arguments)]
140139
pub fn new(
141140
name: Loc<Id>,
142-
attributes: Attributes,
141+
attributes: CompAttrs,
143142
params: Vec<Loc<ParamBind>>,
144143
events: Vec<Loc<EventBind>>,
145144
unannotated_ports: Vec<(Id, u64)>,

crates/filament/src/ast_passes/toplevel.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::ast_visitor::{Action, Visitor};
22
use fil_ast as ast;
3-
use fil_utils::{Diagnostics, Error, GPosIdx};
3+
use fil_utils::{self as utils, AttrCtx, Diagnostics, Error, GPosIdx};
44

55
/// Sets the proper FSM Attributes for every component
66
#[derive(Default)]
@@ -17,7 +17,7 @@ impl Visitor for TopLevel {
1717
}
1818

1919
fn signature(&mut self, sig: &mut ast::Signature) -> Action {
20-
if sig.attributes.get(ast::BoolAttr::TopLevel) == Some(1) {
20+
if sig.attributes.get(utils::CompBool::TopLevel) == Some(&true) {
2121
if self.has_toplevel.is_some() {
2222
let err = Error::malformed("Multiple top-level components")
2323
.add_note(self.diag.add_info(
@@ -28,15 +28,15 @@ impl Visitor for TopLevel {
2828
self.diag.add_info(
2929
"second top-level component here",
3030
sig.attributes
31-
.get_loc(ast::BoolAttr::TopLevel)
31+
.get_loc(utils::CompBool::TopLevel)
3232
.unwrap(),
3333
),
3434
);
3535

3636
self.diag.add_error(err);
3737
} else {
3838
self.has_toplevel = Some(
39-
sig.attributes.get_loc(ast::BoolAttr::TopLevel).unwrap(),
39+
sig.attributes.get_loc(utils::CompBool::TopLevel).unwrap(),
4040
);
4141
}
4242
}
@@ -47,14 +47,14 @@ impl Visitor for TopLevel {
4747

4848
fn external(&mut self, ext: &mut ast::Extern) {
4949
for sig in &mut ext.comps {
50-
if sig.attributes.get(ast::BoolAttr::TopLevel) == Some(1) {
50+
if sig.attributes.get(utils::CompBool::TopLevel) == Some(&true) {
5151
let err =
5252
Error::malformed("External components cannot be top-level")
5353
.add_note(
5454
self.diag.add_info(
5555
"toplevel attribute here",
5656
sig.attributes
57-
.get_loc(ast::BoolAttr::TopLevel)
57+
.get_loc(utils::CompBool::TopLevel)
5858
.unwrap(),
5959
),
6060
);
@@ -74,7 +74,11 @@ impl Visitor for TopLevel {
7474
for comp in ast.components.iter_mut() {
7575
if comp.sig.name.as_ref() == "main" {
7676
// Add the toplevel attribute to the component
77-
comp.sig.attributes.set(ast::BoolAttr::TopLevel, 1);
77+
comp.sig.attributes.set(
78+
utils::CompBool::TopLevel,
79+
true,
80+
GPosIdx::UNKNOWN,
81+
);
7882

7983
return;
8084
}

crates/filament/src/ir_passes/fsm_attributes.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::ir_visitor::{Action, Visitor, VisitorData};
2-
use fil_ast as ast;
32
use fil_ir::TimeSub;
3+
use fil_utils::{self as utils, AttrCtx};
44

55
/// Sets the proper FSM Attributes for every component
66
#[derive(Default)]
@@ -15,7 +15,7 @@ impl Visitor for FSMAttributes {
1515
let attrs = &data.comp.attrs;
1616

1717
// Check if the component already has FSM attributes
18-
if attrs.get(ast::BoolAttr::CounterFSM).is_some() {
18+
if attrs.get(utils::CompBool::CounterFSM).is_some() {
1919
return Action::Stop;
2020
}
2121

@@ -37,12 +37,20 @@ impl Visitor for FSMAttributes {
3737
// If the delay is > 1, add a slow FSM attribute
3838
// TODO(UnsignedByte): Find a better heuristic for slow FSMs
3939
if delay > 1 {
40-
data.comp.attrs.set(ast::BoolAttr::CounterFSM, 1);
40+
data.comp.attrs.set(
41+
utils::CompBool::CounterFSM,
42+
true,
43+
utils::GPosIdx::UNKNOWN,
44+
);
4145
return Action::Stop;
4246
}
4347
}
4448

45-
data.comp.attrs.set(ast::BoolAttr::CounterFSM, 0);
49+
data.comp.attrs.set(
50+
utils::CompBool::CounterFSM,
51+
false,
52+
utils::GPosIdx::UNKNOWN,
53+
);
4654

4755
Action::Stop
4856
}

crates/filament/src/ir_passes/lower/build_ctx.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ use super::fsm::{FsmBind, FsmType};
22
use super::utils::{cell_to_port_def, NameGenerator};
33
use super::Fsm;
44
use calyx_ir::{self as calyx, RRC};
5-
use fil_ast as ast;
65
use fil_ir::{self as ir, Ctx, DenseIndexInfo, DisplayCtx};
6+
use fil_utils::{self as utils, AttrCtx};
77
use itertools::Itertools;
88
use std::{collections::HashMap, rc::Rc};
99

@@ -235,12 +235,10 @@ impl<'a> BuildCtx<'a> {
235235
};
236236
let delay = delay.concrete(self.comp);
237237
let typ =
238-
match self.comp.attrs.get(ast::BoolAttr::CounterFSM).unwrap() {
239-
0 => FsmType::Simple(states),
240-
1 => {
241-
FsmType::CounterChain(states, delay)
242-
}
243-
v => unreachable!("Encountered boolean attribute counter_fsm with non-boolean value {}", v),
238+
match self.comp.attrs.get(utils::CompBool::CounterFSM).unwrap()
239+
{
240+
false => FsmType::Simple(states),
241+
true => FsmType::CounterChain(states, delay),
244242
};
245243
self.implement_fsm(&typ);
246244

0 commit comments

Comments
 (0)