summary refs log tree commit diff stats
path: root/compiler
diff options
context:
space:
mode:
Diffstat (limited to 'compiler')
-rw-r--r--compiler/ccgstmts.nim24
-rw-r--r--compiler/cgen.nim5
2 files changed, 22 insertions, 7 deletions
diff --git a/compiler/ccgstmts.nim b/compiler/ccgstmts.nim
index 294235ae9..10b5641c5 100644
--- a/compiler/ccgstmts.nim
+++ b/compiler/ccgstmts.nim
@@ -293,6 +293,8 @@ proc genIf(p: BProc, n: PNode, d: var TLoc) =
   genLineDir(p, n)
   let lend = getLabel(p)
   for i in countup(0, sonsLen(n) - 1):
+    # bug #4230: avoid false sharing between branches:
+    if d.k == locTemp and isEmptyType(n.typ): d.k = locNone
     let it = n.sons[i]
     if it.len == 2:
       when newScopeForIf: startBlock(p)
@@ -488,16 +490,20 @@ proc genWhileStmt(p: BProc, t: PNode) =
 
   dec(p.withinLoop)
 
-proc genBlock(p: BProc, t: PNode, d: var TLoc) =
+proc genBlock(p: BProc, n: PNode, d: var TLoc) =
+  # bug #4505: allocate the temp in the outer scope
+  # so that it can escape the generated {}:
+  if not isEmptyType(n.typ) and d.k == locNone:
+    getTemp(p, n.typ, d)
   preserveBreakIdx:
     p.breakIdx = startBlock(p)
-    if t.sons[0].kind != nkEmpty:
+    if n.sons[0].kind != nkEmpty:
       # named block?
-      assert(t.sons[0].kind == nkSym)
-      var sym = t.sons[0].sym
+      assert(n.sons[0].kind == nkSym)
+      var sym = n.sons[0].sym
       sym.loc.k = locOther
       sym.position = p.breakIdx+1
-    expr(p, t.sons[1], d)
+    expr(p, n.sons[1], d)
     endBlock(p)
 
 proc genParForStmt(p: BProc, t: PNode) =
@@ -592,6 +598,8 @@ proc genCaseSecondPass(p: BProc, t: PNode, d: var TLoc,
                        labId, until: int): TLabel =
   var lend = getLabel(p)
   for i in 1..until:
+    # bug #4230: avoid false sharing between branches:
+    if d.k == locTemp and isEmptyType(t.typ): d.k = locNone
     lineF(p, cpsStmts, "LA$1: ;$n", [rope(labId + i)])
     if t.sons[i].kind == nkOfBranch:
       var length = sonsLen(t.sons[i])
@@ -727,6 +735,8 @@ proc genOrdinalCase(p: BProc, n: PNode, d: var TLoc) =
     lineF(p, cpsStmts, "switch ($1) {$n", [rdCharLoc(a)])
     var hasDefault = false
     for i in splitPoint+1 .. < n.len:
+      # bug #4230: avoid false sharing between branches:
+      if d.k == locTemp and isEmptyType(n.typ): d.k = locNone
       var branch = n[i]
       if branch.kind == nkOfBranch:
         genCaseRange(p, branch)
@@ -797,6 +807,8 @@ proc genTryCpp(p: BProc, t: PNode, d: var TLoc) =
   var i = 1
   var catchAllPresent = false
   while (i < length) and (t.sons[i].kind == nkExceptBranch):
+    # bug #4230: avoid false sharing between branches:
+    if d.k == locTemp and isEmptyType(t.typ): d.k = locNone
     let blen = sonsLen(t.sons[i])
     if i > 1: addf(p.s(cpsStmts), "else ", [])
     if blen == 1:
@@ -902,6 +914,8 @@ proc genTry(p: BProc, t: PNode, d: var TLoc) =
   inc p.inExceptBlock
   var i = 1
   while (i < length) and (t.sons[i].kind == nkExceptBranch):
+    # bug #4230: avoid false sharing between branches:
+    if d.k == locTemp and isEmptyType(t.typ): d.k = locNone
     var blen = sonsLen(t.sons[i])
     if blen == 1:
       # general except section:
diff --git a/compiler/cgen.nim b/compiler/cgen.nim
index 7333c77c3..48b6e9633 100644
--- a/compiler/cgen.nim
+++ b/compiler/cgen.nim
@@ -323,8 +323,9 @@ proc initLocalVar(p: BProc, v: PSym, immediateAsgn: bool) =
 proc getTemp(p: BProc, t: PType, result: var TLoc; needsInit=false) =
   inc(p.labels)
   result.r = "LOC" & rope(p.labels)
-  addf(p.blocks[0].sections[cpsLocals],
-     "$1 $2;$n", [getTypeDesc(p.module, t), result.r])
+  #addf(p.blocks[0].sections[cpsLocals],
+  #   "$1 $2;$n", [getTypeDesc(p.module, t), result.r])
+  linefmt(p, cpsLocals, "$1 $2;$n", getTypeDesc(p.module, t), result.r)
   result.k = locTemp
   #result.a = - 1
   result.t = t