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