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 --- include/libn.h | 5 +++++ include/libn/macro/map.h | 23 +++++++++++++---------- sys/cmd/cc/cc.c | 38 +++++++++++++++++++++++--------------- sys/cmd/cc/cc.h | 2 ++ sys/cmd/cc/lex.c | 27 ++++++++++++++++++++++++--- sys/cmd/cc/pp.c | 6 +++--- sys/libn/bufio.c | 15 +++++---------- sys/libn/io.c | 2 +- 8 files changed, 76 insertions(+), 42 deletions(-) diff --git a/include/libn.h b/include/libn.h index 87c993c..8e21056 100644 --- a/include/libn.h +++ b/include/libn.h @@ -76,6 +76,11 @@ void *·alloc(void* _, uint n, ulong size) { return malloc(n*size); } +static +void *·calloc(void* _, uint n, ulong size) { + return calloc(n, size); +} + // TODO(nnoll): Allow for nil iterfaces? static mem·Allocator mem·sys = { diff --git a/include/libn/macro/map.h b/include/libn/macro/map.h index 45eb745..bbd4a91 100644 --- a/include/libn/macro/map.h +++ b/include/libn/macro/map.h @@ -62,10 +62,12 @@ static const double __ac_HASH_UPPER = 0.77; while (!__ac_isempty(map->flags, i) && \ (__ac_isdel(map->flags, i) || !equalfunc(map->keys[i], key))) { \ i = (i + (++step)) & mask; \ - if (i == last) \ + if (i == last) { \ i = map->n_buckets; \ + break; \ + } \ } \ - if (__ac_iseither(map->flags, i)) \ + if (i < map->n_buckets && __ac_iseither(map->flags, i)) \ i = map->n_buckets; \ } else \ i = 0; @@ -80,25 +82,24 @@ static const double __ac_HASH_UPPER = 0.77; if (map->size >= (int32)(new_n_buckets * __ac_HASH_UPPER + 0.5)) \ j = 0; \ else { \ - new_flags = \ - alloc(h, __ac_fsize(new_n_buckets), sizeof(int32)); \ + new_flags = alloc(h, __ac_fsize(new_n_buckets), sizeof(int32)); \ if (!new_flags) return -1; \ memset(new_flags, 0xaa, __ac_fsize(new_n_buckets) * sizeof(int32)); \ if (map->n_buckets < new_n_buckets) { /* expand */ \ - key_t *new_keys = \ - alloc(h, new_n_buckets, sizeof(key_t)); \ + key_t *new_keys = alloc(h, new_n_buckets, sizeof(key_t)); \ if (!new_keys) { \ free(h, new_flags); \ return -1; \ } \ + memcpy(new_keys, map->keys, sizeof(key_t) * map->n_buckets); \ free(h, map->keys); \ map->keys = new_keys; \ - val_t *new_vals = \ - alloc(h, new_n_buckets, sizeof(val_t)); \ + val_t *new_vals = alloc(h, new_n_buckets, sizeof(val_t)); \ if (!new_vals) { \ free(h, new_flags); \ return -1; \ } \ + memcpy(new_vals, map->vals, sizeof(val_t) * map->n_buckets); \ free(h, map->vals); \ map->vals = new_vals; \ } \ @@ -142,16 +143,18 @@ static const double __ac_HASH_UPPER = 0.77; } \ if (map->n_buckets > new_n_buckets) { /* shrink the hash table */ \ key_t *new_keys = alloc(h, new_n_buckets, sizeof(key_t)); \ + memcpy(new_keys, map->keys, sizeof(key_t) * map->n_buckets); \ free(h, map->keys); \ map->keys = new_keys; \ \ val_t *new_vals = alloc(h, new_n_buckets, sizeof(val_t)); \ + memcpy(new_vals, map->vals, sizeof(val_t) * map->n_buckets); \ free(h, map->vals); \ map->vals = new_vals; \ } \ free(h, map->flags); /* free the working space */ \ - map->flags = new_flags; \ - map->n_buckets = new_n_buckets; \ + map->flags = new_flags; \ + map->n_buckets = new_n_buckets; \ map->n_occupied = map->size; \ map->upper_bound = (int32)(map->n_buckets * __ac_HASH_UPPER + 0.5); \ } \ 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; } diff --git a/sys/libn/bufio.c b/sys/libn/bufio.c index ac8e175..05b6068 100644 --- a/sys/libn/bufio.c +++ b/sys/libn/bufio.c @@ -18,7 +18,7 @@ bufio·initreader(io·Buffer *buf, io·Reader rdr, void *h) buf->beg = buf->buf + bufio·ungets; buf->pos = buf->beg; buf->end = buf->pos; - buf->size = buf->end - buf->beg; + buf->size = bufio·size - bufio·ungets; return 0; } @@ -40,8 +40,9 @@ refill(io·Buffer *buf) if (buf->state & bufio·end) { return bufio·err; } + memcpy(buf->buf, buf->pos - bufio·ungets, bufio·ungets); - n = buf->rdr.read(buf->h, 1, buf->size, buf->buf); + n = buf->rdr.read(buf->h, 1, buf->size, buf->beg); if (n < 0) return bufio·err; if (n == 0) { @@ -49,14 +50,8 @@ refill(io·Buffer *buf) return 0; } - if (n < buf->size) { - d = buf->size - n; - - buf->state |= bufio·end; - - memmove(buf->pos + d, buf->pos, n); - memmove(buf->pos + d - bufio·ungets, buf->buf, bufio·ungets); - } + buf->pos = buf->beg; + buf->end = buf->pos + n; return n; } diff --git a/sys/libn/io.c b/sys/libn/io.c index 2852f42..08c29b0 100644 --- a/sys/libn/io.c +++ b/sys/libn/io.c @@ -25,7 +25,7 @@ io·stat(Stream *s, io·Stat *buf) int io·exists(byte *path, int flag) { - return access(path, flag); + return access(path, flag) == 0; } error -- cgit v1.2.1