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

Support clock and reset cast #886

Merged
merged 1 commit into from
Aug 15, 2024
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
20 changes: 20 additions & 0 deletions crates/analyzer/src/analyzer_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,17 @@ pub enum AnalyzerError {
error_location: SourceSpan,
},

#[diagnostic(severity(Error), code(invalid_cast), help(""), url(""))]
#[error("Casting from #{from} to #{to} is incompatible")]
InvalidCast {
from: String,
to: String,
#[source_code]
input: NamedSource<String>,
#[label("Error location")]
error_location: SourceSpan,
},

#[diagnostic(
severity(Error),
code(missing_default_argument),
Expand Down Expand Up @@ -1078,6 +1089,15 @@ impl AnalyzerError {
}
}

pub fn invalid_cast(from: &str, to: &str, source: &str, token: &TokenRange) -> Self {
AnalyzerError::InvalidCast {
from: from.into(),
to: to.into(),
input: AnalyzerError::named_source(source, token),
error_location: token.into(),
}
}

pub fn invalid_modport_variable_item(
identifier: &str,
source: &str,
Expand Down
53 changes: 47 additions & 6 deletions crates/analyzer/src/evaluator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ use veryl_parser::veryl_grammar_trait::*;
pub enum Evaluated {
Fixed { width: usize, value: isize },
Variable { width: usize },
Clock,
ClockPosedge,
ClockNegedge,
Reset,
ResetAsyncHigh,
ResetAsyncLow,
ResetSyncHigh,
ResetSyncLow,
Unknown,
UnknownStatic, // A temporary enum value to indicate that a value is knowable statically,
// even if, currently, the compiler doesn't know what its value is
Expand All @@ -16,6 +24,24 @@ impl Evaluated {
matches!(self, Evaluated::Fixed { .. } | Evaluated::UnknownStatic)
}

pub fn is_clock(&self) -> bool {
matches!(
self,
Evaluated::Clock | Evaluated::ClockPosedge | Evaluated::ClockNegedge
)
}

pub fn is_reset(&self) -> bool {
matches!(
self,
Evaluated::Reset
| Evaluated::ResetAsyncHigh
| Evaluated::ResetAsyncLow
| Evaluated::ResetSyncHigh
| Evaluated::ResetSyncLow
)
}

fn binary_op<T: Fn(usize, usize) -> usize, U: Fn(isize, isize) -> Option<isize>>(
left: Evaluated,
right: Evaluated,
Expand Down Expand Up @@ -585,10 +611,25 @@ impl Evaluator {
}

fn expression11(&mut self, arg: &Expression11) -> Evaluated {
self.expression12(&arg.expression12)
let ret = self.expression12(&arg.expression12);
if let Some(x) = &arg.expression11_opt {
match x.casting_type.as_ref() {
CastingType::Clock(_) => Evaluated::Clock,
CastingType::ClockPosedge(_) => Evaluated::ClockPosedge,
CastingType::ClockNegedge(_) => Evaluated::ClockNegedge,
CastingType::Reset(_) => Evaluated::Reset,
CastingType::ResetAsyncHigh(_) => Evaluated::ResetAsyncHigh,
CastingType::ResetAsyncLow(_) => Evaluated::ResetAsyncLow,
CastingType::ResetSyncHigh(_) => Evaluated::ResetSyncHigh,
CastingType::ResetSyncLow(_) => Evaluated::ResetSyncLow,
_ => ret,
}
} else {
ret
}
}

fn expression12(&mut self, arg: &Expression12) -> Evaluated {
pub fn expression12(&mut self, arg: &Expression12) -> Evaluated {
let mut ret = self.factor(&arg.factor);
for x in arg.expression12_list.iter().rev() {
let operator = match &*x.expression12_list_group {
Expand Down Expand Up @@ -681,20 +722,20 @@ impl Evaluator {
Evaluated::Variable { width: rwidth } => Evaluated::Variable {
width: ewidth * rwidth,
},
Evaluated::Unknown => Evaluated::Unknown,
Evaluated::UnknownStatic => Evaluated::UnknownStatic,
_ => Evaluated::Unknown,
},
Evaluated::Variable { width: ewidth } => match rep {
Evaluated::Fixed { width: rwidth, .. } | Evaluated::Variable { width: rwidth } => {
Evaluated::Variable {
width: ewidth * rwidth,
}
}
Evaluated::Unknown => Evaluated::Unknown,
Evaluated::UnknownStatic => Evaluated::UnknownStatic,
_ => Evaluated::Unknown,
},
Evaluated::Unknown => Evaluated::Unknown,
Evaluated::UnknownStatic => Evaluated::UnknownStatic,
_ => Evaluated::Unknown,
}
}

Expand Down Expand Up @@ -732,8 +773,8 @@ impl Evaluator {
match self.expression(arg.expression.as_ref()) {
Evaluated::Fixed { .. } => Evaluated::UnknownStatic,
Evaluated::Variable { .. } => Evaluated::Unknown,
Evaluated::Unknown => Evaluated::Unknown,
Evaluated::UnknownStatic => unreachable!(),
_ => Evaluated::Unknown,
}
}

Expand Down
42 changes: 40 additions & 2 deletions crates/analyzer/src/handlers/check_clock_reset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,8 +256,8 @@ impl<'a> VerylGrammarTrait for CheckClockReset<'a> {
if self.in_if_reset {
// Check to see right hand side of reset is const evaluable
match self.evaluator.expression(&arg.expression) {
UnknownStatic | Fixed { .. } => {}
Variable { .. } | Unknown => {
UnknownStatic | Fixed { .. } => (),
_ => {
self.errors
.push(AnalyzerError::invalid_reset_non_elaborative(
self.text,
Expand All @@ -269,4 +269,42 @@ impl<'a> VerylGrammarTrait for CheckClockReset<'a> {
}
Ok(())
}

fn expression11(&mut self, arg: &Expression11) -> Result<(), ParolError> {
if let HandlerPoint::Before = self.point {
if let Some(x) = &arg.expression11_opt {
let src = self.evaluator.expression12(&arg.expression12);
match x.casting_type.as_ref() {
CastingType::Clock(_)
| CastingType::ClockPosedge(_)
| CastingType::ClockNegedge(_) => {
if !src.is_clock() {
self.errors.push(AnalyzerError::invalid_cast(
"non-clock type",
"clock type",
self.text,
&arg.into(),
));
}
}
CastingType::Reset(_)
| CastingType::ResetAsyncHigh(_)
| CastingType::ResetAsyncLow(_)
| CastingType::ResetSyncHigh(_)
| CastingType::ResetSyncLow(_) => {
if !src.is_reset() {
self.errors.push(AnalyzerError::invalid_cast(
"non-reset type",
"reset type",
self.text,
&arg.into(),
));
}
}
_ => (),
}
}
}
Ok(())
}
}
14 changes: 13 additions & 1 deletion crates/analyzer/src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,19 @@ impl Symbol {
let evaluated = match &self.kind {
SymbolKind::Variable(x) => {
let mut evaluator = Evaluator::new();
if let Some(width) = evaluator.type_width(x.r#type.clone()) {
if x.r#type.kind.is_clock() | x.r#type.kind.is_reset() {
match x.r#type.kind {
TypeKind::Clock => Evaluated::Clock,
TypeKind::ClockPosedge => Evaluated::ClockPosedge,
TypeKind::ClockNegedge => Evaluated::ClockNegedge,
TypeKind::Reset => Evaluated::Reset,
TypeKind::ResetAsyncHigh => Evaluated::ResetAsyncHigh,
TypeKind::ResetAsyncLow => Evaluated::ResetAsyncLow,
TypeKind::ResetSyncHigh => Evaluated::ResetSyncHigh,
TypeKind::ResetSyncLow => Evaluated::ResetSyncLow,
_ => unreachable!(),
}
} else if let Some(width) = evaluator.type_width(x.r#type.clone()) {
Evaluated::Variable { width }
} else {
Evaluated::Unknown
Expand Down
13 changes: 13 additions & 0 deletions crates/analyzer/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1909,6 +1909,19 @@ fn invalid_case_condition_expression() {
));
}

#[test]
fn invalid_cast() {
let code = r#"
module ModuleA {
let a: clock = 1;
let _b: reset = a as reset;
}
"#;

let errors = analyze(code);
assert!(matches!(errors[0], AnalyzerError::InvalidCast { .. }));
}

#[test]
fn clock_domain() {
let code = r#"
Expand Down
80 changes: 69 additions & 11 deletions crates/emitter/src/emitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::fs;
use std::path::Path;
use veryl_analyzer::attribute::Attribute as Attr;
use veryl_analyzer::attribute_table;
use veryl_analyzer::evaluator::{Evaluated, Evaluator};
use veryl_analyzer::namespace::Namespace;
use veryl_analyzer::symbol::TypeModifier as SymTypeModifier;
use veryl_analyzer::symbol::{GenericMap, Symbol, SymbolId, SymbolKind, TypeKind};
Expand Down Expand Up @@ -1107,27 +1108,84 @@ impl VerylWalker for Emitter {
fn expression11(&mut self, arg: &Expression11) {
if let Some(x) = &arg.expression11_opt {
match x.casting_type.as_ref() {
CastingType::U32(_) => self.str("unsigned'(int"),
CastingType::U64(_) => self.str("unsigned'(longint"),
CastingType::I32(_) => self.str("signed'(int"),
CastingType::I64(_) => self.str("signed'(longint"),
CastingType::F32(x) => self.f32(&x.f32),
CastingType::F64(x) => self.f64(&x.f64),
CastingType::ScopedIdentifier(x) => self.scoped_identifier(&x.scoped_identifier),
_ => unimplemented!("clock and reset cast"),
CastingType::U32(_) => self.str("unsigned'(int'("),
CastingType::U64(_) => self.str("unsigned'(longint'("),
CastingType::I32(_) => self.str("signed'(int'("),
CastingType::I64(_) => self.str("signed'(longint'("),
CastingType::F32(x) => {
self.f32(&x.f32);
self.str("'(");
}
CastingType::F64(x) => {
self.f64(&x.f64);
self.str("'(");
}
CastingType::ScopedIdentifier(x) => {
self.scoped_identifier(&x.scoped_identifier);
self.str("'(");
}
// casting to clock type doesn't change polarity
CastingType::Clock(_)
| CastingType::ClockPosedge(_)
| CastingType::ClockNegedge(_) => (),
CastingType::Reset(_)
| CastingType::ResetAsyncHigh(_)
| CastingType::ResetAsyncLow(_)
| CastingType::ResetSyncHigh(_)
| CastingType::ResetSyncLow(_) => {
let mut eval = Evaluator::new();
let src = eval.expression12(&arg.expression12);
let dst = x.casting_type.as_ref();
let reset_type = self.build_opt.reset_type;

let src_is_high =
matches!((src, reset_type), (Evaluated::Reset, ResetType::AsyncHigh))
| matches!((src, reset_type), (Evaluated::Reset, ResetType::SyncHigh))
| matches!(src, Evaluated::ResetAsyncHigh)
| matches!(src, Evaluated::ResetSyncHigh);

let src_is_low =
matches!((src, reset_type), (Evaluated::Reset, ResetType::AsyncLow))
| matches!((src, reset_type), (Evaluated::Reset, ResetType::SyncLow))
| matches!(src, Evaluated::ResetAsyncLow)
| matches!(src, Evaluated::ResetSyncLow);

let dst_is_high = matches!(
(dst, reset_type),
(CastingType::Reset(_), ResetType::AsyncHigh)
) | matches!(
(dst, reset_type),
(CastingType::Reset(_), ResetType::SyncHigh)
) | matches!(dst, CastingType::ResetAsyncHigh(_))
| matches!(dst, CastingType::ResetSyncHigh(_));

let dst_is_low = matches!(
(dst, reset_type),
(CastingType::Reset(_), ResetType::AsyncLow)
) | matches!(
(dst, reset_type),
(CastingType::Reset(_), ResetType::SyncLow)
) | matches!(dst, CastingType::ResetAsyncLow(_))
| matches!(dst, CastingType::ResetSyncLow(_));

if (src_is_high && dst_is_low) || (src_is_low && dst_is_high) {
self.str("~")
}
}
}
self.str("'(");
}
self.expression12(&arg.expression12);
if let Some(x) = &arg.expression11_opt {
match x.casting_type.as_ref() {
CastingType::U32(_)
| CastingType::U64(_)
| CastingType::I32(_)
| CastingType::I64(_) => self.str(")"),
| CastingType::I64(_) => self.str("))"),
CastingType::F32(_) | CastingType::F64(_) | CastingType::ScopedIdentifier(_) => {
self.str(")")
}
_ => (),
}
self.str(")");
}
}

Expand Down
Loading
Loading