ccgreset.nim 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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.snippet == "": fillObjectFields(p.module, typ)
  24. if disc.loc.t == nil:
  25. internalError(p.config, n.info, "specializeResetN()")
  26. let discField = dotField(accessor, disc.loc.snippet)
  27. p.s(cpsStmts).addSwitchStmt(discField):
  28. for i in 1..<n.len:
  29. let branch = n[i]
  30. assert branch.kind in {nkOfBranch, nkElse}
  31. var caseBuilder: SwitchCaseBuilder
  32. p.s(cpsStmts).addSwitchCase(caseBuilder):
  33. if branch.kind == nkOfBranch:
  34. genCaseRange(p, branch, caseBuilder)
  35. else:
  36. p.s(cpsStmts).addCaseElse(caseBuilder)
  37. do:
  38. specializeResetN(p, accessor, lastSon(branch), typ)
  39. p.s(cpsStmts).addBreak()
  40. specializeResetT(p, discField, disc.loc.t)
  41. of nkSym:
  42. let field = n.sym
  43. if field.typ.kind == tyVoid: return
  44. if field.loc.snippet == "": fillObjectFields(p.module, typ)
  45. if field.loc.t == nil:
  46. internalError(p.config, n.info, "specializeResetN()")
  47. specializeResetT(p, dotField(accessor, field.loc.snippet), field.loc.t)
  48. else: internalError(p.config, n.info, "specializeResetN()")
  49. proc specializeResetT(p: BProc, accessor: Rope, typ: PType) =
  50. if typ == nil: return
  51. case typ.kind
  52. of tyGenericInst, tyGenericBody, tyTypeDesc, tyAlias, tyDistinct, tyInferred,
  53. tySink, tyOwned:
  54. specializeResetT(p, accessor, skipModifier(typ))
  55. of tyArray:
  56. let arraySize = lengthOrd(p.config, typ.indexType)
  57. var i: TLoc = getTemp(p, getSysType(p.module.g.graph, unknownLineInfo, tyInt))
  58. p.s(cpsStmts).addForRangeExclusive(i.snippet, cIntValue(0), cIntValue(arraySize)):
  59. specializeResetT(p, subscript(accessor, i.snippet), typ.elementType)
  60. of tyObject:
  61. var x = typ.baseClass
  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, a in typ.ikids:
  68. specializeResetT(p, dotField(accessor, "Field" & $i), a)
  69. of tyString, tyRef, tySequence:
  70. p.s(cpsStmts).addCallStmt(cgsymValue(p.module, "unsureAsgnRef"),
  71. cCast(ptrType(CPointer), cAddr(accessor)),
  72. NimNil)
  73. of tyProc:
  74. if typ.callConv == ccClosure:
  75. p.s(cpsStmts).addCallStmt(cgsymValue(p.module, "unsureAsgnRef"),
  76. cCast(ptrType(CPointer), cAddr(dotField(accessor, "ClE_0"))),
  77. NimNil)
  78. p.s(cpsStmts).addFieldAssignment(accessor, "ClP_0", NimNil)
  79. else:
  80. p.s(cpsStmts).addAssignment(accessor, NimNil)
  81. of tyChar, tyBool, tyEnum, tyRange, tyInt..tyUInt64:
  82. p.s(cpsStmts).addAssignment(accessor, cIntValue(0))
  83. of tyCstring, tyPointer, tyPtr, tyVar, tyLent:
  84. p.s(cpsStmts).addAssignment(accessor, NimNil)
  85. of tySet:
  86. case mapSetType(p.config, typ)
  87. of ctArray:
  88. let t = getTypeDesc(p.module, typ)
  89. p.s(cpsStmts).addCallStmt(cgsymValue(p.module, "nimZeroMem"),
  90. accessor,
  91. cSizeof(t))
  92. of ctInt8, ctInt16, ctInt32, ctInt64:
  93. p.s(cpsStmts).addAssignment(accessor, cIntValue(0))
  94. else:
  95. raiseAssert "unexpected set type kind"
  96. of tyNone, tyEmpty, tyNil, tyUntyped, tyTyped, tyGenericInvocation,
  97. tyGenericParam, tyOrdinal, tyOpenArray, tyForward, tyVarargs,
  98. tyUncheckedArray, tyError, tyBuiltInTypeClass, tyUserTypeClass,
  99. tyUserTypeClassInst, tyCompositeTypeClass, tyAnd, tyOr, tyNot,
  100. tyAnything, tyStatic, tyFromExpr, tyConcept, tyVoid, tyIterable:
  101. discard
  102. proc specializeReset(p: BProc, a: TLoc) =
  103. specializeResetT(p, rdLoc(a), a.t)