Getting started

Building the compiler

x compiles itself. With a copy of bin\x.exe, rebuild it from the source:

bin\x.exe src\x.pb -o bin\x.exe

x was made by studying PureBasic's documentation and making x compatible with it, so PureBasic programs compile with x as they are.

The compiler needs the runtime folder that sits next to bin. It holds the runtime library, which is written in x and compiled into every program.

A first program

Save this as hello.pb:

OpenConsole()
PrintN("Hello, world")

Output:

Hello, world

Compile and run it:

bin\x.exe hello.pb
hello.exe

x writes hello.exe next to the source. Use -o to choose another name: bin\x.exe hello.pb -o greet.exe.

There is no linker, no assembler and no C compiler involved: x writes the executable directly. hello.exe is about 4.5 KB, because only the library procedures, arrays and data tables a program uses are included.

File extensions

x reads PureBasic sources (.pb, .pbi) and x sources (.x). The extension makes no difference to the language.

Two styles, one language

PureBasic code is x code. The C-style spellings are alternatives, not a separate dialect. These two programs are the same:

; PureBasic style
Procedure.i Square(n.i)
  ProcedureReturn n * n
EndProcedure

OpenConsole()
For i = 1 To 3
  PrintN(Str(Square(i)))
Next

Output:

1
4
9
// C style
int Square(int n) {
  return n * n
}

OpenConsole()
for (int i = 1; i <= 3; i++) {
  PrintN(Str(Square(i)))
}

Output:

1
4
9

Things both styles share:

  • A new line ends a statement, so no ; is needed.
  • = is both assignment and comparison. Which one it is depends on where it appears (Operators).
  • Names are case-insensitive: PrintN, printn and PRINTN are the same.

Building for Linux

x also makes programs for Linux on x64 machines:

bin\x.exe hello.pb -target linux-x64 -o hello

The result is a single ELF file with no dependencies, not even the C library: the runtime talks to Linux directly through system calls. Copy it to the Linux machine, mark it executable (chmod +x hello) and run it.

x itself runs on Linux as well (build it with bin\x.exe src\x.pb -target linux-x64 -o x). There it makes Linux programs by default, already executable, and -target windows-x64 makes Windows ones.

Programs behave the same on both, with the usual differences: lines end with a line feed (Chr(10)) instead of carriage return and line feed, paths use /, #PB_Compiler_OS is #PB_OS_Linux, and Import (which loads Windows DLLs) isn't available. Use CompilerIf #PB_Compiler_OS = ... for code that differs.

Other compiler modes

Command Does
x file.pb compile to file.exe
x file.pb -o out.exe compile to out.exe
x file.pb -target linux-x64 -o out compile for Linux x64 (see above)
x file.pb -time print how long each compiler phase took
x file.pb -thread thread-safe runtime (automatic when the program calls CreateThread)
x file.pb -noalign don't pad branches off 32-byte boundaries (see below)
x file.pb -ir generate procedures through the IR where it can (experimental; see below)
x file.pb -ir force the same, and an error for any procedure it can't take
x file.pb -irdump -ir, and print each procedure's IR with its registers, or why it stayed with the tree code generator
x -check file.pb check the program and print its types and symbols
x -ast file.pb print the syntax tree
x -tokens file.pb print the tokens

Branch alignment

On Intel processors from Skylake on, a loop runs much more slowly if one of its jumps, calls or returns crosses or ends on a 32-byte boundary in memory. The same applies to a compare and the jump that goes with it. (It was a microcode fix for an erratum, and is known as the "JCC erratum".) A small change elsewhere in a program can shift its code onto such a boundary, and a tight loop can lose a fifth of its speed. So x puts a few bytes of NOPs in front of any branch that would land on one, as gcc does with -mbranches-within-32B-boundaries. It uses the long multi-byte NOPs, which cost one cheap instruction each.

It also starts each procedure on a 16-byte boundary. A loop that x lays out as "jump to the test, body, test" gets its body on one too, if that takes no more than 7 bytes. That padding goes after a ret or jmp, where it is never executed.

Programs grow by about 4% (the benchmark programs not at all on disk), and their speed no longer depends on where their code happens to fall. Other processors are unaffected either way. -noalign leaves all of this out.

The IR (-ir)

-ir sends each procedure that it can through x's intermediate representation (IR), the base for the optimisations to come. Each procedure becomes SSA form with its values in registers, then machine code. Procedures it can't take yet, such as ones with strings, structures or Select, are compiled as before. A program's output must be the same either way. The design has what it takes and how it's tested.

It's off by default until it does more than match the existing code generator. With -ir, -time prints how many procedures went through it.

Procedure.i SumTo(n.i)
  Protected i.i, s.i
  For i = 1 To n
    s + i
  Next
  ProcedureReturn s
EndProcedure

OpenConsole()
PrintN(Str(SumTo(100)))

Output:

5050

x sum.pb -irdump prints, among the runtime's procedures:

ir: sumto
  b1:
    v1 = param    ; r10
    v3 = const 0
    v4 = const 1
    v15 = copy 0    ; r11
    v16 = copy 1    ; r8
    jmp b2
  b3:
    v7 = add v14, v10    ; r11
    jmp b4
  b4:
    v8 = const 1
    v9 = add v10, 1    ; r8
    v15 = copy v7    ; r11
    v16 = copy v9    ; r8
    jmp b2
  b2:
    v14 = copy v15    ; r11
    v10 = copy v16    ; r8
    v13 = cmp v10, v1
    br v13 ? b3 : b5
  b5:
    ret v14

Registers are shown by their x64 number (r10 is r10, r3 would be rbx). The loop is laid out body, step, test (b3, b4, b2). The copies at the end of b4 and the start of b2 (the phis, out of SSA form) cost nothing: each side is in the same register. Nothing here is live across a call, so it all fits in registers that need no saving.

Status

The compiler produces 64-bit executables for Windows and Linux. macOS, ARM64, RISC-V, the 68000 (Atari ST, Amiga) and the Z80N (ZX Spectrum Next) are planned: see the roadmap.

eXperimental is an independent implementation: no proprietary PureBasic source code or implementation code is incorporated into it. PureBasic is a product of Fantaisie Software. eXperimental is neither affiliated with nor endorsed by Fantaisie Software.