aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRuss Cox <rsc@golang.org>2011-04-26 00:57:03 -0400
committerRuss Cox <rsc@golang.org>2011-04-26 00:57:03 -0400
commitbac8f180352213e7fcbe92cc97c96f625e23d2ab (patch)
tree2c6e82e458b1c59d098279a80305e0627ed88d10
parentf813702f6de54b2cde8df73e5a06bfb5de20b930 (diff)
downloadgo-bac8f180352213e7fcbe92cc97c96f625e23d2ab.tar.gz
go-bac8f180352213e7fcbe92cc97c96f625e23d2ab.zip
gc: fix order of operations for f() < g().
Also, 6g was passing uninitialized Node &n2 to regalloc, causing non-deterministic register collisions (but only when both left and right hand side of comparison had function calls). Fixes #1728. R=ken2 CC=golang-dev https://golang.org/cl/4425070
-rw-r--r--src/cmd/5g/cgen.c16
-rw-r--r--src/cmd/6g/cgen.c16
-rw-r--r--src/cmd/8g/cgen.c4
-rw-r--r--test/func7.go29
4 files changed, 47 insertions, 18 deletions
diff --git a/src/cmd/5g/cgen.c b/src/cmd/5g/cgen.c
index e0fc768215..4e5f7ebcdc 100644
--- a/src/cmd/5g/cgen.c
+++ b/src/cmd/5g/cgen.c
@@ -962,7 +962,7 @@ bgen(Node *n, int true, Prog *to)
}
// make simplest on right
- if(nl->op == OLITERAL || nl->ullman < nr->ullman) {
+ if(nl->op == OLITERAL || (nl->ullman < UINF && nl->ullman < nr->ullman)) {
a = brrev(a);
r = nl;
nl = nr;
@@ -1073,18 +1073,18 @@ bgen(Node *n, int true, Prog *to)
a = optoas(a, nr->type);
if(nr->ullman >= UINF) {
- regalloc(&n1, nr->type, N);
- cgen(nr, &n1);
+ regalloc(&n1, nl->type, N);
+ cgen(nl, &n1);
- tempname(&tmp, nr->type);
+ tempname(&tmp, nl->type);
gmove(&n1, &tmp);
regfree(&n1);
- regalloc(&n1, nl->type, N);
- cgen(nl, &n1);
-
regalloc(&n2, nr->type, N);
- cgen(&tmp, &n2);
+ cgen(nr, &n2);
+
+ regalloc(&n1, nl->type, N);
+ cgen(&tmp, &n1);
gcmp(optoas(OCMP, nr->type), &n1, &n2);
patch(gbranch(a, nr->type), to);
diff --git a/src/cmd/6g/cgen.c b/src/cmd/6g/cgen.c
index 048174e086..75dc4fe134 100644
--- a/src/cmd/6g/cgen.c
+++ b/src/cmd/6g/cgen.c
@@ -829,7 +829,7 @@ bgen(Node *n, int true, Prog *to)
}
// make simplest on right
- if(nl->op == OLITERAL || nl->ullman < nr->ullman) {
+ if(nl->op == OLITERAL || (nl->ullman < nr->ullman && nl->ullman < UINF)) {
a = brrev(a);
r = nl;
nl = nr;
@@ -879,18 +879,18 @@ bgen(Node *n, int true, Prog *to)
}
if(nr->ullman >= UINF) {
- regalloc(&n1, nr->type, N);
- cgen(nr, &n1);
+ regalloc(&n1, nl->type, N);
+ cgen(nl, &n1);
- tempname(&tmp, nr->type);
+ tempname(&tmp, nl->type);
gmove(&n1, &tmp);
regfree(&n1);
- regalloc(&n1, nl->type, N);
- cgen(nl, &n1);
+ regalloc(&n2, nr->type, N);
+ cgen(nr, &n2);
- regalloc(&n2, nr->type, &n2);
- cgen(&tmp, &n2);
+ regalloc(&n1, nl->type, N);
+ cgen(&tmp, &n1);
goto cmp;
}
diff --git a/src/cmd/8g/cgen.c b/src/cmd/8g/cgen.c
index 036188fec4..596824a6cc 100644
--- a/src/cmd/8g/cgen.c
+++ b/src/cmd/8g/cgen.c
@@ -900,7 +900,7 @@ bgen(Node *n, int true, Prog *to)
}
// make simplest on right
- if(nl->op == OLITERAL || nl->ullman < nr->ullman) {
+ if(nl->op == OLITERAL || (nl->ullman < nr->ullman && nl->ullman < UINF)) {
a = brrev(a);
r = nl;
nl = nr;
@@ -1025,8 +1025,8 @@ bgen(Node *n, int true, Prog *to)
if(nr->ullman >= UINF) {
tempname(&n1, nl->type);
tempname(&tmp, nr->type);
- cgen(nr, &tmp);
cgen(nl, &n1);
+ cgen(nr, &tmp);
regalloc(&n2, nr->type, N);
cgen(&tmp, &n2);
goto cmp;
diff --git a/test/func7.go b/test/func7.go
new file mode 100644
index 0000000000..e38b008cc0
--- /dev/null
+++ b/test/func7.go
@@ -0,0 +1,29 @@
+// $G $D/$F.go && $L $F.$A && ./$A.out
+
+// Copyright 2011 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package main
+
+var calledf = false
+
+func f() int {
+ calledf = true
+ return 1
+}
+
+func g() int {
+ if !calledf {
+ println("BUG: func7 - called g before f")
+ }
+ return 0
+}
+
+func main() {
+ // 6g, 8g, 5g all used to evaluate g() before f().
+ if f() < g() {
+ panic("wrong answer")
+ }
+}
+