Op.h 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028
  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. FinishUnwind(Label next)
  655. : Instruction(Type::FinishUnwind)
  656. , m_next_target(move(next))
  657. {
  658. }
  659. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  660. String to_string_impl(Bytecode::Executable const&) const;
  661. void replace_references_impl(BasicBlock const&, BasicBlock const&);
  662. private:
  663. Label m_next_target;
  664. };
  665. class ContinuePendingUnwind final : public Instruction {
  666. public:
  667. constexpr static bool IsTerminator = true;
  668. explicit ContinuePendingUnwind(Label resume_target)
  669. : Instruction(Type::ContinuePendingUnwind)
  670. , m_resume_target(resume_target)
  671. {
  672. }
  673. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  674. String to_string_impl(Bytecode::Executable const&) const;
  675. void replace_references_impl(BasicBlock const&, BasicBlock const&);
  676. auto& resume_target() const { return m_resume_target; }
  677. private:
  678. Label m_resume_target;
  679. };
  680. class Yield final : public Instruction {
  681. public:
  682. constexpr static bool IsTerminator = true;
  683. explicit Yield(Label continuation_label)
  684. : Instruction(Type::Yield)
  685. , m_continuation_label(continuation_label)
  686. {
  687. }
  688. explicit Yield(std::nullptr_t)
  689. : Instruction(Type::Yield)
  690. {
  691. }
  692. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  693. String to_string_impl(Bytecode::Executable const&) const;
  694. void replace_references_impl(BasicBlock const&, BasicBlock const&);
  695. auto& continuation() const { return m_continuation_label; }
  696. private:
  697. Optional<Label> m_continuation_label;
  698. };
  699. class PushDeclarativeEnvironment final : public Instruction {
  700. public:
  701. explicit PushDeclarativeEnvironment(HashMap<u32, Variable> variables)
  702. : Instruction(Type::PushDeclarativeEnvironment)
  703. , m_variables(move(variables))
  704. {
  705. }
  706. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  707. String to_string_impl(Bytecode::Executable const&) const;
  708. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  709. private:
  710. HashMap<u32, Variable> m_variables;
  711. };
  712. class GetIterator final : public Instruction {
  713. public:
  714. GetIterator()
  715. : Instruction(Type::GetIterator)
  716. {
  717. }
  718. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  719. String to_string_impl(Bytecode::Executable const&) const;
  720. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  721. };
  722. class GetObjectPropertyIterator final : public Instruction {
  723. public:
  724. GetObjectPropertyIterator()
  725. : Instruction(Type::GetObjectPropertyIterator)
  726. {
  727. }
  728. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  729. String to_string_impl(Bytecode::Executable const&) const;
  730. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  731. };
  732. class IteratorNext final : public Instruction {
  733. public:
  734. IteratorNext()
  735. : Instruction(Type::IteratorNext)
  736. {
  737. }
  738. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  739. String to_string_impl(Bytecode::Executable const&) const;
  740. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  741. };
  742. class IteratorResultDone final : public Instruction {
  743. public:
  744. IteratorResultDone()
  745. : Instruction(Type::IteratorResultDone)
  746. {
  747. }
  748. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  749. String to_string_impl(Bytecode::Executable const&) const;
  750. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  751. };
  752. class IteratorResultValue final : public Instruction {
  753. public:
  754. IteratorResultValue()
  755. : Instruction(Type::IteratorResultValue)
  756. {
  757. }
  758. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  759. String to_string_impl(Bytecode::Executable const&) const;
  760. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  761. };
  762. class ResolveThisBinding final : public Instruction {
  763. public:
  764. explicit ResolveThisBinding()
  765. : Instruction(Type::ResolveThisBinding)
  766. {
  767. }
  768. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  769. String to_string_impl(Bytecode::Executable const&) const;
  770. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  771. };
  772. class GetNewTarget final : public Instruction {
  773. public:
  774. explicit GetNewTarget()
  775. : Instruction(Type::GetNewTarget)
  776. {
  777. }
  778. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  779. String to_string_impl(Bytecode::Executable const&) const;
  780. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  781. };
  782. class TypeofVariable final : public Instruction {
  783. public:
  784. explicit TypeofVariable(IdentifierTableIndex identifier)
  785. : Instruction(Type::TypeofVariable)
  786. , m_identifier(identifier)
  787. {
  788. }
  789. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  790. String to_string_impl(Bytecode::Executable const&) const;
  791. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  792. private:
  793. IdentifierTableIndex m_identifier;
  794. };
  795. }
  796. namespace JS::Bytecode {
  797. ALWAYS_INLINE ThrowCompletionOr<void> Instruction::execute(Bytecode::Interpreter& interpreter) const
  798. {
  799. #define __BYTECODE_OP(op) \
  800. case Instruction::Type::op: \
  801. return static_cast<Bytecode::Op::op const&>(*this).execute_impl(interpreter);
  802. switch (type()) {
  803. ENUMERATE_BYTECODE_OPS(__BYTECODE_OP)
  804. default:
  805. VERIFY_NOT_REACHED();
  806. }
  807. #undef __BYTECODE_OP
  808. }
  809. ALWAYS_INLINE void Instruction::replace_references(BasicBlock const& from, BasicBlock const& to)
  810. {
  811. #define __BYTECODE_OP(op) \
  812. case Instruction::Type::op: \
  813. return static_cast<Bytecode::Op::op&>(*this).replace_references_impl(from, to);
  814. switch (type()) {
  815. ENUMERATE_BYTECODE_OPS(__BYTECODE_OP)
  816. default:
  817. VERIFY_NOT_REACHED();
  818. }
  819. #undef __BYTECODE_OP
  820. }
  821. ALWAYS_INLINE size_t Instruction::length() const
  822. {
  823. if (type() == Type::NewArray)
  824. return round_up_to_power_of_two(static_cast<Op::NewArray const&>(*this).length_impl(), alignof(void*));
  825. if (type() == Type::CopyObjectExcludingProperties)
  826. return round_up_to_power_of_two(static_cast<Op::CopyObjectExcludingProperties const&>(*this).length_impl(), alignof(void*));
  827. #define __BYTECODE_OP(op) \
  828. case Type::op: \
  829. return sizeof(Op::op);
  830. switch (type()) {
  831. ENUMERATE_BYTECODE_OPS(__BYTECODE_OP)
  832. default:
  833. VERIFY_NOT_REACHED();
  834. }
  835. #undef __BYTECODE_OP
  836. }
  837. ALWAYS_INLINE bool Instruction::is_terminator() const
  838. {
  839. #define __BYTECODE_OP(op) \
  840. case Type::op: \
  841. return Op::op::IsTerminator;
  842. switch (type()) {
  843. ENUMERATE_BYTECODE_OPS(__BYTECODE_OP)
  844. default:
  845. VERIFY_NOT_REACHED();
  846. }
  847. #undef __BYTECODE_OP
  848. }
  849. }