Giter Club home page Giter Club logo

bf-jit's Introduction

A JITting Brainfuck Interpreter

##What? Brainfuck is a very minimal programming language which models a (limited) Turing Machine. It has a very small set of commmands which makes it an ideal target for a toy compiler project, like this one.

##Why? This was inspired by Erik's post, as well as my interest in building some form of a compiler. This work follows on from the assembler I started building for the early version of Notch's DCPU.

##How?

  1. Parse the program
  2. Generate code
    1. Pull blocks out of a look-up-table
    2. Construct fixup table
  3. Link jumps and printf calls
  4. mprotect the code buffer to make it executable
  5. Jump to it

Running:

  1. Build (with buildjit.sh)
  2. Run with a brainfuck program in argv[1], e.g. ./jit '>+++++++++[<++++++++>-]<.>+++++++[<++++>-]<+.+++++++..+++.[-]>++++++++[<++++>-]<.>+++++++++++[<++++++++>-]<-.--------.+++.------.--------.[-]>++++++++[<++++>-]<+.[-]++++++++++.' (From Speedy's implementation over at helloworld.org)

##Issues

  • Doesn't support input, yet.
  • Handling of invalid commands (AKA comments) is brittle - spaces break it, for instance.
  • Isn't doing anything particularly clever. Since the IR is a list with some metadata for loops it's not possible to do any fancy optimisations.
  • The lookup-table codegen approach is klunky. Since the code blocks are fixed, smart things like register allocation aren't possible. I guess since block-level info is available it should be feasible to deduce where fragments that repeatedly load & store the same cell could be exchanged for ones that don't waste this time.
  • It doesn't bounds-check the pointer, so it's vulnerable to memory corruption attacks.

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.