backend.go 2.6 KB

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