Wilfred / bfc

An industrial-grade brainfuck compiler
https://bfc.wilfred.me.uk
GNU General Public License v2.0
508 stars 30 forks source link

bfc hangs due to compile-time evaluation #28

Open BambooL opened 5 years ago

BambooL commented 5 years ago

I am trying to compile the following code with bfc -O2: . + [[ [[ >. [+ [<>]]] >]<>+ .]>< ,-.,,+++[]---]

But it shows warning:

24852.bf:1:26 warning: This instruction moves the pointer after the last cell (99999), to cell 100000.
  . + [[ [[ >. [+ [<>]]] >]<>+ .]>< ,-.,,+++[]---]
                         ^

But no exit. It should fall into some infinite loop or similar things.

BambooL commented 5 years ago

Another similar problem is also found when compiling:

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

it shows the warning but the compilation is not stopping.

Wilfred commented 5 years ago

This looks like an issue where we aggressively unroll the loop due to compile-time evaluation, send a huge amount of IR to LLVM, and then it spends ages trying to optimise the IR.

I've added an undocumented BFC_MAX_STEPS which limits the execution steps and thus the LLVM IR size. Nonetheless, execution steps are still fast, it's the IR munging afterwards that's taking the time.

Possibly related: #26.

BambooL commented 5 years ago

Since the issue is addressed by the owner. I will close this thread.

Wilfred commented 5 years ago

I think this a genuine bug worth fixing, so I'm going to reopen this :)