ccgtrav.nim 6.6 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.r == "": 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 == "": 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 = 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.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 = getTemp(p, getSysType(c.p.module.g.graph, unknownLineInfo, tyInt))
  112. var oldCode = p.s(cpsStmts)
  113. freeze oldCode
  114. var a: TLoc = TLoc(r: accessor)
  115. lineF(p, cpsStmts, "for ($1 = 0; $1 < $2; $1++) {$n",
  116. [i.r, lenExpr(c.p, a)])
  117. let oldLen = p.s(cpsStmts).len
  118. genTraverseProc(c, "$1$3[$2]" % [accessor, i.r, dataField(c.p)], typ[0])
  119. if p.s(cpsStmts).len == oldLen:
  120. # do not emit dummy long loops for faster debug builds:
  121. p.s(cpsStmts) = oldCode
  122. else:
  123. lineF(p, cpsStmts, "}$n", [])
  124. proc genTraverseProc(m: BModule, origTyp: PType; sig: SigHash): Rope =
  125. var c: TTraversalClosure
  126. var p = newProc(nil, m)
  127. result = "Marker_" & getTypeName(m, origTyp, sig)
  128. let
  129. hcrOn = m.hcrOn
  130. typ = origTyp.skipTypes(abstractInstOwned)
  131. markerName = if hcrOn: result & "_actual" else: result
  132. header = "static N_NIMCALL(void, $1)(void* p, NI op)" % [markerName]
  133. t = getTypeDesc(m, typ)
  134. lineF(p, cpsLocals, "$1 a;$n", [t])
  135. lineF(p, cpsInit, "a = ($1)p;$n", [t])
  136. c.p = p
  137. c.visitorFrmt = "op" # "#nimGCvisit((void*)$1, op);$n"
  138. assert typ.kind != tyTypeDesc
  139. if typ.kind == tySequence:
  140. genTraverseProcSeq(c, "a".rope, typ)
  141. else:
  142. if skipTypes(typ[0], typedescInst+{tyOwned}).kind == tyArray:
  143. # C's arrays are broken beyond repair:
  144. genTraverseProc(c, "a".rope, typ[0])
  145. else:
  146. genTraverseProc(c, "(*a)".rope, typ[0])
  147. let generatedProc = "$1 {$n$2$3$4}\n" %
  148. [header, p.s(cpsLocals), p.s(cpsInit), p.s(cpsStmts)]
  149. m.s[cfsProcHeaders].addf("$1;\n", [header])
  150. m.s[cfsProcs].add(generatedProc)
  151. if hcrOn:
  152. m.s[cfsProcHeaders].addf("N_NIMCALL_PTR(void, $1)(void*, NI);\n", [result])
  153. m.s[cfsDynLibInit].addf("\t$1 = (N_NIMCALL_PTR(void, )(void*, NI)) hcrRegisterProc($3, \"$1\", (void*)$2);\n",
  154. [result, markerName, getModuleDllPath(m)])
  155. proc genTraverseProcForGlobal(m: BModule, s: PSym; info: TLineInfo): Rope =
  156. discard genTypeInfoV1(m, s.loc.t, info)
  157. var c: TTraversalClosure
  158. var p = newProc(nil, m)
  159. var sLoc = rdLoc(s.loc)
  160. result = getTempName(m)
  161. if sfThread in s.flags and emulatedThreadVars(m.config):
  162. accessThreadLocalVar(p, s)
  163. sLoc = "NimTV_->" & sLoc
  164. c.visitorFrmt = "0" # "#nimGCvisit((void*)$1, 0);$n"
  165. c.p = p
  166. let header = "static N_NIMCALL(void, $1)(void)" % [result]
  167. genTraverseProc(c, sLoc, s.loc.t)
  168. let generatedProc = "$1 {$n$2$3$4}$n" %
  169. [header, p.s(cpsLocals), p.s(cpsInit), p.s(cpsStmts)]
  170. m.s[cfsProcHeaders].addf("$1;$n", [header])
  171. m.s[cfsProcs].add(generatedProc)