Brainfuck Optimization Strategies
calmerthanyouare.org
calmerthanyouare.org
The compiler implements the "Operation offsets" optimization as described in the article. It is actually pretty straightforward. This function converts a map of offset => delta to x86-64 instructions: https://github.com/brianquinlan/brainfuck-jit/blob/master/bf...
and the function following it generates that table.
Some ProTip(tm): JITing Brainfuck doesn't fare a lot; for example, resetting the cell is an O(n) operation in BF, so JIT alone doesn't reduce this into O(1). Some kind of scalar evolution has to be implemented to reduce this O(n) factor. More sophiscated liveness and range analysis might be required to go further (esotope-bfc didn't get there, anyway).
Yo dawg, etc
http://awib.googlecode.com/svn/builds/awib-0.4.b
Awib is a brainfuck compiler written in brainfuck. It is also polyglot in bash, Tcl and C
For instance, using gcc, the following will build an executable file called awib from awib-0.2.b.
$ cp awib-0.2.b awib-0.2.c
$ gcc awib-0.2.c -o awib.tmp
$ ./awib.tmp < awib-0.2.b > awib-0.2.c
$ gcc -O2 awib-0.2.c -o awib
Using bash works fine, but is very very very slow: $ (echo "@386_linux"; cat awib.b) | bash awib.b > awib
$ chmod +x awib
And tcl: $ (echo "@386_linux"; cat awib.b) | tclsh awib.b > awib
$ chmod +x awibThe implications seem more subtle and are more interesting. The very simple phenomenon implies that application development time later can be saved by compiler development time now. If so then what is the most optimal language/compiler? What is the best distribution of time? Is it perhaps so that there exists a language that is trivial to implement, yet also super powerful and expressive to program in (don't say lisp here, though it does almost satisfy the second constraint imo).
Lisp. Satisfies both constraints.
But seriously, Forth is another interesting language that strikes a good balance between ease of implementation vs ease of use and expressive power. But even more than Lisp, it takes some serious getting used to.
I suspect that you are on to something, but instead of the grammer being the guide instead the abstraction that the language gives you, but with a correlation between abstraction and the complexity of the grammar.
[1] http://iolanguage.org/scm/io/docs/IoGuide.html exp ::= { message | terminator } message ::= symbol [arguments] arguments ::= "(" [exp [ { "," exp } ]] ")" symbol ::= identifier | number | string terminator ::= "\n" | ";"
Lest you think I'm completely talking out of my ass, I defer my defense to this random wiki page:
http://esolangs.org/wiki/P%27%27
> P′′ is a primitive programming language created by
> Corrado Böhm in 1964 to describe a family of Turing
> machines.
> [...]
> Brainfuck (apart from its I/O instructions) is a
> simple informal variation of Böhm's P′′. Böhm gives
> explicit P′′ programs for each of a set of basic
> functions sufficient to compute any partial recursive
> function -- and these programs are constructed entirely
> from six P′′ words precisely equivalent to the
> respective Brainfuck commands +, -, <, >, [, and ].Brainfuck is a simple language in the same way a Turing Machine is a simple computer, conceptually simple leaving the complexity in the laborious task of writing any program
It would be interesting to apply this to AI generated programs (http://www.primaryobjects.com/CMS/Article163)
Here is the code to convert BF to C, in case anyone else is interested https://gist.github.com/primaryobjects/0ba06f1f6f0d3e984d89
Here it is running the converted C code https://ideone.com/z16KPJ
Very cool.
NSFW text. ff/feckfeck/f* ckf* ck.
http://web.archive.org/web/20050318095341/http://www.chilliw...
http://web.archive.org/web/20050311055852/http://www.chilliw...