Brainfuck interpreter and jit-compiler in Java

139 views
Skip to first unread message

sleepnova

unread,
Jan 10, 2009, 4:14:17 AM1/10/09
to JVM Languages
I don't know if this subject is appropriate here, if not, please
ignore it.

I just want to share to you my first experiment project related to
language, compiler and bytecode manipulation stuff - a toy interpreter/
jit-compiler for Brainfuck implementation in Java.

I put it here - http://code.google.com/p/bfj/
Hope you enjoy the toy. :)

Eugene Kuleshov

unread,
Jan 10, 2009, 10:10:42 AM1/10/09
to JVM Languages

There is also a bf compiler in examples from ASM framework. :-)

regards,
Eugene


On Jan 10, 4:14 am, sleepnova <wanpee...@gmail.com> wrote:
> I don't know if this subject is appropriate here, if not, please
> ignore it.
>
> I just want to share to you my first experiment project related to
> language, compiler and bytecode manipulation stuff - a toy interpreter/
> jit-compiler for Brainfuck implementation in Java.
>
> I put it here -http://code.google.com/p/bfj/

Jon Harrop

unread,
Jan 10, 2009, 10:58:10 AM1/10/09
to jvm-la...@googlegroups.com

Wonderful. I wanted to benchmark a Java version against my OCaml/LLVM and
F#/CIL versions. Thanks!

--
Dr Jon Harrop, Flying Frog Consultancy Ltd.
http://www.ffconsultancy.com/?e

Charles Oliver Nutter

unread,
Jan 10, 2009, 10:04:46 PM1/10/09
to jvm-la...@googlegroups.com
Jon Harrop wrote:
> Wonderful. I wanted to benchmark a Java version against my OCaml/LLVM and
> F#/CIL versions. Thanks!

Looking forward to seeing that and other comparisons! We shall band
together to make sure BF/JVM is the fastest BF in the world!

- Charlie

Charles Oliver Nutter

unread,
Jan 10, 2009, 10:05:11 PM1/10/09
to jvm-la...@googlegroups.com

Any particularly interesting things you learned from the exercise?

- Charlie

Jon Harrop

unread,
Jan 11, 2009, 1:12:54 AM1/11/09
to jvm-la...@googlegroups.com

I will surely advertise that fact in my forthcoming BF for Scientists book
which will cover ANSI BF in detail.

Jon Harrop

unread,
Jan 11, 2009, 1:26:36 AM1/11/09
to jvm-la...@googlegroups.com
On Saturday 10 January 2009 09:14:17 sleepnova wrote:

SlocCount says this is 66k lines of code. Is that really right?

Liwei

unread,
Jan 11, 2009, 6:07:25 AM1/11/09
to jvm-la...@googlegroups.com
I'm willing to know the result. :)

2009/1/10 Jon Harrop <j...@ffconsultancy.com>

Jon Harrop

unread,
Jan 11, 2009, 7:01:42 AM1/11/09
to jvm-la...@googlegroups.com
On Sunday 11 January 2009 11:07:25 Liwei wrote:
> I'm willing to know the result. :)

I cannot even get it to run:

$ export
CLASSPATH="/home/jdh30/src/bfj-read-only/bfj/lib/asm-3.1.jar:/home/jdh30/src/bfj-read-only/bfj/src/"
$ java bf.runner.ParseCompileRunner
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at bf.runner.ParseCompileRunner.main(ParseCompileRunner.java:16)

Sounds like it is failing because it assumed argvs would contain a BF program
to run, i.e. the compiler is actually running. But:

$ java bf.runner.ParseCompileRunner ../bf/mandelbrot.b
compile and run: ../bf/mandelbrot.b
Exception in thread "main" java.lang.NoClassDefFoundError:
bf/util/BrainfuckParser
at bf.runner.ParseCompileRunner.main(ParseCompileRunner.java:20)
Caused by: java.lang.ClassNotFoundException: bf.util.BrainfuckParser
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:276)
at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
... 1 more

Sounds like the generated code doesn't run. Does it use Windows specific
invocations internally?

I figured out why there is so much Java code in there: the bfj distro contains
some precompiled BF programs as Java source. Removing those, it is only 1,588
LOC. Still 11x longer than the OCaml/LLVM though...

Liwei

unread,
Jan 11, 2009, 10:12:36 AM1/11/09
to jvm-la...@googlegroups.com
bfj didn't use any of Windows specific functionality, I tested it on both Windows and Linux system.
It just does in memory translation to JVM bytecode and execute it directly.

It seems that you have encountered some classpath issues.
You got NoClassDefFoundError while loading bf.util.BrainfuckParser.

1. Check if all java file are properly compiled and class files are properly placed in package structure.
2. I suggest you to use -cp to specify your classpath.

I use following command to execute:
java -cp ../lib/asm-3.1.jar:. bf.runner.ParseCompileRunner ../bf/mandelbrot.b in Linux
java -cp ../lib/asm-3.1.jar;. bf.runner.ParseCompileRunner ../bf/mandelbrot.b in Windows

About the bloated LOC...
The ParseCompileRunner implementation is mixed with the interpreter code.
I did implement some translation only BF2BytecodeRunner which is about 200 LOC, but they are not yet apply some basic optimizations.
I'll update them so that you can have a fare compare of the LOC in different languages.

:)

2009/1/11 Jon Harrop <j...@ffconsultancy.com>

Liwei

unread,
Jan 11, 2009, 12:19:29 PM1/11/09
to jvm-la...@googlegroups.com
I have update my the simple translate and go version, which is about 230 LOC and slightly faster than the old one.

Related source:
src\bf\runner\BF2BytecodeRunner.java
src\bf\program\State.java

Use following command to execute:
java -cp ../lib/asm-3.1.jar:. bf.runner.BF2BytecodeRunner ../bf/mandelbrot.b in Linux
java -cp ../lib/asm-3.1.jar;. bf.runner.BF2BytecodeRunner ../bf/mandelbrot.b in Windows

Jim White

unread,
Jan 12, 2009, 1:12:05 AM1/12/09
to jvm-la...@googlegroups.com
This is probably even less appropriate, although there are Java
implementations, so close enuf:

http://lolcode.com/examples/gimmeh

Better yet would be a Java-to-LOLCode xlator! ;-)

Jim

Reply all
Reply to author
Forward
0 new messages