Strings are values now: `a + b` concatenates and `a == b` / `a != b` compare by
content, replacing the 605 `sconcat(...)` / `streq(...)` calls that made the
compiler read like C.
streq(name, "let") -> name == "let"
sconcat("load ", reg) -> "load " + reg
sconcat(a, sconcat(b, c)) -> a + b + c
Implementation: emit_bin gains a string path. Strings are pointer-typed, so any
`+` with a pointer operand concatenates and `==`/`!=` between pointers compares
content — except when one side is the `null` literal, which stays a pointer
identity test (the only two kinds of pointer `==` in the codebase). Both call a
small hand-written IR prelude, @fn_str_eq / @fn_str_concat, emitted once into any
program that uses string ops (so it works for tools, games and the compiler with
no runtime-splice dependency and no duplicate symbols).
Delivered as two reseeds: (A) add the operators + prelude with the full
pointer-aware dispatch, keeping streq/sconcat; (B) migrate every call site
(354 lines, via a string-literal-safe balanced-paren script that leaves the
function definitions alone) and delete streq/sconcat. examples/strings.ludic +
a test.sh smoke (prints 1 2 3 4 5) guard it.
Reseeded (21890 lines); C-free fixpoint holds; goldens byte-identical; 18/18;
vocab + doc-fences clean.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
95 lines
5 KiB
Text
95 lines
5 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)
|
|
|
|
fn is_intrinsic2(name: ptr) -> bool {
|
|
if (name == "mem_free") or (name == "mem_copy") or (name == "mem_set") { return true }
|
|
if (name == "ptr_add") or (name == "read_byte") or (name == "write_byte") { return true }
|
|
if (name == "str_len") or (name == "os_time") { return true }
|
|
if (name == "peekf") or (name == "pokef") or (name == "as_fixed") or (name == "as_int") { return true }
|
|
if (name == "peekp") or (name == "pokep") { 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
|
|
}
|
|
|
|
fn emit_intrinsic2(name: ptr, e: Node) -> Val {
|
|
if (name == "mem_free") {
|
|
let p = arg_code(e, 0); emit(" call void @free(ptr "); emit(p); emit(")\n"); return val("0", "void")
|
|
}
|
|
if (name == "mem_copy") {
|
|
let d = arg_code(e, 0); let s = arg_code(e, 1); let n = arg_code(e, 2)
|
|
let w = emit_bind(("zext i32 " + (n + " to i64")))
|
|
emit(" call ptr @memcpy(ptr "); emit(d); emit(", ptr "); emit(s); emit(", i64 "); emit(w); emit(")\n")
|
|
return val("0", "void")
|
|
}
|
|
if (name == "mem_set") {
|
|
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 == "ptr_add") {
|
|
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, "ptr")
|
|
}
|
|
if (name == "read_byte") { return val(emit_bind("call i32 @getchar()"), "int") }
|
|
if (name == "write_byte") {
|
|
let v = arg_code(e, 0); emit(" call i32 @putchar(i32 "); emit(v); emit(")\n"); return val("0", "void")
|
|
}
|
|
if (name == "str_len") {
|
|
let s = arg_code(e, 0)
|
|
let r = emit_bind(("call i64 @strlen(ptr " + (s + ")")))
|
|
return val(emit_bind(("trunc i64 " + (r + " to i32"))), "int")
|
|
}
|
|
if (name == "os_time") {
|
|
let r = emit_bind("call i64 @time(ptr null)")
|
|
return val(emit_bind(("trunc i64 " + (r + " to i32"))), "int")
|
|
}
|
|
if (name == "peekf") {
|
|
let p = arg_code(e, 0); let i = arg_code(e, 1)
|
|
let g = nreg(); emit(" "); emit(g); emit(" = getelementptr inbounds i32, ptr "); emit(p); emit(", i32 "); emit(i); emit("\n")
|
|
return val(emit_bind(("load i32, ptr " + g)), "fixed")
|
|
}
|
|
if (name == "pokef") {
|
|
let p = arg_code(e, 0); let i = arg_code(e, 1); let v = arg_code(e, 2)
|
|
let g = nreg(); emit(" "); emit(g); emit(" = getelementptr inbounds i32, ptr "); emit(p); emit(", i32 "); emit(i); emit("\n")
|
|
emit(" store i32 "); emit(v); emit(", ptr "); emit(g); emit("\n")
|
|
return val("0", "void")
|
|
}
|
|
if (name == "peekp") {
|
|
let p = arg_code(e, 0); let i = arg_code(e, 1)
|
|
let g = nreg(); emit(" "); emit(g); emit(" = getelementptr inbounds ptr, ptr "); emit(p); emit(", i32 "); emit(i); emit("\n")
|
|
return val(emit_bind(("load ptr, ptr " + g)), "ptr")
|
|
}
|
|
if (name == "pokep") {
|
|
let p = arg_code(e, 0); let i = arg_code(e, 1); let v = arg_code(e, 2)
|
|
let g = nreg(); emit(" "); emit(g); emit(" = getelementptr inbounds ptr, ptr "); emit(p); emit(", i32 "); emit(i); emit("\n")
|
|
emit(" store ptr "); emit(v); emit(", ptr "); emit(g); emit("\n")
|
|
return val("0", "void")
|
|
}
|
|
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", "str") }
|
|
# 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")
|
|
}
|