3 ms·
Show HN: Optimizing brainfuck interpreter using only the C preprocessor
- camel-cdr 5y agoFor anybody interested in how this works I've written a bit of a tutorial, where I try to explain the code base: https://github.com/camel-cdr/bfcpp/blob/main/TUTORIAL.md https://github.com/camel-cdr/bfcpp/blob/main/TUTORIAL.md Also, if you want to play around with this online, here is the code in godbolt: https://godbolt.org/z/oYM8eW6sj https://godbolt.org/z/oYM8eW6sj
- marcodiego 5y agoI once showed a colleague of mine how easy it is to compile brainfuck to C. He said that compiling to assembly was better and it would be easy to implement optimizations. I told him he was underestimating GCC optimizations capabilities. He them implemented a brainfuck compiler to x86 asm and optimized any sequence of repeating ops and the zero cell ([-]) sequence. Turned out that "gcc -O2" was still significantly faster.
- stevekemp 5y agoI wrote a simple compiler for brainfuck too: https://github.com/skx/bfcc https://github.com/skx/bfcc Compiling to assembly language was pretty fast, but folding in repeated characters definitely made it faster still. Optimizing the assembly wasn't a particular goal, I guess recognizing loops that zero addresses, and similar, would be a good way to start.
- motohagiography 5y agoReally weird question, but has someone looked at BF as a candidate for an outwardly farcically inefficient intermediate representation for transpiling other languages to, but one that has enough consistency that you could use ML models to reason about the transpiled programs functionality over that IR? It's not for running the programs, it's an IR for ML reasoning about them.