This project implements a Stop-the-World Mark-Sweep Garbage Collector integrated into a stack-based virtual machine. It ensures memory safety by automatically reclaiming unreachable heap objects.
- Heap Allocation: Dynamic support for Pairs, Functions, and Closures
- Root Discovery: Scans the VM operand stack for live references
- Cycle Handling: Correctly marks circular data structures (e.g., circular linked lists)
- Safety: Typed stack values (
VAL_INTvsVAL_OBJ) prevent invalid memory access
This project is written in standard C99 and is designed to be cross-platform. It has been tested on:
- Linux (Ubuntu/Debian, Fedora)
- macOS (via Terminal)
- Windows (via WSL, MinGW, or Cygwin)
To compile the project, you need the following installed on your system:
- C Compiler:
gcc(GNU Compiler Collection) orclang. - Build Tool:
make(GNU Make).
Verify Installation:
gcc --version
make --versionmake allThis produces two executables:
assembler: Converts.asmsource files to.binbytecodevm: The virtual machine with garbage collection support
make clean-
Assemble
./assembler tests/test1.asm
-
Execute
./vm a.bin -verbose
The
-verboseflag prints the stack state after every instruction.
The GC is triggered automatically when the heap limit (512 objects) is exceeded. You can also explicitly trigger a collection in assembly:
PUSH 0
PUSH 0
NEW_PAIR ; Allocate object
POP ; Make it unreachable
GC ; Explicitly trigger collectionA C-based test suite verifies GC logic such as reachability, cycles, and deep object graphs.
make testExpected Output:
=== All Tests Passed ===
Run all assembly scripts in the tests/ folder:
make test_asm./assembler tests/stress_test.asm
time ./vm a.binResult: ~71 MIPS on standard hardware
./assembler tests/gc_benchmark.asm
time ./vm a.binResult: ~50,000 allocations handled in < 0.03s
src/: Source code (vm.c,gc.c,assembler.c, etc.)tests/: Assembly tests (.asm) and C test suite (test_gc.c)docs/: DocumentationMakefile: Build scripts