Giter Club home page Giter Club logo

awib's Introduction

example workflow

Awib is a brainfuck compiler entirely written in brainfuck.

  • Awib implements several optimization strategies and its compiled output outperforms that of many other brainfuck compilers
  • Awib is itself a 4-language polyglot and can be run/compiled as brainfuck, Tcl, C and bash
  • Awib has 6 separate backends and is capable of compiling brainfuck source code to Linux executables (i386) and five programming languages: C, Tcl, Go, Ruby and Java

Usage

Feed awib brainfuck source code as input and the compiled program will be written as output.

Awib is a cross-compiler. The supported target platforms are listed below. By default, the target "lang_c" is chosen.

To specify a target platform, insert a line on the form "@TARGET" (without the quotation marks and with "TARGET" suitably replaced) at the very beginning of the source code you wish to compile. Awib will then produce output accordingly.

386_linux - Linux executables for i386
lang_c    - C code
lang_ruby - Ruby code
lang_go   - Go code
lang_tcl  - Tcl code
lang_java - Java code

For instance, the following input would produce an executable hello world-program for Linux:

@386_linux
++++++[->++++++++++++<]>.----[--<+++>]<-.+++++++..+++.[--->+<]>-----.--
-[-<+++>]<.---[--->++++<]>-.+++.------.--------.-[---<+>]<.[--->+<]>-.

The following would produce a hello world-program in Ruby:

@lang_ruby
++++++[->++++++++++++<]>.----[--<+++>]<-.+++++++..+++.[--->+<]>-----.--
-[-<+++>]<.---[--->++++<]>-.+++.------.--------.-[---<+>]<.[--->+<]>-.

And this file would give you the hello world-program in C:

@lang_c
++++++[->++++++++++++<]>.----[--<+++>]<-.+++++++..+++.[--->+<]>-----.--
-[-<+++>]<.---[--->++++<]>-.+++.------.--------.-[---<+>]<.[--->+<]>-.

Optimizations

Awib is an optimizing compiler:

  • Sequences of '-','>','<' and '+' are contracted into single instructions. E.g. "----" is replaced with a single SUB(4).
  • Mutually cancelling instructions are reduced. E.g. "+++-->><" is equivalent to "+>" and is compiled accordingly.
  • Some common constructs are identified and replaced with single instructions. E.g. "[-]" is compiled into a single SET(0).
  • Loops known to never be entered are removed. This is the case for loops opened at the very beginning of a program (when all cells are 0) and loops opened immediately after the closing of another loop.
  • Copy and multiplication loops are replaced with constant time operations. E.g. "[->>+++<+<]" is compiled into two RMUL(2, 3), RMUL(1,1)), SET(0)..

Requirements

Awib will run smoothly in any brainfuck environment where:

  • Cells are 8-bit or larger
  • The read instruction ',' (comma) issued after end of input results in 0 being written OR -1 being written OR no change being made to the cell at all.

The vast majority of brainfuck environments meet these criteria.

Since awib is polyglot, it is also possible to compile and/or run awib directly as C, tcl or bash. For instance, using gcc, the following will build an executable file called awib from awib-0.2.b.

$ cp awib-0.2.b awib-0.2.c
$ gcc awib-0.2.c -o awib.tmp
$ ./awib.tmp < awib-0.2.b > awib-0.2.c
$ gcc -O2 awib-0.2.c -o awib

Using bash works fine, but is very very very slow:

$ (echo "@386_linux"; cat awib.b) | bash awib.b > awib
$ chmod +x awib

And tcl:

$ (echo "@386_linux"; cat awib.b) | tclsh awib.b > awib
$ chmod +x awib

Environment

Code compiled with awib will execute in an environment where:

  • Cells are 8-bit wrapping integers.
  • Issuing the read instruction ',' (comma) after end of input results in the current cell being left as is (no-change on EOF).
  • At least 2^16-1 = 65535 cells are available.
  • Operating beyond the available memory, in either direction, results in undefined behaviour.

License

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.

Mats Linander, 2014-09-14

awib's People

Contributors

matslina avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

awib's Issues

No longer builds with clang 16

See also https://wiki.gentoo.org/wiki/Modern_C_porting.

In Clang/LLVM 16 the following errors are enabled by default:

-Werror=implicit-function-declaration
-Werror=implicit-int
-Werror=int-conversion
-Werror=incompatible-function-pointer-types

Building awib with CLANG 16 and presumably GCC 14 leads to the following errors.

cp awib.b awib_make_binary.c
gcc  -std=c99 awib_make_binary.c  -o awib_make_binary.bin -O2
awib_make_binary.c:522:1: error: type specifier missing, defaults to 'int'; ISO C99 and later do not support implicit int [-Wimplicit-int]
_K_,_KK,__K,____,__[4>>002<<020]; _(__){__K=____?__K:__;____=__;while(
^
int
awib_make_binary.c:522:5: error: type specifier missing, defaults to 'int'; ISO C99 and later do not support implicit int [-Wimplicit-int]
_K_,_KK,__K,____,__[4>>002<<020]; _(__){__K=____?__K:__;____=__;while(
    ^
int
awib_make_binary.c:522:9: error: type specifier missing, defaults to 'int'; ISO C99 and later do not support implicit int [-Wimplicit-int]
_K_,_KK,__K,____,__[4>>002<<020]; _(__){__K=____?__K:__;____=__;while(
        ^
int
awib_make_binary.c:522:13: error: type specifier missing, defaults to 'int'; ISO C99 and later do not support implicit int [-Wimplicit-int]
_K_,_KK,__K,____,__[4>>002<<020]; _(__){__K=____?__K:__;____=__;while(
            ^
int
awib_make_binary.c:522:18: error: type specifier missing, defaults to 'int'; ISO C99 and later do not support implicit int [-Wimplicit-int]
_K_,_KK,__K,____,__[4>>002<<020]; _(__){__K=____?__K:__;____=__;while(
                 ^
int
awib_make_binary.c:522:37: error: parameter '__' was not declared, defaults to 'int'; ISO C99 and later do not support implicit int [-Wimplicit-int]
_K_,_KK,__K,____,__[4>>002<<020]; _(__){__K=____?__K:__;____=__;while(
                                    ^
awib_make_binary.c:522:35: error: type specifier missing, defaults to 'int'; ISO C99 and later do not support implicit int [-Wimplicit-int]
_K_,_KK,__K,____,__[4>>002<<020]; _(__){__K=____?__K:__;____=__;while(
                                  ^
                                  int
awib_make_binary.c:522:35: warning: a function definition without a prototype is deprecated in all versions of C and is not supported in C2x [-Wdeprecated-non-prototype]
awib_make_binary.c:524:30: error: type specifier missing, defaults to 'int'; ISO C99 and later do not support implicit int [-Wimplicit-int]
+K]&1):__*__;_KK+=__;_(__);}}___(){while(K[++_KK]){    _K_('v'^']',__[
                             ^
                             int
awib_make_binary.c:525:13: warning: | has lower precedence than ==; == will be evaluated first [-Wparentheses]
,]++)_K_(055|___==___,__[,]--)_K_('K'^'u',,++)_K_('w'^'K',--,)_K_('_'^
            ^~~~~~~~~
awib_make_binary.c:521:32: note: expanded from macro '_K_'
#define _K_(___,_K,__)(K[_KK]^(___))?_K_^_K_:_K _K_ __;
                               ^~~
awib_make_binary.c:525:13: note: place parentheses around the '==' expression to silence this warning
,]++)_K_(055|___==___,__[,]--)_K_('K'^'u',,++)_K_('w'^'K',--,)_K_('_'^
            ^~~~~~~~~
awib_make_binary.c:521:32: note: expanded from macro '_K_'
#define _K_(___,_K,__)(K[_KK]^(___))?_K_^_K_:_K _K_ __;
                               ^~~
awib_make_binary.c:525:13: note: place parentheses around the | expression to evaluate it first
,]++)_K_(055|___==___,__[,]--)_K_('K'^'u',,++)_K_('w'^'K',--,)_K_('_'^
         ~~~^~~~
awib_make_binary.c:521:32: note: expanded from macro '_K_'
#define _K_(___,_K,__)(K[_KK]^(___))?_K_^_K_:_K _K_ __;
                               ^~~
awib_make_binary.c:527:40: error: call to undeclared function 'putchar'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
[K]&1)):_(0))K[_KK]-46?_(__[_K_]&1>>1):putchar(__[_K_]);if(_KK[K]^(1<<
                                       ^
awib_make_binary.c:528:26: error: call to undeclared function 'getchar'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
1^'.'))_(0);else{__[_K_]=getchar();}}}main(){--_KK;___();}
                         ^
awib_make_binary.c:528:39: error: type specifier missing, defaults to 'int'; ISO C99 and later do not support implicit int [-Wimplicit-int]
1^'.'))_(0);else{__[_K_]=getchar();}}}main(){--_KK;___();}
                                      ^
                                      int
2 warnings and 11 errors generated.
make: *** [Makefile:7: binary] Error 1

awib_make_binary.bin returns 141, fails make

Because of the lack of a return() statement in the main() function, awib_make_binary.bin returns with 141, which makes GNU make exit because of failure. Adding "return(0);" to the main() function in the C part of awib.b fixes that, but this thing is so messy that I have no idea where to even start fixing it.

Unexpected negative array access.

This BF generates

+[>]<- [<<<<<<<<<<<<<<<<+>>>>>>>>>>>>>>>>-]

The below C code, notice the negative array index is accessed despite the fact that the second loop isn't run. The program mandelbrot.b has this sort of construct.
However, this doesn't actually segfault for me, but the @lang_go variant does throw the error both for my tiny example above and mandelbrot.b.

#include <stdio.h>
char    buf[0xffff];
int
main()
{
    char   *p = buf;
    int     c;
    *p += 1;
    while (*p) {
        p += 1;
    }
    p -= 1;
    *p -= 1;
    *(p - 16) += *p * 1;
    *p = 0;
    return 0;
}

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.