compiler.go 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229
  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 jitdec
  17. import (
  18. `encoding/json`
  19. `fmt`
  20. `reflect`
  21. `sort`
  22. `strconv`
  23. `strings`
  24. `unsafe`
  25. `github.com/bytedance/sonic/internal/caching`
  26. `github.com/bytedance/sonic/internal/resolver`
  27. `github.com/bytedance/sonic/internal/rt`
  28. `github.com/bytedance/sonic/option`
  29. )
  30. type _Op uint8
  31. const (
  32. _OP_any _Op = iota + 1
  33. _OP_dyn
  34. _OP_str
  35. _OP_bin
  36. _OP_bool
  37. _OP_num
  38. _OP_i8
  39. _OP_i16
  40. _OP_i32
  41. _OP_i64
  42. _OP_u8
  43. _OP_u16
  44. _OP_u32
  45. _OP_u64
  46. _OP_f32
  47. _OP_f64
  48. _OP_unquote
  49. _OP_nil_1
  50. _OP_nil_2
  51. _OP_nil_3
  52. _OP_empty_bytes
  53. _OP_deref
  54. _OP_index
  55. _OP_is_null
  56. _OP_is_null_quote
  57. _OP_map_init
  58. _OP_map_key_i8
  59. _OP_map_key_i16
  60. _OP_map_key_i32
  61. _OP_map_key_i64
  62. _OP_map_key_u8
  63. _OP_map_key_u16
  64. _OP_map_key_u32
  65. _OP_map_key_u64
  66. _OP_map_key_f32
  67. _OP_map_key_f64
  68. _OP_map_key_str
  69. _OP_map_key_utext
  70. _OP_map_key_utext_p
  71. _OP_array_skip
  72. _OP_array_clear
  73. _OP_array_clear_p
  74. _OP_slice_init
  75. _OP_slice_append
  76. _OP_object_next
  77. _OP_struct_field
  78. _OP_unmarshal
  79. _OP_unmarshal_p
  80. _OP_unmarshal_text
  81. _OP_unmarshal_text_p
  82. _OP_lspace
  83. _OP_match_char
  84. _OP_check_char
  85. _OP_load
  86. _OP_save
  87. _OP_drop
  88. _OP_drop_2
  89. _OP_recurse
  90. _OP_goto
  91. _OP_switch
  92. _OP_check_char_0
  93. _OP_dismatch_err
  94. _OP_go_skip
  95. _OP_skip_emtpy
  96. _OP_add
  97. _OP_check_empty
  98. _OP_unsupported
  99. _OP_debug
  100. )
  101. const (
  102. _INT_SIZE = 32 << (^uint(0) >> 63)
  103. _PTR_SIZE = 32 << (^uintptr(0) >> 63)
  104. _PTR_BYTE = unsafe.Sizeof(uintptr(0))
  105. )
  106. const (
  107. _MAX_ILBUF = 100000 // cutoff at 100k of IL instructions
  108. _MAX_FIELDS = 50 // cutoff at 50 fields struct
  109. )
  110. var _OpNames = [256]string {
  111. _OP_any : "any",
  112. _OP_dyn : "dyn",
  113. _OP_str : "str",
  114. _OP_bin : "bin",
  115. _OP_bool : "bool",
  116. _OP_num : "num",
  117. _OP_i8 : "i8",
  118. _OP_i16 : "i16",
  119. _OP_i32 : "i32",
  120. _OP_i64 : "i64",
  121. _OP_u8 : "u8",
  122. _OP_u16 : "u16",
  123. _OP_u32 : "u32",
  124. _OP_u64 : "u64",
  125. _OP_f32 : "f32",
  126. _OP_f64 : "f64",
  127. _OP_unquote : "unquote",
  128. _OP_nil_1 : "nil_1",
  129. _OP_nil_2 : "nil_2",
  130. _OP_nil_3 : "nil_3",
  131. _OP_empty_bytes : "empty bytes",
  132. _OP_deref : "deref",
  133. _OP_index : "index",
  134. _OP_is_null : "is_null",
  135. _OP_is_null_quote : "is_null_quote",
  136. _OP_map_init : "map_init",
  137. _OP_map_key_i8 : "map_key_i8",
  138. _OP_map_key_i16 : "map_key_i16",
  139. _OP_map_key_i32 : "map_key_i32",
  140. _OP_map_key_i64 : "map_key_i64",
  141. _OP_map_key_u8 : "map_key_u8",
  142. _OP_map_key_u16 : "map_key_u16",
  143. _OP_map_key_u32 : "map_key_u32",
  144. _OP_map_key_u64 : "map_key_u64",
  145. _OP_map_key_f32 : "map_key_f32",
  146. _OP_map_key_f64 : "map_key_f64",
  147. _OP_map_key_str : "map_key_str",
  148. _OP_map_key_utext : "map_key_utext",
  149. _OP_map_key_utext_p : "map_key_utext_p",
  150. _OP_array_skip : "array_skip",
  151. _OP_slice_init : "slice_init",
  152. _OP_slice_append : "slice_append",
  153. _OP_object_next : "object_next",
  154. _OP_struct_field : "struct_field",
  155. _OP_unmarshal : "unmarshal",
  156. _OP_unmarshal_p : "unmarshal_p",
  157. _OP_unmarshal_text : "unmarshal_text",
  158. _OP_unmarshal_text_p : "unmarshal_text_p",
  159. _OP_lspace : "lspace",
  160. _OP_match_char : "match_char",
  161. _OP_check_char : "check_char",
  162. _OP_load : "load",
  163. _OP_save : "save",
  164. _OP_drop : "drop",
  165. _OP_drop_2 : "drop_2",
  166. _OP_recurse : "recurse",
  167. _OP_goto : "goto",
  168. _OP_switch : "switch",
  169. _OP_check_char_0 : "check_char_0",
  170. _OP_dismatch_err : "dismatch_err",
  171. _OP_add : "add",
  172. _OP_go_skip : "go_skip",
  173. _OP_check_empty : "check_empty",
  174. _OP_unsupported : "unsupported type",
  175. _OP_debug : "debug",
  176. }
  177. func (self _Op) String() string {
  178. if ret := _OpNames[self]; ret != "" {
  179. return ret
  180. } else {
  181. return "<invalid>"
  182. }
  183. }
  184. func _OP_int() _Op {
  185. switch _INT_SIZE {
  186. case 32: return _OP_i32
  187. case 64: return _OP_i64
  188. default: panic("unsupported int size")
  189. }
  190. }
  191. func _OP_uint() _Op {
  192. switch _INT_SIZE {
  193. case 32: return _OP_u32
  194. case 64: return _OP_u64
  195. default: panic("unsupported uint size")
  196. }
  197. }
  198. func _OP_uintptr() _Op {
  199. switch _PTR_SIZE {
  200. case 32: return _OP_u32
  201. case 64: return _OP_u64
  202. default: panic("unsupported pointer size")
  203. }
  204. }
  205. func _OP_map_key_int() _Op {
  206. switch _INT_SIZE {
  207. case 32: return _OP_map_key_i32
  208. case 64: return _OP_map_key_i64
  209. default: panic("unsupported int size")
  210. }
  211. }
  212. func _OP_map_key_uint() _Op {
  213. switch _INT_SIZE {
  214. case 32: return _OP_map_key_u32
  215. case 64: return _OP_map_key_u64
  216. default: panic("unsupported uint size")
  217. }
  218. }
  219. func _OP_map_key_uintptr() _Op {
  220. switch _PTR_SIZE {
  221. case 32: return _OP_map_key_u32
  222. case 64: return _OP_map_key_u64
  223. default: panic("unsupported pointer size")
  224. }
  225. }
  226. type _Instr struct {
  227. u uint64 // union {op: 8, vb: 8, vi: 48}, iv maybe int or len([]int)
  228. p unsafe.Pointer // maybe GoSlice.Data, *GoType or *caching.FieldMap
  229. }
  230. func packOp(op _Op) uint64 {
  231. return uint64(op) << 56
  232. }
  233. func newInsOp(op _Op) _Instr {
  234. return _Instr{u: packOp(op)}
  235. }
  236. func newInsVi(op _Op, vi int) _Instr {
  237. return _Instr{u: packOp(op) | rt.PackInt(vi)}
  238. }
  239. func newInsVb(op _Op, vb byte) _Instr {
  240. return _Instr{u: packOp(op) | (uint64(vb) << 48)}
  241. }
  242. func newInsVs(op _Op, vs []int) _Instr {
  243. return _Instr {
  244. u: packOp(op) | rt.PackInt(len(vs)),
  245. p: (*rt.GoSlice)(unsafe.Pointer(&vs)).Ptr,
  246. }
  247. }
  248. func newInsVt(op _Op, vt reflect.Type) _Instr {
  249. return _Instr {
  250. u: packOp(op),
  251. p: unsafe.Pointer(rt.UnpackType(vt)),
  252. }
  253. }
  254. func newInsVtI(op _Op, vt reflect.Type, iv int) _Instr {
  255. return _Instr {
  256. u: packOp(op) | rt.PackInt(iv),
  257. p: unsafe.Pointer(rt.UnpackType(vt)),
  258. }
  259. }
  260. func newInsVf(op _Op, vf *caching.FieldMap) _Instr {
  261. return _Instr {
  262. u: packOp(op),
  263. p: unsafe.Pointer(vf),
  264. }
  265. }
  266. func (self _Instr) op() _Op {
  267. return _Op(self.u >> 56)
  268. }
  269. func (self _Instr) vi() int {
  270. return rt.UnpackInt(self.u)
  271. }
  272. func (self _Instr) vb() byte {
  273. return byte(self.u >> 48)
  274. }
  275. func (self _Instr) vs() (v []int) {
  276. (*rt.GoSlice)(unsafe.Pointer(&v)).Ptr = self.p
  277. (*rt.GoSlice)(unsafe.Pointer(&v)).Cap = self.vi()
  278. (*rt.GoSlice)(unsafe.Pointer(&v)).Len = self.vi()
  279. return
  280. }
  281. func (self _Instr) vf() *caching.FieldMap {
  282. return (*caching.FieldMap)(self.p)
  283. }
  284. func (self _Instr) vk() reflect.Kind {
  285. return (*rt.GoType)(self.p).Kind()
  286. }
  287. func (self _Instr) vt() reflect.Type {
  288. return (*rt.GoType)(self.p).Pack()
  289. }
  290. func (self _Instr) i64() int64 {
  291. return int64(self.vi())
  292. }
  293. func (self _Instr) vlen() int {
  294. return int((*rt.GoType)(self.p).Size)
  295. }
  296. func (self _Instr) isBranch() bool {
  297. switch self.op() {
  298. case _OP_goto : fallthrough
  299. case _OP_switch : fallthrough
  300. case _OP_is_null : fallthrough
  301. case _OP_is_null_quote : fallthrough
  302. case _OP_check_char : return true
  303. default : return false
  304. }
  305. }
  306. func (self _Instr) disassemble() string {
  307. switch self.op() {
  308. case _OP_dyn : fallthrough
  309. case _OP_deref : fallthrough
  310. case _OP_map_key_i8 : fallthrough
  311. case _OP_map_key_i16 : fallthrough
  312. case _OP_map_key_i32 : fallthrough
  313. case _OP_map_key_i64 : fallthrough
  314. case _OP_map_key_u8 : fallthrough
  315. case _OP_map_key_u16 : fallthrough
  316. case _OP_map_key_u32 : fallthrough
  317. case _OP_map_key_u64 : fallthrough
  318. case _OP_map_key_f32 : fallthrough
  319. case _OP_map_key_f64 : fallthrough
  320. case _OP_map_key_str : fallthrough
  321. case _OP_map_key_utext : fallthrough
  322. case _OP_map_key_utext_p : fallthrough
  323. case _OP_slice_init : fallthrough
  324. case _OP_slice_append : fallthrough
  325. case _OP_unmarshal : fallthrough
  326. case _OP_unmarshal_p : fallthrough
  327. case _OP_unmarshal_text : fallthrough
  328. case _OP_unmarshal_text_p : fallthrough
  329. case _OP_recurse : return fmt.Sprintf("%-18s%s", self.op(), self.vt())
  330. case _OP_goto : fallthrough
  331. case _OP_is_null_quote : fallthrough
  332. case _OP_is_null : return fmt.Sprintf("%-18sL_%d", self.op(), self.vi())
  333. case _OP_index : fallthrough
  334. case _OP_array_clear : fallthrough
  335. case _OP_array_clear_p : return fmt.Sprintf("%-18s%d", self.op(), self.vi())
  336. case _OP_switch : return fmt.Sprintf("%-18s%s", self.op(), self.formatSwitchLabels())
  337. case _OP_struct_field : return fmt.Sprintf("%-18s%s", self.op(), self.formatStructFields())
  338. case _OP_match_char : return fmt.Sprintf("%-18s%s", self.op(), strconv.QuoteRune(rune(self.vb())))
  339. case _OP_check_char : return fmt.Sprintf("%-18sL_%d, %s", self.op(), self.vi(), strconv.QuoteRune(rune(self.vb())))
  340. default : return self.op().String()
  341. }
  342. }
  343. func (self _Instr) formatSwitchLabels() string {
  344. var i int
  345. var v int
  346. var m []string
  347. /* format each label */
  348. for i, v = range self.vs() {
  349. m = append(m, fmt.Sprintf("%d=L_%d", i, v))
  350. }
  351. /* join them with "," */
  352. return strings.Join(m, ", ")
  353. }
  354. func (self _Instr) formatStructFields() string {
  355. var i uint64
  356. var r []string
  357. var m []struct{i int; n string}
  358. /* extract all the fields */
  359. for i = 0; i < self.vf().N; i++ {
  360. if v := self.vf().At(i); v.Hash != 0 {
  361. m = append(m, struct{i int; n string}{i: v.ID, n: v.Name})
  362. }
  363. }
  364. /* sort by field name */
  365. sort.Slice(m, func(i, j int) bool {
  366. return m[i].n < m[j].n
  367. })
  368. /* format each field */
  369. for _, v := range m {
  370. r = append(r, fmt.Sprintf("%s=%d", v.n, v.i))
  371. }
  372. /* join them with "," */
  373. return strings.Join(r, ", ")
  374. }
  375. type (
  376. _Program []_Instr
  377. )
  378. func (self _Program) pc() int {
  379. return len(self)
  380. }
  381. func (self _Program) tag(n int) {
  382. if n >= _MaxStack {
  383. panic("type nesting too deep")
  384. }
  385. }
  386. func (self _Program) pin(i int) {
  387. v := &self[i]
  388. v.u &= 0xffff000000000000
  389. v.u |= rt.PackInt(self.pc())
  390. }
  391. func (self _Program) rel(v []int) {
  392. for _, i := range v {
  393. self.pin(i)
  394. }
  395. }
  396. func (self *_Program) add(op _Op) {
  397. *self = append(*self, newInsOp(op))
  398. }
  399. func (self *_Program) int(op _Op, vi int) {
  400. *self = append(*self, newInsVi(op, vi))
  401. }
  402. func (self *_Program) chr(op _Op, vb byte) {
  403. *self = append(*self, newInsVb(op, vb))
  404. }
  405. func (self *_Program) tab(op _Op, vs []int) {
  406. *self = append(*self, newInsVs(op, vs))
  407. }
  408. func (self *_Program) rtt(op _Op, vt reflect.Type) {
  409. *self = append(*self, newInsVt(op, vt))
  410. }
  411. func (self *_Program) rtti(op _Op, vt reflect.Type, iv int) {
  412. *self = append(*self, newInsVtI(op, vt, iv))
  413. }
  414. func (self *_Program) fmv(op _Op, vf *caching.FieldMap) {
  415. *self = append(*self, newInsVf(op, vf))
  416. }
  417. func (self _Program) disassemble() string {
  418. nb := len(self)
  419. tab := make([]bool, nb + 1)
  420. ret := make([]string, 0, nb + 1)
  421. /* prescan to get all the labels */
  422. for _, ins := range self {
  423. if ins.isBranch() {
  424. if ins.op() != _OP_switch {
  425. tab[ins.vi()] = true
  426. } else {
  427. for _, v := range ins.vs() {
  428. tab[v] = true
  429. }
  430. }
  431. }
  432. }
  433. /* disassemble each instruction */
  434. for i, ins := range self {
  435. if !tab[i] {
  436. ret = append(ret, "\t" + ins.disassemble())
  437. } else {
  438. ret = append(ret, fmt.Sprintf("L_%d:\n\t%s", i, ins.disassemble()))
  439. }
  440. }
  441. /* add the last label, if needed */
  442. if tab[nb] {
  443. ret = append(ret, fmt.Sprintf("L_%d:", nb))
  444. }
  445. /* add an "end" indicator, and join all the strings */
  446. return strings.Join(append(ret, "\tend"), "\n")
  447. }
  448. type _Compiler struct {
  449. opts option.CompileOptions
  450. tab map[reflect.Type]bool
  451. rec map[reflect.Type]bool
  452. }
  453. func newCompiler() *_Compiler {
  454. return &_Compiler {
  455. opts: option.DefaultCompileOptions(),
  456. tab: map[reflect.Type]bool{},
  457. rec: map[reflect.Type]bool{},
  458. }
  459. }
  460. func (self *_Compiler) apply(opts option.CompileOptions) *_Compiler {
  461. self.opts = opts
  462. return self
  463. }
  464. func (self *_Compiler) rescue(ep *error) {
  465. if val := recover(); val != nil {
  466. if err, ok := val.(error); ok {
  467. *ep = err
  468. } else {
  469. panic(val)
  470. }
  471. }
  472. }
  473. func (self *_Compiler) compile(vt reflect.Type) (ret _Program, err error) {
  474. defer self.rescue(&err)
  475. self.compileOne(&ret, 0, vt)
  476. return
  477. }
  478. const (
  479. checkMarshalerFlags_quoted = 1
  480. )
  481. func (self *_Compiler) checkMarshaler(p *_Program, vt reflect.Type, flags int, exec bool) bool {
  482. pt := reflect.PtrTo(vt)
  483. /* check for `json.Unmarshaler` with pointer receiver */
  484. if pt.Implements(jsonUnmarshalerType) {
  485. if exec {
  486. p.add(_OP_lspace)
  487. p.rtti(_OP_unmarshal_p, pt, flags)
  488. }
  489. return true
  490. }
  491. /* check for `json.Unmarshaler` */
  492. if vt.Implements(jsonUnmarshalerType) {
  493. if exec {
  494. p.add(_OP_lspace)
  495. self.compileUnmarshalJson(p, vt, flags)
  496. }
  497. return true
  498. }
  499. if flags == checkMarshalerFlags_quoted {
  500. // text marshaler shouldn't be supported for quoted string
  501. return false
  502. }
  503. /* check for `encoding.TextMarshaler` with pointer receiver */
  504. if pt.Implements(encodingTextUnmarshalerType) {
  505. if exec {
  506. p.add(_OP_lspace)
  507. self.compileUnmarshalTextPtr(p, pt, flags)
  508. }
  509. return true
  510. }
  511. /* check for `encoding.TextUnmarshaler` */
  512. if vt.Implements(encodingTextUnmarshalerType) {
  513. if exec {
  514. p.add(_OP_lspace)
  515. self.compileUnmarshalText(p, vt, flags)
  516. }
  517. return true
  518. }
  519. return false
  520. }
  521. func (self *_Compiler) compileOne(p *_Program, sp int, vt reflect.Type) {
  522. /* check for recursive nesting */
  523. ok := self.tab[vt]
  524. if ok {
  525. p.rtt(_OP_recurse, vt)
  526. return
  527. }
  528. if self.checkMarshaler(p, vt, 0, true) {
  529. return
  530. }
  531. /* enter the recursion */
  532. p.add(_OP_lspace)
  533. self.tab[vt] = true
  534. self.compileOps(p, sp, vt)
  535. delete(self.tab, vt)
  536. }
  537. func (self *_Compiler) compileOps(p *_Program, sp int, vt reflect.Type) {
  538. switch vt.Kind() {
  539. case reflect.Bool : self.compilePrimitive (vt, p, _OP_bool)
  540. case reflect.Int : self.compilePrimitive (vt, p, _OP_int())
  541. case reflect.Int8 : self.compilePrimitive (vt, p, _OP_i8)
  542. case reflect.Int16 : self.compilePrimitive (vt, p, _OP_i16)
  543. case reflect.Int32 : self.compilePrimitive (vt, p, _OP_i32)
  544. case reflect.Int64 : self.compilePrimitive (vt, p, _OP_i64)
  545. case reflect.Uint : self.compilePrimitive (vt, p, _OP_uint())
  546. case reflect.Uint8 : self.compilePrimitive (vt, p, _OP_u8)
  547. case reflect.Uint16 : self.compilePrimitive (vt, p, _OP_u16)
  548. case reflect.Uint32 : self.compilePrimitive (vt, p, _OP_u32)
  549. case reflect.Uint64 : self.compilePrimitive (vt, p, _OP_u64)
  550. case reflect.Uintptr : self.compilePrimitive (vt, p, _OP_uintptr())
  551. case reflect.Float32 : self.compilePrimitive (vt, p, _OP_f32)
  552. case reflect.Float64 : self.compilePrimitive (vt, p, _OP_f64)
  553. case reflect.String : self.compileString (p, vt)
  554. case reflect.Array : self.compileArray (p, sp, vt)
  555. case reflect.Interface : self.compileInterface (p, vt)
  556. case reflect.Map : self.compileMap (p, sp, vt)
  557. case reflect.Ptr : self.compilePtr (p, sp, vt)
  558. case reflect.Slice : self.compileSlice (p, sp, vt)
  559. case reflect.Struct : self.compileStruct (p, sp, vt)
  560. default : self.compileUnsupportedType (p, vt)
  561. }
  562. }
  563. func (self *_Compiler) compileUnsupportedType(p *_Program, vt reflect.Type) {
  564. i := p.pc()
  565. p.add(_OP_is_null)
  566. p.rtt(_OP_unsupported, vt)
  567. p.pin(i)
  568. }
  569. func (self *_Compiler) compileMap(p *_Program, sp int, vt reflect.Type) {
  570. if reflect.PtrTo(vt.Key()).Implements(encodingTextUnmarshalerType) {
  571. self.compileMapOp(p, sp, vt, _OP_map_key_utext_p)
  572. } else if vt.Key().Implements(encodingTextUnmarshalerType) {
  573. self.compileMapOp(p, sp, vt, _OP_map_key_utext)
  574. } else {
  575. self.compileMapUt(p, sp, vt)
  576. }
  577. }
  578. func (self *_Compiler) compileMapUt(p *_Program, sp int, vt reflect.Type) {
  579. switch vt.Key().Kind() {
  580. case reflect.Int : self.compileMapOp(p, sp, vt, _OP_map_key_int())
  581. case reflect.Int8 : self.compileMapOp(p, sp, vt, _OP_map_key_i8)
  582. case reflect.Int16 : self.compileMapOp(p, sp, vt, _OP_map_key_i16)
  583. case reflect.Int32 : self.compileMapOp(p, sp, vt, _OP_map_key_i32)
  584. case reflect.Int64 : self.compileMapOp(p, sp, vt, _OP_map_key_i64)
  585. case reflect.Uint : self.compileMapOp(p, sp, vt, _OP_map_key_uint())
  586. case reflect.Uint8 : self.compileMapOp(p, sp, vt, _OP_map_key_u8)
  587. case reflect.Uint16 : self.compileMapOp(p, sp, vt, _OP_map_key_u16)
  588. case reflect.Uint32 : self.compileMapOp(p, sp, vt, _OP_map_key_u32)
  589. case reflect.Uint64 : self.compileMapOp(p, sp, vt, _OP_map_key_u64)
  590. case reflect.Uintptr : self.compileMapOp(p, sp, vt, _OP_map_key_uintptr())
  591. case reflect.Float32 : self.compileMapOp(p, sp, vt, _OP_map_key_f32)
  592. case reflect.Float64 : self.compileMapOp(p, sp, vt, _OP_map_key_f64)
  593. case reflect.String : self.compileMapOp(p, sp, vt, _OP_map_key_str)
  594. default : panic(&json.UnmarshalTypeError{Type: vt})
  595. }
  596. }
  597. func (self *_Compiler) compileMapOp(p *_Program, sp int, vt reflect.Type, op _Op) {
  598. i := p.pc()
  599. p.add(_OP_is_null)
  600. p.tag(sp + 1)
  601. skip := self.checkIfSkip(p, vt, '{')
  602. p.add(_OP_save)
  603. p.add(_OP_map_init)
  604. p.add(_OP_save)
  605. p.add(_OP_lspace)
  606. j := p.pc()
  607. p.chr(_OP_check_char, '}')
  608. p.chr(_OP_match_char, '"')
  609. skip2 := p.pc()
  610. p.rtt(op, vt)
  611. /* match the value separator */
  612. p.add(_OP_lspace)
  613. p.chr(_OP_match_char, ':')
  614. self.compileOne(p, sp + 2, vt.Elem())
  615. p.pin(skip2)
  616. p.add(_OP_load)
  617. k0 := p.pc()
  618. p.add(_OP_lspace)
  619. k1 := p.pc()
  620. p.chr(_OP_check_char, '}')
  621. p.chr(_OP_match_char, ',')
  622. p.add(_OP_lspace)
  623. p.chr(_OP_match_char, '"')
  624. skip3 := p.pc()
  625. p.rtt(op, vt)
  626. /* match the value separator */
  627. p.add(_OP_lspace)
  628. p.chr(_OP_match_char, ':')
  629. self.compileOne(p, sp + 2, vt.Elem())
  630. p.pin(skip3)
  631. p.add(_OP_load)
  632. p.int(_OP_goto, k0)
  633. p.pin(j)
  634. p.pin(k1)
  635. p.add(_OP_drop_2)
  636. x := p.pc()
  637. p.add(_OP_goto)
  638. p.pin(i)
  639. p.add(_OP_nil_1)
  640. p.pin(skip)
  641. p.pin(x)
  642. }
  643. func (self *_Compiler) compilePtr(p *_Program, sp int, et reflect.Type) {
  644. i := p.pc()
  645. p.add(_OP_is_null)
  646. /* dereference all the way down */
  647. for et.Kind() == reflect.Ptr {
  648. if self.checkMarshaler(p, et, 0, true) {
  649. return
  650. }
  651. et = et.Elem()
  652. p.rtt(_OP_deref, et)
  653. }
  654. /* check for recursive nesting */
  655. ok := self.tab[et]
  656. if ok {
  657. p.rtt(_OP_recurse, et)
  658. } else {
  659. /* enter the recursion */
  660. p.add(_OP_lspace)
  661. self.tab[et] = true
  662. /* not inline the pointer type
  663. * recursing the defined pointer type's elem will cause issue379.
  664. */
  665. self.compileOps(p, sp, et)
  666. }
  667. delete(self.tab, et)
  668. j := p.pc()
  669. p.add(_OP_goto)
  670. // set val pointer as nil
  671. p.pin(i)
  672. p.add(_OP_nil_1)
  673. // nothing todo
  674. p.pin(j)
  675. }
  676. func (self *_Compiler) compileArray(p *_Program, sp int, vt reflect.Type) {
  677. x := p.pc()
  678. p.add(_OP_is_null)
  679. p.tag(sp)
  680. skip := self.checkIfSkip(p, vt, '[')
  681. p.add(_OP_save)
  682. p.add(_OP_lspace)
  683. v := []int{p.pc()}
  684. p.chr(_OP_check_char, ']')
  685. /* decode every item */
  686. for i := 1; i <= vt.Len(); i++ {
  687. self.compileOne(p, sp + 1, vt.Elem())
  688. p.add(_OP_load)
  689. p.int(_OP_index, i * int(vt.Elem().Size()))
  690. p.add(_OP_lspace)
  691. v = append(v, p.pc())
  692. p.chr(_OP_check_char, ']')
  693. p.chr(_OP_match_char, ',')
  694. }
  695. /* drop rest of the array */
  696. p.add(_OP_array_skip)
  697. w := p.pc()
  698. p.add(_OP_goto)
  699. p.rel(v)
  700. /* check for pointer data */
  701. if rt.UnpackType(vt.Elem()).PtrData == 0 {
  702. p.int(_OP_array_clear, int(vt.Size()))
  703. } else {
  704. p.int(_OP_array_clear_p, int(vt.Size()))
  705. }
  706. /* restore the stack */
  707. p.pin(w)
  708. p.add(_OP_drop)
  709. p.pin(skip)
  710. p.pin(x)
  711. }
  712. func (self *_Compiler) compileSlice(p *_Program, sp int, vt reflect.Type) {
  713. if vt.Elem().Kind() == byteType.Kind() {
  714. self.compileSliceBin(p, sp, vt)
  715. } else {
  716. self.compileSliceList(p, sp, vt)
  717. }
  718. }
  719. func (self *_Compiler) compileSliceBin(p *_Program, sp int, vt reflect.Type) {
  720. i := p.pc()
  721. p.add(_OP_is_null)
  722. j := p.pc()
  723. p.chr(_OP_check_char, '[')
  724. skip := self.checkIfSkip(p, vt, '"')
  725. k := p.pc()
  726. p.chr(_OP_check_char, '"')
  727. p.add(_OP_bin)
  728. x := p.pc()
  729. p.add(_OP_goto)
  730. p.pin(j)
  731. self.compileSliceBody(p, sp, vt.Elem())
  732. y := p.pc()
  733. p.add(_OP_goto)
  734. // unmarshal `null` and `"` is different
  735. p.pin(i)
  736. p.add(_OP_nil_3)
  737. y2 := p.pc()
  738. p.add(_OP_goto)
  739. p.pin(k)
  740. p.add(_OP_empty_bytes)
  741. p.pin(x)
  742. p.pin(skip)
  743. p.pin(y)
  744. p.pin(y2)
  745. }
  746. func (self *_Compiler) compileSliceList(p *_Program, sp int, vt reflect.Type) {
  747. i := p.pc()
  748. p.add(_OP_is_null)
  749. p.tag(sp)
  750. skip := self.checkIfSkip(p, vt, '[')
  751. self.compileSliceBody(p, sp, vt.Elem())
  752. x := p.pc()
  753. p.add(_OP_goto)
  754. p.pin(i)
  755. p.add(_OP_nil_3)
  756. p.pin(x)
  757. p.pin(skip)
  758. }
  759. func (self *_Compiler) compileSliceBody(p *_Program, sp int, et reflect.Type) {
  760. p.add(_OP_lspace)
  761. j := p.pc()
  762. p.chr(_OP_check_empty, ']')
  763. p.rtt(_OP_slice_init, et)
  764. p.add(_OP_save)
  765. p.rtt(_OP_slice_append, et)
  766. self.compileOne(p, sp + 1, et)
  767. p.add(_OP_load)
  768. k0 := p.pc()
  769. p.add(_OP_lspace)
  770. k1 := p.pc()
  771. p.chr(_OP_check_char, ']')
  772. p.chr(_OP_match_char, ',')
  773. p.rtt(_OP_slice_append, et)
  774. self.compileOne(p, sp + 1, et)
  775. p.add(_OP_load)
  776. p.int(_OP_goto, k0)
  777. p.pin(k1)
  778. p.add(_OP_drop)
  779. p.pin(j)
  780. }
  781. func (self *_Compiler) compileString(p *_Program, vt reflect.Type) {
  782. if vt == jsonNumberType {
  783. self.compilePrimitive(vt, p, _OP_num)
  784. } else {
  785. self.compileStringBody(vt, p)
  786. }
  787. }
  788. func (self *_Compiler) compileStringBody(vt reflect.Type, p *_Program) {
  789. i := p.pc()
  790. p.add(_OP_is_null)
  791. skip := self.checkIfSkip(p, vt, '"')
  792. p.add(_OP_str)
  793. p.pin(i)
  794. p.pin(skip)
  795. }
  796. func (self *_Compiler) compileStruct(p *_Program, sp int, vt reflect.Type) {
  797. if sp >= self.opts.MaxInlineDepth || p.pc() >= _MAX_ILBUF || (sp > 0 && vt.NumField() >= _MAX_FIELDS) {
  798. p.rtt(_OP_recurse, vt)
  799. if self.opts.RecursiveDepth > 0 {
  800. self.rec[vt] = true
  801. }
  802. } else {
  803. self.compileStructBody(p, sp, vt)
  804. }
  805. }
  806. func (self *_Compiler) compileStructBody(p *_Program, sp int, vt reflect.Type) {
  807. fv := resolver.ResolveStruct(vt)
  808. fm, sw := caching.CreateFieldMap(len(fv)), make([]int, len(fv))
  809. /* start of object */
  810. p.tag(sp)
  811. n := p.pc()
  812. p.add(_OP_is_null)
  813. j := p.pc()
  814. p.chr(_OP_check_char_0, '{')
  815. p.rtt(_OP_dismatch_err, vt)
  816. /* special case for empty object */
  817. if len(fv) == 0 {
  818. p.pin(j)
  819. s := p.pc()
  820. p.add(_OP_skip_emtpy)
  821. p.pin(s)
  822. p.pin(n)
  823. return
  824. }
  825. skip := p.pc()
  826. p.add(_OP_go_skip)
  827. p.pin(j)
  828. p.int(_OP_add, 1)
  829. p.add(_OP_save)
  830. p.add(_OP_lspace)
  831. x := p.pc()
  832. p.chr(_OP_check_char, '}')
  833. p.chr(_OP_match_char, '"')
  834. p.fmv(_OP_struct_field, fm)
  835. p.add(_OP_lspace)
  836. p.chr(_OP_match_char, ':')
  837. p.tab(_OP_switch, sw)
  838. p.add(_OP_object_next)
  839. y0 := p.pc()
  840. p.add(_OP_lspace)
  841. y1 := p.pc()
  842. p.chr(_OP_check_char, '}')
  843. p.chr(_OP_match_char, ',')
  844. /* match the remaining fields */
  845. p.add(_OP_lspace)
  846. p.chr(_OP_match_char, '"')
  847. p.fmv(_OP_struct_field, fm)
  848. p.add(_OP_lspace)
  849. p.chr(_OP_match_char, ':')
  850. p.tab(_OP_switch, sw)
  851. p.add(_OP_object_next)
  852. p.int(_OP_goto, y0)
  853. /* process each field */
  854. for i, f := range fv {
  855. sw[i] = p.pc()
  856. fm.Set(f.Name, i)
  857. /* index to the field */
  858. for _, o := range f.Path {
  859. if p.int(_OP_index, int(o.Size)); o.Kind == resolver.F_deref {
  860. p.rtt(_OP_deref, o.Type)
  861. }
  862. }
  863. /* check for "stringnize" option */
  864. if (f.Opts & resolver.F_stringize) == 0 {
  865. self.compileOne(p, sp + 1, f.Type)
  866. } else {
  867. self.compileStructFieldStr(p, sp + 1, f.Type)
  868. }
  869. /* load the state, and try next field */
  870. p.add(_OP_load)
  871. p.int(_OP_goto, y0)
  872. }
  873. p.pin(x)
  874. p.pin(y1)
  875. p.add(_OP_drop)
  876. p.pin(n)
  877. p.pin(skip)
  878. }
  879. func (self *_Compiler) compileStructFieldStrUnmarshal(p *_Program, vt reflect.Type) {
  880. p.add(_OP_lspace)
  881. n0 := p.pc()
  882. p.add(_OP_is_null)
  883. self.checkMarshaler(p, vt, checkMarshalerFlags_quoted, true)
  884. p.pin(n0)
  885. }
  886. func (self *_Compiler) compileStructFieldStr(p *_Program, sp int, vt reflect.Type) {
  887. // according to std, json.Unmarshaler should be called before stringize
  888. // see https://github.com/bytedance/sonic/issues/670
  889. if self.checkMarshaler(p, vt, checkMarshalerFlags_quoted, false) {
  890. self.compileStructFieldStrUnmarshal(p, vt)
  891. return
  892. }
  893. n1 := -1
  894. ft := vt
  895. sv := false
  896. /* dereference the pointer if needed */
  897. if ft.Kind() == reflect.Ptr {
  898. ft = ft.Elem()
  899. }
  900. /* check if it can be stringized */
  901. switch ft.Kind() {
  902. case reflect.Bool : sv = true
  903. case reflect.Int : sv = true
  904. case reflect.Int8 : sv = true
  905. case reflect.Int16 : sv = true
  906. case reflect.Int32 : sv = true
  907. case reflect.Int64 : sv = true
  908. case reflect.Uint : sv = true
  909. case reflect.Uint8 : sv = true
  910. case reflect.Uint16 : sv = true
  911. case reflect.Uint32 : sv = true
  912. case reflect.Uint64 : sv = true
  913. case reflect.Uintptr : sv = true
  914. case reflect.Float32 : sv = true
  915. case reflect.Float64 : sv = true
  916. case reflect.String : sv = true
  917. }
  918. /* if it's not, ignore the "string" and follow the regular path */
  919. if !sv {
  920. self.compileOne(p, sp, vt)
  921. return
  922. }
  923. /* remove the leading space, and match the leading quote */
  924. vk := vt.Kind()
  925. p.add(_OP_lspace)
  926. n0 := p.pc()
  927. p.add(_OP_is_null)
  928. skip := self.checkIfSkip(p, stringType, '"')
  929. /* also check for inner "null" */
  930. n1 = p.pc()
  931. p.add(_OP_is_null_quote)
  932. /* dereference the pointer only when it is not null */
  933. if vk == reflect.Ptr {
  934. vt = vt.Elem()
  935. p.rtt(_OP_deref, vt)
  936. }
  937. n2 := p.pc()
  938. p.chr(_OP_check_char_0, '"')
  939. /* string opcode selector */
  940. _OP_string := func() _Op {
  941. if ft == jsonNumberType {
  942. return _OP_num
  943. } else {
  944. return _OP_unquote
  945. }
  946. }
  947. /* compile for each type */
  948. switch vt.Kind() {
  949. case reflect.Bool : p.add(_OP_bool)
  950. case reflect.Int : p.add(_OP_int())
  951. case reflect.Int8 : p.add(_OP_i8)
  952. case reflect.Int16 : p.add(_OP_i16)
  953. case reflect.Int32 : p.add(_OP_i32)
  954. case reflect.Int64 : p.add(_OP_i64)
  955. case reflect.Uint : p.add(_OP_uint())
  956. case reflect.Uint8 : p.add(_OP_u8)
  957. case reflect.Uint16 : p.add(_OP_u16)
  958. case reflect.Uint32 : p.add(_OP_u32)
  959. case reflect.Uint64 : p.add(_OP_u64)
  960. case reflect.Uintptr : p.add(_OP_uintptr())
  961. case reflect.Float32 : p.add(_OP_f32)
  962. case reflect.Float64 : p.add(_OP_f64)
  963. case reflect.String : p.add(_OP_string())
  964. default : panic("not reachable")
  965. }
  966. /* the closing quote is not needed when parsing a pure string */
  967. if vt == jsonNumberType || vt.Kind() != reflect.String {
  968. p.chr(_OP_match_char, '"')
  969. }
  970. /* pin the `is_null_quote` jump location */
  971. if n1 != -1 && vk != reflect.Ptr {
  972. p.pin(n1)
  973. }
  974. /* "null" but not a pointer, act as if the field is not present */
  975. if vk != reflect.Ptr {
  976. pc2 := p.pc()
  977. p.add(_OP_goto)
  978. p.pin(n2)
  979. p.rtt(_OP_dismatch_err, vt)
  980. p.int(_OP_add, 1)
  981. p.pin(pc2)
  982. p.pin(n0)
  983. return
  984. }
  985. /* the "null" case of the pointer */
  986. pc := p.pc()
  987. p.add(_OP_goto)
  988. p.pin(n0) // `is_null` jump location
  989. p.pin(n1) // `is_null_quote` jump location
  990. p.add(_OP_nil_1)
  991. pc2 := p.pc()
  992. p.add(_OP_goto)
  993. p.pin(n2)
  994. p.rtt(_OP_dismatch_err, vt)
  995. p.int(_OP_add, 1)
  996. p.pin(pc)
  997. p.pin(pc2)
  998. p.pin(skip)
  999. }
  1000. func (self *_Compiler) compileInterface(p *_Program, vt reflect.Type) {
  1001. i := p.pc()
  1002. p.add(_OP_is_null)
  1003. /* check for empty interface */
  1004. if vt.NumMethod() == 0 {
  1005. p.add(_OP_any)
  1006. } else {
  1007. p.rtt(_OP_dyn, vt)
  1008. }
  1009. /* finish the OpCode */
  1010. j := p.pc()
  1011. p.add(_OP_goto)
  1012. p.pin(i)
  1013. p.add(_OP_nil_2)
  1014. p.pin(j)
  1015. }
  1016. func (self *_Compiler) compilePrimitive(_ reflect.Type, p *_Program, op _Op) {
  1017. i := p.pc()
  1018. p.add(_OP_is_null)
  1019. p.add(op)
  1020. p.pin(i)
  1021. }
  1022. func (self *_Compiler) compileUnmarshalEnd(p *_Program, vt reflect.Type, i int) {
  1023. j := p.pc()
  1024. k := vt.Kind()
  1025. /* not a pointer */
  1026. if k != reflect.Ptr {
  1027. p.pin(i)
  1028. return
  1029. }
  1030. /* it seems that in Go JSON library, "null" takes priority over any kind of unmarshaler */
  1031. p.add(_OP_goto)
  1032. p.pin(i)
  1033. p.add(_OP_nil_1)
  1034. p.pin(j)
  1035. }
  1036. func (self *_Compiler) compileUnmarshalJson(p *_Program, vt reflect.Type, flags int) {
  1037. i := p.pc()
  1038. v := _OP_unmarshal
  1039. p.add(_OP_is_null)
  1040. /* check for dynamic interface */
  1041. if vt.Kind() == reflect.Interface {
  1042. v = _OP_dyn
  1043. }
  1044. /* call the unmarshaler */
  1045. p.rtti(v, vt, flags)
  1046. self.compileUnmarshalEnd(p, vt, i)
  1047. }
  1048. func (self *_Compiler) compileUnmarshalText(p *_Program, vt reflect.Type, iv int) {
  1049. i := p.pc()
  1050. v := _OP_unmarshal_text
  1051. p.add(_OP_is_null)
  1052. /* check for dynamic interface */
  1053. if vt.Kind() == reflect.Interface {
  1054. v = _OP_dyn
  1055. } else {
  1056. p.chr(_OP_match_char, '"')
  1057. }
  1058. /* call the unmarshaler */
  1059. p.rtti(v, vt, iv)
  1060. self.compileUnmarshalEnd(p, vt, i)
  1061. }
  1062. func (self *_Compiler) compileUnmarshalTextPtr(p *_Program, vt reflect.Type, iv int) {
  1063. i := p.pc()
  1064. p.add(_OP_is_null)
  1065. p.chr(_OP_match_char, '"')
  1066. p.rtti(_OP_unmarshal_text_p, vt, iv)
  1067. p.pin(i)
  1068. }
  1069. func (self *_Compiler) checkIfSkip(p *_Program, vt reflect.Type, c byte) int {
  1070. j := p.pc()
  1071. p.chr(_OP_check_char_0, c)
  1072. p.rtt(_OP_dismatch_err, vt)
  1073. s := p.pc()
  1074. p.add(_OP_go_skip)
  1075. p.pin(j)
  1076. p.int(_OP_add, 1)
  1077. return s
  1078. }