|
|
|
|
|
by EliasWatson
1121 days ago
|
|
It would be interesting to modify this for optimizing BrainF programs.
It has a very simple instruction set and there are lots of example programs available. To avoid generating useless things like "++--", you could have the optimizer generate instructions that are translated to BrainF operations.
So instead of ">>>--", the optimizer would generate "Move +3; Add -2". |
|
on another hand, there are many opportunities for optimization when compiling brainfuck to an actual real world instruction set
e.g. suppose in your program you want to load the integer 36 into the current cell
a naive way to do this is to increment 36 times, e.g. `++++++++++++++++++++++++++++++++++++`.
if you can't guarantee that the initial value of the current cell is zero, then you could zero it first: `[-]++++++++++++++++++++++++++++++++++++`
if we're optimizing for code size, not instruction count, we could express this as a more complex, compact program: `[-]>[-]++++++[-<++++++>]<` -- assuming it is OK to use the cell one to the right of the pointer as working storage. this will run slower than the less compact naive program as it executes more instructions.
if compiling to some reasonable instruction set, there's likely a single non-BF instruction that lets you load the constant 36 into a register.
extra credit: implementing the optimising brainfuck-to-something compiler in brainfuck itself