1// Copyright 2021 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 walk
6
7import (
8	"cmd/compile/internal/base"
9	"cmd/compile/internal/ir"
10	"cmd/compile/internal/typecheck"
11	"cmd/compile/internal/types"
12)
13
14// initStackTemp appends statements to init to initialize the given
15// temporary variable to val, and then returns the expression &tmp.
16func initStackTemp(init *ir.Nodes, tmp *ir.Name, val ir.Node) *ir.AddrExpr {
17	if val != nil && !types.Identical(tmp.Type(), val.Type()) {
18		base.Fatalf("bad initial value for %L: %L", tmp, val)
19	}
20	appendWalkStmt(init, ir.NewAssignStmt(base.Pos, tmp, val))
21	return typecheck.Expr(typecheck.NodAddr(tmp)).(*ir.AddrExpr)
22}
23
24// stackTempAddr returns the expression &tmp, where tmp is a newly
25// allocated temporary variable of the given type. Statements to
26// zero-initialize tmp are appended to init.
27func stackTempAddr(init *ir.Nodes, typ *types.Type) *ir.AddrExpr {
28	n := typecheck.TempAt(base.Pos, ir.CurFunc, typ)
29	n.SetNonMergeable(true)
30	return initStackTemp(init, n, nil)
31}
32
33// stackBufAddr returns the expression &tmp, where tmp is a newly
34// allocated temporary variable of type [len]elem. This variable is
35// initialized, and elem must not contain pointers.
36func stackBufAddr(len int64, elem *types.Type) *ir.AddrExpr {
37	if elem.HasPointers() {
38		base.FatalfAt(base.Pos, "%v has pointers", elem)
39	}
40	tmp := typecheck.TempAt(base.Pos, ir.CurFunc, types.NewArray(elem, len))
41	return typecheck.Expr(typecheck.NodAddr(tmp)).(*ir.AddrExpr)
42}
43