assembler_regabi_amd64.go 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176
  1. // +build go1.17,!go1.23
  2. /*
  3. * Copyright 2021 ByteDance Inc.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package encoder
  18. import (
  19. `fmt`
  20. `reflect`
  21. `strconv`
  22. `unsafe`
  23. `github.com/bytedance/sonic/internal/cpu`
  24. `github.com/bytedance/sonic/internal/jit`
  25. `github.com/bytedance/sonic/internal/native/types`
  26. `github.com/twitchyliquid64/golang-asm/obj`
  27. `github.com/twitchyliquid64/golang-asm/obj/x86`
  28. `github.com/bytedance/sonic/internal/native`
  29. `github.com/bytedance/sonic/internal/rt`
  30. )
  31. /** Register Allocations
  32. *
  33. * State Registers:
  34. *
  35. * %rbx : stack base
  36. * %rdi : result pointer
  37. * %rsi : result length
  38. * %rdx : result capacity
  39. * %r12 : sp->p
  40. * %r13 : sp->q
  41. * %r14 : sp->x
  42. * %r15 : sp->f
  43. *
  44. * Error Registers:
  45. *
  46. * %r10 : error type register
  47. * %r11 : error pointer register
  48. */
  49. /** Function Prototype & Stack Map
  50. *
  51. * func (buf *[]byte, p unsafe.Pointer, sb *_Stack, fv uint64) (err error)
  52. *
  53. * buf : (FP)
  54. * p : 8(FP)
  55. * sb : 16(FP)
  56. * fv : 24(FP)
  57. * err.vt : 32(FP)
  58. * err.vp : 40(FP)
  59. */
  60. const (
  61. _S_cond = iota
  62. _S_init
  63. )
  64. const (
  65. _FP_args = 32 // 32 bytes for spill registers of arguments
  66. _FP_fargs = 40 // 40 bytes for passing arguments to other Go functions
  67. _FP_saves = 64 // 64 bytes for saving the registers before CALL instructions
  68. _FP_locals = 24 // 24 bytes for local variables
  69. )
  70. const (
  71. _FP_loffs = _FP_fargs + _FP_saves
  72. _FP_offs = _FP_loffs + _FP_locals
  73. // _FP_offs = _FP_loffs + _FP_locals + _FP_debug
  74. _FP_size = _FP_offs + 8 // 8 bytes for the parent frame pointer
  75. _FP_base = _FP_size + 8 // 8 bytes for the return address
  76. )
  77. const (
  78. _FM_exp32 = 0x7f800000
  79. _FM_exp64 = 0x7ff0000000000000
  80. )
  81. const (
  82. _IM_null = 0x6c6c756e // 'null'
  83. _IM_true = 0x65757274 // 'true'
  84. _IM_fals = 0x736c6166 // 'fals' ('false' without the 'e')
  85. _IM_open = 0x00225c22 // '"\"∅'
  86. _IM_array = 0x5d5b // '[]'
  87. _IM_object = 0x7d7b // '{}'
  88. _IM_mulv = -0x5555555555555555
  89. )
  90. const (
  91. _LB_more_space = "_more_space"
  92. _LB_more_space_return = "_more_space_return_"
  93. )
  94. const (
  95. _LB_error = "_error"
  96. _LB_error_too_deep = "_error_too_deep"
  97. _LB_error_invalid_number = "_error_invalid_number"
  98. _LB_error_nan_or_infinite = "_error_nan_or_infinite"
  99. _LB_panic = "_panic"
  100. )
  101. var (
  102. _AX = jit.Reg("AX")
  103. _BX = jit.Reg("BX")
  104. _CX = jit.Reg("CX")
  105. _DX = jit.Reg("DX")
  106. _DI = jit.Reg("DI")
  107. _SI = jit.Reg("SI")
  108. _BP = jit.Reg("BP")
  109. _SP = jit.Reg("SP")
  110. _R8 = jit.Reg("R8")
  111. _R9 = jit.Reg("R9")
  112. )
  113. var (
  114. _X0 = jit.Reg("X0")
  115. _X15 = jit.Reg("X15")
  116. _Y0 = jit.Reg("Y0")
  117. )
  118. var (
  119. _ST = jit.Reg("R15") // can't use R14 since it's always scratched by Go...
  120. _RP = jit.Reg("DI")
  121. _RL = jit.Reg("SI")
  122. _RC = jit.Reg("DX")
  123. )
  124. var (
  125. _LR = jit.Reg("R9")
  126. _ET = jit.Reg("AX")
  127. _EP = jit.Reg("BX")
  128. )
  129. var (
  130. _SP_p = jit.Reg("R10") // saved on BX when call_c
  131. _SP_q = jit.Reg("R11") // saved on BP when call_c
  132. _SP_x = jit.Reg("R12")
  133. _SP_f = jit.Reg("R13")
  134. )
  135. var (
  136. _ARG_rb = jit.Ptr(_SP, _FP_base)
  137. _ARG_vp = jit.Ptr(_SP, _FP_base + 8)
  138. _ARG_sb = jit.Ptr(_SP, _FP_base + 16)
  139. _ARG_fv = jit.Ptr(_SP, _FP_base + 24)
  140. )
  141. var (
  142. _RET_et = _ET
  143. _RET_ep = _EP
  144. )
  145. var (
  146. _VAR_sp = jit.Ptr(_SP, _FP_fargs + _FP_saves)
  147. _VAR_dn = jit.Ptr(_SP, _FP_fargs + _FP_saves + 8)
  148. _VAR_vp = jit.Ptr(_SP, _FP_fargs + _FP_saves + 16)
  149. )
  150. var (
  151. _REG_ffi = []obj.Addr{ _RP, _RL, _RC, _SP_q}
  152. _REG_b64 = []obj.Addr{_SP_p, _SP_q}
  153. _REG_all = []obj.Addr{_ST, _SP_x, _SP_f, _SP_p, _SP_q, _RP, _RL, _RC}
  154. _REG_ms = []obj.Addr{_ST, _SP_x, _SP_f, _SP_p, _SP_q, _LR}
  155. _REG_enc = []obj.Addr{_ST, _SP_x, _SP_f, _SP_p, _SP_q, _RL}
  156. )
  157. type _Assembler struct {
  158. jit.BaseAssembler
  159. p _Program
  160. x int
  161. name string
  162. }
  163. func newAssembler(p _Program) *_Assembler {
  164. return new(_Assembler).Init(p)
  165. }
  166. /** Assembler Interface **/
  167. func (self *_Assembler) Load() _Encoder {
  168. return ptoenc(self.BaseAssembler.Load("encode_"+self.name, _FP_size, _FP_args, argPtrs, localPtrs))
  169. }
  170. func (self *_Assembler) Init(p _Program) *_Assembler {
  171. self.p = p
  172. self.BaseAssembler.Init(self.compile)
  173. return self
  174. }
  175. func (self *_Assembler) compile() {
  176. self.prologue()
  177. self.instrs()
  178. self.epilogue()
  179. self.builtins()
  180. }
  181. /** Assembler Stages **/
  182. var _OpFuncTab = [256]func(*_Assembler, *_Instr) {
  183. _OP_null : (*_Assembler)._asm_OP_null,
  184. _OP_empty_arr : (*_Assembler)._asm_OP_empty_arr,
  185. _OP_empty_obj : (*_Assembler)._asm_OP_empty_obj,
  186. _OP_bool : (*_Assembler)._asm_OP_bool,
  187. _OP_i8 : (*_Assembler)._asm_OP_i8,
  188. _OP_i16 : (*_Assembler)._asm_OP_i16,
  189. _OP_i32 : (*_Assembler)._asm_OP_i32,
  190. _OP_i64 : (*_Assembler)._asm_OP_i64,
  191. _OP_u8 : (*_Assembler)._asm_OP_u8,
  192. _OP_u16 : (*_Assembler)._asm_OP_u16,
  193. _OP_u32 : (*_Assembler)._asm_OP_u32,
  194. _OP_u64 : (*_Assembler)._asm_OP_u64,
  195. _OP_f32 : (*_Assembler)._asm_OP_f32,
  196. _OP_f64 : (*_Assembler)._asm_OP_f64,
  197. _OP_str : (*_Assembler)._asm_OP_str,
  198. _OP_bin : (*_Assembler)._asm_OP_bin,
  199. _OP_quote : (*_Assembler)._asm_OP_quote,
  200. _OP_number : (*_Assembler)._asm_OP_number,
  201. _OP_eface : (*_Assembler)._asm_OP_eface,
  202. _OP_iface : (*_Assembler)._asm_OP_iface,
  203. _OP_byte : (*_Assembler)._asm_OP_byte,
  204. _OP_text : (*_Assembler)._asm_OP_text,
  205. _OP_deref : (*_Assembler)._asm_OP_deref,
  206. _OP_index : (*_Assembler)._asm_OP_index,
  207. _OP_load : (*_Assembler)._asm_OP_load,
  208. _OP_save : (*_Assembler)._asm_OP_save,
  209. _OP_drop : (*_Assembler)._asm_OP_drop,
  210. _OP_drop_2 : (*_Assembler)._asm_OP_drop_2,
  211. _OP_recurse : (*_Assembler)._asm_OP_recurse,
  212. _OP_is_nil : (*_Assembler)._asm_OP_is_nil,
  213. _OP_is_nil_p1 : (*_Assembler)._asm_OP_is_nil_p1,
  214. _OP_is_zero_1 : (*_Assembler)._asm_OP_is_zero_1,
  215. _OP_is_zero_2 : (*_Assembler)._asm_OP_is_zero_2,
  216. _OP_is_zero_4 : (*_Assembler)._asm_OP_is_zero_4,
  217. _OP_is_zero_8 : (*_Assembler)._asm_OP_is_zero_8,
  218. _OP_is_zero_map : (*_Assembler)._asm_OP_is_zero_map,
  219. _OP_goto : (*_Assembler)._asm_OP_goto,
  220. _OP_map_iter : (*_Assembler)._asm_OP_map_iter,
  221. _OP_map_stop : (*_Assembler)._asm_OP_map_stop,
  222. _OP_map_check_key : (*_Assembler)._asm_OP_map_check_key,
  223. _OP_map_write_key : (*_Assembler)._asm_OP_map_write_key,
  224. _OP_map_value_next : (*_Assembler)._asm_OP_map_value_next,
  225. _OP_slice_len : (*_Assembler)._asm_OP_slice_len,
  226. _OP_slice_next : (*_Assembler)._asm_OP_slice_next,
  227. _OP_marshal : (*_Assembler)._asm_OP_marshal,
  228. _OP_marshal_p : (*_Assembler)._asm_OP_marshal_p,
  229. _OP_marshal_text : (*_Assembler)._asm_OP_marshal_text,
  230. _OP_marshal_text_p : (*_Assembler)._asm_OP_marshal_text_p,
  231. _OP_cond_set : (*_Assembler)._asm_OP_cond_set,
  232. _OP_cond_testc : (*_Assembler)._asm_OP_cond_testc,
  233. }
  234. func (self *_Assembler) instr(v *_Instr) {
  235. if fn := _OpFuncTab[v.op()]; fn != nil {
  236. fn(self, v)
  237. } else {
  238. panic(fmt.Sprintf("invalid opcode: %d", v.op()))
  239. }
  240. }
  241. func (self *_Assembler) instrs() {
  242. for i, v := range self.p {
  243. self.Mark(i)
  244. self.instr(&v)
  245. self.debug_instr(i, &v)
  246. }
  247. }
  248. func (self *_Assembler) builtins() {
  249. self.more_space()
  250. self.error_too_deep()
  251. self.error_invalid_number()
  252. self.error_nan_or_infinite()
  253. self.go_panic()
  254. }
  255. func (self *_Assembler) epilogue() {
  256. self.Mark(len(self.p))
  257. self.Emit("XORL", _ET, _ET)
  258. self.Emit("XORL", _EP, _EP)
  259. self.Link(_LB_error)
  260. self.Emit("MOVQ", _ARG_rb, _CX) // MOVQ rb<>+0(FP), CX
  261. self.Emit("MOVQ", _RL, jit.Ptr(_CX, 8)) // MOVQ RL, 8(CX)
  262. self.Emit("MOVQ", jit.Imm(0), _ARG_rb) // MOVQ AX, rb<>+0(FP)
  263. self.Emit("MOVQ", jit.Imm(0), _ARG_vp) // MOVQ BX, vp<>+8(FP)
  264. self.Emit("MOVQ", jit.Imm(0), _ARG_sb) // MOVQ CX, sb<>+16(FP)
  265. self.Emit("MOVQ", jit.Ptr(_SP, _FP_offs), _BP) // MOVQ _FP_offs(SP), BP
  266. self.Emit("ADDQ", jit.Imm(_FP_size), _SP) // ADDQ $_FP_size, SP
  267. self.Emit("RET") // RET
  268. }
  269. func (self *_Assembler) prologue() {
  270. self.Emit("SUBQ", jit.Imm(_FP_size), _SP) // SUBQ $_FP_size, SP
  271. self.Emit("MOVQ", _BP, jit.Ptr(_SP, _FP_offs)) // MOVQ BP, _FP_offs(SP)
  272. self.Emit("LEAQ", jit.Ptr(_SP, _FP_offs), _BP) // LEAQ _FP_offs(SP), BP
  273. self.Emit("MOVQ", _AX, _ARG_rb) // MOVQ AX, rb<>+0(FP)
  274. self.Emit("MOVQ", _BX, _ARG_vp) // MOVQ BX, vp<>+8(FP)
  275. self.Emit("MOVQ", _CX, _ARG_sb) // MOVQ CX, sb<>+16(FP)
  276. self.Emit("MOVQ", _DI, _ARG_fv) // MOVQ DI, rb<>+24(FP)
  277. self.Emit("MOVQ", jit.Ptr(_AX, 0), _RP) // MOVQ (AX) , DI
  278. self.Emit("MOVQ", jit.Ptr(_AX, 8), _RL) // MOVQ 8(AX) , SI
  279. self.Emit("MOVQ", jit.Ptr(_AX, 16), _RC) // MOVQ 16(AX), DX
  280. self.Emit("MOVQ", _BX, _SP_p) // MOVQ BX, R10
  281. self.Emit("MOVQ", _CX, _ST) // MOVQ CX, R8
  282. self.Emit("XORL", _SP_x, _SP_x) // XORL R10, R12
  283. self.Emit("XORL", _SP_f, _SP_f) // XORL R11, R13
  284. self.Emit("XORL", _SP_q, _SP_q) // XORL R13, R11
  285. }
  286. /** Assembler Inline Functions **/
  287. func (self *_Assembler) xsave(reg ...obj.Addr) {
  288. for i, v := range reg {
  289. if i > _FP_saves / 8 - 1 {
  290. panic("too many registers to save")
  291. } else {
  292. self.Emit("MOVQ", v, jit.Ptr(_SP, _FP_fargs + int64(i) * 8))
  293. }
  294. }
  295. }
  296. func (self *_Assembler) xload(reg ...obj.Addr) {
  297. for i, v := range reg {
  298. if i > _FP_saves / 8 - 1 {
  299. panic("too many registers to load")
  300. } else {
  301. self.Emit("MOVQ", jit.Ptr(_SP, _FP_fargs + int64(i) * 8), v)
  302. }
  303. }
  304. }
  305. func (self *_Assembler) rbuf_di() {
  306. if _RP.Reg != x86.REG_DI {
  307. panic("register allocation messed up: RP != DI")
  308. } else {
  309. self.Emit("ADDQ", _RL, _RP)
  310. }
  311. }
  312. func (self *_Assembler) store_int(nd int, fn obj.Addr, ins string) {
  313. self.check_size(nd)
  314. self.save_c() // SAVE $C_regs
  315. self.rbuf_di() // MOVQ RP, DI
  316. self.Emit(ins, jit.Ptr(_SP_p, 0), _SI) // $ins (SP.p), SI
  317. self.call_c(fn) // CALL_C $fn
  318. self.Emit("ADDQ", _AX, _RL) // ADDQ AX, RL
  319. }
  320. func (self *_Assembler) store_str(s string) {
  321. i := 0
  322. m := rt.Str2Mem(s)
  323. /* 8-byte stores */
  324. for i <= len(m) - 8 {
  325. self.Emit("MOVQ", jit.Imm(rt.Get64(m[i:])), _AX) // MOVQ $s[i:], AX
  326. self.Emit("MOVQ", _AX, jit.Sib(_RP, _RL, 1, int64(i))) // MOVQ AX, i(RP)(RL)
  327. i += 8
  328. }
  329. /* 4-byte stores */
  330. if i <= len(m) - 4 {
  331. self.Emit("MOVL", jit.Imm(int64(rt.Get32(m[i:]))), jit.Sib(_RP, _RL, 1, int64(i))) // MOVL $s[i:], i(RP)(RL)
  332. i += 4
  333. }
  334. /* 2-byte stores */
  335. if i <= len(m) - 2 {
  336. self.Emit("MOVW", jit.Imm(int64(rt.Get16(m[i:]))), jit.Sib(_RP, _RL, 1, int64(i))) // MOVW $s[i:], i(RP)(RL)
  337. i += 2
  338. }
  339. /* last byte */
  340. if i < len(m) {
  341. self.Emit("MOVB", jit.Imm(int64(m[i])), jit.Sib(_RP, _RL, 1, int64(i))) // MOVB $s[i:], i(RP)(RL)
  342. }
  343. }
  344. func (self *_Assembler) check_size(n int) {
  345. self.check_size_rl(jit.Ptr(_RL, int64(n)))
  346. }
  347. func (self *_Assembler) check_size_r(r obj.Addr, d int) {
  348. self.check_size_rl(jit.Sib(_RL, r, 1, int64(d)))
  349. }
  350. func (self *_Assembler) check_size_rl(v obj.Addr) {
  351. idx := self.x
  352. key := _LB_more_space_return + strconv.Itoa(idx)
  353. /* the following code relies on LR == R9 to work */
  354. if _LR.Reg != x86.REG_R9 {
  355. panic("register allocation messed up: LR != R9")
  356. }
  357. /* check for buffer capacity */
  358. self.x++
  359. self.Emit("LEAQ", v, _AX) // LEAQ $v, AX
  360. self.Emit("CMPQ", _AX, _RC) // CMPQ AX, RC
  361. self.Sjmp("JBE" , key) // JBE _more_space_return_{n}
  362. self.slice_grow_ax(key) // GROW $key
  363. self.Link(key) // _more_space_return_{n}:
  364. }
  365. func (self *_Assembler) slice_grow_ax(ret string) {
  366. self.Byte(0x4c, 0x8d, 0x0d) // LEAQ ?(PC), R9
  367. self.Sref(ret, 4) // .... &ret
  368. self.Sjmp("JMP" , _LB_more_space) // JMP _more_space
  369. }
  370. /** State Stack Helpers **/
  371. const (
  372. _StateSize = int64(unsafe.Sizeof(_State{}))
  373. _StackLimit = _MaxStack * _StateSize
  374. )
  375. func (self *_Assembler) save_state() {
  376. self.Emit("MOVQ", jit.Ptr(_ST, 0), _CX) // MOVQ (ST), CX
  377. self.Emit("LEAQ", jit.Ptr(_CX, _StateSize), _R9) // LEAQ _StateSize(CX), R9
  378. self.Emit("CMPQ", _R9, jit.Imm(_StackLimit)) // CMPQ R9, $_StackLimit
  379. self.Sjmp("JAE" , _LB_error_too_deep) // JA _error_too_deep
  380. self.Emit("MOVQ", _SP_x, jit.Sib(_ST, _CX, 1, 8)) // MOVQ SP.x, 8(ST)(CX)
  381. self.Emit("MOVQ", _SP_f, jit.Sib(_ST, _CX, 1, 16)) // MOVQ SP.f, 16(ST)(CX)
  382. self.WritePtr(0, _SP_p, jit.Sib(_ST, _CX, 1, 24)) // MOVQ SP.p, 24(ST)(CX)
  383. self.WritePtr(1, _SP_q, jit.Sib(_ST, _CX, 1, 32)) // MOVQ SP.q, 32(ST)(CX)
  384. self.Emit("MOVQ", _R9, jit.Ptr(_ST, 0)) // MOVQ R9, (ST)
  385. }
  386. func (self *_Assembler) drop_state(decr int64) {
  387. self.Emit("MOVQ" , jit.Ptr(_ST, 0), _AX) // MOVQ (ST), AX
  388. self.Emit("SUBQ" , jit.Imm(decr), _AX) // SUBQ $decr, AX
  389. self.Emit("MOVQ" , _AX, jit.Ptr(_ST, 0)) // MOVQ AX, (ST)
  390. self.Emit("MOVQ" , jit.Sib(_ST, _AX, 1, 8), _SP_x) // MOVQ 8(ST)(AX), SP.x
  391. self.Emit("MOVQ" , jit.Sib(_ST, _AX, 1, 16), _SP_f) // MOVQ 16(ST)(AX), SP.f
  392. self.Emit("MOVQ" , jit.Sib(_ST, _AX, 1, 24), _SP_p) // MOVQ 24(ST)(AX), SP.p
  393. self.Emit("MOVQ" , jit.Sib(_ST, _AX, 1, 32), _SP_q) // MOVQ 32(ST)(AX), SP.q
  394. self.Emit("PXOR" , _X0, _X0) // PXOR X0, X0
  395. self.Emit("MOVOU", _X0, jit.Sib(_ST, _AX, 1, 8)) // MOVOU X0, 8(ST)(AX)
  396. self.Emit("MOVOU", _X0, jit.Sib(_ST, _AX, 1, 24)) // MOVOU X0, 24(ST)(AX)
  397. }
  398. /** Buffer Helpers **/
  399. func (self *_Assembler) add_char(ch byte) {
  400. self.Emit("MOVB", jit.Imm(int64(ch)), jit.Sib(_RP, _RL, 1, 0)) // MOVB $ch, (RP)(RL)
  401. self.Emit("ADDQ", jit.Imm(1), _RL) // ADDQ $1, RL
  402. }
  403. func (self *_Assembler) add_long(ch uint32, n int64) {
  404. self.Emit("MOVL", jit.Imm(int64(ch)), jit.Sib(_RP, _RL, 1, 0)) // MOVL $ch, (RP)(RL)
  405. self.Emit("ADDQ", jit.Imm(n), _RL) // ADDQ $n, RL
  406. }
  407. func (self *_Assembler) add_text(ss string) {
  408. self.store_str(ss) // TEXT $ss
  409. self.Emit("ADDQ", jit.Imm(int64(len(ss))), _RL) // ADDQ ${len(ss)}, RL
  410. }
  411. // get *buf at AX
  412. func (self *_Assembler) prep_buffer_AX() {
  413. self.Emit("MOVQ", _ARG_rb, _AX) // MOVQ rb<>+0(FP), AX
  414. self.Emit("MOVQ", _RL, jit.Ptr(_AX, 8)) // MOVQ RL, 8(AX)
  415. }
  416. func (self *_Assembler) save_buffer() {
  417. self.Emit("MOVQ", _ARG_rb, _CX) // MOVQ rb<>+0(FP), CX
  418. self.Emit("MOVQ", _RP, jit.Ptr(_CX, 0)) // MOVQ RP, (CX)
  419. self.Emit("MOVQ", _RL, jit.Ptr(_CX, 8)) // MOVQ RL, 8(CX)
  420. self.Emit("MOVQ", _RC, jit.Ptr(_CX, 16)) // MOVQ RC, 16(CX)
  421. }
  422. // get *buf at AX
  423. func (self *_Assembler) load_buffer_AX() {
  424. self.Emit("MOVQ", _ARG_rb, _AX) // MOVQ rb<>+0(FP), AX
  425. self.Emit("MOVQ", jit.Ptr(_AX, 0), _RP) // MOVQ (AX), RP
  426. self.Emit("MOVQ", jit.Ptr(_AX, 8), _RL) // MOVQ 8(AX), RL
  427. self.Emit("MOVQ", jit.Ptr(_AX, 16), _RC) // MOVQ 16(AX), RC
  428. }
  429. /** Function Interface Helpers **/
  430. func (self *_Assembler) call(pc obj.Addr) {
  431. self.Emit("MOVQ", pc, _LR) // MOVQ $pc, AX
  432. self.Rjmp("CALL", _LR) // CALL AX
  433. }
  434. func (self *_Assembler) save_c() {
  435. self.xsave(_REG_ffi...) // SAVE $REG_ffi
  436. }
  437. func (self *_Assembler) call_b64(pc obj.Addr) {
  438. self.xsave(_REG_b64...) // SAVE $REG_all
  439. self.call(pc) // CALL $pc
  440. self.xload(_REG_b64...) // LOAD $REG_ffi
  441. }
  442. func (self *_Assembler) call_c(pc obj.Addr) {
  443. self.Emit("XCHGQ", _SP_p, _BX)
  444. self.call(pc) // CALL $pc
  445. self.xload(_REG_ffi...) // LOAD $REG_ffi
  446. self.Emit("XCHGQ", _SP_p, _BX)
  447. self.Emit("XORPS", _X15, _X15)
  448. }
  449. func (self *_Assembler) call_go(pc obj.Addr) {
  450. self.xsave(_REG_all...) // SAVE $REG_all
  451. self.call(pc) // CALL $pc
  452. self.xload(_REG_all...) // LOAD $REG_all
  453. }
  454. func (self *_Assembler) call_more_space(pc obj.Addr) {
  455. self.xsave(_REG_ms...) // SAVE $REG_all
  456. self.call(pc) // CALL $pc
  457. self.xload(_REG_ms...) // LOAD $REG_all
  458. }
  459. func (self *_Assembler) call_encoder(pc obj.Addr) {
  460. self.xsave(_REG_enc...) // SAVE $REG_all
  461. self.call(pc) // CALL $pc
  462. self.xload(_REG_enc...) // LOAD $REG_all
  463. }
  464. func (self *_Assembler) call_marshaler(fn obj.Addr, it *rt.GoType, vt reflect.Type) {
  465. switch vt.Kind() {
  466. case reflect.Interface : self.call_marshaler_i(fn, it)
  467. case reflect.Ptr, reflect.Map : self.call_marshaler_v(fn, it, vt, true)
  468. // struct/array of 1 direct iface type can be direct
  469. default : self.call_marshaler_v(fn, it, vt, !rt.UnpackType(vt).Indirect())
  470. }
  471. }
  472. func (self *_Assembler) call_marshaler_i(fn obj.Addr, it *rt.GoType) {
  473. self.Emit("MOVQ" , jit.Ptr(_SP_p, 0), _AX) // MOVQ (SP.p), AX
  474. self.Emit("TESTQ", _AX, _AX) // TESTQ AX, AX
  475. self.Sjmp("JZ" , "_null_{n}") // JZ _null_{n}
  476. self.Emit("MOVQ" , _AX, _BX) // MOVQ AX, BX
  477. self.Emit("MOVQ" , jit.Ptr(_SP_p, 8), _CX) // MOVQ 8(SP.p), CX
  478. self.Emit("MOVQ" , jit.Gtype(it), _AX) // MOVQ $it, AX
  479. self.call_go(_F_assertI2I) // CALL_GO assertI2I
  480. self.Emit("TESTQ", _AX, _AX) // TESTQ AX, AX
  481. self.Sjmp("JZ" , "_null_{n}") // JZ _null_{n}
  482. self.Emit("MOVQ", _BX, _CX) // MOVQ BX, CX
  483. self.Emit("MOVQ", _AX, _BX) // MOVQ AX, BX
  484. self.prep_buffer_AX()
  485. self.Emit("MOVQ", _ARG_fv, _DI) // MOVQ ARG.fv, DI
  486. self.call_go(fn) // CALL $fn
  487. self.Emit("TESTQ", _ET, _ET) // TESTQ ET, ET
  488. self.Sjmp("JNZ" , _LB_error) // JNZ _error
  489. self.load_buffer_AX()
  490. self.Sjmp("JMP" , "_done_{n}") // JMP _done_{n}
  491. self.Link("_null_{n}") // _null_{n}:
  492. self.check_size(4) // SIZE $4
  493. self.Emit("MOVL", jit.Imm(_IM_null), jit.Sib(_RP, _RL, 1, 0)) // MOVL $'null', (RP)(RL*1)
  494. self.Emit("ADDQ", jit.Imm(4), _RL) // ADDQ $4, RL
  495. self.Link("_done_{n}") // _done_{n}:
  496. }
  497. func (self *_Assembler) call_marshaler_v(fn obj.Addr, it *rt.GoType, vt reflect.Type, deref bool) {
  498. self.prep_buffer_AX() // MOVE {buf}, (SP)
  499. self.Emit("MOVQ", jit.Itab(it, vt), _BX) // MOVQ $(itab(it, vt)), BX
  500. /* dereference the pointer if needed */
  501. if !deref {
  502. self.Emit("MOVQ", _SP_p, _CX) // MOVQ SP.p, CX
  503. } else {
  504. self.Emit("MOVQ", jit.Ptr(_SP_p, 0), _CX) // MOVQ 0(SP.p), CX
  505. }
  506. /* call the encoder, and perform error checks */
  507. self.Emit("MOVQ", _ARG_fv, _DI) // MOVQ ARG.fv, DI
  508. self.call_go(fn) // CALL $fn
  509. self.Emit("TESTQ", _ET, _ET) // TESTQ ET, ET
  510. self.Sjmp("JNZ" , _LB_error) // JNZ _error
  511. self.load_buffer_AX()
  512. }
  513. /** Builtin: _more_space **/
  514. var (
  515. _T_byte = jit.Type(byteType)
  516. _F_growslice = jit.Func(growslice)
  517. )
  518. // AX must saving n
  519. func (self *_Assembler) more_space() {
  520. self.Link(_LB_more_space)
  521. self.Emit("MOVQ", _RP, _BX) // MOVQ DI, BX
  522. self.Emit("MOVQ", _RL, _CX) // MOVQ SI, CX
  523. self.Emit("MOVQ", _RC, _DI) // MOVQ DX, DI
  524. self.Emit("MOVQ", _AX, _SI) // MOVQ AX, SI
  525. self.Emit("MOVQ", _T_byte, _AX) // MOVQ $_T_byte, AX
  526. self.call_more_space(_F_growslice) // CALL $pc
  527. self.Emit("MOVQ", _AX, _RP) // MOVQ AX, DI
  528. self.Emit("MOVQ", _BX, _RL) // MOVQ BX, SI
  529. self.Emit("MOVQ", _CX, _RC) // MOVQ CX, DX
  530. self.save_buffer() // SAVE {buf}
  531. self.Rjmp("JMP" , _LR) // JMP LR
  532. }
  533. /** Builtin Errors **/
  534. var (
  535. _V_ERR_too_deep = jit.Imm(int64(uintptr(unsafe.Pointer(_ERR_too_deep))))
  536. _V_ERR_nan_or_infinite = jit.Imm(int64(uintptr(unsafe.Pointer(_ERR_nan_or_infinite))))
  537. _I_json_UnsupportedValueError = jit.Itab(rt.UnpackType(errorType), jsonUnsupportedValueType)
  538. )
  539. func (self *_Assembler) error_too_deep() {
  540. self.Link(_LB_error_too_deep)
  541. self.Emit("MOVQ", _V_ERR_too_deep, _EP) // MOVQ $_V_ERR_too_deep, EP
  542. self.Emit("MOVQ", _I_json_UnsupportedValueError, _ET) // MOVQ $_I_json_UnsupportedValuError, ET
  543. self.Sjmp("JMP" , _LB_error) // JMP _error
  544. }
  545. func (self *_Assembler) error_invalid_number() {
  546. self.Link(_LB_error_invalid_number)
  547. self.Emit("MOVQ", jit.Ptr(_SP_p, 0), _AX) // MOVQ 0(SP), AX
  548. self.Emit("MOVQ", jit.Ptr(_SP_p, 8), _BX) // MOVQ 8(SP), BX
  549. self.call_go(_F_error_number) // CALL_GO error_number
  550. self.Sjmp("JMP" , _LB_error) // JMP _error
  551. }
  552. func (self *_Assembler) error_nan_or_infinite() {
  553. self.Link(_LB_error_nan_or_infinite)
  554. self.Emit("MOVQ", _V_ERR_nan_or_infinite, _EP) // MOVQ $_V_ERR_nan_or_infinite, EP
  555. self.Emit("MOVQ", _I_json_UnsupportedValueError, _ET) // MOVQ $_I_json_UnsupportedValuError, ET
  556. self.Sjmp("JMP" , _LB_error) // JMP _error
  557. }
  558. /** String Encoding Routine **/
  559. var (
  560. _F_quote = jit.Imm(int64(native.S_quote))
  561. _F_panic = jit.Func(goPanic)
  562. )
  563. func (self *_Assembler) go_panic() {
  564. self.Link(_LB_panic)
  565. self.Emit("MOVQ", _SP_p, _BX)
  566. self.call_go(_F_panic)
  567. }
  568. func (self *_Assembler) encode_string(doubleQuote bool) {
  569. self.Emit("MOVQ" , jit.Ptr(_SP_p, 8), _AX) // MOVQ 8(SP.p), AX
  570. self.Emit("TESTQ", _AX, _AX) // TESTQ AX, AX
  571. self.Sjmp("JZ" , "_str_empty_{n}") // JZ _str_empty_{n}
  572. self.Emit("CMPQ", jit.Ptr(_SP_p, 0), jit.Imm(0))
  573. self.Sjmp("JNE" , "_str_next_{n}")
  574. self.Emit("MOVQ", jit.Imm(int64(panicNilPointerOfNonEmptyString)), _AX)
  575. self.Sjmp("JMP", _LB_panic)
  576. self.Link("_str_next_{n}")
  577. /* openning quote, check for double quote */
  578. if !doubleQuote {
  579. self.check_size_r(_AX, 2) // SIZE $2
  580. self.add_char('"') // CHAR $'"'
  581. } else {
  582. self.check_size_r(_AX, 6) // SIZE $6
  583. self.add_long(_IM_open, 3) // TEXT $`"\"`
  584. }
  585. /* quoting loop */
  586. self.Emit("XORL", _AX, _AX) // XORL AX, AX
  587. self.Emit("MOVQ", _AX, _VAR_sp) // MOVQ AX, sp
  588. self.Link("_str_loop_{n}") // _str_loop_{n}:
  589. self.save_c() // SAVE $REG_ffi
  590. /* load the output buffer first, and then input buffer,
  591. * because the parameter registers collide with RP / RL / RC */
  592. self.Emit("MOVQ", _RC, _CX) // MOVQ RC, CX
  593. self.Emit("SUBQ", _RL, _CX) // SUBQ RL, CX
  594. self.Emit("MOVQ", _CX, _VAR_dn) // MOVQ CX, dn
  595. self.Emit("LEAQ", jit.Sib(_RP, _RL, 1, 0), _DX) // LEAQ (RP)(RL), DX
  596. self.Emit("LEAQ", _VAR_dn, _CX) // LEAQ dn, CX
  597. self.Emit("MOVQ", _VAR_sp, _AX) // MOVQ sp, AX
  598. self.Emit("MOVQ", jit.Ptr(_SP_p, 0), _DI) // MOVQ (SP.p), DI
  599. self.Emit("MOVQ", jit.Ptr(_SP_p, 8), _SI) // MOVQ 8(SP.p), SI
  600. self.Emit("ADDQ", _AX, _DI) // ADDQ AX, DI
  601. self.Emit("SUBQ", _AX, _SI) // SUBQ AX, SI
  602. /* set the flags based on `doubleQuote` */
  603. if !doubleQuote {
  604. self.Emit("XORL", _R8, _R8) // XORL R8, R8
  605. } else {
  606. self.Emit("MOVL", jit.Imm(types.F_DOUBLE_UNQUOTE), _R8) // MOVL ${types.F_DOUBLE_UNQUOTE}, R8
  607. }
  608. /* call the native quoter */
  609. self.call_c(_F_quote) // CALL quote
  610. self.Emit("ADDQ" , _VAR_dn, _RL) // ADDQ dn, RL
  611. self.Emit("TESTQ", _AX, _AX) // TESTQ AX, AX
  612. self.Sjmp("JS" , "_str_space_{n}") // JS _str_space_{n}
  613. /* close the string, check for double quote */
  614. if !doubleQuote {
  615. self.check_size(1) // SIZE $1
  616. self.add_char('"') // CHAR $'"'
  617. self.Sjmp("JMP", "_str_end_{n}") // JMP _str_end_{n}
  618. } else {
  619. self.check_size(3) // SIZE $3
  620. self.add_text("\\\"\"") // TEXT $'\""'
  621. self.Sjmp("JMP", "_str_end_{n}") // JMP _str_end_{n}
  622. }
  623. /* not enough space to contain the quoted string */
  624. self.Link("_str_space_{n}") // _str_space_{n}:
  625. self.Emit("NOTQ", _AX) // NOTQ AX
  626. self.Emit("ADDQ", _AX, _VAR_sp) // ADDQ AX, sp
  627. self.Emit("LEAQ", jit.Sib(_RC, _RC, 1, 0), _AX) // LEAQ (RC)(RC), AX
  628. self.slice_grow_ax("_str_loop_{n}") // GROW _str_loop_{n}
  629. /* empty string, check for double quote */
  630. if !doubleQuote {
  631. self.Link("_str_empty_{n}") // _str_empty_{n}:
  632. self.check_size(2) // SIZE $2
  633. self.add_text("\"\"") // TEXT $'""'
  634. self.Link("_str_end_{n}") // _str_end_{n}:
  635. } else {
  636. self.Link("_str_empty_{n}") // _str_empty_{n}:
  637. self.check_size(6) // SIZE $6
  638. self.add_text("\"\\\"\\\"\"") // TEXT $'"\"\""'
  639. self.Link("_str_end_{n}") // _str_end_{n}:
  640. }
  641. }
  642. /** OpCode Assembler Functions **/
  643. var (
  644. _T_json_Marshaler = rt.UnpackType(jsonMarshalerType)
  645. _T_encoding_TextMarshaler = rt.UnpackType(encodingTextMarshalerType)
  646. )
  647. var (
  648. _F_f64toa = jit.Imm(int64(native.S_f64toa))
  649. _F_f32toa = jit.Imm(int64(native.S_f32toa))
  650. _F_i64toa = jit.Imm(int64(native.S_i64toa))
  651. _F_u64toa = jit.Imm(int64(native.S_u64toa))
  652. _F_b64encode = jit.Imm(int64(_subr__b64encode))
  653. )
  654. var (
  655. _F_memmove = jit.Func(memmove)
  656. _F_error_number = jit.Func(error_number)
  657. _F_isValidNumber = jit.Func(isValidNumber)
  658. )
  659. var (
  660. _F_iteratorStop = jit.Func(iteratorStop)
  661. _F_iteratorNext = jit.Func(iteratorNext)
  662. _F_iteratorStart = jit.Func(iteratorStart)
  663. )
  664. var (
  665. _F_encodeTypedPointer obj.Addr
  666. _F_encodeJsonMarshaler obj.Addr
  667. _F_encodeTextMarshaler obj.Addr
  668. )
  669. const (
  670. _MODE_AVX2 = 1 << 2
  671. )
  672. func init() {
  673. _F_encodeTypedPointer = jit.Func(encodeTypedPointer)
  674. _F_encodeJsonMarshaler = jit.Func(encodeJsonMarshaler)
  675. _F_encodeTextMarshaler = jit.Func(encodeTextMarshaler)
  676. }
  677. func (self *_Assembler) _asm_OP_null(_ *_Instr) {
  678. self.check_size(4)
  679. self.Emit("MOVL", jit.Imm(_IM_null), jit.Sib(_RP, _RL, 1, 0)) // MOVL $'null', (RP)(RL*1)
  680. self.Emit("ADDQ", jit.Imm(4), _RL) // ADDQ $4, RL
  681. }
  682. func (self *_Assembler) _asm_OP_empty_arr(_ *_Instr) {
  683. self.Emit("BTQ", jit.Imm(int64(bitNoNullSliceOrMap)), _ARG_fv)
  684. self.Sjmp("JC", "_empty_arr_{n}")
  685. self._asm_OP_null(nil)
  686. self.Sjmp("JMP", "_empty_arr_end_{n}")
  687. self.Link("_empty_arr_{n}")
  688. self.check_size(2)
  689. self.Emit("MOVW", jit.Imm(_IM_array), jit.Sib(_RP, _RL, 1, 0))
  690. self.Emit("ADDQ", jit.Imm(2), _RL)
  691. self.Link("_empty_arr_end_{n}")
  692. }
  693. func (self *_Assembler) _asm_OP_empty_obj(_ *_Instr) {
  694. self.Emit("BTQ", jit.Imm(int64(bitNoNullSliceOrMap)), _ARG_fv)
  695. self.Sjmp("JC", "_empty_obj_{n}")
  696. self._asm_OP_null(nil)
  697. self.Sjmp("JMP", "_empty_obj_end_{n}")
  698. self.Link("_empty_obj_{n}")
  699. self.check_size(2)
  700. self.Emit("MOVW", jit.Imm(_IM_object), jit.Sib(_RP, _RL, 1, 0))
  701. self.Emit("ADDQ", jit.Imm(2), _RL)
  702. self.Link("_empty_obj_end_{n}")
  703. }
  704. func (self *_Assembler) _asm_OP_bool(_ *_Instr) {
  705. self.Emit("CMPB", jit.Ptr(_SP_p, 0), jit.Imm(0)) // CMPB (SP.p), $0
  706. self.Sjmp("JE" , "_false_{n}") // JE _false_{n}
  707. self.check_size(4) // SIZE $4
  708. self.Emit("MOVL", jit.Imm(_IM_true), jit.Sib(_RP, _RL, 1, 0)) // MOVL $'true', (RP)(RL*1)
  709. self.Emit("ADDQ", jit.Imm(4), _RL) // ADDQ $4, RL
  710. self.Sjmp("JMP" , "_end_{n}") // JMP _end_{n}
  711. self.Link("_false_{n}") // _false_{n}:
  712. self.check_size(5) // SIZE $5
  713. self.Emit("MOVL", jit.Imm(_IM_fals), jit.Sib(_RP, _RL, 1, 0)) // MOVL $'fals', (RP)(RL*1)
  714. self.Emit("MOVB", jit.Imm('e'), jit.Sib(_RP, _RL, 1, 4)) // MOVB $'e', 4(RP)(RL*1)
  715. self.Emit("ADDQ", jit.Imm(5), _RL) // ADDQ $5, RL
  716. self.Link("_end_{n}") // _end_{n}:
  717. }
  718. func (self *_Assembler) _asm_OP_i8(_ *_Instr) {
  719. self.store_int(4, _F_i64toa, "MOVBQSX")
  720. }
  721. func (self *_Assembler) _asm_OP_i16(_ *_Instr) {
  722. self.store_int(6, _F_i64toa, "MOVWQSX")
  723. }
  724. func (self *_Assembler) _asm_OP_i32(_ *_Instr) {
  725. self.store_int(17, _F_i64toa, "MOVLQSX")
  726. }
  727. func (self *_Assembler) _asm_OP_i64(_ *_Instr) {
  728. self.store_int(21, _F_i64toa, "MOVQ")
  729. }
  730. func (self *_Assembler) _asm_OP_u8(_ *_Instr) {
  731. self.store_int(3, _F_u64toa, "MOVBQZX")
  732. }
  733. func (self *_Assembler) _asm_OP_u16(_ *_Instr) {
  734. self.store_int(5, _F_u64toa, "MOVWQZX")
  735. }
  736. func (self *_Assembler) _asm_OP_u32(_ *_Instr) {
  737. self.store_int(16, _F_u64toa, "MOVLQZX")
  738. }
  739. func (self *_Assembler) _asm_OP_u64(_ *_Instr) {
  740. self.store_int(20, _F_u64toa, "MOVQ")
  741. }
  742. func (self *_Assembler) _asm_OP_f32(_ *_Instr) {
  743. self.check_size(32)
  744. self.Emit("MOVL" , jit.Ptr(_SP_p, 0), _AX) // MOVL (SP.p), AX
  745. self.Emit("ANDL" , jit.Imm(_FM_exp32), _AX) // ANDL $_FM_exp32, AX
  746. self.Emit("XORL" , jit.Imm(_FM_exp32), _AX) // XORL $_FM_exp32, AX
  747. self.Sjmp("JZ" , _LB_error_nan_or_infinite) // JZ _error_nan_or_infinite
  748. self.save_c() // SAVE $C_regs
  749. self.rbuf_di() // MOVQ RP, DI
  750. self.Emit("MOVSS" , jit.Ptr(_SP_p, 0), _X0) // MOVSS (SP.p), X0
  751. self.call_c(_F_f32toa) // CALL_C f64toa
  752. self.Emit("ADDQ" , _AX, _RL) // ADDQ AX, RL
  753. }
  754. func (self *_Assembler) _asm_OP_f64(_ *_Instr) {
  755. self.check_size(32)
  756. self.Emit("MOVQ" , jit.Ptr(_SP_p, 0), _AX) // MOVQ (SP.p), AX
  757. self.Emit("MOVQ" , jit.Imm(_FM_exp64), _CX) // MOVQ $_FM_exp64, CX
  758. self.Emit("ANDQ" , _CX, _AX) // ANDQ CX, AX
  759. self.Emit("XORQ" , _CX, _AX) // XORQ CX, AX
  760. self.Sjmp("JZ" , _LB_error_nan_or_infinite) // JZ _error_nan_or_infinite
  761. self.save_c() // SAVE $C_regs
  762. self.rbuf_di() // MOVQ RP, DI
  763. self.Emit("MOVSD" , jit.Ptr(_SP_p, 0), _X0) // MOVSD (SP.p), X0
  764. self.call_c(_F_f64toa) // CALL_C f64toa
  765. self.Emit("ADDQ" , _AX, _RL) // ADDQ AX, RL
  766. }
  767. func (self *_Assembler) _asm_OP_str(_ *_Instr) {
  768. self.encode_string(false)
  769. }
  770. func (self *_Assembler) _asm_OP_bin(_ *_Instr) {
  771. self.Emit("MOVQ", jit.Ptr(_SP_p, 8), _AX) // MOVQ 8(SP.p), AX
  772. self.Emit("ADDQ", jit.Imm(2), _AX) // ADDQ $2, AX
  773. self.Emit("MOVQ", jit.Imm(_IM_mulv), _CX) // MOVQ $_MF_mulv, CX
  774. self.Emit("MOVQ", _DX, _BX) // MOVQ DX, BX
  775. self.From("MULQ", _CX) // MULQ CX
  776. self.Emit("LEAQ", jit.Sib(_DX, _DX, 1, 1), _AX) // LEAQ 1(DX)(DX), AX
  777. self.Emit("ORQ" , jit.Imm(2), _AX) // ORQ $2, AX
  778. self.Emit("MOVQ", _BX, _DX) // MOVQ BX, DX
  779. self.check_size_r(_AX, 0) // SIZE AX
  780. self.add_char('"') // CHAR $'"'
  781. self.Emit("MOVQ", _ARG_rb, _DI) // MOVQ rb<>+0(FP), DI
  782. self.Emit("MOVQ", _RL, jit.Ptr(_DI, 8)) // MOVQ SI, 8(DI)
  783. self.Emit("MOVQ", _SP_p, _SI) // MOVQ SP.p, SI
  784. /* check for AVX2 support */
  785. if !cpu.HasAVX2 {
  786. self.Emit("XORL", _DX, _DX) // XORL DX, DX
  787. } else {
  788. self.Emit("MOVL", jit.Imm(_MODE_AVX2), _DX) // MOVL $_MODE_AVX2, DX
  789. }
  790. /* call the encoder */
  791. self.call_b64(_F_b64encode) // CALL b64encode
  792. self.load_buffer_AX() // LOAD {buf}
  793. self.add_char('"') // CHAR $'"'
  794. }
  795. func (self *_Assembler) _asm_OP_quote(_ *_Instr) {
  796. self.encode_string(true)
  797. }
  798. func (self *_Assembler) _asm_OP_number(_ *_Instr) {
  799. self.Emit("MOVQ" , jit.Ptr(_SP_p, 8), _BX) // MOVQ (SP.p), BX
  800. self.Emit("TESTQ", _BX, _BX) // TESTQ BX, BX
  801. self.Sjmp("JZ" , "_empty_{n}")
  802. self.Emit("MOVQ" , jit.Ptr(_SP_p, 0), _AX) // MOVQ (SP.p), AX
  803. self.Emit("TESTQ", _AX, _AX) // TESTQ AX, AX
  804. self.Sjmp("JNZ" , "_number_next_{n}")
  805. self.Emit("MOVQ", jit.Imm(int64(panicNilPointerOfNonEmptyString)), _AX)
  806. self.Sjmp("JMP", _LB_panic)
  807. self.Link("_number_next_{n}")
  808. self.call_go(_F_isValidNumber) // CALL_GO isValidNumber
  809. self.Emit("CMPB" , _AX, jit.Imm(0)) // CMPB AX, $0
  810. self.Sjmp("JE" , _LB_error_invalid_number) // JE _error_invalid_number
  811. self.Emit("MOVQ" , jit.Ptr(_SP_p, 8), _BX) // MOVQ (SP.p), BX
  812. self.check_size_r(_BX, 0) // SIZE BX
  813. self.Emit("LEAQ" , jit.Sib(_RP, _RL, 1, 0), _AX) // LEAQ (RP)(RL), AX
  814. self.Emit("ADDQ" , jit.Ptr(_SP_p, 8), _RL) // ADDQ 8(SP.p), RL
  815. self.Emit("MOVQ", jit.Ptr(_SP_p, 0), _BX) // MOVOU (SP.p), BX
  816. self.Emit("MOVQ", jit.Ptr(_SP_p, 8), _CX) // MOVOU X0, 8(SP)
  817. self.call_go(_F_memmove) // CALL_GO memmove
  818. self.Emit("MOVQ", _ARG_rb, _AX) // MOVQ rb<>+0(FP), AX
  819. self.Emit("MOVQ", _RL, jit.Ptr(_AX, 8)) // MOVQ RL, 8(AX)
  820. self.Sjmp("JMP" , "_done_{n}") // JMP _done_{n}
  821. self.Link("_empty_{n}") // _empty_{n}
  822. self.check_size(1) // SIZE $1
  823. self.add_char('0') // CHAR $'0'
  824. self.Link("_done_{n}") // _done_{n}:
  825. }
  826. func (self *_Assembler) _asm_OP_eface(_ *_Instr) {
  827. self.prep_buffer_AX() // MOVE {buf}, AX
  828. self.Emit("MOVQ" , jit.Ptr(_SP_p, 0), _BX) // MOVQ (SP.p), BX
  829. self.Emit("LEAQ" , jit.Ptr(_SP_p, 8), _CX) // LEAQ 8(SP.p), CX
  830. self.Emit("MOVQ" , _ST, _DI) // MOVQ ST, DI
  831. self.Emit("MOVQ" , _ARG_fv, _SI) // MOVQ fv, AX
  832. self.call_encoder(_F_encodeTypedPointer) // CALL encodeTypedPointer
  833. self.Emit("TESTQ", _ET, _ET) // TESTQ ET, ET
  834. self.Sjmp("JNZ" , _LB_error) // JNZ _error
  835. self.load_buffer_AX()
  836. }
  837. func (self *_Assembler) _asm_OP_iface(_ *_Instr) {
  838. self.prep_buffer_AX() // MOVE {buf}, AX
  839. self.Emit("MOVQ" , jit.Ptr(_SP_p, 0), _CX) // MOVQ (SP.p), CX
  840. self.Emit("MOVQ" , jit.Ptr(_CX, 8), _BX) // MOVQ 8(CX), BX
  841. self.Emit("LEAQ" , jit.Ptr(_SP_p, 8), _CX) // LEAQ 8(SP.p), CX
  842. self.Emit("MOVQ" , _ST, _DI) // MOVQ ST, DI
  843. self.Emit("MOVQ" , _ARG_fv, _SI) // MOVQ fv, AX
  844. self.call_encoder(_F_encodeTypedPointer) // CALL encodeTypedPointer
  845. self.Emit("TESTQ", _ET, _ET) // TESTQ ET, ET
  846. self.Sjmp("JNZ" , _LB_error) // JNZ _error
  847. self.load_buffer_AX()
  848. }
  849. func (self *_Assembler) _asm_OP_byte(p *_Instr) {
  850. self.check_size(1)
  851. self.Emit("MOVB", jit.Imm(p.i64()), jit.Sib(_RP, _RL, 1, 0)) // MOVL p.vi(), (RP)(RL*1)
  852. self.Emit("ADDQ", jit.Imm(1), _RL) // ADDQ $1, RL
  853. }
  854. func (self *_Assembler) _asm_OP_text(p *_Instr) {
  855. self.check_size(len(p.vs())) // SIZE ${len(p.vs())}
  856. self.add_text(p.vs()) // TEXT ${p.vs()}
  857. }
  858. func (self *_Assembler) _asm_OP_deref(_ *_Instr) {
  859. self.Emit("MOVQ", jit.Ptr(_SP_p, 0), _SP_p) // MOVQ (SP.p), SP.p
  860. }
  861. func (self *_Assembler) _asm_OP_index(p *_Instr) {
  862. self.Emit("MOVQ", jit.Imm(p.i64()), _AX) // MOVQ $p.vi(), AX
  863. self.Emit("ADDQ", _AX, _SP_p) // ADDQ AX, SP.p
  864. }
  865. func (self *_Assembler) _asm_OP_load(_ *_Instr) {
  866. self.Emit("MOVQ", jit.Ptr(_ST, 0), _AX) // MOVQ (ST), AX
  867. self.Emit("MOVQ", jit.Sib(_ST, _AX, 1, -24), _SP_x) // MOVQ -24(ST)(AX), SP.x
  868. self.Emit("MOVQ", jit.Sib(_ST, _AX, 1, -8), _SP_p) // MOVQ -8(ST)(AX), SP.p
  869. self.Emit("MOVQ", jit.Sib(_ST, _AX, 1, 0), _SP_q) // MOVQ (ST)(AX), SP.q
  870. }
  871. func (self *_Assembler) _asm_OP_save(_ *_Instr) {
  872. self.save_state()
  873. }
  874. func (self *_Assembler) _asm_OP_drop(_ *_Instr) {
  875. self.drop_state(_StateSize)
  876. }
  877. func (self *_Assembler) _asm_OP_drop_2(_ *_Instr) {
  878. self.drop_state(_StateSize * 2) // DROP $(_StateSize * 2)
  879. self.Emit("MOVOU", _X0, jit.Sib(_ST, _AX, 1, 56)) // MOVOU X0, 56(ST)(AX)
  880. }
  881. func (self *_Assembler) _asm_OP_recurse(p *_Instr) {
  882. self.prep_buffer_AX() // MOVE {buf}, (SP)
  883. vt, pv := p.vp()
  884. self.Emit("MOVQ", jit.Type(vt), _BX) // MOVQ $(type(p.vt())), BX
  885. /* check for indirection */
  886. if !rt.UnpackType(vt).Indirect() {
  887. self.Emit("MOVQ", _SP_p, _CX) // MOVQ SP.p, CX
  888. } else {
  889. self.Emit("MOVQ", _SP_p, _VAR_vp) // MOVQ SP.p, VAR.vp
  890. self.Emit("LEAQ", _VAR_vp, _CX) // LEAQ VAR.vp, CX
  891. }
  892. /* call the encoder */
  893. self.Emit("MOVQ" , _ST, _DI) // MOVQ ST, DI
  894. self.Emit("MOVQ" , _ARG_fv, _SI) // MOVQ $fv, SI
  895. if pv {
  896. self.Emit("BTCQ", jit.Imm(bitPointerValue), _SI) // BTCQ $1, SI
  897. }
  898. self.call_encoder(_F_encodeTypedPointer) // CALL encodeTypedPointer
  899. self.Emit("TESTQ", _ET, _ET) // TESTQ ET, ET
  900. self.Sjmp("JNZ" , _LB_error) // JNZ _error
  901. self.load_buffer_AX()
  902. }
  903. func (self *_Assembler) _asm_OP_is_nil(p *_Instr) {
  904. self.Emit("CMPQ", jit.Ptr(_SP_p, 0), jit.Imm(0)) // CMPQ (SP.p), $0
  905. self.Xjmp("JE" , p.vi()) // JE p.vi()
  906. }
  907. func (self *_Assembler) _asm_OP_is_nil_p1(p *_Instr) {
  908. self.Emit("CMPQ", jit.Ptr(_SP_p, 8), jit.Imm(0)) // CMPQ 8(SP.p), $0
  909. self.Xjmp("JE" , p.vi()) // JE p.vi()
  910. }
  911. func (self *_Assembler) _asm_OP_is_zero_1(p *_Instr) {
  912. self.Emit("CMPB", jit.Ptr(_SP_p, 0), jit.Imm(0)) // CMPB (SP.p), $0
  913. self.Xjmp("JE" , p.vi()) // JE p.vi()
  914. }
  915. func (self *_Assembler) _asm_OP_is_zero_2(p *_Instr) {
  916. self.Emit("CMPW", jit.Ptr(_SP_p, 0), jit.Imm(0)) // CMPW (SP.p), $0
  917. self.Xjmp("JE" , p.vi()) // JE p.vi()
  918. }
  919. func (self *_Assembler) _asm_OP_is_zero_4(p *_Instr) {
  920. self.Emit("CMPL", jit.Ptr(_SP_p, 0), jit.Imm(0)) // CMPL (SP.p), $0
  921. self.Xjmp("JE" , p.vi()) // JE p.vi()
  922. }
  923. func (self *_Assembler) _asm_OP_is_zero_8(p *_Instr) {
  924. self.Emit("CMPQ", jit.Ptr(_SP_p, 0), jit.Imm(0)) // CMPQ (SP.p), $0
  925. self.Xjmp("JE" , p.vi()) // JE p.vi()
  926. }
  927. func (self *_Assembler) _asm_OP_is_zero_map(p *_Instr) {
  928. self.Emit("MOVQ" , jit.Ptr(_SP_p, 0), _AX) // MOVQ (SP.p), AX
  929. self.Emit("TESTQ", _AX, _AX) // TESTQ AX, AX
  930. self.Xjmp("JZ" , p.vi()) // JZ p.vi()
  931. self.Emit("CMPQ" , jit.Ptr(_AX, 0), jit.Imm(0)) // CMPQ (AX), $0
  932. self.Xjmp("JE" , p.vi()) // JE p.vi()
  933. }
  934. func (self *_Assembler) _asm_OP_goto(p *_Instr) {
  935. self.Xjmp("JMP", p.vi())
  936. }
  937. func (self *_Assembler) _asm_OP_map_iter(p *_Instr) {
  938. self.Emit("MOVQ" , jit.Type(p.vt()), _AX) // MOVQ $p.vt(), AX
  939. self.Emit("MOVQ" , jit.Ptr(_SP_p, 0), _BX) // MOVQ (SP.p), BX
  940. self.Emit("MOVQ" , _ARG_fv, _CX) // MOVQ fv, CX
  941. self.call_go(_F_iteratorStart) // CALL_GO iteratorStart
  942. self.Emit("MOVQ" , _AX, _SP_q) // MOVQ AX, SP.q
  943. self.Emit("MOVQ" , _BX, _ET) // MOVQ 32(SP), ET
  944. self.Emit("MOVQ" , _CX, _EP) // MOVQ 40(SP), EP
  945. self.Emit("TESTQ", _ET, _ET) // TESTQ ET, ET
  946. self.Sjmp("JNZ" , _LB_error) // JNZ _error
  947. }
  948. func (self *_Assembler) _asm_OP_map_stop(_ *_Instr) {
  949. self.Emit("MOVQ", _SP_q, _AX) // MOVQ SP.q, AX
  950. self.call_go(_F_iteratorStop) // CALL_GO iteratorStop
  951. self.Emit("XORL", _SP_q, _SP_q) // XORL SP.q, SP.q
  952. }
  953. func (self *_Assembler) _asm_OP_map_check_key(p *_Instr) {
  954. self.Emit("MOVQ" , jit.Ptr(_SP_q, 0), _SP_p) // MOVQ (SP.q), SP.p
  955. self.Emit("TESTQ", _SP_p, _SP_p) // TESTQ SP.p, SP.p
  956. self.Xjmp("JZ" , p.vi()) // JNZ p.vi()
  957. }
  958. func (self *_Assembler) _asm_OP_map_write_key(p *_Instr) {
  959. self.Emit("BTQ", jit.Imm(bitSortMapKeys), _ARG_fv) // BTQ ${SortMapKeys}, fv
  960. self.Sjmp("JNC", "_unordered_key_{n}") // JNC _unordered_key_{n}
  961. self.encode_string(false) // STR $false
  962. self.Xjmp("JMP", p.vi()) // JMP ${p.vi()}
  963. self.Link("_unordered_key_{n}") // _unordered_key_{n}:
  964. }
  965. func (self *_Assembler) _asm_OP_map_value_next(_ *_Instr) {
  966. self.Emit("MOVQ", jit.Ptr(_SP_q, 8), _SP_p) // MOVQ 8(SP.q), SP.p
  967. self.Emit("MOVQ", _SP_q, _AX) // MOVQ SP.q, AX
  968. self.call_go(_F_iteratorNext) // CALL_GO iteratorNext
  969. }
  970. func (self *_Assembler) _asm_OP_slice_len(_ *_Instr) {
  971. self.Emit("MOVQ" , jit.Ptr(_SP_p, 8), _SP_x) // MOVQ 8(SP.p), SP.x
  972. self.Emit("MOVQ" , jit.Ptr(_SP_p, 0), _SP_p) // MOVQ (SP.p), SP.p
  973. self.Emit("ORQ" , jit.Imm(1 << _S_init), _SP_f) // ORQ $(1<<_S_init), SP.f
  974. }
  975. func (self *_Assembler) _asm_OP_slice_next(p *_Instr) {
  976. self.Emit("TESTQ" , _SP_x, _SP_x) // TESTQ SP.x, SP.x
  977. self.Xjmp("JZ" , p.vi()) // JZ p.vi()
  978. self.Emit("SUBQ" , jit.Imm(1), _SP_x) // SUBQ $1, SP.x
  979. self.Emit("BTRQ" , jit.Imm(_S_init), _SP_f) // BTRQ $_S_init, SP.f
  980. self.Emit("LEAQ" , jit.Ptr(_SP_p, int64(p.vlen())), _AX) // LEAQ $(p.vlen())(SP.p), AX
  981. self.Emit("CMOVQCC", _AX, _SP_p) // CMOVQNC AX, SP.p
  982. }
  983. func (self *_Assembler) _asm_OP_marshal(p *_Instr) {
  984. self.call_marshaler(_F_encodeJsonMarshaler, _T_json_Marshaler, p.vt())
  985. }
  986. func (self *_Assembler) _asm_OP_marshal_p(p *_Instr) {
  987. if p.vk() != reflect.Ptr {
  988. panic("marshal_p: invalid type")
  989. } else {
  990. self.call_marshaler_v(_F_encodeJsonMarshaler, _T_json_Marshaler, p.vt(), false)
  991. }
  992. }
  993. func (self *_Assembler) _asm_OP_marshal_text(p *_Instr) {
  994. self.call_marshaler(_F_encodeTextMarshaler, _T_encoding_TextMarshaler, p.vt())
  995. }
  996. func (self *_Assembler) _asm_OP_marshal_text_p(p *_Instr) {
  997. if p.vk() != reflect.Ptr {
  998. panic("marshal_text_p: invalid type")
  999. } else {
  1000. self.call_marshaler_v(_F_encodeTextMarshaler, _T_encoding_TextMarshaler, p.vt(), false)
  1001. }
  1002. }
  1003. func (self *_Assembler) _asm_OP_cond_set(_ *_Instr) {
  1004. self.Emit("ORQ", jit.Imm(1 << _S_cond), _SP_f) // ORQ $(1<<_S_cond), SP.f
  1005. }
  1006. func (self *_Assembler) _asm_OP_cond_testc(p *_Instr) {
  1007. self.Emit("BTRQ", jit.Imm(_S_cond), _SP_f) // BTRQ $_S_cond, SP.f
  1008. self.Xjmp("JC" , p.vi())
  1009. }
  1010. func (self *_Assembler) print_gc(i int, p1 *_Instr, p2 *_Instr) {
  1011. self.Emit("MOVQ", jit.Imm(int64(p2.op())), _CX) // MOVQ $(p2.op()), AX
  1012. self.Emit("MOVQ", jit.Imm(int64(p1.op())), _BX) // MOVQ $(p1.op()), BX
  1013. self.Emit("MOVQ", jit.Imm(int64(i)), _AX) // MOVQ $(i), CX
  1014. self.call_go(_F_println)
  1015. }