|
| 1 | +# Debug Support in Ephapax |
| 2 | + |
| 3 | +**Status**: ✅ Implemented (Phase 1) |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +Ephapax now supports generating debug information during compilation, enabling better debugging experience for developers. This feature is particularly important for Ephapax's dyadic type system (affine + linear modes), as it allows debuggers to display mode-aware variable information. |
| 8 | + |
| 9 | +## Features Implemented |
| 10 | + |
| 11 | +### 1. Source Map Generation (JSON Format) |
| 12 | + |
| 13 | +- Maps WASM instruction offsets to source file line numbers |
| 14 | +- Stores mode information (affine vs linear) |
| 15 | +- JSON format for easy parsing by external tools |
| 16 | +- Automatically generated with `--debug` flag |
| 17 | + |
| 18 | +**Example output** (`test.wasm.map`): |
| 19 | +```json |
| 20 | +{ |
| 21 | + "file": "test.eph", |
| 22 | + "mode": "linear", |
| 23 | + "mappings": [ |
| 24 | + { "wasm_offset": 0, "line": 21, "col": 0 }, |
| 25 | + { "wasm_offset": 1, "line": 29, "col": 0 } |
| 26 | + ] |
| 27 | +} |
| 28 | +``` |
| 29 | + |
| 30 | +### 2. Mode Metadata Custom Section |
| 31 | + |
| 32 | +- Custom WASM section: `ephapax.debug.mode` |
| 33 | +- Contains JSON metadata about compilation mode |
| 34 | +- Includes variable linearity information |
| 35 | +- Can be read by mode-aware debuggers |
| 36 | + |
| 37 | +### 3. CLI Support |
| 38 | + |
| 39 | +New flags added to `ephapax compile`: |
| 40 | +- `--debug`: Enable debug information generation |
| 41 | +- `--mode <linear|affine>`: Specify compilation mode (affects debug output) |
| 42 | + |
| 43 | +**Usage:** |
| 44 | +```bash |
| 45 | +# Compile with debug info in linear mode |
| 46 | +ephapax compile program.eph --debug --mode linear -o program.wasm |
| 47 | + |
| 48 | +# This generates: |
| 49 | +# - program.wasm (WASM bytecode with custom debug sections) |
| 50 | +# - program.wasm.map (JSON source map) |
| 51 | +``` |
| 52 | + |
| 53 | +## Architecture |
| 54 | + |
| 55 | +### Code Organization |
| 56 | + |
| 57 | +| Module | Purpose | |
| 58 | +|--------|---------| |
| 59 | +| `ephapax-wasm/src/debug.rs` | Debug info structures and generation | |
| 60 | +| `ephapax-wasm/src/lib.rs` | Integration with codegen (span tracking, section emission) | |
| 61 | +| `ephapax-cli/src/main.rs` | CLI flag handling and file output | |
| 62 | + |
| 63 | +### Debug Information Flow |
| 64 | + |
| 65 | +``` |
| 66 | +Ephapax Source |
| 67 | + ↓ |
| 68 | +AST (with Spans) |
| 69 | + ↓ |
| 70 | +Type Checker (mode: linear/affine) |
| 71 | + ↓ |
| 72 | +WASM Codegen (tracks instruction → span mappings) |
| 73 | + ↓ |
| 74 | + ╔══════════════════════════════════╗ |
| 75 | + ║ Debug Info Emission ║ |
| 76 | + ║ • Custom section: mode metadata ║ |
| 77 | + ║ • Source map: instruction→line ║ |
| 78 | + ╚══════════════════════════════════╝ |
| 79 | + ↓ |
| 80 | +WASM Module + Source Map (JSON) |
| 81 | +``` |
| 82 | + |
| 83 | +### Span Tracking |
| 84 | + |
| 85 | +During compilation, `compile_expr` records: |
| 86 | +- WASM instruction offset (approximated by instruction count) |
| 87 | +- Source span (start line, end line) from AST |
| 88 | + |
| 89 | +This mapping enables debuggers to highlight source code as WASM executes. |
| 90 | + |
| 91 | +## Future Enhancements |
| 92 | + |
| 93 | +### Phase 2: DWARF Support (Planned) |
| 94 | + |
| 95 | +**Status**: Started but disabled (gimli 0.31 API complexity) |
| 96 | + |
| 97 | +DWARF generation would enable compatibility with standard debuggers (lldb, gdb). The implementation is partially written but disabled due to gimli API changes. |
| 98 | + |
| 99 | +**TODO:** |
| 100 | +- Fix gimli 0.31 API usage in `DwarfBuilder` |
| 101 | +- Emit `.debug_info`, `.debug_abbrev`, `.debug_line` sections |
| 102 | +- Test with lldb/gdb |
| 103 | + |
| 104 | +### Phase 3: Debug Adapter Protocol (DAP) Server |
| 105 | + |
| 106 | +**Status**: Not started |
| 107 | + |
| 108 | +Create a DAP server (`ephapax-debug` crate) to enable VS Code debugging: |
| 109 | + |
| 110 | +**Features:** |
| 111 | +- Mode-aware variable display (affine/linear/both views) |
| 112 | +- Breakpoint support (map source lines to WASM offsets) |
| 113 | +- Step-through execution |
| 114 | +- Variable inspection with linearity annotations |
| 115 | + |
| 116 | +**Components:** |
| 117 | +``` |
| 118 | +ephapax-debug/ |
| 119 | +├── Cargo.toml # DAP dependencies |
| 120 | +├── src/ |
| 121 | +│ ├── lib.rs # DAP adapter implementation |
| 122 | +│ ├── mode_view.rs # Mode-aware variable formatting |
| 123 | +│ └── wasm_state.rs # WASM runtime state tracking |
| 124 | +└── .vscode/ |
| 125 | + └── launch.json # VS Code debug configuration |
| 126 | +``` |
| 127 | + |
| 128 | +### Phase 4: VS Code Extension Integration |
| 129 | + |
| 130 | +Integrate debugger with VS Code extension (task #4): |
| 131 | +- Debugger command palette actions |
| 132 | +- Mode switcher UI |
| 133 | +- Inline variable annotations (shows linearity) |
| 134 | +- Breakpoint gutter icons |
| 135 | + |
| 136 | +## Testing |
| 137 | + |
| 138 | +### Unit Tests |
| 139 | + |
| 140 | +```bash |
| 141 | +# Test source map generation |
| 142 | +cargo test --package ephapax-wasm test_source_map_generation |
| 143 | + |
| 144 | +# Test mode metadata |
| 145 | +cargo test --package ephapax-wasm test_mode_metadata_generation |
| 146 | +``` |
| 147 | + |
| 148 | +### Integration Test |
| 149 | + |
| 150 | +```bash |
| 151 | +# Compile with debug |
| 152 | +ephapax compile test.eph --debug --mode linear |
| 153 | + |
| 154 | +# Verify outputs |
| 155 | +ls test.wasm test.wasm.map |
| 156 | + |
| 157 | +# Check source map content |
| 158 | +cat test.wasm.map |
| 159 | +``` |
| 160 | + |
| 161 | +## Implementation Notes |
| 162 | + |
| 163 | +### Why JSON Source Maps? |
| 164 | + |
| 165 | +1. **Simplicity**: Easy to parse in any language |
| 166 | +2. **Debugger-agnostic**: Works with custom tools, not just DWARF-compatible debuggers |
| 167 | +3. **Mode metadata**: Can include Ephapax-specific info (linearity, modes) |
| 168 | +4. **Incremental deployment**: DWARF can be added later without breaking existing tools |
| 169 | + |
| 170 | +### Span Approximation |
| 171 | + |
| 172 | +Currently, WASM offsets are approximated by instruction count. This is sufficient for line-level debugging but not byte-perfect. Future enhancement: track actual byte offsets during emission. |
| 173 | + |
| 174 | +### Mode-Aware Debugging |
| 175 | + |
| 176 | +The debug system preserves Ephapax's dyadic nature: |
| 177 | +- Source maps tagged with mode (linear/affine) |
| 178 | +- Variable metadata includes `is_linear` flag |
| 179 | +- Future DAP server will display variables differently based on mode |
| 180 | + |
| 181 | +## Related Tasks |
| 182 | + |
| 183 | +- [ ] Task #1: Complete DWARF support (fix gimli API usage) |
| 184 | +- [ ] Task #4: VS Code extension (integrate debugger UI) |
| 185 | +- [ ] Add end-to-end debugging guide to documentation |
| 186 | +- [ ] Create debugging examples showing affine vs linear behavior |
| 187 | + |
| 188 | +## References |
| 189 | + |
| 190 | +- [Debug Adapter Protocol (DAP)](https://microsoft.github.io/debug-adapter-protocol/) |
| 191 | +- [DWARF Debugging Format](https://dwarfstd.org/) |
| 192 | +- [Source Maps v3 Spec](https://sourcemaps.info/spec.html) (inspiration, not directly used) |
| 193 | +- [gimli DWARF library](https://github.com/gimli-rs/gimli) |
0 commit comments