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
99 changes: 99 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -202,3 +202,102 @@ jobs:
./scripts/install.ps1
& "$env:TRACK_INSTALL_DIR\trackc.exe" --version
& "$env:TRACK_INSTALL_DIR\yard.exe" --version

windows-arm-cross:
# Cross-build the aarch64-windows executables on Linux: trackc.exe,
# hello and every example (docs/native.md, "PE32+ layout"); the
# windows-arm job runs them on Windows on ARM.
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Bootstrap trackc
run: scripts/bootstrap.sh

- name: Cross-build for aarch64-windows and parse the images
run: |
tc=build/bootstrap/stage3/trackc
d=build/winarm
mkdir -p "$d/examples"
"$tc" build --target aarch64-windows compiler/src/main.trk -o "$d/trackc.exe"
for f in examples/*.trk; do
s=$(basename "$f" .trk)
case "$s" in *_err) continue ;; esac
"$tc" build --target aarch64-windows "$f" -o "$d/examples/$s.exe"
done
if command -v llvm-readobj >/dev/null; then
for x in "$d/trackc.exe" "$d"/examples/*.exe; do
llvm-readobj --file-headers --coff-imports "$x" | grep -q 'IMAGE_FILE_MACHINE_ARM64'
done
fi

- uses: actions/upload-artifact@v4
with:
name: winarm-binaries
path: build/winarm
if-no-files-found: error

windows-arm:
# [untested here] Runs the cross-built binaries on GitHub's Windows on
# ARM runner: the examples against their goldens, then trackc.exe
# rebuilds hello on the device and must produce the same bytes as the
# Linux trackc did; last, the Arm64 trackc.exe runs the windows suite
# (its default target there is aarch64-windows).
needs: windows-arm-cross
runs-on: windows-11-arm
defaults:
run:
shell: pwsh
steps:
- uses: actions/checkout@v4

- uses: actions/download-artifact@v4
with:
name: winarm-binaries
path: build/winarm

- name: Show the machine
run: |
$env:PROCESSOR_ARCHITECTURE
[System.Runtime.InteropServices.RuntimeInformation]::OSArchitecture

- name: Examples print their goldens
run: |
$d = Resolve-Path build/winarm
$fails = 0
foreach ($f in Get-ChildItem examples -Filter '*.trk' | Sort-Object Name) {
$s = $f.BaseName
if ($s.EndsWith('_err')) { continue }
$exe = Join-Path $d "examples\$s.exe"
$out = Join-Path $d "examples\$s.out"
$psi = New-Object System.Diagnostics.ProcessStartInfo
$psi.FileName = $exe
$psi.WorkingDirectory = (Resolve-Path examples).Path
$psi.UseShellExecute = $false
$psi.RedirectStandardOutput = $true
$p = [System.Diagnostics.Process]::Start($psi)
$fs = [System.IO.File]::Create($out)
try { $p.StandardOutput.BaseStream.CopyTo($fs) } finally { $fs.Close() }
$p.WaitForExit()
$want = ([System.IO.File]::ReadAllText("tests/cases/examples/$s.out")).Replace('@HOSTNAME_BYTES@', '0')
$got = [System.IO.File]::ReadAllText($out)
$code = [int]([System.IO.File]::ReadAllText("tests/cases/examples/$s.code").Trim())
if ($got -eq $want -and $p.ExitCode -eq $code) { Write-Host "ok $s" }
else { Write-Host "FAIL $s (exit $($p.ExitCode), want $code)"; $fails++ }
}
if ($fails -ne 0) { exit 1 }

- name: trackc.exe builds hello on the device, byte-identical to the cross build
run: |
New-Item -ItemType Directory -Force -Path build/ondevice | Out-Null
& build/winarm/trackc.exe build --target aarch64-windows examples/hello.trk -o build/ondevice/hello.exe
if ($LASTEXITCODE -ne 0) { exit 1 }
$a = Get-FileHash build/ondevice/hello.exe
$b = Get-FileHash build/winarm/examples/hello.exe
if ($a.Hash -ne $b.Hash) { Write-Host "hello.exe differs from the cross build"; exit 1 }
& build/ondevice/hello.exe
& build/winarm/trackc.exe check examples/stdlib_demo.trk
if ($LASTEXITCODE -ne 0) { exit 1 }

- name: The Arm64 trackc.exe passes the windows suite natively
run: ./tests/windows/run.ps1 build/winarm/trackc.exe
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@
- **Lexical Lenses.** Scoped mutable access without manual pointer arithmetic or ownership transfer.
- **Native Backend.** `trackc build` writes a static x86-64 Linux executable itself, with no C compiler, assembler or linker.
- **Track only.** The repository holds no C or Rust. Track bootstraps from a committed native seed binary.
- **Cross-compiling.** `trackc build --target x86_64-windows` writes a Windows PE executable from any host; `--target x86_64-macos` / `aarch64-macos` a signed Mach-O.

| target | `trackc build --target` | status |
|---|---|---|
| x86_64-linux | default on Linux | static ELF |
| x86_64-windows | `x86_64-windows` | PE32+ console .exe (kernel32); trackc.exe self-hosts |
| aarch64-linux | `aarch64-linux` | static ELF (A64 lifted from the x86-64 code) |
| x86_64-macos | `x86_64-macos` (default on a Mac) | Mach-O, libSystem, ad-hoc signed |
| aarch64-macos | `aarch64-macos` | Mach-O for Apple silicon, ad-hoc signed |
| aarch64-windows | | not yet |
- **Cross-compiling.** `trackc build --target <arch>-<os>` writes any of six targets from any host: a Windows PE (x86_64 or Windows on ARM), a static Linux ELF, or a signed Mach-O.

| target | `trackc build --target` | output | tested |
|---|---|---|---|
| x86_64-linux | default on Linux | static ELF | native (tests/run.sh) |
| x86_64-windows | default on x64 Windows | PE32+ console .exe (kernel32); trackc.exe self-hosts | wine locally; native in CI (`windows`) |
| aarch64-linux | `aarch64-linux` | static ELF (A64 lifted from the x86-64 code) | qemu-aarch64 locally |
| x86_64-macos | default on a Mac | Mach-O, libSystem, ad-hoc signed | structure offline; runs in CI only (`macos`, Intel) |
| aarch64-macos | `aarch64-macos` | Mach-O for Apple silicon, ad-hoc signed | structure offline; runs in CI only (`macos`, Apple silicon) |
| aarch64-windows | default on Windows on ARM | ARM64 PE32+ console .exe (kernel32) | llvm-readobj/objdump offline; runs in CI only (`windows-arm`) |

## Hello World

Expand Down
Binary file modified bootstrap/seed/trackc
Binary file not shown.
Binary file modified bootstrap/seed/trackc.exe
Binary file not shown.
42 changes: 25 additions & 17 deletions compiler/src/main.trk
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ fn usage() -> i64 {
err("\nOPTIONS:\n");
err(" --deny-warnings exit 1 when any warning is reported\n");
err(" --target <arch>-<os> build/run/check for x86_64|aarch64 - linux|macos|windows\n");
err(" (also TRACKC_TARGET; default: the host; built today: x86_64-linux/macos/windows, aarch64-linux/macos)\n");
err(" (also TRACKC_TARGET; default: the host; every pair builds)\n");
return 0;
}

Expand Down Expand Up @@ -130,6 +130,11 @@ const TG_MACOS = 2;
// The host's target: Windows or macOS when trackc itself runs there.
fn host_target() -> i64 {
if text_eq(env_get("OS"), "Windows_NT") {
// an Arm64 trackc.exe sees ARM64; an x64 one, also under Windows
// on ARM's emulation, sees AMD64
if text_eq(env_get("PROCESSOR_ARCHITECTURE"), "ARM64") {
return "aarch64-windows";
}
return "x86_64-windows";
}
let me = arg_text(0);
Expand Down Expand Up @@ -219,21 +224,10 @@ fn check_target_known() -> i64 {
return 0;
}

// Exit unless this trackc can generate code for the target.
// Exit unless this trackc can generate code for the target: every known
// arch-OS pair builds.
fn check_target_buildable() -> i64 {
check_target_known();
let arch = target_arch_name();
let os = target_os_name();
if text_eq(arch, "x86_64") && (text_eq(os, "linux") || text_eq(os, "windows") || text_eq(os, "macos")) {
return 0;
}
if text_eq(arch, "aarch64") && (text_eq(os, "linux") || text_eq(os, "macos")) {
return 0;
}
err(text_cat3("trackc: target '", target_text(), "' is not supported yet\n"));
err(" supported: x86_64-linux, x86_64-windows, x86_64-macos, aarch64-linux, aarch64-macos\n");
quit(2);
return 0;
return check_target_known();
}

fn front_end(path: i64) -> i64 {
Expand Down Expand Up @@ -295,7 +289,7 @@ fn make_dir(dir: i64) -> i64 {
if path_exists(dir) {
return 0;
}
if text_eq(host_target(), "x86_64-windows") {
if text_ends(host_target(), "-windows") {
// cmd.exe: mkdir makes the parents too; it wants backslashes
let sb = sb_new();
let bytes = text_bytes(dir);
Expand Down Expand Up @@ -360,7 +354,21 @@ fn build_native(path: i64, exe: i64) -> i64 {
quit(1);
}
if os == TG_WINDOWS {
if pe_write_file(exe, body, native_code_len(c), native_imports(c), native_import_fixups(c)) != 0 {
let mut wcode = native_code_len(c);
let mut wfix = native_import_fixups(c);
let arm = text_eq(target_arch_name(), "aarch64");
if arm {
// lift to A64; Win64 import calls become Windows ARM64 calls
let wlen = vfill(vnew(), 1, 0);
let afix = vnew();
body = a64_translate_os(body, wcode, wlen, TG_WINDOWS, wfix, afix);
if body == 0 {
quit(1);
}
wcode = vget(wlen, 0);
wfix = afix;
}
if pe_write_file_m(exe, body, wcode, native_imports(c), wfix, arm) != 0 {
err(text_cat3("trackc: cannot write '", exe, "'\n"));
quit(1);
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/ngen.trk
Original file line number Diff line number Diff line change
Expand Up @@ -5920,7 +5920,7 @@ fn ng_extern_of(c: i64, name: i64) -> i64 {
fn ng_extern_call(c: i64, e: i64, args: i64, count: i64) -> i64 {
let b = ncode(c);
if ng(c, NG_TARGET) != 1 && ng(c, NG_TARGET) != 2 {
err(text_cat3("trackc: native: imported function '", name_text(c, na(c, e)), "' needs --target x86_64-windows or x86_64-macos (this target has no import support yet)\n"));
err(text_cat3("trackc: native: imported function '", name_text(c, na(c, e)), "' needs a windows or macos --target (x86_64 or aarch64; Linux output imports nothing)\n"));
quit(1);
}
let mut i: i64 = 0;
Expand Down
56 changes: 55 additions & 1 deletion compiler/src/ngen_a64.trk
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@
// macOS layer makes no system calls: ngen lowers syscall6 to os_syscall),
// passes LC_MAIN's x0/x1 in as rdi/rsi, places the code at the Mach-O
// __text address and pads it to 16 KiB so __DATA starts on a page.
// Windows (os 1, pe.trk) takes the Win64 sequence (ffi_win64_call) to a
// Windows ARM64 call (ax_import_call_win), also traps in the stub, and
// places the code at the PE .text address, padded to 4 KiB for .data.

import "base";
import "x64";
Expand Down Expand Up @@ -74,7 +77,7 @@ const AT_LIVE = 7; // flags of the current instruction are read later
const AT_AR = 8; // mem_addr result: base register
const AT_AO = 9; // mem_addr result: offset
const AT_POS = 10; // x86 offset of the current instruction
const AT_OS = 11; // target OS (NG_TARGET): 0 linux, 2 macos
const AT_OS = 11; // target OS (NG_TARGET): 0 linux, 1 windows, 2 macos
const AT_IMPIN = 12; // x86 import call fixups (code position, import id), ffi.trk
const AT_IMPOUT = 13; // A64 import call fixups (word of the adrp, import id)
const AT_SLOTS = 14;
Expand Down Expand Up @@ -751,6 +754,9 @@ fn ax_import_call(t: i64, d: i64) -> bool {
if id < 0 {
return a64_fail(t, "indirect call that is not an import call");
}
if ax_tg(t, AT_OS) == 1 {
return ax_import_call_win(t, id);
}
a_mov(w, 1, 3, 1);
a_mov(w, 1, 0, 21);
a_mov(w, 1, 1, 20);
Expand All @@ -775,6 +781,46 @@ fn ax_import_call(t: i64, d: i64) -> bool {
return true;
}

// The Windows variant (aarch64-windows): ngen emitted ffi_win64_call, so
// arguments 1-4 are in rcx, rdx, r8, r9 (x1, x2, x22, x23) and 5-10 at
// [rsp + 32 + 8k], above the 32-byte shadow space. The Windows ARM64 ABI
// is AAPCS64 with no shadow space: rcx -> x0, rdx -> x1, r8 -> x2,
// r9 -> x3, Win64 stack words 0-3 -> x4..x7, words 4 and 5 (arguments 9
// and 10, CreateProcessW) -> [sp], [sp + 8]. SP goes 16-aligned below the
// emulated stack; x9 and x10 are kept at [sp + 16], [sp + 24]. x18 (the
// TEB) is never touched: the translator maps no x86 register to it and
// uses only x13..x17 as scratch. Calls of up to ten word arguments.
fn ax_import_call_win(t: i64, id: i64) -> bool {
let w = ax_tw(t);
a_mov(w, 1, 0, 1);
a_mov(w, 1, 1, 2);
a_mov(w, 1, 2, 22);
a_mov(w, 1, 3, 23);
a_ldr(w, 4, 28, 32);
a_ldr(w, 5, 28, 40);
a_ldr(w, 6, 28, 48);
a_ldr(w, 7, 28, 56);
a_ldr(w, 14, 28, 64);
a_ldr(w, AX17, 28, 72);
a_subi(w, 1, AX16, 28, 32);
a_lsr_i(w, 1, AX16, AX16, 4);
a_lsl_i(w, 1, AX16, AX16, 4);
a_mov_sp(w, A_SP, AX16);
a_mem(w, 3, 0, 14, A_SP, 0);
a_mem(w, 3, 0, AX17, A_SP, 8);
a_mem(w, 3, 0, 9, A_SP, 16);
a_mem(w, 3, 0, 10, A_SP, 24);
let adrp = a_adrp(w, AX16, 0);
a_ldr(w, AX16, AX16, 0);
a_blr(w, AX16);
a_ldr(w, 9, A_SP, 16);
a_ldr(w, 10, A_SP, 24);
let out = ax_tg(t, AT_IMPOUT);
vpush(out, adrp);
vpush(out, id);
return true;
}

fn a64_insn(t: i64, d: i64) -> bool {
let w = ax_tw(t);
let op = ax_dg(d, AD_OP);
Expand Down Expand Up @@ -1182,6 +1228,10 @@ fn a64_translate(body: i64, n: i64, out: i64) -> i64 {

// The macOS image's __text address (macho.trk MO_BASE + MO_TEXT_OFF).
const AX_MACHO_TEXT = 4294983680;
// The Windows image's .text address (pe.trk PE_BASE + PE_TEXT_RVA). The
// loader may move the image (ARM64 images are always DYNAMIC_BASE), but by
// 64 KiB steps, so adrp page distances hold.
const AX_PE_TEXT = 5368713216;

// a64_translate for target OS `os`; `impin` holds the x86 import call
// fixups (0: none), `impout` receives the A64 ones.
Expand Down Expand Up @@ -1230,6 +1280,10 @@ fn a64_translate_os(body: i64, n: i64, out: i64, os: i64, impin: i64, impout: i6
a_brk(w, 1); // no system calls on macOS
origin = AX_MACHO_TEXT;
align = 4096; // words: 16 KiB, the __DATA page
} else if os == 1 {
a_brk(w, 1); // nor on Windows (kernel32 through os_syscall)
origin = AX_PE_TEXT;
align = 1024; // words: 4 KiB, the .data page
} else {
a64_sys_stub(w);
}
Expand Down
Loading
Loading