value_equality.mx raw

   1  // Regression: `==` on a value whose LLVM type is an aggregate emitted one
   2  // `icmp` on the aggregate, which clang rejects.
   3  //
   4  // ir_binop's array branch had a shortcut for "an array of scalars is one
   5  // integer of element-width times length" and compared it whole. An LLVM icmp
   6  // takes scalars and vectors, never a plain aggregate, so the module failed with
   7  // "icmp requires integer operands". Nothing noticed until `crypto/ec` was built
   8  // for the first time: its `FieldVal` keeps its limbs in `[10]uint32` and
   9  // `point.X == inf.X` compared a struct whose only field is that array. legacy's
  10  // createBinOp has compared arrays and structs element-wise all along, so this
  11  // was a stage4-only divergence.
  12  //
  13  // The comparison is element-wise again, matching the spec: array values are
  14  // equal when their corresponding elements are equal, and struct values when
  15  // their corresponding non-blank fields are.
  16  package main
  17  
  18  import (
  19  	"os"
  20  )
  21  
  22  type limbs struct {
  23  	n [10]uint32
  24  }
  25  
  26  type point struct {
  27  	X, Y limbs
  28  	tag  [3]byte
  29  }
  30  
  31  func cmp(name string, got, want bool) {
  32  	if got != want {
  33  		println("FAIL: " | name | " got ", got, " want ", want)
  34  		os.Exit(1)
  35  	}
  36  }
  37  
  38  func main() {
  39  	// Termination: a batch program must still be stoppable, so the entry
  40  	// handles the channel the runtime closes on SIGINT/SIGTERM.
  41  	select {
  42  	case <-shutdown:
  43  		return
  44  	default:
  45  	}
  46  
  47  	var a, b limbs
  48  	// Large elements, so an element compare cannot pass by looking at the low
  49  	// word only.
  50  	a.n[0] = 0xFFFFFFFF
  51  	a.n[9] = 0x80000000
  52  	b = a
  53  	cmp("array equal", a == b, true)
  54  	cmp("array neq", a != b, false)
  55  	b.n[9] = 0x80000001
  56  	cmp("array high word differs", a == b, false)
  57  	cmp("array high word differs neq", a != b, true)
  58  
  59  	var p, q point
  60  	p.X = a
  61  	p.Y = b
  62  	p.tag = [3]byte{'x', 'y', 'z'}
  63  	q = p
  64  	cmp("struct equal", p == q, true)
  65  	q.tag[2] = 'w'
  66  	cmp("struct last byte differs", p == q, false)
  67  
  68  	// Nested, and after mutation through the value.
  69  	cmp("nested same", q.X == p.X, true)
  70  	// p.Y is b, which differs from a in the last word.
  71  	cmp("nested differs", q.Y == a, false)
  72  	cmp("nested other way", a == q.Y, false)
  73  
  74  	// Arrays of bytes and of 64-bit words, the shapes the digest code has.
  75  	var d1, d2 [32]byte
  76  	d1[31] = 9
  77  	d2[31] = 9
  78  	cmp("32 bytes equal", d1 == d2, true)
  79  	d2[31] = 8
  80  	cmp("32 bytes differ", d1 == d2, false)
  81  
  82  	var w1, w2 [4]uint64
  83  	w1[3] = 1 << 63
  84  	w2[3] = 1 << 63
  85  	cmp("4 words equal", w1 == w2, true)
  86  	w2[0] = 1
  87  	cmp("4 words differ low", w1 == w2, false)
  88  	println("ok")
  89  }
  90