BFK - My brainf**k compiler

Programming, for all ages and all languages.
davidv1992
Member
Member
Posts: 223
Joined: Thu Jul 05, 2007 8:58 am

Re: BFK - My brainf**k compiler

Post by davidv1992 »

The way I planned to go about that particular optimization at the time was a quite general method. First of all I would find all innermost loops. For each one I'd check whether the number of < matches the number of >. If that is the case you can then simulate 1 iteration of the loop. For the loop to be optimizable it should decrease the square with index 0 by exactly 1. You can then use the result of that 1 iteration to determine what happens to all the other squares.

For example take the code

Code: Select all

++++[->>+>++<<<]
.

Find all inner loops:

Code: Select all

[->>+>++<<<]
Count number of < and > and compare

Code: Select all

[->>+>++<<<]   >:3 <:3
Determine that the numbers match.
Simulating one iteration then gives:

Code: Select all

d[0] = -1;
d[1] = 0;
d[2] = 1;
d[3] = 2;
Generate the actual code:

Code: Select all

d[1] += 0*d[0]; // Could be optimized away
d[2] += 1*d[0];
d[3] += 2*d[0];
d[0] = 0;
Hope that that gives the general idea.
Post Reply