ludic/selfhost/emit_intrin2.ludic
Orkuncakilkaya effb3f637f refactor(lang): rename the ptr/ptrs types to pointer/pointers
Expand the abbreviated pointer types to full words on the language surface:
  ptr   ->  pointer     (a raw address / FFI handle)
  ptrs  ->  pointers    (a buffer of pointers)

The Ludic type name is distinct from LLVM's own `ptr` spelling: llty() maps
`pointer`/`pointers` to LLVM `ptr`, and the emitted IR keeps `ptr`, so only
the Ludic-level surface changes. Rewrites type annotations across all
sources, the 8 hardcoded pointer type-tags, the `pointers`-buffer indexing
in emit_addr, the grammars/LSP/JetBrains tokens, and the docs
(type-ptr -> type-pointer, type-ptrs -> type-pointers). int/bool keep their
conventional short spelling (like Math).

Reseeded; C-free fixpoint holds; all suites green (45/24/29); site + check.py OK.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-08-30 01:58:54 +03:00

53 lines
2.9 KiB
Text

# emit_intrin2.ludic — the rest of the low-level intrinsics: raw memory, fixed
# memory access, pointer arithmetic, stdio, time, and the windowing hooks.
# Matches compiler/back/ir_intrin.c. Reached from emit_intrinsic's fall-through.
var g_windowed: bool = false # headless by default (games read stdin / dump PPM)
function is_intrinsic2(name: pointer) -> bool {
if (name == "free") or (name == "fill") { return true }
if (name == "offset") or (name == "read_char") { return true }
if (name == "as_fixed") or (name == "as_int") { return true }
if (name == "is_windowed") or (name == "game_title") { return true }
if (name == "win_open") or (name == "win_poll") or (name == "win_present") { return true }
if (name == "win_running") or (name == "win_close") { return true }
return false
}
function emit_intrinsic2(name: pointer, e: Node) -> Val {
if (name == "free") {
let p = arg_code(e, 0); emit(" call void @free(ptr "); emit(p); emit(")\n"); return val("0", "void")
}
if (name == "fill") {
let p = arg_code(e, 0); let v = arg_code(e, 1); let n = arg_code(e, 2)
let w = emit_bind(`zext i32 {n} to i64`)
emit(" call ptr @memset(ptr "); emit(p); emit(", i32 "); emit(v); emit(", i64 "); emit(w); emit(")\n")
return val("0", "void")
}
if (name == "offset") {
let p = arg_code(e, 0); let n = arg_code(e, 1)
let g = nreg()
emit(" "); emit(g); emit(" = getelementptr inbounds i8, ptr "); emit(p); emit(", i32 "); emit(n); emit("\n")
return val(g, "pointer")
}
if (name == "read_char") { return val(emit_bind("call i32 @getchar()"), "int") }
if (name == "as_fixed") { let a = emit_expr(e.kids[0]); return val(a.code, "fixed") }
if (name == "as_int") { let a = emit_expr(e.kids[0]); return val(a.code, "int") }
if (name == "is_windowed") { if g_windowed { return val("1", "bool") }; return val("0", "bool") }
if (name == "game_title") { return val("@.gametitle", "string") }
# windowing hooks — declared external; only reached on the is_windowed() branch
if (name == "win_poll") { return val(emit_bind("call i32 @win_poll()"), "int") }
if (name == "win_running") { return val(emit_bind("call i32 @win_running()"), "bool") }
if (name == "win_open") {
let a = arg_code(e, 0); let b = arg_code(e, 1); let c = arg_code(e, 2); let d = arg_code(e, 3)
emit(" call void @win_open(i32 "); emit(a); emit(", i32 "); emit(b); emit(", i32 "); emit(c); emit(", ptr "); emit(d); emit(")\n")
return val("0", "void")
}
if (name == "win_present") {
let a = arg_code(e, 0); let b = arg_code(e, 1); let c = arg_code(e, 2)
emit(" call void @win_present(ptr "); emit(a); emit(", i32 "); emit(b); emit(", i32 "); emit(c); emit(")\n")
return val("0", "void")
}
if (name == "win_close") { emit(" call void @win_close()\n"); return val("0", "void") }
return val("0", "void")
}