From cdcfc1fd40d54032a4516e33a4e4d05a384789b2 Mon Sep 17 00:00:00 2001 From: dalance Date: Mon, 11 Nov 2024 18:28:14 +0900 Subject: [PATCH] Add filelist test --- .gitignore | 4 + Cargo.lock | 1 + crates/tests/Cargo.toml | 1 + crates/tests/src/lib.rs | 87 +++++++++ crates/veryl/src/cmd_build.rs | 2 +- crates/veryl/src/lib.rs | 234 ++++++++++++++++++++++++ crates/veryl/src/main.rs | 235 +------------------------ crates/veryl/src/runner/vcs.rs | 6 + crates/veryl/src/runner/verilator.rs | 6 + crates/veryl/src/runner/vivado.rs | 6 + testcases/filelist/Veryl.toml | 6 + testcases/filelist/src/module_a.veryl | 3 + testcases/filelist/src/module_b.veryl | 3 + testcases/filelist/src/module_c.veryl | 15 ++ testcases/filelist/src/package_a.veryl | 5 + testcases/filelist/src/package_b.veryl | 5 + 16 files changed, 385 insertions(+), 234 deletions(-) create mode 100644 crates/veryl/src/lib.rs create mode 100644 testcases/filelist/Veryl.toml create mode 100644 testcases/filelist/src/module_a.veryl create mode 100644 testcases/filelist/src/module_b.veryl create mode 100644 testcases/filelist/src/module_c.veryl create mode 100644 testcases/filelist/src/package_a.veryl create mode 100644 testcases/filelist/src/package_b.veryl diff --git a/.gitignore b/.gitignore index 4f16c4ba..6eb656ff 100644 --- a/.gitignore +++ b/.gitignore @@ -12,6 +12,10 @@ veryl_testcase.* /book/po/messages.pot /book/po/*~ /testcases/map/dependencies/ +/testcases/filelist/dependencies/ +/testcases/filelist/src/*.sv +/testcases/filelist/*.f +/testcases/filelist/Veryl.lock /crates/metadata/std/ /crates/std/veryl/*.f /crates/std/veryl/target/ diff --git a/Cargo.lock b/Cargo.lock index 53e61641..60c88181 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5050,6 +5050,7 @@ version = "0.1.0" dependencies = [ "criterion", "pprof", + "veryl", "veryl-analyzer", "veryl-emitter", "veryl-formatter", diff --git a/crates/tests/Cargo.toml b/crates/tests/Cargo.toml index d35df979..8a8d7d6e 100644 --- a/crates/tests/Cargo.toml +++ b/crates/tests/Cargo.toml @@ -12,6 +12,7 @@ veryl-formatter = {version = "0.13.2", path = "../formatter"} veryl-metadata = {version = "0.13.2", path = "../metadata"} veryl-parser = {version = "0.13.2", path = "../parser"} veryl-path = {version = "0.13.2", path = "../path"} +veryl = {version = "0.13.2", path = "../veryl"} [dev-dependencies] criterion = "0.5.1" diff --git a/crates/tests/src/lib.rs b/crates/tests/src/lib.rs index 04235864..c4bab64a 100644 --- a/crates/tests/src/lib.rs +++ b/crates/tests/src/lib.rs @@ -252,3 +252,90 @@ mod path { ); } } + +#[cfg(test)] +mod filelist { + use std::fs; + use std::path::PathBuf; + use veryl_analyzer::Analyzer; + use veryl_metadata::Metadata; + use veryl_parser::Parser; + + fn check_list(paths: &[String], expected: &[&str]) { + let paths: Vec<_> = paths.iter().map(|x| x.as_str()).collect(); + for x in &paths { + assert!(expected.contains(&x)); + } + for x in expected { + assert!(paths.contains(x)); + } + } + + fn check_order(paths: &[String], path0: &str, path1: &str) { + let path0 = paths + .iter() + .enumerate() + .find_map(|(i, x)| if x == path0 { Some(i) } else { None }); + let path1 = paths + .iter() + .enumerate() + .find_map(|(i, x)| if x == path1 { Some(i) } else { None }); + assert!(path0 < path1); + } + + #[test] + fn test() { + let path = std::env::current_dir().unwrap(); + let path = path.join("../../testcases/filelist"); + let metadata_path = Metadata::search_from(path).unwrap(); + let mut metadata = Metadata::load(&metadata_path).unwrap(); + let paths = metadata.paths::(&[], false).unwrap(); + + let mut contexts = Vec::new(); + + for path in &paths { + let input = fs::read_to_string(&path.src).unwrap(); + let parser = Parser::parse(&input, &path.src).unwrap(); + + let analyzer = Analyzer::new(&metadata); + let _ = analyzer.analyze_pass1(&path.prj, &input, &path.src, &parser.veryl); + contexts.push((path, input, parser, analyzer)); + } + + Analyzer::analyze_post_pass1(); + + for (path, input, parser, analyzer) in &contexts { + let _ = analyzer.analyze_pass2(&path.prj, input, &path.src, &parser.veryl); + } + + for (path, input, parser, analyzer) in &contexts { + let _ = analyzer.analyze_pass3(&path.prj, input, &path.src, &parser.veryl); + } + + let paths = veryl::cmd_build::CmdBuild::sort_filelist(&metadata, &paths); + let paths: Vec<_> = paths + .into_iter() + .map(|x| x.src.file_name().unwrap().to_string_lossy().into_owned()) + .collect(); + + dbg!(&paths); + + let all = &[ + "package_a.veryl", + "package_b.veryl", + "module_a.veryl", + "module_b.veryl", + "module_c.veryl", + "fifo_controller.veryl", + "fifo.veryl", + "ram.veryl", + // This should be removed by #1064 + "test_linear_sec.veryl", + ]; + check_list(&paths, all); + + check_order(&paths, "package_a.veryl", "module_a.veryl"); + check_order(&paths, "package_b.veryl", "module_b.veryl"); + check_order(&paths, "ram.veryl", "module_c.veryl"); + } +} diff --git a/crates/veryl/src/cmd_build.rs b/crates/veryl/src/cmd_build.rs index e9d0fe4b..66afc8c5 100644 --- a/crates/veryl/src/cmd_build.rs +++ b/crates/veryl/src/cmd_build.rs @@ -200,7 +200,7 @@ impl CmdBuild { Ok(()) } - fn sort_filelist(metadata: &Metadata, paths: &[PathSet]) -> Vec { + pub fn sort_filelist(metadata: &Metadata, paths: &[PathSet]) -> Vec { let mut table = HashMap::new(); for path in paths { table.insert(path.src.clone(), path); diff --git a/crates/veryl/src/lib.rs b/crates/veryl/src/lib.rs new file mode 100644 index 00000000..3ead3f62 --- /dev/null +++ b/crates/veryl/src/lib.rs @@ -0,0 +1,234 @@ +use clap::{Args, Parser, Subcommand, ValueEnum}; +use std::path::PathBuf; + +pub mod cmd_build; +pub mod cmd_check; +pub mod cmd_clean; +pub mod cmd_doc; +pub mod cmd_dump; +pub mod cmd_fmt; +pub mod cmd_init; +pub mod cmd_metadata; +pub mod cmd_new; +pub mod cmd_publish; +pub mod cmd_test; +pub mod cmd_update; +pub mod doc; +pub mod runner; + +// --------------------------------------------------------------------------------------------------------------------- +// Opt +// --------------------------------------------------------------------------------------------------------------------- + +#[derive(Parser)] +#[command(author, version, about, long_about = None)] +#[command(propagate_version = true)] +pub struct Opt { + /// No output printed to stdout + #[arg(long, global = true)] + pub quiet: bool, + + /// Use verbose output + #[arg(long, global = true)] + pub verbose: bool, + + /// Generate tab-completion + #[arg(long, global = true, hide = true)] + pub completion: Option, + + #[command(subcommand)] + pub command: Commands, +} + +#[derive(Clone, ValueEnum)] +#[clap(rename_all = "lower")] +pub enum CompletionShell { + Bash, + Elvish, + Fish, + PowerShell, + Zsh, +} + +#[derive(Subcommand)] +pub enum Commands { + New(OptNew), + Init(OptInit), + Fmt(OptFmt), + Check(OptCheck), + Build(OptBuild), + Clean(OptClean), + Update(OptUpdate), + Publish(OptPublish), + Doc(OptDoc), + Metadata(OptMetadata), + Dump(OptDump), + Test(OptTest), +} + +/// Create a new project +#[derive(Args)] +pub struct OptNew { + pub path: PathBuf, +} + +/// Create a new project in an existing directory +#[derive(Args)] +pub struct OptInit { + #[arg(default_value = ".")] + pub path: PathBuf, +} + +/// Format the current project +#[derive(Args)] +pub struct OptFmt { + /// Target files + pub files: Vec, + + /// Run fmt in check mode + #[arg(long)] + pub check: bool, +} + +/// Analyze the current project +#[derive(Args)] +pub struct OptCheck { + /// Target files + pub files: Vec, +} + +/// Build the target codes corresponding to the current project +#[derive(Args)] +pub struct OptBuild { + /// Target files + pub files: Vec, +} + +/// Clean-up the current project +#[derive(Args)] +pub struct OptClean {} + +/// Update dependencies +#[derive(Args)] +pub struct OptUpdate {} + +/// Publish the current project +#[derive(Args)] +pub struct OptPublish { + /// Bump version + #[arg(long)] + pub bump: Option, +} + +/// Build the document corresponding to the current project +#[derive(Args)] +pub struct OptDoc { + /// Target files + pub files: Vec, +} + +/// Execute tests +#[derive(Args)] +pub struct OptTest { + /// Target files + pub files: Vec, + + /// Simulator + #[arg(long, value_enum)] + pub sim: Option, + + /// Dump waveform + #[arg(long)] + pub wave: bool, +} + +#[derive(Clone, Copy, Debug, ValueEnum)] +pub enum SimType { + /// Verilator + Verilator, + /// Synopsys VCS + Vcs, + /// AMD Vivado Simulator + Vivado, +} + +impl From for veryl_metadata::SimType { + fn from(x: SimType) -> Self { + match x { + SimType::Verilator => veryl_metadata::SimType::Verilator, + SimType::Vcs => veryl_metadata::SimType::Vcs, + SimType::Vivado => veryl_metadata::SimType::Vivado, + } + } +} + +#[derive(Clone, Copy, Default, Debug, ValueEnum)] +pub enum BumpKind { + /// Increment majoir version + Major, + /// Increment minor version + Minor, + /// Increment patch version + #[default] + Patch, +} + +impl From for veryl_metadata::BumpKind { + fn from(x: BumpKind) -> Self { + match x { + BumpKind::Major => veryl_metadata::BumpKind::Major, + BumpKind::Minor => veryl_metadata::BumpKind::Minor, + BumpKind::Patch => veryl_metadata::BumpKind::Patch, + } + } +} + +/// Dump metadata of the current packege +#[derive(Args)] +pub struct OptMetadata { + /// output format + #[arg(long, value_enum, default_value_t)] + pub format: Format, +} + +#[derive(Clone, Copy, Default, Debug, ValueEnum)] +pub enum Format { + #[default] + Pretty, + Json, +} + +/// Dump debug info +#[derive(Args)] +pub struct OptDump { + /// Target files + pub files: Vec, + + /// output syntex tree + #[arg(long)] + pub syntax_tree: bool, + + /// output symbol table + #[arg(long)] + pub symbol_table: bool, + + /// output assign list + #[arg(long)] + pub assign_list: bool, + + /// output namespace table + #[arg(long)] + pub namespace_table: bool, + + /// output type dag + #[arg(long)] + pub type_dag: bool, + + /// output attribute table + #[arg(long)] + pub attribute_table: bool, + + /// output unsafe table + #[arg(long)] + pub unsafe_table: bool, +} diff --git a/crates/veryl/src/main.rs b/crates/veryl/src/main.rs index deb73905..d7b660e6 100644 --- a/crates/veryl/src/main.rs +++ b/crates/veryl/src/main.rs @@ -1,247 +1,16 @@ -use clap::{Args, CommandFactory, Parser, Subcommand, ValueEnum}; +use clap::{CommandFactory, Parser}; use clap_complete::aot::Shell; use console::Style; use fern::Dispatch; use log::debug; use log::{Level, LevelFilter}; use miette::{IntoDiagnostic, Result}; -use std::path::PathBuf; use std::process::ExitCode; use std::str::FromStr; use std::time::Instant; use veryl_metadata::Metadata; -mod cmd_build; -mod cmd_check; -mod cmd_clean; -mod cmd_doc; -mod cmd_dump; -mod cmd_fmt; -mod cmd_init; -mod cmd_metadata; -mod cmd_new; -mod cmd_publish; -mod cmd_test; -mod cmd_update; -mod doc; -mod runner; - -// --------------------------------------------------------------------------------------------------------------------- -// Opt -// --------------------------------------------------------------------------------------------------------------------- - -#[derive(Parser)] -#[command(author, version, about, long_about = None)] -#[command(propagate_version = true)] -struct Opt { - /// No output printed to stdout - #[arg(long, global = true)] - pub quiet: bool, - - /// Use verbose output - #[arg(long, global = true)] - pub verbose: bool, - - /// Generate tab-completion - #[arg(long, global = true, hide = true)] - pub completion: Option, - - #[command(subcommand)] - command: Commands, -} - -#[derive(Clone, ValueEnum)] -#[clap(rename_all = "lower")] -pub enum CompletionShell { - Bash, - Elvish, - Fish, - PowerShell, - Zsh, -} - -#[derive(Subcommand)] -enum Commands { - New(OptNew), - Init(OptInit), - Fmt(OptFmt), - Check(OptCheck), - Build(OptBuild), - Clean(OptClean), - Update(OptUpdate), - Publish(OptPublish), - Doc(OptDoc), - Metadata(OptMetadata), - Dump(OptDump), - Test(OptTest), -} - -/// Create a new project -#[derive(Args)] -pub struct OptNew { - pub path: PathBuf, -} - -/// Create a new project in an existing directory -#[derive(Args)] -pub struct OptInit { - #[arg(default_value = ".")] - pub path: PathBuf, -} - -/// Format the current project -#[derive(Args)] -pub struct OptFmt { - /// Target files - pub files: Vec, - - /// Run fmt in check mode - #[arg(long)] - pub check: bool, -} - -/// Analyze the current project -#[derive(Args)] -pub struct OptCheck { - /// Target files - pub files: Vec, -} - -/// Build the target codes corresponding to the current project -#[derive(Args)] -pub struct OptBuild { - /// Target files - pub files: Vec, -} - -/// Clean-up the current project -#[derive(Args)] -pub struct OptClean {} - -/// Update dependencies -#[derive(Args)] -pub struct OptUpdate {} - -/// Publish the current project -#[derive(Args)] -pub struct OptPublish { - /// Bump version - #[arg(long)] - pub bump: Option, -} - -/// Build the document corresponding to the current project -#[derive(Args)] -pub struct OptDoc { - /// Target files - pub files: Vec, -} - -/// Execute tests -#[derive(Args)] -pub struct OptTest { - /// Target files - pub files: Vec, - - /// Simulator - #[arg(long, value_enum)] - pub sim: Option, - - /// Dump waveform - #[arg(long)] - pub wave: bool, -} - -#[derive(Clone, Copy, Debug, ValueEnum)] -pub enum SimType { - /// Verilator - Verilator, - /// Synopsys VCS - Vcs, - /// AMD Vivado Simulator - Vivado, -} - -impl From for veryl_metadata::SimType { - fn from(x: SimType) -> Self { - match x { - SimType::Verilator => veryl_metadata::SimType::Verilator, - SimType::Vcs => veryl_metadata::SimType::Vcs, - SimType::Vivado => veryl_metadata::SimType::Vivado, - } - } -} - -#[derive(Clone, Copy, Default, Debug, ValueEnum)] -pub enum BumpKind { - /// Increment majoir version - Major, - /// Increment minor version - Minor, - /// Increment patch version - #[default] - Patch, -} - -impl From for veryl_metadata::BumpKind { - fn from(x: BumpKind) -> Self { - match x { - BumpKind::Major => veryl_metadata::BumpKind::Major, - BumpKind::Minor => veryl_metadata::BumpKind::Minor, - BumpKind::Patch => veryl_metadata::BumpKind::Patch, - } - } -} - -/// Dump metadata of the current packege -#[derive(Args)] -pub struct OptMetadata { - /// output format - #[arg(long, value_enum, default_value_t)] - pub format: Format, -} - -#[derive(Clone, Copy, Default, Debug, ValueEnum)] -pub enum Format { - #[default] - Pretty, - Json, -} - -/// Dump debug info -#[derive(Args)] -pub struct OptDump { - /// Target files - pub files: Vec, - - /// output syntex tree - #[arg(long)] - pub syntax_tree: bool, - - /// output symbol table - #[arg(long)] - pub symbol_table: bool, - - /// output assign list - #[arg(long)] - pub assign_list: bool, - - /// output namespace table - #[arg(long)] - pub namespace_table: bool, - - /// output type dag - #[arg(long)] - pub type_dag: bool, - - /// output attribute table - #[arg(long)] - pub attribute_table: bool, - - /// output unsafe table - #[arg(long)] - pub unsafe_table: bool, -} +use veryl::*; // --------------------------------------------------------------------------------------------------------------------- // Main diff --git a/crates/veryl/src/runner/vcs.rs b/crates/veryl/src/runner/vcs.rs index 305620f4..98a42ce0 100644 --- a/crates/veryl/src/runner/vcs.rs +++ b/crates/veryl/src/runner/vcs.rs @@ -111,6 +111,12 @@ impl Vcs { } } +impl Default for Vcs { + fn default() -> Self { + Self::new() + } +} + impl Runner for Vcs { fn run( &mut self, diff --git a/crates/veryl/src/runner/verilator.rs b/crates/veryl/src/runner/verilator.rs index 274bebf1..22325d79 100644 --- a/crates/veryl/src/runner/verilator.rs +++ b/crates/veryl/src/runner/verilator.rs @@ -121,6 +121,12 @@ impl Verilator { } } +impl Default for Verilator { + fn default() -> Self { + Self::new() + } +} + impl Runner for Verilator { fn run( &mut self, diff --git a/crates/veryl/src/runner/vivado.rs b/crates/veryl/src/runner/vivado.rs index c4862c5b..4e1372d3 100644 --- a/crates/veryl/src/runner/vivado.rs +++ b/crates/veryl/src/runner/vivado.rs @@ -94,6 +94,12 @@ impl Vivado { } } +impl Default for Vivado { + fn default() -> Self { + Self::new() + } +} + impl Runner for Vivado { fn run( &mut self, diff --git a/testcases/filelist/Veryl.toml b/testcases/filelist/Veryl.toml new file mode 100644 index 00000000..14a613fb --- /dev/null +++ b/testcases/filelist/Veryl.toml @@ -0,0 +1,6 @@ +[project] +name = "filelist" +version = "0.1.0" + +[build] +sourcemap_target = {type = "none"} diff --git a/testcases/filelist/src/module_a.veryl b/testcases/filelist/src/module_a.veryl new file mode 100644 index 00000000..18a268a0 --- /dev/null +++ b/testcases/filelist/src/module_a.veryl @@ -0,0 +1,3 @@ +module ModuleA { + let _a: PackageA::StructA = 0; +} diff --git a/testcases/filelist/src/module_b.veryl b/testcases/filelist/src/module_b.veryl new file mode 100644 index 00000000..891106d0 --- /dev/null +++ b/testcases/filelist/src/module_b.veryl @@ -0,0 +1,3 @@ +module ModuleB { + let _a: PackageB::EnumB = 1; +} diff --git a/testcases/filelist/src/module_c.veryl b/testcases/filelist/src/module_c.veryl new file mode 100644 index 00000000..05f7b996 --- /dev/null +++ b/testcases/filelist/src/module_c.veryl @@ -0,0 +1,15 @@ +module ModuleC { + inst u: $std::ram ( + i_clk : _, + i_rst : _, + i_clr : _, + i_mea : _, + i_wea : _, + i_adra: _, + i_da : _, + i_meb : _, + i_adrb: _, + o_qb : _, + ); + +} diff --git a/testcases/filelist/src/package_a.veryl b/testcases/filelist/src/package_a.veryl new file mode 100644 index 00000000..3343e46d --- /dev/null +++ b/testcases/filelist/src/package_a.veryl @@ -0,0 +1,5 @@ +package PackageA { + struct StructA { + A: logic, + } +} diff --git a/testcases/filelist/src/package_b.veryl b/testcases/filelist/src/package_b.veryl new file mode 100644 index 00000000..8a9ce231 --- /dev/null +++ b/testcases/filelist/src/package_b.veryl @@ -0,0 +1,5 @@ +package PackageB { + enum EnumB { + X, + } +}