How paritytech/revive works
Main Components
• Compiler Frontend – This includes the Solidity integration (“resolc” executable) that processes Solidity source input and converts it to an intermediate language (YUL) before further processing.
• LLVM Builder – Implemented in the “crates/llvm-builder” directory, this component handles the acquisition/building of a custom LLVM (v18.1.8) tailored for RISC-V targets with the necessary builtins and compiler-rt.
• LLVM Context – Found under “crates/llvm-context”, this crate deals with building the LLVM IR representation, optimization passes, debugging configuration, and target machine support.
• Code Generation and Linker – The “crates/linker” (and other related crates such as “crates/builtins”) manage linking, embedded builtins and code transformations.
• Runtime Components – The “crates/runner” and “crates/runtime-api” components are in charge of executing contracts and providing the runtime environment required on PolkaVM.
• Solidity JSON Interface – The “crates/solc-json-interface” crate provides tooling to compile and interface with Solidity’s JSON formats, bridging between Solidity source and YUL/LLVM.
• Additional Utilities, Testing, and Benchmarking – Directories like “crates/benchmarks”, “crates/integration”, and “crates/differential” aid with performance testing, integration tests, and verifying correctness.
• JavaScript/Wasm Frontend – The “js” directory contains build scripts, examples (both for Node.js and web), cross-compilation instructions, and end-to-end tests to allow a Wasm version of the compiler to run in browser or Node.js environments.
Relationships and Interactions
• The Solidity frontend (resolc) processes contract source files and hands off an intermediate representation to the compiler core.
• The LLVM Builder supports both local builds of LLVM and cross-compilation scenarios; it supplies the LLVM binaries (or points to them) used by the LLVM Context.
• The LLVM Context interacts closely with both the code generator and the optimization routines to produce target-specific IR.
• The Runner and Runtime API receive finalized compiled code and execute it within an environment that simulates high-level contract execution.
• Testing and benchmarking crates run against these components to verify correctness, performance, and integration.
• The JavaScript layer, through Wasm cross-compilation, wraps around the primary compiler components to provide a frontend that can run on different platforms (browser, Node.js).
Architectural Patterns and Design Principles
• Modular Design – The project is separated into numerous well-defined crates, each with a clear role (frontend, builder, context, runner, etc.) following a separation-of-concerns principle.
• Dependency and Data Flow – There is a logical sequence where source code is ingested, transformed through multiple intermediate representations, built via an external custom LLVM dependency, and finally executed or integrated through testing tools.
• Extensibility – With dedicated crates for the JSON interfaces, integration tests, and differential testing, the system is designed for extensibility and robust automated testing.
Technologies and Frameworks
• Rust and Cargo for building and managing the project’s workspace.
• LLVM (custom build of v18.1.8) as the core compilation backend.
• C++ toolchain and Emscripten used for building the LLVM components and the Wasm target.
• GitHub Actions (CI workflows in .github directory) managing continuous integration and release processes.
• Additional testing tools (like Geth for testing Ethereum compatibility in integration tests) to simulate runtime environments.