{"id":21461494,"url":"https://github.com/n9v9/brainfuck","last_synced_at":"2026-05-18T05:43:38.556Z","repository":{"id":49347609,"uuid":"513946535","full_name":"n9v9/brainfuck","owner":"n9v9","description":"Interpreter, Virtual Machine and JIT-Compiler for Brainfuck Programs","archived":false,"fork":false,"pushed_at":"2022-08-02T15:32:41.000Z","size":60,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-07-07T04:11:44.845Z","etag":null,"topics":["assembly","brainfuck","jit-compiler","rust"],"latest_commit_sha":null,"homepage":"","language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/n9v9.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-07-14T15:07:50.000Z","updated_at":"2022-07-24T18:45:06.000Z","dependencies_parsed_at":"2022-08-03T00:01:08.009Z","dependency_job_id":null,"html_url":"https://github.com/n9v9/brainfuck","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/n9v9/brainfuck","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/n9v9%2Fbrainfuck","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/n9v9%2Fbrainfuck/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/n9v9%2Fbrainfuck/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/n9v9%2Fbrainfuck/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/n9v9","download_url":"https://codeload.github.com/n9v9/brainfuck/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/n9v9%2Fbrainfuck/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":33166730,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-17T22:39:12.733Z","status":"online","status_checked_at":"2026-05-18T02:00:06.436Z","response_time":71,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["assembly","brainfuck","jit-compiler","rust"],"created_at":"2024-11-23T07:09:23.817Z","updated_at":"2026-05-18T05:43:38.538Z","avatar_url":"https://github.com/n9v9.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Brainfuck\n\nThis repository contains the following execution environments for executing\nBrainfuck programs:\n\n- Interpreter\n- Compiler\n- Virtual Machine\n- JIT-Compiler for x64 Linux\n\n## CLI\n\nExecute the program with the JIT-Compiler if available, otherwise use the\nvirtual machine:\n\n```\nbrainfuck ./programs/mandelbrot.b\n```\n\nExplicitly specify the execution environment:\n\n```\nbrainfuck --env jit ./programs/mandelbrot.b\n```\n\n## Execution Environments\n\n### Interpreter\n\nA simple interpreter to execute Brainfuck code. It takes the program as a `\u0026str`\nand iterates over all bytes, executing valid Brainfuck instruction along the\nway.\n\n### Compiler\n\nThe compiler compiles the Brainfuck program into a list of instructions, which\ncan then be executed by the virtual machine; it takes the program as a `\u0026str`\nand produces a `Vec\u003cInstruction\u003e`.\n\nThis is more efficient than the interpreter because repeated instructions like\n`+++` are represented by one instruction (`Instruction::IncDP(3)`) instead of\nthree single increment instructions like in the interpreter.\n\n### Virtual Machine\n\nThe virtual machine is needed to execute the instructions generated by the\ncompiler. Its code base is very similar to that of the interpreter, but instead\nof working with raw bytes, it uses the `Instruction` enum instead.\n\n### JIT-Compiler\n\nThe JIT-Compiler takes instructions generated by the compiler. It then generates\nmachine code that is specific to x86_64 Linux systems and executes it. This is\neven more performant than the virtual machine, because the generated machine\ncode does not have to check which instruction it has to execute; this also means\nthat different Brainfuck programs result in different machine code.\n\nThe data pointer is kept in the register `r12`.\n\n#### Optimizations\n\nThe JIT-Compiler contains a few simple optimizations:\n\n- Single `+`, `-`, `\u003e` and `\u003c` instructions use the `inc` or `dec` assembly\n  instructions while repeating instructions use the `add` or `sub` instructions.\n  See for example the function that generates machine code for the `\u003e`\n  instructions:\n  ```rust\n  pub fn emit_inc_dp(\u0026mut self, n: usize) -\u003e usize {\n      let n = n as u8;\n      match n {\n          1 =\u003e {\n              // inc r12\n              self.write(\u0026[0x49, 0xff, 0xc4])\n          }\n          2..=127 =\u003e {\n              // add r12,\u003cn\u003e\n              self.write(\u0026[0x49, 0x83, 0xc4, n])\n          }\n          128..=255 =\u003e {\n              // add r12,\u003cn\u003e\n              self.write(\u0026[0x49, 0x81, 0xc4, n, 0x00, 0x00, 0x00])\n          }\n          _ =\u003e 0,\n      }\n  }\n  ```\n\n#### Limitations\n\nThere are currently a few limitations:\n\n- The generated machine code is architecture and OS specific as it relies on\n  Linux system calls and 64-Bit registers.\n- Input and output are hard coded to be `stdin` and `stdout` respectively,\n  because the generated machine code uses the syscalls `man 2 read` and\n  `man 2 write` with hard coded file descriptors.\n- Because `stdin` and `stdout` are hard coded, the output of the tests for\n  `jit.rs` have to be manually checked.\n\n## Benchmarks\n\nRunning the following code that runs the `mandelbrot.b` program on my system\nproduces the following result:\n\n```\nInterpreter:      28.961  s\nVirtual Machine:   5.157  s\nJIT Compiled:    583.073 ms\n```\n\nCode:\n\n```rust\nuse std::io;\nuse std::time::{Duration, Instant};\n\nuse brainfuck::compiler::Compiler;\nuse brainfuck::interpreter::Interpreter;\nuse brainfuck::jit::JitCompiler;\nuse brainfuck::virtual_machine::VirtualMachine;\nuse brainfuck::FlushBehavior;\n\nconst PROGRAM: \u0026str = include_str!(\"../programs/mandelbrot.b\");\n\nfn main() {\n    let interpreter = measure(\"Interpreter\", interpreter);\n    let vm = measure(\"Virtual Machine\", virtual_machine);\n    let jit = measure(\"JIT Compiler\", jit);\n\n    eprintln!(\"Interpreter:     {interpreter:.3?}\");\n    eprintln!(\"Virtual Machine: {vm:.3?}\");\n    eprintln!(\"JIT Compiled:    {jit:.3?}\");\n}\n\nfn interpreter() {\n    Interpreter::new(PROGRAM, \u0026mut io::empty(), \u0026mut io::stdout().lock())\n        .execute(FlushBehavior::OnWrite)\n        .unwrap();\n}\n\nfn virtual_machine() {\n    VirtualMachine::new(\n        \u0026Compiler::new(PROGRAM).compile(),\n        \u0026mut io::empty(),\n        \u0026mut io::stdout().lock(),\n    )\n    .execute(FlushBehavior::OnWrite)\n    .unwrap();\n}\n\nfn jit() {\n    JitCompiler::new(\u0026Compiler::new(PROGRAM).compile())\n        .execute()\n        .unwrap();\n}\n\nfn measure(desc: \u0026str, f: impl Fn()) -\u003e Duration {\n    eprintln!(\"{desc}:\\n\");\n    let start = Instant::now();\n    f();\n    let time = start.elapsed();\n    eprintln!();\n    time\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fn9v9%2Fbrainfuck","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fn9v9%2Fbrainfuck","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fn9v9%2Fbrainfuck/lists"}