Skip to content

FernandoTheDev/fiber

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

fiber logo

Fiber

Fiber is a custom-built virtual machine (VM) and compiler toolchain, designed from scratch with a focus on high-speed and optimized bytecode execution.


The Architecture

Fiber utilizes a custom intermediate representation (IR), which allows for seamless integration with APIs from other programming languages. These APIs can generate the Fiber-IR, and the Fiber compiler can then automatically compile and execute the code.

Key Design Principles

  • Memory-to-Memory Architecture: The VM operates directly on memory addresses, eliminating the overhead of a register-based system for a cleaner, more efficient design.
  • High Performance: The entire toolchain achieves ultra-low execution times, with compilation and VM execution measured in microseconds.
  • Bytecode Compression: Drastically reduces the size of compiled binaries for faster distribution and loading.

Getting Started

Prerequisites

  • A D compiler (DMD, LDC, or GDC).
  • Dub

Building

# Clone the repository
git clone https://github.com/fernandothedev/fiber.git

# Navigate to the project directory
cd fiber

# Build the project using dub
dub build --build=release

Usage

# Compile and run a source file
./fiber your_program.fir

# Compile a source file to bytecode
./fiber -o your_program.bc your_program.fir

# Run a pre-compiled bytecode file
./fiber your_program.bc

# Enable debug mode to see compilation steps
./fiber -d your_program.fir

# Show execution statistics
./fiber -s your_program.fir

Example

The following code is compiled and executed by Fiber.

.main {
    x: int = 60
    $0: int;
    store $0, 9
    $1: int;
    add $1, $0, x
    print $1
    halt
}

With function calls:

.main
{
    $0: int;
    main_x: int = 60
    main_y: int = 9

    call sum(int main_x, int main_y), $0
    print $0
    halt
}

fn sum (sum_x, sum_y) int {
    sum_0: int;
    add sum_0, sum_x, sum_y
    ret sum_0
}

Compile and run

❯ ./fiber examples/hard.fir
69

Or view the generated bytecode

❯ ./fiber examples/hard.fir -o hard.bc
Program saved to: hard.bc
Compression: 541 -> 32 tokens (5.9%)
❯ cat hard.bc
FIBERBC 26 512 70 101 114 110 97 110 100 111 68 101 118 6 1 9 1 2 1 0 7 2 12 70 105 98 101 114 MEMORY 60 F511