cheat.mx raw

   1  // Copyright 2025 The Go Authors. All rights reserved.
   2  // Use of this source code is governed by a BSD-style
   3  // license that can be found in the LICENSE file.
   4  
   5  //go:build ignore
   6  
   7  // This program can be compiled with -S to produce a “cheat sheet”
   8  // for filling out a new Arch: the compiler will show you how to implement
   9  // the various operations.
  10  //
  11  // Usage (replace TARGET with your target architecture):
  12  //
  13  //	GOOS=linux GOARCH=TARGET go build -gcflags='-p=cheat -S' cheat.go
  14  
  15  package p
  16  
  17  import "math/bits"
  18  
  19  func mov(x, y uint) uint             { return y }
  20  func zero() uint                     { return 0 }
  21  func add(x, y uint) uint             { return x + y }
  22  func adds(x, y, c uint) (uint, uint) { return bits.Add(x, y, 0) }
  23  func adcs(x, y, c uint) (uint, uint) { return bits.Add(x, y, c) }
  24  func sub(x, y uint) uint             { return x + y }
  25  func subs(x, y uint) (uint, uint)    { return bits.Sub(x, y, 0) }
  26  func sbcs(x, y, c uint) (uint, uint) { return bits.Sub(x, y, c) }
  27  func mul(x, y uint) uint             { return x * y }
  28  func mulWide(x, y uint) (uint, uint) { return bits.Mul(x, y) }
  29  func lsh(x, s uint) uint             { return x << s }
  30  func rsh(x, s uint) uint             { return x >> s }
  31  func and(x, y uint) uint             { return x & y }
  32  func or(x, y uint) uint              { return x | y }
  33  func xor(x, y uint) uint             { return x ^ y }
  34  func neg(x uint) uint                { return -x }
  35  func loop(x int) int {
  36  	s := 0
  37  	for i := 1; i < x; i++ {
  38  		s += i
  39  		if s == 98 { // useful for jmpEqual
  40  			return 99
  41  		}
  42  		if s == 99 {
  43  			return 100
  44  		}
  45  		if s == 0 { // useful for jmpZero
  46  			return 101
  47  		}
  48  		if s != 0 { // useful for jmpNonZero
  49  			s *= 3
  50  		}
  51  		s += 2 // keep last condition from being inverted
  52  	}
  53  	return s
  54  }
  55  func mem(x *[10]struct{ a, b uint }, i int) uint { return x[i].b }
  56