Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

write system.abs in nim, not magic #485

Draft
wants to merge 5 commits into
base: devel
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
28 changes: 11 additions & 17 deletions lib/system.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1891,6 +1891,17 @@ else:
# Autodetect coroutine support.
const nimCoroutines* = false

{.push stackTrace: off.} # pre-existing but seems un-necessary
func abs*[T: SomeSignedInt](x: T): T {.inline.} =
## Returns the absolute value of `x`.
# xxx consider consolidating this with `abs(float)` overload.
runnableExamples:
doAssert abs(-3) == 3
doAssertRaises(OverflowDefect): discard abs(int8.low)
doAssertRaises(OverflowDefect): discard abs(int64.low)
if x < 0: -x else: x
{.pop.}

{.push checks: off.}
# obviously we cannot generate checking operations here :-)
# because it would yield into an endless recursion
Expand Down Expand Up @@ -2028,23 +2039,6 @@ proc getTypeInfo*[T](x: T): pointer {.magic: "GetTypeInfo", benign.}
## Ordinary code should not use this, but the `typeinfo module
## <typeinfo.html>`_ instead.

{.push stackTrace: off.}
func abs*(x: int): int {.magic: "AbsI", inline.} =
if x < 0: -x else: x
func abs*(x: int8): int8 {.magic: "AbsI", inline.} =
if x < 0: -x else: x
func abs*(x: int16): int16 {.magic: "AbsI", inline.} =
if x < 0: -x else: x
func abs*(x: int32): int32 {.magic: "AbsI", inline.} =
if x < 0: -x else: x
func abs*(x: int64): int64 {.magic: "AbsI", inline.} =
## Returns the absolute value of `x`.
##
## If `x` is ``low(x)`` (that is -MININT for its type),
## an overflow exception is thrown (if overflow checking is turned on).
result = if x < 0: -x else: x
{.pop.}

when not defined(js):

proc likelyProc(val: bool): bool {.importc: "NIM_LIKELY", nodecl, noSideEffect.}
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
30 changes: 30 additions & 0 deletions tests/stdlib/tsystem.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
discard """
joinable: false
targets: "c cpp js"
"""

# xxx consolidate / refactor with tests/system/tsystem_misc.nim, etc

template disableVM(body): untyped =
when nimvm: discard
else: body

template main() =
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)

block: # `-`
let a = low(int)
disableVM:
when not defined(js):
doAssertRaises(OverflowDefect): discard -a

static: main()
main()