ccgtrav.nim 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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, needsInit=false): TLoc
  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.snippet == "": 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.snippet])
  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.snippet == "": 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.snippet], 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, skipModifier(typ))
  66. of tyArray:
  67. let arraySize = lengthOrd(c.p.config, typ.indexType)
  68. var i: TLoc = getTemp(p, getSysType(c.p.module.g.graph, unknownLineInfo, tyInt))
  69. var oldCode = p.s(cpsStmts)
  70. freeze oldCode
  71. linefmt(p, cpsStmts, "for ($1 = 0; $1 < $2; $1++) {$n",
  72. [i.snippet, arraySize])
  73. let oldLen = p.s(cpsStmts).len
  74. genTraverseProc(c, ropecg(c.p.module, "$1[$2]", [accessor, i.snippet]), typ.elementType)
  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. var x = typ.baseClass
  82. if x != nil: x = x.skipTypes(skipPtrs)
  83. genTraverseProc(c, accessor.parentObj(c.p.module), x)
  84. if typ.n != nil: genTraverseProc(c, accessor, typ.n, typ)
  85. of tyTuple:
  86. let typ = getUniqueType(typ)
  87. for i, a in typ.ikids:
  88. genTraverseProc(c, ropecg(c.p.module, "$1.Field$2", [accessor, i]), a)
  89. of tyRef:
  90. lineCg(p, cpsStmts, visitorFrmt, [accessor, c.visitorFrmt])
  91. of tySequence:
  92. if optSeqDestructors notin c.p.module.config.globalOptions:
  93. lineCg(p, cpsStmts, visitorFrmt, [accessor, c.visitorFrmt])
  94. elif containsGarbageCollectedRef(typ.elementType):
  95. # destructor based seqs are themselves not traced but their data is, if
  96. # they contain a GC'ed type:
  97. lineCg(p, cpsStmts, "#nimGCvisitSeq((void*)$1, $2);$n", [accessor, c.visitorFrmt])
  98. #genTraverseProcSeq(c, accessor, typ)
  99. of tyString:
  100. if tfHasAsgn notin typ.flags:
  101. lineCg(p, cpsStmts, visitorFrmt, [accessor, c.visitorFrmt])
  102. of tyProc:
  103. if typ.callConv == ccClosure:
  104. lineCg(p, cpsStmts, visitorFrmt, [ropecg(c.p.module, "$1.ClE_0", [accessor]), c.visitorFrmt])
  105. else:
  106. discard
  107. proc genTraverseProcSeq(c: TTraversalClosure, accessor: Rope, typ: PType) =
  108. var p = c.p
  109. assert typ.kind == tySequence
  110. var i = getTemp(p, getSysType(c.p.module.g.graph, unknownLineInfo, tyInt))
  111. var oldCode = p.s(cpsStmts)
  112. freeze oldCode
  113. var a = TLoc(snippet: accessor)
  114. lineF(p, cpsStmts, "for ($1 = 0; $1 < $2; $1++) {$n",
  115. [i.snippet, lenExpr(c.p, a)])
  116. let oldLen = p.s(cpsStmts).len
  117. genTraverseProc(c, "$1$3[$2]" % [accessor, i.snippet, dataField(c.p)], typ.elementType)
  118. if p.s(cpsStmts).len == oldLen:
  119. # do not emit dummy long loops for faster debug builds:
  120. p.s(cpsStmts) = oldCode
  121. else:
  122. lineF(p, cpsStmts, "}$n", [])
  123. proc genTraverseProc(m: BModule, origTyp: PType; sig: SigHash): Rope =
  124. var p = newProc(nil, m)
  125. result = "Marker_" & getTypeName(m, origTyp, sig)
  126. let
  127. hcrOn = m.hcrOn
  128. typ = origTyp.skipTypes(abstractInstOwned)
  129. markerName = if hcrOn: result & "_actual" else: result
  130. header = "static N_NIMCALL(void, $1)(void* p, NI op)" % [markerName]
  131. t = getTypeDesc(m, typ)
  132. lineF(p, cpsLocals, "$1 a;$n", [t])
  133. lineF(p, cpsInit, "a = ($1)p;$n", [t])
  134. var c = TTraversalClosure(p: p,
  135. visitorFrmt: "op" # "#nimGCvisit((void*)$1, op);$n"
  136. )
  137. assert typ.kind != tyTypeDesc
  138. if typ.kind == tySequence:
  139. genTraverseProcSeq(c, "a".rope, typ)
  140. else:
  141. if skipTypes(typ.elementType, typedescInst+{tyOwned}).kind == tyArray:
  142. # C's arrays are broken beyond repair:
  143. genTraverseProc(c, "a".rope, typ.elementType)
  144. else:
  145. genTraverseProc(c, "(*a)".rope, typ.elementType)
  146. let generatedProc = "$1 {$n$2$3$4}\n" %
  147. [header, p.s(cpsLocals), p.s(cpsInit), p.s(cpsStmts)]
  148. m.s[cfsProcHeaders].addf("$1;\n", [header])
  149. m.s[cfsProcs].add(generatedProc)
  150. if hcrOn:
  151. m.s[cfsProcHeaders].addf("N_NIMCALL_PTR(void, $1)(void*, NI);\n", [result])
  152. m.s[cfsDynLibInit].addf("\t$1 = (N_NIMCALL_PTR(void, )(void*, NI)) hcrRegisterProc($3, \"$1\", (void*)$2);\n",
  153. [result, markerName, getModuleDllPath(m)])
  154. proc genTraverseProcForGlobal(m: BModule, s: PSym; info: TLineInfo): Rope =
  155. discard genTypeInfoV1(m, s.loc.t, info)
  156. var p = newProc(nil, m)
  157. var sLoc = rdLoc(s.loc)
  158. result = getTempName(m)
  159. if sfThread in s.flags and emulatedThreadVars(m.config):
  160. accessThreadLocalVar(p, s)
  161. sLoc = "NimTV_->" & sLoc
  162. var c = TTraversalClosure(p: p,
  163. visitorFrmt: "0" # "#nimGCvisit((void*)$1, 0);$n"
  164. )
  165. let header = "static N_NIMCALL(void, $1)(void)" % [result]
  166. genTraverseProc(c, sLoc, s.loc.t)
  167. let generatedProc = "$1 {$n$2$3$4}$n" %
  168. [header, p.s(cpsLocals), p.s(cpsInit), p.s(cpsStmts)]
  169. m.s[cfsProcHeaders].addf("$1;$n", [header])
  170. m.s[cfsProcs].add(generatedProc)