Add garbage collection for WASM linear memory (#51)#202
Merged
Conversation
…#51) Implement a conservative mark-sweep GC entirely in WASM (no host-side logic). Programs that allocate heavily now survive beyond the initial 64 KiB page through automatic memory reclamation and memory.grow fallback. Key changes: - Object headers: 4-byte header (mark bit + payload size) prepended by $alloc - Shadow stack: explicit root tracking in linear memory (4096 bytes) - Mark worklist: iterative marking with conservative pointer scanning (4096 bytes) - $gc_collect: clear marks, mark from shadow stack roots, sweep to free list - $alloc: free-list first-fit before bump allocation, GC trigger on OOM - Function prologue/epilogue: save/restore $gc_sp, push pointer params/returns - Lifted closure functions: same GC instrumentation as regular functions - to_string/strip: allocate exact-size result buffers (no interior pointers) Co-Authored-By: Claude <noreply@anthropic.invalid>
Document GC architecture in vera/README.md: memory layout, allocator, collector phases, shadow stack, and zero-overhead guarantee. Update module line counts to reflect GC implementation growth. Co-Authored-By: Claude <noreply@anthropic.invalid>
Collapse C8 into a <details> block (matching C6/C6.5/C7 pattern), update roadmap table with version range v0.0.40-v0.0.65, mark C8.5 as "In progress" and make it the active "Working on" section. Co-Authored-By: Claude <noreply@anthropic.invalid>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
memory.growfallbackChanges
Core GC implementation:
vera/codegen/assembly.py— GC globals, rewritten$alloc(header + free-list + GC trigger),$gc_collect(clear/mark/sweep)vera/codegen/functions.py— GC prologue/epilogue in_compile_fn(save/restore$gc_sp, push pointer params/returns)vera/codegen/closures.py— Same prologue/epilogue for lifted closure functionsvera/wasm/helpers.py—gc_shadow_push()helper (7 WAT instructions)vera/wasm/data.py— Shadow stack push after constructor/array allocationsvera/wasm/closures.py— Shadow stack push after closure allocation +needs_allocflagvera/wasm/calls.py— Fixto_string/stripinterior pointers; shadow stack pushes in string operationsTests:
TestGarbageCollectionclass (WAT-level + behavioral)Example:
examples/gc_pressure.vera— builds and discards 100-element linked lists 30 timesDocumentation:
spec/12-runtime.mdTest plan
pytest tests/ -q)mypy vera/)python scripts/check_examples.py)python scripts/check_version_sync.py)python scripts/check_readme_examples.py)python scripts/check_skill_examples.py)vera run examples/gc_pressure.verareturns 151500Closes #51
🤖 Generated with Claude Code