summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--pez.c46
1 files changed, 24 insertions, 22 deletions
diff --git a/pez.c b/pez.c
index bc9aaa4..d2c920b 100644
--- a/pez.c
+++ b/pez.c
@@ -72,7 +72,8 @@ typedef struct Proto {
uint8_t variadic : 1,
named : 1;
short nvars, nparams;
- uint ncode, ncon;
+ short ncon;
+ uint ncode;
int linebegin, lineend;
const char *file;
const uint8_t *code;
@@ -171,7 +172,7 @@ struct PezContext {
char errstr[140];
Val *stack, *stktop, *stkend;
Obj *heap;
- int nalloc, gcthresh, gccanrun : 1;
+ uint nalloc, gcthresh, gccanrun : 1;
StrPool strpool;
Globals globals;
};
@@ -317,8 +318,8 @@ static bool
_vecpush(PezContext *cx, void **at, size_t sz,
uint *len, uint *cap, const void *src, size_t n)
{
- if (*len + n >= *cap) {
- uint newcap = (*len + n) * 2;
+ if (*len + n - 1 >= *cap) {
+ uint newcap = (*len + n - 1) * 2;
uint8_t *new;
newcap = newcap < 4 ? 4 : newcap;
new = cxrealloc(cx, *at, *cap * sz, newcap * sz);
@@ -494,7 +495,7 @@ globals_lookup(PezContext *cx, Val key, bool put)
return NULL;
}
memset(new, 0, sizeof(struct KV) * pool->N);
- for (int i = 0; i < pool->N / 2; ++i) {
+ for (uint i = 0; i < pool->N / 2; ++i) {
struct KV kv = pool->dat[i];
if (isvoid(kv.k)) {
continue;
@@ -564,7 +565,7 @@ strpool_lookup(PezContext *cx, const char *str, int len)
return NULL;
}
memset(new, 0, sizeof(Str *) * pool->N);
- for (int i = 0; i < pool->N / 2; ++i) {
+ for (uint i = 0; i < pool->N / 2; ++i) {
Str *s0 = pool->dat[i];
if (!s0 || s0 == &strpool_deleted) {
continue;
@@ -669,7 +670,7 @@ delstring(PezContext *cx, Str *str)
/******/
static void
-freeobj(PezContext *cx, Obj *o)
+delobj(PezContext *cx, Obj *o)
{
size_t sz = 0;
switch (o->t) {
@@ -699,7 +700,7 @@ static void gcmark(PezContext *cx, Obj *o);
static void
markproto(PezContext *cx, Proto *pr)
{
- for (int i = 0; i < pr->ncon; ++i) {
+ for (uint i = 0; i < pr->ncon; ++i) {
if (isobj(pr->con[i])) {
gcmark(cx, unbox_obj(pr->con[i]));
}
@@ -715,7 +716,7 @@ markfn(PezContext *cx, Fn *fn)
static void
markarray(PezContext *cx, Array *arr)
{
- for (int i = 0; i < arr->len; ++i) {
+ for (uint i = 0; i < arr->len; ++i) {
if (isobj(arr->at[i])) {
gcmark(cx, unbox_obj(arr->at[i]));
}
@@ -757,7 +758,7 @@ gc(PezContext *cx)
}
}
if (cx->globals.dat) {
- for (int i = 0; i < cx->globals.N; ++i) {
+ for (uint i = 0; i < cx->globals.N; ++i) {
struct KV *kv = &cx->globals.dat[i];
if (!isvoid(kv->k)) {
if (isobj(kv->k)) {
@@ -784,7 +785,7 @@ gc(PezContext *cx)
if (prev) {
prev->next = next;
}
- freeobj(cx, o);
+ delobj(cx, o);
}
}
if (cx->dbg & DBGgcinfo) {
@@ -996,7 +997,7 @@ static void
inspectproto(Proto *pr)
{
int n;
- for (int i = 0; i < pr->ncon; ++i) {
+ for (uint i = 0; i < pr->ncon; ++i) {
Val k = pr->con[i];
if (isobj_of(k, PEZ_TFnProto)) {
inspectproto(unbox_obj(k));
@@ -1622,7 +1623,7 @@ static bool
xprint1(PezContext *cx, struct vals *seen,
bool (*cb)(PezContext *, void *, const char *, uint), void *u, Val v)
{
- for (int i = 0; i < seen->len; ++i) {
+ for (uint i = 0; i < seen->len; ++i) {
if (seen->at[i].r == v.r) {
char buf[20];
int n = sprintf(buf, "#%d", i);
@@ -1652,7 +1653,7 @@ xprint1(PezContext *cx, struct vals *seen,
str = ((Str *)unbox_obj(v))->dat;
}
ok &= cb(cx, u, "\"", 1);
- for (int i = 0; i < len; ++i) {
+ for (uint i = 0; i < len; ++i) {
extern int isprint(int);
if (str[i] == '\\') {
ok &= cb(cx, u, "\\\\", 2);
@@ -1689,7 +1690,7 @@ xprint1(PezContext *cx, struct vals *seen,
bool ok = 1;
TRY(vecpush(cx, seen, &v, 1));
ok &= cb(cx, u, "#[", 2);
- for (int i = 0; i < arr->len; ++i) {
+ for (uint i = 0; i < arr->len; ++i) {
ok &= xprint1(cx, seen, cb, u, arr->at[i]);
if (i != arr->len - 1) {
ok &= cb(cx, u, ", ", 2);
@@ -1722,7 +1723,7 @@ f_xprintf1(PezContext *cx, const char *fn,
}
fmtlen = pez_length(cx, args + 0);
fmt = pez_getstring(cx, sbuf, args + 0);
- for (int i = 0; i < fmtlen; ++i) {
+ for (uint i = 0; i < fmtlen; ++i) {
char c = fmt[i];
if (c == '%' && i < fmtlen - 1) {
if ((c = fmt[++i]) != '%') {
@@ -1807,7 +1808,7 @@ static const struct coredef { const char *n; PezCFn *f; } core[] = {
static bool
initcore(PezContext *cx)
{
- for (int i = 0; i < sizeof core / sizeof *core; ++i) {
+ for (size_t i = 0; i < sizeof core / sizeof *core; ++i) {
const struct coredef *def = &core[i];
Val s, f;
TRY(box_str(cx, &s, def->n, strlen(def->n)));
@@ -1937,7 +1938,7 @@ compconst(Comp *cm, Val v)
}
if (isobj(v) && (t = objtag(v)) == PEZ_TString) {
uint8_t idx = cm->con.len;
- for (int i = 0; i < cm->con.len; ++i) {
+ for (uint i = 0; i < cm->con.len; ++i) {
if (cm->con.at[i].r == v.r) {
idx = i;
goto K;
@@ -2720,7 +2721,8 @@ isimm(int *imm, Val v)
// returns true if op is commutative or can be made to be,
// (mutates op such that a b lt => b a gt, etc)
static inline bool
-commutate(enum op *op) {
+commutate(enum op *op)
+{
switch (*op) {
case Oadd: case Omul: case Oband: case Obior:
case Obxor: case Oeq: case One:
@@ -2774,7 +2776,7 @@ binexpr(Comp *cm, char okind, bool (*prev)(Comp *))
// commutative try use immediate op
// eliminate code generated for lhs
- for (int i = 0; i < save3 - save2; ++i) {
+ for (uint i = 0; i < save3 - save2; ++i) {
memmove(cm->code.at + save + i, cm->code.at + save2 + i, save2 - save);
}
cm->code.len -= (save2 - save);
@@ -3492,7 +3494,7 @@ pez_new(PezAllocFn *alloc, void *userdata, size_t stacksize)
if (!box_str(cx, &length_sstr, "length", 6)) assert(0);
if (!initcore(cx)) goto Err;
- cx->gcthresh = stacksize * sizeof(Val) * 3 + 128;
+ cx->gcthresh = stacksize * sizeof(Val)*5/2 + 128;
return cx;
@@ -3513,7 +3515,7 @@ pez_del(PezContext *cx)
assert(cx);
for (Obj *o = cx->heap, *next; o; o = next) {
next = o->next;
- freeobj(cx, o);
+ delobj(cx, o);
}
cxfree(cx, cx->stack, (cx->stkend - cx->stack) * sizeof(Val));
if (cx->strpool.dat) {