veryl 0.20.3

A modern hardware description language
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
use clap::{Args, Parser, Subcommand, ValueEnum};
use std::ffi::OsString;
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_migrate;
pub mod cmd_new;
pub mod cmd_publish;
pub mod cmd_register;
pub mod cmd_synth;
pub mod cmd_test;
pub mod cmd_translate;
pub mod cmd_update;
pub mod component_publish;
pub mod context;
pub mod diff;
pub mod doc;
pub mod external_subcommand;
pub mod incremental;
pub mod pipeline;
pub mod runner;
pub mod stopwatch;
pub mod utils;
pub use stopwatch::StopWatch;

// ---------------------------------------------------------------------------------------------------------------------
// Opt
// ---------------------------------------------------------------------------------------------------------------------

#[derive(Parser)]
#[command(author, version, about, long_about = None)]
#[command(override_usage = "veryl [OPTIONS] <COMMAND>")]
#[command(after_help = "\n... See all commands with --list")]
#[command(propagate_version = true)]
#[clap(version(veryl_metadata::VERYL_VERSION))]
#[clap(long_version(veryl_metadata::VERYL_VERSION))]
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,

    /// Use trace output
    #[arg(long, global = true)]
    pub trace: bool,

    /// Generate tab-completion
    #[arg(long, global = true, hide = true)]
    pub completion: Option<CompletionShell>,

    /// List all commands
    #[arg(long)]
    pub list: bool,

    #[command(subcommand)]
    pub command: Option<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),
    Register(OptRegister),
    Migrate(OptMigrate),
    Doc(OptDoc),
    Metadata(OptMetadata),
    Dump(OptDump),
    Test(OptTest),
    Synth(OptSynth),
    Translate(OptTranslate),
    #[command(external_subcommand)]
    External(Vec<OsString>),
}

/// Translate SystemVerilog files into Veryl source
#[derive(Args)]
pub struct OptTranslate {
    /// Input SystemVerilog files. Each `foo.sv` produces a sibling `foo.veryl`.
    pub files: Vec<PathBuf>,

    /// Write the result to stdout instead of writing files. Useful for piping
    /// or redirecting to a non-default path.
    #[arg(long)]
    pub stdout: bool,

    /// Fail if any unsupported constructs are encountered
    #[arg(long)]
    pub strict: bool,

    /// Skip the Veryl formatter pass and emit the raw translator output
    #[arg(long = "no-format")]
    pub no_format: bool,
}

/// Create a new project
#[derive(Args)]
pub struct OptNew {
    pub path: PathBuf,

    /// Create a user-defined verification component (a Rust cargo package
    /// usable as `$comp::<name>`) instead of a Veryl project
    #[arg(long)]
    pub component: bool,
}

/// 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<PathBuf>,

    /// Run fmt in check mode
    #[arg(long)]
    pub check: bool,
}

/// Analyze the current project
#[derive(Args)]
pub struct OptCheck {
    /// Target files
    pub files: Vec<PathBuf>,
}

/// Build the target codes corresponding to the current project
#[derive(Args)]
pub struct OptBuild {
    /// Target files
    pub files: Vec<PathBuf>,

    /// Run build in check mode
    #[arg(long)]
    pub check: bool,

    /// Directory for build outputs, overriding the project path derived
    /// from Veryl.toml. Relative paths resolve against the current
    /// working directory.
    #[arg(long, value_name = "DIR")]
    pub out_dir: Option<PathBuf>,
}

/// 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<BumpKind>,
}

/// Register the current project with the Veryl registry
#[derive(Args)]
pub struct OptRegister {
    /// Register without the confirmation prompt
    #[arg(long)]
    pub yes: bool,
}

/// Migrate breaking changes from the previous version
#[derive(Args)]
pub struct OptMigrate {
    /// Target files
    pub files: Vec<PathBuf>,

    /// Run fmt in check mode
    #[arg(long)]
    pub check: bool,
}

/// Build the document corresponding to the current project
#[derive(Args)]
pub struct OptDoc {
    /// Target files
    pub files: Vec<PathBuf>,
}

/// Execute tests
#[derive(Args)]
pub struct OptTest {
    /// Target files
    pub files: Vec<PathBuf>,

    /// Test name filter (substring match)
    #[arg(short = 't', long = "test")]
    pub test: Option<String>,

    /// Simulator
    #[arg(long, value_enum)]
    pub sim: Option<SimType>,

    /// Dump waveform
    #[arg(long)]
    pub wave: bool,

    /// Native-simulator code-generation backend (default: cc)
    #[arg(long, value_enum, default_value = "cc")]
    pub backend: Backend,

    /// Dual-run the `cc` backend against Cranelift and panic on divergence.
    /// Takes an optional stride: dual-run + diff only every Nth cycle (default
    /// 64 when given with no value); `--backend-validate 1` = every cycle.
    #[arg(long, num_args = 0..=1, default_missing_value = "64", value_name = "STRIDE")]
    pub backend_validate: Option<u64>,

    /// Disable FF classification optimization (force all always_ff variables to FF)
    #[arg(long)]
    pub disable_ff_opt: bool,

    /// Run only ignored tests
    #[arg(long)]
    pub ignored: bool,

    /// Run both ignored and non-ignored tests
    #[arg(long)]
    pub include_ignored: bool,

    /// Define a name visible to `#[ifdef]` (can be specified multiple times).
    /// Merged with `[test].defines` from Veryl.toml.
    #[arg(short = 'D', long = "define", value_name = "NAME")]
    pub define: Vec<String>,

    /// Stream `$display`/`$write` output live instead of buffering it per test.
    /// Output from concurrently-running tests may interleave. Buffering is also
    /// skipped automatically when tests run on a single worker.
    #[arg(long, alias = "nocapture")]
    pub no_capture: bool,

    /// Base seed for user-defined component instances. Unset (and no
    /// `[test].seed`) draws a fresh random seed each run; pass a value to
    /// reproduce a previous run. Overrides `[test].seed` in Veryl.toml.
    #[arg(long)]
    pub seed: Option<u64>,

    /// Run native tests in four-state (X/Z) mode. Also settable via
    /// `[test].four_state`.
    #[arg(long = "4state")]
    pub four_state: bool,

    /// Output format: `pretty` (human-readable summary, default) or `json`
    /// (machine-readable report on stdout)
    #[arg(long, value_enum, default_value_t)]
    pub format: Format,

    /// Report format version (only with `--format json`; currently only 1)
    #[arg(long = "format-version")]
    pub format_version: Option<u32>,
}

/// Native-simulator code-generation backend selected by `veryl test --backend`.
/// Named by codegen mechanism rather than jit/aot (both `cranelift` and `cc`
/// compile to native code at run time, so the meaningful axis is *which*
/// compiler, not jit-vs-aot).
#[derive(Clone, Copy, Debug, Default, ValueEnum)]
pub enum Backend {
    /// Walk the IR statement tree each cycle (no codegen).
    Interpret,
    /// In-process Cranelift JIT.
    Cranelift,
    /// Emit C and compile via an external C compiler (comb + event + async),
    /// falling back to Cranelift for uncovered stmts / when no `cc` is present.
    #[default]
    Cc,
}

#[derive(Clone, Copy, Debug, ValueEnum)]
pub enum SimType {
    /// Verilator
    Verilator,
    /// Synopsys VCS
    Vcs,
    /// Altair DSim
    Dsim,
    /// AMD Vivado Simulator
    Vivado,
}

impl From<SimType> for veryl_metadata::SimType {
    fn from(x: SimType) -> Self {
        match x {
            SimType::Dsim => veryl_metadata::SimType::Dsim,
            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<BumpKind> 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,

    /// metadata output format version
    #[arg(long = "format-version")]
    pub format_version: Option<u32>,
}

#[derive(Clone, Copy, Default, Debug, ValueEnum)]
pub enum Format {
    #[default]
    Pretty,
    Json,
}

/// Mirrors `veryl metadata`'s `--format-version` so `synth`/`test` behave the
/// same. Only version 1 exists yet.
pub(crate) fn check_format_version(format: Format, version: Option<u32>) -> miette::Result<()> {
    if let Some(v) = version {
        if !matches!(format, Format::Json) {
            miette::bail!("--format-version is only supported with --format json");
        }
        if v != 1 {
            miette::bail!("unsupported --format-version {v}; supported versions: 1");
        }
    }
    Ok(())
}

/// Dump debug info
#[derive(Args)]
pub struct OptDump {
    /// Target files
    pub files: Vec<PathBuf>,

    /// output syntex tree
    #[arg(long)]
    pub syntax_tree: bool,

    /// output symbol table
    #[arg(long)]
    pub symbol_table: bool,

    /// output namespace table
    #[arg(long)]
    pub namespace_table: bool,

    /// output type dag
    #[arg(long)]
    pub type_dag: bool,

    /// output file dag
    #[arg(long)]
    pub file_dag: bool,

    /// output attribute table
    #[arg(long)]
    pub attribute_table: bool,

    /// output unsafe table
    #[arg(long)]
    pub unsafe_table: bool,

    /// output IR
    #[arg(long)]
    pub ir: bool,
}

/// Synthesize to a simple gate-level netlist and report area / critical path.
///
/// Design-parameter knobs (`clock_freq`, `activity`) and the default `top` /
/// `timing_paths` live in the `[synth]` section of `Veryl.toml`. CLI
/// `--top` and `--timing-paths` override the toml setting when supplied.
#[derive(Args)]
pub struct OptSynth {
    /// Target files
    pub files: Vec<PathBuf>,

    /// Top module name (overrides `synth.top` in Veryl.toml; otherwise
    /// inferred from the first user module)
    #[arg(long)]
    pub top: Option<String>,

    /// Number of worst-delay endpoints to report when dumping timing
    /// (overrides `synth.timing_paths` in Veryl.toml)
    #[arg(long)]
    pub timing_paths: Option<usize>,

    /// Output format: `pretty` (human-readable summary, default) or `json`
    /// (machine-readable report on stdout)
    #[arg(long, value_enum, default_value_t)]
    pub format: Format,

    /// Report format version (only with `--format json`; currently only 1)
    #[arg(long = "format-version")]
    pub format_version: Option<u32>,

    /// Dump the gate-level IR (netlist of gates and flip-flops)
    #[arg(long)]
    pub dump_ir: bool,

    /// Dump the critical path trace
    #[arg(long)]
    pub dump_timing: bool,

    /// Dump the per-cell-kind area breakdown
    #[arg(long)]
    pub dump_area: bool,

    /// Dump the power estimate (leakage + dynamic breakdown)
    #[arg(long)]
    pub dump_power: bool,
}