Op.h 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365
  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/StdLibExtras.h>
  10. #include <LibCrypto/BigInt/SignedBigInteger.h>
  11. #include <LibJS/Bytecode/IdentifierTable.h>
  12. #include <LibJS/Bytecode/Instruction.h>
  13. #include <LibJS/Bytecode/Label.h>
  14. #include <LibJS/Bytecode/Register.h>
  15. #include <LibJS/Bytecode/StringTable.h>
  16. #include <LibJS/Heap/Cell.h>
  17. #include <LibJS/Runtime/Environment.h>
  18. #include <LibJS/Runtime/EnvironmentCoordinate.h>
  19. #include <LibJS/Runtime/Value.h>
  20. #include <LibJS/Runtime/ValueTraits.h>
  21. namespace JS {
  22. class FunctionExpression;
  23. }
  24. namespace JS::Bytecode::Op {
  25. class Load final : public Instruction {
  26. public:
  27. explicit Load(Register src)
  28. : Instruction(Type::Load)
  29. , m_src(src)
  30. {
  31. }
  32. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  33. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  34. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  35. void replace_references_impl(Register from, Register to)
  36. {
  37. if (m_src == from)
  38. m_src = to;
  39. }
  40. private:
  41. Register m_src;
  42. };
  43. class LoadImmediate final : public Instruction {
  44. public:
  45. explicit LoadImmediate(Value value)
  46. : Instruction(Type::LoadImmediate)
  47. , m_value(value)
  48. {
  49. }
  50. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  51. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  52. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  53. void replace_references_impl(Register, Register) { }
  54. private:
  55. Value m_value;
  56. };
  57. class Store final : public Instruction {
  58. public:
  59. explicit Store(Register dst)
  60. : Instruction(Type::Store)
  61. , m_dst(dst)
  62. {
  63. }
  64. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  65. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  66. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  67. void replace_references_impl(Register, Register) { }
  68. Register dst() const { return m_dst; }
  69. private:
  70. Register m_dst;
  71. };
  72. #define JS_ENUMERATE_COMMON_BINARY_OPS(O) \
  73. O(Add, add) \
  74. O(Sub, sub) \
  75. O(Mul, mul) \
  76. O(Div, div) \
  77. O(Exp, exp) \
  78. O(Mod, mod) \
  79. O(In, in) \
  80. O(InstanceOf, instance_of) \
  81. O(GreaterThan, greater_than) \
  82. O(GreaterThanEquals, greater_than_equals) \
  83. O(LessThan, less_than) \
  84. O(LessThanEquals, less_than_equals) \
  85. O(LooselyInequals, abstract_inequals) \
  86. O(LooselyEquals, abstract_equals) \
  87. O(StrictlyInequals, typed_inequals) \
  88. O(StrictlyEquals, typed_equals) \
  89. O(BitwiseAnd, bitwise_and) \
  90. O(BitwiseOr, bitwise_or) \
  91. O(BitwiseXor, bitwise_xor) \
  92. O(LeftShift, left_shift) \
  93. O(RightShift, right_shift) \
  94. O(UnsignedRightShift, unsigned_right_shift)
  95. #define JS_DECLARE_COMMON_BINARY_OP(OpTitleCase, op_snake_case) \
  96. class OpTitleCase final : public Instruction { \
  97. public: \
  98. explicit OpTitleCase(Register lhs_reg) \
  99. : Instruction(Type::OpTitleCase) \
  100. , m_lhs_reg(lhs_reg) \
  101. { \
  102. } \
  103. \
  104. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const; \
  105. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const; \
  106. void replace_references_impl(BasicBlock const&, BasicBlock const&) \
  107. { \
  108. } \
  109. void replace_references_impl(Register from, Register to) \
  110. { \
  111. if (m_lhs_reg == from) \
  112. m_lhs_reg = to; \
  113. } \
  114. \
  115. private: \
  116. Register m_lhs_reg; \
  117. };
  118. JS_ENUMERATE_COMMON_BINARY_OPS(JS_DECLARE_COMMON_BINARY_OP)
  119. #undef JS_DECLARE_COMMON_BINARY_OP
  120. #define JS_ENUMERATE_COMMON_UNARY_OPS(O) \
  121. O(BitwiseNot, bitwise_not) \
  122. O(Not, not_) \
  123. O(UnaryPlus, unary_plus) \
  124. O(UnaryMinus, unary_minus) \
  125. O(Typeof, typeof_)
  126. #define JS_DECLARE_COMMON_UNARY_OP(OpTitleCase, op_snake_case) \
  127. class OpTitleCase final : public Instruction { \
  128. public: \
  129. OpTitleCase() \
  130. : Instruction(Type::OpTitleCase) \
  131. { \
  132. } \
  133. \
  134. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const; \
  135. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const; \
  136. void replace_references_impl(BasicBlock const&, BasicBlock const&) \
  137. { \
  138. } \
  139. void replace_references_impl(Register, Register) \
  140. { \
  141. } \
  142. };
  143. JS_ENUMERATE_COMMON_UNARY_OPS(JS_DECLARE_COMMON_UNARY_OP)
  144. #undef JS_DECLARE_COMMON_UNARY_OP
  145. class NewString final : public Instruction {
  146. public:
  147. explicit NewString(StringTableIndex string)
  148. : Instruction(Type::NewString)
  149. , m_string(string)
  150. {
  151. }
  152. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  153. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  154. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  155. void replace_references_impl(Register, Register) { }
  156. private:
  157. StringTableIndex m_string;
  158. };
  159. class NewObject final : public Instruction {
  160. public:
  161. NewObject()
  162. : Instruction(Type::NewObject)
  163. {
  164. }
  165. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  166. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  167. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  168. void replace_references_impl(Register, Register) { }
  169. };
  170. class NewRegExp final : public Instruction {
  171. public:
  172. NewRegExp(StringTableIndex source_index, StringTableIndex flags_index)
  173. : Instruction(Type::NewRegExp)
  174. , m_source_index(source_index)
  175. , m_flags_index(flags_index)
  176. {
  177. }
  178. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  179. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  180. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  181. void replace_references_impl(Register, Register) { }
  182. private:
  183. StringTableIndex m_source_index;
  184. StringTableIndex m_flags_index;
  185. };
  186. #define JS_ENUMERATE_NEW_BUILTIN_ERROR_OPS(O) \
  187. O(TypeError)
  188. #define JS_DECLARE_NEW_BUILTIN_ERROR_OP(ErrorName) \
  189. class New##ErrorName final : public Instruction { \
  190. public: \
  191. explicit New##ErrorName(StringTableIndex error_string) \
  192. : Instruction(Type::New##ErrorName) \
  193. , m_error_string(error_string) \
  194. { \
  195. } \
  196. \
  197. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const; \
  198. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const; \
  199. void replace_references_impl(BasicBlock const&, BasicBlock const&) \
  200. { \
  201. } \
  202. void replace_references_impl(Register, Register) \
  203. { \
  204. } \
  205. \
  206. private: \
  207. StringTableIndex m_error_string; \
  208. };
  209. JS_ENUMERATE_NEW_BUILTIN_ERROR_OPS(JS_DECLARE_NEW_BUILTIN_ERROR_OP)
  210. #undef JS_DECLARE_NEW_BUILTIN_ERROR_OP
  211. // NOTE: This instruction is variable-width depending on the number of excluded names
  212. class CopyObjectExcludingProperties final : public Instruction {
  213. public:
  214. CopyObjectExcludingProperties(Register from_object, Vector<Register> const& excluded_names)
  215. : Instruction(Type::CopyObjectExcludingProperties)
  216. , m_from_object(from_object)
  217. , m_excluded_names_count(excluded_names.size())
  218. {
  219. for (size_t i = 0; i < m_excluded_names_count; i++)
  220. m_excluded_names[i] = excluded_names[i];
  221. }
  222. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  223. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  224. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  225. void replace_references_impl(Register from, Register to);
  226. size_t length_impl() const { return sizeof(*this) + sizeof(Register) * m_excluded_names_count; }
  227. private:
  228. Register m_from_object;
  229. size_t m_excluded_names_count { 0 };
  230. Register m_excluded_names[];
  231. };
  232. class NewBigInt final : public Instruction {
  233. public:
  234. explicit NewBigInt(Crypto::SignedBigInteger bigint)
  235. : Instruction(Type::NewBigInt)
  236. , m_bigint(move(bigint))
  237. {
  238. }
  239. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  240. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  241. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  242. void replace_references_impl(Register, Register) { }
  243. private:
  244. Crypto::SignedBigInteger m_bigint;
  245. };
  246. // NOTE: This instruction is variable-width depending on the number of elements!
  247. class NewArray final : public Instruction {
  248. public:
  249. NewArray()
  250. : Instruction(Type::NewArray)
  251. , m_element_count(0)
  252. {
  253. }
  254. explicit NewArray(AK::Array<Register, 2> const& elements_range)
  255. : Instruction(Type::NewArray)
  256. , m_element_count(elements_range[1].index() - elements_range[0].index() + 1)
  257. {
  258. m_elements[0] = elements_range[0];
  259. m_elements[1] = elements_range[1];
  260. }
  261. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  262. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  263. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  264. // Note: The underlying element range shall never be changed item, by item
  265. // shifting it may be done in the future
  266. void replace_references_impl(Register from, Register) { VERIFY(!m_element_count || from.index() < start().index() || from.index() > end().index()); }
  267. size_t length_impl() const
  268. {
  269. return sizeof(*this) + sizeof(Register) * (m_element_count == 0 ? 0 : 2);
  270. }
  271. Register start() const
  272. {
  273. VERIFY(m_element_count);
  274. return m_elements[0];
  275. }
  276. Register end() const
  277. {
  278. VERIFY(m_element_count);
  279. return m_elements[1];
  280. }
  281. size_t element_count() const { return m_element_count; }
  282. private:
  283. size_t m_element_count { 0 };
  284. Register m_elements[];
  285. };
  286. class Append final : public Instruction {
  287. public:
  288. Append(Register lhs, bool is_spread)
  289. : Instruction(Type::Append)
  290. , m_lhs(lhs)
  291. , m_is_spread(is_spread)
  292. {
  293. }
  294. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  295. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  296. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  297. // Note: This should never do anything, the lhs should always be an array, that is currently being constructed
  298. void replace_references_impl(Register from, Register) { VERIFY(from != m_lhs); }
  299. private:
  300. Register m_lhs;
  301. bool m_is_spread = false;
  302. };
  303. class ImportCall final : public Instruction {
  304. public:
  305. ImportCall(Register specifier, Register options)
  306. : Instruction(Type::ImportCall)
  307. , m_specifier(specifier)
  308. , m_options(options)
  309. {
  310. }
  311. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  312. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  313. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  314. void replace_references_impl(Register, Register);
  315. private:
  316. Register m_specifier;
  317. Register m_options;
  318. };
  319. class IteratorToArray final : public Instruction {
  320. public:
  321. IteratorToArray()
  322. : Instruction(Type::IteratorToArray)
  323. {
  324. }
  325. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  326. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  327. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  328. void replace_references_impl(Register, Register) { }
  329. };
  330. class ConcatString final : public Instruction {
  331. public:
  332. explicit ConcatString(Register lhs)
  333. : Instruction(Type::ConcatString)
  334. , m_lhs(lhs)
  335. {
  336. }
  337. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  338. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  339. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  340. // Note: lhs should always be a string in construction, so this should never do anything
  341. void replace_references_impl(Register from, Register) { VERIFY(from != m_lhs); }
  342. private:
  343. Register m_lhs;
  344. };
  345. enum class EnvironmentMode {
  346. Lexical,
  347. Var,
  348. };
  349. class CreateLexicalEnvironment final : public Instruction {
  350. public:
  351. explicit CreateLexicalEnvironment()
  352. : Instruction(Type::CreateLexicalEnvironment)
  353. {
  354. }
  355. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  356. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  357. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  358. void replace_references_impl(Register, Register) { }
  359. };
  360. class EnterObjectEnvironment final : public Instruction {
  361. public:
  362. explicit EnterObjectEnvironment()
  363. : Instruction(Type::EnterObjectEnvironment)
  364. {
  365. }
  366. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  367. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  368. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  369. void replace_references_impl(Register, Register) { }
  370. };
  371. class CreateVariable final : public Instruction {
  372. public:
  373. explicit CreateVariable(IdentifierTableIndex identifier, EnvironmentMode mode, bool is_immutable, bool is_global = false)
  374. : Instruction(Type::CreateVariable)
  375. , m_identifier(identifier)
  376. , m_mode(mode)
  377. , m_is_immutable(is_immutable)
  378. , m_is_global(is_global)
  379. {
  380. }
  381. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  382. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  383. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  384. void replace_references_impl(Register, Register) { }
  385. private:
  386. IdentifierTableIndex m_identifier;
  387. EnvironmentMode m_mode;
  388. bool m_is_immutable : 4 { false };
  389. bool m_is_global : 4 { false };
  390. };
  391. class SetVariable final : public Instruction {
  392. public:
  393. enum class InitializationMode {
  394. Initialize,
  395. Set,
  396. InitializeOrSet,
  397. };
  398. explicit SetVariable(IdentifierTableIndex identifier, InitializationMode initialization_mode = InitializationMode::Set, EnvironmentMode mode = EnvironmentMode::Lexical)
  399. : Instruction(Type::SetVariable)
  400. , m_identifier(identifier)
  401. , m_mode(mode)
  402. , m_initialization_mode(initialization_mode)
  403. {
  404. }
  405. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  406. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  407. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  408. void replace_references_impl(Register, Register) { }
  409. IdentifierTableIndex identifier() const { return m_identifier; }
  410. private:
  411. IdentifierTableIndex m_identifier;
  412. EnvironmentMode m_mode;
  413. InitializationMode m_initialization_mode { InitializationMode::Set };
  414. };
  415. class GetVariable final : public Instruction {
  416. public:
  417. explicit GetVariable(IdentifierTableIndex identifier)
  418. : Instruction(Type::GetVariable)
  419. , m_identifier(identifier)
  420. {
  421. }
  422. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  423. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  424. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  425. void replace_references_impl(Register, Register) { }
  426. IdentifierTableIndex identifier() const { return m_identifier; }
  427. private:
  428. IdentifierTableIndex m_identifier;
  429. Optional<EnvironmentCoordinate> mutable m_cached_environment_coordinate;
  430. };
  431. class DeleteVariable final : public Instruction {
  432. public:
  433. explicit DeleteVariable(IdentifierTableIndex identifier)
  434. : Instruction(Type::DeleteVariable)
  435. , m_identifier(identifier)
  436. {
  437. }
  438. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  439. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  440. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  441. void replace_references_impl(Register, Register) { }
  442. IdentifierTableIndex identifier() const { return m_identifier; }
  443. private:
  444. IdentifierTableIndex m_identifier;
  445. };
  446. class GetById final : public Instruction {
  447. public:
  448. explicit GetById(IdentifierTableIndex property)
  449. : Instruction(Type::GetById)
  450. , m_property(property)
  451. {
  452. }
  453. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  454. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  455. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  456. void replace_references_impl(Register, Register) { }
  457. private:
  458. IdentifierTableIndex m_property;
  459. };
  460. class GetPrivateById final : public Instruction {
  461. public:
  462. explicit GetPrivateById(IdentifierTableIndex property)
  463. : Instruction(Type::GetPrivateById)
  464. , m_property(property)
  465. {
  466. }
  467. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  468. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  469. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  470. void replace_references_impl(Register, Register) { }
  471. private:
  472. IdentifierTableIndex m_property;
  473. };
  474. enum class PropertyKind {
  475. Getter,
  476. Setter,
  477. KeyValue,
  478. Spread,
  479. ProtoSetter,
  480. };
  481. class PutById final : public Instruction {
  482. public:
  483. explicit PutById(Register base, IdentifierTableIndex property, PropertyKind kind = PropertyKind::KeyValue)
  484. : Instruction(Type::PutById)
  485. , m_base(base)
  486. , m_property(property)
  487. , m_kind(kind)
  488. {
  489. }
  490. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  491. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  492. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  493. void replace_references_impl(Register from, Register to)
  494. {
  495. if (m_base == from)
  496. m_base = to;
  497. }
  498. private:
  499. Register m_base;
  500. IdentifierTableIndex m_property;
  501. PropertyKind m_kind;
  502. };
  503. class PutPrivateById final : public Instruction {
  504. public:
  505. explicit PutPrivateById(Register base, IdentifierTableIndex property, PropertyKind kind = PropertyKind::KeyValue)
  506. : Instruction(Type::PutPrivateById)
  507. , m_base(base)
  508. , m_property(property)
  509. , m_kind(kind)
  510. {
  511. }
  512. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  513. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  514. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  515. void replace_references_impl(Register from, Register to)
  516. {
  517. if (m_base == from)
  518. m_base = to;
  519. }
  520. private:
  521. Register m_base;
  522. IdentifierTableIndex m_property;
  523. PropertyKind m_kind;
  524. };
  525. class DeleteById final : public Instruction {
  526. public:
  527. explicit DeleteById(IdentifierTableIndex property)
  528. : Instruction(Type::DeleteById)
  529. , m_property(property)
  530. {
  531. }
  532. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  533. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  534. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  535. void replace_references_impl(Register, Register) { }
  536. private:
  537. IdentifierTableIndex m_property;
  538. };
  539. class GetByValue final : public Instruction {
  540. public:
  541. explicit GetByValue(Register base)
  542. : Instruction(Type::GetByValue)
  543. , m_base(base)
  544. {
  545. }
  546. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  547. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  548. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  549. void replace_references_impl(Register from, Register to)
  550. {
  551. if (m_base == from)
  552. m_base = to;
  553. }
  554. private:
  555. Register m_base;
  556. };
  557. class PutByValue final : public Instruction {
  558. public:
  559. PutByValue(Register base, Register property, PropertyKind kind = PropertyKind::KeyValue)
  560. : Instruction(Type::PutByValue)
  561. , m_base(base)
  562. , m_property(property)
  563. , m_kind(kind)
  564. {
  565. }
  566. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  567. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  568. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  569. void replace_references_impl(Register from, Register to)
  570. {
  571. if (m_base == from)
  572. m_base = to;
  573. }
  574. private:
  575. Register m_base;
  576. Register m_property;
  577. PropertyKind m_kind;
  578. };
  579. class DeleteByValue final : public Instruction {
  580. public:
  581. DeleteByValue(Register base)
  582. : Instruction(Type::DeleteByValue)
  583. , m_base(base)
  584. {
  585. }
  586. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  587. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  588. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  589. void replace_references_impl(Register from, Register to)
  590. {
  591. if (m_base == from)
  592. m_base = to;
  593. }
  594. private:
  595. Register m_base;
  596. };
  597. class Jump : public Instruction {
  598. public:
  599. constexpr static bool IsTerminator = true;
  600. explicit Jump(Type type, Optional<Label> taken_target = {}, Optional<Label> nontaken_target = {})
  601. : Instruction(type)
  602. , m_true_target(move(taken_target))
  603. , m_false_target(move(nontaken_target))
  604. {
  605. }
  606. explicit Jump(Optional<Label> taken_target = {}, Optional<Label> nontaken_target = {})
  607. : Instruction(Type::Jump)
  608. , m_true_target(move(taken_target))
  609. , m_false_target(move(nontaken_target))
  610. {
  611. }
  612. void set_targets(Optional<Label> true_target, Optional<Label> false_target)
  613. {
  614. m_true_target = move(true_target);
  615. m_false_target = move(false_target);
  616. }
  617. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  618. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  619. void replace_references_impl(BasicBlock const&, BasicBlock const&);
  620. void replace_references_impl(Register, Register) { }
  621. auto& true_target() const { return m_true_target; }
  622. auto& false_target() const { return m_false_target; }
  623. protected:
  624. Optional<Label> m_true_target;
  625. Optional<Label> m_false_target;
  626. };
  627. class JumpConditional final : public Jump {
  628. public:
  629. explicit JumpConditional(Optional<Label> true_target = {}, Optional<Label> false_target = {})
  630. : Jump(Type::JumpConditional, move(true_target), move(false_target))
  631. {
  632. }
  633. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  634. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  635. };
  636. class JumpNullish final : public Jump {
  637. public:
  638. explicit JumpNullish(Optional<Label> true_target = {}, Optional<Label> false_target = {})
  639. : Jump(Type::JumpNullish, move(true_target), move(false_target))
  640. {
  641. }
  642. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  643. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  644. };
  645. class JumpUndefined final : public Jump {
  646. public:
  647. explicit JumpUndefined(Optional<Label> true_target = {}, Optional<Label> false_target = {})
  648. : Jump(Type::JumpUndefined, move(true_target), move(false_target))
  649. {
  650. }
  651. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  652. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  653. };
  654. // NOTE: This instruction is variable-width depending on the number of arguments!
  655. class Call final : public Instruction {
  656. public:
  657. enum class CallType {
  658. Call,
  659. Construct,
  660. DirectEval,
  661. };
  662. Call(CallType type, Register callee, Register this_value, Optional<StringTableIndex> expression_string = {})
  663. : Instruction(Type::Call)
  664. , m_callee(callee)
  665. , m_this_value(this_value)
  666. , m_type(type)
  667. , m_expression_string(expression_string)
  668. {
  669. }
  670. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  671. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  672. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  673. void replace_references_impl(Register, Register);
  674. Completion throw_type_error_for_callee(Bytecode::Interpreter&, StringView callee_type) const;
  675. private:
  676. Register m_callee;
  677. Register m_this_value;
  678. CallType m_type;
  679. Optional<StringTableIndex> m_expression_string;
  680. };
  681. // NOTE: This instruction is variable-width depending on the number of arguments!
  682. class SuperCall : public Instruction {
  683. public:
  684. explicit SuperCall(bool is_synthetic)
  685. : Instruction(Type::SuperCall)
  686. , m_is_synthetic(is_synthetic)
  687. {
  688. }
  689. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  690. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  691. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  692. void replace_references_impl(Register, Register) { }
  693. private:
  694. bool m_is_synthetic;
  695. };
  696. class NewClass final : public Instruction {
  697. public:
  698. explicit NewClass(ClassExpression const& class_expression, Optional<DeprecatedFlyString const&> lhs_name)
  699. : Instruction(Type::NewClass)
  700. , m_class_expression(class_expression)
  701. , m_lhs_name(lhs_name.has_value() ? *lhs_name : Optional<DeprecatedFlyString> {})
  702. {
  703. }
  704. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  705. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  706. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  707. void replace_references_impl(Register, Register) { }
  708. private:
  709. ClassExpression const& m_class_expression;
  710. Optional<DeprecatedFlyString> m_lhs_name;
  711. };
  712. class NewFunction final : public Instruction {
  713. public:
  714. explicit NewFunction(FunctionExpression const& function_node, Optional<DeprecatedFlyString const&> lhs_name, Optional<Register> home_object = {})
  715. : Instruction(Type::NewFunction)
  716. , m_function_node(function_node)
  717. , m_lhs_name(lhs_name.has_value() ? *lhs_name : Optional<DeprecatedFlyString> {})
  718. , m_home_object(move(home_object))
  719. {
  720. }
  721. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  722. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  723. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  724. void replace_references_impl(Register, Register);
  725. private:
  726. FunctionExpression const& m_function_node;
  727. Optional<DeprecatedFlyString> m_lhs_name;
  728. Optional<Register> m_home_object;
  729. };
  730. class BlockDeclarationInstantiation final : public Instruction {
  731. public:
  732. explicit BlockDeclarationInstantiation(ScopeNode const& scope_node)
  733. : Instruction(Type::BlockDeclarationInstantiation)
  734. , m_scope_node(scope_node)
  735. {
  736. }
  737. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  738. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  739. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  740. void replace_references_impl(Register, Register) { }
  741. private:
  742. ScopeNode const& m_scope_node;
  743. };
  744. class Return final : public Instruction {
  745. public:
  746. constexpr static bool IsTerminator = true;
  747. Return()
  748. : Instruction(Type::Return)
  749. {
  750. }
  751. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  752. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  753. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  754. void replace_references_impl(Register, Register) { }
  755. };
  756. class Increment final : public Instruction {
  757. public:
  758. Increment()
  759. : Instruction(Type::Increment)
  760. {
  761. }
  762. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  763. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  764. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  765. void replace_references_impl(Register, Register) { }
  766. };
  767. class Decrement final : public Instruction {
  768. public:
  769. Decrement()
  770. : Instruction(Type::Decrement)
  771. {
  772. }
  773. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  774. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  775. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  776. void replace_references_impl(Register, Register) { }
  777. };
  778. class ToNumeric final : public Instruction {
  779. public:
  780. ToNumeric()
  781. : Instruction(Type::ToNumeric)
  782. {
  783. }
  784. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  785. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  786. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  787. void replace_references_impl(Register, Register) { }
  788. };
  789. class Throw final : public Instruction {
  790. public:
  791. constexpr static bool IsTerminator = true;
  792. Throw()
  793. : Instruction(Type::Throw)
  794. {
  795. }
  796. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  797. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  798. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  799. void replace_references_impl(Register, Register) { }
  800. };
  801. class ThrowIfNotObject final : public Instruction {
  802. public:
  803. ThrowIfNotObject()
  804. : Instruction(Type::ThrowIfNotObject)
  805. {
  806. }
  807. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  808. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  809. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  810. void replace_references_impl(Register, Register) { }
  811. };
  812. class ThrowIfNullish final : public Instruction {
  813. public:
  814. ThrowIfNullish()
  815. : Instruction(Type::ThrowIfNullish)
  816. {
  817. }
  818. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  819. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  820. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  821. void replace_references_impl(Register, Register) { }
  822. };
  823. class EnterUnwindContext final : public Instruction {
  824. public:
  825. constexpr static bool IsTerminator = true;
  826. EnterUnwindContext(Label entry_point, Optional<Label> handler_target, Optional<Label> finalizer_target)
  827. : Instruction(Type::EnterUnwindContext)
  828. , m_entry_point(move(entry_point))
  829. , m_handler_target(move(handler_target))
  830. , m_finalizer_target(move(finalizer_target))
  831. {
  832. }
  833. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  834. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  835. void replace_references_impl(BasicBlock const&, BasicBlock const&);
  836. void replace_references_impl(Register, Register) { }
  837. auto& entry_point() const { return m_entry_point; }
  838. auto& handler_target() const { return m_handler_target; }
  839. auto& finalizer_target() const { return m_finalizer_target; }
  840. private:
  841. Label m_entry_point;
  842. Optional<Label> m_handler_target;
  843. Optional<Label> m_finalizer_target;
  844. };
  845. class ScheduleJump final : public Instruction {
  846. public:
  847. // Note: We use this instruction to tell the next `finally` block to
  848. // continue execution with a specific break/continue target;
  849. // FIXME: We currently don't clear the interpreter internal flag, when we change
  850. // the control-flow (`break`, `continue`) in a finally-block,
  851. // FIXME: .NET on x86_64 uses a call to the finally instead, which could make this
  852. // easier, at the cost of making control-flow changes (`break`, `continue`, `return`)
  853. // in the finally-block more difficult, but as stated above, those
  854. // aren't handled 100% correctly at the moment anyway
  855. // It might be worth investigating a similar mechanism
  856. constexpr static bool IsTerminator = true;
  857. ScheduleJump(Label target)
  858. : Instruction(Type::ScheduleJump)
  859. , m_target(target)
  860. {
  861. }
  862. Label target() const { return m_target; }
  863. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  864. void replace_references_impl(BasicBlock const& from, BasicBlock const& to)
  865. {
  866. if (&m_target.block() == &from)
  867. m_target = Label { to };
  868. }
  869. void replace_references_impl(Register, Register) { }
  870. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  871. private:
  872. Label m_target;
  873. };
  874. class LeaveLexicalEnvironment final : public Instruction {
  875. public:
  876. LeaveLexicalEnvironment()
  877. : Instruction(Type::LeaveLexicalEnvironment)
  878. {
  879. }
  880. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  881. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  882. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  883. void replace_references_impl(Register, Register) { }
  884. };
  885. class LeaveUnwindContext final : public Instruction {
  886. public:
  887. LeaveUnwindContext()
  888. : Instruction(Type::LeaveUnwindContext)
  889. {
  890. }
  891. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  892. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  893. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  894. void replace_references_impl(Register, Register) { }
  895. };
  896. class ContinuePendingUnwind final : public Instruction {
  897. public:
  898. constexpr static bool IsTerminator = true;
  899. explicit ContinuePendingUnwind(Label resume_target)
  900. : Instruction(Type::ContinuePendingUnwind)
  901. , m_resume_target(resume_target)
  902. {
  903. }
  904. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  905. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  906. void replace_references_impl(BasicBlock const&, BasicBlock const&);
  907. void replace_references_impl(Register, Register) { }
  908. auto& resume_target() const { return m_resume_target; }
  909. private:
  910. Label m_resume_target;
  911. };
  912. class Yield final : public Instruction {
  913. public:
  914. constexpr static bool IsTerminator = true;
  915. explicit Yield(Label continuation_label)
  916. : Instruction(Type::Yield)
  917. , m_continuation_label(continuation_label)
  918. {
  919. }
  920. explicit Yield(nullptr_t)
  921. : Instruction(Type::Yield)
  922. {
  923. }
  924. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  925. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  926. void replace_references_impl(BasicBlock const&, BasicBlock const&);
  927. void replace_references_impl(Register, Register) { }
  928. auto& continuation() const { return m_continuation_label; }
  929. private:
  930. Optional<Label> m_continuation_label;
  931. };
  932. class PushDeclarativeEnvironment final : public Instruction {
  933. public:
  934. explicit PushDeclarativeEnvironment(HashMap<u32, Variable> variables)
  935. : Instruction(Type::PushDeclarativeEnvironment)
  936. , m_variables(move(variables))
  937. {
  938. }
  939. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  940. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  941. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  942. void replace_references_impl(Register, Register) { }
  943. private:
  944. HashMap<u32, Variable> m_variables;
  945. };
  946. class GetIterator final : public Instruction {
  947. public:
  948. GetIterator()
  949. : Instruction(Type::GetIterator)
  950. {
  951. }
  952. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  953. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  954. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  955. void replace_references_impl(Register, Register) { }
  956. };
  957. class GetMethod final : public Instruction {
  958. public:
  959. GetMethod(IdentifierTableIndex property)
  960. : Instruction(Type::GetMethod)
  961. , m_property(property)
  962. {
  963. }
  964. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  965. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  966. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  967. void replace_references_impl(Register, Register) { }
  968. private:
  969. IdentifierTableIndex m_property;
  970. };
  971. class GetObjectPropertyIterator final : public Instruction {
  972. public:
  973. GetObjectPropertyIterator()
  974. : Instruction(Type::GetObjectPropertyIterator)
  975. {
  976. }
  977. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  978. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  979. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  980. void replace_references_impl(Register, Register) { }
  981. };
  982. class IteratorClose final : public Instruction {
  983. public:
  984. IteratorClose(Completion::Type completion_type, Optional<Value> completion_value)
  985. : Instruction(Type::IteratorClose)
  986. , m_completion_type(completion_type)
  987. , m_completion_value(completion_value)
  988. {
  989. }
  990. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  991. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  992. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  993. void replace_references_impl(Register, Register) { }
  994. private:
  995. Completion::Type m_completion_type { Completion::Type::Normal };
  996. Optional<Value> m_completion_value;
  997. };
  998. class IteratorNext final : public Instruction {
  999. public:
  1000. IteratorNext()
  1001. : Instruction(Type::IteratorNext)
  1002. {
  1003. }
  1004. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1005. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  1006. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  1007. void replace_references_impl(Register, Register) { }
  1008. };
  1009. class IteratorResultDone final : public Instruction {
  1010. public:
  1011. IteratorResultDone()
  1012. : Instruction(Type::IteratorResultDone)
  1013. {
  1014. }
  1015. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1016. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  1017. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  1018. void replace_references_impl(Register, Register) { }
  1019. };
  1020. class IteratorResultValue final : public Instruction {
  1021. public:
  1022. IteratorResultValue()
  1023. : Instruction(Type::IteratorResultValue)
  1024. {
  1025. }
  1026. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1027. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  1028. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  1029. void replace_references_impl(Register, Register) { }
  1030. };
  1031. class ResolveThisBinding final : public Instruction {
  1032. public:
  1033. explicit ResolveThisBinding()
  1034. : Instruction(Type::ResolveThisBinding)
  1035. {
  1036. }
  1037. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1038. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  1039. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  1040. void replace_references_impl(Register, Register) { }
  1041. };
  1042. class ResolveSuperBase final : public Instruction {
  1043. public:
  1044. explicit ResolveSuperBase()
  1045. : Instruction(Type::ResolveSuperBase)
  1046. {
  1047. }
  1048. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1049. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  1050. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  1051. void replace_references_impl(Register, Register) { }
  1052. };
  1053. class GetNewTarget final : public Instruction {
  1054. public:
  1055. explicit GetNewTarget()
  1056. : Instruction(Type::GetNewTarget)
  1057. {
  1058. }
  1059. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1060. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  1061. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  1062. void replace_references_impl(Register, Register) { }
  1063. };
  1064. class TypeofVariable final : public Instruction {
  1065. public:
  1066. explicit TypeofVariable(IdentifierTableIndex identifier)
  1067. : Instruction(Type::TypeofVariable)
  1068. , m_identifier(identifier)
  1069. {
  1070. }
  1071. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1072. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  1073. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  1074. void replace_references_impl(Register, Register) { }
  1075. private:
  1076. IdentifierTableIndex m_identifier;
  1077. };
  1078. }
  1079. namespace JS::Bytecode {
  1080. ALWAYS_INLINE ThrowCompletionOr<void> Instruction::execute(Bytecode::Interpreter& interpreter) const
  1081. {
  1082. #define __BYTECODE_OP(op) \
  1083. case Instruction::Type::op: \
  1084. return static_cast<Bytecode::Op::op const&>(*this).execute_impl(interpreter);
  1085. switch (type()) {
  1086. ENUMERATE_BYTECODE_OPS(__BYTECODE_OP)
  1087. default:
  1088. VERIFY_NOT_REACHED();
  1089. }
  1090. #undef __BYTECODE_OP
  1091. }
  1092. ALWAYS_INLINE void Instruction::replace_references(BasicBlock const& from, BasicBlock const& to)
  1093. {
  1094. #define __BYTECODE_OP(op) \
  1095. case Instruction::Type::op: \
  1096. return static_cast<Bytecode::Op::op&>(*this).replace_references_impl(from, to);
  1097. switch (type()) {
  1098. ENUMERATE_BYTECODE_OPS(__BYTECODE_OP)
  1099. default:
  1100. VERIFY_NOT_REACHED();
  1101. }
  1102. #undef __BYTECODE_OP
  1103. }
  1104. ALWAYS_INLINE void Instruction::replace_references(Register from, Register to)
  1105. {
  1106. #define __BYTECODE_OP(op) \
  1107. case Instruction::Type::op: \
  1108. return static_cast<Bytecode::Op::op&>(*this).replace_references_impl(from, to);
  1109. switch (type()) {
  1110. ENUMERATE_BYTECODE_OPS(__BYTECODE_OP)
  1111. default:
  1112. VERIFY_NOT_REACHED();
  1113. }
  1114. #undef __BYTECODE_OP
  1115. }
  1116. ALWAYS_INLINE size_t Instruction::length() const
  1117. {
  1118. if (type() == Type::NewArray)
  1119. return round_up_to_power_of_two(static_cast<Op::NewArray const&>(*this).length_impl(), alignof(void*));
  1120. if (type() == Type::CopyObjectExcludingProperties)
  1121. return round_up_to_power_of_two(static_cast<Op::CopyObjectExcludingProperties const&>(*this).length_impl(), alignof(void*));
  1122. #define __BYTECODE_OP(op) \
  1123. case Type::op: \
  1124. return sizeof(Op::op);
  1125. switch (type()) {
  1126. ENUMERATE_BYTECODE_OPS(__BYTECODE_OP)
  1127. default:
  1128. VERIFY_NOT_REACHED();
  1129. }
  1130. #undef __BYTECODE_OP
  1131. }
  1132. ALWAYS_INLINE bool Instruction::is_terminator() const
  1133. {
  1134. #define __BYTECODE_OP(op) \
  1135. case Type::op: \
  1136. return Op::op::IsTerminator;
  1137. switch (type()) {
  1138. ENUMERATE_BYTECODE_OPS(__BYTECODE_OP)
  1139. default:
  1140. VERIFY_NOT_REACHED();
  1141. }
  1142. #undef __BYTECODE_OP
  1143. }
  1144. }