Skip to content

Commit fba2b44

Browse files
committed
s2c: --store-check (decls stored as printed text), escape through generic results, type facts (nominal fact types, stale reads); every s1 test covered in tests/s2
1 parent 362c4c1 commit fba2b44

16 files changed

Lines changed: 339 additions & 75 deletions

‎rt/srs_rt.c‎

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,11 +123,27 @@ srs_str rt_store_read(srs_str dir, int64_t a, int64_t b, int64_t c, int64_t d) {
123123
return rt_read_file((srs_str){(const uint8_t*)name, (int64_t)strlen(name)});
124124
}
125125

126-
int64_t rt_print(srs_str s) { fwrite(s.p, 1, (size_t)s.n, stdout); return 0; }
126+
// where rt_print writes: stdout, or the capture buffer between rt_capture_begin and rt_capture_end (a printer's output
127+
// as one string; the bytes live until the program ends)
128+
static FILE* g_out;
129+
static char* g_cap;
130+
static size_t g_cap_n;
131+
static FILE* out(void) { return g_out ? g_out : stdout; }
132+
int64_t rt_capture_begin(void) {
133+
g_out = open_memstream(&g_cap, &g_cap_n);
134+
if (!g_out) { fputs("srs: cannot capture output\n", stderr); exit(70); }
135+
return 0;
136+
}
137+
srs_str rt_capture_end(void) {
138+
if (!g_out || fclose(g_out) != 0) { fputs("srs: capture not open\n", stderr); exit(70); }
139+
g_out = 0;
140+
return (srs_str){(const uint8_t*)g_cap, (int64_t)g_cap_n};
141+
}
142+
int64_t rt_print(srs_str s) { fwrite(s.p, 1, (size_t)s.n, out()); return 0; }
127143
int64_t rt_eprint(srs_str s) { fwrite(s.p, 1, (size_t)s.n, stderr); return 0; }
128-
int64_t rt_print_i64(int64_t v) { printf("%lld", (long long)v); return 0; }
144+
int64_t rt_print_i64(int64_t v) { fprintf(out(), "%lld", (long long)v); return 0; }
129145
int64_t rt_eprint_i64(int64_t v) { fprintf(stderr, "%lld", (long long)v); return 0; }
130-
int64_t rt_print_char(int64_t c) { putchar((int)c); return 0; }
146+
int64_t rt_print_char(int64_t c) { fputc((int)c, out()); return 0; }
131147
int64_t rt_str_len(srs_str s) { return s.n; }
132148
int64_t rt_str_at(srs_str s, int64_t i) { return (i >= 0 && i < s.n) ? s.p[i] : -1; }
133149
srs_str rt_str_slice(srs_str s, int64_t b, int64_t e) {

‎s2/emit.srs‎

Lines changed: 37 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ enum Emit.Alloc:
1212

1313
// the declarations emit reads, by name
1414
enum Emit.Cx:
15-
case Emit.Cx(s: Coll.IMap[K.Term], fns: Coll.SMap[El.Fn], cases: Coll.SMap[Emit.CaseAt], enums: Coll.SMap[Ast.Enum], protos: Coll.SMap[Ast.Proto], hprotos: Coll.SMap[Sym.Sym], alloc: Emit.Alloc)
15+
case Emit.Cx(s: Coll.IMap[K.Term], fns: Coll.SMap[El.Fn], cases: Coll.SMap[Emit.CaseAt], enums: Coll.SMap[Ast.Enum], protos: Coll.SMap[Ast.Proto], hprotos: Coll.SMap[Sym.Sym], alloc: Emit.Alloc, nominal: Coll.SMap[K.Term])
1616

1717
// a C local: a param p_x, or a binding v_x_n (n = the number of locals bound before it)
1818
enum Emit.Name:
@@ -65,7 +65,7 @@ enum Emit.Part:
6565

6666
// ── the declarations by name ──
6767
def Emit.cxOf {r: Region} (ds: Coll.List[Ast.Decl], s: Coll.IMap[K.Term], fs: Coll.List[El.Fn]): Emit.Cx =
68-
Emit.cxDecls(ds, Emit.Cx(s, Emit.fnMap(fs, Coll.sEmpty()), Coll.sEmpty(), Coll.sEmpty(), Coll.sEmpty(), Coll.sEmpty(), Emit.VTable))
68+
Emit.cxDecls(ds, Emit.Cx(s, Emit.fnMap(fs, Coll.sEmpty()), Coll.sEmpty(), Coll.sEmpty(), Coll.sEmpty(), Coll.sEmpty(), Emit.VTable, Coll.sEmpty()))
6969

7070
def Emit.fnMap {r: Region} (fs: Coll.List[El.Fn], m: Coll.SMap[El.Fn]): Coll.SMap[El.Fn] = fs match
7171
case Coll.Nil => m
@@ -76,17 +76,23 @@ def Emit.fnMap {r: Region} (fs: Coll.List[El.Fn], m: Coll.SMap[El.Fn]): Coll.SMa
7676
def Emit.cxDecls {r: Region} (ds: Coll.List[Ast.Decl], cx: Emit.Cx): Emit.Cx = ds match
7777
case Coll.Nil => cx
7878
case Coll.Cons(d, rest) =>
79-
val Emit.Cx(s, fns, cs, es, ps, hp, al) = cx
79+
val Emit.Cx(s, fns, cs, es, ps, hp, al, nm) = cx
8080
d match
8181
case Ast.DEnum(e) =>
8282
val Ast.Enum(_, n, _, cases) = e
83-
Emit.cxDecls(rest, Emit.Cx(s, fns, Emit.caseMap(cases, e, 0, cs), Coll.sInsert(es, n, e), ps, hp, al))
83+
Emit.cxDecls(rest, Emit.Cx(s, fns, Emit.caseMap(cases, e, 0, cs), Coll.sInsert(es, n, e), ps, hp, al, nm))
8484
case Ast.DProto(p) =>
8585
val Ast.Proto(_, n, _, _, _, _, _, _) = p
86-
Emit.cxDecls(rest, Emit.Cx(s, fns, cs, es, Coll.sInsert(ps, n, p), hp, al))
86+
Emit.cxDecls(rest, Emit.Cx(s, fns, cs, es, Coll.sInsert(ps, n, p), hp, al, nm))
8787
case Ast.DHandler(h) =>
8888
val Ast.Handler(_, n, p, _) = h
89-
Emit.cxDecls(rest, Emit.Cx(s, fns, cs, es, ps, Coll.sInsert(hp, n, p), if rt_str_eq(Sym.str(n), "Arena") then Emit.Inline else al))
89+
Emit.cxDecls(rest, Emit.Cx(s, fns, cs, es, ps, Coll.sInsert(hp, n, p), if rt_str_eq(Sym.str(n), "Arena") then Emit.Inline else al, nm))
90+
// a type with <> facts stays nominal through typing (s2/tlevel.srs Tl.nominal): in C it is its body's type
91+
case Ast.DType(td) =>
92+
val Ast.TypeDecl(_, n, _, _, body) = td
93+
body match
94+
case Ast.Alias(t) => Emit.cxDecls(rest, Emit.Cx(s, fns, cs, es, ps, hp, al, Coll.sInsert(nm, n, Ty.term(t, Coll.Nil))))
95+
case _ => Emit.cxDecls(rest, cx)
9096
case _ => Emit.cxDecls(rest, cx)
9197

9298
def Emit.caseMap {r: Region} (cs: Coll.List[Ast.Case], e: Ast.Enum, i: I64, m: Coll.SMap[Emit.CaseAt]): Coll.SMap[Emit.CaseAt] = cs match
@@ -96,17 +102,17 @@ def Emit.caseMap {r: Region} (cs: Coll.List[Ast.Case], e: Ast.Enum, i: I64, m: C
96102
Emit.caseMap(rest, e, i + 1, Coll.sInsert(m, n, Emit.CaseAt(i, e, c)))
97103

98104
def Emit.isProto {r: Region} (cx: Emit.Cx, n: Sym.Sym): Bool =
99-
val Emit.Cx(_, _, _, _, ps, _, _) = cx
105+
val Emit.Cx(_, _, _, _, ps, _, _, _) = cx
100106
Coll.sLookup(ps, n) match
101107
case Coll.Some(_) => true
102108
case Coll.None => false
103109

104110
def Emit.caseAt {r: Region} (cx: Emit.Cx, c: Sym.Sym): Coll.Option[Emit.CaseAt] =
105-
val Emit.Cx(_, _, cs, _, _, _, _) = cx
111+
val Emit.Cx(_, _, cs, _, _, _, _, _) = cx
106112
Coll.sLookup(cs, c)
107113

108114
def Emit.subst(cx: Emit.Cx): Coll.IMap[K.Term] =
109-
val Emit.Cx(s, _, _, _, _, _, _) = cx
115+
val Emit.Cx(s, _, _, _, _, _, _, _) = cx
110116
s
111117

112118
def Emit.grAll {r: Region} (cx: Emit.Cx, fi: Emit.Fi, ts: Coll.List[K.Term]): Coll.List[K.Term] =
@@ -201,12 +207,18 @@ def Emit.ctype {r: Region} (cx: Emit.Cx, t: K.Term): Unit = t match
201207
else if rt_str_eq(x, "Str") then rt_print("srs_str")
202208
else if rt_str_eq(x, "Ptr") then rt_print("void*")
203209
else if Emit.isProto(cx, n) then rt_print("srs_cap*")
204-
else
205-
rt_print("struct E_")
206-
Emit.enumName(n, args)
207-
rt_print("*")
210+
else Emit.nominalOf(cx, n) match
211+
case Coll.Some(b) => Emit.ctype(cx, b)
212+
case Coll.None =>
213+
rt_print("struct E_")
214+
Emit.enumName(n, args)
215+
rt_print("*")
208216
case _ => rt_print("int64_t")
209217

218+
def Emit.nominalOf {r: Region} (cx: Emit.Cx, n: Sym.Sym): Coll.Option[K.Term] =
219+
val Emit.Cx(_, _, _, _, _, _, _, nm) = cx
220+
Coll.sLookup(nm, n)
221+
210222
// ── expressions (spec sec 4) ──
211223
def Emit.ex {r: Region} (cx: Emit.Cx, fi: Emit.Fi, env: Emit.Env, pos: Emit.Pos, e: El.Expr): Unit = e match
212224
case El.Int(_, v) =>
@@ -327,7 +339,7 @@ def Emit.isSelf(fi: Emit.Fi, f: Sym.Sym, ts: Coll.List[K.Term]): Bool =
327339
Sym.eq(f, self) && Mono.termsEq(ts, sts)
328340

329341
def Emit.fnName {r: Region} (cx: Emit.Cx, f: Sym.Sym, ts: Coll.List[K.Term]): Unit =
330-
val Emit.Cx(_, fns, _, _, _, _, _) = cx
342+
val Emit.Cx(_, fns, _, _, _, _, _, _) = cx
331343
Coll.sLookup(fns, f) match
332344
case Coll.Some(g) =>
333345
val El.Fn(_, _, _, _, _, _, body) = g
@@ -412,7 +424,7 @@ def Emit.ctor {r: Region} (cx: Emit.Cx, fi: Emit.Fi, env: Emit.Env, en: Sym.Sym,
412424
case Coll.Some(ca) =>
413425
val Emit.CaseAt(tag, _, cs) = ca
414426
val Ast.Case(_, _, fields) = cs
415-
val Emit.Cx(_, _, _, _, _, _, al) = cx
427+
val Emit.Cx(_, _, _, _, _, _, al, _) = cx
416428
val n = Emit.envN(env)
417429
rt_print("({ struct E_")
418430
Emit.enumName(en, ts)
@@ -469,7 +481,7 @@ def Emit.fieldSets {r: Region} (cx: Emit.Cx, fi: Emit.Fi, env: Emit.Env, n: I64,
469481

470482
// the index of op in protocol p's op list
471483
def Emit.opIndex {r: Region} (cx: Emit.Cx, p: Sym.Sym, op: Sym.Sym): I64 =
472-
val Emit.Cx(_, _, _, _, ps, _, _) = cx
484+
val Emit.Cx(_, _, _, _, ps, _, _, _) = cx
473485
Coll.sLookup(ps, p) match
474486
case Coll.None => 0
475487
case Coll.Some(pr) =>
@@ -486,7 +498,7 @@ def Emit.opIn(os: Coll.List[Ast.Op], op: Sym.Sym, i: I64): I64 = os match
486498
// trait method of {} evidence: the given's static fn (rung T2)
487499
def Emit.op {r: Region} (cx: Emit.Cx, fi: Emit.Fi, env: Emit.Env, recv: Sym.Sym, ty: K.Term, op: Sym.Sym, args: Coll.List[El.Expr]): Unit = ty match
488500
case K.Con(pn, targs) =>
489-
val Emit.Cx(_, _, _, _, ps, _, _) = cx
501+
val Emit.Cx(_, _, _, _, ps, _, _, _) = cx
490502
Coll.sLookup(ps, pn) match
491503
case Coll.Some(p) =>
492504
val Ast.Proto(_, _, _, _, ends, _, ops, _) = p
@@ -650,7 +662,7 @@ def Emit.drops {r: Region} (cx: Emit.Cx, ds: Coll.List[Emit.Drop]): Unit = ds ma
650662
case Coll.Nil => ()
651663
case Coll.Cons(d, rest) =>
652664
val Emit.Drop(v, p) = d
653-
val Emit.Cx(_, _, _, _, ps, _, _) = cx
665+
val Emit.Cx(_, _, _, _, ps, _, _, _) = cx
654666
Coll.sLookup(ps, p) match
655667
case Coll.None => ()
656668
case Coll.Some(pr) =>
@@ -671,7 +683,7 @@ def Emit.drops {r: Region} (cx: Emit.Cx, ds: Coll.List[Emit.Drop]): Unit = ds ma
671683

672684
// the protocol of handler h
673685
def Emit.protoOfH {r: Region} (cx: Emit.Cx, h: Sym.Sym): Sym.Sym =
674-
val Emit.Cx(_, _, _, _, _, hp, _) = cx
686+
val Emit.Cx(_, _, _, _, _, hp, _, _) = cx
675687
Coll.sLookup(hp, h) match
676688
case Coll.Some(p) => p
677689
case Coll.None => h
@@ -836,7 +848,7 @@ def Emit.closeEnums {r: Region} (cx: Emit.Cx, todo: Coll.List[K.Term], seen: Col
836848
case Coll.Nil => Coll.rev(out)
837849
case Coll.Cons(t, rest) => t match
838850
case K.Con(n, args) =>
839-
val Emit.Cx(_, _, _, es, _, _, _) = cx
851+
val Emit.Cx(_, _, _, es, _, _, _, _) = cx
840852
val h = Mono.thash(t, 0)
841853
Coll.sLookup(es, n) match
842854
case Coll.None => Emit.closeEnums(cx, rest, seen, out)
@@ -894,7 +906,7 @@ def Emit.statics {r: Region} (cx: Emit.Cx, ts: Coll.List[K.Term]): Unit = ts mat
894906
// the struct (Layout) or the nullary statics (Code) of enum term t
895907
def Emit.withEnum {r: Region} (cx: Emit.Cx, t: K.Term, what: Emit.Part): Unit = t match
896908
case K.Con(n, args) =>
897-
val Emit.Cx(_, _, _, es, _, _, _) = cx
909+
val Emit.Cx(_, _, _, es, _, _, _, _) = cx
898910
Coll.sLookup(es, n) match
899911
case Coll.None => ()
900912
case Coll.Some(e) =>
@@ -971,7 +983,7 @@ def Emit.ptrs {r: Region} (tps: Coll.List[Sym.Sym], acc: Coll.List[Ty.Bind]): Co
971983

972984
// a generic extern whose result is a [] binder returns void*: the call casts it to the instance's type (spec sec 4 XInst)
973985
def Emit.externCast {r: Region} (cx: Emit.Cx, f: Sym.Sym, gts: Coll.List[K.Term]): Coll.Option[K.Term] =
974-
val Emit.Cx(_, fns, _, _, _, _, _) = cx
986+
val Emit.Cx(_, fns, _, _, _, _, _, _) = cx
975987
Coll.sLookup(fns, f) match
976988
case Coll.Some(g) =>
977989
val El.Fn(_, _, tps, _, _, ret, body) = g
@@ -1028,7 +1040,7 @@ def Emit.vtables {r: Region} (cx: Emit.Cx, hs: Coll.List[El.Handler]): Unit = hs
10281040
case Coll.Nil => ()
10291041
case Coll.Cons(h, rest) =>
10301042
val El.Handler(hn, p, _) = h
1031-
val Emit.Cx(_, _, _, _, ps, _, _) = cx
1043+
val Emit.Cx(_, _, _, _, ps, _, _, _) = cx
10321044
rt_print("static void* const vt_")
10331045
Emit.id(hn)
10341046
rt_print("[] = {")
@@ -1067,7 +1079,7 @@ def Emit.allocFast {r: Region} (cx: Emit.Cx): Unit =
10671079
rt_print("}\n")
10681080

10691081
def Emit.hasMain {r: Region} (cx: Emit.Cx): Bool =
1070-
val Emit.Cx(_, fns, _, _, _, _, _) = cx
1082+
val Emit.Cx(_, fns, _, _, _, _, _, _) = cx
10711083
Coll.sLookup(fns, Sym.of("main")) match
10721084
case Coll.Some(_) => true
10731085
case Coll.None => false
@@ -1090,7 +1102,7 @@ def Emit.program {r: Region} (ds: Coll.List[Ast.Decl], res: Ty.Result): Unit =
10901102
Emit.protos(cx, is)
10911103
Emit.handlers(cx, hs, Emit.Layout)
10921104
Emit.vtables(cx, hs)
1093-
val Emit.Cx(_, _, _, _, _, _, al) = cx
1105+
val Emit.Cx(_, _, _, _, _, _, al, _) = cx
10941106
al match
10951107
case Emit.Inline => Emit.allocFast(cx)
10961108
case Emit.VTable => ()

0 commit comments

Comments
 (0)