| 1 |
// Copyright 2020 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:generate go run mknode.go |
| 6 |
|
| 7 |
package ir |
| 8 |
|
| 9 |
import ( |
| 10 |
"cmd/compile/internal/types" |
| 11 |
"cmd/internal/src" |
| 12 |
"fmt" |
| 13 |
"go/constant" |
| 14 |
) |
| 15 |
|
| 16 |
// A miniNode is a minimal node implementation, |
| 17 |
// meant to be embedded as the first field in a larger node implementation, |
| 18 |
// at a cost of 12 bytes. |
| 19 |
// |
| 20 |
// A miniNode is NOT a valid Node by itself: the embedding struct |
| 21 |
// must at the least provide: |
| 22 |
// |
| 23 |
// func (n *MyNode) String() string { return fmt.Sprint(n) } |
| 24 |
// func (n *MyNode) rawCopy() Node { c := *n; return &c } |
| 25 |
// func (n *MyNode) Format(s fmt.State, verb rune) { FmtNode(n, s, verb) } |
| 26 |
// |
| 27 |
// The embedding struct should also fill in n.op in its constructor, |
| 28 |
// for more useful panic messages when invalid methods are called, |
| 29 |
// instead of implementing Op itself. |
| 30 |
type miniNode struct { |
| 31 |
pos src.XPos |
| 32 |
op Op |
| 33 |
bits bitset8 |
| 34 |
esc uint16 |
| 35 |
} |
| 36 |
|
| 37 |
// op can be read, but not written. |
| 38 |
// An embedding implementation can provide a SetOp if desired. |
| 39 |
// (The panicking SetOp is with the other panics below.) |
| 40 |
func (n *miniNode) Op() Op { return n.op } |
| 41 |
func (n *miniNode) Pos() src.XPos { return n.pos } |
| 42 |
func (n *miniNode) SetPos(x src.XPos) { n.pos = x } |
| 43 |
func (n *miniNode) Esc() uint16 { return n.esc } |
| 44 |
func (n *miniNode) SetEsc(x uint16) { n.esc = x } |
| 45 |
|
| 46 |
const ( |
| 47 |
miniTypecheckShift = 0 |
| 48 |
miniWalked = 1 << 2 // to prevent/catch re-walking |
| 49 |
) |
| 50 |
|
| 51 |
func (n *miniNode) Typecheck() uint8 { return n.bits.get2(miniTypecheckShift) } |
| 52 |
func (n *miniNode) SetTypecheck(x uint8) { |
| 53 |
if x > 2 { |
| 54 |
panic(fmt.Sprintf("cannot SetTypecheck %d", x)) |
| 55 |
} |
| 56 |
n.bits.set2(miniTypecheckShift, x) |
| 57 |
} |
| 58 |
|
| 59 |
func (n *miniNode) Walked() bool { return n.bits&miniWalked != 0 } |
| 60 |
func (n *miniNode) SetWalked(x bool) { n.bits.set(miniWalked, x) } |
| 61 |
|
| 62 |
// Empty, immutable graph structure. |
| 63 |
|
| 64 |
func (n *miniNode) Init() Nodes { return Nodes{} } |
| 65 |
|
| 66 |
// Additional functionality unavailable. |
| 67 |
|
| 68 |
func (n *miniNode) no(name string) string { return "cannot " + name + " on " + n.op.String() } |
| 69 |
|
| 70 |
func (n *miniNode) Type() *types.Type { return nil } |
| 71 |
func (n *miniNode) SetType(*types.Type) { panic(n.no("SetType")) } |
| 72 |
func (n *miniNode) Name() *Name { return nil } |
| 73 |
func (n *miniNode) Sym() *types.Sym { return nil } |
| 74 |
func (n *miniNode) Val() constant.Value { panic(n.no("Val")) } |
| 75 |
func (n *miniNode) SetVal(v constant.Value) { panic(n.no("SetVal")) } |
| 76 |
func (n *miniNode) NonNil() bool { return false } |
| 77 |
func (n *miniNode) MarkNonNil() { panic(n.no("MarkNonNil")) } |