vm.go 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. // Copyright 2024 CloudWeGo Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package vm
  15. import (
  16. "encoding"
  17. "encoding/json"
  18. "fmt"
  19. "math"
  20. "reflect"
  21. "unsafe"
  22. "github.com/bytedance/sonic/internal/encoder/alg"
  23. "github.com/bytedance/sonic/internal/encoder/ir"
  24. "github.com/bytedance/sonic/internal/encoder/vars"
  25. "github.com/bytedance/sonic/internal/rt"
  26. )
  27. const (
  28. _S_cond = iota
  29. _S_init
  30. )
  31. var (
  32. _T_json_Marshaler = rt.UnpackType(vars.JsonMarshalerType)
  33. _T_encoding_TextMarshaler = rt.UnpackType(vars.EncodingTextMarshalerType)
  34. )
  35. func print_instr(buf []byte, pc int, op ir.Op, ins *ir.Instr, p unsafe.Pointer) {
  36. if len(buf) > 20 {
  37. fmt.Println(string(buf[len(buf)-20:]))
  38. } else {
  39. fmt.Println(string(buf))
  40. }
  41. fmt.Printf("pc %04d, op %v, ins %#v, ptr: %x\n", pc, op, ins.Disassemble(), p)
  42. }
  43. func Execute(b *[]byte, p unsafe.Pointer, s *vars.Stack, flags uint64, prog *ir.Program) (error) {
  44. pl := len(*prog)
  45. if pl <= 0 {
  46. return nil
  47. }
  48. var buf = *b
  49. var x int
  50. var q unsafe.Pointer
  51. var f uint64
  52. var pro = &(*prog)[0]
  53. for pc := 0; pc < pl; {
  54. ins := (*ir.Instr)(rt.Add(unsafe.Pointer(pro), ir.OpSize*uintptr(pc)))
  55. pc++
  56. op := ins.Op()
  57. switch op {
  58. case ir.OP_goto:
  59. pc = ins.Vi()
  60. continue
  61. case ir.OP_byte:
  62. v := ins.Byte()
  63. buf = append(buf, v)
  64. case ir.OP_text:
  65. v := ins.Vs()
  66. buf = append(buf, v...)
  67. case ir.OP_deref:
  68. p = *(*unsafe.Pointer)(p)
  69. case ir.OP_index:
  70. p = rt.Add(p, uintptr(ins.I64()))
  71. case ir.OP_load:
  72. // NOTICE: load CANNOT change f!
  73. x, _, p, q = s.Load()
  74. case ir.OP_save:
  75. if !s.Save(x, f, p, q) {
  76. return vars.ERR_too_deep
  77. }
  78. case ir.OP_drop:
  79. x, f, p, q = s.Drop()
  80. case ir.OP_drop_2:
  81. s.Drop()
  82. x, f, p, q = s.Drop()
  83. case ir.OP_recurse:
  84. vt, pv := ins.Vp2()
  85. f := flags
  86. if pv {
  87. f |= (1 << alg.BitPointerValue)
  88. }
  89. *b = buf
  90. if vt.Indirect() {
  91. if err := EncodeTypedPointer(b, vt, (*unsafe.Pointer)(rt.NoEscape(unsafe.Pointer(&p))), s, f); err != nil {
  92. return err
  93. }
  94. } else {
  95. vp := (*unsafe.Pointer)(p)
  96. if err := EncodeTypedPointer(b, vt, vp, s, f); err != nil {
  97. return err
  98. }
  99. }
  100. buf = *b
  101. case ir.OP_is_nil:
  102. if is_nil(p) {
  103. pc = ins.Vi()
  104. continue
  105. }
  106. case ir.OP_is_nil_p1:
  107. if (*rt.GoEface)(p).Value == nil {
  108. pc = ins.Vi()
  109. continue
  110. }
  111. case ir.OP_null:
  112. buf = append(buf, 'n', 'u', 'l', 'l')
  113. case ir.OP_str:
  114. v := *(*string)(p)
  115. buf = alg.Quote(buf, v, false)
  116. case ir.OP_bool:
  117. if *(*bool)(p) {
  118. buf = append(buf, 't', 'r', 'u', 'e')
  119. } else {
  120. buf = append(buf, 'f', 'a', 'l', 's', 'e')
  121. }
  122. case ir.OP_i8:
  123. v := *(*int8)(p)
  124. buf = alg.I64toa(buf, int64(v))
  125. case ir.OP_i16:
  126. v := *(*int16)(p)
  127. buf = alg.I64toa(buf, int64(v))
  128. case ir.OP_i32:
  129. v := *(*int32)(p)
  130. buf = alg.I64toa(buf, int64(v))
  131. case ir.OP_i64:
  132. v := *(*int64)(p)
  133. buf = alg.I64toa(buf, int64(v))
  134. case ir.OP_u8:
  135. v := *(*uint8)(p)
  136. buf = alg.U64toa(buf, uint64(v))
  137. case ir.OP_u16:
  138. v := *(*uint16)(p)
  139. buf = alg.U64toa(buf, uint64(v))
  140. case ir.OP_u32:
  141. v := *(*uint32)(p)
  142. buf = alg.U64toa(buf, uint64(v))
  143. case ir.OP_u64:
  144. v := *(*uint64)(p)
  145. buf = alg.U64toa(buf, uint64(v))
  146. case ir.OP_f32:
  147. v := *(*float32)(p)
  148. if math.IsNaN(float64(v)) || math.IsInf(float64(v), 0) {
  149. if flags&(1<<alg.BitEncodeNullForInfOrNan) != 0 {
  150. buf = append(buf, 'n', 'u', 'l', 'l')
  151. continue
  152. }
  153. return vars.ERR_nan_or_infinite
  154. }
  155. buf = alg.F32toa(buf, v)
  156. case ir.OP_f64:
  157. v := *(*float64)(p)
  158. if math.IsNaN(v) || math.IsInf(v, 0) {
  159. if flags&(1<<alg.BitEncodeNullForInfOrNan) != 0 {
  160. buf = append(buf, 'n', 'u', 'l', 'l')
  161. continue
  162. }
  163. return vars.ERR_nan_or_infinite
  164. }
  165. buf = alg.F64toa(buf, v)
  166. case ir.OP_bin:
  167. v := *(*[]byte)(p)
  168. buf = rt.EncodeBase64(buf, v)
  169. case ir.OP_quote:
  170. v := *(*string)(p)
  171. buf = alg.Quote(buf, v, true)
  172. case ir.OP_number:
  173. v := *(*json.Number)(p)
  174. if v == "" {
  175. buf = append(buf, '0')
  176. } else if !rt.IsValidNumber(string(v)) {
  177. return vars.Error_number(v)
  178. } else {
  179. buf = append(buf, v...)
  180. }
  181. case ir.OP_eface:
  182. *b = buf
  183. if err := EncodeTypedPointer(b, *(**rt.GoType)(p), (*unsafe.Pointer)(rt.Add(p, 8)), s, flags); err != nil {
  184. return err
  185. }
  186. buf = *b
  187. case ir.OP_iface:
  188. *b = buf
  189. if err := EncodeTypedPointer(b, (*(**rt.GoItab)(p)).Vt, (*unsafe.Pointer)(rt.Add(p, 8)), s, flags); err != nil {
  190. return err
  191. }
  192. buf = *b
  193. case ir.OP_is_zero_map:
  194. v := *(*unsafe.Pointer)(p)
  195. if v == nil || rt.Maplen(v) == 0 {
  196. pc = ins.Vi()
  197. continue
  198. }
  199. case ir.OP_map_iter:
  200. v := *(*unsafe.Pointer)(p)
  201. vt := ins.Vr()
  202. it, err := alg.IteratorStart(rt.MapType(vt), v, flags)
  203. if err != nil {
  204. return err
  205. }
  206. q = unsafe.Pointer(it)
  207. case ir.OP_map_stop:
  208. it := (*alg.MapIterator)(q)
  209. alg.IteratorStop(it)
  210. q = nil
  211. case ir.OP_map_value_next:
  212. it := (*alg.MapIterator)(q)
  213. p = it.It.V
  214. alg.IteratorNext(it)
  215. case ir.OP_map_check_key:
  216. it := (*alg.MapIterator)(q)
  217. if it.It.K == nil {
  218. pc = ins.Vi()
  219. continue
  220. }
  221. p = it.It.K
  222. case ir.OP_marshal_text:
  223. vt, itab := ins.Vtab()
  224. var it rt.GoIface
  225. switch vt.Kind() {
  226. case reflect.Interface :
  227. if is_nil(p) {
  228. buf = append(buf, 'n', 'u', 'l', 'l')
  229. continue
  230. }
  231. it = rt.AssertI2I(_T_encoding_TextMarshaler, *(*rt.GoIface)(p))
  232. case reflect.Ptr, reflect.Map : it = convT2I(p, true, itab)
  233. default : it = convT2I(p, !vt.Indirect(), itab)
  234. }
  235. if err := alg.EncodeTextMarshaler(&buf, *(*encoding.TextMarshaler)(unsafe.Pointer(&it)), (flags)); err != nil {
  236. return err
  237. }
  238. case ir.OP_marshal_text_p:
  239. _, itab := ins.Vtab()
  240. it := convT2I(p, false, itab)
  241. if err := alg.EncodeTextMarshaler(&buf, *(*encoding.TextMarshaler)(unsafe.Pointer(&it)), (flags)); err != nil {
  242. return err
  243. }
  244. case ir.OP_map_write_key:
  245. if has_opts(flags, alg.BitSortMapKeys) {
  246. v := *(*string)(p)
  247. buf = alg.Quote(buf, v, false)
  248. pc = ins.Vi()
  249. continue
  250. }
  251. case ir.OP_slice_len:
  252. v := (*rt.GoSlice)(p)
  253. x = v.Len
  254. p = v.Ptr
  255. //TODO: why?
  256. f |= 1<<_S_init
  257. case ir.OP_slice_next:
  258. if x == 0 {
  259. pc = ins.Vi()
  260. continue
  261. }
  262. x--
  263. if has_opts(f, _S_init) {
  264. f &= ^uint64(1 << _S_init)
  265. } else {
  266. p = rt.Add(p, uintptr(ins.Vlen()))
  267. }
  268. case ir.OP_cond_set:
  269. f |= 1<<_S_cond
  270. case ir.OP_cond_testc:
  271. if has_opts(f, _S_cond) {
  272. f &= ^uint64(1 << _S_cond)
  273. pc = ins.Vi()
  274. continue
  275. }
  276. case ir.OP_is_zero:
  277. fv := ins.VField()
  278. if alg.IsZero(p, fv) {
  279. pc = ins.Vi()
  280. continue
  281. }
  282. case ir.OP_is_zero_1:
  283. if *(*uint8)(p) == 0 {
  284. pc = ins.Vi()
  285. continue
  286. }
  287. case ir.OP_is_zero_2:
  288. if *(*uint16)(p) == 0 {
  289. pc = ins.Vi()
  290. continue
  291. }
  292. case ir.OP_is_zero_4:
  293. if *(*uint32)(p) == 0 {
  294. pc = ins.Vi()
  295. continue
  296. }
  297. case ir.OP_is_zero_8:
  298. if *(*uint64)(p) == 0 {
  299. pc = ins.Vi()
  300. continue
  301. }
  302. case ir.OP_empty_arr:
  303. if has_opts(flags, alg.BitNoNullSliceOrMap) {
  304. buf = append(buf, '[', ']')
  305. } else {
  306. buf = append(buf, 'n', 'u', 'l', 'l')
  307. }
  308. case ir.OP_empty_obj:
  309. if has_opts(flags, alg.BitNoNullSliceOrMap) {
  310. buf = append(buf, '{', '}')
  311. } else {
  312. buf = append(buf, 'n', 'u', 'l', 'l')
  313. }
  314. case ir.OP_marshal:
  315. vt, itab := ins.Vtab()
  316. var it rt.GoIface
  317. switch vt.Kind() {
  318. case reflect.Interface :
  319. if is_nil(p) {
  320. buf = append(buf, 'n', 'u', 'l', 'l')
  321. continue
  322. }
  323. it = rt.AssertI2I(_T_json_Marshaler, *(*rt.GoIface)(p))
  324. case reflect.Ptr, reflect.Map : it = convT2I(p, true, itab)
  325. default : it = convT2I(p, !vt.Indirect(), itab)
  326. }
  327. if err := alg.EncodeJsonMarshaler(&buf, *(*json.Marshaler)(unsafe.Pointer(&it)), (flags)); err != nil {
  328. return err
  329. }
  330. case ir.OP_marshal_p:
  331. _, itab := ins.Vtab()
  332. it := convT2I(p, false, itab)
  333. if err := alg.EncodeJsonMarshaler(&buf, *(*json.Marshaler)(unsafe.Pointer(&it)), (flags)); err != nil {
  334. return err
  335. }
  336. case ir.OP_unsupported:
  337. return vars.Error_unsuppoted(ins.GoType())
  338. default:
  339. panic(fmt.Sprintf("not implement %s at %d", ins.Op().String(), pc))
  340. }
  341. }
  342. *b = buf
  343. return nil
  344. }
  345. func has_opts(opts uint64, bit int) bool {
  346. return opts & (1<<bit) != 0
  347. }
  348. func is_nil(p unsafe.Pointer) bool {
  349. return *(*unsafe.Pointer)(p) == nil
  350. }
  351. func convT2I(ptr unsafe.Pointer, deref bool, itab *rt.GoItab) (rt.GoIface) {
  352. if deref {
  353. ptr = *(*unsafe.Pointer)(ptr)
  354. }
  355. return rt.GoIface{
  356. Itab: itab,
  357. Value: ptr,
  358. }
  359. }