sysstr.nim 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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, 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. when declared(newObjRC1) and not defined(gcRegions):
  101. var s = src.len
  102. if s < 7: s = 7
  103. result = cast[NimString](newObjRC1(addr(strDesc), sizeof(TGenericSeq) +
  104. s+1))
  105. result.reserved = s
  106. when defined(gogc):
  107. result.elemSize = 1
  108. else:
  109. result = rawNewStringNoInit(src.len)
  110. result.len = src.len
  111. copyMem(addr(result.data), addr(src.data), src.len + 1)
  112. sysAssert((seqShallowFlag and result.reserved) == 0, "copyStringRC1")
  113. when defined(nimShallowStrings):
  114. if (src.reserved and strlitFlag) != 0:
  115. result.reserved = (result.reserved and not strlitFlag) or seqShallowFlag
  116. proc copyDeepString(src: NimString): NimString {.inline.} =
  117. if src != nil:
  118. result = rawNewStringNoInit(src.len)
  119. result.len = src.len
  120. copyMem(addr(result.data), addr(src.data), src.len + 1)
  121. proc addChar(s: NimString, c: char): NimString =
  122. # is compilerproc!
  123. if s == nil:
  124. result = rawNewStringNoInit(1)
  125. result.len = 0
  126. else:
  127. result = s
  128. if result.len >= result.space:
  129. let r = resize(result.space)
  130. when defined(nimIncrSeqV3):
  131. result = rawNewStringNoInit(r)
  132. result.len = s.len
  133. copyMem(addr result.data[0], unsafeAddr(s.data[0]), s.len+1)
  134. else:
  135. result = cast[NimString](growObj(result,
  136. sizeof(TGenericSeq) + r + 1))
  137. result.reserved = r
  138. result.data[result.len] = c
  139. result.data[result.len+1] = '\0'
  140. inc(result.len)
  141. # These routines should be used like following:
  142. # <Nim code>
  143. # s &= "Hello " & name & ", how do you feel?"
  144. #
  145. # <generated C code>
  146. # {
  147. # s = resizeString(s, 6 + name->len + 17);
  148. # appendString(s, strLit1);
  149. # appendString(s, strLit2);
  150. # appendString(s, strLit3);
  151. # }
  152. #
  153. # <Nim code>
  154. # s = "Hello " & name & ", how do you feel?"
  155. #
  156. # <generated C code>
  157. # {
  158. # string tmp0;
  159. # tmp0 = rawNewString(6 + name->len + 17);
  160. # appendString(s, strLit1);
  161. # appendString(s, strLit2);
  162. # appendString(s, strLit3);
  163. # s = tmp0;
  164. # }
  165. #
  166. # <Nim code>
  167. # s = ""
  168. #
  169. # <generated C code>
  170. # s = rawNewString(0);
  171. proc resizeString(dest: NimString, addlen: int): NimString {.compilerRtl.} =
  172. if dest == nil:
  173. result = rawNewStringNoInit(addlen)
  174. elif dest.len + addlen <= dest.space:
  175. result = dest
  176. else: # slow path:
  177. let sp = max(resize(dest.space), dest.len + addlen)
  178. when defined(nimIncrSeqV3):
  179. result = rawNewStringNoInit(sp)
  180. result.len = dest.len
  181. copyMem(addr result.data[0], unsafeAddr(dest.data[0]), dest.len+1)
  182. else:
  183. result = cast[NimString](growObj(dest, sizeof(TGenericSeq) + sp + 1))
  184. result.reserved = sp
  185. #result = rawNewString(sp)
  186. #copyMem(result, dest, dest.len + sizeof(TGenericSeq))
  187. # DO NOT UPDATE LEN YET: dest.len = newLen
  188. proc appendString(dest, src: NimString) {.compilerproc, inline.} =
  189. if src != nil:
  190. copyMem(addr(dest.data[dest.len]), addr(src.data), src.len + 1)
  191. inc(dest.len, src.len)
  192. proc appendChar(dest: NimString, c: char) {.compilerproc, inline.} =
  193. dest.data[dest.len] = c
  194. dest.data[dest.len+1] = '\0'
  195. inc(dest.len)
  196. proc setLengthStr(s: NimString, newLen: int): NimString {.compilerRtl.} =
  197. let n = max(newLen, 0)
  198. if s == nil:
  199. result = mnewString(newLen)
  200. elif n <= s.space:
  201. result = s
  202. else:
  203. let sp = max(resize(s.space), newLen)
  204. when defined(nimIncrSeqV3):
  205. result = rawNewStringNoInit(sp)
  206. result.len = s.len
  207. copyMem(addr result.data[0], unsafeAddr(s.data[0]), s.len+1)
  208. zeroMem(addr result.data[s.len], newLen - s.len)
  209. result.reserved = sp
  210. else:
  211. result = resizeString(s, n)
  212. result.len = n
  213. result.data[n] = '\0'
  214. # ----------------- sequences ----------------------------------------------
  215. proc incrSeq(seq: PGenericSeq, elemSize: int): PGenericSeq {.compilerProc.} =
  216. # increments the length by one:
  217. # this is needed for supporting ``add``;
  218. #
  219. # add(seq, x) generates:
  220. # seq = incrSeq(seq, sizeof(x));
  221. # seq[seq->len-1] = x;
  222. result = seq
  223. if result.len >= result.space:
  224. let r = resize(result.space)
  225. result = cast[PGenericSeq](growObj(result, elemSize * r +
  226. GenericSeqSize))
  227. result.reserved = r
  228. inc(result.len)
  229. proc incrSeqV2(seq: PGenericSeq, elemSize: int): PGenericSeq {.compilerProc.} =
  230. # incrSeq version 2
  231. result = seq
  232. if result.len >= result.space:
  233. let r = resize(result.space)
  234. result = cast[PGenericSeq](growObj(result, elemSize * r +
  235. GenericSeqSize))
  236. result.reserved = r
  237. template `+!`(p: pointer, s: int): pointer =
  238. cast[pointer](cast[int](p) +% s)
  239. proc incrSeqV3(s: PGenericSeq, typ: PNimType): PGenericSeq {.compilerProc.} =
  240. if s == nil:
  241. result = cast[PGenericSeq](newSeq(typ, 1))
  242. result.len = 0
  243. else:
  244. result = s
  245. if result.len >= result.space:
  246. let r = resize(result.space)
  247. when defined(nimIncrSeqV3):
  248. result = cast[PGenericSeq](newSeq(typ, r))
  249. result.len = s.len
  250. copyMem(result +! GenericSeqSize, s +! GenericSeqSize, s.len * typ.base.size)
  251. # since we steal the content from 's', it's crucial to set s's len to 0.
  252. s.len = 0
  253. else:
  254. result = cast[PGenericSeq](growObj(result, typ.base.size * r +
  255. GenericSeqSize))
  256. result.reserved = r
  257. proc setLengthSeq(seq: PGenericSeq, elemSize, newLen: int): PGenericSeq {.
  258. compilerRtl, inl.} =
  259. result = seq
  260. if result.space < newLen:
  261. let r = max(resize(result.space), newLen)
  262. result = cast[PGenericSeq](growObj(result, elemSize * r +
  263. GenericSeqSize))
  264. result.reserved = r
  265. elif newLen < result.len:
  266. # we need to decref here, otherwise the GC leaks!
  267. when not defined(boehmGC) and not defined(nogc) and
  268. not defined(gcMarkAndSweep) and not defined(gogc) and
  269. not defined(gcRegions):
  270. when false: # compileOption("gc", "v2"):
  271. for i in newLen..result.len-1:
  272. let len0 = gch.tempStack.len
  273. forAllChildrenAux(cast[pointer](cast[ByteAddress](result) +%
  274. GenericSeqSize +% (i*%elemSize)),
  275. extGetCellType(result).base, waPush)
  276. let len1 = gch.tempStack.len
  277. for i in len0 ..< len1:
  278. doDecRef(gch.tempStack.d[i], LocalHeap, MaybeCyclic)
  279. gch.tempStack.len = len0
  280. else:
  281. if ntfNoRefs notin extGetCellType(result).base.flags:
  282. for i in newLen..result.len-1:
  283. forAllChildrenAux(cast[pointer](cast[ByteAddress](result) +%
  284. GenericSeqSize +% (i*%elemSize)),
  285. extGetCellType(result).base, waZctDecRef)
  286. # XXX: zeroing out the memory can still result in crashes if a wiped-out
  287. # cell is aliased by another pointer (ie proc parameter or a let variable).
  288. # This is a tough problem, because even if we don't zeroMem here, in the
  289. # presence of user defined destructors, the user will expect the cell to be
  290. # "destroyed" thus creating the same problem. We can destoy the cell in the
  291. # finalizer of the sequence, but this makes destruction non-deterministic.
  292. zeroMem(cast[pointer](cast[ByteAddress](result) +% GenericSeqSize +%
  293. (newLen*%elemSize)), (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. if s.space < newLen:
  304. let r = max(resize(s.space), newLen)
  305. result = cast[PGenericSeq](newSeq(typ, r))
  306. copyMem(result +! GenericSeqSize, s +! GenericSeqSize, s.len * elemSize)
  307. # since we steal the content from 's', it's crucial to set s's len to 0.
  308. s.len = 0
  309. elif newLen < s.len:
  310. result = s
  311. # we need to decref here, otherwise the GC leaks!
  312. when not defined(boehmGC) and not defined(nogc) and
  313. not defined(gcMarkAndSweep) and not defined(gogc) and
  314. not defined(gcRegions):
  315. if ntfNoRefs notin typ.base.flags:
  316. for i in newLen..result.len-1:
  317. forAllChildrenAux(cast[pointer](cast[ByteAddress](result) +%
  318. GenericSeqSize +% (i*%elemSize)),
  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 destoy the cell in the
  325. # finalizer of the sequence, but this makes destruction non-deterministic.
  326. zeroMem(cast[pointer](cast[ByteAddress](result) +% GenericSeqSize +%
  327. (newLen*%elemSize)), (result.len-%newLen) *% elemSize)
  328. else:
  329. result = s
  330. result.len = newLen
  331. else:
  332. result = setLengthSeq(s, typ.base.size, newLen)