Op.h 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032
  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 <AK/StdLibExtras.h>
  10. #include <LibCrypto/BigInt/SignedBigInteger.h>
  11. #include <LibJS/Bytecode/IdentifierTable.h>
  12. #include <LibJS/Bytecode/Instruction.h>
  13. #include <LibJS/Bytecode/Label.h>
  14. #include <LibJS/Bytecode/Register.h>
  15. #include <LibJS/Bytecode/StringTable.h>
  16. #include <LibJS/Heap/Cell.h>
  17. #include <LibJS/Runtime/Environment.h>
  18. #include <LibJS/Runtime/EnvironmentCoordinate.h>
  19. #include <LibJS/Runtime/Value.h>
  20. #include <LibJS/Runtime/ValueTraits.h>
  21. namespace JS::Bytecode::Op {
  22. class Load final : public Instruction {
  23. public:
  24. explicit Load(Register src)
  25. : Instruction(Type::Load)
  26. , m_src(src)
  27. {
  28. }
  29. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  30. String to_string_impl(Bytecode::Executable const&) const;
  31. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  32. private:
  33. Register m_src;
  34. };
  35. class LoadImmediate final : public Instruction {
  36. public:
  37. explicit LoadImmediate(Value value)
  38. : Instruction(Type::LoadImmediate)
  39. , m_value(value)
  40. {
  41. }
  42. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  43. String to_string_impl(Bytecode::Executable const&) const;
  44. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  45. private:
  46. Value m_value;
  47. };
  48. class Store final : public Instruction {
  49. public:
  50. explicit Store(Register dst)
  51. : Instruction(Type::Store)
  52. , m_dst(dst)
  53. {
  54. }
  55. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  56. String to_string_impl(Bytecode::Executable const&) const;
  57. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  58. private:
  59. Register m_dst;
  60. };
  61. #define JS_ENUMERATE_COMMON_BINARY_OPS(O) \
  62. O(Add, add) \
  63. O(Sub, sub) \
  64. O(Mul, mul) \
  65. O(Div, div) \
  66. O(Exp, exp) \
  67. O(Mod, mod) \
  68. O(In, in) \
  69. O(InstanceOf, instance_of) \
  70. O(GreaterThan, greater_than) \
  71. O(GreaterThanEquals, greater_than_equals) \
  72. O(LessThan, less_than) \
  73. O(LessThanEquals, less_than_equals) \
  74. O(LooselyInequals, abstract_inequals) \
  75. O(LooselyEquals, abstract_equals) \
  76. O(StrictlyInequals, typed_inequals) \
  77. O(StrictlyEquals, typed_equals) \
  78. O(BitwiseAnd, bitwise_and) \
  79. O(BitwiseOr, bitwise_or) \
  80. O(BitwiseXor, bitwise_xor) \
  81. O(LeftShift, left_shift) \
  82. O(RightShift, right_shift) \
  83. O(UnsignedRightShift, unsigned_right_shift)
  84. #define JS_DECLARE_COMMON_BINARY_OP(OpTitleCase, op_snake_case) \
  85. class OpTitleCase final : public Instruction { \
  86. public: \
  87. explicit OpTitleCase(Register lhs_reg) \
  88. : Instruction(Type::OpTitleCase) \
  89. , m_lhs_reg(lhs_reg) \
  90. { \
  91. } \
  92. \
  93. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const; \
  94. String to_string_impl(Bytecode::Executable const&) const; \
  95. void replace_references_impl(BasicBlock const&, BasicBlock const&) { } \
  96. \
  97. private: \
  98. Register m_lhs_reg; \
  99. };
  100. JS_ENUMERATE_COMMON_BINARY_OPS(JS_DECLARE_COMMON_BINARY_OP)
  101. #undef JS_DECLARE_COMMON_BINARY_OP
  102. #define JS_ENUMERATE_COMMON_UNARY_OPS(O) \
  103. O(BitwiseNot, bitwise_not) \
  104. O(Not, not_) \
  105. O(UnaryPlus, unary_plus) \
  106. O(UnaryMinus, unary_minus) \
  107. O(Typeof, typeof_)
  108. #define JS_DECLARE_COMMON_UNARY_OP(OpTitleCase, op_snake_case) \
  109. class OpTitleCase final : public Instruction { \
  110. public: \
  111. OpTitleCase() \
  112. : Instruction(Type::OpTitleCase) \
  113. { \
  114. } \
  115. \
  116. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const; \
  117. String to_string_impl(Bytecode::Executable const&) const; \
  118. void replace_references_impl(BasicBlock const&, BasicBlock const&) { } \
  119. };
  120. JS_ENUMERATE_COMMON_UNARY_OPS(JS_DECLARE_COMMON_UNARY_OP)
  121. #undef JS_DECLARE_COMMON_UNARY_OP
  122. class NewString final : public Instruction {
  123. public:
  124. explicit NewString(StringTableIndex string)
  125. : Instruction(Type::NewString)
  126. , m_string(string)
  127. {
  128. }
  129. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  130. String to_string_impl(Bytecode::Executable const&) const;
  131. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  132. private:
  133. StringTableIndex m_string;
  134. };
  135. class NewObject final : public Instruction {
  136. public:
  137. NewObject()
  138. : Instruction(Type::NewObject)
  139. {
  140. }
  141. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  142. String to_string_impl(Bytecode::Executable const&) const;
  143. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  144. };
  145. class NewRegExp final : public Instruction {
  146. public:
  147. NewRegExp(StringTableIndex source_index, StringTableIndex flags_index)
  148. : Instruction(Type::NewRegExp)
  149. , m_source_index(source_index)
  150. , m_flags_index(flags_index)
  151. {
  152. }
  153. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  154. String to_string_impl(Bytecode::Executable const&) const;
  155. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  156. private:
  157. StringTableIndex m_source_index;
  158. StringTableIndex m_flags_index;
  159. };
  160. // NOTE: This instruction is variable-width depending on the number of excluded names
  161. class CopyObjectExcludingProperties final : public Instruction {
  162. public:
  163. CopyObjectExcludingProperties(Register from_object, Vector<Register> const& excluded_names)
  164. : Instruction(Type::CopyObjectExcludingProperties)
  165. , m_from_object(from_object)
  166. , m_excluded_names_count(excluded_names.size())
  167. {
  168. for (size_t i = 0; i < m_excluded_names_count; i++)
  169. m_excluded_names[i] = excluded_names[i];
  170. }
  171. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  172. String to_string_impl(Bytecode::Executable const&) const;
  173. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  174. size_t length_impl() const { return sizeof(*this) + sizeof(Register) * m_excluded_names_count; }
  175. private:
  176. Register m_from_object;
  177. size_t m_excluded_names_count { 0 };
  178. Register m_excluded_names[];
  179. };
  180. class NewBigInt final : public Instruction {
  181. public:
  182. explicit NewBigInt(Crypto::SignedBigInteger bigint)
  183. : Instruction(Type::NewBigInt)
  184. , m_bigint(move(bigint))
  185. {
  186. }
  187. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  188. String to_string_impl(Bytecode::Executable const&) const;
  189. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  190. private:
  191. Crypto::SignedBigInteger m_bigint;
  192. };
  193. // NOTE: This instruction is variable-width depending on the number of elements!
  194. class NewArray final : public Instruction {
  195. public:
  196. NewArray()
  197. : Instruction(Type::NewArray)
  198. , m_element_count(0)
  199. {
  200. }
  201. explicit NewArray(AK::Array<Register, 2> const& elements_range)
  202. : Instruction(Type::NewArray)
  203. , m_element_count(elements_range[1].index() - elements_range[0].index() + 1)
  204. {
  205. m_elements[0] = elements_range[0];
  206. m_elements[1] = elements_range[1];
  207. }
  208. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  209. String to_string_impl(Bytecode::Executable const&) const;
  210. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  211. size_t length_impl() const
  212. {
  213. return sizeof(*this) + sizeof(Register) * (m_element_count == 0 ? 0 : 2);
  214. }
  215. private:
  216. size_t m_element_count { 0 };
  217. Register m_elements[];
  218. };
  219. class Append final : public Instruction {
  220. public:
  221. Append(Register lhs, bool is_spread)
  222. : Instruction(Type::Append)
  223. , m_lhs(lhs)
  224. , m_is_spread(is_spread)
  225. {
  226. }
  227. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  228. String to_string_impl(Bytecode::Executable const&) const;
  229. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  230. private:
  231. Register m_lhs;
  232. bool m_is_spread = false;
  233. };
  234. class IteratorToArray final : public Instruction {
  235. public:
  236. IteratorToArray()
  237. : Instruction(Type::IteratorToArray)
  238. {
  239. }
  240. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  241. String to_string_impl(Bytecode::Executable const&) const;
  242. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  243. };
  244. class ConcatString final : public Instruction {
  245. public:
  246. explicit ConcatString(Register lhs)
  247. : Instruction(Type::ConcatString)
  248. , m_lhs(lhs)
  249. {
  250. }
  251. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  252. String to_string_impl(Bytecode::Executable const&) const;
  253. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  254. private:
  255. Register m_lhs;
  256. };
  257. enum class EnvironmentMode {
  258. Lexical,
  259. Var,
  260. };
  261. class CreateEnvironment final : public Instruction {
  262. public:
  263. explicit CreateEnvironment(EnvironmentMode mode)
  264. : Instruction(Type::CreateEnvironment)
  265. , m_mode(mode)
  266. {
  267. }
  268. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  269. String to_string_impl(Bytecode::Executable const&) const;
  270. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  271. private:
  272. EnvironmentMode m_mode { EnvironmentMode::Lexical };
  273. };
  274. class EnterObjectEnvironment final : public Instruction {
  275. public:
  276. explicit EnterObjectEnvironment()
  277. : Instruction(Type::EnterObjectEnvironment)
  278. {
  279. }
  280. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  281. String to_string_impl(Bytecode::Executable const&) const;
  282. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  283. };
  284. class CreateVariable final : public Instruction {
  285. public:
  286. explicit CreateVariable(IdentifierTableIndex identifier, EnvironmentMode mode, bool is_immutable, bool is_global = false)
  287. : Instruction(Type::CreateVariable)
  288. , m_identifier(identifier)
  289. , m_mode(mode)
  290. , m_is_immutable(is_immutable)
  291. , m_is_global(is_global)
  292. {
  293. }
  294. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  295. String to_string_impl(Bytecode::Executable const&) const;
  296. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  297. private:
  298. IdentifierTableIndex m_identifier;
  299. EnvironmentMode m_mode;
  300. bool m_is_immutable : 4 { false };
  301. bool m_is_global : 4 { false };
  302. };
  303. class SetVariable final : public Instruction {
  304. public:
  305. enum class InitializationMode {
  306. Initialize,
  307. Set,
  308. InitializeOrSet,
  309. };
  310. explicit SetVariable(IdentifierTableIndex identifier, InitializationMode initialization_mode = InitializationMode::Set, EnvironmentMode mode = EnvironmentMode::Lexical)
  311. : Instruction(Type::SetVariable)
  312. , m_identifier(identifier)
  313. , m_mode(mode)
  314. , m_initialization_mode(initialization_mode)
  315. {
  316. }
  317. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  318. String to_string_impl(Bytecode::Executable const&) const;
  319. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  320. private:
  321. IdentifierTableIndex m_identifier;
  322. EnvironmentMode m_mode;
  323. InitializationMode m_initialization_mode { InitializationMode::Set };
  324. };
  325. class GetVariable final : public Instruction {
  326. public:
  327. explicit GetVariable(IdentifierTableIndex identifier)
  328. : Instruction(Type::GetVariable)
  329. , m_identifier(identifier)
  330. {
  331. }
  332. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  333. String to_string_impl(Bytecode::Executable const&) const;
  334. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  335. private:
  336. IdentifierTableIndex m_identifier;
  337. Optional<EnvironmentCoordinate> mutable m_cached_environment_coordinate;
  338. };
  339. class DeleteVariable final : public Instruction {
  340. public:
  341. explicit DeleteVariable(IdentifierTableIndex identifier)
  342. : Instruction(Type::DeleteVariable)
  343. , m_identifier(identifier)
  344. {
  345. }
  346. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  347. String to_string_impl(Bytecode::Executable const&) const;
  348. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  349. private:
  350. IdentifierTableIndex m_identifier;
  351. };
  352. class GetById final : public Instruction {
  353. public:
  354. explicit GetById(IdentifierTableIndex property)
  355. : Instruction(Type::GetById)
  356. , m_property(property)
  357. {
  358. }
  359. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  360. String to_string_impl(Bytecode::Executable const&) const;
  361. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  362. private:
  363. IdentifierTableIndex m_property;
  364. };
  365. enum class PropertyKind {
  366. Getter,
  367. Setter,
  368. KeyValue,
  369. Spread,
  370. ProtoSetter,
  371. };
  372. class PutById final : public Instruction {
  373. public:
  374. explicit PutById(Register base, IdentifierTableIndex property, PropertyKind kind = PropertyKind::KeyValue)
  375. : Instruction(Type::PutById)
  376. , m_base(base)
  377. , m_property(property)
  378. , m_kind(kind)
  379. {
  380. }
  381. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  382. String to_string_impl(Bytecode::Executable const&) const;
  383. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  384. private:
  385. Register m_base;
  386. IdentifierTableIndex m_property;
  387. PropertyKind m_kind;
  388. };
  389. class DeleteById final : public Instruction {
  390. public:
  391. explicit DeleteById(IdentifierTableIndex property)
  392. : Instruction(Type::DeleteById)
  393. , m_property(property)
  394. {
  395. }
  396. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  397. String to_string_impl(Bytecode::Executable const&) const;
  398. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  399. private:
  400. IdentifierTableIndex m_property;
  401. };
  402. class GetByValue final : public Instruction {
  403. public:
  404. explicit GetByValue(Register base)
  405. : Instruction(Type::GetByValue)
  406. , m_base(base)
  407. {
  408. }
  409. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  410. String to_string_impl(Bytecode::Executable const&) const;
  411. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  412. private:
  413. Register m_base;
  414. };
  415. class PutByValue final : public Instruction {
  416. public:
  417. PutByValue(Register base, Register property, PropertyKind kind = PropertyKind::KeyValue)
  418. : Instruction(Type::PutByValue)
  419. , m_base(base)
  420. , m_property(property)
  421. , m_kind(kind)
  422. {
  423. }
  424. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  425. String to_string_impl(Bytecode::Executable const&) const;
  426. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  427. private:
  428. Register m_base;
  429. Register m_property;
  430. PropertyKind m_kind;
  431. };
  432. class DeleteByValue final : public Instruction {
  433. public:
  434. DeleteByValue(Register base)
  435. : Instruction(Type::DeleteByValue)
  436. , m_base(base)
  437. {
  438. }
  439. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  440. String to_string_impl(Bytecode::Executable const&) const;
  441. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  442. private:
  443. Register m_base;
  444. };
  445. class Jump : public Instruction {
  446. public:
  447. constexpr static bool IsTerminator = true;
  448. explicit Jump(Type type, Optional<Label> taken_target = {}, Optional<Label> nontaken_target = {})
  449. : Instruction(type)
  450. , m_true_target(move(taken_target))
  451. , m_false_target(move(nontaken_target))
  452. {
  453. }
  454. explicit Jump(Optional<Label> taken_target = {}, Optional<Label> nontaken_target = {})
  455. : Instruction(Type::Jump)
  456. , m_true_target(move(taken_target))
  457. , m_false_target(move(nontaken_target))
  458. {
  459. }
  460. void set_targets(Optional<Label> true_target, Optional<Label> false_target)
  461. {
  462. m_true_target = move(true_target);
  463. m_false_target = move(false_target);
  464. }
  465. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  466. String to_string_impl(Bytecode::Executable const&) const;
  467. void replace_references_impl(BasicBlock const&, BasicBlock const&);
  468. auto& true_target() const { return m_true_target; }
  469. auto& false_target() const { return m_false_target; }
  470. protected:
  471. Optional<Label> m_true_target;
  472. Optional<Label> m_false_target;
  473. };
  474. class JumpConditional final : public Jump {
  475. public:
  476. explicit JumpConditional(Optional<Label> true_target = {}, Optional<Label> false_target = {})
  477. : Jump(Type::JumpConditional, 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. class JumpNullish final : public Jump {
  484. public:
  485. explicit JumpNullish(Optional<Label> true_target = {}, Optional<Label> false_target = {})
  486. : Jump(Type::JumpNullish, move(true_target), move(false_target))
  487. {
  488. }
  489. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  490. String to_string_impl(Bytecode::Executable const&) const;
  491. };
  492. class JumpUndefined final : public Jump {
  493. public:
  494. explicit JumpUndefined(Optional<Label> true_target = {}, Optional<Label> false_target = {})
  495. : Jump(Type::JumpUndefined, move(true_target), move(false_target))
  496. {
  497. }
  498. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  499. String to_string_impl(Bytecode::Executable const&) const;
  500. };
  501. // NOTE: This instruction is variable-width depending on the number of arguments!
  502. class Call final : public Instruction {
  503. public:
  504. enum class CallType {
  505. Call,
  506. Construct,
  507. };
  508. Call(CallType type, Register callee, Register this_value, Optional<StringTableIndex> expression_string = {})
  509. : Instruction(Type::Call)
  510. , m_callee(callee)
  511. , m_this_value(this_value)
  512. , m_type(type)
  513. , m_expression_string(expression_string)
  514. {
  515. }
  516. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  517. String to_string_impl(Bytecode::Executable const&) const;
  518. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  519. Completion throw_type_error_for_callee(Bytecode::Interpreter&, StringView callee_type) const;
  520. private:
  521. Register m_callee;
  522. Register m_this_value;
  523. CallType m_type;
  524. Optional<StringTableIndex> m_expression_string;
  525. };
  526. // NOTE: This instruction is variable-width depending on the number of arguments!
  527. class SuperCall : public Instruction {
  528. public:
  529. explicit SuperCall(bool is_synthetic)
  530. : Instruction(Type::SuperCall)
  531. , m_is_synthetic(is_synthetic)
  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. bool m_is_synthetic;
  539. };
  540. class NewClass final : public Instruction {
  541. public:
  542. explicit NewClass(ClassExpression const& class_expression)
  543. : Instruction(Type::NewClass)
  544. , m_class_expression(class_expression)
  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. private:
  551. ClassExpression const& m_class_expression;
  552. };
  553. class NewFunction final : public Instruction {
  554. public:
  555. explicit NewFunction(FunctionNode const& function_node)
  556. : Instruction(Type::NewFunction)
  557. , m_function_node(function_node)
  558. {
  559. }
  560. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  561. String to_string_impl(Bytecode::Executable const&) const;
  562. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  563. private:
  564. FunctionNode const& m_function_node;
  565. };
  566. class Return final : public Instruction {
  567. public:
  568. constexpr static bool IsTerminator = true;
  569. Return()
  570. : Instruction(Type::Return)
  571. {
  572. }
  573. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  574. String to_string_impl(Bytecode::Executable const&) const;
  575. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  576. };
  577. class Increment final : public Instruction {
  578. public:
  579. Increment()
  580. : Instruction(Type::Increment)
  581. {
  582. }
  583. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  584. String to_string_impl(Bytecode::Executable const&) const;
  585. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  586. };
  587. class Decrement final : public Instruction {
  588. public:
  589. Decrement()
  590. : Instruction(Type::Decrement)
  591. {
  592. }
  593. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  594. String to_string_impl(Bytecode::Executable const&) const;
  595. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  596. };
  597. class Throw final : public Instruction {
  598. public:
  599. constexpr static bool IsTerminator = true;
  600. Throw()
  601. : Instruction(Type::Throw)
  602. {
  603. }
  604. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  605. String to_string_impl(Bytecode::Executable const&) const;
  606. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  607. };
  608. class EnterUnwindContext final : public Instruction {
  609. public:
  610. constexpr static bool IsTerminator = true;
  611. EnterUnwindContext(Label entry_point, Optional<Label> handler_target, Optional<Label> finalizer_target)
  612. : Instruction(Type::EnterUnwindContext)
  613. , m_entry_point(move(entry_point))
  614. , m_handler_target(move(handler_target))
  615. , m_finalizer_target(move(finalizer_target))
  616. {
  617. }
  618. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  619. String to_string_impl(Bytecode::Executable const&) const;
  620. void replace_references_impl(BasicBlock const&, BasicBlock const&);
  621. auto& entry_point() const { return m_entry_point; }
  622. auto& handler_target() const { return m_handler_target; }
  623. auto& finalizer_target() const { return m_finalizer_target; }
  624. private:
  625. Label m_entry_point;
  626. Optional<Label> m_handler_target;
  627. Optional<Label> m_finalizer_target;
  628. };
  629. class LeaveEnvironment final : public Instruction {
  630. public:
  631. LeaveEnvironment(EnvironmentMode mode)
  632. : Instruction(Type::LeaveEnvironment)
  633. , m_mode(mode)
  634. {
  635. }
  636. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  637. String to_string_impl(Bytecode::Executable const&) const;
  638. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  639. private:
  640. EnvironmentMode m_mode { EnvironmentMode::Lexical };
  641. };
  642. class LeaveUnwindContext final : public Instruction {
  643. public:
  644. LeaveUnwindContext()
  645. : Instruction(Type::LeaveUnwindContext)
  646. {
  647. }
  648. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  649. String to_string_impl(Bytecode::Executable const&) const;
  650. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  651. };
  652. class FinishUnwind final : public Instruction {
  653. public:
  654. constexpr static bool IsTerminator = true;
  655. FinishUnwind(Label next)
  656. : Instruction(Type::FinishUnwind)
  657. , m_next_target(move(next))
  658. {
  659. }
  660. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  661. String to_string_impl(Bytecode::Executable const&) const;
  662. void replace_references_impl(BasicBlock const&, BasicBlock const&);
  663. Label next_target() const { return m_next_target; }
  664. private:
  665. Label m_next_target;
  666. };
  667. class ContinuePendingUnwind final : public Instruction {
  668. public:
  669. constexpr static bool IsTerminator = true;
  670. explicit ContinuePendingUnwind(Label resume_target)
  671. : Instruction(Type::ContinuePendingUnwind)
  672. , m_resume_target(resume_target)
  673. {
  674. }
  675. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  676. String to_string_impl(Bytecode::Executable const&) const;
  677. void replace_references_impl(BasicBlock const&, BasicBlock const&);
  678. auto& resume_target() const { return m_resume_target; }
  679. private:
  680. Label m_resume_target;
  681. };
  682. class Yield final : public Instruction {
  683. public:
  684. constexpr static bool IsTerminator = true;
  685. explicit Yield(Label continuation_label)
  686. : Instruction(Type::Yield)
  687. , m_continuation_label(continuation_label)
  688. {
  689. }
  690. explicit Yield(std::nullptr_t)
  691. : Instruction(Type::Yield)
  692. {
  693. }
  694. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  695. String to_string_impl(Bytecode::Executable const&) const;
  696. void replace_references_impl(BasicBlock const&, BasicBlock const&);
  697. auto& continuation() const { return m_continuation_label; }
  698. private:
  699. Optional<Label> m_continuation_label;
  700. };
  701. class PushDeclarativeEnvironment final : public Instruction {
  702. public:
  703. explicit PushDeclarativeEnvironment(HashMap<u32, Variable> variables)
  704. : Instruction(Type::PushDeclarativeEnvironment)
  705. , m_variables(move(variables))
  706. {
  707. }
  708. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  709. String to_string_impl(Bytecode::Executable const&) const;
  710. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  711. private:
  712. HashMap<u32, Variable> m_variables;
  713. };
  714. class GetIterator final : public Instruction {
  715. public:
  716. GetIterator()
  717. : Instruction(Type::GetIterator)
  718. {
  719. }
  720. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  721. String to_string_impl(Bytecode::Executable const&) const;
  722. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  723. };
  724. class GetObjectPropertyIterator final : public Instruction {
  725. public:
  726. GetObjectPropertyIterator()
  727. : Instruction(Type::GetObjectPropertyIterator)
  728. {
  729. }
  730. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  731. String to_string_impl(Bytecode::Executable const&) const;
  732. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  733. };
  734. class IteratorNext final : public Instruction {
  735. public:
  736. IteratorNext()
  737. : Instruction(Type::IteratorNext)
  738. {
  739. }
  740. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  741. String to_string_impl(Bytecode::Executable const&) const;
  742. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  743. };
  744. class IteratorResultDone final : public Instruction {
  745. public:
  746. IteratorResultDone()
  747. : Instruction(Type::IteratorResultDone)
  748. {
  749. }
  750. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  751. String to_string_impl(Bytecode::Executable const&) const;
  752. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  753. };
  754. class IteratorResultValue final : public Instruction {
  755. public:
  756. IteratorResultValue()
  757. : Instruction(Type::IteratorResultValue)
  758. {
  759. }
  760. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  761. String to_string_impl(Bytecode::Executable const&) const;
  762. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  763. };
  764. class ResolveThisBinding final : public Instruction {
  765. public:
  766. explicit ResolveThisBinding()
  767. : Instruction(Type::ResolveThisBinding)
  768. {
  769. }
  770. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  771. String to_string_impl(Bytecode::Executable const&) const;
  772. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  773. };
  774. class GetNewTarget final : public Instruction {
  775. public:
  776. explicit GetNewTarget()
  777. : Instruction(Type::GetNewTarget)
  778. {
  779. }
  780. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  781. String to_string_impl(Bytecode::Executable const&) const;
  782. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  783. };
  784. class TypeofVariable final : public Instruction {
  785. public:
  786. explicit TypeofVariable(IdentifierTableIndex identifier)
  787. : Instruction(Type::TypeofVariable)
  788. , m_identifier(identifier)
  789. {
  790. }
  791. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  792. String to_string_impl(Bytecode::Executable const&) const;
  793. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  794. private:
  795. IdentifierTableIndex m_identifier;
  796. };
  797. }
  798. namespace JS::Bytecode {
  799. ALWAYS_INLINE ThrowCompletionOr<void> Instruction::execute(Bytecode::Interpreter& interpreter) const
  800. {
  801. #define __BYTECODE_OP(op) \
  802. case Instruction::Type::op: \
  803. return static_cast<Bytecode::Op::op const&>(*this).execute_impl(interpreter);
  804. switch (type()) {
  805. ENUMERATE_BYTECODE_OPS(__BYTECODE_OP)
  806. default:
  807. VERIFY_NOT_REACHED();
  808. }
  809. #undef __BYTECODE_OP
  810. }
  811. ALWAYS_INLINE void Instruction::replace_references(BasicBlock const& from, BasicBlock const& to)
  812. {
  813. #define __BYTECODE_OP(op) \
  814. case Instruction::Type::op: \
  815. return static_cast<Bytecode::Op::op&>(*this).replace_references_impl(from, to);
  816. switch (type()) {
  817. ENUMERATE_BYTECODE_OPS(__BYTECODE_OP)
  818. default:
  819. VERIFY_NOT_REACHED();
  820. }
  821. #undef __BYTECODE_OP
  822. }
  823. ALWAYS_INLINE size_t Instruction::length() const
  824. {
  825. if (type() == Type::NewArray)
  826. return round_up_to_power_of_two(static_cast<Op::NewArray const&>(*this).length_impl(), alignof(void*));
  827. if (type() == Type::CopyObjectExcludingProperties)
  828. return round_up_to_power_of_two(static_cast<Op::CopyObjectExcludingProperties const&>(*this).length_impl(), alignof(void*));
  829. #define __BYTECODE_OP(op) \
  830. case Type::op: \
  831. return sizeof(Op::op);
  832. switch (type()) {
  833. ENUMERATE_BYTECODE_OPS(__BYTECODE_OP)
  834. default:
  835. VERIFY_NOT_REACHED();
  836. }
  837. #undef __BYTECODE_OP
  838. }
  839. ALWAYS_INLINE bool Instruction::is_terminator() const
  840. {
  841. #define __BYTECODE_OP(op) \
  842. case Type::op: \
  843. return Op::op::IsTerminator;
  844. switch (type()) {
  845. ENUMERATE_BYTECODE_OPS(__BYTECODE_OP)
  846. default:
  847. VERIFY_NOT_REACHED();
  848. }
  849. #undef __BYTECODE_OP
  850. }
  851. }