diff options
Diffstat (limited to 'sys/cmd/cc/cc.c')
-rw-r--r-- | sys/cmd/cc/cc.c | 182 |
1 files changed, 63 insertions, 119 deletions
diff --git a/sys/cmd/cc/cc.c b/sys/cmd/cc/cc.c index cf479f8..c06d1c3 100644 --- a/sys/cmd/cc/cc.c +++ b/sys/cmd/cc/cc.c @@ -92,89 +92,6 @@ END: } // ----------------------------------------------------------------------- -// io buffer management - -#define asrdr(x) (io·Reader){(int (*)(void *, int, int, void *))x} -// path should be absolute -Io* -openio(byte *path) -{ - string *it, *end; - Stream *f; - - intern(&path); - - // See if we have already opened file; - // If so, and it hasn't been flagged return it - for (it = C.omit.path, end = it + C.omit.len; it < end; ++it) { - if ((uintptr)(*it) == (uintptr)(path)) - return nil; - } - - // TODO: See if we have already loaded the file - // - if ((C.io - C.iostk) >= arrlen(C.iostk)-1) - panicf("out of I/O space!"); - - C.io->f = io·open(path, "r"); - if (!C.io->f) - panicf("file %s not found", path); - - C.io->kind = IOfile; - C.io->path = path; - bufio·initreader(&C.io->rdr, asrdr(io·read), C.io->f); - - return C.io++; -} - -Io* -makeio(byte *name) -{ - if ((C.io - C.iostk) >= arrlen(C.iostk)-1) - panicf("out of I/O space!"); - - C.io->path = name; - C.io->rdr = (io·Buffer) { - .state = bufio·rdr | bufio·end, - .runesize = 0, - .h = nil, - .size = bufio·size, - .beg = C.io->rdr.buf + bufio·ungets, - .pos = C.io->rdr.buf + bufio·ungets, - .end = C.io->rdr.buf + bufio·ungets, - }; - C.io->b = C.io->rdr.beg; - - return C.io++; -} -#undef asrdr - -void -freeio(Io *io) -{ - if (io->kind & IOfile) { - io·close(io->f); - } - - io->rdr.state = 0; - io->kind = 0; - io->link = nil; - io->path = nil; - io->store = (Pos){ 0 }; - io->path = "<empty>"; -} - -void -pushomit(string omit) -{ - if (C.omit.len == C.omit.cap) { - C.omit.cap += 20; - C.omit.path = realloc(C.omit.path, C.omit.cap*sizeof(*C.omit.path)); - } - C.omit.path[C.omit.len++] = omit; -} - -// ----------------------------------------------------------------------- // universal compiler builtins #define KEYWORD(a, b) b, @@ -227,79 +144,102 @@ init(void) { int i; - for (i = 0; i < arrlen(keywords); i++) { + for (i = 0; i < arrlen(keywords); i++) intern(&keywords[i]); - } - for (i = 0; i < arrlen(directives); i++) { + for (i = 0; i < arrlen(directives); i++) intern(&directives[i]); - } - C.heap = mem·makearena(mem·sys, nil); + C.heap = mem·makearena(mem·sys, nil); + /* compiler definitions */ + C.def.len = 0; + C.def.cap = 100; + C.def.val = calloc(C.def.cap, sizeof(*C.def.val)); + + /* compiler include paths */ C.inc.len = 0; C.inc.cap = 100; C.inc.dir = calloc(C.inc.cap, sizeof(*C.inc.dir)); C.inc.dir[C.inc.len++] = "."; - C.omit.len = 0; - C.omit.cap = 100; - C.omit.path = calloc(C.omit.cap, sizeof(*C.inc.dir)); - C.outfile = nil; - C.io = C.iostk; - C.io->link = nil; - memset(C.iostk, 0, sizeof(C.iostk)); +} - C.lxr = (Lexer){ 0 }; - C.lxr.b = C.lxr.buf; +void +initl(Lexer *lx) +{ + int i; + + *lx = (Lexer){ 0 }; + lx->b = lx->buf; /* predefine macros */ - dodefine(&C.lxr, "__LINE__"); - dodefine(&C.lxr, "__FILE__"); - C.lxr.macline = (uintptr)lookup(&C.lxr.sym, "__LINE__"); - C.lxr.macfile = (uintptr)lookup(&C.lxr.sym, "__FILE__"); + dodefine(lx, "__LINE__"); + dodefine(lx, "__FILE__"); + lx->macline = (uintptr)lookup(&lx->sym, "__LINE__"); + lx->macfile = (uintptr)lookup(&lx->sym, "__FILE__"); + + for (i = 0; i < C.def.len; i++) + dodefine(lx, C.def.val[i]); + + lx->omit.len = 0; + lx->omit.cap = 100; + lx->omit.path = calloc(lx->omit.cap, sizeof(*C.inc.dir)); + + lx->io = lx->iostk; + lx->io->link = nil; + memset(lx->iostk, 0, sizeof(lx->iostk)); +} + +void +initp(Parser *p) +{ + p->sp = p->spstk; + p->nm = p->nmstk; + p->dt = p->dtstk; } error compile(byte *path) { - Io *io; - Token tok; - byte *p, out[400]; + Lexer lx; + Parser p; + byte *sep, out[400]; intern(&path); strcpy(out, path); - p = utf8·findrrune(out, '/'); - if (p) - *p++ = '\0'; + sep = utf8·findrrune(out, '/'); + if (sep) + *sep++ = '\0'; else - p = out; + sep = out; if (!C.outfile) { - C.outfile = p; + C.outfile = sep; if (C.outfile) { - if ((p = utf8·findrrune(C.outfile, '.'))) { - p[0] = '.'; - p[1] = 'o'; - p[2] = '\0'; + if ((sep = utf8·findrrune(C.outfile, '.'))) { + sep[0] = '.'; + sep[1] = 'o'; + sep[2] = '\0'; } } else { C.outfile = "/dev/null"; } } - C.lxr.io = openio(path); - C.lxr.pos = (Pos){ + initl(&lx); + initp(&p); + + lx.io = openio(path); + lx.pos = (Pos){ .path = path, .line = 1, .col = 1, }; - parse(&C.psr, &C.lxr); - - return tok.kind != Anil; + return parse(&p, &lx); } error @@ -319,7 +259,11 @@ main(int argc, byte *argv[]) a = ARGF(); if (a) { intern(&a); - dodefine(&C.lxr, a); + if (C.def.len >= C.def.cap) { + C.def.cap += 20; + C.def.val = realloc(C.def.val, C.def.cap * sizeof(*C.def.val)); + } + C.def.val[C.def.len++] = a; } break; |