summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--compiler/ccgliterals.nim19
1 files changed, 12 insertions, 7 deletions
diff --git a/compiler/ccgliterals.nim b/compiler/ccgliterals.nim
index 904d01e81..efbae3cb8 100644
--- a/compiler/ccgliterals.nim
+++ b/compiler/ccgliterals.nim
@@ -51,8 +51,7 @@ proc genStringLiteralV1(m: BModule; n: PNode): Rope =
 
 # ------ Version 2: destructor based strings and seqs -----------------------
 
-proc genStringLiteralDataOnlyV2(m: BModule, s: string): Rope =
-  result = getTempName(m)
+proc genStringLiteralDataOnlyV2(m: BModule, s: string; result: Rope) =
   addf(m.s[cfsData], "static const struct {$n" &
        "  NI cap; void* allocator; NIM_CHAR data[$2+1];$n" &
        "} $1 = { $2, NIM_NIL, $3 };$n",
@@ -61,24 +60,28 @@ proc genStringLiteralDataOnlyV2(m: BModule, s: string): Rope =
 proc genStringLiteralV2(m: BModule; n: PNode): Rope =
   let id = nodeTableTestOrSet(m.dataCache, n, m.labels)
   if id == m.labels:
+    let pureLit = getTempName(m)
+    genStringLiteralDataOnlyV2(m, n.strVal, pureLit)
+    result = getTempName(m)
     discard cgsym(m, "NimStrPayload")
     discard cgsym(m, "NimStringV2")
     # string literal not found in the cache:
-    let pureLit = genStringLiteralDataOnlyV2(m, n.strVal)
-    result = getTempName(m)
     addf(m.s[cfsData], "static const NimStringV2 $1 = {$2, (NimStrPayload*)&$3};$n",
           [result, rope(len(n.strVal)), pureLit])
   else:
-    result = m.tmpBase & rope(id+1)
+    result = getTempName(m)
+    addf(m.s[cfsData], "static const NimStringV2 $1 = {$2, (NimStrPayload*)&$3};$n",
+          [result, rope(len(n.strVal)), m.tmpBase & rope(id)])
 
 proc genStringLiteralV2Const(m: BModule; n: PNode): Rope =
   let id = nodeTableTestOrSet(m.dataCache, n, m.labels)
   var pureLit: Rope
   if id == m.labels:
+    pureLit = getTempName(m)
     discard cgsym(m, "NimStrPayload")
     discard cgsym(m, "NimStringV2")
     # string literal not found in the cache:
-    pureLit = genStringLiteralDataOnlyV2(m, n.strVal)
+    genStringLiteralDataOnlyV2(m, n.strVal, pureLit)
   else:
     pureLit = m.tmpBase & rope(id)
   result = "{$1, (NimStrPayload*)&$2}" % [rope(len(n.strVal)), pureLit]
@@ -88,7 +91,9 @@ proc genStringLiteralV2Const(m: BModule; n: PNode): Rope =
 proc genStringLiteralDataOnly(m: BModule; s: string; info: TLineInfo): Rope =
   case detectStrVersion(m)
   of 0, 1: result = genStringLiteralDataOnlyV1(m, s)
-  of 2: result = genStringLiteralDataOnlyV2(m, s)
+  of 2:
+    result = getTempName(m)
+    genStringLiteralDataOnlyV2(m, s, result)
   else:
     localError(m.config, info, "cannot determine how to produce code for string literal")