ccgreset.nim 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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, lastSon(typ))
  52. of tyArray:
  53. let arraySize = lengthOrd(p.config, typ[0])
  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[1])
  58. lineF(p, cpsStmts, "}$n", [])
  59. of tyObject:
  60. for i in 0..<typ.len:
  61. var x = typ[i]
  62. if x != nil: x = x.skipTypes(skipPtrs)
  63. specializeResetT(p, accessor.parentObj(p.module), x)
  64. if typ.n != nil: specializeResetN(p, accessor, typ.n, typ)
  65. of tyTuple:
  66. let typ = getUniqueType(typ)
  67. for i in 0..<typ.len:
  68. specializeResetT(p, ropecg(p.module, "$1.Field$2", [accessor, i]), typ[i])
  69. of tyString, tyRef, tySequence:
  70. lineCg(p, cpsStmts, "#unsureAsgnRef((void**)&$1, NIM_NIL);$n", [accessor])
  71. of tyProc:
  72. if typ.callConv == ccClosure:
  73. lineCg(p, cpsStmts, "#unsureAsgnRef((void**)&$1.ClE_0, NIM_NIL);$n", [accessor])
  74. lineCg(p, cpsStmts, "$1.ClP_0 = NIM_NIL;$n", [accessor])
  75. else:
  76. lineCg(p, cpsStmts, "$1 = NIM_NIL;$n", [accessor])
  77. of tyChar, tyBool, tyEnum, tyInt..tyUInt64:
  78. lineCg(p, cpsStmts, "$1 = 0;$n", [accessor])
  79. of tyCstring, tyPointer, tyPtr, tyVar, tyLent:
  80. lineCg(p, cpsStmts, "$1 = NIM_NIL;$n", [accessor])
  81. of tySet:
  82. case mapSetType(p.config, typ)
  83. of ctArray:
  84. lineCg(p, cpsStmts, "#nimZeroMem($1, sizeof($2));$n",
  85. [accessor, getTypeDesc(p.module, typ)])
  86. of ctInt8, ctInt16, ctInt32, ctInt64:
  87. lineCg(p, cpsStmts, "$1 = 0;$n", [accessor])
  88. else:
  89. raiseAssert "unexpected set type kind"
  90. of tyNone, tyEmpty, tyNil, tyUntyped, tyTyped, tyGenericInvocation,
  91. tyGenericParam, tyOrdinal, tyRange, tyOpenArray, tyForward, tyVarargs,
  92. tyUncheckedArray, tyProxy, tyBuiltInTypeClass, tyUserTypeClass,
  93. tyUserTypeClassInst, tyCompositeTypeClass, tyAnd, tyOr, tyNot,
  94. tyAnything, tyStatic, tyFromExpr, tyConcept, tyVoid, tyIterable:
  95. discard
  96. proc specializeReset(p: BProc, a: TLoc) =
  97. specializeResetT(p, rdLoc(a), a.t)