Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions content/changelog.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ releases can be downloaded from [GitHub](https://github.com/burnlang/burn/releas

## Unreleased

- Windows: `burn build` makes native Windows x86-64 executables, and the whole toolchain runs on Windows: `burn.exe`, `burni.exe`, `burnc.exe`, `burn-lsp.exe`, `bvm.exe` and `burnfmt.cmd`, with the JIT, async tasks, processes (`cmd /c` for `Process.shell`), HTTP and stack overflow reports. Releases include `burn-windows-x86_64.zip`, built from Linux with mingw-w64 and tested on Windows in CI ([installation](./tooling/installation.mdx#windows), [cross-compiling](./tooling/cross-compiling.mdx)).
- `@Extern` takes a Windows name next to the macOS one (`windows: "_errno"`), and C functions can take more than 6 integer parameters ([calling C](./language/calling-c.mdx)).
- Native code is much faster ([native executables](./internals/native.mdx#a-function)): the hottest locals in loops live in registers, parameters are no longer copied on every call, `push` stores in place, division by a constant uses a multiplication instead of the divide instruction, small constants are immediates, and small functions are inlined. A prime sieve runs 8× faster and pushing to an array 20× faster, on x86-64 and arm64.
- Builds are about twice as fast: large programs are assembled in parallel on every core, the generated assembly is a quarter smaller, and the runtime frees memory and collects cycles without allocating. The compiler builds itself in 6.3 s instead of 11.8 s, and the same runtime changes make every program that allocates a lot faster.
- Local variables that only hold a parameter, another variable, or a field or element of one now borrow their value instead of counting a reference, when nothing can free it while they are used ([memory](./language/memory.mdx#what-the-compiler-does)). Loops over arrays of objects and strings and walks over linked structures no longer touch reference counts; the compiler's own code does 8.5% fewer of them and builds faster.
Expand Down
20 changes: 18 additions & 2 deletions content/internals/native.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ order: 53
# Native executables

`burn build` (and `burnc`) turns a program into a standalone executable for the machine: x86-64 or 64-bit ARM, on
Linux or macOS. It starts from the same IR as bvm, after the [ownership pass](./memory.mdx), and generates assembly
Linux or macOS, and x86-64 on Windows. It starts from the same IR as bvm, after the [ownership pass](./memory.mdx), and generates assembly
text that the system's C compiler assembles and links. A large program's assembly is cut into pieces of about 150000
lines at function boundaries, which are assembled in parallel on every core and linked together.

Expand Down Expand Up @@ -149,8 +149,24 @@ Code generation only depends on the target platform, which `BURN_PLATFORM` selec
`macos-x86_64`, `linux-aarch64`, `macos-aarch64`). Linking needs a runtime library and a linker for that platform:
`BURN_RUNTIME_DIR` and `BURN_CC`. See [cross-compiling](../tooling/cross-compiling.mdx).

## Windows

Windows executables are PE/COFF files made by mingw-w64. Burn functions call each other and the Burn runtime with the
same convention as on Linux; only code that crosses into Windows uses the Win64 convention:

- calls to C and Win32 functions put the first four arguments in `rcx`, `rdx`, `r8` and `r9` (floats in
`xmm0`–`xmm3`) and the rest above 32 bytes of shadow space;
- `main` takes `argc` and `argv` from `rcx` and `rdx` and keeps `rdi` and `rsi`, which Windows callers expect;
- every function the runtime exports has a `_w64` entry that moves the Win64 arguments into place, which the
runtime hands to Windows for thread starts and exception handlers.

The runtime's `lib/rt/win.bn` provides what Windows lacks or lays out differently (`uname`, `stat`, `readdir`,
`localtime_r`, `realpath`, `mmap` for the JIT, ...) in the layout the rest of the runtime reads; `@Extern(...,
windows: "...")` points the declarations at them. Programs run with `CreateProcessA`, sockets go through Winsock,
and standard output and files are binary, so `\n` stays `\n`.

## Stack overflow

The runtime catches a stack overflow with a signal handler running on its own small stack and prints
`runtime error: stack overflow (recursion is too deep)` before exiting with `1`, on Linux and macOS, x86-64 and
arm64.
arm64. On Windows a vectored exception handler moves the thread to a stack of its own and prints the same message.
7 changes: 5 additions & 2 deletions content/language/calling-c.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,11 @@ unsafe {
[`std/memory`](../stdlib/memory.mdx).
- **Results.** A sized integer comes back the way C returns it and is widened like the type says: an `Int32` result
of `-1` is `-1`, a `UInt8` result keeps only its lowest 8 bits.
- **Registers.** At most 6 integer and 8 float parameters, which is what C passes in registers on every platform
Burn supports. Variadic functions such as `printf` are not supported.
- **Parameters.** Any number of integer parameters (the ones after the registers go on the stack) and at most 8 float
parameters. Variadic functions such as `printf` are not supported.
- **Names per system.** When the symbol differs on macOS or Windows, name it there too:
`@Extern("__errno_location", macos: "__error", windows: "_errno")`. On Windows the call uses the Win64 calling
convention.
- **Return type.** It must be written, `: void` when there is none.
- **Unsafe.** Nothing checks what a C function does with memory, so calling one, or using it as a value, needs
[unsafe code](./unsafe.mdx).
Expand Down
29 changes: 24 additions & 5 deletions content/tooling/cross-compiling.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ machine code itself, then hands it to a C toolchain to assemble and link it with
| `linux-aarch64` | 64-bit ARM | Raspberry Pi 4 and 5 with a 64-bit OS, ARM servers such as AWS Graviton |
| `macos-x86_64` | x86-64 | Macs with an Intel processor |
| `macos-aarch64` | 64-bit ARM | Macs with Apple silicon: M1, M2, M3, M4 and later |
| `windows-x86_64` | x86-64 | Windows 10 and 11 PCs (executables are `.exe`) |

On any other system (Windows, 32-bit ARM, RISC-V), use `burn run`, bytecode (`--target bvm`), archives
On any other system (32-bit ARM, RISC-V), use `burn run`, bytecode (`--target bvm`), archives
(`--target bar`) or JavaScript (`--target js`), which run everywhere bvm or Node.js runs.

```sh
Expand All @@ -31,8 +32,9 @@ burn dump --asm app.bn # the assembly it was built from
## What a native build needs

- **A C toolchain.** `cc` assembles the generated `.s` file and links the executable. On macOS, install the Xcode
command line tools (`xcode-select --install`); on Linux, `gcc` or `clang` (`apt install build-essential`). Set
`BURN_CC` (or `CC`) to use another one.
command line tools (`xcode-select --install`); on Linux, `gcc` or `clang` (`apt install build-essential`); on
Windows, `gcc` from MinGW-w64 ([MSYS2](https://www.msys2.org/) or [w64devkit](https://github.com/skeeto/w64devkit)),
which `burn build` runs by default there. Set `BURN_CC` (or `CC`) to use another one.
- **The runtime.** Every toolchain ships it in `share/burn/lib`: `libburn_runtime.a` (the runtime written in Burn),
`libburn_launcher.a` (bvm written in Burn, for executables that run bytecode) and `native-libs.txt` (the system
libraries the runtime needs).
Expand Down Expand Up @@ -73,8 +75,25 @@ burnc app.bn -o app-arm64
```

`scripts/native-runtime.sh` is in the Burn repository; it compiles the runtime written in Burn (and bvm) with `burn`
for the target you name (`aarch64-apple-darwin`, `x86_64-apple-darwin`, `aarch64-unknown-linux-gnu` or
`x86_64-unknown-linux-gnu`) and assembles it with `BURN_CC`. `BURN_PLATFORM` alone is enough to look at the generated code:
for the target you name (`aarch64-apple-darwin`, `x86_64-apple-darwin`, `aarch64-unknown-linux-gnu`,
`x86_64-unknown-linux-gnu` or `x86_64-w64-mingw32`) and assembles it with `BURN_CC`.

Windows executables are built the same way with mingw-w64, from Linux or macOS:

```sh
sudo apt install mingw-w64

TARGET=x86_64-w64-mingw32 BURN_CC=x86_64-w64-mingw32-gcc AR=x86_64-w64-mingw32-ar \
sh scripts/native-runtime.sh ~/burn-runtime-windows

BURN_PLATFORM=windows-x86_64 \
BURN_RUNTIME_DIR=~/burn-runtime-windows \
BURN_CC=x86_64-w64-mingw32-gcc \
burnc app.bn -o app # app.exe
```

`scripts/package-windows.sh` builds the whole toolchain for Windows this way: `burn.exe` and its other names,
`burnfmt.cmd` and `share/burn`, the folder that `burn-windows-x86_64.zip` in each release contains. `BURN_PLATFORM` alone is enough to look at the generated code:

```sh
BURN_PLATFORM=macos-aarch64 burn dump --asm app.bn
Expand Down
18 changes: 17 additions & 1 deletion content/tooling/installation.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ See [the toolchain](./toolchain.mdx) for what each command does.

## Requirements

- Linux or macOS (Windows: see [manual installation](#manual-installation))
- Linux or macOS (Windows: see [Windows](#windows))
- `git`, which burnup and ash use to download Burn versions and packages
- A C compiler (`cc`), `tar` and `xz` for versions built from source (branches such as `master`, and any version
without a prebuilt release): the C compiler assembles the seed that comes with the sources, which builds the rest.
Expand Down Expand Up @@ -124,6 +124,22 @@ tar -xzf burn-linux-x86_64.tar.gz
./burn/bin/burn version
```

## Windows

Each release has `burn-windows-x86_64.zip`. Unzip it, for example to `%LOCALAPPDATA%\Programs\burn`, and add its
`bin` folder to `PATH`:

```powershell
Expand-Archive burn-windows-x86_64.zip "$env:LOCALAPPDATA\Programs"
[Environment]::SetEnvironmentVariable("Path", "$env:Path;$env:LOCALAPPDATA\Programs\burn\bin", "User")
```

`burn.exe`, `burni.exe`, `burnc.exe`, `burn-lsp.exe`, `bvm.exe` and `burnfmt.cmd` then work like on the other
systems. Building native executables needs `gcc` from MinGW-w64, such as [MSYS2](https://www.msys2.org/)
(`pacman -S mingw-w64-ucrt-x86_64-gcc`) or [w64devkit](https://github.com/skeeto/w64devkit), on `PATH`; everything
else works without it. The Windows toolchain is built from Linux with `scripts/package-windows.sh`
([cross-compiling](./cross-compiling.mdx#cross-compiling)).

## Manual installation

Linux and macOS, on x86-64 and arm64, can build the toolchain from a checkout with a C compiler, `tar` and `xz`:
Expand Down
Loading