Commit be870dcf

be870dcfa82eMerged

Yukihiro "Matz" Matsumoto <[email protected]> · 8 months ago · 1 file · ~1 modified

Description

Update README with ao_render results and full feature list

Both benchmarks now compile to standalone binaries (no mruby). 57-59x faster than CRuby, 16-21KB binary sizes.

Co-Authored-By: Claude Opus 4.6 (1M context) [email protected]

Details

Commit
be870dcfa82e
Parent
c83a43b7b491

README.mdmodifiedView file

@@ -1,9 +1,10 @@
11 # Spinel — AOT Compiler for Ruby
22
3-Spinel compiles Ruby source code to C via [Prism](https://github.com/ruby/prism)
4-parsing and type inference, then links against [mruby](https://github.com/mruby/mruby)
5-for the runtime. For proven types (Integer, Float, Boolean), it generates direct
6-C operations with no dynamic dispatch.
3+Spinel compiles Ruby source code to standalone C executables via
4+[Prism](https://github.com/ruby/prism) parsing and whole-program type inference.
5+Classes become C structs, methods become direct function calls, and numeric
6+operations compile to native C arithmetic with zero dynamic dispatch overhead.
7+Generated binaries have no runtime dependencies — no mruby, no GC, just libc and libm.
78
89 ## Quick Start
910
@@ -14,76 +15,87 @@
1415 # 2. Build the spinel compiler
1516 make
1617
17-# 3. Compile a Ruby program to C
18-./spinel --source=app.rb --output=app_aot.c
19-
20-# 4. Build the final binary (adjust mruby path as needed)
21-cc -O2 app_aot.c \
22- $(~/work/mruby/build/host/bin/mruby-config --cflags) \
23- $(~/work/mruby/build/host/bin/mruby-config --ldflags --libs) \
24- -o app
18+# 3. Compile a Ruby program to C, then to a native binary
19+./spinel --source=app.rb --output=app.c
20+cc -O2 app.c -lm -o app
2521 ```
2622
27-## Benchmark
23+## Benchmarks
2824
29-First target: `bm_so_mandelbrot.rb` (Mandelbrot set, 600x600 PBM output).
25+| Benchmark | CRuby 3.2 | mruby | **Spinel AOT** | Speedup |
26+|-----------|-----------|-------|----------------|---------|
27+| mandelbrot (600x600 PBM) | 1.14s | 3.18s | **0.02s** | **57x** |
28+| ao_render (64x64 AO raytracer) | 3.55s | 13.69s | **0.06s** | **59x** |
3029
30+Binary sizes: mandelbrot 16KB, ao_render 21KB (stripped).
31+
3132 ```bash
32-make test # compile, run, and verify output matches CRuby
33+make test # compile mandelbrot, run, verify output matches CRuby
3334 ```
34-
35-| Runtime | Time | Speedup |
36-|---------|------|---------|
37-| CRuby 3.2 | 1.14s | 1x |
38-| mruby interpreter | 3.18s | 0.36x |
39-| **Spinel AOT** | **0.06s** | **19x** |
40-
41-Output is bit-identical to CRuby.
4235
4336 ## How It Works
4437
4538 ```
46-Ruby Source
39+Ruby Source (.rb)
4740 |
4841 v
49-Prism (libprism) -- parse to AST
42+Prism (libprism) -- parse to AST
5043 |
5144 v
52-Type Inference (Pass 1) -- infer Integer/Float/Boolean/String per variable
45+Pass 1: Class Analysis -- find classes, methods, instance variables
5346 |
5447 v
55-C Code Generation (Pass 2)
56- | - Proven numeric types -> direct C arithmetic
57- | - String operations -> mruby API calls
58- | - I/O (puts/print) -> stdio or mruby
48+Pass 2: Type Inference -- infer types for all variables, ivars, params
49+ | (Integer, Float, Boolean, Object types)
50+ v
51+Pass 3: Struct/Method Emit -- classes -> C structs
52+ | methods -> C functions (direct calls)
53+ | getters/setters -> inline field access
5954 v
60-Generated C file
55+Pass 4: Main Codegen -- top-level code -> main()
56+ | while/for/times -> C loops
57+ | arithmetic -> C operators
58+ | puts/print/printf -> stdio
59+ v
60+Standalone C file
6161 |
6262 v
63-cc + libmruby -> executable
63+cc -O2 -lm -> native binary -- no mruby, no GC, just libc
6464 ```
6565
66-For `bm_so_mandelbrot.rb`, the inner loop compiles to pure C float/int
67-arithmetic with zero mruby dispatch. Only the PBM header string interpolation
68-uses the mruby runtime.
66+For `bm_ao_render.rb`, the compiler:
67+- Converts 6 Ruby classes (Vec, Sphere, Plane, Ray, Isect, Scene) into C structs
68+- Vec (3 floats) is passed/returned by value — no heap allocation
69+- All method calls are devirtualized to direct C function calls
70+- `Integer#times` blocks become C for loops
71+- `Math.sqrt/cos/sin` map directly to C math functions
72+- The Rand module's xorshift PRNG compiles to inline integer arithmetic
6973
7074 ## Supported Language Features
7175
72-Currently handles the subset needed for mandelbrot:
73-
74-- Local variables (assignment, read, operator-assignment like `+=`, `<<=`)
75-- `while` loops, `for..in` with Range
76-- `if` / `elsif` / `else`, ternary operator
77-- Integer, Float, Boolean, String, nil literals
78-- Binary integer literals (`0b0`, `0b1`)
79-- Arithmetic (`+`, `-`, `*`, `/`, `%`)
80-- Comparison (`>`, `<`, `>=`, `<=`, `==`, `!=`)
81-- Bitwise (`<<`, `>>`, `|`, `&`, `^`)
82-- String interpolation (`"hello #{name}"`)
83-- `puts`, `print`
84-- `Integer#chr`
85-- `break`
86-- Parallel assignment (`a, b = c, d`)
76+| Feature | Example |
77+|---------|---------|
78+| Classes with instance variables | `class Vec; def initialize(x,y,z); @x=x; end; end` |
79+| Method definitions | `def vadd(b); Vec.new(@x+b.x, ...); end` |
80+| Getters/setters (inlined) | `def x; @x; end` / `def x=(v); @x=v; end` |
81+| Object construction | `Vec.new(1.0, 2.0, 3.0)` |
82+| Method calls on typed objects | `ray.org.vsub(@center)` |
83+| Modules with state | `module Rand; @x = 123; def self.rand; ...; end; end` |
84+| Local variables, constants | `size = 600`, `ITER = 49` |
85+| `while` loops | `while x <= count_size` |
86+| `Integer#times` with block | `n.times do \|i\| ... end` |
87+| `if` / `elsif` / `else` | conditional branching |
88+| Ternary operator | `escape ? 0 : 1` |
89+| `and` / `or` | `t > 0.0 and t < isect.t` |
90+| Arithmetic, comparison, bitwise | `+`, `-`, `*`, `/`, `%`, `<`, `>`, `==`, `<<`, `\|`, `^` |
91+| Unary minus | `-b`, `-(expr)` |
92+| `Math.sqrt`, `Math.cos`, `Math.sin` | C math functions |
93+| String interpolation | `"P4\n#{size} #{size}"` → printf |
94+| `puts`, `print`, `printf` | stdio calls |
95+| `Integer#chr` | `print byte_acc.chr` → putchar |
96+| `break`, `return` | loop/method exit |
97+| Parallel assignment | `zr, zi = tr, ti` |
98+| Array indexing | `basis[0].x`, `@spheres[1].intersect(...)` |
8799
88100 ## Project Structure
89101
@@ -91,11 +103,12 @@
91103 spinel/
92104 ├── src/
93105 │ ├── main.c # CLI, file reading, Prism parsing
94-│ ├── codegen.h # Type definitions, codegen API
95-│ └── codegen.c # Type inference + C code generation
106+│ ├── codegen.h # Type system, class/method/module info structs
107+│ └── codegen.c # Multi-pass code generator (~2000 lines)
96108 ├── prototype/
97109 │ └── tools/ # Step 0 prototype (RBS extraction, LumiTrace, etc.)
98-├── bm_so_mandelbrot.rb # First compilation target
110+├── bm_so_mandelbrot.rb # Benchmark: Mandelbrot set renderer
111+├── bm_ao_render.rb # Benchmark: Ambient occlusion raytracer
99112 ├── Makefile
100113 ├── PLAN.md # Implementation roadmap
101114 └── ruby_aot_compiler_design.md # Detailed design document
@@ -104,7 +117,7 @@
104117 ## Dependencies
105118
106119 - **Build time**: [Prism](https://github.com/ruby/prism) (fetched automatically by `make deps`)
107-- **Link time**: [mruby](https://github.com/mruby/mruby) (set `MRUBY_DIR` if not at `~/work/mruby`)
120+- **Run time**: None. Generated binaries are standalone (libc + libm only).
108121
109122 ## License
110123