Source file src/cmd/compile/internal/walk/temp.go

     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  
     5  package walk
     6  
     7  import (
     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.
    16  func 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.
    27  func stackTempAddr(init *ir.Nodes, typ *types.Type) *ir.AddrExpr {
    28  	return initStackTemp(init, typecheck.TempAt(base.Pos, ir.CurFunc, typ), nil)
    29  }
    30  
    31  // stackBufAddr returns the expression &tmp, where tmp is a newly
    32  // allocated temporary variable of type [len]elem. This variable is
    33  // initialized, and elem must not contain pointers.
    34  func stackBufAddr(len int64, elem *types.Type) *ir.AddrExpr {
    35  	if elem.HasPointers() {
    36  		base.FatalfAt(base.Pos, "%v has pointers", elem)
    37  	}
    38  	tmp := typecheck.TempAt(base.Pos, ir.CurFunc, types.NewArray(elem, len))
    39  	return typecheck.Expr(typecheck.NodAddr(tmp)).(*ir.AddrExpr)
    40  }
    41  

View as plain text