diff --git a/compiler/semfold.nim b/compiler/semfold.nim index b134d666d3..020d1e46a7 100644 --- a/compiler/semfold.nim +++ b/compiler/semfold.nim @@ -16,7 +16,7 @@ import commands, magicsys, modulegraphs, lineinfos, wordrecg import std/[strutils, math, strtabs] -from system/memory import nimCStrLen +#from system/memory import nimCStrLen when defined(nimPreviewSlimSystem): import std/[assertions, formatfloat] diff --git a/lib/system.nim b/lib/system.nim index ecc14b2ea7..e51a0965f7 100644 --- a/lib/system.nim +++ b/lib/system.nim @@ -1618,7 +1618,7 @@ proc instantiationInfo*(index = -1, fullPaths = false): tuple[ when notJSnotNims: import system/ansi_c - import system/memory + include system/sysmem when notJSnotNims and defined(nimSeqsV2): const nimStrVersion {.core.} = 2 diff --git a/lib/system/memory.nim b/lib/system/memory.nim index c6c3cb3ab0..72a7b73d2c 100644 --- a/lib/system/memory.nim +++ b/lib/system/memory.nim @@ -2,10 +2,9 @@ const useLibC = not defined(nimNoLibc) -when useLibC: - import ansi_c +import ansi_c -proc nimCopyMem*(dest, source: pointer, size: Natural) {.nonReloadable, compilerproc, inline, enforceNoRaises.} = +proc nimCopyMem*(dest, source: pointer, size: Natural) {.nonReloadable, inline, enforceNoRaises.} = when useLibC: c_memcpy(dest, source, cast[csize_t](size)) else: @@ -27,10 +26,10 @@ proc nimSetMem*(a: pointer, v: cint, size: Natural) {.nonReloadable, inline, enf a[i] = v inc i -proc nimZeroMem*(p: pointer, size: Natural) {.compilerproc, nonReloadable, inline, enforceNoRaises.} = +proc nimZeroMem*(p: pointer, size: Natural) {.nonReloadable, inline, enforceNoRaises.} = nimSetMem(p, 0, size) -proc nimCmpMem*(a, b: pointer, size: Natural): cint {.compilerproc, nonReloadable, inline, enforceNoRaises.} = +proc nimCmpMem*(a, b: pointer, size: Natural): cint {.nonReloadable, inline, enforceNoRaises.} = when useLibC: c_memcmp(a, b, cast[csize_t](size)) else: @@ -42,7 +41,7 @@ proc nimCmpMem*(a, b: pointer, size: Natural): cint {.compilerproc, nonReloadabl if d != 0: return d inc i -proc nimCStrLen*(a: cstring): int {.compilerproc, nonReloadable, inline, enforceNoRaises.} = +proc nimCStrLen*(a: cstring): int {.nonReloadable, inline, enforceNoRaises.} = if a.isNil: return 0 when useLibC: cast[int](c_strlen(a)) diff --git a/lib/system/sysmem.nim b/lib/system/sysmem.nim new file mode 100644 index 0000000000..c1d1b57138 --- /dev/null +++ b/lib/system/sysmem.nim @@ -0,0 +1,52 @@ +{.push stack_trace: off.} + +const useLibC = not defined(nimNoLibc) + +proc nimCopyMem(dest, source: pointer, size: Natural) {.nonReloadable, compilerproc, inline, enforceNoRaises.} = + when useLibC: + c_memcpy(dest, source, cast[csize_t](size)) + else: + let d = cast[ptr UncheckedArray[byte]](dest) + let s = cast[ptr UncheckedArray[byte]](source) + var i = 0 + while i < size: + d[i] = s[i] + inc i + +proc nimSetMem(a: pointer, v: cint, size: Natural) {.nonReloadable, inline, enforceNoRaises.} = + when useLibC: + c_memset(a, v, cast[csize_t](size)) + else: + let a = cast[ptr UncheckedArray[byte]](a) + var i = 0 + let v = cast[byte](v) + while i < size: + a[i] = v + inc i + +proc nimZeroMem(p: pointer, size: Natural) {.compilerproc, nonReloadable, inline, enforceNoRaises.} = + nimSetMem(p, 0, size) + +proc nimCmpMem(a, b: pointer, size: Natural): cint {.compilerproc, nonReloadable, inline, enforceNoRaises.} = + when useLibC: + c_memcmp(a, b, cast[csize_t](size)) + else: + let a = cast[ptr UncheckedArray[byte]](a) + let b = cast[ptr UncheckedArray[byte]](b) + var i = 0 + while i < size: + let d = a[i].cint - b[i].cint + if d != 0: return d + inc i + +proc nimCStrLen*(a: cstring): int {.compilerproc, nonReloadable, inline, enforceNoRaises.} = + if a.isNil: return 0 + when useLibC: + cast[int](c_strlen(a)) + else: + var a = cast[ptr byte](a) + while a[] != 0: + a = cast[ptr byte](cast[uint](a) + 1) + inc result + +{.pop.}