Op.h 46 KB

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