runtime.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /*
  2. * Copyright 2021 ByteDance Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package jit
  17. import (
  18. `reflect`
  19. `unsafe`
  20. `github.com/bytedance/sonic/internal/rt`
  21. `github.com/twitchyliquid64/golang-asm/obj`
  22. )
  23. //go:noescape
  24. //go:linkname getitab runtime.getitab
  25. //goland:noinspection ALL
  26. func getitab(inter *rt.GoType, typ *rt.GoType, canfail bool) *rt.GoItab
  27. func Func(f interface{}) obj.Addr {
  28. if p := rt.UnpackEface(f); p.Type.Kind() != reflect.Func {
  29. panic("f is not a function")
  30. } else {
  31. return Imm(*(*int64)(p.Value))
  32. }
  33. }
  34. func Type(t reflect.Type) obj.Addr {
  35. return Gtype(rt.UnpackType(t))
  36. }
  37. func Itab(i *rt.GoType, t reflect.Type) obj.Addr {
  38. return Imm(int64(uintptr(unsafe.Pointer(getitab(i, rt.UnpackType(t), false)))))
  39. }
  40. func Gitab(i *rt.GoItab) obj.Addr {
  41. return Imm(int64(uintptr(unsafe.Pointer(i))))
  42. }
  43. func Gtype(t *rt.GoType) obj.Addr {
  44. return Imm(int64(uintptr(unsafe.Pointer(t))))
  45. }