Op.h 31 KB

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