summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOri Bernstein <ori@markovcorp.com>2019-02-26 13:03:14 -0800
committerOri Bernstein <ori@markovcorp.com>2019-02-26 13:03:14 -0800
commit02201160614e5f25a7a73d86ed26a1f90fa4f5bb (patch)
treece958c6982673794612cf2ea91ab8b353bf4c243
parent16a7bb8f8cc86b22ce157e0944f66f60a556590f (diff)
downloadmc-02201160614e5f25a7a73d86ed26a1f90fa4f5bb.tar.gz
Add support for `__fini__` functions.
-rw-r--r--6/main.c8
-rw-r--r--6/simp.c2
-rw-r--r--mbld/libs.myr3
-rw-r--r--parse/dump.c2
-rw-r--r--parse/export.c10
-rw-r--r--parse/gram.y54
-rw-r--r--parse/infer.c6
-rw-r--r--parse/parse.h11
-rw-r--r--parse/specialize.c12
-rw-r--r--parse/stab.c2
-rw-r--r--parse/use.c44
-rw-r--r--rt/start-freebsd.s6
-rw-r--r--rt/start-linux.s4
-rw-r--r--rt/start-netbsd.s5
-rw-r--r--rt/start-openbsd.s5
-rw-r--r--rt/start-osx.s4
16 files changed, 108 insertions, 70 deletions
diff --git a/6/main.c b/6/main.c
index 5e3d6da..c980887 100644
--- a/6/main.c
+++ b/6/main.c
@@ -293,8 +293,12 @@ main(int argc, char **argv)
if (debugopt['T'])
dump(file, stdout);
loaduses();
- if (hasmain(file))
- geninit();
+ if (hasmain(file)) {
+ genautocall(file->file.init, file->file.ninit,
+ file->file.localinit, "__init__");
+ genautocall(file->file.fini, file->file.nfini,
+ file->file.localfini, "__fini__");
+ }
infer();
tagexports(0);
/* after all type inference */
diff --git a/6/simp.c b/6/simp.c
index 415f49d..cff77ba 100644
--- a/6/simp.c
+++ b/6/simp.c
@@ -1403,7 +1403,7 @@ isexport(Node *dcl)
n = dcl->decl.name;
if (!n->name.ns && streq(n->name.name, "main"))
return 1;
- if (streq(n->name.name, "__init__"))
+ if (streq(n->name.name, "__init__") || streq(n->name.name, "__fini__"))
return 1;
return 0;
}
diff --git a/mbld/libs.myr b/mbld/libs.myr
index 2e6db6d..19fd869 100644
--- a/mbld/libs.myr
+++ b/mbld/libs.myr
@@ -22,7 +22,8 @@ pkg bld =
incs : byte[:][:] -> void)
;;
-const Abiversion = 20
+/* Keep in sync with parse/parse.h */
+const Abiversion = 21
const builtlib = {b, mt, dep, dyndep
var ldep, l, u
diff --git a/parse/dump.c b/parse/dump.c
index 9437984..c24ac0f 100644
--- a/parse/dump.c
+++ b/parse/dump.c
@@ -185,7 +185,7 @@ outnode(Node *n, FILE *fd, int depth)
findentf(fd, depth + 1, "ishidden=%d\n", n->decl.ishidden);
findentf(fd, depth + 1, "isimport=%d\n", n->decl.isimport);
findentf(fd, depth + 1, "isnoret=%d\n", n->decl.isnoret);
- findentf(fd, depth + 1, "isexportinit=%d\n", n->decl.isexportinit);
+ findentf(fd, depth + 1, "isexportval=%d\n", n->decl.isexportval);
findentf(fd, depth, ")\n");
outsym(n, fd, depth + 1);
outnode(n->decl.init, fd, depth + 1);
diff --git a/parse/export.c b/parse/export.c
index 136f0de..f63f4b6 100644
--- a/parse/export.c
+++ b/parse/export.c
@@ -112,7 +112,7 @@ tagtype(Stab *st, Type *t, int ingeneric, int hidelocal)
}
int
-isexportinit(Node *n)
+isexportval(Node *n)
{
if (n->decl.isgeneric && !n->decl.trait)
return 1;
@@ -185,8 +185,8 @@ tagnode(Stab *st, Node *n, int ingeneric, int hidelocal)
tagtype(st, n->decl.type, ingeneric, hidelocal);
if (hidelocal && n->decl.ispkglocal)
n->decl.vis = Vishidden;
- n->decl.isexportinit = isexportinit(n);
- if (n->decl.isexportinit || ingeneric)
+ n->decl.isexportval = isexportval(n);
+ if (n->decl.isexportval || ingeneric)
tagnode(st, n->decl.init, n->decl.isgeneric, hidelocal);
break;
case Nfunc:
@@ -229,8 +229,12 @@ tagexports(int hidelocal)
/* tag the initializers */
for (i = 0; i < file->file.ninit; i++)
tagnode(st, file->file.init[i], 0, hidelocal);
+ for (i = 0; i < file->file.nfini; i++)
+ tagnode(st, file->file.fini[i], 0, hidelocal);
if (file->file.localinit)
tagnode(st, file->file.localinit, 0, hidelocal);
+ if (file->file.localfini)
+ tagnode(st, file->file.localfini, 0, hidelocal);
/* tag the exported nodes */
k = htkeys(st->dcl, &n);
diff --git a/parse/gram.y b/parse/gram.y
index bd54217..dd106f8 100644
--- a/parse/gram.y
+++ b/parse/gram.y
@@ -29,14 +29,14 @@ static int lbldepth = -1;
void yyerror(const char *s);
int yylex(void);
-static Op binop(int toktype);
-static Node *mkpseudodecl(Srcloc l, Type *t);
-static void installucons(Stab *st, Type *t);
-static void setattrs(Node *dcl, char **attrs, size_t nattrs);
-static void setwith(Type *ty, Traitspec **spec, size_t nspec);
-static void setupinit(Node *n);
-static void addinit(Node *blk, Node *dcl);
-static void setuname(Type *ty);
+static Op binop(int);
+static Node *mkpseudodecl(Srcloc, Type *);
+static void installucons(Stab *, Type *);
+static void setattrs(Node *, char **attrs, size_t);
+static void setwith(Type *, Traitspec **, size_t);
+static void mangleautocall(Node *, char *);
+static void addinit(Node *, Node *);
+static void setuname(Type *);
%}
@@ -249,18 +249,20 @@ toplev : package
}
| decl {
size_t i;
- Node *n;
+ Node *n, *d;
for (i = 0; i < $1.nn; i++) {
- if (!strcmp(declname($1.nl[i]), "__init__"))
- setupinit($1.nl[i]);
+ d = $1.nl[i];
/* putdcl can merge, so we need to getdcl after */
- putdcl(file->file.globls, $1.nl[i]);
- n = getdcl(file->file.globls, $1.nl[i]->decl.name);
+ mangleautocall(d, declname(d));
+ putdcl(file->file.globls, d);
+ n = getdcl(file->file.globls, d->decl.name);
lappend(&file->file.stmts, &file->file.nstmts, n);
- $1.nl[i]->decl.isglobl = 1;
- if ($1.nl[i]->decl.isinit)
- file->file.localinit = $1.nl[i];
+ d->decl.isglobl = 1;
+ if (d->decl.isinit)
+ file->file.localinit = d;
+ if (d->decl.isfini)
+ file->file.localfini = d;
}
}
| /* empty */
@@ -1129,24 +1131,26 @@ addinit(Node *blk, Node *dcl)
}
static void
-setupinit(Node *n)
+mangleautocall(Node *n, char *fn)
{
char name[1024];
char *p, *s;
- bprintf(name, sizeof name, "%s$__init__", file->file.files[0]);
- s = strrchr(name, '/');
- if (s)
+ if (strcmp(fn, "__init__") == 0)
+ n->decl.isinit = 1;
+ else if (strcmp(fn, "__fini__") == 0)
+ n->decl.isfini = 1;
+ else
+ return;
+
+ bprintf(name, sizeof name, "%s$%s", file->file.files[0], fn);
+ if ((s = strrchr(name, '/')) != NULL)
s++;
else
s = name;
- p = s;
- while (*p) {
+ for(p = s; *p; p++)
if (!isalnum(*p) && *p != '_')
*p = '$';
- p++;
- }
- n->decl.isinit = 1;
n->decl.vis = Vishidden;
n->decl.name->name.name = strdup(s);
}
diff --git a/parse/infer.c b/parse/infer.c
index 30d8747..4e640af 100644
--- a/parse/infer.c
+++ b/parse/infer.c
@@ -2654,6 +2654,7 @@ verifyop(Node *n)
static void
typesub(Node *n, int noerr)
{
+ char *name;
size_t i;
if (!n)
@@ -2675,9 +2676,10 @@ typesub(Node *n, int noerr)
if (!maincompatible(tybase(decltype(n))))
fatal(n, "main must be (->void) or (byte[:][:] -> void), got %s",
tystr(decltype(n)));
- if (streq(declname(n), "__init__"))
+ name = declname(n);
+ if (streq(name, "__init__") || streq(name, "__fini__"))
if (!initcompatible(tybase(decltype(n))))
- fatal(n, "__init__ must be (->void), got %s", tystr(decltype(n)));
+ fatal(n, "%s must be (->void), got %s", name, tystr(decltype(n)));
popenv(n->decl.env);
break;
case Nblock:
diff --git a/parse/parse.h b/parse/parse.h
index 5cf7216..9be2f57 100644
--- a/parse/parse.h
+++ b/parse/parse.h
@@ -1,4 +1,4 @@
-#define Abiversion 20
+#define Abiversion 21
typedef struct Srcloc Srcloc;
typedef struct Tysubst Tysubst;
@@ -217,9 +217,13 @@ struct Node {
Node **init; /* all __init__ function names of our deps. NB, this
is a Nname, not an Ndecl */
size_t ninit;
+ Node **fini; /* all __fini__ function names of our deps. NB, this
+ is a Nname, not an Ndecl */
+ size_t nfini;
Node **impl; /* impls defined in this file, across all namespaces */
size_t nimpl;
Node *localinit;/* and the local one, if any */
+ Node *localfini;/* and the local one, if any */
Stab *globls; /* global symtab */
Stab *builtins; /* global symtab */
Htab *ns; /* namespaces */
@@ -331,8 +335,9 @@ struct Node {
char ishidden;
char isimport;
char isnoret;
- char isexportinit;
+ char isexportval;
char isinit;
+ char isfini;
} decl;
struct {
@@ -549,7 +554,7 @@ Type *substget(Tysubst *subst, Type *from);
Node *specializedcl(Node *n, Type *param, Type *to, Node **name);
Type *tyspecialize(Type *t, Tysubst *tymap, Htab *delayed);
Node *genericname(Node *n, Type *param, Type *t);
-void geninit(void);
+void genautocall(Node **, size_t, Node *, char *);
/* usefiles */
void loaduses(void);
diff --git a/parse/specialize.c b/parse/specialize.c
index becf273..a6f2d27 100644
--- a/parse/specialize.c
+++ b/parse/specialize.c
@@ -639,24 +639,24 @@ callinit(Node *block, Node *init, Type *tyvoid, Type *tyvoidfn)
}
void
-geninit(void)
+genautocall(Node **call, size_t ncall, Node *local, char *fn)
{
Node *name, *decl, *func, *block, *init;
Type *tyvoid, *tyvoidfn;
size_t i;
- name = mkname(Zloc, "__init__");
+ name = mkname(Zloc, fn);
decl = mkdecl(Zloc, name, mktyvar(Zloc));
block = mkblock(Zloc, mkstab(0));
tyvoid = mktype(Zloc, Tyvoid);
tyvoidfn = mktyfunc(Zloc, NULL, 0, tyvoid);
- for (i = 0; i < file->file.ninit; i++) {
- init = initdecl(file, file->file.init[i], tyvoidfn);
+ for (i = 0; i < ncall; i++) {
+ init = initdecl(file, call[i], tyvoidfn);
callinit(block, init, tyvoid, tyvoidfn);
}
- if (file->file.localinit)
- callinit(block, file->file.localinit, tyvoid, tyvoidfn);
+ if (local)
+ callinit(block, local, tyvoid, tyvoidfn);
func = mkfunc(Zloc, NULL, 0, mktype(Zloc, Tyvoid), block);
init = mkexpr(Zloc, Olit, func, NULL);
diff --git a/parse/stab.c b/parse/stab.c
index 53f10b2..3efcd03 100644
--- a/parse/stab.c
+++ b/parse/stab.c
@@ -404,7 +404,7 @@ mergedecl(Node *old, Node *new)
old->decl.ishidden = e->decl.ishidden || g->decl.ishidden;
old->decl.isimport = e->decl.isimport || g->decl.isimport;
old->decl.isnoret = e->decl.isnoret || g->decl.isnoret;
- old->decl.isexportinit = e->decl.isexportinit || g->decl.isexportinit;
+ old->decl.isexportval = e->decl.isexportval || g->decl.isexportval;
old->decl.isglobl = e->decl.isglobl || g->decl.isglobl;
old->decl.ispkglocal = e->decl.ispkglocal || g->decl.ispkglocal;
old->decl.isextern = e->decl.isextern || g->decl.isextern;
diff --git a/parse/use.c b/parse/use.c
index c40c9bc..5a28a0e 100644
--- a/parse/use.c
+++ b/parse/use.c
@@ -39,7 +39,7 @@ static Node *unpickle(FILE *fd);
static Htab *trdeduptab; /* map from name -> type, contains all Tynames loaded ever */
static Htab *tidmap; /* map from tid -> type */
static Htab *trmap; /* map from trait id -> trait */
-static Htab *initmap; /* map from init name -> int */
+static Htab *autocallmap; /* map from autocall name -> int */
#define Builtinmask (1 << 30)
static Typefix *typefix; /* list of types we need to replace */
@@ -176,9 +176,10 @@ wrsym(FILE *fd, Node *val)
wrbool(fd, val->decl.isextern);
wrbool(fd, val->decl.ispkglocal);
wrbool(fd, val->decl.isnoret);
- wrbool(fd, val->decl.isexportinit);
- wrbool(fd, val->decl.isinit);
- if (val->decl.isexportinit)
+ wrbool(fd, val->decl.isexportval);
+ wrbyte(fd, val->decl.isinit);
+ wrbyte(fd, val->decl.isfini);
+ if (val->decl.isexportval)
pickle(fd, val->decl.init);
}
@@ -205,9 +206,10 @@ rdsym(FILE *fd, Trait *ctx)
n->decl.ispkglocal = rdbool(fd);
n->decl.isnoret = rdbool(fd);
n->decl.isimport = 1;
- n->decl.isexportinit = rdbool(fd);
+ n->decl.isexportval = rdbool(fd);
n->decl.isinit = rdbool(fd);
- if (n->decl.isexportinit)
+ n->decl.isfini = rdbool(fd);
+ if (n->decl.isexportval)
n->decl.init = unpickle(fd);
return n;
}
@@ -942,7 +944,7 @@ int
loaduse(char *path, FILE *f, Stab *st, Vis vis)
{
size_t startdecl, starttype, startimpl;
- Node *dcl, *impl, *init;
+ Node *dcl, *impl, *fn;
Stab *s, *ns;
intptr_t tid;
size_t i, j;
@@ -990,8 +992,8 @@ loaduse(char *path, FILE *f, Stab *st, Vis vis)
}
tidmap = mkht(ptrhash, ptreq);
trmap = mkht(ptrhash, ptreq);
- if (!initmap)
- initmap = mkht(namehash, nameeq);
+ if (!autocallmap)
+ autocallmap = mkht(namehash, nameeq);
/* builtin traits */
for (i = 0; i < Ntraits; i++)
htput(trmap, itop(i), traittab[i]);
@@ -1024,11 +1026,15 @@ foundextlib:
putdcl(s, dcl);
break;
case 'S':
- init = unpickle(f);
- if (!hthas(initmap, init)) {
- htput(initmap, init, init);
- lappend(&file->file.init, &file->file.ninit, init);
- }
+ case 'E':
+ fn = unpickle(f);
+ if (hthas(autocallmap, fn))
+ break;
+ htput(autocallmap, fn, fn);
+ if (c == 'S')
+ lappend(&file->file.init, &file->file.ninit, fn);
+ else
+ lappend(&file->file.fini, &file->file.nfini, fn);
break;
case 'R':
tr = traitunpickle(f);
@@ -1289,7 +1295,7 @@ writeuse(FILE *f, Node *file)
if (s->decl.vis == Visintern || s->decl.vis == Visbuiltin)
continue;
/* trait functions get written out with their traits */
- if (s->decl.trait || s->decl.isinit)
+ if (s->decl.trait || s->decl.isinit || s->decl.isfini)
continue;
else if (s->decl.isgeneric)
wrbyte(f, 'G');
@@ -1305,5 +1311,13 @@ writeuse(FILE *f, Node *file)
wrbyte(f, 'S');
pickle(f, file->file.localinit->decl.name);
}
+ for (i = 0; i < file->file.nfini; i++) {
+ wrbyte(f, 'E');
+ pickle(f, file->file.fini[i]);
+ }
+ if (file->file.localfini) {
+ wrbyte(f, 'E');
+ pickle(f, file->file.localfini->decl.name);
+ }
free(k);
}
diff --git a/rt/start-freebsd.s b/rt/start-freebsd.s
index 399fac0..b995559 100644
--- a/rt/start-freebsd.s
+++ b/rt/start-freebsd.s
@@ -51,11 +51,11 @@ _start:
addq $0x10,%rsp
xorq %rbp,%rbp
- /* call pre-main initializers */
- call __init__
- /* enter the main program */
+ call __init__
call main
+ call __fini__
+
/* exit(0) */
xorq %rdi,%rdi
movq $1,%rax
diff --git a/rt/start-linux.s b/rt/start-linux.s
index a0cbfb6..e671fec 100644
--- a/rt/start-linux.s
+++ b/rt/start-linux.s
@@ -48,10 +48,10 @@ _start:
syscall
xorq %rbp,%rbp
- /* call pre-main initializers */
call __init__
- /* enter the main program */
call main
+ call __fini__
+
/* exit(0) */
xorq %rdi,%rdi
movq $60,%rax
diff --git a/rt/start-netbsd.s b/rt/start-netbsd.s
index 3a6cfc7..7765afe 100644
--- a/rt/start-netbsd.s
+++ b/rt/start-netbsd.s
@@ -60,10 +60,11 @@ _start:
addq $0x10,%rsp
xorq %rbp,%rbp
- /* call pre-main initializers */
+
call __init__
- /* enter the main program */
call main
+ call __fini__
+
/* exit(0) */
xorq %rdi,%rdi
movq $1,%rax
diff --git a/rt/start-openbsd.s b/rt/start-openbsd.s
index eef7b45..7c9c67a 100644
--- a/rt/start-openbsd.s
+++ b/rt/start-openbsd.s
@@ -61,10 +61,11 @@ _start:
to call kbind here, but this breaks
when we dynamically link in libc.
*/
- /* call pre-main initializers */
+
call __init__
- /* enter the main program */
call main
+ call __init__
+
/* exit(0) */
xorq %rdi,%rdi
movq $1,%rax
diff --git a/rt/start-osx.s b/rt/start-osx.s
index a1e1856..53820d4 100644
--- a/rt/start-osx.s
+++ b/rt/start-osx.s
@@ -48,9 +48,11 @@ start:
syscall
xorq %rbp,%rbp
+
call ___init__
- /* enter the main program */
call _main
+ call ___fini__
+
/* exit */
xorq %rdi,%rdi
movq $0x2000001,%rax