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