Op.h 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820
  1. /*
  2. * Copyright (c) 2021, 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 <LibCrypto/BigInt/SignedBigInteger.h>
  10. #include <LibJS/Bytecode/IdentifierTable.h>
  11. #include <LibJS/Bytecode/Instruction.h>
  12. #include <LibJS/Bytecode/Label.h>
  13. #include <LibJS/Bytecode/Register.h>
  14. #include <LibJS/Bytecode/StringTable.h>
  15. #include <LibJS/Heap/Cell.h>
  16. #include <LibJS/Runtime/Environment.h>
  17. #include <LibJS/Runtime/EnvironmentCoordinate.h>
  18. #include <LibJS/Runtime/Value.h>
  19. #include <LibJS/Runtime/ValueTraits.h>
  20. namespace JS::Bytecode::Op {
  21. class Load final : public Instruction {
  22. public:
  23. explicit Load(Register src)
  24. : Instruction(Type::Load)
  25. , m_src(src)
  26. {
  27. }
  28. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  29. String to_string_impl(Bytecode::Executable const&) const;
  30. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  31. private:
  32. Register m_src;
  33. };
  34. class LoadImmediate final : public Instruction {
  35. public:
  36. explicit LoadImmediate(Value value)
  37. : Instruction(Type::LoadImmediate)
  38. , m_value(value)
  39. {
  40. }
  41. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  42. String to_string_impl(Bytecode::Executable const&) const;
  43. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  44. private:
  45. Value m_value;
  46. };
  47. class Store final : public Instruction {
  48. public:
  49. explicit Store(Register dst)
  50. : Instruction(Type::Store)
  51. , m_dst(dst)
  52. {
  53. }
  54. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  55. String to_string_impl(Bytecode::Executable const&) const;
  56. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  57. private:
  58. Register m_dst;
  59. };
  60. #define JS_ENUMERATE_COMMON_BINARY_OPS(O) \
  61. O(Add, add) \
  62. O(Sub, sub) \
  63. O(Mul, mul) \
  64. O(Div, div) \
  65. O(Exp, exp) \
  66. O(Mod, mod) \
  67. O(In, in) \
  68. O(InstanceOf, instance_of) \
  69. O(GreaterThan, greater_than) \
  70. O(GreaterThanEquals, greater_than_equals) \
  71. O(LessThan, less_than) \
  72. O(LessThanEquals, less_than_equals) \
  73. O(LooselyInequals, abstract_inequals) \
  74. O(LooselyEquals, abstract_equals) \
  75. O(StrictlyInequals, typed_inequals) \
  76. O(StrictlyEquals, typed_equals) \
  77. O(BitwiseAnd, bitwise_and) \
  78. O(BitwiseOr, bitwise_or) \
  79. O(BitwiseXor, bitwise_xor) \
  80. O(LeftShift, left_shift) \
  81. O(RightShift, right_shift) \
  82. O(UnsignedRightShift, unsigned_right_shift)
  83. #define JS_DECLARE_COMMON_BINARY_OP(OpTitleCase, op_snake_case) \
  84. class OpTitleCase final : public Instruction { \
  85. public: \
  86. explicit OpTitleCase(Register lhs_reg) \
  87. : Instruction(Type::OpTitleCase) \
  88. , m_lhs_reg(lhs_reg) \
  89. { \
  90. } \
  91. \
  92. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const; \
  93. String to_string_impl(Bytecode::Executable const&) const; \
  94. void replace_references_impl(BasicBlock const&, BasicBlock const&) { } \
  95. \
  96. private: \
  97. Register m_lhs_reg; \
  98. };
  99. JS_ENUMERATE_COMMON_BINARY_OPS(JS_DECLARE_COMMON_BINARY_OP)
  100. #undef JS_DECLARE_COMMON_BINARY_OP
  101. #define JS_ENUMERATE_COMMON_UNARY_OPS(O) \
  102. O(BitwiseNot, bitwise_not) \
  103. O(Not, not_) \
  104. O(UnaryPlus, unary_plus) \
  105. O(UnaryMinus, unary_minus) \
  106. O(Typeof, typeof_)
  107. #define JS_DECLARE_COMMON_UNARY_OP(OpTitleCase, op_snake_case) \
  108. class OpTitleCase final : public Instruction { \
  109. public: \
  110. OpTitleCase() \
  111. : Instruction(Type::OpTitleCase) \
  112. { \
  113. } \
  114. \
  115. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const; \
  116. String to_string_impl(Bytecode::Executable const&) const; \
  117. void replace_references_impl(BasicBlock const&, BasicBlock const&) { } \
  118. };
  119. JS_ENUMERATE_COMMON_UNARY_OPS(JS_DECLARE_COMMON_UNARY_OP)
  120. #undef JS_DECLARE_COMMON_UNARY_OP
  121. class NewString final : public Instruction {
  122. public:
  123. explicit NewString(StringTableIndex string)
  124. : Instruction(Type::NewString)
  125. , m_string(string)
  126. {
  127. }
  128. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  129. String to_string_impl(Bytecode::Executable const&) const;
  130. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  131. private:
  132. StringTableIndex m_string;
  133. };
  134. class NewObject final : public Instruction {
  135. public:
  136. NewObject()
  137. : Instruction(Type::NewObject)
  138. {
  139. }
  140. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  141. String to_string_impl(Bytecode::Executable const&) const;
  142. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  143. };
  144. class NewRegExp final : public Instruction {
  145. public:
  146. NewRegExp(StringTableIndex source_index, StringTableIndex flags_index)
  147. : Instruction(Type::NewRegExp)
  148. , m_source_index(source_index)
  149. , m_flags_index(flags_index)
  150. {
  151. }
  152. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  153. String to_string_impl(Bytecode::Executable const&) const;
  154. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  155. private:
  156. StringTableIndex m_source_index;
  157. StringTableIndex m_flags_index;
  158. };
  159. // NOTE: This instruction is variable-width depending on the number of excluded names
  160. class CopyObjectExcludingProperties final : public Instruction {
  161. public:
  162. CopyObjectExcludingProperties(Register from_object, Vector<Register> const& excluded_names)
  163. : Instruction(Type::CopyObjectExcludingProperties)
  164. , m_from_object(from_object)
  165. , m_excluded_names_count(excluded_names.size())
  166. {
  167. for (size_t i = 0; i < m_excluded_names_count; i++)
  168. m_excluded_names[i] = excluded_names[i];
  169. }
  170. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  171. String to_string_impl(Bytecode::Executable const&) const;
  172. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  173. size_t length_impl() const { return sizeof(*this) + sizeof(Register) * m_excluded_names_count; }
  174. private:
  175. Register m_from_object;
  176. size_t m_excluded_names_count { 0 };
  177. Register m_excluded_names[];
  178. };
  179. class NewBigInt final : public Instruction {
  180. public:
  181. explicit NewBigInt(Crypto::SignedBigInteger bigint)
  182. : Instruction(Type::NewBigInt)
  183. , m_bigint(move(bigint))
  184. {
  185. }
  186. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  187. String to_string_impl(Bytecode::Executable const&) const;
  188. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  189. private:
  190. Crypto::SignedBigInteger m_bigint;
  191. };
  192. // NOTE: This instruction is variable-width depending on the number of elements!
  193. class NewArray final : public Instruction {
  194. public:
  195. NewArray()
  196. : Instruction(Type::NewArray)
  197. , m_element_count(0)
  198. {
  199. }
  200. explicit NewArray(Vector<Register> const& elements)
  201. : Instruction(Type::NewArray)
  202. , m_element_count(elements.size())
  203. {
  204. for (size_t i = 0; i < m_element_count; ++i)
  205. m_elements[i] = elements[i];
  206. }
  207. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  208. String to_string_impl(Bytecode::Executable const&) const;
  209. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  210. size_t length_impl() const
  211. {
  212. return sizeof(*this) + sizeof(Register) * m_element_count;
  213. }
  214. private:
  215. size_t m_element_count { 0 };
  216. Register m_elements[];
  217. };
  218. class IteratorToArray final : public Instruction {
  219. public:
  220. IteratorToArray()
  221. : Instruction(Type::IteratorToArray)
  222. {
  223. }
  224. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  225. String to_string_impl(Bytecode::Executable const&) const;
  226. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  227. };
  228. class ConcatString final : public Instruction {
  229. public:
  230. explicit ConcatString(Register lhs)
  231. : Instruction(Type::ConcatString)
  232. , m_lhs(lhs)
  233. {
  234. }
  235. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  236. String to_string_impl(Bytecode::Executable const&) const;
  237. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  238. private:
  239. Register m_lhs;
  240. };
  241. class SetVariable final : public Instruction {
  242. public:
  243. explicit SetVariable(IdentifierTableIndex identifier)
  244. : Instruction(Type::SetVariable)
  245. , m_identifier(identifier)
  246. {
  247. }
  248. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  249. String to_string_impl(Bytecode::Executable const&) const;
  250. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  251. private:
  252. IdentifierTableIndex m_identifier;
  253. };
  254. class GetVariable final : public Instruction {
  255. public:
  256. explicit GetVariable(IdentifierTableIndex identifier)
  257. : Instruction(Type::GetVariable)
  258. , m_identifier(identifier)
  259. {
  260. }
  261. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  262. String to_string_impl(Bytecode::Executable const&) const;
  263. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  264. private:
  265. IdentifierTableIndex m_identifier;
  266. Optional<EnvironmentCoordinate> mutable m_cached_environment_coordinate;
  267. };
  268. class GetById final : public Instruction {
  269. public:
  270. explicit GetById(IdentifierTableIndex property)
  271. : Instruction(Type::GetById)
  272. , m_property(property)
  273. {
  274. }
  275. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  276. String to_string_impl(Bytecode::Executable const&) const;
  277. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  278. private:
  279. IdentifierTableIndex m_property;
  280. };
  281. class PutById final : public Instruction {
  282. public:
  283. explicit PutById(Register base, IdentifierTableIndex property)
  284. : Instruction(Type::PutById)
  285. , m_base(base)
  286. , m_property(property)
  287. {
  288. }
  289. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  290. String to_string_impl(Bytecode::Executable const&) const;
  291. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  292. private:
  293. Register m_base;
  294. IdentifierTableIndex m_property;
  295. };
  296. class GetByValue final : public Instruction {
  297. public:
  298. explicit GetByValue(Register base)
  299. : Instruction(Type::GetByValue)
  300. , m_base(base)
  301. {
  302. }
  303. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  304. String to_string_impl(Bytecode::Executable const&) const;
  305. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  306. private:
  307. Register m_base;
  308. };
  309. class PutByValue final : public Instruction {
  310. public:
  311. PutByValue(Register base, Register property)
  312. : Instruction(Type::PutByValue)
  313. , m_base(base)
  314. , m_property(property)
  315. {
  316. }
  317. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  318. String to_string_impl(Bytecode::Executable const&) const;
  319. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  320. private:
  321. Register m_base;
  322. Register m_property;
  323. };
  324. class Jump : public Instruction {
  325. public:
  326. constexpr static bool IsTerminator = true;
  327. explicit Jump(Type type, Optional<Label> taken_target = {}, Optional<Label> nontaken_target = {})
  328. : Instruction(type)
  329. , m_true_target(move(taken_target))
  330. , m_false_target(move(nontaken_target))
  331. {
  332. }
  333. explicit Jump(Optional<Label> taken_target = {}, Optional<Label> nontaken_target = {})
  334. : Instruction(Type::Jump)
  335. , m_true_target(move(taken_target))
  336. , m_false_target(move(nontaken_target))
  337. {
  338. }
  339. void set_targets(Optional<Label> true_target, Optional<Label> false_target)
  340. {
  341. m_true_target = move(true_target);
  342. m_false_target = move(false_target);
  343. }
  344. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  345. String to_string_impl(Bytecode::Executable const&) const;
  346. void replace_references_impl(BasicBlock const&, BasicBlock const&);
  347. auto& true_target() const { return m_true_target; }
  348. auto& false_target() const { return m_false_target; }
  349. protected:
  350. Optional<Label> m_true_target;
  351. Optional<Label> m_false_target;
  352. };
  353. class JumpConditional final : public Jump {
  354. public:
  355. explicit JumpConditional(Optional<Label> true_target = {}, Optional<Label> false_target = {})
  356. : Jump(Type::JumpConditional, move(true_target), move(false_target))
  357. {
  358. }
  359. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  360. String to_string_impl(Bytecode::Executable const&) const;
  361. };
  362. class JumpNullish final : public Jump {
  363. public:
  364. explicit JumpNullish(Optional<Label> true_target = {}, Optional<Label> false_target = {})
  365. : Jump(Type::JumpNullish, move(true_target), move(false_target))
  366. {
  367. }
  368. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  369. String to_string_impl(Bytecode::Executable const&) const;
  370. };
  371. class JumpUndefined final : public Jump {
  372. public:
  373. explicit JumpUndefined(Optional<Label> true_target = {}, Optional<Label> false_target = {})
  374. : Jump(Type::JumpUndefined, move(true_target), move(false_target))
  375. {
  376. }
  377. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  378. String to_string_impl(Bytecode::Executable const&) const;
  379. };
  380. // NOTE: This instruction is variable-width depending on the number of arguments!
  381. class Call final : public Instruction {
  382. public:
  383. enum class CallType {
  384. Call,
  385. Construct,
  386. };
  387. Call(CallType type, Register callee, Register this_value, Vector<Register> const& arguments)
  388. : Instruction(Type::Call)
  389. , m_callee(callee)
  390. , m_this_value(this_value)
  391. , m_type(type)
  392. , m_argument_count(arguments.size())
  393. {
  394. for (size_t i = 0; i < m_argument_count; ++i)
  395. m_arguments[i] = arguments[i];
  396. }
  397. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  398. String to_string_impl(Bytecode::Executable const&) const;
  399. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  400. size_t length_impl() const
  401. {
  402. return sizeof(*this) + sizeof(Register) * m_argument_count;
  403. }
  404. private:
  405. Register m_callee;
  406. Register m_this_value;
  407. CallType m_type;
  408. size_t m_argument_count { 0 };
  409. Register m_arguments[];
  410. };
  411. class NewClass final : public Instruction {
  412. public:
  413. explicit NewClass(ClassExpression const& class_expression)
  414. : Instruction(Type::NewClass)
  415. , m_class_expression(class_expression)
  416. {
  417. }
  418. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  419. String to_string_impl(Bytecode::Executable const&) const;
  420. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  421. private:
  422. ClassExpression const& m_class_expression;
  423. };
  424. class NewFunction final : public Instruction {
  425. public:
  426. explicit NewFunction(FunctionNode const& function_node)
  427. : Instruction(Type::NewFunction)
  428. , m_function_node(function_node)
  429. {
  430. }
  431. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  432. String to_string_impl(Bytecode::Executable const&) const;
  433. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  434. private:
  435. FunctionNode const& m_function_node;
  436. };
  437. class Return final : public Instruction {
  438. public:
  439. constexpr static bool IsTerminator = true;
  440. Return()
  441. : Instruction(Type::Return)
  442. {
  443. }
  444. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  445. String to_string_impl(Bytecode::Executable const&) const;
  446. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  447. };
  448. class Increment final : public Instruction {
  449. public:
  450. Increment()
  451. : Instruction(Type::Increment)
  452. {
  453. }
  454. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  455. String to_string_impl(Bytecode::Executable const&) const;
  456. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  457. };
  458. class Decrement final : public Instruction {
  459. public:
  460. Decrement()
  461. : Instruction(Type::Decrement)
  462. {
  463. }
  464. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  465. String to_string_impl(Bytecode::Executable const&) const;
  466. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  467. };
  468. class Throw final : public Instruction {
  469. public:
  470. constexpr static bool IsTerminator = true;
  471. Throw()
  472. : Instruction(Type::Throw)
  473. {
  474. }
  475. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  476. String to_string_impl(Bytecode::Executable const&) const;
  477. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  478. };
  479. class EnterUnwindContext final : public Instruction {
  480. public:
  481. constexpr static bool IsTerminator = true;
  482. EnterUnwindContext(Label entry_point, Optional<Label> handler_target, Optional<Label> finalizer_target)
  483. : Instruction(Type::EnterUnwindContext)
  484. , m_entry_point(move(entry_point))
  485. , m_handler_target(move(handler_target))
  486. , m_finalizer_target(move(finalizer_target))
  487. {
  488. }
  489. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  490. String to_string_impl(Bytecode::Executable const&) const;
  491. void replace_references_impl(BasicBlock const&, BasicBlock const&);
  492. auto& entry_point() const { return m_entry_point; }
  493. auto& handler_target() const { return m_handler_target; }
  494. auto& finalizer_target() const { return m_finalizer_target; }
  495. private:
  496. Label m_entry_point;
  497. Optional<Label> m_handler_target;
  498. Optional<Label> m_finalizer_target;
  499. };
  500. class LeaveUnwindContext final : public Instruction {
  501. public:
  502. LeaveUnwindContext()
  503. : Instruction(Type::LeaveUnwindContext)
  504. {
  505. }
  506. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  507. String to_string_impl(Bytecode::Executable const&) const;
  508. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  509. };
  510. class FinishUnwind final : public Instruction {
  511. public:
  512. FinishUnwind(Label next)
  513. : Instruction(Type::FinishUnwind)
  514. , m_next_target(move(next))
  515. {
  516. }
  517. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  518. String to_string_impl(Bytecode::Executable const&) const;
  519. void replace_references_impl(BasicBlock const&, BasicBlock const&);
  520. private:
  521. Label m_next_target;
  522. };
  523. class ContinuePendingUnwind final : public Instruction {
  524. public:
  525. constexpr static bool IsTerminator = true;
  526. explicit ContinuePendingUnwind(Label resume_target)
  527. : Instruction(Type::ContinuePendingUnwind)
  528. , m_resume_target(resume_target)
  529. {
  530. }
  531. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  532. String to_string_impl(Bytecode::Executable const&) const;
  533. void replace_references_impl(BasicBlock const&, BasicBlock const&);
  534. auto& resume_target() const { return m_resume_target; }
  535. private:
  536. Label m_resume_target;
  537. };
  538. class Yield final : public Instruction {
  539. public:
  540. constexpr static bool IsTerminator = true;
  541. explicit Yield(Label continuation_label)
  542. : Instruction(Type::Yield)
  543. , m_continuation_label(continuation_label)
  544. {
  545. }
  546. explicit Yield(std::nullptr_t)
  547. : Instruction(Type::Yield)
  548. {
  549. }
  550. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  551. String to_string_impl(Bytecode::Executable const&) const;
  552. void replace_references_impl(BasicBlock const&, BasicBlock const&);
  553. auto& continuation() const { return m_continuation_label; }
  554. private:
  555. Optional<Label> m_continuation_label;
  556. };
  557. class PushDeclarativeEnvironment final : public Instruction {
  558. public:
  559. explicit PushDeclarativeEnvironment(HashMap<u32, Variable> variables)
  560. : Instruction(Type::PushDeclarativeEnvironment)
  561. , m_variables(move(variables))
  562. {
  563. }
  564. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  565. String to_string_impl(Bytecode::Executable const&) const;
  566. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  567. private:
  568. HashMap<u32, Variable> m_variables;
  569. };
  570. class GetIterator final : public Instruction {
  571. public:
  572. GetIterator()
  573. : Instruction(Type::GetIterator)
  574. {
  575. }
  576. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  577. String to_string_impl(Bytecode::Executable const&) const;
  578. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  579. };
  580. class IteratorNext final : public Instruction {
  581. public:
  582. IteratorNext()
  583. : Instruction(Type::IteratorNext)
  584. {
  585. }
  586. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  587. String to_string_impl(Bytecode::Executable const&) const;
  588. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  589. };
  590. class IteratorResultDone final : public Instruction {
  591. public:
  592. IteratorResultDone()
  593. : Instruction(Type::IteratorResultDone)
  594. {
  595. }
  596. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  597. String to_string_impl(Bytecode::Executable const&) const;
  598. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  599. };
  600. class IteratorResultValue final : public Instruction {
  601. public:
  602. IteratorResultValue()
  603. : Instruction(Type::IteratorResultValue)
  604. {
  605. }
  606. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  607. String to_string_impl(Bytecode::Executable const&) const;
  608. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  609. };
  610. class ResolveThisBinding final : public Instruction {
  611. public:
  612. explicit ResolveThisBinding()
  613. : Instruction(Type::ResolveThisBinding)
  614. {
  615. }
  616. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  617. String to_string_impl(Bytecode::Executable const&) const;
  618. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  619. };
  620. }
  621. namespace JS::Bytecode {
  622. ALWAYS_INLINE ThrowCompletionOr<void> Instruction::execute(Bytecode::Interpreter& interpreter) const
  623. {
  624. #define __BYTECODE_OP(op) \
  625. case Instruction::Type::op: \
  626. return static_cast<Bytecode::Op::op const&>(*this).execute_impl(interpreter);
  627. switch (type()) {
  628. ENUMERATE_BYTECODE_OPS(__BYTECODE_OP)
  629. default:
  630. VERIFY_NOT_REACHED();
  631. }
  632. #undef __BYTECODE_OP
  633. }
  634. ALWAYS_INLINE void Instruction::replace_references(BasicBlock const& from, BasicBlock const& to)
  635. {
  636. #define __BYTECODE_OP(op) \
  637. case Instruction::Type::op: \
  638. return static_cast<Bytecode::Op::op&>(*this).replace_references_impl(from, to);
  639. switch (type()) {
  640. ENUMERATE_BYTECODE_OPS(__BYTECODE_OP)
  641. default:
  642. VERIFY_NOT_REACHED();
  643. }
  644. #undef __BYTECODE_OP
  645. }
  646. ALWAYS_INLINE size_t Instruction::length() const
  647. {
  648. if (type() == Type::Call)
  649. return static_cast<Op::Call const&>(*this).length_impl();
  650. else if (type() == Type::NewArray)
  651. return static_cast<Op::NewArray const&>(*this).length_impl();
  652. else if (type() == Type::CopyObjectExcludingProperties)
  653. return static_cast<Op::CopyObjectExcludingProperties const&>(*this).length_impl();
  654. #define __BYTECODE_OP(op) \
  655. case Type::op: \
  656. return sizeof(Op::op);
  657. switch (type()) {
  658. ENUMERATE_BYTECODE_OPS(__BYTECODE_OP)
  659. default:
  660. VERIFY_NOT_REACHED();
  661. }
  662. #undef __BYTECODE_OP
  663. }
  664. ALWAYS_INLINE bool Instruction::is_terminator() const
  665. {
  666. #define __BYTECODE_OP(op) \
  667. case Type::op: \
  668. return Op::op::IsTerminator;
  669. switch (type()) {
  670. ENUMERATE_BYTECODE_OPS(__BYTECODE_OP)
  671. default:
  672. VERIFY_NOT_REACHED();
  673. }
  674. #undef __BYTECODE_OP
  675. }
  676. }