ccgtrav.nim 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. #
  2. #
  3. # The Nim Compiler
  4. # (c) Copyright 2013 Andreas Rumpf
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. ## Generates traversal procs for the C backend.
  10. # included from cgen.nim
  11. type
  12. TTraversalClosure = object
  13. p: BProc
  14. visitorFrmt: string
  15. const
  16. visitorFrmt = "#nimGCvisit((void*)$1, $2);$n"
  17. proc genTraverseProc(c: TTraversalClosure, accessor: Rope, typ: PType)
  18. proc genCaseRange(p: BProc, branch: PNode)
  19. proc getTemp(p: BProc, t: PType, result: var TLoc; needsInit=false)
  20. proc genTraverseProc(c: TTraversalClosure, accessor: Rope, n: PNode;
  21. typ: PType) =
  22. if n == nil: return
  23. case n.kind
  24. of nkRecList:
  25. for i in 0..<n.len:
  26. genTraverseProc(c, accessor, n[i], typ)
  27. of nkRecCase:
  28. if (n[0].kind != nkSym): internalError(c.p.config, n.info, "genTraverseProc")
  29. var p = c.p
  30. let disc = n[0].sym
  31. if disc.loc.r == nil: fillObjectFields(c.p.module, typ)
  32. if disc.loc.t == nil:
  33. internalError(c.p.config, n.info, "genTraverseProc()")
  34. lineF(p, cpsStmts, "switch ($1.$2) {$n", [accessor, disc.loc.r])
  35. for i in 1..<n.len:
  36. let branch = n[i]
  37. assert branch.kind in {nkOfBranch, nkElse}
  38. if branch.kind == nkOfBranch:
  39. genCaseRange(c.p, branch)
  40. else:
  41. lineF(p, cpsStmts, "default:$n", [])
  42. genTraverseProc(c, accessor, lastSon(branch), typ)
  43. lineF(p, cpsStmts, "break;$n", [])
  44. lineF(p, cpsStmts, "} $n", [])
  45. of nkSym:
  46. let field = n.sym
  47. if field.typ.kind == tyVoid: return
  48. if field.loc.r == nil: fillObjectFields(c.p.module, typ)
  49. if field.loc.t == nil:
  50. internalError(c.p.config, n.info, "genTraverseProc()")
  51. genTraverseProc(c, "$1.$2" % [accessor, field.loc.r], field.loc.t)
  52. else: internalError(c.p.config, n.info, "genTraverseProc()")
  53. proc parentObj(accessor: Rope; m: BModule): Rope {.inline.} =
  54. if not m.compileToCpp:
  55. result = "$1.Sup" % [accessor]
  56. else:
  57. result = accessor
  58. proc genTraverseProcSeq(c: TTraversalClosure, accessor: Rope, typ: PType)
  59. proc genTraverseProc(c: TTraversalClosure, accessor: Rope, typ: PType) =
  60. if typ == nil: return
  61. var p = c.p
  62. case typ.kind
  63. of tyGenericInst, tyGenericBody, tyTypeDesc, tyAlias, tyDistinct, tyInferred,
  64. tySink, tyOwned:
  65. genTraverseProc(c, accessor, lastSon(typ))
  66. of tyArray:
  67. let arraySize = lengthOrd(c.p.config, typ[0])
  68. var i: TLoc
  69. getTemp(p, getSysType(c.p.module.g.graph, unknownLineInfo, tyInt), i)
  70. let oldCode = p.s(cpsStmts)
  71. linefmt(p, cpsStmts, "for ($1 = 0; $1 < $2; $1++) {$n",
  72. [i.r, arraySize])
  73. let oldLen = p.s(cpsStmts).len
  74. genTraverseProc(c, ropecg(c.p.module, "$1[$2]", [accessor, i.r]), typ[1])
  75. if p.s(cpsStmts).len == oldLen:
  76. # do not emit dummy long loops for faster debug builds:
  77. p.s(cpsStmts) = oldCode
  78. else:
  79. lineF(p, cpsStmts, "}$n", [])
  80. of tyObject:
  81. for i in 0..<typ.len:
  82. var x = typ[i]
  83. if x != nil: x = x.skipTypes(skipPtrs)
  84. genTraverseProc(c, accessor.parentObj(c.p.module), x)
  85. if typ.n != nil: genTraverseProc(c, accessor, typ.n, typ)
  86. of tyTuple:
  87. let typ = getUniqueType(typ)
  88. for i in 0..<typ.len:
  89. genTraverseProc(c, ropecg(c.p.module, "$1.Field$2", [accessor, i]), typ[i])
  90. of tyRef:
  91. lineCg(p, cpsStmts, visitorFrmt, [accessor, c.visitorFrmt])
  92. of tySequence:
  93. if optSeqDestructors notin c.p.module.config.globalOptions:
  94. lineCg(p, cpsStmts, visitorFrmt, [accessor, c.visitorFrmt])
  95. elif containsGarbageCollectedRef(typ.lastSon):
  96. # destructor based seqs are themselves not traced but their data is, if
  97. # they contain a GC'ed type:
  98. lineCg(p, cpsStmts, "#nimGCvisitSeq((void*)$1, $2);$n", [accessor, c.visitorFrmt])
  99. #genTraverseProcSeq(c, accessor, typ)
  100. of tyString:
  101. if tfHasAsgn notin typ.flags:
  102. lineCg(p, cpsStmts, visitorFrmt, [accessor, c.visitorFrmt])
  103. of tyProc:
  104. if typ.callConv == ccClosure:
  105. lineCg(p, cpsStmts, visitorFrmt, [ropecg(c.p.module, "$1.ClE_0", [accessor]), c.visitorFrmt])
  106. else:
  107. discard
  108. proc genTraverseProcSeq(c: TTraversalClosure, accessor: Rope, typ: PType) =
  109. var p = c.p
  110. assert typ.kind == tySequence
  111. var i: TLoc
  112. getTemp(p, getSysType(c.p.module.g.graph, unknownLineInfo, tyInt), i)
  113. let oldCode = p.s(cpsStmts)
  114. var a: TLoc
  115. a.r = accessor
  116. lineF(p, cpsStmts, "for ($1 = 0; $1 < $2; $1++) {$n",
  117. [i.r, lenExpr(c.p, a)])
  118. let oldLen = p.s(cpsStmts).len
  119. genTraverseProc(c, "$1$3[$2]" % [accessor, i.r, dataField(c.p)], typ[0])
  120. if p.s(cpsStmts).len == oldLen:
  121. # do not emit dummy long loops for faster debug builds:
  122. p.s(cpsStmts) = oldCode
  123. else:
  124. lineF(p, cpsStmts, "}$n", [])
  125. proc genTraverseProc(m: BModule, origTyp: PType; sig: SigHash): Rope =
  126. var c: TTraversalClosure
  127. var p = newProc(nil, m)
  128. result = "Marker_" & getTypeName(m, origTyp, sig)
  129. let
  130. hcrOn = m.hcrOn
  131. typ = origTyp.skipTypes(abstractInstOwned)
  132. markerName = if hcrOn: result & "_actual" else: result
  133. header = "static N_NIMCALL(void, $1)(void* p, NI op)" % [markerName]
  134. t = getTypeDesc(m, typ)
  135. lineF(p, cpsLocals, "$1 a;$n", [t])
  136. lineF(p, cpsInit, "a = ($1)p;$n", [t])
  137. c.p = p
  138. c.visitorFrmt = "op" # "#nimGCvisit((void*)$1, op);$n"
  139. assert typ.kind != tyTypeDesc
  140. if typ.kind == tySequence:
  141. genTraverseProcSeq(c, "a".rope, typ)
  142. else:
  143. if skipTypes(typ[0], typedescInst+{tyOwned}).kind == tyArray:
  144. # C's arrays are broken beyond repair:
  145. genTraverseProc(c, "a".rope, typ[0])
  146. else:
  147. genTraverseProc(c, "(*a)".rope, typ[0])
  148. let generatedProc = "$1 {$n$2$3$4}\n" %
  149. [header, p.s(cpsLocals), p.s(cpsInit), p.s(cpsStmts)]
  150. m.s[cfsProcHeaders].addf("$1;\n", [header])
  151. m.s[cfsProcs].add(generatedProc)
  152. if hcrOn:
  153. m.s[cfsProcHeaders].addf("N_NIMCALL_PTR(void, $1)(void*, NI);\n", [result])
  154. m.s[cfsDynLibInit].addf("\t$1 = (N_NIMCALL_PTR(void, )(void*, NI)) hcrRegisterProc($3, \"$1\", (void*)$2);\n",
  155. [result, markerName, getModuleDllPath(m)])
  156. proc genTraverseProcForGlobal(m: BModule, s: PSym; info: TLineInfo): Rope =
  157. discard genTypeInfoV1(m, s.loc.t, info)
  158. var c: TTraversalClosure
  159. var p = newProc(nil, m)
  160. var sLoc = rdLoc(s.loc)
  161. result = getTempName(m)
  162. if sfThread in s.flags and emulatedThreadVars(m.config):
  163. accessThreadLocalVar(p, s)
  164. sLoc = "NimTV_->" & sLoc
  165. c.visitorFrmt = "0" # "#nimGCvisit((void*)$1, 0);$n"
  166. c.p = p
  167. let header = "static N_NIMCALL(void, $1)(void)" % [result]
  168. genTraverseProc(c, sLoc, s.loc.t)
  169. let generatedProc = "$1 {$n$2$3$4}$n" %
  170. [header, p.s(cpsLocals), p.s(cpsInit), p.s(cpsStmts)]
  171. m.s[cfsProcHeaders].addf("$1;$n", [header])
  172. m.s[cfsProcs].add(generatedProc)