Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ Every change to srs that lands, newest first. A project that builds with srs (sn

## Unreleased

- strings: `Str.find(s, i, c)` is the first index >= i whose byte is c, else `Str.len(s)` (one memchr), and `Str.runWhile(s, i, n, t)` is the first index >= i whose byte b is not in the run table t (`b >= n` or `t(b) == 0`), else `Str.len(s)` (one loop over raw bytes, no per-byte bound check or call). Both are total (i is clamped), pure, and emitted as static inline C only in a program that calls them. A lexer's "skip while in class" loop becomes one call over a 256-byte table. Programs that do not call them emit the same C; s2c compiling itself: 5.000 G instructions before and after. The `--mem` goldens list the new externs' params.

- atomics: the same four ops on a slot of a word array: `Array.aload(xs, i)`, `Array.astore(xs, i, v)`, `Array.cas(xs, i, want, v)` (a Bool) and `Array.fetchAdd(xs, i, d)` work on `&xs[i]` with the memory orders of `ACell`, so one `Array[I64]` holds many atomic words. The index is proved in range as for `Array.put` (`error[index-unproved]`); each op is a request on `Atomic`. Programs that do not use them emit the same C; s2c compiling itself: 5.0003 G instructions before and after.

- atomics: `ACell[A]` is an atomic word (A is I64, U64 or Ptr). `ACell.new(v)` makes one in the innermost Region; `ACell.load(c)`, `ACell.store(c, v)`, `ACell.cas(c, want, v)` (a Bool) and `ACell.fetchAdd(c, d)` (the word before the add) are the C `__atomic` builtins with their memory order: load acquire, store release, cas acq_rel (failure acquire), fetchAdd seq_cst. Each op is a request on the new capability `Atomic` (handler `Srs.Atomic`), so plain code cannot touch an atomic word (`error[unhandled-effect]`). A cell of a non-word type is `error[mismatch]`. Programs without ACell emit the same C as before; s2c compiling itself: 5.0005 G instructions, same as before. The `--mem` goldens list the new handler's var.
Expand Down
6 changes: 4 additions & 2 deletions bootstrap/s2c-seed.c

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions lib/prelude.srs
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,11 @@ extern def I64.ofU64(x: U64): I64
// a sorts before b by bytes as unsigned values, a proper prefix first (memcmp); byte c is in s[i, e), i and e clamped
// to [0, Str.len(s)) (memchr). Static inline (Emit.strPrims)
extern def Str.less(a: Str, b: Str): Bool
// scans, TOTAL (i clamped to [0, Str.len(s))), static inline when called (Emit.scanPrims): Str.find is the first index
// >= i whose byte is c, else Str.len(s) (memchr); Str.runWhile is the first index >= i whose byte b is not in the run
// table t (b >= n or t(b) == 0), else Str.len(s): one loop over raw bytes, no per-byte bound or call
extern def Str.find(s: Str, i: I64, c: I64): I64
extern def Str.runWhile(s: Str, i: I64, n: I64, t: Array[U8](n)): I64
extern def Str.has(s: Str, c: I64, i: I64, e: I64): Bool

// R0a bytes: U8 is storage only (an Array[U8](n) element), with no literal and no operator (C promotes uint8_t to
Expand Down
9 changes: 8 additions & 1 deletion s2/emit.srs
Original file line number Diff line number Diff line change
Expand Up @@ -1801,7 +1801,7 @@ def Emit.strInline(n: Sym.Sym): Bool =

// the word conversions of lib/prelude.srs, static inline too (Emit.wordPrims)
def Emit.wordInline(s: Str): Bool =
Str.eq(s, "U32.of") || Str.eq(s, "U32.ofU64") || Str.eq(s, "U64.of") || Str.eq(s, "U64.ofI64") || Str.eq(s, "I64.of") || Str.eq(s, "I64.ofU64") || Str.eq(s, "Str.u32le") || Str.eq(s, "Str.u64le") || Str.eq(s, "Str.less") || Str.eq(s, "Str.has") || Str.eq(s, "U8.of") || Str.eq(s, "I64.ofU8") || Str.eq(s, "I32.of") || Str.eq(s, "I64.ofI32") || Emit.ptrPrim(s) || Emit.f64Prim(s)
Str.eq(s, "U32.of") || Str.eq(s, "U32.ofU64") || Str.eq(s, "U64.of") || Str.eq(s, "U64.ofI64") || Str.eq(s, "I64.of") || Str.eq(s, "I64.ofU64") || Str.eq(s, "Str.u32le") || Str.eq(s, "Str.u64le") || Str.eq(s, "Str.less") || Str.eq(s, "Str.has") || Str.eq(s, "Str.find") || Str.eq(s, "Str.runWhile") || Str.eq(s, "U8.of") || Str.eq(s, "I64.ofU8") || Str.eq(s, "I32.of") || Str.eq(s, "I64.ofI32") || Emit.ptrPrim(s) || Emit.f64Prim(s)

// C conversion to an unsigned type is mod 2^width (C11 6.3.1.3p2); uint64_t to int64_t above INT64_MAX and int64_t to int32_t out of range are
// implementation-defined (6.3.1.3p3), two's complement on every target compiler (gcc, clang)
Expand All @@ -1811,6 +1811,12 @@ def Emit.wordInline(s: Str): Bool =
def Emit.ptrPrim(s: Str): Bool =
Str.eq(s, "rt_ptr_get") || Str.eq(s, "rt_ptr_set") || Str.eq(s, "rt_ptr_add") || Str.eq(s, "rt_ptr_addr") || Str.eq(s, "rt_ptr_of") || Str.eq(s, "rt_globals")

// Str.find and Str.runWhile (lib/prelude.srs), only in a program that calls them: i clamped into [0, n], one memchr or one
// raw-byte loop whose bound is the loop guard
def Emit.scanPrims(cs: Coll.ISet): Unit =
Emit.primIf(cs, "Str.find", "static inline int64_t Str__find(srs_str s, int64_t i, int64_t c) {\n if (i < 0) i = 0;\n if (i >= s.n || (uint64_t)c > 255) return s.n;\n const uint8_t* q = memchr(s.p + i, (int)c, (size_t)(s.n - i));\n return q ? (int64_t)(q - s.p) : s.n;\n}\n")
Emit.primIf(cs, "Str.runWhile", "static inline int64_t Str__runWhile(srs_str s, int64_t i, int64_t n, uint8_t* t) {\n if (i < 0) i = 0;\n const uint8_t* p = s.p;\n int64_t e = s.n;\n if (n >= 256) { while (i < e && t[p[i]]) i++; }\n else { while (i < e && (int64_t)p[i] < n && t[p[i]]) i++; }\n return i < e ? i : e;\n}\n")

def Emit.ptrPrims(cs: Coll.ISet): Unit =
Emit.primIf(cs, "rt_ptr_get", "static inline int64_t rt_ptr_get(void* p, int64_t i) { return ((int64_t*)p)[i]; }\n")
Emit.primIf(cs, "rt_ptr_set", "static inline int64_t rt_ptr_set(void* p, int64_t i, int64_t v) { ((int64_t*)p)[i] = v; return 0; }\n")
Expand Down Expand Up @@ -3138,6 +3144,7 @@ def Emit.program(ds: Coll.List[Ast.Decl], res0: Ty.Result, opt: Emit.Opt): Unit
Emit.strPrims()
Emit.wordPrims()
Emit.ptrPrims(cs)
Emit.scanPrims(cs)
// cur, end: the free bytes of an Arena capability's head chunk (Emit.allocFast); null until its first alloc, and
// always null for other handlers
Console.print("typedef struct srs_cap { void* self; void* const* vt; int64_t live; char* cur; char* end; } srs_cap;\n")
Expand Down
2 changes: 1 addition & 1 deletion tests/s2/mem/binders.out

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion tests/s2/mem/borrow.out

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion tests/s2/mem/bytes.out

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion tests/s2/mem/compact.out

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion tests/s2/mem/contents.out

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion tests/s2/mem/cycle.out

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion tests/s2/mem/death.out

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion tests/s2/mem/frees.out

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion tests/s2/mem/inner.out

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion tests/s2/mem/many.out

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion tests/s2/mem/moved.out

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion tests/s2/mem/moves.out

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion tests/s2/mem/once.out

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion tests/s2/mem/reasons.out

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion tests/s2/mem/regions.out

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion tests/s2/mem/reuse.out

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion tests/s2/mem/reuse_region.out

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions tests/s2/prog/str_scan.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0 61226
60 changes: 60 additions & 0 deletions tests/s2/prog/str_scan.srs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// pass: Str.find and Str.runWhile agree with plain byte loops (Str.at) on 2000 pseudo-random strings and start indexes,
// edge starts (-3, 0, len, len + 5) and bytes >= the table length; prints the number of disagreements (0) and a checksum
def Sc.find(s: Str, i: I64, c: I64): I64 =
if i < 0 then Sc.find(s, 0, c)
else if i >= Str.len(s) then Str.len(s)
else if Str.at(s, i) == c then i
else Sc.find(s, i + 1, c)

def Sc.isWs(b: I64): Bool = b == 32 || b == 9 || b == 10

def Sc.run(s: Str, i: I64): I64 =
if i < 0 then Sc.run(s, 0)
else if i >= Str.len(s) then Str.len(s)
else if Sc.isWs(Str.at(s, i)) then Sc.run(s, i + 1)
else i

def Sc.mark(n: I64, t: Array[U8](n), i: I64): Unit =
if 0 <= i && i < n then
Array.put(t, i, U8.of(if Sc.isWs(i) then 1 else 0))
Sc.mark(n, t, i + 1)
else ()

def Sc.next(x: I64): I64 = (x * 1103515245 + 12345) % 2147483648

def Sc.fill(n: I64, xs: Array[U8](n), i: I64, x: I64): I64 =
if 0 <= i && i < n then
val r = Sc.next(x)
val k = r % 6
Array.put(xs, i, U8.of(if k == 0 then 32 else if k == 1 then 9 else if k == 2 then 10 else if k == 3 then 97 else if k == 4 then 200 else 122))
Sc.fill(n, xs, i + 1, r)
else x

def Sc.loop {r: Region} (k: I64, x: I64, tn: I64, t: Array[U8](tn), bad: I64, sum: I64): I64 =
if k <= 0 then
Console.printI64(bad)
Console.print(" ")
Console.printI64(sum)
Console.print("\n")
0
else
val n = x % 40
val xs = Array.fill(n, U8.of(0))
val x2 = Sc.fill(n, xs, 0, x)
val s = Str.ofBytes(n, xs)
val i = x2 % (n + 9) - 3
val c = if x2 % 3 == 0 then 97 else 122
val f1 = Str.find(s, i, c)
val w1 = Str.runWhile(s, i, tn, t)
val ok =
f1 == Sc.find(s, i, c) && w1 == Sc.run(s, i) && Str.find(s, n + 5, c) == n && Str.runWhile(s, n, tn, t) == n
Sc.loop(k - 1, Sc.next(x2), tn, t, if ok then bad else bad + 1, sum + f1 + w1)

def main(): I64 =
handle io = Srs.Console()
handle m = Srs.Mut()
handle r = Srs.Arena()
val tn = 128
val t = Array.fill(tn, U8.of(0))
Sc.mark(tn, t, 0)
Sc.loop(2000, 7, tn, t, 0, 0)
Loading