Op.h 61 KB

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