github.com/shogo82148/std@v1.22.1-0.20240327122250-4e474527810c/cmd/compile/internal/compare/compare.go (about) 1 // Copyright 2022 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 // Package compare contains code for generating comparison 6 // routines for structs, strings and interfaces. 7 package compare 8 9 import ( 10 "github.com/shogo82148/std/cmd/compile/internal/ir" 11 "github.com/shogo82148/std/cmd/compile/internal/types" 12 ) 13 14 // IsRegularMemory reports whether t can be compared/hashed as regular memory. 15 func IsRegularMemory(t *types.Type) bool 16 17 // Memrun finds runs of struct fields for which memory-only algs are appropriate. 18 // t is the parent struct type, and start is the field index at which to start the run. 19 // size is the length in bytes of the memory included in the run. 20 // next is the index just after the end of the memory run. 21 func Memrun(t *types.Type, start int) (size int64, next int) 22 23 // EqCanPanic reports whether == on type t could panic (has an interface somewhere). 24 // t must be comparable. 25 func EqCanPanic(t *types.Type) bool 26 27 // EqStructCost returns the cost of an equality comparison of two structs. 28 // 29 // The cost is determined using an algorithm which takes into consideration 30 // the size of the registers in the current architecture and the size of the 31 // memory-only fields in the struct. 32 func EqStructCost(t *types.Type) int64 33 34 // EqStruct compares two structs np and nq for equality. 35 // It works by building a list of boolean conditions to satisfy. 36 // Conditions must be evaluated in the returned order and 37 // properly short-circuited by the caller. 38 // The first return value is the flattened list of conditions, 39 // the second value is a boolean indicating whether any of the 40 // comparisons could panic. 41 func EqStruct(t *types.Type, np, nq ir.Node) ([]ir.Node, bool) 42 43 // EqString returns the nodes 44 // 45 // len(s) == len(t) 46 // 47 // and 48 // 49 // memequal(s.ptr, t.ptr, len(s)) 50 // 51 // which can be used to construct string equality comparison. 52 // eqlen must be evaluated before eqmem, and shortcircuiting is required. 53 func EqString(s, t ir.Node) (eqlen *ir.BinaryExpr, eqmem *ir.CallExpr) 54 55 // EqInterface returns the nodes 56 // 57 // s.tab == t.tab (or s.typ == t.typ, as appropriate) 58 // 59 // and 60 // 61 // ifaceeq(s.tab, s.data, t.data) (or efaceeq(s.typ, s.data, t.data), as appropriate) 62 // 63 // which can be used to construct interface equality comparison. 64 // eqtab must be evaluated before eqdata, and shortcircuiting is required. 65 func EqInterface(s, t ir.Node) (eqtab *ir.BinaryExpr, eqdata *ir.CallExpr)