Op.h 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423
  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. class HasPrivateId final : public Instruction {
  476. public:
  477. explicit HasPrivateId(IdentifierTableIndex property)
  478. : Instruction(Type::HasPrivateId)
  479. , m_property(property)
  480. {
  481. }
  482. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  483. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  484. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  485. void replace_references_impl(Register, Register) { }
  486. private:
  487. IdentifierTableIndex m_property;
  488. };
  489. enum class PropertyKind {
  490. Getter,
  491. Setter,
  492. KeyValue,
  493. Spread,
  494. ProtoSetter,
  495. };
  496. class PutById final : public Instruction {
  497. public:
  498. explicit PutById(Register base, IdentifierTableIndex property, PropertyKind kind = PropertyKind::KeyValue)
  499. : Instruction(Type::PutById)
  500. , m_base(base)
  501. , m_property(property)
  502. , m_kind(kind)
  503. {
  504. }
  505. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  506. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  507. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  508. void replace_references_impl(Register from, Register to)
  509. {
  510. if (m_base == from)
  511. m_base = to;
  512. }
  513. private:
  514. Register m_base;
  515. IdentifierTableIndex m_property;
  516. PropertyKind m_kind;
  517. };
  518. class PutPrivateById final : public Instruction {
  519. public:
  520. explicit PutPrivateById(Register base, IdentifierTableIndex property, PropertyKind kind = PropertyKind::KeyValue)
  521. : Instruction(Type::PutPrivateById)
  522. , m_base(base)
  523. , m_property(property)
  524. , m_kind(kind)
  525. {
  526. }
  527. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  528. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  529. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  530. void replace_references_impl(Register from, Register to)
  531. {
  532. if (m_base == from)
  533. m_base = to;
  534. }
  535. private:
  536. Register m_base;
  537. IdentifierTableIndex m_property;
  538. PropertyKind m_kind;
  539. };
  540. class DeleteById final : public Instruction {
  541. public:
  542. explicit DeleteById(IdentifierTableIndex property)
  543. : Instruction(Type::DeleteById)
  544. , m_property(property)
  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, Register) { }
  551. private:
  552. IdentifierTableIndex m_property;
  553. };
  554. class GetByValue final : public Instruction {
  555. public:
  556. explicit GetByValue(Register base)
  557. : Instruction(Type::GetByValue)
  558. , m_base(base)
  559. {
  560. }
  561. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  562. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  563. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  564. void replace_references_impl(Register from, Register to)
  565. {
  566. if (m_base == from)
  567. m_base = to;
  568. }
  569. private:
  570. Register m_base;
  571. };
  572. class PutByValue final : public Instruction {
  573. public:
  574. PutByValue(Register base, Register property, PropertyKind kind = PropertyKind::KeyValue)
  575. : Instruction(Type::PutByValue)
  576. , m_base(base)
  577. , m_property(property)
  578. , m_kind(kind)
  579. {
  580. }
  581. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  582. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  583. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  584. void replace_references_impl(Register from, Register to)
  585. {
  586. if (m_base == from)
  587. m_base = to;
  588. }
  589. private:
  590. Register m_base;
  591. Register m_property;
  592. PropertyKind m_kind;
  593. };
  594. class DeleteByValue final : public Instruction {
  595. public:
  596. DeleteByValue(Register base)
  597. : Instruction(Type::DeleteByValue)
  598. , m_base(base)
  599. {
  600. }
  601. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  602. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  603. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  604. void replace_references_impl(Register from, Register to)
  605. {
  606. if (m_base == from)
  607. m_base = to;
  608. }
  609. private:
  610. Register m_base;
  611. };
  612. class Jump : public Instruction {
  613. public:
  614. constexpr static bool IsTerminator = true;
  615. explicit Jump(Type type, Optional<Label> taken_target = {}, Optional<Label> nontaken_target = {})
  616. : Instruction(type)
  617. , m_true_target(move(taken_target))
  618. , m_false_target(move(nontaken_target))
  619. {
  620. }
  621. explicit Jump(Optional<Label> taken_target = {}, Optional<Label> nontaken_target = {})
  622. : Instruction(Type::Jump)
  623. , m_true_target(move(taken_target))
  624. , m_false_target(move(nontaken_target))
  625. {
  626. }
  627. void set_targets(Optional<Label> true_target, Optional<Label> false_target)
  628. {
  629. m_true_target = move(true_target);
  630. m_false_target = move(false_target);
  631. }
  632. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  633. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  634. void replace_references_impl(BasicBlock const&, BasicBlock const&);
  635. void replace_references_impl(Register, Register) { }
  636. auto& true_target() const { return m_true_target; }
  637. auto& false_target() const { return m_false_target; }
  638. protected:
  639. Optional<Label> m_true_target;
  640. Optional<Label> m_false_target;
  641. };
  642. class JumpConditional final : public Jump {
  643. public:
  644. explicit JumpConditional(Optional<Label> true_target = {}, Optional<Label> false_target = {})
  645. : Jump(Type::JumpConditional, move(true_target), move(false_target))
  646. {
  647. }
  648. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  649. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  650. };
  651. class JumpNullish final : public Jump {
  652. public:
  653. explicit JumpNullish(Optional<Label> true_target = {}, Optional<Label> false_target = {})
  654. : Jump(Type::JumpNullish, move(true_target), move(false_target))
  655. {
  656. }
  657. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  658. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  659. };
  660. class JumpUndefined final : public Jump {
  661. public:
  662. explicit JumpUndefined(Optional<Label> true_target = {}, Optional<Label> false_target = {})
  663. : Jump(Type::JumpUndefined, move(true_target), move(false_target))
  664. {
  665. }
  666. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  667. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  668. };
  669. enum class CallType {
  670. Call,
  671. Construct,
  672. DirectEval,
  673. };
  674. class Call final : public Instruction {
  675. public:
  676. Call(CallType type, Register callee, Register this_value, Register first_argument, u32 argument_count, Optional<StringTableIndex> expression_string = {})
  677. : Instruction(Type::Call)
  678. , m_callee(callee)
  679. , m_this_value(this_value)
  680. , m_first_argument(first_argument)
  681. , m_argument_count(argument_count)
  682. , m_type(type)
  683. , m_expression_string(expression_string)
  684. {
  685. }
  686. CallType call_type() const { return m_type; }
  687. Register callee() const { return m_callee; }
  688. Register this_value() const { return m_this_value; }
  689. Optional<StringTableIndex> const& expression_string() const { return m_expression_string; }
  690. Register first_argument() const { return m_first_argument; }
  691. u32 argument_count() const { return m_argument_count; }
  692. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  693. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  694. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  695. void replace_references_impl(Register, Register);
  696. private:
  697. Register m_callee;
  698. Register m_this_value;
  699. Register m_first_argument;
  700. u32 m_argument_count { 0 };
  701. CallType m_type;
  702. Optional<StringTableIndex> m_expression_string;
  703. };
  704. class CallWithArgumentArray final : public Instruction {
  705. public:
  706. CallWithArgumentArray(CallType type, Register callee, Register this_value, Optional<StringTableIndex> expression_string = {})
  707. : Instruction(Type::CallWithArgumentArray)
  708. , m_callee(callee)
  709. , m_this_value(this_value)
  710. , m_type(type)
  711. , m_expression_string(expression_string)
  712. {
  713. }
  714. CallType call_type() const { return m_type; }
  715. Register callee() const { return m_callee; }
  716. Register this_value() const { return m_this_value; }
  717. Optional<StringTableIndex> const& expression_string() const { return m_expression_string; }
  718. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  719. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  720. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  721. void replace_references_impl(Register, Register);
  722. private:
  723. Register m_callee;
  724. Register m_this_value;
  725. CallType m_type;
  726. Optional<StringTableIndex> m_expression_string;
  727. };
  728. class SuperCallWithArgumentArray : public Instruction {
  729. public:
  730. explicit SuperCallWithArgumentArray(bool is_synthetic)
  731. : Instruction(Type::SuperCallWithArgumentArray)
  732. , m_is_synthetic(is_synthetic)
  733. {
  734. }
  735. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  736. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  737. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  738. void replace_references_impl(Register, Register) { }
  739. private:
  740. bool m_is_synthetic;
  741. };
  742. class NewClass final : public Instruction {
  743. public:
  744. explicit NewClass(ClassExpression const& class_expression, Optional<IdentifierTableIndex> lhs_name)
  745. : Instruction(Type::NewClass)
  746. , m_class_expression(class_expression)
  747. , m_lhs_name(lhs_name)
  748. {
  749. }
  750. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  751. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  752. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  753. void replace_references_impl(Register, Register) { }
  754. private:
  755. ClassExpression const& m_class_expression;
  756. Optional<IdentifierTableIndex> m_lhs_name;
  757. };
  758. class NewFunction final : public Instruction {
  759. public:
  760. explicit NewFunction(FunctionExpression const& function_node, Optional<IdentifierTableIndex> lhs_name, Optional<Register> home_object = {})
  761. : Instruction(Type::NewFunction)
  762. , m_function_node(function_node)
  763. , m_lhs_name(lhs_name)
  764. , m_home_object(move(home_object))
  765. {
  766. }
  767. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  768. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  769. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  770. void replace_references_impl(Register, Register);
  771. private:
  772. FunctionExpression const& m_function_node;
  773. Optional<IdentifierTableIndex> m_lhs_name;
  774. Optional<Register> m_home_object;
  775. };
  776. class BlockDeclarationInstantiation final : public Instruction {
  777. public:
  778. explicit BlockDeclarationInstantiation(ScopeNode const& scope_node)
  779. : Instruction(Type::BlockDeclarationInstantiation)
  780. , m_scope_node(scope_node)
  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. private:
  788. ScopeNode const& m_scope_node;
  789. };
  790. class Return final : public Instruction {
  791. public:
  792. constexpr static bool IsTerminator = true;
  793. Return()
  794. : Instruction(Type::Return)
  795. {
  796. }
  797. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  798. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  799. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  800. void replace_references_impl(Register, Register) { }
  801. };
  802. class Increment final : public Instruction {
  803. public:
  804. Increment()
  805. : Instruction(Type::Increment)
  806. {
  807. }
  808. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  809. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  810. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  811. void replace_references_impl(Register, Register) { }
  812. };
  813. class Decrement final : public Instruction {
  814. public:
  815. Decrement()
  816. : Instruction(Type::Decrement)
  817. {
  818. }
  819. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  820. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  821. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  822. void replace_references_impl(Register, Register) { }
  823. };
  824. class ToNumeric final : public Instruction {
  825. public:
  826. ToNumeric()
  827. : Instruction(Type::ToNumeric)
  828. {
  829. }
  830. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  831. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  832. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  833. void replace_references_impl(Register, Register) { }
  834. };
  835. class Throw final : public Instruction {
  836. public:
  837. constexpr static bool IsTerminator = true;
  838. Throw()
  839. : Instruction(Type::Throw)
  840. {
  841. }
  842. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  843. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  844. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  845. void replace_references_impl(Register, Register) { }
  846. };
  847. class ThrowIfNotObject final : public Instruction {
  848. public:
  849. ThrowIfNotObject()
  850. : Instruction(Type::ThrowIfNotObject)
  851. {
  852. }
  853. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  854. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  855. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  856. void replace_references_impl(Register, Register) { }
  857. };
  858. class ThrowIfNullish final : public Instruction {
  859. public:
  860. ThrowIfNullish()
  861. : Instruction(Type::ThrowIfNullish)
  862. {
  863. }
  864. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  865. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  866. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  867. void replace_references_impl(Register, Register) { }
  868. };
  869. class EnterUnwindContext final : public Instruction {
  870. public:
  871. constexpr static bool IsTerminator = true;
  872. EnterUnwindContext(Label entry_point, Optional<Label> handler_target, Optional<Label> finalizer_target)
  873. : Instruction(Type::EnterUnwindContext)
  874. , m_entry_point(move(entry_point))
  875. , m_handler_target(move(handler_target))
  876. , m_finalizer_target(move(finalizer_target))
  877. {
  878. }
  879. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  880. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  881. void replace_references_impl(BasicBlock const&, BasicBlock const&);
  882. void replace_references_impl(Register, Register) { }
  883. auto& entry_point() const { return m_entry_point; }
  884. auto& handler_target() const { return m_handler_target; }
  885. auto& finalizer_target() const { return m_finalizer_target; }
  886. private:
  887. Label m_entry_point;
  888. Optional<Label> m_handler_target;
  889. Optional<Label> m_finalizer_target;
  890. };
  891. class ScheduleJump final : public Instruction {
  892. public:
  893. // Note: We use this instruction to tell the next `finally` block to
  894. // continue execution with a specific break/continue target;
  895. // FIXME: We currently don't clear the interpreter internal flag, when we change
  896. // the control-flow (`break`, `continue`) in a finally-block,
  897. // FIXME: .NET on x86_64 uses a call to the finally instead, which could make this
  898. // easier, at the cost of making control-flow changes (`break`, `continue`, `return`)
  899. // in the finally-block more difficult, but as stated above, those
  900. // aren't handled 100% correctly at the moment anyway
  901. // It might be worth investigating a similar mechanism
  902. constexpr static bool IsTerminator = true;
  903. ScheduleJump(Label target)
  904. : Instruction(Type::ScheduleJump)
  905. , m_target(target)
  906. {
  907. }
  908. Label target() const { return m_target; }
  909. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  910. void replace_references_impl(BasicBlock const& from, BasicBlock const& to)
  911. {
  912. if (&m_target.block() == &from)
  913. m_target = Label { to };
  914. }
  915. void replace_references_impl(Register, Register) { }
  916. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  917. private:
  918. Label m_target;
  919. };
  920. class LeaveLexicalEnvironment final : public Instruction {
  921. public:
  922. LeaveLexicalEnvironment()
  923. : Instruction(Type::LeaveLexicalEnvironment)
  924. {
  925. }
  926. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  927. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  928. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  929. void replace_references_impl(Register, Register) { }
  930. };
  931. class LeaveUnwindContext final : public Instruction {
  932. public:
  933. LeaveUnwindContext()
  934. : Instruction(Type::LeaveUnwindContext)
  935. {
  936. }
  937. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  938. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  939. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  940. void replace_references_impl(Register, Register) { }
  941. };
  942. class ContinuePendingUnwind final : public Instruction {
  943. public:
  944. constexpr static bool IsTerminator = true;
  945. explicit ContinuePendingUnwind(Label resume_target)
  946. : Instruction(Type::ContinuePendingUnwind)
  947. , m_resume_target(resume_target)
  948. {
  949. }
  950. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  951. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  952. void replace_references_impl(BasicBlock const&, BasicBlock const&);
  953. void replace_references_impl(Register, Register) { }
  954. auto& resume_target() const { return m_resume_target; }
  955. private:
  956. Label m_resume_target;
  957. };
  958. class Yield final : public Instruction {
  959. public:
  960. constexpr static bool IsTerminator = true;
  961. explicit Yield(Label continuation_label)
  962. : Instruction(Type::Yield)
  963. , m_continuation_label(continuation_label)
  964. {
  965. }
  966. explicit Yield(nullptr_t)
  967. : Instruction(Type::Yield)
  968. {
  969. }
  970. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  971. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  972. void replace_references_impl(BasicBlock const&, BasicBlock const&);
  973. void replace_references_impl(Register, Register) { }
  974. auto& continuation() const { return m_continuation_label; }
  975. private:
  976. Optional<Label> m_continuation_label;
  977. };
  978. class PushDeclarativeEnvironment final : public Instruction {
  979. public:
  980. explicit PushDeclarativeEnvironment(HashMap<u32, Variable> variables)
  981. : Instruction(Type::PushDeclarativeEnvironment)
  982. , m_variables(move(variables))
  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. HashMap<u32, Variable> m_variables;
  991. };
  992. class GetIterator final : public Instruction {
  993. public:
  994. GetIterator(IteratorHint hint = IteratorHint::Sync)
  995. : Instruction(Type::GetIterator)
  996. , m_hint(hint)
  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. IteratorHint m_hint { IteratorHint::Sync };
  1005. };
  1006. class GetMethod final : public Instruction {
  1007. public:
  1008. GetMethod(IdentifierTableIndex property)
  1009. : Instruction(Type::GetMethod)
  1010. , m_property(property)
  1011. {
  1012. }
  1013. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1014. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  1015. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  1016. void replace_references_impl(Register, Register) { }
  1017. private:
  1018. IdentifierTableIndex m_property;
  1019. };
  1020. class GetObjectPropertyIterator final : public Instruction {
  1021. public:
  1022. GetObjectPropertyIterator()
  1023. : Instruction(Type::GetObjectPropertyIterator)
  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 IteratorClose final : public Instruction {
  1032. public:
  1033. IteratorClose(Completion::Type completion_type, Optional<Value> completion_value)
  1034. : Instruction(Type::IteratorClose)
  1035. , m_completion_type(completion_type)
  1036. , m_completion_value(completion_value)
  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. private:
  1044. Completion::Type m_completion_type { Completion::Type::Normal };
  1045. Optional<Value> m_completion_value;
  1046. };
  1047. class IteratorNext final : public Instruction {
  1048. public:
  1049. IteratorNext()
  1050. : Instruction(Type::IteratorNext)
  1051. {
  1052. }
  1053. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1054. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  1055. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  1056. void replace_references_impl(Register, Register) { }
  1057. };
  1058. class IteratorResultDone final : public Instruction {
  1059. public:
  1060. IteratorResultDone()
  1061. : Instruction(Type::IteratorResultDone)
  1062. {
  1063. }
  1064. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1065. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  1066. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  1067. void replace_references_impl(Register, Register) { }
  1068. };
  1069. class IteratorResultValue final : public Instruction {
  1070. public:
  1071. IteratorResultValue()
  1072. : Instruction(Type::IteratorResultValue)
  1073. {
  1074. }
  1075. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1076. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  1077. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  1078. void replace_references_impl(Register, Register) { }
  1079. };
  1080. class ResolveThisBinding final : public Instruction {
  1081. public:
  1082. explicit ResolveThisBinding()
  1083. : Instruction(Type::ResolveThisBinding)
  1084. {
  1085. }
  1086. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1087. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  1088. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  1089. void replace_references_impl(Register, Register) { }
  1090. };
  1091. class ResolveSuperBase final : public Instruction {
  1092. public:
  1093. explicit ResolveSuperBase()
  1094. : Instruction(Type::ResolveSuperBase)
  1095. {
  1096. }
  1097. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1098. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  1099. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  1100. void replace_references_impl(Register, Register) { }
  1101. };
  1102. class GetNewTarget final : public Instruction {
  1103. public:
  1104. explicit GetNewTarget()
  1105. : Instruction(Type::GetNewTarget)
  1106. {
  1107. }
  1108. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1109. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  1110. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  1111. void replace_references_impl(Register, Register) { }
  1112. };
  1113. class TypeofVariable final : public Instruction {
  1114. public:
  1115. explicit TypeofVariable(IdentifierTableIndex identifier)
  1116. : Instruction(Type::TypeofVariable)
  1117. , m_identifier(identifier)
  1118. {
  1119. }
  1120. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1121. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  1122. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  1123. void replace_references_impl(Register, Register) { }
  1124. private:
  1125. IdentifierTableIndex m_identifier;
  1126. };
  1127. }
  1128. namespace JS::Bytecode {
  1129. ALWAYS_INLINE ThrowCompletionOr<void> Instruction::execute(Bytecode::Interpreter& interpreter) const
  1130. {
  1131. #define __BYTECODE_OP(op) \
  1132. case Instruction::Type::op: \
  1133. return static_cast<Bytecode::Op::op const&>(*this).execute_impl(interpreter);
  1134. switch (type()) {
  1135. ENUMERATE_BYTECODE_OPS(__BYTECODE_OP)
  1136. default:
  1137. VERIFY_NOT_REACHED();
  1138. }
  1139. #undef __BYTECODE_OP
  1140. }
  1141. ALWAYS_INLINE void Instruction::replace_references(BasicBlock const& from, BasicBlock const& to)
  1142. {
  1143. #define __BYTECODE_OP(op) \
  1144. case Instruction::Type::op: \
  1145. return static_cast<Bytecode::Op::op&>(*this).replace_references_impl(from, to);
  1146. switch (type()) {
  1147. ENUMERATE_BYTECODE_OPS(__BYTECODE_OP)
  1148. default:
  1149. VERIFY_NOT_REACHED();
  1150. }
  1151. #undef __BYTECODE_OP
  1152. }
  1153. ALWAYS_INLINE void Instruction::replace_references(Register from, Register to)
  1154. {
  1155. #define __BYTECODE_OP(op) \
  1156. case Instruction::Type::op: \
  1157. return static_cast<Bytecode::Op::op&>(*this).replace_references_impl(from, to);
  1158. switch (type()) {
  1159. ENUMERATE_BYTECODE_OPS(__BYTECODE_OP)
  1160. default:
  1161. VERIFY_NOT_REACHED();
  1162. }
  1163. #undef __BYTECODE_OP
  1164. }
  1165. ALWAYS_INLINE size_t Instruction::length() const
  1166. {
  1167. if (type() == Type::NewArray)
  1168. return round_up_to_power_of_two(static_cast<Op::NewArray const&>(*this).length_impl(), alignof(void*));
  1169. if (type() == Type::CopyObjectExcludingProperties)
  1170. return round_up_to_power_of_two(static_cast<Op::CopyObjectExcludingProperties const&>(*this).length_impl(), alignof(void*));
  1171. #define __BYTECODE_OP(op) \
  1172. case Type::op: \
  1173. return sizeof(Op::op);
  1174. switch (type()) {
  1175. ENUMERATE_BYTECODE_OPS(__BYTECODE_OP)
  1176. default:
  1177. VERIFY_NOT_REACHED();
  1178. }
  1179. #undef __BYTECODE_OP
  1180. }
  1181. ALWAYS_INLINE bool Instruction::is_terminator() const
  1182. {
  1183. #define __BYTECODE_OP(op) \
  1184. case Type::op: \
  1185. return Op::op::IsTerminator;
  1186. switch (type()) {
  1187. ENUMERATE_BYTECODE_OPS(__BYTECODE_OP)
  1188. default:
  1189. VERIFY_NOT_REACHED();
  1190. }
  1191. #undef __BYTECODE_OP
  1192. }
  1193. }