{"id":16916124,"url":"https://github.com/zesterer/funkicrab","last_synced_at":"2025-07-06T19:35:07.117Z","repository":{"id":66134590,"uuid":"158870483","full_name":"zesterer/funkicrab","owner":"zesterer","description":"Optimising Brainfuck compiler: Run your beloved Brainfuck code, but faster.","archived":false,"fork":false,"pushed_at":"2019-02-22T12:19:30.000Z","size":75,"stargazers_count":69,"open_issues_count":1,"forks_count":2,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-04-04T06:22:24.989Z","etag":null,"topics":["brainfuck","compiler","optimization"],"latest_commit_sha":null,"homepage":"","language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/zesterer.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-11-23T19:22:57.000Z","updated_at":"2024-11-06T22:06:20.000Z","dependencies_parsed_at":"2023-06-25T20:27:06.270Z","dependency_job_id":null,"html_url":"https://github.com/zesterer/funkicrab","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/zesterer/funkicrab","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zesterer%2Ffunkicrab","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zesterer%2Ffunkicrab/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zesterer%2Ffunkicrab/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zesterer%2Ffunkicrab/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/zesterer","download_url":"https://codeload.github.com/zesterer/funkicrab/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zesterer%2Ffunkicrab/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":263960278,"owners_count":23536052,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","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":["brainfuck","compiler","optimization"],"created_at":"2024-10-13T19:25:04.791Z","updated_at":"2025-07-06T19:35:07.064Z","avatar_url":"https://github.com/zesterer.png","language":"Rust","readme":"# Funki Crab\n\nFunki Crab is an optimising Brainfuck compiler written in Rust.\n\n## Example\n\n### Brainfuck Input\n\n```bf\n++++++++++[\u003e+++++++\u003e++++++++++\u003e+++\u003e+\u003c\u003c\u003c\u003c-]\u003e++.\u003e+.+++++++\n..+++.\u003e++.\u003c\u003c+++++++++++++++.\u003e.+++.------.--------.\u003e+.\u003e.\n```\n### Funki Crab Output (C)\n\n```c\n#include \u003cstdio.h\u003e\n\nchar outputs[13] = { 72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33, 10 };\n\nint main() {\n    fwrite(outputs, sizeof(char), 13, stdout);\n\treturn 0;\n}\n```\n\n### Execution Result\n\n```\nHello World!\n```\n\n## Purpose\n\nI created Funki Crab as an exercise in learning about compiler development, Immediate Representation (IR) techniques and optimisation. Brainfuck struck me as a\nsensible language for such a project given its simplicity, Turing-completeness and wealth of potential optimisations.\n\nFunki Crab is an anagram of Brainfuck. 'Crab' is a reference to Ferris, the Rust mascot.\n\n## Targets\n\nCurrently, Funki Crab can only transpile Brainfuck to C. In the future, it will be capable of compiling to assembly or even raw binaries.\n\n## Optimisations\n\n### Combining Shifts, Increments And Decrements\n\nFunki Crab can combine consecutive shifts, increments and decrements together into a single IR instruction.\n\nExample:\n\n`++++\u003e\u003e\u003e\u003e\u003c\u003c---+\u003c`\n\nBecomes:\n\n```c\n*ptr += 4;\nptr += 2;\n*ptr -= 2;\n--ptr;\n```\n\n### Copy-And-Multiply Loop Elision\n\nFunki Crab can unroll loops that count down one cell in order to add-and-multiply other cells into single IR instructions.\n\nExample:\n\n`[-\u003e+++\u003e\u003e\u003e--\u003c\u003c\u003c\u003c]`\n\nBecomes:\n\n```c\nptr[1] += *ptr * 3;\nptr[4] -= *ptr * 2;\n*ptr = 0;\n```\n\n### Zeroing And Set Loop Elision\n\nFunki Crab can transform loops that decrement to zero and add into a single IR instruction.\n\nExample:\n\n`[-]++++`\n\nBecomes:\n\n```c\n*ptr = 4;\n```\n\n### Shift Elision\n\nFunki Crab can elide shift operations by keeping track of the increment and passing it on to future operations.\n\nExample:\n\n`+\u003e\u003e\u003e+`\n\nBecomes:\n\n```c\n++*ptr;\n++ptr[3];\n```\n\n### Inter-Loop Shift Elision\n\nFunki Crab can elide shifts across loop boundaries.\n\nExample:\n\n`+\u003c\u003c[\u003e]\u003e\u003e`\n\n```c\n++*ptr;\nwhile (ptr[-2]) {\n\t++ptr;\n}\n```\n\n### Dead Code Elimination\n\nFunki Crab can eliminate many common instances of dead code.\n\nExample:\n\n`+,\u003e[-],`\n\nBecomes:\n\n```c\n*ptr = getchar();\n++ptr;\n*ptr = getchar();\n```\n\n### Static Cell Analysis\n\nFunki Crab can analyse the value of cells and operations performed upon cells at compile-time and reason about their effect on later parts of the program.\nThis means that even more dead code can be eliminated and certain instructions can safely be elided. In addition, Funki Crab can use this analysis information to\ntest the accessibility of a loop, eliding it if possible.\n\nExample:\n\n`\u003e[.-]`\n\nBecomes:\n\n*Nothing*\n\n### Dead Tail Elimination\n\nFunki Crab can analyse instructions at the tail of a program and eliminate them if it can prove that they have no impact.\n\nExample:\n\n`+.\u003e\u003e+\u003c---+\u003e`\n\nBecomes:\n\n```c\n++*ptr;\nputchar(ptr[0]);\n```\n\n### Compile-Time Execution\n\nFunki Crab is capable of partial execution of the program at compile-time for all instructions other than `.`. For programs that do not rely on input, and finish\nexecution with fewer than 1 million instructions, it's even possible for Funki Crab to fully execute the entire program and emit only the code necessary to print\nthe end result.\n\n## Combining Optimisations\n\nFunki Crab is capable of combining the optimisations listed above, and more, through multiple transformations of the IR.\n\nExample:\n\n`+.\u003e\u003e+\u003c---+\u003e[-\u003c+[-][\u003e\u003e++++[.-]][-++]-\u003e+-]--`\n\nBecomes:\n\n*Nothing*\n\n## Speedup\n\nTypical speedup with Funki Crab optimisations enabled vs disabled for substantial Brainfuck programs is ~100x.\n\n## Future\n\nI don't plan to maintain Funki Crab as a long-term project. Perhaps I'll add things like better loop unrolling, constant propagation, compile-time execution or\ninstruction reordering to improve cache coherency in the future.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzesterer%2Ffunkicrab","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fzesterer%2Ffunkicrab","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzesterer%2Ffunkicrab/lists"}