|
| 1 | +# CLAUDE.md |
| 2 | + |
| 3 | +This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository. |
| 4 | + |
| 5 | +## Repository Overview |
| 6 | + |
| 7 | +MIR Semantics provides a K Framework-based formal semantics for Rust's Stable MIR (Mid-level Intermediate Representation), enabling symbolic execution and formal verification of Rust programs. |
| 8 | + |
| 9 | +## Essential Commands |
| 10 | + |
| 11 | +### Build and Setup |
| 12 | +```bash |
| 13 | +# Initial setup - install stable-mir-json tool for SMIR generation |
| 14 | +make stable-mir-json |
| 15 | + |
| 16 | +# Build K semantics definitions (required after any K file changes) |
| 17 | +make build |
| 18 | + |
| 19 | +# Full build and check |
| 20 | +make check build |
| 21 | +``` |
| 22 | + |
| 23 | +### Testing |
| 24 | +```bash |
| 25 | +# Run all tests |
| 26 | +make test |
| 27 | + |
| 28 | +# Run unit tests only |
| 29 | +make test-unit |
| 30 | + |
| 31 | +# Run integration tests (requires stable-mir-json and build) |
| 32 | +make test-integration |
| 33 | + |
| 34 | +# Run a single test |
| 35 | +uv --directory kmir run pytest kmir/src/tests/integration/test_prove.py::test_prove_rs -k "test_name" |
| 36 | + |
| 37 | +# Generate and parse SMIR for test files |
| 38 | +make smir-parse-tests |
| 39 | +``` |
| 40 | + |
| 41 | +### Code Quality |
| 42 | +```bash |
| 43 | +# Format code |
| 44 | +make format |
| 45 | + |
| 46 | +# Check code quality (linting, type checking, formatting) |
| 47 | +make check |
| 48 | + |
| 49 | +# Individual checks |
| 50 | +make check-flake8 |
| 51 | +make check-mypy |
| 52 | +make check-black |
| 53 | +``` |
| 54 | + |
| 55 | +### Working with KMIR Tool |
| 56 | +```bash |
| 57 | +# Activate environment for interactive use |
| 58 | +source kmir/.venv/bin/activate |
| 59 | + |
| 60 | +# Or run commands directly |
| 61 | +uv --directory kmir run kmir <command> |
| 62 | + |
| 63 | +# Prove Rust code directly (recommended) |
| 64 | +uv --directory kmir run kmir prove-rs path/to/file.rs --verbose |
| 65 | + |
| 66 | +# Generate SMIR JSON from Rust |
| 67 | +./scripts/generate-smir-json.sh file.rs output_dir |
| 68 | + |
| 69 | +# View proof results |
| 70 | +uv --directory kmir run kmir show proof_id --proof-dir ./proof_dir |
| 71 | +``` |
| 72 | + |
| 73 | +## Architecture Overview |
| 74 | + |
| 75 | +### Directory Structure |
| 76 | +- `kmir/` - Python frontend tool and K semantics |
| 77 | + - `src/kmir/` - Python implementation |
| 78 | + - `kmir.py` - Main KMIR class handling K semantics interaction |
| 79 | + - `smir.py` - SMIR JSON parsing and info extraction |
| 80 | + - `kdist/mir-semantics/` - K semantics definitions |
| 81 | + - `src/tests/` - Test suites |
| 82 | + - `integration/data/prove-rs/` - Rust test programs for prove-rs |
| 83 | + - `integration/data/exec-smir/` - Rust programs for execution tests |
| 84 | + |
| 85 | +### Key K Semantics Files |
| 86 | +- `kmir.md` - Main execution semantics and control flow |
| 87 | +- `mono.md` - Monomorphized item definitions |
| 88 | +- `body.md` - Function body and basic block semantics |
| 89 | +- `rt/configuration.md` - Runtime configuration cells |
| 90 | +- `rt/data.md` - Runtime data structures |
| 91 | +- `ty.md` - Type system definitions |
| 92 | + |
| 93 | +### Python-K Integration |
| 94 | +The Python layer (`kmir.py`) bridges between SMIR JSON and K semantics: |
| 95 | +1. Parses SMIR JSON via `SMIRInfo` class |
| 96 | +2. Transforms to K terms using `_make_function_map`, `_make_type_and_adt_maps` |
| 97 | +3. Executes via K framework's `KProve`/`KRun` interfaces |
| 98 | + |
| 99 | +### Intrinsic Functions |
| 100 | +Intrinsic functions (like `black_box`) don't have regular function bodies. They're handled by: |
| 101 | +1. Python: `_make_function_map` adds `IntrinsicFunction` entries to function map |
| 102 | +2. K: Special rules in `kmir.md` execute intrinsics via `#execIntrinsic` |
| 103 | + |
| 104 | +## Testing Patterns |
| 105 | + |
| 106 | +### prove-rs Tests |
| 107 | +Tests in `kmir/src/tests/integration/data/prove-rs/` follow this pattern: |
| 108 | +- Simple Rust programs with assertions |
| 109 | +- File naming: `test-name.rs` (passes), `test-name-fail.rs` (expected to fail) |
| 110 | +- Tests run via `kmir prove-rs` command |
| 111 | +- Generate SMIR automatically during test execution |
| 112 | + |
| 113 | +### Adding New Tests |
| 114 | +1. Add Rust file to `prove-rs/` directory |
| 115 | +2. Use assertions to verify behavior |
| 116 | +3. Run with: `uv --directory kmir run kmir prove-rs your-test.rs` |
| 117 | + |
| 118 | +## Development Workflow |
| 119 | + |
| 120 | +### Before Starting Any Task |
| 121 | +1. Read README and documentation in docs/ directory first |
| 122 | +2. Study existing development patterns and conventions |
| 123 | +3. Understand the codebase structure before making changes |
| 124 | + |
| 125 | +### Modifying K Semantics |
| 126 | +1. Edit `.md` files in `kmir/src/kmir/kdist/mir-semantics/` |
| 127 | +2. Run `make build` to compile changes |
| 128 | +3. Test with `make test-integration` |
| 129 | + |
| 130 | +### Modifying Python Code |
| 131 | +1. Edit files in `kmir/src/kmir/` |
| 132 | +2. Run `make format && make check` to verify code quality and formatting |
| 133 | +3. Test with `make test-unit` |
| 134 | + |
| 135 | +### Adding Intrinsic Support |
| 136 | +1. Update `_make_function_map` in `kmir.py` to recognize intrinsic |
| 137 | +2. Add `IntrinsicFunction` constructor in `mono.md` |
| 138 | +3. Add execution rules in `kmir.md` under `#execIntrinsic` |
| 139 | +4. Add test in `prove-rs/` directory |
| 140 | + |
| 141 | +## Debugging Tips |
| 142 | + |
| 143 | +### Viewing Proof Execution |
| 144 | +```bash |
| 145 | +# Show specific nodes in proof |
| 146 | +uv --directory kmir run kmir show proof_id --nodes "1,2,3" --proof-dir ./proof_dir |
| 147 | + |
| 148 | +# Show transitions between nodes |
| 149 | +uv --directory kmir run kmir show proof_id --node-deltas "1:2,2:3" --proof-dir ./proof_dir |
| 150 | + |
| 151 | +# Show rules applied |
| 152 | +uv --directory kmir run kmir show proof_id --rules "1:2" --proof-dir ./proof_dir |
| 153 | + |
| 154 | +# Full details with static info |
| 155 | +uv --directory kmir run kmir show proof_id --full-printer --no-omit-static-info --proof-dir ./proof_dir |
| 156 | +``` |
| 157 | + |
| 158 | +### Common Issues |
| 159 | +- `Function not found` errors: Check if function is in `FUNCTIONS_CELL` (may be intrinsic) |
| 160 | +- K compilation errors: Rules must be properly formatted, check syntax |
| 161 | +- SMIR generation fails: Ensure using correct Rust nightly version (2024-11-29) |
0 commit comments