Skip to content
/ bflc Public
forked from bynect/bflc

A modular compiler framework for Brainfuck-like languages

License

Notifications You must be signed in to change notification settings

ganajtur0/bflc

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

89 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

The bflc framework

Overview

The BrainFuck-like Languages Compiler (bflc) is a compiler framework that targets Brainfuck and closely related languages.

Installation

    git clone [email protected]:bynect/bflc.git
    cmake -B build
    make -C build

This will create both a static and a dynamic version of the bflc library, and the reference bflc compiler implementation.

Design and API

units

The framework is made of many component, which we call unit, which can be a backend, a frontend or a middle pass. Adding new units is easy thanks to the modular structure of the project.

Backend units

Backend units are made of two functions with the following signatures.

    error_t emit_asm_ARCH(context_t *ctx, bytebuffer_t *buf, ir_t *ir);
    error_t emit_mach_ARCH(context_t *ctx, bytebuffer_t *buf, ir_t *ir);

The emit_asm_ARCH functions emit assembly for ARCH. The emit_mach_ARCH functions emit machine code for ARCH. These functions can report errors based on incorrect context or instructions.

Frontend units

Frontend units are made of one functions with the following signature.

    error_t scan_LANG(context_t *ctx, const char *src, size_t len, ir_t *ir);

The scan_LANG functions generate and intermediate representation from a source language, reporting errors if necessary.

Middle pass units

Middle pass units are made of one function with the following signature.

    error_t pass_PASS(context_t *ctx, ir_t *ir);

The pass_PASS functions pass on the intermediate representation and can change it if appropriate.

Units

As of today the following units are available.

Backends

  • x86_64
    • Assembly output in GAS syntax.
    • Assembly output in Intel Nasm syntax.
    • Assembly output compatible with Nasm -fbin option.
    • Machine code output compatible with jit.

Frontends

Middle passes

  • Validation

    • Check cell overflow.
    • Check pointer overflow.
    • Check unmatched loops.
  • Folding

    • Fold similar instructions.
    • Remove nop instructions.

Planned units

An assembly backend for x86 is being actively developed. A pseudo-backend for the Graphviz Dot language is planned. Machine code output for backends will be added in the future.

Frontends for other Brainfuck-like languages are planned.

JIT integration

Units may provide a machine code generation alongside asm generation. Integrating such code generator in a JIT should be quite straightforward, and an integration example is also available.

Compiler usage

The reference compiler requires a lot of flags to be passed, because it can be used with multiple frontend and backend.

    bflc --front=Brainfuck --back=x86_64 myfile.bf -o outfile.asm

This will compile a Brainfuck input file myfile.bf to x86_64 assembly in Intel Nasm syntax and store it in outfile.asm.

CLI arguments

Flags

-fwrite
-fread
-flibc
-fwrap-cell
-fwrap-ptr

Options

-o filename
--validation=[true/false]
--folding=[true/false]
--front=[brainfuck/fuckbees]
--back=[x86_64]
--asm=[ as|gas|gnu / nasm|intel / nasm-bin|intel-bin ]
--cells=<long long int>

GAS assembly syntax is set by default. The Validation and Folding passes are enabled by default. flibc is set when neither fwrite or fread are set.

History

A very early and incomplete implementation of the project can be found on the early branch of this repository.

Motivation

Why this project was created?

motivation

Contributing

Contributions are welcome. Please open an issue first for major changes.

Before opening a pull request please check for memory leaks, errors and other memory problems with either Asan or Valgrind.

License

The bflc framework is licensed under the terms and conditions of the Apache-2.0 License.

About

A modular compiler framework for Brainfuck-like languages

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C 98.7%
  • CMake 1.3%