[refactoring] remove zeroExtend and friends from the compiler builtins. (#11531)

* remove zeroExtend and friends from the compiler builtins.
* fix jssys

(cherry picked from commit e90d91f0e4)
This commit is contained in:
Arne Döring
2019-06-24 09:19:02 +02:00
committed by narimiran
parent 06904a212a
commit a5a62b562f
10 changed files with 76 additions and 116 deletions

View File

@@ -621,10 +621,6 @@ type
mUnaryMinusI, mUnaryMinusI64, mAbsI, mNot,
mUnaryPlusI, mBitnotI,
mUnaryPlusF64, mUnaryMinusF64, mAbsF64,
mZe8ToI, mZe8ToI64,
mZe16ToI, mZe16ToI64,
mZe32ToI64, mZeIToI64,
mToU8, mToU16, mToU32,
mToFloat, mToBiggestFloat,
mToInt, mToBiggestInt,
mCharToStr, mBoolToStr, mIntToStr, mInt64ToStr, mFloatToStr, mCStrToStr,
@@ -695,10 +691,6 @@ const
mEqRef, mEqProc, mEqUntracedRef, mLePtr, mLtPtr, mEqCString, mXor,
mUnaryMinusI, mUnaryMinusI64, mAbsI, mNot, mUnaryPlusI, mBitnotI,
mUnaryPlusF64, mUnaryMinusF64, mAbsF64,
mZe8ToI, mZe8ToI64,
mZe16ToI, mZe16ToI64,
mZe32ToI64, mZeIToI64,
mToU8, mToU16, mToU32,
mToFloat, mToBiggestFloat,
mToInt, mToBiggestInt,
mCharToStr, mBoolToStr, mIntToStr, mInt64ToStr, mFloatToStr, mCStrToStr,

View File

@@ -639,28 +639,6 @@ proc genIsNil(p: BProc, e: PNode, d: var TLoc) =
unaryExpr(p, e, d, "($1 == 0)")
proc unaryArith(p: BProc, e: PNode, d: var TLoc, op: TMagic) =
const
unArithTab: array[mNot..mToBiggestInt, string] = [
"!($1)", # Not
"$1", # UnaryPlusI
"($3)((NU$2) ~($1))", # BitnotI
"$1", # UnaryPlusF64
"-($1)", # UnaryMinusF64
"($1 < 0? -($1) : ($1))", # AbsF64; BUGFIX: fabs() makes problems
# for Tiny C, so we don't use it
"(($3)(NU)(NU8)($1))", # mZe8ToI
"(($3)(NU64)(NU8)($1))", # mZe8ToI64
"(($3)(NU)(NU16)($1))", # mZe16ToI
"(($3)(NU64)(NU16)($1))", # mZe16ToI64
"(($3)(NU64)(NU32)($1))", # mZe32ToI64
"(($3)(NU64)(NU)($1))", # mZeIToI64
"(($3)(NU8)(NU)($1))", # ToU8
"(($3)(NU16)(NU)($1))", # ToU16
"(($3)(NU32)(NU64)($1))", # ToU32
"((double) ($1))", # ToFloat
"((double) ($1))", # ToBiggestFloat
"float64ToInt32($1)", # ToInt
"float64ToInt64($1)"] # ToBiggestInt
var
a: TLoc
t: PType
@@ -671,7 +649,6 @@ proc unaryArith(p: BProc, e: PNode, d: var TLoc, op: TMagic) =
template applyFormat(frmt: untyped) =
putIntoDest(p, d, e, frmt % [rdLoc(a), rope(getSize(p.config, t) * 8),
getSimpleTypeDesc(p.module, e.typ)])
case op
of mNot:
applyFormat("!($1)")
@@ -686,24 +663,6 @@ proc unaryArith(p: BProc, e: PNode, d: var TLoc, op: TMagic) =
of mAbsF64:
applyFormat("($1 < 0? -($1) : ($1))")
# BUGFIX: fabs() makes problems for Tiny C
of mZe8ToI:
applyFormat("(($3)(NU)(NU8)($1))")
of mZe8ToI64:
applyFormat("(($3)(NU64)(NU8)($1))")
of mZe16ToI:
applyFormat("(($3)(NU)(NU16)($1))")
of mZe16ToI64:
applyFormat("(($3)(NU64)(NU16)($1))")
of mZe32ToI64:
applyFormat("(($3)(NU64)(NU32)($1))")
of mZeIToI64:
applyFormat("(($3)(NU64)(NU)($1))")
of mToU8:
applyFormat("(($3)(NU8)(NU)($1))")
of mToU16:
applyFormat("(($3)(NU16)(NU)($1))")
of mToU32:
applyFormat("(($3)(NU32)(NU64)($1))")
of mToFloat:
applyFormat("((double) ($1))")
of mToBiggestFloat:

View File

@@ -86,6 +86,7 @@ proc initDefines*(symbols: StringTableRef) =
defineSymbol("nimHasSignatureHashInMacro")
defineSymbol("nimHasDefault")
defineSymbol("nimMacrosSizealignof")
defineSymbol("nimNoZeroExtendMagic")
for f in low(Feature)..high(Feature):
defineSymbol("nimHas" & $f)

View File

@@ -431,15 +431,6 @@ const # magic checked op; magic unchecked op;
["", ""], # UnaryPlusF64
["", ""], # UnaryMinusF64
["", ""], # AbsF64
["Ze8ToI", "Ze8ToI"], # mZe8ToI
["Ze8ToI64", "Ze8ToI64"], # mZe8ToI64
["Ze16ToI", "Ze16ToI"], # mZe16ToI
["Ze16ToI64", "Ze16ToI64"], # mZe16ToI64
["Ze32ToI64", "Ze32ToI64"], # mZe32ToI64
["ZeIToI64", "ZeIToI64"], # mZeIToI64
["toU8", "toU8"], # toU8
["toU16", "toU16"], # toU16
["toU32", "toU32"], # toU32
["", ""], # ToFloat
["", ""], # ToBiggestFloat
["", ""], # ToInt
@@ -621,15 +612,6 @@ proc arithAux(p: PProc, n: PNode, r: var TCompRes, op: TMagic) =
of mUnaryPlusF64: applyFormat("+($1)", "+($1)")
of mUnaryMinusF64: applyFormat("-($1)", "-($1)")
of mAbsF64: applyFormat("Math.abs($1)", "Math.abs($1)")
of mZe8ToI: applyFormat("Ze8ToI($1)", "Ze8ToI($1)")
of mZe8ToI64: applyFormat("Ze8ToI64($1)", "Ze8ToI64($1)")
of mZe16ToI: applyFormat("Ze16ToI($1)", "Ze16ToI($1)")
of mZe16ToI64: applyFormat("Ze16ToI64($1)", "Ze16ToI64($1)")
of mZe32ToI64: applyFormat("Ze32ToI64($1)", "Ze32ToI64($1)")
of mZeIToI64: applyFormat("ZeIToI64($1)", "ZeIToI64($1)")
of mtoU8: applyFormat("toU8($1)", "toU8($1)")
of mtoU16: applyFormat("toU16($1)", "toU16($1)")
of mtoU32: applyFormat("toU32($1)", "toU32($1)")
of mToFloat: applyFormat("$1", "$1")
of mToBiggestFloat: applyFormat("$1", "$1")
of mToInt: applyFormat("Math.trunc($1)", "Math.trunc($1)")

View File

@@ -219,12 +219,6 @@ proc evalOp(m: TMagic, n, a, b, c: PNode; g: ModuleGraph): PNode =
of mToInt, mToBiggestInt: result = newIntNodeT(system.toInt(getFloat(a)), n, g)
of mAbsF64: result = newFloatNodeT(abs(getFloat(a)), n, g)
of mAbsI: result = foldAbs(getInt(a), n, g)
of mZe8ToI, mZe8ToI64, mZe16ToI, mZe16ToI64, mZe32ToI64, mZeIToI64:
# byte(-128) = 1...1..1000_0000'64 --> 0...0..1000_0000'64
result = newIntNodeT(getInt(a) and (`shl`(1, getSize(g.config, a.typ) * 8) - 1), n, g)
of mToU8: result = newIntNodeT(getInt(a) and 0x000000FF, n, g)
of mToU16: result = newIntNodeT(getInt(a) and 0x0000FFFF, n, g)
of mToU32: result = newIntNodeT(getInt(a) and 0x00000000FFFFFFFF'i64, n, g)
of mUnaryLt: result = doAndFit(foldSub(getOrdValue(a), 1, n, g))
of mSucc: result = doAndFit(foldAdd(getOrdValue(a), getInt(b), n, g))
of mPred: result = doAndFit(foldSub(getOrdValue(a), getInt(b), n, g))

View File

@@ -1905,16 +1905,6 @@ proc rawExecute(c: PCtx, start: int, tos: PStackFrame): TFullReg =
when not defined(nimNoNilSeqs):
if regs[ra].node.strVal.isNil: regs[ra].node.strVal = newStringOfCap(1000)
storeAny(regs[ra].node.strVal, typ, regs[rb].regToNode, c.config)
of opcToNarrowInt:
decodeBC(rkInt)
let mask = (1'i64 shl rc) - 1 # 0xFF
let signbit = 1'i64 shl (rc - 1) # 0x80
let toggle = mask - signbit # 0x7F
# algorithm: -((i8 and 0xFF) xor 0x7F) + 0x7F
# mask off higher bits.
# uses two's complement to sign-extend integer.
# reajust integer into desired range.
regs[ra].intVal = -((regs[rb].intVal and mask) xor toggle) + toggle
inc pc

View File

@@ -149,7 +149,6 @@ type
opcSetType, # dest.typ = types[Bx]
opcTypeTrait,
opcMarshalLoad, opcMarshalStore,
opcToNarrowInt,
opcSymOwner,
opcSymIsInstantiationOf

View File

@@ -1077,21 +1077,6 @@ proc genMagic(c: PCtx; n: PNode; dest: var TDest; m: TMagic) =
mToBiggestInt, mCharToStr, mBoolToStr, mIntToStr, mInt64ToStr,
mFloatToStr, mCStrToStr, mStrToStr, mEnumToStr:
genConv(c, n, n.sons[1], dest)
of mZe8ToI, mZe8ToI64, mZe16ToI, mZe16ToI64, mZe32ToI64, mZeIToI64:
#genNarrowU modified
let t = skipTypes(n.sons[1].typ, abstractVar-{tyTypeDesc})
let tmp = c.genx(n.sons[1])
c.gABC(n, opcNarrowU, tmp, TRegister(t.size*8))
# assign result to dest register
if dest < 0: dest = c.getTemp(n.typ)
c.gABC(n, opcAsgnInt, dest, tmp)
c.freeTemp(tmp)
of mToU8, mToU16, mToU32:
let t = skipTypes(n.typ, abstractVar-{tyTypeDesc})
var tmp = c.genx(n.sons[1])
if dest < 0: dest = c.getTemp(n.typ)
c.gABC(n, opcToNarrowInt, dest, tmp, TRegister(t.size*8))
c.freeTemp(tmp)
of mEqStr, mEqCString: genBinaryABC(c, n, dest, opcEqStr)
of mLeStr: genBinaryABC(c, n, dest, opcLeStr)
of mLtStr: genBinaryABC(c, n, dest, opcLtStr)

View File

@@ -1116,7 +1116,64 @@ proc chr*(u: range[0..255]): char {.magic: "Chr", noSideEffect.}
# --------------------------------------------------------------------------
# built-in operators
when not defined(JS):
when defined(nimNoZeroExtendMagic):
proc ze*(x: int8): int =
## zero extends a smaller integer type to ``int``. This treats `x` as
## unsigned.
## **Deprecated since version 0.19.9**: Use unsigned integers instead.
cast[int](uint(cast[uint8](x)))
proc ze*(x: int16): int =
## zero extends a smaller integer type to ``int``. This treats `x` as
## unsigned.
## **Deprecated since version 0.19.9**: Use unsigned integers instead.
cast[int](uint(cast[uint16](x)))
proc ze64*(x: int8): int64 =
## zero extends a smaller integer type to ``int64``. This treats `x` as
## unsigned.
## **Deprecated since version 0.19.9**: Use unsigned integers instead.
cast[int64](uint64(cast[uint8](x)))
proc ze64*(x: int16): int64 =
## zero extends a smaller integer type to ``int64``. This treats `x` as
## unsigned.
## **Deprecated since version 0.19.9**: Use unsigned integers instead.
cast[int64](uint64(cast[uint16](x)))
proc ze64*(x: int32): int64 =
## zero extends a smaller integer type to ``int64``. This treats `x` as
## unsigned.
## **Deprecated since version 0.19.9**: Use unsigned integers instead.
cast[int64](uint64(cast[uint32](x)))
proc ze64*(x: int): int64 =
## zero extends a smaller integer type to ``int64``. This treats `x` as
## unsigned. Does nothing if the size of an ``int`` is the same as ``int64``.
## (This is the case on 64 bit processors.)
## **Deprecated since version 0.19.9**: Use unsigned integers instead.
cast[int64](uint64(cast[uint](x)))
proc toU8*(x: int): int8 =
## treats `x` as unsigned and converts it to a byte by taking the last 8 bits
## from `x`.
## **Deprecated since version 0.19.9**: Use unsigned integers instead.
cast[int8](x)
proc toU16*(x: int): int16 =
## treats `x` as unsigned and converts it to an ``int16`` by taking the last
## 16 bits from `x`.
## **Deprecated since version 0.19.9**: Use unsigned integers instead.
cast[int16](x)
proc toU32*(x: int64): int32 =
## treats `x` as unsigned and converts it to an ``int32`` by taking the
## last 32 bits from `x`.
## **Deprecated since version 0.19.9**: Use unsigned integers instead.
cast[int32](x)
elif not defined(JS):
proc ze*(x: int8): int {.magic: "Ze8ToI", noSideEffect, deprecated.}
## zero extends a smaller integer type to ``int``. This treats `x` as
## unsigned.

View File

@@ -475,26 +475,27 @@ proc absInt(a: int): int {.compilerproc.} =
proc absInt64(a: int64): int64 {.compilerproc.} =
result = if a < 0: a*(-1) else: a
proc ze*(a: int): int {.compilerproc.} =
result = a
when not defined(nimNoZeroExtendMagic):
proc ze*(a: int): int {.compilerproc.} =
result = a
proc ze64*(a: int64): int64 {.compilerproc.} =
result = a
proc ze64*(a: int64): int64 {.compilerproc.} =
result = a
proc toU8*(a: int): int8 {.asmNoStackFrame, compilerproc.} =
asm """
return `a`;
"""
proc toU8*(a: int): int8 {.asmNoStackFrame, compilerproc.} =
asm """
return `a`;
"""
proc toU16*(a: int): int16 {.asmNoStackFrame, compilerproc.} =
asm """
return `a`;
"""
proc toU16*(a: int): int16 {.asmNoStackFrame, compilerproc.} =
asm """
return `a`;
"""
proc toU32*(a: int64): int32 {.asmNoStackFrame, compilerproc.} =
asm """
return `a`;
"""
proc toU32*(a: int64): int32 {.asmNoStackFrame, compilerproc.} =
asm """
return `a`;
"""
proc nimMin(a, b: int): int {.compilerproc.} = return if a <= b: a else: b
proc nimMax(a, b: int): int {.compilerproc.} = return if a >= b: a else: b