ww: implement package initialization
This commit is contained in:
@@ -3,8 +3,9 @@ package wcc;
|
||||
import syntax;
|
||||
|
||||
export fn compilefile(file: *syntax.node, testmode: i32, testpackage: i32,
|
||||
testmodule: str, testtarget: str, sepmode: i32, wwiout: str,
|
||||
entrymode: i32) i32 = {
|
||||
testmodule: str, testtarget: str, sepmode: i32, wwifd: i32,
|
||||
haswwi: i32,
|
||||
entrymode: i32, packageinitsymbol: str, initdispatchsymbol: str) i32 = {
|
||||
let tc: syntax.tctx;
|
||||
syntax.typesinit(&tc);
|
||||
let ck: checker;
|
||||
@@ -14,18 +15,19 @@ export fn compilefile(file: *syntax.node, testmode: i32, testpackage: i32,
|
||||
if (testmodule.len > 0) { ck.testmodule = testmodule; };
|
||||
ck.testtarget = testtarget;
|
||||
ck.sepmode = sepmode;
|
||||
ck.packageinitsymbol = packageinitsymbol;
|
||||
checkfile(&ck, file);
|
||||
if (ck.errs > 0) { return 1; };
|
||||
if (wwiout.len > 0) {
|
||||
if (wwiemit(&ck, file, wwiout) != 0) { return 1; };
|
||||
if (haswwi != 0) {
|
||||
if (wwiemitfd(&ck, file, wwifd) != 0) { return 1; };
|
||||
};
|
||||
|
||||
let cg: cgen;
|
||||
cgeninit(&cg);
|
||||
cg.sepmode = sepmode;
|
||||
if (wwiout.len > 0 && entrymode == 0) { cg.sepisdep = 1i32; };
|
||||
cgfile(&cg, file);
|
||||
return 0;
|
||||
if (haswwi != 0 && entrymode == 0) { cg.sepisdep = 1i32; };
|
||||
cg.initdispatchsymbol = initdispatchsymbol;
|
||||
return cgfile(&cg, file);
|
||||
};
|
||||
|
||||
export fn resolvefile(file: *syntax.node, verbose: i32,
|
||||
|
||||
@@ -509,6 +509,7 @@ type cgen = struct {
|
||||
// root entry stays bare. Like sepmode, NOT reset by cgeninit (per-fn).
|
||||
// Symmetric with cstage Cg.sep_isdep.
|
||||
sepisdep: i32,
|
||||
initdispatchsymbol: str, // root-owned complete package-init schedule
|
||||
};
|
||||
|
||||
// Top-level mutable `let` registry. Mirrors cmd/w6c/cgen.c LetVar.
|
||||
@@ -741,6 +742,17 @@ fn localfind(c: *cgen, name: str) i32 = {
|
||||
let cgoutstream: memio.stream;
|
||||
let cgoutmode: i32 = 0;
|
||||
let cgoutinit: i32 = 0;
|
||||
let cgwritefailed: i32 = 0;
|
||||
|
||||
fn cgwrite(p: *u8, n: u64) void = {
|
||||
if (cgwritefailed != 0) { return; };
|
||||
match (os.writeall(1i32, p, n)) {
|
||||
case let wrote: i64 => {
|
||||
if (wrote < 0 || (wrote: u64) != n) { cgwritefailed = 1; };
|
||||
};
|
||||
case let e: os.oserror => cgwritefailed = 1;
|
||||
};
|
||||
};
|
||||
|
||||
fn cgout_enable() void = {
|
||||
if (cgoutinit == 0) {
|
||||
@@ -754,7 +766,7 @@ fn cgout_disable() void = { cgoutmode = 0; };
|
||||
|
||||
fn cgout_flush() void = {
|
||||
if (cgoutstream.pos > 0) {
|
||||
os.write(1, cgoutstream.ptr, cgoutstream.pos: u64);
|
||||
cgwrite(cgoutstream.ptr, cgoutstream.pos: u64);
|
||||
memio.reset(&cgoutstream);
|
||||
};
|
||||
};
|
||||
@@ -769,7 +781,7 @@ fn emitbytes(p: *u8, n: u64) void = {
|
||||
// lib/log/log.ww stdprintln. #94 fold-eFinal.
|
||||
io.write(&cgoutstream.vt, buf);
|
||||
} else {
|
||||
os.write(1, p, n);
|
||||
cgwrite(p, n);
|
||||
};
|
||||
};
|
||||
|
||||
@@ -2894,6 +2906,35 @@ fn emittupledata(c: *cgen, name: str, module: str, tt: *syntax.node, rhs: *synta
|
||||
return true;
|
||||
};
|
||||
|
||||
fn emitinitbackings(n: *syntax.node) void = {
|
||||
for (n != nil) {
|
||||
if (n.kind == syntax.nkind.N_ARRLIT && n.linksym.len > 0) {
|
||||
let u: *syntax.tinfo = tichase(n.type_: *syntax.tinfo);
|
||||
if (u == nil || u.kind != syntax.tykind.TY_ARRAY) {
|
||||
let msg: str = "runtime package slice backing has no array type\n";
|
||||
os.write(2, msg.ptr, msg.len: u64);
|
||||
os.exit(1);
|
||||
};
|
||||
emitline("DATAW ");
|
||||
emitbytes(n.linksym.ptr, n.linksym.len: u64);
|
||||
emitline("(SB),\"");
|
||||
let count: u64 = u.size;
|
||||
if (count == 0u64) { count = 1u64; };
|
||||
let i: u64 = 0u64;
|
||||
for (i < count) { emitdatawbyte(0u8); i += 1u64; };
|
||||
emitline("\"\n");
|
||||
};
|
||||
emitinitbackings(n.attr);
|
||||
emitinitbackings(n.lhs);
|
||||
emitinitbackings(n.rhs);
|
||||
emitinitbackings(n.cond);
|
||||
emitinitbackings(n.body);
|
||||
emitinitbackings(n.els);
|
||||
emitinitbackings(n.list);
|
||||
n = n.next;
|
||||
};
|
||||
};
|
||||
|
||||
fn emitletdataw(c: *cgen, file: *syntax.node) void = {
|
||||
let savedmod: str = c.curmod;
|
||||
let savedsource: i32 = c.cursource;
|
||||
@@ -3458,6 +3499,10 @@ fn collectfnrets(c: *cgen, file: *syntax.node) void = {
|
||||
let d: *syntax.node = file.list;
|
||||
for (d != nil) {
|
||||
if (d.kind == syntax.nkind.N_FNDECL) {
|
||||
if (d.initfn != 0 || d.initsynthetic != 0) {
|
||||
d = d.next;
|
||||
continue;
|
||||
};
|
||||
let f: *fnret = alloc(fnret{fname=d.str, fmod=d.nmod, rtype=d.lhs, params=d.list, frnext=c.fnrets})!;
|
||||
c.fnrets = f;
|
||||
};
|
||||
@@ -3788,12 +3833,16 @@ fn collectmods(c: *cgen, file: *syntax.node) void = {
|
||||
let d: *syntax.node = file.list;
|
||||
for (d != nil) {
|
||||
// M1 #22: record alias→path for the qualified-ref hint.
|
||||
if (d.kind == syntax.nkind.N_USE) {
|
||||
if (d.kind == syntax.nkind.N_USE && d.useblank == 0) {
|
||||
if (d.usepath.len > 0) {
|
||||
let um: *modent = alloc(modent{mname=d.str, nmod=d.usepath, omod=d.nmod, sourceid=d.sourceid, mnext=c.uses})!;
|
||||
c.uses = um;
|
||||
};
|
||||
};
|
||||
if (d.initfn != 0 || d.initsynthetic != 0) {
|
||||
d = d.next;
|
||||
continue;
|
||||
};
|
||||
// Mirror collectfnrets' shape exactly (plain prepend in one
|
||||
// branch). Earlier nested-if/early-return variants tickled a
|
||||
// wwstage cgen bug that dropped most prepends.
|
||||
@@ -3988,6 +4037,10 @@ fn fficollect(c: *cgen, file: *syntax.node) void = {
|
||||
let d: *syntax.node = file.list;
|
||||
for (d != nil) {
|
||||
if (d.kind == syntax.nkind.N_FNDECL) {
|
||||
if (d.initfn != 0 || d.initsynthetic != 0) {
|
||||
d = d.next;
|
||||
continue;
|
||||
};
|
||||
let a: *syntax.node = d.attr;
|
||||
for (a != nil) {
|
||||
if (a.kind == syntax.nkind.N_ATTR) {
|
||||
|
||||
@@ -544,6 +544,12 @@ fn cgfn(c: *cgen, fn_: *syntax.node) void = {
|
||||
};
|
||||
|
||||
cgfnparams(c, fn_.list);
|
||||
if (c.initdispatchsymbol.len > 0 && syntax.streq(fn_.str, "main")
|
||||
&& fn_.imported == 0 && c.sepisdep == 0) {
|
||||
emitline("\tCALL\t");
|
||||
emitbytes(c.initdispatchsymbol.ptr, c.initdispatchsymbol.len: u64);
|
||||
emitline("(SB)\n");
|
||||
};
|
||||
c.lastwasreturn = 0;
|
||||
// Iterate the fn body's statements directly rather than dispatching
|
||||
// the outermost N_BLOCK through cgstmt — cgblock now save/restores
|
||||
@@ -594,11 +600,13 @@ fn cgfn(c: *cgen, fn_: *syntax.node) void = {
|
||||
// (path-carrying `//ww:module-reset`, #57); only the root/link-entry
|
||||
// unit (wwiout==nil, #69) keeps the bare label.
|
||||
emitline("TEXT ");
|
||||
if (syntax.streq(fn_.str, "main") && fn_.imported == 0 && c.sepisdep == 0) {
|
||||
if (fn_.linksym.len > 0) {
|
||||
emitbytes(fn_.linksym.ptr, fn_.linksym.len: u64);
|
||||
} else { if (syntax.streq(fn_.str, "main") && fn_.imported == 0 && c.sepisdep == 0) {
|
||||
emitbytes(fn_.str.ptr, fn_.str.len: u64);
|
||||
} else {
|
||||
emitfnname(c, fn_.str, fn_.nmod);
|
||||
};
|
||||
}; };
|
||||
emitline(",$");
|
||||
emitint(frame: i64);
|
||||
emitline("\n");
|
||||
@@ -612,8 +620,9 @@ fn cgfn(c: *cgen, fn_: *syntax.node) void = {
|
||||
cgout_flush();
|
||||
};
|
||||
|
||||
fn cgfile(c: *cgen, file: *syntax.node) void = {
|
||||
if (file == nil) { return; };
|
||||
fn cgfile(c: *cgen, file: *syntax.node) i32 = {
|
||||
cgwritefailed = 0;
|
||||
if (file == nil) { return 0; };
|
||||
c.strlits = nil;
|
||||
c.strlitseq = 0;
|
||||
collectaliases(c, file);
|
||||
@@ -647,5 +656,7 @@ fn cgfile(c: *cgen, file: *syntax.node) void = {
|
||||
letpreintern(c, file);
|
||||
emitdatasection(c);
|
||||
emitdefconstants(c, file);
|
||||
emitinitbackings(file.list);
|
||||
emitletdataw(c, file);
|
||||
return cgwritefailed;
|
||||
};
|
||||
|
||||
@@ -8698,7 +8698,11 @@ fn cgcall(c: *cgen, n: *syntax.node) void = {
|
||||
emitline("\tCALL\tAX\n");
|
||||
} else {
|
||||
emitline("\tCALL\t");
|
||||
if (callee != nil) {
|
||||
if (callee != nil && callee.refdecl != nil
|
||||
&& callee.refdecl.linksym.len > 0) {
|
||||
emitbytes(callee.refdecl.linksym.ptr,
|
||||
callee.refdecl.linksym.len: u64);
|
||||
} else { if (callee != nil) {
|
||||
if (callee.kind == syntax.nkind.N_IDENT) {
|
||||
// Bare `f()` — same-module by ww's resolver,
|
||||
// so c.curmod is the disambiguation hint.
|
||||
@@ -8718,7 +8722,7 @@ fn cgcall(c: *cgen, n: *syntax.node) void = {
|
||||
};
|
||||
emitfnname(c, calleename, hint);
|
||||
};};
|
||||
};
|
||||
}; };
|
||||
emitline("(SB)\n");
|
||||
};
|
||||
// Caller cleanup for stack-passed args (args 7+, or any
|
||||
|
||||
@@ -2173,6 +2173,284 @@ fn cgarrlitfillbp(c: *cgen, arrtn: *syntax.node, rhs: *syntax.node, off: i32) vo
|
||||
};
|
||||
};
|
||||
|
||||
// Compiler-generated package-variable helpers need a memory-directed literal
|
||||
// path. It bypasses the finite tuple cursor, recursively fills nested
|
||||
// aggregates, and moves runtime slice-literal backing into canonical writable
|
||||
// package storage before the helper returns. The hook is gated solely by the
|
||||
// checker-owned N_LET.initsynthetic bit, so ordinary locals are unchanged.
|
||||
fn cginitfatal(msg: str) void = {
|
||||
os.write(2, msg.ptr, msg.len: u64);
|
||||
os.write(2, "\n".ptr, 1u64);
|
||||
os.exit(1);
|
||||
};
|
||||
|
||||
fn cginitzerobp(c: *cgen, off: i32, sz: i32) void = {
|
||||
emitline("\tXORQ\tAX, AX\n");
|
||||
let k: i32 = 0;
|
||||
for (k + 8 <= sz) {
|
||||
emitline("\tMOVQ\tAX, "); emitoff((off + k): i64);
|
||||
emitline("(BP)\n"); k += 8;
|
||||
};
|
||||
if (k + 4 <= sz) {
|
||||
emitline("\tMOVL\tAX, "); emitoff((off + k): i64);
|
||||
emitline("(BP)\n"); k += 4;
|
||||
};
|
||||
if (k + 2 <= sz) {
|
||||
emitline("\tMOVW\tAX, "); emitoff((off + k): i64);
|
||||
emitline("(BP)\n"); k += 2;
|
||||
};
|
||||
if (k + 1 <= sz) {
|
||||
emitline("\tMOVB\tAX, "); emitoff((off + k): i64);
|
||||
emitline("(BP)\n");
|
||||
};
|
||||
};
|
||||
|
||||
fn cginitcopybp(c: *cgen, src: i32, dst: i32, sz: i32) void = {
|
||||
emitline("\tLEAQ\t"); emitoff(src: i64); emitline("(BP), SI\n");
|
||||
emitline("\tLEAQ\t"); emitoff(dst: i64); emitline("(BP), BX\n");
|
||||
aggcopy(c, sz);
|
||||
};
|
||||
|
||||
fn cginitstripcast(n: *syntax.node) *syntax.node = {
|
||||
for (n != nil && n.kind == syntax.nkind.N_CAST) { n = n.lhs; };
|
||||
return n;
|
||||
};
|
||||
|
||||
fn cginitfield(u: *syntax.tinfo, name: str) *syntax.tfield = {
|
||||
let f: *syntax.tfield = nil;
|
||||
if (u != nil) { f = u.fields; };
|
||||
for (f != nil) {
|
||||
if (syntax.streq(name, f.name)) { return f; };
|
||||
f = f.tnext;
|
||||
};
|
||||
return nil;
|
||||
};
|
||||
|
||||
fn cginitarraybp(c: *cgen, u: *syntax.tinfo, lit: *syntax.node,
|
||||
off: i32) void = {
|
||||
let et: *syntax.tinfo = u.sub;
|
||||
let eu: *syntax.tinfo = tichase(et);
|
||||
let esz: i32 = 1;
|
||||
if (eu != nil) { esz = eu.size: i32; };
|
||||
let total: i32 = u.alen: i32;
|
||||
cginitzerobp(c, off, u.size: i32);
|
||||
let idx: i32 = 0;
|
||||
let lastoff: i32 = 0;
|
||||
let e: *syntax.node = lit.list;
|
||||
for (e != nil) {
|
||||
if (e.kind == syntax.nkind.N_FIELD
|
||||
&& syntax.streq(e.str, "...")) {
|
||||
if (idx == 0) {
|
||||
cginitfatal("package initializer array repeat has no value");
|
||||
};
|
||||
for (idx < total) {
|
||||
cginitcopybp(c, lastoff, off + idx * esz, esz);
|
||||
idx += 1;
|
||||
};
|
||||
return;
|
||||
};
|
||||
if (idx >= total) {
|
||||
cginitfatal("package initializer array literal exceeds destination");
|
||||
};
|
||||
lastoff = off + idx * esz;
|
||||
cginitvaluebp(c, et, e, lastoff);
|
||||
idx += 1;
|
||||
e = e.next;
|
||||
};
|
||||
};
|
||||
|
||||
fn cginitstructbp(c: *cgen, u: *syntax.tinfo, lit: *syntax.node,
|
||||
off: i32) void = {
|
||||
cginitzerobp(c, off, u.size: i32);
|
||||
let e: *syntax.node = lit.list;
|
||||
for (e != nil) {
|
||||
let f: *syntax.tfield = cginitfield(u, e.str);
|
||||
if (f != nil) {
|
||||
cginitvaluebp(c, f.type_, e.lhs, off + (f.offset: i32));
|
||||
};
|
||||
e = e.next;
|
||||
};
|
||||
};
|
||||
|
||||
fn cginittuplebp(c: *cgen, u: *syntax.tinfo, lit: *syntax.node,
|
||||
off: i32) void = {
|
||||
cginitzerobp(c, off, u.size: i32);
|
||||
let te: *syntax.ttupleelem = u.tupleelems;
|
||||
let e: *syntax.node = lit.list;
|
||||
for (te != nil && e != nil) {
|
||||
cginitvaluebp(c, te.type_, e, off + (te.offset: i32));
|
||||
te = te.tnext;
|
||||
e = e.next;
|
||||
};
|
||||
};
|
||||
|
||||
fn cginitslicebp(c: *cgen, u: *syntax.tinfo, lit: *syntax.node,
|
||||
off: i32) void = {
|
||||
if (lit.linksym.len == 0) {
|
||||
cginitfatal("runtime package slice literal has no canonical backing");
|
||||
};
|
||||
let at: *syntax.tinfo = tichase(lit.type_: *syntax.tinfo);
|
||||
if (at == nil || at.kind != syntax.tykind.TY_ARRAY || at.sub == nil) {
|
||||
cginitfatal("runtime package slice literal has no backing type");
|
||||
};
|
||||
let count: i32 = at.alen: i32;
|
||||
let bsz: i32 = at.size: i32;
|
||||
if (bsz != 0) {
|
||||
let scr: i32 = localalloc(c, "@initbacking", bsz, nil);
|
||||
cginitarraybp(c, at, lit, scr);
|
||||
emitline("\tLEAQ\t"); emitoff(scr: i64); emitline("(BP), SI\n");
|
||||
emitline("\tLEAQ\t"); emitbytes(lit.linksym.ptr,
|
||||
lit.linksym.len: u64); emitline("(SB), BX\n");
|
||||
aggcopy(c, bsz);
|
||||
};
|
||||
emitline("\tLEAQ\t"); emitbytes(lit.linksym.ptr,
|
||||
lit.linksym.len: u64); emitline("(SB), AX\n");
|
||||
emitline("\tMOVQ\tAX, "); emitoff(off: i64); emitline("(BP)\n");
|
||||
emitline("\tMOVQ\t$"); emitint(count: i64); emitline(", ");
|
||||
emitoff((off + 8): i64); emitline("(BP)\n");
|
||||
emitline("\tMOVQ\t$"); emitint(count: i64); emitline(", ");
|
||||
emitoff((off + 16): i64); emitline("(BP)\n");
|
||||
};
|
||||
|
||||
fn cginitcursorstore(c: *cgen, ti: *syntax.tinfo, gpcur: i32,
|
||||
ssecur: i32, off: i32) void = {
|
||||
let eslot: i32 = tupeslot(ti);
|
||||
if (eslot == 0) { return; };
|
||||
if (eslot > 8) {
|
||||
let k: i32 = 0;
|
||||
for (k < eslot / 8) {
|
||||
emitline("\tMOVQ\t"); emitline(tupreg(gpcur + k));
|
||||
emitline(", "); emitoff((off + k * 8): i64);
|
||||
emitline("(BP)\n"); k += 1;
|
||||
};
|
||||
return;
|
||||
};
|
||||
if (syntax.typeisfloat(ti)) {
|
||||
let op: str = "MOVSD";
|
||||
if (syntax.typeisf32(ti)) { op = "MOVSS"; };
|
||||
emitline("\t"); emitline(op); emitline("\t");
|
||||
emitline(tupsse(ssecur)); emitline(", ");
|
||||
emitoff(off: i64); emitline("(BP)\n");
|
||||
return;
|
||||
};
|
||||
emitline("\tMOVQ\t"); emitline(tupreg(gpcur)); emitline(", ");
|
||||
emitoff(off: i64); emitline("(BP)\n");
|
||||
};
|
||||
|
||||
fn cginitaggregatebp(c: *cgen, ti: *syntax.tinfo, u: *syntax.tinfo,
|
||||
expr: *syntax.node, off: i32) void = {
|
||||
let sz: i32 = u.size: i32;
|
||||
if (expr.kind == syntax.nkind.N_CALL && sretretsizetn(c, ti) > 0) {
|
||||
c.sretdestoff = off;
|
||||
cgexpr(c, expr);
|
||||
c.sretdestoff = 0;
|
||||
return;
|
||||
};
|
||||
if (u.kind == syntax.tykind.TY_TUPLE && sretretsizetn(c, ti) == 0) {
|
||||
cgexpr(c, expr);
|
||||
let gpcur: i32 = 0;
|
||||
let ssecur: i32 = 0;
|
||||
let te: *syntax.ttupleelem = u.tupleelems;
|
||||
for (te != nil) {
|
||||
cginitcursorstore(c, te.type_, gpcur, ssecur,
|
||||
off + (te.offset: i32));
|
||||
if (syntax.typeisfloat(te.type_)) { ssecur += 1; }
|
||||
else { gpcur += tupeslot(te.type_) / 8; };
|
||||
te = te.tnext;
|
||||
};
|
||||
return;
|
||||
};
|
||||
if (expr.kind == syntax.nkind.N_CALL
|
||||
&& u.kind == syntax.tykind.TY_STRUCT) {
|
||||
let fc: i32 = structfloatclassti(u);
|
||||
if (fc != 0) {
|
||||
cgexpr(c, expr);
|
||||
let nb: i32 = fc & 15;
|
||||
let gp: i32 = 0;
|
||||
let sse: i32 = 0;
|
||||
let i: i32 = 0;
|
||||
for (i < nb) {
|
||||
let issse: bool = false;
|
||||
if (i == 0 && (fc & 16) != 0) { issse = true; };
|
||||
if (i == 1 && (fc & 32) != 0) { issse = true; };
|
||||
if (issse) {
|
||||
emitline("\tMOVSD\t"); emitline(tupsse(sse));
|
||||
emitline(", "); sse += 1;
|
||||
} else {
|
||||
emitline("\tMOVQ\t"); emitline(tupreg(gp));
|
||||
emitline(", "); gp += 1;
|
||||
};
|
||||
emitoff((off + i * 8): i64); emitline("(BP)\n");
|
||||
i += 1;
|
||||
};
|
||||
return;
|
||||
};
|
||||
};
|
||||
if (expr.kind == syntax.nkind.N_CALL && sz <= 24) {
|
||||
cgexpr(c, expr);
|
||||
cgaggregstore(c, "BP", off, sz, true);
|
||||
return;
|
||||
};
|
||||
if (aggargsrcaddr(c, expr, "SI")) {
|
||||
emitline("\tLEAQ\t"); emitoff(off: i64); emitline("(BP), BX\n");
|
||||
aggcopy(c, sz);
|
||||
return;
|
||||
};
|
||||
cginitfatal("package initializer aggregate expression shape unsupported");
|
||||
};
|
||||
|
||||
fn cginitvaluebp(c: *cgen, ti: *syntax.tinfo, expr: *syntax.node,
|
||||
off: i32) void = {
|
||||
let u: *syntax.tinfo = tichase(ti);
|
||||
let r: *syntax.node = cginitstripcast(expr);
|
||||
if (u == nil || r == nil) {
|
||||
cginitfatal("package initializer value has no type or expression");
|
||||
};
|
||||
if (u.kind == syntax.tykind.TY_ARRAY
|
||||
&& r.kind == syntax.nkind.N_ARRLIT) {
|
||||
cginitarraybp(c, u, r, off); return;
|
||||
};
|
||||
if (u.kind == syntax.tykind.TY_STRUCT
|
||||
&& r.kind == syntax.nkind.N_STRUCTLIT) {
|
||||
cginitstructbp(c, u, r, off); return;
|
||||
};
|
||||
if (u.kind == syntax.tykind.TY_TUPLE
|
||||
&& r.kind == syntax.nkind.N_TUPLE) {
|
||||
cginittuplebp(c, u, r, off); return;
|
||||
};
|
||||
if (u.kind == syntax.tykind.TY_SLICE
|
||||
&& r.kind == syntax.nkind.N_ARRLIT) {
|
||||
cginitslicebp(c, u, r, off); return;
|
||||
};
|
||||
if (u.kind == syntax.tykind.TY_TAGGED) {
|
||||
cgwidentaggedstore(c, u, expr, "BP", off, u.size: i32);
|
||||
return;
|
||||
};
|
||||
if (syntax.typeisstr(ti) || u.kind == syntax.tykind.TY_SLICE) {
|
||||
cgexpr(c, expr);
|
||||
emitline("\tMOVQ\tAX, "); emitoff(off: i64); emitline("(BP)\n");
|
||||
emitline("\tMOVQ\tBX, "); emitoff((off + 8): i64); emitline("(BP)\n");
|
||||
emitline("\tMOVQ\tCX, "); emitoff((off + 16): i64); emitline("(BP)\n");
|
||||
return;
|
||||
};
|
||||
if (u.kind == syntax.tykind.TY_ARRAY
|
||||
|| u.kind == syntax.tykind.TY_STRUCT
|
||||
|| u.kind == syntax.tykind.TY_TUPLE) {
|
||||
cginitaggregatebp(c, ti, u, r, off); return;
|
||||
};
|
||||
cgexpr(c, expr);
|
||||
if (syntax.typeisfloat(ti)) {
|
||||
let op: str = "MOVSD";
|
||||
if (syntax.typeisf32(ti)) { op = "MOVSS"; };
|
||||
emitline("\t"); emitline(op); emitline("\tX0, ");
|
||||
emitoff(off: i64); emitline("(BP)\n"); return;
|
||||
};
|
||||
let sz: i32 = u.size: i32;
|
||||
if (sz != 1 && sz != 2 && sz != 4) { sz = 8; };
|
||||
emitline("\t"); emitline(storeopsz(sz)); emitline("\tAX, ");
|
||||
emitoff(off: i64); emitline("(BP)\n");
|
||||
};
|
||||
|
||||
// #152: reserve the let's frame slot, emit its initializer against the
|
||||
// PRE-binding locals chain, then link the binding. A self-shadowing init
|
||||
// (`let x = f(x)`) resolves x in the OUTER scope because nm is not yet in
|
||||
@@ -2205,6 +2483,20 @@ fn cgletbody(c: *cgen, n: *syntax.node, off: i32) void = {
|
||||
if (tn == nil) { tn = inferletcalltype(c, n.rhs); };
|
||||
if (n.rhs != nil) {
|
||||
let rhs: *syntax.node = n.rhs;
|
||||
let initlit: *syntax.node = cginitstripcast(rhs);
|
||||
if (n.initsynthetic != 0 && initlit != nil
|
||||
&& (initlit.kind == syntax.nkind.N_ARRLIT
|
||||
|| initlit.kind == syntax.nkind.N_STRUCTLIT
|
||||
|| initlit.kind == syntax.nkind.N_TUPLE)) {
|
||||
let iti: *syntax.tinfo = nil;
|
||||
if (tn != nil) { iti = tn.type_: *syntax.tinfo; };
|
||||
if (iti == nil && n.type_ != nil) {
|
||||
iti = n.type_: *syntax.tinfo;
|
||||
};
|
||||
cginitvaluebp(c, iti, rhs, off);
|
||||
c.lastwasreturn = 0;
|
||||
return;
|
||||
};
|
||||
// `let s: []T = alloc([], n)!;` / `?` shortcut (#32, #45).
|
||||
// Mirror of cstage cgen.c N_LET arrlit-empty branch: allocate
|
||||
// n*esz bytes via rt_malloc, then build the {ptr, 0, n} slice
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -24,14 +24,26 @@ import os;
|
||||
import syntax;
|
||||
import strconv;
|
||||
|
||||
let wwiwritefailed: i32 = 0;
|
||||
|
||||
fn wwrite(fd: i32, p: *u8, n: u64) void = {
|
||||
if (wwiwritefailed != 0) { return; };
|
||||
match (os.writeall(fd, p, n)) {
|
||||
case let wrote: i64 => {
|
||||
if (wrote < 0 || (wrote: u64) != n) { wwiwritefailed = 1; };
|
||||
};
|
||||
case let e: os.oserror => wwiwritefailed = 1;
|
||||
};
|
||||
};
|
||||
|
||||
fn wputs(fd: i32, s: str) void = {
|
||||
os.write(fd, s.ptr, s.len: u64);
|
||||
wwrite(fd, s.ptr, s.len: u64);
|
||||
};
|
||||
|
||||
fn wputb(fd: i32, b: u8) void = {
|
||||
let buf: [1]u8;
|
||||
buf[0] = b;
|
||||
os.write(fd, buf.ptr, 1u64);
|
||||
wwrite(fd, buf.ptr, 1u64);
|
||||
};
|
||||
|
||||
fn wquote(fd: i32, s: str) void = {
|
||||
@@ -59,7 +71,7 @@ fn wquote(fd: i32, s: str) void = {
|
||||
buf[1] = 120u8;
|
||||
buf[2] = h;
|
||||
buf[3] = l;
|
||||
os.write(fd, buf.ptr, 4u64);
|
||||
wwrite(fd, buf.ptr, 4u64);
|
||||
} else {
|
||||
wputb(fd, c);
|
||||
};};};};};
|
||||
@@ -85,7 +97,8 @@ fn wwimodeq(a: str, b: str) bool = {
|
||||
fn wwiusepath(c: *checker, owner: str, source: i32, alias: str) str = {
|
||||
let u: *syntax.node = c.file.list;
|
||||
for (u != nil) {
|
||||
if (u.kind == syntax.nkind.N_USE && u.sourceid == source
|
||||
if (u.kind == syntax.nkind.N_USE && u.useblank == 0
|
||||
&& u.sourceid == source
|
||||
&& syntax.streq(u.str, alias)) {
|
||||
let same: bool = false;
|
||||
if (owner.len == 0) {
|
||||
@@ -301,7 +314,7 @@ fn wwirune(fd: i32, cp: u64) void = {
|
||||
buf[1] = 120u8;
|
||||
buf[2] = h;
|
||||
buf[3] = l;
|
||||
os.write(fd, buf.ptr, 4u64);
|
||||
wwrite(fd, buf.ptr, 4u64);
|
||||
} else {
|
||||
wputb(fd, c);
|
||||
};};};};};
|
||||
@@ -591,6 +604,7 @@ fn wwiprimary(n: *syntax.node) bool = {
|
||||
};
|
||||
|
||||
fn wwiisdecl(d: *syntax.node) bool = {
|
||||
if (d.initfn != 0 || d.initsynthetic != 0) { return false; };
|
||||
return d.kind == syntax.nkind.N_FNDECL || d.kind == syntax.nkind.N_TYPEDECL ||
|
||||
d.kind == syntax.nkind.N_DEF || d.kind == syntax.nkind.N_LET;
|
||||
};
|
||||
@@ -887,7 +901,7 @@ fn wwisortfacts(fs: *wwifactset) void = {
|
||||
};
|
||||
|
||||
fn wwiowneduse(u: *syntax.node, owner: str, source: i32) bool = {
|
||||
return u.kind == syntax.nkind.N_USE && u.imported != 0
|
||||
return u.kind == syntax.nkind.N_USE && u.useblank == 0 && u.imported != 0
|
||||
&& u.sourceid == source && wwimodeq(u.nmod, owner);
|
||||
};
|
||||
|
||||
@@ -900,7 +914,8 @@ fn wwiemitimports(fd: i32, file: *syntax.node, owner: str, source: i32,
|
||||
if (imported) {
|
||||
owned = wwiowneduse(u, owner, source);
|
||||
} else {
|
||||
owned = u.kind == syntax.nkind.N_USE && u.imported == 0
|
||||
owned = u.kind == syntax.nkind.N_USE && u.useblank == 0
|
||||
&& u.imported == 0
|
||||
&& u.sourceid == source;
|
||||
};
|
||||
if (owned) { nuse += 1; };
|
||||
@@ -916,7 +931,8 @@ fn wwiemitimports(fd: i32, file: *syntax.node, owner: str, source: i32,
|
||||
if (imported) {
|
||||
owned = wwiowneduse(u, owner, source);
|
||||
} else {
|
||||
owned = u.kind == syntax.nkind.N_USE && u.imported == 0
|
||||
owned = u.kind == syntax.nkind.N_USE && u.useblank == 0
|
||||
&& u.imported == 0
|
||||
&& u.sourceid == source;
|
||||
};
|
||||
if (owned) {
|
||||
@@ -944,7 +960,7 @@ fn wwiprimarysectionhas(c: *checker, file: *syntax.node,
|
||||
source: i32) bool = {
|
||||
let u: *syntax.node = file.list;
|
||||
for (u != nil) {
|
||||
if (u.kind == syntax.nkind.N_USE && u.imported == 0
|
||||
if (u.kind == syntax.nkind.N_USE && u.useblank == 0 && u.imported == 0
|
||||
&& u.sourceid == source) { return true; };
|
||||
u = u.next;
|
||||
};
|
||||
@@ -1025,7 +1041,8 @@ fn wwiemitprimarysection(c: *checker, fd: i32, file: *syntax.node,
|
||||
};
|
||||
};
|
||||
|
||||
fn wwiemit(c: *checker, file: *syntax.node, path: str) i32 = {
|
||||
fn wwiemitfd(c: *checker, file: *syntax.node, fd: i32) i32 = {
|
||||
wwiwritefailed = 0;
|
||||
// §5: check_exported_type FIRST, before any byte — a producer
|
||||
// without it can emit a dangling `.wwi`.
|
||||
let bad: i32 = 0;
|
||||
@@ -1101,15 +1118,6 @@ fn wwiemit(c: *checker, file: *syntax.node, path: str) i32 = {
|
||||
wwisortdecls(dkeys, dnodes, ndecl);
|
||||
};
|
||||
|
||||
let fd: i32 = os.open(path,
|
||||
os.flag.WRONLY | os.flag.CREATE | os.flag.TRUNC, 420i32); // 0o644
|
||||
if (fd < 0) {
|
||||
wputs(2, "w6c: cannot open ");
|
||||
wputs(2, path);
|
||||
wputs(2, "\n");
|
||||
return 1i32;
|
||||
};
|
||||
|
||||
// Canonical ownership, declared name, and lexical source scope are
|
||||
// independent export facts. Repeated owner sections preserve the file
|
||||
// that owns each binding while every symbol/action remains keyed by owner.
|
||||
@@ -1154,6 +1162,5 @@ fn wwiemit(c: *checker, file: *syntax.node, path: str) i32 = {
|
||||
fi += 1;
|
||||
};
|
||||
|
||||
os.close(fd);
|
||||
return 0i32;
|
||||
return wwiwritefailed;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user