Op.h 59 KB

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