X11-Basic compiler

From x11-basic

Jump to: navigation, search

In addition to the interpreter xbasic (or xbasic.exe) the X11-Basic package ships with a real compiler.

The compilation process is done in three steps:

  1. The BASIC sourcecode (*.bas) is translated into bytecode (*.b) --> xbbc
  2. The bytecode is translated to C sourcecode (using a pseudo assembling language implementing a stack machine). --> xb2c
  3. Finally the produced C source is compiled into native excecutables via a C compiler like gcc or tcc

Steps 1 and 2 are still portable and platform independant.


  • On UNIX and linux systems BASIC source code can behave like a shell script, beeing executed as if it was an excecutable file (involung the interpreter, which therefore needs to be installed.).
  • The BASIC sourcecode can be packed together with the interpreter runtime library to form a standalone executable (thus still interpreted, but sourcecode is not eaysily readable anymore.).
  • The bytecode can be packed together with a virtual machine (--> xbvm)to form a standalone executable (thus not really compiled to native code, but already much faster).
  • Steps 1 to 3 can be done in a very convinient way using xbc which invokes the necessary steps.


Constant propagation is done for all operations.

The xbc compiler

The program xbc is written in X11-Basic and compiles itself. You can find the compiler source code in examples/compiler/xbc.bas.

 xbasic xbc.bas

Will produce a compiled file called xbc which can be directly excecuted.

Please note: If you link your programs dynamically, you will eventually have to compile them again when the libx11basic.so will have changed due to a version update of your X11-Basic package.

See the man page xbc(1) for further information on the compiler.

compiling X11-Basic programs under MS WINDOWS

The X11-Basic packages comes with the compiler xbbc.exe. This is the bytecode compiler. Compiling a basic program is as easy as draging the .bas source file onto the xbbc.exe icon. A bytecode file b.b will be produced. You can run this bytecode with the virtual machine:

xbvm.exe b.b

Different possibilities to compile X11-basic programs under MS Windows

If you want to produce a standalone executable from your BASIC sourcecode, you can use xbc.exe, which is a small compiler-IDE.

The program will ask you for the sourcecode file and the filename of the .exe to be produced. It then compiles the basic program to bytecode and attaches it to a copy of xbvm.exe, thus making this standalone.

The IDE will also ask you which method of compilation should be used. You have four options:

  1. make bytecode, then make a standalone exe from the bytecode,
  2. make bytecode, then translate bytecode to C, and use tcc to compile it,|
  3. only produce bytecode, will produce b.b (see above).
  4. pseudo compile, then use tcc.

Option 1 is recommended. For options 2 and 4 tcc needs to be installed (see below).

In most cases a generated standalone .exe file is what you want. Option 1 will do, al though internally bytecode is interpreted by a virtual machine. If you want a real native compiled program (which is even a little faster and smaller in size), you will need a C compiler, e.g. tcc. If you have installed tcc, you can use Option 2 (or 4, which is not recommended, because it will produce slow and internally interpretted code)

xbc.exe should do all necessary steps to get there, but if you want to understand what is going on, you can do the compile steps manually:

Drag your bytecode (b.b) onto xb2c.exe. This will produce a translated C source file 11.c

You can now compile this to native excecutables using a C compiler, e.g. gcc or tcc. If you do not have a C compiler installed yet, I recommend to get tcc (tiny C compiler) from that page: http://bellard.org/tcc/ To use tcc together with X11-basic, tcc needs to be installed and tcc needs to be in the PATH.

To compile X11-Basic programs, you need x11basic.lib, libgfx.lib, and xb2csol.h e.g.

tcc.exe 11.c x11basic.lib -o myprogram.exe

Please see also the compatibility issues on using the compiler.

(maybe you want to improve these instructions)