Op.h 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906
  1. /*
  2. * Copyright (c) 2021-2023, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, Linus Groh <linusg@serenityos.org>
  4. * Copyright (c) 2021, Gunnar Beutner <gbeutner@serenityos.org>
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #pragma once
  9. #include <AK/FixedArray.h>
  10. #include <AK/StdLibExtras.h>
  11. #include <LibCrypto/BigInt/SignedBigInteger.h>
  12. #include <LibJS/Bytecode/Builtins.h>
  13. #include <LibJS/Bytecode/IdentifierTable.h>
  14. #include <LibJS/Bytecode/Instruction.h>
  15. #include <LibJS/Bytecode/Label.h>
  16. #include <LibJS/Bytecode/Operand.h>
  17. #include <LibJS/Bytecode/RegexTable.h>
  18. #include <LibJS/Bytecode/Register.h>
  19. #include <LibJS/Bytecode/StringTable.h>
  20. #include <LibJS/Heap/Cell.h>
  21. #include <LibJS/Runtime/Environment.h>
  22. #include <LibJS/Runtime/Iterator.h>
  23. #include <LibJS/Runtime/Value.h>
  24. #include <LibJS/Runtime/ValueTraits.h>
  25. namespace JS {
  26. class FunctionExpression;
  27. }
  28. namespace JS::Bytecode::Op {
  29. class Mov final : public Instruction {
  30. public:
  31. Mov(Operand dst, Operand src)
  32. : Instruction(Type::Mov, sizeof(*this))
  33. , m_dst(dst)
  34. , m_src(src)
  35. {
  36. }
  37. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  38. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  39. Operand dst() const { return m_dst; }
  40. Operand src() const { return m_src; }
  41. private:
  42. Operand m_dst;
  43. Operand m_src;
  44. };
  45. #define JS_ENUMERATE_COMMON_BINARY_OPS_WITH_FAST_PATH(O) \
  46. O(Add, add) \
  47. O(BitwiseAnd, bitwise_and) \
  48. O(BitwiseOr, bitwise_or) \
  49. O(BitwiseXor, bitwise_xor) \
  50. O(GreaterThan, greater_than) \
  51. O(GreaterThanEquals, greater_than_equals) \
  52. O(LeftShift, left_shift) \
  53. O(LessThan, less_than) \
  54. O(LessThanEquals, less_than_equals) \
  55. O(Mul, mul) \
  56. O(RightShift, right_shift) \
  57. O(Sub, sub) \
  58. O(UnsignedRightShift, unsigned_right_shift)
  59. #define JS_ENUMERATE_COMMON_BINARY_OPS_WITHOUT_FAST_PATH(O) \
  60. O(Div, div) \
  61. O(Exp, exp) \
  62. O(Mod, mod) \
  63. O(In, in) \
  64. O(InstanceOf, instance_of) \
  65. O(LooselyInequals, loosely_inequals) \
  66. O(LooselyEquals, loosely_equals) \
  67. O(StrictlyInequals, strict_inequals) \
  68. O(StrictlyEquals, strict_equals)
  69. #define JS_DECLARE_COMMON_BINARY_OP(OpTitleCase, op_snake_case) \
  70. class OpTitleCase final : public Instruction { \
  71. public: \
  72. explicit OpTitleCase(Operand dst, Operand lhs, Operand rhs) \
  73. : Instruction(Type::OpTitleCase, sizeof(*this)) \
  74. , m_dst(dst) \
  75. , m_lhs(lhs) \
  76. , m_rhs(rhs) \
  77. { \
  78. } \
  79. \
  80. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const; \
  81. ByteString to_byte_string_impl(Bytecode::Executable const&) const; \
  82. \
  83. Operand dst() const { return m_dst; } \
  84. Operand lhs() const { return m_lhs; } \
  85. Operand rhs() const { return m_rhs; } \
  86. \
  87. private: \
  88. Operand m_dst; \
  89. Operand m_lhs; \
  90. Operand m_rhs; \
  91. };
  92. JS_ENUMERATE_COMMON_BINARY_OPS_WITHOUT_FAST_PATH(JS_DECLARE_COMMON_BINARY_OP)
  93. JS_ENUMERATE_COMMON_BINARY_OPS_WITH_FAST_PATH(JS_DECLARE_COMMON_BINARY_OP)
  94. #undef JS_DECLARE_COMMON_BINARY_OP
  95. #define JS_ENUMERATE_COMMON_UNARY_OPS(O) \
  96. O(BitwiseNot, bitwise_not) \
  97. O(Not, not_) \
  98. O(UnaryPlus, unary_plus) \
  99. O(UnaryMinus, unary_minus) \
  100. O(Typeof, typeof_)
  101. #define JS_DECLARE_COMMON_UNARY_OP(OpTitleCase, op_snake_case) \
  102. class OpTitleCase final : public Instruction { \
  103. public: \
  104. OpTitleCase(Operand dst, Operand src) \
  105. : Instruction(Type::OpTitleCase, sizeof(*this)) \
  106. , m_dst(dst) \
  107. , m_src(src) \
  108. { \
  109. } \
  110. \
  111. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const; \
  112. ByteString to_byte_string_impl(Bytecode::Executable const&) const; \
  113. \
  114. Operand dst() const { return m_dst; } \
  115. Operand src() const { return m_src; } \
  116. \
  117. private: \
  118. Operand m_dst; \
  119. Operand m_src; \
  120. };
  121. JS_ENUMERATE_COMMON_UNARY_OPS(JS_DECLARE_COMMON_UNARY_OP)
  122. #undef JS_DECLARE_COMMON_UNARY_OP
  123. class NewObject final : public Instruction {
  124. public:
  125. explicit NewObject(Operand dst)
  126. : Instruction(Type::NewObject, sizeof(*this))
  127. , m_dst(dst)
  128. {
  129. }
  130. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  131. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  132. Operand dst() const { return m_dst; }
  133. private:
  134. Operand m_dst;
  135. };
  136. class NewRegExp final : public Instruction {
  137. public:
  138. NewRegExp(Operand dst, StringTableIndex source_index, StringTableIndex flags_index, RegexTableIndex regex_index)
  139. : Instruction(Type::NewRegExp, sizeof(*this))
  140. , m_dst(dst)
  141. , m_source_index(source_index)
  142. , m_flags_index(flags_index)
  143. , m_regex_index(regex_index)
  144. {
  145. }
  146. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  147. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  148. Operand dst() const { return m_dst; }
  149. StringTableIndex source_index() const { return m_source_index; }
  150. StringTableIndex flags_index() const { return m_flags_index; }
  151. RegexTableIndex regex_index() const { return m_regex_index; }
  152. private:
  153. Operand m_dst;
  154. StringTableIndex m_source_index;
  155. StringTableIndex m_flags_index;
  156. RegexTableIndex m_regex_index;
  157. };
  158. #define JS_ENUMERATE_NEW_BUILTIN_ERROR_OPS(O) \
  159. O(TypeError)
  160. #define JS_DECLARE_NEW_BUILTIN_ERROR_OP(ErrorName) \
  161. class New##ErrorName final : public Instruction { \
  162. public: \
  163. New##ErrorName(Operand dst, StringTableIndex error_string) \
  164. : Instruction(Type::New##ErrorName, sizeof(*this)) \
  165. , m_dst(dst) \
  166. , m_error_string(error_string) \
  167. { \
  168. } \
  169. \
  170. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const; \
  171. ByteString to_byte_string_impl(Bytecode::Executable const&) const; \
  172. \
  173. Operand dst() const { return m_dst; } \
  174. StringTableIndex error_string() const { return m_error_string; } \
  175. \
  176. private: \
  177. Operand m_dst; \
  178. StringTableIndex m_error_string; \
  179. };
  180. JS_ENUMERATE_NEW_BUILTIN_ERROR_OPS(JS_DECLARE_NEW_BUILTIN_ERROR_OP)
  181. #undef JS_DECLARE_NEW_BUILTIN_ERROR_OP
  182. // NOTE: This instruction is variable-width depending on the number of excluded names
  183. class CopyObjectExcludingProperties final : public Instruction {
  184. public:
  185. CopyObjectExcludingProperties(Operand dst, Operand from_object, Vector<Operand> const& excluded_names)
  186. : Instruction(Type::CopyObjectExcludingProperties, length_impl(excluded_names.size()))
  187. , m_dst(dst)
  188. , m_from_object(from_object)
  189. , m_excluded_names_count(excluded_names.size())
  190. {
  191. for (size_t i = 0; i < m_excluded_names_count; i++)
  192. m_excluded_names[i] = excluded_names[i];
  193. }
  194. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  195. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  196. size_t length_impl(size_t excluded_names_count) const
  197. {
  198. return round_up_to_power_of_two(alignof(void*), sizeof(*this) + sizeof(Operand) * excluded_names_count);
  199. }
  200. Operand dst() const { return m_dst; }
  201. Operand from_object() const { return m_from_object; }
  202. size_t excluded_names_count() const { return m_excluded_names_count; }
  203. Operand const* excluded_names() const { return m_excluded_names; }
  204. private:
  205. Operand m_dst;
  206. Operand m_from_object;
  207. size_t m_excluded_names_count { 0 };
  208. Operand m_excluded_names[];
  209. };
  210. // NOTE: This instruction is variable-width depending on the number of elements!
  211. class NewArray final : public Instruction {
  212. public:
  213. explicit NewArray(Operand dst)
  214. : Instruction(Type::NewArray, length_impl(0))
  215. , m_dst(dst)
  216. , m_element_count(0)
  217. {
  218. }
  219. NewArray(Operand dst, AK::Array<Operand, 2> const& elements_range)
  220. : Instruction(Type::NewArray, length_impl(elements_range[1].index() - elements_range[0].index() + 1))
  221. , m_dst(dst)
  222. , m_element_count(elements_range[1].index() - elements_range[0].index() + 1)
  223. {
  224. m_elements[0] = elements_range[0];
  225. m_elements[1] = elements_range[1];
  226. }
  227. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  228. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  229. Operand dst() const { return m_dst; }
  230. size_t length_impl(size_t element_count) const
  231. {
  232. return round_up_to_power_of_two(alignof(void*), sizeof(*this) + sizeof(Operand) * (element_count == 0 ? 0 : 2));
  233. }
  234. Operand start() const
  235. {
  236. VERIFY(m_element_count);
  237. return m_elements[0];
  238. }
  239. Operand end() const
  240. {
  241. VERIFY(m_element_count);
  242. return m_elements[1];
  243. }
  244. size_t element_count() const { return m_element_count; }
  245. private:
  246. Operand m_dst;
  247. size_t m_element_count { 0 };
  248. Operand m_elements[];
  249. };
  250. class NewPrimitiveArray final : public Instruction {
  251. public:
  252. NewPrimitiveArray(Operand dst, ReadonlySpan<Value> elements)
  253. : Instruction(Type::NewPrimitiveArray, length_impl(elements.size()))
  254. , m_dst(dst)
  255. , m_element_count(elements.size())
  256. {
  257. for (size_t i = 0; i < m_element_count; ++i)
  258. m_elements[i] = elements[i];
  259. }
  260. size_t length_impl(size_t element_count) const
  261. {
  262. return round_up_to_power_of_two(alignof(void*), sizeof(*this) + sizeof(Value) * element_count);
  263. }
  264. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  265. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  266. Operand dst() const { return m_dst; }
  267. ReadonlySpan<Value> elements() const { return { m_elements, m_element_count }; }
  268. private:
  269. Operand m_dst;
  270. size_t m_element_count { 0 };
  271. Value m_elements[];
  272. };
  273. class ArrayAppend final : public Instruction {
  274. public:
  275. ArrayAppend(Operand dst, Operand src, bool is_spread)
  276. : Instruction(Type::ArrayAppend, sizeof(*this))
  277. , m_dst(dst)
  278. , m_src(src)
  279. , m_is_spread(is_spread)
  280. {
  281. }
  282. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  283. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  284. Operand dst() const { return m_dst; }
  285. Operand src() const { return m_src; }
  286. bool is_spread() const { return m_is_spread; }
  287. private:
  288. Operand m_dst;
  289. Operand m_src;
  290. bool m_is_spread = false;
  291. };
  292. class ImportCall final : public Instruction {
  293. public:
  294. ImportCall(Operand dst, Operand specifier, Operand options)
  295. : Instruction(Type::ImportCall, sizeof(*this))
  296. , m_dst(dst)
  297. , m_specifier(specifier)
  298. , m_options(options)
  299. {
  300. }
  301. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  302. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  303. Operand dst() const { return m_dst; }
  304. Operand specifier() const { return m_specifier; }
  305. Operand options() const { return m_options; }
  306. private:
  307. Operand m_dst;
  308. Operand m_specifier;
  309. Operand m_options;
  310. };
  311. class IteratorToArray final : public Instruction {
  312. public:
  313. explicit IteratorToArray(Operand dst, Operand iterator)
  314. : Instruction(Type::IteratorToArray, sizeof(*this))
  315. , m_dst(dst)
  316. , m_iterator(iterator)
  317. {
  318. }
  319. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  320. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  321. Operand dst() const { return m_dst; }
  322. Operand iterator() const { return m_iterator; }
  323. private:
  324. Operand m_dst;
  325. Operand m_iterator;
  326. };
  327. class ConcatString final : public Instruction {
  328. public:
  329. explicit ConcatString(Operand dst, Operand src)
  330. : Instruction(Type::ConcatString, sizeof(*this))
  331. , m_dst(dst)
  332. , m_src(src)
  333. {
  334. }
  335. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  336. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  337. Operand dst() const { return m_dst; }
  338. Operand src() const { return m_src; }
  339. private:
  340. Operand m_dst;
  341. Operand m_src;
  342. };
  343. enum class EnvironmentMode {
  344. Lexical,
  345. Var,
  346. };
  347. class CreateLexicalEnvironment final : public Instruction {
  348. public:
  349. explicit CreateLexicalEnvironment()
  350. : Instruction(Type::CreateLexicalEnvironment, sizeof(*this))
  351. {
  352. }
  353. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  354. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  355. };
  356. class EnterObjectEnvironment final : public Instruction {
  357. public:
  358. explicit EnterObjectEnvironment(Operand object)
  359. : Instruction(Type::EnterObjectEnvironment, sizeof(*this))
  360. , m_object(object)
  361. {
  362. }
  363. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  364. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  365. Operand object() const { return m_object; }
  366. private:
  367. Operand m_object;
  368. };
  369. class Catch final : public Instruction {
  370. public:
  371. explicit Catch(Operand dst)
  372. : Instruction(Type::Catch, sizeof(*this))
  373. , m_dst(dst)
  374. {
  375. }
  376. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  377. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  378. Operand dst() const { return m_dst; }
  379. private:
  380. Operand m_dst;
  381. };
  382. class CreateVariable final : public Instruction {
  383. public:
  384. explicit CreateVariable(IdentifierTableIndex identifier, EnvironmentMode mode, bool is_immutable, bool is_global = false, bool is_strict = false)
  385. : Instruction(Type::CreateVariable, sizeof(*this))
  386. , m_identifier(identifier)
  387. , m_mode(mode)
  388. , m_is_immutable(is_immutable)
  389. , m_is_global(is_global)
  390. , m_is_strict(is_strict)
  391. {
  392. }
  393. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  394. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  395. IdentifierTableIndex identifier() const { return m_identifier; }
  396. EnvironmentMode mode() const { return m_mode; }
  397. bool is_immutable() const { return m_is_immutable; }
  398. bool is_global() const { return m_is_global; }
  399. bool is_strict() const { return m_is_strict; }
  400. private:
  401. IdentifierTableIndex m_identifier;
  402. EnvironmentMode m_mode;
  403. bool m_is_immutable : 4 { false };
  404. bool m_is_global : 4 { false };
  405. bool m_is_strict { false };
  406. };
  407. class SetVariable final : public Instruction {
  408. public:
  409. enum class InitializationMode {
  410. Initialize,
  411. Set,
  412. };
  413. explicit SetVariable(IdentifierTableIndex identifier, Operand src, u32 cache_index, InitializationMode initialization_mode = InitializationMode::Set, EnvironmentMode mode = EnvironmentMode::Lexical)
  414. : Instruction(Type::SetVariable, sizeof(*this))
  415. , m_identifier(identifier)
  416. , m_src(src)
  417. , m_mode(mode)
  418. , m_initialization_mode(initialization_mode)
  419. , m_cache_index(cache_index)
  420. {
  421. }
  422. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  423. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  424. IdentifierTableIndex identifier() const { return m_identifier; }
  425. Operand src() const { return m_src; }
  426. EnvironmentMode mode() const { return m_mode; }
  427. InitializationMode initialization_mode() const { return m_initialization_mode; }
  428. u32 cache_index() const { return m_cache_index; }
  429. private:
  430. IdentifierTableIndex m_identifier;
  431. Operand m_src;
  432. EnvironmentMode m_mode;
  433. InitializationMode m_initialization_mode { InitializationMode::Set };
  434. u32 m_cache_index { 0 };
  435. };
  436. class SetLocal final : public Instruction {
  437. public:
  438. SetLocal(size_t index, Operand src)
  439. : Instruction(Type::SetLocal, sizeof(*this))
  440. , m_index(index)
  441. , m_src(src)
  442. {
  443. }
  444. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  445. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  446. size_t index() const { return m_index; }
  447. Operand dst() const { return Operand(Operand::Type::Local, m_index); }
  448. Operand src() const { return m_src; }
  449. private:
  450. size_t m_index;
  451. Operand m_src;
  452. };
  453. class GetCalleeAndThisFromEnvironment final : public Instruction {
  454. public:
  455. explicit GetCalleeAndThisFromEnvironment(Operand callee, Operand this_value, IdentifierTableIndex identifier, u32 cache_index)
  456. : Instruction(Type::GetCalleeAndThisFromEnvironment, sizeof(*this))
  457. , m_identifier(identifier)
  458. , m_callee(callee)
  459. , m_this_value(this_value)
  460. , m_cache_index(cache_index)
  461. {
  462. }
  463. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  464. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  465. IdentifierTableIndex identifier() const { return m_identifier; }
  466. u32 cache_index() const { return m_cache_index; }
  467. Operand callee() const { return m_callee; }
  468. Operand this_() const { return m_this_value; }
  469. private:
  470. IdentifierTableIndex m_identifier;
  471. Operand m_callee;
  472. Operand m_this_value;
  473. u32 m_cache_index { 0 };
  474. };
  475. class GetVariable final : public Instruction {
  476. public:
  477. explicit GetVariable(Operand dst, IdentifierTableIndex identifier, u32 cache_index)
  478. : Instruction(Type::GetVariable, sizeof(*this))
  479. , m_dst(dst)
  480. , m_identifier(identifier)
  481. , m_cache_index(cache_index)
  482. {
  483. }
  484. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  485. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  486. Operand dst() const { return m_dst; }
  487. IdentifierTableIndex identifier() const { return m_identifier; }
  488. u32 cache_index() const { return m_cache_index; }
  489. private:
  490. Operand m_dst;
  491. IdentifierTableIndex m_identifier;
  492. u32 m_cache_index { 0 };
  493. };
  494. class GetGlobal final : public Instruction {
  495. public:
  496. GetGlobal(Operand dst, IdentifierTableIndex identifier, u32 cache_index)
  497. : Instruction(Type::GetGlobal, sizeof(*this))
  498. , m_dst(dst)
  499. , m_identifier(identifier)
  500. , m_cache_index(cache_index)
  501. {
  502. }
  503. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  504. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  505. Operand dst() const { return m_dst; }
  506. IdentifierTableIndex identifier() const { return m_identifier; }
  507. u32 cache_index() const { return m_cache_index; }
  508. private:
  509. Operand m_dst;
  510. IdentifierTableIndex m_identifier;
  511. u32 m_cache_index { 0 };
  512. };
  513. class DeleteVariable final : public Instruction {
  514. public:
  515. explicit DeleteVariable(Operand dst, IdentifierTableIndex identifier)
  516. : Instruction(Type::DeleteVariable, sizeof(*this))
  517. , m_dst(dst)
  518. , m_identifier(identifier)
  519. {
  520. }
  521. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  522. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  523. Operand dst() const { return m_dst; }
  524. IdentifierTableIndex identifier() const { return m_identifier; }
  525. private:
  526. Operand m_dst;
  527. IdentifierTableIndex m_identifier;
  528. };
  529. class GetById final : public Instruction {
  530. public:
  531. GetById(Operand dst, Operand base, IdentifierTableIndex property, u32 cache_index)
  532. : Instruction(Type::GetById, sizeof(*this))
  533. , m_dst(dst)
  534. , m_base(base)
  535. , m_property(property)
  536. , m_cache_index(cache_index)
  537. {
  538. }
  539. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  540. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  541. Operand dst() const { return m_dst; }
  542. Operand base() const { return m_base; }
  543. IdentifierTableIndex property() const { return m_property; }
  544. u32 cache_index() const { return m_cache_index; }
  545. private:
  546. Operand m_dst;
  547. Operand m_base;
  548. IdentifierTableIndex m_property;
  549. u32 m_cache_index { 0 };
  550. };
  551. class GetByIdWithThis final : public Instruction {
  552. public:
  553. GetByIdWithThis(Operand dst, Operand base, IdentifierTableIndex property, Operand this_value, u32 cache_index)
  554. : Instruction(Type::GetByIdWithThis, sizeof(*this))
  555. , m_dst(dst)
  556. , m_base(base)
  557. , m_property(property)
  558. , m_this_value(this_value)
  559. , m_cache_index(cache_index)
  560. {
  561. }
  562. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  563. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  564. Operand dst() const { return m_dst; }
  565. Operand base() const { return m_base; }
  566. IdentifierTableIndex property() const { return m_property; }
  567. Operand this_value() const { return m_this_value; }
  568. u32 cache_index() const { return m_cache_index; }
  569. private:
  570. Operand m_dst;
  571. Operand m_base;
  572. IdentifierTableIndex m_property;
  573. Operand m_this_value;
  574. u32 m_cache_index { 0 };
  575. };
  576. class GetPrivateById final : public Instruction {
  577. public:
  578. explicit GetPrivateById(Operand dst, Operand base, IdentifierTableIndex property)
  579. : Instruction(Type::GetPrivateById, sizeof(*this))
  580. , m_dst(dst)
  581. , m_base(base)
  582. , m_property(property)
  583. {
  584. }
  585. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  586. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  587. Operand dst() const { return m_dst; }
  588. Operand base() const { return m_base; }
  589. IdentifierTableIndex property() const { return m_property; }
  590. private:
  591. Operand m_dst;
  592. Operand m_base;
  593. IdentifierTableIndex m_property;
  594. };
  595. class HasPrivateId final : public Instruction {
  596. public:
  597. HasPrivateId(Operand dst, Operand base, IdentifierTableIndex property)
  598. : Instruction(Type::HasPrivateId, sizeof(*this))
  599. , m_dst(dst)
  600. , m_base(base)
  601. , m_property(property)
  602. {
  603. }
  604. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  605. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  606. Operand dst() const { return m_dst; }
  607. Operand base() const { return m_base; }
  608. IdentifierTableIndex property() const { return m_property; }
  609. private:
  610. Operand m_dst;
  611. Operand m_base;
  612. IdentifierTableIndex m_property;
  613. };
  614. enum class PropertyKind {
  615. Getter,
  616. Setter,
  617. KeyValue,
  618. DirectKeyValue, // Used for Object expressions. Always sets an own property, never calls a setter.
  619. Spread,
  620. ProtoSetter,
  621. };
  622. class PutById final : public Instruction {
  623. public:
  624. explicit PutById(Operand base, IdentifierTableIndex property, Operand src, PropertyKind kind, u32 cache_index)
  625. : Instruction(Type::PutById, sizeof(*this))
  626. , m_base(base)
  627. , m_property(property)
  628. , m_src(src)
  629. , m_kind(kind)
  630. , m_cache_index(cache_index)
  631. {
  632. }
  633. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  634. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  635. Operand base() const { return m_base; }
  636. IdentifierTableIndex property() const { return m_property; }
  637. Operand src() const { return m_src; }
  638. PropertyKind kind() const { return m_kind; }
  639. u32 cache_index() const { return m_cache_index; }
  640. private:
  641. Operand m_base;
  642. IdentifierTableIndex m_property;
  643. Operand m_src;
  644. PropertyKind m_kind;
  645. u32 m_cache_index { 0 };
  646. };
  647. class PutByIdWithThis final : public Instruction {
  648. public:
  649. PutByIdWithThis(Operand base, Operand this_value, IdentifierTableIndex property, Operand src, PropertyKind kind, u32 cache_index)
  650. : Instruction(Type::PutByIdWithThis, sizeof(*this))
  651. , m_base(base)
  652. , m_this_value(this_value)
  653. , m_property(property)
  654. , m_src(src)
  655. , m_kind(kind)
  656. , m_cache_index(cache_index)
  657. {
  658. }
  659. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  660. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  661. Operand base() const { return m_base; }
  662. Operand this_value() const { return m_this_value; }
  663. IdentifierTableIndex property() const { return m_property; }
  664. Operand src() const { return m_src; }
  665. PropertyKind kind() const { return m_kind; }
  666. u32 cache_index() const { return m_cache_index; }
  667. private:
  668. Operand m_base;
  669. Operand m_this_value;
  670. IdentifierTableIndex m_property;
  671. Operand m_src;
  672. PropertyKind m_kind;
  673. u32 m_cache_index { 0 };
  674. };
  675. class PutPrivateById final : public Instruction {
  676. public:
  677. explicit PutPrivateById(Operand base, IdentifierTableIndex property, Operand src, PropertyKind kind = PropertyKind::KeyValue)
  678. : Instruction(Type::PutPrivateById, sizeof(*this))
  679. , m_base(base)
  680. , m_property(property)
  681. , m_src(src)
  682. , m_kind(kind)
  683. {
  684. }
  685. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  686. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  687. Operand base() const { return m_base; }
  688. IdentifierTableIndex property() const { return m_property; }
  689. Operand src() const { return m_src; }
  690. private:
  691. Operand m_base;
  692. IdentifierTableIndex m_property;
  693. Operand m_src;
  694. PropertyKind m_kind;
  695. };
  696. class DeleteById final : public Instruction {
  697. public:
  698. explicit DeleteById(Operand dst, Operand base, IdentifierTableIndex property)
  699. : Instruction(Type::DeleteById, sizeof(*this))
  700. , m_dst(dst)
  701. , m_base(base)
  702. , m_property(property)
  703. {
  704. }
  705. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  706. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  707. Operand dst() const { return m_dst; }
  708. Operand base() const { return m_base; }
  709. IdentifierTableIndex property() const { return m_property; }
  710. private:
  711. Operand m_dst;
  712. Operand m_base;
  713. IdentifierTableIndex m_property;
  714. };
  715. class DeleteByIdWithThis final : public Instruction {
  716. public:
  717. DeleteByIdWithThis(Operand dst, Operand base, Operand this_value, IdentifierTableIndex property)
  718. : Instruction(Type::DeleteByIdWithThis, sizeof(*this))
  719. , m_dst(dst)
  720. , m_base(base)
  721. , m_this_value(this_value)
  722. , m_property(property)
  723. {
  724. }
  725. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  726. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  727. Operand dst() const { return m_dst; }
  728. Operand base() const { return m_base; }
  729. Operand this_value() const { return m_this_value; }
  730. IdentifierTableIndex property() const { return m_property; }
  731. private:
  732. Operand m_dst;
  733. Operand m_base;
  734. Operand m_this_value;
  735. IdentifierTableIndex m_property;
  736. };
  737. class GetByValue final : public Instruction {
  738. public:
  739. explicit GetByValue(Operand dst, Operand base, Operand property)
  740. : Instruction(Type::GetByValue, sizeof(*this))
  741. , m_dst(dst)
  742. , m_base(base)
  743. , m_property(property)
  744. {
  745. }
  746. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  747. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  748. Operand dst() const { return m_dst; }
  749. Operand base() const { return m_base; }
  750. Operand property() const { return m_property; }
  751. private:
  752. Operand m_dst;
  753. Operand m_base;
  754. Operand m_property;
  755. };
  756. class GetByValueWithThis final : public Instruction {
  757. public:
  758. GetByValueWithThis(Operand dst, Operand base, Operand property, Operand this_value)
  759. : Instruction(Type::GetByValueWithThis, sizeof(*this))
  760. , m_dst(dst)
  761. , m_base(base)
  762. , m_property(property)
  763. , m_this_value(this_value)
  764. {
  765. }
  766. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  767. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  768. Operand dst() const { return m_dst; }
  769. Operand base() const { return m_base; }
  770. Operand property() const { return m_property; }
  771. Operand this_value() const { return m_this_value; }
  772. private:
  773. Operand m_dst;
  774. Operand m_base;
  775. Operand m_property;
  776. Operand m_this_value;
  777. };
  778. class PutByValue final : public Instruction {
  779. public:
  780. PutByValue(Operand base, Operand property, Operand src, PropertyKind kind = PropertyKind::KeyValue)
  781. : Instruction(Type::PutByValue, sizeof(*this))
  782. , m_base(base)
  783. , m_property(property)
  784. , m_src(src)
  785. , m_kind(kind)
  786. {
  787. }
  788. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  789. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  790. Operand base() const { return m_base; }
  791. Operand property() const { return m_property; }
  792. Operand src() const { return m_src; }
  793. PropertyKind kind() const { return m_kind; }
  794. private:
  795. Operand m_base;
  796. Operand m_property;
  797. Operand m_src;
  798. PropertyKind m_kind;
  799. };
  800. class PutByValueWithThis final : public Instruction {
  801. public:
  802. PutByValueWithThis(Operand base, Operand property, Operand this_value, Operand src, PropertyKind kind = PropertyKind::KeyValue)
  803. : Instruction(Type::PutByValueWithThis, sizeof(*this))
  804. , m_base(base)
  805. , m_property(property)
  806. , m_this_value(this_value)
  807. , m_src(src)
  808. , m_kind(kind)
  809. {
  810. }
  811. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  812. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  813. Operand base() const { return m_base; }
  814. Operand property() const { return m_property; }
  815. Operand this_value() const { return m_this_value; }
  816. Operand src() const { return m_src; }
  817. PropertyKind kind() const { return m_kind; }
  818. private:
  819. Operand m_base;
  820. Operand m_property;
  821. Operand m_this_value;
  822. Operand m_src;
  823. PropertyKind m_kind;
  824. };
  825. class DeleteByValue final : public Instruction {
  826. public:
  827. DeleteByValue(Operand dst, Operand base, Operand property)
  828. : Instruction(Type::DeleteByValue, sizeof(*this))
  829. , m_dst(dst)
  830. , m_base(base)
  831. , m_property(property)
  832. {
  833. }
  834. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  835. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  836. Operand dst() const { return m_dst; }
  837. Operand base() const { return m_base; }
  838. Operand property() const { return m_property; }
  839. private:
  840. Operand m_dst;
  841. Operand m_base;
  842. Operand m_property;
  843. };
  844. class DeleteByValueWithThis final : public Instruction {
  845. public:
  846. DeleteByValueWithThis(Operand dst, Operand base, Operand this_value, Operand property)
  847. : Instruction(Type::DeleteByValueWithThis, sizeof(*this))
  848. , m_dst(dst)
  849. , m_base(base)
  850. , m_this_value(this_value)
  851. , m_property(property)
  852. {
  853. }
  854. Operand dst() const { return m_dst; }
  855. Operand base() const { return m_base; }
  856. Operand this_value() const { return m_this_value; }
  857. Operand property() const { return m_property; }
  858. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  859. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  860. private:
  861. Operand m_dst;
  862. Operand m_base;
  863. Operand m_this_value;
  864. Operand m_property;
  865. };
  866. class Jump : public Instruction {
  867. public:
  868. constexpr static bool IsTerminator = true;
  869. explicit Jump(Type type, Label taken_target, Optional<Label> nontaken_target = {})
  870. : Instruction(type, sizeof(*this))
  871. , m_true_target(move(taken_target))
  872. , m_false_target(move(nontaken_target))
  873. {
  874. }
  875. explicit Jump(Type type, Label taken_target, Label nontaken_target, size_t sizeof_self)
  876. : Instruction(type, sizeof_self)
  877. , m_true_target(move(taken_target))
  878. , m_false_target(move(nontaken_target))
  879. {
  880. }
  881. explicit Jump(Label taken_target, Optional<Label> nontaken_target = {})
  882. : Instruction(Type::Jump, sizeof(*this))
  883. , m_true_target(move(taken_target))
  884. , m_false_target(move(nontaken_target))
  885. {
  886. }
  887. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  888. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  889. auto& true_target() const { return m_true_target; }
  890. auto& false_target() const { return m_false_target; }
  891. protected:
  892. Optional<Label> m_true_target;
  893. Optional<Label> m_false_target;
  894. };
  895. class JumpIf final : public Jump {
  896. public:
  897. explicit JumpIf(Operand condition, Label true_target, Label false_target)
  898. : Jump(Type::JumpIf, move(true_target), move(false_target), sizeof(*this))
  899. , m_condition(condition)
  900. {
  901. }
  902. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  903. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  904. Operand condition() const { return m_condition; }
  905. private:
  906. Operand m_condition;
  907. };
  908. class JumpNullish final : public Jump {
  909. public:
  910. explicit JumpNullish(Operand condition, Label true_target, Label false_target)
  911. : Jump(Type::JumpNullish, move(true_target), move(false_target), sizeof(*this))
  912. , m_condition(condition)
  913. {
  914. }
  915. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  916. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  917. Operand condition() const { return m_condition; }
  918. private:
  919. Operand m_condition;
  920. };
  921. class JumpUndefined final : public Jump {
  922. public:
  923. explicit JumpUndefined(Operand condition, Label true_target, Label false_target)
  924. : Jump(Type::JumpUndefined, move(true_target), move(false_target), sizeof(*this))
  925. , m_condition(condition)
  926. {
  927. }
  928. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  929. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  930. Operand condition() const { return m_condition; }
  931. private:
  932. Operand m_condition;
  933. };
  934. enum class CallType {
  935. Call,
  936. Construct,
  937. DirectEval,
  938. };
  939. class Call final : public Instruction {
  940. public:
  941. Call(CallType type, Operand dst, Operand callee, Operand this_value, ReadonlySpan<Operand> arguments, Optional<StringTableIndex> expression_string = {}, Optional<Builtin> builtin = {})
  942. : Instruction(Type::Call, length_impl(arguments.size()))
  943. , m_dst(dst)
  944. , m_callee(callee)
  945. , m_this_value(this_value)
  946. , m_argument_count(arguments.size())
  947. , m_type(type)
  948. , m_expression_string(expression_string)
  949. , m_builtin(builtin)
  950. {
  951. for (size_t i = 0; i < arguments.size(); ++i)
  952. m_arguments[i] = arguments[i];
  953. }
  954. size_t length_impl(size_t argument_count) const
  955. {
  956. return round_up_to_power_of_two(alignof(void*), sizeof(*this) + sizeof(Operand) * argument_count);
  957. }
  958. CallType call_type() const { return m_type; }
  959. Operand dst() const { return m_dst; }
  960. Operand callee() const { return m_callee; }
  961. Operand this_value() const { return m_this_value; }
  962. Optional<StringTableIndex> const& expression_string() const { return m_expression_string; }
  963. u32 argument_count() const { return m_argument_count; }
  964. Optional<Builtin> const& builtin() const { return m_builtin; }
  965. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  966. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  967. private:
  968. Operand m_dst;
  969. Operand m_callee;
  970. Operand m_this_value;
  971. u32 m_argument_count { 0 };
  972. CallType m_type;
  973. Optional<StringTableIndex> m_expression_string;
  974. Optional<Builtin> m_builtin;
  975. Operand m_arguments[];
  976. };
  977. class CallWithArgumentArray final : public Instruction {
  978. public:
  979. CallWithArgumentArray(CallType type, Operand dst, Operand callee, Operand this_value, Operand arguments, Optional<StringTableIndex> expression_string = {})
  980. : Instruction(Type::CallWithArgumentArray, sizeof(*this))
  981. , m_dst(dst)
  982. , m_callee(callee)
  983. , m_this_value(this_value)
  984. , m_arguments(arguments)
  985. , m_type(type)
  986. , m_expression_string(expression_string)
  987. {
  988. }
  989. Operand dst() const { return m_dst; }
  990. CallType call_type() const { return m_type; }
  991. Operand callee() const { return m_callee; }
  992. Operand this_value() const { return m_this_value; }
  993. Operand arguments() const { return m_arguments; }
  994. Optional<StringTableIndex> const& expression_string() const { return m_expression_string; }
  995. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  996. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  997. private:
  998. Operand m_dst;
  999. Operand m_callee;
  1000. Operand m_this_value;
  1001. Operand m_arguments;
  1002. CallType m_type;
  1003. Optional<StringTableIndex> m_expression_string;
  1004. };
  1005. class SuperCallWithArgumentArray : public Instruction {
  1006. public:
  1007. explicit SuperCallWithArgumentArray(Operand dst, Operand arguments, bool is_synthetic)
  1008. : Instruction(Type::SuperCallWithArgumentArray, sizeof(*this))
  1009. , m_dst(dst)
  1010. , m_arguments(arguments)
  1011. , m_is_synthetic(is_synthetic)
  1012. {
  1013. }
  1014. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1015. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  1016. Operand dst() const { return m_dst; }
  1017. Operand arguments() const { return m_arguments; }
  1018. bool is_synthetic() const { return m_is_synthetic; }
  1019. private:
  1020. Operand m_dst;
  1021. Operand m_arguments;
  1022. bool m_is_synthetic;
  1023. };
  1024. class NewClass final : public Instruction {
  1025. public:
  1026. explicit NewClass(Operand dst, Optional<Operand> super_class, ClassExpression const& class_expression, Optional<IdentifierTableIndex> lhs_name)
  1027. : Instruction(Type::NewClass, sizeof(*this))
  1028. , m_dst(dst)
  1029. , m_super_class(super_class)
  1030. , m_class_expression(class_expression)
  1031. , m_lhs_name(lhs_name)
  1032. {
  1033. }
  1034. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1035. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  1036. Operand dst() const { return m_dst; }
  1037. Optional<Operand> const& super_class() const { return m_super_class; }
  1038. ClassExpression const& class_expression() const { return m_class_expression; }
  1039. Optional<IdentifierTableIndex> const& lhs_name() const { return m_lhs_name; }
  1040. private:
  1041. Operand m_dst;
  1042. Optional<Operand> m_super_class;
  1043. ClassExpression const& m_class_expression;
  1044. Optional<IdentifierTableIndex> m_lhs_name;
  1045. };
  1046. class NewFunction final : public Instruction {
  1047. public:
  1048. explicit NewFunction(Operand dst, FunctionExpression const& function_node, Optional<IdentifierTableIndex> lhs_name, Optional<Operand> home_object = {})
  1049. : Instruction(Type::NewFunction, sizeof(*this))
  1050. , m_dst(dst)
  1051. , m_function_node(function_node)
  1052. , m_lhs_name(lhs_name)
  1053. , m_home_object(move(home_object))
  1054. {
  1055. }
  1056. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1057. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  1058. Operand dst() const { return m_dst; }
  1059. FunctionExpression const& function_node() const { return m_function_node; }
  1060. Optional<IdentifierTableIndex> const& lhs_name() const { return m_lhs_name; }
  1061. Optional<Operand> const& home_object() const { return m_home_object; }
  1062. private:
  1063. Operand m_dst;
  1064. FunctionExpression const& m_function_node;
  1065. Optional<IdentifierTableIndex> m_lhs_name;
  1066. Optional<Operand> m_home_object;
  1067. };
  1068. class BlockDeclarationInstantiation final : public Instruction {
  1069. public:
  1070. explicit BlockDeclarationInstantiation(ScopeNode const& scope_node)
  1071. : Instruction(Type::BlockDeclarationInstantiation, sizeof(*this))
  1072. , m_scope_node(scope_node)
  1073. {
  1074. }
  1075. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1076. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  1077. ScopeNode const& scope_node() const { return m_scope_node; }
  1078. private:
  1079. ScopeNode const& m_scope_node;
  1080. };
  1081. class Return final : public Instruction {
  1082. public:
  1083. constexpr static bool IsTerminator = true;
  1084. explicit Return(Optional<Operand> value = {})
  1085. : Instruction(Type::Return, sizeof(*this))
  1086. , m_value(value)
  1087. {
  1088. }
  1089. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1090. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  1091. Optional<Operand> const& value() const { return m_value; }
  1092. private:
  1093. Optional<Operand> m_value;
  1094. };
  1095. class Increment final : public Instruction {
  1096. public:
  1097. explicit Increment(Operand dst)
  1098. : Instruction(Type::Increment, sizeof(*this))
  1099. , m_dst(dst)
  1100. {
  1101. }
  1102. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1103. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  1104. Operand dst() const { return m_dst; }
  1105. private:
  1106. Operand m_dst;
  1107. };
  1108. class PostfixIncrement final : public Instruction {
  1109. public:
  1110. explicit PostfixIncrement(Operand dst, Operand src)
  1111. : Instruction(Type::PostfixIncrement, sizeof(*this))
  1112. , m_dst(dst)
  1113. , m_src(src)
  1114. {
  1115. }
  1116. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1117. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  1118. Operand dst() const { return m_dst; }
  1119. Operand src() const { return m_src; }
  1120. private:
  1121. Operand m_dst;
  1122. Operand m_src;
  1123. };
  1124. class Decrement final : public Instruction {
  1125. public:
  1126. explicit Decrement(Operand dst)
  1127. : Instruction(Type::Decrement, sizeof(*this))
  1128. , m_dst(dst)
  1129. {
  1130. }
  1131. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1132. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  1133. Operand dst() const { return m_dst; }
  1134. private:
  1135. Operand m_dst;
  1136. };
  1137. class PostfixDecrement final : public Instruction {
  1138. public:
  1139. explicit PostfixDecrement(Operand dst, Operand src)
  1140. : Instruction(Type::PostfixDecrement, sizeof(*this))
  1141. , m_dst(dst)
  1142. , m_src(src)
  1143. {
  1144. }
  1145. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1146. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  1147. Operand dst() const { return m_dst; }
  1148. Operand src() const { return m_src; }
  1149. private:
  1150. Operand m_dst;
  1151. Operand m_src;
  1152. };
  1153. class Throw final : public Instruction {
  1154. public:
  1155. constexpr static bool IsTerminator = true;
  1156. explicit Throw(Operand src)
  1157. : Instruction(Type::Throw, sizeof(*this))
  1158. , m_src(src)
  1159. {
  1160. }
  1161. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1162. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  1163. Operand src() const { return m_src; }
  1164. private:
  1165. Operand m_src;
  1166. };
  1167. class ThrowIfNotObject final : public Instruction {
  1168. public:
  1169. ThrowIfNotObject(Operand src)
  1170. : Instruction(Type::ThrowIfNotObject, sizeof(*this))
  1171. , m_src(src)
  1172. {
  1173. }
  1174. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1175. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  1176. Operand src() const { return m_src; }
  1177. private:
  1178. Operand m_src;
  1179. };
  1180. class ThrowIfNullish final : public Instruction {
  1181. public:
  1182. explicit ThrowIfNullish(Operand src)
  1183. : Instruction(Type::ThrowIfNullish, sizeof(*this))
  1184. , m_src(src)
  1185. {
  1186. }
  1187. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1188. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  1189. Operand src() const { return m_src; }
  1190. private:
  1191. Operand m_src;
  1192. };
  1193. class ThrowIfTDZ final : public Instruction {
  1194. public:
  1195. explicit ThrowIfTDZ(Operand src)
  1196. : Instruction(Type::ThrowIfTDZ, sizeof(*this))
  1197. , m_src(src)
  1198. {
  1199. }
  1200. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1201. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  1202. Operand src() const { return m_src; }
  1203. private:
  1204. Operand m_src;
  1205. };
  1206. class EnterUnwindContext final : public Instruction {
  1207. public:
  1208. constexpr static bool IsTerminator = true;
  1209. EnterUnwindContext(Label entry_point)
  1210. : Instruction(Type::EnterUnwindContext, sizeof(*this))
  1211. , m_entry_point(move(entry_point))
  1212. {
  1213. }
  1214. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1215. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  1216. auto& entry_point() const { return m_entry_point; }
  1217. private:
  1218. Label m_entry_point;
  1219. };
  1220. class ScheduleJump final : public Instruction {
  1221. public:
  1222. // Note: We use this instruction to tell the next `finally` block to
  1223. // continue execution with a specific break/continue target;
  1224. // FIXME: We currently don't clear the interpreter internal flag, when we change
  1225. // the control-flow (`break`, `continue`) in a finally-block,
  1226. // FIXME: .NET on x86_64 uses a call to the finally instead, which could make this
  1227. // easier, at the cost of making control-flow changes (`break`, `continue`, `return`)
  1228. // in the finally-block more difficult, but as stated above, those
  1229. // aren't handled 100% correctly at the moment anyway
  1230. // It might be worth investigating a similar mechanism
  1231. constexpr static bool IsTerminator = true;
  1232. ScheduleJump(Label target)
  1233. : Instruction(Type::ScheduleJump, sizeof(*this))
  1234. , m_target(target)
  1235. {
  1236. }
  1237. Label target() const { return m_target; }
  1238. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1239. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  1240. private:
  1241. Label m_target;
  1242. };
  1243. class LeaveLexicalEnvironment final : public Instruction {
  1244. public:
  1245. LeaveLexicalEnvironment()
  1246. : Instruction(Type::LeaveLexicalEnvironment, sizeof(*this))
  1247. {
  1248. }
  1249. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1250. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  1251. };
  1252. class LeaveUnwindContext final : public Instruction {
  1253. public:
  1254. LeaveUnwindContext()
  1255. : Instruction(Type::LeaveUnwindContext, sizeof(*this))
  1256. {
  1257. }
  1258. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1259. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  1260. };
  1261. class ContinuePendingUnwind final : public Instruction {
  1262. public:
  1263. constexpr static bool IsTerminator = true;
  1264. explicit ContinuePendingUnwind(Label resume_target)
  1265. : Instruction(Type::ContinuePendingUnwind, sizeof(*this))
  1266. , m_resume_target(resume_target)
  1267. {
  1268. }
  1269. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1270. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  1271. auto& resume_target() const { return m_resume_target; }
  1272. private:
  1273. Label m_resume_target;
  1274. };
  1275. class Yield final : public Instruction {
  1276. public:
  1277. constexpr static bool IsTerminator = true;
  1278. explicit Yield(Label continuation_label, Operand value)
  1279. : Instruction(Type::Yield, sizeof(*this))
  1280. , m_continuation_label(continuation_label)
  1281. , m_value(value)
  1282. {
  1283. }
  1284. explicit Yield(nullptr_t, Operand value)
  1285. : Instruction(Type::Yield, sizeof(*this))
  1286. , m_value(value)
  1287. {
  1288. }
  1289. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1290. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  1291. auto& continuation() const { return m_continuation_label; }
  1292. Operand value() const { return m_value; }
  1293. private:
  1294. Optional<Label> m_continuation_label;
  1295. Operand m_value;
  1296. };
  1297. class Await final : public Instruction {
  1298. public:
  1299. constexpr static bool IsTerminator = true;
  1300. explicit Await(Label continuation_label, Operand argument)
  1301. : Instruction(Type::Await, sizeof(*this))
  1302. , m_continuation_label(continuation_label)
  1303. , m_argument(argument)
  1304. {
  1305. }
  1306. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1307. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  1308. auto& continuation() const { return m_continuation_label; }
  1309. Operand argument() const { return m_argument; }
  1310. private:
  1311. Label m_continuation_label;
  1312. Operand m_argument;
  1313. };
  1314. class GetIterator final : public Instruction {
  1315. public:
  1316. GetIterator(Operand dst, Operand iterable, IteratorHint hint = IteratorHint::Sync)
  1317. : Instruction(Type::GetIterator, sizeof(*this))
  1318. , m_dst(dst)
  1319. , m_iterable(iterable)
  1320. , m_hint(hint)
  1321. {
  1322. }
  1323. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1324. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  1325. Operand dst() const { return m_dst; }
  1326. Operand iterable() const { return m_iterable; }
  1327. IteratorHint hint() const { return m_hint; }
  1328. private:
  1329. Operand m_dst;
  1330. Operand m_iterable;
  1331. IteratorHint m_hint { IteratorHint::Sync };
  1332. };
  1333. class GetObjectFromIteratorRecord final : public Instruction {
  1334. public:
  1335. GetObjectFromIteratorRecord(Operand object, Operand iterator_record)
  1336. : Instruction(Type::GetObjectFromIteratorRecord, sizeof(*this))
  1337. , m_object(object)
  1338. , m_iterator_record(iterator_record)
  1339. {
  1340. }
  1341. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1342. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  1343. Operand object() const { return m_object; }
  1344. Operand iterator_record() const { return m_iterator_record; }
  1345. private:
  1346. Operand m_object;
  1347. Operand m_iterator_record;
  1348. };
  1349. class GetNextMethodFromIteratorRecord final : public Instruction {
  1350. public:
  1351. GetNextMethodFromIteratorRecord(Operand next_method, Operand iterator_record)
  1352. : Instruction(Type::GetNextMethodFromIteratorRecord, sizeof(*this))
  1353. , m_next_method(next_method)
  1354. , m_iterator_record(iterator_record)
  1355. {
  1356. }
  1357. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1358. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  1359. Operand next_method() const { return m_next_method; }
  1360. Operand iterator_record() const { return m_iterator_record; }
  1361. private:
  1362. Operand m_next_method;
  1363. Operand m_iterator_record;
  1364. };
  1365. class GetMethod final : public Instruction {
  1366. public:
  1367. GetMethod(Operand dst, Operand object, IdentifierTableIndex property)
  1368. : Instruction(Type::GetMethod, sizeof(*this))
  1369. , m_dst(dst)
  1370. , m_object(object)
  1371. , m_property(property)
  1372. {
  1373. }
  1374. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1375. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  1376. Operand dst() const { return m_dst; }
  1377. Operand object() const { return m_object; }
  1378. IdentifierTableIndex property() const { return m_property; }
  1379. private:
  1380. Operand m_dst;
  1381. Operand m_object;
  1382. IdentifierTableIndex m_property;
  1383. };
  1384. class GetObjectPropertyIterator final : public Instruction {
  1385. public:
  1386. GetObjectPropertyIterator(Operand dst, Operand object)
  1387. : Instruction(Type::GetObjectPropertyIterator, sizeof(*this))
  1388. , m_dst(dst)
  1389. , m_object(object)
  1390. {
  1391. }
  1392. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1393. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  1394. Operand dst() const { return m_dst; }
  1395. Operand object() const { return m_object; }
  1396. private:
  1397. Operand m_dst;
  1398. Operand m_object;
  1399. };
  1400. class IteratorClose final : public Instruction {
  1401. public:
  1402. IteratorClose(Operand iterator_record, Completion::Type completion_type, Optional<Value> completion_value)
  1403. : Instruction(Type::IteratorClose, sizeof(*this))
  1404. , m_iterator_record(iterator_record)
  1405. , m_completion_type(completion_type)
  1406. , m_completion_value(completion_value)
  1407. {
  1408. }
  1409. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1410. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  1411. Operand iterator_record() const { return m_iterator_record; }
  1412. Completion::Type completion_type() const { return m_completion_type; }
  1413. Optional<Value> const& completion_value() const { return m_completion_value; }
  1414. private:
  1415. Operand m_iterator_record;
  1416. Completion::Type m_completion_type { Completion::Type::Normal };
  1417. Optional<Value> m_completion_value;
  1418. };
  1419. class AsyncIteratorClose final : public Instruction {
  1420. public:
  1421. AsyncIteratorClose(Operand iterator_record, Completion::Type completion_type, Optional<Value> completion_value)
  1422. : Instruction(Type::AsyncIteratorClose, sizeof(*this))
  1423. , m_iterator_record(iterator_record)
  1424. , m_completion_type(completion_type)
  1425. , m_completion_value(completion_value)
  1426. {
  1427. }
  1428. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1429. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  1430. Operand iterator_record() const { return m_iterator_record; }
  1431. Completion::Type completion_type() const { return m_completion_type; }
  1432. Optional<Value> const& completion_value() const { return m_completion_value; }
  1433. private:
  1434. Operand m_iterator_record;
  1435. Completion::Type m_completion_type { Completion::Type::Normal };
  1436. Optional<Value> m_completion_value;
  1437. };
  1438. class IteratorNext final : public Instruction {
  1439. public:
  1440. IteratorNext(Operand dst, Operand iterator_record)
  1441. : Instruction(Type::IteratorNext, sizeof(*this))
  1442. , m_dst(dst)
  1443. , m_iterator_record(iterator_record)
  1444. {
  1445. }
  1446. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1447. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  1448. Operand dst() const { return m_dst; }
  1449. Operand iterator_record() const { return m_iterator_record; }
  1450. private:
  1451. Operand m_dst;
  1452. Operand m_iterator_record;
  1453. };
  1454. class ResolveThisBinding final : public Instruction {
  1455. public:
  1456. explicit ResolveThisBinding(Operand dst)
  1457. : Instruction(Type::ResolveThisBinding, sizeof(*this))
  1458. , m_dst(dst)
  1459. {
  1460. }
  1461. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1462. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  1463. Operand dst() const { return m_dst; }
  1464. private:
  1465. Operand m_dst;
  1466. };
  1467. class ResolveSuperBase final : public Instruction {
  1468. public:
  1469. explicit ResolveSuperBase(Operand dst)
  1470. : Instruction(Type::ResolveSuperBase, sizeof(*this))
  1471. , m_dst(dst)
  1472. {
  1473. }
  1474. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1475. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  1476. Operand dst() const { return m_dst; }
  1477. private:
  1478. Operand m_dst;
  1479. };
  1480. class GetNewTarget final : public Instruction {
  1481. public:
  1482. explicit GetNewTarget(Operand dst)
  1483. : Instruction(Type::GetNewTarget, sizeof(*this))
  1484. , m_dst(dst)
  1485. {
  1486. }
  1487. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1488. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  1489. Operand dst() const { return m_dst; }
  1490. private:
  1491. Operand m_dst;
  1492. };
  1493. class GetImportMeta final : public Instruction {
  1494. public:
  1495. explicit GetImportMeta(Operand dst)
  1496. : Instruction(Type::GetImportMeta, sizeof(*this))
  1497. , m_dst(dst)
  1498. {
  1499. }
  1500. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1501. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  1502. Operand dst() const { return m_dst; }
  1503. private:
  1504. Operand m_dst;
  1505. };
  1506. class TypeofVariable final : public Instruction {
  1507. public:
  1508. TypeofVariable(Operand dst, IdentifierTableIndex identifier)
  1509. : Instruction(Type::TypeofVariable, sizeof(*this))
  1510. , m_dst(dst)
  1511. , m_identifier(identifier)
  1512. {
  1513. }
  1514. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1515. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  1516. Operand dst() const { return m_dst; }
  1517. IdentifierTableIndex identifier() const { return m_identifier; }
  1518. private:
  1519. Operand m_dst;
  1520. IdentifierTableIndex m_identifier;
  1521. };
  1522. class End final : public Instruction {
  1523. public:
  1524. constexpr static bool IsTerminator = true;
  1525. explicit End(Operand value)
  1526. : Instruction(Type::End, sizeof(*this))
  1527. , m_value(value)
  1528. {
  1529. }
  1530. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1531. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  1532. Operand value() const { return m_value; }
  1533. private:
  1534. Operand m_value;
  1535. };
  1536. class Dump final : public Instruction {
  1537. public:
  1538. explicit Dump(StringView text, Operand value)
  1539. : Instruction(Type::Dump, sizeof(*this))
  1540. , m_text(text)
  1541. , m_value(value)
  1542. {
  1543. }
  1544. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1545. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  1546. private:
  1547. StringView m_text;
  1548. Operand m_value;
  1549. };
  1550. }
  1551. namespace JS::Bytecode {
  1552. ALWAYS_INLINE ThrowCompletionOr<void> Instruction::execute(Bytecode::Interpreter& interpreter) const
  1553. {
  1554. #define __BYTECODE_OP(op) \
  1555. case Instruction::Type::op: \
  1556. return static_cast<Bytecode::Op::op const&>(*this).execute_impl(interpreter);
  1557. switch (type()) {
  1558. ENUMERATE_BYTECODE_OPS(__BYTECODE_OP)
  1559. default:
  1560. VERIFY_NOT_REACHED();
  1561. }
  1562. #undef __BYTECODE_OP
  1563. }
  1564. }