[dvsim] Document and slightly improve subst_wildcards in utils.py

When messing around with config files, I managed to trigger an
infinite loop in the subst_wildcards function. This patch fixes that,
and also adds a big documentation comment and tests.

Changes:

  - Document exactly what subst_wildcards does (it's quite
    complicated!)

  - Spot circular recursion rather than blowing the call stack.

  - Fix some odd behaviour in {eval_cmd} support.

    The previous code would transform "foo {eval_cmd} echo bar" to
    "bar", ignoring the "foo" prefix entirely. Similarly, it would
    transform "{eval_cmd}xecho foo" to "foo" (skipping over the 'x',
    which it assumed to be a space).

  - Be more explicit about how values get stringified

  - Make the interaction between {eval_cmd} and ignore_error=True a
    more uniform.

    The previous code performed partial evaluation when ignore_error
    was true except when doing {eval_cmd}, when it would discard the
    partially evaluated command string.

  - Add some simple tests.

    These correspond to documentation examples. You can run them with
    pytest. Note that this doesn't add a dependency on pytest unless
    you actually want to run the tests (since Python will merrily
    "parse" code where the named modules aren't in scope!)

Differences in behaviour from the original code:

  - Clearer eval_cmd behavior (see above)

  - Circular references are now spotted. Before, a call to

      subst_wildcards('{a}', {'a': '{b}', 'b': '{a}'})

    would cause an infinite loop. This is the sort of thing that you
    can trigger by a mistake in your config files, and what caused me
    to look at the function in the first place. Now it reports an
    error (regardless of the value of ignore_error).

  - List items are now stringified recursively in
    _stringify_wildcard_value. This makes rules like {'a': ['b', 10]}
    work. The functionality isn't used in dvsim at the moment (because
    all lists are lists of strings), but it's probably a bit cleaner.

  - Computed wildcard names are now possible. Probably not a
    particularly useful feature, but it comes for free with the
    "iterate over matches from the left" implementation, so it can't
    hurt to support explicitly.

Signed-off-by: Rupert Swarbrick <rswarbrick@lowrisc.org>
3 files changed
tree: bb587eacbf7ada9ae45edd58ed71f60ac737eeb4
  1. .github/
  2. ci/
  3. doc/
  4. hw/
  5. site/
  6. sw/
  7. test/
  8. util/
  9. .clang-format
  10. .dockerignore
  11. .flake8
  12. .gitignore
  13. .style.yapf
  14. .svlint.toml
  15. .svls.toml
  16. _index.md
  17. apt-requirements.txt
  18. azure-pipelines.yml
  19. check_tool_requirements.core
  20. CLA
  21. COMMITTERS
  22. CONTRIBUTING.md
  23. LICENSE
  24. meson.build
  25. meson_init.sh
  26. meson_options.txt
  27. python-requirements.txt
  28. README.md
  29. tool_requirements.py
  30. toolchain.txt
README.md

OpenTitan

OpenTitan logo

About the project

OpenTitan is an open source silicon Root of Trust (RoT) project. OpenTitan will make the silicon RoT design and implementation more transparent, trustworthy, and secure for enterprises, platform providers, and chip manufacturers. OpenTitan is administered by lowRISC CIC as a collaborative project to produce high quality, open IP for instantiation as a full-featured product. See the OpenTitan site and OpenTitan docs for more information about the project.

About this repository

This repository contains hardware, software and utilities written as part of the OpenTitan project. It is structured as monolithic repository, or “monorepo”, where all components live in one repository. It exists to enable collaboration across partners participating in the OpenTitan project.

Documentation

The project contains comprehensive documentation of all IPs and tools. You can access it online at docs.opentitan.org.

How to contribute

Have a look at CONTRIBUTING for guidelines on how to contribute code to this repository.

Licensing

Unless otherwise noted, everything in this repository is covered by the Apache License, Version 2.0 (see LICENSE for full text).