builtin.go 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. // Copyright 2019 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. package goobj
  5. // Builtin (compiler-generated) function references appear
  6. // frequently. We assign special indices for them, so they
  7. // don't need to be referenced by name.
  8. // NBuiltin returns the number of listed builtin
  9. // symbols.
  10. func NBuiltin() int {
  11. return len(builtins)
  12. }
  13. // BuiltinName returns the name and ABI of the i-th
  14. // builtin symbol.
  15. func BuiltinName(i int) (string, int) {
  16. return builtins[i].name, builtins[i].abi
  17. }
  18. // BuiltinIdx returns the index of the builtin with the
  19. // given name and abi, or -1 if it is not a builtin.
  20. func BuiltinIdx(name string, abi int) int {
  21. i, ok := builtinMap[name]
  22. if !ok {
  23. return -1
  24. }
  25. if builtins[i].abi != abi {
  26. return -1
  27. }
  28. return i
  29. }
  30. //go:generate go run mkbuiltin.go
  31. var builtinMap map[string]int
  32. func init() {
  33. builtinMap = make(map[string]int, len(builtins))
  34. for i, b := range builtins {
  35. builtinMap[b.name] = i
  36. }
  37. }