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>
558 lines
23 KiB
Text
558 lines
23 KiB
Text
# emit_text.ludic — the Text.* namespace over `str` (null-terminated byte
|
|
# strings). The libc-backed queries (length/char_at/starts_with/ends_with/
|
|
# contains/index_of/to_int) allocate nothing; slice/from_int/equals/concat reuse
|
|
# the string preludes that the `+`, `s[a..b]` and string(int) operators emit.
|
|
|
|
function is_text_ns(meth: pointer) -> bool {
|
|
if (meth == "length") or (meth == "char_at") or (meth == "slice") { return true }
|
|
if (meth == "equals") or (meth == "concat") or (meth == "to_int") or (meth == "from_int") { return true }
|
|
if (meth == "starts_with") or (meth == "ends_with") { return true }
|
|
if (meth == "contains") or (meth == "index_of") { return true }
|
|
if (meth == "upper") or (meth == "lower") or (meth == "trim") or (meth == "repeat") { return true }
|
|
if (meth == "pad_left") or (meth == "pad_right") { return true }
|
|
if (meth == "split") or (meth == "join") or (meth == "replace") { return true }
|
|
return false
|
|
}
|
|
|
|
function emit_text_ns(meth: pointer, e: Node) -> Val {
|
|
if (meth == "from_int") { # int -> string, same as string(n)
|
|
let n = emit_expr(e.kids[0])
|
|
g_uses_intstr = true
|
|
return val(emit_bind(`call ptr @fn_int_str(i32 {n.code})`), "string")
|
|
}
|
|
if (meth == "slice") { # s[a..b], same substring helper
|
|
let s0 = emit_expr(e.kids[0]); let a = emit_expr(e.kids[1]); let b = emit_expr(e.kids[2])
|
|
g_uses_strslice = true
|
|
return val(emit_bind(`call ptr @fn_str_slice(ptr {s0.code}, i32 {a.code}, i32 {b.code})`), "string")
|
|
}
|
|
if (meth == "equals") { # byte-wise equality, same as ==
|
|
let a = emit_expr(e.kids[0]); let b = emit_expr(e.kids[1])
|
|
g_uses_str = true
|
|
return val(emit_bind(`call i32 @fn_str_eq(ptr {a.code}, ptr {b.code})`), "bool")
|
|
}
|
|
if (meth == "concat") { # a + b, same as the + operator
|
|
let a = emit_expr(e.kids[0]); let b = emit_expr(e.kids[1])
|
|
g_uses_str = true
|
|
return val(emit_bind(`call ptr @fn_str_concat(ptr {a.code}, ptr {b.code})`), "string")
|
|
}
|
|
|
|
let s = emit_expr(e.kids[0])
|
|
if (meth == "length") { # byte length
|
|
let r = emit_bind(`call i64 @strlen(ptr {s.code})`)
|
|
return val(emit_bind(`trunc i64 {r} to i32`), "int")
|
|
}
|
|
if (meth == "char_at") { # the byte at index i, 0..255
|
|
let i = emit_expr(e.kids[1])
|
|
let a = emit_bind(`getelementptr inbounds i8, ptr {s.code}, i32 {i.code}`)
|
|
let c = emit_bind(`load i8, ptr {a}`)
|
|
return val(emit_bind(`zext i8 {c} to i32`), "int")
|
|
}
|
|
if (meth == "to_int") { # parse a leading integer, 0 if none
|
|
return val(emit_bind(`call i32 @atoi(ptr {s.code})`), "int")
|
|
}
|
|
if (meth == "upper") { # ASCII a-z -> A-Z, fresh string
|
|
g_uses_textrt = true
|
|
return val(emit_bind(`call ptr @fn_str_upper(ptr {s.code})`), "string")
|
|
}
|
|
if (meth == "lower") { # ASCII A-Z -> a-z, fresh string
|
|
g_uses_textrt = true
|
|
return val(emit_bind(`call ptr @fn_str_lower(ptr {s.code})`), "string")
|
|
}
|
|
if (meth == "trim") { # drop leading/trailing whitespace
|
|
g_uses_textrt = true
|
|
return val(emit_bind(`call ptr @fn_str_trim(ptr {s.code})`), "string")
|
|
}
|
|
if (meth == "repeat") { # s repeated n times
|
|
g_uses_textrt = true
|
|
let n = emit_expr(e.kids[1])
|
|
return val(emit_bind(`call ptr @fn_str_repeat(ptr {s.code}, i32 {n.code})`), "string")
|
|
}
|
|
if (meth == "pad_left") { # pad with spaces to width, on the left
|
|
g_uses_textrt = true
|
|
let w = emit_expr(e.kids[1])
|
|
return val(emit_bind(`call ptr @fn_str_pad(ptr {s.code}, i32 {w.code}, i1 1)`), "string")
|
|
}
|
|
if (meth == "pad_right") { # pad with spaces to width, on the right
|
|
g_uses_textrt = true
|
|
let w = emit_expr(e.kids[1])
|
|
return val(emit_bind(`call ptr @fn_str_pad(ptr {s.code}, i32 {w.code}, i1 0)`), "string")
|
|
}
|
|
if (meth == "replace") { # replace every `from` with `to`
|
|
g_uses_textrt2 = true
|
|
let from = emit_expr(e.kids[1]); let to = emit_expr(e.kids[2])
|
|
return val(emit_bind(`call ptr @fn_str_replace(ptr {s.code}, ptr {from.code}, ptr {to.code})`), "string")
|
|
}
|
|
if (meth == "split") { # split on a separator -> []string
|
|
g_uses_textrt2 = true
|
|
let sep = emit_expr(e.kids[1])
|
|
return val(emit_bind(`call ptr @fn_str_split(ptr {s.code}, ptr {sep.code})`), "[]string")
|
|
}
|
|
if (meth == "join") { # join a []string with a separator (s is the slice)
|
|
g_uses_textrt2 = true
|
|
let sep = emit_expr(e.kids[1])
|
|
return val(emit_bind(`call ptr @fn_str_join(ptr {s.code}, ptr {sep.code})`), "string")
|
|
}
|
|
if (meth == "contains") or (meth == "index_of") { # substring search
|
|
let sub = emit_expr(e.kids[1])
|
|
let p = emit_bind(`call ptr @strstr(ptr {s.code}, ptr {sub.code})`)
|
|
if (meth == "contains") {
|
|
let nn = emit_bind(`icmp ne ptr {p}, null`)
|
|
return val(emit_bind(`zext i1 {nn} to i32`), "bool")
|
|
}
|
|
let isnull = emit_bind(`icmp eq ptr {p}, null`) # index_of -> byte offset or -1
|
|
let pi = emit_bind(`ptrtoint ptr {p} to i64`)
|
|
let si = emit_bind(`ptrtoint ptr {s.code} to i64`)
|
|
let d = emit_bind(`sub i64 {pi}, {si}`)
|
|
let d32 = emit_bind(`trunc i64 {d} to i32`)
|
|
return val(emit_bind(`select i1 {isnull}, i32 -1, i32 {d32}`), "int")
|
|
}
|
|
|
|
# starts_with / ends_with: compare against the affix over its own length
|
|
let affix = emit_expr(e.kids[1])
|
|
let la = emit_bind(`call i64 @strlen(ptr {affix.code})`)
|
|
if (meth == "starts_with") { # strncmp of the head is null-safe
|
|
let cmp = emit_bind(`call i32 @strncmp(ptr {s.code}, ptr {affix.code}, i64 {la})`)
|
|
let eqz = emit_bind(`icmp eq i32 {cmp}, 0`)
|
|
return val(emit_bind(`zext i1 {eqz} to i32`), "bool")
|
|
}
|
|
# ends_with: compare the tail, but only when the affix fits (else a negative
|
|
# offset would read before the string) — branch so the strncmp never underflows
|
|
let ls = emit_bind(`call i64 @strlen(ptr {s.code})`)
|
|
let off = emit_bind(`sub i64 {ls}, {la}`)
|
|
let res = emit_alloca("i32")
|
|
store_at("i32", "0", res)
|
|
let neg = emit_bind(`icmp slt i64 {off}, 0`)
|
|
let cmpl = lbl("ew_cmp"); let en = lbl("ew_end")
|
|
emit(" br i1 "); emit(neg); emit(", label %"); emit(en); emit(", label %"); emit(cmpl); emit("\n")
|
|
emit(cmpl); emit(":\n")
|
|
let tail = emit_bind(`getelementptr inbounds i8, ptr {s.code}, i64 {off}`)
|
|
let cmp = emit_bind(`call i32 @strncmp(ptr {tail}, ptr {affix.code}, i64 {la})`)
|
|
let eqz = emit_bind(`icmp eq i32 {cmp}, 0`)
|
|
let z = emit_bind(`zext i1 {eqz} to i32`)
|
|
store_at("i32", z, res)
|
|
emit(" br label %"); emit(en); emit("\n")
|
|
emit(en); emit(":\n")
|
|
return val(emit_bind(`load i32, ptr {res}`), "bool")
|
|
}
|
|
|
|
# emit_text_prelude — string builders that allocate: upper/lower/trim/
|
|
# repeat/pad. Emitted once per program that uses them (g_uses_textrt). Plain
|
|
# libc (strlen/malloc/memcpy), deterministic, C-string in and out.
|
|
function emit_text_prelude() -> void {
|
|
emith("define ptr @fn_str_upper(ptr %s) {\n")
|
|
emith("entry:\n")
|
|
emith(" %n = call i64 @strlen(ptr %s)\n")
|
|
emith(" %sz = add i64 %n, 1\n")
|
|
emith(" %out = call ptr @malloc(i64 %sz)\n")
|
|
emith(" %ip = alloca i64\n")
|
|
emith(" store i64 0, ptr %ip\n")
|
|
emith(" br label %cond\n")
|
|
emith("cond:\n")
|
|
emith(" %i = load i64, ptr %ip\n")
|
|
emith(" %d = icmp ult i64 %i, %n\n")
|
|
emith(" br i1 %d, label %body, label %fin\n")
|
|
emith("body:\n")
|
|
emith(" %sp = getelementptr i8, ptr %s, i64 %i\n")
|
|
emith(" %c = load i8, ptr %sp\n")
|
|
emith(" %ge = icmp uge i8 %c, 97\n")
|
|
emith(" %le = icmp ule i8 %c, 122\n")
|
|
emith(" %in = and i1 %ge, %le\n")
|
|
emith(" %cc = add i8 %c, -32\n")
|
|
emith(" %oc = select i1 %in, i8 %cc, i8 %c\n")
|
|
emith(" %op = getelementptr i8, ptr %out, i64 %i\n")
|
|
emith(" store i8 %oc, ptr %op\n")
|
|
emith(" %i1 = add i64 %i, 1\n")
|
|
emith(" store i64 %i1, ptr %ip\n")
|
|
emith(" br label %cond\n")
|
|
emith("fin:\n")
|
|
emith(" %tp = getelementptr i8, ptr %out, i64 %n\n")
|
|
emith(" store i8 0, ptr %tp\n")
|
|
emith(" ret ptr %out\n")
|
|
emith("}\n")
|
|
emith("define ptr @fn_str_lower(ptr %s) {\n")
|
|
emith("entry:\n")
|
|
emith(" %n = call i64 @strlen(ptr %s)\n")
|
|
emith(" %sz = add i64 %n, 1\n")
|
|
emith(" %out = call ptr @malloc(i64 %sz)\n")
|
|
emith(" %ip = alloca i64\n")
|
|
emith(" store i64 0, ptr %ip\n")
|
|
emith(" br label %cond\n")
|
|
emith("cond:\n")
|
|
emith(" %i = load i64, ptr %ip\n")
|
|
emith(" %d = icmp ult i64 %i, %n\n")
|
|
emith(" br i1 %d, label %body, label %fin\n")
|
|
emith("body:\n")
|
|
emith(" %sp = getelementptr i8, ptr %s, i64 %i\n")
|
|
emith(" %c = load i8, ptr %sp\n")
|
|
emith(" %ge = icmp uge i8 %c, 65\n")
|
|
emith(" %le = icmp ule i8 %c, 90\n")
|
|
emith(" %in = and i1 %ge, %le\n")
|
|
emith(" %cc = add i8 %c, 32\n")
|
|
emith(" %oc = select i1 %in, i8 %cc, i8 %c\n")
|
|
emith(" %op = getelementptr i8, ptr %out, i64 %i\n")
|
|
emith(" store i8 %oc, ptr %op\n")
|
|
emith(" %i1 = add i64 %i, 1\n")
|
|
emith(" store i64 %i1, ptr %ip\n")
|
|
emith(" br label %cond\n")
|
|
emith("fin:\n")
|
|
emith(" %tp = getelementptr i8, ptr %out, i64 %n\n")
|
|
emith(" store i8 0, ptr %tp\n")
|
|
emith(" ret ptr %out\n")
|
|
emith("}\n")
|
|
emith("define ptr @fn_str_trim(ptr %s) {\n")
|
|
emith("entry:\n")
|
|
emith(" %n = call i64 @strlen(ptr %s)\n")
|
|
emith(" %sp = alloca i64\n")
|
|
emith(" %ep = alloca i64\n")
|
|
emith(" store i64 0, ptr %sp\n")
|
|
emith(" store i64 %n, ptr %ep\n")
|
|
emith(" br label %lcond\n")
|
|
emith("lcond:\n")
|
|
emith(" %a0 = load i64, ptr %sp\n")
|
|
emith(" %e0 = load i64, ptr %ep\n")
|
|
emith(" %lt0 = icmp ult i64 %a0, %e0\n")
|
|
emith(" br i1 %lt0, label %lchk, label %rcond\n")
|
|
emith("lchk:\n")
|
|
emith(" %p0 = getelementptr i8, ptr %s, i64 %a0\n")
|
|
emith(" %c0 = load i8, ptr %p0\n")
|
|
emith(" %ws0 = call i1 @fn_is_ws(i8 %c0)\n")
|
|
emith(" br i1 %ws0, label %linc, label %rcond\n")
|
|
emith("linc:\n")
|
|
emith(" %a1 = add i64 %a0, 1\n")
|
|
emith(" store i64 %a1, ptr %sp\n")
|
|
emith(" br label %lcond\n")
|
|
emith("rcond:\n")
|
|
emith(" %a2 = load i64, ptr %sp\n")
|
|
emith(" %e2 = load i64, ptr %ep\n")
|
|
emith(" %lt2 = icmp ult i64 %a2, %e2\n")
|
|
emith(" br i1 %lt2, label %rchk, label %build\n")
|
|
emith("rchk:\n")
|
|
emith(" %em1 = sub i64 %e2, 1\n")
|
|
emith(" %p1 = getelementptr i8, ptr %s, i64 %em1\n")
|
|
emith(" %c1 = load i8, ptr %p1\n")
|
|
emith(" %ws1 = call i1 @fn_is_ws(i8 %c1)\n")
|
|
emith(" br i1 %ws1, label %rdec, label %build\n")
|
|
emith("rdec:\n")
|
|
emith(" store i64 %em1, ptr %ep\n")
|
|
emith(" br label %rcond\n")
|
|
emith("build:\n")
|
|
emith(" %st = load i64, ptr %sp\n")
|
|
emith(" %en = load i64, ptr %ep\n")
|
|
emith(" %len = sub i64 %en, %st\n")
|
|
emith(" %sz = add i64 %len, 1\n")
|
|
emith(" %out = call ptr @malloc(i64 %sz)\n")
|
|
emith(" %src = getelementptr i8, ptr %s, i64 %st\n")
|
|
emith(" call ptr @memcpy(ptr %out, ptr %src, i64 %len)\n")
|
|
emith(" %tp = getelementptr i8, ptr %out, i64 %len\n")
|
|
emith(" store i8 0, ptr %tp\n")
|
|
emith(" ret ptr %out\n")
|
|
emith("}\n")
|
|
emith("define i1 @fn_is_ws(i8 %c) {\n")
|
|
emith(" %a = icmp eq i8 %c, 32\n")
|
|
emith(" %b = icmp eq i8 %c, 9\n")
|
|
emith(" %d = icmp eq i8 %c, 10\n")
|
|
emith(" %e = icmp eq i8 %c, 13\n")
|
|
emith(" %ab = or i1 %a, %b\n")
|
|
emith(" %de = or i1 %d, %e\n")
|
|
emith(" %r = or i1 %ab, %de\n")
|
|
emith(" ret i1 %r\n")
|
|
emith("}\n")
|
|
emith("define ptr @fn_str_repeat(ptr %s, i32 %n32) {\n")
|
|
emith("entry:\n")
|
|
emith(" %nneg = icmp slt i32 %n32, 0\n")
|
|
emith(" %nn = select i1 %nneg, i32 0, i32 %n32\n")
|
|
emith(" %n = zext i32 %nn to i64\n")
|
|
emith(" %L = call i64 @strlen(ptr %s)\n")
|
|
emith(" %tot = mul i64 %L, %n\n")
|
|
emith(" %sz = add i64 %tot, 1\n")
|
|
emith(" %out = call ptr @malloc(i64 %sz)\n")
|
|
emith(" %ip = alloca i64\n")
|
|
emith(" store i64 0, ptr %ip\n")
|
|
emith(" br label %cond\n")
|
|
emith("cond:\n")
|
|
emith(" %i = load i64, ptr %ip\n")
|
|
emith(" %d = icmp ult i64 %i, %n\n")
|
|
emith(" br i1 %d, label %body, label %fin\n")
|
|
emith("body:\n")
|
|
emith(" %off = mul i64 %i, %L\n")
|
|
emith(" %dst = getelementptr i8, ptr %out, i64 %off\n")
|
|
emith(" call ptr @memcpy(ptr %dst, ptr %s, i64 %L)\n")
|
|
emith(" %i1 = add i64 %i, 1\n")
|
|
emith(" store i64 %i1, ptr %ip\n")
|
|
emith(" br label %cond\n")
|
|
emith("fin:\n")
|
|
emith(" %tp = getelementptr i8, ptr %out, i64 %tot\n")
|
|
emith(" store i8 0, ptr %tp\n")
|
|
emith(" ret ptr %out\n")
|
|
emith("}\n")
|
|
emith("define ptr @fn_str_pad(ptr %s, i32 %w32, i1 %left) {\n")
|
|
emith("entry:\n")
|
|
emith(" %L = call i64 @strlen(ptr %s)\n")
|
|
emith(" %w0 = sext i32 %w32 to i64\n")
|
|
emith(" %need = icmp ugt i64 %w0, %L\n")
|
|
emith(" br i1 %need, label %do, label %copy\n")
|
|
emith("copy:\n")
|
|
emith(" %sz0 = add i64 %L, 1\n")
|
|
emith(" %o0 = call ptr @malloc(i64 %sz0)\n")
|
|
emith(" %e0 = add i64 %L, 0\n")
|
|
emith(" call ptr @memcpy(ptr %o0, ptr %s, i64 %L)\n")
|
|
emith(" %t0 = getelementptr i8, ptr %o0, i64 %L\n")
|
|
emith(" store i8 0, ptr %t0\n")
|
|
emith(" ret ptr %o0\n")
|
|
emith("do:\n")
|
|
emith(" %pad = sub i64 %w0, %L\n")
|
|
emith(" %sz = add i64 %w0, 1\n")
|
|
emith(" %out = call ptr @malloc(i64 %sz)\n")
|
|
emith(" br i1 %left, label %padleft, label %padright\n")
|
|
emith("padleft:\n")
|
|
emith(" call void @fn_fill_sp(ptr %out, i64 0, i64 %pad)\n")
|
|
emith(" %dstL = getelementptr i8, ptr %out, i64 %pad\n")
|
|
emith(" call ptr @memcpy(ptr %dstL, ptr %s, i64 %L)\n")
|
|
emith(" br label %term\n")
|
|
emith("padright:\n")
|
|
emith(" call ptr @memcpy(ptr %out, ptr %s, i64 %L)\n")
|
|
emith(" call void @fn_fill_sp(ptr %out, i64 %L, i64 %pad)\n")
|
|
emith(" br label %term\n")
|
|
emith("term:\n")
|
|
emith(" %tp = getelementptr i8, ptr %out, i64 %w0\n")
|
|
emith(" store i8 0, ptr %tp\n")
|
|
emith(" ret ptr %out\n")
|
|
emith("}\n")
|
|
emith("define void @fn_fill_sp(ptr %buf, i64 %start, i64 %count) {\n")
|
|
emith("entry:\n")
|
|
emith(" %ip = alloca i64\n")
|
|
emith(" store i64 0, ptr %ip\n")
|
|
emith(" br label %cond\n")
|
|
emith("cond:\n")
|
|
emith(" %i = load i64, ptr %ip\n")
|
|
emith(" %d = icmp ult i64 %i, %count\n")
|
|
emith(" br i1 %d, label %body, label %done\n")
|
|
emith("body:\n")
|
|
emith(" %off = add i64 %start, %i\n")
|
|
emith(" %p = getelementptr i8, ptr %buf, i64 %off\n")
|
|
emith(" store i8 32, ptr %p\n")
|
|
emith(" %i1 = add i64 %i, 1\n")
|
|
emith(" store i64 %i1, ptr %ip\n")
|
|
emith(" br label %cond\n")
|
|
emith("done:\n")
|
|
emith(" ret void\n")
|
|
emith("}\n")
|
|
}
|
|
|
|
# emit_text2_prelude — the allocating Text ops that build strings/slices:
|
|
# replace, join (over a []string), split (returns a []string). Emitted once per
|
|
# program that uses them (g_uses_textrt2). Slices are the {data,len,cap}
|
|
# %LSlice with str (ptr) elements.
|
|
function emit_text2_prelude() -> void {
|
|
emith("define ptr @fn_str_replace(ptr %s, ptr %from, ptr %to) {\n")
|
|
emith("entry:\n")
|
|
emith(" %lf = call i64 @strlen(ptr %from)\n")
|
|
emith(" %lz = icmp eq i64 %lf, 0\n")
|
|
emith(" br i1 %lz, label %copyall, label %scan\n")
|
|
emith("copyall:\n")
|
|
emith(" %ls0 = call i64 @strlen(ptr %s)\n")
|
|
emith(" %sz0 = add i64 %ls0, 1\n")
|
|
emith(" %o0 = call ptr @malloc(i64 %sz0)\n")
|
|
emith(" call ptr @memcpy(ptr %o0, ptr %s, i64 %sz0)\n")
|
|
emith(" ret ptr %o0\n")
|
|
emith("scan:\n")
|
|
emith(" %lt = call i64 @strlen(ptr %to)\n")
|
|
emith(" %ls = call i64 @strlen(ptr %s)\n")
|
|
emith(" %cntp = alloca i64\n")
|
|
emith(" store i64 0, ptr %cntp\n")
|
|
emith(" %curp = alloca ptr\n")
|
|
emith(" store ptr %s, ptr %curp\n")
|
|
emith(" br label %ccond\n")
|
|
emith("ccond:\n")
|
|
emith(" %cur = load ptr, ptr %curp\n")
|
|
emith(" %hit = call ptr @strstr(ptr %cur, ptr %from)\n")
|
|
emith(" %isnull = icmp eq ptr %hit, null\n")
|
|
emith(" br i1 %isnull, label %csize, label %cinc\n")
|
|
emith("cinc:\n")
|
|
emith(" %c0 = load i64, ptr %cntp\n")
|
|
emith(" %c1 = add i64 %c0, 1\n")
|
|
emith(" store i64 %c1, ptr %cntp\n")
|
|
emith(" %adv = getelementptr i8, ptr %hit, i64 %lf\n")
|
|
emith(" store ptr %adv, ptr %curp\n")
|
|
emith(" br label %ccond\n")
|
|
emith("csize:\n")
|
|
emith(" %cnt = load i64, ptr %cntp\n")
|
|
emith(" %delta = sub i64 %lt, %lf\n")
|
|
emith(" %grow = mul i64 %cnt, %delta\n")
|
|
emith(" %newlen = add i64 %ls, %grow\n")
|
|
emith(" %osz = add i64 %newlen, 1\n")
|
|
emith(" %out = call ptr @malloc(i64 %osz)\n")
|
|
emith(" %srcp = alloca ptr\n")
|
|
emith(" store ptr %s, ptr %srcp\n")
|
|
emith(" %dstp = alloca ptr\n")
|
|
emith(" store ptr %out, ptr %dstp\n")
|
|
emith(" br label %bcond\n")
|
|
emith("bcond:\n")
|
|
emith(" %src = load ptr, ptr %srcp\n")
|
|
emith(" %h2 = call ptr @strstr(ptr %src, ptr %from)\n")
|
|
emith(" %n2 = icmp eq ptr %h2, null\n")
|
|
emith(" br i1 %n2, label %tail, label %seg\n")
|
|
emith("seg:\n")
|
|
emith(" %si = ptrtoint ptr %src to i64\n")
|
|
emith(" %hi = ptrtoint ptr %h2 to i64\n")
|
|
emith(" %seglen = sub i64 %hi, %si\n")
|
|
emith(" %dst0 = load ptr, ptr %dstp\n")
|
|
emith(" call ptr @memcpy(ptr %dst0, ptr %src, i64 %seglen)\n")
|
|
emith(" %dst1 = getelementptr i8, ptr %dst0, i64 %seglen\n")
|
|
emith(" call ptr @memcpy(ptr %dst1, ptr %to, i64 %lt)\n")
|
|
emith(" %dst2 = getelementptr i8, ptr %dst1, i64 %lt\n")
|
|
emith(" store ptr %dst2, ptr %dstp\n")
|
|
emith(" %src2 = getelementptr i8, ptr %h2, i64 %lf\n")
|
|
emith(" store ptr %src2, ptr %srcp\n")
|
|
emith(" br label %bcond\n")
|
|
emith("tail:\n")
|
|
emith(" %src3 = load ptr, ptr %srcp\n")
|
|
emith(" %rem = call i64 @strlen(ptr %src3)\n")
|
|
emith(" %dst3 = load ptr, ptr %dstp\n")
|
|
emith(" %remp1 = add i64 %rem, 1\n")
|
|
emith(" call ptr @memcpy(ptr %dst3, ptr %src3, i64 %remp1)\n")
|
|
emith(" ret ptr %out\n")
|
|
emith("}\n")
|
|
emith("define ptr @fn_str_join(ptr %h, ptr %sep) {\n")
|
|
emith("entry:\n")
|
|
emith(" %lp = getelementptr inbounds %LSlice, ptr %h, i32 0, i32 1\n")
|
|
emith(" %len32 = load i32, ptr %lp\n")
|
|
emith(" %len = sext i32 %len32 to i64\n")
|
|
emith(" %dp = getelementptr inbounds %LSlice, ptr %h, i32 0, i32 0\n")
|
|
emith(" %data = load ptr, ptr %dp\n")
|
|
emith(" %lsep = call i64 @strlen(ptr %sep)\n")
|
|
emith(" %totp = alloca i64\n")
|
|
emith(" store i64 0, ptr %totp\n")
|
|
emith(" %ip = alloca i64\n")
|
|
emith(" store i64 0, ptr %ip\n")
|
|
emith(" br label %scond\n")
|
|
emith("scond:\n")
|
|
emith(" %i = load i64, ptr %ip\n")
|
|
emith(" %d = icmp ult i64 %i, %len\n")
|
|
emith(" br i1 %d, label %sbody, label %alloc\n")
|
|
emith("sbody:\n")
|
|
emith(" %ep = getelementptr ptr, ptr %data, i64 %i\n")
|
|
emith(" %es = load ptr, ptr %ep\n")
|
|
emith(" %el = call i64 @strlen(ptr %es)\n")
|
|
emith(" %t0 = load i64, ptr %totp\n")
|
|
emith(" %t1 = add i64 %t0, %el\n")
|
|
emith(" store i64 %t1, ptr %totp\n")
|
|
emith(" %i1 = add i64 %i, 1\n")
|
|
emith(" store i64 %i1, ptr %ip\n")
|
|
emith(" br label %scond\n")
|
|
emith("alloc:\n")
|
|
emith(" %nz = icmp ugt i64 %len, 0\n")
|
|
emith(" %lm1 = sub i64 %len, 1\n")
|
|
emith(" %seps = mul i64 %lm1, %lsep\n")
|
|
emith(" %sepsz = select i1 %nz, i64 %seps, i64 0\n")
|
|
emith(" %tot0 = load i64, ptr %totp\n")
|
|
emith(" %tot = add i64 %tot0, %sepsz\n")
|
|
emith(" %osz = add i64 %tot, 1\n")
|
|
emith(" %out = call ptr @malloc(i64 %osz)\n")
|
|
emith(" %dstp = alloca ptr\n")
|
|
emith(" store ptr %out, ptr %dstp\n")
|
|
emith(" %jp = alloca i64\n")
|
|
emith(" store i64 0, ptr %jp\n")
|
|
emith(" br label %jcond\n")
|
|
emith("jcond:\n")
|
|
emith(" %j = load i64, ptr %jp\n")
|
|
emith(" %d2 = icmp ult i64 %j, %len\n")
|
|
emith(" br i1 %d2, label %jbody, label %jfin\n")
|
|
emith("jbody:\n")
|
|
emith(" %jnz = icmp ugt i64 %j, 0\n")
|
|
emith(" br i1 %jnz, label %putsep, label %putel\n")
|
|
emith("putsep:\n")
|
|
emith(" %ds0 = load ptr, ptr %dstp\n")
|
|
emith(" call ptr @memcpy(ptr %ds0, ptr %sep, i64 %lsep)\n")
|
|
emith(" %ds1 = getelementptr i8, ptr %ds0, i64 %lsep\n")
|
|
emith(" store ptr %ds1, ptr %dstp\n")
|
|
emith(" br label %putel\n")
|
|
emith("putel:\n")
|
|
emith(" %ep2 = getelementptr ptr, ptr %data, i64 %j\n")
|
|
emith(" %es2 = load ptr, ptr %ep2\n")
|
|
emith(" %el2 = call i64 @strlen(ptr %es2)\n")
|
|
emith(" %ds2 = load ptr, ptr %dstp\n")
|
|
emith(" call ptr @memcpy(ptr %ds2, ptr %es2, i64 %el2)\n")
|
|
emith(" %ds3 = getelementptr i8, ptr %ds2, i64 %el2\n")
|
|
emith(" store ptr %ds3, ptr %dstp\n")
|
|
emith(" %j1 = add i64 %j, 1\n")
|
|
emith(" store i64 %j1, ptr %jp\n")
|
|
emith(" br label %jcond\n")
|
|
emith("jfin:\n")
|
|
emith(" %dsf = load ptr, ptr %dstp\n")
|
|
emith(" store i8 0, ptr %dsf\n")
|
|
emith(" ret ptr %out\n")
|
|
emith("}\n")
|
|
emith("define ptr @fn_str_split(ptr %s, ptr %sep) {\n")
|
|
emith("entry:\n")
|
|
emith(" %lsep = call i64 @strlen(ptr %sep)\n")
|
|
emith(" %cntp = alloca i64\n")
|
|
emith(" store i64 1, ptr %cntp\n")
|
|
emith(" %curp = alloca ptr\n")
|
|
emith(" store ptr %s, ptr %curp\n")
|
|
emith(" br label %ccond\n")
|
|
emith("ccond:\n")
|
|
emith(" %cur = load ptr, ptr %curp\n")
|
|
emith(" %hit = call ptr @strstr(ptr %cur, ptr %sep)\n")
|
|
emith(" %isnull = icmp eq ptr %hit, null\n")
|
|
emith(" br i1 %isnull, label %mk, label %cinc\n")
|
|
emith("cinc:\n")
|
|
emith(" %c0 = load i64, ptr %cntp\n")
|
|
emith(" %c1 = add i64 %c0, 1\n")
|
|
emith(" store i64 %c1, ptr %cntp\n")
|
|
emith(" %adv = getelementptr i8, ptr %hit, i64 %lsep\n")
|
|
emith(" store ptr %adv, ptr %curp\n")
|
|
emith(" br label %ccond\n")
|
|
emith("mk:\n")
|
|
emith(" %cnt = load i64, ptr %cntp\n")
|
|
emith(" %hdr = call ptr @malloc(i64 16)\n")
|
|
emith(" %arrsz = mul i64 %cnt, 8\n")
|
|
emith(" %arr = call ptr @malloc(i64 %arrsz)\n")
|
|
emith(" %d0 = getelementptr inbounds %LSlice, ptr %hdr, i32 0, i32 0\n")
|
|
emith(" store ptr %arr, ptr %d0\n")
|
|
emith(" %cnt32 = trunc i64 %cnt to i32\n")
|
|
emith(" %d1 = getelementptr inbounds %LSlice, ptr %hdr, i32 0, i32 1\n")
|
|
emith(" store i32 %cnt32, ptr %d1\n")
|
|
emith(" %d2 = getelementptr inbounds %LSlice, ptr %hdr, i32 0, i32 2\n")
|
|
emith(" store i32 %cnt32, ptr %d2\n")
|
|
emith(" %srcp = alloca ptr\n")
|
|
emith(" store ptr %s, ptr %srcp\n")
|
|
emith(" %ip = alloca i64\n")
|
|
emith(" store i64 0, ptr %ip\n")
|
|
emith(" br label %fcond\n")
|
|
emith("fcond:\n")
|
|
emith(" %i = load i64, ptr %ip\n")
|
|
emith(" %lt = icmp ult i64 %i, %cnt\n")
|
|
emith(" br i1 %lt, label %fbody, label %done\n")
|
|
emith("fbody:\n")
|
|
emith(" %src = load ptr, ptr %srcp\n")
|
|
emith(" %h2 = call ptr @strstr(ptr %src, ptr %sep)\n")
|
|
emith(" %n2 = icmp eq ptr %h2, null\n")
|
|
emith(" br i1 %n2, label %last, label %mid\n")
|
|
emith("mid:\n")
|
|
emith(" %si = ptrtoint ptr %src to i64\n")
|
|
emith(" %hi = ptrtoint ptr %h2 to i64\n")
|
|
emith(" %seglen = sub i64 %hi, %si\n")
|
|
emith(" br label %store\n")
|
|
emith("last:\n")
|
|
emith(" %rem = call i64 @strlen(ptr %src)\n")
|
|
emith(" br label %store\n")
|
|
emith("store:\n")
|
|
emith(" %seg = phi i64 [ %seglen, %mid ], [ %rem, %last ]\n")
|
|
emith(" %ssz = add i64 %seg, 1\n")
|
|
emith(" %sub = call ptr @malloc(i64 %ssz)\n")
|
|
emith(" %src2 = load ptr, ptr %srcp\n")
|
|
emith(" call ptr @memcpy(ptr %sub, ptr %src2, i64 %seg)\n")
|
|
emith(" %tp = getelementptr i8, ptr %sub, i64 %seg\n")
|
|
emith(" store i8 0, ptr %tp\n")
|
|
emith(" %slot = getelementptr ptr, ptr %arr, i64 %i\n")
|
|
emith(" store ptr %sub, ptr %slot\n")
|
|
emith(" %adv2 = getelementptr i8, ptr %h2, i64 %lsep\n")
|
|
emith(" store ptr %adv2, ptr %srcp\n")
|
|
emith(" %i1 = add i64 %i, 1\n")
|
|
emith(" store i64 %i1, ptr %ip\n")
|
|
emith(" br label %fcond\n")
|
|
emith("done:\n")
|
|
emith(" ret ptr %hdr\n")
|
|
emith("}\n")
|
|
}
|