#include "from.h" #include "dbg.h" #include "fn.h" #include "errloc.h" #include "stmt.h" #include "storage.h" #include "tmp.h" #include "triplet.h" #include "type.h" #include #include #include #include #include void from_free(struct from *fr) { } static int checksym(const struct fn *fn, const struct tk *tk, struct from_it *fr) { const struct stentry *e = fn_var(fn, tk); if (!e) { errloc(tk, "undefined reference to \"%s\"", tk->s); return -1; } switch (e->t->type) { case C: case BUILTIN: break; case U: case S: case P: case T: case PTR: case ARY: { char *s = type_name(e->t); if (s) errloc(tk, "\"%s\" (\"%s\") cannot be used in \"from\" loop", tk->s, s); free(s); return -1; } } fr->type = FR_VAR; fr->u.e = e; return 0; } static int check(const struct fn *fn, const struct tk *tk, struct from_it *fr) { switch (tk->type) { case NUM: fr->type = FR_VAL; fr->u.tk = tk; break; case ID: return checksym(fn, tk, fr); case LIT: errloc(tk, "unexpected literal"); return -1; case UNDEF: case ANY: fprintf(stderr, "%s: unreachable\n", __func__); return -1; } return 0; } static int finalize(struct prv *p, struct fn *fn, struct from *fr) { struct stmt *s; struct pos *pos = &p->pos[p->i]; size_t nst = fn->nstmts + 1, nbl = fn->nblocks + 1, *bl = realloc(fn->blocks, nbl * sizeof *bl); if (!bl) { perror("realloc(3)"); return -1; } fr->block_i = bl[fn->nblocks++] = fn->nstmts; fn->blocks = bl; if (!(s = realloc(fn->stmts, nst * sizeof *s))) { perror("realloc(3)"); return -1; } s[fn->nstmts++] = (struct stmt) { .type = FROM, .u.from = *fr, .end = from_end }; fn->stmts = s; pos->seq = pos->stseq = stmts; pos->step = stmts->steps; dbg(", iterator %s, block index %zu\n", fr->var->tk->s, bl[fn->nblocks - 1]); return 0; } static const struct type *valtype(const struct fn *fn, const char *tr, const struct tk *tk) { char *end; errno = 0; strtol(tk->s, &end, 0); if (errno || *end) { errno = 0; strtoll(tk->s, &end, 0); if (errno || *end) return type_find(fn, tr, "ulong"); return type_find(fn, tr, "long"); } return type_find(fn, tr, "word"); } static const struct type *ittype(const struct fn *fn, const char *tr, const struct from_it *it) { switch (it->type) { case FR_VAL: return valtype(fn, tr, it->u.tk); case FR_VAR: return it->u.e->t; } fprintf(stderr, "%s: unreachable\n", __func__); return NULL; } static const struct type *gettype(const struct fn *fn, const char *tr, const struct from_it *a, const struct from_it *b) { const struct type *ta = ittype(fn, tr, a), *tb = ittype(fn, tr, b); return ta->sz > tb->sz ? ta : tb; } int from(const struct lex *l, struct prv *p) { const struct tk *start = p->stk + 1, *end = p->stk + 3; const char *tr = triplet(p); struct fn *fn = fn_cur(p); struct from fr = {0}; const struct type *t; if (check(fn, start, &fr.start) || check(fn, end, &fr.end) || !(t = gettype(fn, tr, &fr.start, &fr.end)) || !(fr.var = tmp_create(fn, t))) return -1; dbg("\t\tadd from statement [%s, %s]", start->s, end->s); if (finalize(p, fn, &fr)) return -1; p->stk = ++end; return 0; } static int checkvar(const struct fn *fn, const struct tk *tk, struct from *fr) { const struct stentry *e; if (tk->type != ID) { errloc(tk, "\"%s\" not a variable", tk->s); return -1; } else if (!(e = fn_var(fn, tk))) { errloc(tk, "undefined reference to \"%s\"", tk->s); return -1; } else if (e->t->type != BUILTIN) { errloc(tk, "variable \"%s\" not an integer", tk->s); return -1; } fr->var = e; return 0; } int from_id(const struct lex *l, struct prv *p) { const struct tk *start = p->stk + 1, *end = p->stk + 3, *var = p->stk + 5; struct fn *fn = fn_cur(p); struct from fr = {0}; if (check(fn, start, &fr.start) || check(fn, end, &fr.end) || checkvar(fn, var, &fr)) return -1; dbg("\t\tadd from statement [%s, %s] using %s", start->s, end->s, var->s); if (finalize(p, fn, &fr)) return -1; p->stk = ++var; return 0; } int from_by(const struct lex *l, struct prv *p) { fprintf(stderr, "%s: TODO\n", __func__); return -1; } int from_idby(const struct lex *l, struct prv *p) { fprintf(stderr, "%s: TODO\n", __func__); return -1; }