ccgliterals.nim 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #
  2. #
  3. # The Nim Compiler
  4. # (c) Copyright 2018 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. ## This include file contains the logic to produce constant string
  11. ## and seq literals. The code here is responsible that
  12. ## ``const x = ["a", "b"]`` works without hidden runtime creation code.
  13. ## The price is that seqs and strings are not purely a library
  14. ## implementation.
  15. template detectVersion(field, corename) =
  16. if m.g.field == 0:
  17. let core = getCompilerProc(m.g.graph, corename)
  18. if core == nil or core.kind != skConst:
  19. m.g.field = 1
  20. else:
  21. m.g.field = toInt(ast.getInt(core.ast))
  22. result = m.g.field
  23. proc detectStrVersion(m: BModule): int =
  24. detectVersion(strVersion, "nimStrVersion")
  25. proc detectSeqVersion(m: BModule): int =
  26. detectVersion(seqVersion, "nimSeqVersion")
  27. # ----- Version 1: GC'ed strings and seqs --------------------------------
  28. proc genStringLiteralDataOnlyV1(m: BModule, s: string): Rope =
  29. discard cgsym(m, "TGenericSeq")
  30. result = getTempName(m)
  31. m.s[cfsData].addf("STRING_LITERAL($1, $2, $3);$n",
  32. [result, makeCString(s), rope(s.len)])
  33. proc genStringLiteralV1(m: BModule; n: PNode): Rope =
  34. if s.isNil:
  35. result = ropecg(m, "((#NimStringDesc*) NIM_NIL)", [])
  36. else:
  37. let id = nodeTableTestOrSet(m.dataCache, n, m.labels)
  38. if id == m.labels:
  39. # string literal not found in the cache:
  40. result = ropecg(m, "((#NimStringDesc*) &$1)",
  41. [genStringLiteralDataOnlyV1(m, n.strVal)])
  42. else:
  43. result = ropecg(m, "((#NimStringDesc*) &$1$2)",
  44. [m.tmpBase, id])
  45. # ------ Version 2: destructor based strings and seqs -----------------------
  46. proc genStringLiteralDataOnlyV2(m: BModule, s: string; result: Rope; isConst: bool) =
  47. m.s[cfsData].addf("static $4 struct {$n" &
  48. " NI cap; NIM_CHAR data[$2+1];$n" &
  49. "} $1 = { $2 | NIM_STRLIT_FLAG, $3 };$n",
  50. [result, rope(s.len), makeCString(s),
  51. rope(if isConst: "const" else: "")])
  52. proc genStringLiteralV2(m: BModule; n: PNode; isConst: bool): Rope =
  53. let id = nodeTableTestOrSet(m.dataCache, n, m.labels)
  54. if id == m.labels:
  55. let pureLit = getTempName(m)
  56. genStringLiteralDataOnlyV2(m, n.strVal, pureLit, isConst)
  57. result = getTempName(m)
  58. discard cgsym(m, "NimStrPayload")
  59. discard cgsym(m, "NimStringV2")
  60. # string literal not found in the cache:
  61. m.s[cfsData].addf("static $4 NimStringV2 $1 = {$2, (NimStrPayload*)&$3};$n",
  62. [result, rope(n.strVal.len), pureLit, rope(if isConst: "const" else: "")])
  63. else:
  64. result = getTempName(m)
  65. m.s[cfsData].addf("static $4 NimStringV2 $1 = {$2, (NimStrPayload*)&$3};$n",
  66. [result, rope(n.strVal.len), m.tmpBase & rope(id),
  67. rope(if isConst: "const" else: "")])
  68. proc genStringLiteralV2Const(m: BModule; n: PNode; isConst: bool): Rope =
  69. let id = nodeTableTestOrSet(m.dataCache, n, m.labels)
  70. var pureLit: Rope
  71. if id == m.labels:
  72. pureLit = getTempName(m)
  73. discard cgsym(m, "NimStrPayload")
  74. discard cgsym(m, "NimStringV2")
  75. # string literal not found in the cache:
  76. genStringLiteralDataOnlyV2(m, n.strVal, pureLit, isConst)
  77. else:
  78. pureLit = m.tmpBase & rope(id)
  79. result = "{$1, (NimStrPayload*)&$2}" % [rope(n.strVal.len), pureLit]
  80. # ------ Version selector ---------------------------------------------------
  81. proc genStringLiteralDataOnly(m: BModule; s: string; info: TLineInfo;
  82. isConst: bool): Rope =
  83. case detectStrVersion(m)
  84. of 0, 1: result = genStringLiteralDataOnlyV1(m, s)
  85. of 2:
  86. result = getTempName(m)
  87. genStringLiteralDataOnlyV2(m, s, result, isConst)
  88. else:
  89. localError(m.config, info, "cannot determine how to produce code for string literal")
  90. proc genNilStringLiteral(m: BModule; info: TLineInfo): Rope =
  91. result = ropecg(m, "((#NimStringDesc*) NIM_NIL)", [])
  92. proc genStringLiteral(m: BModule; n: PNode): Rope =
  93. case detectStrVersion(m)
  94. of 0, 1: result = genStringLiteralV1(m, n)
  95. of 2: result = genStringLiteralV2(m, n, isConst = true)
  96. else:
  97. localError(m.config, n.info, "cannot determine how to produce code for string literal")