DEV Community

COMMENTERTHE9
COMMENTERTHE9

Posted on • Originally published at cx-lang.com

Cx Dev Log — 2026-05-01

Two sub-packets landed on submain today, moving the IR backend closer to supporting structs properly. The first package upgrades the instruction set to handle memory operations, and the second implements a struct registry integrated into the lowering pass. Together, these changes allow the lowering pass to recognize and manipulate the structs' memory representations, setting the stage for future struct-aware code generation. Meanwhile, main remains unchanged, while submain is breaking ground with 22 new commits developed over 34 days.

IR Memory Operations

Commit 6252994 brought in a crucial update: introducing a Ptr type to the IR type system, complete with a size of 8 bytes and an alignment of 8 bytes. Alongside, three fundamental IR instructions were added:

  • Alloca: It allocates stack space defined by size and alignment.
  • Load: It performs a read operation from a pointer with a specific type.
  • Store: It enables writing a value through a pointer.

The IR validator has adapted to these changes, embedding 43 lines of new checks. Interestingly, it requires Alloca to have a size greater than zero and a power-of-two alignment. Both Load and Store demand a pointer operand typed explicitly as IrType::Ptr.

This memory operation extension is crucial. Without it, the backend couldn’t handle memory structures that exceed single-register values. The improvement reflects typical infrastructure you’d find in a lowering pass designed for IR architectures like Cranelift or LLVM, where memory management is cornerstone.

Struct Registry and Struct-to-Ptr Lowering

Diving further into struct support, commit c95eea6 revealed a robust struct registry. This involves build_struct_table() walking through every SemanticStmt::StructDef. As it encounters each, it lowers field types and computes their layout via compute_struct_layout(), established back in Phase 8.

The produced StructLayoutInfo details field offsets, sizes, and alignments for each struct, threading this struct metadata through key components like LoweringCtx and the lowering functions. The main change here is how lower_type operates, shifting from returning UnsupportedSemanticType for structs to correctly mapping SemanticType::Struct(_) to Ok(IrType::Ptr).

LLVM has a similar stance on treating structs as pointers, which supports computational efficiency. Importantly, the struct layout calculation is performed just once, and then utilized across functions, preventing redundant computations.

The Submain Divergence

Submain’s trajectory reflects a 34-day, 22-commit lead over main, carrying a slew of updates from Phases 10-11 including error management, overflow controls, and optional semicolons. This branch also holds 117 tests against main’s 78.

Concerns rise as days pass without merging these advancements back into main. The longer this continues, the more challenging integration will become, posing significant risks to the overall project. There’s an urgent need to close this divergence to streamline development and mitigate integration complexities.

What Landed vs. What Was Predicted

Interestingly, the predicted advancement in Phase 11's expression lowering occurred, albeit through focus on underlying struct and memory operation enhancement rather than direct expression work. However, several anticipated actions did not see progression — the crucial merge into main remained unaddressed, as did tackling existing IR backend test failures or resolving inactive PRs.

What's Next

Struct support is now foundational. Following this, we should:

  • Lower struct field accesses. Implement DotAccess lowering utilizing StructLayoutInfo for accurate field handling through pointer arithmetic accompanied by Load and Store.
  • Lower struct literals. Translate SemanticExprKind::StructLit into an Alloca followed by Store operations for each field based on layout offsets.
  • Merge submain to main. This remains the top priority with 22 new commits and 39 tests awaiting integration.

The groundwork is laid for more sophisticated struct handling in Cx. The path forward involves both immediate function implementations and resolving the pressing main-submain merge to unify the project’s progress.


Follow the Cx language project:

Originally published at https://cx-lang.com/blog/2026-05-01

Top comments (0)