From 23ac1f4f98accc3bb84e81be264d8408be372028 Mon Sep 17 00:00:00 2001 From: Nicholas Noll Date: Sun, 17 May 2020 18:28:05 -0700 Subject: fix: bugs associated to map reallocating --- sys/cmd/cc/cc.c | 38 +++++++++++++++++++++++--------------- sys/cmd/cc/cc.h | 2 ++ sys/cmd/cc/lex.c | 27 ++++++++++++++++++++++++--- sys/cmd/cc/pp.c | 6 +++--- 4 files changed, 52 insertions(+), 21 deletions(-) (limited to 'sys/cmd/cc') diff --git a/sys/cmd/cc/cc.c b/sys/cmd/cc/cc.c index 39ad5f2..3dae0fd 100644 --- a/sys/cmd/cc/cc.c +++ b/sys/cmd/cc/cc.c @@ -12,7 +12,7 @@ hash_string(byte* s) int32 h; h = 0; - if (h != 0) { + if (s != nil) { for (; *s; ++s) { h += *s; h = (h << 10); @@ -58,7 +58,7 @@ static int morestrtab(StrTab *tab, int n) { - MAP_GROW(tab, string, int32, n, HASH, mem·sys.alloc, mem·sys.free, nil); + MAP_GROW(tab, string, int32, n, HASH, ·calloc, ·free, nil); } static @@ -106,7 +106,7 @@ openio(byte *path) // See if we have already opened file; // If so, and it hasn't been flagged return it - for (it = C.iostk; it != C.io + 1; ++it) { + for (it = C.iostk; it != C.io; ++it) { if ((uintptr)it->path == (uintptr)path) { if (it->kind & IOonce) { return nil; @@ -119,6 +119,9 @@ openio(byte *path) 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->path = path; bufio·initreader(&C.io->buf, asrdr(io·read), C.io->f); @@ -176,19 +179,19 @@ byte *directives[NUM_DIRECTIVES] = { DIRECTIVES }; struct Compiler C = { 0 }; // ----------------------------------------------------------------------- -// flag handlers +// cli flag handlers void pushinclude(byte *dirs) { string d, s, *it, *end; - while (*dirs != 0) { + while (*dirs != '\0') { d = strchr(dirs, ' '); if (d != nil) *d = '\0'; - s = d; + s = dirs; intern(&s); for (it = C.inc.dir, end = it + C.inc.len; it != end; ++it) { if ((uintptr)s == (uintptr)(*it)) @@ -198,15 +201,13 @@ pushinclude(byte *dirs) if (C.inc.len == C.inc.cap) { C.inc.cap += 20; C.inc.dir = realloc(C.inc.dir, C.inc.cap*sizeof(*C.inc.dir)); - C.inc.dir[C.inc.len++] = s; } - + C.inc.dir[C.inc.len++] = s; Nextdir: if (d == nil) break; dirs = d + 1; } - } // ----------------------------------------------------------------------- @@ -233,6 +234,9 @@ init(void) C.inc.dir[C.inc.len++] = "."; C.outfile = nil; + C.io = C.iostk; + memset(C.iostk, 0, sizeof(C.iostk)); + C.lxr = (Lexer){ 0 }; } @@ -241,14 +245,14 @@ compile(byte *path) { Io *io; Token tok; - byte *p, file[400]; + byte *p, out[400]; - strcpy(file, path); - p = utf8·findrrune(file, '/'); + strcpy(out, path); + p = utf8·findrrune(out, '/'); if (p) *p++ = '\0'; else - p = file; + p = out; if (!C.outfile) { C.outfile = p; @@ -263,9 +267,9 @@ compile(byte *path) } } - C.lxr.io = openio(file); + C.lxr.io = openio(path); while (tok = lex(&C.lxr), tok.kind > Aeof) { - ; + puttok(tok); } freeio(C.lxr.io); @@ -305,6 +309,10 @@ main(int argc, byte *argv[]) exit(1); } + // NOTE: This is just for my comfort during debugging. + pushinclude("/home/nolln/root/include"); + pushinclude("/home/nolln/root/include/vendor/libc"); + src = (argc == 0) ? "" : argv[0]; intern(&src); diff --git a/sys/cmd/cc/cc.h b/sys/cmd/cc/cc.h index 5488f3c..9871e99 100644 --- a/sys/cmd/cc/cc.h +++ b/sys/cmd/cc/cc.h @@ -245,6 +245,8 @@ rune ungetrune(Lexer *, rune r); void pushio(Lexer *lx, Io *new); void popio(Lexer *lx); +void puttok(Token); + // ----------------------------------------------------------------------- // parsing & type resolution // tokens -> ast diff --git a/sys/cmd/cc/lex.c b/sys/cmd/cc/lex.c index 6b85d8c..6d3da59 100644 --- a/sys/cmd/cc/lex.c +++ b/sys/cmd/cc/lex.c @@ -1,7 +1,27 @@ #include "cc.h" - #include +// ----------------------------------------------------------------------- +// printing functions + +void +puttok(Token tok) +{ + if (tok.kind < Alit) + printf("%s", tokens[tok.kind]); + else if (tok.kind & Alit) { + if (tok.kind & Vchar) + if (tok.kind & Vint) + if (tok.kind & Vlong) + if (tok.kind & Vvlong) + printf("literal <%lld>", tok.val.i); + if (tok.kind & Vfloat) + printf("literal <%f>", tok.val.f); + printf("literal <%s>", tok.val.s); + } else + printf("ident <%s>", tok.val.s); +} + // ----------------------------------------------------------------------- // simple wrappers @@ -607,7 +627,7 @@ static int moresymtab(SymTab *tab, int n) { - MAP_GROW(tab, string, Sym*, n, PTR_HASH, mem·sys.alloc, mem·sys.free, nil); + MAP_GROW(tab, string, Sym*, n, PTR_HASH, ·calloc, ·free, nil); } static @@ -642,8 +662,9 @@ errorat(Pos x, byte *fmt, ...) va_start(args, fmt); printf("error %d: ", x.line); - vprintf(fmt, args); + printf("\n"); + va_end(args); } diff --git a/sys/cmd/cc/pp.c b/sys/cmd/cc/pp.c index abef3c6..d1a5d83 100644 --- a/sys/cmd/cc/pp.c +++ b/sys/cmd/cc/pp.c @@ -466,11 +466,11 @@ ppinc(Lexer *lx) } } if (i == C.inc.len) { - errorat(lx->pos, "could not find included file '%s' on given search paths", s); + errorat(lx->pos, "could not find file '%s' on standard include search path", s); goto Bad; } - io = makeio(lx->buf); + io = openio(lx->buf); if (io != nil) { pushio(lx, io); } @@ -494,7 +494,7 @@ ppprag(Lexer *lx) errorat(lx->pos, "failed to parse pragma identifier"); goto Bad; } - if (strcmp(s, "once")) { + if (strcmp(s, "once") == 0) { lx->io->kind |= IOonce; return 0; } -- cgit v1.2.1