From d84a52c6bbd55ba725071b5dd3b132a2af4152d9 Mon Sep 17 00:00:00 2001 From: Timothee Cour Date: Sun, 27 Dec 2020 00:49:40 -0800 Subject: [PATCH] remove mAbsI, absInt etc --- compiler/ast.nim | 7 ++++--- compiler/ccgexprs.nim | 4 +--- compiler/jsgen.nim | 2 -- compiler/semfold.nim | 1 - compiler/vmgen.nim | 2 +- lib/system/arithm.nim | 12 ------------ lib/system/jssys.nim | 6 ------ tests/stdlib/tsystem.nim | 20 ++++++++++++-------- 8 files changed, 18 insertions(+), 36 deletions(-) diff --git a/compiler/ast.nim b/compiler/ast.nim index 4ca5035edc2d..c69c77e246ae 100644 --- a/compiler/ast.nim +++ b/compiler/ast.nim @@ -607,7 +607,8 @@ var ## might exclude {tfGcSafe, tfNoSideEffect}. type - TMagic* = enum # symbols that require compiler magic: + TMagic* = enum # Symbols that require compiler magic. + # These can be removed or renamed if needed. mNone, mDefined, mDeclared, mDeclaredInScope, mCompiles, mArrGet, mArrPut, mAsgn, mLow, mHigh, mSizeOf, mAlignOf, mOffsetOf, mTypeTrait, @@ -633,7 +634,7 @@ type mEqB, mLeB, mLtB, mEqRef, mLePtr, mLtPtr, mXor, mEqCString, mEqProc, - mUnaryMinusI, mUnaryMinusI64, mAbsI, mNot, + mUnaryMinusI, mUnaryMinusI64, mNot, mUnaryPlusI, mBitnotI, mUnaryPlusF64, mUnaryMinusF64, mCharToStr, mBoolToStr, mIntToStr, mInt64ToStr, mFloatToStr, mCStrToStr, @@ -702,7 +703,7 @@ const mEqCh, mLeCh, mLtCh, mEqB, mLeB, mLtB, mEqRef, mEqProc, mLePtr, mLtPtr, mEqCString, mXor, - mUnaryMinusI, mUnaryMinusI64, mAbsI, mNot, mUnaryPlusI, mBitnotI, + mUnaryMinusI, mUnaryMinusI64, mNot, mUnaryPlusI, mBitnotI, mUnaryPlusF64, mUnaryMinusF64, mCharToStr, mBoolToStr, mIntToStr, mInt64ToStr, mFloatToStr, mCStrToStr, mStrToStr, mEnumToStr, diff --git a/compiler/ccgexprs.nim b/compiler/ccgexprs.nim index ef3ceae62f9a..9f54582fe524 100644 --- a/compiler/ccgexprs.nim +++ b/compiler/ccgexprs.nim @@ -601,8 +601,6 @@ proc unaryArithOverflow(p: BProc, e: PNode, d: var TLoc, m: TMagic) = putIntoDest(p, d, e, "((NI$2)-($1))" % [rdLoc(a), rope(getSize(p.config, t) * 8)]) of mUnaryMinusI64: putIntoDest(p, d, e, "-($1)" % [rdLoc(a)]) - of mAbsI: - putIntoDest(p, d, e, "($1 > 0? ($1) : -($1))" % [rdLoc(a)]) else: assert(false, $m) @@ -2249,7 +2247,7 @@ proc genMagicExpr(p: BProc, e: PNode, d: var TLoc, op: TMagic) = case op of mOr, mAnd: genAndOr(p, e, d, op) of mNot..mUnaryMinusF64: unaryArith(p, e, d, op) - of mUnaryMinusI..mAbsI: unaryArithOverflow(p, e, d, op) + of mUnaryMinusI..mUnaryMinusI64: unaryArithOverflow(p, e, d, op) of mAddF64..mDivF64: binaryFloatArith(p, e, d, op) of mShrI..mXor: binaryArith(p, e, d, op) of mEqProc: genEqProc(p, e, d) diff --git a/compiler/jsgen.nim b/compiler/jsgen.nim index 675a24c9238e..3e9366baaec9 100644 --- a/compiler/jsgen.nim +++ b/compiler/jsgen.nim @@ -425,7 +425,6 @@ const # magic checked op; magic unchecked op; mEqProc: ["", ""], mUnaryMinusI: ["negInt", ""], mUnaryMinusI64: ["negInt64", ""], - mAbsI: ["absInt", ""], mNot: ["", ""], mUnaryPlusI: ["", ""], mBitnotI: ["", ""], @@ -647,7 +646,6 @@ proc arithAux(p: PProc, n: PNode, r: var TCompRes, op: TMagic) = of mEqProc: applyFormat("($1 == $2)", "($1 == $2)") of mUnaryMinusI: applyFormat("negInt($1)", "-($1)") of mUnaryMinusI64: applyFormat("negInt64($1)", "-($1)") - of mAbsI: applyFormat("absInt($1)", "Math.abs($1)") of mNot: applyFormat("!($1)", "!($1)") of mUnaryPlusI: applyFormat("+($1)", "+($1)") of mBitnotI: applyFormat("~($1)", "~($1)") diff --git a/compiler/semfold.nim b/compiler/semfold.nim index 958d8c14e7f5..b5cb2ecf49cc 100644 --- a/compiler/semfold.nim +++ b/compiler/semfold.nim @@ -138,7 +138,6 @@ proc evalOp(m: TMagic, n, a, b, c: PNode; g: ModuleGraph): PNode = result = newIntNodeT(toInt128(a.len), n, g) of mUnaryPlusI, mUnaryPlusF64: result = a # throw `+` away # XXX: Hides overflow/underflow - of mAbsI: result = foldAbs(getInt(a), n, g) of mSucc: result = foldAdd(getOrdValue(a), getInt(b), n, g) of mPred: result = foldSub(getOrdValue(a), getInt(b), n, g) of mAddI: result = foldAdd(getInt(a), getInt(b), n, g) diff --git a/compiler/vmgen.nim b/compiler/vmgen.nim index 2fbb78c8f303..891f3275ee08 100644 --- a/compiler/vmgen.nim +++ b/compiler/vmgen.nim @@ -1310,7 +1310,7 @@ proc genMagic(c: PCtx; n: PNode; dest: var TDest; m: TMagic) = if dest < 0: dest = c.getTemp(n.typ) c.gABC(n, opcCallSite, dest) of mNGenSym: genBinaryABC(c, n, dest, opcGenSym) - of mMinI, mMaxI, mAbsI, mDotDot: + of mMinI, mMaxI, mDotDot: c.genCall(n, dest) of mExpandToAst: if n.len != 2: diff --git a/lib/system/arithm.nim b/lib/system/arithm.nim index 64caddce8d01..041deb2d4c50 100644 --- a/lib/system/arithm.nim +++ b/lib/system/arithm.nim @@ -130,12 +130,6 @@ proc negInt64(a: int64): int64 {.compilerproc, inline.} = if a != low(int64): return -a raiseOverflow() -proc absInt64(a: int64): int64 {.compilerproc, inline.} = - if a != low(int64): - if a >= 0: return a - else: return -a - raiseOverflow() - proc divInt64(a, b: int64): int64 {.compilerproc, inline.} = if b == int64(0): raiseDivByZero() @@ -148,12 +142,6 @@ proc modInt64(a, b: int64): int64 {.compilerproc, inline.} = raiseDivByZero() return a mod b -proc absInt(a: int): int {.compilerproc, inline.} = - if a != low(int): - if a >= 0: return a - else: return -a - raiseOverflow() - const asmVersion = defined(I386) and (defined(vcc) or defined(wcc) or defined(dmc) or defined(gcc) or defined(llvm_gcc)) diff --git a/lib/system/jssys.nim b/lib/system/jssys.nim index 5f18f01cb0ac..4fa335d21c8a 100644 --- a/lib/system/jssys.nim +++ b/lib/system/jssys.nim @@ -508,12 +508,6 @@ proc nimFloatToString(a: float): cstring {.compilerproc.} = } """ -proc absInt(a: int): int {.compilerproc.} = - result = if a < 0: a*(-1) else: a - -proc absInt64(a: int64): int64 {.compilerproc.} = - result = if a < 0: a*(-1) else: a - when not defined(nimNoZeroExtendMagic): proc ze*(a: int): int {.compilerproc.} = result = a diff --git a/tests/stdlib/tsystem.nim b/tests/stdlib/tsystem.nim index 4d2eaf5d809f..5715714bb89b 100644 --- a/tests/stdlib/tsystem.nim +++ b/tests/stdlib/tsystem.nim @@ -10,14 +10,18 @@ template disableVM(body): untyped = else: body template main() = - doAssert abs(0) == 0 - doAssert abs(int.high) == int.high - doAssert abs(-3) == 3 - disableVM: # xxx bug: in vm, we get an un-recoverable program abort. - when not defined(js): # xxx bug: these should raise in js - doAssertRaises(OverflowDefect): discard abs(int8.low) - doAssertRaises(OverflowDefect): discard abs(int32.low) - doAssertRaises(OverflowDefect): discard abs(int64.low) + block: # abs + doAssert abs(0) == 0 + doAssert abs(int.high) == int.high + doAssert abs(-3) == 3 + disableVM: # xxx bug: in vm, we get an un-recoverable program abort. + when not defined(js): # xxx bug: these should raise in js + doAssertRaises(OverflowDefect): discard abs(int8.low) + doAssertRaises(OverflowDefect): discard abs(int32.low) + doAssertRaises(OverflowDefect): discard abs(int64.low) + + let a = low(int) + doAssertRaises(OverflowDefect): discard -a static: main() main()