morty 0.9.0

A SystemVerilog source file pickler.
Documentation
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
// Copyright 2019 Fabian Schuiki
// Copyright 2019 Florian Zaruba

// SPDX-License-Identifier: Apache-2.0
#![recursion_limit = "256"]

#[macro_use]
extern crate log;

use anyhow::Result;
use clap::{Arg, ArgAction, Command};
use log::LevelFilter;
use simple_logger::SimpleLogger;
use std::collections::{HashMap, HashSet};
use std::fs::File;
use std::io;
use std::io::{BufRead, BufReader, BufWriter, Write};
use std::path::{Path, PathBuf};
use std::process;

use morty::*;

fn main() -> Result<()> {
    let matches = Command::new(env!("CARGO_PKG_NAME"))
        .version(clap::crate_version!())
        .author(clap::crate_authors!())
        .about(clap::crate_description!())
        .arg(
            Arg::new("inc")
                .short('I')
                .value_name("DIR")
                .help("Add a search path for SystemVerilog includes")
                .action(ArgAction::Append)
                .num_args(1),
        )
        .arg(
            Arg::new("exclude_rename")
                .short('e')
                .long("exclude-rename")
                .value_name("MODULE|INTERFACE|PACKAGE")
                .help("Add module, interface, package which should not be renamed")
                .action(ArgAction::Append)
                .num_args(1),
        )
        .arg(
            Arg::new("exclude")
                .long("exclude")
                .value_name("MODULE|INTERFACE|PACKAGE")
                .help("Do not include module, interface, package in the pickled file list")
                .action(ArgAction::Append)
                .num_args(1),
        )
        .arg(
            Arg::new("v")
                .short('v')
                .action(ArgAction::Count)
                .num_args(0)
                .help("Sets the level of verbosity"),
        )
        .arg(
            Arg::new("prefix")
                .short('p')
                .long("prefix")
                .value_name("PREFIX")
                .help("Prepend a name to all global names")
                .num_args(1),
        )
        .arg(
            Arg::new("def")
                .short('D')
                .value_name("DEFINE")
                .help("Define a preprocesor macro")
                .action(ArgAction::Append)
                .num_args(1),
        )
        .arg(
            Arg::new("suffix")
                .short('s')
                .long("suffix")
                .value_name("SUFFIX")
                .help("Append a name to all global names")
                .num_args(1),
        )
        .arg(
            Arg::new("preproc")
                .short('E')
                .help("Write preprocessed input files to stdout")
                .num_args(0)
                .action(ArgAction::SetTrue),
        )
        .arg(
            Arg::new("file_list")
                .short('f')
                .value_name("LIST")
                .help("Gather files from a manifest")
                .action(ArgAction::Append)
                .num_args(1),
        )
        .arg(
            Arg::new("flist")
                .long("flist")
                .value_name("FILE_LIST")
                .help("Gather files from a file list")
                .num_args(1)
                .action(ArgAction::Append),
        )
        .arg(
            Arg::new("strip_comments")
                .long("strip-comments")
                .help("Strip comments from the output")
                .num_args(0)
                .action(ArgAction::SetTrue),
        )
        .arg(
            Arg::new("INPUT")
                .help("The input files to compile")
                .action(ArgAction::Append)
                .num_args(1..),
        )
        .arg(
            Arg::new("docdir")
                .short('d')
                .long("doc")
                .value_name("OUTDIR")
                .help("Generate documentation in a directory")
                .num_args(1),
        )
        .arg(
            Arg::new("output")
                .short('o')
                .value_name("FILE")
                .help("Write output to file")
                .num_args(1),
        )
        .arg(
            Arg::new("library_file")
                .long("library-file")
                .help("File to search for SystemVerilog modules")
                .value_name("FILE")
                .action(ArgAction::Append)
                .num_args(1),
        )
        .arg(
            Arg::new("library_dir")
                .short('y')
                .long("library-dir")
                .help("Directory to search for SystemVerilog modules")
                .value_name("DIR")
                .action(ArgAction::Append)
                .num_args(1),
        )
        .arg(
            Arg::new("manifest")
                .long("manifest")
                .value_name("FILE")
                .help("Output a JSON-encoded source information manifest to FILE")
                .num_args(1),
        )
        .arg(
            Arg::new("top_module")
                .long("top")
                .value_name("TOP_MODULE")
                .help("Top module, strips all unneeded files. May be incompatible with `--propagate_defines`.")
                .num_args(1),
        )
        .arg(
            Arg::new("graph_file")
                .long("graph_file")
                .value_name("FILE")
                .help("Output a DOT graph of the parsed modules")
                .num_args(1),
        )
        .arg(
            Arg::new("ignore_unparseable")
                .short('i')
                .help("Ignore files that cannot be parsed")
                .num_args(0)
                .action(ArgAction::SetTrue),
        )
        .arg(
            Arg::new("keep_defines")
                .long("keep_defines")
                .help("Prevents removal of `define statements.")
                .num_args(0)
                .action(ArgAction::SetTrue),
        )
        .arg(
            Arg::new("propagate_defines")
                .long("propagate_defines")
                .help("Propagate defines from first files to the following files. Enables sequential.")
                .num_args(0)
                .action(ArgAction::SetTrue),
        )
        .arg(
            Arg::new("sequential")
                .short('q')
                .long("sequential")
                .help("Enforce sequential processing of files. Slows down performance, but can avoid STACK_OVERFLOW.")
                .num_args(0)
                .action(ArgAction::SetTrue),
        )
        .arg(
            Arg::new("keep_timeunits")
                .long("keep_timeunits")
                .help("Keeps timeunits declared throughout the design, may result in bad pickles.")
                .num_args(0)
                .action(ArgAction::SetTrue),
        )
        .get_matches();

    let logger_level = matches.get_count("v");

    // Instantiate a new logger with the verbosity level the user requested.
    SimpleLogger::new()
        .with_level(match logger_level {
            0 => LevelFilter::Warn,
            1 => LevelFilter::Info,
            2 => LevelFilter::Debug,
            _ => LevelFilter::Trace,
        })
        .with_utc_timestamps()
        .init()
        .unwrap();

    let mut file_list = Vec::new();

    // Handle user defines.
    let defines: HashMap<_, _> = match matches.get_many::<String>("def") {
        Some(args) => args
            .map(|x| {
                let mut iter = x.split('=');
                (
                    iter.next().unwrap().to_string(),
                    iter.next().map(String::from),
                )
            })
            .collect(),
        None => HashMap::new(),
    };

    // Prepare a list of include paths.
    let include_dirs: Vec<_> = matches
        .get_many::<String>("inc")
        .into_iter()
        .flatten()
        .map(|x| x.to_string())
        .collect();

    // a hashmap from 'module name' to 'path' for all libraries.
    let mut library_files = HashMap::new();
    // a list of paths for all library files
    let mut library_paths: Vec<PathBuf> = Vec::new();

    // we first accumulate all library files from the 'library_dir' and 'library_file' options into
    // a vector of paths, and then construct the library hashmap.
    for dir in matches
        .get_many::<String>("library_dir")
        .into_iter()
        .flatten()
    {
        for entry in std::fs::read_dir(dir).unwrap_or_else(|e| {
            eprintln!("error accessing library directory `{}`: {}", dir, e);
            process::exit(1)
        }) {
            let dir = entry.unwrap();
            library_paths.push(dir.path());
        }
    }

    if let Some(library_names) = matches.get_many::<String>("library_file") {
        let files = library_names.map(PathBuf::from).collect();
        library_paths.push(files);
    }

    for p in &library_paths {
        // must have the library extension (.v or .sv).
        if has_libext(p) {
            if let Some(m) = lib_module(p) {
                library_files.insert(m, p.to_owned());
            }
        }
    }

    let library_bundle = LibraryBundle {
        include_dirs: include_dirs.clone(),
        defines: defines.clone(),
        files: library_files,
    };

    for path in matches
        .get_many::<String>("file_list")
        .into_iter()
        .flatten()
    {
        let file = File::open(path).unwrap_or_else(|e| {
            eprintln!("error opening `{}`: {}", path, e);
            process::exit(1)
        });
        let reader = BufReader::new(file);

        // Read the JSON contents of the file as an instance of `User`.
        let mut u: Vec<FileBundle> = serde_json::from_reader(reader).unwrap_or_else(|e| {
            eprintln!("error parsing json in `{}`: {}", path, e);
            process::exit(1)
        });
        for fb in &mut u {
            for (_k, v) in fb.export_incdirs.clone() {
                fb.include_dirs.extend(v);
            }
            fb.defines.extend(defines.clone());
            fb.include_dirs.extend(include_dirs.clone());
        }
        file_list.extend(u);
    }

    let mut all_files = Vec::<String>::new();

    if let Some(file_names) = matches.get_many::<String>("INPUT") {
        all_files.extend(file_names.map(|x| x.to_string()).collect::<Vec<_>>());
    }

    for path in matches.get_many::<String>("flist").into_iter().flatten() {
        let file = File::open(path).unwrap_or_else(|e| {
            eprintln!("error opening `{}`: {}", path, e);
            process::exit(1)
        });
        let lines = BufReader::new(file).lines();

        let proper_lines: Vec<String> = lines.filter_map(|x| x.ok()).collect();

        all_files.extend(proper_lines);
    }

    let mut stdin_incdirs = include_dirs;
    let mut stdin_defines = HashMap::<String, Option<String>>::new();

    let stdin_files = all_files
        .into_iter()
        .map(String::from)
        .filter_map(|file_str| {
            let split_str = file_str.splitn(3, '+').collect::<Vec<_>>();
            if split_str.len() > 1 {
                match split_str[1] {
                    "define" => {
                        let def_str = split_str[2];
                        match def_str.split_once('=') {
                            Some((def, val)) => {
                                stdin_defines.insert(def.to_string(), Some(val.to_string()));
                            }
                            None => {
                                stdin_defines.insert(def_str.to_string(), None);
                            }
                        }
                        None
                    }
                    "incdir" => {
                        stdin_incdirs.push(split_str[2].to_string());
                        None
                    }
                    _ => {
                        eprintln!("Unimplemented argument, ignoring for now: {}", split_str[1]);
                        None
                    }
                }
            } else {
                Some(file_str)
            }
        })
        .collect();

    stdin_defines.extend(defines);

    file_list.push(FileBundle {
        include_dirs: stdin_incdirs.clone(),
        export_incdirs: HashMap::new(),
        defines: stdin_defines.clone(),
        files: stdin_files,
    });

    let (mut exclude_rename, mut exclude) = (HashSet::new(), HashSet::new());
    exclude_rename.extend(
        matches
            .get_many::<String>("exclude_rename")
            .into_iter()
            .flatten(),
    );
    exclude.extend(matches.get_many::<String>("exclude").into_iter().flatten());

    let strip_comments = matches.get_flag("strip_comments");

    let syntax_trees = build_syntax_tree(
        &file_list,
        strip_comments,
        matches.get_flag("ignore_unparseable"),
        matches.get_flag("propagate_defines"),
        matches.get_flag("sequential"),
    )?;

    let out = match matches.get_one::<String>("output") {
        Some(file) => {
            info!("Setting output to `{}`", file);
            let path = Path::new(file);
            Box::new(BufWriter::new(File::create(path).unwrap_or_else(|e| {
                eprintln!("could not create `{}`: {}", file, e);
                process::exit(1);
            }))) as Box<dyn Write>
        }
        None => Box::new(io::stdout()) as Box<dyn Write>,
    };

    // Just preprocess.
    if matches.get_flag("preproc") {
        return just_preprocess(syntax_trees, out);
    }

    info!("Finished reading {} source files.", syntax_trees.len());

    // Emit documentation if requested.
    if let Some(dir) = matches.get_one::<String>("docdir") {
        info!("Generating documentation in `{}`", dir);
        return build_doc(syntax_trees, dir);
    }

    let pickle = do_pickle(
        matches.get_one::<String>("prefix"),
        matches.get_one::<String>("suffix"),
        exclude_rename,
        exclude,
        library_bundle,
        syntax_trees,
        out,
        matches.get_one::<String>("top_module"),
        matches.get_flag("keep_defines"),
        matches.get_flag("propagate_defines"),
        !matches.get_flag("keep_timeunits"),
    )?;

    if let Some(graph_file) = matches.get_one::<String>("graph_file") {
        write_dot_graph(&pickle, graph_file)?;
    }

    // if the user requested a manifest we need to compute the information and output it in json
    // form
    if let Some(manifest_file) = matches.get_one::<String>("manifest") {
        write_manifest(
            manifest_file,
            pickle,
            file_list,
            stdin_incdirs,
            stdin_defines,
            matches.get_one::<String>("top_module"),
        )?;
    }

    Ok(())
}