]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile: make cmpstackvarlt properly asymmetric
authorMatthew Dempsky <mdempsky@google.com>
Sun, 21 Feb 2016 05:36:12 +0000 (21:36 -0800)
committerMatthew Dempsky <mdempsky@google.com>
Mon, 22 Feb 2016 00:09:36 +0000 (00:09 +0000)
Previously, given two Nodes n1 and n2 of different non-PAUTO classes
(e.g., PPARAM and PPARAMOUT), cmpstackvarlt(n1, n2) and
cmpstackvarlt(n2, n1) both returned true, which is nonsense.

This doesn't seem to cause any visible miscompilation problems, but
notably fixing it does cause toolstash/buildall to fail.

Change-Id: I33b2c66e902c5eced875d8fbf18b7cfdc81e8aed
Reviewed-on: https://go-review.googlesource.com/19778
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>

src/cmd/compile/internal/gc/pgen.go
src/cmd/compile/internal/gc/pgen_test.go

index ffc0ab9cfb81824845c41ea7f01f41b4a93a7124..3471b977ed113bd4d6442bfc7b4d53157fa01f3e 100644 (file)
@@ -186,21 +186,12 @@ func emitptrargsmap() {
 // the top of the stack and increasing in size.
 // Non-autos sort on offset.
 func cmpstackvarlt(a, b *Node) bool {
-       if a.Class != b.Class {
-               if a.Class == PAUTO {
-                       return false
-               }
-               return true
+       if (a.Class == PAUTO) != (b.Class == PAUTO) {
+               return b.Class == PAUTO
        }
 
        if a.Class != PAUTO {
-               if a.Xoffset < b.Xoffset {
-                       return true
-               }
-               if a.Xoffset > b.Xoffset {
-                       return false
-               }
-               return false
+               return a.Xoffset < b.Xoffset
        }
 
        if a.Used != b.Used {
@@ -219,11 +210,8 @@ func cmpstackvarlt(a, b *Node) bool {
                return ap
        }
 
-       if a.Type.Width < b.Type.Width {
-               return false
-       }
-       if a.Type.Width > b.Type.Width {
-               return true
+       if a.Type.Width != b.Type.Width {
+               return a.Type.Width > b.Type.Width
        }
 
        return a.Sym.Name < b.Sym.Name
index ebc910113551d24504e2a776399513798fb12070..909b8a95071f71a67f5fc49c21b2ff0a29423e31 100644 (file)
@@ -40,6 +40,16 @@ func TestCmpstackvar(t *testing.T) {
                        Node{Class: PFUNC, Xoffset: 10},
                        false,
                },
+               {
+                       Node{Class: PPARAM, Xoffset: 10},
+                       Node{Class: PPARAMOUT, Xoffset: 20},
+                       true,
+               },
+               {
+                       Node{Class: PPARAMOUT, Xoffset: 10},
+                       Node{Class: PPARAM, Xoffset: 20},
+                       true,
+               },
                {
                        Node{Class: PAUTO, Used: true},
                        Node{Class: PAUTO, Used: false},
@@ -101,6 +111,10 @@ func TestCmpstackvar(t *testing.T) {
                if got != d.lt {
                        t.Errorf("want %#v < %#v", d.a, d.b)
                }
+               // If we expect a < b to be true, check that b < a is false.
+               if d.lt && cmpstackvarlt(&d.b, &d.a) {
+                       t.Errorf("unexpected %#v < %#v", d.b, d.a)
+               }
        }
 }