Op.h 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406
  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. enum class CallType {
  656. Call,
  657. Construct,
  658. DirectEval,
  659. };
  660. class Call final : public Instruction {
  661. public:
  662. Call(CallType type, Register callee, Register this_value, Register first_argument, u32 argument_count, Optional<StringTableIndex> expression_string = {})
  663. : Instruction(Type::Call)
  664. , m_callee(callee)
  665. , m_this_value(this_value)
  666. , m_first_argument(first_argument)
  667. , m_argument_count(argument_count)
  668. , m_type(type)
  669. , m_expression_string(expression_string)
  670. {
  671. }
  672. CallType call_type() const { return m_type; }
  673. Register callee() const { return m_callee; }
  674. Register this_value() const { return m_this_value; }
  675. Optional<StringTableIndex> const& expression_string() const { return m_expression_string; }
  676. Register first_argument() const { return m_first_argument; }
  677. u32 argument_count() const { return m_argument_count; }
  678. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  679. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  680. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  681. void replace_references_impl(Register, Register);
  682. private:
  683. Register m_callee;
  684. Register m_this_value;
  685. Register m_first_argument;
  686. u32 m_argument_count { 0 };
  687. CallType m_type;
  688. Optional<StringTableIndex> m_expression_string;
  689. };
  690. class CallWithArgumentArray final : public Instruction {
  691. public:
  692. CallWithArgumentArray(CallType type, Register callee, Register this_value, Optional<StringTableIndex> expression_string = {})
  693. : Instruction(Type::CallWithArgumentArray)
  694. , m_callee(callee)
  695. , m_this_value(this_value)
  696. , m_type(type)
  697. , m_expression_string(expression_string)
  698. {
  699. }
  700. CallType call_type() const { return m_type; }
  701. Register callee() const { return m_callee; }
  702. Register this_value() const { return m_this_value; }
  703. Optional<StringTableIndex> const& expression_string() const { return m_expression_string; }
  704. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  705. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  706. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  707. void replace_references_impl(Register, Register);
  708. private:
  709. Register m_callee;
  710. Register m_this_value;
  711. CallType m_type;
  712. Optional<StringTableIndex> m_expression_string;
  713. };
  714. class SuperCallWithArgumentArray : public Instruction {
  715. public:
  716. explicit SuperCallWithArgumentArray(bool is_synthetic)
  717. : Instruction(Type::SuperCallWithArgumentArray)
  718. , m_is_synthetic(is_synthetic)
  719. {
  720. }
  721. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  722. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  723. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  724. void replace_references_impl(Register, Register) { }
  725. private:
  726. bool m_is_synthetic;
  727. };
  728. class NewClass final : public Instruction {
  729. public:
  730. explicit NewClass(ClassExpression const& class_expression, Optional<IdentifierTableIndex> lhs_name)
  731. : Instruction(Type::NewClass)
  732. , m_class_expression(class_expression)
  733. , m_lhs_name(lhs_name)
  734. {
  735. }
  736. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  737. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  738. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  739. void replace_references_impl(Register, Register) { }
  740. private:
  741. ClassExpression const& m_class_expression;
  742. Optional<IdentifierTableIndex> m_lhs_name;
  743. };
  744. class NewFunction final : public Instruction {
  745. public:
  746. explicit NewFunction(FunctionExpression const& function_node, Optional<IdentifierTableIndex> lhs_name, Optional<Register> home_object = {})
  747. : Instruction(Type::NewFunction)
  748. , m_function_node(function_node)
  749. , m_lhs_name(lhs_name)
  750. , m_home_object(move(home_object))
  751. {
  752. }
  753. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  754. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  755. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  756. void replace_references_impl(Register, Register);
  757. private:
  758. FunctionExpression const& m_function_node;
  759. Optional<IdentifierTableIndex> m_lhs_name;
  760. Optional<Register> m_home_object;
  761. };
  762. class BlockDeclarationInstantiation final : public Instruction {
  763. public:
  764. explicit BlockDeclarationInstantiation(ScopeNode const& scope_node)
  765. : Instruction(Type::BlockDeclarationInstantiation)
  766. , m_scope_node(scope_node)
  767. {
  768. }
  769. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  770. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  771. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  772. void replace_references_impl(Register, Register) { }
  773. private:
  774. ScopeNode const& m_scope_node;
  775. };
  776. class Return final : public Instruction {
  777. public:
  778. constexpr static bool IsTerminator = true;
  779. Return()
  780. : Instruction(Type::Return)
  781. {
  782. }
  783. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  784. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  785. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  786. void replace_references_impl(Register, Register) { }
  787. };
  788. class Increment final : public Instruction {
  789. public:
  790. Increment()
  791. : Instruction(Type::Increment)
  792. {
  793. }
  794. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  795. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  796. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  797. void replace_references_impl(Register, Register) { }
  798. };
  799. class Decrement final : public Instruction {
  800. public:
  801. Decrement()
  802. : Instruction(Type::Decrement)
  803. {
  804. }
  805. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  806. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  807. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  808. void replace_references_impl(Register, Register) { }
  809. };
  810. class ToNumeric final : public Instruction {
  811. public:
  812. ToNumeric()
  813. : Instruction(Type::ToNumeric)
  814. {
  815. }
  816. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  817. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  818. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  819. void replace_references_impl(Register, Register) { }
  820. };
  821. class Throw final : public Instruction {
  822. public:
  823. constexpr static bool IsTerminator = true;
  824. Throw()
  825. : Instruction(Type::Throw)
  826. {
  827. }
  828. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  829. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  830. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  831. void replace_references_impl(Register, Register) { }
  832. };
  833. class ThrowIfNotObject final : public Instruction {
  834. public:
  835. ThrowIfNotObject()
  836. : Instruction(Type::ThrowIfNotObject)
  837. {
  838. }
  839. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  840. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  841. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  842. void replace_references_impl(Register, Register) { }
  843. };
  844. class ThrowIfNullish final : public Instruction {
  845. public:
  846. ThrowIfNullish()
  847. : Instruction(Type::ThrowIfNullish)
  848. {
  849. }
  850. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  851. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  852. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  853. void replace_references_impl(Register, Register) { }
  854. };
  855. class EnterUnwindContext final : public Instruction {
  856. public:
  857. constexpr static bool IsTerminator = true;
  858. EnterUnwindContext(Label entry_point, Optional<Label> handler_target, Optional<Label> finalizer_target)
  859. : Instruction(Type::EnterUnwindContext)
  860. , m_entry_point(move(entry_point))
  861. , m_handler_target(move(handler_target))
  862. , m_finalizer_target(move(finalizer_target))
  863. {
  864. }
  865. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  866. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  867. void replace_references_impl(BasicBlock const&, BasicBlock const&);
  868. void replace_references_impl(Register, Register) { }
  869. auto& entry_point() const { return m_entry_point; }
  870. auto& handler_target() const { return m_handler_target; }
  871. auto& finalizer_target() const { return m_finalizer_target; }
  872. private:
  873. Label m_entry_point;
  874. Optional<Label> m_handler_target;
  875. Optional<Label> m_finalizer_target;
  876. };
  877. class ScheduleJump final : public Instruction {
  878. public:
  879. // Note: We use this instruction to tell the next `finally` block to
  880. // continue execution with a specific break/continue target;
  881. // FIXME: We currently don't clear the interpreter internal flag, when we change
  882. // the control-flow (`break`, `continue`) in a finally-block,
  883. // FIXME: .NET on x86_64 uses a call to the finally instead, which could make this
  884. // easier, at the cost of making control-flow changes (`break`, `continue`, `return`)
  885. // in the finally-block more difficult, but as stated above, those
  886. // aren't handled 100% correctly at the moment anyway
  887. // It might be worth investigating a similar mechanism
  888. constexpr static bool IsTerminator = true;
  889. ScheduleJump(Label target)
  890. : Instruction(Type::ScheduleJump)
  891. , m_target(target)
  892. {
  893. }
  894. Label target() const { return m_target; }
  895. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  896. void replace_references_impl(BasicBlock const& from, BasicBlock const& to)
  897. {
  898. if (&m_target.block() == &from)
  899. m_target = Label { to };
  900. }
  901. void replace_references_impl(Register, Register) { }
  902. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  903. private:
  904. Label m_target;
  905. };
  906. class LeaveLexicalEnvironment final : public Instruction {
  907. public:
  908. LeaveLexicalEnvironment()
  909. : Instruction(Type::LeaveLexicalEnvironment)
  910. {
  911. }
  912. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  913. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  914. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  915. void replace_references_impl(Register, Register) { }
  916. };
  917. class LeaveUnwindContext final : public Instruction {
  918. public:
  919. LeaveUnwindContext()
  920. : Instruction(Type::LeaveUnwindContext)
  921. {
  922. }
  923. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  924. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  925. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  926. void replace_references_impl(Register, Register) { }
  927. };
  928. class ContinuePendingUnwind final : public Instruction {
  929. public:
  930. constexpr static bool IsTerminator = true;
  931. explicit ContinuePendingUnwind(Label resume_target)
  932. : Instruction(Type::ContinuePendingUnwind)
  933. , m_resume_target(resume_target)
  934. {
  935. }
  936. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  937. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  938. void replace_references_impl(BasicBlock const&, BasicBlock const&);
  939. void replace_references_impl(Register, Register) { }
  940. auto& resume_target() const { return m_resume_target; }
  941. private:
  942. Label m_resume_target;
  943. };
  944. class Yield final : public Instruction {
  945. public:
  946. constexpr static bool IsTerminator = true;
  947. explicit Yield(Label continuation_label)
  948. : Instruction(Type::Yield)
  949. , m_continuation_label(continuation_label)
  950. {
  951. }
  952. explicit Yield(nullptr_t)
  953. : Instruction(Type::Yield)
  954. {
  955. }
  956. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  957. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  958. void replace_references_impl(BasicBlock const&, BasicBlock const&);
  959. void replace_references_impl(Register, Register) { }
  960. auto& continuation() const { return m_continuation_label; }
  961. private:
  962. Optional<Label> m_continuation_label;
  963. };
  964. class PushDeclarativeEnvironment final : public Instruction {
  965. public:
  966. explicit PushDeclarativeEnvironment(HashMap<u32, Variable> variables)
  967. : Instruction(Type::PushDeclarativeEnvironment)
  968. , m_variables(move(variables))
  969. {
  970. }
  971. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  972. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  973. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  974. void replace_references_impl(Register, Register) { }
  975. private:
  976. HashMap<u32, Variable> m_variables;
  977. };
  978. class GetIterator final : public Instruction {
  979. public:
  980. GetIterator(IteratorHint hint = IteratorHint::Sync)
  981. : Instruction(Type::GetIterator)
  982. , m_hint(hint)
  983. {
  984. }
  985. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  986. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  987. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  988. void replace_references_impl(Register, Register) { }
  989. private:
  990. IteratorHint m_hint { IteratorHint::Sync };
  991. };
  992. class GetMethod final : public Instruction {
  993. public:
  994. GetMethod(IdentifierTableIndex property)
  995. : Instruction(Type::GetMethod)
  996. , m_property(property)
  997. {
  998. }
  999. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1000. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  1001. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  1002. void replace_references_impl(Register, Register) { }
  1003. private:
  1004. IdentifierTableIndex m_property;
  1005. };
  1006. class GetObjectPropertyIterator final : public Instruction {
  1007. public:
  1008. GetObjectPropertyIterator()
  1009. : Instruction(Type::GetObjectPropertyIterator)
  1010. {
  1011. }
  1012. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1013. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  1014. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  1015. void replace_references_impl(Register, Register) { }
  1016. };
  1017. class IteratorClose final : public Instruction {
  1018. public:
  1019. IteratorClose(Completion::Type completion_type, Optional<Value> completion_value)
  1020. : Instruction(Type::IteratorClose)
  1021. , m_completion_type(completion_type)
  1022. , m_completion_value(completion_value)
  1023. {
  1024. }
  1025. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1026. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  1027. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  1028. void replace_references_impl(Register, Register) { }
  1029. private:
  1030. Completion::Type m_completion_type { Completion::Type::Normal };
  1031. Optional<Value> m_completion_value;
  1032. };
  1033. class IteratorNext final : public Instruction {
  1034. public:
  1035. IteratorNext()
  1036. : Instruction(Type::IteratorNext)
  1037. {
  1038. }
  1039. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1040. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  1041. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  1042. void replace_references_impl(Register, Register) { }
  1043. };
  1044. class IteratorResultDone final : public Instruction {
  1045. public:
  1046. IteratorResultDone()
  1047. : Instruction(Type::IteratorResultDone)
  1048. {
  1049. }
  1050. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1051. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  1052. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  1053. void replace_references_impl(Register, Register) { }
  1054. };
  1055. class IteratorResultValue final : public Instruction {
  1056. public:
  1057. IteratorResultValue()
  1058. : Instruction(Type::IteratorResultValue)
  1059. {
  1060. }
  1061. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1062. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  1063. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  1064. void replace_references_impl(Register, Register) { }
  1065. };
  1066. class ResolveThisBinding final : public Instruction {
  1067. public:
  1068. explicit ResolveThisBinding()
  1069. : Instruction(Type::ResolveThisBinding)
  1070. {
  1071. }
  1072. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1073. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  1074. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  1075. void replace_references_impl(Register, Register) { }
  1076. };
  1077. class ResolveSuperBase final : public Instruction {
  1078. public:
  1079. explicit ResolveSuperBase()
  1080. : Instruction(Type::ResolveSuperBase)
  1081. {
  1082. }
  1083. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1084. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  1085. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  1086. void replace_references_impl(Register, Register) { }
  1087. };
  1088. class GetNewTarget final : public Instruction {
  1089. public:
  1090. explicit GetNewTarget()
  1091. : Instruction(Type::GetNewTarget)
  1092. {
  1093. }
  1094. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1095. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  1096. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  1097. void replace_references_impl(Register, Register) { }
  1098. };
  1099. class TypeofVariable final : public Instruction {
  1100. public:
  1101. explicit TypeofVariable(IdentifierTableIndex identifier)
  1102. : Instruction(Type::TypeofVariable)
  1103. , m_identifier(identifier)
  1104. {
  1105. }
  1106. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1107. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  1108. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  1109. void replace_references_impl(Register, Register) { }
  1110. private:
  1111. IdentifierTableIndex m_identifier;
  1112. };
  1113. }
  1114. namespace JS::Bytecode {
  1115. ALWAYS_INLINE ThrowCompletionOr<void> Instruction::execute(Bytecode::Interpreter& interpreter) const
  1116. {
  1117. #define __BYTECODE_OP(op) \
  1118. case Instruction::Type::op: \
  1119. return static_cast<Bytecode::Op::op const&>(*this).execute_impl(interpreter);
  1120. switch (type()) {
  1121. ENUMERATE_BYTECODE_OPS(__BYTECODE_OP)
  1122. default:
  1123. VERIFY_NOT_REACHED();
  1124. }
  1125. #undef __BYTECODE_OP
  1126. }
  1127. ALWAYS_INLINE void Instruction::replace_references(BasicBlock const& from, BasicBlock const& to)
  1128. {
  1129. #define __BYTECODE_OP(op) \
  1130. case Instruction::Type::op: \
  1131. return static_cast<Bytecode::Op::op&>(*this).replace_references_impl(from, to);
  1132. switch (type()) {
  1133. ENUMERATE_BYTECODE_OPS(__BYTECODE_OP)
  1134. default:
  1135. VERIFY_NOT_REACHED();
  1136. }
  1137. #undef __BYTECODE_OP
  1138. }
  1139. ALWAYS_INLINE void Instruction::replace_references(Register from, Register to)
  1140. {
  1141. #define __BYTECODE_OP(op) \
  1142. case Instruction::Type::op: \
  1143. return static_cast<Bytecode::Op::op&>(*this).replace_references_impl(from, to);
  1144. switch (type()) {
  1145. ENUMERATE_BYTECODE_OPS(__BYTECODE_OP)
  1146. default:
  1147. VERIFY_NOT_REACHED();
  1148. }
  1149. #undef __BYTECODE_OP
  1150. }
  1151. ALWAYS_INLINE size_t Instruction::length() const
  1152. {
  1153. if (type() == Type::NewArray)
  1154. return round_up_to_power_of_two(static_cast<Op::NewArray const&>(*this).length_impl(), alignof(void*));
  1155. if (type() == Type::CopyObjectExcludingProperties)
  1156. return round_up_to_power_of_two(static_cast<Op::CopyObjectExcludingProperties const&>(*this).length_impl(), alignof(void*));
  1157. #define __BYTECODE_OP(op) \
  1158. case Type::op: \
  1159. return sizeof(Op::op);
  1160. switch (type()) {
  1161. ENUMERATE_BYTECODE_OPS(__BYTECODE_OP)
  1162. default:
  1163. VERIFY_NOT_REACHED();
  1164. }
  1165. #undef __BYTECODE_OP
  1166. }
  1167. ALWAYS_INLINE bool Instruction::is_terminator() const
  1168. {
  1169. #define __BYTECODE_OP(op) \
  1170. case Type::op: \
  1171. return Op::op::IsTerminator;
  1172. switch (type()) {
  1173. ENUMERATE_BYTECODE_OPS(__BYTECODE_OP)
  1174. default:
  1175. VERIFY_NOT_REACHED();
  1176. }
  1177. #undef __BYTECODE_OP
  1178. }
  1179. }