sysstr.nim 13 KB

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