Debian Codegraph @

go/ssa/interp/external.go

raw symbols SCIP
1 // Copyright 2013 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 interp
6
7 // Emulated functions that we cannot interpret because they are
8 // external or because they use "unsafe" or "reflect" operations.
9
10 import (
11 "bytes"
12 "maps"
13 "math"
14 "os"
15 "runtime"
16 "slices"
17 "sort"
18 "strconv"
19 "strings"
20 "time"
21 "unicode/utf8"
22 )
23
24 type externalFn func(fr *frame, args []value) value
25
26 // TODO(adonovan): fix: reflect.Value abstracts an lvalue or an
27 // rvalue; Set() causes mutations that can be observed via aliases.
28 // We have not captured that correctly here.
29
30 // Key strings are from Function.String().
31 var externals = make(map[string]externalFn)
32
33 func init() {
34 // That little dot ۰ is an Arabic zero numeral (U+06F0), categories [Nd].
35 maps.Copy(externals, map[string]externalFn{
36 "(reflect.Value).Bool": ext۰reflect۰Value۰Bool,
37 "(reflect.Value).CanAddr": ext۰reflect۰Value۰CanAddr,
38 "(reflect.Value).CanInterface": ext۰reflect۰Value۰CanInterface,
39 "(reflect.Value).Elem": ext۰reflect۰Value۰Elem,
40 "(reflect.Value).Field": ext۰reflect۰Value۰Field,
41 "(reflect.Value).Float": ext۰reflect۰Value۰Float,
42 "(reflect.Value).Index": ext۰reflect۰Value۰Index,
43 "(reflect.Value).Int": ext۰reflect۰Value۰Int,
44 "(reflect.Value).Interface": ext۰reflect۰Value۰Interface,
45 "(reflect.Value).IsNil": ext۰reflect۰Value۰IsNil,
46 "(reflect.Value).IsValid": ext۰reflect۰Value۰IsValid,
47 "(reflect.Value).Kind": ext۰reflect۰Value۰Kind,
48 "(reflect.Value).Len": ext۰reflect۰Value۰Len,
49 "(reflect.Value).MapIndex": ext۰reflect۰Value۰MapIndex,
50 "(reflect.Value).MapKeys": ext۰reflect۰Value۰MapKeys,
51 "(reflect.Value).NumField": ext۰reflect۰Value۰NumField,
52 "(reflect.Value).NumMethod": ext۰reflect۰Value۰NumMethod,
53 "(reflect.Value).Pointer": ext۰reflect۰Value۰Pointer,
54 "(reflect.Value).Set": ext۰reflect۰Value۰Set,
55 "(reflect.Value).String": ext۰reflect۰Value۰String,
56 "(reflect.Value).Type": ext۰reflect۰Value۰Type,
57 "(reflect.Value).Uint": ext۰reflect۰Value۰Uint,
58 "(reflect.error).Error": ext۰reflect۰error۰Error,
59 "(reflect.rtype).Bits": ext۰reflect۰rtype۰Bits,
60 "(reflect.rtype).Elem": ext۰reflect۰rtype۰Elem,
61 "(reflect.rtype).Field": ext۰reflect۰rtype۰Field,
62 "(reflect.rtype).In": ext۰reflect۰rtype۰In,
63 "(reflect.rtype).Kind": ext۰reflect۰rtype۰Kind,
64 "(reflect.rtype).NumField": ext۰reflect۰rtype۰NumField,
65 "(reflect.rtype).NumIn": ext۰reflect۰rtype۰NumIn,
66 "(reflect.rtype).NumMethod": ext۰reflect۰rtype۰NumMethod,
67 "(reflect.rtype).NumOut": ext۰reflect۰rtype۰NumOut,
68 "(reflect.rtype).Out": ext۰reflect۰rtype۰Out,
69 "(reflect.rtype).Size": ext۰reflect۰rtype۰Size,
70 "(reflect.rtype).String": ext۰reflect۰rtype۰String,
71 "bytes.Equal": ext۰bytes۰Equal,
72 "bytes.IndexByte": ext۰bytes۰IndexByte,
73 "fmt.Sprint": ext۰fmt۰Sprint,
74 "math.Abs": ext۰math۰Abs,
75 "math.Copysign": ext۰math۰Copysign,
76 "math.Exp": ext۰math۰Exp,
77 "math.Float32bits": ext۰math۰Float32bits,
78 "math.Float32frombits": ext۰math۰Float32frombits,
79 "math.Float64bits": ext۰math۰Float64bits,
80 "math.Float64frombits": ext۰math۰Float64frombits,
81 "math.Inf": ext۰math۰Inf,
82 "math.IsNaN": ext۰math۰IsNaN,
83 "math.Ldexp": ext۰math۰Ldexp,
84 "math.Log": ext۰math۰Log,
85 "math.Min": ext۰math۰Min,
86 "math.NaN": ext۰math۰NaN,
87 "math.Sqrt": ext۰math۰Sqrt,
88 "os.Exit": ext۰os۰Exit,
89 "os.Getenv": ext۰os۰Getenv,
90 "reflect.New": ext۰reflect۰New,
91 "reflect.SliceOf": ext۰reflect۰SliceOf,
92 "reflect.TypeOf": ext۰reflect۰TypeOf,
93 "reflect.ValueOf": ext۰reflect۰ValueOf,
94 "reflect.Zero": ext۰reflect۰Zero,
95 "runtime.Breakpoint": ext۰runtime۰Breakpoint,
96 "runtime.GC": ext۰runtime۰GC,
97 "runtime.GOMAXPROCS": ext۰runtime۰GOMAXPROCS,
98 "runtime.GOROOT": ext۰runtime۰GOROOT,
99 "runtime.Goexit": ext۰runtime۰Goexit,
100 "runtime.Gosched": ext۰runtime۰Gosched,
101 "runtime.NumCPU": ext۰runtime۰NumCPU,
102 "sort.Float64s": ext۰sort۰Float64s,
103 "sort.Ints": ext۰sort۰Ints,
104 "sort.Strings": ext۰sort۰Strings,
105 "strconv.Atoi": ext۰strconv۰Atoi,
106 "strconv.Itoa": ext۰strconv۰Itoa,
107 "strconv.FormatFloat": ext۰strconv۰FormatFloat,
108 "strings.Count": ext۰strings۰Count,
109 "strings.EqualFold": ext۰strings۰EqualFold,
110 "strings.Index": ext۰strings۰Index,
111 "strings.IndexByte": ext۰strings۰IndexByte,
112 "strings.Replace": ext۰strings۰Replace,
113 "strings.ToLower": ext۰strings۰ToLower,
114 "time.Sleep": ext۰time۰Sleep,
115 "unicode/utf8.DecodeRuneInString": ext۰unicode۰utf8۰DecodeRuneInString,
116 })
117 }
118
119 func ext۰bytes۰Equal(fr *frame, args []value) value {
120 // func Equal(a, b []byte) bool
121 a := args[0].([]value)
122 b := args[1].([]value)
123 return slices.Equal(a, b)
124 }
125
126 func ext۰bytes۰IndexByte(fr *frame, args []value) value {
127 // func IndexByte(s []byte, c byte) int
128 s := args[0].([]value)
129 c := args[1].(byte)
130 for i, b := range s {
131 if b.(byte) == c {
132 return i
133 }
134 }
135 return -1
136 }
137
138 func ext۰math۰Float64frombits(fr *frame, args []value) value {
139 return math.Float64frombits(args[0].(uint64))
140 }
141
142 func ext۰math۰Float64bits(fr *frame, args []value) value {
143 return math.Float64bits(args[0].(float64))
144 }
145
146 func ext۰math۰Float32frombits(fr *frame, args []value) value {
147 return math.Float32frombits(args[0].(uint32))
148 }
149
150 func ext۰math۰Abs(fr *frame, args []value) value {
151 return math.Abs(args[0].(float64))
152 }
153
154 func ext۰math۰Copysign(fr *frame, args []value) value {
155 return math.Copysign(args[0].(float64), args[1].(float64))
156 }
157
158 func ext۰math۰Exp(fr *frame, args []value) value {
159 return math.Exp(args[0].(float64))
160 }
161
162 func ext۰math۰Float32bits(fr *frame, args []value) value {
163 return math.Float32bits(args[0].(float32))
164 }
165
166 func ext۰math۰Min(fr *frame, args []value) value {
167 return math.Min(args[0].(float64), args[1].(float64))
168 }
169
170 func ext۰math۰NaN(fr *frame, args []value) value {
171 return math.NaN()
172 }
173
174 func ext۰math۰IsNaN(fr *frame, args []value) value {
175 return math.IsNaN(args[0].(float64))
176 }
177
178 func ext۰math۰Inf(fr *frame, args []value) value {
179 return math.Inf(args[0].(int))
180 }
181
182 func ext۰math۰Ldexp(fr *frame, args []value) value {
183 return math.Ldexp(args[0].(float64), args[1].(int))
184 }
185
186 func ext۰math۰Log(fr *frame, args []value) value {
187 return math.Log(args[0].(float64))
188 }
189
190 func ext۰math۰Sqrt(fr *frame, args []value) value {
191 return math.Sqrt(args[0].(float64))
192 }
193
194 func ext۰runtime۰Breakpoint(fr *frame, args []value) value {
195 runtime.Breakpoint()
196 return nil
197 }
198
199 func ext۰sort۰Ints(fr *frame, args []value) value {
200 x := args[0].([]value)
201 sort.Slice(x, func(i, j int) bool {
202 return x[i].(int) < x[j].(int)
203 })
204 return nil
205 }
206 func ext۰sort۰Strings(fr *frame, args []value) value {
207 x := args[0].([]value)
208 sort.Slice(x, func(i, j int) bool {
209 return x[i].(string) < x[j].(string)
210 })
211 return nil
212 }
213 func ext۰sort۰Float64s(fr *frame, args []value) value {
214 x := args[0].([]value)
215 sort.Slice(x, func(i, j int) bool {
216 return x[i].(float64) < x[j].(float64)
217 })
218 return nil
219 }
220
221 func ext۰strconv۰Atoi(fr *frame, args []value) value {
222 i, e := strconv.Atoi(args[0].(string))
223 if e != nil {
224 return tuple{i, iface{fr.i.runtimeErrorString, e.Error()}}
225 }
226 return tuple{i, iface{}}
227 }
228 func ext۰strconv۰Itoa(fr *frame, args []value) value {
229 return strconv.Itoa(args[0].(int))
230 }
231 func ext۰strconv۰FormatFloat(fr *frame, args []value) value {
232 return strconv.FormatFloat(args[0].(float64), args[1].(byte), args[2].(int), args[3].(int))
233 }
234
235 func ext۰strings۰Count(fr *frame, args []value) value {
236 return strings.Count(args[0].(string), args[1].(string))
237 }
238
239 func ext۰strings۰EqualFold(fr *frame, args []value) value {
240 return strings.EqualFold(args[0].(string), args[1].(string))
241 }
242 func ext۰strings۰IndexByte(fr *frame, args []value) value {
243 return strings.IndexByte(args[0].(string), args[1].(byte))
244 }
245
246 func ext۰strings۰Index(fr *frame, args []value) value {
247 return strings.Index(args[0].(string), args[1].(string))
248 }
249
250 func ext۰strings۰Replace(fr *frame, args []value) value {
251 // func Replace(s, old, new string, n int) string
252 s := args[0].(string)
253 new := args[1].(string)
254 old := args[2].(string)
255 n := args[3].(int)
256 return strings.Replace(s, old, new, n)
257 }
258
259 func ext۰strings۰ToLower(fr *frame, args []value) value {
260 return strings.ToLower(args[0].(string))
261 }
262
263 func ext۰runtime۰GOMAXPROCS(fr *frame, args []value) value {
264 // Ignore args[0]; don't let the interpreted program
265 // set the interpreter's GOMAXPROCS!
266 return runtime.GOMAXPROCS(0)
267 }
268
269 func ext۰runtime۰Goexit(fr *frame, args []value) value {
270 // TODO(adonovan): don't kill the interpreter's main goroutine.
271 runtime.Goexit()
272 return nil
273 }
274
275 func ext۰runtime۰GOROOT(fr *frame, args []value) value {
276 return runtime.GOROOT()
277 }
278
279 func ext۰runtime۰GC(fr *frame, args []value) value {
280 runtime.GC()
281 return nil
282 }
283
284 func ext۰runtime۰Gosched(fr *frame, args []value) value {
285 runtime.Gosched()
286 return nil
287 }
288
289 func ext۰runtime۰NumCPU(fr *frame, args []value) value {
290 return runtime.NumCPU()
291 }
292
293 func ext۰time۰Sleep(fr *frame, args []value) value {
294 time.Sleep(time.Duration(args[0].(int64)))
295 return nil
296 }
297
298 func ext۰os۰Getenv(fr *frame, args []value) value {
299 name := args[0].(string)
300 switch name {
301 case "GOSSAINTERP":
302 return "1"
303 }
304 return os.Getenv(name)
305 }
306
307 func ext۰os۰Exit(fr *frame, args []value) value {
308 panic(exitPanic(args[0].(int)))
309 }
310
311 func ext۰unicode۰utf8۰DecodeRuneInString(fr *frame, args []value) value {
312 r, n := utf8.DecodeRuneInString(args[0].(string))
313 return tuple{r, n}
314 }
315
316 // A fake function for turning an arbitrary value into a string.
317 // Handles only the cases needed by the tests.
318 // Uses same logic as 'print' built-in.
319 func ext۰fmt۰Sprint(fr *frame, args []value) value {
320 buf := new(bytes.Buffer)
321 wasStr := false
322 for i, arg := range args[0].([]value) {
323 x := arg.(iface).v
324 _, isStr := x.(string)
325 if i > 0 && !wasStr && !isStr {
326 buf.WriteByte(' ')
327 }
328 wasStr = isStr
329 buf.WriteString(toString(x))
330 }
331 return buf.String()
332 }