Op.h 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710
  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/Iterator.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 Await final : public Instruction {
  1167. public:
  1168. constexpr static bool IsTerminator = true;
  1169. explicit Await(Label continuation_label)
  1170. : Instruction(Type::Await)
  1171. , m_continuation_label(continuation_label)
  1172. {
  1173. }
  1174. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1175. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  1176. void replace_references_impl(BasicBlock const&, BasicBlock const&);
  1177. void replace_references_impl(Register, Register) { }
  1178. auto& continuation() const { return m_continuation_label; }
  1179. private:
  1180. Label m_continuation_label;
  1181. };
  1182. class PushDeclarativeEnvironment final : public Instruction {
  1183. public:
  1184. explicit PushDeclarativeEnvironment(HashMap<u32, Variable> variables)
  1185. : Instruction(Type::PushDeclarativeEnvironment)
  1186. , m_variables(move(variables))
  1187. {
  1188. }
  1189. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1190. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  1191. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  1192. void replace_references_impl(Register, Register) { }
  1193. private:
  1194. HashMap<u32, Variable> m_variables;
  1195. };
  1196. class GetIterator final : public Instruction {
  1197. public:
  1198. GetIterator(IteratorHint hint = IteratorHint::Sync)
  1199. : Instruction(Type::GetIterator)
  1200. , m_hint(hint)
  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. IteratorHint m_hint { IteratorHint::Sync };
  1209. };
  1210. class GetMethod final : public Instruction {
  1211. public:
  1212. GetMethod(IdentifierTableIndex property)
  1213. : Instruction(Type::GetMethod)
  1214. , m_property(property)
  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. private:
  1222. IdentifierTableIndex m_property;
  1223. };
  1224. class GetObjectPropertyIterator final : public Instruction {
  1225. public:
  1226. GetObjectPropertyIterator()
  1227. : Instruction(Type::GetObjectPropertyIterator)
  1228. {
  1229. }
  1230. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1231. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  1232. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  1233. void replace_references_impl(Register, Register) { }
  1234. };
  1235. class IteratorClose final : public Instruction {
  1236. public:
  1237. IteratorClose(Completion::Type completion_type, Optional<Value> completion_value)
  1238. : Instruction(Type::IteratorClose)
  1239. , m_completion_type(completion_type)
  1240. , m_completion_value(completion_value)
  1241. {
  1242. }
  1243. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1244. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  1245. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  1246. void replace_references_impl(Register, Register) { }
  1247. private:
  1248. Completion::Type m_completion_type { Completion::Type::Normal };
  1249. Optional<Value> m_completion_value;
  1250. };
  1251. class AsyncIteratorClose final : public Instruction {
  1252. public:
  1253. AsyncIteratorClose(Completion::Type completion_type, Optional<Value> completion_value)
  1254. : Instruction(Type::AsyncIteratorClose)
  1255. , m_completion_type(completion_type)
  1256. , m_completion_value(completion_value)
  1257. {
  1258. }
  1259. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1260. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  1261. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  1262. void replace_references_impl(Register, Register) { }
  1263. private:
  1264. Completion::Type m_completion_type { Completion::Type::Normal };
  1265. Optional<Value> m_completion_value;
  1266. };
  1267. class IteratorNext final : public Instruction {
  1268. public:
  1269. IteratorNext()
  1270. : Instruction(Type::IteratorNext)
  1271. {
  1272. }
  1273. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1274. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  1275. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  1276. void replace_references_impl(Register, Register) { }
  1277. };
  1278. class IteratorResultDone final : public Instruction {
  1279. public:
  1280. IteratorResultDone()
  1281. : Instruction(Type::IteratorResultDone)
  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. };
  1289. class IteratorResultValue final : public Instruction {
  1290. public:
  1291. IteratorResultValue()
  1292. : Instruction(Type::IteratorResultValue)
  1293. {
  1294. }
  1295. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1296. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  1297. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  1298. void replace_references_impl(Register, Register) { }
  1299. };
  1300. class ResolveThisBinding final : public Instruction {
  1301. public:
  1302. explicit ResolveThisBinding()
  1303. : Instruction(Type::ResolveThisBinding)
  1304. {
  1305. }
  1306. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1307. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  1308. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  1309. void replace_references_impl(Register, Register) { }
  1310. };
  1311. class ResolveSuperBase final : public Instruction {
  1312. public:
  1313. explicit ResolveSuperBase()
  1314. : Instruction(Type::ResolveSuperBase)
  1315. {
  1316. }
  1317. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1318. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  1319. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  1320. void replace_references_impl(Register, Register) { }
  1321. };
  1322. class GetNewTarget final : public Instruction {
  1323. public:
  1324. explicit GetNewTarget()
  1325. : Instruction(Type::GetNewTarget)
  1326. {
  1327. }
  1328. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1329. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  1330. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  1331. void replace_references_impl(Register, Register) { }
  1332. };
  1333. class GetImportMeta final : public Instruction {
  1334. public:
  1335. explicit GetImportMeta()
  1336. : Instruction(Type::GetImportMeta)
  1337. {
  1338. }
  1339. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1340. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  1341. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  1342. void replace_references_impl(Register, Register) { }
  1343. };
  1344. class TypeofVariable final : public Instruction {
  1345. public:
  1346. explicit TypeofVariable(IdentifierTableIndex identifier)
  1347. : Instruction(Type::TypeofVariable)
  1348. , m_identifier(identifier)
  1349. {
  1350. }
  1351. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1352. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  1353. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  1354. void replace_references_impl(Register, Register) { }
  1355. private:
  1356. IdentifierTableIndex m_identifier;
  1357. };
  1358. class TypeofLocal final : public Instruction {
  1359. public:
  1360. explicit TypeofLocal(size_t index)
  1361. : Instruction(Type::TypeofLocal)
  1362. , m_index(index)
  1363. {
  1364. }
  1365. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1366. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  1367. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  1368. void replace_references_impl(Register, Register) { }
  1369. private:
  1370. size_t m_index;
  1371. };
  1372. }
  1373. namespace JS::Bytecode {
  1374. ALWAYS_INLINE ThrowCompletionOr<void> Instruction::execute(Bytecode::Interpreter& interpreter) const
  1375. {
  1376. #define __BYTECODE_OP(op) \
  1377. case Instruction::Type::op: \
  1378. return static_cast<Bytecode::Op::op const&>(*this).execute_impl(interpreter);
  1379. switch (type()) {
  1380. ENUMERATE_BYTECODE_OPS(__BYTECODE_OP)
  1381. default:
  1382. VERIFY_NOT_REACHED();
  1383. }
  1384. #undef __BYTECODE_OP
  1385. }
  1386. ALWAYS_INLINE void Instruction::replace_references(BasicBlock const& from, BasicBlock const& to)
  1387. {
  1388. #define __BYTECODE_OP(op) \
  1389. case Instruction::Type::op: \
  1390. return static_cast<Bytecode::Op::op&>(*this).replace_references_impl(from, to);
  1391. switch (type()) {
  1392. ENUMERATE_BYTECODE_OPS(__BYTECODE_OP)
  1393. default:
  1394. VERIFY_NOT_REACHED();
  1395. }
  1396. #undef __BYTECODE_OP
  1397. }
  1398. ALWAYS_INLINE void Instruction::replace_references(Register from, Register to)
  1399. {
  1400. #define __BYTECODE_OP(op) \
  1401. case Instruction::Type::op: \
  1402. return static_cast<Bytecode::Op::op&>(*this).replace_references_impl(from, to);
  1403. switch (type()) {
  1404. ENUMERATE_BYTECODE_OPS(__BYTECODE_OP)
  1405. default:
  1406. VERIFY_NOT_REACHED();
  1407. }
  1408. #undef __BYTECODE_OP
  1409. }
  1410. ALWAYS_INLINE size_t Instruction::length() const
  1411. {
  1412. if (type() == Type::NewArray)
  1413. return round_up_to_power_of_two(static_cast<Op::NewArray const&>(*this).length_impl(), alignof(void*));
  1414. if (type() == Type::CopyObjectExcludingProperties)
  1415. return round_up_to_power_of_two(static_cast<Op::CopyObjectExcludingProperties const&>(*this).length_impl(), alignof(void*));
  1416. #define __BYTECODE_OP(op) \
  1417. case Type::op: \
  1418. return sizeof(Op::op);
  1419. switch (type()) {
  1420. ENUMERATE_BYTECODE_OPS(__BYTECODE_OP)
  1421. default:
  1422. VERIFY_NOT_REACHED();
  1423. }
  1424. #undef __BYTECODE_OP
  1425. }
  1426. ALWAYS_INLINE bool Instruction::is_terminator() const
  1427. {
  1428. #define __BYTECODE_OP(op) \
  1429. case Type::op: \
  1430. return Op::op::IsTerminator;
  1431. switch (type()) {
  1432. ENUMERATE_BYTECODE_OPS(__BYTECODE_OP)
  1433. default:
  1434. VERIFY_NOT_REACHED();
  1435. }
  1436. #undef __BYTECODE_OP
  1437. }
  1438. }