cmd/compile: add debugging mode for import/export

Just add a simple magic number with each op, to detect when
the reader gets desynchronized from the writer.

Change-Id: Iac7dab7f465b0021b1d7ae31c8f8a353ac3663a2
Reviewed-on: https://go-review.googlesource.com/c/go/+/299769
Trust: Keith Randall <khr@golang.org>
Run-TryBot: Keith Randall <khr@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Dan Scales <danscales@google.com>
diff --git a/src/cmd/compile/internal/typecheck/iexport.go b/src/cmd/compile/internal/typecheck/iexport.go
index 38ac753..6f33ca1 100644
--- a/src/cmd/compile/internal/typecheck/iexport.go
+++ b/src/cmd/compile/internal/typecheck/iexport.go
@@ -246,6 +246,11 @@
 	interfaceType
 )
 
+const (
+	debug = false
+	magic = 0x6742937dc293105
+)
+
 func WriteExports(out *bufio.Writer) {
 	p := iexporter{
 		allPkgs:     map[*types.Pkg]bool{},
@@ -1584,6 +1589,9 @@
 }
 
 func (w *exportWriter) op(op ir.Op) {
+	if debug {
+		w.uint64(magic)
+	}
 	w.uint64(uint64(op))
 }
 
diff --git a/src/cmd/compile/internal/typecheck/iimport.go b/src/cmd/compile/internal/typecheck/iimport.go
index 8df75b2..d7c118b 100644
--- a/src/cmd/compile/internal/typecheck/iimport.go
+++ b/src/cmd/compile/internal/typecheck/iimport.go
@@ -1228,6 +1228,9 @@
 }
 
 func (r *importReader) op() ir.Op {
+	if debug && r.uint64() != magic {
+		base.Fatalf("import stream has desynchronized")
+	}
 	return ir.Op(r.uint64())
 }