feat: enhance C ABI compatibility, type inference, and JSON serialization#293
Merged
LunaStev merged 1 commit intowavefnd:masterfrom Feb 22, 2026
Conversation
…tion
This commit improves the robustness of the LLVM backend regarding type
resolution under opaque pointers, refines C ABI lowering for aggregates,
and introduces JSON serialization utilities.
Changes:
- **LLVM Codegen & ABI**:
- **C ABI Refinement**: Updated `abi_c.rs` to pass small "mixed"
aggregates as direct values, allowing LLVM to handle register
assignment (INTEGER vs. SSE) more effectively for the C calling
convention.
- **Implicit Main Return**: Implemented implicit `return 0` for the
`main` function. If `main` is defined as void-like in Wave, it is
now lowered to an `i32` return in LLVM to satisfy standard
execution environments.
- **Symbol Resolution**: Fixed a bug in `gen_function_call` where
extern C functions were being looked up by their Wave name instead
of their redirected LLVM symbol name.
- **Address Generation**: Introduced `generate_address_and_type_ir`
to provide both the memory address and its corresponding LLVM type,
simplifying indexing and field access logic.
- **Type System & Pointers**:
- **Enhanced Inference**: Implemented `infer_wave_type_of_expr` and
`basic_ty_to_wave_ty` to improve Wave-to-LLVM type mapping,
especially for complex lvalues.
- **Legacy Compatibility**: Updated `deref` handling to allow redundant
dereferences on already-addressable lvalues (e.g., `deref array[i]`),
maintaining compatibility with existing code.
- **Parser & Verification**:
- **ABI Validation**: Added a verification pass to ensure only
`extern(c)` is used, as other ABIs are not yet supported.
- **JSON Utilities**:
- Added `write_pretty_to` and `write_compact_to` to the `Json` enum.
- Implemented full JSON string escaping (handles newlines, quotes,
slashes, and control characters).
- **CLI & Flags**:
- Added support for `-Os` (optimize for size) and `-Ofast`
(aggressive optimization) flags in the CLI and LLVM pass builder.
These updates stabilize the transition to opaque pointers and provide
a more predictable interface for system-level FFI and serialization.
Signed-off-by: LunaStev <luna@lunastev.org>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR focuses on stabilizing the compiler's backend following the LLVM 21 (Opaque Pointer) migration. It introduces more robust type inference mechanisms, refines the C ABI lowering for complex aggregates, and adds utility functions for JSON serialization.
Key Changes
1. LLVM Backend & C ABI Refinements
abi_c.rs) to pass small "mixed" aggregates (structs with mixed types) as direct values. This allows the LLVM backend to more effectively assign values to either INTEGER or SSE registers according to the System V ABI.mainReturn: Implemented standard-compliant behavior for the entry point. If themainfunction is defined without a return type in Wave, the compiler now automatically lowers it to ani32return type in LLVM and inserts areturn 0at the end.generate_address_and_type_ir. This is a critical utility for LLVM 21, as it provides both the memory address and its explicit type simultaneously, simplifying the logic for indexing and field access.2. Advanced Type Inference
infer_wave_type_of_exprandbasic_ty_to_wave_ty. These helpers allow the backend to reconstruct Wave-level type information from LLVM basic types and AST nodes, which is essential now that pointers (ptr) are ambiguous in LLVM.derefoperations on already-addressable L-values. This ensures that legacy code and complex pointer arithmetic remain compatible with the stricter modular structure.3. JSON Serialization Utilities
write_pretty_to(indented) andwrite_compact_tomethods to theJsonutility enum.4. CLI & Optimization Flags
-Os(optimize for size) and-Ofast(aggressive/unsafe optimizations) in both the CLI argument parser and the LLVM PassBuilder.5. Verification Pass
extern(c)is used in declarations, as Wave currently focuses solely on C ABI compatibility for foreign imports.Benefits
mainreturns and better JSON support make the language and its toolchain easier to work with.