Skip to content

Commit

Permalink
[refactoring] refactor the compiler and stdlib to deprecation warnings (
Browse files Browse the repository at this point in the history
#11419)

(cherry picked from commit c7e1c66)
  • Loading branch information
krux02 authored and narimiran committed Jun 16, 2019
1 parent 7e6c3f3 commit 4c8a021
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 44 deletions.
34 changes: 19 additions & 15 deletions compiler/bitsets.nim
Expand Up @@ -11,12 +11,17 @@
# the code here should be reused in the Nim standard library

type
TBitSet* = seq[int8] # we use byte here to avoid issues with
ElemType = byte
TBitSet* = seq[ElemType] # we use byte here to avoid issues with
# cross-compiling; uint would be more efficient
# however

const
ElemSize* = sizeof(int8) * 8
ElemSize* = 8
One = ElemType(1)
Zero = ElemType(0)

template modElemSize(arg: untyped): untyped = arg and 7
template divElemSize(arg: untyped): untyped = arg shr 3

proc bitSetInit*(b: var TBitSet, length: int)
proc bitSetUnion*(x: var TBitSet, y: TBitSet)
Expand All @@ -32,17 +37,16 @@ proc bitSetCard*(x: TBitSet): BiggestInt
# implementation

proc bitSetIn(x: TBitSet, e: BiggestInt): bool =
result = (x[int(e div ElemSize)] and toU8(int(1 shl (e mod ElemSize)))) !=
toU8(0)
result = (x[int(e.divElemSize)] and (One shl e.modElemSize)) != Zero

proc bitSetIncl(x: var TBitSet, elem: BiggestInt) =
assert(elem >= 0)
x[int(elem div ElemSize)] = x[int(elem div ElemSize)] or
toU8(int(1 shl (elem mod ElemSize)))
x[int(elem.divElemSize)] = x[int(elem.divElemSize)] or
(One shl elem.modElemSize)

proc bitSetExcl(x: var TBitSet, elem: BiggestInt) =
x[int(elem div ElemSize)] = x[int(elem div ElemSize)] and
not toU8(int(1 shl (elem mod ElemSize)))
x[int(elem.divElemSize)] = x[int(elem.divElemSize)] and
not(One shl elem.modElemSize)

proc bitSetInit(b: var TBitSet, length: int) =
newSeq(b, length)
Expand All @@ -67,13 +71,13 @@ proc bitSetEquals(x, y: TBitSet): bool =

proc bitSetContains(x, y: TBitSet): bool =
for i in 0 .. high(x):
if (x[i] and not y[i]) != int8(0):
if (x[i] and not y[i]) != Zero:
return false
result = true

# Number of set bits for all values of int8
const populationCount: array[low(int8)..high(int8), int8] = block:
var arr: array[low(int8)..high(int8), int8]
const populationCount: array[uint8, uint8] = block:
var arr: array[uint8, uint8]

proc countSetBits(x: uint8): uint8 =
return
Expand All @@ -87,11 +91,11 @@ const populationCount: array[low(int8)..high(int8), int8] = block:
((x and 0b10000000'u8) shr 7)


for it in low(int8)..high(int8):
arr[it] = cast[int8](countSetBits(cast[uint8](it)))
for it in low(uint8)..high(uint8):
arr[it] = countSetBits(cast[uint8](it))

arr

proc bitSetCard(x: TBitSet): BiggestInt =
for it in x:
result.inc populationCount[it]
result.inc int(populationCount[it])
24 changes: 14 additions & 10 deletions compiler/ccgexprs.nim
Expand Up @@ -93,26 +93,30 @@ proc genLiteral(p: BProc, n: PNode, ty: PType): Rope =
proc genLiteral(p: BProc, n: PNode): Rope =
result = genLiteral(p, n, n.typ)

proc bitSetToWord(s: TBitSet, size: int): BiggestInt =
proc bitSetToWord(s: TBitSet, size: int): BiggestUInt =
result = 0
for j in 0 ..< size:
if j < len(s): result = result or (ze64(s[j]) shl (j * 8))
if j < len(s): result = result or (BiggestUInt(s[j]) shl (j * 8))

proc genRawSetData(cs: TBitSet, size: int): Rope =
if size > 8:
result = "{$n" % []
var res = "{\n"
for i in 0 ..< size:
res.add "0x"
res.add "0123456789abcdef"[cs[i] div 16]
res.add "0123456789abcdef"[cs[i] mod 16]
if i < size - 1:
# not last iteration?
if (i + 1) mod 8 == 0:
addf(result, "0x$1,$n", [rope(toHex(ze64(cs[i]), 2))])
# not last iteration
if i mod 8 == 7:
res.add ",\n"
else:
addf(result, "0x$1, ", [rope(toHex(ze64(cs[i]), 2))])
res.add ", "
else:
addf(result, "0x$1}$n", [rope(toHex(ze64(cs[i]), 2))])
res.add "}\n"

result = rope(res)
else:
result = intLiteral(bitSetToWord(cs, size))
# result := rope('0x' + ToHex(bitSetToWord(cs, size), size * 2))
result = intLiteral(cast[BiggestInt](bitSetToWord(cs, size)))

proc genSetNode(p: BProc, n: PNode): Rope =
var cs: TBitSet
Expand Down
12 changes: 6 additions & 6 deletions compiler/lexer.nim
Expand Up @@ -534,13 +534,13 @@ proc getNumber(L: var TLexer, result: var TToken) =

case result.tokType
of tkIntLit, tkInt64Lit: result.iNumber = xi
of tkInt8Lit: result.iNumber = BiggestInt(int8(toU8(int(xi))))
of tkInt16Lit: result.iNumber = BiggestInt(int16(toU16(int(xi))))
of tkInt32Lit: result.iNumber = BiggestInt(int32(toU32(int64(xi))))
of tkInt8Lit: result.iNumber = ashr(xi shl 56, 56)
of tkInt16Lit: result.iNumber = ashr(xi shl 48, 48)
of tkInt32Lit: result.iNumber = ashr(xi shl 32, 32)
of tkUIntLit, tkUInt64Lit: result.iNumber = xi
of tkUInt8Lit: result.iNumber = BiggestInt(cast[uint8](toU8(int(xi))))
of tkUInt16Lit: result.iNumber = BiggestInt(cast[uint16](toU16(int(xi))))
of tkUInt32Lit: result.iNumber = BiggestInt(cast[uint32](toU32(int64(xi))))
of tkUInt8Lit: result.iNumber = xi and 0xff
of tkUInt16Lit: result.iNumber = xi and 0xffff
of tkUInt32Lit: result.iNumber = xi and 0xffffffff
of tkFloat32Lit:
result.fNumber = (cast[PFloat32](addr(xi)))[]
# note: this code is endian neutral!
Expand Down
4 changes: 2 additions & 2 deletions lib/pure/collections/sets.nim
Expand Up @@ -61,7 +61,7 @@ when not defined(nimhygiene):
type
KeyValuePair[A] = tuple[hcode: Hash, key: A]
KeyValuePairSeq[A] = seq[KeyValuePair[A]]
HashSet* {.myShallow.} [A] = object ## \
HashSet*[A] {.myShallow.} = object ## \
## A generic hash set.
##
## Use `init proc <#init,HashSet[A],int>`_ or `initHashSet proc <#initHashSet,int>`_
Expand All @@ -73,7 +73,7 @@ type
OrderedKeyValuePair[A] = tuple[
hcode: Hash, next: int, key: A]
OrderedKeyValuePairSeq[A] = seq[OrderedKeyValuePair[A]]
OrderedSet* {.myShallow.} [A] = object ## \
OrderedSet*[A] {.myShallow.} = object ## \
## A generic hash set that remembers insertion order.
##
## Use `init proc <#init,OrderedSet[A],int>`_ or `initOrderedSet proc
Expand Down
16 changes: 8 additions & 8 deletions lib/system.nim
Expand Up @@ -3646,21 +3646,21 @@ when not defined(JS): #and not defined(nimscript):
const GenericSeqSize = (2 * sizeof(int))

when not defined(nimV2):
proc getDiscriminant(aa: pointer, n: ptr TNimNode): int =
proc getDiscriminant(aa: pointer, n: ptr TNimNode): uint =
sysAssert(n.kind == nkCase, "getDiscriminant: node != nkCase")
var d: int
var a = cast[ByteAddress](aa)
var d: uint
var a = cast[uint](aa)
case n.typ.size
of 1: d = ze(cast[ptr int8](a +% n.offset)[])
of 2: d = ze(cast[ptr int16](a +% n.offset)[])
of 4: d = int(cast[ptr int32](a +% n.offset)[])
of 8: d = int(cast[ptr int64](a +% n.offset)[])
of 1: d = uint(cast[ptr uint8](a + uint(n.offset))[])
of 2: d = uint(cast[ptr uint16](a + uint(n.offset))[])
of 4: d = uint(cast[ptr uint32](a + uint(n.offset))[])
of 8: d = uint(cast[ptr uint64](a + uint(n.offset))[])
else: sysAssert(false, "getDiscriminant: invalid n.typ.size")
return d

proc selectBranch(aa: pointer, n: ptr TNimNode): ptr TNimNode =
var discr = getDiscriminant(aa, n)
if discr <% n.len:
if discr < cast[uint](n.len):
result = n.sons[discr]
if result == nil: result = n.sons[n.len]
# n.sons[n.len] contains the ``else`` part (but may be nil)
Expand Down
4 changes: 2 additions & 2 deletions lib/system/repr.nim
Expand Up @@ -78,7 +78,7 @@ proc reprEnum(e: int, typ: PNimType): string {.compilerRtl.} =
result = $e & " (invalid data!)"

type
PByteArray = ptr array[0xffff, int8]
PByteArray = ptr array[0xffff, byte]

proc addSetElem(result: var string, elem: int, typ: PNimType) {.benign.} =
case typ.kind
Expand All @@ -104,7 +104,7 @@ proc reprSetAux(result: var string, p: pointer, typ: PNimType) =
else:
var a = cast[PByteArray](p)
for i in 0 .. typ.size*8-1:
if (ze(a[i div 8]) and (1 shl (i mod 8))) != 0:
if (uint(a[i shr 3]) and (1'u shl (i and 7))) != 0:
if elemCounter > 0: add result, ", "
addSetElem(result, i+typ.node.len, typ.base)
inc(elemCounter)
Expand Down
2 changes: 1 addition & 1 deletion tests/errmsgs/tproper_stacktrace.nim
Expand Up @@ -22,7 +22,7 @@ proc matchStackTrace(actualEntries: openarray[StackTraceEntry], expected: string
var line: int
if not scanf(l, "$s$w.nim($i) $w", filename, line, procname):
doAssert(false, "Wrong expected stack trace")
checkEqual($actualEntries[i].filename, filename & ".nim", "file name")
checkEqual(actualEntries[i].filename.`$`.split('/')[^1], filename & ".nim", "file name")
if line != 0:
checkEqual(actualEntries[i].line, line, "line number")
checkEqual($actualEntries[i].procname, procname, "proc name")
Expand Down

0 comments on commit 4c8a021

Please sign in to comment.