Op.h 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982
  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(AK::Array<Register, 2> const& elements_range)
  201. : Instruction(Type::NewArray)
  202. , m_element_count(elements_range[1].index() - elements_range[0].index() + 1)
  203. {
  204. m_elements[0] = elements_range[0];
  205. m_elements[1] = elements_range[1];
  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 == 0 ? 0 : 2);
  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 DeleteVariable final : public Instruction {
  322. public:
  323. explicit DeleteVariable(IdentifierTableIndex identifier)
  324. : Instruction(Type::DeleteVariable)
  325. , m_identifier(identifier)
  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_identifier;
  333. };
  334. class GetById final : public Instruction {
  335. public:
  336. explicit GetById(IdentifierTableIndex property)
  337. : Instruction(Type::GetById)
  338. , m_property(property)
  339. {
  340. }
  341. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  342. String to_string_impl(Bytecode::Executable const&) const;
  343. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  344. private:
  345. IdentifierTableIndex m_property;
  346. };
  347. enum class PropertyKind {
  348. Getter,
  349. Setter,
  350. KeyValue,
  351. Spread,
  352. ProtoSetter,
  353. };
  354. class PutById final : public Instruction {
  355. public:
  356. explicit PutById(Register base, IdentifierTableIndex property, PropertyKind kind = PropertyKind::KeyValue)
  357. : Instruction(Type::PutById)
  358. , m_base(base)
  359. , m_property(property)
  360. , m_kind(kind)
  361. {
  362. }
  363. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  364. String to_string_impl(Bytecode::Executable const&) const;
  365. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  366. private:
  367. Register m_base;
  368. IdentifierTableIndex m_property;
  369. PropertyKind m_kind;
  370. };
  371. class DeleteById final : public Instruction {
  372. public:
  373. explicit DeleteById(IdentifierTableIndex property)
  374. : Instruction(Type::DeleteById)
  375. , m_property(property)
  376. {
  377. }
  378. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  379. String to_string_impl(Bytecode::Executable const&) const;
  380. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  381. private:
  382. IdentifierTableIndex m_property;
  383. };
  384. class GetByValue final : public Instruction {
  385. public:
  386. explicit GetByValue(Register base)
  387. : Instruction(Type::GetByValue)
  388. , m_base(base)
  389. {
  390. }
  391. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  392. String to_string_impl(Bytecode::Executable const&) const;
  393. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  394. private:
  395. Register m_base;
  396. };
  397. class PutByValue final : public Instruction {
  398. public:
  399. PutByValue(Register base, Register property, PropertyKind kind = PropertyKind::KeyValue)
  400. : Instruction(Type::PutByValue)
  401. , m_base(base)
  402. , m_property(property)
  403. , m_kind(kind)
  404. {
  405. }
  406. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  407. String to_string_impl(Bytecode::Executable const&) const;
  408. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  409. private:
  410. Register m_base;
  411. Register m_property;
  412. PropertyKind m_kind;
  413. };
  414. class DeleteByValue final : public Instruction {
  415. public:
  416. DeleteByValue(Register base)
  417. : Instruction(Type::DeleteByValue)
  418. , m_base(base)
  419. {
  420. }
  421. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  422. String to_string_impl(Bytecode::Executable const&) const;
  423. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  424. private:
  425. Register m_base;
  426. };
  427. class Jump : public Instruction {
  428. public:
  429. constexpr static bool IsTerminator = true;
  430. explicit Jump(Type type, Optional<Label> taken_target = {}, Optional<Label> nontaken_target = {})
  431. : Instruction(type)
  432. , m_true_target(move(taken_target))
  433. , m_false_target(move(nontaken_target))
  434. {
  435. }
  436. explicit Jump(Optional<Label> taken_target = {}, Optional<Label> nontaken_target = {})
  437. : Instruction(Type::Jump)
  438. , m_true_target(move(taken_target))
  439. , m_false_target(move(nontaken_target))
  440. {
  441. }
  442. void set_targets(Optional<Label> true_target, Optional<Label> false_target)
  443. {
  444. m_true_target = move(true_target);
  445. m_false_target = move(false_target);
  446. }
  447. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  448. String to_string_impl(Bytecode::Executable const&) const;
  449. void replace_references_impl(BasicBlock const&, BasicBlock const&);
  450. auto& true_target() const { return m_true_target; }
  451. auto& false_target() const { return m_false_target; }
  452. protected:
  453. Optional<Label> m_true_target;
  454. Optional<Label> m_false_target;
  455. };
  456. class JumpConditional final : public Jump {
  457. public:
  458. explicit JumpConditional(Optional<Label> true_target = {}, Optional<Label> false_target = {})
  459. : Jump(Type::JumpConditional, move(true_target), move(false_target))
  460. {
  461. }
  462. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  463. String to_string_impl(Bytecode::Executable const&) const;
  464. };
  465. class JumpNullish final : public Jump {
  466. public:
  467. explicit JumpNullish(Optional<Label> true_target = {}, Optional<Label> false_target = {})
  468. : Jump(Type::JumpNullish, move(true_target), move(false_target))
  469. {
  470. }
  471. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  472. String to_string_impl(Bytecode::Executable const&) const;
  473. };
  474. class JumpUndefined final : public Jump {
  475. public:
  476. explicit JumpUndefined(Optional<Label> true_target = {}, Optional<Label> false_target = {})
  477. : Jump(Type::JumpUndefined, move(true_target), move(false_target))
  478. {
  479. }
  480. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  481. String to_string_impl(Bytecode::Executable const&) const;
  482. };
  483. // NOTE: This instruction is variable-width depending on the number of arguments!
  484. class Call final : public Instruction {
  485. public:
  486. enum class CallType {
  487. Call,
  488. Construct,
  489. };
  490. Call(CallType type, Register callee, Register this_value, Vector<Register> const& arguments)
  491. : Instruction(Type::Call)
  492. , m_callee(callee)
  493. , m_this_value(this_value)
  494. , m_type(type)
  495. , m_argument_count(arguments.size())
  496. {
  497. for (size_t i = 0; i < m_argument_count; ++i)
  498. m_arguments[i] = arguments[i];
  499. }
  500. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  501. String to_string_impl(Bytecode::Executable const&) const;
  502. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  503. size_t length_impl() const
  504. {
  505. return sizeof(*this) + sizeof(Register) * m_argument_count;
  506. }
  507. private:
  508. Register m_callee;
  509. Register m_this_value;
  510. CallType m_type;
  511. size_t m_argument_count { 0 };
  512. Register m_arguments[];
  513. };
  514. class NewClass final : public Instruction {
  515. public:
  516. explicit NewClass(ClassExpression const& class_expression)
  517. : Instruction(Type::NewClass)
  518. , m_class_expression(class_expression)
  519. {
  520. }
  521. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  522. String to_string_impl(Bytecode::Executable const&) const;
  523. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  524. private:
  525. ClassExpression const& m_class_expression;
  526. };
  527. class NewFunction final : public Instruction {
  528. public:
  529. explicit NewFunction(FunctionNode const& function_node)
  530. : Instruction(Type::NewFunction)
  531. , m_function_node(function_node)
  532. {
  533. }
  534. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  535. String to_string_impl(Bytecode::Executable const&) const;
  536. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  537. private:
  538. FunctionNode const& m_function_node;
  539. };
  540. class Return final : public Instruction {
  541. public:
  542. constexpr static bool IsTerminator = true;
  543. Return()
  544. : Instruction(Type::Return)
  545. {
  546. }
  547. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  548. String to_string_impl(Bytecode::Executable const&) const;
  549. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  550. };
  551. class Increment final : public Instruction {
  552. public:
  553. Increment()
  554. : Instruction(Type::Increment)
  555. {
  556. }
  557. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  558. String to_string_impl(Bytecode::Executable const&) const;
  559. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  560. };
  561. class Decrement final : public Instruction {
  562. public:
  563. Decrement()
  564. : Instruction(Type::Decrement)
  565. {
  566. }
  567. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  568. String to_string_impl(Bytecode::Executable const&) const;
  569. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  570. };
  571. class Throw final : public Instruction {
  572. public:
  573. constexpr static bool IsTerminator = true;
  574. Throw()
  575. : Instruction(Type::Throw)
  576. {
  577. }
  578. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  579. String to_string_impl(Bytecode::Executable const&) const;
  580. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  581. };
  582. class EnterUnwindContext final : public Instruction {
  583. public:
  584. constexpr static bool IsTerminator = true;
  585. EnterUnwindContext(Label entry_point, Optional<Label> handler_target, Optional<Label> finalizer_target)
  586. : Instruction(Type::EnterUnwindContext)
  587. , m_entry_point(move(entry_point))
  588. , m_handler_target(move(handler_target))
  589. , m_finalizer_target(move(finalizer_target))
  590. {
  591. }
  592. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  593. String to_string_impl(Bytecode::Executable const&) const;
  594. void replace_references_impl(BasicBlock const&, BasicBlock const&);
  595. auto& entry_point() const { return m_entry_point; }
  596. auto& handler_target() const { return m_handler_target; }
  597. auto& finalizer_target() const { return m_finalizer_target; }
  598. private:
  599. Label m_entry_point;
  600. Optional<Label> m_handler_target;
  601. Optional<Label> m_finalizer_target;
  602. };
  603. class LeaveEnvironment final : public Instruction {
  604. public:
  605. LeaveEnvironment(EnvironmentMode mode)
  606. : Instruction(Type::LeaveEnvironment)
  607. , m_mode(mode)
  608. {
  609. }
  610. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  611. String to_string_impl(Bytecode::Executable const&) const;
  612. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  613. private:
  614. EnvironmentMode m_mode { EnvironmentMode::Lexical };
  615. };
  616. class LeaveUnwindContext final : public Instruction {
  617. public:
  618. LeaveUnwindContext()
  619. : Instruction(Type::LeaveUnwindContext)
  620. {
  621. }
  622. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  623. String to_string_impl(Bytecode::Executable const&) const;
  624. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  625. };
  626. class FinishUnwind final : public Instruction {
  627. public:
  628. FinishUnwind(Label next)
  629. : Instruction(Type::FinishUnwind)
  630. , m_next_target(move(next))
  631. {
  632. }
  633. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  634. String to_string_impl(Bytecode::Executable const&) const;
  635. void replace_references_impl(BasicBlock const&, BasicBlock const&);
  636. private:
  637. Label m_next_target;
  638. };
  639. class ContinuePendingUnwind final : public Instruction {
  640. public:
  641. constexpr static bool IsTerminator = true;
  642. explicit ContinuePendingUnwind(Label resume_target)
  643. : Instruction(Type::ContinuePendingUnwind)
  644. , m_resume_target(resume_target)
  645. {
  646. }
  647. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  648. String to_string_impl(Bytecode::Executable const&) const;
  649. void replace_references_impl(BasicBlock const&, BasicBlock const&);
  650. auto& resume_target() const { return m_resume_target; }
  651. private:
  652. Label m_resume_target;
  653. };
  654. class Yield final : public Instruction {
  655. public:
  656. constexpr static bool IsTerminator = true;
  657. explicit Yield(Label continuation_label)
  658. : Instruction(Type::Yield)
  659. , m_continuation_label(continuation_label)
  660. {
  661. }
  662. explicit Yield(std::nullptr_t)
  663. : Instruction(Type::Yield)
  664. {
  665. }
  666. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  667. String to_string_impl(Bytecode::Executable const&) const;
  668. void replace_references_impl(BasicBlock const&, BasicBlock const&);
  669. auto& continuation() const { return m_continuation_label; }
  670. private:
  671. Optional<Label> m_continuation_label;
  672. };
  673. class PushDeclarativeEnvironment final : public Instruction {
  674. public:
  675. explicit PushDeclarativeEnvironment(HashMap<u32, Variable> variables)
  676. : Instruction(Type::PushDeclarativeEnvironment)
  677. , m_variables(move(variables))
  678. {
  679. }
  680. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  681. String to_string_impl(Bytecode::Executable const&) const;
  682. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  683. private:
  684. HashMap<u32, Variable> m_variables;
  685. };
  686. class GetIterator final : public Instruction {
  687. public:
  688. GetIterator()
  689. : Instruction(Type::GetIterator)
  690. {
  691. }
  692. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  693. String to_string_impl(Bytecode::Executable const&) const;
  694. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  695. };
  696. class GetObjectPropertyIterator final : public Instruction {
  697. public:
  698. GetObjectPropertyIterator()
  699. : Instruction(Type::GetObjectPropertyIterator)
  700. {
  701. }
  702. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  703. String to_string_impl(Bytecode::Executable const&) const;
  704. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  705. };
  706. class IteratorNext final : public Instruction {
  707. public:
  708. IteratorNext()
  709. : Instruction(Type::IteratorNext)
  710. {
  711. }
  712. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  713. String to_string_impl(Bytecode::Executable const&) const;
  714. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  715. };
  716. class IteratorResultDone final : public Instruction {
  717. public:
  718. IteratorResultDone()
  719. : Instruction(Type::IteratorResultDone)
  720. {
  721. }
  722. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  723. String to_string_impl(Bytecode::Executable const&) const;
  724. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  725. };
  726. class IteratorResultValue final : public Instruction {
  727. public:
  728. IteratorResultValue()
  729. : Instruction(Type::IteratorResultValue)
  730. {
  731. }
  732. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  733. String to_string_impl(Bytecode::Executable const&) const;
  734. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  735. };
  736. class ResolveThisBinding final : public Instruction {
  737. public:
  738. explicit ResolveThisBinding()
  739. : Instruction(Type::ResolveThisBinding)
  740. {
  741. }
  742. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  743. String to_string_impl(Bytecode::Executable const&) const;
  744. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  745. };
  746. class GetNewTarget final : public Instruction {
  747. public:
  748. explicit GetNewTarget()
  749. : Instruction(Type::GetNewTarget)
  750. {
  751. }
  752. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  753. String to_string_impl(Bytecode::Executable const&) const;
  754. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  755. };
  756. }
  757. namespace JS::Bytecode {
  758. ALWAYS_INLINE ThrowCompletionOr<void> Instruction::execute(Bytecode::Interpreter& interpreter) const
  759. {
  760. #define __BYTECODE_OP(op) \
  761. case Instruction::Type::op: \
  762. return static_cast<Bytecode::Op::op const&>(*this).execute_impl(interpreter);
  763. switch (type()) {
  764. ENUMERATE_BYTECODE_OPS(__BYTECODE_OP)
  765. default:
  766. VERIFY_NOT_REACHED();
  767. }
  768. #undef __BYTECODE_OP
  769. }
  770. ALWAYS_INLINE void Instruction::replace_references(BasicBlock const& from, BasicBlock const& to)
  771. {
  772. #define __BYTECODE_OP(op) \
  773. case Instruction::Type::op: \
  774. return static_cast<Bytecode::Op::op&>(*this).replace_references_impl(from, to);
  775. switch (type()) {
  776. ENUMERATE_BYTECODE_OPS(__BYTECODE_OP)
  777. default:
  778. VERIFY_NOT_REACHED();
  779. }
  780. #undef __BYTECODE_OP
  781. }
  782. ALWAYS_INLINE size_t Instruction::length() const
  783. {
  784. if (type() == Type::Call)
  785. return static_cast<Op::Call const&>(*this).length_impl();
  786. else if (type() == Type::NewArray)
  787. return static_cast<Op::NewArray const&>(*this).length_impl();
  788. else if (type() == Type::CopyObjectExcludingProperties)
  789. return static_cast<Op::CopyObjectExcludingProperties const&>(*this).length_impl();
  790. #define __BYTECODE_OP(op) \
  791. case Type::op: \
  792. return sizeof(Op::op);
  793. switch (type()) {
  794. ENUMERATE_BYTECODE_OPS(__BYTECODE_OP)
  795. default:
  796. VERIFY_NOT_REACHED();
  797. }
  798. #undef __BYTECODE_OP
  799. }
  800. ALWAYS_INLINE bool Instruction::is_terminator() const
  801. {
  802. #define __BYTECODE_OP(op) \
  803. case Type::op: \
  804. return Op::op::IsTerminator;
  805. switch (type()) {
  806. ENUMERATE_BYTECODE_OPS(__BYTECODE_OP)
  807. default:
  808. VERIFY_NOT_REACHED();
  809. }
  810. #undef __BYTECODE_OP
  811. }
  812. }