Home Befunge
News News
Befunge Befunge
   - Programs
   - Compilers
   - Interpreters
   - Documentation

Doom Doom
Inform Inform
Quake Quake
RISC OS RISC OS
Misc Misc
Links Links
Contact Contact

C-to-Befunge compiler

(c2b.zip, 18,379 bytes)

Yup, you read that right. For a while I worked on a project to provide a set of tools to enable a standard C compiler to produce Befunge code. And it would have worked too, if it wasn't for you pesky kids me never getting round to finishing it. Basic operation is as follows:

  1. The C compiler (e.g. LCC) would produce Befunge-Object files; i.e. files containing sections of Befunge code
  2. The linker (At the moment written in PHP, since I felt that was the easiest langauge to use at the time) would read the series of object files (I.e. the program and a hand-coded C library) and link it together into the output program
  3. ... which would then be fed into your favourite interpeter.

My language of choice was Befunge-93, since in my opinion Befunge 98 would have made things waaay too easy, and writing a compiler would have been nowhere near as hard fun. Obviously you won't be able to fit much code into a standard 80x25 space, so the interpreter itself would have to be befunge 98, but only the befunge-93 instructions would be in use.

The layout of a compiled program would be roughly as follows:

ZZZsssMMMM
ZZZsssSSSS
ZZZsss
BBB CCCCCC
BB
B B CCCCCC
BBB CCCCCC
 B
  B CCCCCC

Where:

  • Z is the 'zero page' area; i.e. coordinates 0-9 so it can be addressed with only 2 instructions.
  • s is the stub code for handling the function call stack - i.e. calling a function, returning from a function, etc.
  • M is the program main memory - note that it is only a 1 dimensional strip, in order to allow normal pointer math to be used
  • S is the function call stack. The main befunge stack is used for parameter passing though.
  • B is the binary branch tree, to allow functions to be entered by passing the function number to the stub code
  • The C's are the functions themselves, which are likely to be fairly linear due to the compilation technique. Exactly whether local branches (if's, while's, etc.) will be produced as seperate functions or normal Befunge loops is undecided at the moment.

The current version of the linker supports:

  • Inserting code sequences to generate routine/data addresses
  • Code segments with multiple entry points
  • Local and global symbols

The archive of files you can download isn't particularly user friendly; it's just everything I could find stuffed into one directory and a short readme file to tell you what's what. The rest is up to you to work out :p

Update: 07/11/2003

At some point I may have a go at finishing this off. The program structure is likely to stay the same, but the Befunge Object files may get replaced with code graphs similar to those used in the befc V3 compiler; This would also mean making use of the full Funge-98 instruction set, as well as the meta-instructions which the compiler uses. This way, all that's needed are modifications to a C compiler to produce befc-compatible code graphs, a modification to befc to allow the loading of graphs, as well as a Befunge output module for the compiler. Now that befc 3 is out, there's a good chance this project could happen soon.

Update: 02/03/2005

  "Now that befc 3 is out, there's a good chance this project could happen soon."

Hah!



Page last modified 03/03/2005