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 typecheck
6
7import (
8	"cmd/compile/internal/base"
9	"cmd/compile/internal/ir"
10	"cmd/compile/internal/types"
11	"cmd/internal/obj"
12	"cmd/internal/src"
13)
14
15func LookupRuntime(name string) *ir.Name {
16	s := ir.Pkgs.Runtime.Lookup(name)
17	if s == nil || s.Def == nil {
18		base.Fatalf("LookupRuntime: can't find runtime.%s", name)
19	}
20	return ir.AsNode(s.Def).(*ir.Name)
21}
22
23// SubstArgTypes substitutes the given list of types for
24// successive occurrences of the "any" placeholder in the
25// type syntax expression n.Type.
26// The result of SubstArgTypes MUST be assigned back to old, e.g.
27// 	n.Left = SubstArgTypes(n.Left, t1, t2)
28func SubstArgTypes(old *ir.Name, types_ ...*types.Type) *ir.Name {
29	for _, t := range types_ {
30		types.CalcSize(t)
31	}
32	n := ir.NewNameAt(old.Pos(), old.Sym())
33	n.Class = old.Class
34	n.SetType(types.SubstAny(old.Type(), &types_))
35	n.Func = old.Func
36	if len(types_) > 0 {
37		base.Fatalf("SubstArgTypes: too many argument types")
38	}
39	return n
40}
41
42// AutoLabel generates a new Name node for use with
43// an automatically generated label.
44// prefix is a short mnemonic (e.g. ".s" for switch)
45// to help with debugging.
46// It should begin with "." to avoid conflicts with
47// user labels.
48func AutoLabel(prefix string) *types.Sym {
49	if prefix[0] != '.' {
50		base.Fatalf("autolabel prefix must start with '.', have %q", prefix)
51	}
52	fn := ir.CurFunc
53	if ir.CurFunc == nil {
54		base.Fatalf("autolabel outside function")
55	}
56	n := fn.Label
57	fn.Label++
58	return LookupNum(prefix, int(n))
59}
60
61func Lookup(name string) *types.Sym {
62	return types.LocalPkg.Lookup(name)
63}
64
65// InitRuntime loads the definitions for the low-level runtime functions,
66// so that the compiler can generate calls to them,
67// but does not make them visible to user code.
68func InitRuntime() {
69	base.Timer.Start("fe", "loadsys")
70	types.Block = 1
71
72	typs := runtimeTypes()
73	for _, d := range &runtimeDecls {
74		sym := ir.Pkgs.Runtime.Lookup(d.name)
75		typ := typs[d.typ]
76		switch d.tag {
77		case funcTag:
78			importfunc(src.NoXPos, sym, typ)
79		case varTag:
80			importvar(src.NoXPos, sym, typ)
81		default:
82			base.Fatalf("unhandled declaration tag %v", d.tag)
83		}
84	}
85}
86
87// LookupRuntimeFunc looks up Go function name in package runtime. This function
88// must follow the internal calling convention.
89func LookupRuntimeFunc(name string) *obj.LSym {
90	return LookupRuntimeABI(name, obj.ABIInternal)
91}
92
93// LookupRuntimeVar looks up a variable (or assembly function) name in package
94// runtime. If this is a function, it may have a special calling
95// convention.
96func LookupRuntimeVar(name string) *obj.LSym {
97	return LookupRuntimeABI(name, obj.ABI0)
98}
99
100// LookupRuntimeABI looks up a name in package runtime using the given ABI.
101func LookupRuntimeABI(name string, abi obj.ABI) *obj.LSym {
102	return base.PkgLinksym("runtime", name, abi)
103}
104