diff options
author | Andreas Rumpf <ar@kimeta.de> | 2014-04-10 01:47:20 +0200 |
---|---|---|
committer | Andreas Rumpf <ar@kimeta.de> | 2014-04-10 01:47:20 +0200 |
commit | dcfc7a8896166563f6fd80fbab81bc50e0b5a217 (patch) | |
tree | 4a247ec2d64bafec53e5ab24b0fda89642908a07 /lib/system | |
parent | a86c774932afd8b6782df1925837df9df04ef381 (diff) | |
parent | 8b82004359b8d852fa0107d79cc78b21eb35c028 (diff) | |
download | Nim-dcfc7a8896166563f6fd80fbab81bc50e0b5a217.tar.gz |
resolved conflict
Diffstat (limited to 'lib/system')
-rw-r--r-- | lib/system/alloc.nim | 79 | ||||
-rw-r--r-- | lib/system/ansi_c.nim | 85 | ||||
-rw-r--r-- | lib/system/arithm.nim | 12 | ||||
-rw-r--r-- | lib/system/assign.nim | 54 | ||||
-rw-r--r-- | lib/system/atomics.nim | 126 | ||||
-rw-r--r-- | lib/system/avltree.nim | 10 | ||||
-rw-r--r-- | lib/system/cellsets.nim | 56 | ||||
-rw-r--r-- | lib/system/channels.nim | 26 | ||||
-rw-r--r-- | lib/system/chcks.nim | 4 | ||||
-rw-r--r-- | lib/system/debugger.nim | 16 | ||||
-rw-r--r-- | lib/system/dyncalls.nim | 17 | ||||
-rw-r--r-- | lib/system/endb.nim | 44 | ||||
-rw-r--r-- | lib/system/excpt.nim | 16 | ||||
-rw-r--r-- | lib/system/gc.nim | 83 | ||||
-rw-r--r-- | lib/system/gc_ms.nim | 28 | ||||
-rw-r--r-- | lib/system/hti.nim | 2 | ||||
-rw-r--r-- | lib/system/jssys.nim | 117 | ||||
-rw-r--r-- | lib/system/mmdisp.nim | 99 | ||||
-rw-r--r-- | lib/system/repr.nim | 38 | ||||
-rw-r--r-- | lib/system/sysio.nim | 73 | ||||
-rw-r--r-- | lib/system/syslocks.nim | 57 | ||||
-rw-r--r-- | lib/system/sysstr.nim | 32 | ||||
-rw-r--r-- | lib/system/threads.nim | 60 | ||||
-rw-r--r-- | lib/system/widestrs.nim | 300 |
24 files changed, 743 insertions, 691 deletions
diff --git a/lib/system/alloc.nim b/lib/system/alloc.nim index 17258cf68..954485eb4 100644 --- a/lib/system/alloc.nim +++ b/lib/system/alloc.nim @@ -59,15 +59,16 @@ elif defined(windows): MEM_DECOMMIT = 0x4000 MEM_RELEASE = 0x8000 - proc VirtualAlloc(lpAddress: pointer, dwSize: int, flAllocationType, + proc virtualAlloc(lpAddress: pointer, dwSize: int, flAllocationType, flProtect: int32): pointer {. - header: "<windows.h>", stdcall.} + header: "<windows.h>", stdcall, importc: "VirtualAlloc".} - proc VirtualFree(lpAddress: pointer, dwSize: int, - dwFreeType: int32) {.header: "<windows.h>", stdcall.} + proc virtualFree(lpAddress: pointer, dwSize: int, + dwFreeType: int32) {.header: "<windows.h>", stdcall, + importc: "VirtualFree".} proc osAllocPages(size: int): pointer {.inline.} = - result = VirtualAlloc(nil, size, MEM_RESERVE or MEM_COMMIT, + result = virtualAlloc(nil, size, MEM_RESERVE or MEM_COMMIT, PAGE_READWRITE) if result == nil: raiseOutOfMem() @@ -78,7 +79,7 @@ elif defined(windows): # Windows :-(. We have to live with MEM_DECOMMIT instead. # Well that used to be the case but MEM_DECOMMIT fragments the address # space heavily, so we now treat Windows as a strange unmap target. - when reallyOsDealloc: VirtualFree(p, 0, MEM_RELEASE) + when reallyOsDealloc: virtualFree(p, 0, MEM_RELEASE) #VirtualFree(p, size, MEM_DECOMMIT) else: @@ -108,7 +109,7 @@ type data: TTrunkBuckets type - TAlignType = biggestFloat + TAlignType = BiggestFloat TFreeCell {.final, pure.} = object next: ptr TFreeCell # next free cell in chunk (overlaid with refcount) zeroField: int # 0 means cell is not used (overlaid with typ field) @@ -251,36 +252,36 @@ proc llDeallocAll(a: var TMemRegion) = osDeallocPages(it, PageSize) it = next -proc IntSetGet(t: TIntSet, key: int): PTrunk = +proc intSetGet(t: TIntSet, key: int): PTrunk = var it = t.data[key and high(t.data)] while it != nil: if it.key == key: return it it = it.next result = nil -proc IntSetPut(a: var TMemRegion, t: var TIntSet, key: int): PTrunk = - result = IntSetGet(t, key) +proc intSetPut(a: var TMemRegion, t: var TIntSet, key: int): PTrunk = + result = intSetGet(t, key) if result == nil: result = cast[PTrunk](llAlloc(a, sizeof(result[]))) result.next = t.data[key and high(t.data)] t.data[key and high(t.data)] = result result.key = key -proc Contains(s: TIntSet, key: int): bool = - var t = IntSetGet(s, key shr TrunkShift) +proc contains(s: TIntSet, key: int): bool = + var t = intSetGet(s, key shr TrunkShift) if t != nil: var u = key and TrunkMask result = (t.bits[u shr IntShift] and (1 shl (u and IntMask))) != 0 else: result = false -proc Incl(a: var TMemRegion, s: var TIntSet, key: int) = - var t = IntSetPut(a, s, key shr TrunkShift) +proc incl(a: var TMemRegion, s: var TIntSet, key: int) = + var t = intSetPut(a, s, key shr TrunkShift) var u = key and TrunkMask t.bits[u shr IntShift] = t.bits[u shr IntShift] or (1 shl (u and IntMask)) -proc Excl(s: var TIntSet, key: int) = - var t = IntSetGet(s, key shr TrunkShift) +proc excl(s: var TIntSet, key: int) = + var t = intSetGet(s, key shr TrunkShift) if t != nil: var u = key and TrunkMask t.bits[u shr IntShift] = t.bits[u shr IntShift] and not @@ -387,7 +388,7 @@ proc freeOsChunks(a: var TMemRegion, p: pointer, size: int) = #c_fprintf(c_stdout, "[Alloc] back to OS: %ld\n", size) proc isAccessible(a: TMemRegion, p: pointer): bool {.inline.} = - result = Contains(a.chunkStarts, pageIndex(p)) + result = contains(a.chunkStarts, pageIndex(p)) proc contains[T](list, x: T): bool = var it = list @@ -403,7 +404,7 @@ proc writeFreeList(a: TMemRegion) = it, it.next, it.prev) it = it.next -proc ListAdd[T](head: var T, c: T) {.inline.} = +proc listAdd[T](head: var T, c: T) {.inline.} = sysAssert(c notin head, "listAdd 1") sysAssert c.prev == nil, "listAdd 2" sysAssert c.next == nil, "listAdd 3" @@ -413,7 +414,7 @@ proc ListAdd[T](head: var T, c: T) {.inline.} = head.prev = c head = c -proc ListRemove[T](head: var T, c: T) {.inline.} = +proc listRemove[T](head: var T, c: T) {.inline.} = sysAssert(c in head, "listRemove") if c == head: head = c.next @@ -443,7 +444,7 @@ proc freeBigChunk(a: var TMemRegion, c: PBigChunk) = if isAccessible(a, ri) and chunkUnused(ri): sysAssert(not isSmallChunk(ri), "freeBigChunk 3") if not isSmallChunk(ri): - ListRemove(a.freeChunksList, cast[PBigChunk](ri)) + listRemove(a.freeChunksList, cast[PBigChunk](ri)) inc(c.size, ri.size) excl(a.chunkStarts, pageIndex(ri)) when coalescLeft: @@ -453,7 +454,7 @@ proc freeBigChunk(a: var TMemRegion, c: PBigChunk) = if isAccessible(a, le) and chunkUnused(le): sysAssert(not isSmallChunk(le), "freeBigChunk 5") if not isSmallChunk(le): - ListRemove(a.freeChunksList, cast[PBigChunk](le)) + listRemove(a.freeChunksList, cast[PBigChunk](le)) inc(le.size, c.size) excl(a.chunkStarts, pageIndex(c)) c = cast[PBigChunk](le) @@ -461,7 +462,7 @@ proc freeBigChunk(a: var TMemRegion, c: PBigChunk) = if c.size < ChunkOsReturn or weirdUnmap: incl(a, a.chunkStarts, pageIndex(c)) updatePrevSize(a, c, c.size) - ListAdd(a.freeChunksList, c) + listAdd(a.freeChunksList, c) c.used = false else: freeOsChunks(a, c, c.size) @@ -477,7 +478,7 @@ proc splitChunk(a: var TMemRegion, c: PBigChunk, size: int) = updatePrevSize(a, c, rest.size) c.size = size incl(a, a.chunkStarts, pageIndex(rest)) - ListAdd(a.freeChunksList, rest) + listAdd(a.freeChunksList, rest) proc getBigChunk(a: var TMemRegion, size: int): PBigChunk = # use first fit for now: @@ -488,10 +489,10 @@ proc getBigChunk(a: var TMemRegion, size: int): PBigChunk = while result != nil: sysAssert chunkUnused(result), "getBigChunk 3" if result.size == size: - ListRemove(a.freeChunksList, result) + listRemove(a.freeChunksList, result) break search elif result.size > size: - ListRemove(a.freeChunksList, result) + listRemove(a.freeChunksList, result) splitChunk(a, result, size) break search result = result.next @@ -530,7 +531,7 @@ proc allocInv(a: TMemRegion): bool = while it != nil: if it.zeroField != 0: echo "[SYSASSERT] it.zeroField != 0" - cprintf("%ld %p\n", it.zeroField, it) + c_printf("%ld %p\n", it.zeroField, it) return false it = it.next c = c.next @@ -556,7 +557,7 @@ proc rawAlloc(a: var TMemRegion, requestedSize: int): pointer = c.free = SmallChunkSize - smallChunkOverhead() - size c.next = nil c.prev = nil - ListAdd(a.freeSmallChunks[s], c) + listAdd(a.freeSmallChunks[s], c) result = addr(c.data) sysAssert((cast[TAddress](result) and (MemAlign-1)) == 0, "rawAlloc 4") else: @@ -580,7 +581,7 @@ proc rawAlloc(a: var TMemRegion, requestedSize: int): pointer = sysAssert(allocInv(a), "rawAlloc: before c.free < size") if c.free < size: sysAssert(allocInv(a), "rawAlloc: before listRemove test") - ListRemove(a.freeSmallChunks[s], c) + listRemove(a.freeSmallChunks[s], c) sysAssert(allocInv(a), "rawAlloc: end listRemove test") sysAssert(((cast[TAddress](result) and PageMask) - smallChunkOverhead()) %% size == 0, "rawAlloc 21") @@ -627,12 +628,12 @@ proc rawDealloc(a: var TMemRegion, p: pointer) = # check if it is not in the freeSmallChunks[s] list: if c.free < s: # add it to the freeSmallChunks[s] array: - ListAdd(a.freeSmallChunks[s div memAlign], c) + listAdd(a.freeSmallChunks[s div MemAlign], c) inc(c.free, s) else: inc(c.free, s) if c.free == SmallChunkSize-smallChunkOverhead(): - ListRemove(a.freeSmallChunks[s div memAlign], c) + listRemove(a.freeSmallChunks[s div MemAlign], c) c.size = SmallChunkSize freeBigChunk(a, cast[PBigChunk](c)) sysAssert(((cast[TAddress](p) and PageMask) - smallChunkOverhead()) %% @@ -738,7 +739,7 @@ proc realloc(allocator: var TMemRegion, p: pointer, newsize: int): pointer = proc deallocOsPages(a: var TMemRegion) = # we free every 'ordinarily' allocated page by iterating over the page bits: for p in elements(a.chunkStarts): - var page = cast[PChunk](p shl pageShift) + var page = cast[PChunk](p shl PageShift) when not weirdUnmap: var size = if page.size < PageSize: PageSize else: page.size osDeallocPages(page, size) @@ -759,7 +760,7 @@ proc getOccupiedMem(a: TMemRegion): int {.inline.} = # ---------------------- thread memory region ------------------------------- -template InstantiateForRegion(allocator: expr) = +template instantiateForRegion(allocator: expr) = when defined(fulldebug): proc interiorAllocatedPtr*(p: pointer): pointer = result = interiorAllocatedPtr(allocator, p) @@ -801,13 +802,13 @@ template InstantiateForRegion(allocator: expr) = when hasThreadSupport: var sharedHeap: TMemRegion var heapLock: TSysLock - InitSysLock(HeapLock) + initSysLock(heapLock) proc allocShared(size: int): pointer = when hasThreadSupport: - AcquireSys(HeapLock) + acquireSys(heapLock) result = alloc(sharedHeap, size) - ReleaseSys(HeapLock) + releaseSys(heapLock) else: result = alloc(size) @@ -817,17 +818,17 @@ template InstantiateForRegion(allocator: expr) = proc deallocShared(p: pointer) = when hasThreadSupport: - AcquireSys(HeapLock) + acquireSys(heapLock) dealloc(sharedHeap, p) - ReleaseSys(HeapLock) + releaseSys(heapLock) else: dealloc(p) proc reallocShared(p: pointer, newsize: int): pointer = when hasThreadSupport: - AcquireSys(HeapLock) + acquireSys(heapLock) result = realloc(sharedHeap, p, newsize) - ReleaseSys(HeapLock) + releaseSys(heapLock) else: result = realloc(p, newsize) diff --git a/lib/system/ansi_c.nim b/lib/system/ansi_c.nim index 13e8496d2..2d33965e3 100644 --- a/lib/system/ansi_c.nim +++ b/lib/system/ansi_c.nim @@ -13,12 +13,12 @@ {.push hints:off} -proc c_strcmp(a, b: CString): cint {.header: "<string.h>", +proc c_strcmp(a, b: cstring): cint {.header: "<string.h>", noSideEffect, importc: "strcmp".} -proc c_memcmp(a, b: CString, size: int): cint {.header: "<string.h>", +proc c_memcmp(a, b: cstring, size: int): cint {.header: "<string.h>", noSideEffect, importc: "memcmp".} -proc c_memcpy(a, b: CString, size: int) {.header: "<string.h>", importc: "memcpy".} -proc c_strlen(a: CString): int {.header: "<string.h>", +proc c_memcpy(a, b: cstring, size: int) {.header: "<string.h>", importc: "memcpy".} +proc c_strlen(a: cstring): int {.header: "<string.h>", noSideEffect, importc: "strlen".} proc c_memset(p: pointer, value: cint, size: int) {. header: "<string.h>", importc: "memset".} @@ -28,8 +28,8 @@ type final, incompleteStruct.} = object C_BinaryFile {.importc: "FILE", header: "<stdio.h>", final, incompleteStruct.} = object - C_TextFileStar = ptr CTextFile - C_BinaryFileStar = ptr CBinaryFile + C_TextFileStar = ptr C_TextFile + C_BinaryFileStar = ptr C_BinaryFile C_JmpBuf {.importc: "jmp_buf", header: "<setjmp.h>".} = object @@ -40,21 +40,40 @@ var # constants faked as variables: when not defined(SIGINT): - var - SIGINT {.importc: "SIGINT", nodecl.}: cint - SIGSEGV {.importc: "SIGSEGV", nodecl.}: cint - SIGABRT {.importc: "SIGABRT", nodecl.}: cint - SIGFPE {.importc: "SIGFPE", nodecl.}: cint - SIGILL {.importc: "SIGILL", nodecl.}: cint + when NoFakeVars: + when defined(windows): + const + SIGABRT = cint(22) + SIGFPE = cint(8) + SIGILL = cint(4) + SIGINT = cint(2) + SIGSEGV = cint(11) + SIGTERM = cint(15) + elif defined(macosx) or defined(linux): + const + SIGABRT = cint(6) + SIGFPE = cint(8) + SIGILL = cint(4) + SIGINT = cint(2) + SIGSEGV = cint(11) + SIGTERM = cint(15) + else: + {.error: "SIGABRT not ported to your platform".} + else: + var + SIGINT {.importc: "SIGINT", nodecl.}: cint + SIGSEGV {.importc: "SIGSEGV", nodecl.}: cint + SIGABRT {.importc: "SIGABRT", nodecl.}: cint + SIGFPE {.importc: "SIGFPE", nodecl.}: cint + SIGILL {.importc: "SIGILL", nodecl.}: cint when defined(macosx): - var - SIGBUS {.importc: "SIGBUS", nodecl.}: cint - # hopefully this does not lead to new bugs + when NoFakeVars: + const SIGBUS = cint(10) + else: + var SIGBUS {.importc: "SIGBUS", nodecl.}: cint else: - var - SIGBUS {.importc: "SIGSEGV", nodecl.}: cint - # only Mac OS X has this shit + template SIGBUS: expr = SIGSEGV proc c_longjmp(jmpb: C_JmpBuf, retval: cint) {. header: "<setjmp.h>", importc: "longjmp".} @@ -73,27 +92,27 @@ proc c_fgetc(stream: C_TextFileStar): int {.importc: "fgetc", header: "<stdio.h>".} proc c_ungetc(c: int, f: C_TextFileStar) {.importc: "ungetc", header: "<stdio.h>".} -proc c_putc(c: Char, stream: C_TextFileStar) {.importc: "putc", +proc c_putc(c: char, stream: C_TextFileStar) {.importc: "putc", header: "<stdio.h>".} -proc c_fprintf(f: C_TextFileStar, frmt: CString) {. +proc c_fprintf(f: C_TextFileStar, frmt: cstring) {. importc: "fprintf", header: "<stdio.h>", varargs.} -proc c_printf(frmt: CString) {. +proc c_printf(frmt: cstring) {. importc: "printf", header: "<stdio.h>", varargs.} proc c_fopen(filename, mode: cstring): C_TextFileStar {. importc: "fopen", header: "<stdio.h>".} proc c_fclose(f: C_TextFileStar) {.importc: "fclose", header: "<stdio.h>".} -proc c_sprintf(buf, frmt: CString) {.header: "<stdio.h>", +proc c_sprintf(buf, frmt: cstring) {.header: "<stdio.h>", importc: "sprintf", varargs, noSideEffect.} # we use it only in a way that cannot lead to security issues -proc c_fread(buf: Pointer, size, n: int, f: C_BinaryFileStar): int {. +proc c_fread(buf: pointer, size, n: int, f: C_BinaryFileStar): int {. importc: "fread", header: "<stdio.h>".} proc c_fseek(f: C_BinaryFileStar, offset: clong, whence: int): int {. importc: "fseek", header: "<stdio.h>".} -proc c_fwrite(buf: Pointer, size, n: int, f: C_BinaryFileStar): int {. +proc c_fwrite(buf: pointer, size, n: int, f: C_BinaryFileStar): int {. importc: "fwrite", header: "<stdio.h>".} proc c_exit(errorcode: cint) {.importc: "exit", header: "<stdlib.h>".} @@ -111,16 +130,22 @@ proc c_realloc(p: pointer, newsize: int): pointer {. when hostOS != "standalone": when not defined(errno): - var errno {.importc, header: "<errno.h>".}: cint ## error variable + when defined(NimrodVM): + var vmErrnoWrapper {.importc.}: ptr cint + template errno: expr = + bind vmErrnoWrapper + vmErrnoWrapper[] + else: + var errno {.importc, header: "<errno.h>".}: cint ## error variable proc strerror(errnum: cint): cstring {.importc, header: "<string.h>".} -proc c_remove(filename: CString): cint {. +proc c_remove(filename: cstring): cint {. importc: "remove", header: "<stdio.h>".} -proc c_rename(oldname, newname: CString): cint {. +proc c_rename(oldname, newname: cstring): cint {. importc: "rename", header: "<stdio.h>".} -proc c_system(cmd: CString): cint {.importc: "system", header: "<stdlib.h>".} -proc c_getenv(env: CString): CString {.importc: "getenv", header: "<stdlib.h>".} -proc c_putenv(env: CString): cint {.importc: "putenv", header: "<stdlib.h>".} +proc c_system(cmd: cstring): cint {.importc: "system", header: "<stdlib.h>".} +proc c_getenv(env: cstring): cstring {.importc: "getenv", header: "<stdlib.h>".} +proc c_putenv(env: cstring): cint {.importc: "putenv", header: "<stdlib.h>".} {.pop} diff --git a/lib/system/arithm.nim b/lib/system/arithm.nim index d764a6672..d9b3aebac 100644 --- a/lib/system/arithm.nim +++ b/lib/system/arithm.nim @@ -111,7 +111,7 @@ const when asmVersion and not defined(gcc) and not defined(llvm_gcc): # assembler optimized versions for compilers that # have an intel syntax assembler: - proc addInt(a, b: int): int {.compilerProc, noStackFrame.} = + proc addInt(a, b: int): int {.compilerProc, asmNoStackFrame.} = # a in eax, and b in edx asm """ mov eax, `a` @@ -121,7 +121,7 @@ when asmVersion and not defined(gcc) and not defined(llvm_gcc): theEnd: """ - proc subInt(a, b: int): int {.compilerProc, noStackFrame.} = + proc subInt(a, b: int): int {.compilerProc, asmNoStackFrame.} = asm """ mov eax, `a` sub eax, `b` @@ -130,7 +130,7 @@ when asmVersion and not defined(gcc) and not defined(llvm_gcc): theEnd: """ - proc negInt(a: int): int {.compilerProc, noStackFrame.} = + proc negInt(a: int): int {.compilerProc, asmNoStackFrame.} = asm """ mov eax, `a` neg eax @@ -139,7 +139,7 @@ when asmVersion and not defined(gcc) and not defined(llvm_gcc): theEnd: """ - proc divInt(a, b: int): int {.compilerProc, noStackFrame.} = + proc divInt(a, b: int): int {.compilerProc, asmNoStackFrame.} = asm """ mov eax, `a` mov ecx, `b` @@ -150,7 +150,7 @@ when asmVersion and not defined(gcc) and not defined(llvm_gcc): theEnd: """ - proc modInt(a, b: int): int {.compilerProc, noStackFrame.} = + proc modInt(a, b: int): int {.compilerProc, asmNoStackFrame.} = asm """ mov eax, `a` mov ecx, `b` @@ -162,7 +162,7 @@ when asmVersion and not defined(gcc) and not defined(llvm_gcc): mov eax, edx """ - proc mulInt(a, b: int): int {.compilerProc, noStackFrame.} = + proc mulInt(a, b: int): int {.compilerProc, asmNoStackFrame.} = asm """ mov eax, `a` mov ecx, `b` diff --git a/lib/system/assign.nim b/lib/system/assign.nim index 3b43abcd1..bed8820be 100644 --- a/lib/system/assign.nim +++ b/lib/system/assign.nim @@ -7,10 +7,10 @@ # distribution, for details about the copyright. # -proc genericResetAux(dest: Pointer, n: ptr TNimNode) +proc genericResetAux(dest: pointer, n: ptr TNimNode) -proc genericAssignAux(dest, src: Pointer, mt: PNimType, shallow: bool) -proc genericAssignAux(dest, src: Pointer, n: ptr TNimNode, shallow: bool) = +proc genericAssignAux(dest, src: pointer, mt: PNimType, shallow: bool) +proc genericAssignAux(dest, src: pointer, n: ptr TNimNode, shallow: bool) = var d = cast[TAddress](dest) s = cast[TAddress](src) @@ -37,37 +37,37 @@ proc genericAssignAux(dest, src: Pointer, n: ptr TNimNode, shallow: bool) = # echo "ugh memory corruption! ", n.kind # quit 1 -proc genericAssignAux(dest, src: Pointer, mt: PNimType, shallow: bool) = +proc genericAssignAux(dest, src: pointer, mt: PNimType, shallow: bool) = var d = cast[TAddress](dest) s = cast[TAddress](src) sysAssert(mt != nil, "genericAssignAux 2") - case mt.Kind + case mt.kind of tyString: - var x = cast[ppointer](dest) - var s2 = cast[ppointer](s)[] + var x = cast[PPointer](dest) + var s2 = cast[PPointer](s)[] if s2 == nil or shallow or ( cast[PGenericSeq](s2).reserved and seqShallowFlag) != 0: unsureAsgnRef(x, s2) else: unsureAsgnRef(x, copyString(cast[NimString](s2))) of tySequence: - var s2 = cast[ppointer](src)[] + var s2 = cast[PPointer](src)[] var seq = cast[PGenericSeq](s2) - var x = cast[ppointer](dest) + var x = cast[PPointer](dest) if s2 == nil or shallow or (seq.reserved and seqShallowFlag) != 0: # this can happen! nil sequences are allowed unsureAsgnRef(x, s2) return sysAssert(dest != nil, "genericAssignAux 3") unsureAsgnRef(x, newSeq(mt, seq.len)) - var dst = cast[taddress](cast[ppointer](dest)[]) + var dst = cast[TAddress](cast[PPointer](dest)[]) for i in 0..seq.len-1: genericAssignAux( cast[pointer](dst +% i*% mt.base.size +% GenericSeqSize), - cast[pointer](cast[taddress](s2) +% i *% mt.base.size +% + cast[pointer](cast[TAddress](s2) +% i *% mt.base.size +% GenericSeqSize), - mt.Base, shallow) + mt.base, shallow) of tyObject: # we need to copy m_type field for tyObject, as it could be empty for # sequence reallocations: @@ -83,16 +83,16 @@ proc genericAssignAux(dest, src: Pointer, mt: PNimType, shallow: bool) = genericAssignAux(cast[pointer](d +% i*% mt.base.size), cast[pointer](s +% i*% mt.base.size), mt.base, shallow) of tyRef: - unsureAsgnRef(cast[ppointer](dest), cast[ppointer](s)[]) + unsureAsgnRef(cast[PPointer](dest), cast[PPointer](s)[]) else: copyMem(dest, src, mt.size) # copy raw bits -proc genericAssign(dest, src: Pointer, mt: PNimType) {.compilerProc.} = +proc genericAssign(dest, src: pointer, mt: PNimType) {.compilerProc.} = GC_disable() genericAssignAux(dest, src, mt, false) GC_enable() -proc genericShallowAssign(dest, src: Pointer, mt: PNimType) {.compilerProc.} = +proc genericShallowAssign(dest, src: pointer, mt: PNimType) {.compilerProc.} = GC_disable() genericAssignAux(dest, src, mt, true) GC_enable() @@ -126,7 +126,7 @@ when false: cprintf("%s %ld\n", k, t.size) debugNimType(t.base) -proc genericSeqAssign(dest, src: Pointer, mt: PNimType) {.compilerProc.} = +proc genericSeqAssign(dest, src: pointer, mt: PNimType) {.compilerProc.} = var src = src # ugly, but I like to stress the parser sometimes :-) genericAssign(dest, addr(src), mt) @@ -139,12 +139,12 @@ proc genericAssignOpenArray(dest, src: pointer, len: int, genericAssign(cast[pointer](d +% i*% mt.base.size), cast[pointer](s +% i*% mt.base.size), mt.base) -proc objectInit(dest: Pointer, typ: PNimType) {.compilerProc.} -proc objectInitAux(dest: Pointer, n: ptr TNimNode) = +proc objectInit(dest: pointer, typ: PNimType) {.compilerProc.} +proc objectInitAux(dest: pointer, n: ptr TNimNode) = var d = cast[TAddress](dest) case n.kind of nkNone: sysAssert(false, "objectInitAux") - of nkSLot: objectInit(cast[pointer](d +% n.offset), n.typ) + of nkSlot: objectInit(cast[pointer](d +% n.offset), n.typ) of nkList: for i in 0..n.len-1: objectInitAux(dest, n.sons[i]) @@ -152,7 +152,7 @@ proc objectInitAux(dest: Pointer, n: ptr TNimNode) = var m = selectBranch(dest, n) if m != nil: objectInitAux(dest, m) -proc objectInit(dest: Pointer, typ: PNimType) = +proc objectInit(dest: pointer, typ: PNimType) = # the generic init proc that takes care of initialization of complex # objects on the stack or heap var d = cast[TAddress](dest) @@ -168,12 +168,12 @@ proc objectInit(dest: Pointer, typ: PNimType) = of tyArray, tyArrayConstr: for i in 0..(typ.size div typ.base.size)-1: objectInit(cast[pointer](d +% i * typ.base.size), typ.base) - else: nil # nothing to do + else: discard # nothing to do # ---------------------- assign zero ----------------------------------------- when not defined(nimmixin): - proc destroy(x: int) = nil + proc destroy(x: int) = discard proc nimDestroyRange*[T](r: T) = # internal proc used for destroying sequences and arrays for i in countup(0, r.len - 1): destroy(r[i]) @@ -184,8 +184,8 @@ else: mixin destroy for i in countup(0, r.len - 1): destroy(r[i]) -proc genericReset(dest: Pointer, mt: PNimType) {.compilerProc.} -proc genericResetAux(dest: Pointer, n: ptr TNimNode) = +proc genericReset(dest: pointer, mt: PNimType) {.compilerProc.} +proc genericResetAux(dest: pointer, n: ptr TNimNode) = var d = cast[TAddress](dest) case n.kind of nkNone: sysAssert(false, "genericResetAux") @@ -197,12 +197,12 @@ proc genericResetAux(dest: Pointer, n: ptr TNimNode) = if m != nil: genericResetAux(dest, m) zeroMem(cast[pointer](d +% n.offset), n.typ.size) -proc genericReset(dest: Pointer, mt: PNimType) = +proc genericReset(dest: pointer, mt: PNimType) = var d = cast[TAddress](dest) sysAssert(mt != nil, "genericReset 2") - case mt.Kind + case mt.kind of tyString, tyRef, tySequence: - unsureAsgnRef(cast[ppointer](dest), nil) + unsureAsgnRef(cast[PPointer](dest), nil) of tyObject, tyTuple: # we don't need to reset m_type field for tyObject genericResetAux(dest, mt.node) diff --git a/lib/system/atomics.nim b/lib/system/atomics.nim index 36185e0a8..b1a96b209 100644 --- a/lib/system/atomics.nim +++ b/lib/system/atomics.nim @@ -7,62 +7,60 @@ # distribution, for details about the copyright. # -# Atomic operations for Nimrod. +## Atomic operations for Nimrod. -when (defined(gcc) or defined(llvm_gcc)) and hasThreadSupport: - +when (defined(gcc) or defined(llvm_gcc)) and hasThreadSupport: type AtomMemModel* = enum - ATOMIC_RELAXED, - ## No barriers or synchronization. - ATOMIC_CONSUME, - ## Data dependency only for both barrier and synchronization with another thread. - ATOMIC_ACQUIRE, - ## Barrier to hoisting of code and synchronizes with release (or stronger) - ## semantic stores from another thread. - ATOMIC_RELEASE, - ## Barrier to sinking of code and synchronizes with acquire (or stronger) - ## semantic loads from another thread. - ATOMIC_ACQ_REL, - ## Full barrier in both directions and synchronizes with acquire loads - ## and release stores in another thread. - ATOMIC_SEQ_CST - ## Full barrier in both directions and synchronizes with acquire loads - ## and release stores in all threads. + ATOMIC_RELAXED, ## No barriers or synchronization. + ATOMIC_CONSUME, ## Data dependency only for both barrier and + ## synchronization with another thread. + ATOMIC_ACQUIRE, ## Barrier to hoisting of code and synchronizes with + ## release (or stronger) + ## semantic stores from another thread. + ATOMIC_RELEASE, ## Barrier to sinking of code and synchronizes with + ## acquire (or stronger) + ## semantic loads from another thread. + ATOMIC_ACQ_REL, ## Full barrier in both directions and synchronizes + ## with acquire loads + ## and release stores in another thread. + ATOMIC_SEQ_CST ## Full barrier in both directions and synchronizes + ## with acquire loads + ## and release stores in all threads. TAtomType* = TNumber|pointer|ptr|char - ## Type Class representing valid types for use with atomic procs + ## Type Class representing valid types for use with atomic procs - proc atomic_load_n*[T: TAtomType](p: ptr T, mem: AtomMemModel): T {. + proc atomicLoadN*[T: TAtomType](p: ptr T, mem: AtomMemModel): T {. importc: "__atomic_load_n", nodecl.} ## This proc implements an atomic load operation. It returns the contents at p. ## ATOMIC_RELAXED, ATOMIC_SEQ_CST, ATOMIC_ACQUIRE, ATOMIC_CONSUME. - proc atomic_load*[T: TAtomType](p: ptr T, ret: ptr T, mem: AtomMemModel) {. + proc atomicLoad*[T: TAtomType](p, ret: ptr T, mem: AtomMemModel) {. importc: "__atomic_load", nodecl.} ## This is the generic version of an atomic load. It returns the contents at p in ret. - proc atomic_store_n*[T: TAtomType](p: ptr T, val: T, mem: AtomMemModel) {. + proc atomicStoreN*[T: TAtomType](p: ptr T, val: T, mem: AtomMemModel) {. importc: "__atomic_store_n", nodecl.} ## This proc implements an atomic store operation. It writes val at p. ## ATOMIC_RELAXED, ATOMIC_SEQ_CST, and ATOMIC_RELEASE. - proc atomic_store*[T: TAtomType](p: ptr T, val: ptr T, mem: AtomMemModel) {. + proc atomicStore*[T: TAtomType](p, val: ptr T, mem: AtomMemModel) {. importc: "__atomic_store", nodecl.} ## This is the generic version of an atomic store. It stores the value of val at p - proc atomic_exchange_n*[T: TAtomType](p: ptr T, val: T, mem: AtomMemModel): T {. + proc atomicExchangeN*[T: TAtomType](p: ptr T, val: T, mem: AtomMemModel): T {. importc: "__atomic_exchange_n", nodecl.} ## This proc implements an atomic exchange operation. It writes val at p, ## and returns the previous contents at p. ## ATOMIC_RELAXED, ATOMIC_SEQ_CST, ATOMIC_ACQUIRE, ATOMIC_RELEASE, ATOMIC_ACQ_REL - proc atomic_exchange*[T: TAtomType](p: ptr T, val: ptr T, ret: ptr T, mem: AtomMemModel) {. + proc atomicExchange*[T: TAtomType](p, val, ret: ptr T, mem: AtomMemModel) {. importc: "__atomic_exchange", nodecl.} ## This is the generic version of an atomic exchange. It stores the contents at val at p. ## The original value at p is copied into ret. - proc atomic_compare_exchange_n*[T: TAtomType](p: ptr T, expected: ptr T, desired: T, + proc atomicCompareExchangeN*[T: TAtomType](p, expected: ptr T, desired: T, weak: bool, success_memmodel: AtomMemModel, failure_memmodel: AtomMemModel): bool {. importc: "__atomic_compare_exchange_n ", nodecl.} ## This proc implements an atomic compare and exchange operation. This compares the @@ -78,7 +76,7 @@ when (defined(gcc) or defined(llvm_gcc)) and hasThreadSupport: ## cannot be __ATOMIC_RELEASE nor __ATOMIC_ACQ_REL. It also cannot be a stronger model ## than that specified by success_memmodel. - proc atomic_compare_exchange*[T: TAtomType](p: ptr T, expected: ptr T, desired: ptr T, + proc atomicCompareExchange*[T: TAtomType](p, expected, desired: ptr T, weak: bool, success_memmodel: AtomMemModel, failure_memmodel: AtomMemModel): bool {. importc: "__atomic_compare_exchange_n ", nodecl.} ## This proc implements the generic version of atomic_compare_exchange. @@ -86,58 +84,58 @@ when (defined(gcc) or defined(llvm_gcc)) and hasThreadSupport: ## value is also a pointer. ## Perform the operation return the new value, all memory models are valid - proc atomic_add_fetch*[T: TAtomType](p: ptr T, val: T, mem: AtomMemModel): T {. + proc atomicAddFetch*[T: TAtomType](p: ptr T, val: T, mem: AtomMemModel): T {. importc: "__atomic_add_fetch", nodecl.} - proc atomic_sub_fetch*[T: TAtomType](p: ptr T, val: T, mem: AtomMemModel): T {. + proc atomicSubFetch*[T: TAtomType](p: ptr T, val: T, mem: AtomMemModel): T {. importc: "__atomic_sub_fetch", nodecl.} - proc atomic_or_fetch*[T: TAtomType](p: ptr T, val: T, mem: AtomMemModel): T {. + proc atomicOrFetch*[T: TAtomType](p: ptr T, val: T, mem: AtomMemModel): T {. importc: "__atomic_or_fetch ", nodecl.} - proc atomic_and_fetch*[T: TAtomType](p: ptr T, val: T, mem: AtomMemModel): T {. + proc atomicAndFetch*[T: TAtomType](p: ptr T, val: T, mem: AtomMemModel): T {. importc: "__atomic_and_fetch", nodecl.} - proc atomic_xor_fetch*[T: TAtomType](p: ptr T, val: T, mem: AtomMemModel): T {. + proc atomicXorFetch*[T: TAtomType](p: ptr T, val: T, mem: AtomMemModel): T {. importc: "__atomic_xor_fetch", nodecl.} - proc atomic_nand_fetch*[T: TAtomType](p: ptr T, val: T, mem: AtomMemModel): T {. + proc atomicNandFetch*[T: TAtomType](p: ptr T, val: T, mem: AtomMemModel): T {. importc: "__atomic_nand_fetch ", nodecl.} ## Perform the operation return the old value, all memory models are valid - proc atomic_fetch_add*[T: TAtomType](p: ptr T, val: T, mem: AtomMemModel): T {. - importc: "__atomic_fetch_add ", nodecl.} - proc atomic_fetch_sub*[T: TAtomType](p: ptr T, val: T, mem: AtomMemModel): T {. - importc: "__atomic_fetch_sub ", nodecl.} - proc atomic_fetch_or*[T: TAtomType](p: ptr T, val: T, mem: AtomMemModel): T {. - importc: "__atomic_fetch_or ", nodecl.} - proc atomic_fetch_and*[T: TAtomType](p: ptr T, val: T, mem: AtomMemModel): T {. - importc: "__atomic_fetch_and ", nodecl.} - proc atomic_fetch_xor*[T: TAtomType](p: ptr T, val: T, mem: AtomMemModel): T {. - importc: "__atomic_fetch_xor ", nodecl.} - proc atomic_fetch_nand*[T: TAtomType](p: ptr T, val: T, mem: AtomMemModel): T {. + proc atomicFetchAdd*[T: TAtomType](p: ptr T, val: T, mem: AtomMemModel): T {. + importc: "__atomic_fetch_add", nodecl.} + proc atomicFetchSub*[T: TAtomType](p: ptr T, val: T, mem: AtomMemModel): T {. + importc: "__atomic_fetch_sub", nodecl.} + proc atomicFetchOr*[T: TAtomType](p: ptr T, val: T, mem: AtomMemModel): T {. + importc: "__atomic_fetch_or", nodecl.} + proc atomicFetchAnd*[T: TAtomType](p: ptr T, val: T, mem: AtomMemModel): T {. + importc: "__atomic_fetch_and", nodecl.} + proc atomicFetchXor*[T: TAtomType](p: ptr T, val: T, mem: AtomMemModel): T {. + importc: "__atomic_fetch_xor", nodecl.} + proc atomicFetchNand*[T: TAtomType](p: ptr T, val: T, mem: AtomMemModel): T {. importc: "__atomic_fetch_nand", nodecl.} - proc atomic_test_and_set*(p: pointer, mem: AtomMemModel): bool {. - importc: "__atomic_test_and_set ", nodecl.} + proc atomicTestAndSet*(p: pointer, mem: AtomMemModel): bool {. + importc: "__atomic_test_and_set", nodecl.} ## This built-in function performs an atomic test-and-set operation on the byte at p. ## The byte is set to some implementation defined nonzero “set” value and the return ## value is true if and only if the previous contents were “set”. ## All memory models are valid. - proc atomic_clear*(p: pointer, mem: AtomMemModel) {. + proc atomicClear*(p: pointer, mem: AtomMemModel) {. importc: "__atomic_clear", nodecl.} ## This built-in function performs an atomic clear operation at p. ## After the operation, at p contains 0. ## ATOMIC_RELAXED, ATOMIC_SEQ_CST, ATOMIC_RELEASE - proc atomic_thread_fence*(mem: AtomMemModel) {. - importc: "__atomic_thread_fence ", nodecl.} + proc atomicThreadFence*(mem: AtomMemModel) {. + importc: "__atomic_thread_fence", nodecl.} ## This built-in function acts as a synchronization fence between threads based ## on the specified memory model. All memory orders are valid. - proc atomic_signal_fence*(mem: AtomMemModel) {. - importc: "__atomic_signal_fence ", nodecl.} + proc atomicSignalFence*(mem: AtomMemModel) {. + importc: "__atomic_signal_fence", nodecl.} ## This built-in function acts as a synchronization fence between a thread and ## signal handlers based in the same thread. All memory orders are valid. - proc atomic_always_lock_free*(size: int, p: pointer): bool {. - importc: "__atomic_always_lock_free ", nodecl.} + proc atomicAlwaysLockFree*(size: int, p: pointer): bool {. + importc: "__atomic_always_lock_free", nodecl.} ## This built-in function returns true if objects of size bytes always generate ## lock free atomic instructions for the target architecture. size must resolve ## to a compile-time constant and the result also resolves to a compile-time constant. @@ -145,8 +143,8 @@ when (defined(gcc) or defined(llvm_gcc)) and hasThreadSupport: ## A value of 0 indicates typical alignment should be used. The compiler may also ## ignore this parameter. - proc atomic_is_lock_free*(size: int, p: pointer): bool {. - importc: "__atomic_is_lock_free ", nodecl.} + proc atomicIsLockFree*(size: int, p: pointer): bool {. + importc: "__atomic_is_lock_free", nodecl.} ## This built-in function returns true if objects of size bytes always generate ## lock free atomic instructions for the target architecture. If it is not known ## to be lock free a call is made to a runtime routine named __atomic_is_lock_free. @@ -154,19 +152,14 @@ when (defined(gcc) or defined(llvm_gcc)) and hasThreadSupport: ## A value of 0 indicates typical alignment should be used. The compiler may also ## ignore this parameter. - - elif defined(vcc) and hasThreadSupport: - proc add_and_fetch*(p: ptr int, val: int): int {. + proc addAndFetch*(p: ptr int, val: int): int {. importc: "NimXadd", nodecl.} else: - proc add_and_fetch*(p: ptr int, val: int): int {.inline.} = + proc addAndFetch*(p: ptr int, val: int): int {.inline.} = inc(p[], val) result = p[] - - - # atomic compare and swap (CAS) funcitons to implement lock-free algorithms #if defined(windows) and not defined(gcc) and hasThreadSupport: @@ -183,7 +176,7 @@ else: #elif not hasThreadSupport: # proc compareAndSwap*[T](mem: ptr T, -# expected: T, newValue: T): bool {.inline.} = +# expected: T, newValue: T): bool {.inline.} = # ## Returns true if successfully set value at mem to newValue when value # ## at mem == expected # var oldval = mem[] @@ -209,7 +202,4 @@ proc atomicDec*(memLoc: var int, x: int = 1): int = result = atomic_add_fetch(memLoc.addr, -x, ATOMIC_RELAXED) else: dec(memLoc, x) - result = memLoc - - - + result = memLoc diff --git a/lib/system/avltree.nim b/lib/system/avltree.nim index 6a268b453..fc965d6aa 100644 --- a/lib/system/avltree.nim +++ b/lib/system/avltree.nim @@ -9,30 +9,30 @@ # not really an AVL tree anymore, but still balanced ... -template IsBottom(n: PAvlNode): bool = n == bottom +template isBottom(n: PAvlNode): bool = n == bottom proc lowGauge(n: PAvlNode): int = var it = n - while not IsBottom(it): + while not isBottom(it): result = it.key it = it.link[0] proc highGauge(n: PAvlNode): int = result = -1 var it = n - while not IsBottom(it): + while not isBottom(it): result = it.upperBound it = it.link[1] proc find(root: PAvlNode, key: int): PAvlNode = var it = root - while not IsBottom(it): + while not isBottom(it): if it.key == key: return it it = it.link[ord(it.key <% key)] proc inRange(root: PAvlNode, key: int): PAvlNode = var it = root - while not IsBottom(it): + while not isBottom(it): if it.key <=% key and key <% it.upperBound: return it it = it.link[ord(it.key <% key)] diff --git a/lib/system/cellsets.nim b/lib/system/cellsets.nim index 7ad814da4..3825e5b47 100644 --- a/lib/system/cellsets.nim +++ b/lib/system/cellsets.nim @@ -18,6 +18,8 @@ type when trackAllocationSource: filename: cstring line: int + when useCellIds: + id: int PCell = ptr TCell @@ -43,15 +45,15 @@ type proc contains(s: TCellSeq, c: PCell): bool {.inline.} = for i in 0 .. s.len-1: - if s.d[i] == c: return True - return False + if s.d[i] == c: return true + return false proc add(s: var TCellSeq, c: PCell) {.inline.} = if s.len >= s.cap: s.cap = s.cap * 3 div 2 - var d = cast[PCellArray](Alloc(s.cap * sizeof(PCell))) + var d = cast[PCellArray](alloc(s.cap * sizeof(PCell))) copyMem(d, s.d, s.len * sizeof(PCell)) - Dealloc(s.d) + dealloc(s.d) s.d = d # XXX: realloc? s.d[s.len] = c @@ -60,10 +62,10 @@ proc add(s: var TCellSeq, c: PCell) {.inline.} = proc init(s: var TCellSeq, 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 TCellSeq) = - Dealloc(s.d) + dealloc(s.d) s.d = nil s.len = 0 s.cap = 0 @@ -73,20 +75,20 @@ proc deinit(s: var TCellSeq) = const InitCellSetSize = 1024 # must be a power of two! -proc Init(s: var TCellSet) = - s.data = cast[PPageDescArray](Alloc0(InitCellSetSize * sizeof(PPageDesc))) +proc init(s: var TCellSet) = + s.data = cast[PPageDescArray](alloc0(InitCellSetSize * sizeof(PPageDesc))) s.max = InitCellSetSize-1 s.counter = 0 s.head = nil -proc Deinit(s: var TCellSet) = +proc deinit(s: var TCellSet) = var it = s.head while it != nil: var n = it.next - Dealloc(it) + dealloc(it) it = n s.head = nil # play it safe here - Dealloc(s.data) + dealloc(s.data) s.data = nil s.counter = 0 @@ -96,14 +98,14 @@ proc nextTry(h, maxHash: int): int {.inline.} = # generates each int in range(maxHash) exactly once (see any text on # random-number generation for proof). -proc CellSetGet(t: TCellSet, key: TAddress): PPageDesc = +proc cellSetGet(t: TCellSet, key: TAddress): PPageDesc = var h = cast[int](key) and t.max while t.data[h] != nil: if t.data[h].key == key: return t.data[h] h = nextTry(h, t.max) return nil -proc CellSetRawInsert(t: TCellSet, data: PPageDescArray, desc: PPageDesc) = +proc cellSetRawInsert(t: TCellSet, data: PPageDescArray, desc: PPageDesc) = var h = cast[int](desc.key) and t.max while data[h] != nil: sysAssert(data[h] != desc, "CellSetRawInsert 1") @@ -111,17 +113,17 @@ proc CellSetRawInsert(t: TCellSet, data: PPageDescArray, desc: PPageDesc) = sysAssert(data[h] == nil, "CellSetRawInsert 2") data[h] = desc -proc CellSetEnlarge(t: var TCellSet) = +proc cellSetEnlarge(t: var TCellSet) = var oldMax = t.max t.max = ((t.max+1)*2)-1 - var n = cast[PPageDescArray](Alloc0((t.max + 1) * sizeof(PPageDesc))) - for i in 0 .. oldmax: + var n = cast[PPageDescArray](alloc0((t.max + 1) * sizeof(PPageDesc))) + for i in 0 .. oldMax: if t.data[i] != nil: - CellSetRawInsert(t, n, t.data[i]) - Dealloc(t.data) + cellSetRawInsert(t, n, t.data[i]) + dealloc(t.data) t.data = n -proc CellSetPut(t: var TCellSet, key: TAddress): PPageDesc = +proc cellSetPut(t: var TCellSet, key: TAddress): PPageDesc = var h = cast[int](key) and t.max while true: var x = t.data[h] @@ -130,13 +132,13 @@ proc CellSetPut(t: var TCellSet, key: TAddress): PPageDesc = h = nextTry(h, t.max) if ((t.max+1)*2 < t.counter*3) or ((t.max+1)-t.counter < 4): - CellSetEnlarge(t) + cellSetEnlarge(t) inc(t.counter) h = cast[int](key) and t.max while t.data[h] != nil: h = nextTry(h, t.max) sysAssert(t.data[h] == nil, "CellSetPut") # the new page descriptor goes into result - result = cast[PPageDesc](Alloc0(sizeof(TPageDesc))) + result = cast[PPageDesc](alloc0(sizeof(TPageDesc))) result.next = t.head result.key = key t.head = result @@ -146,7 +148,7 @@ proc CellSetPut(t: var TCellSet, key: TAddress): PPageDesc = proc contains(s: TCellSet, cell: PCell): bool = var u = cast[TAddress](cell) - var t = CellSetGet(s, u shr PageShift) + var t = cellSetGet(s, u shr PageShift) if t != nil: u = (u %% PageSize) /% MemAlign result = (t.bits[u shr IntShift] and (1 shl (u and IntMask))) != 0 @@ -155,13 +157,13 @@ proc contains(s: TCellSet, cell: PCell): bool = proc incl(s: var TCellSet, cell: PCell) {.noinline.} = var u = cast[TAddress](cell) - var t = CellSetPut(s, u shr PageShift) + var t = cellSetPut(s, u shr PageShift) u = (u %% PageSize) /% MemAlign t.bits[u shr IntShift] = t.bits[u shr IntShift] or (1 shl (u and IntMask)) proc excl(s: var TCellSet, cell: PCell) = var u = cast[TAddress](cell) - var t = CellSetGet(s, u shr PageShift) + var t = cellSetGet(s, u shr PageShift) if t != nil: u = (u %% PageSize) /% MemAlign t.bits[u shr IntShift] = (t.bits[u shr IntShift] and @@ -169,7 +171,7 @@ proc excl(s: var TCellSet, cell: PCell) = proc containsOrIncl(s: var TCellSet, cell: PCell): bool = var u = cast[TAddress](cell) - var t = CellSetGet(s, u shr PageShift) + var t = cellSetGet(s, u shr PageShift) if t != nil: u = (u %% PageSize) /% MemAlign result = (t.bits[u shr IntShift] and (1 shl (u and IntMask))) != 0 @@ -177,7 +179,7 @@ proc containsOrIncl(s: var TCellSet, cell: PCell): bool = t.bits[u shr IntShift] = t.bits[u shr IntShift] or (1 shl (u and IntMask)) else: - Incl(s, cell) + incl(s, cell) result = false iterator elements(t: TCellSet): PCell {.inline.} = @@ -201,7 +203,7 @@ iterator elements(t: TCellSet): PCell {.inline.} = iterator elementsExcept(t, s: TCellSet): PCell {.inline.} = var r = t.head while r != nil: - let ss = CellSetGet(s, r.key) + let ss = cellSetGet(s, r.key) var i = 0 while i <= high(r.bits): var w = r.bits[i] diff --git a/lib/system/channels.nim b/lib/system/channels.nim index 9c3cc93e0..bf949529b 100644 --- a/lib/system/channels.nim +++ b/lib/system/channels.nim @@ -48,9 +48,9 @@ proc deinitRawChannel(p: pointer) = deinitSys(c.lock) deinitSysCond(c.cond) -proc storeAux(dest, src: Pointer, mt: PNimType, t: PRawChannel, +proc storeAux(dest, src: pointer, mt: PNimType, t: PRawChannel, mode: TLoadStoreMode) -proc storeAux(dest, src: Pointer, n: ptr TNimNode, t: PRawChannel, +proc storeAux(dest, src: pointer, n: ptr TNimNode, t: PRawChannel, mode: TLoadStoreMode) = var d = cast[TAddress](dest) @@ -67,7 +67,7 @@ proc storeAux(dest, src: Pointer, n: ptr TNimNode, t: PRawChannel, if m != nil: storeAux(dest, src, m, t, mode) of nkNone: sysAssert(false, "storeAux") -proc storeAux(dest, src: Pointer, mt: PNimType, t: PRawChannel, +proc storeAux(dest, src: pointer, mt: PNimType, t: PRawChannel, mode: TLoadStoreMode) = var d = cast[TAddress](dest) @@ -82,7 +82,7 @@ proc storeAux(dest, src: Pointer, mt: PNimType, t: PRawChannel, x[] = nil else: var ss = cast[NimString](s2) - var ns = cast[NimString](Alloc(t.region, ss.len+1 + GenericSeqSize)) + var ns = cast[NimString](alloc(t.region, ss.len+1 + GenericSeqSize)) copyMem(ns, ss, ss.len+1 + GenericSeqSize) x[] = ns else: @@ -92,7 +92,7 @@ proc storeAux(dest, src: Pointer, mt: PNimType, t: PRawChannel, unsureAsgnRef(x, s2) else: unsureAsgnRef(x, copyString(cast[NimString](s2))) - Dealloc(t.region, s2) + dealloc(t.region, s2) of tySequence: var s2 = cast[ppointer](src)[] var seq = cast[PGenericSeq](s2) @@ -105,7 +105,7 @@ proc storeAux(dest, src: Pointer, mt: PNimType, t: PRawChannel, else: sysAssert(dest != nil, "dest == nil") if mode == mStore: - x[] = Alloc(t.region, seq.len *% mt.base.size +% GenericSeqSize) + x[] = alloc(t.region, seq.len *% mt.base.size +% GenericSeqSize) else: unsureAsgnRef(x, newObj(mt, seq.len * mt.base.size + GenericSeqSize)) var dst = cast[taddress](cast[ppointer](dest)[]) @@ -118,7 +118,7 @@ proc storeAux(dest, src: Pointer, mt: PNimType, t: PRawChannel, var dstseq = cast[PGenericSeq](dst) dstseq.len = seq.len dstseq.reserved = seq.len - if mode != mStore: Dealloc(t.region, s2) + if mode != mStore: dealloc(t.region, s2) of tyObject: # copy type field: var pint = cast[ptr PNimType](dest) @@ -143,7 +143,7 @@ proc storeAux(dest, src: Pointer, mt: PNimType, t: PRawChannel, unsureAsgnRef(x, nil) else: if mode == mStore: - x[] = Alloc(t.region, mt.base.size) + x[] = alloc(t.region, mt.base.size) else: # XXX we should use the dynamic type here too, but that is not stored # in the inbox at all --> use source[]'s object type? but how? we need @@ -151,7 +151,7 @@ proc storeAux(dest, src: Pointer, mt: PNimType, t: PRawChannel, var obj = newObj(mt, mt.base.size) unsureAsgnRef(x, obj) storeAux(x[], s, mt.base, t, mode) - if mode != mStore: Dealloc(t.region, s) + if mode != mStore: dealloc(t.region, s) else: copyMem(dest, src, mt.size) # copy raw bits @@ -161,7 +161,7 @@ proc rawSend(q: PRawChannel, data: pointer, typ: PNimType) = if q.count >= cap: # start with capacity for 2 entries in the queue: if cap == 0: cap = 1 - var n = cast[pbytes](Alloc0(q.region, cap*2*typ.size)) + var n = cast[pbytes](alloc0(q.region, cap*2*typ.size)) var z = 0 var i = q.rd var c = q.count @@ -170,7 +170,7 @@ proc rawSend(q: PRawChannel, data: pointer, typ: PNimType) = copyMem(addr(n[z*typ.size]), addr(q.data[i*typ.size]), typ.size) i = (i + 1) and q.mask inc z - if q.data != nil: Dealloc(q.region, q.data) + if q.data != nil: dealloc(q.region, q.data) q.data = n q.mask = cap*2 - 1 q.wr = q.count @@ -200,7 +200,7 @@ template sendImpl(q: expr) {.immediate.} = rawSend(q, addr(m), typ) q.elemType = typ releaseSys(q.lock) - SignalSysCond(q.cond) + signalSysCond(q.cond) proc send*[TMsg](c: var TChannel[TMsg], msg: TMsg) = ## sends a message to a thread. `msg` is deeply copied. @@ -212,7 +212,7 @@ proc llRecv(q: PRawChannel, res: pointer, typ: PNimType) = acquireSys(q.lock) q.ready = true while q.count <= 0: - WaitSysCond(q.cond, q.lock) + waitSysCond(q.cond, q.lock) q.ready = false if typ != q.elemType: releaseSys(q.lock) diff --git a/lib/system/chcks.nim b/lib/system/chcks.nim index e78129483..f29e222e8 100644 --- a/lib/system/chcks.nim +++ b/lib/system/chcks.nim @@ -9,8 +9,8 @@ # Implementation of some runtime checks. -proc raiseRangeError(val: biggestInt) {.compilerproc, noreturn, noinline.} = - when hostOs == "standalone": +proc raiseRangeError(val: BiggestInt) {.compilerproc, noreturn, noinline.} = + when hostOS == "standalone": sysFatal(EOutOfRange, "value out of range") else: sysFatal(EOutOfRange, "value out of range: ", $val) diff --git a/lib/system/debugger.nim b/lib/system/debugger.nim index eade1707f..b5cb5e9ba 100644 --- a/lib/system/debugger.nim +++ b/lib/system/debugger.nim @@ -170,24 +170,24 @@ proc `!$`(h: THash): THash {.inline.} = result = result xor (result shr 11) result = result +% result shl 15 -proc hash(Data: Pointer, Size: int): THash = +proc hash(data: pointer, size: int): THash = var h: THash = 0 - var p = cast[cstring](Data) + var p = cast[cstring](data) var i = 0 var s = size while s > 0: h = h !& ord(p[i]) - Inc(i) - Dec(s) + inc(i) + cec(s) result = !$h proc hashGcHeader(data: pointer): THash = const headerSize = sizeof(int)*2 result = hash(cast[pointer](cast[int](data) -% headerSize), headerSize) -proc genericHashAux(dest: Pointer, mt: PNimType, shallow: bool, +proc genericHashAux(dest: pointer, mt: PNimType, shallow: bool, h: THash): THash -proc genericHashAux(dest: Pointer, n: ptr TNimNode, shallow: bool, +proc genericHashAux(dest: pointer, n: ptr TNimNode, shallow: bool, h: THash): THash = var d = cast[TAddress](dest) case n.kind @@ -203,7 +203,7 @@ proc genericHashAux(dest: Pointer, n: ptr TNimNode, shallow: bool, if m != nil: result = genericHashAux(dest, m, shallow, result) of nkNone: sysAssert(false, "genericHashAux") -proc genericHashAux(dest: Pointer, mt: PNimType, shallow: bool, +proc genericHashAux(dest: pointer, mt: PNimType, shallow: bool, h: THash): THash = sysAssert(mt != nil, "genericHashAux 2") case mt.Kind @@ -253,7 +253,7 @@ proc genericHashAux(dest: Pointer, mt: PNimType, shallow: bool, else: result = h !& hash(dest, mt.size) # hash raw bits -proc genericHash(dest: Pointer, mt: PNimType): int = +proc genericHash(dest: pointer, mt: PNimType): int = result = genericHashAux(dest, mt, false, 0) proc dbgRegisterWatchpoint(address: pointer, name: cstring, diff --git a/lib/system/dyncalls.nim b/lib/system/dyncalls.nim index 6a80369b9..9ef1a99ca 100644 --- a/lib/system/dyncalls.nim +++ b/lib/system/dyncalls.nim @@ -28,7 +28,7 @@ proc nimLoadLibraryError(path: string) = stdout.rawWrite("\n") quit(1) -proc ProcAddrError(name: cstring) {.noinline.} = +proc procAddrError(name: cstring) {.noinline.} = # carefully written to avoid memory allocation: stdout.rawWrite("could not import: ") stdout.write(name) @@ -56,7 +56,7 @@ when defined(posix): RTLD_NOW {.importc: "RTLD_NOW", header: "<dlfcn.h>".}: int proc dlclose(lib: TLibHandle) {.importc, header: "<dlfcn.h>".} - proc dlopen(path: CString, mode: int): TLibHandle {. + proc dlopen(path: cstring, mode: int): TLibHandle {. importc, header: "<dlfcn.h>".} proc dlsym(lib: TLibHandle, name: cstring): TProcAddr {. importc, header: "<dlfcn.h>".} @@ -72,7 +72,7 @@ when defined(posix): proc nimGetProcAddr(lib: TLibHandle, name: cstring): TProcAddr = result = dlsym(lib, name) - if result == nil: ProcAddrError(name) + if result == nil: procAddrError(name) elif defined(windows) or defined(dos): # @@ -83,21 +83,22 @@ elif defined(windows) or defined(dos): type THINSTANCE {.importc: "HINSTANCE".} = pointer - proc FreeLibrary(lib: THINSTANCE) {.importc, header: "<windows.h>", stdcall.} + proc freeLibrary(lib: THINSTANCE) {. + importc: "FreeLibrary", header: "<windows.h>", stdcall.} proc winLoadLibrary(path: cstring): THINSTANCE {. importc: "LoadLibraryA", header: "<windows.h>", stdcall.} - proc GetProcAddress(lib: THINSTANCE, name: cstring): TProcAddr {. + proc getProcAddress(lib: THINSTANCE, name: cstring): TProcAddr {. importc: "GetProcAddress", header: "<windows.h>", stdcall.} proc nimUnloadLibrary(lib: TLibHandle) = - FreeLibrary(cast[THINSTANCE](lib)) + freeLibrary(cast[THINSTANCE](lib)) proc nimLoadLibrary(path: string): TLibHandle = result = cast[TLibHandle](winLoadLibrary(path)) proc nimGetProcAddr(lib: TLibHandle, name: cstring): TProcAddr = - result = GetProcAddress(cast[THINSTANCE](lib), name) - if result == nil: ProcAddrError(name) + result = getProcAddress(cast[THINSTANCE](lib), name) + if result == nil: procAddrError(name) elif defined(mac): # diff --git a/lib/system/endb.nim b/lib/system/endb.nim index 2d6a25824..f7e95e216 100644 --- a/lib/system/endb.nim +++ b/lib/system/endb.nim @@ -79,7 +79,7 @@ proc `==`(a: TStaticStr, b: cstring): bool = proc write(f: TFile, s: TStaticStr) = write(f, cstring(s.data)) -proc ListBreakPoints() = +proc listBreakPoints() = write(stdout, EndbBeg) write(stdout, "| Breakpoints:\n") for b in listBreakpoints(): @@ -117,7 +117,7 @@ proc writeVariable(stream: TFile, slot: TVarSlot) = write(stream, " = ") writeln(stream, dbgRepr(slot.address, slot.typ)) -proc ListFrame(stream: TFile, f: PFrame) = +proc listFrame(stream: TFile, f: PFrame) = write(stream, EndbBeg) write(stream, "| Frame (") write(stream, f.len) @@ -126,7 +126,7 @@ proc ListFrame(stream: TFile, f: PFrame) = writeln(stream, getLocal(f, i).name) write(stream, EndbEnd) -proc ListLocals(stream: TFile, f: PFrame) = +proc listLocals(stream: TFile, f: PFrame) = write(stream, EndbBeg) write(stream, "| Frame (") write(stream, f.len) @@ -135,7 +135,7 @@ proc ListLocals(stream: TFile, f: PFrame) = writeVariable(stream, getLocal(f, i)) write(stream, EndbEnd) -proc ListGlobals(stream: TFile) = +proc listGlobals(stream: TFile) = write(stream, EndbBeg) write(stream, "| Globals:\n") for i in 0 .. getGlobalLen()-1: @@ -179,7 +179,7 @@ proc scanAndAppendWord(src: cstring, a: var TStaticStr, start: int): int = while True: case src[result] of 'a'..'z', '0'..'9': add(a, src[result]) - of '_': nil # just skip it + of '_': discard # just skip it of 'A'..'Z': add(a, chr(ord(src[result]) - ord('A') + ord('a'))) else: break inc(result) @@ -203,7 +203,7 @@ proc scanNumber(src: cstring, a: var int, start: int): int = while true: case src[result] of '0'..'9': a = a * 10 + ord(src[result]) - ord('0') - of '_': nil # skip underscores (nice for long line numbers) + of '_': discard # skip underscores (nice for long line numbers) else: break inc(result) @@ -240,7 +240,7 @@ g, globals display available global variables maxdisplay <integer> set the display's recursion maximum """) -proc InvalidCommand() = +proc invalidCommand() = debugOut("[Warning] invalid command ignored (type 'h' for help) ") proc hasExt(s: cstring): bool = @@ -272,7 +272,7 @@ proc createBreakPoint(s: cstring, start: int) = if not addBreakpoint(br.filename, br.low, br.high): debugOut("[Warning] no breakpoint could be set; out of breakpoint space ") -proc BreakpointToggle(s: cstring, start: int) = +proc breakpointToggle(s: cstring, start: int) = var a = parseBreakpoint(s, start) if not a.filename.isNil: var b = checkBreakpoints(a.filename, a.low) @@ -316,13 +316,13 @@ proc dbgStackFrame(s: cstring, start: int, currFrame: PFrame) = var i = scanFilename(s, dbgTemp, start) if dbgTemp.len == 0: # just write it to stdout: - ListFrame(stdout, currFrame) + listFrame(stdout, currFrame) else: var stream = openAppend(dbgTemp.data) if stream == nil: debugOut("[Warning] could not open or create file ") return - ListFrame(stream, currFrame) + listFrame(stream, currFrame) close(stream) proc readLine(f: TFile, line: var TStaticStr): bool = @@ -339,7 +339,7 @@ proc readLine(f: TFile, line: var TStaticStr): bool = add line, chr(int(c)) result = true -proc ListFilenames() = +proc listFilenames() = write(stdout, EndbBeg) write(stdout, "| Files:\n") var i = 0 @@ -352,7 +352,7 @@ proc ListFilenames() = write(stdout, EndbEnd) proc dbgWriteStackTrace(f: PFrame) -proc CommandPrompt() = +proc commandPrompt() = # if we return from this routine, user code executes again var again = True @@ -426,11 +426,11 @@ proc CommandPrompt() = elif ?"b" or ?"break": createBreakPoint(dbgUser.data, i) elif ?"breakpoints": - ListBreakPoints() + listBreakPoints() elif ?"toggle": - BreakpointToggle(dbgUser.data, i) + breakpointToggle(dbgUser.data, i) elif ?"filenames": - ListFilenames() + listFilenames() elif ?"maxdisplay": var parsed: int i = scanNumber(dbgUser.data, parsed, i) @@ -438,15 +438,15 @@ proc CommandPrompt() = if parsed == 0: maxDisplayRecDepth = -1 else: maxDisplayRecDepth = parsed else: - InvalidCommand() - else: InvalidCommand() + invalidCommand() + else: invalidCommand() proc endbStep() = # we get into here if an unhandled exception has been raised # XXX: do not allow the user to run the program any further? # XXX: BUG: the frame is lost here! dbgShowExecutionPoint() - CommandPrompt() + commandPrompt() proc dbgWriteStackTrace(f: PFrame) = const @@ -506,25 +506,25 @@ proc checkForBreakpoint = write(stdout, ") ") write(stdout, framePtr.procname) write(stdout, " ***\n") - CommandPrompt() + commandPrompt() proc lineHookImpl() {.nimcall.} = case dbgState of dbStepInto: # we really want the command prompt here: dbgShowExecutionPoint() - CommandPrompt() + commandPrompt() of dbSkipCurrent, dbStepOver: # skip current routine if framePtr == dbgSkipToFrame: dbgShowExecutionPoint() - CommandPrompt() + commandPrompt() else: # breakpoints are wanted though (I guess) checkForBreakpoint() of dbBreakpoints: # debugger is only interested in breakpoints checkForBreakpoint() - else: nil + else: discard proc watchpointHookImpl(name: cstring) {.nimcall.} = dbgWriteStackTrace(framePtr) diff --git a/lib/system/excpt.nim b/lib/system/excpt.nim index 9b6a64fb0..e50ba7b9f 100644 --- a/lib/system/excpt.nim +++ b/lib/system/excpt.nim @@ -11,19 +11,19 @@ # use the heap (and nor exceptions) do not include the GC or memory allocator. var - errorMessageWriter*: (proc(msg: string): void {.tags: [FWriteIO].}) + errorMessageWriter*: (proc(msg: string) {.tags: [FWriteIO].}) ## Function that will be called ## instead of stdmsg.write when printing stacktrace. ## Unstable API. when not defined(windows) or not defined(guiapp): - proc writeToStdErr(msg: CString) = write(stdmsg, msg) + proc writeToStdErr(msg: cstring) = write(stdmsg, msg) else: proc MessageBoxA(hWnd: cint, lpText, lpCaption: cstring, uType: int): int32 {. header: "<windows.h>", nodecl.} - proc writeToStdErr(msg: CString) = + proc writeToStdErr(msg: cstring) = discard MessageBoxA(0, msg, nil, 0) proc showErrorMessage(data: cstring) = @@ -80,9 +80,9 @@ when defined(nativeStacktrace) and nativeStackTraceSupported: type TDl_info {.importc: "Dl_info", header: "<dlfcn.h>", final, pure.} = object - dli_fname: CString + dli_fname: cstring dli_fbase: pointer - dli_sname: CString + dli_sname: cstring dli_saddr: pointer proc backtrace(symbols: ptr pointer, size: int): int {. @@ -240,7 +240,7 @@ proc raiseExceptionAux(e: ref E_Base) = showErrorMessage(buf) quitOrDebug() -proc raiseException(e: ref E_Base, ename: CString) {.compilerRtl.} = +proc raiseException(e: ref E_Base, ename: cstring) {.compilerRtl.} = e.name = ename when hasSomeStackTrace: e.trace = "" @@ -253,7 +253,7 @@ proc reraiseException() {.compilerRtl.} = else: raiseExceptionAux(currException) -proc WriteStackTrace() = +proc writeStackTrace() = when hasSomeStackTrace: var s = "" rawWriteStackTrace(s) @@ -280,7 +280,7 @@ when defined(endb): when not defined(noSignalHandler): proc signalHandler(sig: cint) {.exportc: "signalHandler", noconv.} = - template processSignal(s, action: expr) {.immediate.} = + template processSignal(s, action: expr) {.immediate, dirty.} = if s == SIGINT: action("SIGINT: Interrupted by Ctrl-C.\n") elif s == SIGSEGV: action("SIGSEGV: Illegal storage access. (Attempt to read from nil?)\n") diff --git a/lib/system/gc.nim b/lib/system/gc.nim index 68e8b423d..ec1760914 100644 --- a/lib/system/gc.nim +++ b/lib/system/gc.nim @@ -68,6 +68,8 @@ type # non-zero count table stackBottom: pointer cycleThreshold: int + when useCellIds: + idGenerator: int zct: TCellSeq # the zero count table decStack: TCellSeq # cells in the stack that are to decref again cycleRoots: TCellSet @@ -84,7 +86,7 @@ var gch {.rtlThreadVar.}: TGcHeap when not defined(useNimRtl): - InstantiateForRegion(gch.region) + instantiateForRegion(gch.region) template acquire(gch: TGcHeap) = when hasThreadSupport and hasSharedHeap: @@ -132,9 +134,9 @@ when BitsPerPage mod (sizeof(int)*8) != 0: template color(c): expr = c.refCount and colorMask template setColor(c, col) = when col == rcBlack: - c.refcount = c.refCount and not colorMask + c.refcount = c.refcount and not colorMask else: - c.refcount = c.refCount and not colorMask or col + c.refcount = c.refcount and not colorMask or col proc writeCell(msg: cstring, c: PCell) = var kind = -1 @@ -151,10 +153,10 @@ template gcTrace(cell, state: expr): stmt {.immediate.} = # forward declarations: proc collectCT(gch: var TGcHeap) -proc IsOnStack*(p: pointer): bool {.noinline.} +proc isOnStack*(p: pointer): bool {.noinline.} proc forAllChildren(cell: PCell, op: TWalkOp) proc doOperation(p: pointer, op: TWalkOp) -proc forAllChildrenAux(dest: Pointer, mt: PNimType, op: TWalkOp) +proc forAllChildrenAux(dest: pointer, mt: PNimType, op: TWalkOp) # we need the prototype here for debugging purposes when hasThreadSupport and hasSharedHeap: @@ -162,9 +164,9 @@ when hasThreadSupport and hasSharedHeap: template `++`(x: expr): stmt = discard atomicInc(x, rcIncrement) else: template `--`(x: expr): expr = - Dec(x, rcIncrement) + dec(x, rcIncrement) x <% rcIncrement - template `++`(x: expr): stmt = Inc(x, rcIncrement) + template `++`(x: expr): stmt = inc(x, rcIncrement) proc prepareDealloc(cell: PCell) = when useMarkForDebug: @@ -203,7 +205,7 @@ proc decRef(c: PCell) {.inline.} = gcAssert(c.refcount >=% rcIncrement, "decRef") if --c.refcount: rtlAddZCT(c) - elif canBeCycleRoot(c): + elif canbeCycleRoot(c): # unfortunately this is necessary here too, because a cycle might just # have been broken up and we could recycle it. rtlAddCycleRoot(c) @@ -211,10 +213,10 @@ proc decRef(c: PCell) {.inline.} = proc incRef(c: PCell) {.inline.} = gcAssert(isAllocatedPtr(gch.region, c), "incRef: interiorPtr") - c.refcount = c.refCount +% rcIncrement + c.refcount = c.refcount +% rcIncrement # and not colorMask #writeCell("incRef", c) - if canBeCycleRoot(c): + if canbeCycleRoot(c): rtlAddCycleRoot(c) proc nimGCref(p: pointer) {.compilerProc, inline.} = incRef(usrToCell(p)) @@ -235,7 +237,7 @@ proc nimGCunrefNoCycle(p: pointer) {.compilerProc, inline.} = sysAssert(allocInv(gch.region), "end nimGCunrefNoCycle 2") sysAssert(allocInv(gch.region), "end nimGCunrefNoCycle 5") -proc asgnRef(dest: ppointer, src: pointer) {.compilerProc, inline.} = +proc asgnRef(dest: PPointer, src: pointer) {.compilerProc, inline.} = # the code generator calls this proc! gcAssert(not isOnStack(dest), "asgnRef") # BUGFIX: first incRef then decRef! @@ -243,7 +245,7 @@ proc asgnRef(dest: ppointer, src: pointer) {.compilerProc, inline.} = if dest[] != nil: decRef(usrToCell(dest[])) dest[] = src -proc asgnRefNoCycle(dest: ppointer, src: pointer) {.compilerProc, inline.} = +proc asgnRefNoCycle(dest: PPointer, src: pointer) {.compilerProc, inline.} = # the code generator calls this proc if it is known at compile time that no # cycle is possible. if src != nil: @@ -255,7 +257,7 @@ proc asgnRefNoCycle(dest: ppointer, src: pointer) {.compilerProc, inline.} = rtlAddZCT(c) dest[] = src -proc unsureAsgnRef(dest: ppointer, src: pointer) {.compilerProc.} = +proc unsureAsgnRef(dest: PPointer, src: pointer) {.compilerProc.} = # unsureAsgnRef updates the reference counters only if dest is not on the # stack. It is used by the code generator if it cannot decide wether a # reference is in the stack or not (this can happen for var parameters). @@ -318,7 +320,7 @@ proc forAllSlotsAux(dest: pointer, n: ptr TNimNode, op: TWalkOp) = # inlined for speed if n.sons[i].kind == nkSlot: if n.sons[i].typ.kind in {tyRef, tyString, tySequence}: - doOperation(cast[ppointer](d +% n.sons[i].offset)[], op) + doOperation(cast[PPointer](d +% n.sons[i].offset)[], op) else: forAllChildrenAux(cast[pointer](d +% n.sons[i].offset), n.sons[i].typ, op) @@ -329,19 +331,19 @@ proc forAllSlotsAux(dest: pointer, n: ptr TNimNode, op: TWalkOp) = if m != nil: forAllSlotsAux(dest, m, op) of nkNone: sysAssert(false, "forAllSlotsAux") -proc forAllChildrenAux(dest: Pointer, mt: PNimType, op: TWalkOp) = +proc forAllChildrenAux(dest: pointer, mt: PNimType, op: TWalkOp) = var d = cast[TAddress](dest) if dest == nil: return # nothing to do if ntfNoRefs notin mt.flags: - case mt.Kind + case mt.kind of tyRef, tyString, tySequence: # leaf: - doOperation(cast[ppointer](d)[], op) + doOperation(cast[PPointer](d)[], op) of tyObject, tyTuple: forAllSlotsAux(dest, mt.node, op) of tyArray, tyArrayConstr, tyOpenArray: for i in 0..(mt.size div mt.base.size)-1: forAllChildrenAux(cast[pointer](d +% i *% mt.base.size), mt.base, op) - else: nil + else: discard proc forAllChildren(cell: PCell, op: TWalkOp) = gcAssert(cell != nil, "forAllChildren: 1") @@ -352,7 +354,7 @@ proc forAllChildren(cell: PCell, op: TWalkOp) = if marker != nil: marker(cellToUsr(cell), op.int) else: - case cell.typ.Kind + case cell.typ.kind of tyRef: # common case forAllChildrenAux(cellToUsr(cell), cell.typ.base, op) of tySequence: @@ -437,6 +439,9 @@ proc rawNewObj(typ: PNimType, size: int, gch: var TGcHeap): pointer = when logGC: writeCell("new cell", res) gcTrace(res, csAllocated) release(gch) + when useCellIds: + inc gch.idGenerator + res.id = gch.idGenerator result = cellToUsr(res) sysAssert(allocInv(gch.region), "rawNewObj end") @@ -477,6 +482,9 @@ proc newObjRC1(typ: PNimType, size: int): pointer {.compilerRtl.} = when logGC: writeCell("new cell", res) gcTrace(res, csAllocated) release(gch) + when useCellIds: + inc gch.idGenerator + res.id = gch.idGenerator result = cellToUsr(res) zeroMem(result, size) sysAssert(allocInv(gch.region), "newObjRC1 end") @@ -519,7 +527,7 @@ proc growObj(old: pointer, newsize: int, gch: var TGcHeap): pointer = d[j] = res break dec(j) - if canBeCycleRoot(ol): excl(gch.cycleRoots, ol) + if canbeCycleRoot(ol): excl(gch.cycleRoots, ol) when logGC: writeCell("growObj old cell", ol) writeCell("growObj new cell", res) @@ -532,6 +540,9 @@ proc growObj(old: pointer, newsize: int, gch: var TGcHeap): pointer = sysAssert(ol.typ != nil, "growObj: 5") zeroMem(ol, sizeof(TCell)) release(gch) + when useCellIds: + inc gch.idGenerator + res.id = gch.idGenerator result = cellToUsr(res) sysAssert(allocInv(gch.region), "growObj end") when defined(memProfiler): nimProfile(newsize-oldsize) @@ -573,7 +584,7 @@ proc scan(s: PCell) = proc collectWhite(s: PCell) = if s.color == rcWhite and s notin gch.cycleRoots: - s.setcolor(rcBlack) + s.setColor(rcBlack) forAllChildren(s, waCollectWhite) freeCyclicCell(gch, s) @@ -582,7 +593,7 @@ proc markRoots(gch: var TGcHeap) = for s in elements(gch.cycleRoots): #writeCell("markRoot", s) inc tabSize - if s.color == rcPurple and s.refCount >=% rcIncrement: + if s.color == rcPurple and s.refcount >=% rcIncrement: markGray(s) else: excl(gch.cycleRoots, s) @@ -669,7 +680,7 @@ proc doOperation(p: pointer, op: TWalkOp) = proc nimGCvisit(d: pointer, op: int) {.compilerRtl.} = doOperation(d, TWalkOp(op)) -proc CollectZCT(gch: var TGcHeap): bool +proc collectZCT(gch: var TGcHeap): bool when useMarkForDebug or useBackupGc: proc markStackAndRegistersForSweep(gch: var TGcHeap) {.noinline, cdecl.} @@ -802,7 +813,7 @@ when defined(sparc): # For SPARC architecture. # Addresses decrease as the stack grows. while sp <= max: gcMark(gch, sp[]) - sp = cast[ppointer](cast[TAddress](sp) +% sizeof(pointer)) + sp = cast[PPointer](cast[TAddress](sp) +% sizeof(pointer)) elif defined(ELATE): {.error: "stack marking code is to be written for this architecture".} @@ -869,7 +880,7 @@ else: sp = sp +% sizeof(pointer)*8 # last few entries: while sp <=% max: - gcMark(gch, cast[ppointer](sp)[]) + gcMark(gch, cast[PPointer](sp)[]) sp = sp +% sizeof(pointer) proc markStackAndRegisters(gch: var TGcHeap) {.noinline, cdecl.} = @@ -891,7 +902,7 @@ proc collectZCT(gch: var TGcHeap): bool = const workPackage = 100 var L = addr(gch.zct.len) - when withRealtime: + when withRealTime: var steps = workPackage var t0: TTicks if gch.maxPause > 0: t0 = getticks() @@ -904,7 +915,7 @@ proc collectZCT(gch: var TGcHeap): bool = c.refcount = c.refcount and not ZctFlag gch.zct.d[0] = gch.zct.d[L[] - 1] dec(L[]) - when withRealtime: dec steps + when withRealTime: dec steps if c.refcount <% rcIncrement: # It may have a RC > 0, if it is in the hardware stack or # it has not been removed yet from the ZCT. This is because @@ -913,7 +924,7 @@ proc collectZCT(gch: var TGcHeap): bool = # In any case, it should be removed from the ZCT. But not # freed. **KEEP THIS IN MIND WHEN MAKING THIS INCREMENTAL!** when cycleGC: - if canBeCycleRoot(c): excl(gch.cycleRoots, c) + if canbeCycleRoot(c): excl(gch.cycleRoots, c) when logGC: writeCell("zct dealloc cell", c) gcTrace(c, csZctFreed) # We are about to free the object, call the finalizer BEFORE its @@ -927,7 +938,7 @@ proc collectZCT(gch: var TGcHeap): bool = else: sysAssert(c.typ != nil, "collectZCT 2") zeroMem(c, sizeof(TCell)) - when withRealtime: + when withRealTime: if steps == 0: steps = workPackage if gch.maxPause > 0: @@ -952,7 +963,7 @@ proc unmarkStackAndRegisters(gch: var TGcHeap) = gch.decStack.len = 0 proc collectCTBody(gch: var TGcHeap) = - when withRealtime: + when withRealTime: let t0 = getticks() sysAssert(allocInv(gch.region), "collectCT: begin") @@ -970,12 +981,12 @@ proc collectCTBody(gch: var TGcHeap) = #discard collectZCT(gch) inc(gch.stat.cycleCollections) gch.cycleThreshold = max(InitialCycleThreshold, getOccupiedMem() * - cycleIncrease) + CycleIncrease) gch.stat.maxThreshold = max(gch.stat.maxThreshold, gch.cycleThreshold) unmarkStackAndRegisters(gch) sysAssert(allocInv(gch.region), "collectCT: end") - when withRealtime: + when withRealTime: let duration = getticks() - t0 gch.stat.maxPause = max(gch.stat.maxPause, duration) when defined(reportMissedDeadlines): @@ -997,7 +1008,7 @@ proc collectCT(gch: var TGcHeap) = markForDebug(gch) collectCTBody(gch) -when withRealtime: +when withRealTime: proc toNano(x: int): TNanos {.inline.} = result = x * 1000 @@ -1029,11 +1040,7 @@ when not defined(useNimRtl): dec(gch.recGcLock) proc GC_setStrategy(strategy: TGC_Strategy) = - case strategy - of gcThroughput: nil - of gcResponsiveness: nil - of gcOptimizeSpace: nil - of gcOptimizeTime: nil + discard proc GC_enableMarkAndSweep() = gch.cycleThreshold = InitialCycleThreshold diff --git a/lib/system/gc_ms.nim b/lib/system/gc_ms.nim index 2e3596985..e78a4e5cd 100644 --- a/lib/system/gc_ms.nim +++ b/lib/system/gc_ms.nim @@ -1,7 +1,7 @@ # # # Nimrod's Runtime Library -# (c) Copyright 2013 Andreas Rumpf +# (c) Copyright 2014 Andreas Rumpf # # See the file "copying.txt", included in this # distribution, for details about the copyright. @@ -59,11 +59,11 @@ var gch {.rtlThreadVar.}: TGcHeap when not defined(useNimRtl): - InstantiateForRegion(gch.region) + instantiateForRegion(gch.region) template acquire(gch: TGcHeap) = when hasThreadSupport and hasSharedHeap: - AcquireSys(HeapLock) + acquireSys(HeapLock) template release(gch: TGcHeap) = when hasThreadSupport and hasSharedHeap: @@ -90,7 +90,7 @@ proc extGetCellType(c: pointer): PNimType {.compilerproc.} = # used for code generation concerning debugging result = usrToCell(c).typ -proc unsureAsgnRef(dest: ppointer, src: pointer) {.inline.} = +proc unsureAsgnRef(dest: PPointer, src: pointer) {.inline.} = dest[] = src proc internRefcount(p: pointer): int {.exportc: "getRefcount".} = @@ -114,10 +114,10 @@ when BitsPerPage mod (sizeof(int)*8) != 0: # forward declarations: proc collectCT(gch: var TGcHeap) -proc IsOnStack*(p: pointer): bool {.noinline.} +proc isOnStack*(p: pointer): bool {.noinline.} proc forAllChildren(cell: PCell, op: TWalkOp) proc doOperation(p: pointer, op: TWalkOp) -proc forAllChildrenAux(dest: Pointer, mt: PNimType, op: TWalkOp) +proc forAllChildrenAux(dest: pointer, mt: PNimType, op: TWalkOp) # we need the prototype here for debugging purposes proc prepareDealloc(cell: PCell) = @@ -162,19 +162,19 @@ proc forAllSlotsAux(dest: pointer, n: ptr TNimNode, op: TWalkOp) = if m != nil: forAllSlotsAux(dest, m, op) of nkNone: sysAssert(false, "forAllSlotsAux") -proc forAllChildrenAux(dest: Pointer, mt: PNimType, op: TWalkOp) = +proc forAllChildrenAux(dest: pointer, mt: PNimType, op: TWalkOp) = var d = cast[TAddress](dest) if dest == nil: return # nothing to do if ntfNoRefs notin mt.flags: - case mt.Kind + case mt.kind of tyRef, tyString, tySequence: # leaf: - doOperation(cast[ppointer](d)[], op) + doOperation(cast[PPointer](d)[], op) of tyObject, tyTuple: forAllSlotsAux(dest, mt.node, op) of tyArray, tyArrayConstr, tyOpenArray: for i in 0..(mt.size div mt.base.size)-1: forAllChildrenAux(cast[pointer](d +% i *% mt.base.size), mt.base, op) - else: nil + else: discard proc forAllChildren(cell: PCell, op: TWalkOp) = gcAssert(cell != nil, "forAllChildren: 1") @@ -184,7 +184,7 @@ proc forAllChildren(cell: PCell, op: TWalkOp) = if marker != nil: marker(cellToUsr(cell), op.int) else: - case cell.typ.Kind + case cell.typ.kind of tyRef: # common case forAllChildrenAux(cellToUsr(cell), cell.typ.base, op) of tySequence: @@ -194,7 +194,7 @@ proc forAllChildren(cell: PCell, op: TWalkOp) = for i in 0..s.len-1: forAllChildrenAux(cast[pointer](d +% i *% cell.typ.base.size +% GenericSeqSize), cell.typ.base, op) - else: nil + else: discard proc rawNewObj(typ: PNimType, size: int, gch: var TGcHeap): pointer = # generates a new object and sets its reference counter to 0 @@ -466,7 +466,7 @@ else: sp = sp +% sizeof(pointer)*8 # last few entries: while sp <=% max: - gcMark(gch, cast[ppointer](sp)[]) + gcMark(gch, cast[PPointer](sp)[]) sp = sp +% sizeof(pointer) # ---------------------------------------------------------------------------- @@ -505,7 +505,7 @@ when not defined(useNimRtl): else: dec(gch.recGcLock) - proc GC_setStrategy(strategy: TGC_Strategy) = nil + proc GC_setStrategy(strategy: TGC_Strategy) = discard proc GC_enableMarkAndSweep() = gch.cycleThreshold = InitialThreshold diff --git a/lib/system/hti.nim b/lib/system/hti.nim index a2d132dbf..9d8ece7df 100644 --- a/lib/system/hti.nim +++ b/lib/system/hti.nim @@ -69,7 +69,7 @@ type kind: TNimNodeKind offset: int typ: ptr TNimType - name: Cstring + name: cstring len: int sons: ptr array [0..0x7fff, ptr TNimNode] diff --git a/lib/system/jssys.nim b/lib/system/jssys.nim index 850dd1e11..1720804c4 100644 --- a/lib/system/jssys.nim +++ b/lib/system/jssys.nim @@ -23,9 +23,9 @@ type PCallFrame = ptr TCallFrame TCallFrame {.importc, nodecl, final.} = object prev: PCallFrame - procname: CString + procname: cstring line: int # current line number - filename: CString + filename: cstring var framePtr {.importc, nodecl, volatile.}: PCallFrame @@ -48,7 +48,7 @@ proc getCurrentExceptionMsg*(): string = proc auxWriteStackTrace(f: PCallFrame): string = type - TTempFrame = tuple[procname: CString, line: int] + TTempFrame = tuple[procname: cstring, line: int] var it = f i = 0 @@ -84,7 +84,7 @@ proc rawWriteStackTrace(): string = framePtr = nil proc raiseException(e: ref E_Base, ename: cstring) {. - compilerproc, noStackFrame.} = + compilerproc, asmNoStackFrame.} = e.name = ename if excHandler != nil: excHandler.exc = e @@ -104,7 +104,7 @@ proc raiseException(e: ref E_Base, ename: cstring) {. alert(buf) asm """throw `e`;""" -proc reraiseException() {.compilerproc, noStackFrame.} = +proc reraiseException() {.compilerproc, asmNoStackFrame.} = if excHandler == nil: raise newException(ENoExceptionToReraise, "no exception to reraise") else: @@ -125,7 +125,7 @@ proc raiseIndexError() {.compilerproc, noreturn.} = proc raiseFieldError(f: string) {.compilerproc, noreturn.} = raise newException(EInvalidField, f & " is not accessible") -proc SetConstr() {.varargs, noStackFrame, compilerproc.} = +proc SetConstr() {.varargs, asmNoStackFrame, compilerproc.} = asm """ var result = {}; for (var i = 0; i < arguments.length; ++i) { @@ -141,7 +141,7 @@ proc SetConstr() {.varargs, noStackFrame, compilerproc.} = return result; """ -proc cstrToNimstr(c: cstring): string {.noStackFrame, compilerproc.} = +proc cstrToNimstr(c: cstring): string {.asmNoStackFrame, compilerproc.} = asm """ var result = []; for (var i = 0; i < `c`.length; ++i) { @@ -151,7 +151,7 @@ proc cstrToNimstr(c: cstring): string {.noStackFrame, compilerproc.} = return result; """ -proc toJSStr(s: string): cstring {.noStackFrame, compilerproc.} = +proc toJSStr(s: string): cstring {.asmNoStackFrame, compilerproc.} = asm """ var len = `s`.length-1; var result = new Array(len); @@ -162,7 +162,7 @@ proc toJSStr(s: string): cstring {.noStackFrame, compilerproc.} = return result.join(""); """ -proc mnewString(len: int): string {.noStackFrame, compilerproc.} = +proc mnewString(len: int): string {.asmNoStackFrame, compilerproc.} = asm """ var result = new Array(`len`+1); result[0] = 0; @@ -170,7 +170,7 @@ proc mnewString(len: int): string {.noStackFrame, compilerproc.} = return result; """ -proc SetCard(a: int): int {.compilerproc, noStackFrame.} = +proc SetCard(a: int): int {.compilerproc, asmNoStackFrame.} = # argument type is a fake asm """ var result = 0; @@ -178,14 +178,14 @@ proc SetCard(a: int): int {.compilerproc, noStackFrame.} = return result; """ -proc SetEq(a, b: int): bool {.compilerproc, noStackFrame.} = +proc SetEq(a, b: int): bool {.compilerproc, asmNoStackFrame.} = asm """ for (var elem in `a`) { if (!`b`[elem]) return false; } for (var elem in `b`) { if (!`a`[elem]) return false; } return true; """ -proc SetLe(a, b: int): bool {.compilerproc, noStackFrame.} = +proc SetLe(a, b: int): bool {.compilerproc, asmNoStackFrame.} = asm """ for (var elem in `a`) { if (!`b`[elem]) return false; } return true; @@ -194,7 +194,7 @@ proc SetLe(a, b: int): bool {.compilerproc, noStackFrame.} = proc SetLt(a, b: int): bool {.compilerproc.} = result = SetLe(a, b) and not SetEq(a, b) -proc SetMul(a, b: int): int {.compilerproc, noStackFrame.} = +proc SetMul(a, b: int): int {.compilerproc, asmNoStackFrame.} = asm """ var result = {}; for (var elem in `a`) { @@ -203,7 +203,7 @@ proc SetMul(a, b: int): int {.compilerproc, noStackFrame.} = return result; """ -proc SetPlus(a, b: int): int {.compilerproc, noStackFrame.} = +proc SetPlus(a, b: int): int {.compilerproc, asmNoStackFrame.} = asm """ var result = {}; for (var elem in `a`) { result[elem] = true; } @@ -211,7 +211,7 @@ proc SetPlus(a, b: int): int {.compilerproc, noStackFrame.} = return result; """ -proc SetMinus(a, b: int): int {.compilerproc, noStackFrame.} = +proc SetMinus(a, b: int): int {.compilerproc, asmNoStackFrame.} = asm """ var result = {}; for (var elem in `a`) { @@ -220,7 +220,7 @@ proc SetMinus(a, b: int): int {.compilerproc, noStackFrame.} = return result; """ -proc cmpStrings(a, b: string): int {.noStackFrame, compilerProc.} = +proc cmpStrings(a, b: string): int {.asmNoStackFrame, compilerProc.} = asm """ if (`a` == `b`) return 0; if (!`a`) return -1; @@ -234,7 +234,7 @@ proc cmpStrings(a, b: string): int {.noStackFrame, compilerProc.} = proc cmp(x, y: string): int = return cmpStrings(x, y) -proc eqStrings(a, b: string): bool {.noStackFrame, compilerProc.} = +proc eqStrings(a, b: string): bool {.asmNoStackFrame, compilerProc.} = asm """ if (`a` == `b`) return true; if ((!`a`) || (!`b`)) return false; @@ -300,7 +300,7 @@ type setAttributeNode*: proc (attr: ref TNode) {.nimcall.} when defined(kwin): - proc rawEcho {.compilerproc, nostackframe.} = + proc rawEcho {.compilerproc, asmNoStackFrame.} = asm """ var buf = ""; for (var i = 0; i < arguments.length; ++i) { @@ -312,7 +312,7 @@ when defined(kwin): elif defined(nodejs): proc ewriteln(x: cstring) = log(x) - proc rawEcho {.compilerproc, nostackframe.} = + proc rawEcho {.compilerproc, asmNoStackFrame.} = asm """ var buf = ""; for (var i = 0; i < arguments.length; ++i) { @@ -345,42 +345,42 @@ else: node.appendChild(document.createElement("br")) # Arithmetic: -proc addInt(a, b: int): int {.noStackFrame, compilerproc.} = +proc addInt(a, b: int): int {.asmNoStackFrame, compilerproc.} = asm """ var result = `a` + `b`; if (result > 2147483647 || result < -2147483648) `raiseOverflow`(); return result; """ -proc subInt(a, b: int): int {.noStackFrame, compilerproc.} = +proc subInt(a, b: int): int {.asmNoStackFrame, compilerproc.} = asm """ var result = `a` - `b`; if (result > 2147483647 || result < -2147483648) `raiseOverflow`(); return result; """ -proc mulInt(a, b: int): int {.noStackFrame, compilerproc.} = +proc mulInt(a, b: int): int {.asmNoStackFrame, compilerproc.} = asm """ var result = `a` * `b`; if (result > 2147483647 || result < -2147483648) `raiseOverflow`(); return result; """ -proc divInt(a, b: int): int {.noStackFrame, compilerproc.} = +proc divInt(a, b: int): int {.asmNoStackFrame, compilerproc.} = asm """ if (`b` == 0) `raiseDivByZero`(); if (`b` == -1 && `a` == 2147483647) `raiseOverflow`(); return Math.floor(`a` / `b`); """ -proc modInt(a, b: int): int {.noStackFrame, compilerproc.} = +proc modInt(a, b: int): int {.asmNoStackFrame, compilerproc.} = asm """ if (`b` == 0) `raiseDivByZero`(); if (`b` == -1 && `a` == 2147483647) `raiseOverflow`(); return Math.floor(`a` % `b`); """ -proc addInt64(a, b: int): int {.noStackFrame, compilerproc.} = +proc addInt64(a, b: int): int {.asmNoStackFrame, compilerproc.} = asm """ var result = `a` + `b`; if (result > 9223372036854775807 @@ -388,7 +388,7 @@ proc addInt64(a, b: int): int {.noStackFrame, compilerproc.} = return result; """ -proc subInt64(a, b: int): int {.noStackFrame, compilerproc.} = +proc subInt64(a, b: int): int {.asmNoStackFrame, compilerproc.} = asm """ var result = `a` - `b`; if (result > 9223372036854775807 @@ -396,7 +396,7 @@ proc subInt64(a, b: int): int {.noStackFrame, compilerproc.} = return result; """ -proc mulInt64(a, b: int): int {.noStackFrame, compilerproc.} = +proc mulInt64(a, b: int): int {.asmNoStackFrame, compilerproc.} = asm """ var result = `a` * `b`; if (result > 9223372036854775807 @@ -404,90 +404,89 @@ proc mulInt64(a, b: int): int {.noStackFrame, compilerproc.} = return result; """ -proc divInt64(a, b: int): int {.noStackFrame, compilerproc.} = +proc divInt64(a, b: int): int {.asmNoStackFrame, compilerproc.} = asm """ if (`b` == 0) `raiseDivByZero`(); if (`b` == -1 && `a` == 9223372036854775807) `raiseOverflow`(); return Math.floor(`a` / `b`); """ -proc modInt64(a, b: int): int {.noStackFrame, compilerproc.} = +proc modInt64(a, b: int): int {.asmNoStackFrame, compilerproc.} = asm """ if (`b` == 0) `raiseDivByZero`(); if (`b` == -1 && `a` == 9223372036854775807) `raiseOverflow`(); return Math.floor(`a` % `b`); """ -proc NegInt(a: int): int {.compilerproc.} = +proc negInt(a: int): int {.compilerproc.} = result = a*(-1) -proc NegInt64(a: int64): int64 {.compilerproc.} = +proc negInt64(a: int64): int64 {.compilerproc.} = result = a*(-1) -proc AbsInt(a: int): int {.compilerproc.} = +proc absInt(a: int): int {.compilerproc.} = result = if a < 0: a*(-1) else: a -proc AbsInt64(a: int64): int64 {.compilerproc.} = +proc absInt64(a: int64): int64 {.compilerproc.} = result = if a < 0: a*(-1) else: a -proc LeU(a, b: int): bool {.compilerproc.} = +proc leU(a, b: int): bool {.compilerproc.} = result = abs(a) <= abs(b) -proc LtU(a, b: int): bool {.compilerproc.} = +proc ltU(a, b: int): bool {.compilerproc.} = result = abs(a) < abs(b) -proc LeU64(a, b: int64): bool {.compilerproc.} = +proc leU64(a, b: int64): bool {.compilerproc.} = result = abs(a) <= abs(b) - -proc LtU64(a, b: int64): bool {.compilerproc.} = +proc ltU64(a, b: int64): bool {.compilerproc.} = result = abs(a) < abs(b) -proc AddU(a, b: int): int {.compilerproc.} = +proc addU(a, b: int): int {.compilerproc.} = result = abs(a) + abs(b) -proc AddU64(a, b: int64): int64 {.compilerproc.} = +proc addU64(a, b: int64): int64 {.compilerproc.} = result = abs(a) + abs(b) -proc SubU(a, b: int): int {.compilerproc.} = +proc subU(a, b: int): int {.compilerproc.} = result = abs(a) - abs(b) -proc SubU64(a, b: int64): int64 {.compilerproc.} = +proc subU64(a, b: int64): int64 {.compilerproc.} = result = abs(a) - abs(b) -proc MulU(a, b: int): int {.compilerproc.} = +proc mulU(a, b: int): int {.compilerproc.} = result = abs(a) * abs(b) -proc MulU64(a, b: int64): int64 {.compilerproc.} = +proc mulU64(a, b: int64): int64 {.compilerproc.} = result = abs(a) * abs(b) -proc DivU(a, b: int): int {.compilerproc.} = +proc divU(a, b: int): int {.compilerproc.} = result = abs(a) div abs(b) -proc DivU64(a, b: int64): int64 {.compilerproc.} = +proc divU64(a, b: int64): int64 {.compilerproc.} = result = abs(a) div abs(b) -proc ModU(a, b: int): int {.compilerproc.} = +proc modU(a, b: int): int {.compilerproc.} = result = abs(a) mod abs(b) -proc ModU64(a, b: int64): int64 {.compilerproc.} = +proc modU64(a, b: int64): int64 {.compilerproc.} = result = abs(a) mod abs(b) -proc Ze(a: int): int {.compilerproc.} = +proc ze*(a: int): int {.compilerproc.} = result = a -proc Ze64(a: int64): int64 {.compilerproc.} = + +proc ze64*(a: int64): int64 {.compilerproc.} = result = a -proc toU8(a: int): int8 {.noStackFrame, compilerproc.} = +proc ToU8(a: int): int8 {.asmNoStackFrame, compilerproc.} = asm """ return `a`; """ -proc toU16(a: int): int16 {.noStackFrame, compilerproc.} = +proc ToU16(a: int): int16 {.asmNoStackFrame, compilerproc.} = asm """ return `a`; """ -proc toU32(a: int): int32 {.noStackFrame, compilerproc.} = +proc ToU32(a: int): 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 @@ -500,9 +499,9 @@ proc isFatPointer(ti: PNimType): bool = tyArray, tyArrayConstr, tyTuple, tyOpenArray, tySet, tyVar, tyRef, tyPtr} -proc NimCopy(x: pointer, ti: PNimType): pointer {.compilerproc.} +proc nimCopy(x: pointer, ti: PNimType): pointer {.compilerproc.} -proc NimCopyAux(dest, src: Pointer, n: ptr TNimNode) {.compilerproc.} = +proc nimCopyAux(dest, src: Pointer, n: ptr TNimNode) {.compilerproc.} = case n.kind of nkNone: sysAssert(false, "NimCopyAux") of nkSlot: @@ -518,7 +517,7 @@ proc NimCopyAux(dest, src: Pointer, n: ptr TNimNode) {.compilerproc.} = } """ -proc NimCopy(x: pointer, ti: PNimType): pointer = +proc nimCopy(x: pointer, ti: PNimType): pointer = case ti.kind of tyPtr, tyRef, tyVar, tyNil: if not isFatPointer(ti): @@ -586,7 +585,7 @@ proc genericReset(x: Pointer, ti: PNimType): pointer {.compilerproc.} = result = nil proc ArrayConstr(len: int, value: pointer, typ: PNimType): pointer {. - noStackFrame, compilerproc.} = + asmNoStackFrame, compilerproc.} = # types are fake asm """ var result = new Array(`len`); @@ -620,7 +619,7 @@ proc isObj(obj, subclass: PNimType): bool {.compilerproc.} = x = x.base return true -proc addChar(x: string, c: char) {.compilerproc, noStackFrame.} = +proc addChar(x: string, c: char) {.compilerproc, asmNoStackFrame.} = asm """ `x`[`x`.length-1] = `c`; `x`.push(0); """ diff --git a/lib/system/mmdisp.nim b/lib/system/mmdisp.nim index 942b6778e..a09b6cf93 100644 --- a/lib/system/mmdisp.nim +++ b/lib/system/mmdisp.nim @@ -30,10 +30,11 @@ const coalescRight = true coalescLeft = true logAlloc = false + useCellIds = defined(corruption) type PPointer = ptr pointer - TByteArray = array[0..1000_0000, byte] + TByteArray = array[0..1000_0000, Byte] PByte = ptr TByteArray PString = ptr string @@ -114,10 +115,10 @@ when defined(boehmgc): proc alloc0(size: int): pointer = result = alloc(size) zeroMem(result, size) - proc realloc(p: Pointer, newsize: int): pointer = + proc realloc(p: pointer, newsize: int): pointer = result = boehmRealloc(p, newsize) if result == nil: raiseOutOfMem() - proc dealloc(p: Pointer) = boehmDealloc(p) + proc dealloc(p: pointer) = boehmDealloc(p) proc allocShared(size: int): pointer = result = boehmAlloc(size) @@ -125,26 +126,26 @@ when defined(boehmgc): proc allocShared0(size: int): pointer = result = alloc(size) zeroMem(result, size) - proc reallocShared(p: Pointer, newsize: int): pointer = + proc reallocShared(p: pointer, newsize: int): pointer = result = boehmRealloc(p, newsize) if result == nil: raiseOutOfMem() - proc deallocShared(p: Pointer) = boehmDealloc(p) + proc deallocShared(p: pointer) = boehmDealloc(p) #boehmGCincremental() proc GC_disable() = boehmGC_disable() proc GC_enable() = boehmGC_enable() proc GC_fullCollect() = boehmGCfullCollect() - proc GC_setStrategy(strategy: TGC_Strategy) = nil - proc GC_enableMarkAndSweep() = nil - proc GC_disableMarkAndSweep() = nil + proc GC_setStrategy(strategy: TGC_Strategy) = discard + proc GC_enableMarkAndSweep() = discard + proc GC_disableMarkAndSweep() = discard proc GC_getStatistics(): string = return "" proc getOccupiedMem(): int = return boehmGetHeapSize()-boehmGetFreeBytes() proc getFreeMem(): int = return boehmGetFreeBytes() proc getTotalMem(): int = return boehmGetHeapSize() - proc setStackBottom(theStackBottom: pointer) = nil + proc setStackBottom(theStackBottom: pointer) = discard proc initGC() = when defined(macosx): boehmGCinit() @@ -160,8 +161,8 @@ when defined(boehmgc): proc growObj(old: pointer, newsize: int): pointer = result = realloc(old, newsize) - proc nimGCref(p: pointer) {.compilerproc, inline.} = nil - proc nimGCunref(p: pointer) {.compilerproc, inline.} = nil + proc nimGCref(p: pointer) {.compilerproc, inline.} = discard + proc nimGCunref(p: pointer) {.compilerproc, inline.} = discard proc unsureAsgnRef(dest: ppointer, src: pointer) {.compilerproc, inline.} = dest[] = src @@ -173,15 +174,15 @@ when defined(boehmgc): type TMemRegion = object {.final, pure.} - proc Alloc(r: var TMemRegion, size: int): pointer = + proc alloc(r: var TMemRegion, size: int): pointer = result = boehmAlloc(size) if result == nil: raiseOutOfMem() - proc Alloc0(r: var TMemRegion, size: int): pointer = + proc alloc0(r: var TMemRegion, size: int): pointer = result = alloc(size) zeroMem(result, size) - proc Dealloc(r: var TMemRegion, p: Pointer) = boehmDealloc(p) - proc deallocOsPages(r: var TMemRegion) {.inline.} = nil - proc deallocOsPages() {.inline.} = nil + proc dealloc(r: var TMemRegion, p: Pointer) = boehmDealloc(p) + proc deallocOsPages(r: var TMemRegion) {.inline.} = discard + proc deallocOsPages() {.inline.} = discard include "system/cellsets" elif defined(nogc) and defined(useMalloc): @@ -193,10 +194,10 @@ elif defined(nogc) and defined(useMalloc): proc alloc0(size: int): pointer = result = alloc(size) zeroMem(result, size) - proc realloc(p: Pointer, newsize: int): pointer = + proc realloc(p: pointer, newsize: int): pointer = result = crealloc(p, newsize) if result == nil: raiseOutOfMem() - proc dealloc(p: Pointer) = cfree(p) + proc dealloc(p: pointer) = cfree(p) proc allocShared(size: int): pointer = result = cmalloc(size) @@ -204,26 +205,26 @@ elif defined(nogc) and defined(useMalloc): proc allocShared0(size: int): pointer = result = alloc(size) zeroMem(result, size) - proc reallocShared(p: Pointer, newsize: int): pointer = + proc reallocShared(p: pointer, newsize: int): pointer = result = crealloc(p, newsize) if result == nil: raiseOutOfMem() - proc deallocShared(p: Pointer) = cfree(p) - - proc GC_disable() = nil - proc GC_enable() = nil - proc GC_fullCollect() = nil - proc GC_setStrategy(strategy: TGC_Strategy) = nil - proc GC_enableMarkAndSweep() = nil - proc GC_disableMarkAndSweep() = nil + proc deallocShared(p: pointer) = cfree(p) + + proc GC_disable() = discard + proc GC_enable() = discard + proc GC_fullCollect() = discard + proc GC_setStrategy(strategy: TGC_Strategy) = discard + proc GC_enableMarkAndSweep() = discard + proc GC_disableMarkAndSweep() = discard proc GC_getStatistics(): string = return "" - proc getOccupiedMem(): int = nil - proc getFreeMem(): int = nil - proc getTotalMem(): int = nil + proc getOccupiedMem(): int = discard + proc getFreeMem(): int = discard + proc getTotalMem(): int = discard - proc setStackBottom(theStackBottom: pointer) = nil + proc setStackBottom(theStackBottom: pointer) = discard - proc initGC() = nil + proc initGC() = discard proc newObj(typ: PNimType, size: int): pointer {.compilerproc.} = result = alloc(size) @@ -235,8 +236,8 @@ elif defined(nogc) and defined(useMalloc): proc growObj(old: pointer, newsize: int): pointer = result = realloc(old, newsize) - proc nimGCref(p: pointer) {.compilerproc, inline.} = nil - proc nimGCunref(p: pointer) {.compilerproc, inline.} = nil + proc nimGCref(p: pointer) {.compilerproc, inline.} = discard + proc nimGCunref(p: pointer) {.compilerproc, inline.} = discard proc unsureAsgnRef(dest: ppointer, src: pointer) {.compilerproc, inline.} = dest[] = src @@ -248,13 +249,13 @@ elif defined(nogc) and defined(useMalloc): type TMemRegion = object {.final, pure.} - proc Alloc(r: var TMemRegion, size: int): pointer = + proc alloc(r: var TMemRegion, size: int): pointer = result = alloc(size) - proc Alloc0(r: var TMemRegion, size: int): pointer = + proc alloc0(r: var TMemRegion, size: int): pointer = result = alloc0(size) - proc Dealloc(r: var TMemRegion, p: Pointer) = Dealloc(p) - proc deallocOsPages(r: var TMemRegion) {.inline.} = nil - proc deallocOsPages() {.inline.} = nil + proc dealloc(r: var TMemRegion, p: pointer) = dealloc(p) + proc deallocOsPages(r: var TMemRegion) {.inline.} = discard + proc deallocOsPages() {.inline.} = discard elif defined(nogc): # Even though we don't want the GC, we cannot simply use C's memory manager @@ -268,13 +269,13 @@ elif defined(nogc): include "system/alloc" - proc initGC() = nil - proc GC_disable() = nil - proc GC_enable() = nil - proc GC_fullCollect() = nil - proc GC_setStrategy(strategy: TGC_Strategy) = nil - proc GC_enableMarkAndSweep() = nil - proc GC_disableMarkAndSweep() = nil + proc initGC() = discard + proc GC_disable() = discard + proc GC_enable() = discard + proc GC_fullCollect() = discard + proc GC_setStrategy(strategy: TGC_Strategy) = discard + proc GC_enableMarkAndSweep() = discard + proc GC_disableMarkAndSweep() = discard proc GC_getStatistics(): string = return "" @@ -287,9 +288,9 @@ elif defined(nogc): proc growObj(old: pointer, newsize: int): pointer = result = realloc(old, newsize) - proc setStackBottom(theStackBottom: pointer) = nil - proc nimGCref(p: pointer) {.compilerproc, inline.} = nil - proc nimGCunref(p: pointer) {.compilerproc, inline.} = nil + proc setStackBottom(theStackBottom: pointer) = discard + proc nimGCref(p: pointer) {.compilerproc, inline.} = discard + proc nimGCunref(p: pointer) {.compilerproc, inline.} = discard proc unsureAsgnRef(dest: ppointer, src: pointer) {.compilerproc, inline.} = dest[] = src diff --git a/lib/system/repr.nim b/lib/system/repr.nim index a51864ac2..7c1a68bc7 100644 --- a/lib/system/repr.nim +++ b/lib/system/repr.nim @@ -59,7 +59,11 @@ proc reprChar(x: char): string {.compilerRtl.} = proc reprEnum(e: int, typ: PNimType): string {.compilerRtl.} = # we read an 'int' but this may have been too large, so mask the other bits: - let e = e and (1 shl (typ.size*8)-1) + let e = if typ.size == 1: e and 0xff + elif typ.size == 2: e and 0xffff + else: e + # XXX we need a proper narrowing based on signedness here + #e and ((1 shl (typ.size*8)) - 1) if ntfEnumHole notin typ.flags: if e <% typ.node.len: return $typ.node.sons[e].name @@ -72,7 +76,7 @@ proc reprEnum(e: int, typ: PNimType): string {.compilerRtl.} = result = $e & " (invalid data!)" type - pbyteArray = ptr array[0.. 0xffff, int8] + PByteArray = ptr array[0.. 0xffff, int8] proc addSetElem(result: var string, elem: int, typ: PNimType) = case typ.kind @@ -96,7 +100,7 @@ proc reprSetAux(result: var string, p: pointer, typ: PNimType) = of 4: u = ze64(cast[ptr int32](p)[]) of 8: u = cast[ptr int64](p)[] else: - var a = cast[pbyteArray](p) + 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 elemCounter > 0: add result, ", " @@ -129,12 +133,12 @@ when not defined(useNimRtl): when hasThreadSupport and hasSharedHeap and defined(heapLock): AcquireSys(HeapLock) when defined(TCellSet): - Init(cl.marked) + init(cl.marked) cl.recdepth = -1 # default is to display everything! cl.indent = 0 proc deinitReprClosure(cl: var TReprClosure) = - when defined(TCellSet): Deinit(cl.marked) + when defined(TCellSet): deinit(cl.marked) when hasThreadSupport and hasSharedHeap and defined(heapLock): ReleaseSys(HeapLock) @@ -164,7 +168,7 @@ when not defined(useNimRtl): for i in 0..cast[PGenericSeq](p).len-1: if i > 0: add result, ", " reprAux(result, cast[pointer](cast[TAddress](p) + GenericSeqSize + i*bs), - typ.Base, cl) + typ.base, cl) add result, "]" proc reprRecordAux(result: var string, p: pointer, n: ptr TNimNode, @@ -224,17 +228,17 @@ when not defined(useNimRtl): reprRecord(result, p, t, cl) of tyRef, tyPtr: sysAssert(p != nil, "reprAux") - if cast[ppointer](p)[] == nil: add result, "nil" - else: reprRef(result, cast[ppointer](p)[], typ, cl) + if cast[PPointer](p)[] == nil: add result, "nil" + else: reprRef(result, cast[PPointer](p)[], typ, cl) of tySequence: - reprSequence(result, cast[ppointer](p)[], typ, cl) + reprSequence(result, cast[PPointer](p)[], typ, cl) of tyInt: add result, $(cast[ptr int](p)[]) - of tyInt8: add result, $int(cast[ptr Int8](p)[]) - of tyInt16: add result, $int(cast[ptr Int16](p)[]) - of tyInt32: add result, $int(cast[ptr Int32](p)[]) - of tyInt64: add result, $(cast[ptr Int64](p)[]) - of tyUInt8: add result, $ze(cast[ptr Int8](p)[]) - of tyUInt16: add result, $ze(cast[ptr Int16](p)[]) + of tyInt8: add result, $int(cast[ptr int8](p)[]) + of tyInt16: add result, $int(cast[ptr int16](p)[]) + of tyInt32: add result, $int(cast[ptr int32](p)[]) + of tyInt64: add result, $(cast[ptr int64](p)[]) + of tyUInt8: add result, $ze(cast[ptr int8](p)[]) + of tyUInt16: add result, $ze(cast[ptr int16](p)[]) of tyFloat: add result, $(cast[ptr float](p)[]) of tyFloat32: add result, $(cast[ptr float32](p)[]) @@ -246,8 +250,8 @@ when not defined(useNimRtl): of tyCString: reprStrAux(result, $(cast[ptr cstring](p)[])) of tyRange: reprAux(result, p, typ.base, cl) of tyProc, tyPointer: - if cast[ppointer](p)[] == nil: add result, "nil" - else: add result, reprPointer(cast[ppointer](p)[]) + if cast[PPointer](p)[] == nil: add result, "nil" + else: add result, reprPointer(cast[PPointer](p)[]) else: add result, "(invalid data!)" inc(cl.recdepth) diff --git a/lib/system/sysio.nim b/lib/system/sysio.nim index a877f8b28..02c17b92b 100644 --- a/lib/system/sysio.nim +++ b/lib/system/sysio.nim @@ -24,16 +24,16 @@ proc fgetc(stream: TFile): cint {.importc: "fgetc", header: "<stdio.h>", tags: [FReadIO].} proc ungetc(c: cint, f: TFile) {.importc: "ungetc", header: "<stdio.h>", tags: [].} -proc putc(c: Char, stream: TFile) {.importc: "putc", header: "<stdio.h>", +proc putc(c: char, stream: TFile) {.importc: "putc", header: "<stdio.h>", tags: [FWriteIO].} -proc fprintf(f: TFile, frmt: CString) {.importc: "fprintf", +proc fprintf(f: TFile, frmt: cstring) {.importc: "fprintf", header: "<stdio.h>", varargs, tags: [FWriteIO].} proc strlen(c: cstring): int {. importc: "strlen", header: "<string.h>", tags: [].} # C routine that is used here: -proc fread(buf: Pointer, size, n: int, f: TFile): int {. +proc fread(buf: pointer, size, n: int, f: TFile): int {. importc: "fread", header: "<stdio.h>", tags: [FReadIO].} proc fseek(f: TFile, offset: clong, whence: int): int {. importc: "fseek", header: "<stdio.h>", tags: [].} @@ -45,12 +45,24 @@ proc setvbuf(stream: TFile, buf: pointer, typ, size: cint): cint {. proc write(f: TFile, c: cstring) = fputs(c, f) {.pop.} -var - IOFBF {.importc: "_IOFBF", nodecl.}: cint - IONBF {.importc: "_IONBF", nodecl.}: cint +when NoFakeVars: + when defined(windows): + const + IOFBF = cint(0) + IONBF = cint(4) + elif defined(macosx) or defined(linux): + const + IOFBF = cint(0) + IONBF = cint(2) + else: + {.error: "IOFBF not ported to your platform".} +else: + var + IOFBF {.importc: "_IOFBF", nodecl.}: cint + IONBF {.importc: "_IONBF", nodecl.}: cint const - buf_size = 4000 + BufSize = 4000 proc raiseEIO(msg: string) {.noinline, noreturn.} = sysFatal(EIO, msg) @@ -59,7 +71,7 @@ proc readLine(f: TFile, line: var TaintedString): bool = # of course this could be optimized a bit; but IO is slow anyway... # and it was difficult to get this CORRECT with Ansi C's methods setLen(line.string, 0) # reuse the buffer! - while True: + while true: var c = fgetc(f) if c < 0'i32: if line.len > 0: break @@ -82,8 +94,8 @@ proc write(f: TFile, i: int) = else: fprintf(f, "%ld", i) -proc write(f: TFile, i: biggestInt) = - when sizeof(biggestint) == 8: +proc write(f: TFile, i: BiggestInt) = + when sizeof(BiggestInt) == 8: fprintf(f, "%lld", i) else: fprintf(f, "%ld", i) @@ -92,9 +104,9 @@ proc write(f: TFile, b: bool) = if b: write(f, "true") else: write(f, "false") proc write(f: TFile, r: float32) = fprintf(f, "%g", r) -proc write(f: TFile, r: biggestFloat) = fprintf(f, "%g", r) +proc write(f: TFile, r: BiggestFloat) = fprintf(f, "%g", r) -proc write(f: TFile, c: Char) = putc(c, f) +proc write(f: TFile, c: char) = putc(c, f) proc write(f: TFile, a: varargs[string, `$`]) = for x in items(a): write(f, x) @@ -102,10 +114,10 @@ proc readAllBuffer(file: TFile): string = # This proc is for TFile we want to read but don't know how many # bytes we need to read before the buffer is empty. result = "" - var buffer = newString(buf_size) - var bytesRead = buf_size - while bytesRead == buf_size: - bytesRead = readBuffer(file, addr(buffer[0]), buf_size) + var buffer = newString(BufSize) + var bytesRead = BufSize + while bytesRead == BufSize: + bytesRead = readBuffer(file, addr(buffer[0]), BufSize) result.add(buffer) proc rawFileSize(file: TFile): int = @@ -149,7 +161,7 @@ proc writeFile(filename, content: string) = finally: close(f) -proc EndOfFile(f: TFile): bool = +proc endOfFile(f: TFile): bool = # do not blame me; blame the ANSI C standard this is so brain-damaged var c = fgetc(f) ungetc(c, f) @@ -164,15 +176,16 @@ proc rawEchoNL() {.inline, compilerproc.} = write(stdout, "\n") # interface to the C procs: -when defined(windows) and not defined(useWinAnsi): +when (defined(windows) and not defined(useWinAnsi)) or defined(nimdoc): include "system/widestrs" - - proc wfopen(filename, mode: widecstring): pointer {. + +when defined(windows) and not defined(useWinAnsi): + proc wfopen(filename, mode: WideCString): pointer {. importc: "_wfopen", nodecl.} - proc wfreopen(filename, mode: widecstring, stream: TFile): TFile {. + proc wfreopen(filename, mode: WideCString, stream: TFile): TFile {. importc: "_wfreopen", nodecl.} - proc fopen(filename, mode: CString): pointer = + proc fopen(filename, mode: cstring): pointer = var f = newWideCString(filename) var m = newWideCString(mode) result = wfopen(f, m) @@ -183,7 +196,7 @@ when defined(windows) and not defined(useWinAnsi): result = wfreopen(f, m, stream) else: - proc fopen(filename, mode: CString): pointer {.importc: "fopen", noDecl.} + proc fopen(filename, mode: cstring): pointer {.importc: "fopen", noDecl.} proc freopen(filename, mode: cstring, stream: TFile): TFile {. importc: "freopen", nodecl.} @@ -194,9 +207,9 @@ const # should not be translated. -proc Open(f: var TFile, filename: string, +proc open(f: var TFile, filename: string, mode: TFileMode = fmRead, - bufSize: int = -1): Bool = + bufSize: int = -1): bool = var p: pointer = fopen(filename, FormatOpen[mode]) result = (p != nil) f = cast[TFile](p) @@ -217,23 +230,23 @@ proc open(f: var TFile, filehandle: TFileHandle, mode: TFileMode): bool = f = fdopen(filehandle, FormatOpen[mode]) result = f != nil -proc fwrite(buf: Pointer, size, n: int, f: TFile): int {. +proc fwrite(buf: pointer, size, n: int, f: TFile): int {. importc: "fwrite", noDecl.} proc readBuffer(f: TFile, buffer: pointer, len: int): int = result = fread(buffer, 1, len, f) -proc ReadBytes(f: TFile, a: var openarray[int8], start, len: int): int = +proc readBytes(f: TFile, a: var openArray[int8], start, len: int): int = result = readBuffer(f, addr(a[start]), len) -proc ReadChars(f: TFile, a: var openarray[char], start, len: int): int = +proc readChars(f: TFile, a: var openArray[char], start, len: int): int = result = readBuffer(f, addr(a[start]), len) {.push stackTrace:off, profiler:off.} -proc writeBytes(f: TFile, a: openarray[int8], start, len: int): int = +proc writeBytes(f: TFile, a: openArray[int8], start, len: int): int = var x = cast[ptr array[0..1000_000_000, int8]](a) result = writeBuffer(f, addr(x[start]), len) -proc writeChars(f: TFile, a: openarray[char], start, len: int): int = +proc writeChars(f: TFile, a: openArray[char], start, len: int): int = var x = cast[ptr array[0..1000_000_000, int8]](a) result = writeBuffer(f, addr(x[start]), len) proc writeBuffer(f: TFile, buffer: pointer, len: int): int = diff --git a/lib/system/syslocks.nim b/lib/system/syslocks.nim index 4d81dee01..5e3b04b7f 100644 --- a/lib/system/syslocks.nim +++ b/lib/system/syslocks.nim @@ -22,50 +22,53 @@ when defined(Windows): TSysCond = THandle - proc InitSysLock(L: var TSysLock) {.stdcall, noSideEffect, + proc initSysLock(L: var TSysLock) {.stdcall, noSideEffect, dynlib: "kernel32", importc: "InitializeCriticalSection".} ## Initializes the lock `L`. - proc TryAcquireSysAux(L: var TSysLock): int32 {.stdcall, noSideEffect, + proc tryAcquireSysAux(L: var TSysLock): int32 {.stdcall, noSideEffect, dynlib: "kernel32", importc: "TryEnterCriticalSection".} ## Tries to acquire the lock `L`. - proc TryAcquireSys(L: var TSysLock): bool {.inline.} = + proc tryAcquireSys(L: var TSysLock): bool {.inline.} = result = TryAcquireSysAux(L) != 0'i32 - proc AcquireSys(L: var TSysLock) {.stdcall, noSideEffect, + proc acquireSys(L: var TSysLock) {.stdcall, noSideEffect, dynlib: "kernel32", importc: "EnterCriticalSection".} ## Acquires the lock `L`. - proc ReleaseSys(L: var TSysLock) {.stdcall, noSideEffect, + proc releaseSys(L: var TSysLock) {.stdcall, noSideEffect, dynlib: "kernel32", importc: "LeaveCriticalSection".} ## Releases the lock `L`. - proc DeinitSys(L: var TSysLock) {.stdcall, noSideEffect, + proc deinitSys(L: var TSysLock) {.stdcall, noSideEffect, dynlib: "kernel32", importc: "DeleteCriticalSection".} - proc CreateEvent(lpEventAttributes: pointer, + proc createEvent(lpEventAttributes: pointer, bManualReset, bInitialState: int32, lpName: cstring): TSysCond {.stdcall, noSideEffect, dynlib: "kernel32", importc: "CreateEventA".} - proc CloseHandle(hObject: THandle) {.stdcall, noSideEffect, + proc closeHandle(hObject: THandle) {.stdcall, noSideEffect, dynlib: "kernel32", importc: "CloseHandle".} - proc WaitForSingleObject(hHandle: THandle, dwMilliseconds: int32): int32 {. + proc waitForSingleObject(hHandle: THandle, dwMilliseconds: int32): int32 {. stdcall, dynlib: "kernel32", importc: "WaitForSingleObject".} - proc SignalSysCond(hEvent: TSysCond) {.stdcall, noSideEffect, + proc signalSysCond(hEvent: TSysCond) {.stdcall, noSideEffect, dynlib: "kernel32", importc: "SetEvent".} - proc InitSysCond(cond: var TSysCond) {.inline.} = - cond = CreateEvent(nil, 0'i32, 0'i32, nil) - proc DeinitSysCond(cond: var TSysCond) {.inline.} = - CloseHandle(cond) - proc WaitSysCond(cond: var TSysCond, lock: var TSysLock) = + proc initSysCond(cond: var TSysCond) {.inline.} = + cond = createEvent(nil, 0'i32, 0'i32, nil) + proc deinitSysCond(cond: var TSysCond) {.inline.} = + closeHandle(cond) + proc waitSysCond(cond: var TSysCond, lock: var TSysLock) = releaseSys(lock) - discard WaitForSingleObject(cond, -1'i32) + discard waitForSingleObject(cond, -1'i32) acquireSys(lock) + proc waitSysCondWindows(cond: var TSysCond) = + discard waitForSingleObject(cond, -1'i32) + else: type TSysLock {.importc: "pthread_mutex_t", pure, final, @@ -73,29 +76,29 @@ else: TSysCond {.importc: "pthread_cond_t", pure, final, header: "<sys/types.h>".} = object - proc InitSysLock(L: var TSysLock, attr: pointer = nil) {. + proc initSysLock(L: var TSysLock, attr: pointer = nil) {. importc: "pthread_mutex_init", header: "<pthread.h>", noSideEffect.} - proc AcquireSys(L: var TSysLock) {.noSideEffect, + proc acquireSys(L: var TSysLock) {.noSideEffect, importc: "pthread_mutex_lock", header: "<pthread.h>".} - proc TryAcquireSysAux(L: var TSysLock): cint {.noSideEffect, + proc tryAcquireSysAux(L: var TSysLock): cint {.noSideEffect, importc: "pthread_mutex_trylock", header: "<pthread.h>".} - proc TryAcquireSys(L: var TSysLock): bool {.inline.} = - result = TryAcquireSysAux(L) == 0'i32 + proc tryAcquireSys(L: var TSysLock): bool {.inline.} = + result = tryAcquireSysAux(L) == 0'i32 - proc ReleaseSys(L: var TSysLock) {.noSideEffect, + proc releaseSys(L: var TSysLock) {.noSideEffect, importc: "pthread_mutex_unlock", header: "<pthread.h>".} - proc DeinitSys(L: var TSysLock) {. + proc deinitSys(L: var TSysLock) {. importc: "pthread_mutex_destroy", header: "<pthread.h>".} - proc InitSysCond(cond: var TSysCond, cond_attr: pointer = nil) {. + proc initSysCond(cond: var TSysCond, cond_attr: pointer = nil) {. importc: "pthread_cond_init", header: "<pthread.h>".} - proc WaitSysCond(cond: var TSysCond, lock: var TSysLock) {. + proc waitSysCond(cond: var TSysCond, lock: var TSysLock) {. importc: "pthread_cond_wait", header: "<pthread.h>".} - proc SignalSysCond(cond: var TSysCond) {. + proc signalSysCond(cond: var TSysCond) {. importc: "pthread_cond_signal", header: "<pthread.h>".} - proc DeinitSysCond(cond: var TSysCond) {. + proc deinitSysCond(cond: var TSysCond) {. importc: "pthread_cond_destroy", header: "<pthread.h>".} diff --git a/lib/system/sysstr.nim b/lib/system/sysstr.nim index d62a987ff..eb9d2000b 100644 --- a/lib/system/sysstr.nim +++ b/lib/system/sysstr.nim @@ -55,7 +55,7 @@ proc copyStrLast(s: NimString, start, last: int): NimString {.compilerProc.} = if len > 0: result = rawNewString(len) result.len = len - c_memcpy(result.data, addr(s.data[start]), len * sizeof(Char)) + c_memcpy(result.data, addr(s.data[start]), len * sizeof(char)) #result.data[len] = '\0' else: result = rawNewString(len) @@ -63,14 +63,14 @@ proc copyStrLast(s: NimString, start, last: int): NimString {.compilerProc.} = proc copyStr(s: NimString, start: int): NimString {.compilerProc.} = result = copyStrLast(s, start, s.len-1) -proc toNimStr(str: CString, len: int): NimString {.compilerProc.} = +proc toNimStr(str: cstring, len: int): NimString {.compilerProc.} = result = rawNewString(len) result.len = len - c_memcpy(result.data, str, (len+1) * sizeof(Char)) + c_memcpy(result.data, str, (len+1) * sizeof(char)) #result.data[len] = '\0' # readline relies on this! -proc cstrToNimstr(str: CString): NimString {.compilerRtl.} = - result = toNimstr(str, c_strlen(str)) +proc cstrToNimstr(str: cstring): NimString {.compilerRtl.} = + result = toNimStr(str, c_strlen(str)) proc copyString(src: NimString): NimString {.compilerRtl.} = if src != nil: @@ -79,7 +79,7 @@ proc copyString(src: NimString): NimString {.compilerRtl.} = else: result = rawNewString(src.space) result.len = src.len - c_memcpy(result.data, src.data, (src.len + 1) * sizeof(Char)) + c_memcpy(result.data, src.data, (src.len + 1) * sizeof(char)) proc copyStringRC1(src: NimString): NimString {.compilerRtl.} = if src != nil: @@ -98,8 +98,8 @@ proc hashString(s: string): int {.compilerproc.} = # the compiler needs exactly the same hash function! # this used to be used for efficient generation of string case statements var h = 0 - for i in 0..Len(s)-1: - h = h +% Ord(s[i]) + for i in 0..len(s)-1: + h = h +% ord(s[i]) h = h +% h shl 10 h = h xor (h shr 6) h = h +% h shl 3 @@ -150,10 +150,10 @@ proc addChar(s: NimString, c: char): NimString = # s = rawNewString(0); proc resizeString(dest: NimString, addlen: int): NimString {.compilerRtl.} = - if dest.len + addLen <= dest.space: + if dest.len + addlen <= dest.space: result = dest else: # slow path: - var sp = max(resize(dest.space), dest.len + addLen) + var sp = max(resize(dest.space), dest.len + addlen) result = cast[NimString](growObj(dest, sizeof(TGenericSeq) + sp + 1)) result.reserved = sp #result = rawNewString(sp) @@ -236,7 +236,7 @@ proc nimIntToStr(x: int): string {.compilerRtl.} = result = newString(sizeof(x)*4) var i = 0 var y = x - while True: + while true: var d = y div 10 result[i] = chr(abs(int(y - d*10)) + ord('0')) inc(i) @@ -252,14 +252,16 @@ proc nimIntToStr(x: int): string {.compilerRtl.} = proc nimFloatToStr(x: float): string {.compilerproc.} = var buf: array [0..59, char] - c_sprintf(buf, "%#.16e", x) - return $buf + c_sprintf(buf, "%#.f", x) + result = $buf + if result[len(result)-1] == '.': + result.add("0") proc nimInt64ToStr(x: int64): string {.compilerRtl.} = result = newString(sizeof(x)*4) var i = 0 var y = x - while True: + while true: var d = y div 10 result[i] = chr(abs(int(y - d*10)) + ord('0')) inc(i) @@ -280,7 +282,7 @@ proc nimCharToStr(x: char): string {.compilerRtl.} = result = newString(1) result[0] = x -proc binaryStrSearch(x: openarray[string], y: string): int {.compilerproc.} = +proc binaryStrSearch(x: openArray[string], y: string): int {.compilerproc.} = var a = 0 b = len(x) diff --git a/lib/system/threads.nim b/lib/system/threads.nim index 80420d791..ff9ab6cc0 100644 --- a/lib/system/threads.nim +++ b/lib/system/threads.nim @@ -29,11 +29,11 @@ ## ## proc threadFunc(interval: tuple[a,b: int]) {.thread.} = ## for i in interval.a..interval.b: -## Acquire(L) # lock stdout +## acquire(L) # lock stdout ## echo i -## Release(L) +## release(L) ## -## InitLock(L) +## initLock(L) ## ## for i in 0..high(thr): ## createThread(thr[i], threadFunc, (i*10, i*10+5)) @@ -54,9 +54,9 @@ when defined(windows): TSysThread = THandle TWinThreadProc = proc (x: pointer): int32 {.stdcall.} - proc CreateThread(lpThreadAttributes: Pointer, dwStackSize: int32, + proc createThread(lpThreadAttributes: pointer, dwStackSize: int32, lpStartAddress: TWinThreadProc, - lpParameter: Pointer, + lpParameter: pointer, dwCreationFlags: int32, lpThreadId: var int32): TSysThread {. stdcall, dynlib: "kernel32", importc: "CreateThread".} @@ -67,23 +67,23 @@ when defined(windows): proc winResumeThread(hThread: TSysThread): int32 {. stdcall, dynlib: "kernel32", importc: "ResumeThread".} - proc WaitForMultipleObjects(nCount: int32, + proc waitForMultipleObjects(nCount: int32, lpHandles: ptr TSysThread, bWaitAll: int32, dwMilliseconds: int32): int32 {. stdcall, dynlib: "kernel32", importc: "WaitForMultipleObjects".} - proc TerminateThread(hThread: TSysThread, dwExitCode: int32): int32 {. + proc terminateThread(hThread: TSysThread, dwExitCode: int32): int32 {. stdcall, dynlib: "kernel32", importc: "TerminateThread".} type TThreadVarSlot = distinct int32 - proc ThreadVarAlloc(): TThreadVarSlot {. + proc threadVarAlloc(): TThreadVarSlot {. importc: "TlsAlloc", stdcall, dynlib: "kernel32".} - proc ThreadVarSetValue(dwTlsIndex: TThreadVarSlot, lpTlsValue: pointer) {. + proc threadVarSetValue(dwTlsIndex: TThreadVarSlot, lpTlsValue: pointer) {. importc: "TlsSetValue", stdcall, dynlib: "kernel32".} - proc ThreadVarGetValue(dwTlsIndex: TThreadVarSlot): pointer {. + proc threadVarGetValue(dwTlsIndex: TThreadVarSlot): pointer {. importc: "TlsGetValue", stdcall, dynlib: "kernel32".} else: @@ -116,14 +116,14 @@ else: proc pthread_cancel(a1: TSysThread): cint {. importc: "pthread_cancel", header: "<pthread.h>".} - proc AcquireSysTimeoutAux(L: var TSysLock, timeout: var Ttimespec): cint {. + proc acquireSysTimeoutAux(L: var TSysLock, timeout: var Ttimespec): cint {. importc: "pthread_mutex_timedlock", header: "<time.h>".} - proc AcquireSysTimeout(L: var TSysLock, msTimeout: int) {.inline.} = + proc acquireSysTimeout(L: var TSysLock, msTimeout: int) {.inline.} = var a: Ttimespec a.tv_sec = msTimeout div 1000 a.tv_nsec = (msTimeout mod 1000) * 1000 - var res = AcquireSysTimeoutAux(L, a) + var res = acquireSysTimeoutAux(L, a) if res != 0'i32: raise newException(EResourceExhausted, $strerror(res)) type @@ -141,11 +141,11 @@ else: proc pthread_setspecific(a1: TThreadVarSlot, a2: pointer): int32 {. importc: "pthread_setspecific", header: "<pthread.h>".} - proc ThreadVarAlloc(): TThreadVarSlot {.inline.} = + proc threadVarAlloc(): TThreadVarSlot {.inline.} = discard pthread_key_create(addr(result), nil) - proc ThreadVarSetValue(s: TThreadVarSlot, value: pointer) {.inline.} = + proc threadVarSetValue(s: TThreadVarSlot, value: pointer) {.inline.} = discard pthread_setspecific(s, value) - proc ThreadVarGetValue(s: TThreadVarSlot): pointer {.inline.} = + proc threadVarGetValue(s: TThreadVarSlot): pointer {.inline.} = result = pthread_getspecific(s) when useStackMaskHack: @@ -159,7 +159,7 @@ const when emulatedThreadVars: # the compiler generates this proc for us, so that we can get the size of # the thread local var block; we use this only for sanity checking though - proc NimThreadVarsSize(): int {.noconv, importc: "NimThreadVarsSize".} + proc nimThreadVarsSize(): int {.noconv, importc: "NimThreadVarsSize".} # we preallocate a fixed size for thread local storage, so that no heap # allocations are needed. Currently less than 7K are used on a 64bit machine. @@ -184,7 +184,7 @@ type # XXX it'd be more efficient to not use a global variable for the # thread storage slot, but to rely on the implementation to assign slot X # for us... ;-) -var globalsSlot = ThreadVarAlloc() +var globalsSlot = threadVarAlloc() #const globalsSlot = TThreadVarSlot(0) #sysAssert checkSlot.int == globalsSlot.int @@ -193,7 +193,7 @@ when emulatedThreadVars: result = addr(cast[PGcThread](ThreadVarGetValue(globalsSlot)).tls) when useStackMaskHack: - proc MaskStackPointer(offset: int): pointer {.compilerRtl, inl.} = + proc maskStackPointer(offset: int): pointer {.compilerRtl, inl.} = var x {.volatile.}: pointer x = addr(x) result = cast[pointer]((cast[int](x) and not ThreadStackMask) +% @@ -205,7 +205,7 @@ when not defined(useNimRtl): when not useStackMaskHack: var mainThread: TGcThread - ThreadVarSetValue(globalsSlot, addr(mainThread)) + threadVarSetValue(globalsSlot, addr(mainThread)) when not defined(createNimRtl): initStackBottom() initGC() @@ -220,18 +220,18 @@ when not defined(useNimRtl): proc registerThread(t: PGcThread) = # we need to use the GC global lock here! - AcquireSys(HeapLock) + acquireSys(HeapLock) t.prev = nil t.next = threadList if threadList != nil: sysAssert(threadList.prev == nil, "threadList.prev == nil") threadList.prev = t threadList = t - ReleaseSys(HeapLock) + releaseSys(HeapLock) proc unregisterThread(t: PGcThread) = # we need to use the GC global lock here! - AcquireSys(HeapLock) + acquireSys(HeapLock) if t == threadList: threadList = t.next if t.next != nil: t.next.prev = t.prev if t.prev != nil: t.prev.next = t.next @@ -239,7 +239,7 @@ when not defined(useNimRtl): # code executes `destroyThread`: t.next = nil t.prev = nil - ReleaseSys(HeapLock) + releaseSys(HeapLock) # on UNIX, the GC uses ``SIGFREEZE`` to tell every thread to stop so that # the GC can examine the stacks? @@ -266,7 +266,7 @@ type when not defined(boehmgc) and not hasSharedHeap: proc deallocOsPages() -template ThreadProcWrapperBody(closure: expr) {.immediate.} = +template threadProcWrapperBody(closure: expr) {.immediate.} = when defined(globalsSlot): ThreadVarSetValue(globalsSlot, closure) var t = cast[ptr TThread[TArg]](closure) when useStackMaskHack: @@ -294,11 +294,11 @@ template ThreadProcWrapperBody(closure: expr) {.immediate.} = {.push stack_trace:off.} when defined(windows): proc threadProcWrapper[TArg](closure: pointer): int32 {.stdcall.} = - ThreadProcWrapperBody(closure) + threadProcWrapperBody(closure) # implicitly return 0 else: proc threadProcWrapper[TArg](closure: pointer) {.noconv.} = - ThreadProcWrapperBody(closure) + threadProcWrapperBody(closure) {.pop.} proc running*[TArg](t: TThread[TArg]): bool {.inline.} = @@ -308,7 +308,7 @@ proc running*[TArg](t: TThread[TArg]): bool {.inline.} = proc joinThread*[TArg](t: TThread[TArg]) {.inline.} = ## waits for the thread `t` to finish. when hostOS == "windows": - discard WaitForSingleObject(t.sys, -1'i32) + discard waitForSingleObject(t.sys, -1'i32) else: discard pthread_join(t.sys, nil) @@ -318,7 +318,7 @@ proc joinThreads*[TArg](t: varargs[TThread[TArg]]) = var a: array[0..255, TSysThread] sysAssert a.len >= t.len, "a.len >= t.len" for i in 0..t.high: a[i] = t[i].sys - discard WaitForMultipleObjects(t.len.int32, + discard waitForMultipleObjects(t.len.int32, cast[ptr TSysThread](addr(a)), 1, -1) else: for i in 0..t.high: joinThread(t[i]) @@ -346,7 +346,7 @@ proc createThread*[TArg](t: var TThread[TArg], when hasSharedHeap: t.stackSize = ThreadStackSize when hostOS == "windows": var dummyThreadId: int32 - t.sys = CreateThread(nil, ThreadStackSize, threadProcWrapper[TArg], + t.sys = createThread(nil, ThreadStackSize, threadProcWrapper[TArg], addr(t), 0'i32, dummyThreadId) if t.sys <= 0: raise newException(EResourceExhausted, "cannot create thread") diff --git a/lib/system/widestrs.nim b/lib/system/widestrs.nim index cf1f0910c..e2a5d87e9 100644 --- a/lib/system/widestrs.nim +++ b/lib/system/widestrs.nim @@ -1,149 +1,153 @@ -# -# -# Nimrod's Runtime Library -# (c) Copyright 2012 Andreas Rumpf -# -# See the file "copying.txt", included in this -# distribution, for details about the copyright. -# - -## Nimrod support for C/C++'s `wide strings`:idx:. This is part of the system -## module! Do not import it directly! - -type - TUtf16Char* = distinct int16 - WideCString* = ref array[0.. 1_000_000, TUtf16Char] - -proc len*(w: WideCString): int = - ## returns the length of a widestring. This traverses the whole string to - ## find the binary zero end marker! - while int16(w[result]) != 0'i16: inc result - -const - UNI_REPLACEMENT_CHAR = TUtf16Char(0xFFFD'i16) - UNI_MAX_BMP = 0x0000FFFF - UNI_MAX_UTF16 = 0x0010FFFF - UNI_MAX_UTF32 = 0x7FFFFFFF - UNI_MAX_LEGAL_UTF32 = 0x0010FFFF - - halfShift = 10 - halfBase = 0x0010000 - halfMask = 0x3FF - - UNI_SUR_HIGH_START = 0xD800 - UNI_SUR_HIGH_END = 0xDBFF - UNI_SUR_LOW_START = 0xDC00 - UNI_SUR_LOW_END = 0xDFFF - -template ones(n: expr): expr = ((1 shl n)-1) - -template fastRuneAt(s: cstring, i: int, result: expr, doInc = true) = - ## Returns the unicode character ``s[i]`` in `result`. If ``doInc == true`` - ## `i` is incremented by the number of bytes that have been processed. - bind ones - - if ord(s[i]) <=% 127: - result = ord(s[i]) - when doInc: inc(i) - elif ord(s[i]) shr 5 == 0b110: - #assert(ord(s[i+1]) shr 6 == 0b10) - result = (ord(s[i]) and (ones(5))) shl 6 or (ord(s[i+1]) and ones(6)) - when doInc: inc(i, 2) - elif ord(s[i]) shr 4 == 0b1110: - #assert(ord(s[i+1]) shr 6 == 0b10) - #assert(ord(s[i+2]) shr 6 == 0b10) - result = (ord(s[i]) and ones(4)) shl 12 or - (ord(s[i+1]) and ones(6)) shl 6 or - (ord(s[i+2]) and ones(6)) - when doInc: inc(i, 3) - elif ord(s[i]) shr 3 == 0b11110: - #assert(ord(s[i+1]) shr 6 == 0b10) - #assert(ord(s[i+2]) shr 6 == 0b10) - #assert(ord(s[i+3]) shr 6 == 0b10) - result = (ord(s[i]) and ones(3)) shl 18 or - (ord(s[i+1]) and ones(6)) shl 12 or - (ord(s[i+2]) and ones(6)) shl 6 or - (ord(s[i+3]) and ones(6)) - when doInc: inc(i, 4) - else: - result = 0xFFFD - when doInc: inc(i) - -iterator runes(s: cstring): int = - var - i = 0 - result: int - while s[i] != '\0': - fastRuneAt(s, i, result, true) - yield result - -proc newWideCString*(source: cstring, L: int): WideCString = +# +# +# Nimrod's Runtime Library +# (c) Copyright 2012 Andreas Rumpf +# +# See the file "copying.txt", included in this +# distribution, for details about the copyright. +# + +## Nimrod support for C/C++'s `wide strings`:idx:. This is part of the system +## module! Do not import it directly! + +when not defined(NimString): + {.error: "You must not import this module explicitly".} + +type + TUtf16Char* = distinct int16 + WideCString* = ref array[0.. 1_000_000, TUtf16Char] + +proc len*(w: WideCString): int = + ## returns the length of a widestring. This traverses the whole string to + ## find the binary zero end marker! + while int16(w[result]) != 0'i16: inc result + +const + UNI_REPLACEMENT_CHAR = TUtf16Char(0xFFFD'i16) + UNI_MAX_BMP = 0x0000FFFF + UNI_MAX_UTF16 = 0x0010FFFF + UNI_MAX_UTF32 = 0x7FFFFFFF + UNI_MAX_LEGAL_UTF32 = 0x0010FFFF + + halfShift = 10 + halfBase = 0x0010000 + halfMask = 0x3FF + + UNI_SUR_HIGH_START = 0xD800 + UNI_SUR_HIGH_END = 0xDBFF + UNI_SUR_LOW_START = 0xDC00 + UNI_SUR_LOW_END = 0xDFFF + +template ones(n: expr): expr = ((1 shl n)-1) + +template fastRuneAt(s: cstring, i: int, result: expr, doInc = true) = + ## Returns the unicode character ``s[i]`` in `result`. If ``doInc == true`` + ## `i` is incremented by the number of bytes that have been processed. + bind ones + + if ord(s[i]) <=% 127: + result = ord(s[i]) + when doInc: inc(i) + elif ord(s[i]) shr 5 == 0b110: + #assert(ord(s[i+1]) shr 6 == 0b10) + result = (ord(s[i]) and (ones(5))) shl 6 or (ord(s[i+1]) and ones(6)) + when doInc: inc(i, 2) + elif ord(s[i]) shr 4 == 0b1110: + #assert(ord(s[i+1]) shr 6 == 0b10) + #assert(ord(s[i+2]) shr 6 == 0b10) + result = (ord(s[i]) and ones(4)) shl 12 or + (ord(s[i+1]) and ones(6)) shl 6 or + (ord(s[i+2]) and ones(6)) + when doInc: inc(i, 3) + elif ord(s[i]) shr 3 == 0b11110: + #assert(ord(s[i+1]) shr 6 == 0b10) + #assert(ord(s[i+2]) shr 6 == 0b10) + #assert(ord(s[i+3]) shr 6 == 0b10) + result = (ord(s[i]) and ones(3)) shl 18 or + (ord(s[i+1]) and ones(6)) shl 12 or + (ord(s[i+2]) and ones(6)) shl 6 or + (ord(s[i+3]) and ones(6)) + when doInc: inc(i, 4) + else: + result = 0xFFFD + when doInc: inc(i) + +iterator runes(s: cstring): int = + var + i = 0 + result: int + while s[i] != '\0': + fastRuneAt(s, i, result, true) + yield result + +proc newWideCString*(source: cstring, L: int): WideCString = unsafeNew(result, L * 4 + 2) - #result = cast[wideCString](alloc(L * 4 + 2)) - var d = 0 - for ch in runes(source): - if ch <=% UNI_MAX_BMP: - if ch >=% UNI_SUR_HIGH_START and ch <=% UNI_SUR_LOW_END: - result[d] = UNI_REPLACEMENT_CHAR - else: - result[d] = TUtf16Char(toU16(ch)) - elif ch >% UNI_MAX_UTF16: - result[d] = UNI_REPLACEMENT_CHAR - else: - let ch = ch -% halfBase - result[d] = TUtf16Char(toU16((ch shr halfShift) +% UNI_SUR_HIGH_START)) - inc d - result[d] = TUtf16Char(toU16((ch and halfMask) +% UNI_SUR_LOW_START)) - inc d - result[d] = TUtf16Char(0'i16) - -proc newWideCString*(s: cstring): WideCString = - if s.isNil: return nil - - when not defined(c_strlen): - proc c_strlen(a: CString): int {.nodecl, noSideEffect, importc: "strlen".} - - let L = cstrlen(s) - result = newWideCString(s, L) - -proc newWideCString*(s: string): WideCString = - result = newWideCString(s, s.len) - -proc `$`*(w: wideCString, estimate: int): string = - result = newStringOfCap(estimate + estimate shr 2) - - var i = 0 - while w[i].int16 != 0'i16: - var ch = w[i].int - inc i - if ch >=% UNI_SUR_HIGH_START and ch <=% UNI_SUR_HIGH_END: - # If the 16 bits following the high surrogate are in the source buffer... - let ch2 = w[i].int - # If it's a low surrogate, convert to UTF32: - if ch2 >=% UNI_SUR_LOW_START and ch2 <=% UNI_SUR_LOW_END: - ch = ((ch -% UNI_SUR_HIGH_START) shr halfShift) +% - (ch2 -% UNI_SUR_LOW_START) +% halfBase - inc i - - if ch <=% 127: - result.add chr(ch) - elif ch <=% 0x07FF: - result.add chr((ch shr 6) or 0b110_00000) - result.add chr((ch and ones(6)) or 0b10_000000) - elif ch <=% 0xFFFF: - result.add chr(ch shr 12 or 0b1110_0000) - result.add chr(ch shr 6 and ones(6) or 0b10_0000_00) - result.add chr(ch and ones(6) or 0b10_0000_00) - elif ch <=% 0x0010FFFF: - result.add chr(ch shr 18 or 0b1111_0000) - result.add chr(ch shr 12 and ones(6) or 0b10_0000_00) - result.add chr(ch shr 6 and ones(6) or 0b10_0000_00) - result.add chr(ch and ones(6) or 0b10_0000_00) - else: - # replacement char: - result.add chr(0xFFFD shr 12 or 0b1110_0000) - result.add chr(0xFFFD shr 6 and ones(6) or 0b10_0000_00) - result.add chr(0xFFFD and ones(6) or 0b10_0000_00) - -proc `$`*(s: WideCString): string = - result = s $ 80 + #result = cast[wideCString](alloc(L * 4 + 2)) + var d = 0 + for ch in runes(source): + if ch <=% UNI_MAX_BMP: + if ch >=% UNI_SUR_HIGH_START and ch <=% UNI_SUR_LOW_END: + result[d] = UNI_REPLACEMENT_CHAR + else: + result[d] = TUtf16Char(toU16(ch)) + elif ch >% UNI_MAX_UTF16: + result[d] = UNI_REPLACEMENT_CHAR + else: + let ch = ch -% halfBase + result[d] = TUtf16Char(toU16((ch shr halfShift) +% UNI_SUR_HIGH_START)) + inc d + result[d] = TUtf16Char(toU16((ch and halfMask) +% UNI_SUR_LOW_START)) + inc d + result[d] = TUtf16Char(0'i16) + +proc newWideCString*(s: cstring): WideCString = + if s.isNil: return nil + + when not defined(c_strlen): + proc c_strlen(a: cstring): int {. + header: "<string.h>", noSideEffect, importc: "strlen".} + + let L = c_strlen(s) + result = newWideCString(s, L) + +proc newWideCString*(s: string): WideCString = + result = newWideCString(s, s.len) + +proc `$`*(w: WideCString, estimate: int): string = + result = newStringOfCap(estimate + estimate shr 2) + + var i = 0 + while w[i].int16 != 0'i16: + var ch = w[i].int + inc i + if ch >=% UNI_SUR_HIGH_START and ch <=% UNI_SUR_HIGH_END: + # If the 16 bits following the high surrogate are in the source buffer... + let ch2 = w[i].int + # If it's a low surrogate, convert to UTF32: + if ch2 >=% UNI_SUR_LOW_START and ch2 <=% UNI_SUR_LOW_END: + ch = ((ch -% UNI_SUR_HIGH_START) shr halfShift) +% + (ch2 -% UNI_SUR_LOW_START) +% halfBase + inc i + + if ch <=% 127: + result.add chr(ch) + elif ch <=% 0x07FF: + result.add chr((ch shr 6) or 0b110_00000) + result.add chr((ch and ones(6)) or 0b10_000000) + elif ch <=% 0xFFFF: + result.add chr(ch shr 12 or 0b1110_0000) + result.add chr(ch shr 6 and ones(6) or 0b10_0000_00) + result.add chr(ch and ones(6) or 0b10_0000_00) + elif ch <=% 0x0010FFFF: + result.add chr(ch shr 18 or 0b1111_0000) + result.add chr(ch shr 12 and ones(6) or 0b10_0000_00) + result.add chr(ch shr 6 and ones(6) or 0b10_0000_00) + result.add chr(ch and ones(6) or 0b10_0000_00) + else: + # replacement char: + result.add chr(0xFFFD shr 12 or 0b1110_0000) + result.add chr(0xFFFD shr 6 and ones(6) or 0b10_0000_00) + result.add chr(0xFFFD and ones(6) or 0b10_0000_00) + +proc `$`*(s: WideCString): string = + result = s $ 80 |