How chipsalliance/cde works
Project type and purpose
• This is a Scala library (not a webapp or microservice) called “CDE” for Context-Dependent Environments.
• It exposes an API for defining key-value environments that thread through a module hierarchy, with context-sensitive lookup semantics.
• It provides core classes: Parameters, Field, Config, ChainParameters, ChainView, and utility methods (alter, orElse, alterMap, alterPartial).
• It includes tests (cde/tests), documentation (doc/*), and is buildable via Mill (build.sc), with Nix/Flake integration and GitHub Actions CI.
File structure
• Top-level build and config: Makefile, build.sc (Mill), .github/workflows/test.yml, flake.nix, overlay.nix, common.sc.
• Source code under cde/src/chipsalliance/rocketchip/config.scala – so package namespace is chipsalliance.rocketchip.cde.
• Test code under cde/tests/src/SanityTest.scala.
• Root README.md describes the API and internal details.
• Formatting config: .scalafmt.conf.
README for additional insights
• Public API: Parameters, Field[T], Config, plus overrides via alter, orElse.
• Environment reference semantics: (site, here, up).
• Internal representation: binary tree of ChainParameters & ChainView, tree-traversal on lookup.
• Example svg in doc/ChainParameters.svg showing query flow.
a. Main components
• Public API module (Parameters, Field, Config)
• Internal engine (ChainParameters, ChainView, query algorithm)
• Data structures (binary tree of Config layers)
• Test harness (SanityTest)
• Documentation (ChainParameters.tex, ChainParameters.svg)
• Build & CI (Mill build.sc, Nix flakes, GitHub Actions)
b. Relationships & interactions
• Client code invokes Config.apply to produce a Parameters instance.
• Parameters delegates queries to ChainParameters.chain.
• ChainParameters holds left/right child references forming a binary tree.
• ChainView wraps right subtrees, tracking up context.
• Query flow: Parameters→ChainParameters→(ChainView)→leaf→(back up tree) until Field found.
• Build system compiles src and tests, runs docs build; CI workflow triggers tests on push.
c. Architectural patterns & principles
• Pure functional API: immutable parameter objects, first-class functions as lookup tables.
• Composite Pattern: Config layers form a tree of Composite nodes (ChainParameters).
• Visitor-like traversal: chain method traverses the tree at query time.
• Separation of concerns: public API vs internal engine vs build & docs vs CI.
d. Key technologies & frameworks
• Scala 2.x/3.x (as implied by .sc sources)
• Mill build tool (build.sc)
• Nix flakes (flake.nix, overlay.nix) for reproducible environment
• GitHub Actions for CI
• LaTeX for documentation (ChainParameters.tex)