quickjs-opcode.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. /*
  2. * QuickJS opcode definitions
  3. *
  4. * Copyright (c) 2017-2018 Fabrice Bellard
  5. * Copyright (c) 2017-2018 Charlie Gordon
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a copy
  8. * of this software and associated documentation files (the "Software"), to deal
  9. * in the Software without restriction, including without limitation the rights
  10. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. * copies of the Software, and to permit persons to whom the Software is
  12. * furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in
  15. * all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  20. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. * THE SOFTWARE.
  24. */
  25. #ifdef FMT
  26. FMT(none)
  27. FMT(none_int)
  28. FMT(none_loc)
  29. FMT(none_arg)
  30. FMT(none_var_ref)
  31. FMT(u8)
  32. FMT(i8)
  33. FMT(loc8)
  34. FMT(const8)
  35. FMT(label8)
  36. FMT(u16)
  37. FMT(i16)
  38. FMT(label16)
  39. FMT(npop)
  40. FMT(npopx)
  41. FMT(npop_u16)
  42. FMT(loc)
  43. FMT(arg)
  44. FMT(var_ref)
  45. FMT(u32)
  46. FMT(i32)
  47. FMT(const)
  48. FMT(label)
  49. FMT(atom)
  50. FMT(atom_u8)
  51. FMT(atom_u16)
  52. FMT(atom_label_u8)
  53. FMT(atom_label_u16)
  54. FMT(label_u16)
  55. #undef FMT
  56. #endif /* FMT */
  57. #ifdef DEF
  58. #ifndef def
  59. #define def(id, size, n_pop, n_push, f) DEF(id, size, n_pop, n_push, f)
  60. #endif
  61. DEF(invalid, 1, 0, 0, none) /* never emitted */
  62. /* push values */
  63. DEF( push_i32, 5, 0, 1, i32)
  64. DEF( push_const, 5, 0, 1, const)
  65. DEF( fclosure, 5, 0, 1, const) /* must follow push_const */
  66. DEF(push_atom_value, 5, 0, 1, atom)
  67. DEF( private_symbol, 5, 0, 1, atom)
  68. DEF( undefined, 1, 0, 1, none)
  69. DEF( null, 1, 0, 1, none)
  70. DEF( push_this, 1, 0, 1, none) /* only used at the start of a function */
  71. DEF( push_false, 1, 0, 1, none)
  72. DEF( push_true, 1, 0, 1, none)
  73. DEF( object, 1, 0, 1, none)
  74. DEF( special_object, 2, 0, 1, u8) /* only used at the start of a function */
  75. DEF( rest, 3, 0, 1, u16) /* only used at the start of a function */
  76. DEF( drop, 1, 1, 0, none) /* a -> */
  77. DEF( nip, 1, 2, 1, none) /* a b -> b */
  78. DEF( nip1, 1, 3, 2, none) /* a b c -> b c */
  79. DEF( dup, 1, 1, 2, none) /* a -> a a */
  80. DEF( dup1, 1, 2, 3, none) /* a b -> a a b */
  81. DEF( dup2, 1, 2, 4, none) /* a b -> a b a b */
  82. DEF( dup3, 1, 3, 6, none) /* a b c -> a b c a b c */
  83. DEF( insert2, 1, 2, 3, none) /* obj a -> a obj a (dup_x1) */
  84. DEF( insert3, 1, 3, 4, none) /* obj prop a -> a obj prop a (dup_x2) */
  85. DEF( insert4, 1, 4, 5, none) /* this obj prop a -> a this obj prop a */
  86. DEF( perm3, 1, 3, 3, none) /* obj a b -> a obj b */
  87. DEF( perm4, 1, 4, 4, none) /* obj prop a b -> a obj prop b */
  88. DEF( perm5, 1, 5, 5, none) /* this obj prop a b -> a this obj prop b */
  89. DEF( swap, 1, 2, 2, none) /* a b -> b a */
  90. DEF( swap2, 1, 4, 4, none) /* a b c d -> c d a b */
  91. DEF( rot3l, 1, 3, 3, none) /* x a b -> a b x */
  92. DEF( rot3r, 1, 3, 3, none) /* a b x -> x a b */
  93. DEF( rot4l, 1, 4, 4, none) /* x a b c -> a b c x */
  94. DEF( rot5l, 1, 5, 5, none) /* x a b c d -> a b c d x */
  95. DEF(call_constructor, 3, 2, 1, npop) /* func new.target args -> ret. arguments are not counted in n_pop */
  96. DEF( call, 3, 1, 1, npop) /* arguments are not counted in n_pop */
  97. DEF( tail_call, 3, 1, 0, npop) /* arguments are not counted in n_pop */
  98. DEF( call_method, 3, 2, 1, npop) /* arguments are not counted in n_pop */
  99. DEF(tail_call_method, 3, 2, 0, npop) /* arguments are not counted in n_pop */
  100. DEF( array_from, 3, 0, 1, npop) /* arguments are not counted in n_pop */
  101. DEF( apply, 3, 3, 1, u16)
  102. DEF( return, 1, 1, 0, none)
  103. DEF( return_undef, 1, 0, 0, none)
  104. DEF(check_ctor_return, 1, 1, 2, none)
  105. DEF( check_ctor, 1, 0, 0, none)
  106. DEF( init_ctor, 1, 0, 1, none)
  107. DEF( check_brand, 1, 2, 2, none) /* this_obj func -> this_obj func */
  108. DEF( add_brand, 1, 2, 0, none) /* this_obj home_obj -> */
  109. DEF( return_async, 1, 1, 0, none)
  110. DEF( throw, 1, 1, 0, none)
  111. DEF( throw_error, 6, 0, 0, atom_u8)
  112. DEF( eval, 5, 1, 1, npop_u16) /* func args... -> ret_val */
  113. DEF( apply_eval, 3, 2, 1, u16) /* func array -> ret_eval */
  114. DEF( regexp, 1, 2, 1, none) /* create a RegExp object from the pattern and a
  115. bytecode string */
  116. DEF( get_super, 1, 1, 1, none)
  117. DEF( import, 1, 2, 1, none) /* dynamic module import */
  118. DEF( get_var_undef, 3, 0, 1, var_ref) /* push undefined if the variable does not exist */
  119. DEF( get_var, 3, 0, 1, var_ref) /* throw an exception if the variable does not exist */
  120. DEF( put_var, 3, 1, 0, var_ref) /* must come after get_var */
  121. DEF( put_var_init, 3, 1, 0, var_ref) /* must come after put_var. Used to initialize a global lexical variable */
  122. DEF( get_ref_value, 1, 2, 3, none)
  123. DEF( put_ref_value, 1, 3, 0, none)
  124. DEF( get_field, 5, 1, 1, atom)
  125. DEF( get_field2, 5, 1, 2, atom)
  126. DEF( put_field, 5, 2, 0, atom)
  127. DEF( get_private_field, 1, 2, 1, none) /* obj prop -> value */
  128. DEF( put_private_field, 1, 3, 0, none) /* obj value prop -> */
  129. DEF(define_private_field, 1, 3, 1, none) /* obj prop value -> obj */
  130. DEF( get_array_el, 1, 2, 1, none)
  131. DEF( get_array_el2, 1, 2, 2, none) /* obj prop -> obj value */
  132. DEF( get_array_el3, 1, 2, 3, none) /* obj prop -> obj prop1 value */
  133. DEF( put_array_el, 1, 3, 0, none)
  134. DEF(get_super_value, 1, 3, 1, none) /* this obj prop -> value */
  135. DEF(put_super_value, 1, 4, 0, none) /* this obj prop value -> */
  136. DEF( define_field, 5, 2, 1, atom)
  137. DEF( set_name, 5, 1, 1, atom)
  138. DEF(set_name_computed, 1, 2, 2, none)
  139. DEF( set_proto, 1, 2, 1, none)
  140. DEF(set_home_object, 1, 2, 2, none)
  141. DEF(define_array_el, 1, 3, 2, none)
  142. DEF( append, 1, 3, 2, none) /* append enumerated object, update length */
  143. DEF(copy_data_properties, 2, 3, 3, u8)
  144. DEF( define_method, 6, 2, 1, atom_u8)
  145. DEF(define_method_computed, 2, 3, 1, u8) /* must come after define_method */
  146. DEF( define_class, 6, 2, 2, atom_u8) /* parent ctor -> ctor proto */
  147. DEF( define_class_computed, 6, 3, 3, atom_u8) /* field_name parent ctor -> field_name ctor proto (class with computed name) */
  148. DEF( get_loc, 3, 0, 1, loc)
  149. DEF( put_loc, 3, 1, 0, loc) /* must come after get_loc */
  150. DEF( set_loc, 3, 1, 1, loc) /* must come after put_loc */
  151. DEF( get_arg, 3, 0, 1, arg)
  152. DEF( put_arg, 3, 1, 0, arg) /* must come after get_arg */
  153. DEF( set_arg, 3, 1, 1, arg) /* must come after put_arg */
  154. DEF( get_var_ref, 3, 0, 1, var_ref)
  155. DEF( put_var_ref, 3, 1, 0, var_ref) /* must come after get_var_ref */
  156. DEF( set_var_ref, 3, 1, 1, var_ref) /* must come after put_var_ref */
  157. DEF(set_loc_uninitialized, 3, 0, 0, loc)
  158. DEF( get_loc_check, 3, 0, 1, loc)
  159. DEF( put_loc_check, 3, 1, 0, loc) /* must come after get_loc_check */
  160. DEF( set_loc_check, 3, 1, 1, loc) /* must come after put_loc_check */
  161. DEF( put_loc_check_init, 3, 1, 0, loc)
  162. DEF(get_loc_checkthis, 3, 0, 1, loc)
  163. DEF(get_var_ref_check, 3, 0, 1, var_ref)
  164. DEF(put_var_ref_check, 3, 1, 0, var_ref) /* must come after get_var_ref_check */
  165. DEF(put_var_ref_check_init, 3, 1, 0, var_ref)
  166. DEF( close_loc, 3, 0, 0, loc)
  167. DEF( if_false, 5, 1, 0, label)
  168. DEF( if_true, 5, 1, 0, label) /* must come after if_false */
  169. DEF( goto, 5, 0, 0, label) /* must come after if_true */
  170. DEF( catch, 5, 0, 1, label)
  171. DEF( gosub, 5, 0, 0, label) /* used to execute the finally block */
  172. DEF( ret, 1, 1, 0, none) /* used to return from the finally block */
  173. DEF( nip_catch, 1, 2, 1, none) /* catch ... a -> a */
  174. DEF( to_object, 1, 1, 1, none)
  175. //DEF( to_string, 1, 1, 1, none)
  176. DEF( to_propkey, 1, 1, 1, none)
  177. DEF( with_get_var, 10, 1, 0, atom_label_u8) /* must be in the same order as scope_xxx */
  178. DEF( with_put_var, 10, 2, 1, atom_label_u8) /* must be in the same order as scope_xxx */
  179. DEF(with_delete_var, 10, 1, 0, atom_label_u8) /* must be in the same order as scope_xxx */
  180. DEF( with_make_ref, 10, 1, 0, atom_label_u8) /* must be in the same order as scope_xxx */
  181. DEF( with_get_ref, 10, 1, 0, atom_label_u8) /* must be in the same order as scope_xxx */
  182. DEF( make_loc_ref, 7, 0, 2, atom_u16)
  183. DEF( make_arg_ref, 7, 0, 2, atom_u16)
  184. DEF(make_var_ref_ref, 7, 0, 2, atom_u16)
  185. DEF( make_var_ref, 5, 0, 2, atom)
  186. DEF( for_in_start, 1, 1, 1, none)
  187. DEF( for_of_start, 1, 1, 3, none)
  188. DEF(for_await_of_start, 1, 1, 3, none)
  189. DEF( for_in_next, 1, 1, 3, none)
  190. DEF( for_of_next, 2, 3, 5, u8)
  191. DEF(for_await_of_next, 1, 3, 4, none) /* iter next catch_offset -> iter next catch_offset obj */
  192. DEF(iterator_check_object, 1, 1, 1, none)
  193. DEF(iterator_get_value_done, 1, 2, 3, none) /* catch_offset obj -> catch_offset value done */
  194. DEF( iterator_close, 1, 3, 0, none)
  195. DEF( iterator_next, 1, 4, 4, none)
  196. DEF( iterator_call, 2, 4, 5, u8)
  197. DEF( initial_yield, 1, 0, 0, none)
  198. DEF( yield, 1, 1, 2, none)
  199. DEF( yield_star, 1, 1, 2, none)
  200. DEF(async_yield_star, 1, 1, 2, none)
  201. DEF( await, 1, 1, 1, none)
  202. /* arithmetic/logic operations */
  203. DEF( neg, 1, 1, 1, none)
  204. DEF( plus, 1, 1, 1, none)
  205. DEF( dec, 1, 1, 1, none)
  206. DEF( inc, 1, 1, 1, none)
  207. DEF( post_dec, 1, 1, 2, none)
  208. DEF( post_inc, 1, 1, 2, none)
  209. DEF( dec_loc, 2, 0, 0, loc8)
  210. DEF( inc_loc, 2, 0, 0, loc8)
  211. DEF( add_loc, 2, 1, 0, loc8)
  212. DEF( not, 1, 1, 1, none)
  213. DEF( lnot, 1, 1, 1, none)
  214. DEF( typeof, 1, 1, 1, none)
  215. DEF( delete, 1, 2, 1, none)
  216. DEF( delete_var, 5, 0, 1, atom)
  217. DEF( mul, 1, 2, 1, none)
  218. DEF( div, 1, 2, 1, none)
  219. DEF( mod, 1, 2, 1, none)
  220. DEF( add, 1, 2, 1, none)
  221. DEF( sub, 1, 2, 1, none)
  222. DEF( pow, 1, 2, 1, none)
  223. DEF( shl, 1, 2, 1, none)
  224. DEF( sar, 1, 2, 1, none)
  225. DEF( shr, 1, 2, 1, none)
  226. DEF( lt, 1, 2, 1, none)
  227. DEF( lte, 1, 2, 1, none)
  228. DEF( gt, 1, 2, 1, none)
  229. DEF( gte, 1, 2, 1, none)
  230. DEF( instanceof, 1, 2, 1, none)
  231. DEF( in, 1, 2, 1, none)
  232. DEF( eq, 1, 2, 1, none)
  233. DEF( neq, 1, 2, 1, none)
  234. DEF( strict_eq, 1, 2, 1, none)
  235. DEF( strict_neq, 1, 2, 1, none)
  236. DEF( and, 1, 2, 1, none)
  237. DEF( xor, 1, 2, 1, none)
  238. DEF( or, 1, 2, 1, none)
  239. DEF(is_undefined_or_null, 1, 1, 1, none)
  240. DEF( private_in, 1, 2, 1, none)
  241. DEF(push_bigint_i32, 5, 0, 1, i32)
  242. /* must be the last non short and non temporary opcode */
  243. DEF( nop, 1, 0, 0, none)
  244. /* temporary opcodes: never emitted in the final bytecode */
  245. def( enter_scope, 3, 0, 0, u16) /* emitted in phase 1, removed in phase 2 */
  246. def( leave_scope, 3, 0, 0, u16) /* emitted in phase 1, removed in phase 2 */
  247. def( label, 5, 0, 0, label) /* emitted in phase 1, removed in phase 3 */
  248. /* the following opcodes must be in the same order as the 'with_x' and
  249. get_var_undef, get_var and put_var opcodes */
  250. def(scope_get_var_undef, 7, 0, 1, atom_u16) /* emitted in phase 1, removed in phase 2 */
  251. def( scope_get_var, 7, 0, 1, atom_u16) /* emitted in phase 1, removed in phase 2 */
  252. def( scope_put_var, 7, 1, 0, atom_u16) /* emitted in phase 1, removed in phase 2 */
  253. def(scope_delete_var, 7, 0, 1, atom_u16) /* emitted in phase 1, removed in phase 2 */
  254. def( scope_make_ref, 11, 0, 2, atom_label_u16) /* emitted in phase 1, removed in phase 2 */
  255. def( scope_get_ref, 7, 0, 2, atom_u16) /* emitted in phase 1, removed in phase 2 */
  256. def(scope_put_var_init, 7, 0, 2, atom_u16) /* emitted in phase 1, removed in phase 2 */
  257. def(scope_get_var_checkthis, 7, 0, 1, atom_u16) /* emitted in phase 1, removed in phase 2, only used to return 'this' in derived class constructors */
  258. def(scope_get_private_field, 7, 1, 1, atom_u16) /* obj -> value, emitted in phase 1, removed in phase 2 */
  259. def(scope_get_private_field2, 7, 1, 2, atom_u16) /* obj -> obj value, emitted in phase 1, removed in phase 2 */
  260. def(scope_put_private_field, 7, 2, 0, atom_u16) /* obj value ->, emitted in phase 1, removed in phase 2 */
  261. def(scope_in_private_field, 7, 1, 1, atom_u16) /* obj -> res emitted in phase 1, removed in phase 2 */
  262. def(get_field_opt_chain, 5, 1, 1, atom) /* emitted in phase 1, removed in phase 2 */
  263. def(get_array_el_opt_chain, 1, 2, 1, none) /* emitted in phase 1, removed in phase 2 */
  264. def( set_class_name, 5, 1, 1, u32) /* emitted in phase 1, removed in phase 2 */
  265. def( line_num, 5, 0, 0, u32) /* emitted in phase 1, removed in phase 3 */
  266. #if SHORT_OPCODES
  267. DEF( push_minus1, 1, 0, 1, none_int)
  268. DEF( push_0, 1, 0, 1, none_int)
  269. DEF( push_1, 1, 0, 1, none_int)
  270. DEF( push_2, 1, 0, 1, none_int)
  271. DEF( push_3, 1, 0, 1, none_int)
  272. DEF( push_4, 1, 0, 1, none_int)
  273. DEF( push_5, 1, 0, 1, none_int)
  274. DEF( push_6, 1, 0, 1, none_int)
  275. DEF( push_7, 1, 0, 1, none_int)
  276. DEF( push_i8, 2, 0, 1, i8)
  277. DEF( push_i16, 3, 0, 1, i16)
  278. DEF( push_const8, 2, 0, 1, const8)
  279. DEF( fclosure8, 2, 0, 1, const8) /* must follow push_const8 */
  280. DEF(push_empty_string, 1, 0, 1, none)
  281. DEF( get_loc8, 2, 0, 1, loc8)
  282. DEF( put_loc8, 2, 1, 0, loc8)
  283. DEF( set_loc8, 2, 1, 1, loc8)
  284. DEF( get_loc0, 1, 0, 1, none_loc)
  285. DEF( get_loc1, 1, 0, 1, none_loc)
  286. DEF( get_loc2, 1, 0, 1, none_loc)
  287. DEF( get_loc3, 1, 0, 1, none_loc)
  288. DEF( put_loc0, 1, 1, 0, none_loc)
  289. DEF( put_loc1, 1, 1, 0, none_loc)
  290. DEF( put_loc2, 1, 1, 0, none_loc)
  291. DEF( put_loc3, 1, 1, 0, none_loc)
  292. DEF( set_loc0, 1, 1, 1, none_loc)
  293. DEF( set_loc1, 1, 1, 1, none_loc)
  294. DEF( set_loc2, 1, 1, 1, none_loc)
  295. DEF( set_loc3, 1, 1, 1, none_loc)
  296. DEF( get_arg0, 1, 0, 1, none_arg)
  297. DEF( get_arg1, 1, 0, 1, none_arg)
  298. DEF( get_arg2, 1, 0, 1, none_arg)
  299. DEF( get_arg3, 1, 0, 1, none_arg)
  300. DEF( put_arg0, 1, 1, 0, none_arg)
  301. DEF( put_arg1, 1, 1, 0, none_arg)
  302. DEF( put_arg2, 1, 1, 0, none_arg)
  303. DEF( put_arg3, 1, 1, 0, none_arg)
  304. DEF( set_arg0, 1, 1, 1, none_arg)
  305. DEF( set_arg1, 1, 1, 1, none_arg)
  306. DEF( set_arg2, 1, 1, 1, none_arg)
  307. DEF( set_arg3, 1, 1, 1, none_arg)
  308. DEF( get_var_ref0, 1, 0, 1, none_var_ref)
  309. DEF( get_var_ref1, 1, 0, 1, none_var_ref)
  310. DEF( get_var_ref2, 1, 0, 1, none_var_ref)
  311. DEF( get_var_ref3, 1, 0, 1, none_var_ref)
  312. DEF( put_var_ref0, 1, 1, 0, none_var_ref)
  313. DEF( put_var_ref1, 1, 1, 0, none_var_ref)
  314. DEF( put_var_ref2, 1, 1, 0, none_var_ref)
  315. DEF( put_var_ref3, 1, 1, 0, none_var_ref)
  316. DEF( set_var_ref0, 1, 1, 1, none_var_ref)
  317. DEF( set_var_ref1, 1, 1, 1, none_var_ref)
  318. DEF( set_var_ref2, 1, 1, 1, none_var_ref)
  319. DEF( set_var_ref3, 1, 1, 1, none_var_ref)
  320. DEF( get_length, 1, 1, 1, none)
  321. DEF( if_false8, 2, 1, 0, label8)
  322. DEF( if_true8, 2, 1, 0, label8) /* must come after if_false8 */
  323. DEF( goto8, 2, 0, 0, label8) /* must come after if_true8 */
  324. DEF( goto16, 3, 0, 0, label16)
  325. DEF( call0, 1, 1, 1, npopx)
  326. DEF( call1, 1, 1, 1, npopx)
  327. DEF( call2, 1, 1, 1, npopx)
  328. DEF( call3, 1, 1, 1, npopx)
  329. DEF( is_undefined, 1, 1, 1, none)
  330. DEF( is_null, 1, 1, 1, none)
  331. DEF(typeof_is_undefined, 1, 1, 1, none)
  332. DEF( typeof_is_function, 1, 1, 1, none)
  333. #endif
  334. #undef DEF
  335. #undef def
  336. #endif /* DEF */