Op.h 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000
  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. class NewClass final : public Instruction {
  517. public:
  518. explicit NewClass(ClassExpression const& class_expression)
  519. : Instruction(Type::NewClass)
  520. , m_class_expression(class_expression)
  521. {
  522. }
  523. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  524. String to_string_impl(Bytecode::Executable const&) const;
  525. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  526. private:
  527. ClassExpression const& m_class_expression;
  528. };
  529. class NewFunction final : public Instruction {
  530. public:
  531. explicit NewFunction(FunctionNode const& function_node)
  532. : Instruction(Type::NewFunction)
  533. , m_function_node(function_node)
  534. {
  535. }
  536. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  537. String to_string_impl(Bytecode::Executable const&) const;
  538. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  539. private:
  540. FunctionNode const& m_function_node;
  541. };
  542. class Return final : public Instruction {
  543. public:
  544. constexpr static bool IsTerminator = true;
  545. Return()
  546. : Instruction(Type::Return)
  547. {
  548. }
  549. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  550. String to_string_impl(Bytecode::Executable const&) const;
  551. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  552. };
  553. class Increment final : public Instruction {
  554. public:
  555. Increment()
  556. : Instruction(Type::Increment)
  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. };
  563. class Decrement final : public Instruction {
  564. public:
  565. Decrement()
  566. : Instruction(Type::Decrement)
  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 Throw final : public Instruction {
  574. public:
  575. constexpr static bool IsTerminator = true;
  576. Throw()
  577. : Instruction(Type::Throw)
  578. {
  579. }
  580. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  581. String to_string_impl(Bytecode::Executable const&) const;
  582. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  583. };
  584. class EnterUnwindContext final : public Instruction {
  585. public:
  586. constexpr static bool IsTerminator = true;
  587. EnterUnwindContext(Label entry_point, Optional<Label> handler_target, Optional<Label> finalizer_target)
  588. : Instruction(Type::EnterUnwindContext)
  589. , m_entry_point(move(entry_point))
  590. , m_handler_target(move(handler_target))
  591. , m_finalizer_target(move(finalizer_target))
  592. {
  593. }
  594. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  595. String to_string_impl(Bytecode::Executable const&) const;
  596. void replace_references_impl(BasicBlock const&, BasicBlock const&);
  597. auto& entry_point() const { return m_entry_point; }
  598. auto& handler_target() const { return m_handler_target; }
  599. auto& finalizer_target() const { return m_finalizer_target; }
  600. private:
  601. Label m_entry_point;
  602. Optional<Label> m_handler_target;
  603. Optional<Label> m_finalizer_target;
  604. };
  605. class LeaveEnvironment final : public Instruction {
  606. public:
  607. LeaveEnvironment(EnvironmentMode mode)
  608. : Instruction(Type::LeaveEnvironment)
  609. , m_mode(mode)
  610. {
  611. }
  612. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  613. String to_string_impl(Bytecode::Executable const&) const;
  614. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  615. private:
  616. EnvironmentMode m_mode { EnvironmentMode::Lexical };
  617. };
  618. class LeaveUnwindContext final : public Instruction {
  619. public:
  620. LeaveUnwindContext()
  621. : Instruction(Type::LeaveUnwindContext)
  622. {
  623. }
  624. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  625. String to_string_impl(Bytecode::Executable const&) const;
  626. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  627. };
  628. class FinishUnwind final : public Instruction {
  629. public:
  630. FinishUnwind(Label next)
  631. : Instruction(Type::FinishUnwind)
  632. , m_next_target(move(next))
  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. Label m_next_target;
  640. };
  641. class ContinuePendingUnwind final : public Instruction {
  642. public:
  643. constexpr static bool IsTerminator = true;
  644. explicit ContinuePendingUnwind(Label resume_target)
  645. : Instruction(Type::ContinuePendingUnwind)
  646. , m_resume_target(resume_target)
  647. {
  648. }
  649. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  650. String to_string_impl(Bytecode::Executable const&) const;
  651. void replace_references_impl(BasicBlock const&, BasicBlock const&);
  652. auto& resume_target() const { return m_resume_target; }
  653. private:
  654. Label m_resume_target;
  655. };
  656. class Yield final : public Instruction {
  657. public:
  658. constexpr static bool IsTerminator = true;
  659. explicit Yield(Label continuation_label)
  660. : Instruction(Type::Yield)
  661. , m_continuation_label(continuation_label)
  662. {
  663. }
  664. explicit Yield(std::nullptr_t)
  665. : Instruction(Type::Yield)
  666. {
  667. }
  668. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  669. String to_string_impl(Bytecode::Executable const&) const;
  670. void replace_references_impl(BasicBlock const&, BasicBlock const&);
  671. auto& continuation() const { return m_continuation_label; }
  672. private:
  673. Optional<Label> m_continuation_label;
  674. };
  675. class PushDeclarativeEnvironment final : public Instruction {
  676. public:
  677. explicit PushDeclarativeEnvironment(HashMap<u32, Variable> variables)
  678. : Instruction(Type::PushDeclarativeEnvironment)
  679. , m_variables(move(variables))
  680. {
  681. }
  682. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  683. String to_string_impl(Bytecode::Executable const&) const;
  684. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  685. private:
  686. HashMap<u32, Variable> m_variables;
  687. };
  688. class GetIterator final : public Instruction {
  689. public:
  690. GetIterator()
  691. : Instruction(Type::GetIterator)
  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. };
  698. class GetObjectPropertyIterator final : public Instruction {
  699. public:
  700. GetObjectPropertyIterator()
  701. : Instruction(Type::GetObjectPropertyIterator)
  702. {
  703. }
  704. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  705. String to_string_impl(Bytecode::Executable const&) const;
  706. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  707. };
  708. class IteratorNext final : public Instruction {
  709. public:
  710. IteratorNext()
  711. : Instruction(Type::IteratorNext)
  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 IteratorResultDone final : public Instruction {
  719. public:
  720. IteratorResultDone()
  721. : Instruction(Type::IteratorResultDone)
  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 IteratorResultValue final : public Instruction {
  729. public:
  730. IteratorResultValue()
  731. : Instruction(Type::IteratorResultValue)
  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 ResolveThisBinding final : public Instruction {
  739. public:
  740. explicit ResolveThisBinding()
  741. : Instruction(Type::ResolveThisBinding)
  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 GetNewTarget final : public Instruction {
  749. public:
  750. explicit GetNewTarget()
  751. : Instruction(Type::GetNewTarget)
  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 TypeofVariable final : public Instruction {
  759. public:
  760. explicit TypeofVariable(IdentifierTableIndex identifier)
  761. : Instruction(Type::TypeofVariable)
  762. , m_identifier(identifier)
  763. {
  764. }
  765. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  766. String to_string_impl(Bytecode::Executable const&) const;
  767. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  768. private:
  769. IdentifierTableIndex m_identifier;
  770. };
  771. }
  772. namespace JS::Bytecode {
  773. ALWAYS_INLINE ThrowCompletionOr<void> Instruction::execute(Bytecode::Interpreter& interpreter) const
  774. {
  775. #define __BYTECODE_OP(op) \
  776. case Instruction::Type::op: \
  777. return static_cast<Bytecode::Op::op const&>(*this).execute_impl(interpreter);
  778. switch (type()) {
  779. ENUMERATE_BYTECODE_OPS(__BYTECODE_OP)
  780. default:
  781. VERIFY_NOT_REACHED();
  782. }
  783. #undef __BYTECODE_OP
  784. }
  785. ALWAYS_INLINE void Instruction::replace_references(BasicBlock const& from, BasicBlock const& to)
  786. {
  787. #define __BYTECODE_OP(op) \
  788. case Instruction::Type::op: \
  789. return static_cast<Bytecode::Op::op&>(*this).replace_references_impl(from, to);
  790. switch (type()) {
  791. ENUMERATE_BYTECODE_OPS(__BYTECODE_OP)
  792. default:
  793. VERIFY_NOT_REACHED();
  794. }
  795. #undef __BYTECODE_OP
  796. }
  797. ALWAYS_INLINE size_t Instruction::length() const
  798. {
  799. if (type() == Type::Call)
  800. return static_cast<Op::Call const&>(*this).length_impl();
  801. else if (type() == Type::NewArray)
  802. return static_cast<Op::NewArray const&>(*this).length_impl();
  803. else if (type() == Type::CopyObjectExcludingProperties)
  804. return static_cast<Op::CopyObjectExcludingProperties const&>(*this).length_impl();
  805. #define __BYTECODE_OP(op) \
  806. case Type::op: \
  807. return sizeof(Op::op);
  808. switch (type()) {
  809. ENUMERATE_BYTECODE_OPS(__BYTECODE_OP)
  810. default:
  811. VERIFY_NOT_REACHED();
  812. }
  813. #undef __BYTECODE_OP
  814. }
  815. ALWAYS_INLINE bool Instruction::is_terminator() const
  816. {
  817. #define __BYTECODE_OP(op) \
  818. case Type::op: \
  819. return Op::op::IsTerminator;
  820. switch (type()) {
  821. ENUMERATE_BYTECODE_OPS(__BYTECODE_OP)
  822. default:
  823. VERIFY_NOT_REACHED();
  824. }
  825. #undef __BYTECODE_OP
  826. }
  827. }