Op.h 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370
  1. /*
  2. * Copyright (c) 2021-2023, 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/IteratorOperations.h>
  20. #include <LibJS/Runtime/Value.h>
  21. #include <LibJS/Runtime/ValueTraits.h>
  22. namespace JS {
  23. class FunctionExpression;
  24. }
  25. namespace JS::Bytecode::Op {
  26. class Load final : public Instruction {
  27. public:
  28. explicit Load(Register src)
  29. : Instruction(Type::Load)
  30. , m_src(src)
  31. {
  32. }
  33. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  34. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  35. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  36. void replace_references_impl(Register from, Register to)
  37. {
  38. if (m_src == from)
  39. m_src = to;
  40. }
  41. private:
  42. Register m_src;
  43. };
  44. class LoadImmediate final : public Instruction {
  45. public:
  46. explicit LoadImmediate(Value value)
  47. : Instruction(Type::LoadImmediate)
  48. , m_value(value)
  49. {
  50. }
  51. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  52. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  53. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  54. void replace_references_impl(Register, Register) { }
  55. private:
  56. Value m_value;
  57. };
  58. class Store final : public Instruction {
  59. public:
  60. explicit Store(Register dst)
  61. : Instruction(Type::Store)
  62. , m_dst(dst)
  63. {
  64. }
  65. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  66. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  67. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  68. void replace_references_impl(Register, Register) { }
  69. Register dst() const { return m_dst; }
  70. private:
  71. Register m_dst;
  72. };
  73. #define JS_ENUMERATE_COMMON_BINARY_OPS(O) \
  74. O(Add, add) \
  75. O(Sub, sub) \
  76. O(Mul, mul) \
  77. O(Div, div) \
  78. O(Exp, exp) \
  79. O(Mod, mod) \
  80. O(In, in) \
  81. O(InstanceOf, instance_of) \
  82. O(GreaterThan, greater_than) \
  83. O(GreaterThanEquals, greater_than_equals) \
  84. O(LessThan, less_than) \
  85. O(LessThanEquals, less_than_equals) \
  86. O(LooselyInequals, abstract_inequals) \
  87. O(LooselyEquals, abstract_equals) \
  88. O(StrictlyInequals, typed_inequals) \
  89. O(StrictlyEquals, typed_equals) \
  90. O(BitwiseAnd, bitwise_and) \
  91. O(BitwiseOr, bitwise_or) \
  92. O(BitwiseXor, bitwise_xor) \
  93. O(LeftShift, left_shift) \
  94. O(RightShift, right_shift) \
  95. O(UnsignedRightShift, unsigned_right_shift)
  96. #define JS_DECLARE_COMMON_BINARY_OP(OpTitleCase, op_snake_case) \
  97. class OpTitleCase final : public Instruction { \
  98. public: \
  99. explicit OpTitleCase(Register lhs_reg) \
  100. : Instruction(Type::OpTitleCase) \
  101. , m_lhs_reg(lhs_reg) \
  102. { \
  103. } \
  104. \
  105. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const; \
  106. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const; \
  107. void replace_references_impl(BasicBlock const&, BasicBlock const&) \
  108. { \
  109. } \
  110. void replace_references_impl(Register from, Register to) \
  111. { \
  112. if (m_lhs_reg == from) \
  113. m_lhs_reg = to; \
  114. } \
  115. \
  116. private: \
  117. Register m_lhs_reg; \
  118. };
  119. JS_ENUMERATE_COMMON_BINARY_OPS(JS_DECLARE_COMMON_BINARY_OP)
  120. #undef JS_DECLARE_COMMON_BINARY_OP
  121. #define JS_ENUMERATE_COMMON_UNARY_OPS(O) \
  122. O(BitwiseNot, bitwise_not) \
  123. O(Not, not_) \
  124. O(UnaryPlus, unary_plus) \
  125. O(UnaryMinus, unary_minus) \
  126. O(Typeof, typeof_)
  127. #define JS_DECLARE_COMMON_UNARY_OP(OpTitleCase, op_snake_case) \
  128. class OpTitleCase final : public Instruction { \
  129. public: \
  130. OpTitleCase() \
  131. : Instruction(Type::OpTitleCase) \
  132. { \
  133. } \
  134. \
  135. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const; \
  136. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const; \
  137. void replace_references_impl(BasicBlock const&, BasicBlock const&) \
  138. { \
  139. } \
  140. void replace_references_impl(Register, Register) \
  141. { \
  142. } \
  143. };
  144. JS_ENUMERATE_COMMON_UNARY_OPS(JS_DECLARE_COMMON_UNARY_OP)
  145. #undef JS_DECLARE_COMMON_UNARY_OP
  146. class NewString final : public Instruction {
  147. public:
  148. explicit NewString(StringTableIndex string)
  149. : Instruction(Type::NewString)
  150. , m_string(string)
  151. {
  152. }
  153. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  154. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  155. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  156. void replace_references_impl(Register, Register) { }
  157. private:
  158. StringTableIndex m_string;
  159. };
  160. class NewObject final : public Instruction {
  161. public:
  162. NewObject()
  163. : Instruction(Type::NewObject)
  164. {
  165. }
  166. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  167. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  168. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  169. void replace_references_impl(Register, Register) { }
  170. };
  171. class NewRegExp final : public Instruction {
  172. public:
  173. NewRegExp(StringTableIndex source_index, StringTableIndex flags_index)
  174. : Instruction(Type::NewRegExp)
  175. , m_source_index(source_index)
  176. , m_flags_index(flags_index)
  177. {
  178. }
  179. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  180. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  181. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  182. void replace_references_impl(Register, Register) { }
  183. private:
  184. StringTableIndex m_source_index;
  185. StringTableIndex m_flags_index;
  186. };
  187. #define JS_ENUMERATE_NEW_BUILTIN_ERROR_OPS(O) \
  188. O(TypeError)
  189. #define JS_DECLARE_NEW_BUILTIN_ERROR_OP(ErrorName) \
  190. class New##ErrorName final : public Instruction { \
  191. public: \
  192. explicit New##ErrorName(StringTableIndex error_string) \
  193. : Instruction(Type::New##ErrorName) \
  194. , m_error_string(error_string) \
  195. { \
  196. } \
  197. \
  198. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const; \
  199. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const; \
  200. void replace_references_impl(BasicBlock const&, BasicBlock const&) \
  201. { \
  202. } \
  203. void replace_references_impl(Register, Register) \
  204. { \
  205. } \
  206. \
  207. private: \
  208. StringTableIndex m_error_string; \
  209. };
  210. JS_ENUMERATE_NEW_BUILTIN_ERROR_OPS(JS_DECLARE_NEW_BUILTIN_ERROR_OP)
  211. #undef JS_DECLARE_NEW_BUILTIN_ERROR_OP
  212. // NOTE: This instruction is variable-width depending on the number of excluded names
  213. class CopyObjectExcludingProperties final : public Instruction {
  214. public:
  215. CopyObjectExcludingProperties(Register from_object, Vector<Register> const& excluded_names)
  216. : Instruction(Type::CopyObjectExcludingProperties)
  217. , m_from_object(from_object)
  218. , m_excluded_names_count(excluded_names.size())
  219. {
  220. for (size_t i = 0; i < m_excluded_names_count; i++)
  221. m_excluded_names[i] = excluded_names[i];
  222. }
  223. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  224. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  225. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  226. void replace_references_impl(Register from, Register to);
  227. size_t length_impl() const { return sizeof(*this) + sizeof(Register) * m_excluded_names_count; }
  228. private:
  229. Register m_from_object;
  230. size_t m_excluded_names_count { 0 };
  231. Register m_excluded_names[];
  232. };
  233. class NewBigInt final : public Instruction {
  234. public:
  235. explicit NewBigInt(Crypto::SignedBigInteger bigint)
  236. : Instruction(Type::NewBigInt)
  237. , m_bigint(move(bigint))
  238. {
  239. }
  240. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  241. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  242. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  243. void replace_references_impl(Register, Register) { }
  244. private:
  245. Crypto::SignedBigInteger m_bigint;
  246. };
  247. // NOTE: This instruction is variable-width depending on the number of elements!
  248. class NewArray final : public Instruction {
  249. public:
  250. NewArray()
  251. : Instruction(Type::NewArray)
  252. , m_element_count(0)
  253. {
  254. }
  255. explicit NewArray(AK::Array<Register, 2> const& elements_range)
  256. : Instruction(Type::NewArray)
  257. , m_element_count(elements_range[1].index() - elements_range[0].index() + 1)
  258. {
  259. m_elements[0] = elements_range[0];
  260. m_elements[1] = elements_range[1];
  261. }
  262. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  263. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  264. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  265. // Note: The underlying element range shall never be changed item, by item
  266. // shifting it may be done in the future
  267. void replace_references_impl(Register from, Register) { VERIFY(!m_element_count || from.index() < start().index() || from.index() > end().index()); }
  268. size_t length_impl() const
  269. {
  270. return sizeof(*this) + sizeof(Register) * (m_element_count == 0 ? 0 : 2);
  271. }
  272. Register start() const
  273. {
  274. VERIFY(m_element_count);
  275. return m_elements[0];
  276. }
  277. Register end() const
  278. {
  279. VERIFY(m_element_count);
  280. return m_elements[1];
  281. }
  282. size_t element_count() const { return m_element_count; }
  283. private:
  284. size_t m_element_count { 0 };
  285. Register m_elements[];
  286. };
  287. class Append final : public Instruction {
  288. public:
  289. Append(Register lhs, bool is_spread)
  290. : Instruction(Type::Append)
  291. , m_lhs(lhs)
  292. , m_is_spread(is_spread)
  293. {
  294. }
  295. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  296. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  297. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  298. // Note: This should never do anything, the lhs should always be an array, that is currently being constructed
  299. void replace_references_impl(Register from, Register) { VERIFY(from != m_lhs); }
  300. private:
  301. Register m_lhs;
  302. bool m_is_spread = false;
  303. };
  304. class ImportCall final : public Instruction {
  305. public:
  306. ImportCall(Register specifier, Register options)
  307. : Instruction(Type::ImportCall)
  308. , m_specifier(specifier)
  309. , m_options(options)
  310. {
  311. }
  312. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  313. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  314. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  315. void replace_references_impl(Register, Register);
  316. private:
  317. Register m_specifier;
  318. Register m_options;
  319. };
  320. class IteratorToArray final : public Instruction {
  321. public:
  322. IteratorToArray()
  323. : Instruction(Type::IteratorToArray)
  324. {
  325. }
  326. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  327. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  328. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  329. void replace_references_impl(Register, Register) { }
  330. };
  331. class ConcatString final : public Instruction {
  332. public:
  333. explicit ConcatString(Register lhs)
  334. : Instruction(Type::ConcatString)
  335. , m_lhs(lhs)
  336. {
  337. }
  338. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  339. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  340. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  341. // Note: lhs should always be a string in construction, so this should never do anything
  342. void replace_references_impl(Register from, Register) { VERIFY(from != m_lhs); }
  343. private:
  344. Register m_lhs;
  345. };
  346. enum class EnvironmentMode {
  347. Lexical,
  348. Var,
  349. };
  350. class CreateLexicalEnvironment final : public Instruction {
  351. public:
  352. explicit CreateLexicalEnvironment()
  353. : Instruction(Type::CreateLexicalEnvironment)
  354. {
  355. }
  356. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  357. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  358. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  359. void replace_references_impl(Register, Register) { }
  360. };
  361. class EnterObjectEnvironment final : public Instruction {
  362. public:
  363. explicit EnterObjectEnvironment()
  364. : Instruction(Type::EnterObjectEnvironment)
  365. {
  366. }
  367. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  368. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  369. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  370. void replace_references_impl(Register, Register) { }
  371. };
  372. class CreateVariable final : public Instruction {
  373. public:
  374. explicit CreateVariable(IdentifierTableIndex identifier, EnvironmentMode mode, bool is_immutable, bool is_global = false)
  375. : Instruction(Type::CreateVariable)
  376. , m_identifier(identifier)
  377. , m_mode(mode)
  378. , m_is_immutable(is_immutable)
  379. , m_is_global(is_global)
  380. {
  381. }
  382. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  383. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  384. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  385. void replace_references_impl(Register, Register) { }
  386. private:
  387. IdentifierTableIndex m_identifier;
  388. EnvironmentMode m_mode;
  389. bool m_is_immutable : 4 { false };
  390. bool m_is_global : 4 { false };
  391. };
  392. class SetVariable final : public Instruction {
  393. public:
  394. enum class InitializationMode {
  395. Initialize,
  396. Set,
  397. InitializeOrSet,
  398. };
  399. explicit SetVariable(IdentifierTableIndex identifier, InitializationMode initialization_mode = InitializationMode::Set, EnvironmentMode mode = EnvironmentMode::Lexical)
  400. : Instruction(Type::SetVariable)
  401. , m_identifier(identifier)
  402. , m_mode(mode)
  403. , m_initialization_mode(initialization_mode)
  404. {
  405. }
  406. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  407. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  408. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  409. void replace_references_impl(Register, Register) { }
  410. IdentifierTableIndex identifier() const { return m_identifier; }
  411. private:
  412. IdentifierTableIndex m_identifier;
  413. EnvironmentMode m_mode;
  414. InitializationMode m_initialization_mode { InitializationMode::Set };
  415. };
  416. class GetVariable final : public Instruction {
  417. public:
  418. explicit GetVariable(IdentifierTableIndex identifier)
  419. : Instruction(Type::GetVariable)
  420. , m_identifier(identifier)
  421. {
  422. }
  423. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  424. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  425. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  426. void replace_references_impl(Register, Register) { }
  427. IdentifierTableIndex identifier() const { return m_identifier; }
  428. private:
  429. IdentifierTableIndex m_identifier;
  430. Optional<EnvironmentCoordinate> mutable m_cached_environment_coordinate;
  431. };
  432. class DeleteVariable final : public Instruction {
  433. public:
  434. explicit DeleteVariable(IdentifierTableIndex identifier)
  435. : Instruction(Type::DeleteVariable)
  436. , m_identifier(identifier)
  437. {
  438. }
  439. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  440. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  441. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  442. void replace_references_impl(Register, Register) { }
  443. IdentifierTableIndex identifier() const { return m_identifier; }
  444. private:
  445. IdentifierTableIndex m_identifier;
  446. };
  447. class GetById final : public Instruction {
  448. public:
  449. explicit GetById(IdentifierTableIndex property)
  450. : Instruction(Type::GetById)
  451. , m_property(property)
  452. {
  453. }
  454. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  455. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  456. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  457. void replace_references_impl(Register, Register) { }
  458. private:
  459. IdentifierTableIndex m_property;
  460. };
  461. class GetPrivateById final : public Instruction {
  462. public:
  463. explicit GetPrivateById(IdentifierTableIndex property)
  464. : Instruction(Type::GetPrivateById)
  465. , m_property(property)
  466. {
  467. }
  468. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  469. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  470. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  471. void replace_references_impl(Register, Register) { }
  472. private:
  473. IdentifierTableIndex m_property;
  474. };
  475. enum class PropertyKind {
  476. Getter,
  477. Setter,
  478. KeyValue,
  479. Spread,
  480. ProtoSetter,
  481. };
  482. class PutById final : public Instruction {
  483. public:
  484. explicit PutById(Register base, IdentifierTableIndex property, PropertyKind kind = PropertyKind::KeyValue)
  485. : Instruction(Type::PutById)
  486. , m_base(base)
  487. , m_property(property)
  488. , m_kind(kind)
  489. {
  490. }
  491. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  492. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  493. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  494. void replace_references_impl(Register from, Register to)
  495. {
  496. if (m_base == from)
  497. m_base = to;
  498. }
  499. private:
  500. Register m_base;
  501. IdentifierTableIndex m_property;
  502. PropertyKind m_kind;
  503. };
  504. class PutPrivateById final : public Instruction {
  505. public:
  506. explicit PutPrivateById(Register base, IdentifierTableIndex property, PropertyKind kind = PropertyKind::KeyValue)
  507. : Instruction(Type::PutPrivateById)
  508. , m_base(base)
  509. , m_property(property)
  510. , m_kind(kind)
  511. {
  512. }
  513. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  514. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  515. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  516. void replace_references_impl(Register from, Register to)
  517. {
  518. if (m_base == from)
  519. m_base = to;
  520. }
  521. private:
  522. Register m_base;
  523. IdentifierTableIndex m_property;
  524. PropertyKind m_kind;
  525. };
  526. class DeleteById final : public Instruction {
  527. public:
  528. explicit DeleteById(IdentifierTableIndex property)
  529. : Instruction(Type::DeleteById)
  530. , m_property(property)
  531. {
  532. }
  533. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  534. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  535. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  536. void replace_references_impl(Register, Register) { }
  537. private:
  538. IdentifierTableIndex m_property;
  539. };
  540. class GetByValue final : public Instruction {
  541. public:
  542. explicit GetByValue(Register base)
  543. : Instruction(Type::GetByValue)
  544. , m_base(base)
  545. {
  546. }
  547. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  548. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  549. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  550. void replace_references_impl(Register from, Register to)
  551. {
  552. if (m_base == from)
  553. m_base = to;
  554. }
  555. private:
  556. Register m_base;
  557. };
  558. class PutByValue final : public Instruction {
  559. public:
  560. PutByValue(Register base, Register property, PropertyKind kind = PropertyKind::KeyValue)
  561. : Instruction(Type::PutByValue)
  562. , m_base(base)
  563. , m_property(property)
  564. , m_kind(kind)
  565. {
  566. }
  567. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  568. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  569. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  570. void replace_references_impl(Register from, Register to)
  571. {
  572. if (m_base == from)
  573. m_base = to;
  574. }
  575. private:
  576. Register m_base;
  577. Register m_property;
  578. PropertyKind m_kind;
  579. };
  580. class DeleteByValue final : public Instruction {
  581. public:
  582. DeleteByValue(Register base)
  583. : Instruction(Type::DeleteByValue)
  584. , m_base(base)
  585. {
  586. }
  587. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  588. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  589. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  590. void replace_references_impl(Register from, Register to)
  591. {
  592. if (m_base == from)
  593. m_base = to;
  594. }
  595. private:
  596. Register m_base;
  597. };
  598. class Jump : public Instruction {
  599. public:
  600. constexpr static bool IsTerminator = true;
  601. explicit Jump(Type type, Optional<Label> taken_target = {}, Optional<Label> nontaken_target = {})
  602. : Instruction(type)
  603. , m_true_target(move(taken_target))
  604. , m_false_target(move(nontaken_target))
  605. {
  606. }
  607. explicit Jump(Optional<Label> taken_target = {}, Optional<Label> nontaken_target = {})
  608. : Instruction(Type::Jump)
  609. , m_true_target(move(taken_target))
  610. , m_false_target(move(nontaken_target))
  611. {
  612. }
  613. void set_targets(Optional<Label> true_target, Optional<Label> false_target)
  614. {
  615. m_true_target = move(true_target);
  616. m_false_target = move(false_target);
  617. }
  618. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  619. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  620. void replace_references_impl(BasicBlock const&, BasicBlock const&);
  621. void replace_references_impl(Register, Register) { }
  622. auto& true_target() const { return m_true_target; }
  623. auto& false_target() const { return m_false_target; }
  624. protected:
  625. Optional<Label> m_true_target;
  626. Optional<Label> m_false_target;
  627. };
  628. class JumpConditional final : public Jump {
  629. public:
  630. explicit JumpConditional(Optional<Label> true_target = {}, Optional<Label> false_target = {})
  631. : Jump(Type::JumpConditional, move(true_target), move(false_target))
  632. {
  633. }
  634. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  635. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  636. };
  637. class JumpNullish final : public Jump {
  638. public:
  639. explicit JumpNullish(Optional<Label> true_target = {}, Optional<Label> false_target = {})
  640. : Jump(Type::JumpNullish, move(true_target), move(false_target))
  641. {
  642. }
  643. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  644. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  645. };
  646. class JumpUndefined final : public Jump {
  647. public:
  648. explicit JumpUndefined(Optional<Label> true_target = {}, Optional<Label> false_target = {})
  649. : Jump(Type::JumpUndefined, move(true_target), move(false_target))
  650. {
  651. }
  652. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  653. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  654. };
  655. // NOTE: This instruction is variable-width depending on the number of arguments!
  656. class Call final : public Instruction {
  657. public:
  658. enum class CallType {
  659. Call,
  660. Construct,
  661. DirectEval,
  662. };
  663. Call(CallType type, Register callee, Register this_value, Optional<StringTableIndex> expression_string = {})
  664. : Instruction(Type::Call)
  665. , m_callee(callee)
  666. , m_this_value(this_value)
  667. , m_type(type)
  668. , m_expression_string(expression_string)
  669. {
  670. }
  671. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  672. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  673. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  674. void replace_references_impl(Register, Register);
  675. Completion throw_type_error_for_callee(Bytecode::Interpreter&, StringView callee_type) const;
  676. private:
  677. Register m_callee;
  678. Register m_this_value;
  679. CallType m_type;
  680. Optional<StringTableIndex> m_expression_string;
  681. };
  682. // NOTE: This instruction is variable-width depending on the number of arguments!
  683. class SuperCall : public Instruction {
  684. public:
  685. explicit SuperCall(bool is_synthetic)
  686. : Instruction(Type::SuperCall)
  687. , m_is_synthetic(is_synthetic)
  688. {
  689. }
  690. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  691. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  692. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  693. void replace_references_impl(Register, Register) { }
  694. private:
  695. bool m_is_synthetic;
  696. };
  697. class NewClass final : public Instruction {
  698. public:
  699. explicit NewClass(ClassExpression const& class_expression, Optional<DeprecatedFlyString const&> lhs_name)
  700. : Instruction(Type::NewClass)
  701. , m_class_expression(class_expression)
  702. , m_lhs_name(lhs_name.has_value() ? *lhs_name : Optional<DeprecatedFlyString> {})
  703. {
  704. }
  705. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  706. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  707. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  708. void replace_references_impl(Register, Register) { }
  709. private:
  710. ClassExpression const& m_class_expression;
  711. Optional<DeprecatedFlyString> m_lhs_name;
  712. };
  713. class NewFunction final : public Instruction {
  714. public:
  715. explicit NewFunction(FunctionExpression const& function_node, Optional<DeprecatedFlyString const&> lhs_name, Optional<Register> home_object = {})
  716. : Instruction(Type::NewFunction)
  717. , m_function_node(function_node)
  718. , m_lhs_name(lhs_name.has_value() ? *lhs_name : Optional<DeprecatedFlyString> {})
  719. , m_home_object(move(home_object))
  720. {
  721. }
  722. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  723. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  724. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  725. void replace_references_impl(Register, Register);
  726. private:
  727. FunctionExpression const& m_function_node;
  728. Optional<DeprecatedFlyString> m_lhs_name;
  729. Optional<Register> m_home_object;
  730. };
  731. class BlockDeclarationInstantiation final : public Instruction {
  732. public:
  733. explicit BlockDeclarationInstantiation(ScopeNode const& scope_node)
  734. : Instruction(Type::BlockDeclarationInstantiation)
  735. , m_scope_node(scope_node)
  736. {
  737. }
  738. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  739. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  740. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  741. void replace_references_impl(Register, Register) { }
  742. private:
  743. ScopeNode const& m_scope_node;
  744. };
  745. class Return final : public Instruction {
  746. public:
  747. constexpr static bool IsTerminator = true;
  748. Return()
  749. : Instruction(Type::Return)
  750. {
  751. }
  752. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  753. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  754. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  755. void replace_references_impl(Register, Register) { }
  756. };
  757. class Increment final : public Instruction {
  758. public:
  759. Increment()
  760. : Instruction(Type::Increment)
  761. {
  762. }
  763. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  764. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  765. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  766. void replace_references_impl(Register, Register) { }
  767. };
  768. class Decrement final : public Instruction {
  769. public:
  770. Decrement()
  771. : Instruction(Type::Decrement)
  772. {
  773. }
  774. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  775. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  776. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  777. void replace_references_impl(Register, Register) { }
  778. };
  779. class ToNumeric final : public Instruction {
  780. public:
  781. ToNumeric()
  782. : Instruction(Type::ToNumeric)
  783. {
  784. }
  785. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  786. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  787. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  788. void replace_references_impl(Register, Register) { }
  789. };
  790. class Throw final : public Instruction {
  791. public:
  792. constexpr static bool IsTerminator = true;
  793. Throw()
  794. : Instruction(Type::Throw)
  795. {
  796. }
  797. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  798. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  799. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  800. void replace_references_impl(Register, Register) { }
  801. };
  802. class ThrowIfNotObject final : public Instruction {
  803. public:
  804. ThrowIfNotObject()
  805. : Instruction(Type::ThrowIfNotObject)
  806. {
  807. }
  808. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  809. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  810. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  811. void replace_references_impl(Register, Register) { }
  812. };
  813. class ThrowIfNullish final : public Instruction {
  814. public:
  815. ThrowIfNullish()
  816. : Instruction(Type::ThrowIfNullish)
  817. {
  818. }
  819. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  820. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  821. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  822. void replace_references_impl(Register, Register) { }
  823. };
  824. class EnterUnwindContext final : public Instruction {
  825. public:
  826. constexpr static bool IsTerminator = true;
  827. EnterUnwindContext(Label entry_point, Optional<Label> handler_target, Optional<Label> finalizer_target)
  828. : Instruction(Type::EnterUnwindContext)
  829. , m_entry_point(move(entry_point))
  830. , m_handler_target(move(handler_target))
  831. , m_finalizer_target(move(finalizer_target))
  832. {
  833. }
  834. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  835. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  836. void replace_references_impl(BasicBlock const&, BasicBlock const&);
  837. void replace_references_impl(Register, Register) { }
  838. auto& entry_point() const { return m_entry_point; }
  839. auto& handler_target() const { return m_handler_target; }
  840. auto& finalizer_target() const { return m_finalizer_target; }
  841. private:
  842. Label m_entry_point;
  843. Optional<Label> m_handler_target;
  844. Optional<Label> m_finalizer_target;
  845. };
  846. class ScheduleJump final : public Instruction {
  847. public:
  848. // Note: We use this instruction to tell the next `finally` block to
  849. // continue execution with a specific break/continue target;
  850. // FIXME: We currently don't clear the interpreter internal flag, when we change
  851. // the control-flow (`break`, `continue`) in a finally-block,
  852. // FIXME: .NET on x86_64 uses a call to the finally instead, which could make this
  853. // easier, at the cost of making control-flow changes (`break`, `continue`, `return`)
  854. // in the finally-block more difficult, but as stated above, those
  855. // aren't handled 100% correctly at the moment anyway
  856. // It might be worth investigating a similar mechanism
  857. constexpr static bool IsTerminator = true;
  858. ScheduleJump(Label target)
  859. : Instruction(Type::ScheduleJump)
  860. , m_target(target)
  861. {
  862. }
  863. Label target() const { return m_target; }
  864. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  865. void replace_references_impl(BasicBlock const& from, BasicBlock const& to)
  866. {
  867. if (&m_target.block() == &from)
  868. m_target = Label { to };
  869. }
  870. void replace_references_impl(Register, Register) { }
  871. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  872. private:
  873. Label m_target;
  874. };
  875. class LeaveLexicalEnvironment final : public Instruction {
  876. public:
  877. LeaveLexicalEnvironment()
  878. : Instruction(Type::LeaveLexicalEnvironment)
  879. {
  880. }
  881. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  882. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  883. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  884. void replace_references_impl(Register, Register) { }
  885. };
  886. class LeaveUnwindContext final : public Instruction {
  887. public:
  888. LeaveUnwindContext()
  889. : Instruction(Type::LeaveUnwindContext)
  890. {
  891. }
  892. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  893. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  894. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  895. void replace_references_impl(Register, Register) { }
  896. };
  897. class ContinuePendingUnwind final : public Instruction {
  898. public:
  899. constexpr static bool IsTerminator = true;
  900. explicit ContinuePendingUnwind(Label resume_target)
  901. : Instruction(Type::ContinuePendingUnwind)
  902. , m_resume_target(resume_target)
  903. {
  904. }
  905. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  906. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  907. void replace_references_impl(BasicBlock const&, BasicBlock const&);
  908. void replace_references_impl(Register, Register) { }
  909. auto& resume_target() const { return m_resume_target; }
  910. private:
  911. Label m_resume_target;
  912. };
  913. class Yield final : public Instruction {
  914. public:
  915. constexpr static bool IsTerminator = true;
  916. explicit Yield(Label continuation_label)
  917. : Instruction(Type::Yield)
  918. , m_continuation_label(continuation_label)
  919. {
  920. }
  921. explicit Yield(nullptr_t)
  922. : Instruction(Type::Yield)
  923. {
  924. }
  925. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  926. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  927. void replace_references_impl(BasicBlock const&, BasicBlock const&);
  928. void replace_references_impl(Register, Register) { }
  929. auto& continuation() const { return m_continuation_label; }
  930. private:
  931. Optional<Label> m_continuation_label;
  932. };
  933. class PushDeclarativeEnvironment final : public Instruction {
  934. public:
  935. explicit PushDeclarativeEnvironment(HashMap<u32, Variable> variables)
  936. : Instruction(Type::PushDeclarativeEnvironment)
  937. , m_variables(move(variables))
  938. {
  939. }
  940. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  941. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  942. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  943. void replace_references_impl(Register, Register) { }
  944. private:
  945. HashMap<u32, Variable> m_variables;
  946. };
  947. class GetIterator final : public Instruction {
  948. public:
  949. GetIterator(IteratorHint hint = IteratorHint::Sync)
  950. : Instruction(Type::GetIterator)
  951. , m_hint(hint)
  952. {
  953. }
  954. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  955. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  956. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  957. void replace_references_impl(Register, Register) { }
  958. private:
  959. IteratorHint m_hint { IteratorHint::Sync };
  960. };
  961. class GetMethod final : public Instruction {
  962. public:
  963. GetMethod(IdentifierTableIndex property)
  964. : Instruction(Type::GetMethod)
  965. , m_property(property)
  966. {
  967. }
  968. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  969. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  970. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  971. void replace_references_impl(Register, Register) { }
  972. private:
  973. IdentifierTableIndex m_property;
  974. };
  975. class GetObjectPropertyIterator final : public Instruction {
  976. public:
  977. GetObjectPropertyIterator()
  978. : Instruction(Type::GetObjectPropertyIterator)
  979. {
  980. }
  981. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  982. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  983. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  984. void replace_references_impl(Register, Register) { }
  985. };
  986. class IteratorClose final : public Instruction {
  987. public:
  988. IteratorClose(Completion::Type completion_type, Optional<Value> completion_value)
  989. : Instruction(Type::IteratorClose)
  990. , m_completion_type(completion_type)
  991. , m_completion_value(completion_value)
  992. {
  993. }
  994. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  995. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  996. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  997. void replace_references_impl(Register, Register) { }
  998. private:
  999. Completion::Type m_completion_type { Completion::Type::Normal };
  1000. Optional<Value> m_completion_value;
  1001. };
  1002. class IteratorNext final : public Instruction {
  1003. public:
  1004. IteratorNext()
  1005. : Instruction(Type::IteratorNext)
  1006. {
  1007. }
  1008. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1009. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  1010. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  1011. void replace_references_impl(Register, Register) { }
  1012. };
  1013. class IteratorResultDone final : public Instruction {
  1014. public:
  1015. IteratorResultDone()
  1016. : Instruction(Type::IteratorResultDone)
  1017. {
  1018. }
  1019. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1020. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  1021. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  1022. void replace_references_impl(Register, Register) { }
  1023. };
  1024. class IteratorResultValue final : public Instruction {
  1025. public:
  1026. IteratorResultValue()
  1027. : Instruction(Type::IteratorResultValue)
  1028. {
  1029. }
  1030. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1031. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  1032. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  1033. void replace_references_impl(Register, Register) { }
  1034. };
  1035. class ResolveThisBinding final : public Instruction {
  1036. public:
  1037. explicit ResolveThisBinding()
  1038. : Instruction(Type::ResolveThisBinding)
  1039. {
  1040. }
  1041. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1042. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  1043. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  1044. void replace_references_impl(Register, Register) { }
  1045. };
  1046. class ResolveSuperBase final : public Instruction {
  1047. public:
  1048. explicit ResolveSuperBase()
  1049. : Instruction(Type::ResolveSuperBase)
  1050. {
  1051. }
  1052. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1053. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  1054. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  1055. void replace_references_impl(Register, Register) { }
  1056. };
  1057. class GetNewTarget final : public Instruction {
  1058. public:
  1059. explicit GetNewTarget()
  1060. : Instruction(Type::GetNewTarget)
  1061. {
  1062. }
  1063. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1064. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  1065. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  1066. void replace_references_impl(Register, Register) { }
  1067. };
  1068. class TypeofVariable final : public Instruction {
  1069. public:
  1070. explicit TypeofVariable(IdentifierTableIndex identifier)
  1071. : Instruction(Type::TypeofVariable)
  1072. , m_identifier(identifier)
  1073. {
  1074. }
  1075. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1076. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  1077. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  1078. void replace_references_impl(Register, Register) { }
  1079. private:
  1080. IdentifierTableIndex m_identifier;
  1081. };
  1082. }
  1083. namespace JS::Bytecode {
  1084. ALWAYS_INLINE ThrowCompletionOr<void> Instruction::execute(Bytecode::Interpreter& interpreter) const
  1085. {
  1086. #define __BYTECODE_OP(op) \
  1087. case Instruction::Type::op: \
  1088. return static_cast<Bytecode::Op::op const&>(*this).execute_impl(interpreter);
  1089. switch (type()) {
  1090. ENUMERATE_BYTECODE_OPS(__BYTECODE_OP)
  1091. default:
  1092. VERIFY_NOT_REACHED();
  1093. }
  1094. #undef __BYTECODE_OP
  1095. }
  1096. ALWAYS_INLINE void Instruction::replace_references(BasicBlock const& from, BasicBlock const& to)
  1097. {
  1098. #define __BYTECODE_OP(op) \
  1099. case Instruction::Type::op: \
  1100. return static_cast<Bytecode::Op::op&>(*this).replace_references_impl(from, to);
  1101. switch (type()) {
  1102. ENUMERATE_BYTECODE_OPS(__BYTECODE_OP)
  1103. default:
  1104. VERIFY_NOT_REACHED();
  1105. }
  1106. #undef __BYTECODE_OP
  1107. }
  1108. ALWAYS_INLINE void Instruction::replace_references(Register from, Register to)
  1109. {
  1110. #define __BYTECODE_OP(op) \
  1111. case Instruction::Type::op: \
  1112. return static_cast<Bytecode::Op::op&>(*this).replace_references_impl(from, to);
  1113. switch (type()) {
  1114. ENUMERATE_BYTECODE_OPS(__BYTECODE_OP)
  1115. default:
  1116. VERIFY_NOT_REACHED();
  1117. }
  1118. #undef __BYTECODE_OP
  1119. }
  1120. ALWAYS_INLINE size_t Instruction::length() const
  1121. {
  1122. if (type() == Type::NewArray)
  1123. return round_up_to_power_of_two(static_cast<Op::NewArray const&>(*this).length_impl(), alignof(void*));
  1124. if (type() == Type::CopyObjectExcludingProperties)
  1125. return round_up_to_power_of_two(static_cast<Op::CopyObjectExcludingProperties const&>(*this).length_impl(), alignof(void*));
  1126. #define __BYTECODE_OP(op) \
  1127. case Type::op: \
  1128. return sizeof(Op::op);
  1129. switch (type()) {
  1130. ENUMERATE_BYTECODE_OPS(__BYTECODE_OP)
  1131. default:
  1132. VERIFY_NOT_REACHED();
  1133. }
  1134. #undef __BYTECODE_OP
  1135. }
  1136. ALWAYS_INLINE bool Instruction::is_terminator() const
  1137. {
  1138. #define __BYTECODE_OP(op) \
  1139. case Type::op: \
  1140. return Op::op::IsTerminator;
  1141. switch (type()) {
  1142. ENUMERATE_BYTECODE_OPS(__BYTECODE_OP)
  1143. default:
  1144. VERIFY_NOT_REACHED();
  1145. }
  1146. #undef __BYTECODE_OP
  1147. }
  1148. }