Source file src/cmd/compile/internal/ssa/debug.go

     1  // Copyright 2017 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 ssa
     6  
     7  import (
     8  	"cmd/compile/internal/abi"
     9  	"cmd/compile/internal/abt"
    10  	"cmd/compile/internal/ir"
    11  	"cmd/compile/internal/types"
    12  	"cmd/internal/dwarf"
    13  	"cmd/internal/obj"
    14  	"cmd/internal/src"
    15  	"cmp"
    16  	"encoding/hex"
    17  	"fmt"
    18  	"internal/buildcfg"
    19  	"math/bits"
    20  	"slices"
    21  	"strings"
    22  )
    23  
    24  type SlotID int32
    25  type VarID int32
    26  
    27  // A FuncDebug contains all the debug information for the variables in a
    28  // function. Variables are identified by their LocalSlot, which may be
    29  // the result of decomposing a larger variable.
    30  type FuncDebug struct {
    31  	// Slots is all the slots used in the debug info, indexed by their SlotID.
    32  	Slots []LocalSlot
    33  	// The user variables, indexed by VarID.
    34  	Vars []*ir.Name
    35  	// The slots that make up each variable, indexed by VarID.
    36  	VarSlots [][]SlotID
    37  	// The location list data, indexed by VarID. Must be processed by PutLocationList.
    38  	LocationLists [][]byte
    39  	// Register-resident output parameters for the function. This is filled in at
    40  	// SSA generation time.
    41  	RegOutputParams []*ir.Name
    42  	// Variable declarations that were removed during optimization
    43  	OptDcl []*ir.Name
    44  
    45  	// Filled in by the user. Translates Block and Value ID to PC.
    46  	//
    47  	// NOTE: block is only used if value is BlockStart.ID or BlockEnd.ID.
    48  	// Otherwise, it is ignored.
    49  	GetPC func(block, value ID) int64
    50  }
    51  
    52  type BlockDebug struct {
    53  	// State at the start and end of the block. These are initialized,
    54  	// and updated from new information that flows on back edges.
    55  	startState, endState abt.T
    56  	// Use these to avoid excess work in the merge. If none of the
    57  	// predecessors has changed since the last check, the old answer is
    58  	// still good.
    59  	lastCheckedTime, lastChangedTime int32
    60  	// Whether the block had any changes to user variables at all.
    61  	relevant bool
    62  	// false until the block has been processed at least once. This
    63  	// affects how the merge is done; the goal is to maximize sharing
    64  	// and avoid allocation.
    65  	everProcessed bool
    66  }
    67  
    68  // A liveSlot is a slot that's live in loc at entry/exit of a block.
    69  type liveSlot struct {
    70  	VarLoc
    71  }
    72  
    73  func (ls *liveSlot) String() string {
    74  	return fmt.Sprintf("0x%x.%d.%d", ls.Registers, ls.stackOffsetValue(), int32(ls.StackOffset)&1)
    75  }
    76  
    77  func (ls liveSlot) absent() bool {
    78  	return ls.Registers == 0 && !ls.onStack()
    79  }
    80  
    81  // StackOffset encodes whether a value is on the stack and if so, where.
    82  // It is a 31-bit integer followed by a presence flag at the low-order
    83  // bit.
    84  type StackOffset int32
    85  
    86  func (s StackOffset) onStack() bool {
    87  	return s != 0
    88  }
    89  
    90  func (s StackOffset) stackOffsetValue() int32 {
    91  	return int32(s) >> 1
    92  }
    93  
    94  // stateAtPC is the current state of all variables at some point.
    95  type stateAtPC struct {
    96  	// The location of each known slot, indexed by SlotID.
    97  	slots []VarLoc
    98  	// The slots present in each register, indexed by register number.
    99  	registers [][]SlotID
   100  }
   101  
   102  // reset fills state with the live variables from live.
   103  func (state *stateAtPC) reset(live abt.T) {
   104  	slots, registers := state.slots, state.registers
   105  	clear(slots)
   106  	for i := range registers {
   107  		registers[i] = registers[i][:0]
   108  	}
   109  	for it := live.Iterator(); !it.Done(); {
   110  		k, d := it.Next()
   111  		live := d.(*liveSlot)
   112  		slots[k] = live.VarLoc
   113  		if live.VarLoc.Registers == 0 {
   114  			continue
   115  		}
   116  
   117  		mask := uint64(live.VarLoc.Registers)
   118  		for {
   119  			if mask == 0 {
   120  				break
   121  			}
   122  			reg := uint8(bits.TrailingZeros64(mask))
   123  			mask &^= 1 << reg
   124  
   125  			registers[reg] = append(registers[reg], SlotID(k))
   126  		}
   127  	}
   128  	state.slots, state.registers = slots, registers
   129  }
   130  
   131  func (s *debugState) LocString(loc VarLoc) string {
   132  	if loc.absent() {
   133  		return "<nil>"
   134  	}
   135  
   136  	var storage []string
   137  	if loc.onStack() {
   138  		storage = append(storage, fmt.Sprintf("@%+d", loc.stackOffsetValue()))
   139  	}
   140  
   141  	mask := uint64(loc.Registers)
   142  	for {
   143  		if mask == 0 {
   144  			break
   145  		}
   146  		reg := uint8(bits.TrailingZeros64(mask))
   147  		mask &^= 1 << reg
   148  
   149  		storage = append(storage, s.registers[reg].String())
   150  	}
   151  	return strings.Join(storage, ",")
   152  }
   153  
   154  // A VarLoc describes the storage for part of a user variable.
   155  type VarLoc struct {
   156  	// The registers this variable is available in. There can be more than
   157  	// one in various situations, e.g. it's being moved between registers.
   158  	Registers RegisterSet
   159  
   160  	StackOffset
   161  }
   162  
   163  func (loc VarLoc) absent() bool {
   164  	return loc.Registers == 0 && !loc.onStack()
   165  }
   166  
   167  func (loc VarLoc) intersect(other VarLoc) VarLoc {
   168  	if !loc.onStack() || !other.onStack() || loc.StackOffset != other.StackOffset {
   169  		loc.StackOffset = 0
   170  	}
   171  	loc.Registers &= other.Registers
   172  	return loc
   173  }
   174  
   175  var BlockStart = &Value{
   176  	ID:  -10000,
   177  	Op:  OpInvalid,
   178  	Aux: StringToAux("BlockStart"),
   179  }
   180  
   181  var BlockEnd = &Value{
   182  	ID:  -20000,
   183  	Op:  OpInvalid,
   184  	Aux: StringToAux("BlockEnd"),
   185  }
   186  
   187  var FuncEnd = &Value{
   188  	ID:  -30000,
   189  	Op:  OpInvalid,
   190  	Aux: StringToAux("FuncEnd"),
   191  }
   192  
   193  // RegisterSet is a bitmap of registers, indexed by Register.num.
   194  type RegisterSet uint64
   195  
   196  // logf prints debug-specific logging to stdout (always stdout) if the
   197  // current function is tagged by GOSSAFUNC (for ssa output directed
   198  // either to stdout or html).
   199  func (s *debugState) logf(msg string, args ...interface{}) {
   200  	if s.f.PrintOrHtmlSSA {
   201  		fmt.Printf(msg, args...)
   202  	}
   203  }
   204  
   205  type debugState struct {
   206  	// See FuncDebug.
   207  	slots    []LocalSlot
   208  	vars     []*ir.Name
   209  	varSlots [][]SlotID
   210  	lists    [][]byte
   211  
   212  	// The user variable that each slot rolls up to, indexed by SlotID.
   213  	slotVars []VarID
   214  
   215  	f             *Func
   216  	loggingLevel  int
   217  	convergeCount int // testing; iterate over block debug state this many times
   218  	registers     []Register
   219  	stackOffset   func(LocalSlot) int32
   220  	ctxt          *obj.Link
   221  
   222  	// The names (slots) associated with each value, indexed by Value ID.
   223  	valueNames [][]SlotID
   224  
   225  	// The current state of whatever analysis is running.
   226  	currentState stateAtPC
   227  	changedVars  *sparseSet
   228  	changedSlots *sparseSet
   229  
   230  	// The pending location list entry for each user variable, indexed by VarID.
   231  	pendingEntries []pendingEntry
   232  
   233  	varParts        map[*ir.Name][]SlotID
   234  	blockDebug      []BlockDebug
   235  	pendingSlotLocs []VarLoc
   236  }
   237  
   238  func (state *debugState) initializeCache(f *Func, numVars, numSlots int) {
   239  	// One blockDebug per block. Initialized in allocBlock.
   240  	if cap(state.blockDebug) < f.NumBlocks() {
   241  		state.blockDebug = make([]BlockDebug, f.NumBlocks())
   242  	} else {
   243  		clear(state.blockDebug[:f.NumBlocks()])
   244  	}
   245  
   246  	// A list of slots per Value. Reuse the previous child slices.
   247  	if cap(state.valueNames) < f.NumValues() {
   248  		old := state.valueNames
   249  		state.valueNames = make([][]SlotID, f.NumValues())
   250  		copy(state.valueNames, old)
   251  	}
   252  	vn := state.valueNames[:f.NumValues()]
   253  	for i := range vn {
   254  		vn[i] = vn[i][:0]
   255  	}
   256  
   257  	// Slot and register contents for currentState. Cleared by reset().
   258  	if cap(state.currentState.slots) < numSlots {
   259  		state.currentState.slots = make([]VarLoc, numSlots)
   260  	} else {
   261  		state.currentState.slots = state.currentState.slots[:numSlots]
   262  	}
   263  	if cap(state.currentState.registers) < len(state.registers) {
   264  		state.currentState.registers = make([][]SlotID, len(state.registers))
   265  	} else {
   266  		state.currentState.registers = state.currentState.registers[:len(state.registers)]
   267  	}
   268  
   269  	// A relatively small slice, but used many times as the return from processValue.
   270  	state.changedVars = newSparseSet(numVars)
   271  	state.changedSlots = newSparseSet(numSlots)
   272  
   273  	// A pending entry per user variable, with space to track each of its pieces.
   274  	numPieces := 0
   275  	for i := range state.varSlots {
   276  		numPieces += len(state.varSlots[i])
   277  	}
   278  	if cap(state.pendingSlotLocs) < numPieces {
   279  		state.pendingSlotLocs = make([]VarLoc, numPieces)
   280  	} else {
   281  		clear(state.pendingSlotLocs[:numPieces])
   282  	}
   283  	if cap(state.pendingEntries) < numVars {
   284  		state.pendingEntries = make([]pendingEntry, numVars)
   285  	}
   286  	pe := state.pendingEntries[:numVars]
   287  	freePieceIdx := 0
   288  	for varID, slots := range state.varSlots {
   289  		pe[varID] = pendingEntry{
   290  			pieces: state.pendingSlotLocs[freePieceIdx : freePieceIdx+len(slots)],
   291  		}
   292  		freePieceIdx += len(slots)
   293  	}
   294  	state.pendingEntries = pe
   295  
   296  	if cap(state.lists) < numVars {
   297  		state.lists = make([][]byte, numVars)
   298  	} else {
   299  		state.lists = state.lists[:numVars]
   300  		clear(state.lists)
   301  	}
   302  }
   303  
   304  func (state *debugState) allocBlock(b *Block) *BlockDebug {
   305  	return &state.blockDebug[b.ID]
   306  }
   307  
   308  func (s *debugState) blockEndStateString(b *BlockDebug) string {
   309  	endState := stateAtPC{slots: make([]VarLoc, len(s.slots)), registers: make([][]SlotID, len(s.registers))}
   310  	endState.reset(b.endState)
   311  	return s.stateString(endState)
   312  }
   313  
   314  func (s *debugState) stateString(state stateAtPC) string {
   315  	var strs []string
   316  	for slotID, loc := range state.slots {
   317  		if !loc.absent() {
   318  			strs = append(strs, fmt.Sprintf("\t%v = %v\n", s.slots[slotID], s.LocString(loc)))
   319  		}
   320  	}
   321  
   322  	strs = append(strs, "\n")
   323  	for reg, slots := range state.registers {
   324  		if len(slots) != 0 {
   325  			var slotStrs []string
   326  			for _, slot := range slots {
   327  				slotStrs = append(slotStrs, s.slots[slot].String())
   328  			}
   329  			strs = append(strs, fmt.Sprintf("\t%v = %v\n", &s.registers[reg], slotStrs))
   330  		}
   331  	}
   332  
   333  	if len(strs) == 1 {
   334  		return "(no vars)\n"
   335  	}
   336  	return strings.Join(strs, "")
   337  }
   338  
   339  // slotCanonicalizer is a table used to lookup and canonicalize
   340  // LocalSlot's in a type insensitive way (e.g. taking into account the
   341  // base name, offset, and width of the slot, but ignoring the slot
   342  // type).
   343  type slotCanonicalizer struct {
   344  	slmap  map[slotKey]SlKeyIdx
   345  	slkeys []LocalSlot
   346  }
   347  
   348  func newSlotCanonicalizer() *slotCanonicalizer {
   349  	return &slotCanonicalizer{
   350  		slmap:  make(map[slotKey]SlKeyIdx),
   351  		slkeys: []LocalSlot{LocalSlot{N: nil}},
   352  	}
   353  }
   354  
   355  type SlKeyIdx uint32
   356  
   357  const noSlot = SlKeyIdx(0)
   358  
   359  // slotKey is a type-insensitive encapsulation of a LocalSlot; it
   360  // is used to key a map within slotCanonicalizer.
   361  type slotKey struct {
   362  	name        *ir.Name
   363  	offset      int64
   364  	width       int64
   365  	splitOf     SlKeyIdx // idx in slkeys slice in slotCanonicalizer
   366  	splitOffset int64
   367  }
   368  
   369  // lookup looks up a LocalSlot in the slot canonicalizer "sc", returning
   370  // a canonical index for the slot, and adding it to the table if need
   371  // be. Return value is the canonical slot index, and a boolean indicating
   372  // whether the slot was found in the table already (TRUE => found).
   373  func (sc *slotCanonicalizer) lookup(ls LocalSlot) (SlKeyIdx, bool) {
   374  	split := noSlot
   375  	if ls.SplitOf != nil {
   376  		split, _ = sc.lookup(*ls.SplitOf)
   377  	}
   378  	k := slotKey{
   379  		name: ls.N, offset: ls.Off, width: ls.Type.Size(),
   380  		splitOf: split, splitOffset: ls.SplitOffset,
   381  	}
   382  	if idx, ok := sc.slmap[k]; ok {
   383  		return idx, true
   384  	}
   385  	rv := SlKeyIdx(len(sc.slkeys))
   386  	sc.slkeys = append(sc.slkeys, ls)
   387  	sc.slmap[k] = rv
   388  	return rv, false
   389  }
   390  
   391  func (sc *slotCanonicalizer) canonSlot(idx SlKeyIdx) LocalSlot {
   392  	return sc.slkeys[idx]
   393  }
   394  
   395  // PopulateABIInRegArgOps examines the entry block of the function
   396  // and looks for incoming parameters that have missing or partial
   397  // OpArg{Int,Float}Reg values, inserting additional values in
   398  // cases where they are missing. Example:
   399  //
   400  //	func foo(s string, used int, notused int) int {
   401  //	  return len(s) + used
   402  //	}
   403  //
   404  // In the function above, the incoming parameter "used" is fully live,
   405  // "notused" is not live, and "s" is partially live (only the length
   406  // field of the string is used). At the point where debug value
   407  // analysis runs, we might expect to see an entry block with:
   408  //
   409  //	b1:
   410  //	  v4 = ArgIntReg <uintptr> {s+8} [0] : BX
   411  //	  v5 = ArgIntReg <int> {used} [0] : CX
   412  //
   413  // While this is an accurate picture of the live incoming params,
   414  // we also want to have debug locations for non-live params (or
   415  // their non-live pieces), e.g. something like
   416  //
   417  //	b1:
   418  //	  v9 = ArgIntReg <*uint8> {s+0} [0] : AX
   419  //	  v4 = ArgIntReg <uintptr> {s+8} [0] : BX
   420  //	  v5 = ArgIntReg <int> {used} [0] : CX
   421  //	  v10 = ArgIntReg <int> {unused} [0] : DI
   422  //
   423  // This function examines the live OpArg{Int,Float}Reg values and
   424  // synthesizes new (dead) values for the non-live params or the
   425  // non-live pieces of partially live params.
   426  func PopulateABIInRegArgOps(f *Func) {
   427  	pri := f.ABISelf.ABIAnalyzeFuncType(f.Type)
   428  
   429  	// When manufacturing new slots that correspond to splits of
   430  	// composite parameters, we want to avoid creating a new sub-slot
   431  	// that differs from some existing sub-slot only by type, since
   432  	// the debug location analysis will treat that slot as a separate
   433  	// entity. To achieve this, create a lookup table of existing
   434  	// slots that is type-insenstitive.
   435  	sc := newSlotCanonicalizer()
   436  	for _, sl := range f.Names {
   437  		sc.lookup(*sl)
   438  	}
   439  
   440  	// Add slot -> value entry to f.NamedValues if not already present.
   441  	addToNV := func(v *Value, sl LocalSlot) {
   442  		values, ok := f.NamedValues[sl]
   443  		if !ok {
   444  			// Haven't seen this slot yet.
   445  			sla := f.localSlotAddr(sl)
   446  			f.Names = append(f.Names, sla)
   447  		} else {
   448  			for _, ev := range values {
   449  				if v == ev {
   450  					return
   451  				}
   452  			}
   453  		}
   454  		values = append(values, v)
   455  		f.NamedValues[sl] = values
   456  	}
   457  
   458  	newValues := []*Value{}
   459  
   460  	abiRegIndexToRegister := func(reg abi.RegIndex) int8 {
   461  		i := f.ABISelf.FloatIndexFor(reg)
   462  		if i >= 0 { // float PR
   463  			return f.Config.floatParamRegs[i]
   464  		} else {
   465  			return f.Config.intParamRegs[reg]
   466  		}
   467  	}
   468  
   469  	// Helper to construct a new OpArg{Float,Int}Reg op value.
   470  	var pos src.XPos
   471  	if len(f.Entry.Values) != 0 {
   472  		pos = f.Entry.Values[0].Pos
   473  	}
   474  	synthesizeOpIntFloatArg := func(n *ir.Name, t *types.Type, reg abi.RegIndex, sl LocalSlot) *Value {
   475  		aux := &AuxNameOffset{n, sl.Off}
   476  		op, auxInt := ArgOpAndRegisterFor(reg, f.ABISelf)
   477  		v := f.newValueNoBlock(op, t, pos)
   478  		v.AuxInt = auxInt
   479  		v.Aux = aux
   480  		v.Args = nil
   481  		v.Block = f.Entry
   482  		newValues = append(newValues, v)
   483  		addToNV(v, sl)
   484  		f.setHome(v, &f.Config.registers[abiRegIndexToRegister(reg)])
   485  		return v
   486  	}
   487  
   488  	// Make a pass through the entry block looking for
   489  	// OpArg{Int,Float}Reg ops. Record the slots they use in a table
   490  	// ("sc"). We use a type-insensitive lookup for the slot table,
   491  	// since the type we get from the ABI analyzer won't always match
   492  	// what the compiler uses when creating OpArg{Int,Float}Reg ops.
   493  	for _, v := range f.Entry.Values {
   494  		if v.Op == OpArgIntReg || v.Op == OpArgFloatReg {
   495  			aux := v.Aux.(*AuxNameOffset)
   496  			sl := LocalSlot{N: aux.Name, Type: v.Type, Off: aux.Offset}
   497  			// install slot in lookup table
   498  			idx, _ := sc.lookup(sl)
   499  			// add to f.NamedValues if not already present
   500  			addToNV(v, sc.canonSlot(idx))
   501  		} else if v.Op.IsCall() {
   502  			// if we hit a call, we've gone too far.
   503  			break
   504  		}
   505  	}
   506  
   507  	// Now make a pass through the ABI in-params, looking for params
   508  	// or pieces of params that we didn't encounter in the loop above.
   509  	for _, inp := range pri.InParams() {
   510  		if !isNamedRegParam(inp) {
   511  			continue
   512  		}
   513  		n := inp.Name
   514  
   515  		// Param is spread across one or more registers. Walk through
   516  		// each piece to see whether we've seen an arg reg op for it.
   517  		types, offsets := inp.RegisterTypesAndOffsets()
   518  		for k, t := range types {
   519  			// Note: this recipe for creating a LocalSlot is designed
   520  			// to be compatible with the one used in expand_calls.go
   521  			// as opposed to decompose.go. The expand calls code just
   522  			// takes the base name and creates an offset into it,
   523  			// without using the SplitOf/SplitOffset fields. The code
   524  			// in decompose.go does the opposite -- it creates a
   525  			// LocalSlot object with "Off" set to zero, but with
   526  			// SplitOf pointing to a parent slot, and SplitOffset
   527  			// holding the offset into the parent object.
   528  			pieceSlot := LocalSlot{N: n, Type: t, Off: offsets[k]}
   529  
   530  			// Look up this piece to see if we've seen a reg op
   531  			// for it. If not, create one.
   532  			_, found := sc.lookup(pieceSlot)
   533  			if !found {
   534  				// This slot doesn't appear in the map, meaning it
   535  				// corresponds to an in-param that is not live, or
   536  				// a portion of an in-param that is not live/used.
   537  				// Add a new dummy OpArg{Int,Float}Reg for it.
   538  				synthesizeOpIntFloatArg(n, t, inp.Registers[k],
   539  					pieceSlot)
   540  			}
   541  		}
   542  	}
   543  
   544  	// Insert the new values into the head of the block.
   545  	f.Entry.Values = append(newValues, f.Entry.Values...)
   546  }
   547  
   548  // BuildFuncDebug builds debug information for f, placing the results
   549  // in "rval". f must be fully processed, so that each Value is where it
   550  // will be when machine code is emitted.
   551  func BuildFuncDebug(ctxt *obj.Link, f *Func, loggingLevel int, stackOffset func(LocalSlot) int32, rval *FuncDebug) {
   552  	if f.RegAlloc == nil {
   553  		f.Fatalf("BuildFuncDebug on func %v that has not been fully processed", f)
   554  	}
   555  	state := &f.Cache.debugState
   556  	state.loggingLevel = loggingLevel % 1000
   557  
   558  	// A specific number demands exactly that many iterations. Under
   559  	// particular circumstances it make require more than the total of
   560  	// 2 passes implied by a single run through liveness and a single
   561  	// run through location list generation.
   562  	state.convergeCount = loggingLevel / 1000
   563  	state.f = f
   564  	state.registers = f.Config.registers
   565  	state.stackOffset = stackOffset
   566  	state.ctxt = ctxt
   567  
   568  	if buildcfg.Experiment.RegabiArgs {
   569  		PopulateABIInRegArgOps(f)
   570  	}
   571  
   572  	if state.loggingLevel > 0 {
   573  		state.logf("Generating location lists for function %q\n", f.Name)
   574  	}
   575  
   576  	if state.varParts == nil {
   577  		state.varParts = make(map[*ir.Name][]SlotID)
   578  	} else {
   579  		clear(state.varParts)
   580  	}
   581  
   582  	// Recompose any decomposed variables, and establish the canonical
   583  	// IDs for each var and slot by filling out state.vars and state.slots.
   584  
   585  	state.slots = state.slots[:0]
   586  	state.vars = state.vars[:0]
   587  	for i, slot := range f.Names {
   588  		state.slots = append(state.slots, *slot)
   589  		if ir.IsSynthetic(slot.N) || !IsVarWantedForDebug(slot.N) {
   590  			continue
   591  		}
   592  
   593  		topSlot := slot
   594  		for topSlot.SplitOf != nil {
   595  			topSlot = topSlot.SplitOf
   596  		}
   597  		if _, ok := state.varParts[topSlot.N]; !ok {
   598  			state.vars = append(state.vars, topSlot.N)
   599  		}
   600  		state.varParts[topSlot.N] = append(state.varParts[topSlot.N], SlotID(i))
   601  	}
   602  
   603  	// Recreate the LocalSlot for each stack-only variable.
   604  	// This would probably be better as an output from stackframe.
   605  	for _, b := range f.Blocks {
   606  		for _, v := range b.Values {
   607  			if v.Op == OpVarDef {
   608  				n := v.Aux.(*ir.Name)
   609  				if ir.IsSynthetic(n) || !IsVarWantedForDebug(n) {
   610  					continue
   611  				}
   612  
   613  				if _, ok := state.varParts[n]; !ok {
   614  					slot := LocalSlot{N: n, Type: v.Type, Off: 0}
   615  					state.slots = append(state.slots, slot)
   616  					state.varParts[n] = []SlotID{SlotID(len(state.slots) - 1)}
   617  					state.vars = append(state.vars, n)
   618  				}
   619  			}
   620  		}
   621  	}
   622  
   623  	// Fill in the var<->slot mappings.
   624  	if cap(state.varSlots) < len(state.vars) {
   625  		state.varSlots = make([][]SlotID, len(state.vars))
   626  	} else {
   627  		state.varSlots = state.varSlots[:len(state.vars)]
   628  		for i := range state.varSlots {
   629  			state.varSlots[i] = state.varSlots[i][:0]
   630  		}
   631  	}
   632  	if cap(state.slotVars) < len(state.slots) {
   633  		state.slotVars = make([]VarID, len(state.slots))
   634  	} else {
   635  		state.slotVars = state.slotVars[:len(state.slots)]
   636  	}
   637  
   638  	for varID, n := range state.vars {
   639  		parts := state.varParts[n]
   640  		slices.SortFunc(parts, func(a, b SlotID) int {
   641  			return cmp.Compare(varOffset(state.slots[a]), varOffset(state.slots[b]))
   642  		})
   643  
   644  		state.varSlots[varID] = parts
   645  		for _, slotID := range parts {
   646  			state.slotVars[slotID] = VarID(varID)
   647  		}
   648  	}
   649  
   650  	state.initializeCache(f, len(state.varParts), len(state.slots))
   651  
   652  	for i, slot := range f.Names {
   653  		if ir.IsSynthetic(slot.N) || !IsVarWantedForDebug(slot.N) {
   654  			continue
   655  		}
   656  		for _, value := range f.NamedValues[*slot] {
   657  			state.valueNames[value.ID] = append(state.valueNames[value.ID], SlotID(i))
   658  		}
   659  	}
   660  
   661  	blockLocs := state.liveness()
   662  	state.buildLocationLists(blockLocs)
   663  
   664  	// Populate "rval" with what we've computed.
   665  	rval.Slots = state.slots
   666  	rval.VarSlots = state.varSlots
   667  	rval.Vars = state.vars
   668  	rval.LocationLists = state.lists
   669  }
   670  
   671  // liveness walks the function in control flow order, calculating the start
   672  // and end state of each block.
   673  func (state *debugState) liveness() []*BlockDebug {
   674  	blockLocs := make([]*BlockDebug, state.f.NumBlocks())
   675  	counterTime := int32(1)
   676  
   677  	// Reverse postorder: visit a block after as many as possible of its
   678  	// predecessors have been visited.
   679  	po := state.f.Postorder()
   680  	converged := false
   681  
   682  	// The iteration rule is that by default, run until converged, but
   683  	// if a particular iteration count is specified, run that many
   684  	// iterations, no more, no less.  A count is specified as the
   685  	// thousands digit of the location lists debug flag,
   686  	// e.g. -d=locationlists=4000
   687  	keepGoing := func(k int) bool {
   688  		if state.convergeCount == 0 {
   689  			return !converged
   690  		}
   691  		return k < state.convergeCount
   692  	}
   693  	for k := 0; keepGoing(k); k++ {
   694  		if state.loggingLevel > 0 {
   695  			state.logf("Liveness pass %d\n", k)
   696  		}
   697  		converged = true
   698  		for i := len(po) - 1; i >= 0; i-- {
   699  			b := po[i]
   700  			locs := blockLocs[b.ID]
   701  			if locs == nil {
   702  				locs = state.allocBlock(b)
   703  				blockLocs[b.ID] = locs
   704  			}
   705  
   706  			// Build the starting state for the block from the final
   707  			// state of its predecessors.
   708  			startState, blockChanged := state.mergePredecessors(b, blockLocs, nil, false)
   709  			locs.lastCheckedTime = counterTime
   710  			counterTime++
   711  			if state.loggingLevel > 1 {
   712  				state.logf("Processing %v, block changed %v, initial state:\n%v", b, blockChanged, state.stateString(state.currentState))
   713  			}
   714  
   715  			if blockChanged {
   716  				// If the start did not change, then the old endState is good
   717  				converged = false
   718  				changed := false
   719  				state.changedSlots.clear()
   720  
   721  				// Update locs/registers with the effects of each Value.
   722  				for _, v := range b.Values {
   723  					slots := state.valueNames[v.ID]
   724  
   725  					// Loads and stores inherit the names of their sources.
   726  					var source *Value
   727  					switch v.Op {
   728  					case OpStoreReg:
   729  						source = v.Args[0]
   730  					case OpLoadReg:
   731  						switch a := v.Args[0]; a.Op {
   732  						case OpArg, OpPhi:
   733  							source = a
   734  						case OpStoreReg:
   735  							source = a.Args[0]
   736  						default:
   737  							if state.loggingLevel > 1 {
   738  								state.logf("at %v: load with unexpected source op: %v (%v)\n", v, a.Op, a)
   739  							}
   740  						}
   741  					}
   742  					// Update valueNames with the source so that later steps
   743  					// don't need special handling.
   744  					if source != nil && k == 0 {
   745  						// limit to k == 0 otherwise there are duplicates.
   746  						slots = append(slots, state.valueNames[source.ID]...)
   747  						state.valueNames[v.ID] = slots
   748  					}
   749  
   750  					reg, _ := state.f.getHome(v.ID).(*Register)
   751  					c := state.processValue(v, slots, reg)
   752  					changed = changed || c
   753  				}
   754  
   755  				if state.loggingLevel > 1 {
   756  					state.logf("Block %v done, locs:\n%v", b, state.stateString(state.currentState))
   757  				}
   758  
   759  				locs.relevant = locs.relevant || changed
   760  				if !changed {
   761  					locs.endState = startState
   762  				} else {
   763  					for _, id := range state.changedSlots.contents() {
   764  						slotID := SlotID(id)
   765  						slotLoc := state.currentState.slots[slotID]
   766  						if slotLoc.absent() {
   767  							startState.Delete(int32(slotID))
   768  							continue
   769  						}
   770  						old := startState.Find(int32(slotID)) // do NOT replace existing values
   771  						if oldLS, ok := old.(*liveSlot); !ok || oldLS.VarLoc != slotLoc {
   772  							startState.Insert(int32(slotID),
   773  								&liveSlot{VarLoc: slotLoc})
   774  						}
   775  					}
   776  					locs.endState = startState
   777  				}
   778  				locs.lastChangedTime = counterTime
   779  			}
   780  			counterTime++
   781  		}
   782  	}
   783  	return blockLocs
   784  }
   785  
   786  // mergePredecessors takes the end state of each of b's predecessors and
   787  // intersects them to form the starting state for b. It puts that state
   788  // in blockLocs[b.ID].startState, and fills state.currentState with it.
   789  // It returns the start state and whether this is changed from the
   790  // previously approximated value of startState for this block.  After
   791  // the first call, subsequent calls can only shrink startState.
   792  //
   793  // Passing forLocationLists=true enables additional side-effects that
   794  // are necessary for building location lists but superfluous while still
   795  // iterating to an answer.
   796  //
   797  // If previousBlock is non-nil, it registers changes vs. that block's
   798  // end state in state.changedVars. Note that previousBlock will often
   799  // not be a predecessor.
   800  //
   801  // Note that mergePredecessors behaves slightly differently between
   802  // first and subsequent calls for a block.  For the first call, the
   803  // starting state is approximated by taking the state from the
   804  // predecessor whose state is smallest, and removing any elements not
   805  // in all the other predecessors; this makes the smallest number of
   806  // changes and shares the most state.  On subsequent calls the old
   807  // value of startState is adjusted with new information; this is judged
   808  // to do the least amount of extra work.
   809  //
   810  // To improve performance, each block's state information is marked with
   811  // lastChanged and lastChecked "times" so unchanged predecessors can be
   812  // skipped on after-the-first iterations.  Doing this allows extra
   813  // iterations by the caller to be almost free.
   814  //
   815  // It is important to know that the set representation used for
   816  // startState, endState, and merges can share data for two sets where
   817  // one is a small delta from the other.  Doing this does require a
   818  // little care in how sets are updated, both in mergePredecessors, and
   819  // using its result.
   820  func (state *debugState) mergePredecessors(b *Block, blockLocs []*BlockDebug, previousBlock *Block, forLocationLists bool) (abt.T, bool) {
   821  	// Filter out back branches.
   822  	var predsBuf [10]*Block
   823  
   824  	preds := predsBuf[:0]
   825  	locs := blockLocs[b.ID]
   826  
   827  	blockChanged := !locs.everProcessed // the first time it always changes.
   828  	updating := locs.everProcessed
   829  
   830  	// For the first merge, exclude predecessors that have not been seen yet.
   831  	// I.e., backedges.
   832  	for _, pred := range b.Preds {
   833  		if bl := blockLocs[pred.b.ID]; bl != nil && bl.everProcessed {
   834  			// crucially, a self-edge has bl != nil, but bl.everProcessed is false the first time.
   835  			preds = append(preds, pred.b)
   836  		}
   837  	}
   838  
   839  	locs.everProcessed = true
   840  
   841  	if state.loggingLevel > 1 {
   842  		// The logf below would cause preds to be heap-allocated if
   843  		// it were passed directly.
   844  		preds2 := make([]*Block, len(preds))
   845  		copy(preds2, preds)
   846  		state.logf("Merging %v into %v (changed=%d, checked=%d)\n", preds2, b, locs.lastChangedTime, locs.lastCheckedTime)
   847  	}
   848  
   849  	state.changedVars.clear()
   850  
   851  	markChangedVars := func(slots, merged abt.T) {
   852  		if !forLocationLists {
   853  			return
   854  		}
   855  		// Fill changedVars with those that differ between the previous
   856  		// block (in the emit order, not necessarily a flow predecessor)
   857  		// and the start state for this block.
   858  		for it := slots.Iterator(); !it.Done(); {
   859  			k, v := it.Next()
   860  			m := merged.Find(k)
   861  			if m == nil || v.(*liveSlot).VarLoc != m.(*liveSlot).VarLoc {
   862  				state.changedVars.add(ID(state.slotVars[k]))
   863  			}
   864  		}
   865  	}
   866  
   867  	reset := func(ourStartState abt.T) {
   868  		if !(forLocationLists || blockChanged) {
   869  			// there is no change and this is not for location lists, do
   870  			// not bother to reset currentState because it will not be
   871  			// examined.
   872  			return
   873  		}
   874  		state.currentState.reset(ourStartState)
   875  	}
   876  
   877  	// Zero predecessors
   878  	if len(preds) == 0 {
   879  		if previousBlock != nil {
   880  			state.f.Fatalf("Function %v, block %s with no predecessors is not first block, has previous %s", state.f, b.String(), previousBlock.String())
   881  		}
   882  		// startState is empty
   883  		reset(abt.T{})
   884  		return abt.T{}, blockChanged
   885  	}
   886  
   887  	// One predecessor
   888  	l0 := blockLocs[preds[0].ID]
   889  	p0 := l0.endState
   890  	if len(preds) == 1 {
   891  		if previousBlock != nil && preds[0].ID != previousBlock.ID {
   892  			// Change from previous block is its endState minus the predecessor's endState
   893  			markChangedVars(blockLocs[previousBlock.ID].endState, p0)
   894  		}
   895  		locs.startState = p0
   896  		blockChanged = blockChanged || l0.lastChangedTime > locs.lastCheckedTime
   897  		reset(p0)
   898  		return p0, blockChanged
   899  	}
   900  
   901  	// More than one predecessor
   902  
   903  	if updating {
   904  		// After the first approximation, i.e., when updating, results
   905  		// can only get smaller, because initially backedge
   906  		// predecessors do not participate in the intersection.  This
   907  		// means that for the update, given the prior approximation of
   908  		// startState, there is no need to re-intersect with unchanged
   909  		// blocks.  Therefore remove unchanged blocks from the
   910  		// predecessor list.
   911  		for i := len(preds) - 1; i >= 0; i-- {
   912  			pred := preds[i]
   913  			if blockLocs[pred.ID].lastChangedTime > locs.lastCheckedTime {
   914  				continue // keep this predecessor
   915  			}
   916  			preds[i] = preds[len(preds)-1]
   917  			preds = preds[:len(preds)-1]
   918  			if state.loggingLevel > 2 {
   919  				state.logf("Pruned b%d, lastChanged was %d but b%d lastChecked is %d\n", pred.ID, blockLocs[pred.ID].lastChangedTime, b.ID, locs.lastCheckedTime)
   920  			}
   921  		}
   922  		// Check for an early out; this should always hit for the update
   923  		// if there are no cycles.
   924  		if len(preds) == 0 {
   925  			blockChanged = false
   926  
   927  			reset(locs.startState)
   928  			if state.loggingLevel > 2 {
   929  				state.logf("Early out, no predecessors changed since last check\n")
   930  			}
   931  			if previousBlock != nil {
   932  				markChangedVars(blockLocs[previousBlock.ID].endState, locs.startState)
   933  			}
   934  			return locs.startState, blockChanged
   935  		}
   936  	}
   937  
   938  	baseID := preds[0].ID
   939  	baseState := p0
   940  
   941  	// Choose the predecessor with the smallest endState for intersection work
   942  	for _, pred := range preds[1:] {
   943  		if blockLocs[pred.ID].endState.Size() < baseState.Size() {
   944  			baseState = blockLocs[pred.ID].endState
   945  			baseID = pred.ID
   946  		}
   947  	}
   948  
   949  	if state.loggingLevel > 2 {
   950  		state.logf("Starting %v with state from b%v:\n%v", b, baseID, state.blockEndStateString(blockLocs[baseID]))
   951  		for _, pred := range preds {
   952  			if pred.ID == baseID {
   953  				continue
   954  			}
   955  			state.logf("Merging in state from %v:\n%v", pred, state.blockEndStateString(blockLocs[pred.ID]))
   956  		}
   957  	}
   958  
   959  	state.currentState.reset(abt.T{})
   960  	// The normal logic of "reset" is included in the intersection loop below.
   961  
   962  	slotLocs := state.currentState.slots
   963  
   964  	// If this is the first call, do updates on the "baseState"; if this
   965  	// is a subsequent call, tweak the startState instead. Note that
   966  	// these "set" values are values; there are no side effects to
   967  	// other values as these are modified.
   968  	newState := baseState
   969  	if updating {
   970  		newState = blockLocs[b.ID].startState
   971  	}
   972  
   973  	for it := newState.Iterator(); !it.Done(); {
   974  		k, d := it.Next()
   975  		thisSlot := d.(*liveSlot)
   976  		x := thisSlot.VarLoc
   977  		x0 := x // initial value in newState
   978  
   979  		// Intersect this slot with the slot in all the predecessors
   980  		for _, other := range preds {
   981  			if !updating && other.ID == baseID {
   982  				continue
   983  			}
   984  			otherSlot := blockLocs[other.ID].endState.Find(k)
   985  			if otherSlot == nil {
   986  				x = VarLoc{}
   987  				break
   988  			}
   989  			y := otherSlot.(*liveSlot).VarLoc
   990  			x = x.intersect(y)
   991  			if x.absent() {
   992  				x = VarLoc{}
   993  				break
   994  			}
   995  		}
   996  
   997  		// Delete if necessary, but not otherwise (in order to maximize sharing).
   998  		if x.absent() {
   999  			if !x0.absent() {
  1000  				blockChanged = true
  1001  				newState.Delete(k)
  1002  			}
  1003  			slotLocs[k] = VarLoc{}
  1004  			continue
  1005  		}
  1006  		if x != x0 {
  1007  			blockChanged = true
  1008  			newState.Insert(k, &liveSlot{VarLoc: x})
  1009  		}
  1010  
  1011  		slotLocs[k] = x
  1012  		mask := uint64(x.Registers)
  1013  		for {
  1014  			if mask == 0 {
  1015  				break
  1016  			}
  1017  			reg := uint8(bits.TrailingZeros64(mask))
  1018  			mask &^= 1 << reg
  1019  			state.currentState.registers[reg] = append(state.currentState.registers[reg], SlotID(k))
  1020  		}
  1021  	}
  1022  
  1023  	if previousBlock != nil {
  1024  		markChangedVars(blockLocs[previousBlock.ID].endState, newState)
  1025  	}
  1026  	locs.startState = newState
  1027  	return newState, blockChanged
  1028  }
  1029  
  1030  // processValue updates locs and state.registerContents to reflect v, a
  1031  // value with the names in vSlots and homed in vReg.  "v" becomes
  1032  // visible after execution of the instructions evaluating it. It
  1033  // returns which VarIDs were modified by the Value's execution.
  1034  func (state *debugState) processValue(v *Value, vSlots []SlotID, vReg *Register) bool {
  1035  	locs := state.currentState
  1036  	changed := false
  1037  	setSlot := func(slot SlotID, loc VarLoc) {
  1038  		changed = true
  1039  		state.changedVars.add(ID(state.slotVars[slot]))
  1040  		state.changedSlots.add(ID(slot))
  1041  		state.currentState.slots[slot] = loc
  1042  	}
  1043  
  1044  	// Handle any register clobbering. Call operations, for example,
  1045  	// clobber all registers even though they don't explicitly write to
  1046  	// them.
  1047  	clobbers := uint64(opcodeTable[v.Op].reg.clobbers)
  1048  	for {
  1049  		if clobbers == 0 {
  1050  			break
  1051  		}
  1052  		reg := uint8(bits.TrailingZeros64(clobbers))
  1053  		clobbers &^= 1 << reg
  1054  
  1055  		for _, slot := range locs.registers[reg] {
  1056  			if state.loggingLevel > 1 {
  1057  				state.logf("at %v: %v clobbered out of %v\n", v, state.slots[slot], &state.registers[reg])
  1058  			}
  1059  
  1060  			last := locs.slots[slot]
  1061  			if last.absent() {
  1062  				state.f.Fatalf("at %v: slot %v in register %v with no location entry", v, state.slots[slot], &state.registers[reg])
  1063  				continue
  1064  			}
  1065  			regs := last.Registers &^ (1 << reg)
  1066  			setSlot(slot, VarLoc{regs, last.StackOffset})
  1067  		}
  1068  
  1069  		locs.registers[reg] = locs.registers[reg][:0]
  1070  	}
  1071  
  1072  	switch {
  1073  	case v.Op == OpVarDef:
  1074  		n := v.Aux.(*ir.Name)
  1075  		if ir.IsSynthetic(n) || !IsVarWantedForDebug(n) {
  1076  			break
  1077  		}
  1078  
  1079  		slotID := state.varParts[n][0]
  1080  		var stackOffset StackOffset
  1081  		if v.Op == OpVarDef {
  1082  			stackOffset = StackOffset(state.stackOffset(state.slots[slotID])<<1 | 1)
  1083  		}
  1084  		setSlot(slotID, VarLoc{0, stackOffset})
  1085  		if state.loggingLevel > 1 {
  1086  			if v.Op == OpVarDef {
  1087  				state.logf("at %v: stack-only var %v now live\n", v, state.slots[slotID])
  1088  			} else {
  1089  				state.logf("at %v: stack-only var %v now dead\n", v, state.slots[slotID])
  1090  			}
  1091  		}
  1092  
  1093  	case v.Op == OpArg:
  1094  		home := state.f.getHome(v.ID).(LocalSlot)
  1095  		stackOffset := state.stackOffset(home)<<1 | 1
  1096  		for _, slot := range vSlots {
  1097  			if state.loggingLevel > 1 {
  1098  				state.logf("at %v: arg %v now on stack in location %v\n", v, state.slots[slot], home)
  1099  				if last := locs.slots[slot]; !last.absent() {
  1100  					state.logf("at %v: unexpected arg op on already-live slot %v\n", v, state.slots[slot])
  1101  				}
  1102  			}
  1103  
  1104  			setSlot(slot, VarLoc{0, StackOffset(stackOffset)})
  1105  		}
  1106  
  1107  	case v.Op == OpStoreReg:
  1108  		home := state.f.getHome(v.ID).(LocalSlot)
  1109  		stackOffset := state.stackOffset(home)<<1 | 1
  1110  		for _, slot := range vSlots {
  1111  			last := locs.slots[slot]
  1112  			if last.absent() {
  1113  				if state.loggingLevel > 1 {
  1114  					state.logf("at %v: unexpected spill of unnamed register %s\n", v, vReg)
  1115  				}
  1116  				break
  1117  			}
  1118  
  1119  			setSlot(slot, VarLoc{last.Registers, StackOffset(stackOffset)})
  1120  			if state.loggingLevel > 1 {
  1121  				state.logf("at %v: %v spilled to stack location %v@%d\n", v, state.slots[slot], home, state.stackOffset(home))
  1122  			}
  1123  		}
  1124  
  1125  	case vReg != nil:
  1126  		if state.loggingLevel > 1 {
  1127  			newSlots := make([]bool, len(state.slots))
  1128  			for _, slot := range vSlots {
  1129  				newSlots[slot] = true
  1130  			}
  1131  
  1132  			for _, slot := range locs.registers[vReg.num] {
  1133  				if !newSlots[slot] {
  1134  					state.logf("at %v: overwrote %v in register %v\n", v, state.slots[slot], vReg)
  1135  				}
  1136  			}
  1137  		}
  1138  
  1139  		for _, slot := range locs.registers[vReg.num] {
  1140  			last := locs.slots[slot]
  1141  			setSlot(slot, VarLoc{last.Registers &^ (1 << uint8(vReg.num)), last.StackOffset})
  1142  		}
  1143  		locs.registers[vReg.num] = locs.registers[vReg.num][:0]
  1144  		locs.registers[vReg.num] = append(locs.registers[vReg.num], vSlots...)
  1145  		for _, slot := range vSlots {
  1146  			if state.loggingLevel > 1 {
  1147  				state.logf("at %v: %v now in %s\n", v, state.slots[slot], vReg)
  1148  			}
  1149  
  1150  			last := locs.slots[slot]
  1151  			setSlot(slot, VarLoc{1<<uint8(vReg.num) | last.Registers, last.StackOffset})
  1152  		}
  1153  	}
  1154  	return changed
  1155  }
  1156  
  1157  // varOffset returns the offset of slot within the user variable it was
  1158  // decomposed from. This has nothing to do with its stack offset.
  1159  func varOffset(slot LocalSlot) int64 {
  1160  	offset := slot.Off
  1161  	s := &slot
  1162  	for ; s.SplitOf != nil; s = s.SplitOf {
  1163  		offset += s.SplitOffset
  1164  	}
  1165  	return offset
  1166  }
  1167  
  1168  // A pendingEntry represents the beginning of a location list entry, missing
  1169  // only its end coordinate.
  1170  type pendingEntry struct {
  1171  	present                bool
  1172  	startBlock, startValue ID
  1173  	// The location of each piece of the variable, in the same order as the
  1174  	// SlotIDs in varParts.
  1175  	pieces []VarLoc
  1176  }
  1177  
  1178  func (e *pendingEntry) clear() {
  1179  	e.present = false
  1180  	e.startBlock = 0
  1181  	e.startValue = 0
  1182  	clear(e.pieces)
  1183  }
  1184  
  1185  // canMerge reports whether a new location description is a superset
  1186  // of the (non-empty) pending location description, if so, the two
  1187  // can be merged (i.e., pending is still a valid and useful location
  1188  // description).
  1189  func canMerge(pending, new VarLoc) bool {
  1190  	if pending.absent() && new.absent() {
  1191  		return true
  1192  	}
  1193  	if pending.absent() || new.absent() {
  1194  		return false
  1195  	}
  1196  	// pending is not absent, therefore it has either a stack mapping,
  1197  	// or registers, or both.
  1198  	if pending.onStack() && pending.StackOffset != new.StackOffset {
  1199  		// if pending has a stack offset, then new must also, and it
  1200  		// must be the same (StackOffset encodes onStack).
  1201  		return false
  1202  	}
  1203  	if pending.Registers&new.Registers != pending.Registers {
  1204  		// There is at least one register in pending not mentioned in new.
  1205  		return false
  1206  	}
  1207  	return true
  1208  }
  1209  
  1210  // firstReg returns the first register in set that is present.
  1211  func firstReg(set RegisterSet) uint8 {
  1212  	if set == 0 {
  1213  		// This is wrong, but there seem to be some situations where we
  1214  		// produce locations with no storage.
  1215  		return 0
  1216  	}
  1217  	return uint8(bits.TrailingZeros64(uint64(set)))
  1218  }
  1219  
  1220  // buildLocationLists builds location lists for all the user variables
  1221  // in state.f, using the information about block state in blockLocs.
  1222  // The returned location lists are not fully complete. They are in
  1223  // terms of SSA values rather than PCs, and have no base address/end
  1224  // entries. They will be finished by PutLocationList.
  1225  func (state *debugState) buildLocationLists(blockLocs []*BlockDebug) {
  1226  	// Run through the function in program text order, building up location
  1227  	// lists as we go. The heavy lifting has mostly already been done.
  1228  
  1229  	var prevBlock *Block
  1230  	for _, b := range state.f.Blocks {
  1231  		state.mergePredecessors(b, blockLocs, prevBlock, true)
  1232  
  1233  		// Handle any differences among predecessor blocks and previous block (perhaps not a predecessor)
  1234  		for _, varID := range state.changedVars.contents() {
  1235  			state.updateVar(VarID(varID), b, BlockStart)
  1236  		}
  1237  		state.changedVars.clear()
  1238  
  1239  		if !blockLocs[b.ID].relevant {
  1240  			continue
  1241  		}
  1242  
  1243  		mustBeFirst := func(v *Value) bool {
  1244  			return v.Op == OpPhi || v.Op.isLoweredGetClosurePtr() ||
  1245  				v.Op == OpArgIntReg || v.Op == OpArgFloatReg
  1246  		}
  1247  
  1248  		blockPrologComplete := func(v *Value) bool {
  1249  			if b.ID != state.f.Entry.ID {
  1250  				return !opcodeTable[v.Op].zeroWidth
  1251  			} else {
  1252  				return v.Op == OpInitMem
  1253  			}
  1254  		}
  1255  
  1256  		// Examine the prolog portion of the block to process special
  1257  		// zero-width ops such as Arg, Phi, LoweredGetClosurePtr (etc)
  1258  		// whose lifetimes begin at the block starting point. In an
  1259  		// entry block, allow for the possibility that we may see Arg
  1260  		// ops that appear _after_ other non-zero-width operations.
  1261  		// Example:
  1262  		//
  1263  		//   v33 = ArgIntReg <uintptr> {foo+0} [0] : AX (foo)
  1264  		//   v34 = ArgIntReg <uintptr> {bar+0} [0] : BX (bar)
  1265  		//   ...
  1266  		//   v77 = StoreReg <unsafe.Pointer> v67 : ctx+8[unsafe.Pointer]
  1267  		//   v78 = StoreReg <unsafe.Pointer> v68 : ctx[unsafe.Pointer]
  1268  		//   v79 = Arg <*uint8> {args} : args[*uint8] (args[*uint8])
  1269  		//   v80 = Arg <int> {args} [8] : args+8[int] (args+8[int])
  1270  		//   ...
  1271  		//   v1 = InitMem <mem>
  1272  		//
  1273  		// We can stop scanning the initial portion of the block when
  1274  		// we either see the InitMem op (for entry blocks) or the
  1275  		// first non-zero-width op (for other blocks).
  1276  		for idx := 0; idx < len(b.Values); idx++ {
  1277  			v := b.Values[idx]
  1278  			if blockPrologComplete(v) {
  1279  				break
  1280  			}
  1281  			// Consider only "lifetime begins at block start" ops.
  1282  			if !mustBeFirst(v) && v.Op != OpArg {
  1283  				continue
  1284  			}
  1285  			slots := state.valueNames[v.ID]
  1286  			reg, _ := state.f.getHome(v.ID).(*Register)
  1287  			changed := state.processValue(v, slots, reg) // changed == added to state.changedVars
  1288  			if changed {
  1289  				for _, varID := range state.changedVars.contents() {
  1290  					state.updateVar(VarID(varID), v.Block, BlockStart)
  1291  				}
  1292  				state.changedVars.clear()
  1293  			}
  1294  		}
  1295  
  1296  		// Now examine the block again, handling things other than the
  1297  		// "begins at block start" lifetimes.
  1298  		zeroWidthPending := false
  1299  		prologComplete := false
  1300  		// expect to see values in pattern (apc)* (zerowidth|real)*
  1301  		for _, v := range b.Values {
  1302  			if blockPrologComplete(v) {
  1303  				prologComplete = true
  1304  			}
  1305  			slots := state.valueNames[v.ID]
  1306  			reg, _ := state.f.getHome(v.ID).(*Register)
  1307  			changed := state.processValue(v, slots, reg) // changed == added to state.changedVars
  1308  
  1309  			if opcodeTable[v.Op].zeroWidth {
  1310  				if prologComplete && mustBeFirst(v) {
  1311  					panic(fmt.Errorf("Unexpected placement of op '%s' appearing after non-pseudo-op at beginning of block %s in %s\n%s", v.LongString(), b, b.Func.Name, b.Func))
  1312  				}
  1313  				if changed {
  1314  					if mustBeFirst(v) || v.Op == OpArg {
  1315  						// already taken care of above
  1316  						continue
  1317  					}
  1318  					zeroWidthPending = true
  1319  				}
  1320  				continue
  1321  			}
  1322  			if !changed && !zeroWidthPending {
  1323  				continue
  1324  			}
  1325  
  1326  			// Not zero-width; i.e., a "real" instruction.
  1327  			zeroWidthPending = false
  1328  			for _, varID := range state.changedVars.contents() {
  1329  				state.updateVar(VarID(varID), v.Block, v)
  1330  			}
  1331  			state.changedVars.clear()
  1332  		}
  1333  		for _, varID := range state.changedVars.contents() {
  1334  			state.updateVar(VarID(varID), b, BlockEnd)
  1335  		}
  1336  
  1337  		prevBlock = b
  1338  	}
  1339  
  1340  	if state.loggingLevel > 0 {
  1341  		state.logf("location lists:\n")
  1342  	}
  1343  
  1344  	// Flush any leftover entries live at the end of the last block.
  1345  	for varID := range state.lists {
  1346  		state.writePendingEntry(VarID(varID), -1, FuncEnd.ID)
  1347  		list := state.lists[varID]
  1348  		if state.loggingLevel > 0 {
  1349  			if len(list) == 0 {
  1350  				state.logf("\t%v : empty list\n", state.vars[varID])
  1351  			} else {
  1352  				state.logf("\t%v : %q\n", state.vars[varID], hex.EncodeToString(state.lists[varID]))
  1353  			}
  1354  		}
  1355  	}
  1356  }
  1357  
  1358  // updateVar updates the pending location list entry for varID to
  1359  // reflect the new locations in curLoc, beginning at v in block b.
  1360  // v may be one of the special values indicating block start or end.
  1361  func (state *debugState) updateVar(varID VarID, b *Block, v *Value) {
  1362  	curLoc := state.currentState.slots
  1363  	// Assemble the location list entry with whatever's live.
  1364  	empty := true
  1365  	for _, slotID := range state.varSlots[varID] {
  1366  		if !curLoc[slotID].absent() {
  1367  			empty = false
  1368  			break
  1369  		}
  1370  	}
  1371  	pending := &state.pendingEntries[varID]
  1372  	if empty {
  1373  		state.writePendingEntry(varID, b.ID, v.ID)
  1374  		pending.clear()
  1375  		return
  1376  	}
  1377  
  1378  	// Extend the previous entry if possible.
  1379  	if pending.present {
  1380  		merge := true
  1381  		for i, slotID := range state.varSlots[varID] {
  1382  			if !canMerge(pending.pieces[i], curLoc[slotID]) {
  1383  				merge = false
  1384  				break
  1385  			}
  1386  		}
  1387  		if merge {
  1388  			return
  1389  		}
  1390  	}
  1391  
  1392  	state.writePendingEntry(varID, b.ID, v.ID)
  1393  	pending.present = true
  1394  	pending.startBlock = b.ID
  1395  	pending.startValue = v.ID
  1396  	for i, slot := range state.varSlots[varID] {
  1397  		pending.pieces[i] = curLoc[slot]
  1398  	}
  1399  }
  1400  
  1401  // writePendingEntry writes out the pending entry for varID, if any,
  1402  // terminated at endBlock/Value.
  1403  func (state *debugState) writePendingEntry(varID VarID, endBlock, endValue ID) {
  1404  	pending := state.pendingEntries[varID]
  1405  	if !pending.present {
  1406  		return
  1407  	}
  1408  
  1409  	// Pack the start/end coordinates into the start/end addresses
  1410  	// of the entry, for decoding by PutLocationList.
  1411  	start, startOK := encodeValue(state.ctxt, pending.startBlock, pending.startValue)
  1412  	end, endOK := encodeValue(state.ctxt, endBlock, endValue)
  1413  	if !startOK || !endOK {
  1414  		// If someone writes a function that uses >65K values,
  1415  		// they get incomplete debug info on 32-bit platforms.
  1416  		return
  1417  	}
  1418  	if start == end {
  1419  		if state.loggingLevel > 1 {
  1420  			// Printf not logf so not gated by GOSSAFUNC; this should fire very rarely.
  1421  			// TODO this fires a lot, need to figure out why.
  1422  			state.logf("Skipping empty location list for %v in %s\n", state.vars[varID], state.f.Name)
  1423  		}
  1424  		return
  1425  	}
  1426  
  1427  	list := state.lists[varID]
  1428  	list = appendPtr(state.ctxt, list, start)
  1429  	list = appendPtr(state.ctxt, list, end)
  1430  	// Where to write the length of the location description once
  1431  	// we know how big it is.
  1432  	sizeIdx := len(list)
  1433  	list = list[:len(list)+2]
  1434  
  1435  	if state.loggingLevel > 1 {
  1436  		var partStrs []string
  1437  		for i, slot := range state.varSlots[varID] {
  1438  			partStrs = append(partStrs, fmt.Sprintf("%v@%v", state.slots[slot], state.LocString(pending.pieces[i])))
  1439  		}
  1440  		state.logf("Add entry for %v: \tb%vv%v-b%vv%v = \t%v\n", state.vars[varID], pending.startBlock, pending.startValue, endBlock, endValue, strings.Join(partStrs, " "))
  1441  	}
  1442  
  1443  	for i, slotID := range state.varSlots[varID] {
  1444  		loc := pending.pieces[i]
  1445  		slot := state.slots[slotID]
  1446  
  1447  		if !loc.absent() {
  1448  			if loc.onStack() {
  1449  				if loc.stackOffsetValue() == 0 {
  1450  					list = append(list, dwarf.DW_OP_call_frame_cfa)
  1451  				} else {
  1452  					list = append(list, dwarf.DW_OP_fbreg)
  1453  					list = dwarf.AppendSleb128(list, int64(loc.stackOffsetValue()))
  1454  				}
  1455  			} else {
  1456  				regnum := state.ctxt.Arch.DWARFRegisters[state.registers[firstReg(loc.Registers)].ObjNum()]
  1457  				if regnum < 32 {
  1458  					list = append(list, dwarf.DW_OP_reg0+byte(regnum))
  1459  				} else {
  1460  					list = append(list, dwarf.DW_OP_regx)
  1461  					list = dwarf.AppendUleb128(list, uint64(regnum))
  1462  				}
  1463  			}
  1464  		}
  1465  
  1466  		if len(state.varSlots[varID]) > 1 {
  1467  			list = append(list, dwarf.DW_OP_piece)
  1468  			list = dwarf.AppendUleb128(list, uint64(slot.Type.Size()))
  1469  		}
  1470  	}
  1471  	state.ctxt.Arch.ByteOrder.PutUint16(list[sizeIdx:], uint16(len(list)-sizeIdx-2))
  1472  	state.lists[varID] = list
  1473  }
  1474  
  1475  // PutLocationList adds list (a location list in its intermediate
  1476  // representation) to listSym.
  1477  func (debugInfo *FuncDebug) PutLocationList(list []byte, ctxt *obj.Link, listSym, startPC *obj.LSym) {
  1478  	if buildcfg.Experiment.Dwarf5 {
  1479  		debugInfo.PutLocationListDwarf5(list, ctxt, listSym, startPC)
  1480  	} else {
  1481  		debugInfo.PutLocationListDwarf4(list, ctxt, listSym, startPC)
  1482  	}
  1483  }
  1484  
  1485  // PutLocationListDwarf5 adds list (a location list in its intermediate
  1486  // representation) to listSym in DWARF 5 format. NB: this is a somewhat
  1487  // hacky implementation in that it actually reads a DWARF4 encoded
  1488  // info from list (with all its DWARF4-specific quirks) then re-encodes
  1489  // it in DWARF5. It would probably be better at some point to have
  1490  // ssa/debug encode the list in a version-independent form and then
  1491  // have this func (and PutLocationListDwarf4) intoduce the quirks.
  1492  func (debugInfo *FuncDebug) PutLocationListDwarf5(list []byte, ctxt *obj.Link, listSym, startPC *obj.LSym) {
  1493  	getPC := debugInfo.GetPC
  1494  
  1495  	// base address entry
  1496  	listSym.WriteInt(ctxt, listSym.Size, 1, dwarf.DW_LLE_base_addressx)
  1497  	listSym.WriteDwTxtAddrx(ctxt, listSym.Size, startPC, ctxt.DwTextCount*2)
  1498  
  1499  	var stbuf, enbuf [10]byte
  1500  	stb, enb := stbuf[:], enbuf[:]
  1501  	// Re-read list, translating its address from block/value ID to PC.
  1502  	for i := 0; i < len(list); {
  1503  		begin := getPC(decodeValue(ctxt, readPtr(ctxt, list[i:])))
  1504  		end := getPC(decodeValue(ctxt, readPtr(ctxt, list[i+ctxt.Arch.PtrSize:])))
  1505  
  1506  		// Write LLE_offset_pair tag followed by payload (ULEB for start
  1507  		// and then end).
  1508  		listSym.WriteInt(ctxt, listSym.Size, 1, dwarf.DW_LLE_offset_pair)
  1509  		stb, enb = stb[:0], enb[:0]
  1510  		stb = dwarf.AppendUleb128(stb, uint64(begin))
  1511  		enb = dwarf.AppendUleb128(enb, uint64(end))
  1512  		listSym.WriteBytes(ctxt, listSym.Size, stb)
  1513  		listSym.WriteBytes(ctxt, listSym.Size, enb)
  1514  
  1515  		// The encoded data in "list" is in DWARF4 format, which uses
  1516  		// a 2-byte length; DWARF5 uses an LEB-encoded value for this
  1517  		// length. Read the length and then re-encode it.
  1518  		i += 2 * ctxt.Arch.PtrSize
  1519  		datalen := int(ctxt.Arch.ByteOrder.Uint16(list[i:]))
  1520  		i += 2
  1521  		stb = stb[:0]
  1522  		stb = dwarf.AppendUleb128(stb, uint64(datalen))
  1523  		listSym.WriteBytes(ctxt, listSym.Size, stb)               // copy length
  1524  		listSym.WriteBytes(ctxt, listSym.Size, list[i:i+datalen]) // loc desc
  1525  
  1526  		i += datalen
  1527  	}
  1528  
  1529  	// Terminator
  1530  	listSym.WriteInt(ctxt, listSym.Size, 1, dwarf.DW_LLE_end_of_list)
  1531  }
  1532  
  1533  // PutLocationListDwarf4 adds list (a location list in its intermediate
  1534  // representation) to listSym in DWARF 4 format.
  1535  func (debugInfo *FuncDebug) PutLocationListDwarf4(list []byte, ctxt *obj.Link, listSym, startPC *obj.LSym) {
  1536  	getPC := debugInfo.GetPC
  1537  
  1538  	if ctxt.UseBASEntries {
  1539  		listSym.WriteInt(ctxt, listSym.Size, ctxt.Arch.PtrSize, ^0)
  1540  		listSym.WriteAddr(ctxt, listSym.Size, ctxt.Arch.PtrSize, startPC, 0)
  1541  	}
  1542  
  1543  	// Re-read list, translating its address from block/value ID to PC.
  1544  	for i := 0; i < len(list); {
  1545  		begin := getPC(decodeValue(ctxt, readPtr(ctxt, list[i:])))
  1546  		end := getPC(decodeValue(ctxt, readPtr(ctxt, list[i+ctxt.Arch.PtrSize:])))
  1547  
  1548  		// Horrible hack. If a range contains only zero-width
  1549  		// instructions, e.g. an Arg, and it's at the beginning of the
  1550  		// function, this would be indistinguishable from an
  1551  		// end entry. Fudge it.
  1552  		if begin == 0 && end == 0 {
  1553  			end = 1
  1554  		}
  1555  
  1556  		if ctxt.UseBASEntries {
  1557  			listSym.WriteInt(ctxt, listSym.Size, ctxt.Arch.PtrSize, int64(begin))
  1558  			listSym.WriteInt(ctxt, listSym.Size, ctxt.Arch.PtrSize, int64(end))
  1559  		} else {
  1560  			listSym.WriteCURelativeAddr(ctxt, listSym.Size, startPC, int64(begin))
  1561  			listSym.WriteCURelativeAddr(ctxt, listSym.Size, startPC, int64(end))
  1562  		}
  1563  
  1564  		i += 2 * ctxt.Arch.PtrSize
  1565  		datalen := 2 + int(ctxt.Arch.ByteOrder.Uint16(list[i:]))
  1566  		listSym.WriteBytes(ctxt, listSym.Size, list[i:i+datalen]) // copy datalen and location encoding
  1567  		i += datalen
  1568  	}
  1569  
  1570  	// Location list contents, now with real PCs.
  1571  	// End entry.
  1572  	listSym.WriteInt(ctxt, listSym.Size, ctxt.Arch.PtrSize, 0)
  1573  	listSym.WriteInt(ctxt, listSym.Size, ctxt.Arch.PtrSize, 0)
  1574  }
  1575  
  1576  // Pack a value and block ID into an address-sized uint, returning
  1577  // encoded value and boolean indicating whether the encoding succeeded.
  1578  // For 32-bit architectures the process may fail for very large
  1579  // procedures(the theory being that it's ok to have degraded debug
  1580  // quality in this case).
  1581  func encodeValue(ctxt *obj.Link, b, v ID) (uint64, bool) {
  1582  	if ctxt.Arch.PtrSize == 8 {
  1583  		result := uint64(b)<<32 | uint64(uint32(v))
  1584  		//ctxt.Logf("b %#x (%d) v %#x (%d) -> %#x\n", b, b, v, v, result)
  1585  		return result, true
  1586  	}
  1587  	if ctxt.Arch.PtrSize != 4 {
  1588  		panic("unexpected pointer size")
  1589  	}
  1590  	if ID(int16(b)) != b || ID(int16(v)) != v {
  1591  		return 0, false
  1592  	}
  1593  	return uint64(b)<<16 | uint64(uint16(v)), true
  1594  }
  1595  
  1596  // Unpack a value and block ID encoded by encodeValue.
  1597  func decodeValue(ctxt *obj.Link, word uint64) (ID, ID) {
  1598  	if ctxt.Arch.PtrSize == 8 {
  1599  		b, v := ID(word>>32), ID(word)
  1600  		//ctxt.Logf("%#x -> b %#x (%d) v %#x (%d)\n", word, b, b, v, v)
  1601  		return b, v
  1602  	}
  1603  	if ctxt.Arch.PtrSize != 4 {
  1604  		panic("unexpected pointer size")
  1605  	}
  1606  	return ID(word >> 16), ID(int16(word))
  1607  }
  1608  
  1609  // Append a pointer-sized uint to buf.
  1610  func appendPtr(ctxt *obj.Link, buf []byte, word uint64) []byte {
  1611  	if cap(buf) < len(buf)+20 {
  1612  		b := make([]byte, len(buf), 20+cap(buf)*2)
  1613  		copy(b, buf)
  1614  		buf = b
  1615  	}
  1616  	writeAt := len(buf)
  1617  	buf = buf[0 : len(buf)+ctxt.Arch.PtrSize]
  1618  	writePtr(ctxt, buf[writeAt:], word)
  1619  	return buf
  1620  }
  1621  
  1622  // Write a pointer-sized uint to the beginning of buf.
  1623  func writePtr(ctxt *obj.Link, buf []byte, word uint64) {
  1624  	switch ctxt.Arch.PtrSize {
  1625  	case 4:
  1626  		ctxt.Arch.ByteOrder.PutUint32(buf, uint32(word))
  1627  	case 8:
  1628  		ctxt.Arch.ByteOrder.PutUint64(buf, word)
  1629  	default:
  1630  		panic("unexpected pointer size")
  1631  	}
  1632  
  1633  }
  1634  
  1635  // Read a pointer-sized uint from the beginning of buf.
  1636  func readPtr(ctxt *obj.Link, buf []byte) uint64 {
  1637  	switch ctxt.Arch.PtrSize {
  1638  	case 4:
  1639  		return uint64(ctxt.Arch.ByteOrder.Uint32(buf))
  1640  	case 8:
  1641  		return ctxt.Arch.ByteOrder.Uint64(buf)
  1642  	default:
  1643  		panic("unexpected pointer size")
  1644  	}
  1645  
  1646  }
  1647  
  1648  // setupLocList creates the initial portion of a location list for a
  1649  // user variable. It emits the encoded start/end of the range and a
  1650  // placeholder for the size. Return value is the new list plus the
  1651  // slot in the list holding the size (to be updated later).
  1652  func setupLocList(ctxt *obj.Link, f *Func, list []byte, st, en ID) ([]byte, int) {
  1653  	start, startOK := encodeValue(ctxt, f.Entry.ID, st)
  1654  	end, endOK := encodeValue(ctxt, f.Entry.ID, en)
  1655  	if !startOK || !endOK {
  1656  		// This could happen if someone writes a function that uses
  1657  		// >65K values on a 32-bit platform. Hopefully a degraded debugging
  1658  		// experience is ok in that case.
  1659  		return nil, 0
  1660  	}
  1661  	list = appendPtr(ctxt, list, start)
  1662  	list = appendPtr(ctxt, list, end)
  1663  
  1664  	// Where to write the length of the location description once
  1665  	// we know how big it is.
  1666  	sizeIdx := len(list)
  1667  	list = list[:len(list)+2]
  1668  	return list, sizeIdx
  1669  }
  1670  
  1671  // locatePrologEnd walks the entry block of a function with incoming
  1672  // register arguments and locates the last instruction in the prolog
  1673  // that spills a register arg. It returns the ID of that instruction,
  1674  // and (where appropriate) the prolog's lowered closure ptr store inst.
  1675  //
  1676  // Example:
  1677  //
  1678  //	b1:
  1679  //	    v3 = ArgIntReg <int> {p1+0} [0] : AX
  1680  //	    ... more arg regs ..
  1681  //	    v4 = ArgFloatReg <float32> {f1+0} [0] : X0
  1682  //	    v52 = MOVQstore <mem> {p1} v2 v3 v1
  1683  //	    ... more stores ...
  1684  //	    v68 = MOVSSstore <mem> {f4} v2 v67 v66
  1685  //	    v38 = MOVQstoreconst <mem> {blob} [val=0,off=0] v2 v32
  1686  //
  1687  // Important: locatePrologEnd is expected to work properly only with
  1688  // optimization turned off (e.g. "-N"). If optimization is enabled
  1689  // we can't be assured of finding all input arguments spilled in the
  1690  // entry block prolog.
  1691  func locatePrologEnd(f *Func, needCloCtx bool) (ID, *Value) {
  1692  
  1693  	// returns true if this instruction looks like it moves an ABI
  1694  	// register (or context register for rangefunc bodies) to the
  1695  	// stack, along with the value being stored.
  1696  	isRegMoveLike := func(v *Value) (bool, ID) {
  1697  		n, ok := v.Aux.(*ir.Name)
  1698  		var r ID
  1699  		if (!ok || n.Class != ir.PPARAM) && !needCloCtx {
  1700  			return false, r
  1701  		}
  1702  		regInputs, memInputs, spInputs := 0, 0, 0
  1703  		for _, a := range v.Args {
  1704  			if a.Op == OpArgIntReg || a.Op == OpArgFloatReg ||
  1705  				(needCloCtx && a.Op.isLoweredGetClosurePtr()) {
  1706  				regInputs++
  1707  				r = a.ID
  1708  			} else if a.Type.IsMemory() {
  1709  				memInputs++
  1710  			} else if a.Op == OpSP {
  1711  				spInputs++
  1712  			} else {
  1713  				return false, r
  1714  			}
  1715  		}
  1716  		return v.Type.IsMemory() && memInputs == 1 &&
  1717  			regInputs == 1 && spInputs == 1, r
  1718  	}
  1719  
  1720  	// OpArg*Reg values we've seen so far on our forward walk,
  1721  	// for which we have not yet seen a corresponding spill.
  1722  	regArgs := make([]ID, 0, 32)
  1723  
  1724  	// removeReg tries to remove a value from regArgs, returning true
  1725  	// if found and removed, or false otherwise.
  1726  	removeReg := func(r ID) bool {
  1727  		for i := 0; i < len(regArgs); i++ {
  1728  			if regArgs[i] == r {
  1729  				regArgs = slices.Delete(regArgs, i, i+1)
  1730  				return true
  1731  			}
  1732  		}
  1733  		return false
  1734  	}
  1735  
  1736  	// Walk forwards through the block. When we see OpArg*Reg, record
  1737  	// the value it produces in the regArgs list. When see a store that uses
  1738  	// the value, remove the entry. When we hit the last store (use)
  1739  	// then we've arrived at the end of the prolog.
  1740  	var cloRegStore *Value
  1741  	for k, v := range f.Entry.Values {
  1742  		if v.Op == OpArgIntReg || v.Op == OpArgFloatReg {
  1743  			regArgs = append(regArgs, v.ID)
  1744  			continue
  1745  		}
  1746  		if needCloCtx && v.Op.isLoweredGetClosurePtr() {
  1747  			regArgs = append(regArgs, v.ID)
  1748  			cloRegStore = v
  1749  			continue
  1750  		}
  1751  		if ok, r := isRegMoveLike(v); ok {
  1752  			if removed := removeReg(r); removed {
  1753  				if len(regArgs) == 0 {
  1754  					// Found our last spill; return the value after
  1755  					// it. Note that it is possible that this spill is
  1756  					// the last instruction in the block. If so, then
  1757  					// return the "end of block" sentinel.
  1758  					if k < len(f.Entry.Values)-1 {
  1759  						return f.Entry.Values[k+1].ID, cloRegStore
  1760  					}
  1761  					return BlockEnd.ID, cloRegStore
  1762  				}
  1763  			}
  1764  		}
  1765  		if v.Op.IsCall() {
  1766  			// if we hit a call, we've gone too far.
  1767  			return v.ID, cloRegStore
  1768  		}
  1769  	}
  1770  	// nothing found
  1771  	return ID(-1), cloRegStore
  1772  }
  1773  
  1774  // isNamedRegParam returns true if the param corresponding to "p"
  1775  // is a named, non-blank input parameter assigned to one or more
  1776  // registers.
  1777  func isNamedRegParam(p abi.ABIParamAssignment) bool {
  1778  	if p.Name == nil {
  1779  		return false
  1780  	}
  1781  	n := p.Name
  1782  	if n.Sym() == nil || n.Sym().IsBlank() {
  1783  		return false
  1784  	}
  1785  	if len(p.Registers) == 0 {
  1786  		return false
  1787  	}
  1788  	return true
  1789  }
  1790  
  1791  // BuildFuncDebugNoOptimized populates a FuncDebug object "rval" with
  1792  // entries corresponding to the register-resident input parameters for
  1793  // the function "f"; it is used when we are compiling without
  1794  // optimization but the register ABI is enabled. For each reg param,
  1795  // it constructs a 2-element location list: the first element holds
  1796  // the input register, and the second element holds the stack location
  1797  // of the param (the assumption being that when optimization is off,
  1798  // each input param reg will be spilled in the prolog). In addition
  1799  // to the register params, here we also build location lists (where
  1800  // appropriate for the ".closureptr" compiler-synthesized variable
  1801  // needed by the debugger for range func bodies.
  1802  func BuildFuncDebugNoOptimized(ctxt *obj.Link, f *Func, loggingEnabled bool, stackOffset func(LocalSlot) int32, rval *FuncDebug) {
  1803  
  1804  	needCloCtx := f.CloSlot != nil
  1805  	pri := f.ABISelf.ABIAnalyzeFuncType(f.Type)
  1806  
  1807  	// Look to see if we have any named register-promoted parameters,
  1808  	// and/or whether we need location info for the ".closureptr"
  1809  	// synthetic variable; if not bail early and let the caller sort
  1810  	// things out for the remainder of the params/locals.
  1811  	numRegParams := 0
  1812  	for _, inp := range pri.InParams() {
  1813  		if isNamedRegParam(inp) {
  1814  			numRegParams++
  1815  		}
  1816  	}
  1817  	if numRegParams == 0 && !needCloCtx {
  1818  		return
  1819  	}
  1820  
  1821  	state := debugState{f: f}
  1822  
  1823  	if loggingEnabled {
  1824  		state.logf("generating -N reg param loc lists for func %q\n", f.Name)
  1825  	}
  1826  
  1827  	// cloReg stores the obj register num that the context register
  1828  	// appears in within the function prolog, where appropriate.
  1829  	var cloReg int16
  1830  
  1831  	extraForCloCtx := 0
  1832  	if needCloCtx {
  1833  		extraForCloCtx = 1
  1834  	}
  1835  
  1836  	// Allocate location lists.
  1837  	rval.LocationLists = make([][]byte, numRegParams+extraForCloCtx)
  1838  
  1839  	// Locate the value corresponding to the last spill of
  1840  	// an input register.
  1841  	afterPrologVal, cloRegStore := locatePrologEnd(f, needCloCtx)
  1842  
  1843  	if needCloCtx {
  1844  		reg, _ := state.f.getHome(cloRegStore.ID).(*Register)
  1845  		cloReg = reg.ObjNum()
  1846  		if loggingEnabled {
  1847  			state.logf("needCloCtx is true for func %q, cloreg=%v\n",
  1848  				f.Name, reg)
  1849  		}
  1850  	}
  1851  
  1852  	addVarSlot := func(name *ir.Name, typ *types.Type) {
  1853  		sl := LocalSlot{N: name, Type: typ, Off: 0}
  1854  		rval.Vars = append(rval.Vars, name)
  1855  		rval.Slots = append(rval.Slots, sl)
  1856  		slid := len(rval.VarSlots)
  1857  		rval.VarSlots = append(rval.VarSlots, []SlotID{SlotID(slid)})
  1858  	}
  1859  
  1860  	// Make an initial pass to populate the vars/slots for our return
  1861  	// value, covering first the input parameters and then (if needed)
  1862  	// the special ".closureptr" var for rangefunc bodies.
  1863  	params := []abi.ABIParamAssignment{}
  1864  	for _, inp := range pri.InParams() {
  1865  		if !isNamedRegParam(inp) {
  1866  			// will be sorted out elsewhere
  1867  			continue
  1868  		}
  1869  		if !IsVarWantedForDebug(inp.Name) {
  1870  			continue
  1871  		}
  1872  		addVarSlot(inp.Name, inp.Type)
  1873  		params = append(params, inp)
  1874  	}
  1875  	if needCloCtx {
  1876  		addVarSlot(f.CloSlot, f.CloSlot.Type())
  1877  		cloAssign := abi.ABIParamAssignment{
  1878  			Type:      f.CloSlot.Type(),
  1879  			Name:      f.CloSlot,
  1880  			Registers: []abi.RegIndex{0}, // dummy
  1881  		}
  1882  		params = append(params, cloAssign)
  1883  	}
  1884  
  1885  	// Walk the input params again and process the register-resident elements.
  1886  	pidx := 0
  1887  	for _, inp := range params {
  1888  		if !isNamedRegParam(inp) {
  1889  			// will be sorted out elsewhere
  1890  			continue
  1891  		}
  1892  		if !IsVarWantedForDebug(inp.Name) {
  1893  			continue
  1894  		}
  1895  
  1896  		sl := rval.Slots[pidx]
  1897  		n := rval.Vars[pidx]
  1898  
  1899  		if afterPrologVal == ID(-1) {
  1900  			// This can happen for degenerate functions with infinite
  1901  			// loops such as that in issue 45948. In such cases, leave
  1902  			// the var/slot set up for the param, but don't try to
  1903  			// emit a location list.
  1904  			if loggingEnabled {
  1905  				state.logf("locatePrologEnd failed, skipping %v\n", n)
  1906  			}
  1907  			pidx++
  1908  			continue
  1909  		}
  1910  
  1911  		// Param is arriving in one or more registers. We need a 2-element
  1912  		// location expression for it. First entry in location list
  1913  		// will correspond to lifetime in input registers.
  1914  		list, sizeIdx := setupLocList(ctxt, f, rval.LocationLists[pidx],
  1915  			BlockStart.ID, afterPrologVal)
  1916  		if list == nil {
  1917  			pidx++
  1918  			continue
  1919  		}
  1920  		if loggingEnabled {
  1921  			state.logf("param %v:\n  [<entry>, %d]:\n", n, afterPrologVal)
  1922  		}
  1923  		rtypes, _ := inp.RegisterTypesAndOffsets()
  1924  		padding := make([]uint64, 0, 32)
  1925  		padding = inp.ComputePadding(padding)
  1926  		for k, r := range inp.Registers {
  1927  			var reg int16
  1928  			if n == f.CloSlot {
  1929  				reg = cloReg
  1930  			} else {
  1931  				reg = ObjRegForAbiReg(r, f.Config)
  1932  			}
  1933  			dwreg := ctxt.Arch.DWARFRegisters[reg]
  1934  			if dwreg < 32 {
  1935  				list = append(list, dwarf.DW_OP_reg0+byte(dwreg))
  1936  			} else {
  1937  				list = append(list, dwarf.DW_OP_regx)
  1938  				list = dwarf.AppendUleb128(list, uint64(dwreg))
  1939  			}
  1940  			if loggingEnabled {
  1941  				state.logf("    piece %d -> dwreg %d", k, dwreg)
  1942  			}
  1943  			if len(inp.Registers) > 1 {
  1944  				list = append(list, dwarf.DW_OP_piece)
  1945  				ts := rtypes[k].Size()
  1946  				list = dwarf.AppendUleb128(list, uint64(ts))
  1947  				if padding[k] > 0 {
  1948  					if loggingEnabled {
  1949  						state.logf(" [pad %d bytes]", padding[k])
  1950  					}
  1951  					list = append(list, dwarf.DW_OP_piece)
  1952  					list = dwarf.AppendUleb128(list, padding[k])
  1953  				}
  1954  			}
  1955  			if loggingEnabled {
  1956  				state.logf("\n")
  1957  			}
  1958  		}
  1959  		// fill in length of location expression element
  1960  		ctxt.Arch.ByteOrder.PutUint16(list[sizeIdx:], uint16(len(list)-sizeIdx-2))
  1961  
  1962  		// Second entry in the location list will be the stack home
  1963  		// of the param, once it has been spilled.  Emit that now.
  1964  		list, sizeIdx = setupLocList(ctxt, f, list,
  1965  			afterPrologVal, FuncEnd.ID)
  1966  		if list == nil {
  1967  			pidx++
  1968  			continue
  1969  		}
  1970  		soff := stackOffset(sl)
  1971  		if soff == 0 {
  1972  			list = append(list, dwarf.DW_OP_call_frame_cfa)
  1973  		} else {
  1974  			list = append(list, dwarf.DW_OP_fbreg)
  1975  			list = dwarf.AppendSleb128(list, int64(soff))
  1976  		}
  1977  		if loggingEnabled {
  1978  			state.logf("  [%d, <end>): stackOffset=%d\n", afterPrologVal, soff)
  1979  		}
  1980  
  1981  		// fill in size
  1982  		ctxt.Arch.ByteOrder.PutUint16(list[sizeIdx:], uint16(len(list)-sizeIdx-2))
  1983  
  1984  		rval.LocationLists[pidx] = list
  1985  		pidx++
  1986  	}
  1987  }
  1988  
  1989  // IsVarWantedForDebug returns true if the debug info for the node should
  1990  // be generated.
  1991  // For example, internal variables for range-over-func loops have little
  1992  // value to users, so we don't generate debug info for them.
  1993  func IsVarWantedForDebug(n ir.Node) bool {
  1994  	name := n.Sym().Name
  1995  	if len(name) > 0 && name[0] == '&' {
  1996  		name = name[1:]
  1997  	}
  1998  	if len(name) > 0 && name[0] == '#' {
  1999  		// #yield is used by delve.
  2000  		return strings.HasPrefix(name, "#yield")
  2001  	}
  2002  	return true
  2003  }
  2004  

View as plain text