ack/lang/b
2017-01-01 17:40:06 +00:00
..
compiler Add support for the ~ operator. 2017-01-01 17:40:06 +00:00
distr Initial EM-ification; start threading word size stuff through the code. 2016-11-27 11:58:59 +01:00
lib Allow programs to override binit() (so they can register their own modules). 2016-12-31 17:39:51 +00:00
LICENSE Initial EM-ification; start threading word size stuff through the code. 2016-11-27 11:58:59 +01:00
README.md Initial EM-ification; start threading word size stuff through the code. 2016-11-27 11:58:59 +01:00

A B Compiler

abc is a compiler for the B Programming Language that targets x86_32 processors. It is currently tested under Linux but should work (or at least be easily ported) to other UNIX-like systems. The code is based on an early C compiler (last1120c) by Dennis Ritchie.

Documentation

  • The Programming Language B

  • B Reference by Ken Thompson describes a presumably earlier variant of B, which is slightly different from the one described above. The compiler cannot understand it, but I plan to implement a compatibility mode (the differences are minor).

Implementation

Since B was first implemented for machines with word addressing, some hacking was required to make it work on the byte addressed x86. Addresses filled in by the linker are always byte addresses, so pointers to these addresses are collectively stored at the end of the .data section and are then converted to word addresses at runtime, before main() is called.

The generated assembly is very inefficient, not even constant expressions are reduced at compile time. Also I/O is currently not buffered.

How to use

The installation requires a little configuration: 'abc' is a frontend for the actual compiler which feels somewhat like gcc (it also handles assembling and linking). Before you can use it, set it's BDIR variable to the directory of the B compiler. In the Makefile, change the directory of the 'install' rule to wherever you want your 'abc' file to reside. Then type

make install libs

which compiles the compiler 'b', installs the 'abc' frontend and compiles the B runtime and library (brt.o and lib.o).

To compile and link a B program, simply type

abc -o outfile file1.b [file2.b ...]

If you want to compile and assemble only:

abc -c file1.b [file2.b ...]

or generate only the assembly:

abc -S file1.b [file2.b ...]

Examples of B programs are in the 'examples' directory, they are mostly from Brian Kernighan's tutorial.

Bugs

Since command line parameters aren't passed word-aligned, B can't handle them easily. brt.s copies the strings to another location and aligns them, the space is not dynamically allocated however and only 256 bytes are available by default.

The library is incomplete but has some of the most important functions.

I have only tested the compiler on an x86_64 gentoo system.