Op.h 31 KB

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