summary refs log tree commit diff stats
path: root/lib/system/gc_common.nim
blob: 47e8b4b1f7fb864aec397dce7ae7fb35407f3229 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#
#
#            Nim's Runtime Library
#        (c) Copyright 2015 Rokas Kupstys
#
#    See the file "copying.txt", included in this
#    distribution, for details about the copyright.
#

proc len(stack: ptr GcStack): int =
  if stack == nil:
    return 0

  var s = stack
  result = 1
  while s.next != nil:
    inc(result)
    s = s.next

when defined(nimCoroutines):
  proc stackSize(stackBottom: pointer, pos: pointer=nil): int {.noinline.} =
    var sp: pointer
    if pos == nil:
      var stackTop {.volatile.}: pointer
      sp = addr(stackTop)
    else:
      sp = pos
    result = abs(cast[int](sp) - cast[int](stackBottom))

  proc GC_addStack*(starts: pointer) {.cdecl, exportc.} =
    var sp {.volatile.}: pointer
    var stack = cast[ptr GcStack](alloc0(sizeof(GcStack)))
    stack.starts = starts
    stack.pos = addr sp
    if gch.stack == nil:
      gch.stack = stack
    else:
      stack.next = gch.stack
      gch.stack.prev = stack
      gch.stack = stack
    # c_fprintf(c_stdout, "[GC] added stack 0x%016X\n", starts)

  proc GC_removeStack*(starts: pointer) {.cdecl, exportc.} =
    var stack = gch.stack
    while stack != nil:
      if stack.starts == starts:
        if stack.prev == nil:
          if stack.next != nil:
            stack.next.prev = nil
          gch.stack = stack.next
        else:
          stack.prev.next = stack.next
          if stack.next != nil:
              stack.next.prev = stack.prev
        dealloc(stack)
        # echo "[GC] removed stack ", starts.repr
        break
      else:
        stack = stack.next

  proc GC_setCurrentStack*(starts, pos: pointer) {.cdecl, exportc.} =
    var stack = gch.stack
    while stack != nil:
      if stack.starts == starts:
        stack.pos = pos
        stack.maxStackSize = max(stack.maxStackSize, stackSize(stack.starts, pos))
        return
      stack = stack.next
    gcAssert(false, "Current stack position does not belong to registered stack")
else:
  proc stackSize(): int {.noinline.} =
    var stackTop {.volatile.}: pointer
    result = abs(cast[int](addr(stackTop)) - cast[int](gch.stackBottom))

iterator items(stack: ptr GcStack): ptr GcStack =
  var s = stack
  while not isNil(s):
    yield s
    s = s.next

var
  localGcInitialized {.rtlThreadVar.}: bool

proc setupForeignThreadGc*() =
  ## call this if you registered a callback that will be run from a thread not
  ## under your control. This has a cheap thread-local guard, so the GC for
  ## this thread will only be initialized once per thread, no matter how often
  ## it is called.
  if not localGcInitialized:
    localGcInitialized = true
    var stackTop {.volatile.}: pointer
    setStackBottom(addr(stackTop))
    initGC()

# ----------------- stack management --------------------------------------
#  inspired from Smart Eiffel

when defined(emscripten):
  const stackIncreases = true
elif defined(sparc):
  const stackIncreases = false
elif defined(hppa) or defined(hp9000) or defined(hp9000s300) or
     defined(hp9000s700) or defined(hp9000s800) or defined(hp9000s820):
  const stackIncreases = true
else:
  const stackIncreases = false

when not defined(useNimRtl):
  {.push stack_trace: off.}
  proc setStackBottom(theStackBottom: pointer) =
    #c_fprintf(c_stdout, "stack bottom: %p;\n", theStackBottom)
    # the first init must be the one that defines the stack bottom:
    when defined(nimCoroutines):
      GC_addStack(theStackBottom)
    else:
      if gch.stackBottom == nil: gch.stackBottom = theStackBottom
      else:
        var a = cast[ByteAddress](theStackBottom) # and not PageMask - PageSize*2
        var b = cast[ByteAddress](gch.stackBottom)
        #c_fprintf(c_stdout, "old: %p new: %p;\n",gch.stackBottom,theStackBottom)
        when stackIncreases:
          gch.stackBottom = cast[pointer](min(a, b))
        else:
          gch.stackBottom = cast[pointer](max(a, b))
  {.pop.}

when defined(sparc): # For SPARC architecture.
  when defined(nimCoroutines):
    {.error: "Nim coroutines are not supported on this platform."}

  proc isOnStack(p: pointer): bool =
    var stackTop {.volatile.}: pointer
    stackTop = addr(stackTop)
    var b = cast[TAddress](gch.stackBottom)
    var a = cast[TAddress](stackTop)
    var x = cast[TAddress](p)
    result = a <=% x and x <=% b

  template forEachStackSlot(gch, gcMark: expr) {.immediate, dirty.} =
    when defined(sparcv9):
      asm  """"flushw \n" """
    else:
      asm  """"ta      0x3   ! ST_FLUSH_WINDOWS\n" """

    var
      max = gch.stackBottom
      sp: PPointer
      stackTop: array[0..1, pointer]
    sp = addr(stackTop[0])
    # Addresses decrease as the stack grows.
    while sp <= max:
      gcMark(gch, sp[])
      sp = cast[PPointer](cast[TAddress](sp) +% sizeof(pointer))

elif defined(ELATE):
  {.error: "stack marking code is to be written for this architecture".}

elif stackIncreases:
  # ---------------------------------------------------------------------------
  # Generic code for architectures where addresses increase as the stack grows.
  # ---------------------------------------------------------------------------
  when defined(nimCoroutines):
    {.error: "Nim coroutines are not supported on this platform."}
  proc isOnStack(p: pointer): bool =
    var stackTop {.volatile.}: pointer
    stackTop = addr(stackTop)
    var a = cast[ByteAddress](gch.stackBottom)
    var b = cast[ByteAddress](stackTop)
    var x = cast[ByteAddress](p)
    result = a <=% x and x <=% b

  var
    jmpbufSize {.importc: "sizeof(jmp_buf)", nodecl.}: int
      # a little hack to get the size of a JmpBuf in the generated C code
      # in a platform independent way

  template forEachStackSlot(gch, gcMark: expr) {.immediate, dirty.} =
    var registers {.noinit.}: C_JmpBuf
    if c_setjmp(registers) == 0'i32: # To fill the C stack with registers.
      var max = cast[ByteAddress](gch.stackBottom)
      var sp = cast[ByteAddress](addr(registers)) +% jmpbufSize -% sizeof(pointer)
      # sp will traverse the JMP_BUF as well (jmp_buf size is added,
      # otherwise sp would be below the registers structure).
      while sp >=% max:
        gcMark(gch, cast[PPointer](sp)[])
        sp = sp -% sizeof(pointer)

else:
  # ---------------------------------------------------------------------------
  # Generic code for architectures where addresses decrease as the stack grows.
  # ---------------------------------------------------------------------------
  when defined(nimCoroutines):
    proc isOnStack(p: pointer): bool =
      var stackTop {.volatile.}: pointer
      stackTop = addr(stackTop)
      for stack in items(gch.stack):
        var b = cast[ByteAddress](stack.starts)
        var a = cast[ByteAddress](stack.starts) - stack.maxStackSize
        var x = cast[ByteAddress](p)
        if a <=% x and x <=% b:
          return true

    template forEachStackSlot(gch, gcMark: expr) {.immediate, dirty.} =
      # We use a jmp_buf buffer that is in the C stack.
      # Used to traverse the stack and registers assuming
      # that 'setjmp' will save registers in the C stack.
      type PStackSlice = ptr array [0..7, pointer]
      var registers {.noinit.}: Registers
      getRegisters(registers)
      for i in registers.low .. registers.high:
        gcMark(gch, cast[PPointer](registers[i]))

      for stack in items(gch.stack):
        stack.maxStackSize = max(stack.maxStackSize, stackSize(stack.starts))
        var max = cast[ByteAddress](stack.starts)
        var sp = cast[ByteAddress](stack.pos)
        # loop unrolled:
        while sp <% max - 8*sizeof(pointer):
          gcMark(gch, cast[PStackSlice](sp)[0])
          gcMark(gch, cast[PStackSlice](sp)[1])
          gcMark(gch, cast[PStackSlice](sp)[2])
          gcMark(gch, cast[PStackSlice](sp)[3])
          gcMark(gch, cast[PStackSlice](sp)[4])
          gcMark(gch, cast[PStackSlice](sp)[5])
          gcMark(gch, cast[PStackSlice](sp)[6])
          gcMark(gch, cast[PStackSlice](sp)[7])
          sp = sp +% sizeof(pointer)*8
        # last few entries:
        while sp <=% max:
          gcMark(gch, cast[PPointer](sp)[])
          sp = sp +% sizeof(pointer)
  else:
    proc isOnStack(p: pointer): bool =
      var stackTop {.volatile.}: pointer
      stackTop = addr(stackTop)
      var b = cast[ByteAddress](gch.stackBottom)
      var a = cast[ByteAddress](stackTop)
      var x = cast[ByteAddress](p)
      result = a <=% x and x <=% b

    template forEachStackSlot(gch, gcMark: expr) {.immediate, dirty.} =
      # We use a jmp_buf buffer that is in the C stack.
      # Used to traverse the stack and registers assuming
      # that 'setjmp' will save registers in the C stack.
      type PStackSlice = ptr array [0..7, pointer]
      var registers {.noinit.}: C_JmpBuf
      if c_setjmp(registers) == 0'i32: # To fill the C stack with registers.
        var max = cast[ByteAddress](gch.stackBottom)
        var sp = cast[ByteAddress](addr(registers))
        when defined(amd64):
          # words within the jmp_buf structure may not be properly aligned.
          let regEnd = sp +% sizeof(registers)
          while sp <% regEnd:
            gcMark(gch, cast[PPointer](sp)[])
            gcMark(gch, cast[PPointer](sp +% sizeof(pointer) div 2)[])
            sp = sp +% sizeof(pointer)
        # Make sure sp is word-aligned
        sp = sp and not (sizeof(pointer) - 1)
        # loop unrolled:
        while sp <% max - 8*sizeof(pointer):
          gcMark(gch, cast[PStackSlice](sp)[0])
          gcMark(gch, cast[PStackSlice](sp)[1])
          gcMark(gch, cast[PStackSlice](sp)[2])
          gcMark(gch, cast[PStackSlice](sp)[3])
          gcMark(gch, cast[PStackSlice](sp)[4])
          gcMark(gch, cast[PStackSlice](sp)[5])
          gcMark(gch, cast[PStackSlice](sp)[6])
          gcMark(gch, cast[PStackSlice](sp)[7])
          sp = sp +% sizeof(pointer)*8
        # last few entries:
        while sp <=% max:
          gcMark(gch, cast[PPointer](sp)[])
          sp = sp +% sizeof(pointer)

# ----------------------------------------------------------------------------
# end of non-portable code
# ----------------------------------------------------------------------------