# emit_fs.ludic — the Fs.* / Path.* / Mime.* namespaces: the filesystem, wrapped # into one safe, ergonomic API so a non-expert never touches a file descriptor or # a byte buffer. The foundation for saves, config, mods, and asset loading. The # bare file_* builtins remain the low-level primitive; this is the layer above. # # Path.join(a, b) join two path segments with the separator # Path.dir(p) the directory part ("mods/foo/x.json" -> "mods/foo") # Path.base(p) the final component ("mods/foo/x.json" -> "x.json") # Path.ext(p) the extension incl. dot (".json"), or "" if none # Path.stem(p) the base without its extension ("x") # Path.normalize(p) collapse "." / ".." / duplicate separators # # Fs.exists(p) does the path exist? -> bool # Fs.is_dir(p) is it a directory? -> bool # Fs.read_text(p) read a whole file as a string -> string (null on error) # Fs.write_text(p, s) write a string (atomic tmp+rename) -> bool # Fs.append_text(p, s) append a string -> bool # Fs.remove(p) delete a file -> bool # Fs.size(p) file size in bytes -> int (-1 on error) # Fs.mkdir(p) create a directory (and its parents) -> bool # Fs.copy(src, dst) copy a file, byte-for-byte -> bool # Fs.list(dir) directory entries, sorted, stable -> []string # # Mime.of(path) MIME type from the file extension -> string # Mime.sniff(path) ...refined by magic bytes -> string # # Errors are surfaced as values, never crashes: a fallible call returns null / "" # / false / -1 that the caller can branch on (a first-class try/else lands with # the error-handling work). Determinism: Fs.list is sorted, so a directory walk # is reproducible across runs and platforms. # # Coverage (v1): paths use "/" and the native (macOS/BSD) filesystem — the fully # supported target today. Windows separators, a sandboxed virtual FS for wasm, # recursive directory copy, and richer magic-byte sniffing are follow-ups. Text # helpers are UTF-8 (they tie into Unicode.*); write_text is atomic (tmp+rename) # so a crash mid-write never corrupts the previous file. function is_fs_ns(meth: pointer) -> bool { if (meth == "exists") or (meth == "is_dir") or (meth == "read_text") { return true } if (meth == "write_text") or (meth == "append_text") or (meth == "remove") { return true } if (meth == "size") or (meth == "mkdir") or (meth == "copy") or (meth == "list") { return true } if (meth == "mount") or (meth == "unmount") { return true } return false } function is_path_ns(meth: pointer) -> bool { if (meth == "join") or (meth == "dir") or (meth == "base") { return true } if (meth == "ext") or (meth == "stem") or (meth == "normalize") { return true } return false } function is_mime_ns(meth: pointer) -> bool { if (meth == "of") or (meth == "sniff") { return true } return false } function emit_fs_ns(meth: pointer, e: Node) -> Val { use_pak() let a = emit_expr(e.kids[0]) if (meth == "exists") { return val(emit_bind(`call i32 @lp_fs_exists(ptr {a.code})`), "bool") } # a pack (.lpak / .lmap) mounted at run time over the packs.index ones, searched first; and let go if (meth == "mount") { return val(emit_bind(`call i32 @lp_pak_mount(ptr {a.code})`), "bool") } if (meth == "unmount") { return val(emit_bind(`call i32 @lp_pak_unmount(ptr {a.code})`), "bool") } if (meth == "is_dir") { return val(emit_bind(`call i32 @lp_fs_is_dir(ptr {a.code})`), "bool") } if (meth == "read_text") { return val(emit_bind(`call ptr @lp_fs_read_text(ptr {a.code})`), "string") } if (meth == "remove") { return val(emit_bind(`call i32 @lp_fs_remove(ptr {a.code})`), "bool") } if (meth == "size") { return val(emit_bind(`call i32 @lp_fs_size(ptr {a.code})`), "int") } if (meth == "mkdir") { return val(emit_bind(`call i32 @lp_fs_mkdir(ptr {a.code})`), "bool") } if (meth == "list") { return val(emit_bind(`call ptr @lp_fs_list(ptr {a.code})`), "[]string") } let b = emit_expr(e.kids[1]) if (meth == "write_text") { return val(emit_bind(`call i32 @lp_fs_write_text(ptr {a.code}, ptr {b.code})`), "bool") } if (meth == "append_text") { return val(emit_bind(`call i32 @lp_fs_append_text(ptr {a.code}, ptr {b.code})`), "bool") } # copy return val(emit_bind(`call i32 @lp_fs_copy(ptr {a.code}, ptr {b.code})`), "bool") } # every Path and Mime answer is a copy of its own, so it is fresh: freed once a +, == or print reads it function emit_path_ns(meth: pointer, e: Node) -> Val { use_pak() let a = emit_expr(e.kids[0]) if (meth == "dir") { return fresh_val(emit_bind(`call ptr @lp_path_dir(ptr {a.code})`), "string") } if (meth == "base") { return fresh_val(emit_bind(`call ptr @lp_path_base(ptr {a.code})`), "string") } if (meth == "ext") { return fresh_val(emit_bind(`call ptr @lp_path_ext(ptr {a.code})`), "string") } if (meth == "stem") { return fresh_val(emit_bind(`call ptr @lp_path_stem(ptr {a.code})`), "string") } if (meth == "normalize") { return fresh_val(emit_bind(`call ptr @lp_path_norm(ptr {a.code})`), "string") } # join let b = emit_expr(e.kids[1]) return fresh_val(emit_bind(`call ptr @lp_path_join(ptr {a.code}, ptr {b.code})`), "string") } function emit_mime_ns(meth: pointer, e: Node) -> Val { use_pak() let a = emit_expr(e.kids[0]) if (meth == "sniff") { return fresh_val(emit_bind(`call ptr @lp_mime_sniff(ptr {a.code})`), "string") } return fresh_val(emit_bind(`call ptr @lp_mime_of(ptr {a.code})`), "string") } # emit_fs_prelude — the Fs/Path/Mime runtime, emitted once per program that uses # any of the three namespaces (g_uses_fsrt). Pure libc + byte IR. function emit_fs_prelude() -> void { if not g_target_win { # defined by emit_win_prelude on Windows emith("declare i32 @remove(ptr)\n") emith("declare i32 @access(ptr, i32)\n") emith("declare i32 @mkdir(ptr, i32)\n") emith("declare i32 @rename(ptr, ptr)\n") emith("declare ptr @opendir(ptr)\n") emith("declare ptr @readdir(ptr)\n") emith("declare i32 @closedir(ptr)\n") } emit_fs_common() emit_fs_path() emit_fs_io() emit_fs_dir() emit_fs_mime() } # ---- shared helpers -------------------------------------------------------- function emit_fs_common() -> void { # duplicate %n bytes of %s into a fresh NUL-terminated buffer emith("define ptr @lp_fs_dup(ptr %s, i32 %n) {\n") emith("entry:\n %nz = zext i32 %n to i64\n %t = add i64 %nz, 1\n %m = call ptr @lp_malloc(i64 %t)\n call ptr @memcpy(ptr %m, ptr %s, i64 %nz)\n") emith(" %end = getelementptr i8, ptr %m, i32 %n\n store i8 0, ptr %end\n ret ptr %m\n}\n") emith("define ptr @lp_fs_strdup(ptr %s) {\n") emith("entry:\n %l = call i64 @strlen(ptr %s)\n %li = trunc i64 %l to i32\n %r = call ptr @lp_fs_dup(ptr %s, i32 %li)\n ret ptr %r\n}\n") # index of the last '/' in %s, or -1 if none emith("define i32 @lp_fs_lastslash(ptr %s) {\n") emith("entry:\n %ip = alloca i32\n %rp = alloca i32\n store i32 0, ptr %ip\n store i32 -1, ptr %rp\n br label %lp\n") emith("lp:\n %i = load i32, ptr %ip\n %p = getelementptr i8, ptr %s, i32 %i\n %b = load i8, ptr %p\n %c = zext i8 %b to i32\n") emith(" %z = icmp eq i32 %c, 0\n br i1 %z, label %done, label %go\n") emith("go:\n %sl = icmp eq i32 %c, 47\n br i1 %sl, label %set, label %nx\n") emith("set:\n store i32 %i, ptr %rp\n br label %nx\n") emith("nx:\n %i1 = add i32 %i, 1\n store i32 %i1, ptr %ip\n br label %lp\n") emith("done:\n %r = load i32, ptr %rp\n ret i32 %r\n}\n") } # ---- Path.* (pure string) -------------------------------------------------- function emit_fs_path() -> void { # join(a, b): b absolute -> b; a empty -> b; b empty -> a; else a + "/" + b # (avoiding a doubled separator when a already ends with one). emith("define ptr @lp_path_join(ptr %a, ptr %b) {\n") emith("entry:\n %la64 = call i64 @strlen(ptr %a)\n %la = trunc i64 %la64 to i32\n %lb64 = call i64 @strlen(ptr %b)\n %lb = trunc i64 %lb64 to i32\n") emith(" %b0 = load i8, ptr %b\n %b0i = zext i8 %b0 to i32\n %babs = icmp eq i32 %b0i, 47\n br i1 %babs, label %retb, label %ka\n") emith("retb:\n %rb = call ptr @lp_fs_strdup(ptr %b)\n ret ptr %rb\n") emith("ka:\n %aemp = icmp eq i32 %la, 0\n br i1 %aemp, label %retb2, label %kb\n") emith("retb2:\n %rb2 = call ptr @lp_fs_strdup(ptr %b)\n ret ptr %rb2\n") emith("kb:\n %bemp = icmp eq i32 %lb, 0\n br i1 %bemp, label %reta, label %chk\n") emith("reta:\n %ra = call ptr @lp_fs_strdup(ptr %a)\n ret ptr %ra\n") emith("chk:\n %lam1 = sub i32 %la, 1\n %pe = getelementptr i8, ptr %a, i32 %lam1\n %ae = load i8, ptr %pe\n %aei = zext i8 %ae to i32\n %ends = icmp eq i32 %aei, 47\n") emith(" %sep = select i1 %ends, i32 0, i32 1\n %tot = add i32 %la, %lb\n %tot2 = add i32 %tot, %sep\n %tot3 = add i32 %tot2, 1\n %totz = zext i32 %tot3 to i64\n %m = call ptr @lp_malloc(i64 %totz)\n") emith(" %laz = zext i32 %la to i64\n call ptr @memcpy(ptr %m, ptr %a, i64 %laz)\n") # write separator if needed emith(" br i1 %ends, label %nosep, label %wsep\n") emith("wsep:\n %sp = getelementptr i8, ptr %m, i32 %la\n store i8 47, ptr %sp\n br label %after\n") emith("nosep:\n br label %after\n") emith("after:\n %boff = add i32 %la, %sep\n %dp = getelementptr i8, ptr %m, i32 %boff\n %lbz = zext i32 %lb to i64\n call ptr @memcpy(ptr %dp, ptr %b, i64 %lbz)\n") emith(" %endoff = add i32 %boff, %lb\n %ep = getelementptr i8, ptr %m, i32 %endoff\n store i8 0, ptr %ep\n ret ptr %m\n}\n") # base(p): the component after the last '/', or p itself emith("define ptr @lp_path_base(ptr %s) {\n") emith("entry:\n %ls = call i32 @lp_fs_lastslash(ptr %s)\n %none = icmp eq i32 %ls, -1\n br i1 %none, label %whole, label %tail\n") emith("whole:\n %r = call ptr @lp_fs_strdup(ptr %s)\n ret ptr %r\n") emith("tail:\n %st = add i32 %ls, 1\n %p = getelementptr i8, ptr %s, i32 %st\n %r2 = call ptr @lp_fs_strdup(ptr %p)\n ret ptr %r2\n}\n") # dir(p): everything before the last '/', or "." if none; "/" stays "/" emith("define ptr @lp_path_dir(ptr %s) {\n") emith("entry:\n %ls = call i32 @lp_fs_lastslash(ptr %s)\n %none = icmp eq i32 %ls, -1\n br i1 %none, label %dot, label %chk0\n") emith("dot:\n %d = call ptr @lp_fs_strdup(ptr @fn_str_dot)\n ret ptr %d\n") emith("chk0:\n %isroot = icmp eq i32 %ls, 0\n br i1 %isroot, label %root, label %cut\n") emith("root:\n %r = call ptr @lp_fs_strdup(ptr @fn_str_slash)\n ret ptr %r\n") emith("cut:\n %r2 = call ptr @lp_fs_dup(ptr %s, i32 %ls)\n ret ptr %r2\n}\n") # ext(p): from the last '.' in the base component to the end, incl. the dot; # "" when the base has no '.' or begins with '.' (a dotfile has no extension) emith("define ptr @lp_path_ext(ptr %s) {\n") emith("entry:\n %ls = call i32 @lp_fs_lastslash(ptr %s)\n %bstart = add i32 %ls, 1\n") # ls=-1 -> bstart=0 emith(" %dp = alloca i32\n %ip = alloca i32\n store i32 -1, ptr %dp\n store i32 %bstart, ptr %ip\n br label %lp\n") emith("lp:\n %i = load i32, ptr %ip\n %p = getelementptr i8, ptr %s, i32 %i\n %b = load i8, ptr %p\n %c = zext i8 %b to i32\n %z = icmp eq i32 %c, 0\n br i1 %z, label %done, label %go\n") emith("go:\n %dot = icmp eq i32 %c, 46\n br i1 %dot, label %sd, label %nx\n") emith("sd:\n store i32 %i, ptr %dp\n br label %nx\n") emith("nx:\n %i1 = add i32 %i, 1\n store i32 %i1, ptr %ip\n br label %lp\n") emith("done:\n %d = load i32, ptr %dp\n %nod = icmp eq i32 %d, -1\n br i1 %nod, label %empty, label %chkpos\n") emith("empty:\n %e = call ptr @lp_fs_strdup(ptr @fn_str_empty)\n ret ptr %e\n") emith("chkpos:\n %atstart = icmp eq i32 %d, %bstart\n br i1 %atstart, label %empty2, label %take\n") emith("empty2:\n %e2 = call ptr @lp_fs_strdup(ptr @fn_str_empty)\n ret ptr %e2\n") emith("take:\n %pp = getelementptr i8, ptr %s, i32 %d\n %r = call ptr @lp_fs_strdup(ptr %pp)\n ret ptr %r\n}\n") # stem(p): the base component without its extension emith("define ptr @lp_path_stem(ptr %s) {\n") emith("entry:\n %base = call ptr @lp_path_base(ptr %s)\n %ext = call ptr @lp_path_ext(ptr %s)\n") emith(" %bl64 = call i64 @strlen(ptr %base)\n %bl = trunc i64 %bl64 to i32\n %el64 = call i64 @strlen(ptr %ext)\n %el = trunc i64 %el64 to i32\n") # the base and the extension are its own copies: freed once the answer is cut out of them emith(" %keep = sub i32 %bl, %el\n %r = call ptr @lp_fs_dup(ptr %base, i32 %keep)\n call void @lp_free(ptr %base)\n call void @lp_free(ptr %ext)\n ret ptr %r\n}\n") emit_fs_normalize() } # normalize(p): collapse duplicate '/', drop "." segments, and resolve ".." by # popping the previous kept segment (never above an absolute root). Preserves a # leading '/'. An empty result becomes ".". function emit_fs_normalize() -> void { emith("define ptr @lp_path_norm(ptr %s) {\n") emith("entry:\n %len64 = call i64 @strlen(ptr %s)\n %len = trunc i64 %len64 to i32\n %cap = add i32 %len, 2\n %capz = zext i32 %cap to i64\n") emith(" %out = call ptr @lp_malloc(i64 %capz)\n") # segst holds output offsets (i32) of each kept segment's start; size cap ints emith(" %stz = mul i32 %cap, 4\n %stzz = zext i32 %stz to i64\n %segst = call ptr @lp_malloc(i64 %stzz)\n") emith(" %b0 = load i8, ptr %s\n %b0i = zext i8 %b0 to i32\n %abs = icmp eq i32 %b0i, 47\n") emith(" %ip = alloca i32\n %op = alloca i32\n %scp = alloca i32\n store i32 0, ptr %ip\n store i32 0, ptr %scp\n") emith(" br i1 %abs, label %ldr, label %noldr\n") emith("ldr:\n store i8 47, ptr %out\n store i32 1, ptr %op\n br label %seg\n") emith("noldr:\n store i32 0, ptr %op\n br label %seg\n") emith("seg:\n br label %sksl\n") # skip leading slashes between segments emith("sksl:\n %i = load i32, ptr %ip\n %pp = getelementptr i8, ptr %s, i32 %i\n %cc = load i8, ptr %pp\n %cci = zext i8 %cc to i32\n %issl = icmp eq i32 %cci, 47\n br i1 %issl, label %adv, label %segstart\n") emith("adv:\n %i2 = add i32 %i, 1\n store i32 %i2, ptr %ip\n br label %sksl\n") # gather one segment [sstart, send) emith("segstart:\n %ss = load i32, ptr %ip\n br label %scan\n") emith("scan:\n %j = load i32, ptr %ip\n %pj = getelementptr i8, ptr %s, i32 %j\n %cj = load i8, ptr %pj\n %cji = zext i8 %cj to i32\n %endc = icmp eq i32 %cji, 0\n %slc = icmp eq i32 %cji, 47\n %stop = or i1 %endc, %slc\n br i1 %stop, label %seghave, label %scanadv\n") emith("scanadv:\n %j1 = add i32 %j, 1\n store i32 %j1, ptr %ip\n br label %scan\n") emith("seghave:\n %se = load i32, ptr %ip\n %slen = sub i32 %se, %ss\n %emptyseg = icmp eq i32 %slen, 0\n br i1 %emptyseg, label %atend, label %classify\n") # empty segment only happens at the very end (trailing slashes) -> finish emith("classify:\n") # is it "." ? (len 1, char '.') emith(" %is1 = icmp eq i32 %slen, 1\n %c0p = getelementptr i8, ptr %s, i32 %ss\n %c0 = load i8, ptr %c0p\n %c0i = zext i8 %c0 to i32\n %isdotchar = icmp eq i32 %c0i, 46\n %isdot = and i1 %is1, %isdotchar\n br i1 %isdot, label %contseg, label %chkdd\n") # is it ".." ? emith("chkdd:\n %is2 = icmp eq i32 %slen, 2\n %d0p = getelementptr i8, ptr %s, i32 %ss\n %d0 = load i8, ptr %d0p\n %d0i = zext i8 %d0 to i32\n %d1o = add i32 %ss, 1\n %d1p = getelementptr i8, ptr %s, i32 %d1o\n %d1 = load i8, ptr %d1p\n %d1i = zext i8 %d1 to i32\n") emith(" %dd0 = icmp eq i32 %d0i, 46\n %dd1 = icmp eq i32 %d1i, 46\n %ddx = and i1 %dd0, %dd1\n %isdd = and i1 %is2, %ddx\n br i1 %isdd, label %dotdot, label %keepseg\n") # ".." : pop a segment if we have one, else (relative) keep it literally emith("dotdot:\n %sc = load i32, ptr %scp\n %has = icmp sgt i32 %sc, 0\n br i1 %has, label %pop, label %chkrel\n") emith("pop:\n %sc1 = sub i32 %sc, 1\n %stp = getelementptr i32, ptr %segst, i32 %sc1\n %newop = load i32, ptr %stp\n store i32 %newop, ptr %op\n store i32 %sc1, ptr %scp\n br label %contseg\n") emith("chkrel:\n br i1 %abs, label %contseg, label %keepseg\n") # absolute: drop; relative: keep ".." # keep the segment: record its start, append it + a trailing '/' emith("keepseg:\n %sc2 = load i32, ptr %scp\n %o0 = load i32, ptr %op\n %stp2 = getelementptr i32, ptr %segst, i32 %sc2\n store i32 %o0, ptr %stp2\n %sc2n = add i32 %sc2, 1\n store i32 %sc2n, ptr %scp\n") emith(" %cpp = alloca i32\n store i32 %ss, ptr %cpp\n br label %cpy\n") emith("cpy:\n %k = load i32, ptr %cpp\n %klt = icmp slt i32 %k, %se\n br i1 %klt, label %cpyb, label %cpysl\n") emith("cpyb:\n %skp = getelementptr i8, ptr %s, i32 %k\n %sk = load i8, ptr %skp\n %oo = load i32, ptr %op\n %dkp = getelementptr i8, ptr %out, i32 %oo\n store i8 %sk, ptr %dkp\n %oo1 = add i32 %oo, 1\n store i32 %oo1, ptr %op\n %k1 = add i32 %k, 1\n store i32 %k1, ptr %cpp\n br label %cpy\n") emith("cpysl:\n %oc = load i32, ptr %op\n %scp3 = getelementptr i8, ptr %out, i32 %oc\n store i8 47, ptr %scp3\n %oc1 = add i32 %oc, 1\n store i32 %oc1, ptr %op\n br label %contseg\n") emith("contseg:\n %ci = load i32, ptr %ip\n %cip = getelementptr i8, ptr %s, i32 %ci\n %cic = load i8, ptr %cip\n %cici = zext i8 %cic to i32\n %atz = icmp eq i32 %cici, 0\n br i1 %atz, label %finish, label %seg\n") emith("atend:\n br label %finish\n") # trim a trailing '/' (unless the whole result is just "/"), then NUL-terminate emith("finish:\n %fo = load i32, ptr %op\n %gt1 = icmp sgt i32 %fo, 1\n br i1 %gt1, label %trimchk, label %fdefault\n") emith("trimchk:\n %lasto = sub i32 %fo, 1\n %lop = getelementptr i8, ptr %out, i32 %lasto\n %lc = load i8, ptr %lop\n %lci = zext i8 %lc to i32\n %istsl = icmp eq i32 %lci, 47\n br i1 %istsl, label %trim, label %term\n") emith("trim:\n store i32 %lasto, ptr %op\n br label %term\n") emith("fdefault:\n br label %term\n") emith("term:\n %eo = load i32, ptr %op\n %ez = icmp eq i32 %eo, 0\n br i1 %ez, label %emptyout, label %putnul\n") # the segment table is this call's own: freed on both ways out emith("emptyout:\n store i8 46, ptr %out\n %e1 = getelementptr i8, ptr %out, i32 1\n store i8 0, ptr %e1\n call void @lp_free(ptr %segst)\n ret ptr %out\n") emith("putnul:\n %ep = getelementptr i8, ptr %out, i32 %eo\n store i8 0, ptr %ep\n call void @lp_free(ptr %segst)\n ret ptr %out\n}\n") } # ---- Fs.* (libc) ----------------------------------------------------------- function emit_fs_io() -> void { # A path that is in a mounted pack exists, whether or not anything is on disk # at that name: a game that guards a load with Fs.exists (the sound layer does) # has to keep finding its assets once they are packed. emith("define i32 @lp_fs_exists(ptr %p) {\n") emith("entry:\n %ip = call i32 @lp_pak_has(ptr %p)\n %inpak = icmp ne i32 %ip, 0\n br i1 %inpak, label %yes, label %disk\n") emith("yes:\n ret i32 1\n") emith("disk:\n %r = call i32 @access(ptr %p, i32 0)\n %ok = icmp eq i32 %r, 0\n %z = zext i1 %ok to i32\n ret i32 %z\n}\n") # is_dir: opendir succeeds iff it is a directory emith("define i32 @lp_fs_is_dir(ptr %p) {\n") emith("entry:\n %d = call ptr @opendir(ptr %p)\n %nz = icmp ne ptr %d, null\n br i1 %nz, label %yes, label %no\n") emith("yes:\n call i32 @closedir(ptr %d)\n ret i32 1\n") emith("no:\n ret i32 0\n}\n") # size: bytes via fseek/ftell, or -1 if it cannot be opened emith("define i32 @lp_fs_size(ptr %p) {\n") emith("entry:\n %pn = call i32 @lp_pak_bytes(ptr %p)\n %inpak = icmp sge i32 %pn, 0\n br i1 %inpak, label %packed, label %disk\n") emith("packed:\n ret i32 %pn\n") emith("disk:\n %f = call ptr @fopen(ptr %p, ptr @fn_str_rb)\n %nz = icmp ne ptr %f, null\n br i1 %nz, label %ok, label %bad\n") emith("bad:\n ret i32 -1\n") emith("ok:\n call i32 @fseek(ptr %f, i64 0, i32 2)\n %n = call i64 @ftell(ptr %f)\n call i32 @fclose(ptr %f)\n %ni = trunc i64 %n to i32\n ret i32 %ni\n}\n") # readall: whole file into a fresh buffer; store byte length to %lenout; null on # failure. The buffer is NUL-terminated (one past the length) so text callers # can use it directly while binary callers use the length. emith("define ptr @lp_fs_readall(ptr %p, ptr %lenout) {\n") emith("entry:\n store i32 0, ptr %lenout\n %f = call ptr @lp_pak_open(ptr %p, ptr @fn_str_rb)\n %nz = icmp ne ptr %f, null\n br i1 %nz, label %ok, label %bad\n") emith("bad:\n ret ptr null\n") emith("ok:\n call i32 @fseek(ptr %f, i64 0, i32 2)\n %n64 = call i64 @ftell(ptr %f)\n call i32 @fseek(ptr %f, i64 0, i32 0)\n %n = trunc i64 %n64 to i32\n") emith(" %cap = add i64 %n64, 1\n %m = call ptr @lp_malloc(i64 %cap)\n %rd = call i64 @fread(ptr %m, i64 1, i64 %n64, ptr %f)\n call i32 @fclose(ptr %f)\n") emith(" %rdi = trunc i64 %rd to i32\n %endp = getelementptr i8, ptr %m, i32 %rdi\n store i8 0, ptr %endp\n store i32 %rdi, ptr %lenout\n ret ptr %m\n}\n") emith("define ptr @lp_fs_read_text(ptr %p) {\n") emith("entry:\n %lp = alloca i32\n %r = call ptr @lp_fs_readall(ptr %p, ptr %lp)\n ret ptr %r\n}\n") # write_text: write to "
.tmp" then rename over %p, so a crash mid-write # never truncates the previous file. Returns 1 on success. emith("define i32 @lp_fs_write_text(ptr %p, ptr %s) {\n") # the "
.tmp" name is this call's own and goes on both ways out emith("entry:\n %tmp = call ptr @lp_path_join_ext(ptr %p, ptr @fn_str_dottmp)\n %f = call ptr @fopen(ptr %tmp, ptr @fn_str_wb)\n %nz = icmp ne ptr %f, null\n br i1 %nz, label %ok, label %bad\n") emith("bad:\n call void @lp_free(ptr %tmp)\n ret i32 0\n") emith("ok:\n %n = call i64 @strlen(ptr %s)\n %w = call i64 @fwrite(ptr %s, i64 1, i64 %n, ptr %f)\n call i32 @fclose(ptr %f)\n") emith(" %rr = call i32 @rename(ptr %tmp, ptr %p)\n call void @lp_free(ptr %tmp)\n %ok2 = icmp eq i32 %rr, 0\n %z = zext i1 %ok2 to i32\n ret i32 %z\n}\n") # concat two strings (used to build "
.tmp"); local so write_text needs no
# dependency on the Os prelude
emith("define ptr @lp_path_join_ext(ptr %a, ptr %b) {\n")
emith("entry:\n %la = call i64 @strlen(ptr %a)\n %lb = call i64 @strlen(ptr %b)\n %sum = add i64 %la, %lb\n %tot = add i64 %sum, 1\n %m = call ptr @lp_malloc(i64 %tot)\n")
emith(" call ptr @memcpy(ptr %m, ptr %a, i64 %la)\n %m2 = getelementptr i8, ptr %m, i64 %la\n call ptr @memcpy(ptr %m2, ptr %b, i64 %lb)\n %ep = getelementptr i8, ptr %m, i64 %sum\n store i8 0, ptr %ep\n ret ptr %m\n}\n")
emith("define i32 @lp_fs_append_text(ptr %p, ptr %s) {\n")
emith("entry:\n %f = call ptr @fopen(ptr %p, ptr @fn_str_ab)\n %nz = icmp ne ptr %f, null\n br i1 %nz, label %ok, label %bad\n")
emith("bad:\n ret i32 0\n")
emith("ok:\n %n = call i64 @strlen(ptr %s)\n call i64 @fwrite(ptr %s, i64 1, i64 %n, ptr %f)\n call i32 @fclose(ptr %f)\n ret i32 1\n}\n")
emith("define i32 @lp_fs_remove(ptr %p) {\n")
emith("entry:\n %r = call i32 @remove(ptr %p)\n %ok = icmp eq i32 %r, 0\n %z = zext i1 %ok to i32\n ret i32 %z\n}\n")
# copy: byte-for-byte via readall + a sized write. Returns 1 on success.
emith("define i32 @lp_fs_copy(ptr %src, ptr %dst) {\n")
emith("entry:\n %lp = alloca i32\n %buf = call ptr @lp_fs_readall(ptr %src, ptr %lp)\n %nz = icmp ne ptr %buf, null\n br i1 %nz, label %ok, label %bad\n")
emith("bad:\n ret i32 0\n")
# the whole file read and the "