ccgreset.nim 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #
  2. #
  3. # The Nim Compiler
  4. # (c) Copyright 2020 Andreas Rumpf
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. # included from cgen.nim
  10. ## Code specialization instead of the old, incredibly slow 'genericReset'
  11. ## implementation.
  12. proc specializeResetT(p: BProc, accessor: Rope, typ: PType)
  13. proc specializeResetN(p: BProc, accessor: Rope, n: PNode;
  14. typ: PType) =
  15. if n == nil: return
  16. case n.kind
  17. of nkRecList:
  18. for i in 0..<n.len:
  19. specializeResetN(p, accessor, n[i], typ)
  20. of nkRecCase:
  21. if (n[0].kind != nkSym): internalError(p.config, n.info, "specializeResetN")
  22. let disc = n[0].sym
  23. if disc.loc.r == "": fillObjectFields(p.module, typ)
  24. if disc.loc.t == nil:
  25. internalError(p.config, n.info, "specializeResetN()")
  26. lineF(p, cpsStmts, "switch ($1.$2) {$n", [accessor, disc.loc.r])
  27. for i in 1..<n.len:
  28. let branch = n[i]
  29. assert branch.kind in {nkOfBranch, nkElse}
  30. if branch.kind == nkOfBranch:
  31. genCaseRange(p, branch)
  32. else:
  33. lineF(p, cpsStmts, "default:$n", [])
  34. specializeResetN(p, accessor, lastSon(branch), typ)
  35. lineF(p, cpsStmts, "break;$n", [])
  36. lineF(p, cpsStmts, "} $n", [])
  37. specializeResetT(p, "$1.$2" % [accessor, disc.loc.r], disc.loc.t)
  38. of nkSym:
  39. let field = n.sym
  40. if field.typ.kind == tyVoid: return
  41. if field.loc.r == "": fillObjectFields(p.module, typ)
  42. if field.loc.t == nil:
  43. internalError(p.config, n.info, "specializeResetN()")
  44. specializeResetT(p, "$1.$2" % [accessor, field.loc.r], field.loc.t)
  45. else: internalError(p.config, n.info, "specializeResetN()")
  46. proc specializeResetT(p: BProc, accessor: Rope, typ: PType) =
  47. if typ == nil: return
  48. case typ.kind
  49. of tyGenericInst, tyGenericBody, tyTypeDesc, tyAlias, tyDistinct, tyInferred,
  50. tySink, tyOwned:
  51. specializeResetT(p, accessor, skipModifier(typ))
  52. of tyArray:
  53. let arraySize = lengthOrd(p.config, typ.indexType)
  54. var i: TLoc = getTemp(p, getSysType(p.module.g.graph, unknownLineInfo, tyInt))
  55. linefmt(p, cpsStmts, "for ($1 = 0; $1 < $2; $1++) {$n",
  56. [i.r, arraySize])
  57. specializeResetT(p, ropecg(p.module, "$1[$2]", [accessor, i.r]), typ.elementType)
  58. lineF(p, cpsStmts, "}$n", [])
  59. of tyObject:
  60. var x = typ.baseClass
  61. if x != nil: x = x.skipTypes(skipPtrs)
  62. specializeResetT(p, accessor.parentObj(p.module), x)
  63. if typ.n != nil: specializeResetN(p, accessor, typ.n, typ)
  64. of tyTuple:
  65. let typ = getUniqueType(typ)
  66. for i, a in typ.ikids:
  67. specializeResetT(p, ropecg(p.module, "$1.Field$2", [accessor, i]), a)
  68. of tyString, tyRef, tySequence:
  69. lineCg(p, cpsStmts, "#unsureAsgnRef((void**)&$1, NIM_NIL);$n", [accessor])
  70. of tyProc:
  71. if typ.callConv == ccClosure:
  72. lineCg(p, cpsStmts, "#unsureAsgnRef((void**)&$1.ClE_0, NIM_NIL);$n", [accessor])
  73. lineCg(p, cpsStmts, "$1.ClP_0 = NIM_NIL;$n", [accessor])
  74. else:
  75. lineCg(p, cpsStmts, "$1 = NIM_NIL;$n", [accessor])
  76. of tyChar, tyBool, tyEnum, tyRange, tyInt..tyUInt64:
  77. lineCg(p, cpsStmts, "$1 = 0;$n", [accessor])
  78. of tyCstring, tyPointer, tyPtr, tyVar, tyLent:
  79. lineCg(p, cpsStmts, "$1 = NIM_NIL;$n", [accessor])
  80. of tySet:
  81. case mapSetType(p.config, typ)
  82. of ctArray:
  83. lineCg(p, cpsStmts, "#nimZeroMem($1, sizeof($2));$n",
  84. [accessor, getTypeDesc(p.module, typ)])
  85. of ctInt8, ctInt16, ctInt32, ctInt64:
  86. lineCg(p, cpsStmts, "$1 = 0;$n", [accessor])
  87. else:
  88. raiseAssert "unexpected set type kind"
  89. of tyNone, tyEmpty, tyNil, tyUntyped, tyTyped, tyGenericInvocation,
  90. tyGenericParam, tyOrdinal, tyOpenArray, tyForward, tyVarargs,
  91. tyUncheckedArray, tyProxy, tyBuiltInTypeClass, tyUserTypeClass,
  92. tyUserTypeClassInst, tyCompositeTypeClass, tyAnd, tyOr, tyNot,
  93. tyAnything, tyStatic, tyFromExpr, tyConcept, tyVoid, tyIterable:
  94. discard
  95. proc specializeReset(p: BProc, a: TLoc) =
  96. specializeResetT(p, rdLoc(a), a.t)