ref: 9057331b1a5952440d7e48e29492a4f7cfd111a3
parent: 357b2411f4cb3d37ff54e8a7a13ae36d387a1bb8
author: Ori Bernstein <[email protected]>
date: Mon Jun 11 14:01:14 EDT 2012
Integer correctness fixes. We were mixing signed and unsigned comparisons. This can lead to weird edge cases that nobody should have to reason about. Don't do that.
--- a/8/isel.c
+++ b/8/isel.c
@@ -322,7 +322,7 @@
Loc gencall(Isel *s, Node *n)
{
int argsz, argoff;
- int i;
+ size_t i;
Loc eax, esp; /* hard-coded registers */
Loc stkbump; /* calculated stack offset */
Loc dst, arg, fn; /* values we reduced */
@@ -707,7 +707,7 @@
static void writeasm(Func *fn, Isel *is, FILE *fd)
{
- int i;
+ size_t i;
if (fn->isglobl)
fprintf(fd, ".globl %s\n", fn->name);
@@ -722,7 +722,7 @@
void genasm(FILE *fd, Func *fn, Htab *globls)
{
struct Isel is = {0,};
- int i;
+ size_t i;
is.locs = fn->locs;
is.globls = globls;
--- a/8/reduce.c
+++ b/8/reduce.c
@@ -72,7 +72,7 @@
static char *asmname(Node *n)
{
- int i;
+ size_t i;
char *s;
char *sep;
int len;
@@ -94,7 +94,7 @@
size_t tysize(Type *t)
{
size_t sz;
- int i;
+ size_t i;
sz = 0;
switch (t->type) {
@@ -245,7 +245,7 @@
void simpblk(Simp *s, Node *n)
{
- int i;
+ size_t i;
for (i = 0; i < n->block.nstmts; i++) {
simp(s, n->block.stmts[i]);
@@ -256,7 +256,7 @@
{
Type *ty;
Node **nl;
- int nn, i;
+ size_t nn, i;
size_t off;
if (aggr->expr.type->type == Typtr)
@@ -395,7 +395,7 @@
{
Node *r; /* expression result */
Node *t, *u, *v; /* temporary nodes */
- int i;
+ size_t i;
Node **args;
const Op fusedmap[] = {
[Oaddeq] = Oadd,
@@ -541,7 +541,7 @@
Node *simp(Simp *s, Node *n)
{
Node *r;
- int i;
+ size_t i;
if (!n)
return NULL;
@@ -583,7 +583,7 @@
void reduce(Simp *s, Node *f)
{
- int i;
+ size_t i;
assert(f->type == Nfunc);
s->nstmts = 0;
@@ -605,7 +605,7 @@
static void lowerfn(char *name, Node *n, Htab *globls, FILE *fd)
{
- int i;
+ size_t i;
Simp s = {0,};
Func fn;
Cfg *cfg;
@@ -622,8 +622,8 @@
reduce(&s, n);
if (debug)
- for (i = 0; i < s.nstmts; i++)
- dump(s.stmts[i], stdout);
+ for (i = 0; i < s.nstmts; i++)
+ dump(s.stmts[i], stdout);
cfg = mkcfg(s.stmts, s.nstmts);
if (debug)
dumpcfg(cfg, stdout);
@@ -640,7 +640,7 @@
void blobdump(Blob *b, FILE *fd)
{
- int i;
+ size_t i;
char *p;
p = b->data;
--- a/mk/c.mk
+++ b/mk/c.mk
@@ -1,7 +1,7 @@
DEPSDIR = .deps
DEPS=$(addprefix $(DEPSDIR)/, $(OBJ:.o=.d))
-CFLAGS += -Wall -Werror
+CFLAGS += -Wall -Werror -Wextra -Wno-unused-parameter -Wno-missing-field-initializers
CFLAGS += -g
CFLAGS += -MMD -MP -MF ${DEPSDIR}/$(subst /,-,$*).d
--- a/opt/cfg.c
+++ b/opt/cfg.c
@@ -50,12 +50,12 @@
return 0;
}
-Cfg *mkcfg(Node **nl, int nn)
+Cfg *mkcfg(Node **nl, size_t nn)
{
Cfg *cfg;
Bb *bb, *targ;
Node *a, *b;
- int i;
+ size_t i;
cfg = zalloc(sizeof(Cfg));
cfg->lblmap = mkht(strhash, streq);
@@ -116,7 +116,7 @@
}
void dumpcfg(Cfg *cfg, FILE *fd)
{
- int i, j;
+ size_t i, j;
Bb *bb;
char *sep;
@@ -125,7 +125,7 @@
fprintf(fd, "\n");
fprintf(fd, "Bb: %d labels=(", bb->id);
sep = "";
- for (i = 0; i < bb->nlbls; i++) {
+ for (i = 0; i < bb->nlbls; i++) {;
fprintf(fd, "%s%s", bb->lbls[i], sep);
sep = ",";
}
@@ -136,7 +136,7 @@
sep = "";
for (i = 0; i < bsmax(bb->in); i++) {
if (bshas(bb->in, i)) {
- fprintf(fd, "%s%d", sep, i);
+ fprintf(fd, "%s%zd", sep, i);
sep = ",";
}
}
@@ -147,7 +147,7 @@
sep = "";
for (i = 0; i < bsmax(bb->out); i++) {
if (bshas(bb->out, i)) {
- fprintf(fd, "%s%d", sep, i);
+ fprintf(fd, "%s%zd", sep, i);
sep = ",";
}
}
--- a/opt/opt.h
+++ b/opt/opt.h
@@ -24,6 +24,6 @@
};
/* Takes a reduced block, and returns a flow graph. */
-Cfg *mkcfg(Node **nl, int nn);
+Cfg *mkcfg(Node **nl, size_t nn);
void dumpcfg(Cfg *c, FILE *fd);
void flow(Cfg *cfg);
--- a/parse/bitset.c
+++ b/parse/bitset.c
@@ -44,9 +44,9 @@
return bs;
}
-int bscount(Bitset *bs)
+size_t bscount(Bitset *bs)
{
- int i, j, n;
+ size_t i, j, n;
n = 0;
for (i = 0; i < bs->nchunks; i++)
@@ -59,7 +59,7 @@
/* Returns the largest value that the bitset can possibly
* hold. It's conservative, but scanning the entire bitset
* is a bit slow. This is mostly an aid to iterate over it. */
-int bsmax(Bitset *bs)
+size_t bsmax(Bitset *bs)
{
return bs->nchunks*sizeof(uint)*CHAR_BIT;
}
@@ -97,7 +97,7 @@
void bsunion(Bitset *a, Bitset *b)
{
- int i;
+ size_t i;
eqsz(a, b);
for (i = 0; i < a->nchunks; i++)
@@ -106,7 +106,7 @@
void bsintersect(Bitset *a, Bitset *b)
{
- int i;
+ size_t i;
eqsz(a, b);
for (i = 0; i < a->nchunks; i++)
@@ -115,7 +115,7 @@
void bsdiff(Bitset *a, Bitset *b)
{
- int i;
+ size_t i;
eqsz(a, b);
for (i = 0; i < a->nchunks; i++)
@@ -124,7 +124,7 @@
int bsissubset(Bitset *set, Bitset *sub)
{
- int i;
+ size_t i;
eqsz(set, sub);
for (i = 0; i < set->nchunks; i++)
--- a/parse/dump.c
+++ b/parse/dump.c
@@ -20,9 +20,9 @@
static void outname(Node *n, FILE *fd)
{
- int i;
+ size_t i;
char *sep;
-
+
sep = "";
for (i = 0; i < n->name.nparts; i++) {
fprintf(fd, "%s%s", sep, n->name.parts[i]);
@@ -91,7 +91,7 @@
static void outnode(Node *n, FILE *fd, int depth)
{
- int i;
+ size_t i;
char *ty;
indent(fd, depth);
--- a/parse/htab.c
+++ b/parse/htab.c
@@ -127,7 +127,7 @@
void **htkeys(Htab *ht, int *nkeys)
{
void **k;
- int i, j;
+ size_t i, j;
j = 0;
k = xalloc(sizeof(void*)*ht->nelt);
@@ -171,4 +171,3 @@
{
return a == b;
}
-
--- a/parse/infer.c
+++ b/parse/infer.c
@@ -32,7 +32,7 @@
static void tyresolve(Type *t)
{
- int i, nn;
+ size_t i, nn;
Node **n;
if (t->resolved)
@@ -83,7 +83,8 @@
static void loaduses(Node *n)
{
- int i;
+ size_t i;
+
/* uses only allowed at top level. Do we want to keep it this way? */
for (i = 0; i < n->file.nuses; i++)
fprintf(stderr, "INTERNAL: implement use loading\n");
@@ -182,7 +183,7 @@
{
Type *t;
Type *r;
- int i;
+ size_t i;
/* a ==> b */
a = tf(a);
@@ -218,7 +219,7 @@
static void unifycall(Node *n)
{
- int i;
+ size_t i;
Type *ft;
inferexpr(n->expr.args[0], NULL, NULL);
@@ -377,7 +378,7 @@
static void inferfunc(Node *n)
{
- int i;
+ size_t i;
int sawret;
sawret = 0;
@@ -416,7 +417,7 @@
static void infernode(Node *n, Type *ret, int *sawret)
{
- int i;
+ size_t i;
if (!n)
return;
@@ -482,7 +483,7 @@
static Type *tyfin(Node *ctx, Type *t)
{
static Type *tyint;
- int i;
+ size_t i;
char buf[1024];
if (!tyint)
@@ -507,7 +508,7 @@
static void infercompn(Node *file)
{
- int i, j, nn;
+ size_t i, j, nn;
Node *aggr;
Node *memb;
Node *n;
@@ -532,7 +533,7 @@
static void typesub(Node *n)
{
- int i;
+ size_t i;
if (!n)
return;
@@ -600,4 +601,3 @@
checkcast(file);
typesub(file);
}
-
--- a/parse/node.c
+++ b/parse/node.c
@@ -61,7 +61,7 @@
Node *mkcall(int line, Node *fn, Node **args, size_t nargs)
{
Node *n;
- int i;
+ size_t i;
n = mkexpr(line, Ocall, fn, NULL);
for (i = 0; i < nargs; i++)
@@ -98,7 +98,7 @@
{
Node *n;
Node *f;
- int i;
+ size_t i;
f = mknode(line, Nfunc);
f->func.args = args;
@@ -258,7 +258,7 @@
return e->expr.op;
}
-Node **aggrmemb(Type *t, int *n)
+Node **aggrmemb(Type *t, size_t *n)
{
*n = t->nmemb;
switch (t->type) {
--- a/parse/parse.h
+++ b/parse/parse.h
@@ -53,7 +53,7 @@
} Dclflags;
struct Bitset {
- int nchunks;
+ size_t nchunks;
uint *chunks;
};
@@ -228,13 +228,13 @@
void delbs(Bitset *bs);
void bsput(Bitset *bs, uint elt);
void bsdel(Bitset *bs, uint elt);
-int bshas(Bitset *bs, uint elt);
void bsunion(Bitset *a, Bitset *b);
void bsintersect(Bitset *a, Bitset *b);
void bsdiff(Bitset *a, Bitset *b);
-int bscount(Bitset *bs);
-int bsmax(Bitset *bs);
+int bshas(Bitset *bs, uint elt);
int bsissubset(Bitset *set, Bitset *sub);
+size_t bscount(Bitset *bs);
+size_t bsmax(Bitset *bs);
Htab *mkht(ulong (*hash)(void *key), int (*cmp)(void *k1, void *k2));
int htput(Htab *ht, void *k, void *v);
@@ -335,7 +335,7 @@
void addstmt(Node *file, Node *stmt);
void setns(Node *n, char *name);
Op exprop(Node *n);
-Node **aggrmemb(Type *t, int *n);
+Node **aggrmemb(Type *t, size_t *n);
char *namestr(Node *name);
/* usefiles */
--- a/parse/pickle.c
+++ b/parse/pickle.c
@@ -20,30 +20,49 @@
static void wrsym(FILE *fd, Sym *val);
static Sym *rdsym(FILE *fd);
-static vlong be64(vlong v)
+static void be64(vlong v, char buf[8])
{
- if (htonl(42) != 42)
- return ((vlong)htonl(v >> 32) << 32) | htonl((uint32_t)v);
- else
- return v;
+ buf[0] = (v >> 56) & 0xff;
+ buf[1] = (v >> 48) & 0xff;
+ buf[2] = (v >> 40) & 0xff;
+ buf[3] = (v >> 32) & 0xff;
+ buf[4] = (v >> 24) & 0xff;
+ buf[5] = (v >> 16) & 0xff;
+ buf[6] = (v >> 8) & 0xff;
+ buf[7] = (v >> 0) & 0xff;
}
-static vlong host64(vlong v)
+static vlong host64(char buf[8])
{
- if (htonl(42) != 42) /* we need to swap */
- return (vlong)ntohl(v >> 32) << 32 | htonl((uint32_t)v);
- else
- return v;
+ vlong v = 0;
+
+ v |= ((vlong)buf[0] << 56LL) & 0xff;
+ v |= ((vlong)buf[1] << 48LL) & 0xff;
+ v |= ((vlong)buf[2] << 40LL) & 0xff;
+ v |= ((vlong)buf[3] << 32LL) & 0xff;
+ v |= ((vlong)buf[4] << 24LL) & 0xff;
+ v |= ((vlong)buf[5] << 16LL) & 0xff;
+ v |= ((vlong)buf[6] << 8LL) & 0xff;
+ v |= ((vlong)buf[7] << 0LL) & 0xff;
+ return v;
}
-static long be32(long v)
+static void be32(long v, char buf[4])
{
- return htonl(v);
+ buf[0] = (v >> 24) & 0xff;
+ buf[1] = (v >> 16) & 0xff;
+ buf[2] = (v >> 8) & 0xff;
+ buf[3] = (v >> 0) & 0xff;
}
-static long host32(long v)
+static long host32(char buf[4])
{
- return ntohl(v);
+ long v = 0;
+ v |= (buf[4] << 24) & 0xff;
+ v |= (buf[5] << 16) & 0xff;
+ v |= (buf[6] << 8) & 0xff;
+ v |= (buf[7] << 0) & 0xff;
+ return v;
}
static void wrbyte(FILE *fd, char val)
@@ -63,34 +82,42 @@
static void wrint(FILE *fd, int32_t val)
{
- val = be32(val);
- if (fwrite(&val, sizeof(int32_t), 1, fd) == EOF)
+ char buf[4];
+ be32(val, buf);
+ if (fwrite(buf, 4, 1, fd) < 4)
die("Unexpected EOF");
}
static int32_t rdint(FILE *fd)
{
- uint32_t val;
-
- if (fread(&val, sizeof(uint32_t), 1, fd) == EOF)
+ char buf[4];
+ if (fread(buf, sizeof(uint32_t), 1, fd) < 4)
die("Unexpected EOF");
- return host32(val);
+ return host32(buf);
}
static void wrstr(FILE *fd, char *val)
{
+ size_t len;
+ size_t n;
+
if (!val) {
wrint(fd, -1);
} else {
wrint(fd, strlen(val));
- if (fwrite(val, strlen(val), 1, fd) == EOF)
- die("Unexpected EOF");
+ len = strlen(val);
+ n = 0;
+ while (n < len) {
+ n += fwrite(val, len - n, 1, fd);
+ if (feof(fd) || ferror(fd))
+ die("Unexpected EOF");
+ }
}
}
/*static */char *rdstr(FILE *fd)
{
- int len;
+ ssize_t len;
char *s;
len = rdint(fd);
@@ -98,7 +125,7 @@
return NULL;
} else {
s = xalloc(len + 1);
- if (fread(s, len, 1, fd) == EOF)
+ if (fread(s, len, 1, fd) != (size_t)len)
die("Unexpected EOF");
s[len] = '\0';
return s;
@@ -107,6 +134,7 @@
static void wrflt(FILE *fd, double val)
{
+ char buf[8];
/* Assumption: We have 'val' in 64 bit IEEE format */
union {
uvlong ival;
@@ -114,21 +142,22 @@
} u;
u.fval = val;
- u.ival = be64(u.ival);
- if (fwrite(&u.ival, sizeof(uvlong), 1, fd) == EOF)
+ be64(u.ival, buf);
+ if (fwrite(buf, 8, 1, fd) < 8)
die("Unexpected EOF");
}
/*static */double rdflt(FILE *fd)
{
+ char buf[8];
union {
uvlong ival;
double fval;
} u;
- if (fread(&u.ival, sizeof(uvlong), 1, fd) == EOF)
+ if (fread(buf, 8, 1, fd) < 8)
die("Unexpected EOF");
- u.ival = host64(u.ival);
+ u.ival = host64(buf);
return u.fval;
}
@@ -226,7 +255,7 @@
static void wrtype(FILE *fd, Type *ty)
{
- int i;
+ size_t i;
if (!ty) {
die("trying to pickle null type\n");
@@ -280,7 +309,7 @@
{
Type *ty;
Ty t;
- int i;
+ size_t i;
t = rdbyte(fd);
ty = mkty(-1, t);
@@ -338,7 +367,7 @@
*/
void pickle(Node *n, FILE *fd)
{
- int i;
+ size_t i;
if (!n) {
wrbyte(fd, Nnone);
@@ -429,7 +458,7 @@
Node *unpickle(FILE *fd)
{
- int i;
+ size_t i;
Ntype type;
Node *n;
--- a/parse/type.c
+++ b/parse/type.c
@@ -146,7 +146,7 @@
Type *mktyfunc(int line, Node **args, size_t nargs, Type *ret)
{
Type *t;
- int i;
+ size_t i;
t = mkty(line, Tyfunc);
t->nsub = nargs + 1;
@@ -207,7 +207,7 @@
static int namefmt(char *buf, size_t len, Node *name)
{
- int i;
+ size_t i;
char *p;
char *end;
char *sep;
@@ -261,7 +261,7 @@
static int fmtstruct(char *buf, size_t len, Type *t)
{
- int i;
+ size_t i;
char *end, *p;
char *name, *ty;
@@ -280,7 +280,7 @@
static int fmtunion(char *buf, size_t len, Type *t)
{
- int i;
+ size_t i;
*buf = 0;
for (i = 0; i < t->nmemb; i++)
dump(t->sdecls[i], stdout);
@@ -289,7 +289,7 @@
static int fmtenum(char *buf, size_t len, Type *t)
{
- int i;
+ size_t i;
*buf = 0;
for (i = 0; i < t->nmemb; i++)
dump(t->sdecls[i], stdout);
@@ -298,9 +298,9 @@
static int tybfmt(char *buf, size_t len, Type *t)
{
+ size_t i;
char *p;
char *end;
- int i;
char *sep;
p = buf;