Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions examples/nic.rsf
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ block Main {
}

/// Phy registers.
#[attr(foo = "this is an attribute")]
#[attr(bar = "embedded quota\" attr")]
block Phy {
/// Configuration register.
config: PhyConfig @ 0x200,
Expand Down
12 changes: 12 additions & 0 deletions lib/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ impl Emit for Register {
for x in &self.doc {
writeln!(f, "///{x}")?;
}
for x in &self.attrs {
writeln!(f, "{x}")?;
}
writeln!(
f,
"{}register<{}> {}{} {{",
Expand Down Expand Up @@ -104,6 +107,9 @@ impl Emit for Field {
for x in &self.doc {
writeln!(f, " ///{x}")?;
}
for x in &self.attrs {
writeln!(f, " {x}")?;
}
write!(
f,
" {}: {} {}",
Expand Down Expand Up @@ -142,6 +148,9 @@ impl Emit for Block {
for x in &self.doc {
writeln!(f, "///{x}")?;
}
for x in &self.attrs {
writeln!(f, "{x}")?;
}
writeln!(
f,
"{}block {} {{",
Expand All @@ -164,6 +173,9 @@ impl Emit for BlockElement {
for x in &self.doc {
writeln!(f, " ///{x}")?;
}
for x in &self.attrs {
writeln!(f, " {x}")?;
}
self.component.emit(f)?;
write!(f, " @ {},", self.offset.to_code())?;
writeln!(f)
Expand Down
32 changes: 31 additions & 1 deletion lib/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,46 @@ use std::{fmt::Display, ops::Range};

use crate::ast::Emit;

#[derive(Debug, Clone, PartialEq)]
pub struct Attribute {
pub id: Identifier,
pub value: String,
}

impl Display for Attribute {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"#[attr({name} = \"{value}\")]",
name = self.id.name,
value = self.value,
)
}
}

#[derive(Debug, Clone, PartialEq)]
pub struct Enum {
pub doc: Vec<String>,
pub id: Identifier,
pub width: Number,
pub alternatives: Vec<Alternative>,
pub attrs: Vec<Attribute>,
}

impl Emit for Enum {
fn emit(&self, f: &mut impl std::fmt::Write) -> std::fmt::Result {
for x in &self.doc {
writeln!(f, "///{x}")?;
}
writeln!(f, "enum<{}> {} {{", self.width.to_code(), self.id.name)?;
for x in &self.attrs {
writeln!(f, "{x}")?;
}
writeln!(
f,
"enum<{width}> {name} {{",
width = self.width.to_code(),
name = self.id.name
)?;
for x in &self.alternatives {
x.emit(f)?;
}
Expand Down Expand Up @@ -90,6 +116,7 @@ pub struct Field<T> {
pub mode: FieldMode,
pub typ: T,
pub offset: Number,
pub attrs: Vec<Attribute>,
}

#[derive(Debug, Clone, PartialEq)]
Expand All @@ -100,6 +127,7 @@ pub struct Register<T> {
pub reset_value: Option<Number>,
pub sram: bool,
pub fields: Vec<Field<T>>,
pub attrs: Vec<Attribute>,
}

#[derive(Debug, Clone, PartialEq)]
Expand All @@ -108,13 +136,15 @@ pub struct Block<T> {
pub id: Identifier,
pub sram: bool,
pub elements: Vec<BlockElement<T>>,
pub attrs: Vec<Attribute>,
}

#[derive(Debug, Clone, PartialEq)]
pub struct BlockElement<T> {
pub doc: Vec<String>,
pub component: Component<T>,
pub offset: Number,
pub attrs: Vec<Attribute>,
}

#[derive(Debug, PartialEq, Clone)]
Expand Down
29 changes: 27 additions & 2 deletions lib/src/model.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub use crate::common::Attribute;
pub use crate::common::Enum;

use crate::{
Expand Down Expand Up @@ -76,6 +77,7 @@ pub struct Model {
pub enums: Vec<Arc<Enum>>,
pub registers: Vec<Arc<Register>>,
pub blocks: Vec<Arc<Block>>,
pub attrs: Vec<Arc<Attribute>>,
}

#[derive(Debug, Clone, PartialEq)]
Expand Down Expand Up @@ -211,6 +213,9 @@ impl Model {

impl Display for Model {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
for x in &self.attrs {
writeln!(f, "{x}")?;
}
let name = if self.id.is_empty() {
"root"
} else {
Expand All @@ -235,13 +240,19 @@ impl Display for Register {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
writeln!(
f,
"{}\n{} {}{}{}{}",
"{}\n{}{} {}{}{}{}",
self.doc
.iter()
.map(|x| x.trim())
.collect::<Vec<_>>()
.join("\n ")
.dimmed(),
self.attrs
.iter()
.map(|attr| format!("{attr}\n"))
.collect::<Vec<_>>()
.join("")
.dimmed(),
"register".blue(),
self.id.name.cyan(),
"<".dimmed(),
Expand All @@ -263,13 +274,19 @@ impl Display for Field {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"{}\n {}{} {} {}",
"{}\n {}\n {}{} {} {}",
self.doc
.iter()
.map(|x| x.trim())
.collect::<Vec<_>>()
.join("\n ")
.dimmed(),
self.attrs
.iter()
.map(|attr| format!("{attr}\n "))
.collect::<Vec<_>>()
.join("")
.dimmed(),
self.id.name,
":".dimmed(),
self.mode.to_string().blue(),
Expand Down Expand Up @@ -305,6 +322,9 @@ impl Display for Enum {

impl Display for Block {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
for x in &self.attrs {
writeln!(f, "{}", format!("{x}").dimmed())?;
}
writeln!(f, "{} {}", "block".blue(), self.id.name.cyan())?;
for x in &self.elements {
writeln!(f, " {x}")?;
Expand Down Expand Up @@ -475,6 +495,7 @@ impl ModelModules {
enums: vec![],
registers: vec![],
blocks: vec![],
attrs: vec![],
},
};

Expand All @@ -497,6 +518,7 @@ impl ModelModules {
mode: f.mode.clone(),
typ: mm.resolve_field_type(&f.typ)?,
offset: f.offset.clone(),
attrs: f.attrs.clone(),
});
}
mm.root.registers.push(Arc::new(Register {
Expand All @@ -506,6 +528,7 @@ impl ModelModules {
sram: r.sram,
reset_value: r.reset_value.clone(),
fields,
attrs: r.attrs.clone(),
}));
}

Expand Down Expand Up @@ -539,13 +562,15 @@ impl ModelModules {
},
},
offset: e.offset.clone(),
attrs: e.attrs.clone(),
})
}
mm.root.blocks.push(Arc::new(Block {
doc: b.doc.clone(),
id: b.id.clone(),
sram: b.sram,
elements,
attrs: b.attrs.clone(),
}));
}

Expand Down
Loading