Skip to content

feat: implement advanced cross-compilation support and backend customization#303

Merged
LunaStev merged 1 commit intowavefnd:masterfrom
LunaStev:feat/implement-advanced-cross-compilation-support-and-backend-customization
Mar 7, 2026
Merged

feat: implement advanced cross-compilation support and backend customization#303
LunaStev merged 1 commit intowavefnd:masterfrom
LunaStev:feat/implement-advanced-cross-compilation-support-and-backend-customization

Conversation

@LunaStev
Copy link
Member

@LunaStev LunaStev commented Mar 7, 2026

This PR transforms the Wave compiler into a truly portable toolchain by introducing a robust cross-compilation framework and fine-grained backend control. Key features include expanded architecture support for ARM64 and x86_64, a comprehensive CLI for LLVM and linker customization, and a modernized preprocessor for platform-specific code.


Key Changes

1. Advanced Cross-Compilation Framework

  • Parameterized Backend: Introduced BackendOptions to manage the target triple, CPU, features, ABI, sysroot, and linker settings.
  • Global Target Initialization: Updated the backend to initialize all supported LLVM targets, enabling any host to compile for any supported target.
  • ABI support: Refactored abi_c.rs to support the standard System V ABI for both x86_64 and ARM64 across Linux and macOS.

2. Architecture-Specific Inline Assembly

  • ARM64 Mapping: Implemented full register group mapping for ARM64 (e.g., x0 through x30, sp) in the assembly planner.
  • Dialect Support: The compiler now automatically selects the appropriate assembly dialect (Intel for x86, AT&T/Standard for ARM) based on the target architecture.
  • Clobber Optimization: Refined the automatic clobber logic to avoid trashing general-purpose registers during empty or barrier-style assembly blocks.

3. Granular CLI Interface

  • LLVM Control: Added the --llvm subcommand to expose low-level backend options: --target, --cpu, --features, --abi, and --sysroot.
  • Linker Customization: Introduced -C flags for advanced linking: linker=<path>, link-arg=<arg>, and no-default-libs.
  • Extensibility: Reserved the --whale flag for future alternative backend integrations.

4. Robust #[target] Preprocessing

  • Awareness Scanner: Significantly improved the #[target] preprocessor. It is now fully aware of block comments, string/char literals, and nested braces, ensuring that platform-specific code blocks are parsed accurately without premature truncation.

5. Infrastructure & Policy

  • Tiered Platform Policy: Formally established a Tier 1–4 support matrix in README.md to set clear expectations for target stability.
  • Build Orchestration: Updated x.py to include aarch64-unknown-linux-gnu and improved build artifact hygiene by streamlining the object file placement during the linking phase.

Example Usage

Cross-compiling for ARM64 Linux from an x86 host:

wavec build main.wave \
    --llvm-target aarch64-unknown-linux-gnu \
    -C linker=aarch64-linux-gnu-gcc

Using the improved preprocessor:

#[target(os="linux")]
fun init() {
    /* Nested braces { and strings "}" no longer break the preprocessor */
    println("Initializing Linux...");
}

Benefits

  • Portability: Allows developers to target embedded systems, ARM-based servers, and Apple Silicon hardware from a single development environment.
  • Precision: Fine-grained CLI control over CPU features and sysroots enables the creation of highly optimized and specialized binaries.
  • Robustness: The enhanced preprocessor eliminates subtle bugs when managing large, platform-specific codebases.

…ization

This commit introduces a robust framework for cross-compilation and
fine-grained backend control. It expands architecture support for ARM64
and x86_64 across Linux and Darwin, while providing a comprehensive CLI
interface for LLVM backend options.

Changes:
- **LLVM Backend & Cross-Compilation**:
  - Introduced `BackendOptions` to parameterize target triple, CPU,
    features, ABI, sysroot, and linker settings.
  - Updated `generate_ir` to initialize all LLVM targets and use
    provided backend options during target machine creation.
  - Refactored `abi_c.rs` to support System V ABI for both x86_64 and
    Arm64 on Linux and macOS.
- **Enhanced Inline Assembly**:
  - Implemented Arm64 register group mapping (`x0`-`x30`, `sp`, etc.)
    in `plan.rs`.
  - Added support for target-specific inline assembly dialects
    (Intel for x86, AT&T for ARM).
  - Refined automatic clobber logic to avoid trashing GPRs in empty
    barrier-like assembly blocks.
- **Advanced CLI Interface**:
  - Added `--llvm` subcommand flags to control the backend: `--target`,
    `--cpu`, `--features`, `--abi`, and `--sysroot`.
  - Added `-C` flags for linker customization: `linker=<path>`,
    `link-arg=<arg>`, and `no-default-libs`.
  - Reserved the `--whale` flag for future alternative backend support.
- **Parser & Preprocessor**:
  - Significantly improved `#[target(os="...")]` preprocessing logic.
    The new scanner is aware of block comments, strings, char literals,
    and nested braces, preventing premature truncation of target blocks.
- **Infrastructure & Documentation**:
  - Formalized a **Tiered Platform Policy** (Tier 1-4) in `README.md`
    to set support expectations.
  - Updated `x.py` build script to include `aarch64-unknown-linux-gnu`.
  - Cleaned up build artifacts by moving object files to the root
    temporary directory before linking.

These updates transform Wave into a portable toolchain capable of
targeting multiple architectures and operating systems from a single host.

Signed-off-by: LunaStev <luna@lunastev.org>
@LunaStev LunaStev merged commit 91f79a8 into wavefnd:master Mar 7, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant