use std::{hash::Hash, str::FromStr, sync::Arc};
use crate::{
ast::{AttriValue, GroupComments, GroupFn, NamedGroup, SimpleAttri, SimpleWrapper},
common::items::WordSet,
expression::IdBooleanExpression,
pin::Direction,
timing::items::Mode,
ArcStr,
};
use itertools::Itertools;
#[derive(Default, Debug, Clone)]
#[derive(liberty_macros::Group)]
#[mut_set_derive::item(
sort,
macro(derive(Debug, Clone,Default);)
)]
#[derive(serde::Serialize, serde::Deserialize)]
pub struct LeakagePower {
#[id]
#[liberty(name)]
name: Vec<ArcStr>,
#[liberty(comments)]
pub comments: GroupComments<Self>,
#[liberty(undefined)]
pub undefined: crate::ast::AttributeList,
#[id]
#[liberty(simple(type = Option))]
power_level: Option<ArcStr>,
#[id]
#[liberty(simple)]
related_pg_pin: WordSet,
#[id]
#[liberty(simple(type = Option))]
when: Option<IdBooleanExpression>,
#[liberty(simple)]
value: f64,
#[liberty(complex(type = Option))]
mode: Option<Mode>,
}
impl GroupFn for LeakagePower {}
#[derive(Default, Debug, Clone)]
#[derive(liberty_macros::Group)]
#[mut_set_derive::item(
sort,
macro(derive(Debug, Clone,Default);)
)]
#[derive(serde::Serialize, serde::Deserialize)]
pub struct Statetable {
#[id]
#[liberty(name)]
pub input_nodes: Vec<ArcStr>,
#[id]
#[liberty(name)]
pub internal_nodes: Vec<ArcStr>,
#[liberty(comments)]
pub comments: GroupComments<Self>,
#[liberty(undefined)]
pub undefined: crate::ast::AttributeList,
#[liberty(simple)]
pub table: Table,
}
impl GroupFn for Statetable {}
impl NamedGroup for Statetable {
#[inline]
fn parse(mut v: Vec<ArcStr>) -> Result<Self::Name, crate::ast::IdError> {
let l = v.len();
if l != 2 {
return Err(crate::ast::IdError::LengthDismatch(2, l, v));
}
if let Some(var2) = v.pop() {
if let Some(var1) = v.pop() {
Ok(Self::Name {
input_nodes: var1.split_ascii_whitespace().map(ArcStr::from).collect(),
internal_nodes: var2.split_ascii_whitespace().map(ArcStr::from).collect(),
})
} else {
Err(crate::ast::IdError::Other("Unkown pop error".into()))
}
} else {
Err(crate::ast::IdError::Other("Unkown pop error".into()))
}
}
#[inline]
fn name2vec(name: Self::Name) -> Vec<ArcStr> {
vec![name.input_nodes.join(" ").into(), name.internal_nodes.join(" ").into()]
}
}
#[derive(Default, Debug, Clone)]
#[derive(serde::Serialize, serde::Deserialize)]
pub struct Table {
pub v: Vec<ArcStr>,
}
impl std::fmt::Display for Table {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
std::fmt::Debug::fmt(&self.v, f)
}
}
impl FromStr for Table {
type Err = std::fmt::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(Self {
v: s
.split("\\\n")
.filter_map(|line| {
let _l = line
.trim_start()
.trim_end_matches(|c: char| c == ',' || c.is_ascii_whitespace());
if _l == "" {
None
} else {
Some(ArcStr::from(_l))
}
})
.collect(),
})
}
}
impl SimpleAttri for Table {
#[inline]
fn nom_parse<'a>(
i: &'a str,
line_num: &mut usize,
) -> nom::IResult<
&'a str,
Result<Self, (Self::Err, AttriValue)>,
nom::error::Error<&'a str>,
> {
let (input, simple_multi) = crate::ast::parser::simple_multi(i, line_num)?;
match Self::parse(simple_multi) {
Ok(s) => Ok((input, Ok(s))),
Err(e) => {
Ok((input, Err((e, crate::ast::AttriValue::Simple(ArcStr::from(simple_multi))))))
}
}
}
#[inline]
fn to_wrapper(&self) -> SimpleWrapper {
self.v.join(" ,\\\n").into()
}
}
#[test]
fn statetable_test() {
let _ = crate::ast::test_parse_group::<Statetable>(
r#"(" CLK EN SE",ENL) {
table : " H L L : - : L ,\
H L H : - : H ,\
H H L : - : H ,\
H H H : - : H ,\
L - - : - : N ";
}
"#,
);
}
#[derive(Debug, Default, Clone)]
#[derive(liberty_macros::Group)]
#[mut_set_derive::item(
sort,
macro(derive(Debug, Clone,Default);)
)]
#[derive(serde::Serialize, serde::Deserialize)]
pub struct PgPin {
#[liberty(name)]
#[id]
name: Option<ArcStr>,
#[liberty(comments)]
pub comments: GroupComments<Self>,
#[liberty(undefined)]
pub undefined: crate::ast::AttributeList,
#[liberty(simple(type = Option))]
pub direction: Option<Direction>,
#[liberty(simple)]
pub voltage_name: ArcStr,
#[liberty(simple)]
pub pg_type: PgType,
#[liberty(simple)]
pub user_pg_type: ArcStr,
#[liberty(simple)]
pub physical_connection: ArcStr,
#[liberty(simple)]
pub related_bias_pin: ArcStr,
}
impl GroupFn for PgPin {}
#[derive(Debug, Clone, Copy)]
#[derive(Hash, PartialEq, Eq)]
#[derive(Ord, PartialOrd, Default)]
#[derive(strum_macros::EnumString, strum_macros::EnumIter, strum_macros::Display)]
#[derive(serde::Serialize, serde::Deserialize)]
pub enum PgType {
#[strum(serialize = "primary_power")]
PrimaryPower,
#[strum(serialize = "primary_ground")]
PrimaryGround,
#[strum(serialize = "backup_power")]
BackupPower,
#[strum(serialize = "backup_ground")]
#[default]
BackupGround,
#[strum(serialize = "internal_power")]
InternalPower,
#[strum(serialize = "internal_ground")]
InternalGround,
#[strum(serialize = "pwell")]
Pwell,
#[strum(serialize = "nwell")]
Nwell,
#[strum(serialize = "deepnwell")]
DeepNwell,
#[strum(serialize = "deeppwell")]
DeepPwell,
}
impl SimpleAttri for PgType {}