Giter Club home page Giter Club logo

brainfuck-jit's Introduction

brainfuck-jit

An optimizing JIT Brainfuck interpreter for x86_64, i386, ARM, x86, as well as generic interpreter and C converter.

i386, x86_64, ARM, and aarch64 JIT are supported on both *nix and Windows. Other platforms use a generic interpreter.

A minimal but fast standalone assembly wrapper for the debug output of x86_64 *nix is provided.

void brainfuck(const char *code, size_t len, int optlevel);

Usage:

const char *code = "+[-[<<[+[--->]-[<<<]]]>>>-]>-.---.>..>.<<<<-.<+.>>>>>.>.<<.<-.";
brainfuck(code, strlen(code), 2);
$ make
$ ./brainfuck-jit file.bf

The program will default run an example which prints the alphabet backwards, but you can provide a single filename and it will run that instead. Add -O[n] as the first argument to play with optlevel.

Brainfuck behavior

  • All cells are 8-bit unsigned integers.
  • 65536 cells, allocated in heap memory.
  • Pointer starts at the beginning of the tape.
  • Writing out of bounds is UB.
  • Mismatched []s are treated as errors during compilation.
  • EOF from , is treated as 0xFF.
  • optlevel turns on or off optimizations.
  • When make DEBUG=1 is used, it outputs opcodes to bf.s, raw machine code to bf.o, and emits a bunch of debugging info.

The compiler will optimize consecutive operations with optlevel >= 1, and optimize clear loops and multiply loops when optlevel >= 2.

Internally, the compiler uses mmap (or VirtualAlloc) to allocate a block of executable memory, and then executes it.

Unlike some JIT implementations which use syscall, this uses function pointers to getchar and putchar. This means that this has access to fully buffered IO instead of laggy syscalls. The assembly wrapper also uses buffered IO in its syscall wrapper.

This is written in C99, but the code is compatible with a C++ compiler if you prefer.

brainfuck-jit's People

Contributors

easyaspi314 avatar

Stargazers

Joe Hinkle avatar Russell Hernandez Ruiz avatar

Watchers

James Cloos avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.