Skip to content

Conversation

@LunaStev
Copy link
Member

@LunaStev LunaStev commented Jan 6, 2026

This PR enhances the inline assembly (asm) block functionality and strengthens the compiler's type system. By supporting generic expressions as assembly operands and implementing a robust type coercion layer, Wave now allows for more expressive low-level programming (such as syscall wrappers) while maintaining higher safety standards during code generation.

Key Changes

1. Advanced Inline ASM Parsing

  • Expression Support: asm blocks now accept generic Expression variants for in and out clauses. This allows for literals (decimal, hex, binary), variable references, address-of (&x), and dereferences (deref x).
  • Operand Management:
    • Introduced parse_asm_operand and parse_asm_inout_clause for cleaner, more maintainable parsing logic.
    • Implemented is_assignable checks to ensure that out operands are valid L-values (memory locations that can be written to).

2. LLVM Codegen & Type Coercion

  • Multi-Output Support: Refactored gen_asm_stmt_ir to support multiple assembly outputs by utilizing LLVM struct return types.
  • Operand Translation: asm_operand_to_value now converts AST expressions to LLVM values while preserving radix information (hex/binary) for literals.
  • Coercion Engine:
    • Implemented coerce_basic_value and coerce_to_expected to handle implicit widening (e.g., i32 to i64) and explicit pointer casting.
    • This is particularly useful for syscalls where integers must often be treated as pointers and vice versa.
  • Output Storage: store_asm_output correctly writes assembly results back to Wave variables, handling necessary pointer-bitcasts.

3. Type System & Error Reporting

  • Radix Preservation: The lexer now produces an IntLiteral token that stores the raw string representation. This allows the backend to correctly interpret different numeric bases.
  • UI/UX: Integrated the colorex crate into front/error to provide helpful, color-coded error messages in the terminal.
  • Initialization Safety: gen_variable_ir now utilizes the coercion engine to ensure initializers are safely promoted to the variable's declared type.

4. Testing & Infrastructure

  • Syscall Verification: Updated test56.wave to demonstrate new assembly features via type-safe syscall wrappers (syscall4i, syscall4p).
  • Stability:
    • Fixed array sizing in test66.wave.
    • Added explicit type annotations to overflow tests in test69.wave.
    • Updated the test runner to mark test56.wave as a known timeout (due to its server-like nature).

Benefits

  • Versatility: Inline assembly is now powerful enough to write sophisticated system-level abstractions.
  • Safety: Stricter type checking and L-value validation catch errors before they reach the LLVM optimizer.
  • Clarity: Radix preservation and colored errors improve the developer experience.

Enhance inline assembly block parsing and type handling for greater
robustness and expressiveness.

Changes:
- **ASM Parsing**:
  - `parse_asm_block` now supports generic `Expression` for inputs/outputs,
    allowing variable references, literals (decimal, hex, binary),
    pointers (`&x`), and dereferences (`deref x`).
  - Added `parse_asm_operand` to handle various operand types.
  - Implemented `parse_asm_inout_clause` for cleaner `in`/`out` parsing.
  - Enforced `is_assignable` check for `out` operands.
- **LLVM Codegen**:
  - `gen_asm_stmt_ir` refactored to support multiple outputs via struct
    return values.
  - `asm_operand_to_value` converts AST expressions to LLVM values,
    handling literals (with correct radix) and address-of operations.
  - `store_asm_output` handles writing assembly results back to variables,
    including pointer casts.
  - Added `coerce_basic_value` for explicit type conversions (e.g., int to
    pointer for syscalls) and implicit widening.
- **Type Safety**:
  - Implemented `coerce_to_expected` in function calls to automatically
    widen integers (e.g., `i32` -> `i64`) and cast pointers where safe.
  - `gen_variable_ir` now uses `coerce_basic_value` for initializers.
- **Tests**:
  - Updated `test56.wave` (syscalls) to use new asm features and type-safe wrappers (`syscall4i`, `syscall4p`).
  - Fixed array size in `test66.wave`.
  - Added explicit type for overflow test in `test69.wave`.
  - Marked `test56.wave` as a known timeout in test runner (server test).
- **Error Reporting**:
  - Integrated `colorex` crate for colored error messages in `front/error`.
  - Lexer now produces `IntLiteral` token type, storing the raw string
    representation for later parsing (preserving radix info).

This update makes inline assembly more versatile and integrates it better
with the type system.

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