https://github.com/dentosal/brain-opt
Optimizing BrainFuck compiler
https://github.com/dentosal/brain-opt
Last synced: 3 months ago
JSON representation
Optimizing BrainFuck compiler
- Host: GitHub
- URL: https://github.com/dentosal/brain-opt
- Owner: Dentosal
- License: mit
- Created: 2019-08-21T11:47:03.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2019-08-21T14:03:53.000Z (almost 7 years ago)
- Last Synced: 2025-01-26T06:25:42.147Z (over 1 year ago)
- Language: Rust
- Size: 27.3 KB
- Stars: 1
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# BrainOpt - Optimizing BrainFuck compiler
Compiles BrainFuck code to x86-64 assembly.
Quick compilation, and quick exection.
As if you would need those features with BrainFuck.
## Example: Hello World
Consider this rather beautiful `Hello World!` program:
```brainfuck
++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>
---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++.
```
That gets optimized to: (manually commented)
```assembly
extern read
extern write
extern exit
global main
section .text
main: ; entry point
sub rsp, 30000 ; allocate stack space
mov rcx, 30000 ; set argument: count
mov rdi, rsp ; set argument: start
xor al, al ; set argument: byte to clear with
rep stosb ; zero allocated stack space
mov rbx, rsp ; set cell pointer
sub rsp, 8 ; align stack for extern calls
mov rdi, 1 ; set argument: fd = stdout
mov rsi, constant_output0 ; set argument: buf = constant_output0
mov rdx, 13 ; set argument: count = 13
call _write ; actually write to stdout
xor rdi, rdi ; set status code = 0
call _exit ; exit
section .data
constant_output0: db "Hello World!",0xa
```
It's rather well optimized fast, although cleaning 30000 bytes of stack is still not optimized away.
## Features
- [x] Deterministic builds
- [ ] CI tests for Linux (using Vagrant locally)
## Operating system support
- [x] Linux
- [x] MacOS
- [ ] Windows