backend.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. `fmt`
  19. `sync`
  20. _ `unsafe`
  21. `github.com/bytedance/sonic/internal/rt`
  22. `github.com/twitchyliquid64/golang-asm/asm/arch`
  23. `github.com/twitchyliquid64/golang-asm/obj`
  24. `github.com/twitchyliquid64/golang-asm/objabi`
  25. )
  26. type Backend struct {
  27. Ctxt *obj.Link
  28. Arch *arch.Arch
  29. Head *obj.Prog
  30. Tail *obj.Prog
  31. Prog []*obj.Prog
  32. }
  33. var (
  34. _progPool sync.Pool
  35. )
  36. func newProg() *obj.Prog {
  37. if val := _progPool.Get(); val == nil {
  38. return new(obj.Prog)
  39. } else {
  40. return remProg(val.(*obj.Prog))
  41. }
  42. }
  43. func remProg(p *obj.Prog) *obj.Prog {
  44. *p = obj.Prog{}
  45. return p
  46. }
  47. func newBackend(name string) (ret *Backend) {
  48. ret = new(Backend)
  49. ret.Arch = arch.Set(name)
  50. ret.Ctxt = newLinkContext(ret.Arch.LinkArch)
  51. ret.Arch.Init(ret.Ctxt)
  52. return
  53. }
  54. func newLinkContext(arch *obj.LinkArch) (ret *obj.Link) {
  55. ret = obj.Linknew(arch)
  56. ret.Headtype = objabi.Hlinux
  57. ret.DiagFunc = diagLinkContext
  58. return
  59. }
  60. func diagLinkContext(str string, args ...interface{}) {
  61. rt.Throw(fmt.Sprintf(str, args...))
  62. }
  63. func (self *Backend) New() (ret *obj.Prog) {
  64. ret = newProg()
  65. ret.Ctxt = self.Ctxt
  66. self.Prog = append(self.Prog, ret)
  67. return
  68. }
  69. func (self *Backend) Append(p *obj.Prog) {
  70. if self.Head == nil {
  71. self.Head = p
  72. self.Tail = p
  73. } else {
  74. self.Tail.Link = p
  75. self.Tail = p
  76. }
  77. }
  78. func (self *Backend) Release() {
  79. self.Arch = nil
  80. self.Ctxt = nil
  81. /* return all the progs into pool */
  82. for _, p := range self.Prog {
  83. _progPool.Put(p)
  84. }
  85. /* clear all the references */
  86. self.Head = nil
  87. self.Tail = nil
  88. self.Prog = nil
  89. }
  90. func (self *Backend) Assemble() []byte {
  91. var sym obj.LSym
  92. var fnv obj.FuncInfo
  93. /* construct the function */
  94. sym.Func = &fnv
  95. fnv.Text = self.Head
  96. /* call the assembler */
  97. self.Arch.Assemble(self.Ctxt, &sym, self.New)
  98. return sym.P
  99. }