Commit 4edb291e

4edb291e3c51Merged

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

Description

Update README with build instructions, benchmark, and architecture

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

Details

Commit
4edb291e3c51
Parent
5e761980cedb

README.mdmodifiedView file

@@ -1,10 +1,116 @@
1-# Spinel the AOT compiler for Ruby
1+# Spinel — AOT Compiler for Ruby
2+
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.
7+
8+## Quick Start
9+
10+```bash
11+# 1. Fetch and build the Prism parser library
12+make deps
13+
14+# 2. Build the spinel compiler
15+make
16+
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
25+```
26+
27+## Benchmark
28+
29+First target: `bm_so_mandelbrot.rb` (Mandelbrot set, 600x600 PBM output).
30+
31+```bash
32+make test # compile, run, and verify output matches CRuby
33+```
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.
42+
43+## How It Works
44+
45+```
46+Ruby Source
47+ |
48+ v
49+Prism (libprism) -- parse to AST
50+ |
51+ v
52+Type Inference (Pass 1) -- infer Integer/Float/Boolean/String per variable
53+ |
54+ 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
59+ v
60+Generated C file
61+ |
62+ v
63+cc + libmruby -> executable
64+```
65+
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.
69+
70+## Supported Language Features
71+
72+Currently handles the subset needed for mandelbrot:
273
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`)
87+
88+## Project Structure
89+
90+```
91+spinel/
92+├── src/
93+│ ├── main.c # CLI, file reading, Prism parsing
94+│ ├── codegen.h # Type definitions, codegen API
95+│ └── codegen.c # Type inference + C code generation
96+├── prototype/
97+│ └── tools/ # Step 0 prototype (RBS extraction, LumiTrace, etc.)
98+├── bm_so_mandelbrot.rb # First compilation target
99+├── Makefile
100+├── PLAN.md # Implementation roadmap
101+└── ruby_aot_compiler_design.md # Detailed design document
102+```
103+
104+## Dependencies
105+
106+- **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`)
108+
3109 ## License
4110
5111 Spinel is released under the [MIT License](LICENSE).
6112
7-## Note for License
113+### Note on License
8114
9115 mruby has chosen a MIT License due to its permissive license allowing
10116 developers to target various environments such as embedded systems.