From 0bcf3b592f32d3131d5f7c30a5680ee1e9bb80b6 Mon Sep 17 00:00:00 2001 From: dzaima Date: Tue, 20 Jul 2021 21:56:35 +0300 Subject: [PATCH] more comparison type fixing and some more / opt to make it not slower --- src/builtins/md2.c | 6 +++--- src/builtins/sfns.c | 17 ++++++++--------- src/builtins/sysfn.c | 22 +++++++++++----------- src/core/gstack.h | 2 +- src/h.h | 4 ++++ src/jit/nvm_x86_64.c | 2 +- src/main.c | 2 +- src/ns.c | 2 +- src/vm.c | 14 +++++++------- 9 files changed, 37 insertions(+), 34 deletions(-) diff --git a/src/builtins/md2.c b/src/builtins/md2.c index 6dc3a36b..b483dd24 100644 --- a/src/builtins/md2.c +++ b/src/builtins/md2.c @@ -75,7 +75,7 @@ B repeat_replace(B g, B* q) { // doesn't consume } \ i64 bound[2] = {0,0}; \ repeat_bounds(bound, g); \ - u64 min=(u64)-bound[0]; u64 max=(u64)bound[1]; \ + i64 min=(u64)-bound[0]; i64 max=(u64)bound[1]; \ TALLOC(B, all, min+max+1); \ B* q = all+min; \ q[0] = inc(x); \ @@ -86,11 +86,11 @@ B repeat_replace(B g, B* q) { // doesn't consume dec(x2); \ dec(fi); \ } \ - for (u64 i = 0; i < max; i++) q[i+1] = inc(x = CN(f, __VA_ARGS__ x)); \ + for (i64 i = 0; i < max; i++) q[i+1] = inc(x = CN(f, __VA_ARGS__ x)); \ dec(x); \ B r = repeat_replace(g, q); \ dec(g); \ - for (u64 i = 0; i < min+max+1; i++) dec(all[i]); \ + for (i64 i = 0; i < min+max+1; i++) dec(all[i]); \ END; TFREE(all); \ return r; diff --git a/src/builtins/sfns.c b/src/builtins/sfns.c index 166baff5..fa0cbcad 100644 --- a/src/builtins/sfns.c +++ b/src/builtins/sfns.c @@ -208,7 +208,7 @@ B slash_c1(B t, B x) { if (TI(x,elType)==el_i32) { i32* xp = i32any_ptr(x); while (xia>0 && !xp[xia-1]) xia--; - for (i32 i = 0; i < xia; i++) { + for (u64 i = 0; i < xia; i++) { i32 c = xp[i]; if (LIKELY(c==0 || c==1)) { *rp = i; @@ -284,8 +284,8 @@ B slash_c2(B t, B w, B x) { for (usz i = 0; i < wia; i++) { i32 cw = wp[i]; if (cw==0) continue; - B cx = xgetU(x, i); - for (i64 j = 0; j < cw; j++) r.a[ri++] = inc(cx); + B cx = incBy(xgetU(x, i), cw); + for (i64 j = 0; j < cw; j++) r.a[ri++] = cx; } dec(w); dec(x); return withFill(harr_fv(r), xf); @@ -299,8 +299,8 @@ B slash_c2(B t, B w, B x) { for (usz i = 0; i < wia; i++) { usz c = o2s(wgetU(w, i)); if (c) { - B cx = xgetU(x, i); - for (usz j = 0; j < c; j++) r.a[ri++] = inc(cx); + B cx = incBy(xgetU(x, i), c); + for (usz j = 0; RARE(j < c); j++) *r.a++ = cx; } } dec(w); dec(x); @@ -316,12 +316,11 @@ B slash_c2(B t, B w, B x) { arr_shVec(r,0); return taga(r); } - usz ri = 0; if (TI(x,elType)==el_i32) { i32* xp = i32any_ptr(x); i32* rp; B r = m_i32arrv(&rp, xia*wv); for (usz i = 0; i < xia; i++) { - for (usz j = 0; j < wv; j++) rp[ri++] = xp[i]; + for (i64 j = 0; j < wv; j++) *rp++ = xp[i]; } dec(x); return r; @@ -330,8 +329,8 @@ B slash_c2(B t, B w, B x) { HArr_p r = m_harrUv(xia*wv); BS2B xgetU = TI(x,getU); for (usz i = 0; i < xia; i++) { - B cx = xgetU(x, i); - for (usz j = 0; j < wv; j++) r.a[ri++] = inc(cx); + B cx = incBy(xgetU(x, i), wv); + for (i64 j = 0; j < wv; j++) *r.a++ = cx; } dec(x); return withFill(r.b, xf); diff --git a/src/builtins/sysfn.c b/src/builtins/sysfn.c index d5710050..80c2809c 100644 --- a/src/builtins/sysfn.c +++ b/src/builtins/sysfn.c @@ -225,7 +225,7 @@ B rand_range_c2(B t, B w, B x) { f64* rp; r = m_f64arrv(&rp, am); for (usz i = 0; i < am; i++) rp[i] = wy2u01(wyrand(&seed)); } else if (max > I32_MAX) { - if (max >= 1ULL<<53) thrM("(rand).Range: 𝕩 must be less than 2⋆53"); + if (max >= 1LL<<53) thrM("(rand).Range: 𝕩 must be less than 2⋆53"); f64* rp; r = m_f64arrv(&rp, am); for (usz i = 0; i < am; i++) rp[i] = wy2u0k(wyrand(&seed), max); } else { @@ -281,12 +281,12 @@ B rand_deal_c2(B t, B w, B x) { i64 sz = 1; while (sz < wi*2) sz*= 2; TALLOC(i32, hash, 2*sz); i32* val = hash+1; - for (u64 i = 0; i < 2*sz; i++) hash[i] = 0; + for (i64 i = 0; i < 2*sz; i++) hash[i] = 0; for (i64 i = 0; i < wi; i++) rp[i] = i; u64 mask = 2*(sz-1); for (i64 i = 0; i < wi; i++) { u64 j = wy2u0k(wyrand(&seed), xi-i) + i; - if (j xi/2; i32 wn = invert ? xi-wi : wi; - for (u64 i = 0; i < xi; i++) set[i] = 0; + for (i64 i = 0; i < xi; i++) set[i] = 0; for (i32 i = xi-wn; i < xi; i++) { i32 j = wy2u0k(wyrand(&seed), i+1); if (set[j]) j=i; set[j] = 1; } i32* rp; r = m_i32arrv(&rp, wi); - if (!invert) { for (u64 i = 0; i < xi; i++) if ( set[i]) *rp++=i; } - else { for (u64 i = 0; i < xi; i++) if (!set[i]) *rp++=i; } + if (!invert) { for (i64 i = 0; i < xi; i++) if ( set[i]) *rp++=i; } + else { for (i64 i = 0; i < xi; i++) if (!set[i]) *rp++=i; } TFREE(set); } else { // Sorted "hash" set u64 sh = 0; - for (u64 xt=xi/4; xt>=wi; xt>>=1) sh++; + for (u64 xt=xi/4; xt>=(u64)wi; xt>>=1) sh++; u64 sz = ((xi-1)>>sh)+1 + wi; TALLOC(i32, hash, sz); for (u64 i = 0; i < sz; i++) hash[i] = xi; diff --git a/src/core/gstack.h b/src/core/gstack.h index 89488ff1..4531d2b2 100644 --- a/src/core/gstack.h +++ b/src/core/gstack.h @@ -22,7 +22,7 @@ static void gsReserve(u64 am) { gStackEnd = gStackStart+n; } #elif DEBUG - if (am>gStackEnd-gStack) thrM("Stack overflow"); + if ((i64)am > gStackEnd-gStack) thrM("Stack overflow"); #endif } diff --git a/src/h.h b/src/h.h index c823d3af..e17ab0b1 100644 --- a/src/h.h +++ b/src/h.h @@ -422,6 +422,10 @@ static B inc(B x) { if (isVal(VALIDATE(x))) v(x)->refc++; return x; } +static B incBy(B x, i64 am) { + if (isVal(VALIDATE(x))) v(x)->refc+= am; + return x; +} static void ptr_inc(void* x) { VALIDATEP((Value*)x)->refc++; } diff --git a/src/jit/nvm_x86_64.c b/src/jit/nvm_x86_64.c index 824606ac..0c0914cf 100644 --- a/src/jit/nvm_x86_64.c +++ b/src/jit/nvm_x86_64.c @@ -376,7 +376,7 @@ static OptRes opt(u32* bc0) { TSADDA(rbc, sbc, ebc-sbc); } u64 added = TSSIZE(rbc)-psz; - for (i32 i = 0; i < added; i++) TSADD(roff, sbc-bc0); + for (u64 i = 0; i < added; i++) TSADD(roff, sbc-bc0); #undef A64 if (ret) break; bc = ebc; diff --git a/src/main.c b/src/main.c index ec450056..5cd2710e 100644 --- a/src/main.c +++ b/src/main.c @@ -129,7 +129,7 @@ int main(int argc, char* argv[]) { args = emptySVec(); } else { HArr_p ap = m_harrUv(argc-i); // eh whatever, erroring will exit anyways - for (usz j = 0; j < argc-i; j++) { + for (i64 j = 0; j < argc-i; j++) { ap.a[j] = fromUTF8l(argv[i+j]); } args = ap.b; diff --git a/src/ns.c b/src/ns.c index d9a254fd..e7304f25 100644 --- a/src/ns.c +++ b/src/ns.c @@ -41,7 +41,7 @@ B ns_getU(B ns, B cNL, i32 nameID) { VTY(ns, t_ns); NS* n = c(NS, ns); NSDesc* d = n->desc; i32 dVarAm = d->varAm; - assert(nameIDia && nameID>=0); + assert((u64)nameID < a(cNL)->ia && nameID>=0); B dNL = d->nameList; if (cNL.u != dNL.u) { B cName = TI(cNL,getU)(cNL, nameID); diff --git a/src/vm.c b/src/vm.c index 393ea1d6..c6054956 100644 --- a/src/vm.c +++ b/src/vm.c @@ -217,9 +217,9 @@ Block* compileBlock(B block, Comp* comp, bool* bDone, u32* bc, usz bcIA, B allBl i32 mpsc = 0; if (depth==0 && sc && vam > sc->varAm) { if (boIA==2) thrM("VM compiler: Full block info must be provided for extending scopes"); - i32 regAm = sc->varAm; + u32 regAm = sc->varAm; ScopeExt* oE = sc->ext; - if (oE==NULL || vam > regAm + oE->varAm) { + if (oE==NULL || vam > regAm+oE->varAm) { i32 nSZ = vam - regAm; ScopeExt* nE = mm_alloc(fsizeof(ScopeExt, vars, B, nSZ*2), t_scopeExt); nE->varAm = nSZ; @@ -311,7 +311,7 @@ Block* compileBlock(B block, Comp* comp, bool* bDone, u32* bc, usz bcIA, B allBl m_nsDesc(body, imm, ty, inc(nameList), biGetU(bodyRepr,2), biGetU(bodyRepr,3)); } else { body->nsDesc = NULL; - for (i32 i = 0; i < vam; i++) body->varIDs[i] = -1; + for (u64 i = 0; i < vam; i++) body->varIDs[i] = -1; } TSADD(bodies, body); @@ -697,7 +697,7 @@ Scope* m_scope(Body* body, Scope* psc, u16 varAm, i32 initVarAm, B* initVars) { static void scope_dec(Scope* sc) { i32 varAm = sc->varAm; if (sc->refc>1) { - usz innerRef = 1; + i32 innerRef = 1; for (i32 i = 0; i < varAm; i++) { B c = sc->vars[i]; if (isVal(c) && v(c)->refc==1) { @@ -707,7 +707,7 @@ static void scope_dec(Scope* sc) { else if (t==t_md2_block && c(Md2Block,c)->sc==sc) innerRef++; } } - assert(innerRef<=sc->refc); + assert(innerRef <= sc->refc); if (innerRef==sc->refc) { value_free((Value*)sc); return; @@ -958,8 +958,8 @@ NOINLINE B vm_fmtPoint(B src, B prepend, B path, usz cs, usz ce) { // consumes p ce-= srcS; ACHR('\n'); for (i64 i = padStart; i < padEnd; i++) ACHR(' '); - for (i32 i = 0; i < cs; i++) ACHR(o2cu(srcGetU(src, srcS+i))=='\t'? '\t' : ' '); // ugh tabs - for (i32 i = cs; i < ce; i++) ACHR('^'); + for (u64 i = 0; i < cs; i++) ACHR(o2cu(srcGetU(src, srcS+i))=='\t'? '\t' : ' '); // ugh tabs + for (u64 i = cs; i < ce; i++) ACHR('^'); return s; }