Skip to content

Commit

Permalink
remove mAbsI, absInt etc
Browse files Browse the repository at this point in the history
  • Loading branch information
timotheecour committed Dec 27, 2020
1 parent e2a658f commit d84a52c
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 36 deletions.
7 changes: 4 additions & 3 deletions compiler/ast.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 1 addition & 3 deletions compiler/ccgexprs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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)
Expand Down
2 changes: 0 additions & 2 deletions compiler/jsgen.nim
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,6 @@ const # magic checked op; magic unchecked op;
mEqProc: ["", ""],
mUnaryMinusI: ["negInt", ""],
mUnaryMinusI64: ["negInt64", ""],
mAbsI: ["absInt", ""],
mNot: ["", ""],
mUnaryPlusI: ["", ""],
mBitnotI: ["", ""],
Expand Down Expand Up @@ -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)")
Expand Down
1 change: 0 additions & 1 deletion compiler/semfold.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion compiler/vmgen.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
12 changes: 0 additions & 12 deletions lib/system/arithm.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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))
Expand Down
6 changes: 0 additions & 6 deletions lib/system/jssys.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 12 additions & 8 deletions tests/stdlib/tsystem.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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()

0 comments on commit d84a52c

Please sign in to comment.