Skip to content

Commit 80cf9a8

Browse files
authored
system.nim: memory must be part of system so that its compilerprocs c… (#25365)
…an work for IC
1 parent 8747160 commit 80cf9a8

File tree

4 files changed

+59
-8
lines changed

4 files changed

+59
-8
lines changed

compiler/semfold.nim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import
1616
commands, magicsys, modulegraphs, lineinfos, wordrecg
1717

1818
import std/[strutils, math, strtabs]
19-
from system/memory import nimCStrLen
19+
#from system/memory import nimCStrLen
2020

2121
when defined(nimPreviewSlimSystem):
2222
import std/[assertions, formatfloat]

lib/system.nim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1618,7 +1618,7 @@ proc instantiationInfo*(index = -1, fullPaths = false): tuple[
16181618

16191619
when notJSnotNims:
16201620
import system/ansi_c
1621-
import system/memory
1621+
include system/sysmem
16221622

16231623
when notJSnotNims and defined(nimSeqsV2):
16241624
const nimStrVersion {.core.} = 2

lib/system/memory.nim

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@
22

33
const useLibC = not defined(nimNoLibc)
44

5-
when useLibC:
6-
import ansi_c
5+
import ansi_c
76

8-
proc nimCopyMem*(dest, source: pointer, size: Natural) {.nonReloadable, compilerproc, inline, enforceNoRaises.} =
7+
proc nimCopyMem*(dest, source: pointer, size: Natural) {.nonReloadable, inline, enforceNoRaises.} =
98
when useLibC:
109
c_memcpy(dest, source, cast[csize_t](size))
1110
else:
@@ -27,10 +26,10 @@ proc nimSetMem*(a: pointer, v: cint, size: Natural) {.nonReloadable, inline, enf
2726
a[i] = v
2827
inc i
2928

30-
proc nimZeroMem*(p: pointer, size: Natural) {.compilerproc, nonReloadable, inline, enforceNoRaises.} =
29+
proc nimZeroMem*(p: pointer, size: Natural) {.nonReloadable, inline, enforceNoRaises.} =
3130
nimSetMem(p, 0, size)
3231

33-
proc nimCmpMem*(a, b: pointer, size: Natural): cint {.compilerproc, nonReloadable, inline, enforceNoRaises.} =
32+
proc nimCmpMem*(a, b: pointer, size: Natural): cint {.nonReloadable, inline, enforceNoRaises.} =
3433
when useLibC:
3534
c_memcmp(a, b, cast[csize_t](size))
3635
else:
@@ -42,7 +41,7 @@ proc nimCmpMem*(a, b: pointer, size: Natural): cint {.compilerproc, nonReloadabl
4241
if d != 0: return d
4342
inc i
4443

45-
proc nimCStrLen*(a: cstring): int {.compilerproc, nonReloadable, inline, enforceNoRaises.} =
44+
proc nimCStrLen*(a: cstring): int {.nonReloadable, inline, enforceNoRaises.} =
4645
if a.isNil: return 0
4746
when useLibC:
4847
cast[int](c_strlen(a))

lib/system/sysmem.nim

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
{.push stack_trace: off.}
2+
3+
const useLibC = not defined(nimNoLibc)
4+
5+
proc nimCopyMem(dest, source: pointer, size: Natural) {.nonReloadable, compilerproc, inline, enforceNoRaises.} =
6+
when useLibC:
7+
c_memcpy(dest, source, cast[csize_t](size))
8+
else:
9+
let d = cast[ptr UncheckedArray[byte]](dest)
10+
let s = cast[ptr UncheckedArray[byte]](source)
11+
var i = 0
12+
while i < size:
13+
d[i] = s[i]
14+
inc i
15+
16+
proc nimSetMem(a: pointer, v: cint, size: Natural) {.nonReloadable, inline, enforceNoRaises.} =
17+
when useLibC:
18+
c_memset(a, v, cast[csize_t](size))
19+
else:
20+
let a = cast[ptr UncheckedArray[byte]](a)
21+
var i = 0
22+
let v = cast[byte](v)
23+
while i < size:
24+
a[i] = v
25+
inc i
26+
27+
proc nimZeroMem(p: pointer, size: Natural) {.compilerproc, nonReloadable, inline, enforceNoRaises.} =
28+
nimSetMem(p, 0, size)
29+
30+
proc nimCmpMem(a, b: pointer, size: Natural): cint {.compilerproc, nonReloadable, inline, enforceNoRaises.} =
31+
when useLibC:
32+
c_memcmp(a, b, cast[csize_t](size))
33+
else:
34+
let a = cast[ptr UncheckedArray[byte]](a)
35+
let b = cast[ptr UncheckedArray[byte]](b)
36+
var i = 0
37+
while i < size:
38+
let d = a[i].cint - b[i].cint
39+
if d != 0: return d
40+
inc i
41+
42+
proc nimCStrLen*(a: cstring): int {.compilerproc, nonReloadable, inline, enforceNoRaises.} =
43+
if a.isNil: return 0
44+
when useLibC:
45+
cast[int](c_strlen(a))
46+
else:
47+
var a = cast[ptr byte](a)
48+
while a[] != 0:
49+
a = cast[ptr byte](cast[uint](a) + 1)
50+
inc result
51+
52+
{.pop.}

0 commit comments

Comments
 (0)