cmd/compile: fix exporting of 'for' loops

The existing code for encoding 'for' loops in exported, inlineable
functions incorrectly assumed that the 'Right' field points to an
'expression' node. Adjusted the code to be able to handle any kind
of node. Made matching changes for the binary and indexed exporter.

This only shows up together with other pending compiler changes that
enable exporting of such functions which contain for loops.

No tests yet because we can't test this w/o those pending compiler
changes. Once those changes are in, this code will be tested implicitly.
However, the changes were tested manually together with the patches
described in the issue.

Fixes #25222.

Change-Id: I54babb87e5d665d2c1ef6116c1de1b8c50b1138e
Reviewed-on: https://go-review.googlesource.com/119595
Reviewed-by: David Chase <drchase@google.com>
diff --git a/src/cmd/compile/internal/gc/bexport.go b/src/cmd/compile/internal/gc/bexport.go
index c2672cb..0d4997c 100644
--- a/src/cmd/compile/internal/gc/bexport.go
+++ b/src/cmd/compile/internal/gc/bexport.go
@@ -1128,16 +1128,20 @@
 		}
 		// TODO inlining produces expressions with ninits. we can't export these yet.
 		// (from fmt.go:1461ff)
-		if opprec[n.Op] < 0 {
-			p.stmt(n)
-		} else {
-			p.expr(n)
-		}
+		p.node(n)
 	}
 
 	p.op(OEND)
 }
 
+func (p *exporter) node(n *Node) {
+	if opprec[n.Op] < 0 {
+		p.stmt(n)
+	} else {
+		p.expr(n)
+	}
+}
+
 func (p *exporter) exprList(list Nodes) {
 	if p.trace {
 		if list.Len() == 0 {
@@ -1552,7 +1556,7 @@
 		p.expr(a)
 	}
 	if ab&2 != 0 {
-		p.expr(b)
+		p.node(b)
 	}
 }
 
diff --git a/src/cmd/compile/internal/gc/bimport.go b/src/cmd/compile/internal/gc/bimport.go
index 8215e46..c19f548 100644
--- a/src/cmd/compile/internal/gc/bimport.go
+++ b/src/cmd/compile/internal/gc/bimport.go
@@ -1209,7 +1209,7 @@
 		a = p.expr()
 	}
 	if ab&2 != 0 {
-		b = p.expr()
+		b = p.node()
 	}
 	return
 }
diff --git a/src/cmd/compile/internal/gc/iexport.go b/src/cmd/compile/internal/gc/iexport.go
index f6e9b8b..3abbd15 100644
--- a/src/cmd/compile/internal/gc/iexport.go
+++ b/src/cmd/compile/internal/gc/iexport.go
@@ -970,15 +970,19 @@
 
 func (w *exportWriter) stmtList(list Nodes) {
 	for _, n := range list.Slice() {
-		if opprec[n.Op] < 0 {
-			w.stmt(n)
-		} else {
-			w.expr(n)
-		}
+		w.node(n)
 	}
 	w.op(OEND)
 }
 
+func (w *exportWriter) node(n *Node) {
+	if opprec[n.Op] < 0 {
+		w.stmt(n)
+	} else {
+		w.expr(n)
+	}
+}
+
 // Caution: stmt will emit more than one node for statement nodes n that have a non-empty
 // n.Ninit and where n cannot have a natural init section (such as in "if", "for", etc.).
 func (w *exportWriter) stmt(n *Node) {
@@ -1338,7 +1342,7 @@
 		w.expr(a)
 	}
 	if ab&2 != 0 {
-		w.expr(b)
+		w.node(b)
 	}
 }
 
diff --git a/src/cmd/compile/internal/gc/iimport.go b/src/cmd/compile/internal/gc/iimport.go
index 4d66b4b..d158899 100644
--- a/src/cmd/compile/internal/gc/iimport.go
+++ b/src/cmd/compile/internal/gc/iimport.go
@@ -1060,7 +1060,7 @@
 		a = r.expr()
 	}
 	if ab&2 != 0 {
-		b = r.expr()
+		b = r.node()
 	}
 	return
 }