Skip to content

feat: implement static globals, explicit type casting, and conditional compilation#300

Merged
LunaStev merged 2 commits intowavefnd:masterfrom
LunaStev:feat/implement-static-globals-explicit-type-casting-and-conditional-compilation
Mar 4, 2026
Merged

feat: implement static globals, explicit type casting, and conditional compilation#300
LunaStev merged 2 commits intowavefnd:masterfrom
LunaStev:feat/implement-static-globals-explicit-type-casting-and-conditional-compilation

Conversation

@LunaStev
Copy link
Member

@LunaStev LunaStev commented Mar 4, 2026

This PR introduces critical language primitives required for professional systems programming in Wave. Key additions include the as casting operator, global static variables, and a tiered platform abstraction layer using #[target] conditional compilation. These changes are supported by significant backend optimizations and a refactored standard library for better cross-platform portability.


Key Changes

1. Core Language Features

  • Explicit Type Casting (as): Implemented the as operator for explicit type conversions (e.g., value as i64). This is supported both in runtime expressions and during compile-time constant evaluation.
  • Static Globals: Introduced the static keyword to declare global variables with persistent storage. These are correctly initialized in the LLVM data segment.
  • Conditional Compilation (#[target]): Implemented a preprocessor-style attribute system. Using #[target(os="linux")] or #[target(os="macos")], developers can now write platform-specific code blocks within the same codebase.

2. LLVM Backend Optimizations

  • Entry Block Allocas: Refactored the variable allocation strategy to use build_entry_alloca. By moving all local variable allocations to the function's entry block, we ensure better compatibility with LLVM's mem2reg pass, leading to more optimized machine code.
  • Enhanced Constant Evaluation: Added support for integer sign-extension and pointer casts during compile-time evaluation.
  • Cleanup: Removed legacy and unused image-generation logic (compile_ir_to_img_code) to streamline the backend.

3. Standard Library Portability

  • Platform-Agnostic sys Imports: Refactored std::sys so users can import modules via generic paths like std::sys::fs rather than platform-specific paths like std::sys::linux::fs.
  • C Compatibility: Updated errno handling to utilize __errno_location() for more reliable integration with standard C libraries.

4. CLI & Tooling

  • Refined Build Flags:
    • Added -c for compile-only mode (producing object files).
    • Refined -o for specifying custom output paths.
  • Tiered Platform Policy: Updated README.md to document the new support tiers for different operating systems and architectures.

Example Usage

Static Globals, Casting, and Conditional Compilation:

static TOTAL_COUNT: i32 = 0;

#[target(os="linux")]
fun get_platform_name() -> str {
    var linux: str = "Linux";
    return linux;
}

#[target(os="macos")]
fun get_platform_name() -> str {
    var macos: str = "macOS";
    return macos;
}

fun main() {
    var x: i64 = 1000;
    var y: i64 = x as i32;

    println("Running on: {}", get_platform_name());
}

Benefits

  • Portability: The #[target] attribute and agnostic std::sys imports make it significantly easier to maintain cross-platform applications.
  • Performance: Moving allocas to the entry block allows LLVM to perform better register allocation and dead-code elimination.
  • Control: static variables and explicit casting provide the low-level memory and type control necessary for systems-level development.

LunaStev added 2 commits March 4, 2026 16:42
…l compilation

feat(lang): add static globals
feat(lang): implement explicit type casting
feat(lang): add #[target] conditional compilation

refactor(llvm): move allocas to entry block

refactor(std): introduce platform-agnostic sys imports

feat(cli): update wavec build flags

This commit introduces core language features including global static
variables and explicit type casting. It also establishes a tiered platform
support policy and refactors the standard library for better cross-platform
portability.

Changes:
- **Language Features**:
  - **Type Casting**: Added the `as` operator for explicit type conversions
    (e.g., `x as i64`). Supported in expressions and constant evaluation.
  - **Static Globals**: Introduced the `static` keyword for declaring
    global variables with persistent storage.
  - **Conditional Compilation**: Implemented a preprocessor-like pass for
    `#[target(os="...")]` attributes, allowing OS-specific code blocks.
- **LLVM Backend**:
  - Implemented global variable generation for `static` declarations.
  - Refactored `build_entry_alloca` to ensure all local variables are
    allocated in the function entry block, improving optimization compatibility.
  - Added support for constant integer sign-extension and pointer casts
    during compile-time evaluation.
  - Removed legacy image-generation logic (`compile_ir_to_img_code`).
- **Standard Library & IO**:
  - Refactored `std::sys` modules to use platform-agnostic import paths
    (e.g., `std::sys::fs` instead of `std::sys::linux::fs`).
  - Updated `errno` handling to use `__errno_location()` for better
    C compatibility.
- **CLI & Tooling**:
  - Updated `wavec build` syntax: added `-c` for compile-only mode and
    refined `-o` for output path specification.
  - Updated `README.md` to document the new Tiered Platform Policy and
    build instructions.
- **Cleanup**:
  - Applied consistent formatting across the lexer, parser, and utils crates.
  - Improved error diagnostic rendering with better span marking.

This update moves Wave closer to being a production-ready systems language
 by providing necessary primitives for memory management and platform
 abstraction.

Signed-off-by: LunaStev <luna@lunastev.org>
…l compilation

feat(lang): add static globals
feat(lang): implement explicit type casting
feat(lang): add #[target] conditional compilation

refactor(llvm): move allocas to entry block

refactor(std): introduce platform-agnostic sys imports

feat(cli): update wavec build flags

This commit introduces core language features including global static
variables and explicit type casting. It also establishes a tiered platform
support policy and refactors the standard library for better cross-platform
portability.

Changes:
- **Language Features**:
  - **Type Casting**: Added the `as` operator for explicit type conversions
    (e.g., `x as i64`). Supported in expressions and constant evaluation.
  - **Static Globals**: Introduced the `static` keyword for declaring
    global variables with persistent storage.
  - **Conditional Compilation**: Implemented a preprocessor-like pass for
    `#[target(os="...")]` attributes, allowing OS-specific code blocks.
- **LLVM Backend**:
  - Implemented global variable generation for `static` declarations.
  - Refactored `build_entry_alloca` to ensure all local variables are
    allocated in the function entry block, improving optimization compatibility.
  - Added support for constant integer sign-extension and pointer casts
    during compile-time evaluation.
  - Removed legacy image-generation logic (`compile_ir_to_img_code`).
- **Standard Library & IO**:
  - Refactored `std::sys` modules to use platform-agnostic import paths
    (e.g., `std::sys::fs` instead of `std::sys::linux::fs`).
  - Updated `errno` handling to use `__errno_location()` for better
    C compatibility.
- **CLI & Tooling**:
  - Updated `wavec build` syntax: added `-c` for compile-only mode and
    refined `-o` for output path specification.
  - Updated `README.md` to document the new Tiered Platform Policy and
    build instructions.
- **Cleanup**:
  - Applied consistent formatting across the lexer, parser, and utils crates.
  - Improved error diagnostic rendering with better span marking.

This update moves Wave closer to being a production-ready systems language
 by providing necessary primitives for memory management and platform
 abstraction.

Signed-off-by: LunaStev <luna@lunastev.org>
@LunaStev LunaStev merged commit 1b706b6 into wavefnd:master Mar 4, 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