Skip to content

feat: implement static globals, explicit type casting, and pointer arithmetic#298

Merged
LunaStev merged 1 commit intowavefnd:masterfrom
LunaStev:feat/implement-static-globals-type-casts-pointer-arithmetic-and-enhanced-diagnostics
Mar 2, 2026
Merged

feat: implement static globals, explicit type casting, and pointer arithmetic#298
LunaStev merged 1 commit intowavefnd:masterfrom
LunaStev:feat/implement-static-globals-type-casts-pointer-arithmetic-and-enhanced-diagnostics

Conversation

@LunaStev
Copy link
Member

@LunaStev LunaStev commented Mar 2, 2026

This PR introduces a significant set of core language features and architectural improvements that bridge the gap between high-level safety and low-level systems programming. Key updates include the addition of static globals, the as casting operator, robust pointer arithmetic, and a "panic-guarded" diagnostic system that maps backend failures back to the Wave source code.


Key Changes

1. Core Language Features

  • Static Globals: Introduced the static keyword for global variables. These persist for the lifetime of the program and are correctly initialized in the data segment.
  • Explicit Type Casting (as): Implemented the as operator (e.g., value as i64) for explicit type conversions. The backend handles these via LLVM trunc, zext/sext, fpext/fptrunc, and bitcast instructions.
  • Pointer Arithmetic: Wave now supports industry-standard pointer operations:
    • ptr + int / ptr - int: For navigating memory buffers.
    • ptr - ptr: To calculate the distance (offset) between two memory addresses.
  • Match Statement Finalization: Finalized the match expression syntax and added a verification pass to prevent duplicate patterns in match arms.

2. Compiler Infrastructure & Diagnostics

  • Panic-Guarded Diagnostics: Introduced a robust runner that captures backend panics. Using "source-span inference," the compiler can now map low-level LLVM errors or backend crashes back to the specific line and column in the original Wave source code.
  • Structured ParseError: Refactored the frontend to return detailed error objects. These include "expected vs. found" token information, multi-line context, and actionable "help" hints.
  • Cross-Platform ABI Support: Overhauled the abi_c.rs module to support target-specific calling conventions for both Linux x86_64 (System V) and macOS arm64 (Darwin). This includes correct handling of aggregate splitting and SRet (Structured Return) rules for M1/M2/M3 chips.

3. Standard Library & Safety

  • Linux Syscall Layer: Updated the std/sys/linux modules (FS, Memory, Socket, Process) to utilize explicit as i64 casts, complying with the compiler's stricter type-checking rules for system registers.
  • Allocation Safety: Improved memory management primitives with mandatory null checks and better syscall error propagation.

4. Documentation & Tooling

  • Documentation: Expanded the README.md with comprehensive build instructions, an updated target support matrix, and a detailed guide for the new CLI subcommands.

Example Usage

static COUNTER: i32 = 1;
static VGA_BUFFER: ptr<char> = 0xb8000 as ptr<char>;
const BASE: i32 = 41;

fun main() -> i32 {
    COUNTER = COUNTER + 1;
    if (COUNTER != 2) {
        return 1;
    }

    var addr: i64 = VGA_BUFFER as i64;
    if (addr != 0xb8000) {
        return 2;
    }

    if (BASE != 41) {
        return 3;
    }

    return 0;
}

Benefits

  • Portability: The addition of macOS arm64 support allows Wave to target modern Apple Silicon hardware with ABI-compliant C FFI.
  • Safety: Stricter type checks and explicit casting prevent subtle bugs related to implicit integer promotion or narrowing.
  • Developer Experience: The panic-guarded diagnostic system ensures that even if the compiler's backend fails, the developer receives helpful feedback in the context of their code.

…nhanced diagnostics

This commit introduces several core language features, including global
static variables, explicit type casting, and pointer arithmetic. It also
significantly upgrades the compiler's resilience by introducing a
panic-guarded diagnostic system and cross-platform ABI support.

Changes:
- **Language Features**:
  - **Static Globals**: Added the `static` keyword for global variables
    that persist throughout the program.
  - **Type Casting**: Implemented the `as` operator (e.g., `expr as type`)
    for explicit type conversions, supported in both the parser and
    LLVM backend.
  - **Pointer Arithmetic**: Added support for `ptr + int`, `ptr - int`,
    and `ptr - ptr` (pointer difference) using LLVM `gep` and `sub`
    instructions.
  - **Match Statement**: Finalized the `match` expression syntax and
    verification, ensuring no duplicate patterns in arms.
- **Compiler Infrastructure**:
  - **Panic-Guarded Diagnostics**: Introduced a robust runner that
    captures backend panics and uses "source-span inference" to map
    low-level LLVM errors back to the original Wave source code.
  - **Cross-ABI Support**: Overhauled `abi_c.rs` to support target-specific
    calling conventions for both **Linux x86_64 (SysV)** and
    **macOS arm64 (Darwin)**, handling aggregate splitting and SRet rules.
  - **Structured Errors**: Refactored the parser to return `ParseError`
    objects containing detailed diagnostics (expected/found tokens,
    help messages, and context).
- **Standard Library Updates**:
  - Updated the Linux syscall layer (`fs`, `memory`, `socket`, `process`)
    with explicit `as i64` casts to comply with stricter type-checking
    rules for register arguments.
  - Improved memory allocation safety with `null` checks and syscall
    error handling.
- **Documentation**:
  - Expanded `README.md` with build instructions, target support status,
    and a comprehensive CLI usage guide.

These updates bridge the gap between low-level system access and
high-level safety, making Wave more suitable for systems programming
on multiple architectures.

Signed-off-by: LunaStev <luna@lunastev.org>
@LunaStev LunaStev self-assigned this Mar 2, 2026
@LunaStev LunaStev merged commit f8a8fa5 into wavefnd:master Mar 2, 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