Goals of this Presentation • Build a full-functioned virtual machine from scratch – The full source code is inside the slides. • Basic concepts about interpreter, optimizations techniques, language specialization, and platform specific tweaks. • Brainfuck is selected as the primary programming language because – it's a very simple turing-complete programming language. – it's easier to write its compiler than its interpreter.– it's easier to write its interpreter than its real programs.

Learn such a stupid language! Why? • Understand how basic a Turing-complete programming language can be. – A common argument when programmers compare languages is “well they’re all Turing-complete”, meaning that anything you can do in one language you can do in another. • Once you’ve learnt brainfuck, you’l understand just how difficult it can be to use a Turing-complete language, and how that argument holds no water. Source: http://skilldrick.co.uk/2011/02/why-you-should-learn-brainfuck-or-learn-you-a-brainfuck- for-great-good/

Brainfuck Instructions (mapped to C language) Increment the data pointer to point to the next cel .Decrement the data pointer to point to the previous cell. Increment the byte value at the data pointer. Decrement the byte value at the data pointer. Output the byte value at the data pointer. Input one byte and store its value at the data pointer. If the byte value at the data pointer is zero, jump to the instruction fol owing the matching ] bracket. Otherwise, continue execution. Unconditionally jump back to the matching [ bracket.

Statement: while • Implementing a while statement is easy, because the Brainfuck [ .. ] statement is a while loop. • Thus, while (x) { <foobar> } becomes (move pointer to a)[(foobar)(move pointer to a)]

Statement: if • The if statement is like a while-loop, but it should run its block only once. Again, a temporary variable is needed to implement if (x) { <foobar> }:(move pointer to x) [ (move pointer to t) +(move pointer to x) ](move pointer to t) [ [ (move pointer to x) + (move pointer to t) ] (foobar)(move pointer to t) ]

Using Artificial Intelligence to Write Self- Modifying/Improving Programs • AI program works, as fol ows: – A genome consists of an array of doubles.– Each gene corresponds to an instruction in the brainf-ck programming language. – Start with a population of random genomes.– Decode each genome into a resulting program by converting each double into its corresponding instruction and execute the program. – Get each program's fitness score, based upon the output it writes to the console (if any), and rank them. – Mate the best genomes together using roulette selection, crossover, and mutation to produce a new generation. – Repeat the process with the new generation until the target fitness score is achieved. Source: http://www.primaryobjects.com/CMS/Article149

Optimization Techniques • To evaluate the impact different optimization techniques can have on performance, we need a set of Brainfuck programs that are sufficiently non-trivial for optimization to make sense. – awib-0.4 (Brainfuck compiler)– factor.b– mandelbrot.b– hanoi.b– dbfi.b (self-interpreter)– long.b • source: https://github.com/matslina/bfoptimization