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 Ruby1+
# Spinel — AOT Compiler for Ruby2+
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 direct6+
C operations with no dynamic dispatch.7+
8+
## Quick Start9+
10+
```bash11+
# 1. Fetch and build the Prism parser library12+
make deps13+
14+
# 2. Build the spinel compiler15+
make16+
17+
# 3. Compile a Ruby program to C18+
./spinel --source=app.rb --output=app_aot.c19+
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 app25+
```26+
27+
## Benchmark28+
29+
First target: `bm_so_mandelbrot.rb` (Mandelbrot set, 600x600 PBM output).30+
31+
```bash32+
make test # compile, run, and verify output matches CRuby33+
```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 Works44+
45+
```46+
Ruby Source47+
|48+
v49+
Prism (libprism) -- parse to AST50+
|51+
v52+
Type Inference (Pass 1) -- infer Integer/Float/Boolean/String per variable53+
|54+
v55+
C Code Generation (Pass 2)56+
| - Proven numeric types -> direct C arithmetic57+
| - String operations -> mruby API calls58+
| - I/O (puts/print) -> stdio or mruby59+
v60+
Generated C file61+
|62+
v63+
cc + libmruby -> executable64+
```65+
66+
For `bm_so_mandelbrot.rb`, the inner loop compiles to pure C float/int67+
arithmetic with zero mruby dispatch. Only the PBM header string interpolation68+
uses the mruby runtime.69+
70+
## Supported Language Features71+
72+
Currently handles the subset needed for mandelbrot:273
74+
- Local variables (assignment, read, operator-assignment like `+=`, `<<=`)75+
- `while` loops, `for..in` with Range76+
- `if` / `elsif` / `else`, ternary operator77+
- Integer, Float, Boolean, String, nil literals78+
- 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 Structure89+
90+
```91+
spinel/92+
├── src/93+
│ ├── main.c # CLI, file reading, Prism parsing94+
│ ├── codegen.h # Type definitions, codegen API95+
│ └── codegen.c # Type inference + C code generation96+
├── prototype/97+
│ └── tools/ # Step 0 prototype (RBS extraction, LumiTrace, etc.)98+
├── bm_so_mandelbrot.rb # First compilation target99+
├── Makefile100+
├── PLAN.md # Implementation roadmap101+
└── ruby_aot_compiler_design.md # Detailed design document102+
```103+
104+
## Dependencies105+
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
## License4110
5111
Spinel is released under the [MIT License](LICENSE).6112
7-
## Note for License113+
### Note on License8114
9115
mruby has chosen a MIT License due to its permissive license allowing10116
developers to target various environments such as embedded systems.