meiao / esolangs

A collection of interpreters for esoteric languages.
GNU General Public License v3.0
0 stars 0 forks source link

Brainfuck optimization - group together multiple invocations of the same command #9

Open meiao opened 1 year ago

meiao commented 1 year ago

To decrease the number of instructions when multiple similar commands are grouped, these can be grouped as in the example:

For example:

IncDataPointer(1)
IncDataPointer(1)
IncDataPointer(1)
IncDataPointer(-1)

can become:

IncDataPointer(2)
Noop
Noop
Noop

This optimization can be done to IncDataPointer and IncData. But all the commands must be of the same type and must be sequentially in the code.

The Noops are added so the blocks' start_block_instr and end_block_instr are not changed by this optimization. They shall be removed by a later optimization.

Note:

IncDataPointer(1)
IncDataPointer(-1)

should become:

Noop
Noop
meiao commented 1 year ago

Note, this instrumentation may make programs that would panic in runtime to run. For instance, a program that increments the data until it overflows, then decrements it, would fail without this optimization, but will run fine after it.