1// Copyright 2009 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 gc
6
7import (
8	"cmd/compile/internal/types"
9	"cmd/internal/obj"
10	"cmd/internal/src"
11	"strconv"
12)
13
14// sysfunc looks up Go function name in package runtime. This function
15// must follow the internal calling convention.
16func sysfunc(name string) *obj.LSym {
17	s := Runtimepkg.Lookup(name)
18	s.SetFunc(true)
19	return s.Linksym()
20}
21
22// sysvar looks up a variable (or assembly function) name in package
23// runtime. If this is a function, it may have a special calling
24// convention.
25func sysvar(name string) *obj.LSym {
26	return Runtimepkg.Lookup(name).Linksym()
27}
28
29// isParamStackCopy reports whether this is the on-stack copy of a
30// function parameter that moved to the heap.
31func (n *Node) isParamStackCopy() bool {
32	return n.Op == ONAME && (n.Class() == PPARAM || n.Class() == PPARAMOUT) && n.Name.Param.Heapaddr != nil
33}
34
35// isParamHeapCopy reports whether this is the on-heap copy of
36// a function parameter that moved to the heap.
37func (n *Node) isParamHeapCopy() bool {
38	return n.Op == ONAME && n.Class() == PAUTOHEAP && n.Name.Param.Stackcopy != nil
39}
40
41// autotmpname returns the name for an autotmp variable numbered n.
42func autotmpname(n int) string {
43	// Give each tmp a different name so that they can be registerized.
44	// Add a preceding . to avoid clashing with legal names.
45	const prefix = ".autotmp_"
46	// Start with a buffer big enough to hold a large n.
47	b := []byte(prefix + "      ")[:len(prefix)]
48	b = strconv.AppendInt(b, int64(n), 10)
49	return types.InternString(b)
50}
51
52// make a new Node off the books
53func tempAt(pos src.XPos, curfn *Node, t *types.Type) *Node {
54	if curfn == nil {
55		Fatalf("no curfn for tempAt")
56	}
57	if curfn.Func.Closure != nil && curfn.Op == OCLOSURE {
58		Dump("tempAt", curfn)
59		Fatalf("adding tempAt to wrong closure function")
60	}
61	if t == nil {
62		Fatalf("tempAt called with nil type")
63	}
64
65	s := &types.Sym{
66		Name: autotmpname(len(curfn.Func.Dcl)),
67		Pkg:  localpkg,
68	}
69	n := newnamel(pos, s)
70	s.Def = asTypesNode(n)
71	n.Type = t
72	n.SetClass(PAUTO)
73	n.Esc = EscNever
74	n.Name.Curfn = curfn
75	n.Name.SetUsed(true)
76	n.Name.SetAutoTemp(true)
77	curfn.Func.Dcl = append(curfn.Func.Dcl, n)
78
79	dowidth(t)
80
81	return n.Orig
82}
83
84func temp(t *types.Type) *Node {
85	return tempAt(lineno, Curfn, t)
86}
87