Op.h 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333
  1. /*
  2. * Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, Linus Groh <linusg@serenityos.org>
  4. * Copyright (c) 2021, Gunnar Beutner <gbeutner@serenityos.org>
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #pragma once
  9. #include <AK/StdLibExtras.h>
  10. #include <LibCrypto/BigInt/SignedBigInteger.h>
  11. #include <LibJS/Bytecode/IdentifierTable.h>
  12. #include <LibJS/Bytecode/Instruction.h>
  13. #include <LibJS/Bytecode/Label.h>
  14. #include <LibJS/Bytecode/Register.h>
  15. #include <LibJS/Bytecode/StringTable.h>
  16. #include <LibJS/Heap/Cell.h>
  17. #include <LibJS/Runtime/Environment.h>
  18. #include <LibJS/Runtime/EnvironmentCoordinate.h>
  19. #include <LibJS/Runtime/Value.h>
  20. #include <LibJS/Runtime/ValueTraits.h>
  21. namespace JS {
  22. class FunctionExpression;
  23. }
  24. namespace JS::Bytecode::Op {
  25. class Load final : public Instruction {
  26. public:
  27. explicit Load(Register src)
  28. : Instruction(Type::Load)
  29. , m_src(src)
  30. {
  31. }
  32. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  33. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  34. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  35. void replace_references_impl(Register from, Register to)
  36. {
  37. if (m_src == from)
  38. m_src = to;
  39. }
  40. private:
  41. Register m_src;
  42. };
  43. class LoadImmediate final : public Instruction {
  44. public:
  45. explicit LoadImmediate(Value value)
  46. : Instruction(Type::LoadImmediate)
  47. , m_value(value)
  48. {
  49. }
  50. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  51. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  52. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  53. void replace_references_impl(Register, Register) { }
  54. private:
  55. Value m_value;
  56. };
  57. class Store final : public Instruction {
  58. public:
  59. explicit Store(Register dst)
  60. : Instruction(Type::Store)
  61. , m_dst(dst)
  62. {
  63. }
  64. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  65. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  66. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  67. void replace_references_impl(Register, Register) { }
  68. Register dst() const { return m_dst; }
  69. private:
  70. Register m_dst;
  71. };
  72. #define JS_ENUMERATE_COMMON_BINARY_OPS(O) \
  73. O(Add, add) \
  74. O(Sub, sub) \
  75. O(Mul, mul) \
  76. O(Div, div) \
  77. O(Exp, exp) \
  78. O(Mod, mod) \
  79. O(In, in) \
  80. O(InstanceOf, instance_of) \
  81. O(GreaterThan, greater_than) \
  82. O(GreaterThanEquals, greater_than_equals) \
  83. O(LessThan, less_than) \
  84. O(LessThanEquals, less_than_equals) \
  85. O(LooselyInequals, abstract_inequals) \
  86. O(LooselyEquals, abstract_equals) \
  87. O(StrictlyInequals, typed_inequals) \
  88. O(StrictlyEquals, typed_equals) \
  89. O(BitwiseAnd, bitwise_and) \
  90. O(BitwiseOr, bitwise_or) \
  91. O(BitwiseXor, bitwise_xor) \
  92. O(LeftShift, left_shift) \
  93. O(RightShift, right_shift) \
  94. O(UnsignedRightShift, unsigned_right_shift)
  95. #define JS_DECLARE_COMMON_BINARY_OP(OpTitleCase, op_snake_case) \
  96. class OpTitleCase final : public Instruction { \
  97. public: \
  98. explicit OpTitleCase(Register lhs_reg) \
  99. : Instruction(Type::OpTitleCase) \
  100. , m_lhs_reg(lhs_reg) \
  101. { \
  102. } \
  103. \
  104. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const; \
  105. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const; \
  106. void replace_references_impl(BasicBlock const&, BasicBlock const&) \
  107. { \
  108. } \
  109. void replace_references_impl(Register from, Register to) \
  110. { \
  111. if (m_lhs_reg == from) \
  112. m_lhs_reg = to; \
  113. } \
  114. \
  115. private: \
  116. Register m_lhs_reg; \
  117. };
  118. JS_ENUMERATE_COMMON_BINARY_OPS(JS_DECLARE_COMMON_BINARY_OP)
  119. #undef JS_DECLARE_COMMON_BINARY_OP
  120. #define JS_ENUMERATE_COMMON_UNARY_OPS(O) \
  121. O(BitwiseNot, bitwise_not) \
  122. O(Not, not_) \
  123. O(UnaryPlus, unary_plus) \
  124. O(UnaryMinus, unary_minus) \
  125. O(Typeof, typeof_)
  126. #define JS_DECLARE_COMMON_UNARY_OP(OpTitleCase, op_snake_case) \
  127. class OpTitleCase final : public Instruction { \
  128. public: \
  129. OpTitleCase() \
  130. : Instruction(Type::OpTitleCase) \
  131. { \
  132. } \
  133. \
  134. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const; \
  135. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const; \
  136. void replace_references_impl(BasicBlock const&, BasicBlock const&) \
  137. { \
  138. } \
  139. void replace_references_impl(Register, Register) \
  140. { \
  141. } \
  142. };
  143. JS_ENUMERATE_COMMON_UNARY_OPS(JS_DECLARE_COMMON_UNARY_OP)
  144. #undef JS_DECLARE_COMMON_UNARY_OP
  145. class NewString final : public Instruction {
  146. public:
  147. explicit NewString(StringTableIndex string)
  148. : Instruction(Type::NewString)
  149. , m_string(string)
  150. {
  151. }
  152. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  153. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  154. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  155. void replace_references_impl(Register, Register) { }
  156. private:
  157. StringTableIndex m_string;
  158. };
  159. class NewObject final : public Instruction {
  160. public:
  161. NewObject()
  162. : Instruction(Type::NewObject)
  163. {
  164. }
  165. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  166. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  167. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  168. void replace_references_impl(Register, Register) { }
  169. };
  170. class NewRegExp final : public Instruction {
  171. public:
  172. NewRegExp(StringTableIndex source_index, StringTableIndex flags_index)
  173. : Instruction(Type::NewRegExp)
  174. , m_source_index(source_index)
  175. , m_flags_index(flags_index)
  176. {
  177. }
  178. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  179. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  180. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  181. void replace_references_impl(Register, Register) { }
  182. private:
  183. StringTableIndex m_source_index;
  184. StringTableIndex m_flags_index;
  185. };
  186. #define JS_ENUMERATE_NEW_BUILTIN_ERROR_OPS(O) \
  187. O(TypeError)
  188. #define JS_DECLARE_NEW_BUILTIN_ERROR_OP(ErrorName) \
  189. class New##ErrorName final : public Instruction { \
  190. public: \
  191. explicit New##ErrorName(StringTableIndex error_string) \
  192. : Instruction(Type::New##ErrorName) \
  193. , m_error_string(error_string) \
  194. { \
  195. } \
  196. \
  197. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const; \
  198. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const; \
  199. void replace_references_impl(BasicBlock const&, BasicBlock const&) \
  200. { \
  201. } \
  202. void replace_references_impl(Register, Register) \
  203. { \
  204. } \
  205. \
  206. private: \
  207. StringTableIndex m_error_string; \
  208. };
  209. JS_ENUMERATE_NEW_BUILTIN_ERROR_OPS(JS_DECLARE_NEW_BUILTIN_ERROR_OP)
  210. #undef JS_DECLARE_NEW_BUILTIN_ERROR_OP
  211. // NOTE: This instruction is variable-width depending on the number of excluded names
  212. class CopyObjectExcludingProperties final : public Instruction {
  213. public:
  214. CopyObjectExcludingProperties(Register from_object, Vector<Register> const& excluded_names)
  215. : Instruction(Type::CopyObjectExcludingProperties)
  216. , m_from_object(from_object)
  217. , m_excluded_names_count(excluded_names.size())
  218. {
  219. for (size_t i = 0; i < m_excluded_names_count; i++)
  220. m_excluded_names[i] = excluded_names[i];
  221. }
  222. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  223. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  224. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  225. void replace_references_impl(Register from, Register to);
  226. size_t length_impl() const { return sizeof(*this) + sizeof(Register) * m_excluded_names_count; }
  227. private:
  228. Register m_from_object;
  229. size_t m_excluded_names_count { 0 };
  230. Register m_excluded_names[];
  231. };
  232. class NewBigInt final : public Instruction {
  233. public:
  234. explicit NewBigInt(Crypto::SignedBigInteger bigint)
  235. : Instruction(Type::NewBigInt)
  236. , m_bigint(move(bigint))
  237. {
  238. }
  239. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  240. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  241. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  242. void replace_references_impl(Register, Register) { }
  243. private:
  244. Crypto::SignedBigInteger m_bigint;
  245. };
  246. // NOTE: This instruction is variable-width depending on the number of elements!
  247. class NewArray final : public Instruction {
  248. public:
  249. NewArray()
  250. : Instruction(Type::NewArray)
  251. , m_element_count(0)
  252. {
  253. }
  254. explicit NewArray(AK::Array<Register, 2> const& elements_range)
  255. : Instruction(Type::NewArray)
  256. , m_element_count(elements_range[1].index() - elements_range[0].index() + 1)
  257. {
  258. m_elements[0] = elements_range[0];
  259. m_elements[1] = elements_range[1];
  260. }
  261. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  262. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  263. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  264. // Note: The underlying element range shall never be changed item, by item
  265. // shifting it may be done in the future
  266. void replace_references_impl(Register from, Register) { VERIFY(!m_element_count || from.index() < start().index() || from.index() > end().index()); }
  267. size_t length_impl() const
  268. {
  269. return sizeof(*this) + sizeof(Register) * (m_element_count == 0 ? 0 : 2);
  270. }
  271. Register start() const
  272. {
  273. VERIFY(m_element_count);
  274. return m_elements[0];
  275. }
  276. Register end() const
  277. {
  278. VERIFY(m_element_count);
  279. return m_elements[1];
  280. }
  281. size_t element_count() const { return m_element_count; }
  282. private:
  283. size_t m_element_count { 0 };
  284. Register m_elements[];
  285. };
  286. class Append final : public Instruction {
  287. public:
  288. Append(Register lhs, bool is_spread)
  289. : Instruction(Type::Append)
  290. , m_lhs(lhs)
  291. , m_is_spread(is_spread)
  292. {
  293. }
  294. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  295. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  296. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  297. // Note: This should never do anything, the lhs should always be an array, that is currently being constructed
  298. void replace_references_impl(Register from, Register) { VERIFY(from != m_lhs); }
  299. private:
  300. Register m_lhs;
  301. bool m_is_spread = false;
  302. };
  303. class IteratorToArray final : public Instruction {
  304. public:
  305. IteratorToArray()
  306. : Instruction(Type::IteratorToArray)
  307. {
  308. }
  309. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  310. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  311. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  312. void replace_references_impl(Register, Register) { }
  313. };
  314. class ConcatString final : public Instruction {
  315. public:
  316. explicit ConcatString(Register lhs)
  317. : Instruction(Type::ConcatString)
  318. , m_lhs(lhs)
  319. {
  320. }
  321. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  322. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  323. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  324. // Note: lhs should always be a string in construction, so this should never do anything
  325. void replace_references_impl(Register from, Register) { VERIFY(from != m_lhs); }
  326. private:
  327. Register m_lhs;
  328. };
  329. enum class EnvironmentMode {
  330. Lexical,
  331. Var,
  332. };
  333. class CreateLexicalEnvironment final : public Instruction {
  334. public:
  335. explicit CreateLexicalEnvironment()
  336. : Instruction(Type::CreateLexicalEnvironment)
  337. {
  338. }
  339. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  340. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  341. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  342. void replace_references_impl(Register, Register) { }
  343. };
  344. class EnterObjectEnvironment final : public Instruction {
  345. public:
  346. explicit EnterObjectEnvironment()
  347. : Instruction(Type::EnterObjectEnvironment)
  348. {
  349. }
  350. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  351. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  352. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  353. void replace_references_impl(Register, Register) { }
  354. };
  355. class CreateVariable final : public Instruction {
  356. public:
  357. explicit CreateVariable(IdentifierTableIndex identifier, EnvironmentMode mode, bool is_immutable, bool is_global = false)
  358. : Instruction(Type::CreateVariable)
  359. , m_identifier(identifier)
  360. , m_mode(mode)
  361. , m_is_immutable(is_immutable)
  362. , m_is_global(is_global)
  363. {
  364. }
  365. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  366. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  367. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  368. void replace_references_impl(Register, Register) { }
  369. private:
  370. IdentifierTableIndex m_identifier;
  371. EnvironmentMode m_mode;
  372. bool m_is_immutable : 4 { false };
  373. bool m_is_global : 4 { false };
  374. };
  375. class SetVariable final : public Instruction {
  376. public:
  377. enum class InitializationMode {
  378. Initialize,
  379. Set,
  380. InitializeOrSet,
  381. };
  382. explicit SetVariable(IdentifierTableIndex identifier, InitializationMode initialization_mode = InitializationMode::Set, EnvironmentMode mode = EnvironmentMode::Lexical)
  383. : Instruction(Type::SetVariable)
  384. , m_identifier(identifier)
  385. , m_mode(mode)
  386. , m_initialization_mode(initialization_mode)
  387. {
  388. }
  389. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  390. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  391. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  392. void replace_references_impl(Register, Register) { }
  393. IdentifierTableIndex identifier() const { return m_identifier; }
  394. private:
  395. IdentifierTableIndex m_identifier;
  396. EnvironmentMode m_mode;
  397. InitializationMode m_initialization_mode { InitializationMode::Set };
  398. };
  399. class GetVariable final : public Instruction {
  400. public:
  401. explicit GetVariable(IdentifierTableIndex identifier)
  402. : Instruction(Type::GetVariable)
  403. , m_identifier(identifier)
  404. {
  405. }
  406. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  407. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  408. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  409. void replace_references_impl(Register, Register) { }
  410. IdentifierTableIndex identifier() const { return m_identifier; }
  411. private:
  412. IdentifierTableIndex m_identifier;
  413. Optional<EnvironmentCoordinate> mutable m_cached_environment_coordinate;
  414. };
  415. class DeleteVariable final : public Instruction {
  416. public:
  417. explicit DeleteVariable(IdentifierTableIndex identifier)
  418. : Instruction(Type::DeleteVariable)
  419. , m_identifier(identifier)
  420. {
  421. }
  422. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  423. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  424. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  425. void replace_references_impl(Register, Register) { }
  426. IdentifierTableIndex identifier() const { return m_identifier; }
  427. private:
  428. IdentifierTableIndex m_identifier;
  429. };
  430. class GetById final : public Instruction {
  431. public:
  432. explicit GetById(IdentifierTableIndex property)
  433. : Instruction(Type::GetById)
  434. , m_property(property)
  435. {
  436. }
  437. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  438. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  439. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  440. void replace_references_impl(Register, Register) { }
  441. private:
  442. IdentifierTableIndex m_property;
  443. };
  444. class GetPrivateById final : public Instruction {
  445. public:
  446. explicit GetPrivateById(IdentifierTableIndex property)
  447. : Instruction(Type::GetPrivateById)
  448. , m_property(property)
  449. {
  450. }
  451. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  452. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  453. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  454. void replace_references_impl(Register, Register) { }
  455. private:
  456. IdentifierTableIndex m_property;
  457. };
  458. enum class PropertyKind {
  459. Getter,
  460. Setter,
  461. KeyValue,
  462. Spread,
  463. ProtoSetter,
  464. };
  465. class PutById final : public Instruction {
  466. public:
  467. explicit PutById(Register base, IdentifierTableIndex property, PropertyKind kind = PropertyKind::KeyValue)
  468. : Instruction(Type::PutById)
  469. , m_base(base)
  470. , m_property(property)
  471. , m_kind(kind)
  472. {
  473. }
  474. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  475. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  476. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  477. void replace_references_impl(Register from, Register to)
  478. {
  479. if (m_base == from)
  480. m_base = to;
  481. }
  482. private:
  483. Register m_base;
  484. IdentifierTableIndex m_property;
  485. PropertyKind m_kind;
  486. };
  487. class PutPrivateById final : public Instruction {
  488. public:
  489. explicit PutPrivateById(Register base, IdentifierTableIndex property, PropertyKind kind = PropertyKind::KeyValue)
  490. : Instruction(Type::PutPrivateById)
  491. , m_base(base)
  492. , m_property(property)
  493. , m_kind(kind)
  494. {
  495. }
  496. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  497. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  498. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  499. void replace_references_impl(Register from, Register to)
  500. {
  501. if (m_base == from)
  502. m_base = to;
  503. }
  504. private:
  505. Register m_base;
  506. IdentifierTableIndex m_property;
  507. PropertyKind m_kind;
  508. };
  509. class DeleteById final : public Instruction {
  510. public:
  511. explicit DeleteById(IdentifierTableIndex property)
  512. : Instruction(Type::DeleteById)
  513. , m_property(property)
  514. {
  515. }
  516. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  517. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  518. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  519. void replace_references_impl(Register, Register) { }
  520. private:
  521. IdentifierTableIndex m_property;
  522. };
  523. class GetByValue final : public Instruction {
  524. public:
  525. explicit GetByValue(Register base)
  526. : Instruction(Type::GetByValue)
  527. , m_base(base)
  528. {
  529. }
  530. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  531. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  532. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  533. void replace_references_impl(Register from, Register to)
  534. {
  535. if (m_base == from)
  536. m_base = to;
  537. }
  538. private:
  539. Register m_base;
  540. };
  541. class PutByValue final : public Instruction {
  542. public:
  543. PutByValue(Register base, Register property, PropertyKind kind = PropertyKind::KeyValue)
  544. : Instruction(Type::PutByValue)
  545. , m_base(base)
  546. , m_property(property)
  547. , m_kind(kind)
  548. {
  549. }
  550. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  551. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  552. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  553. void replace_references_impl(Register from, Register to)
  554. {
  555. if (m_base == from)
  556. m_base = to;
  557. }
  558. private:
  559. Register m_base;
  560. Register m_property;
  561. PropertyKind m_kind;
  562. };
  563. class DeleteByValue final : public Instruction {
  564. public:
  565. DeleteByValue(Register base)
  566. : Instruction(Type::DeleteByValue)
  567. , m_base(base)
  568. {
  569. }
  570. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  571. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  572. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  573. void replace_references_impl(Register from, Register to)
  574. {
  575. if (m_base == from)
  576. m_base = to;
  577. }
  578. private:
  579. Register m_base;
  580. };
  581. class Jump : public Instruction {
  582. public:
  583. constexpr static bool IsTerminator = true;
  584. explicit Jump(Type type, Optional<Label> taken_target = {}, Optional<Label> nontaken_target = {})
  585. : Instruction(type)
  586. , m_true_target(move(taken_target))
  587. , m_false_target(move(nontaken_target))
  588. {
  589. }
  590. explicit Jump(Optional<Label> taken_target = {}, Optional<Label> nontaken_target = {})
  591. : Instruction(Type::Jump)
  592. , m_true_target(move(taken_target))
  593. , m_false_target(move(nontaken_target))
  594. {
  595. }
  596. void set_targets(Optional<Label> true_target, Optional<Label> false_target)
  597. {
  598. m_true_target = move(true_target);
  599. m_false_target = move(false_target);
  600. }
  601. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  602. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  603. void replace_references_impl(BasicBlock const&, BasicBlock const&);
  604. void replace_references_impl(Register, Register) { }
  605. auto& true_target() const { return m_true_target; }
  606. auto& false_target() const { return m_false_target; }
  607. protected:
  608. Optional<Label> m_true_target;
  609. Optional<Label> m_false_target;
  610. };
  611. class JumpConditional final : public Jump {
  612. public:
  613. explicit JumpConditional(Optional<Label> true_target = {}, Optional<Label> false_target = {})
  614. : Jump(Type::JumpConditional, move(true_target), move(false_target))
  615. {
  616. }
  617. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  618. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  619. };
  620. class JumpNullish final : public Jump {
  621. public:
  622. explicit JumpNullish(Optional<Label> true_target = {}, Optional<Label> false_target = {})
  623. : Jump(Type::JumpNullish, move(true_target), move(false_target))
  624. {
  625. }
  626. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  627. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  628. };
  629. class JumpUndefined final : public Jump {
  630. public:
  631. explicit JumpUndefined(Optional<Label> true_target = {}, Optional<Label> false_target = {})
  632. : Jump(Type::JumpUndefined, move(true_target), move(false_target))
  633. {
  634. }
  635. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  636. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  637. };
  638. // NOTE: This instruction is variable-width depending on the number of arguments!
  639. class Call final : public Instruction {
  640. public:
  641. enum class CallType {
  642. Call,
  643. Construct,
  644. DirectEval,
  645. };
  646. Call(CallType type, Register callee, Register this_value, Optional<StringTableIndex> expression_string = {})
  647. : Instruction(Type::Call)
  648. , m_callee(callee)
  649. , m_this_value(this_value)
  650. , m_type(type)
  651. , m_expression_string(expression_string)
  652. {
  653. }
  654. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  655. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  656. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  657. void replace_references_impl(Register, Register);
  658. Completion throw_type_error_for_callee(Bytecode::Interpreter&, StringView callee_type) const;
  659. private:
  660. Register m_callee;
  661. Register m_this_value;
  662. CallType m_type;
  663. Optional<StringTableIndex> m_expression_string;
  664. };
  665. // NOTE: This instruction is variable-width depending on the number of arguments!
  666. class SuperCall : public Instruction {
  667. public:
  668. explicit SuperCall(bool is_synthetic)
  669. : Instruction(Type::SuperCall)
  670. , m_is_synthetic(is_synthetic)
  671. {
  672. }
  673. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  674. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  675. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  676. void replace_references_impl(Register, Register) { }
  677. private:
  678. bool m_is_synthetic;
  679. };
  680. class NewClass final : public Instruction {
  681. public:
  682. explicit NewClass(ClassExpression const& class_expression, Optional<DeprecatedFlyString const&> lhs_name)
  683. : Instruction(Type::NewClass)
  684. , m_class_expression(class_expression)
  685. , m_lhs_name(lhs_name.has_value() ? *lhs_name : Optional<DeprecatedFlyString> {})
  686. {
  687. }
  688. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  689. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  690. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  691. void replace_references_impl(Register, Register) { }
  692. private:
  693. ClassExpression const& m_class_expression;
  694. Optional<DeprecatedFlyString> m_lhs_name;
  695. };
  696. class NewFunction final : public Instruction {
  697. public:
  698. explicit NewFunction(FunctionExpression const& function_node, Optional<DeprecatedFlyString const&> lhs_name, Optional<Register> home_object = {})
  699. : Instruction(Type::NewFunction)
  700. , m_function_node(function_node)
  701. , m_lhs_name(lhs_name.has_value() ? *lhs_name : Optional<DeprecatedFlyString> {})
  702. , m_home_object(move(home_object))
  703. {
  704. }
  705. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  706. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  707. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  708. void replace_references_impl(Register, Register);
  709. private:
  710. FunctionExpression const& m_function_node;
  711. Optional<DeprecatedFlyString> m_lhs_name;
  712. Optional<Register> m_home_object;
  713. };
  714. class BlockDeclarationInstantiation final : public Instruction {
  715. public:
  716. explicit BlockDeclarationInstantiation(ScopeNode const& scope_node)
  717. : Instruction(Type::BlockDeclarationInstantiation)
  718. , m_scope_node(scope_node)
  719. {
  720. }
  721. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  722. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  723. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  724. void replace_references_impl(Register, Register) { }
  725. private:
  726. ScopeNode const& m_scope_node;
  727. };
  728. class Return final : public Instruction {
  729. public:
  730. constexpr static bool IsTerminator = true;
  731. Return()
  732. : Instruction(Type::Return)
  733. {
  734. }
  735. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  736. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  737. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  738. void replace_references_impl(Register, Register) { }
  739. };
  740. class Increment final : public Instruction {
  741. public:
  742. Increment()
  743. : Instruction(Type::Increment)
  744. {
  745. }
  746. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  747. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  748. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  749. void replace_references_impl(Register, Register) { }
  750. };
  751. class Decrement final : public Instruction {
  752. public:
  753. Decrement()
  754. : Instruction(Type::Decrement)
  755. {
  756. }
  757. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  758. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  759. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  760. void replace_references_impl(Register, Register) { }
  761. };
  762. class ToNumeric final : public Instruction {
  763. public:
  764. ToNumeric()
  765. : Instruction(Type::ToNumeric)
  766. {
  767. }
  768. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  769. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  770. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  771. void replace_references_impl(Register, Register) { }
  772. };
  773. class Throw final : public Instruction {
  774. public:
  775. constexpr static bool IsTerminator = true;
  776. Throw()
  777. : Instruction(Type::Throw)
  778. {
  779. }
  780. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  781. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  782. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  783. void replace_references_impl(Register, Register) { }
  784. };
  785. class ThrowIfNotObject final : public Instruction {
  786. public:
  787. ThrowIfNotObject()
  788. : Instruction(Type::ThrowIfNotObject)
  789. {
  790. }
  791. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  792. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  793. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  794. void replace_references_impl(Register, Register) { }
  795. };
  796. class EnterUnwindContext final : public Instruction {
  797. public:
  798. constexpr static bool IsTerminator = true;
  799. EnterUnwindContext(Label entry_point, Optional<Label> handler_target, Optional<Label> finalizer_target)
  800. : Instruction(Type::EnterUnwindContext)
  801. , m_entry_point(move(entry_point))
  802. , m_handler_target(move(handler_target))
  803. , m_finalizer_target(move(finalizer_target))
  804. {
  805. }
  806. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  807. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  808. void replace_references_impl(BasicBlock const&, BasicBlock const&);
  809. void replace_references_impl(Register, Register) { }
  810. auto& entry_point() const { return m_entry_point; }
  811. auto& handler_target() const { return m_handler_target; }
  812. auto& finalizer_target() const { return m_finalizer_target; }
  813. private:
  814. Label m_entry_point;
  815. Optional<Label> m_handler_target;
  816. Optional<Label> m_finalizer_target;
  817. };
  818. class ScheduleJump final : public Instruction {
  819. public:
  820. // Note: We use this instruction to tell the next `finally` block to
  821. // continue execution with a specific break/continue target;
  822. // FIXME: We currently don't clear the interpreter internal flag, when we change
  823. // the control-flow (`break`, `continue`) in a finally-block,
  824. // FIXME: .NET on x86_64 uses a call to the finally instead, which could make this
  825. // easier, at the cost of making control-flow changes (`break`, `continue`, `return`)
  826. // in the finally-block more difficult, but as stated above, those
  827. // aren't handled 100% correctly at the moment anyway
  828. // It might be worth investigating a similar mechanism
  829. constexpr static bool IsTerminator = true;
  830. ScheduleJump(Label target)
  831. : Instruction(Type::ScheduleJump)
  832. , m_target(target)
  833. {
  834. }
  835. Label target() const { return m_target; }
  836. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  837. void replace_references_impl(BasicBlock const& from, BasicBlock const& to)
  838. {
  839. if (&m_target.block() == &from)
  840. m_target = Label { to };
  841. }
  842. void replace_references_impl(Register, Register) { }
  843. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  844. private:
  845. Label m_target;
  846. };
  847. class LeaveLexicalEnvironment final : public Instruction {
  848. public:
  849. LeaveLexicalEnvironment()
  850. : Instruction(Type::LeaveLexicalEnvironment)
  851. {
  852. }
  853. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  854. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  855. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  856. void replace_references_impl(Register, Register) { }
  857. };
  858. class LeaveUnwindContext final : public Instruction {
  859. public:
  860. LeaveUnwindContext()
  861. : Instruction(Type::LeaveUnwindContext)
  862. {
  863. }
  864. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  865. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  866. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  867. void replace_references_impl(Register, Register) { }
  868. };
  869. class ContinuePendingUnwind final : public Instruction {
  870. public:
  871. constexpr static bool IsTerminator = true;
  872. explicit ContinuePendingUnwind(Label resume_target)
  873. : Instruction(Type::ContinuePendingUnwind)
  874. , m_resume_target(resume_target)
  875. {
  876. }
  877. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  878. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  879. void replace_references_impl(BasicBlock const&, BasicBlock const&);
  880. void replace_references_impl(Register, Register) { }
  881. auto& resume_target() const { return m_resume_target; }
  882. private:
  883. Label m_resume_target;
  884. };
  885. class Yield final : public Instruction {
  886. public:
  887. constexpr static bool IsTerminator = true;
  888. explicit Yield(Label continuation_label)
  889. : Instruction(Type::Yield)
  890. , m_continuation_label(continuation_label)
  891. {
  892. }
  893. explicit Yield(nullptr_t)
  894. : Instruction(Type::Yield)
  895. {
  896. }
  897. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  898. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  899. void replace_references_impl(BasicBlock const&, BasicBlock const&);
  900. void replace_references_impl(Register, Register) { }
  901. auto& continuation() const { return m_continuation_label; }
  902. private:
  903. Optional<Label> m_continuation_label;
  904. };
  905. class PushDeclarativeEnvironment final : public Instruction {
  906. public:
  907. explicit PushDeclarativeEnvironment(HashMap<u32, Variable> variables)
  908. : Instruction(Type::PushDeclarativeEnvironment)
  909. , m_variables(move(variables))
  910. {
  911. }
  912. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  913. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  914. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  915. void replace_references_impl(Register, Register) { }
  916. private:
  917. HashMap<u32, Variable> m_variables;
  918. };
  919. class GetIterator final : public Instruction {
  920. public:
  921. GetIterator()
  922. : Instruction(Type::GetIterator)
  923. {
  924. }
  925. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  926. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  927. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  928. void replace_references_impl(Register, Register) { }
  929. };
  930. class GetMethod final : public Instruction {
  931. public:
  932. GetMethod(IdentifierTableIndex property)
  933. : Instruction(Type::GetMethod)
  934. , m_property(property)
  935. {
  936. }
  937. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  938. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  939. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  940. void replace_references_impl(Register, Register) { }
  941. private:
  942. IdentifierTableIndex m_property;
  943. };
  944. class GetObjectPropertyIterator final : public Instruction {
  945. public:
  946. GetObjectPropertyIterator()
  947. : Instruction(Type::GetObjectPropertyIterator)
  948. {
  949. }
  950. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  951. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  952. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  953. void replace_references_impl(Register, Register) { }
  954. };
  955. class IteratorClose final : public Instruction {
  956. public:
  957. IteratorClose(Completion::Type completion_type, Optional<Value> completion_value)
  958. : Instruction(Type::IteratorClose)
  959. , m_completion_type(completion_type)
  960. , m_completion_value(completion_value)
  961. {
  962. }
  963. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  964. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  965. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  966. void replace_references_impl(Register, Register) { }
  967. private:
  968. Completion::Type m_completion_type { Completion::Type::Normal };
  969. Optional<Value> m_completion_value;
  970. };
  971. class IteratorNext final : public Instruction {
  972. public:
  973. IteratorNext()
  974. : Instruction(Type::IteratorNext)
  975. {
  976. }
  977. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  978. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  979. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  980. void replace_references_impl(Register, Register) { }
  981. };
  982. class IteratorResultDone final : public Instruction {
  983. public:
  984. IteratorResultDone()
  985. : Instruction(Type::IteratorResultDone)
  986. {
  987. }
  988. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  989. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  990. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  991. void replace_references_impl(Register, Register) { }
  992. };
  993. class IteratorResultValue final : public Instruction {
  994. public:
  995. IteratorResultValue()
  996. : Instruction(Type::IteratorResultValue)
  997. {
  998. }
  999. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1000. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  1001. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  1002. void replace_references_impl(Register, Register) { }
  1003. };
  1004. class ResolveThisBinding final : public Instruction {
  1005. public:
  1006. explicit ResolveThisBinding()
  1007. : Instruction(Type::ResolveThisBinding)
  1008. {
  1009. }
  1010. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1011. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  1012. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  1013. void replace_references_impl(Register, Register) { }
  1014. };
  1015. class ResolveSuperBase final : public Instruction {
  1016. public:
  1017. explicit ResolveSuperBase()
  1018. : Instruction(Type::ResolveSuperBase)
  1019. {
  1020. }
  1021. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1022. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  1023. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  1024. void replace_references_impl(Register, Register) { }
  1025. };
  1026. class GetNewTarget final : public Instruction {
  1027. public:
  1028. explicit GetNewTarget()
  1029. : Instruction(Type::GetNewTarget)
  1030. {
  1031. }
  1032. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1033. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  1034. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  1035. void replace_references_impl(Register, Register) { }
  1036. };
  1037. class TypeofVariable final : public Instruction {
  1038. public:
  1039. explicit TypeofVariable(IdentifierTableIndex identifier)
  1040. : Instruction(Type::TypeofVariable)
  1041. , m_identifier(identifier)
  1042. {
  1043. }
  1044. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1045. DeprecatedString to_deprecated_string_impl(Bytecode::Executable const&) const;
  1046. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  1047. void replace_references_impl(Register, Register) { }
  1048. private:
  1049. IdentifierTableIndex m_identifier;
  1050. };
  1051. }
  1052. namespace JS::Bytecode {
  1053. ALWAYS_INLINE ThrowCompletionOr<void> Instruction::execute(Bytecode::Interpreter& interpreter) const
  1054. {
  1055. #define __BYTECODE_OP(op) \
  1056. case Instruction::Type::op: \
  1057. return static_cast<Bytecode::Op::op const&>(*this).execute_impl(interpreter);
  1058. switch (type()) {
  1059. ENUMERATE_BYTECODE_OPS(__BYTECODE_OP)
  1060. default:
  1061. VERIFY_NOT_REACHED();
  1062. }
  1063. #undef __BYTECODE_OP
  1064. }
  1065. ALWAYS_INLINE void Instruction::replace_references(BasicBlock const& from, BasicBlock const& to)
  1066. {
  1067. #define __BYTECODE_OP(op) \
  1068. case Instruction::Type::op: \
  1069. return static_cast<Bytecode::Op::op&>(*this).replace_references_impl(from, to);
  1070. switch (type()) {
  1071. ENUMERATE_BYTECODE_OPS(__BYTECODE_OP)
  1072. default:
  1073. VERIFY_NOT_REACHED();
  1074. }
  1075. #undef __BYTECODE_OP
  1076. }
  1077. ALWAYS_INLINE void Instruction::replace_references(Register from, Register to)
  1078. {
  1079. #define __BYTECODE_OP(op) \
  1080. case Instruction::Type::op: \
  1081. return static_cast<Bytecode::Op::op&>(*this).replace_references_impl(from, to);
  1082. switch (type()) {
  1083. ENUMERATE_BYTECODE_OPS(__BYTECODE_OP)
  1084. default:
  1085. VERIFY_NOT_REACHED();
  1086. }
  1087. #undef __BYTECODE_OP
  1088. }
  1089. ALWAYS_INLINE size_t Instruction::length() const
  1090. {
  1091. if (type() == Type::NewArray)
  1092. return round_up_to_power_of_two(static_cast<Op::NewArray const&>(*this).length_impl(), alignof(void*));
  1093. if (type() == Type::CopyObjectExcludingProperties)
  1094. return round_up_to_power_of_two(static_cast<Op::CopyObjectExcludingProperties const&>(*this).length_impl(), alignof(void*));
  1095. #define __BYTECODE_OP(op) \
  1096. case Type::op: \
  1097. return sizeof(Op::op);
  1098. switch (type()) {
  1099. ENUMERATE_BYTECODE_OPS(__BYTECODE_OP)
  1100. default:
  1101. VERIFY_NOT_REACHED();
  1102. }
  1103. #undef __BYTECODE_OP
  1104. }
  1105. ALWAYS_INLINE bool Instruction::is_terminator() const
  1106. {
  1107. #define __BYTECODE_OP(op) \
  1108. case Type::op: \
  1109. return Op::op::IsTerminator;
  1110. switch (type()) {
  1111. ENUMERATE_BYTECODE_OPS(__BYTECODE_OP)
  1112. default:
  1113. VERIFY_NOT_REACHED();
  1114. }
  1115. #undef __BYTECODE_OP
  1116. }
  1117. }