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

fixed multiple issues with expression checking #786

Merged
merged 8 commits into from
Jun 17, 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
81 changes: 63 additions & 18 deletions crates/analyzer/src/handlers/check_expression.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::analyzer_error::AnalyzerError;
use crate::evaluator::{Evaluated, Evaluator};
use crate::symbol::SymbolKind;
use crate::symbol::{Direction, SymbolKind};
use crate::symbol_table;
use veryl_parser::veryl_grammar_trait::*;
use veryl_parser::veryl_token::TokenRange;
Expand All @@ -14,6 +14,13 @@ pub struct CheckExpression<'a> {
point: HandlerPoint,
case_condition_depth: usize,
evaluator: Evaluator,
call_stack_kind: Vec<FunctionKind>,
}

#[derive(Debug, Clone, Copy, PartialEq)]
enum FunctionKind {
System,
NonSystem,
}

impl<'a> CheckExpression<'a> {
Expand Down Expand Up @@ -63,17 +70,25 @@ impl<'a> VerylGrammarTrait for CheckExpression<'a> {
if let Ok(rr) = symbol_table::resolve(expid) {
let identifier = rr.found.token.to_string();
let token: TokenRange = x.expression_identifier.as_ref().into();
let error = AnalyzerError::invalid_factor(
&identifier,
&rr.found.kind.to_kind_name(),
self.text,
&token,
);
match rr.found.kind {
SymbolKind::Function(_)
| SymbolKind::ModportFunctionMember(_)
| SymbolKind::SystemFunction => {
SymbolKind::Function(_) | SymbolKind::ModportFunctionMember(_) => {
if x.factor_opt.is_none() {
self.errors.push(AnalyzerError::invalid_factor(
&identifier,
&rr.found.kind.to_kind_name(),
self.text,
&token,
));
self.errors.push(error);
} else {
self.call_stack_kind.push(FunctionKind::NonSystem);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think self.call_stack_kind.pop corresponding to this push may be required at HandlerPoint::After.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're correct.

}
}
SymbolKind::SystemFunction => {
if x.factor_opt.is_none() {
self.errors.push(error);
} else {
self.call_stack_kind.push(FunctionKind::System);
}
}
SymbolKind::Module(_)
Expand All @@ -82,18 +97,35 @@ impl<'a> VerylGrammarTrait for CheckExpression<'a> {
| SymbolKind::Block
| SymbolKind::Package(_)
| SymbolKind::TypeDef(_)
| SymbolKind::Enum(_)
| SymbolKind::Modport(_)
| SymbolKind::Namespace
| SymbolKind::GenericInstance(_) => {
self.errors.push(AnalyzerError::invalid_factor(
&identifier,
&rr.found.kind.to_kind_name(),
self.text,
&token,
));
self.errors.push(error);
}
SymbolKind::Port(x) => match x.direction {
Direction::Interface | Direction::Modport => {
self.errors.push(error);
}
_ => {}
},
SymbolKind::Parameter(_)
| SymbolKind::EnumMember(_)
| SymbolKind::Genvar
| SymbolKind::ModportVariableMember(_)
| SymbolKind::SystemVerilog
| SymbolKind::GenericParameter(_)
| SymbolKind::Variable(_) => {}

SymbolKind::Enum(_)
| SymbolKind::Union(_)
| SymbolKind::Struct(_)
| SymbolKind::StructMember(_)
| SymbolKind::UnionMember(_) => {
if let Some(FunctionKind::System) = self.call_stack_kind.last() {
} else {
self.errors.push(error);
}
}
_ => {}
}
}

Expand All @@ -120,6 +152,19 @@ impl<'a> VerylGrammarTrait for CheckExpression<'a> {
}
}
}
} else if let Factor::ExpressionIdentifierFactorOpt(x) = arg {
let expid = x.expression_identifier.as_ref();
if let Ok(rr) = symbol_table::resolve(expid) {
match rr.found.kind {
SymbolKind::Function(_) | SymbolKind::ModportFunctionMember(_) => {
self.call_stack_kind.pop();
}
SymbolKind::SystemFunction => {
self.call_stack_kind.pop();
}
_ => {}
}
}
}
Ok(())
}
Expand Down
87 changes: 86 additions & 1 deletion crates/analyzer/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1637,7 +1637,93 @@
}

#[test]
fn test_factors() {
let code = r#"
interface InterfaceA {
var a: logic;
modport master {
a: input,
}
}

module ModuleA #(
param K: i32 = 32,
) (
i_clk: input clock ,
i_rst: input reset ,
mst : modport InterfaceA::master,
) {

enum State: logic<3> {
Idle = 3'bxx1,
Run0 = 3'b000,
Run1 = 3'b010,
Run2 = 3'b100,
Done = 3'b110,
}

struct S {
v: logic,
}

union U {
v: logic,
w: logic,
}

let state: State = State::Run1;
var u : U ;
var s : S ;
local J : i32 = 32;

for i in 0..1 :g_display {
always_ff {
$display("%d", i);
}
}

function foo () -> logic {
return 1'b1;
}

function bar (
l: input logic,
) -> logic {
return foo();
}

assign u.v = 1'b1;
assign s.v = 1'b1;
initial {
$display("%d", u);
$display("%d", s);
$display("%d", state);
$display("%d", mst.a);
$display("%d", i_clk);
$display("%d", K);
$display("%d", J);
$display("%d", foo());
// Using $bits as a placeholder SystemFunciton.
$display("%d", $bits(S));
$display("%d", $bits(U));
$display("%d", $bits(foo(), State));
$display("%d", bar($bits(State)));
$display("%d", $bits(State));
$display("%d", bar(S));
$display("%d", bar(U));
$display("%d", bar(State));
$display("%d", $bits(bar(State)));
}
}"#;

let errors = analyze(code);
assert!(errors.len() == 4);
for error in errors {
assert!(matches!(error, AnalyzerError::InvalidFactor { .. }));
}
}

fn enum_non_const_exception() {

Check warning on line 1726 in crates/analyzer/src/tests.rs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, stable, x86_64-unknown-linux-gnu, false)

function `enum_non_const_exception` is never used

Check warning on line 1726 in crates/analyzer/src/tests.rs

View workflow job for this annotation

GitHub Actions / build (macOS-latest, stable, x86_64-apple-darwin, false)

function `enum_non_const_exception` is never used

Check warning on line 1726 in crates/analyzer/src/tests.rs

View workflow job for this annotation

GitHub Actions / build (windows-latest, stable, x86_64-pc-windows-msvc, false)

function `enum_non_const_exception` is never used
let code = r#"
module ModuleA (
i_clk: input clock,
Expand All @@ -1651,7 +1737,6 @@
Run2 = 3'b100,
Done = 3'b110,
}

var state: State;

always_ff {
Expand Down
Loading