Skip to content
Open
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
37 changes: 16 additions & 21 deletions lib/core/typeinfo.nim
Original file line number Diff line number Diff line change
Expand Up @@ -134,19 +134,17 @@ else:
proc zeroNewElements(len: int; p: pointer; addlen, elemSize, elemAlign: int) {.
importCompilerProc.}

template `+!!`(a, b): untyped = cast[pointer](cast[int](a) + b)
include system/ptrarith

proc getDiscriminant(aa: pointer, n: ptr TNimNode): int =
assert(n.kind == nkCase)
var d: int
let a = cast[int](aa)
let a = aa +! n.offset
case n.typ.size
of 1: d = int(cast[ptr uint8](a +% n.offset)[])
of 2: d = int(cast[ptr uint16](a +% n.offset)[])
of 4: d = int(cast[ptr uint32](a +% n.offset)[])
of 8: d = int(cast[ptr uint64](a +% n.offset)[])
of 1: int(cast[ptr uint8](a)[])
of 2: int(cast[ptr uint16](a)[])
of 4: int(cast[ptr uint32](a)[])
of 8: cast[int](cast[ptr uint64](a)[])
else: raiseAssert "unreachable"
return d

proc selectBranch(aa: pointer, n: ptr TNimNode): ptr TNimNode =
let discr = getDiscriminant(aa, n)
Expand Down Expand Up @@ -240,17 +238,14 @@ proc skipRange(x: PNimType): PNimType {.inline.} =
result = x
if result.kind == tyRange: result = result.base

proc align(address, alignment: int): int =
result = (address + (alignment - 1)) and not (alignment - 1)

proc `[]`*(x: Any, i: int): Any =
## Accessor for an any `x` that represents an array or a sequence.
case x.rawType.kind
of tyArray:
let bs = x.rawType.base.size
if i >=% x.rawType.size div bs:
raise newException(IndexDefect, formatErrorIndexBound(i, x.rawType.size div bs))
return newAny(x.value +!! i*bs, x.rawType.base)
return newAny(x.value +! i*bs, x.rawType.base)
of tySequence:
when defined(gcDestructors):
var s = cast[ptr NimSeqV2Reimpl](x.value)
Expand All @@ -259,14 +254,14 @@ proc `[]`*(x: Any, i: int): Any =
let bs = x.rawType.base.size
let ba = x.rawType.base.align
let headerSize = align(sizeof(int), ba)
return newAny(s.p +!! (headerSize+i*bs), x.rawType.base)
return newAny(s.p +! (headerSize+i*bs), x.rawType.base)
else:
var s = cast[ppointer](x.value)[]
if s == nil: raise newException(ValueError, "sequence is nil")
let bs = x.rawType.base.size
if i >=% cast[PGenSeq](s).len:
raise newException(IndexDefect, formatErrorIndexBound(i, cast[PGenSeq](s).len-1))
return newAny(s +!! (align(GenericSeqSize, x.rawType.base.align)+i*bs), x.rawType.base)
return newAny(s +! (align(GenericSeqSize, x.rawType.base.align)+i*bs), x.rawType.base)
else: raiseAssert "unreachable"

proc `[]=`*(x: Any, i: int, y: Any) =
Expand All @@ -277,7 +272,7 @@ proc `[]=`*(x: Any, i: int, y: Any) =
if i >=% x.rawType.size div bs:
raise newException(IndexDefect, formatErrorIndexBound(i, x.rawType.size div bs))
assert y.rawType == x.rawType.base
genericAssign(x.value +!! i*bs, y.value, y.rawType)
genericAssign(x.value +! i*bs, y.value, y.rawType)
of tySequence:
when defined(gcDestructors):
var s = cast[ptr NimSeqV2Reimpl](x.value)
Expand All @@ -287,15 +282,15 @@ proc `[]=`*(x: Any, i: int, y: Any) =
let ba = x.rawType.base.align
let headerSize = align(sizeof(int), ba)
assert y.rawType == x.rawType.base
genericAssign(s.p +!! (headerSize+i*bs), y.value, y.rawType)
genericAssign(s.p +! (headerSize+i*bs), y.value, y.rawType)
else:
var s = cast[ppointer](x.value)[]
if s == nil: raise newException(ValueError, "sequence is nil")
var bs = x.rawType.base.size
if i >=% cast[PGenSeq](s).len:
raise newException(IndexDefect, formatErrorIndexBound(i, cast[PGenSeq](s).len-1))
assert y.rawType == x.rawType.base
genericAssign(s +!! (align(GenericSeqSize, x.rawType.base.align)+i*bs), y.value, y.rawType)
genericAssign(s +! (align(GenericSeqSize, x.rawType.base.align)+i*bs), y.value, y.rawType)
else: raiseAssert "unreachable"

proc len*(x: Any): int =
Expand Down Expand Up @@ -352,13 +347,13 @@ proc fieldsAux(p: pointer, n: ptr TNimNode,
case n.kind
of nkNone: assert(false)
of nkSlot:
ret.add((n.name, newAny(p +!! n.offset, n.typ)))
ret.add((n.name, newAny(p +! n.offset, n.typ)))
assert ret[ret.len()-1][0] != nil
of nkList:
for i in 0..n.len-1: fieldsAux(p, n.sons[i], ret)
of nkCase:
var m = selectBranch(p, n)
ret.add((n.name, newAny(p +!! n.offset, n.typ)))
ret.add((n.name, newAny(p +! n.offset, n.typ)))
if m != nil: fieldsAux(p, m, ret)

iterator fields*(x: Any): tuple[name: string, any: Any] =
Expand Down Expand Up @@ -409,7 +404,7 @@ proc `[]=`*(x: Any, fieldName: string, value: Any) =
let n = getFieldNode(x.value, t.node, fieldName)
if n != nil:
assert n.typ == value.rawType
genericAssign(x.value +!! n.offset, value.value, value.rawType)
genericAssign(x.value +! n.offset, value.value, value.rawType)
else:
raise newException(ValueError, "invalid field name: " & fieldName)

Expand All @@ -422,7 +417,7 @@ proc `[]`*(x: Any, fieldName: string): Any =
assert x.rawType.kind in {tyTuple, tyObject}
let n = getFieldNode(x.value, t.node, fieldName)
if n != nil:
result = Any(value: x.value +!! n.offset)
result = Any(value: x.value +! n.offset)
result.rawType = n.typ
elif x.rawType.kind == tyObject and x.rawType.base != nil:
return `[]`(newAny(x.value, x.rawType.base), fieldName)
Expand Down
9 changes: 0 additions & 9 deletions lib/std/private/dragonbox.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1043,15 +1043,6 @@ proc toDecimal64*(ieeeSignificand: uint64; ieeeExponent: uint64): FloatingDecima
# ToChars
# ==================================================================================================

when false:
template `+!`(x: cstring; offset: int): cstring = cast[cstring](cast[uint](x) + uint(offset))

template dec(x: cstring; offset=1) = x = cast[cstring](cast[uint](x) - uint(offset))
template inc(x: cstring; offset=1) = x = cast[cstring](cast[uint](x) + uint(offset))

proc memset(x: cstring; ch: char; L: int) {.importc, nodecl.}
proc memmove(a, b: cstring; L: int) {.importc, nodecl.}

proc utoa8DigitsSkipTrailingZeros*(buf: var openArray[char]; pos: int; digits: uint32): int {.inline.} =
dragonbox_Assert(digits >= 1)
dragonbox_Assert(digits <= 99999999'u32)
Expand Down
7 changes: 1 addition & 6 deletions lib/system.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1132,12 +1132,7 @@ import std/private/since
import system/ctypes
export ctypes

proc align(address, alignment: int): int =
if alignment == 0: # Actually, this is illegal. This branch exists to actively
# hide problems.
result = address
else:
result = (address + (alignment - 1)) and not (alignment - 1)
include system/ptrarith

include system/rawquits
when defined(genode):
Expand Down
6 changes: 3 additions & 3 deletions lib/system/arc.nim
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,9 @@ when defined(gcAtomicArc) and hasThreadSupport:
atomicLoadN(x.rc.addr, ATOMIC_ACQUIRE) shr rcShift
else:
template decrement(cell: Cell): untyped =
dec(cell.rc, rcIncrement)
cell.rc = cell.rc -% rcIncrement
template increment(cell: Cell): untyped =
inc(cell.rc, rcIncrement)
cell.rc = cell.rc +% rcIncrement
template count(x: Cell): untyped =
x.rc shr rcShift
Expand All @@ -94,7 +94,7 @@ when not defined(nimHasQuirky):
proc nimNewObj(size, alignment: int): pointer {.compilerRtl.} =
let hdrSize = align(sizeof(RefHeader), alignment)
let s = size + hdrSize
let s = size +% hdrSize
when defined(nimscript):
discard
else:
Expand Down
8 changes: 4 additions & 4 deletions lib/system/cellseqs_v1.nim
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ proc contains(s: CellSeq, c: PCell): bool {.inline.} =
return false

proc resize(s: var CellSeq) =
s.cap = s.cap div 2 + s.cap
let d = cast[PCellArray](alloc(s.cap * sizeof(PCell)))
copyMem(d, s.d, s.len * sizeof(PCell))
s.cap = s.cap div 2 +% s.cap
let d = cast[PCellArray](alloc(uint(s.cap *% sizeof(PCell))))
copyMem(d, s.d, s.len *% sizeof(PCell))
dealloc(s.d)
s.d = d

Expand All @@ -37,7 +37,7 @@ proc add(s: var CellSeq, c: PCell) {.inline.} =
proc init(s: var CellSeq, cap: int = 1024) =
s.len = 0
s.cap = cap
s.d = cast[PCellArray](alloc0(cap * sizeof(PCell)))
s.d = cast[PCellArray](alloc0(cap *% sizeof(PCell)))

proc deinit(s: var CellSeq) =
dealloc(s.d)
Expand Down
15 changes: 8 additions & 7 deletions lib/system/cellseqs_v2.nim
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ type
d: CellArray[T]

proc resize[T](s: var CellSeq[T]) =
s.cap = s.cap div 2 + s.cap
var newSize = s.cap * sizeof(CellTuple[T])
s.cap = s.cap div 2 +% s.cap
let newSize = uint(s.cap *% sizeof(CellTuple[T]))
when compileOption("threads"):
s.d = cast[CellArray[T]](reallocShared(s.d, newSize))
else:
Expand All @@ -28,15 +28,15 @@ proc add[T](s: var CellSeq[T], c: T, t: PNimTypeV2) {.inline.} =
if s.len >= s.cap:
s.resize()
s.d[s.len] = (c, t)
inc(s.len)
s.len = s.len +% 1

proc init[T](s: var CellSeq[T], cap: int = 1024) =
s.len = 0
s.cap = cap
when compileOption("threads"):
s.d = cast[CellArray[T]](allocShared(uint(s.cap * sizeof(CellTuple[T]))))
s.d = cast[CellArray[T]](allocShared(uint(s.cap *% sizeof(CellTuple[T]))))
else:
s.d = cast[CellArray[T]](alloc(s.cap * sizeof(CellTuple[T])))
s.d = cast[CellArray[T]](alloc(uint(s.cap *% sizeof(CellTuple[T]))))

proc deinit[T](s: var CellSeq[T]) =
if s.d != nil:
Expand All @@ -49,5 +49,6 @@ proc deinit[T](s: var CellSeq[T]) =
s.cap = 0

proc pop[T](s: var CellSeq[T]): (T, PNimTypeV2) =
result = s.d[s.len-1]
dec s.len
let last = s.len -% 1
s.len = last
s.d[last]
4 changes: 0 additions & 4 deletions lib/system/channels_builtin.nim
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,6 @@ proc deinitRawChannel(p: pointer) =
deinitSysCond(c.cond)

when not usesDestructors:

proc storeAux(dest, src: pointer, mt: PNimType, t: PRawChannel,
mode: LoadStoreMode) {.benign.}

Expand All @@ -203,9 +202,6 @@ when not usesDestructors:

proc storeAux(dest, src: pointer, mt: PNimType, t: PRawChannel,
mode: LoadStoreMode) =
template `+!`(p: pointer; x: int): pointer =
cast[pointer](cast[int](p) +% x)

var
d = cast[int](dest)
s = cast[int](src)
Expand Down
4 changes: 2 additions & 2 deletions lib/system/gc.nim
Original file line number Diff line number Diff line change
Expand Up @@ -173,11 +173,11 @@ proc addZCT(s: var CellSeq, c: PCell) {.noinline.} =

proc cellToUsr(cell: PCell): pointer {.inline.} =
# convert object (=pointer to refcount) to pointer to userdata
result = cast[pointer](cast[int](cell)+%ByteAddress(sizeof(Cell)))
result = cast[pointer](cast[uint](cell)+uint(sizeof(Cell)))

proc usrToCell(usr: pointer): PCell {.inline.} =
# convert pointer to userdata to object (=pointer to refcount)
result = cast[PCell](cast[int](usr)-%ByteAddress(sizeof(Cell)))
result = cast[PCell](cast[uint](usr)-uint(sizeof(Cell)))

proc extGetCellType(c: pointer): PNimType {.compilerproc.} =
# used for code generation concerning debugging
Expand Down
4 changes: 2 additions & 2 deletions lib/system/gc_ms.nim
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,11 @@ template gcAssert(cond: bool, msg: string) =

proc cellToUsr(cell: PCell): pointer {.inline.} =
# convert object (=pointer to refcount) to pointer to userdata
result = cast[pointer](cast[int](cell)+%ByteAddress(sizeof(Cell)))
result = cast[pointer](cast[uint](cell)+uint(sizeof(Cell)))

proc usrToCell(usr: pointer): PCell {.inline.} =
# convert pointer to userdata to object (=pointer to refcount)
result = cast[PCell](cast[int](usr)-%ByteAddress(sizeof(Cell)))
result = cast[PCell](cast[uint](usr)-uint(sizeof(Cell)))
proc extGetCellType(c: pointer): PNimType {.compilerproc.} =
# used for code generation concerning debugging
Expand Down
10 changes: 2 additions & 8 deletions lib/system/gc_regions.nim
Original file line number Diff line number Diff line change
Expand Up @@ -101,16 +101,10 @@ template withRegion*(r: var MemRegion; body: untyped) =
tlRegion = oldRegion

template inc(p: pointer, s: int) =
p = cast[pointer](cast[int](p) +% s)
p = p +! s

template dec(p: pointer, s: int) =
p = cast[pointer](cast[int](p) -% s)

template `+!`(p: pointer, s: int): pointer =
cast[pointer](cast[int](p) +% s)

template `-!`(p: pointer, s: int): pointer =
cast[pointer](cast[int](p) -% s)
p = p -! s

const nimMinHeapPages {.intdefine.} = 4

Expand Down
34 changes: 15 additions & 19 deletions lib/system/memalloc.nim
Original file line number Diff line number Diff line change
Expand Up @@ -319,12 +319,6 @@ when hasAlloc and not defined(js):

include bitmasks

template `+!`(p: pointer, s: SomeInteger): pointer =
cast[pointer](cast[int](p) +% int(s))

template `-!`(p: pointer, s: SomeInteger): pointer =
cast[pointer](cast[int](p) -% int(s))

proc alignedAlloc(size, align: Natural): pointer =
if align <= MemAlign:
when compileOption("threads"):
Expand All @@ -334,15 +328,16 @@ when hasAlloc and not defined(js):
else:
# allocate (size + align - 1) necessary for alignment,
# plus 2 bytes to store offset
when compileOption("threads"):
let base = allocShared(size + align - 1 + sizeof(uint16))
else:
let base = alloc(size + align - 1 + sizeof(uint16))
let base =
when compileOption("threads"):
allocShared(uint(size +% align -% 1 +% sizeof(uint16)))
else:
alloc(uint(size +% align -% 1 +% sizeof(uint16)))
# memory layout: padding + offset (2 bytes) + user_data
# in order to deallocate: read offset at user_data - 2 bytes,
# then deallocate user_data - offset
let offset = align - (cast[int](base) and (align - 1))
cast[ptr uint16](base +! (offset - sizeof(uint16)))[] = uint16(offset)
let offset = align -% cast[int](cast[uint](base) and uint(align -% 1))
cast[ptr uint16](base +! (offset -% sizeof(uint16)))[] = uint16(offset)
result = base +! offset

proc alignedAlloc0(size, align: Natural): pointer =
Expand All @@ -353,12 +348,13 @@ when hasAlloc and not defined(js):
result = alloc0(size)
else:
# see comments for alignedAlloc
when compileOption("threads"):
let base = allocShared0(size + align - 1 + sizeof(uint16))
else:
let base = alloc0(size + align - 1 + sizeof(uint16))
let offset = align - (cast[int](base) and (align - 1))
cast[ptr uint16](base +! (offset - sizeof(uint16)))[] = uint16(offset)
let base =
when compileOption("threads"):
allocShared0(size +% align -% 1 +% sizeof(uint16))
else:
alloc0(size +% align -% 1 +% sizeof(uint16))
let offset = align -% cast[int](cast[uint](base) and uint(align -% 1))
cast[ptr uint16](base +! (offset -% sizeof(uint16)))[] = uint16(offset)
result = base +! offset

proc alignedDealloc(p: pointer, align: int) {.compilerproc.} =
Expand Down Expand Up @@ -395,7 +391,7 @@ when hasAlloc and not defined(js):
else:
result = alignedAlloc(newSize, align)
copyMem(result, p, oldSize)
zeroMem(result +! oldSize, newSize - oldSize)
zeroMem(result +! oldSize, newSize -% oldSize)
alignedDealloc(p, align)

{.pop.}
Expand Down
Loading
Loading