Skip to content

Build-Time Keywords (arch, os) and Platform-Specific Branching in Wave #211

@LunaStev

Description

@LunaStev

Background

  • Wave supports inline assembly, so platform-specific branching (architecture & OS) is required.
  • Existing approaches have drawbacks:
    • C #ifdef → macro hell, poor readability
    • Rust #[cfg(...)] → verbose and cluttered syntax
  • Wave needs a cleaner and more intuitive way to handle platform-dependent code.

Goals

  1. Use natural if-statement-like syntax
  2. Evaluate at build-time only → zero runtime overhead
  3. No compile-time slowdown
  4. Avoid confusion or conflicts with regular variables
  5. Must remain compatible when transitioning to the Whale toolchain

Final Decision

  • Introduce arch and os as build-time reserved keywords
  • Cannot use arch or os as regular variable names
  • Values are determined by build target (vex build --arch amd64 --os linux)
  • False branches are removed at the AST stage → not included in final binary

Final Syntax

fun syscall_alloc(size: i32) {
    if arch == "amd64" && os == "linux" {
        asm { "mov rax, 9" }
    } else if arch == "arm64" {
        asm { "mov x8, 222" }
    } else {
        println("unsupported platform");
    }
}

✅ Looks like a normal if statement
✅ Zero runtime overhead
✅ Parser instantly recognizes keywords → fast compile-time
✅ Will remain compatible with Whale toolchain


Build Flow

  1. vex build --arch amd64 --os linux
  2. Wave compiler sets arch="amd64", os="linux" as compile-time constants
  3. During AST generation, false branches are removed
  4. IR → final binary contains only the selected code

Future Keyword Extensions

  • Initial set: arch, os
  • Potential future: endian, cpu

Why this decision?

  • Regular developers rarely need arch/os as variable names
  • Allowing it as a variable would increase parser complexity → potential compile-time slowdown
  • Keeping them as reserved keywords ensures simpler syntax and better performance

Relation to Whale

  • Whale will use the same build target parameters as LLVM → same behavior
  • After migrating to Whale, AST optimizations can be even more aggressive

Conclusion:

Wave will use arch and os as build-time reserved keywords for platform-specific branching.
This approach ensures simplicity, performance, and intuitiveness, while remaining compatible with the future Whale toolchain.

Metadata

Metadata

Assignees

No one assigned

    Labels

    buildIssues related to building the project or compiling errors.enhancementA request for a new feature or improvement.feature requestA request for a new feature or functionality.os patchA patch required to support or improve compatibility with an operating system.

    Type

    No type

    Projects

    Status

    Backlog

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions