Close

Type System Progress: Metadata is now baked into Z80 opcodes!

A project log for XiAleste

XiAleste Next is an 8-bit home computer, which is compatible with software for the Amstrad CPC6128 released on 13 June 1985

h2wh2w 02/06/2026 at 19:070 Comments

Great progress on the compiler! I’ve finally bridged the gap between high-level type definitions and the Z80 assembler. The compiler now handles all offset and size calculations "under the hood," injecting pure constants directly into the generated instructions.

The core benefit:

No more manual byte-counting or maintaining a mess of X_OFFSET = 2 constants. You define the structure once, and from then on, you simply refer to fields by name directly within zasm blocks.

Example implementation:

We define a type and immediately use its metadata to generate assembly:

(deftype vector (structure)  
  ((x int16)   ;; offset 0   
   (y int16)   ;; offset 2   
   (z int16)))) ;; offset 4

(zasm
$begin   
  (ld @hl (size-of vector))           ;; Get total structure size   
  (ld @hl (offset-of vector x))       ;; Get offset of field 'x'   
  (ld @hl (offset-of vector y))       ;; Get offset of field 'y'   
  (ld @hl (size-of vector x))         ;; Get size of the type used by field 'x' (int16)   
  (ld [+ iy (offset-of vector y)] 10) ;; Direct write to object field via IY
)

Compilation Result (Binary Dump):

8000 | begin:           | 8000 | 21 06 00         | ld hl, 06h      ; Total size: 3 fields * 2 bytes
8003 | 21 00 00         | ld hl, 00h      ; 'x' at offset 0
8006 | 21 02 00         | ld hl, 02h      ; 'y' at offset 2
8009 | 21 02 00         | ld hl, 02h      ; size of int16 = 2
800C | 21 02 00         | ld hl, 02h      800F | FD 36 02 0A      | ld (iy+2), 0Ah  ; Indexed write (IY+2) with value 10 (0Ah)

Why this matters:

  1. Refactoring Resilience: If I change a field from int16 to int32, the compiler automatically updates every offset across the entire project.
  2. Native IY/IX Support: Support for [+ iy displacement] syntax allows us to leverage the Z80’s indexed addressing mode as natively as possible.
  3. Readability: The code feels like a modern systems language, but the output is honest, tight assembly.

Next steps:

Implementing instances (memory allocation for objects) and beginning the port of existing code to this new workflow. Finally, I can focus on logic without turning my brain into an offset calculator!

Discussions