diff options
Diffstat (limited to 'bootstrap')
| -rw-r--r-- | bootstrap/all.h | 3 | ||||
| -rw-r--r-- | bootstrap/cgen.c | 16 | ||||
| -rw-r--r-- | bootstrap/parse.c | 3 |
3 files changed, 13 insertions, 9 deletions
diff --git a/bootstrap/all.h b/bootstrap/all.h index 64ab9b1..cd51499 100644 --- a/bootstrap/all.h +++ b/bootstrap/all.h @@ -346,6 +346,7 @@ struct blockstmt { struct env env; slice_t(struct stmt) stmts; struct defer *defers; + int id; }; struct expr { @@ -496,7 +497,7 @@ struct stmt { }; struct defer { struct defer *next; - int age; + int blockid, age; struct expr ex; } ; diff --git a/bootstrap/cgen.c b/bootstrap/cgen.c index 94ae420..5fae699 100644 --- a/bootstrap/cgen.c +++ b/bootstrap/cgen.c @@ -488,6 +488,14 @@ genblock(struct blockstmt block) { pri("{\n"); for (int i = 0; i < block.stmts.n; ++i) genstmt(&block, &block.stmts.d[i]); + for (struct defer* defer = block.defers; defer; defer = defer->next) { + if (defer->blockid != block.id) + continue; + const char *p = fileid2path(defer->ex.span.fileid); + pri("\n#line %d %S\n", defer->ex.span.line, p, (u64)strlen(p)); + genexpr(&defer->ex); + pri(";\n"); + } pri("}\n"); } @@ -699,15 +707,7 @@ genfn(bool externp, const char *cname, struct fn *fn) { if (!fn->body) { pri(";\n"); } else { - pri("{"); genblock(fn->body->block); - for (struct defer* defer = fn->body->block.defers; defer; defer = defer->next) { - const char *p = fileid2path(defer->ex.span.fileid); - pri("\n#line %d %S\n", defer->ex.span.line, p, (u64)strlen(p)); - genexpr(&defer->ex); - pri(";\n"); - } - pri("}"); } } diff --git a/bootstrap/parse.c b/bootstrap/parse.c index 4b56c2b..371289f 100644 --- a/bootstrap/parse.c +++ b/bootstrap/parse.c @@ -2569,6 +2569,7 @@ parsestmt(stmt_yielder_t yield, void *yarg, struct parser *P) { assert(block != NULL && "defer outside block"); defer->age = deferage++; defer->ex = ex; + defer->blockid = P->curblock->id; defer->next = block->defers; block->defers = defer; st.t = Sblock; @@ -2617,10 +2618,12 @@ parseblock0(struct parser *P) { vec_t(struct stmt) stmts = {0}; struct blockstmt block = {0}; int tokt; + static int id; block.env.parent = P->curenv; pushenv(P, &block.env); block.defers = P->curblock ? P->curblock->defers : NULL; + block.id = id++; WITH_TMPCHANGE(struct blockstmt *, P->curblock, &block) { while ((tokt = lexpeek(P).t) != '}' && tokt != TKkw_case && tokt != ')') { if (lexmatch(P, NULL, ';')) |