Op.h 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588
  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 SetLocal final : public Instruction {
  417. public:
  418. explicit SetLocal(size_t index)
  419. : Instruction(Type::SetLocal)
  420. , m_index(index)
  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. size_t index() const { return m_index; }
  428. private:
  429. size_t m_index;
  430. };
  431. class GetVariable final : public Instruction {
  432. public:
  433. explicit GetVariable(IdentifierTableIndex identifier)
  434. : Instruction(Type::GetVariable)
  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. Optional<EnvironmentCoordinate> mutable m_cached_environment_coordinate;
  446. };
  447. class GetLocal final : public Instruction {
  448. public:
  449. explicit GetLocal(size_t index)
  450. : Instruction(Type::GetLocal)
  451. , m_index(index)
  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. size_t index() const { return m_index; }
  459. private:
  460. size_t m_index;
  461. };
  462. class DeleteVariable final : public Instruction {
  463. public:
  464. explicit DeleteVariable(IdentifierTableIndex identifier)
  465. : Instruction(Type::DeleteVariable)
  466. , m_identifier(identifier)
  467. {
  468. }
  469. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  470. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  471. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  472. void replace_references_impl(Register, Register) { }
  473. IdentifierTableIndex identifier() const { return m_identifier; }
  474. private:
  475. IdentifierTableIndex m_identifier;
  476. };
  477. class GetById final : public Instruction {
  478. public:
  479. explicit GetById(IdentifierTableIndex property)
  480. : Instruction(Type::GetById)
  481. , m_property(property)
  482. {
  483. }
  484. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  485. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  486. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  487. void replace_references_impl(Register, Register) { }
  488. private:
  489. IdentifierTableIndex m_property;
  490. };
  491. class GetByIdWithThis final : public Instruction {
  492. public:
  493. GetByIdWithThis(IdentifierTableIndex property, Register this_value)
  494. : Instruction(Type::GetByIdWithThis)
  495. , m_property(property)
  496. , m_this_value(this_value)
  497. {
  498. }
  499. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  500. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  501. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  502. void replace_references_impl(Register from, Register to)
  503. {
  504. if (m_this_value == from)
  505. m_this_value = to;
  506. }
  507. private:
  508. IdentifierTableIndex m_property;
  509. Register m_this_value;
  510. };
  511. class GetPrivateById final : public Instruction {
  512. public:
  513. explicit GetPrivateById(IdentifierTableIndex property)
  514. : Instruction(Type::GetPrivateById)
  515. , m_property(property)
  516. {
  517. }
  518. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  519. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  520. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  521. void replace_references_impl(Register, Register) { }
  522. private:
  523. IdentifierTableIndex m_property;
  524. };
  525. class HasPrivateId final : public Instruction {
  526. public:
  527. explicit HasPrivateId(IdentifierTableIndex property)
  528. : Instruction(Type::HasPrivateId)
  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. enum class PropertyKind {
  540. Getter,
  541. Setter,
  542. KeyValue,
  543. Spread,
  544. ProtoSetter,
  545. };
  546. class PutById final : public Instruction {
  547. public:
  548. explicit PutById(Register base, IdentifierTableIndex property, PropertyKind kind = PropertyKind::KeyValue)
  549. : Instruction(Type::PutById)
  550. , m_base(base)
  551. , m_property(property)
  552. , m_kind(kind)
  553. {
  554. }
  555. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  556. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  557. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  558. void replace_references_impl(Register from, Register to)
  559. {
  560. if (m_base == from)
  561. m_base = to;
  562. }
  563. private:
  564. Register m_base;
  565. IdentifierTableIndex m_property;
  566. PropertyKind m_kind;
  567. };
  568. class PutByIdWithThis final : public Instruction {
  569. public:
  570. PutByIdWithThis(Register base, Register this_value, IdentifierTableIndex property, PropertyKind kind = PropertyKind::KeyValue)
  571. : Instruction(Type::PutByIdWithThis)
  572. , m_base(base)
  573. , m_this_value(this_value)
  574. , m_property(property)
  575. , m_kind(kind)
  576. {
  577. }
  578. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  579. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  580. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  581. void replace_references_impl(Register from, Register to)
  582. {
  583. if (m_base == from)
  584. m_base = to;
  585. if (m_this_value == from)
  586. m_this_value = to;
  587. }
  588. private:
  589. Register m_base;
  590. Register m_this_value;
  591. IdentifierTableIndex m_property;
  592. PropertyKind m_kind;
  593. };
  594. class PutPrivateById final : public Instruction {
  595. public:
  596. explicit PutPrivateById(Register base, IdentifierTableIndex property, PropertyKind kind = PropertyKind::KeyValue)
  597. : Instruction(Type::PutPrivateById)
  598. , m_base(base)
  599. , m_property(property)
  600. , m_kind(kind)
  601. {
  602. }
  603. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  604. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  605. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  606. void replace_references_impl(Register from, Register to)
  607. {
  608. if (m_base == from)
  609. m_base = to;
  610. }
  611. private:
  612. Register m_base;
  613. IdentifierTableIndex m_property;
  614. PropertyKind m_kind;
  615. };
  616. class DeleteById final : public Instruction {
  617. public:
  618. explicit DeleteById(IdentifierTableIndex property)
  619. : Instruction(Type::DeleteById)
  620. , m_property(property)
  621. {
  622. }
  623. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  624. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  625. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  626. void replace_references_impl(Register, Register) { }
  627. private:
  628. IdentifierTableIndex m_property;
  629. };
  630. class GetByValue final : public Instruction {
  631. public:
  632. explicit GetByValue(Register base)
  633. : Instruction(Type::GetByValue)
  634. , m_base(base)
  635. {
  636. }
  637. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  638. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  639. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  640. void replace_references_impl(Register from, Register to)
  641. {
  642. if (m_base == from)
  643. m_base = to;
  644. }
  645. private:
  646. Register m_base;
  647. };
  648. class GetByValueWithThis final : public Instruction {
  649. public:
  650. GetByValueWithThis(Register base, Register this_value)
  651. : Instruction(Type::GetByValueWithThis)
  652. , m_base(base)
  653. , m_this_value(this_value)
  654. {
  655. }
  656. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  657. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  658. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  659. void replace_references_impl(Register from, Register to)
  660. {
  661. if (m_base == from)
  662. m_base = to;
  663. if (m_this_value == from)
  664. m_this_value = to;
  665. }
  666. private:
  667. Register m_base;
  668. Register m_this_value;
  669. };
  670. class PutByValue final : public Instruction {
  671. public:
  672. PutByValue(Register base, Register property, PropertyKind kind = PropertyKind::KeyValue)
  673. : Instruction(Type::PutByValue)
  674. , m_base(base)
  675. , m_property(property)
  676. , m_kind(kind)
  677. {
  678. }
  679. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  680. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  681. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  682. void replace_references_impl(Register from, Register to)
  683. {
  684. if (m_base == from)
  685. m_base = to;
  686. if (m_property == from)
  687. m_property = to;
  688. }
  689. private:
  690. Register m_base;
  691. Register m_property;
  692. PropertyKind m_kind;
  693. };
  694. class PutByValueWithThis final : public Instruction {
  695. public:
  696. PutByValueWithThis(Register base, Register property, Register this_value, PropertyKind kind = PropertyKind::KeyValue)
  697. : Instruction(Type::PutByValueWithThis)
  698. , m_base(base)
  699. , m_property(property)
  700. , m_this_value(this_value)
  701. , m_kind(kind)
  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 from, Register to)
  708. {
  709. if (m_base == from)
  710. m_base = to;
  711. if (m_property == from)
  712. m_property = to;
  713. if (m_this_value == from)
  714. m_this_value = to;
  715. }
  716. private:
  717. Register m_base;
  718. Register m_property;
  719. Register m_this_value;
  720. PropertyKind m_kind;
  721. };
  722. class DeleteByValue final : public Instruction {
  723. public:
  724. DeleteByValue(Register base)
  725. : Instruction(Type::DeleteByValue)
  726. , m_base(base)
  727. {
  728. }
  729. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  730. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  731. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  732. void replace_references_impl(Register from, Register to)
  733. {
  734. if (m_base == from)
  735. m_base = to;
  736. }
  737. private:
  738. Register m_base;
  739. };
  740. class Jump : public Instruction {
  741. public:
  742. constexpr static bool IsTerminator = true;
  743. explicit Jump(Type type, Optional<Label> taken_target = {}, Optional<Label> nontaken_target = {})
  744. : Instruction(type)
  745. , m_true_target(move(taken_target))
  746. , m_false_target(move(nontaken_target))
  747. {
  748. }
  749. explicit Jump(Optional<Label> taken_target = {}, Optional<Label> nontaken_target = {})
  750. : Instruction(Type::Jump)
  751. , m_true_target(move(taken_target))
  752. , m_false_target(move(nontaken_target))
  753. {
  754. }
  755. void set_targets(Optional<Label> true_target, Optional<Label> false_target)
  756. {
  757. m_true_target = move(true_target);
  758. m_false_target = move(false_target);
  759. }
  760. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  761. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  762. void replace_references_impl(BasicBlock const&, BasicBlock const&);
  763. void replace_references_impl(Register, Register) { }
  764. auto& true_target() const { return m_true_target; }
  765. auto& false_target() const { return m_false_target; }
  766. protected:
  767. Optional<Label> m_true_target;
  768. Optional<Label> m_false_target;
  769. };
  770. class JumpConditional final : public Jump {
  771. public:
  772. explicit JumpConditional(Optional<Label> true_target = {}, Optional<Label> false_target = {})
  773. : Jump(Type::JumpConditional, move(true_target), move(false_target))
  774. {
  775. }
  776. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  777. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  778. };
  779. class JumpNullish final : public Jump {
  780. public:
  781. explicit JumpNullish(Optional<Label> true_target = {}, Optional<Label> false_target = {})
  782. : Jump(Type::JumpNullish, move(true_target), move(false_target))
  783. {
  784. }
  785. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  786. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  787. };
  788. class JumpUndefined final : public Jump {
  789. public:
  790. explicit JumpUndefined(Optional<Label> true_target = {}, Optional<Label> false_target = {})
  791. : Jump(Type::JumpUndefined, move(true_target), move(false_target))
  792. {
  793. }
  794. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  795. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  796. };
  797. enum class CallType {
  798. Call,
  799. Construct,
  800. DirectEval,
  801. };
  802. class Call final : public Instruction {
  803. public:
  804. Call(CallType type, Register callee, Register this_value, Register first_argument, u32 argument_count, Optional<StringTableIndex> expression_string = {})
  805. : Instruction(Type::Call)
  806. , m_callee(callee)
  807. , m_this_value(this_value)
  808. , m_first_argument(first_argument)
  809. , m_argument_count(argument_count)
  810. , m_type(type)
  811. , m_expression_string(expression_string)
  812. {
  813. }
  814. CallType call_type() const { return m_type; }
  815. Register callee() const { return m_callee; }
  816. Register this_value() const { return m_this_value; }
  817. Optional<StringTableIndex> const& expression_string() const { return m_expression_string; }
  818. Register first_argument() const { return m_first_argument; }
  819. u32 argument_count() const { return m_argument_count; }
  820. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  821. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  822. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  823. void replace_references_impl(Register, Register);
  824. private:
  825. Register m_callee;
  826. Register m_this_value;
  827. Register m_first_argument;
  828. u32 m_argument_count { 0 };
  829. CallType m_type;
  830. Optional<StringTableIndex> m_expression_string;
  831. };
  832. class CallWithArgumentArray final : public Instruction {
  833. public:
  834. CallWithArgumentArray(CallType type, Register callee, Register this_value, Optional<StringTableIndex> expression_string = {})
  835. : Instruction(Type::CallWithArgumentArray)
  836. , m_callee(callee)
  837. , m_this_value(this_value)
  838. , m_type(type)
  839. , m_expression_string(expression_string)
  840. {
  841. }
  842. CallType call_type() const { return m_type; }
  843. Register callee() const { return m_callee; }
  844. Register this_value() const { return m_this_value; }
  845. Optional<StringTableIndex> const& expression_string() const { return m_expression_string; }
  846. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  847. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  848. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  849. void replace_references_impl(Register, Register);
  850. private:
  851. Register m_callee;
  852. Register m_this_value;
  853. CallType m_type;
  854. Optional<StringTableIndex> m_expression_string;
  855. };
  856. class SuperCallWithArgumentArray : public Instruction {
  857. public:
  858. explicit SuperCallWithArgumentArray(bool is_synthetic)
  859. : Instruction(Type::SuperCallWithArgumentArray)
  860. , m_is_synthetic(is_synthetic)
  861. {
  862. }
  863. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  864. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  865. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  866. void replace_references_impl(Register, Register) { }
  867. private:
  868. bool m_is_synthetic;
  869. };
  870. class NewClass final : public Instruction {
  871. public:
  872. explicit NewClass(ClassExpression const& class_expression, Optional<IdentifierTableIndex> lhs_name)
  873. : Instruction(Type::NewClass)
  874. , m_class_expression(class_expression)
  875. , m_lhs_name(lhs_name)
  876. {
  877. }
  878. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  879. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  880. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  881. void replace_references_impl(Register, Register) { }
  882. private:
  883. ClassExpression const& m_class_expression;
  884. Optional<IdentifierTableIndex> m_lhs_name;
  885. };
  886. class NewFunction final : public Instruction {
  887. public:
  888. explicit NewFunction(FunctionExpression const& function_node, Optional<IdentifierTableIndex> lhs_name, Optional<Register> home_object = {})
  889. : Instruction(Type::NewFunction)
  890. , m_function_node(function_node)
  891. , m_lhs_name(lhs_name)
  892. , m_home_object(move(home_object))
  893. {
  894. }
  895. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  896. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  897. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  898. void replace_references_impl(Register, Register);
  899. private:
  900. FunctionExpression const& m_function_node;
  901. Optional<IdentifierTableIndex> m_lhs_name;
  902. Optional<Register> m_home_object;
  903. };
  904. class BlockDeclarationInstantiation final : public Instruction {
  905. public:
  906. explicit BlockDeclarationInstantiation(ScopeNode const& scope_node)
  907. : Instruction(Type::BlockDeclarationInstantiation)
  908. , m_scope_node(scope_node)
  909. {
  910. }
  911. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  912. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  913. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  914. void replace_references_impl(Register, Register) { }
  915. private:
  916. ScopeNode const& m_scope_node;
  917. };
  918. class Return final : public Instruction {
  919. public:
  920. constexpr static bool IsTerminator = true;
  921. Return()
  922. : Instruction(Type::Return)
  923. {
  924. }
  925. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  926. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  927. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  928. void replace_references_impl(Register, Register) { }
  929. };
  930. class Increment final : public Instruction {
  931. public:
  932. Increment()
  933. : Instruction(Type::Increment)
  934. {
  935. }
  936. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  937. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  938. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  939. void replace_references_impl(Register, Register) { }
  940. };
  941. class Decrement final : public Instruction {
  942. public:
  943. Decrement()
  944. : Instruction(Type::Decrement)
  945. {
  946. }
  947. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  948. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  949. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  950. void replace_references_impl(Register, Register) { }
  951. };
  952. class ToNumeric final : public Instruction {
  953. public:
  954. ToNumeric()
  955. : Instruction(Type::ToNumeric)
  956. {
  957. }
  958. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  959. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  960. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  961. void replace_references_impl(Register, Register) { }
  962. };
  963. class Throw final : public Instruction {
  964. public:
  965. constexpr static bool IsTerminator = true;
  966. Throw()
  967. : Instruction(Type::Throw)
  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. };
  975. class ThrowIfNotObject final : public Instruction {
  976. public:
  977. ThrowIfNotObject()
  978. : Instruction(Type::ThrowIfNotObject)
  979. {
  980. }
  981. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  982. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  983. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  984. void replace_references_impl(Register, Register) { }
  985. };
  986. class ThrowIfNullish final : public Instruction {
  987. public:
  988. ThrowIfNullish()
  989. : Instruction(Type::ThrowIfNullish)
  990. {
  991. }
  992. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  993. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  994. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  995. void replace_references_impl(Register, Register) { }
  996. };
  997. class EnterUnwindContext final : public Instruction {
  998. public:
  999. constexpr static bool IsTerminator = true;
  1000. EnterUnwindContext(Label entry_point, Optional<Label> handler_target, Optional<Label> finalizer_target)
  1001. : Instruction(Type::EnterUnwindContext)
  1002. , m_entry_point(move(entry_point))
  1003. , m_handler_target(move(handler_target))
  1004. , m_finalizer_target(move(finalizer_target))
  1005. {
  1006. }
  1007. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1008. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  1009. void replace_references_impl(BasicBlock const&, BasicBlock const&);
  1010. void replace_references_impl(Register, Register) { }
  1011. auto& entry_point() const { return m_entry_point; }
  1012. auto& handler_target() const { return m_handler_target; }
  1013. auto& finalizer_target() const { return m_finalizer_target; }
  1014. private:
  1015. Label m_entry_point;
  1016. Optional<Label> m_handler_target;
  1017. Optional<Label> m_finalizer_target;
  1018. };
  1019. class ScheduleJump final : public Instruction {
  1020. public:
  1021. // Note: We use this instruction to tell the next `finally` block to
  1022. // continue execution with a specific break/continue target;
  1023. // FIXME: We currently don't clear the interpreter internal flag, when we change
  1024. // the control-flow (`break`, `continue`) in a finally-block,
  1025. // FIXME: .NET on x86_64 uses a call to the finally instead, which could make this
  1026. // easier, at the cost of making control-flow changes (`break`, `continue`, `return`)
  1027. // in the finally-block more difficult, but as stated above, those
  1028. // aren't handled 100% correctly at the moment anyway
  1029. // It might be worth investigating a similar mechanism
  1030. constexpr static bool IsTerminator = true;
  1031. ScheduleJump(Label target)
  1032. : Instruction(Type::ScheduleJump)
  1033. , m_target(target)
  1034. {
  1035. }
  1036. Label target() const { return m_target; }
  1037. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1038. void replace_references_impl(BasicBlock const& from, BasicBlock const& to)
  1039. {
  1040. if (&m_target.block() == &from)
  1041. m_target = Label { to };
  1042. }
  1043. void replace_references_impl(Register, Register) { }
  1044. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  1045. private:
  1046. Label m_target;
  1047. };
  1048. class LeaveLexicalEnvironment final : public Instruction {
  1049. public:
  1050. LeaveLexicalEnvironment()
  1051. : Instruction(Type::LeaveLexicalEnvironment)
  1052. {
  1053. }
  1054. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1055. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  1056. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  1057. void replace_references_impl(Register, Register) { }
  1058. };
  1059. class LeaveUnwindContext final : public Instruction {
  1060. public:
  1061. LeaveUnwindContext()
  1062. : Instruction(Type::LeaveUnwindContext)
  1063. {
  1064. }
  1065. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1066. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  1067. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  1068. void replace_references_impl(Register, Register) { }
  1069. };
  1070. class ContinuePendingUnwind final : public Instruction {
  1071. public:
  1072. constexpr static bool IsTerminator = true;
  1073. explicit ContinuePendingUnwind(Label resume_target)
  1074. : Instruction(Type::ContinuePendingUnwind)
  1075. , m_resume_target(resume_target)
  1076. {
  1077. }
  1078. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1079. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  1080. void replace_references_impl(BasicBlock const&, BasicBlock const&);
  1081. void replace_references_impl(Register, Register) { }
  1082. auto& resume_target() const { return m_resume_target; }
  1083. private:
  1084. Label m_resume_target;
  1085. };
  1086. class Yield final : public Instruction {
  1087. public:
  1088. constexpr static bool IsTerminator = true;
  1089. explicit Yield(Label continuation_label)
  1090. : Instruction(Type::Yield)
  1091. , m_continuation_label(continuation_label)
  1092. {
  1093. }
  1094. explicit Yield(nullptr_t)
  1095. : Instruction(Type::Yield)
  1096. {
  1097. }
  1098. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1099. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  1100. void replace_references_impl(BasicBlock const&, BasicBlock const&);
  1101. void replace_references_impl(Register, Register) { }
  1102. auto& continuation() const { return m_continuation_label; }
  1103. private:
  1104. Optional<Label> m_continuation_label;
  1105. };
  1106. class PushDeclarativeEnvironment final : public Instruction {
  1107. public:
  1108. explicit PushDeclarativeEnvironment(HashMap<u32, Variable> variables)
  1109. : Instruction(Type::PushDeclarativeEnvironment)
  1110. , m_variables(move(variables))
  1111. {
  1112. }
  1113. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1114. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  1115. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  1116. void replace_references_impl(Register, Register) { }
  1117. private:
  1118. HashMap<u32, Variable> m_variables;
  1119. };
  1120. class GetIterator final : public Instruction {
  1121. public:
  1122. GetIterator(IteratorHint hint = IteratorHint::Sync)
  1123. : Instruction(Type::GetIterator)
  1124. , m_hint(hint)
  1125. {
  1126. }
  1127. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1128. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  1129. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  1130. void replace_references_impl(Register, Register) { }
  1131. private:
  1132. IteratorHint m_hint { IteratorHint::Sync };
  1133. };
  1134. class GetMethod final : public Instruction {
  1135. public:
  1136. GetMethod(IdentifierTableIndex property)
  1137. : Instruction(Type::GetMethod)
  1138. , m_property(property)
  1139. {
  1140. }
  1141. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1142. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  1143. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  1144. void replace_references_impl(Register, Register) { }
  1145. private:
  1146. IdentifierTableIndex m_property;
  1147. };
  1148. class GetObjectPropertyIterator final : public Instruction {
  1149. public:
  1150. GetObjectPropertyIterator()
  1151. : Instruction(Type::GetObjectPropertyIterator)
  1152. {
  1153. }
  1154. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1155. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  1156. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  1157. void replace_references_impl(Register, Register) { }
  1158. };
  1159. class IteratorClose final : public Instruction {
  1160. public:
  1161. IteratorClose(Completion::Type completion_type, Optional<Value> completion_value)
  1162. : Instruction(Type::IteratorClose)
  1163. , m_completion_type(completion_type)
  1164. , m_completion_value(completion_value)
  1165. {
  1166. }
  1167. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1168. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  1169. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  1170. void replace_references_impl(Register, Register) { }
  1171. private:
  1172. Completion::Type m_completion_type { Completion::Type::Normal };
  1173. Optional<Value> m_completion_value;
  1174. };
  1175. class IteratorNext final : public Instruction {
  1176. public:
  1177. IteratorNext()
  1178. : Instruction(Type::IteratorNext)
  1179. {
  1180. }
  1181. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1182. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  1183. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  1184. void replace_references_impl(Register, Register) { }
  1185. };
  1186. class IteratorResultDone final : public Instruction {
  1187. public:
  1188. IteratorResultDone()
  1189. : Instruction(Type::IteratorResultDone)
  1190. {
  1191. }
  1192. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1193. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  1194. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  1195. void replace_references_impl(Register, Register) { }
  1196. };
  1197. class IteratorResultValue final : public Instruction {
  1198. public:
  1199. IteratorResultValue()
  1200. : Instruction(Type::IteratorResultValue)
  1201. {
  1202. }
  1203. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1204. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  1205. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  1206. void replace_references_impl(Register, Register) { }
  1207. };
  1208. class ResolveThisBinding final : public Instruction {
  1209. public:
  1210. explicit ResolveThisBinding()
  1211. : Instruction(Type::ResolveThisBinding)
  1212. {
  1213. }
  1214. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1215. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  1216. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  1217. void replace_references_impl(Register, Register) { }
  1218. };
  1219. class ResolveSuperBase final : public Instruction {
  1220. public:
  1221. explicit ResolveSuperBase()
  1222. : Instruction(Type::ResolveSuperBase)
  1223. {
  1224. }
  1225. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1226. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  1227. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  1228. void replace_references_impl(Register, Register) { }
  1229. };
  1230. class GetNewTarget final : public Instruction {
  1231. public:
  1232. explicit GetNewTarget()
  1233. : Instruction(Type::GetNewTarget)
  1234. {
  1235. }
  1236. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1237. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  1238. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  1239. void replace_references_impl(Register, Register) { }
  1240. };
  1241. class TypeofVariable final : public Instruction {
  1242. public:
  1243. explicit TypeofVariable(IdentifierTableIndex identifier)
  1244. : Instruction(Type::TypeofVariable)
  1245. , m_identifier(identifier)
  1246. {
  1247. }
  1248. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1249. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  1250. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  1251. void replace_references_impl(Register, Register) { }
  1252. private:
  1253. IdentifierTableIndex m_identifier;
  1254. };
  1255. class TypeofLocal final : public Instruction {
  1256. public:
  1257. explicit TypeofLocal(size_t index)
  1258. : Instruction(Type::TypeofLocal)
  1259. , m_index(index)
  1260. {
  1261. }
  1262. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1263. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  1264. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  1265. void replace_references_impl(Register, Register) { }
  1266. private:
  1267. size_t m_index;
  1268. };
  1269. }
  1270. namespace JS::Bytecode {
  1271. ALWAYS_INLINE ThrowCompletionOr<void> Instruction::execute(Bytecode::Interpreter& interpreter) const
  1272. {
  1273. #define __BYTECODE_OP(op) \
  1274. case Instruction::Type::op: \
  1275. return static_cast<Bytecode::Op::op const&>(*this).execute_impl(interpreter);
  1276. switch (type()) {
  1277. ENUMERATE_BYTECODE_OPS(__BYTECODE_OP)
  1278. default:
  1279. VERIFY_NOT_REACHED();
  1280. }
  1281. #undef __BYTECODE_OP
  1282. }
  1283. ALWAYS_INLINE void Instruction::replace_references(BasicBlock const& from, BasicBlock const& to)
  1284. {
  1285. #define __BYTECODE_OP(op) \
  1286. case Instruction::Type::op: \
  1287. return static_cast<Bytecode::Op::op&>(*this).replace_references_impl(from, to);
  1288. switch (type()) {
  1289. ENUMERATE_BYTECODE_OPS(__BYTECODE_OP)
  1290. default:
  1291. VERIFY_NOT_REACHED();
  1292. }
  1293. #undef __BYTECODE_OP
  1294. }
  1295. ALWAYS_INLINE void Instruction::replace_references(Register from, Register to)
  1296. {
  1297. #define __BYTECODE_OP(op) \
  1298. case Instruction::Type::op: \
  1299. return static_cast<Bytecode::Op::op&>(*this).replace_references_impl(from, to);
  1300. switch (type()) {
  1301. ENUMERATE_BYTECODE_OPS(__BYTECODE_OP)
  1302. default:
  1303. VERIFY_NOT_REACHED();
  1304. }
  1305. #undef __BYTECODE_OP
  1306. }
  1307. ALWAYS_INLINE size_t Instruction::length() const
  1308. {
  1309. if (type() == Type::NewArray)
  1310. return round_up_to_power_of_two(static_cast<Op::NewArray const&>(*this).length_impl(), alignof(void*));
  1311. if (type() == Type::CopyObjectExcludingProperties)
  1312. return round_up_to_power_of_two(static_cast<Op::CopyObjectExcludingProperties const&>(*this).length_impl(), alignof(void*));
  1313. #define __BYTECODE_OP(op) \
  1314. case Type::op: \
  1315. return sizeof(Op::op);
  1316. switch (type()) {
  1317. ENUMERATE_BYTECODE_OPS(__BYTECODE_OP)
  1318. default:
  1319. VERIFY_NOT_REACHED();
  1320. }
  1321. #undef __BYTECODE_OP
  1322. }
  1323. ALWAYS_INLINE bool Instruction::is_terminator() const
  1324. {
  1325. #define __BYTECODE_OP(op) \
  1326. case Type::op: \
  1327. return Op::op::IsTerminator;
  1328. switch (type()) {
  1329. ENUMERATE_BYTECODE_OPS(__BYTECODE_OP)
  1330. default:
  1331. VERIFY_NOT_REACHED();
  1332. }
  1333. #undef __BYTECODE_OP
  1334. }
  1335. }