ludic/selfhost/emit_addr.ludic
Orkuncakilkaya 5a9e8cb2da Phase 8a: finish typed indexing — fixeds/ptrs buffers (retire peekf/pokef/peekp/pokep/str_len)
The last peek/poke pairs were the same "typed buffer access wearing a C name" as
peek8/peek32, so they become indexing too:

  peekf(sc_x, i)     -> sc_x[i]        (a `fixeds` buffer -> a fixed)
  pokep(gc_bmp, s,b) -> gc_bmp[s] = b  (a `ptrs` buffer -> a pointer)
  str_len(s)         -> len(s)         (len is polymorphic since 7f; str_len was dead)

Two new element-typed buffers join words: `fixeds` (32-bit fixed) and `ptrs`
(pointer, 8-byte stride). emit_index_addr dispatches on the base type; IR is
byte-identical to the old intrinsics.

The peekf/peekp buffers (ed_x0/ed_x1/ol_x/sc_x fixed coords; gc_bmp/img_px/
tt_data/ui_text pointer arrays) were retyped scope-aware, then the 33 sites
migrated to indexing. Two bugs found via a menu golden diff / a link-time type
error and fixed: the buffer-name regex truncated digit suffixes (ed_x0 -> ed_x),
and the char-literal brace-miscount skipped a few module declarations (same class
as 7j).

Reseeded (22371 lines); C-free fixpoint holds; goldens byte-identical; 18/18;
vocab (fixeds/ptrs types in, 5 intrinsics out) + doc-fences clean.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-08-28 02:49:40 +03:00

58 lines
2.5 KiB
Text

# emit_addr.ludic — addresses of lvalues (struct fields and slice elements),
# shared by expression loads and assignment stores. Each returns the address
# register; the element/field type is written into g_addr_ty.
var g_addr_ty: ptr # out-param: the type at the computed address
# address of `base.field`
fn emit_member_addr(e: Node) -> ptr {
let base = emit_expr(e.a)
let s = layout_node(base.ty)
if (s == null) { perr(`member access on non-aggregate {base.ty}`) }
let fidx = field_index(s, e.s)
if fidx < 0 { perr(`no such field {e.s}`) }
g_addr_ty = field_type(s, e.s)
let r = nreg()
emit(" "); emit(r); emit(" = getelementptr inbounds "); emit(layout_ty(base.ty))
emit(", ptr "); emit(base.code); emit(", i32 0, i32 "); emit(itoa(fidx)); emit("\n")
return r
}
# address of `base[index]` (slices only in this subset)
fn emit_index_addr(e: Node) -> ptr {
let base = emit_expr(e.a)
if not is_slice_ty(base.ty) { # a raw pointer: address of element i
let bi = emit_expr(e.b) # (evaluate index first — it may set g_addr_ty)
if (base.ty == "words") { # a `words` buffer: 32-bit int elements
let rw = emit_bind(`getelementptr inbounds i32, ptr {base.code}, i32 {bi.code}`)
g_addr_ty = "int"
return rw
}
if (base.ty == "fixeds") { # a `fixeds` buffer: 32-bit fixed elements
let rf = emit_bind(`getelementptr inbounds i32, ptr {base.code}, i32 {bi.code}`)
g_addr_ty = "fixed"
return rf
}
if (base.ty == "ptrs") { # a `ptrs` buffer: pointer elements
let rp = emit_bind(`getelementptr inbounds ptr, ptr {base.code}, i32 {bi.code}`)
g_addr_ty = "ptr"
return rp
}
let r = emit_bind(`getelementptr inbounds i8, ptr {base.code}, i32 {bi.code}`) # a ptr/str: bytes
g_addr_ty = "byte" # set last so the caller sees it
return r
}
let el = slice_elem(base.ty)
g_addr_ty = el
# load the data pointer from the slice header (field 0)
let dp = nreg()
emit(" "); emit(dp); emit(" = getelementptr inbounds %LSlice, ptr ")
emit(base.code); emit(", i32 0, i32 0\n")
let data = nreg()
emit(" "); emit(data); emit(" = load ptr, ptr "); emit(dp); emit("\n")
let ix = emit_expr(e.b)
let r = nreg()
emit(" "); emit(r); emit(" = getelementptr inbounds "); emit(llty(el))
emit(", ptr "); emit(data); emit(", i32 "); emit(ix.code); emit("\n")
return r
}