Op.h 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630
  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 DeleteByIdWithThis final : public Instruction {
  631. public:
  632. DeleteByIdWithThis(Register this_value, IdentifierTableIndex property)
  633. : Instruction(Type::DeleteByIdWithThis)
  634. , m_this_value(this_value)
  635. , m_property(property)
  636. {
  637. }
  638. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  639. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  640. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  641. void replace_references_impl(Register, Register) { }
  642. private:
  643. Register m_this_value;
  644. IdentifierTableIndex m_property;
  645. };
  646. class GetByValue final : public Instruction {
  647. public:
  648. explicit GetByValue(Register base)
  649. : Instruction(Type::GetByValue)
  650. , m_base(base)
  651. {
  652. }
  653. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  654. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  655. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  656. void replace_references_impl(Register from, Register to)
  657. {
  658. if (m_base == from)
  659. m_base = to;
  660. }
  661. private:
  662. Register m_base;
  663. };
  664. class GetByValueWithThis final : public Instruction {
  665. public:
  666. GetByValueWithThis(Register base, Register this_value)
  667. : Instruction(Type::GetByValueWithThis)
  668. , m_base(base)
  669. , m_this_value(this_value)
  670. {
  671. }
  672. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  673. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  674. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  675. void replace_references_impl(Register from, Register to)
  676. {
  677. if (m_base == from)
  678. m_base = to;
  679. if (m_this_value == from)
  680. m_this_value = to;
  681. }
  682. private:
  683. Register m_base;
  684. Register m_this_value;
  685. };
  686. class PutByValue final : public Instruction {
  687. public:
  688. PutByValue(Register base, Register property, PropertyKind kind = PropertyKind::KeyValue)
  689. : Instruction(Type::PutByValue)
  690. , m_base(base)
  691. , m_property(property)
  692. , m_kind(kind)
  693. {
  694. }
  695. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  696. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  697. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  698. void replace_references_impl(Register from, Register to)
  699. {
  700. if (m_base == from)
  701. m_base = to;
  702. if (m_property == from)
  703. m_property = to;
  704. }
  705. private:
  706. Register m_base;
  707. Register m_property;
  708. PropertyKind m_kind;
  709. };
  710. class PutByValueWithThis final : public Instruction {
  711. public:
  712. PutByValueWithThis(Register base, Register property, Register this_value, PropertyKind kind = PropertyKind::KeyValue)
  713. : Instruction(Type::PutByValueWithThis)
  714. , m_base(base)
  715. , m_property(property)
  716. , m_this_value(this_value)
  717. , m_kind(kind)
  718. {
  719. }
  720. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  721. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  722. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  723. void replace_references_impl(Register from, Register to)
  724. {
  725. if (m_base == from)
  726. m_base = to;
  727. if (m_property == from)
  728. m_property = to;
  729. if (m_this_value == from)
  730. m_this_value = to;
  731. }
  732. private:
  733. Register m_base;
  734. Register m_property;
  735. Register m_this_value;
  736. PropertyKind m_kind;
  737. };
  738. class DeleteByValue final : public Instruction {
  739. public:
  740. DeleteByValue(Register base)
  741. : Instruction(Type::DeleteByValue)
  742. , m_base(base)
  743. {
  744. }
  745. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  746. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  747. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  748. void replace_references_impl(Register from, Register to)
  749. {
  750. if (m_base == from)
  751. m_base = to;
  752. }
  753. private:
  754. Register m_base;
  755. };
  756. class DeleteByValueWithThis final : public Instruction {
  757. public:
  758. DeleteByValueWithThis(Register base, Register this_value)
  759. : Instruction(Type::DeleteByValueWithThis)
  760. , m_base(base)
  761. , m_this_value(this_value)
  762. {
  763. }
  764. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  765. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  766. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  767. void replace_references_impl(Register from, Register to)
  768. {
  769. if (m_base == from)
  770. m_base = to;
  771. }
  772. private:
  773. Register m_base;
  774. Register m_this_value;
  775. };
  776. class Jump : public Instruction {
  777. public:
  778. constexpr static bool IsTerminator = true;
  779. explicit Jump(Type type, Optional<Label> taken_target = {}, Optional<Label> nontaken_target = {})
  780. : Instruction(type)
  781. , m_true_target(move(taken_target))
  782. , m_false_target(move(nontaken_target))
  783. {
  784. }
  785. explicit Jump(Optional<Label> taken_target = {}, Optional<Label> nontaken_target = {})
  786. : Instruction(Type::Jump)
  787. , m_true_target(move(taken_target))
  788. , m_false_target(move(nontaken_target))
  789. {
  790. }
  791. void set_targets(Optional<Label> true_target, Optional<Label> false_target)
  792. {
  793. m_true_target = move(true_target);
  794. m_false_target = move(false_target);
  795. }
  796. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  797. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  798. void replace_references_impl(BasicBlock const&, BasicBlock const&);
  799. void replace_references_impl(Register, Register) { }
  800. auto& true_target() const { return m_true_target; }
  801. auto& false_target() const { return m_false_target; }
  802. protected:
  803. Optional<Label> m_true_target;
  804. Optional<Label> m_false_target;
  805. };
  806. class JumpConditional final : public Jump {
  807. public:
  808. explicit JumpConditional(Optional<Label> true_target = {}, Optional<Label> false_target = {})
  809. : Jump(Type::JumpConditional, move(true_target), move(false_target))
  810. {
  811. }
  812. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  813. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  814. };
  815. class JumpNullish final : public Jump {
  816. public:
  817. explicit JumpNullish(Optional<Label> true_target = {}, Optional<Label> false_target = {})
  818. : Jump(Type::JumpNullish, move(true_target), move(false_target))
  819. {
  820. }
  821. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  822. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  823. };
  824. class JumpUndefined final : public Jump {
  825. public:
  826. explicit JumpUndefined(Optional<Label> true_target = {}, Optional<Label> false_target = {})
  827. : Jump(Type::JumpUndefined, move(true_target), move(false_target))
  828. {
  829. }
  830. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  831. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  832. };
  833. enum class CallType {
  834. Call,
  835. Construct,
  836. DirectEval,
  837. };
  838. class Call final : public Instruction {
  839. public:
  840. Call(CallType type, Register callee, Register this_value, Register first_argument, u32 argument_count, Optional<StringTableIndex> expression_string = {})
  841. : Instruction(Type::Call)
  842. , m_callee(callee)
  843. , m_this_value(this_value)
  844. , m_first_argument(first_argument)
  845. , m_argument_count(argument_count)
  846. , m_type(type)
  847. , m_expression_string(expression_string)
  848. {
  849. }
  850. CallType call_type() const { return m_type; }
  851. Register callee() const { return m_callee; }
  852. Register this_value() const { return m_this_value; }
  853. Optional<StringTableIndex> const& expression_string() const { return m_expression_string; }
  854. Register first_argument() const { return m_first_argument; }
  855. u32 argument_count() const { return m_argument_count; }
  856. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  857. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  858. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  859. void replace_references_impl(Register, Register);
  860. private:
  861. Register m_callee;
  862. Register m_this_value;
  863. Register m_first_argument;
  864. u32 m_argument_count { 0 };
  865. CallType m_type;
  866. Optional<StringTableIndex> m_expression_string;
  867. };
  868. class CallWithArgumentArray final : public Instruction {
  869. public:
  870. CallWithArgumentArray(CallType type, Register callee, Register this_value, Optional<StringTableIndex> expression_string = {})
  871. : Instruction(Type::CallWithArgumentArray)
  872. , m_callee(callee)
  873. , m_this_value(this_value)
  874. , m_type(type)
  875. , m_expression_string(expression_string)
  876. {
  877. }
  878. CallType call_type() const { return m_type; }
  879. Register callee() const { return m_callee; }
  880. Register this_value() const { return m_this_value; }
  881. Optional<StringTableIndex> const& expression_string() const { return m_expression_string; }
  882. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  883. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  884. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  885. void replace_references_impl(Register, Register);
  886. private:
  887. Register m_callee;
  888. Register m_this_value;
  889. CallType m_type;
  890. Optional<StringTableIndex> m_expression_string;
  891. };
  892. class SuperCallWithArgumentArray : public Instruction {
  893. public:
  894. explicit SuperCallWithArgumentArray(bool is_synthetic)
  895. : Instruction(Type::SuperCallWithArgumentArray)
  896. , m_is_synthetic(is_synthetic)
  897. {
  898. }
  899. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  900. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  901. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  902. void replace_references_impl(Register, Register) { }
  903. private:
  904. bool m_is_synthetic;
  905. };
  906. class NewClass final : public Instruction {
  907. public:
  908. explicit NewClass(ClassExpression const& class_expression, Optional<IdentifierTableIndex> lhs_name)
  909. : Instruction(Type::NewClass)
  910. , m_class_expression(class_expression)
  911. , m_lhs_name(lhs_name)
  912. {
  913. }
  914. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  915. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  916. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  917. void replace_references_impl(Register, Register) { }
  918. private:
  919. ClassExpression const& m_class_expression;
  920. Optional<IdentifierTableIndex> m_lhs_name;
  921. };
  922. class NewFunction final : public Instruction {
  923. public:
  924. explicit NewFunction(FunctionExpression const& function_node, Optional<IdentifierTableIndex> lhs_name, Optional<Register> home_object = {})
  925. : Instruction(Type::NewFunction)
  926. , m_function_node(function_node)
  927. , m_lhs_name(lhs_name)
  928. , m_home_object(move(home_object))
  929. {
  930. }
  931. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  932. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  933. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  934. void replace_references_impl(Register, Register);
  935. private:
  936. FunctionExpression const& m_function_node;
  937. Optional<IdentifierTableIndex> m_lhs_name;
  938. Optional<Register> m_home_object;
  939. };
  940. class BlockDeclarationInstantiation final : public Instruction {
  941. public:
  942. explicit BlockDeclarationInstantiation(ScopeNode const& scope_node)
  943. : Instruction(Type::BlockDeclarationInstantiation)
  944. , m_scope_node(scope_node)
  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. private:
  952. ScopeNode const& m_scope_node;
  953. };
  954. class Return final : public Instruction {
  955. public:
  956. constexpr static bool IsTerminator = true;
  957. Return()
  958. : Instruction(Type::Return)
  959. {
  960. }
  961. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  962. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  963. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  964. void replace_references_impl(Register, Register) { }
  965. };
  966. class Increment final : public Instruction {
  967. public:
  968. Increment()
  969. : Instruction(Type::Increment)
  970. {
  971. }
  972. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  973. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  974. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  975. void replace_references_impl(Register, Register) { }
  976. };
  977. class Decrement final : public Instruction {
  978. public:
  979. Decrement()
  980. : Instruction(Type::Decrement)
  981. {
  982. }
  983. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  984. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  985. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  986. void replace_references_impl(Register, Register) { }
  987. };
  988. class ToNumeric final : public Instruction {
  989. public:
  990. ToNumeric()
  991. : Instruction(Type::ToNumeric)
  992. {
  993. }
  994. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  995. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  996. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  997. void replace_references_impl(Register, Register) { }
  998. };
  999. class Throw final : public Instruction {
  1000. public:
  1001. constexpr static bool IsTerminator = true;
  1002. Throw()
  1003. : Instruction(Type::Throw)
  1004. {
  1005. }
  1006. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1007. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  1008. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  1009. void replace_references_impl(Register, Register) { }
  1010. };
  1011. class ThrowIfNotObject final : public Instruction {
  1012. public:
  1013. ThrowIfNotObject()
  1014. : Instruction(Type::ThrowIfNotObject)
  1015. {
  1016. }
  1017. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1018. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  1019. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  1020. void replace_references_impl(Register, Register) { }
  1021. };
  1022. class ThrowIfNullish final : public Instruction {
  1023. public:
  1024. ThrowIfNullish()
  1025. : Instruction(Type::ThrowIfNullish)
  1026. {
  1027. }
  1028. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1029. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  1030. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  1031. void replace_references_impl(Register, Register) { }
  1032. };
  1033. class EnterUnwindContext final : public Instruction {
  1034. public:
  1035. constexpr static bool IsTerminator = true;
  1036. EnterUnwindContext(Label entry_point, Optional<Label> handler_target, Optional<Label> finalizer_target)
  1037. : Instruction(Type::EnterUnwindContext)
  1038. , m_entry_point(move(entry_point))
  1039. , m_handler_target(move(handler_target))
  1040. , m_finalizer_target(move(finalizer_target))
  1041. {
  1042. }
  1043. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1044. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  1045. void replace_references_impl(BasicBlock const&, BasicBlock const&);
  1046. void replace_references_impl(Register, Register) { }
  1047. auto& entry_point() const { return m_entry_point; }
  1048. auto& handler_target() const { return m_handler_target; }
  1049. auto& finalizer_target() const { return m_finalizer_target; }
  1050. private:
  1051. Label m_entry_point;
  1052. Optional<Label> m_handler_target;
  1053. Optional<Label> m_finalizer_target;
  1054. };
  1055. class ScheduleJump final : public Instruction {
  1056. public:
  1057. // Note: We use this instruction to tell the next `finally` block to
  1058. // continue execution with a specific break/continue target;
  1059. // FIXME: We currently don't clear the interpreter internal flag, when we change
  1060. // the control-flow (`break`, `continue`) in a finally-block,
  1061. // FIXME: .NET on x86_64 uses a call to the finally instead, which could make this
  1062. // easier, at the cost of making control-flow changes (`break`, `continue`, `return`)
  1063. // in the finally-block more difficult, but as stated above, those
  1064. // aren't handled 100% correctly at the moment anyway
  1065. // It might be worth investigating a similar mechanism
  1066. constexpr static bool IsTerminator = true;
  1067. ScheduleJump(Label target)
  1068. : Instruction(Type::ScheduleJump)
  1069. , m_target(target)
  1070. {
  1071. }
  1072. Label target() const { return m_target; }
  1073. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1074. void replace_references_impl(BasicBlock const& from, BasicBlock const& to)
  1075. {
  1076. if (&m_target.block() == &from)
  1077. m_target = Label { to };
  1078. }
  1079. void replace_references_impl(Register, Register) { }
  1080. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  1081. private:
  1082. Label m_target;
  1083. };
  1084. class LeaveLexicalEnvironment final : public Instruction {
  1085. public:
  1086. LeaveLexicalEnvironment()
  1087. : Instruction(Type::LeaveLexicalEnvironment)
  1088. {
  1089. }
  1090. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1091. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  1092. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  1093. void replace_references_impl(Register, Register) { }
  1094. };
  1095. class LeaveUnwindContext final : public Instruction {
  1096. public:
  1097. LeaveUnwindContext()
  1098. : Instruction(Type::LeaveUnwindContext)
  1099. {
  1100. }
  1101. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1102. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  1103. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  1104. void replace_references_impl(Register, Register) { }
  1105. };
  1106. class ContinuePendingUnwind final : public Instruction {
  1107. public:
  1108. constexpr static bool IsTerminator = true;
  1109. explicit ContinuePendingUnwind(Label resume_target)
  1110. : Instruction(Type::ContinuePendingUnwind)
  1111. , m_resume_target(resume_target)
  1112. {
  1113. }
  1114. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1115. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  1116. void replace_references_impl(BasicBlock const&, BasicBlock const&);
  1117. void replace_references_impl(Register, Register) { }
  1118. auto& resume_target() const { return m_resume_target; }
  1119. private:
  1120. Label m_resume_target;
  1121. };
  1122. class Yield final : public Instruction {
  1123. public:
  1124. constexpr static bool IsTerminator = true;
  1125. explicit Yield(Label continuation_label)
  1126. : Instruction(Type::Yield)
  1127. , m_continuation_label(continuation_label)
  1128. {
  1129. }
  1130. explicit Yield(nullptr_t)
  1131. : Instruction(Type::Yield)
  1132. {
  1133. }
  1134. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1135. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  1136. void replace_references_impl(BasicBlock const&, BasicBlock const&);
  1137. void replace_references_impl(Register, Register) { }
  1138. auto& continuation() const { return m_continuation_label; }
  1139. private:
  1140. Optional<Label> m_continuation_label;
  1141. };
  1142. class PushDeclarativeEnvironment final : public Instruction {
  1143. public:
  1144. explicit PushDeclarativeEnvironment(HashMap<u32, Variable> variables)
  1145. : Instruction(Type::PushDeclarativeEnvironment)
  1146. , m_variables(move(variables))
  1147. {
  1148. }
  1149. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1150. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  1151. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  1152. void replace_references_impl(Register, Register) { }
  1153. private:
  1154. HashMap<u32, Variable> m_variables;
  1155. };
  1156. class GetIterator final : public Instruction {
  1157. public:
  1158. GetIterator(IteratorHint hint = IteratorHint::Sync)
  1159. : Instruction(Type::GetIterator)
  1160. , m_hint(hint)
  1161. {
  1162. }
  1163. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1164. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  1165. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  1166. void replace_references_impl(Register, Register) { }
  1167. private:
  1168. IteratorHint m_hint { IteratorHint::Sync };
  1169. };
  1170. class GetMethod final : public Instruction {
  1171. public:
  1172. GetMethod(IdentifierTableIndex property)
  1173. : Instruction(Type::GetMethod)
  1174. , m_property(property)
  1175. {
  1176. }
  1177. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1178. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  1179. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  1180. void replace_references_impl(Register, Register) { }
  1181. private:
  1182. IdentifierTableIndex m_property;
  1183. };
  1184. class GetObjectPropertyIterator final : public Instruction {
  1185. public:
  1186. GetObjectPropertyIterator()
  1187. : Instruction(Type::GetObjectPropertyIterator)
  1188. {
  1189. }
  1190. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1191. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  1192. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  1193. void replace_references_impl(Register, Register) { }
  1194. };
  1195. class IteratorClose final : public Instruction {
  1196. public:
  1197. IteratorClose(Completion::Type completion_type, Optional<Value> completion_value)
  1198. : Instruction(Type::IteratorClose)
  1199. , m_completion_type(completion_type)
  1200. , m_completion_value(completion_value)
  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. private:
  1208. Completion::Type m_completion_type { Completion::Type::Normal };
  1209. Optional<Value> m_completion_value;
  1210. };
  1211. class IteratorNext final : public Instruction {
  1212. public:
  1213. IteratorNext()
  1214. : Instruction(Type::IteratorNext)
  1215. {
  1216. }
  1217. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1218. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  1219. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  1220. void replace_references_impl(Register, Register) { }
  1221. };
  1222. class IteratorResultDone final : public Instruction {
  1223. public:
  1224. IteratorResultDone()
  1225. : Instruction(Type::IteratorResultDone)
  1226. {
  1227. }
  1228. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1229. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  1230. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  1231. void replace_references_impl(Register, Register) { }
  1232. };
  1233. class IteratorResultValue final : public Instruction {
  1234. public:
  1235. IteratorResultValue()
  1236. : Instruction(Type::IteratorResultValue)
  1237. {
  1238. }
  1239. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1240. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  1241. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  1242. void replace_references_impl(Register, Register) { }
  1243. };
  1244. class ResolveThisBinding final : public Instruction {
  1245. public:
  1246. explicit ResolveThisBinding()
  1247. : Instruction(Type::ResolveThisBinding)
  1248. {
  1249. }
  1250. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1251. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  1252. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  1253. void replace_references_impl(Register, Register) { }
  1254. };
  1255. class ResolveSuperBase final : public Instruction {
  1256. public:
  1257. explicit ResolveSuperBase()
  1258. : Instruction(Type::ResolveSuperBase)
  1259. {
  1260. }
  1261. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1262. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  1263. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  1264. void replace_references_impl(Register, Register) { }
  1265. };
  1266. class GetNewTarget final : public Instruction {
  1267. public:
  1268. explicit GetNewTarget()
  1269. : Instruction(Type::GetNewTarget)
  1270. {
  1271. }
  1272. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1273. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  1274. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  1275. void replace_references_impl(Register, Register) { }
  1276. };
  1277. class TypeofVariable final : public Instruction {
  1278. public:
  1279. explicit TypeofVariable(IdentifierTableIndex identifier)
  1280. : Instruction(Type::TypeofVariable)
  1281. , m_identifier(identifier)
  1282. {
  1283. }
  1284. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1285. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  1286. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  1287. void replace_references_impl(Register, Register) { }
  1288. private:
  1289. IdentifierTableIndex m_identifier;
  1290. };
  1291. class TypeofLocal final : public Instruction {
  1292. public:
  1293. explicit TypeofLocal(size_t index)
  1294. : Instruction(Type::TypeofLocal)
  1295. , m_index(index)
  1296. {
  1297. }
  1298. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1299. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  1300. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  1301. void replace_references_impl(Register, Register) { }
  1302. private:
  1303. size_t m_index;
  1304. };
  1305. }
  1306. namespace JS::Bytecode {
  1307. ALWAYS_INLINE ThrowCompletionOr<void> Instruction::execute(Bytecode::Interpreter& interpreter) const
  1308. {
  1309. #define __BYTECODE_OP(op) \
  1310. case Instruction::Type::op: \
  1311. return static_cast<Bytecode::Op::op const&>(*this).execute_impl(interpreter);
  1312. switch (type()) {
  1313. ENUMERATE_BYTECODE_OPS(__BYTECODE_OP)
  1314. default:
  1315. VERIFY_NOT_REACHED();
  1316. }
  1317. #undef __BYTECODE_OP
  1318. }
  1319. ALWAYS_INLINE void Instruction::replace_references(BasicBlock const& from, BasicBlock const& to)
  1320. {
  1321. #define __BYTECODE_OP(op) \
  1322. case Instruction::Type::op: \
  1323. return static_cast<Bytecode::Op::op&>(*this).replace_references_impl(from, to);
  1324. switch (type()) {
  1325. ENUMERATE_BYTECODE_OPS(__BYTECODE_OP)
  1326. default:
  1327. VERIFY_NOT_REACHED();
  1328. }
  1329. #undef __BYTECODE_OP
  1330. }
  1331. ALWAYS_INLINE void Instruction::replace_references(Register from, Register to)
  1332. {
  1333. #define __BYTECODE_OP(op) \
  1334. case Instruction::Type::op: \
  1335. return static_cast<Bytecode::Op::op&>(*this).replace_references_impl(from, to);
  1336. switch (type()) {
  1337. ENUMERATE_BYTECODE_OPS(__BYTECODE_OP)
  1338. default:
  1339. VERIFY_NOT_REACHED();
  1340. }
  1341. #undef __BYTECODE_OP
  1342. }
  1343. ALWAYS_INLINE size_t Instruction::length() const
  1344. {
  1345. if (type() == Type::NewArray)
  1346. return round_up_to_power_of_two(static_cast<Op::NewArray const&>(*this).length_impl(), alignof(void*));
  1347. if (type() == Type::CopyObjectExcludingProperties)
  1348. return round_up_to_power_of_two(static_cast<Op::CopyObjectExcludingProperties const&>(*this).length_impl(), alignof(void*));
  1349. #define __BYTECODE_OP(op) \
  1350. case Type::op: \
  1351. return sizeof(Op::op);
  1352. switch (type()) {
  1353. ENUMERATE_BYTECODE_OPS(__BYTECODE_OP)
  1354. default:
  1355. VERIFY_NOT_REACHED();
  1356. }
  1357. #undef __BYTECODE_OP
  1358. }
  1359. ALWAYS_INLINE bool Instruction::is_terminator() const
  1360. {
  1361. #define __BYTECODE_OP(op) \
  1362. case Type::op: \
  1363. return Op::op::IsTerminator;
  1364. switch (type()) {
  1365. ENUMERATE_BYTECODE_OPS(__BYTECODE_OP)
  1366. default:
  1367. VERIFY_NOT_REACHED();
  1368. }
  1369. #undef __BYTECODE_OP
  1370. }
  1371. }