Op.h 78 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608
  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/ScopedOperand.h>
  20. #include <LibJS/Bytecode/StringTable.h>
  21. #include <LibJS/Heap/Cell.h>
  22. #include <LibJS/Runtime/Environment.h>
  23. #include <LibJS/Runtime/Iterator.h>
  24. #include <LibJS/Runtime/Value.h>
  25. #include <LibJS/Runtime/ValueTraits.h>
  26. namespace JS {
  27. class FunctionExpression;
  28. }
  29. namespace JS::Bytecode::Op {
  30. class CreateRestParams final : public Instruction {
  31. public:
  32. CreateRestParams(Operand dst, u32 rest_index)
  33. : Instruction(Type::CreateRestParams)
  34. , m_dst(dst)
  35. , m_rest_index(rest_index)
  36. {
  37. }
  38. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  39. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  40. void visit_operands_impl(Function<void(Operand&)> visitor)
  41. {
  42. visitor(m_dst);
  43. }
  44. private:
  45. Operand m_dst;
  46. u32 m_rest_index;
  47. };
  48. class CreateArguments final : public Instruction {
  49. public:
  50. enum class Kind {
  51. Mapped,
  52. Unmapped,
  53. };
  54. CreateArguments(Kind kind, bool is_immutable)
  55. : Instruction(Type::CreateArguments)
  56. , m_kind(kind)
  57. , m_is_immutable(is_immutable)
  58. {
  59. }
  60. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  61. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  62. private:
  63. Kind m_kind;
  64. bool m_is_immutable { false };
  65. };
  66. class Mov final : public Instruction {
  67. public:
  68. Mov(Operand dst, Operand src)
  69. : Instruction(Type::Mov)
  70. , m_dst(dst)
  71. , m_src(src)
  72. {
  73. }
  74. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  75. void visit_operands_impl(Function<void(Operand&)> visitor)
  76. {
  77. visitor(m_dst);
  78. visitor(m_src);
  79. }
  80. Operand dst() const { return m_dst; }
  81. Operand src() const { return m_src; }
  82. private:
  83. Operand m_dst;
  84. Operand m_src;
  85. };
  86. #define JS_ENUMERATE_COMMON_BINARY_OPS_WITH_FAST_PATH(O) \
  87. O(Add, add) \
  88. O(BitwiseAnd, bitwise_and) \
  89. O(BitwiseOr, bitwise_or) \
  90. O(BitwiseXor, bitwise_xor) \
  91. O(GreaterThan, greater_than) \
  92. O(GreaterThanEquals, greater_than_equals) \
  93. O(LeftShift, left_shift) \
  94. O(LessThan, less_than) \
  95. O(LessThanEquals, less_than_equals) \
  96. O(Mul, mul) \
  97. O(RightShift, right_shift) \
  98. O(Sub, sub) \
  99. O(UnsignedRightShift, unsigned_right_shift)
  100. #define JS_ENUMERATE_COMMON_BINARY_OPS_WITHOUT_FAST_PATH(O) \
  101. O(Div, div) \
  102. O(Exp, exp) \
  103. O(Mod, mod) \
  104. O(In, in) \
  105. O(InstanceOf, instance_of) \
  106. O(LooselyInequals, loosely_inequals) \
  107. O(LooselyEquals, loosely_equals) \
  108. O(StrictlyInequals, strict_inequals) \
  109. O(StrictlyEquals, strict_equals)
  110. #define JS_DECLARE_COMMON_BINARY_OP(OpTitleCase, op_snake_case) \
  111. class OpTitleCase final : public Instruction { \
  112. public: \
  113. explicit OpTitleCase(Operand dst, Operand lhs, Operand rhs) \
  114. : Instruction(Type::OpTitleCase) \
  115. , m_dst(dst) \
  116. , m_lhs(lhs) \
  117. , m_rhs(rhs) \
  118. { \
  119. } \
  120. \
  121. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const; \
  122. ByteString to_byte_string_impl(Bytecode::Executable const&) const; \
  123. void visit_operands_impl(Function<void(Operand&)> visitor) \
  124. { \
  125. visitor(m_dst); \
  126. visitor(m_lhs); \
  127. visitor(m_rhs); \
  128. } \
  129. \
  130. Operand dst() const { return m_dst; } \
  131. Operand lhs() const { return m_lhs; } \
  132. Operand rhs() const { return m_rhs; } \
  133. \
  134. private: \
  135. Operand m_dst; \
  136. Operand m_lhs; \
  137. Operand m_rhs; \
  138. };
  139. JS_ENUMERATE_COMMON_BINARY_OPS_WITHOUT_FAST_PATH(JS_DECLARE_COMMON_BINARY_OP)
  140. JS_ENUMERATE_COMMON_BINARY_OPS_WITH_FAST_PATH(JS_DECLARE_COMMON_BINARY_OP)
  141. #undef JS_DECLARE_COMMON_BINARY_OP
  142. #define JS_ENUMERATE_COMMON_UNARY_OPS(O) \
  143. O(BitwiseNot, bitwise_not) \
  144. O(Not, not_) \
  145. O(UnaryPlus, unary_plus) \
  146. O(UnaryMinus, unary_minus) \
  147. O(Typeof, typeof_)
  148. #define JS_DECLARE_COMMON_UNARY_OP(OpTitleCase, op_snake_case) \
  149. class OpTitleCase final : public Instruction { \
  150. public: \
  151. OpTitleCase(Operand dst, Operand src) \
  152. : Instruction(Type::OpTitleCase) \
  153. , m_dst(dst) \
  154. , m_src(src) \
  155. { \
  156. } \
  157. \
  158. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const; \
  159. ByteString to_byte_string_impl(Bytecode::Executable const&) const; \
  160. void visit_operands_impl(Function<void(Operand&)> visitor) \
  161. { \
  162. visitor(m_dst); \
  163. visitor(m_src); \
  164. } \
  165. \
  166. Operand dst() const { return m_dst; } \
  167. Operand src() const { return m_src; } \
  168. \
  169. private: \
  170. Operand m_dst; \
  171. Operand m_src; \
  172. };
  173. JS_ENUMERATE_COMMON_UNARY_OPS(JS_DECLARE_COMMON_UNARY_OP)
  174. #undef JS_DECLARE_COMMON_UNARY_OP
  175. class NewObject final : public Instruction {
  176. public:
  177. explicit NewObject(Operand dst)
  178. : Instruction(Type::NewObject)
  179. , m_dst(dst)
  180. {
  181. }
  182. void execute_impl(Bytecode::Interpreter&) const;
  183. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  184. void visit_operands_impl(Function<void(Operand&)> visitor)
  185. {
  186. visitor(m_dst);
  187. }
  188. Operand dst() const { return m_dst; }
  189. private:
  190. Operand m_dst;
  191. };
  192. class NewRegExp final : public Instruction {
  193. public:
  194. NewRegExp(Operand dst, StringTableIndex source_index, StringTableIndex flags_index, RegexTableIndex regex_index)
  195. : Instruction(Type::NewRegExp)
  196. , m_dst(dst)
  197. , m_source_index(source_index)
  198. , m_flags_index(flags_index)
  199. , m_regex_index(regex_index)
  200. {
  201. }
  202. void execute_impl(Bytecode::Interpreter&) const;
  203. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  204. void visit_operands_impl(Function<void(Operand&)> visitor)
  205. {
  206. visitor(m_dst);
  207. }
  208. Operand dst() const { return m_dst; }
  209. StringTableIndex source_index() const { return m_source_index; }
  210. StringTableIndex flags_index() const { return m_flags_index; }
  211. RegexTableIndex regex_index() const { return m_regex_index; }
  212. private:
  213. Operand m_dst;
  214. StringTableIndex m_source_index;
  215. StringTableIndex m_flags_index;
  216. RegexTableIndex m_regex_index;
  217. };
  218. #define JS_ENUMERATE_NEW_BUILTIN_ERROR_OPS(O) \
  219. O(TypeError)
  220. #define JS_DECLARE_NEW_BUILTIN_ERROR_OP(ErrorName) \
  221. class New##ErrorName final : public Instruction { \
  222. public: \
  223. New##ErrorName(Operand dst, StringTableIndex error_string) \
  224. : Instruction(Type::New##ErrorName) \
  225. , m_dst(dst) \
  226. , m_error_string(error_string) \
  227. { \
  228. } \
  229. \
  230. void execute_impl(Bytecode::Interpreter&) const; \
  231. ByteString to_byte_string_impl(Bytecode::Executable const&) const; \
  232. void visit_operands_impl(Function<void(Operand&)> visitor) \
  233. { \
  234. visitor(m_dst); \
  235. } \
  236. \
  237. Operand dst() const { return m_dst; } \
  238. StringTableIndex error_string() const { return m_error_string; } \
  239. \
  240. private: \
  241. Operand m_dst; \
  242. StringTableIndex m_error_string; \
  243. };
  244. JS_ENUMERATE_NEW_BUILTIN_ERROR_OPS(JS_DECLARE_NEW_BUILTIN_ERROR_OP)
  245. #undef JS_DECLARE_NEW_BUILTIN_ERROR_OP
  246. // NOTE: This instruction is variable-width depending on the number of excluded names
  247. class CopyObjectExcludingProperties final : public Instruction {
  248. public:
  249. static constexpr bool IsVariableLength = true;
  250. CopyObjectExcludingProperties(Operand dst, Operand from_object, Vector<ScopedOperand> const& excluded_names)
  251. : Instruction(Type::CopyObjectExcludingProperties)
  252. , m_dst(dst)
  253. , m_from_object(from_object)
  254. , m_excluded_names_count(excluded_names.size())
  255. {
  256. for (size_t i = 0; i < m_excluded_names_count; i++)
  257. m_excluded_names[i] = excluded_names[i];
  258. }
  259. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  260. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  261. void visit_operands_impl(Function<void(Operand&)> visitor)
  262. {
  263. visitor(m_dst);
  264. visitor(m_from_object);
  265. for (size_t i = 0; i < m_excluded_names_count; i++)
  266. visitor(m_excluded_names[i]);
  267. }
  268. size_t length_impl() const
  269. {
  270. return round_up_to_power_of_two(alignof(void*), sizeof(*this) + sizeof(Operand) * m_excluded_names_count);
  271. }
  272. Operand dst() const { return m_dst; }
  273. Operand from_object() const { return m_from_object; }
  274. size_t excluded_names_count() const { return m_excluded_names_count; }
  275. Operand const* excluded_names() const { return m_excluded_names; }
  276. private:
  277. Operand m_dst;
  278. Operand m_from_object;
  279. size_t m_excluded_names_count { 0 };
  280. Operand m_excluded_names[];
  281. };
  282. // NOTE: This instruction is variable-width depending on the number of elements!
  283. class NewArray final : public Instruction {
  284. public:
  285. static constexpr bool IsVariableLength = true;
  286. explicit NewArray(Operand dst)
  287. : Instruction(Type::NewArray)
  288. , m_dst(dst)
  289. , m_element_count(0)
  290. {
  291. }
  292. NewArray(Operand dst, ReadonlySpan<ScopedOperand> elements)
  293. : Instruction(Type::NewArray)
  294. , m_dst(dst)
  295. , m_element_count(elements.size())
  296. {
  297. for (size_t i = 0; i < m_element_count; ++i)
  298. m_elements[i] = elements[i];
  299. }
  300. void execute_impl(Bytecode::Interpreter&) const;
  301. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  302. void visit_operands_impl(Function<void(Operand&)> visitor)
  303. {
  304. visitor(m_dst);
  305. for (size_t i = 0; i < m_element_count; i++)
  306. visitor(m_elements[i]);
  307. }
  308. Operand dst() const { return m_dst; }
  309. size_t length_impl() const
  310. {
  311. return round_up_to_power_of_two(alignof(void*), sizeof(*this) + sizeof(Operand) * m_element_count);
  312. }
  313. size_t element_count() const { return m_element_count; }
  314. private:
  315. Operand m_dst;
  316. size_t m_element_count { 0 };
  317. Operand m_elements[];
  318. };
  319. class NewPrimitiveArray final : public Instruction {
  320. public:
  321. static constexpr bool IsVariableLength = true;
  322. NewPrimitiveArray(Operand dst, ReadonlySpan<Value> elements)
  323. : Instruction(Type::NewPrimitiveArray)
  324. , m_dst(dst)
  325. , m_element_count(elements.size())
  326. {
  327. for (size_t i = 0; i < m_element_count; ++i)
  328. m_elements[i] = elements[i];
  329. }
  330. size_t length_impl() const
  331. {
  332. return round_up_to_power_of_two(alignof(void*), sizeof(*this) + sizeof(Value) * m_element_count);
  333. }
  334. void execute_impl(Bytecode::Interpreter&) const;
  335. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  336. void visit_operands_impl(Function<void(Operand&)> visitor)
  337. {
  338. visitor(m_dst);
  339. }
  340. Operand dst() const { return m_dst; }
  341. ReadonlySpan<Value> elements() const { return { m_elements, m_element_count }; }
  342. private:
  343. Operand m_dst;
  344. size_t m_element_count { 0 };
  345. Value m_elements[];
  346. };
  347. class AddPrivateName final : public Instruction {
  348. public:
  349. explicit AddPrivateName(IdentifierTableIndex name)
  350. : Instruction(Type::AddPrivateName)
  351. , m_name(name)
  352. {
  353. }
  354. void execute_impl(Bytecode::Interpreter&) const;
  355. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  356. private:
  357. IdentifierTableIndex m_name;
  358. };
  359. class ArrayAppend final : public Instruction {
  360. public:
  361. ArrayAppend(Operand dst, Operand src, bool is_spread)
  362. : Instruction(Type::ArrayAppend)
  363. , m_dst(dst)
  364. , m_src(src)
  365. , m_is_spread(is_spread)
  366. {
  367. }
  368. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  369. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  370. void visit_operands_impl(Function<void(Operand&)> visitor)
  371. {
  372. visitor(m_dst);
  373. visitor(m_src);
  374. }
  375. Operand dst() const { return m_dst; }
  376. Operand src() const { return m_src; }
  377. bool is_spread() const { return m_is_spread; }
  378. private:
  379. Operand m_dst;
  380. Operand m_src;
  381. bool m_is_spread = false;
  382. };
  383. class ImportCall final : public Instruction {
  384. public:
  385. ImportCall(Operand dst, Operand specifier, Operand options)
  386. : Instruction(Type::ImportCall)
  387. , m_dst(dst)
  388. , m_specifier(specifier)
  389. , m_options(options)
  390. {
  391. }
  392. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  393. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  394. void visit_operands_impl(Function<void(Operand&)> visitor)
  395. {
  396. visitor(m_dst);
  397. visitor(m_specifier);
  398. visitor(m_options);
  399. }
  400. Operand dst() const { return m_dst; }
  401. Operand specifier() const { return m_specifier; }
  402. Operand options() const { return m_options; }
  403. private:
  404. Operand m_dst;
  405. Operand m_specifier;
  406. Operand m_options;
  407. };
  408. class IteratorToArray final : public Instruction {
  409. public:
  410. explicit IteratorToArray(Operand dst, Operand iterator)
  411. : Instruction(Type::IteratorToArray)
  412. , m_dst(dst)
  413. , m_iterator(iterator)
  414. {
  415. }
  416. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  417. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  418. Operand dst() const { return m_dst; }
  419. Operand iterator() const { return m_iterator; }
  420. void visit_operands_impl(Function<void(Operand&)> visitor)
  421. {
  422. visitor(m_dst);
  423. visitor(m_iterator);
  424. }
  425. private:
  426. Operand m_dst;
  427. Operand m_iterator;
  428. };
  429. class ConcatString final : public Instruction {
  430. public:
  431. explicit ConcatString(Operand dst, Operand src)
  432. : Instruction(Type::ConcatString)
  433. , m_dst(dst)
  434. , m_src(src)
  435. {
  436. }
  437. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  438. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  439. Operand dst() const { return m_dst; }
  440. Operand src() const { return m_src; }
  441. void visit_operands_impl(Function<void(Operand&)> visitor)
  442. {
  443. visitor(m_dst);
  444. visitor(m_src);
  445. }
  446. private:
  447. Operand m_dst;
  448. Operand m_src;
  449. };
  450. enum class EnvironmentMode {
  451. Lexical,
  452. Var,
  453. };
  454. enum class BindingInitializationMode {
  455. Initialize,
  456. Set,
  457. };
  458. class CreateLexicalEnvironment final : public Instruction {
  459. public:
  460. explicit CreateLexicalEnvironment(u32 capacity = 0)
  461. : Instruction(Type::CreateLexicalEnvironment)
  462. , m_capacity(capacity)
  463. {
  464. }
  465. void execute_impl(Bytecode::Interpreter&) const;
  466. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  467. private:
  468. u32 m_capacity { 0 };
  469. };
  470. class CreateVariableEnvironment final : public Instruction {
  471. public:
  472. explicit CreateVariableEnvironment(u32 capacity = 0)
  473. : Instruction(Type::CreateVariableEnvironment)
  474. , m_capacity(capacity)
  475. {
  476. }
  477. void execute_impl(Bytecode::Interpreter&) const;
  478. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  479. private:
  480. u32 m_capacity { 0 };
  481. };
  482. class CreatePrivateEnvironment final : public Instruction {
  483. public:
  484. explicit CreatePrivateEnvironment()
  485. : Instruction(Type::CreatePrivateEnvironment)
  486. {
  487. }
  488. void execute_impl(Bytecode::Interpreter&) const;
  489. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  490. };
  491. class EnterObjectEnvironment final : public Instruction {
  492. public:
  493. explicit EnterObjectEnvironment(Operand object)
  494. : Instruction(Type::EnterObjectEnvironment)
  495. , m_object(object)
  496. {
  497. }
  498. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  499. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  500. Operand object() const { return m_object; }
  501. void visit_operands_impl(Function<void(Operand&)> visitor)
  502. {
  503. visitor(m_object);
  504. }
  505. private:
  506. Operand m_object;
  507. };
  508. class Catch final : public Instruction {
  509. public:
  510. explicit Catch(Operand dst)
  511. : Instruction(Type::Catch)
  512. , m_dst(dst)
  513. {
  514. }
  515. void execute_impl(Bytecode::Interpreter&) const;
  516. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  517. Operand dst() const { return m_dst; }
  518. void visit_operands_impl(Function<void(Operand&)> visitor)
  519. {
  520. visitor(m_dst);
  521. }
  522. private:
  523. Operand m_dst;
  524. };
  525. class LeaveFinally final : public Instruction {
  526. public:
  527. explicit LeaveFinally()
  528. : Instruction(Type::LeaveFinally)
  529. {
  530. }
  531. void execute_impl(Bytecode::Interpreter&) const;
  532. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  533. };
  534. class RestoreScheduledJump final : public Instruction {
  535. public:
  536. explicit RestoreScheduledJump()
  537. : Instruction(Type::RestoreScheduledJump)
  538. {
  539. }
  540. void execute_impl(Bytecode::Interpreter&) const;
  541. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  542. };
  543. class CreateVariable final : public Instruction {
  544. public:
  545. explicit CreateVariable(IdentifierTableIndex identifier, EnvironmentMode mode, bool is_immutable, bool is_global = false, bool is_strict = false)
  546. : Instruction(Type::CreateVariable)
  547. , m_identifier(identifier)
  548. , m_mode(mode)
  549. , m_is_immutable(is_immutable)
  550. , m_is_global(is_global)
  551. , m_is_strict(is_strict)
  552. {
  553. }
  554. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  555. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  556. IdentifierTableIndex identifier() const { return m_identifier; }
  557. EnvironmentMode mode() const { return m_mode; }
  558. bool is_immutable() const { return m_is_immutable; }
  559. bool is_global() const { return m_is_global; }
  560. bool is_strict() const { return m_is_strict; }
  561. private:
  562. IdentifierTableIndex m_identifier;
  563. EnvironmentMode m_mode;
  564. bool m_is_immutable : 4 { false };
  565. bool m_is_global : 4 { false };
  566. bool m_is_strict { false };
  567. };
  568. class InitializeLexicalBinding final : public Instruction {
  569. public:
  570. explicit InitializeLexicalBinding(IdentifierTableIndex identifier, Operand src)
  571. : Instruction(Type::InitializeLexicalBinding)
  572. , m_identifier(identifier)
  573. , m_src(src)
  574. {
  575. }
  576. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  577. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  578. void visit_operands_impl(Function<void(Operand&)> visitor)
  579. {
  580. visitor(m_src);
  581. }
  582. IdentifierTableIndex identifier() const { return m_identifier; }
  583. Operand src() const { return m_src; }
  584. private:
  585. IdentifierTableIndex m_identifier;
  586. Operand m_src;
  587. mutable EnvironmentCoordinate m_cache;
  588. };
  589. class InitializeVariableBinding final : public Instruction {
  590. public:
  591. explicit InitializeVariableBinding(IdentifierTableIndex identifier, Operand src)
  592. : Instruction(Type::InitializeVariableBinding)
  593. , m_identifier(identifier)
  594. , m_src(src)
  595. {
  596. }
  597. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  598. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  599. void visit_operands_impl(Function<void(Operand&)> visitor)
  600. {
  601. visitor(m_src);
  602. }
  603. IdentifierTableIndex identifier() const { return m_identifier; }
  604. Operand src() const { return m_src; }
  605. private:
  606. IdentifierTableIndex m_identifier;
  607. Operand m_src;
  608. mutable EnvironmentCoordinate m_cache;
  609. };
  610. class SetLexicalBinding final : public Instruction {
  611. public:
  612. explicit SetLexicalBinding(IdentifierTableIndex identifier, Operand src)
  613. : Instruction(Type::SetLexicalBinding)
  614. , m_identifier(identifier)
  615. , m_src(src)
  616. {
  617. }
  618. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  619. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  620. void visit_operands_impl(Function<void(Operand&)> visitor)
  621. {
  622. visitor(m_src);
  623. }
  624. IdentifierTableIndex identifier() const { return m_identifier; }
  625. Operand src() const { return m_src; }
  626. private:
  627. IdentifierTableIndex m_identifier;
  628. Operand m_src;
  629. mutable EnvironmentCoordinate m_cache;
  630. };
  631. class SetVariableBinding final : public Instruction {
  632. public:
  633. explicit SetVariableBinding(IdentifierTableIndex identifier, Operand src)
  634. : Instruction(Type::SetVariableBinding)
  635. , m_identifier(identifier)
  636. , m_src(src)
  637. {
  638. }
  639. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  640. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  641. void visit_operands_impl(Function<void(Operand&)> visitor)
  642. {
  643. visitor(m_src);
  644. }
  645. IdentifierTableIndex identifier() const { return m_identifier; }
  646. Operand src() const { return m_src; }
  647. private:
  648. IdentifierTableIndex m_identifier;
  649. Operand m_src;
  650. mutable EnvironmentCoordinate m_cache;
  651. };
  652. class SetArgument final : public Instruction {
  653. public:
  654. SetArgument(size_t index, Operand src)
  655. : Instruction(Type::SetArgument)
  656. , m_index(index)
  657. , m_src(src)
  658. {
  659. }
  660. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  661. void visit_operands_impl(Function<void(Operand&)> visitor)
  662. {
  663. visitor(m_src);
  664. }
  665. size_t index() const { return m_index; }
  666. Operand src() const { return m_src; }
  667. private:
  668. u32 m_index;
  669. Operand m_src;
  670. };
  671. class GetArgument final : public Instruction {
  672. public:
  673. GetArgument(Operand dst, size_t index)
  674. : Instruction(Type::GetArgument)
  675. , m_index(index)
  676. , m_dst(dst)
  677. {
  678. }
  679. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  680. void visit_operands_impl(Function<void(Operand&)> visitor)
  681. {
  682. visitor(m_dst);
  683. }
  684. u32 index() const { return m_index; }
  685. Operand dst() const { return m_dst; }
  686. private:
  687. u32 m_index;
  688. Operand m_dst;
  689. };
  690. class GetCalleeAndThisFromEnvironment final : public Instruction {
  691. public:
  692. explicit GetCalleeAndThisFromEnvironment(Operand callee, Operand this_value, IdentifierTableIndex identifier)
  693. : Instruction(Type::GetCalleeAndThisFromEnvironment)
  694. , m_identifier(identifier)
  695. , m_callee(callee)
  696. , m_this_value(this_value)
  697. {
  698. }
  699. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  700. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  701. void visit_operands_impl(Function<void(Operand&)> visitor)
  702. {
  703. visitor(m_callee);
  704. visitor(m_this_value);
  705. }
  706. IdentifierTableIndex identifier() const { return m_identifier; }
  707. Operand callee() const { return m_callee; }
  708. Operand this_() const { return m_this_value; }
  709. private:
  710. IdentifierTableIndex m_identifier;
  711. Operand m_callee;
  712. Operand m_this_value;
  713. mutable EnvironmentCoordinate m_cache;
  714. };
  715. class GetBinding final : public Instruction {
  716. public:
  717. explicit GetBinding(Operand dst, IdentifierTableIndex identifier)
  718. : Instruction(Type::GetBinding)
  719. , m_dst(dst)
  720. , m_identifier(identifier)
  721. {
  722. }
  723. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  724. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  725. Operand dst() const { return m_dst; }
  726. IdentifierTableIndex identifier() const { return m_identifier; }
  727. void visit_operands_impl(Function<void(Operand&)> visitor)
  728. {
  729. visitor(m_dst);
  730. }
  731. private:
  732. Operand m_dst;
  733. IdentifierTableIndex m_identifier;
  734. mutable EnvironmentCoordinate m_cache;
  735. };
  736. class GetGlobal final : public Instruction {
  737. public:
  738. GetGlobal(Operand dst, IdentifierTableIndex identifier, u32 cache_index)
  739. : Instruction(Type::GetGlobal)
  740. , m_dst(dst)
  741. , m_identifier(identifier)
  742. , m_cache_index(cache_index)
  743. {
  744. }
  745. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  746. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  747. Operand dst() const { return m_dst; }
  748. IdentifierTableIndex identifier() const { return m_identifier; }
  749. u32 cache_index() const { return m_cache_index; }
  750. void visit_operands_impl(Function<void(Operand&)> visitor)
  751. {
  752. visitor(m_dst);
  753. }
  754. private:
  755. Operand m_dst;
  756. IdentifierTableIndex m_identifier;
  757. u32 m_cache_index { 0 };
  758. };
  759. class DeleteVariable final : public Instruction {
  760. public:
  761. explicit DeleteVariable(Operand dst, IdentifierTableIndex identifier)
  762. : Instruction(Type::DeleteVariable)
  763. , m_dst(dst)
  764. , m_identifier(identifier)
  765. {
  766. }
  767. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  768. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  769. Operand dst() const { return m_dst; }
  770. IdentifierTableIndex identifier() const { return m_identifier; }
  771. void visit_operands_impl(Function<void(Operand&)> visitor)
  772. {
  773. visitor(m_dst);
  774. }
  775. private:
  776. Operand m_dst;
  777. IdentifierTableIndex m_identifier;
  778. };
  779. class GetById final : public Instruction {
  780. public:
  781. GetById(Operand dst, Operand base, IdentifierTableIndex property, Optional<IdentifierTableIndex> base_identifier, u32 cache_index)
  782. : Instruction(Type::GetById)
  783. , m_dst(dst)
  784. , m_base(base)
  785. , m_property(property)
  786. , m_base_identifier(move(base_identifier))
  787. , m_cache_index(cache_index)
  788. {
  789. }
  790. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  791. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  792. void visit_operands_impl(Function<void(Operand&)> visitor)
  793. {
  794. visitor(m_dst);
  795. visitor(m_base);
  796. }
  797. Operand dst() const { return m_dst; }
  798. Operand base() const { return m_base; }
  799. IdentifierTableIndex property() const { return m_property; }
  800. u32 cache_index() const { return m_cache_index; }
  801. private:
  802. Operand m_dst;
  803. Operand m_base;
  804. IdentifierTableIndex m_property;
  805. Optional<IdentifierTableIndex> m_base_identifier;
  806. u32 m_cache_index { 0 };
  807. };
  808. class GetByIdWithThis final : public Instruction {
  809. public:
  810. GetByIdWithThis(Operand dst, Operand base, IdentifierTableIndex property, Operand this_value, u32 cache_index)
  811. : Instruction(Type::GetByIdWithThis)
  812. , m_dst(dst)
  813. , m_base(base)
  814. , m_property(property)
  815. , m_this_value(this_value)
  816. , m_cache_index(cache_index)
  817. {
  818. }
  819. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  820. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  821. void visit_operands_impl(Function<void(Operand&)> visitor)
  822. {
  823. visitor(m_dst);
  824. visitor(m_base);
  825. visitor(m_this_value);
  826. }
  827. Operand dst() const { return m_dst; }
  828. Operand base() const { return m_base; }
  829. IdentifierTableIndex property() const { return m_property; }
  830. Operand this_value() const { return m_this_value; }
  831. u32 cache_index() const { return m_cache_index; }
  832. private:
  833. Operand m_dst;
  834. Operand m_base;
  835. IdentifierTableIndex m_property;
  836. Operand m_this_value;
  837. u32 m_cache_index { 0 };
  838. };
  839. class GetPrivateById final : public Instruction {
  840. public:
  841. explicit GetPrivateById(Operand dst, Operand base, IdentifierTableIndex property)
  842. : Instruction(Type::GetPrivateById)
  843. , m_dst(dst)
  844. , m_base(base)
  845. , m_property(property)
  846. {
  847. }
  848. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  849. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  850. void visit_operands_impl(Function<void(Operand&)> visitor)
  851. {
  852. visitor(m_dst);
  853. visitor(m_base);
  854. }
  855. Operand dst() const { return m_dst; }
  856. Operand base() const { return m_base; }
  857. IdentifierTableIndex property() const { return m_property; }
  858. private:
  859. Operand m_dst;
  860. Operand m_base;
  861. IdentifierTableIndex m_property;
  862. };
  863. class HasPrivateId final : public Instruction {
  864. public:
  865. HasPrivateId(Operand dst, Operand base, IdentifierTableIndex property)
  866. : Instruction(Type::HasPrivateId)
  867. , m_dst(dst)
  868. , m_base(base)
  869. , m_property(property)
  870. {
  871. }
  872. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  873. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  874. void visit_operands_impl(Function<void(Operand&)> visitor)
  875. {
  876. visitor(m_dst);
  877. visitor(m_base);
  878. }
  879. Operand dst() const { return m_dst; }
  880. Operand base() const { return m_base; }
  881. IdentifierTableIndex property() const { return m_property; }
  882. private:
  883. Operand m_dst;
  884. Operand m_base;
  885. IdentifierTableIndex m_property;
  886. };
  887. enum class PropertyKind {
  888. Getter,
  889. Setter,
  890. KeyValue,
  891. DirectKeyValue, // Used for Object expressions. Always sets an own property, never calls a setter.
  892. Spread,
  893. ProtoSetter,
  894. };
  895. class PutById final : public Instruction {
  896. public:
  897. explicit PutById(Operand base, IdentifierTableIndex property, Operand src, PropertyKind kind, u32 cache_index, Optional<IdentifierTableIndex> base_identifier = {})
  898. : Instruction(Type::PutById)
  899. , m_base(base)
  900. , m_property(property)
  901. , m_src(src)
  902. , m_kind(kind)
  903. , m_cache_index(cache_index)
  904. , m_base_identifier(move(base_identifier))
  905. {
  906. }
  907. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  908. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  909. void visit_operands_impl(Function<void(Operand&)> visitor)
  910. {
  911. visitor(m_base);
  912. visitor(m_src);
  913. }
  914. Operand base() const { return m_base; }
  915. IdentifierTableIndex property() const { return m_property; }
  916. Operand src() const { return m_src; }
  917. PropertyKind kind() const { return m_kind; }
  918. u32 cache_index() const { return m_cache_index; }
  919. private:
  920. Operand m_base;
  921. IdentifierTableIndex m_property;
  922. Operand m_src;
  923. PropertyKind m_kind;
  924. u32 m_cache_index { 0 };
  925. Optional<IdentifierTableIndex> m_base_identifier {};
  926. };
  927. class PutByIdWithThis final : public Instruction {
  928. public:
  929. PutByIdWithThis(Operand base, Operand this_value, IdentifierTableIndex property, Operand src, PropertyKind kind, u32 cache_index)
  930. : Instruction(Type::PutByIdWithThis)
  931. , m_base(base)
  932. , m_this_value(this_value)
  933. , m_property(property)
  934. , m_src(src)
  935. , m_kind(kind)
  936. , m_cache_index(cache_index)
  937. {
  938. }
  939. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  940. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  941. void visit_operands_impl(Function<void(Operand&)> visitor)
  942. {
  943. visitor(m_base);
  944. visitor(m_this_value);
  945. visitor(m_src);
  946. }
  947. Operand base() const { return m_base; }
  948. Operand this_value() const { return m_this_value; }
  949. IdentifierTableIndex property() const { return m_property; }
  950. Operand src() const { return m_src; }
  951. PropertyKind kind() const { return m_kind; }
  952. u32 cache_index() const { return m_cache_index; }
  953. private:
  954. Operand m_base;
  955. Operand m_this_value;
  956. IdentifierTableIndex m_property;
  957. Operand m_src;
  958. PropertyKind m_kind;
  959. u32 m_cache_index { 0 };
  960. };
  961. class PutPrivateById final : public Instruction {
  962. public:
  963. explicit PutPrivateById(Operand base, IdentifierTableIndex property, Operand src, PropertyKind kind = PropertyKind::KeyValue)
  964. : Instruction(Type::PutPrivateById)
  965. , m_base(base)
  966. , m_property(property)
  967. , m_src(src)
  968. , m_kind(kind)
  969. {
  970. }
  971. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  972. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  973. void visit_operands_impl(Function<void(Operand&)> visitor)
  974. {
  975. visitor(m_base);
  976. visitor(m_src);
  977. }
  978. Operand base() const { return m_base; }
  979. IdentifierTableIndex property() const { return m_property; }
  980. Operand src() const { return m_src; }
  981. private:
  982. Operand m_base;
  983. IdentifierTableIndex m_property;
  984. Operand m_src;
  985. PropertyKind m_kind;
  986. };
  987. class DeleteById final : public Instruction {
  988. public:
  989. explicit DeleteById(Operand dst, Operand base, IdentifierTableIndex property)
  990. : Instruction(Type::DeleteById)
  991. , m_dst(dst)
  992. , m_base(base)
  993. , m_property(property)
  994. {
  995. }
  996. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  997. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  998. void visit_operands_impl(Function<void(Operand&)> visitor)
  999. {
  1000. visitor(m_dst);
  1001. visitor(m_base);
  1002. }
  1003. Operand dst() const { return m_dst; }
  1004. Operand base() const { return m_base; }
  1005. IdentifierTableIndex property() const { return m_property; }
  1006. private:
  1007. Operand m_dst;
  1008. Operand m_base;
  1009. IdentifierTableIndex m_property;
  1010. };
  1011. class DeleteByIdWithThis final : public Instruction {
  1012. public:
  1013. DeleteByIdWithThis(Operand dst, Operand base, Operand this_value, IdentifierTableIndex property)
  1014. : Instruction(Type::DeleteByIdWithThis)
  1015. , m_dst(dst)
  1016. , m_base(base)
  1017. , m_this_value(this_value)
  1018. , m_property(property)
  1019. {
  1020. }
  1021. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1022. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  1023. void visit_operands_impl(Function<void(Operand&)> visitor)
  1024. {
  1025. visitor(m_dst);
  1026. visitor(m_base);
  1027. visitor(m_this_value);
  1028. }
  1029. Operand dst() const { return m_dst; }
  1030. Operand base() const { return m_base; }
  1031. Operand this_value() const { return m_this_value; }
  1032. IdentifierTableIndex property() const { return m_property; }
  1033. private:
  1034. Operand m_dst;
  1035. Operand m_base;
  1036. Operand m_this_value;
  1037. IdentifierTableIndex m_property;
  1038. };
  1039. class GetByValue final : public Instruction {
  1040. public:
  1041. GetByValue(Operand dst, Operand base, Operand property, Optional<IdentifierTableIndex> base_identifier = {})
  1042. : Instruction(Type::GetByValue)
  1043. , m_dst(dst)
  1044. , m_base(base)
  1045. , m_property(property)
  1046. , m_base_identifier(move(base_identifier))
  1047. {
  1048. }
  1049. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1050. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  1051. void visit_operands_impl(Function<void(Operand&)> visitor)
  1052. {
  1053. visitor(m_dst);
  1054. visitor(m_base);
  1055. visitor(m_property);
  1056. }
  1057. Operand dst() const { return m_dst; }
  1058. Operand base() const { return m_base; }
  1059. Operand property() const { return m_property; }
  1060. Optional<DeprecatedFlyString const&> base_identifier(Bytecode::Interpreter const&) const;
  1061. private:
  1062. Operand m_dst;
  1063. Operand m_base;
  1064. Operand m_property;
  1065. Optional<IdentifierTableIndex> m_base_identifier;
  1066. };
  1067. class GetByValueWithThis final : public Instruction {
  1068. public:
  1069. GetByValueWithThis(Operand dst, Operand base, Operand property, Operand this_value)
  1070. : Instruction(Type::GetByValueWithThis)
  1071. , m_dst(dst)
  1072. , m_base(base)
  1073. , m_property(property)
  1074. , m_this_value(this_value)
  1075. {
  1076. }
  1077. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1078. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  1079. void visit_operands_impl(Function<void(Operand&)> visitor)
  1080. {
  1081. visitor(m_dst);
  1082. visitor(m_base);
  1083. visitor(m_property);
  1084. visitor(m_this_value);
  1085. }
  1086. Operand dst() const { return m_dst; }
  1087. Operand base() const { return m_base; }
  1088. Operand property() const { return m_property; }
  1089. Operand this_value() const { return m_this_value; }
  1090. private:
  1091. Operand m_dst;
  1092. Operand m_base;
  1093. Operand m_property;
  1094. Operand m_this_value;
  1095. };
  1096. class PutByValue final : public Instruction {
  1097. public:
  1098. PutByValue(Operand base, Operand property, Operand src, PropertyKind kind = PropertyKind::KeyValue, Optional<IdentifierTableIndex> base_identifier = {})
  1099. : Instruction(Type::PutByValue)
  1100. , m_base(base)
  1101. , m_property(property)
  1102. , m_src(src)
  1103. , m_kind(kind)
  1104. , m_base_identifier(move(base_identifier))
  1105. {
  1106. }
  1107. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1108. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  1109. void visit_operands_impl(Function<void(Operand&)> visitor)
  1110. {
  1111. visitor(m_base);
  1112. visitor(m_property);
  1113. visitor(m_src);
  1114. }
  1115. Operand base() const { return m_base; }
  1116. Operand property() const { return m_property; }
  1117. Operand src() const { return m_src; }
  1118. PropertyKind kind() const { return m_kind; }
  1119. private:
  1120. Operand m_base;
  1121. Operand m_property;
  1122. Operand m_src;
  1123. PropertyKind m_kind;
  1124. Optional<IdentifierTableIndex> m_base_identifier;
  1125. };
  1126. class PutByValueWithThis final : public Instruction {
  1127. public:
  1128. PutByValueWithThis(Operand base, Operand property, Operand this_value, Operand src, PropertyKind kind = PropertyKind::KeyValue)
  1129. : Instruction(Type::PutByValueWithThis)
  1130. , m_base(base)
  1131. , m_property(property)
  1132. , m_this_value(this_value)
  1133. , m_src(src)
  1134. , m_kind(kind)
  1135. {
  1136. }
  1137. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1138. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  1139. void visit_operands_impl(Function<void(Operand&)> visitor)
  1140. {
  1141. visitor(m_base);
  1142. visitor(m_property);
  1143. visitor(m_this_value);
  1144. visitor(m_src);
  1145. }
  1146. Operand base() const { return m_base; }
  1147. Operand property() const { return m_property; }
  1148. Operand this_value() const { return m_this_value; }
  1149. Operand src() const { return m_src; }
  1150. PropertyKind kind() const { return m_kind; }
  1151. private:
  1152. Operand m_base;
  1153. Operand m_property;
  1154. Operand m_this_value;
  1155. Operand m_src;
  1156. PropertyKind m_kind;
  1157. };
  1158. class DeleteByValue final : public Instruction {
  1159. public:
  1160. DeleteByValue(Operand dst, Operand base, Operand property)
  1161. : Instruction(Type::DeleteByValue)
  1162. , m_dst(dst)
  1163. , m_base(base)
  1164. , m_property(property)
  1165. {
  1166. }
  1167. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1168. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  1169. void visit_operands_impl(Function<void(Operand&)> visitor)
  1170. {
  1171. visitor(m_dst);
  1172. visitor(m_base);
  1173. visitor(m_property);
  1174. }
  1175. Operand dst() const { return m_dst; }
  1176. Operand base() const { return m_base; }
  1177. Operand property() const { return m_property; }
  1178. private:
  1179. Operand m_dst;
  1180. Operand m_base;
  1181. Operand m_property;
  1182. };
  1183. class DeleteByValueWithThis final : public Instruction {
  1184. public:
  1185. DeleteByValueWithThis(Operand dst, Operand base, Operand this_value, Operand property)
  1186. : Instruction(Type::DeleteByValueWithThis)
  1187. , m_dst(dst)
  1188. , m_base(base)
  1189. , m_this_value(this_value)
  1190. , m_property(property)
  1191. {
  1192. }
  1193. Operand dst() const { return m_dst; }
  1194. Operand base() const { return m_base; }
  1195. Operand this_value() const { return m_this_value; }
  1196. Operand property() const { return m_property; }
  1197. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1198. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  1199. void visit_operands_impl(Function<void(Operand&)> visitor)
  1200. {
  1201. visitor(m_dst);
  1202. visitor(m_base);
  1203. visitor(m_this_value);
  1204. visitor(m_property);
  1205. }
  1206. private:
  1207. Operand m_dst;
  1208. Operand m_base;
  1209. Operand m_this_value;
  1210. Operand m_property;
  1211. };
  1212. class Jump final : public Instruction {
  1213. public:
  1214. constexpr static bool IsTerminator = true;
  1215. explicit Jump(Label target)
  1216. : Instruction(Type::Jump)
  1217. , m_target(target)
  1218. {
  1219. }
  1220. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  1221. void visit_labels_impl(Function<void(Label&)> visitor)
  1222. {
  1223. visitor(m_target);
  1224. }
  1225. auto& target() const { return m_target; }
  1226. protected:
  1227. Label m_target;
  1228. };
  1229. class JumpIf final : public Instruction {
  1230. public:
  1231. constexpr static bool IsTerminator = true;
  1232. explicit JumpIf(Operand condition, Label true_target, Label false_target)
  1233. : Instruction(Type::JumpIf)
  1234. , m_condition(condition)
  1235. , m_true_target(true_target)
  1236. , m_false_target(false_target)
  1237. {
  1238. }
  1239. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  1240. void visit_labels_impl(Function<void(Label&)> visitor)
  1241. {
  1242. visitor(m_true_target);
  1243. visitor(m_false_target);
  1244. }
  1245. void visit_operands_impl(Function<void(Operand&)> visitor)
  1246. {
  1247. visitor(m_condition);
  1248. }
  1249. Operand condition() const { return m_condition; }
  1250. auto& true_target() const { return m_true_target; }
  1251. auto& false_target() const { return m_false_target; }
  1252. private:
  1253. Operand m_condition;
  1254. Label m_true_target;
  1255. Label m_false_target;
  1256. };
  1257. class JumpTrue final : public Instruction {
  1258. public:
  1259. constexpr static bool IsTerminator = true;
  1260. explicit JumpTrue(Operand condition, Label target)
  1261. : Instruction(Type::JumpTrue)
  1262. , m_condition(condition)
  1263. , m_target(target)
  1264. {
  1265. }
  1266. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  1267. void visit_labels_impl(Function<void(Label&)> visitor)
  1268. {
  1269. visitor(m_target);
  1270. }
  1271. void visit_operands_impl(Function<void(Operand&)> visitor)
  1272. {
  1273. visitor(m_condition);
  1274. }
  1275. Operand condition() const { return m_condition; }
  1276. auto& target() const { return m_target; }
  1277. private:
  1278. Operand m_condition;
  1279. Label m_target;
  1280. };
  1281. class JumpFalse final : public Instruction {
  1282. public:
  1283. constexpr static bool IsTerminator = true;
  1284. explicit JumpFalse(Operand condition, Label target)
  1285. : Instruction(Type::JumpFalse)
  1286. , m_condition(condition)
  1287. , m_target(target)
  1288. {
  1289. }
  1290. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  1291. void visit_labels_impl(Function<void(Label&)> visitor)
  1292. {
  1293. visitor(m_target);
  1294. }
  1295. void visit_operands_impl(Function<void(Operand&)> visitor)
  1296. {
  1297. visitor(m_condition);
  1298. }
  1299. Operand condition() const { return m_condition; }
  1300. auto& target() const { return m_target; }
  1301. private:
  1302. Operand m_condition;
  1303. Label m_target;
  1304. };
  1305. #define JS_ENUMERATE_COMPARISON_OPS(X) \
  1306. X(LessThan, less_than, <) \
  1307. X(LessThanEquals, less_than_equals, <=) \
  1308. X(GreaterThan, greater_than, >) \
  1309. X(GreaterThanEquals, greater_than_equals, >=) \
  1310. X(LooselyEquals, loosely_equals, ==) \
  1311. X(LooselyInequals, loosely_inequals, !=) \
  1312. X(StrictlyEquals, strict_equals, ==) \
  1313. X(StrictlyInequals, strict_inequals, !=)
  1314. #define DECLARE_COMPARISON_OP(op_TitleCase, op_snake_case, numeric_operator) \
  1315. class Jump##op_TitleCase final : public Instruction { \
  1316. public: \
  1317. constexpr static bool IsTerminator = true; \
  1318. \
  1319. explicit Jump##op_TitleCase(Operand lhs, Operand rhs, Label true_target, Label false_target) \
  1320. : Instruction(Type::Jump##op_TitleCase) \
  1321. , m_lhs(lhs) \
  1322. , m_rhs(rhs) \
  1323. , m_true_target(true_target) \
  1324. , m_false_target(false_target) \
  1325. { \
  1326. } \
  1327. \
  1328. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const; \
  1329. ByteString to_byte_string_impl(Bytecode::Executable const&) const; \
  1330. void visit_labels_impl(Function<void(Label&)> visitor) \
  1331. { \
  1332. visitor(m_true_target); \
  1333. visitor(m_false_target); \
  1334. } \
  1335. void visit_operands_impl(Function<void(Operand&)> visitor) \
  1336. { \
  1337. visitor(m_lhs); \
  1338. visitor(m_rhs); \
  1339. } \
  1340. \
  1341. Operand lhs() const { return m_lhs; } \
  1342. Operand rhs() const { return m_rhs; } \
  1343. auto& true_target() const { return m_true_target; } \
  1344. auto& false_target() const { return m_false_target; } \
  1345. \
  1346. private: \
  1347. Operand m_lhs; \
  1348. Operand m_rhs; \
  1349. Label m_true_target; \
  1350. Label m_false_target; \
  1351. };
  1352. JS_ENUMERATE_COMPARISON_OPS(DECLARE_COMPARISON_OP)
  1353. class JumpNullish final : public Instruction {
  1354. public:
  1355. constexpr static bool IsTerminator = true;
  1356. explicit JumpNullish(Operand condition, Label true_target, Label false_target)
  1357. : Instruction(Type::JumpNullish)
  1358. , m_condition(condition)
  1359. , m_true_target(true_target)
  1360. , m_false_target(false_target)
  1361. {
  1362. }
  1363. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  1364. void visit_labels_impl(Function<void(Label&)> visitor)
  1365. {
  1366. visitor(m_true_target);
  1367. visitor(m_false_target);
  1368. }
  1369. void visit_operands_impl(Function<void(Operand&)> visitor)
  1370. {
  1371. visitor(m_condition);
  1372. }
  1373. Operand condition() const { return m_condition; }
  1374. auto& true_target() const { return m_true_target; }
  1375. auto& false_target() const { return m_false_target; }
  1376. private:
  1377. Operand m_condition;
  1378. Label m_true_target;
  1379. Label m_false_target;
  1380. };
  1381. class JumpUndefined final : public Instruction {
  1382. public:
  1383. constexpr static bool IsTerminator = true;
  1384. explicit JumpUndefined(Operand condition, Label true_target, Label false_target)
  1385. : Instruction(Type::JumpUndefined)
  1386. , m_condition(condition)
  1387. , m_true_target(true_target)
  1388. , m_false_target(false_target)
  1389. {
  1390. }
  1391. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  1392. void visit_labels_impl(Function<void(Label&)> visitor)
  1393. {
  1394. visitor(m_true_target);
  1395. visitor(m_false_target);
  1396. }
  1397. void visit_operands_impl(Function<void(Operand&)> visitor)
  1398. {
  1399. visitor(m_condition);
  1400. }
  1401. Operand condition() const { return m_condition; }
  1402. auto& true_target() const { return m_true_target; }
  1403. auto& false_target() const { return m_false_target; }
  1404. private:
  1405. Operand m_condition;
  1406. Label m_true_target;
  1407. Label m_false_target;
  1408. };
  1409. enum class CallType : u8 {
  1410. Call,
  1411. Construct,
  1412. DirectEval,
  1413. };
  1414. class Call final : public Instruction {
  1415. public:
  1416. static constexpr bool IsVariableLength = true;
  1417. Call(CallType type, Operand dst, Operand callee, Operand this_value, ReadonlySpan<ScopedOperand> arguments, Optional<StringTableIndex> expression_string = {}, Optional<Builtin> builtin = {})
  1418. : Instruction(Type::Call)
  1419. , m_dst(dst)
  1420. , m_callee(callee)
  1421. , m_this_value(this_value)
  1422. , m_argument_count(arguments.size())
  1423. , m_type(type)
  1424. , m_builtin(builtin)
  1425. , m_expression_string(expression_string)
  1426. {
  1427. for (size_t i = 0; i < arguments.size(); ++i)
  1428. m_arguments[i] = arguments[i];
  1429. }
  1430. size_t length_impl() const
  1431. {
  1432. return round_up_to_power_of_two(alignof(void*), sizeof(*this) + sizeof(Operand) * m_argument_count);
  1433. }
  1434. CallType call_type() const { return m_type; }
  1435. Operand dst() const { return m_dst; }
  1436. Operand callee() const { return m_callee; }
  1437. Operand this_value() const { return m_this_value; }
  1438. Optional<StringTableIndex> const& expression_string() const { return m_expression_string; }
  1439. u32 argument_count() const { return m_argument_count; }
  1440. Optional<Builtin> const& builtin() const { return m_builtin; }
  1441. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1442. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  1443. void visit_operands_impl(Function<void(Operand&)> visitor)
  1444. {
  1445. visitor(m_dst);
  1446. visitor(m_callee);
  1447. visitor(m_this_value);
  1448. for (size_t i = 0; i < m_argument_count; i++)
  1449. visitor(m_arguments[i]);
  1450. }
  1451. private:
  1452. Operand m_dst;
  1453. Operand m_callee;
  1454. Operand m_this_value;
  1455. u32 m_argument_count { 0 };
  1456. CallType m_type;
  1457. Optional<Builtin> m_builtin;
  1458. Optional<StringTableIndex> m_expression_string;
  1459. Operand m_arguments[];
  1460. };
  1461. class CallWithArgumentArray final : public Instruction {
  1462. public:
  1463. CallWithArgumentArray(CallType type, Operand dst, Operand callee, Operand this_value, Operand arguments, Optional<StringTableIndex> expression_string = {})
  1464. : Instruction(Type::CallWithArgumentArray)
  1465. , m_dst(dst)
  1466. , m_callee(callee)
  1467. , m_this_value(this_value)
  1468. , m_arguments(arguments)
  1469. , m_type(type)
  1470. , m_expression_string(expression_string)
  1471. {
  1472. }
  1473. Operand dst() const { return m_dst; }
  1474. CallType call_type() const { return m_type; }
  1475. Operand callee() const { return m_callee; }
  1476. Operand this_value() const { return m_this_value; }
  1477. Operand arguments() const { return m_arguments; }
  1478. Optional<StringTableIndex> const& expression_string() const { return m_expression_string; }
  1479. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1480. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  1481. void visit_operands_impl(Function<void(Operand&)> visitor)
  1482. {
  1483. visitor(m_dst);
  1484. visitor(m_callee);
  1485. visitor(m_this_value);
  1486. visitor(m_arguments);
  1487. }
  1488. private:
  1489. Operand m_dst;
  1490. Operand m_callee;
  1491. Operand m_this_value;
  1492. Operand m_arguments;
  1493. CallType m_type;
  1494. Optional<StringTableIndex> m_expression_string;
  1495. };
  1496. class SuperCallWithArgumentArray : public Instruction {
  1497. public:
  1498. explicit SuperCallWithArgumentArray(Operand dst, Operand arguments, bool is_synthetic)
  1499. : Instruction(Type::SuperCallWithArgumentArray)
  1500. , m_dst(dst)
  1501. , m_arguments(arguments)
  1502. , m_is_synthetic(is_synthetic)
  1503. {
  1504. }
  1505. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1506. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  1507. void visit_operands_impl(Function<void(Operand&)> visitor)
  1508. {
  1509. visitor(m_dst);
  1510. visitor(m_arguments);
  1511. }
  1512. Operand dst() const { return m_dst; }
  1513. Operand arguments() const { return m_arguments; }
  1514. bool is_synthetic() const { return m_is_synthetic; }
  1515. private:
  1516. Operand m_dst;
  1517. Operand m_arguments;
  1518. bool m_is_synthetic;
  1519. };
  1520. class NewClass final : public Instruction {
  1521. public:
  1522. static constexpr bool IsVariableLength = true;
  1523. explicit NewClass(Operand dst, Optional<Operand> super_class, ClassExpression const& class_expression, Optional<IdentifierTableIndex> lhs_name, ReadonlySpan<Optional<ScopedOperand>> elements_keys)
  1524. : Instruction(Type::NewClass)
  1525. , m_dst(dst)
  1526. , m_super_class(super_class)
  1527. , m_class_expression(class_expression)
  1528. , m_lhs_name(lhs_name)
  1529. , m_element_keys_count(elements_keys.size())
  1530. {
  1531. for (size_t i = 0; i < m_element_keys_count; i++) {
  1532. if (elements_keys[i].has_value())
  1533. m_element_keys[i] = elements_keys[i]->operand();
  1534. }
  1535. }
  1536. size_t length_impl() const
  1537. {
  1538. return round_up_to_power_of_two(alignof(void*), sizeof(*this) + sizeof(Optional<Operand>) * m_element_keys_count);
  1539. }
  1540. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1541. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  1542. void visit_operands_impl(Function<void(Operand&)> visitor)
  1543. {
  1544. visitor(m_dst);
  1545. if (m_super_class.has_value())
  1546. visitor(m_super_class.value());
  1547. for (size_t i = 0; i < m_element_keys_count; i++) {
  1548. if (m_element_keys[i].has_value())
  1549. visitor(m_element_keys[i].value());
  1550. }
  1551. }
  1552. Operand dst() const { return m_dst; }
  1553. Optional<Operand> const& super_class() const { return m_super_class; }
  1554. ClassExpression const& class_expression() const { return m_class_expression; }
  1555. Optional<IdentifierTableIndex> const& lhs_name() const { return m_lhs_name; }
  1556. private:
  1557. Operand m_dst;
  1558. Optional<Operand> m_super_class;
  1559. ClassExpression const& m_class_expression;
  1560. Optional<IdentifierTableIndex> m_lhs_name;
  1561. size_t m_element_keys_count { 0 };
  1562. Optional<Operand> m_element_keys[];
  1563. };
  1564. class NewFunction final : public Instruction {
  1565. public:
  1566. explicit NewFunction(Operand dst, FunctionNode const& function_node, Optional<IdentifierTableIndex> lhs_name, Optional<Operand> home_object = {})
  1567. : Instruction(Type::NewFunction)
  1568. , m_dst(dst)
  1569. , m_function_node(function_node)
  1570. , m_lhs_name(lhs_name)
  1571. , m_home_object(move(home_object))
  1572. {
  1573. }
  1574. void execute_impl(Bytecode::Interpreter&) const;
  1575. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  1576. void visit_operands_impl(Function<void(Operand&)> visitor)
  1577. {
  1578. visitor(m_dst);
  1579. if (m_home_object.has_value())
  1580. visitor(m_home_object.value());
  1581. }
  1582. Operand dst() const { return m_dst; }
  1583. FunctionNode const& function_node() const { return m_function_node; }
  1584. Optional<IdentifierTableIndex> const& lhs_name() const { return m_lhs_name; }
  1585. Optional<Operand> const& home_object() const { return m_home_object; }
  1586. private:
  1587. Operand m_dst;
  1588. FunctionNode const& m_function_node;
  1589. Optional<IdentifierTableIndex> m_lhs_name;
  1590. Optional<Operand> m_home_object;
  1591. };
  1592. class BlockDeclarationInstantiation final : public Instruction {
  1593. public:
  1594. explicit BlockDeclarationInstantiation(ScopeNode const& scope_node)
  1595. : Instruction(Type::BlockDeclarationInstantiation)
  1596. , m_scope_node(scope_node)
  1597. {
  1598. }
  1599. void execute_impl(Bytecode::Interpreter&) const;
  1600. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  1601. ScopeNode const& scope_node() const { return m_scope_node; }
  1602. private:
  1603. ScopeNode const& m_scope_node;
  1604. };
  1605. class Return final : public Instruction {
  1606. public:
  1607. constexpr static bool IsTerminator = true;
  1608. explicit Return(Optional<Operand> value = {})
  1609. : Instruction(Type::Return)
  1610. , m_value(value)
  1611. {
  1612. }
  1613. void execute_impl(Bytecode::Interpreter&) const;
  1614. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  1615. void visit_operands_impl(Function<void(Operand&)> visitor)
  1616. {
  1617. if (m_value.has_value())
  1618. visitor(m_value.value());
  1619. }
  1620. Optional<Operand> const& value() const { return m_value; }
  1621. private:
  1622. Optional<Operand> m_value;
  1623. };
  1624. class Increment final : public Instruction {
  1625. public:
  1626. explicit Increment(Operand dst)
  1627. : Instruction(Type::Increment)
  1628. , m_dst(dst)
  1629. {
  1630. }
  1631. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1632. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  1633. void visit_operands_impl(Function<void(Operand&)> visitor)
  1634. {
  1635. visitor(m_dst);
  1636. }
  1637. Operand dst() const { return m_dst; }
  1638. private:
  1639. Operand m_dst;
  1640. };
  1641. class PostfixIncrement final : public Instruction {
  1642. public:
  1643. explicit PostfixIncrement(Operand dst, Operand src)
  1644. : Instruction(Type::PostfixIncrement)
  1645. , m_dst(dst)
  1646. , m_src(src)
  1647. {
  1648. }
  1649. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1650. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  1651. void visit_operands_impl(Function<void(Operand&)> visitor)
  1652. {
  1653. visitor(m_dst);
  1654. visitor(m_src);
  1655. }
  1656. Operand dst() const { return m_dst; }
  1657. Operand src() const { return m_src; }
  1658. private:
  1659. Operand m_dst;
  1660. Operand m_src;
  1661. };
  1662. class Decrement final : public Instruction {
  1663. public:
  1664. explicit Decrement(Operand dst)
  1665. : Instruction(Type::Decrement)
  1666. , m_dst(dst)
  1667. {
  1668. }
  1669. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1670. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  1671. void visit_operands_impl(Function<void(Operand&)> visitor)
  1672. {
  1673. visitor(m_dst);
  1674. }
  1675. Operand dst() const { return m_dst; }
  1676. private:
  1677. Operand m_dst;
  1678. };
  1679. class PostfixDecrement final : public Instruction {
  1680. public:
  1681. explicit PostfixDecrement(Operand dst, Operand src)
  1682. : Instruction(Type::PostfixDecrement)
  1683. , m_dst(dst)
  1684. , m_src(src)
  1685. {
  1686. }
  1687. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1688. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  1689. void visit_operands_impl(Function<void(Operand&)> visitor)
  1690. {
  1691. visitor(m_dst);
  1692. visitor(m_src);
  1693. }
  1694. Operand dst() const { return m_dst; }
  1695. Operand src() const { return m_src; }
  1696. private:
  1697. Operand m_dst;
  1698. Operand m_src;
  1699. };
  1700. class Throw final : public Instruction {
  1701. public:
  1702. constexpr static bool IsTerminator = true;
  1703. explicit Throw(Operand src)
  1704. : Instruction(Type::Throw)
  1705. , m_src(src)
  1706. {
  1707. }
  1708. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1709. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  1710. void visit_operands_impl(Function<void(Operand&)> visitor)
  1711. {
  1712. visitor(m_src);
  1713. }
  1714. Operand src() const { return m_src; }
  1715. private:
  1716. Operand m_src;
  1717. };
  1718. class ThrowIfNotObject final : public Instruction {
  1719. public:
  1720. ThrowIfNotObject(Operand src)
  1721. : Instruction(Type::ThrowIfNotObject)
  1722. , m_src(src)
  1723. {
  1724. }
  1725. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1726. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  1727. void visit_operands_impl(Function<void(Operand&)> visitor)
  1728. {
  1729. visitor(m_src);
  1730. }
  1731. Operand src() const { return m_src; }
  1732. private:
  1733. Operand m_src;
  1734. };
  1735. class ThrowIfNullish final : public Instruction {
  1736. public:
  1737. explicit ThrowIfNullish(Operand src)
  1738. : Instruction(Type::ThrowIfNullish)
  1739. , m_src(src)
  1740. {
  1741. }
  1742. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1743. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  1744. void visit_operands_impl(Function<void(Operand&)> visitor)
  1745. {
  1746. visitor(m_src);
  1747. }
  1748. Operand src() const { return m_src; }
  1749. private:
  1750. Operand m_src;
  1751. };
  1752. class ThrowIfTDZ final : public Instruction {
  1753. public:
  1754. explicit ThrowIfTDZ(Operand src)
  1755. : Instruction(Type::ThrowIfTDZ)
  1756. , m_src(src)
  1757. {
  1758. }
  1759. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1760. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  1761. void visit_operands_impl(Function<void(Operand&)> visitor)
  1762. {
  1763. visitor(m_src);
  1764. }
  1765. Operand src() const { return m_src; }
  1766. private:
  1767. Operand m_src;
  1768. };
  1769. class EnterUnwindContext final : public Instruction {
  1770. public:
  1771. constexpr static bool IsTerminator = true;
  1772. EnterUnwindContext(Label entry_point)
  1773. : Instruction(Type::EnterUnwindContext)
  1774. , m_entry_point(move(entry_point))
  1775. {
  1776. }
  1777. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  1778. void visit_labels_impl(Function<void(Label&)> visitor)
  1779. {
  1780. visitor(m_entry_point);
  1781. }
  1782. auto& entry_point() const { return m_entry_point; }
  1783. private:
  1784. Label m_entry_point;
  1785. };
  1786. class ScheduleJump final : public Instruction {
  1787. public:
  1788. // Note: We use this instruction to tell the next `finally` block to
  1789. // continue execution with a specific break/continue target;
  1790. constexpr static bool IsTerminator = true;
  1791. ScheduleJump(Label target)
  1792. : Instruction(Type::ScheduleJump)
  1793. , m_target(target)
  1794. {
  1795. }
  1796. Label target() const { return m_target; }
  1797. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  1798. void visit_labels_impl(Function<void(Label&)> visitor)
  1799. {
  1800. visitor(m_target);
  1801. }
  1802. private:
  1803. Label m_target;
  1804. };
  1805. class LeaveLexicalEnvironment final : public Instruction {
  1806. public:
  1807. LeaveLexicalEnvironment()
  1808. : Instruction(Type::LeaveLexicalEnvironment)
  1809. {
  1810. }
  1811. void execute_impl(Bytecode::Interpreter&) const;
  1812. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  1813. };
  1814. class LeavePrivateEnvironment final : public Instruction {
  1815. public:
  1816. LeavePrivateEnvironment()
  1817. : Instruction(Type::LeavePrivateEnvironment)
  1818. {
  1819. }
  1820. void execute_impl(Bytecode::Interpreter&) const;
  1821. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  1822. };
  1823. class LeaveUnwindContext final : public Instruction {
  1824. public:
  1825. LeaveUnwindContext()
  1826. : Instruction(Type::LeaveUnwindContext)
  1827. {
  1828. }
  1829. void execute_impl(Bytecode::Interpreter&) const;
  1830. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  1831. };
  1832. class ContinuePendingUnwind final : public Instruction {
  1833. public:
  1834. constexpr static bool IsTerminator = true;
  1835. explicit ContinuePendingUnwind(Label resume_target)
  1836. : Instruction(Type::ContinuePendingUnwind)
  1837. , m_resume_target(resume_target)
  1838. {
  1839. }
  1840. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  1841. void visit_labels_impl(Function<void(Label&)> visitor)
  1842. {
  1843. visitor(m_resume_target);
  1844. }
  1845. auto& resume_target() const { return m_resume_target; }
  1846. private:
  1847. Label m_resume_target;
  1848. };
  1849. class Yield final : public Instruction {
  1850. public:
  1851. constexpr static bool IsTerminator = true;
  1852. explicit Yield(Label continuation_label, Operand value)
  1853. : Instruction(Type::Yield)
  1854. , m_continuation_label(continuation_label)
  1855. , m_value(value)
  1856. {
  1857. }
  1858. explicit Yield(nullptr_t, Operand value)
  1859. : Instruction(Type::Yield)
  1860. , m_value(value)
  1861. {
  1862. }
  1863. void execute_impl(Bytecode::Interpreter&) const;
  1864. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  1865. void visit_labels_impl(Function<void(Label&)> visitor)
  1866. {
  1867. if (m_continuation_label.has_value())
  1868. visitor(m_continuation_label.value());
  1869. }
  1870. void visit_operands_impl(Function<void(Operand&)> visitor)
  1871. {
  1872. visitor(m_value);
  1873. }
  1874. auto& continuation() const { return m_continuation_label; }
  1875. Operand value() const { return m_value; }
  1876. private:
  1877. Optional<Label> m_continuation_label;
  1878. Operand m_value;
  1879. };
  1880. class Await final : public Instruction {
  1881. public:
  1882. constexpr static bool IsTerminator = true;
  1883. explicit Await(Label continuation_label, Operand argument)
  1884. : Instruction(Type::Await)
  1885. , m_continuation_label(continuation_label)
  1886. , m_argument(argument)
  1887. {
  1888. }
  1889. void execute_impl(Bytecode::Interpreter&) const;
  1890. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  1891. void visit_labels_impl(Function<void(Label&)> visitor)
  1892. {
  1893. visitor(m_continuation_label);
  1894. }
  1895. void visit_operands_impl(Function<void(Operand&)> visitor)
  1896. {
  1897. visitor(m_argument);
  1898. }
  1899. auto& continuation() const { return m_continuation_label; }
  1900. Operand argument() const { return m_argument; }
  1901. private:
  1902. Label m_continuation_label;
  1903. Operand m_argument;
  1904. };
  1905. class GetIterator final : public Instruction {
  1906. public:
  1907. GetIterator(Operand dst, Operand iterable, IteratorHint hint = IteratorHint::Sync)
  1908. : Instruction(Type::GetIterator)
  1909. , m_dst(dst)
  1910. , m_iterable(iterable)
  1911. , m_hint(hint)
  1912. {
  1913. }
  1914. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1915. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  1916. void visit_operands_impl(Function<void(Operand&)> visitor)
  1917. {
  1918. visitor(m_dst);
  1919. visitor(m_iterable);
  1920. }
  1921. Operand dst() const { return m_dst; }
  1922. Operand iterable() const { return m_iterable; }
  1923. IteratorHint hint() const { return m_hint; }
  1924. private:
  1925. Operand m_dst;
  1926. Operand m_iterable;
  1927. IteratorHint m_hint { IteratorHint::Sync };
  1928. };
  1929. class GetObjectFromIteratorRecord final : public Instruction {
  1930. public:
  1931. GetObjectFromIteratorRecord(Operand object, Operand iterator_record)
  1932. : Instruction(Type::GetObjectFromIteratorRecord)
  1933. , m_object(object)
  1934. , m_iterator_record(iterator_record)
  1935. {
  1936. }
  1937. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1938. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  1939. void visit_operands_impl(Function<void(Operand&)> visitor)
  1940. {
  1941. visitor(m_object);
  1942. visitor(m_iterator_record);
  1943. }
  1944. Operand object() const { return m_object; }
  1945. Operand iterator_record() const { return m_iterator_record; }
  1946. private:
  1947. Operand m_object;
  1948. Operand m_iterator_record;
  1949. };
  1950. class GetNextMethodFromIteratorRecord final : public Instruction {
  1951. public:
  1952. GetNextMethodFromIteratorRecord(Operand next_method, Operand iterator_record)
  1953. : Instruction(Type::GetNextMethodFromIteratorRecord)
  1954. , m_next_method(next_method)
  1955. , m_iterator_record(iterator_record)
  1956. {
  1957. }
  1958. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1959. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  1960. void visit_operands_impl(Function<void(Operand&)> visitor)
  1961. {
  1962. visitor(m_next_method);
  1963. visitor(m_iterator_record);
  1964. }
  1965. Operand next_method() const { return m_next_method; }
  1966. Operand iterator_record() const { return m_iterator_record; }
  1967. private:
  1968. Operand m_next_method;
  1969. Operand m_iterator_record;
  1970. };
  1971. class GetMethod final : public Instruction {
  1972. public:
  1973. GetMethod(Operand dst, Operand object, IdentifierTableIndex property)
  1974. : Instruction(Type::GetMethod)
  1975. , m_dst(dst)
  1976. , m_object(object)
  1977. , m_property(property)
  1978. {
  1979. }
  1980. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1981. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  1982. void visit_operands_impl(Function<void(Operand&)> visitor)
  1983. {
  1984. visitor(m_dst);
  1985. visitor(m_object);
  1986. }
  1987. Operand dst() const { return m_dst; }
  1988. Operand object() const { return m_object; }
  1989. IdentifierTableIndex property() const { return m_property; }
  1990. private:
  1991. Operand m_dst;
  1992. Operand m_object;
  1993. IdentifierTableIndex m_property;
  1994. };
  1995. class GetObjectPropertyIterator final : public Instruction {
  1996. public:
  1997. GetObjectPropertyIterator(Operand dst, Operand object)
  1998. : Instruction(Type::GetObjectPropertyIterator)
  1999. , m_dst(dst)
  2000. , m_object(object)
  2001. {
  2002. }
  2003. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  2004. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  2005. void visit_operands_impl(Function<void(Operand&)> visitor)
  2006. {
  2007. visitor(m_dst);
  2008. visitor(m_object);
  2009. }
  2010. Operand dst() const { return m_dst; }
  2011. Operand object() const { return m_object; }
  2012. private:
  2013. Operand m_dst;
  2014. Operand m_object;
  2015. };
  2016. class IteratorClose final : public Instruction {
  2017. public:
  2018. IteratorClose(Operand iterator_record, Completion::Type completion_type, Optional<Value> completion_value)
  2019. : Instruction(Type::IteratorClose)
  2020. , m_iterator_record(iterator_record)
  2021. , m_completion_type(completion_type)
  2022. , m_completion_value(completion_value)
  2023. {
  2024. }
  2025. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  2026. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  2027. void visit_operands_impl(Function<void(Operand&)> visitor)
  2028. {
  2029. visitor(m_iterator_record);
  2030. }
  2031. Operand iterator_record() const { return m_iterator_record; }
  2032. Completion::Type completion_type() const { return m_completion_type; }
  2033. Optional<Value> const& completion_value() const { return m_completion_value; }
  2034. private:
  2035. Operand m_iterator_record;
  2036. Completion::Type m_completion_type { Completion::Type::Normal };
  2037. Optional<Value> m_completion_value;
  2038. };
  2039. class AsyncIteratorClose final : public Instruction {
  2040. public:
  2041. AsyncIteratorClose(Operand iterator_record, Completion::Type completion_type, Optional<Value> completion_value)
  2042. : Instruction(Type::AsyncIteratorClose)
  2043. , m_iterator_record(iterator_record)
  2044. , m_completion_type(completion_type)
  2045. , m_completion_value(completion_value)
  2046. {
  2047. }
  2048. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  2049. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  2050. void visit_operands_impl(Function<void(Operand&)> visitor)
  2051. {
  2052. visitor(m_iterator_record);
  2053. }
  2054. Operand iterator_record() const { return m_iterator_record; }
  2055. Completion::Type completion_type() const { return m_completion_type; }
  2056. Optional<Value> const& completion_value() const { return m_completion_value; }
  2057. private:
  2058. Operand m_iterator_record;
  2059. Completion::Type m_completion_type { Completion::Type::Normal };
  2060. Optional<Value> m_completion_value;
  2061. };
  2062. class IteratorNext final : public Instruction {
  2063. public:
  2064. IteratorNext(Operand dst, Operand iterator_record)
  2065. : Instruction(Type::IteratorNext)
  2066. , m_dst(dst)
  2067. , m_iterator_record(iterator_record)
  2068. {
  2069. }
  2070. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  2071. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  2072. void visit_operands_impl(Function<void(Operand&)> visitor)
  2073. {
  2074. visitor(m_dst);
  2075. visitor(m_iterator_record);
  2076. }
  2077. Operand dst() const { return m_dst; }
  2078. Operand iterator_record() const { return m_iterator_record; }
  2079. private:
  2080. Operand m_dst;
  2081. Operand m_iterator_record;
  2082. };
  2083. class ResolveThisBinding final : public Instruction {
  2084. public:
  2085. explicit ResolveThisBinding(Operand dst)
  2086. : Instruction(Type::ResolveThisBinding)
  2087. , m_dst(dst)
  2088. {
  2089. }
  2090. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  2091. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  2092. void visit_operands_impl(Function<void(Operand&)> visitor)
  2093. {
  2094. visitor(m_dst);
  2095. }
  2096. Operand dst() const { return m_dst; }
  2097. private:
  2098. Operand m_dst;
  2099. };
  2100. class ResolveSuperBase final : public Instruction {
  2101. public:
  2102. explicit ResolveSuperBase(Operand dst)
  2103. : Instruction(Type::ResolveSuperBase)
  2104. , m_dst(dst)
  2105. {
  2106. }
  2107. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  2108. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  2109. void visit_operands_impl(Function<void(Operand&)> visitor)
  2110. {
  2111. visitor(m_dst);
  2112. }
  2113. Operand dst() const { return m_dst; }
  2114. private:
  2115. Operand m_dst;
  2116. };
  2117. class GetNewTarget final : public Instruction {
  2118. public:
  2119. explicit GetNewTarget(Operand dst)
  2120. : Instruction(Type::GetNewTarget)
  2121. , m_dst(dst)
  2122. {
  2123. }
  2124. void execute_impl(Bytecode::Interpreter&) const;
  2125. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  2126. void visit_operands_impl(Function<void(Operand&)> visitor)
  2127. {
  2128. visitor(m_dst);
  2129. }
  2130. Operand dst() const { return m_dst; }
  2131. private:
  2132. Operand m_dst;
  2133. };
  2134. class GetImportMeta final : public Instruction {
  2135. public:
  2136. explicit GetImportMeta(Operand dst)
  2137. : Instruction(Type::GetImportMeta)
  2138. , m_dst(dst)
  2139. {
  2140. }
  2141. void execute_impl(Bytecode::Interpreter&) const;
  2142. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  2143. void visit_operands_impl(Function<void(Operand&)> visitor)
  2144. {
  2145. visitor(m_dst);
  2146. }
  2147. Operand dst() const { return m_dst; }
  2148. private:
  2149. Operand m_dst;
  2150. };
  2151. class TypeofVariable final : public Instruction {
  2152. public:
  2153. TypeofVariable(Operand dst, IdentifierTableIndex identifier)
  2154. : Instruction(Type::TypeofVariable)
  2155. , m_dst(dst)
  2156. , m_identifier(identifier)
  2157. {
  2158. }
  2159. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  2160. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  2161. void visit_operands_impl(Function<void(Operand&)> visitor)
  2162. {
  2163. visitor(m_dst);
  2164. }
  2165. Operand dst() const { return m_dst; }
  2166. IdentifierTableIndex identifier() const { return m_identifier; }
  2167. private:
  2168. Operand m_dst;
  2169. IdentifierTableIndex m_identifier;
  2170. };
  2171. class End final : public Instruction {
  2172. public:
  2173. constexpr static bool IsTerminator = true;
  2174. explicit End(Operand value)
  2175. : Instruction(Type::End)
  2176. , m_value(value)
  2177. {
  2178. }
  2179. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  2180. void visit_operands_impl(Function<void(Operand&)> visitor)
  2181. {
  2182. visitor(m_value);
  2183. }
  2184. Operand value() const { return m_value; }
  2185. private:
  2186. Operand m_value;
  2187. };
  2188. class Dump final : public Instruction {
  2189. public:
  2190. explicit Dump(StringView text, Operand value)
  2191. : Instruction(Type::Dump)
  2192. , m_text(text)
  2193. , m_value(value)
  2194. {
  2195. }
  2196. void execute_impl(Bytecode::Interpreter&) const;
  2197. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  2198. void visit_operands_impl(Function<void(Operand&)> visitor)
  2199. {
  2200. visitor(m_value);
  2201. }
  2202. private:
  2203. StringView m_text;
  2204. Operand m_value;
  2205. };
  2206. }