Op.h 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898
  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. enum class EnvironmentMode {
  242. Lexical,
  243. Var,
  244. };
  245. class CreateEnvironment final : public Instruction {
  246. public:
  247. explicit CreateEnvironment(EnvironmentMode mode)
  248. : Instruction(Type::CreateEnvironment)
  249. , m_mode(mode)
  250. {
  251. }
  252. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  253. String to_string_impl(Bytecode::Executable const&) const;
  254. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  255. private:
  256. EnvironmentMode m_mode { EnvironmentMode::Lexical };
  257. };
  258. class EnterObjectEnvironment final : public Instruction {
  259. public:
  260. explicit EnterObjectEnvironment()
  261. : Instruction(Type::EnterObjectEnvironment)
  262. {
  263. }
  264. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  265. String to_string_impl(Bytecode::Executable const&) const;
  266. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  267. };
  268. class CreateVariable final : public Instruction {
  269. public:
  270. explicit CreateVariable(IdentifierTableIndex identifier, EnvironmentMode mode, bool is_immutable)
  271. : Instruction(Type::CreateVariable)
  272. , m_identifier(identifier)
  273. , m_mode(mode)
  274. , m_is_immutable(is_immutable)
  275. {
  276. }
  277. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  278. String to_string_impl(Bytecode::Executable const&) const;
  279. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  280. private:
  281. IdentifierTableIndex m_identifier;
  282. EnvironmentMode m_mode;
  283. bool m_is_immutable { false };
  284. };
  285. class SetVariable final : public Instruction {
  286. public:
  287. enum class InitializationMode {
  288. Initialize,
  289. Set,
  290. InitializeOrSet,
  291. };
  292. explicit SetVariable(IdentifierTableIndex identifier, InitializationMode initialization_mode = InitializationMode::Set, EnvironmentMode mode = EnvironmentMode::Lexical)
  293. : Instruction(Type::SetVariable)
  294. , m_identifier(identifier)
  295. , m_mode(mode)
  296. , m_initialization_mode(initialization_mode)
  297. {
  298. }
  299. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  300. String to_string_impl(Bytecode::Executable const&) const;
  301. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  302. private:
  303. IdentifierTableIndex m_identifier;
  304. EnvironmentMode m_mode;
  305. InitializationMode m_initialization_mode { InitializationMode::Set };
  306. };
  307. class GetVariable final : public Instruction {
  308. public:
  309. explicit GetVariable(IdentifierTableIndex identifier)
  310. : Instruction(Type::GetVariable)
  311. , m_identifier(identifier)
  312. {
  313. }
  314. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  315. String to_string_impl(Bytecode::Executable const&) const;
  316. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  317. private:
  318. IdentifierTableIndex m_identifier;
  319. Optional<EnvironmentCoordinate> mutable m_cached_environment_coordinate;
  320. };
  321. class GetById final : public Instruction {
  322. public:
  323. explicit GetById(IdentifierTableIndex property)
  324. : Instruction(Type::GetById)
  325. , m_property(property)
  326. {
  327. }
  328. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  329. String to_string_impl(Bytecode::Executable const&) const;
  330. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  331. private:
  332. IdentifierTableIndex m_property;
  333. };
  334. class PutById final : public Instruction {
  335. public:
  336. explicit PutById(Register base, IdentifierTableIndex property)
  337. : Instruction(Type::PutById)
  338. , m_base(base)
  339. , m_property(property)
  340. {
  341. }
  342. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  343. String to_string_impl(Bytecode::Executable const&) const;
  344. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  345. private:
  346. Register m_base;
  347. IdentifierTableIndex m_property;
  348. };
  349. class GetByValue final : public Instruction {
  350. public:
  351. explicit GetByValue(Register base)
  352. : Instruction(Type::GetByValue)
  353. , m_base(base)
  354. {
  355. }
  356. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  357. String to_string_impl(Bytecode::Executable const&) const;
  358. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  359. private:
  360. Register m_base;
  361. };
  362. class PutByValue final : public Instruction {
  363. public:
  364. PutByValue(Register base, Register property)
  365. : Instruction(Type::PutByValue)
  366. , m_base(base)
  367. , m_property(property)
  368. {
  369. }
  370. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  371. String to_string_impl(Bytecode::Executable const&) const;
  372. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  373. private:
  374. Register m_base;
  375. Register m_property;
  376. };
  377. class Jump : public Instruction {
  378. public:
  379. constexpr static bool IsTerminator = true;
  380. explicit Jump(Type type, Optional<Label> taken_target = {}, Optional<Label> nontaken_target = {})
  381. : Instruction(type)
  382. , m_true_target(move(taken_target))
  383. , m_false_target(move(nontaken_target))
  384. {
  385. }
  386. explicit Jump(Optional<Label> taken_target = {}, Optional<Label> nontaken_target = {})
  387. : Instruction(Type::Jump)
  388. , m_true_target(move(taken_target))
  389. , m_false_target(move(nontaken_target))
  390. {
  391. }
  392. void set_targets(Optional<Label> true_target, Optional<Label> false_target)
  393. {
  394. m_true_target = move(true_target);
  395. m_false_target = move(false_target);
  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. auto& true_target() const { return m_true_target; }
  401. auto& false_target() const { return m_false_target; }
  402. protected:
  403. Optional<Label> m_true_target;
  404. Optional<Label> m_false_target;
  405. };
  406. class JumpConditional final : public Jump {
  407. public:
  408. explicit JumpConditional(Optional<Label> true_target = {}, Optional<Label> false_target = {})
  409. : Jump(Type::JumpConditional, move(true_target), move(false_target))
  410. {
  411. }
  412. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  413. String to_string_impl(Bytecode::Executable const&) const;
  414. };
  415. class JumpNullish final : public Jump {
  416. public:
  417. explicit JumpNullish(Optional<Label> true_target = {}, Optional<Label> false_target = {})
  418. : Jump(Type::JumpNullish, move(true_target), move(false_target))
  419. {
  420. }
  421. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  422. String to_string_impl(Bytecode::Executable const&) const;
  423. };
  424. class JumpUndefined final : public Jump {
  425. public:
  426. explicit JumpUndefined(Optional<Label> true_target = {}, Optional<Label> false_target = {})
  427. : Jump(Type::JumpUndefined, move(true_target), move(false_target))
  428. {
  429. }
  430. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  431. String to_string_impl(Bytecode::Executable const&) const;
  432. };
  433. // NOTE: This instruction is variable-width depending on the number of arguments!
  434. class Call final : public Instruction {
  435. public:
  436. enum class CallType {
  437. Call,
  438. Construct,
  439. };
  440. Call(CallType type, Register callee, Register this_value, Vector<Register> const& arguments)
  441. : Instruction(Type::Call)
  442. , m_callee(callee)
  443. , m_this_value(this_value)
  444. , m_type(type)
  445. , m_argument_count(arguments.size())
  446. {
  447. for (size_t i = 0; i < m_argument_count; ++i)
  448. m_arguments[i] = arguments[i];
  449. }
  450. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  451. String to_string_impl(Bytecode::Executable const&) const;
  452. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  453. size_t length_impl() const
  454. {
  455. return sizeof(*this) + sizeof(Register) * m_argument_count;
  456. }
  457. private:
  458. Register m_callee;
  459. Register m_this_value;
  460. CallType m_type;
  461. size_t m_argument_count { 0 };
  462. Register m_arguments[];
  463. };
  464. class NewClass final : public Instruction {
  465. public:
  466. explicit NewClass(ClassExpression const& class_expression)
  467. : Instruction(Type::NewClass)
  468. , m_class_expression(class_expression)
  469. {
  470. }
  471. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  472. String to_string_impl(Bytecode::Executable const&) const;
  473. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  474. private:
  475. ClassExpression const& m_class_expression;
  476. };
  477. class NewFunction final : public Instruction {
  478. public:
  479. explicit NewFunction(FunctionNode const& function_node)
  480. : Instruction(Type::NewFunction)
  481. , m_function_node(function_node)
  482. {
  483. }
  484. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  485. String to_string_impl(Bytecode::Executable const&) const;
  486. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  487. private:
  488. FunctionNode const& m_function_node;
  489. };
  490. class Return final : public Instruction {
  491. public:
  492. constexpr static bool IsTerminator = true;
  493. Return()
  494. : Instruction(Type::Return)
  495. {
  496. }
  497. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  498. String to_string_impl(Bytecode::Executable const&) const;
  499. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  500. };
  501. class Increment final : public Instruction {
  502. public:
  503. Increment()
  504. : Instruction(Type::Increment)
  505. {
  506. }
  507. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  508. String to_string_impl(Bytecode::Executable const&) const;
  509. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  510. };
  511. class Decrement final : public Instruction {
  512. public:
  513. Decrement()
  514. : Instruction(Type::Decrement)
  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. };
  521. class Throw final : public Instruction {
  522. public:
  523. constexpr static bool IsTerminator = true;
  524. Throw()
  525. : Instruction(Type::Throw)
  526. {
  527. }
  528. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  529. String to_string_impl(Bytecode::Executable const&) const;
  530. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  531. };
  532. class EnterUnwindContext final : public Instruction {
  533. public:
  534. constexpr static bool IsTerminator = true;
  535. EnterUnwindContext(Label entry_point, Optional<Label> handler_target, Optional<Label> finalizer_target)
  536. : Instruction(Type::EnterUnwindContext)
  537. , m_entry_point(move(entry_point))
  538. , m_handler_target(move(handler_target))
  539. , m_finalizer_target(move(finalizer_target))
  540. {
  541. }
  542. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  543. String to_string_impl(Bytecode::Executable const&) const;
  544. void replace_references_impl(BasicBlock const&, BasicBlock const&);
  545. auto& entry_point() const { return m_entry_point; }
  546. auto& handler_target() const { return m_handler_target; }
  547. auto& finalizer_target() const { return m_finalizer_target; }
  548. private:
  549. Label m_entry_point;
  550. Optional<Label> m_handler_target;
  551. Optional<Label> m_finalizer_target;
  552. };
  553. class LeaveEnvironment final : public Instruction {
  554. public:
  555. LeaveEnvironment(EnvironmentMode mode)
  556. : Instruction(Type::LeaveEnvironment)
  557. , m_mode(mode)
  558. {
  559. }
  560. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  561. String to_string_impl(Bytecode::Executable const&) const;
  562. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  563. private:
  564. EnvironmentMode m_mode { EnvironmentMode::Lexical };
  565. };
  566. class LeaveUnwindContext final : public Instruction {
  567. public:
  568. LeaveUnwindContext()
  569. : Instruction(Type::LeaveUnwindContext)
  570. {
  571. }
  572. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  573. String to_string_impl(Bytecode::Executable const&) const;
  574. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  575. };
  576. class FinishUnwind final : public Instruction {
  577. public:
  578. FinishUnwind(Label next)
  579. : Instruction(Type::FinishUnwind)
  580. , m_next_target(move(next))
  581. {
  582. }
  583. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  584. String to_string_impl(Bytecode::Executable const&) const;
  585. void replace_references_impl(BasicBlock const&, BasicBlock const&);
  586. private:
  587. Label m_next_target;
  588. };
  589. class ContinuePendingUnwind final : public Instruction {
  590. public:
  591. constexpr static bool IsTerminator = true;
  592. explicit ContinuePendingUnwind(Label resume_target)
  593. : Instruction(Type::ContinuePendingUnwind)
  594. , m_resume_target(resume_target)
  595. {
  596. }
  597. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  598. String to_string_impl(Bytecode::Executable const&) const;
  599. void replace_references_impl(BasicBlock const&, BasicBlock const&);
  600. auto& resume_target() const { return m_resume_target; }
  601. private:
  602. Label m_resume_target;
  603. };
  604. class Yield final : public Instruction {
  605. public:
  606. constexpr static bool IsTerminator = true;
  607. explicit Yield(Label continuation_label)
  608. : Instruction(Type::Yield)
  609. , m_continuation_label(continuation_label)
  610. {
  611. }
  612. explicit Yield(std::nullptr_t)
  613. : Instruction(Type::Yield)
  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. auto& continuation() const { return m_continuation_label; }
  620. private:
  621. Optional<Label> m_continuation_label;
  622. };
  623. class PushDeclarativeEnvironment final : public Instruction {
  624. public:
  625. explicit PushDeclarativeEnvironment(HashMap<u32, Variable> variables)
  626. : Instruction(Type::PushDeclarativeEnvironment)
  627. , m_variables(move(variables))
  628. {
  629. }
  630. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  631. String to_string_impl(Bytecode::Executable const&) const;
  632. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  633. private:
  634. HashMap<u32, Variable> m_variables;
  635. };
  636. class GetIterator final : public Instruction {
  637. public:
  638. GetIterator()
  639. : Instruction(Type::GetIterator)
  640. {
  641. }
  642. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  643. String to_string_impl(Bytecode::Executable const&) const;
  644. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  645. };
  646. class IteratorNext final : public Instruction {
  647. public:
  648. IteratorNext()
  649. : Instruction(Type::IteratorNext)
  650. {
  651. }
  652. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  653. String to_string_impl(Bytecode::Executable const&) const;
  654. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  655. };
  656. class IteratorResultDone final : public Instruction {
  657. public:
  658. IteratorResultDone()
  659. : Instruction(Type::IteratorResultDone)
  660. {
  661. }
  662. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  663. String to_string_impl(Bytecode::Executable const&) const;
  664. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  665. };
  666. class IteratorResultValue final : public Instruction {
  667. public:
  668. IteratorResultValue()
  669. : Instruction(Type::IteratorResultValue)
  670. {
  671. }
  672. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  673. String to_string_impl(Bytecode::Executable const&) const;
  674. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  675. };
  676. class ResolveThisBinding final : public Instruction {
  677. public:
  678. explicit ResolveThisBinding()
  679. : Instruction(Type::ResolveThisBinding)
  680. {
  681. }
  682. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  683. String to_string_impl(Bytecode::Executable const&) const;
  684. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  685. };
  686. }
  687. namespace JS::Bytecode {
  688. ALWAYS_INLINE ThrowCompletionOr<void> Instruction::execute(Bytecode::Interpreter& interpreter) const
  689. {
  690. #define __BYTECODE_OP(op) \
  691. case Instruction::Type::op: \
  692. return static_cast<Bytecode::Op::op const&>(*this).execute_impl(interpreter);
  693. switch (type()) {
  694. ENUMERATE_BYTECODE_OPS(__BYTECODE_OP)
  695. default:
  696. VERIFY_NOT_REACHED();
  697. }
  698. #undef __BYTECODE_OP
  699. }
  700. ALWAYS_INLINE void Instruction::replace_references(BasicBlock const& from, BasicBlock const& to)
  701. {
  702. #define __BYTECODE_OP(op) \
  703. case Instruction::Type::op: \
  704. return static_cast<Bytecode::Op::op&>(*this).replace_references_impl(from, to);
  705. switch (type()) {
  706. ENUMERATE_BYTECODE_OPS(__BYTECODE_OP)
  707. default:
  708. VERIFY_NOT_REACHED();
  709. }
  710. #undef __BYTECODE_OP
  711. }
  712. ALWAYS_INLINE size_t Instruction::length() const
  713. {
  714. if (type() == Type::Call)
  715. return static_cast<Op::Call const&>(*this).length_impl();
  716. else if (type() == Type::NewArray)
  717. return static_cast<Op::NewArray const&>(*this).length_impl();
  718. else if (type() == Type::CopyObjectExcludingProperties)
  719. return static_cast<Op::CopyObjectExcludingProperties const&>(*this).length_impl();
  720. #define __BYTECODE_OP(op) \
  721. case Type::op: \
  722. return sizeof(Op::op);
  723. switch (type()) {
  724. ENUMERATE_BYTECODE_OPS(__BYTECODE_OP)
  725. default:
  726. VERIFY_NOT_REACHED();
  727. }
  728. #undef __BYTECODE_OP
  729. }
  730. ALWAYS_INLINE bool Instruction::is_terminator() const
  731. {
  732. #define __BYTECODE_OP(op) \
  733. case Type::op: \
  734. return Op::op::IsTerminator;
  735. switch (type()) {
  736. ENUMERATE_BYTECODE_OPS(__BYTECODE_OP)
  737. default:
  738. VERIFY_NOT_REACHED();
  739. }
  740. #undef __BYTECODE_OP
  741. }
  742. }