1// Copyright 2015 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
5package ssa
6
7import (
8	"cmd/compile/internal/ir"
9	"cmd/compile/internal/types"
10	"fmt"
11)
12
13// A place that an ssa variable can reside.
14type Location interface {
15	String() string // name to use in assembly templates: AX, 16(SP), ...
16}
17
18// A Register is a machine register, like AX.
19// They are numbered densely from 0 (for each architecture).
20type Register struct {
21	num    int32 // dense numbering
22	objNum int16 // register number from cmd/internal/obj/$ARCH
23	gcNum  int16 // GC register map number (dense numbering of registers that can contain pointers)
24	name   string
25}
26
27func (r *Register) String() string {
28	return r.name
29}
30
31// ObjNum returns the register number from cmd/internal/obj/$ARCH that
32// corresponds to this register.
33func (r *Register) ObjNum() int16 {
34	return r.objNum
35}
36
37// GCNum returns the runtime GC register index of r, or -1 if this
38// register can't contain pointers.
39func (r *Register) GCNum() int16 {
40	return r.gcNum
41}
42
43// A LocalSlot is a location in the stack frame, which identifies and stores
44// part or all of a PPARAM, PPARAMOUT, or PAUTO ONAME node.
45// It can represent a whole variable, part of a larger stack slot, or part of a
46// variable that has been decomposed into multiple stack slots.
47// As an example, a string could have the following configurations:
48//
49//	          stack layout              LocalSlots
50//
51//	Optimizations are disabled. s is on the stack and represented in its entirety.
52//	[ ------- s string ---- ] { N: s, Type: string, Off: 0 }
53//
54//	s was not decomposed, but the SSA operates on its parts individually, so
55//	there is a LocalSlot for each of its fields that points into the single stack slot.
56//	[ ------- s string ---- ] { N: s, Type: *uint8, Off: 0 }, {N: s, Type: int, Off: 8}
57//
58//	s was decomposed. Each of its fields is in its own stack slot and has its own LocalSLot.
59//	[ ptr *uint8 ] [ len int] { N: ptr, Type: *uint8, Off: 0, SplitOf: parent, SplitOffset: 0},
60//	                          { N: len, Type: int, Off: 0, SplitOf: parent, SplitOffset: 8}
61//	                          parent = &{N: s, Type: string}
62type LocalSlot struct {
63	N    *ir.Name    // an ONAME *ir.Name representing a stack location.
64	Type *types.Type // type of slot
65	Off  int64       // offset of slot in N
66
67	SplitOf     *LocalSlot // slot is a decomposition of SplitOf
68	SplitOffset int64      // .. at this offset.
69}
70
71func (s LocalSlot) String() string {
72	if s.Off == 0 {
73		return fmt.Sprintf("%v[%v]", s.N, s.Type)
74	}
75	return fmt.Sprintf("%v+%d[%v]", s.N, s.Off, s.Type)
76}
77
78type LocPair [2]Location
79
80func (t LocPair) String() string {
81	n0, n1 := "nil", "nil"
82	if t[0] != nil {
83		n0 = t[0].String()
84	}
85	if t[1] != nil {
86		n1 = t[1].String()
87	}
88	return fmt.Sprintf("<%s,%s>", n0, n1)
89}
90
91type LocResults []Location
92
93func (t LocResults) String() string {
94	s := ""
95	a := "<"
96	for _, r := range t {
97		a += s
98		s = ","
99		a += r.String()
100	}
101	a += ">"
102	return a
103}
104
105type Spill struct {
106	Type   *types.Type
107	Offset int64
108	Reg    int16
109}
110