Op.h 64 KB

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