sysstr.nim 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. #
  2. #
  3. # Nim's Runtime Library
  4. # (c) Copyright 2012 Andreas Rumpf
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. # string & sequence handling procedures needed by the code generator
  10. # strings are dynamically resized, have a length field
  11. # and are zero-terminated, so they can be casted to C
  12. # strings easily
  13. # we don't use refcounts because that's a behaviour
  14. # the programmer may not want
  15. proc dataPointer(a: PGenericSeq, elemAlign: int): pointer =
  16. cast[pointer](cast[ByteAddress](a) +% align(GenericSeqSize, elemAlign))
  17. proc dataPointer(a: PGenericSeq, elemAlign, elemSize, index: int): pointer =
  18. cast[pointer](cast[ByteAddress](a) +% align(GenericSeqSize, elemAlign) +% (index*%elemSize))
  19. proc resize(old: int): int {.inline.} =
  20. if old <= 0: result = 4
  21. elif old < 65536: result = old * 2
  22. else: result = old * 3 div 2 # for large arrays * 3/2 is better
  23. when declared(allocAtomic):
  24. template allocStr(size: untyped): untyped =
  25. cast[NimString](allocAtomic(size))
  26. template allocStrNoInit(size: untyped): untyped =
  27. cast[NimString](boehmAllocAtomic(size))
  28. elif defined(gcRegions):
  29. template allocStr(size: untyped): untyped =
  30. cast[NimString](newStr(addr(strDesc), size, true))
  31. template allocStrNoInit(size: untyped): untyped =
  32. cast[NimString](newStr(addr(strDesc), size, false))
  33. else:
  34. template allocStr(size: untyped): untyped =
  35. cast[NimString](newObj(addr(strDesc), size))
  36. template allocStrNoInit(size: untyped): untyped =
  37. cast[NimString](newObjNoInit(addr(strDesc), size))
  38. proc rawNewStringNoInit(space: int): NimString {.compilerproc.} =
  39. var s = space
  40. if s < 7: s = 7
  41. result = allocStrNoInit(sizeof(TGenericSeq) + s + 1)
  42. result.reserved = s
  43. result.len = 0
  44. when defined(gogc):
  45. result.elemSize = 1
  46. proc rawNewString(space: int): NimString {.compilerproc.} =
  47. var s = space
  48. if s < 7: s = 7
  49. result = allocStr(sizeof(TGenericSeq) + s + 1)
  50. result.reserved = s
  51. result.len = 0
  52. when defined(gogc):
  53. result.elemSize = 1
  54. proc mnewString(len: int): NimString {.compilerproc.} =
  55. result = rawNewString(len)
  56. result.len = len
  57. proc copyStrLast(s: NimString, start, last: int): NimString {.compilerproc.} =
  58. # This is not used by most recent versions of the compiler anymore, but
  59. # required for bootstrapping purposes.
  60. let start = max(start, 0)
  61. if s == nil: return nil
  62. let len = min(last, s.len-1) - start + 1
  63. if len > 0:
  64. result = rawNewStringNoInit(len)
  65. result.len = len
  66. copyMem(addr(result.data), addr(s.data[start]), len)
  67. result.data[len] = '\0'
  68. else:
  69. result = rawNewString(len)
  70. proc copyStr(s: NimString, start: int): NimString {.compilerproc.} =
  71. # This is not used by most recent versions of the compiler anymore, but
  72. # required for bootstrapping purposes.
  73. if s == nil: return nil
  74. result = copyStrLast(s, start, s.len-1)
  75. proc nimToCStringConv(s: NimString): cstring {.compilerproc, nonReloadable, inline.} =
  76. if s == nil or s.len == 0: result = cstring""
  77. else: result = cstring(addr s.data)
  78. proc toNimStr(str: cstring, len: int): NimString {.compilerproc.} =
  79. result = rawNewStringNoInit(len)
  80. result.len = len
  81. copyMem(addr(result.data), str, len + 1)
  82. proc cstrToNimstr(str: cstring): NimString {.compilerRtl.} =
  83. if str == nil: NimString(nil)
  84. else: toNimStr(str, str.len)
  85. proc copyString(src: NimString): NimString {.compilerRtl.} =
  86. if src != nil:
  87. if (src.reserved and seqShallowFlag) != 0:
  88. result = src
  89. else:
  90. result = rawNewStringNoInit(src.len)
  91. result.len = src.len
  92. copyMem(addr(result.data), addr(src.data), src.len + 1)
  93. sysAssert((seqShallowFlag and result.reserved) == 0, "copyString")
  94. when defined(nimShallowStrings):
  95. if (src.reserved and strlitFlag) != 0:
  96. result.reserved = (result.reserved and not strlitFlag) or seqShallowFlag
  97. proc newOwnedString(src: NimString; n: int): NimString =
  98. result = rawNewStringNoInit(n)
  99. result.len = n
  100. copyMem(addr(result.data), addr(src.data), n)
  101. result.data[n] = '\0'
  102. proc copyStringRC1(src: NimString): NimString {.compilerRtl.} =
  103. if src != nil:
  104. if (src.reserved and seqShallowFlag) != 0:
  105. result = src
  106. when declared(incRef):
  107. incRef(usrToCell(result))
  108. else:
  109. when declared(newObjRC1) and not defined(gcRegions):
  110. var s = src.len
  111. if s < 7: s = 7
  112. result = cast[NimString](newObjRC1(addr(strDesc), sizeof(TGenericSeq) +
  113. s+1))
  114. result.reserved = s
  115. when defined(gogc):
  116. result.elemSize = 1
  117. else:
  118. result = rawNewStringNoInit(src.len)
  119. result.len = src.len
  120. copyMem(addr(result.data), addr(src.data), src.len + 1)
  121. sysAssert((seqShallowFlag and result.reserved) == 0, "copyStringRC1")
  122. when defined(nimShallowStrings):
  123. if (src.reserved and strlitFlag) != 0:
  124. result.reserved = (result.reserved and not strlitFlag) or seqShallowFlag
  125. proc copyDeepString(src: NimString): NimString {.inline.} =
  126. if src != nil:
  127. result = rawNewStringNoInit(src.len)
  128. result.len = src.len
  129. copyMem(addr(result.data), addr(src.data), src.len + 1)
  130. proc addChar(s: NimString, c: char): NimString =
  131. # is compilerproc!
  132. if s == nil:
  133. result = rawNewStringNoInit(1)
  134. result.len = 0
  135. else:
  136. result = s
  137. if result.len >= result.space:
  138. let r = resize(result.space)
  139. when defined(nimIncrSeqV3):
  140. result = rawNewStringNoInit(r)
  141. result.len = s.len
  142. copyMem(addr result.data[0], unsafeAddr(s.data[0]), s.len+1)
  143. else:
  144. result = cast[NimString](growObj(result,
  145. sizeof(TGenericSeq) + r + 1))
  146. result.reserved = r
  147. result.data[result.len] = c
  148. result.data[result.len+1] = '\0'
  149. inc(result.len)
  150. # These routines should be used like following:
  151. # <Nim code>
  152. # s &= "Hello " & name & ", how do you feel?"
  153. #
  154. # <generated C code>
  155. # {
  156. # s = resizeString(s, 6 + name->len + 17);
  157. # appendString(s, strLit1);
  158. # appendString(s, strLit2);
  159. # appendString(s, strLit3);
  160. # }
  161. #
  162. # <Nim code>
  163. # s = "Hello " & name & ", how do you feel?"
  164. #
  165. # <generated C code>
  166. # {
  167. # string tmp0;
  168. # tmp0 = rawNewString(6 + name->len + 17);
  169. # appendString(s, strLit1);
  170. # appendString(s, strLit2);
  171. # appendString(s, strLit3);
  172. # s = tmp0;
  173. # }
  174. #
  175. # <Nim code>
  176. # s = ""
  177. #
  178. # <generated C code>
  179. # s = rawNewString(0);
  180. proc resizeString(dest: NimString, addlen: int): NimString {.compilerRtl.} =
  181. if dest == nil:
  182. result = rawNewStringNoInit(addlen)
  183. elif dest.len + addlen <= dest.space:
  184. result = dest
  185. else: # slow path:
  186. let sp = max(resize(dest.space), dest.len + addlen)
  187. when defined(nimIncrSeqV3):
  188. result = rawNewStringNoInit(sp)
  189. result.len = dest.len
  190. copyMem(addr result.data[0], unsafeAddr(dest.data[0]), dest.len+1)
  191. else:
  192. result = cast[NimString](growObj(dest, sizeof(TGenericSeq) + sp + 1))
  193. result.reserved = sp
  194. #result = rawNewString(sp)
  195. #copyMem(result, dest, dest.len + sizeof(TGenericSeq))
  196. # DO NOT UPDATE LEN YET: dest.len = newLen
  197. proc appendString(dest, src: NimString) {.compilerproc, inline.} =
  198. if src != nil:
  199. copyMem(addr(dest.data[dest.len]), addr(src.data), src.len + 1)
  200. inc(dest.len, src.len)
  201. proc appendChar(dest: NimString, c: char) {.compilerproc, inline.} =
  202. dest.data[dest.len] = c
  203. dest.data[dest.len+1] = '\0'
  204. inc(dest.len)
  205. proc setLengthStr(s: NimString, newLen: int): NimString {.compilerRtl.} =
  206. let n = max(newLen, 0)
  207. if s == nil:
  208. result = mnewString(newLen)
  209. elif n <= s.space:
  210. result = s
  211. else:
  212. let sp = max(resize(s.space), newLen)
  213. when defined(nimIncrSeqV3):
  214. result = rawNewStringNoInit(sp)
  215. result.len = s.len
  216. copyMem(addr result.data[0], unsafeAddr(s.data[0]), s.len+1)
  217. zeroMem(addr result.data[s.len], newLen - s.len)
  218. result.reserved = sp
  219. else:
  220. result = resizeString(s, n)
  221. result.len = n
  222. result.data[n] = '\0'
  223. # ----------------- sequences ----------------------------------------------
  224. proc incrSeq(seq: PGenericSeq, elemSize, elemAlign: int): PGenericSeq {.compilerproc.} =
  225. # increments the length by one:
  226. # this is needed for supporting ``add``;
  227. #
  228. # add(seq, x) generates:
  229. # seq = incrSeq(seq, sizeof(x));
  230. # seq[seq->len-1] = x;
  231. result = seq
  232. if result.len >= result.space:
  233. let r = resize(result.space)
  234. result = cast[PGenericSeq](growObj(result, align(GenericSeqSize, elemAlign) + elemSize * r))
  235. result.reserved = r
  236. inc(result.len)
  237. proc incrSeqV2(seq: PGenericSeq, elemSize, elemAlign: int): PGenericSeq {.compilerproc.} =
  238. # incrSeq version 2
  239. result = seq
  240. if result.len >= result.space:
  241. let r = resize(result.space)
  242. result = cast[PGenericSeq](growObj(result, align(GenericSeqSize, elemAlign) + elemSize * r))
  243. result.reserved = r
  244. proc incrSeqV3(s: PGenericSeq, typ: PNimType): PGenericSeq {.compilerproc.} =
  245. if s == nil:
  246. result = cast[PGenericSeq](newSeq(typ, 1))
  247. result.len = 0
  248. else:
  249. result = s
  250. if result.len >= result.space:
  251. let r = resize(result.space)
  252. when defined(nimIncrSeqV3):
  253. result = cast[PGenericSeq](newSeq(typ, r))
  254. result.len = s.len
  255. copyMem(dataPointer(result, typ.base.align), dataPointer(s, typ.base.align), s.len * typ.base.size)
  256. # since we steal the content from 's', it's crucial to set s's len to 0.
  257. s.len = 0
  258. else:
  259. result = cast[PGenericSeq](growObj(result, align(GenericSeqSize, typ.base.align) + typ.base.size * r))
  260. result.reserved = r
  261. proc setLengthSeq(seq: PGenericSeq, elemSize, elemAlign, newLen: int): PGenericSeq {.
  262. compilerRtl, inl.} =
  263. result = seq
  264. if result.space < newLen:
  265. let r = max(resize(result.space), newLen)
  266. result = cast[PGenericSeq](growObj(result, align(GenericSeqSize, elemAlign) + elemSize * r))
  267. result.reserved = r
  268. elif newLen < result.len:
  269. # we need to decref here, otherwise the GC leaks!
  270. when not defined(boehmGC) and not defined(nogc) and
  271. not defined(gcMarkAndSweep) and not defined(gogc) and
  272. not defined(gcRegions):
  273. when false: # deadcode: was used by `compileOption("gc", "v2")`
  274. for i in newLen..result.len-1:
  275. let len0 = gch.tempStack.len
  276. forAllChildrenAux(dataPointer(result, elemAlign, elemSize, i),
  277. extGetCellType(result).base, waPush)
  278. let len1 = gch.tempStack.len
  279. for i in len0 ..< len1:
  280. doDecRef(gch.tempStack.d[i], LocalHeap, MaybeCyclic)
  281. gch.tempStack.len = len0
  282. else:
  283. if ntfNoRefs notin extGetCellType(result).base.flags:
  284. for i in newLen..result.len-1:
  285. forAllChildrenAux(dataPointer(result, elemAlign, elemSize, i),
  286. extGetCellType(result).base, waZctDecRef)
  287. # XXX: zeroing out the memory can still result in crashes if a wiped-out
  288. # cell is aliased by another pointer (ie proc parameter or a let variable).
  289. # This is a tough problem, because even if we don't zeroMem here, in the
  290. # presence of user defined destructors, the user will expect the cell to be
  291. # "destroyed" thus creating the same problem. We can destroy the cell in the
  292. # finalizer of the sequence, but this makes destruction non-deterministic.
  293. zeroMem(dataPointer(result, elemAlign, elemSize, newLen), (result.len-%newLen) *% elemSize)
  294. result.len = newLen
  295. proc setLengthSeqV2(s: PGenericSeq, typ: PNimType, newLen: int): PGenericSeq {.
  296. compilerRtl.} =
  297. sysAssert typ.kind == tySequence, "setLengthSeqV2: type is not a seq"
  298. if s == nil:
  299. result = cast[PGenericSeq](newSeq(typ, newLen))
  300. else:
  301. when defined(nimIncrSeqV3):
  302. let elemSize = typ.base.size
  303. let elemAlign = typ.base.align
  304. if s.space < newLen:
  305. let r = max(resize(s.space), newLen)
  306. result = cast[PGenericSeq](newSeq(typ, r))
  307. copyMem(dataPointer(result, elemAlign), dataPointer(s, elemAlign), s.len * elemSize)
  308. # since we steal the content from 's', it's crucial to set s's len to 0.
  309. s.len = 0
  310. elif newLen < s.len:
  311. result = s
  312. # we need to decref here, otherwise the GC leaks!
  313. when not defined(boehmGC) and not defined(nogc) and
  314. not defined(gcMarkAndSweep) and not defined(gogc) and
  315. not defined(gcRegions):
  316. if ntfNoRefs notin typ.base.flags:
  317. for i in newLen..result.len-1:
  318. forAllChildrenAux(dataPointer(result, elemAlign, elemSize, i),
  319. extGetCellType(result).base, waZctDecRef)
  320. # XXX: zeroing out the memory can still result in crashes if a wiped-out
  321. # cell is aliased by another pointer (ie proc parameter or a let variable).
  322. # This is a tough problem, because even if we don't zeroMem here, in the
  323. # presence of user defined destructors, the user will expect the cell to be
  324. # "destroyed" thus creating the same problem. We can destroy the cell in the
  325. # finalizer of the sequence, but this makes destruction non-deterministic.
  326. zeroMem(dataPointer(result, elemAlign, elemSize, newLen), (result.len-%newLen) *% elemSize)
  327. else:
  328. result = s
  329. zeroMem(dataPointer(result, elemAlign, elemSize, result.len), (newLen-%result.len) *% elemSize)
  330. result.len = newLen
  331. else:
  332. result = setLengthSeq(s, typ.base.size, newLen)