Op.h 55 KB

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