Skip to content

a simple vm in c which interprets bytecode and has two strategies for garbage collection

Notifications You must be signed in to change notification settings

vTusharr/memory-management-main

Repository files navigation

Snek GC

A toy language implementation in C demonstrating two different garbage collection strategies: Mark and Sweep and Reference Counting.

Features

  • Dual GC Modes: Switch between Mark & Sweep and Reference Counting at runtime.
  • Bytecode Interpreter: A simple stack-based VM.
  • Object System: Supports Integers, Floats, Strings, Vectors, and Arrays.

Building

Windows (MinGW/GCC): Run the build script:

.\build.bat

Running

Run the executable with a flag to select the GC mode:

  • Mark and Sweep:

    .\snek_gc.exe -ms
  • Reference Counting:

    .\snek_gc.exe -rc

Bytecode Execution

The VM executes a hardcoded bytecode sequence defined in main.c. To run your own programs, modify the code array in main.c and rebuild.

Available Opcodes (defined in ops.h)://you can extend if want to

  • OP_CONST_INT (0): Followed by 4 bytes (little-endian integer). Pushes int to stack.
  • OP_ADD (1): Pops two values, adds them, pushes result.
  • OP_PRINT (2): Pops a value and prints it.
  • OP_RETURN (3): Stops execution.

Example:

  • in main.c file

Project Structure

  • main.c: Entry point and CLI.
  • snek_vm.c: Virtual machine and Mark & Sweep implementation.
  • snek_refcount.c: Reference counting logic.
  • snek_object.c: Object creation and management.
  • stack.c: Generic stack implementation.
  • ops.h: Bytecode opcode definitions.

About

a simple vm in c which interprets bytecode and has two strategies for garbage collection

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published