Op.h 79 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633
  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 CreateRestParams final : public Instruction {
  31. public:
  32. CreateRestParams(Operand dst, u32 rest_index)
  33. : Instruction(Type::CreateRestParams)
  34. , m_dst(dst)
  35. , m_rest_index(rest_index)
  36. {
  37. }
  38. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  39. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  40. void visit_operands_impl(Function<void(Operand&)> visitor)
  41. {
  42. visitor(m_dst);
  43. }
  44. private:
  45. Operand m_dst;
  46. u32 m_rest_index;
  47. };
  48. class CreateArguments final : public Instruction {
  49. public:
  50. enum class Kind {
  51. Mapped,
  52. Unmapped,
  53. };
  54. CreateArguments(Kind kind, bool is_immutable)
  55. : Instruction(Type::CreateArguments)
  56. , m_kind(kind)
  57. , m_is_immutable(is_immutable)
  58. {
  59. }
  60. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  61. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  62. private:
  63. Kind m_kind;
  64. bool m_is_immutable { false };
  65. };
  66. class Mov final : public Instruction {
  67. public:
  68. Mov(Operand dst, Operand src)
  69. : Instruction(Type::Mov)
  70. , m_dst(dst)
  71. , m_src(src)
  72. {
  73. }
  74. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  75. void visit_operands_impl(Function<void(Operand&)> visitor)
  76. {
  77. visitor(m_dst);
  78. visitor(m_src);
  79. }
  80. Operand dst() const { return m_dst; }
  81. Operand src() const { return m_src; }
  82. private:
  83. Operand m_dst;
  84. Operand m_src;
  85. };
  86. #define JS_ENUMERATE_COMMON_BINARY_OPS_WITH_FAST_PATH(O) \
  87. O(Add, add) \
  88. O(BitwiseAnd, bitwise_and) \
  89. O(BitwiseOr, bitwise_or) \
  90. O(BitwiseXor, bitwise_xor) \
  91. O(GreaterThan, greater_than) \
  92. O(GreaterThanEquals, greater_than_equals) \
  93. O(LeftShift, left_shift) \
  94. O(LessThan, less_than) \
  95. O(LessThanEquals, less_than_equals) \
  96. O(Mul, mul) \
  97. O(RightShift, right_shift) \
  98. O(Sub, sub) \
  99. O(UnsignedRightShift, unsigned_right_shift)
  100. #define JS_ENUMERATE_COMMON_BINARY_OPS_WITHOUT_FAST_PATH(O) \
  101. O(Div, div) \
  102. O(Exp, exp) \
  103. O(Mod, mod) \
  104. O(In, in) \
  105. O(InstanceOf, instance_of) \
  106. O(LooselyInequals, loosely_inequals) \
  107. O(LooselyEquals, loosely_equals) \
  108. O(StrictlyInequals, strict_inequals) \
  109. O(StrictlyEquals, strict_equals)
  110. #define JS_DECLARE_COMMON_BINARY_OP(OpTitleCase, op_snake_case) \
  111. class OpTitleCase final : public Instruction { \
  112. public: \
  113. explicit OpTitleCase(Operand dst, Operand lhs, Operand rhs) \
  114. : Instruction(Type::OpTitleCase) \
  115. , m_dst(dst) \
  116. , m_lhs(lhs) \
  117. , m_rhs(rhs) \
  118. { \
  119. } \
  120. \
  121. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const; \
  122. ByteString to_byte_string_impl(Bytecode::Executable const&) const; \
  123. void visit_operands_impl(Function<void(Operand&)> visitor) \
  124. { \
  125. visitor(m_dst); \
  126. visitor(m_lhs); \
  127. visitor(m_rhs); \
  128. } \
  129. \
  130. Operand dst() const { return m_dst; } \
  131. Operand lhs() const { return m_lhs; } \
  132. Operand rhs() const { return m_rhs; } \
  133. \
  134. private: \
  135. Operand m_dst; \
  136. Operand m_lhs; \
  137. Operand m_rhs; \
  138. };
  139. JS_ENUMERATE_COMMON_BINARY_OPS_WITHOUT_FAST_PATH(JS_DECLARE_COMMON_BINARY_OP)
  140. JS_ENUMERATE_COMMON_BINARY_OPS_WITH_FAST_PATH(JS_DECLARE_COMMON_BINARY_OP)
  141. #undef JS_DECLARE_COMMON_BINARY_OP
  142. #define JS_ENUMERATE_COMMON_UNARY_OPS(O) \
  143. O(BitwiseNot, bitwise_not) \
  144. O(Not, not_) \
  145. O(UnaryPlus, unary_plus) \
  146. O(UnaryMinus, unary_minus) \
  147. O(Typeof, typeof_)
  148. #define JS_DECLARE_COMMON_UNARY_OP(OpTitleCase, op_snake_case) \
  149. class OpTitleCase final : public Instruction { \
  150. public: \
  151. OpTitleCase(Operand dst, Operand src) \
  152. : Instruction(Type::OpTitleCase) \
  153. , m_dst(dst) \
  154. , m_src(src) \
  155. { \
  156. } \
  157. \
  158. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const; \
  159. ByteString to_byte_string_impl(Bytecode::Executable const&) const; \
  160. void visit_operands_impl(Function<void(Operand&)> visitor) \
  161. { \
  162. visitor(m_dst); \
  163. visitor(m_src); \
  164. } \
  165. \
  166. Operand dst() const { return m_dst; } \
  167. Operand src() const { return m_src; } \
  168. \
  169. private: \
  170. Operand m_dst; \
  171. Operand m_src; \
  172. };
  173. JS_ENUMERATE_COMMON_UNARY_OPS(JS_DECLARE_COMMON_UNARY_OP)
  174. #undef JS_DECLARE_COMMON_UNARY_OP
  175. class NewObject final : public Instruction {
  176. public:
  177. explicit NewObject(Operand dst)
  178. : Instruction(Type::NewObject)
  179. , m_dst(dst)
  180. {
  181. }
  182. void execute_impl(Bytecode::Interpreter&) const;
  183. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  184. void visit_operands_impl(Function<void(Operand&)> visitor)
  185. {
  186. visitor(m_dst);
  187. }
  188. Operand dst() const { return m_dst; }
  189. private:
  190. Operand m_dst;
  191. };
  192. class NewRegExp final : public Instruction {
  193. public:
  194. NewRegExp(Operand dst, StringTableIndex source_index, StringTableIndex flags_index, RegexTableIndex regex_index)
  195. : Instruction(Type::NewRegExp)
  196. , m_dst(dst)
  197. , m_source_index(source_index)
  198. , m_flags_index(flags_index)
  199. , m_regex_index(regex_index)
  200. {
  201. }
  202. void execute_impl(Bytecode::Interpreter&) const;
  203. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  204. void visit_operands_impl(Function<void(Operand&)> visitor)
  205. {
  206. visitor(m_dst);
  207. }
  208. Operand dst() const { return m_dst; }
  209. StringTableIndex source_index() const { return m_source_index; }
  210. StringTableIndex flags_index() const { return m_flags_index; }
  211. RegexTableIndex regex_index() const { return m_regex_index; }
  212. private:
  213. Operand m_dst;
  214. StringTableIndex m_source_index;
  215. StringTableIndex m_flags_index;
  216. RegexTableIndex m_regex_index;
  217. };
  218. #define JS_ENUMERATE_NEW_BUILTIN_ERROR_OPS(O) \
  219. O(TypeError)
  220. #define JS_DECLARE_NEW_BUILTIN_ERROR_OP(ErrorName) \
  221. class New##ErrorName final : public Instruction { \
  222. public: \
  223. New##ErrorName(Operand dst, StringTableIndex error_string) \
  224. : Instruction(Type::New##ErrorName) \
  225. , m_dst(dst) \
  226. , m_error_string(error_string) \
  227. { \
  228. } \
  229. \
  230. void execute_impl(Bytecode::Interpreter&) const; \
  231. ByteString to_byte_string_impl(Bytecode::Executable const&) const; \
  232. void visit_operands_impl(Function<void(Operand&)> visitor) \
  233. { \
  234. visitor(m_dst); \
  235. } \
  236. \
  237. Operand dst() const { return m_dst; } \
  238. StringTableIndex error_string() const { return m_error_string; } \
  239. \
  240. private: \
  241. Operand m_dst; \
  242. StringTableIndex m_error_string; \
  243. };
  244. JS_ENUMERATE_NEW_BUILTIN_ERROR_OPS(JS_DECLARE_NEW_BUILTIN_ERROR_OP)
  245. #undef JS_DECLARE_NEW_BUILTIN_ERROR_OP
  246. // NOTE: This instruction is variable-width depending on the number of excluded names
  247. class CopyObjectExcludingProperties final : public Instruction {
  248. public:
  249. static constexpr bool IsVariableLength = true;
  250. CopyObjectExcludingProperties(Operand dst, Operand from_object, Vector<ScopedOperand> const& excluded_names)
  251. : Instruction(Type::CopyObjectExcludingProperties)
  252. , m_dst(dst)
  253. , m_from_object(from_object)
  254. , m_excluded_names_count(excluded_names.size())
  255. {
  256. for (size_t i = 0; i < m_excluded_names_count; i++)
  257. m_excluded_names[i] = excluded_names[i];
  258. }
  259. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  260. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  261. void visit_operands_impl(Function<void(Operand&)> visitor)
  262. {
  263. visitor(m_dst);
  264. visitor(m_from_object);
  265. for (size_t i = 0; i < m_excluded_names_count; i++)
  266. visitor(m_excluded_names[i]);
  267. }
  268. size_t length_impl() const
  269. {
  270. return round_up_to_power_of_two(alignof(void*), sizeof(*this) + sizeof(Operand) * m_excluded_names_count);
  271. }
  272. Operand dst() const { return m_dst; }
  273. Operand from_object() const { return m_from_object; }
  274. size_t excluded_names_count() const { return m_excluded_names_count; }
  275. Operand const* excluded_names() const { return m_excluded_names; }
  276. private:
  277. Operand m_dst;
  278. Operand m_from_object;
  279. size_t m_excluded_names_count { 0 };
  280. Operand m_excluded_names[];
  281. };
  282. // NOTE: This instruction is variable-width depending on the number of elements!
  283. class NewArray final : public Instruction {
  284. public:
  285. static constexpr bool IsVariableLength = true;
  286. explicit NewArray(Operand dst)
  287. : Instruction(Type::NewArray)
  288. , m_dst(dst)
  289. , m_element_count(0)
  290. {
  291. }
  292. NewArray(Operand dst, ReadonlySpan<ScopedOperand> elements)
  293. : Instruction(Type::NewArray)
  294. , m_dst(dst)
  295. , m_element_count(elements.size())
  296. {
  297. for (size_t i = 0; i < m_element_count; ++i)
  298. m_elements[i] = elements[i];
  299. }
  300. void execute_impl(Bytecode::Interpreter&) const;
  301. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  302. void visit_operands_impl(Function<void(Operand&)> visitor)
  303. {
  304. visitor(m_dst);
  305. for (size_t i = 0; i < m_element_count; i++)
  306. visitor(m_elements[i]);
  307. }
  308. Operand dst() const { return m_dst; }
  309. size_t length_impl() const
  310. {
  311. return round_up_to_power_of_two(alignof(void*), sizeof(*this) + sizeof(Operand) * m_element_count);
  312. }
  313. size_t element_count() const { return m_element_count; }
  314. private:
  315. Operand m_dst;
  316. size_t m_element_count { 0 };
  317. Operand m_elements[];
  318. };
  319. class NewPrimitiveArray final : public Instruction {
  320. public:
  321. static constexpr bool IsVariableLength = true;
  322. NewPrimitiveArray(Operand dst, ReadonlySpan<Value> elements)
  323. : Instruction(Type::NewPrimitiveArray)
  324. , m_dst(dst)
  325. , m_element_count(elements.size())
  326. {
  327. for (size_t i = 0; i < m_element_count; ++i)
  328. m_elements[i] = elements[i];
  329. }
  330. size_t length_impl() const
  331. {
  332. return round_up_to_power_of_two(alignof(void*), sizeof(*this) + sizeof(Value) * m_element_count);
  333. }
  334. void execute_impl(Bytecode::Interpreter&) const;
  335. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  336. void visit_operands_impl(Function<void(Operand&)> visitor)
  337. {
  338. visitor(m_dst);
  339. }
  340. Operand dst() const { return m_dst; }
  341. ReadonlySpan<Value> elements() const { return { m_elements, m_element_count }; }
  342. private:
  343. Operand m_dst;
  344. size_t m_element_count { 0 };
  345. Value m_elements[];
  346. };
  347. class AddPrivateName final : public Instruction {
  348. public:
  349. explicit AddPrivateName(IdentifierTableIndex name)
  350. : Instruction(Type::AddPrivateName)
  351. , m_name(name)
  352. {
  353. }
  354. void execute_impl(Bytecode::Interpreter&) const;
  355. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  356. private:
  357. IdentifierTableIndex m_name;
  358. };
  359. class ArrayAppend final : public Instruction {
  360. public:
  361. ArrayAppend(Operand dst, Operand src, bool is_spread)
  362. : Instruction(Type::ArrayAppend)
  363. , m_dst(dst)
  364. , m_src(src)
  365. , m_is_spread(is_spread)
  366. {
  367. }
  368. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  369. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  370. void visit_operands_impl(Function<void(Operand&)> visitor)
  371. {
  372. visitor(m_dst);
  373. visitor(m_src);
  374. }
  375. Operand dst() const { return m_dst; }
  376. Operand src() const { return m_src; }
  377. bool is_spread() const { return m_is_spread; }
  378. private:
  379. Operand m_dst;
  380. Operand m_src;
  381. bool m_is_spread = false;
  382. };
  383. class ImportCall final : public Instruction {
  384. public:
  385. ImportCall(Operand dst, Operand specifier, Operand options)
  386. : Instruction(Type::ImportCall)
  387. , m_dst(dst)
  388. , m_specifier(specifier)
  389. , m_options(options)
  390. {
  391. }
  392. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  393. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  394. void visit_operands_impl(Function<void(Operand&)> visitor)
  395. {
  396. visitor(m_dst);
  397. visitor(m_specifier);
  398. visitor(m_options);
  399. }
  400. Operand dst() const { return m_dst; }
  401. Operand specifier() const { return m_specifier; }
  402. Operand options() const { return m_options; }
  403. private:
  404. Operand m_dst;
  405. Operand m_specifier;
  406. Operand m_options;
  407. };
  408. class IteratorToArray final : public Instruction {
  409. public:
  410. explicit IteratorToArray(Operand dst, Operand iterator)
  411. : Instruction(Type::IteratorToArray)
  412. , m_dst(dst)
  413. , m_iterator(iterator)
  414. {
  415. }
  416. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  417. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  418. Operand dst() const { return m_dst; }
  419. Operand iterator() const { return m_iterator; }
  420. void visit_operands_impl(Function<void(Operand&)> visitor)
  421. {
  422. visitor(m_dst);
  423. visitor(m_iterator);
  424. }
  425. private:
  426. Operand m_dst;
  427. Operand m_iterator;
  428. };
  429. class ConcatString final : public Instruction {
  430. public:
  431. explicit ConcatString(Operand dst, Operand src)
  432. : Instruction(Type::ConcatString)
  433. , m_dst(dst)
  434. , m_src(src)
  435. {
  436. }
  437. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  438. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  439. Operand dst() const { return m_dst; }
  440. Operand src() const { return m_src; }
  441. void visit_operands_impl(Function<void(Operand&)> visitor)
  442. {
  443. visitor(m_dst);
  444. visitor(m_src);
  445. }
  446. private:
  447. Operand m_dst;
  448. Operand m_src;
  449. };
  450. enum class EnvironmentMode {
  451. Lexical,
  452. Var,
  453. };
  454. enum class BindingInitializationMode {
  455. Initialize,
  456. Set,
  457. };
  458. class CreateLexicalEnvironment final : public Instruction {
  459. public:
  460. explicit CreateLexicalEnvironment(u32 capacity = 0)
  461. : Instruction(Type::CreateLexicalEnvironment)
  462. , m_capacity(capacity)
  463. {
  464. }
  465. void execute_impl(Bytecode::Interpreter&) const;
  466. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  467. private:
  468. u32 m_capacity { 0 };
  469. };
  470. class CreateVariableEnvironment final : public Instruction {
  471. public:
  472. explicit CreateVariableEnvironment(u32 capacity = 0)
  473. : Instruction(Type::CreateVariableEnvironment)
  474. , m_capacity(capacity)
  475. {
  476. }
  477. void execute_impl(Bytecode::Interpreter&) const;
  478. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  479. private:
  480. u32 m_capacity { 0 };
  481. };
  482. class CreatePrivateEnvironment final : public Instruction {
  483. public:
  484. explicit CreatePrivateEnvironment()
  485. : Instruction(Type::CreatePrivateEnvironment)
  486. {
  487. }
  488. void execute_impl(Bytecode::Interpreter&) const;
  489. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  490. };
  491. class EnterObjectEnvironment final : public Instruction {
  492. public:
  493. explicit EnterObjectEnvironment(Operand object)
  494. : Instruction(Type::EnterObjectEnvironment)
  495. , m_object(object)
  496. {
  497. }
  498. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  499. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  500. Operand object() const { return m_object; }
  501. void visit_operands_impl(Function<void(Operand&)> visitor)
  502. {
  503. visitor(m_object);
  504. }
  505. private:
  506. Operand m_object;
  507. };
  508. class Catch final : public Instruction {
  509. public:
  510. explicit Catch(Operand dst)
  511. : Instruction(Type::Catch)
  512. , m_dst(dst)
  513. {
  514. }
  515. void execute_impl(Bytecode::Interpreter&) const;
  516. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  517. Operand dst() const { return m_dst; }
  518. void visit_operands_impl(Function<void(Operand&)> visitor)
  519. {
  520. visitor(m_dst);
  521. }
  522. private:
  523. Operand m_dst;
  524. };
  525. class LeaveFinally final : public Instruction {
  526. public:
  527. explicit LeaveFinally()
  528. : Instruction(Type::LeaveFinally)
  529. {
  530. }
  531. void execute_impl(Bytecode::Interpreter&) const;
  532. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  533. };
  534. class RestoreScheduledJump final : public Instruction {
  535. public:
  536. explicit RestoreScheduledJump()
  537. : Instruction(Type::RestoreScheduledJump)
  538. {
  539. }
  540. void execute_impl(Bytecode::Interpreter&) const;
  541. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  542. };
  543. class CreateVariable final : public Instruction {
  544. public:
  545. explicit CreateVariable(IdentifierTableIndex identifier, EnvironmentMode mode, bool is_immutable, bool is_global = false, bool is_strict = false)
  546. : Instruction(Type::CreateVariable)
  547. , m_identifier(identifier)
  548. , m_mode(mode)
  549. , m_is_immutable(is_immutable)
  550. , m_is_global(is_global)
  551. , m_is_strict(is_strict)
  552. {
  553. }
  554. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  555. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  556. IdentifierTableIndex identifier() const { return m_identifier; }
  557. EnvironmentMode mode() const { return m_mode; }
  558. bool is_immutable() const { return m_is_immutable; }
  559. bool is_global() const { return m_is_global; }
  560. bool is_strict() const { return m_is_strict; }
  561. private:
  562. IdentifierTableIndex m_identifier;
  563. EnvironmentMode m_mode;
  564. bool m_is_immutable : 4 { false };
  565. bool m_is_global : 4 { false };
  566. bool m_is_strict { false };
  567. };
  568. class InitializeLexicalBinding final : public Instruction {
  569. public:
  570. explicit InitializeLexicalBinding(IdentifierTableIndex identifier, Operand src)
  571. : Instruction(Type::InitializeLexicalBinding)
  572. , m_identifier(identifier)
  573. , m_src(src)
  574. {
  575. }
  576. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  577. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  578. void visit_operands_impl(Function<void(Operand&)> visitor)
  579. {
  580. visitor(m_src);
  581. }
  582. IdentifierTableIndex identifier() const { return m_identifier; }
  583. Operand src() const { return m_src; }
  584. private:
  585. IdentifierTableIndex m_identifier;
  586. Operand m_src;
  587. mutable EnvironmentCoordinate m_cache;
  588. };
  589. class InitializeVariableBinding final : public Instruction {
  590. public:
  591. explicit InitializeVariableBinding(IdentifierTableIndex identifier, Operand src)
  592. : Instruction(Type::InitializeVariableBinding)
  593. , m_identifier(identifier)
  594. , m_src(src)
  595. {
  596. }
  597. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  598. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  599. void visit_operands_impl(Function<void(Operand&)> visitor)
  600. {
  601. visitor(m_src);
  602. }
  603. IdentifierTableIndex identifier() const { return m_identifier; }
  604. Operand src() const { return m_src; }
  605. private:
  606. IdentifierTableIndex m_identifier;
  607. Operand m_src;
  608. mutable EnvironmentCoordinate m_cache;
  609. };
  610. class SetLexicalBinding final : public Instruction {
  611. public:
  612. explicit SetLexicalBinding(IdentifierTableIndex identifier, Operand src)
  613. : Instruction(Type::SetLexicalBinding)
  614. , m_identifier(identifier)
  615. , m_src(src)
  616. {
  617. }
  618. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  619. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  620. void visit_operands_impl(Function<void(Operand&)> visitor)
  621. {
  622. visitor(m_src);
  623. }
  624. IdentifierTableIndex identifier() const { return m_identifier; }
  625. Operand src() const { return m_src; }
  626. private:
  627. IdentifierTableIndex m_identifier;
  628. Operand m_src;
  629. mutable EnvironmentCoordinate m_cache;
  630. };
  631. class SetVariableBinding final : public Instruction {
  632. public:
  633. explicit SetVariableBinding(IdentifierTableIndex identifier, Operand src)
  634. : Instruction(Type::SetVariableBinding)
  635. , m_identifier(identifier)
  636. , m_src(src)
  637. {
  638. }
  639. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  640. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  641. void visit_operands_impl(Function<void(Operand&)> visitor)
  642. {
  643. visitor(m_src);
  644. }
  645. IdentifierTableIndex identifier() const { return m_identifier; }
  646. Operand src() const { return m_src; }
  647. private:
  648. IdentifierTableIndex m_identifier;
  649. Operand m_src;
  650. mutable EnvironmentCoordinate m_cache;
  651. };
  652. class SetArgument final : public Instruction {
  653. public:
  654. SetArgument(size_t index, Operand src)
  655. : Instruction(Type::SetArgument)
  656. , m_index(index)
  657. , m_src(src)
  658. {
  659. }
  660. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  661. void visit_operands_impl(Function<void(Operand&)> visitor)
  662. {
  663. visitor(m_src);
  664. }
  665. size_t index() const { return m_index; }
  666. Operand src() const { return m_src; }
  667. private:
  668. u32 m_index;
  669. Operand m_src;
  670. };
  671. class GetArgument final : public Instruction {
  672. public:
  673. GetArgument(Operand dst, size_t index)
  674. : Instruction(Type::GetArgument)
  675. , m_index(index)
  676. , m_dst(dst)
  677. {
  678. }
  679. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  680. void visit_operands_impl(Function<void(Operand&)> visitor)
  681. {
  682. visitor(m_dst);
  683. }
  684. u32 index() const { return m_index; }
  685. Operand dst() const { return m_dst; }
  686. private:
  687. u32 m_index;
  688. Operand m_dst;
  689. };
  690. class GetCalleeAndThisFromEnvironment final : public Instruction {
  691. public:
  692. explicit GetCalleeAndThisFromEnvironment(Operand callee, Operand this_value, IdentifierTableIndex identifier)
  693. : Instruction(Type::GetCalleeAndThisFromEnvironment)
  694. , m_identifier(identifier)
  695. , m_callee(callee)
  696. , m_this_value(this_value)
  697. {
  698. }
  699. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  700. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  701. void visit_operands_impl(Function<void(Operand&)> visitor)
  702. {
  703. visitor(m_callee);
  704. visitor(m_this_value);
  705. }
  706. IdentifierTableIndex identifier() const { return m_identifier; }
  707. Operand callee() const { return m_callee; }
  708. Operand this_() const { return m_this_value; }
  709. private:
  710. IdentifierTableIndex m_identifier;
  711. Operand m_callee;
  712. Operand m_this_value;
  713. mutable EnvironmentCoordinate m_cache;
  714. };
  715. class GetBinding final : public Instruction {
  716. public:
  717. explicit GetBinding(Operand dst, IdentifierTableIndex identifier)
  718. : Instruction(Type::GetBinding)
  719. , m_dst(dst)
  720. , m_identifier(identifier)
  721. {
  722. }
  723. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  724. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  725. Operand dst() const { return m_dst; }
  726. IdentifierTableIndex identifier() const { return m_identifier; }
  727. void visit_operands_impl(Function<void(Operand&)> visitor)
  728. {
  729. visitor(m_dst);
  730. }
  731. private:
  732. Operand m_dst;
  733. IdentifierTableIndex m_identifier;
  734. mutable EnvironmentCoordinate m_cache;
  735. };
  736. class GetGlobal final : public Instruction {
  737. public:
  738. GetGlobal(Operand dst, IdentifierTableIndex identifier, u32 cache_index)
  739. : Instruction(Type::GetGlobal)
  740. , m_dst(dst)
  741. , m_identifier(identifier)
  742. , m_cache_index(cache_index)
  743. {
  744. }
  745. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  746. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  747. Operand dst() const { return m_dst; }
  748. IdentifierTableIndex identifier() const { return m_identifier; }
  749. u32 cache_index() const { return m_cache_index; }
  750. void visit_operands_impl(Function<void(Operand&)> visitor)
  751. {
  752. visitor(m_dst);
  753. }
  754. private:
  755. Operand m_dst;
  756. IdentifierTableIndex m_identifier;
  757. u32 m_cache_index { 0 };
  758. };
  759. class DeleteVariable final : public Instruction {
  760. public:
  761. explicit DeleteVariable(Operand dst, IdentifierTableIndex identifier)
  762. : Instruction(Type::DeleteVariable)
  763. , m_dst(dst)
  764. , m_identifier(identifier)
  765. {
  766. }
  767. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  768. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  769. Operand dst() const { return m_dst; }
  770. IdentifierTableIndex identifier() const { return m_identifier; }
  771. void visit_operands_impl(Function<void(Operand&)> visitor)
  772. {
  773. visitor(m_dst);
  774. }
  775. private:
  776. Operand m_dst;
  777. IdentifierTableIndex m_identifier;
  778. };
  779. class GetById final : public Instruction {
  780. public:
  781. GetById(Operand dst, Operand base, IdentifierTableIndex property, Optional<IdentifierTableIndex> base_identifier, u32 cache_index)
  782. : Instruction(Type::GetById)
  783. , m_dst(dst)
  784. , m_base(base)
  785. , m_property(property)
  786. , m_base_identifier(move(base_identifier))
  787. , m_cache_index(cache_index)
  788. {
  789. }
  790. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  791. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  792. void visit_operands_impl(Function<void(Operand&)> visitor)
  793. {
  794. visitor(m_dst);
  795. visitor(m_base);
  796. }
  797. Operand dst() const { return m_dst; }
  798. Operand base() const { return m_base; }
  799. IdentifierTableIndex property() const { return m_property; }
  800. u32 cache_index() const { return m_cache_index; }
  801. private:
  802. Operand m_dst;
  803. Operand m_base;
  804. IdentifierTableIndex m_property;
  805. Optional<IdentifierTableIndex> m_base_identifier;
  806. u32 m_cache_index { 0 };
  807. };
  808. class GetByIdWithThis final : public Instruction {
  809. public:
  810. GetByIdWithThis(Operand dst, Operand base, IdentifierTableIndex property, Operand this_value, u32 cache_index)
  811. : Instruction(Type::GetByIdWithThis)
  812. , m_dst(dst)
  813. , m_base(base)
  814. , m_property(property)
  815. , m_this_value(this_value)
  816. , m_cache_index(cache_index)
  817. {
  818. }
  819. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  820. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  821. void visit_operands_impl(Function<void(Operand&)> visitor)
  822. {
  823. visitor(m_dst);
  824. visitor(m_base);
  825. visitor(m_this_value);
  826. }
  827. Operand dst() const { return m_dst; }
  828. Operand base() const { return m_base; }
  829. IdentifierTableIndex property() const { return m_property; }
  830. Operand this_value() const { return m_this_value; }
  831. u32 cache_index() const { return m_cache_index; }
  832. private:
  833. Operand m_dst;
  834. Operand m_base;
  835. IdentifierTableIndex m_property;
  836. Operand m_this_value;
  837. u32 m_cache_index { 0 };
  838. };
  839. class GetPrivateById final : public Instruction {
  840. public:
  841. explicit GetPrivateById(Operand dst, Operand base, IdentifierTableIndex property)
  842. : Instruction(Type::GetPrivateById)
  843. , m_dst(dst)
  844. , m_base(base)
  845. , m_property(property)
  846. {
  847. }
  848. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  849. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  850. void visit_operands_impl(Function<void(Operand&)> visitor)
  851. {
  852. visitor(m_dst);
  853. visitor(m_base);
  854. }
  855. Operand dst() const { return m_dst; }
  856. Operand base() const { return m_base; }
  857. IdentifierTableIndex property() const { return m_property; }
  858. private:
  859. Operand m_dst;
  860. Operand m_base;
  861. IdentifierTableIndex m_property;
  862. };
  863. class HasPrivateId final : public Instruction {
  864. public:
  865. HasPrivateId(Operand dst, Operand base, IdentifierTableIndex property)
  866. : Instruction(Type::HasPrivateId)
  867. , m_dst(dst)
  868. , m_base(base)
  869. , m_property(property)
  870. {
  871. }
  872. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  873. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  874. void visit_operands_impl(Function<void(Operand&)> visitor)
  875. {
  876. visitor(m_dst);
  877. visitor(m_base);
  878. }
  879. Operand dst() const { return m_dst; }
  880. Operand base() const { return m_base; }
  881. IdentifierTableIndex property() const { return m_property; }
  882. private:
  883. Operand m_dst;
  884. Operand m_base;
  885. IdentifierTableIndex m_property;
  886. };
  887. enum class PropertyKind {
  888. Getter,
  889. Setter,
  890. KeyValue,
  891. DirectKeyValue, // Used for Object expressions. Always sets an own property, never calls a setter.
  892. Spread,
  893. ProtoSetter,
  894. };
  895. class PutById final : public Instruction {
  896. public:
  897. explicit PutById(Operand base, IdentifierTableIndex property, Operand src, PropertyKind kind, u32 cache_index, Optional<IdentifierTableIndex> base_identifier = {})
  898. : Instruction(Type::PutById)
  899. , m_base(base)
  900. , m_property(property)
  901. , m_src(src)
  902. , m_kind(kind)
  903. , m_cache_index(cache_index)
  904. , m_base_identifier(move(base_identifier))
  905. {
  906. }
  907. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  908. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  909. void visit_operands_impl(Function<void(Operand&)> visitor)
  910. {
  911. visitor(m_base);
  912. visitor(m_src);
  913. }
  914. Operand base() const { return m_base; }
  915. IdentifierTableIndex property() const { return m_property; }
  916. Operand src() const { return m_src; }
  917. PropertyKind kind() const { return m_kind; }
  918. u32 cache_index() const { return m_cache_index; }
  919. private:
  920. Operand m_base;
  921. IdentifierTableIndex m_property;
  922. Operand m_src;
  923. PropertyKind m_kind;
  924. u32 m_cache_index { 0 };
  925. Optional<IdentifierTableIndex> m_base_identifier {};
  926. };
  927. class PutByIdWithThis final : public Instruction {
  928. public:
  929. PutByIdWithThis(Operand base, Operand this_value, IdentifierTableIndex property, Operand src, PropertyKind kind, u32 cache_index)
  930. : Instruction(Type::PutByIdWithThis)
  931. , m_base(base)
  932. , m_this_value(this_value)
  933. , m_property(property)
  934. , m_src(src)
  935. , m_kind(kind)
  936. , m_cache_index(cache_index)
  937. {
  938. }
  939. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  940. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  941. void visit_operands_impl(Function<void(Operand&)> visitor)
  942. {
  943. visitor(m_base);
  944. visitor(m_this_value);
  945. visitor(m_src);
  946. }
  947. Operand base() const { return m_base; }
  948. Operand this_value() const { return m_this_value; }
  949. IdentifierTableIndex property() const { return m_property; }
  950. Operand src() const { return m_src; }
  951. PropertyKind kind() const { return m_kind; }
  952. u32 cache_index() const { return m_cache_index; }
  953. private:
  954. Operand m_base;
  955. Operand m_this_value;
  956. IdentifierTableIndex m_property;
  957. Operand m_src;
  958. PropertyKind m_kind;
  959. u32 m_cache_index { 0 };
  960. };
  961. class PutPrivateById final : public Instruction {
  962. public:
  963. explicit PutPrivateById(Operand base, IdentifierTableIndex property, Operand src, PropertyKind kind = PropertyKind::KeyValue)
  964. : Instruction(Type::PutPrivateById)
  965. , m_base(base)
  966. , m_property(property)
  967. , m_src(src)
  968. , m_kind(kind)
  969. {
  970. }
  971. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  972. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  973. void visit_operands_impl(Function<void(Operand&)> visitor)
  974. {
  975. visitor(m_base);
  976. visitor(m_src);
  977. }
  978. Operand base() const { return m_base; }
  979. IdentifierTableIndex property() const { return m_property; }
  980. Operand src() const { return m_src; }
  981. private:
  982. Operand m_base;
  983. IdentifierTableIndex m_property;
  984. Operand m_src;
  985. PropertyKind m_kind;
  986. };
  987. class DeleteById final : public Instruction {
  988. public:
  989. explicit DeleteById(Operand dst, Operand base, IdentifierTableIndex property)
  990. : Instruction(Type::DeleteById)
  991. , m_dst(dst)
  992. , m_base(base)
  993. , m_property(property)
  994. {
  995. }
  996. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  997. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  998. void visit_operands_impl(Function<void(Operand&)> visitor)
  999. {
  1000. visitor(m_dst);
  1001. visitor(m_base);
  1002. }
  1003. Operand dst() const { return m_dst; }
  1004. Operand base() const { return m_base; }
  1005. IdentifierTableIndex property() const { return m_property; }
  1006. private:
  1007. Operand m_dst;
  1008. Operand m_base;
  1009. IdentifierTableIndex m_property;
  1010. };
  1011. class DeleteByIdWithThis final : public Instruction {
  1012. public:
  1013. DeleteByIdWithThis(Operand dst, Operand base, Operand this_value, IdentifierTableIndex property)
  1014. : Instruction(Type::DeleteByIdWithThis)
  1015. , m_dst(dst)
  1016. , m_base(base)
  1017. , m_this_value(this_value)
  1018. , m_property(property)
  1019. {
  1020. }
  1021. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1022. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  1023. void visit_operands_impl(Function<void(Operand&)> visitor)
  1024. {
  1025. visitor(m_dst);
  1026. visitor(m_base);
  1027. visitor(m_this_value);
  1028. }
  1029. Operand dst() const { return m_dst; }
  1030. Operand base() const { return m_base; }
  1031. Operand this_value() const { return m_this_value; }
  1032. IdentifierTableIndex property() const { return m_property; }
  1033. private:
  1034. Operand m_dst;
  1035. Operand m_base;
  1036. Operand m_this_value;
  1037. IdentifierTableIndex m_property;
  1038. };
  1039. class GetByValue final : public Instruction {
  1040. public:
  1041. GetByValue(Operand dst, Operand base, Operand property, Optional<IdentifierTableIndex> base_identifier = {})
  1042. : Instruction(Type::GetByValue)
  1043. , m_dst(dst)
  1044. , m_base(base)
  1045. , m_property(property)
  1046. , m_base_identifier(move(base_identifier))
  1047. {
  1048. }
  1049. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1050. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  1051. void visit_operands_impl(Function<void(Operand&)> visitor)
  1052. {
  1053. visitor(m_dst);
  1054. visitor(m_base);
  1055. visitor(m_property);
  1056. }
  1057. Operand dst() const { return m_dst; }
  1058. Operand base() const { return m_base; }
  1059. Operand property() const { return m_property; }
  1060. Optional<DeprecatedFlyString const&> base_identifier(Bytecode::Interpreter const&) const;
  1061. private:
  1062. Operand m_dst;
  1063. Operand m_base;
  1064. Operand m_property;
  1065. Optional<IdentifierTableIndex> m_base_identifier;
  1066. };
  1067. class GetByValueWithThis final : public Instruction {
  1068. public:
  1069. GetByValueWithThis(Operand dst, Operand base, Operand property, Operand this_value)
  1070. : Instruction(Type::GetByValueWithThis)
  1071. , m_dst(dst)
  1072. , m_base(base)
  1073. , m_property(property)
  1074. , m_this_value(this_value)
  1075. {
  1076. }
  1077. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1078. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  1079. void visit_operands_impl(Function<void(Operand&)> visitor)
  1080. {
  1081. visitor(m_dst);
  1082. visitor(m_base);
  1083. visitor(m_property);
  1084. visitor(m_this_value);
  1085. }
  1086. Operand dst() const { return m_dst; }
  1087. Operand base() const { return m_base; }
  1088. Operand property() const { return m_property; }
  1089. Operand this_value() const { return m_this_value; }
  1090. private:
  1091. Operand m_dst;
  1092. Operand m_base;
  1093. Operand m_property;
  1094. Operand m_this_value;
  1095. };
  1096. class PutByValue final : public Instruction {
  1097. public:
  1098. PutByValue(Operand base, Operand property, Operand src, PropertyKind kind = PropertyKind::KeyValue, Optional<IdentifierTableIndex> base_identifier = {})
  1099. : Instruction(Type::PutByValue)
  1100. , m_base(base)
  1101. , m_property(property)
  1102. , m_src(src)
  1103. , m_kind(kind)
  1104. , m_base_identifier(move(base_identifier))
  1105. {
  1106. }
  1107. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1108. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  1109. void visit_operands_impl(Function<void(Operand&)> visitor)
  1110. {
  1111. visitor(m_base);
  1112. visitor(m_property);
  1113. visitor(m_src);
  1114. }
  1115. Operand base() const { return m_base; }
  1116. Operand property() const { return m_property; }
  1117. Operand src() const { return m_src; }
  1118. PropertyKind kind() const { return m_kind; }
  1119. private:
  1120. Operand m_base;
  1121. Operand m_property;
  1122. Operand m_src;
  1123. PropertyKind m_kind;
  1124. Optional<IdentifierTableIndex> m_base_identifier;
  1125. };
  1126. class PutByValueWithThis final : public Instruction {
  1127. public:
  1128. PutByValueWithThis(Operand base, Operand property, Operand this_value, Operand src, PropertyKind kind = PropertyKind::KeyValue)
  1129. : Instruction(Type::PutByValueWithThis)
  1130. , m_base(base)
  1131. , m_property(property)
  1132. , m_this_value(this_value)
  1133. , m_src(src)
  1134. , m_kind(kind)
  1135. {
  1136. }
  1137. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1138. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  1139. void visit_operands_impl(Function<void(Operand&)> visitor)
  1140. {
  1141. visitor(m_base);
  1142. visitor(m_property);
  1143. visitor(m_this_value);
  1144. visitor(m_src);
  1145. }
  1146. Operand base() const { return m_base; }
  1147. Operand property() const { return m_property; }
  1148. Operand this_value() const { return m_this_value; }
  1149. Operand src() const { return m_src; }
  1150. PropertyKind kind() const { return m_kind; }
  1151. private:
  1152. Operand m_base;
  1153. Operand m_property;
  1154. Operand m_this_value;
  1155. Operand m_src;
  1156. PropertyKind m_kind;
  1157. };
  1158. class DeleteByValue final : public Instruction {
  1159. public:
  1160. DeleteByValue(Operand dst, Operand base, Operand property)
  1161. : Instruction(Type::DeleteByValue)
  1162. , m_dst(dst)
  1163. , m_base(base)
  1164. , m_property(property)
  1165. {
  1166. }
  1167. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1168. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  1169. void visit_operands_impl(Function<void(Operand&)> visitor)
  1170. {
  1171. visitor(m_dst);
  1172. visitor(m_base);
  1173. visitor(m_property);
  1174. }
  1175. Operand dst() const { return m_dst; }
  1176. Operand base() const { return m_base; }
  1177. Operand property() const { return m_property; }
  1178. private:
  1179. Operand m_dst;
  1180. Operand m_base;
  1181. Operand m_property;
  1182. };
  1183. class DeleteByValueWithThis final : public Instruction {
  1184. public:
  1185. DeleteByValueWithThis(Operand dst, Operand base, Operand this_value, Operand property)
  1186. : Instruction(Type::DeleteByValueWithThis)
  1187. , m_dst(dst)
  1188. , m_base(base)
  1189. , m_this_value(this_value)
  1190. , m_property(property)
  1191. {
  1192. }
  1193. Operand dst() const { return m_dst; }
  1194. Operand base() const { return m_base; }
  1195. Operand this_value() const { return m_this_value; }
  1196. Operand property() const { return m_property; }
  1197. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1198. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  1199. void visit_operands_impl(Function<void(Operand&)> visitor)
  1200. {
  1201. visitor(m_dst);
  1202. visitor(m_base);
  1203. visitor(m_this_value);
  1204. visitor(m_property);
  1205. }
  1206. private:
  1207. Operand m_dst;
  1208. Operand m_base;
  1209. Operand m_this_value;
  1210. Operand m_property;
  1211. };
  1212. class Jump final : public Instruction {
  1213. public:
  1214. constexpr static bool IsTerminator = true;
  1215. explicit Jump(Label target)
  1216. : Instruction(Type::Jump)
  1217. , m_target(target)
  1218. {
  1219. }
  1220. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  1221. void visit_labels_impl(Function<void(Label&)> visitor)
  1222. {
  1223. visitor(m_target);
  1224. }
  1225. auto& target() const { return m_target; }
  1226. protected:
  1227. Label m_target;
  1228. };
  1229. class JumpIf final : public Instruction {
  1230. public:
  1231. constexpr static bool IsTerminator = true;
  1232. explicit JumpIf(Operand condition, Label true_target, Label false_target)
  1233. : Instruction(Type::JumpIf)
  1234. , m_condition(condition)
  1235. , m_true_target(true_target)
  1236. , m_false_target(false_target)
  1237. {
  1238. }
  1239. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  1240. void visit_labels_impl(Function<void(Label&)> visitor)
  1241. {
  1242. visitor(m_true_target);
  1243. visitor(m_false_target);
  1244. }
  1245. void visit_operands_impl(Function<void(Operand&)> visitor)
  1246. {
  1247. visitor(m_condition);
  1248. }
  1249. Operand condition() const { return m_condition; }
  1250. auto& true_target() const { return m_true_target; }
  1251. auto& false_target() const { return m_false_target; }
  1252. private:
  1253. Operand m_condition;
  1254. Label m_true_target;
  1255. Label m_false_target;
  1256. };
  1257. class JumpTrue final : public Instruction {
  1258. public:
  1259. constexpr static bool IsTerminator = true;
  1260. explicit JumpTrue(Operand condition, Label target)
  1261. : Instruction(Type::JumpTrue)
  1262. , m_condition(condition)
  1263. , m_target(target)
  1264. {
  1265. }
  1266. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  1267. void visit_labels_impl(Function<void(Label&)> visitor)
  1268. {
  1269. visitor(m_target);
  1270. }
  1271. void visit_operands_impl(Function<void(Operand&)> visitor)
  1272. {
  1273. visitor(m_condition);
  1274. }
  1275. Operand condition() const { return m_condition; }
  1276. auto& target() const { return m_target; }
  1277. private:
  1278. Operand m_condition;
  1279. Label m_target;
  1280. };
  1281. class JumpFalse final : public Instruction {
  1282. public:
  1283. constexpr static bool IsTerminator = true;
  1284. explicit JumpFalse(Operand condition, Label target)
  1285. : Instruction(Type::JumpFalse)
  1286. , m_condition(condition)
  1287. , m_target(target)
  1288. {
  1289. }
  1290. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  1291. void visit_labels_impl(Function<void(Label&)> visitor)
  1292. {
  1293. visitor(m_target);
  1294. }
  1295. void visit_operands_impl(Function<void(Operand&)> visitor)
  1296. {
  1297. visitor(m_condition);
  1298. }
  1299. Operand condition() const { return m_condition; }
  1300. auto& target() const { return m_target; }
  1301. private:
  1302. Operand m_condition;
  1303. Label m_target;
  1304. };
  1305. #define JS_ENUMERATE_COMPARISON_OPS(X) \
  1306. X(LessThan, less_than, <) \
  1307. X(LessThanEquals, less_than_equals, <=) \
  1308. X(GreaterThan, greater_than, >) \
  1309. X(GreaterThanEquals, greater_than_equals, >=) \
  1310. X(LooselyEquals, loosely_equals, ==) \
  1311. X(LooselyInequals, loosely_inequals, !=) \
  1312. X(StrictlyEquals, strict_equals, ==) \
  1313. X(StrictlyInequals, strict_inequals, !=)
  1314. #define DECLARE_COMPARISON_OP(op_TitleCase, op_snake_case, numeric_operator) \
  1315. class Jump##op_TitleCase final : public Instruction { \
  1316. public: \
  1317. constexpr static bool IsTerminator = true; \
  1318. \
  1319. explicit Jump##op_TitleCase(Operand lhs, Operand rhs, Label true_target, Label false_target) \
  1320. : Instruction(Type::Jump##op_TitleCase) \
  1321. , m_lhs(lhs) \
  1322. , m_rhs(rhs) \
  1323. , m_true_target(true_target) \
  1324. , m_false_target(false_target) \
  1325. { \
  1326. } \
  1327. \
  1328. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const; \
  1329. ByteString to_byte_string_impl(Bytecode::Executable const&) const; \
  1330. void visit_labels_impl(Function<void(Label&)> visitor) \
  1331. { \
  1332. visitor(m_true_target); \
  1333. visitor(m_false_target); \
  1334. } \
  1335. void visit_operands_impl(Function<void(Operand&)> visitor) \
  1336. { \
  1337. visitor(m_lhs); \
  1338. visitor(m_rhs); \
  1339. } \
  1340. \
  1341. Operand lhs() const { return m_lhs; } \
  1342. Operand rhs() const { return m_rhs; } \
  1343. auto& true_target() const { return m_true_target; } \
  1344. auto& false_target() const { return m_false_target; } \
  1345. \
  1346. private: \
  1347. Operand m_lhs; \
  1348. Operand m_rhs; \
  1349. Label m_true_target; \
  1350. Label m_false_target; \
  1351. };
  1352. JS_ENUMERATE_COMPARISON_OPS(DECLARE_COMPARISON_OP)
  1353. class JumpNullish final : public Instruction {
  1354. public:
  1355. constexpr static bool IsTerminator = true;
  1356. explicit JumpNullish(Operand condition, Label true_target, Label false_target)
  1357. : Instruction(Type::JumpNullish)
  1358. , m_condition(condition)
  1359. , m_true_target(true_target)
  1360. , m_false_target(false_target)
  1361. {
  1362. }
  1363. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  1364. void visit_labels_impl(Function<void(Label&)> visitor)
  1365. {
  1366. visitor(m_true_target);
  1367. visitor(m_false_target);
  1368. }
  1369. void visit_operands_impl(Function<void(Operand&)> visitor)
  1370. {
  1371. visitor(m_condition);
  1372. }
  1373. Operand condition() const { return m_condition; }
  1374. auto& true_target() const { return m_true_target; }
  1375. auto& false_target() const { return m_false_target; }
  1376. private:
  1377. Operand m_condition;
  1378. Label m_true_target;
  1379. Label m_false_target;
  1380. };
  1381. class JumpUndefined final : public Instruction {
  1382. public:
  1383. constexpr static bool IsTerminator = true;
  1384. explicit JumpUndefined(Operand condition, Label true_target, Label false_target)
  1385. : Instruction(Type::JumpUndefined)
  1386. , m_condition(condition)
  1387. , m_true_target(true_target)
  1388. , m_false_target(false_target)
  1389. {
  1390. }
  1391. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  1392. void visit_labels_impl(Function<void(Label&)> visitor)
  1393. {
  1394. visitor(m_true_target);
  1395. visitor(m_false_target);
  1396. }
  1397. void visit_operands_impl(Function<void(Operand&)> visitor)
  1398. {
  1399. visitor(m_condition);
  1400. }
  1401. Operand condition() const { return m_condition; }
  1402. auto& true_target() const { return m_true_target; }
  1403. auto& false_target() const { return m_false_target; }
  1404. private:
  1405. Operand m_condition;
  1406. Label m_true_target;
  1407. Label m_false_target;
  1408. };
  1409. enum class CallType : u8 {
  1410. Call,
  1411. Construct,
  1412. DirectEval,
  1413. };
  1414. class Call final : public Instruction {
  1415. public:
  1416. static constexpr bool IsVariableLength = true;
  1417. Call(CallType type, Operand dst, Operand callee, Operand this_value, ReadonlySpan<ScopedOperand> arguments, Optional<StringTableIndex> expression_string = {}, Optional<Builtin> builtin = {})
  1418. : Instruction(Type::Call)
  1419. , m_dst(dst)
  1420. , m_callee(callee)
  1421. , m_this_value(this_value)
  1422. , m_argument_count(arguments.size())
  1423. , m_type(type)
  1424. , m_builtin(builtin)
  1425. , m_expression_string(expression_string)
  1426. {
  1427. for (size_t i = 0; i < arguments.size(); ++i)
  1428. m_arguments[i] = arguments[i];
  1429. }
  1430. size_t length_impl() const
  1431. {
  1432. return round_up_to_power_of_two(alignof(void*), sizeof(*this) + sizeof(Operand) * m_argument_count);
  1433. }
  1434. CallType call_type() const { return m_type; }
  1435. Operand dst() const { return m_dst; }
  1436. Operand callee() const { return m_callee; }
  1437. Operand this_value() const { return m_this_value; }
  1438. Optional<StringTableIndex> const& expression_string() const { return m_expression_string; }
  1439. u32 argument_count() const { return m_argument_count; }
  1440. Optional<Builtin> const& builtin() const { return m_builtin; }
  1441. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1442. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  1443. void visit_operands_impl(Function<void(Operand&)> visitor)
  1444. {
  1445. visitor(m_dst);
  1446. visitor(m_callee);
  1447. visitor(m_this_value);
  1448. for (size_t i = 0; i < m_argument_count; i++)
  1449. visitor(m_arguments[i]);
  1450. }
  1451. private:
  1452. Operand m_dst;
  1453. Operand m_callee;
  1454. Operand m_this_value;
  1455. u32 m_argument_count { 0 };
  1456. CallType m_type;
  1457. Optional<Builtin> m_builtin;
  1458. Optional<StringTableIndex> m_expression_string;
  1459. Operand m_arguments[];
  1460. };
  1461. class CallWithArgumentArray final : public Instruction {
  1462. public:
  1463. CallWithArgumentArray(CallType type, Operand dst, Operand callee, Operand this_value, Operand arguments, Optional<StringTableIndex> expression_string = {})
  1464. : Instruction(Type::CallWithArgumentArray)
  1465. , m_dst(dst)
  1466. , m_callee(callee)
  1467. , m_this_value(this_value)
  1468. , m_arguments(arguments)
  1469. , m_type(type)
  1470. , m_expression_string(expression_string)
  1471. {
  1472. }
  1473. Operand dst() const { return m_dst; }
  1474. CallType call_type() const { return m_type; }
  1475. Operand callee() const { return m_callee; }
  1476. Operand this_value() const { return m_this_value; }
  1477. Operand arguments() const { return m_arguments; }
  1478. Optional<StringTableIndex> const& expression_string() const { return m_expression_string; }
  1479. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1480. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  1481. void visit_operands_impl(Function<void(Operand&)> visitor)
  1482. {
  1483. visitor(m_dst);
  1484. visitor(m_callee);
  1485. visitor(m_this_value);
  1486. visitor(m_arguments);
  1487. }
  1488. private:
  1489. Operand m_dst;
  1490. Operand m_callee;
  1491. Operand m_this_value;
  1492. Operand m_arguments;
  1493. CallType m_type;
  1494. Optional<StringTableIndex> m_expression_string;
  1495. };
  1496. class SuperCallWithArgumentArray : public Instruction {
  1497. public:
  1498. explicit SuperCallWithArgumentArray(Operand dst, Operand arguments, bool is_synthetic)
  1499. : Instruction(Type::SuperCallWithArgumentArray)
  1500. , m_dst(dst)
  1501. , m_arguments(arguments)
  1502. , m_is_synthetic(is_synthetic)
  1503. {
  1504. }
  1505. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1506. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  1507. void visit_operands_impl(Function<void(Operand&)> visitor)
  1508. {
  1509. visitor(m_dst);
  1510. visitor(m_arguments);
  1511. }
  1512. Operand dst() const { return m_dst; }
  1513. Operand arguments() const { return m_arguments; }
  1514. bool is_synthetic() const { return m_is_synthetic; }
  1515. private:
  1516. Operand m_dst;
  1517. Operand m_arguments;
  1518. bool m_is_synthetic;
  1519. };
  1520. class NewClass final : public Instruction {
  1521. public:
  1522. static constexpr bool IsVariableLength = true;
  1523. explicit NewClass(Operand dst, Optional<Operand> super_class, ClassExpression const& class_expression, Optional<IdentifierTableIndex> lhs_name, ReadonlySpan<Optional<ScopedOperand>> elements_keys)
  1524. : Instruction(Type::NewClass)
  1525. , m_dst(dst)
  1526. , m_super_class(super_class)
  1527. , m_class_expression(class_expression)
  1528. , m_lhs_name(lhs_name)
  1529. , m_element_keys_count(elements_keys.size())
  1530. {
  1531. for (size_t i = 0; i < m_element_keys_count; i++) {
  1532. if (elements_keys[i].has_value())
  1533. m_element_keys[i] = elements_keys[i]->operand();
  1534. }
  1535. }
  1536. size_t length_impl() const
  1537. {
  1538. return round_up_to_power_of_two(alignof(void*), sizeof(*this) + sizeof(Optional<Operand>) * m_element_keys_count);
  1539. }
  1540. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1541. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  1542. void visit_operands_impl(Function<void(Operand&)> visitor)
  1543. {
  1544. visitor(m_dst);
  1545. if (m_super_class.has_value())
  1546. visitor(m_super_class.value());
  1547. for (size_t i = 0; i < m_element_keys_count; i++) {
  1548. if (m_element_keys[i].has_value())
  1549. visitor(m_element_keys[i].value());
  1550. }
  1551. }
  1552. Operand dst() const { return m_dst; }
  1553. Optional<Operand> const& super_class() const { return m_super_class; }
  1554. ClassExpression const& class_expression() const { return m_class_expression; }
  1555. Optional<IdentifierTableIndex> const& lhs_name() const { return m_lhs_name; }
  1556. private:
  1557. Operand m_dst;
  1558. Optional<Operand> m_super_class;
  1559. ClassExpression const& m_class_expression;
  1560. Optional<IdentifierTableIndex> m_lhs_name;
  1561. size_t m_element_keys_count { 0 };
  1562. Optional<Operand> m_element_keys[];
  1563. };
  1564. class NewFunction final : public Instruction {
  1565. public:
  1566. explicit NewFunction(Operand dst, FunctionNode const& function_node, Optional<IdentifierTableIndex> lhs_name, Optional<Operand> home_object = {})
  1567. : Instruction(Type::NewFunction)
  1568. , m_dst(dst)
  1569. , m_function_node(function_node)
  1570. , m_lhs_name(lhs_name)
  1571. , m_home_object(move(home_object))
  1572. {
  1573. }
  1574. void execute_impl(Bytecode::Interpreter&) const;
  1575. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  1576. void visit_operands_impl(Function<void(Operand&)> visitor)
  1577. {
  1578. visitor(m_dst);
  1579. if (m_home_object.has_value())
  1580. visitor(m_home_object.value());
  1581. }
  1582. Operand dst() const { return m_dst; }
  1583. FunctionNode const& function_node() const { return m_function_node; }
  1584. Optional<IdentifierTableIndex> const& lhs_name() const { return m_lhs_name; }
  1585. Optional<Operand> const& home_object() const { return m_home_object; }
  1586. private:
  1587. Operand m_dst;
  1588. FunctionNode const& m_function_node;
  1589. Optional<IdentifierTableIndex> m_lhs_name;
  1590. Optional<Operand> m_home_object;
  1591. };
  1592. class BlockDeclarationInstantiation final : public Instruction {
  1593. public:
  1594. explicit BlockDeclarationInstantiation(ScopeNode const& scope_node)
  1595. : Instruction(Type::BlockDeclarationInstantiation)
  1596. , m_scope_node(scope_node)
  1597. {
  1598. }
  1599. void execute_impl(Bytecode::Interpreter&) const;
  1600. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  1601. ScopeNode const& scope_node() const { return m_scope_node; }
  1602. private:
  1603. ScopeNode const& m_scope_node;
  1604. };
  1605. class Return final : public Instruction {
  1606. public:
  1607. constexpr static bool IsTerminator = true;
  1608. explicit Return(Optional<Operand> value = {})
  1609. : Instruction(Type::Return)
  1610. , m_value(value)
  1611. {
  1612. }
  1613. void execute_impl(Bytecode::Interpreter&) const;
  1614. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  1615. void visit_operands_impl(Function<void(Operand&)> visitor)
  1616. {
  1617. if (m_value.has_value())
  1618. visitor(m_value.value());
  1619. }
  1620. Optional<Operand> const& value() const { return m_value; }
  1621. private:
  1622. Optional<Operand> m_value;
  1623. };
  1624. class Increment final : public Instruction {
  1625. public:
  1626. explicit Increment(Operand dst)
  1627. : Instruction(Type::Increment)
  1628. , m_dst(dst)
  1629. {
  1630. }
  1631. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1632. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  1633. void visit_operands_impl(Function<void(Operand&)> visitor)
  1634. {
  1635. visitor(m_dst);
  1636. }
  1637. Operand dst() const { return m_dst; }
  1638. private:
  1639. Operand m_dst;
  1640. };
  1641. class PostfixIncrement final : public Instruction {
  1642. public:
  1643. explicit PostfixIncrement(Operand dst, Operand src)
  1644. : Instruction(Type::PostfixIncrement)
  1645. , m_dst(dst)
  1646. , m_src(src)
  1647. {
  1648. }
  1649. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1650. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  1651. void visit_operands_impl(Function<void(Operand&)> visitor)
  1652. {
  1653. visitor(m_dst);
  1654. visitor(m_src);
  1655. }
  1656. Operand dst() const { return m_dst; }
  1657. Operand src() const { return m_src; }
  1658. private:
  1659. Operand m_dst;
  1660. Operand m_src;
  1661. };
  1662. class Decrement final : public Instruction {
  1663. public:
  1664. explicit Decrement(Operand dst)
  1665. : Instruction(Type::Decrement)
  1666. , m_dst(dst)
  1667. {
  1668. }
  1669. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1670. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  1671. void visit_operands_impl(Function<void(Operand&)> visitor)
  1672. {
  1673. visitor(m_dst);
  1674. }
  1675. Operand dst() const { return m_dst; }
  1676. private:
  1677. Operand m_dst;
  1678. };
  1679. class PostfixDecrement final : public Instruction {
  1680. public:
  1681. explicit PostfixDecrement(Operand dst, Operand src)
  1682. : Instruction(Type::PostfixDecrement)
  1683. , m_dst(dst)
  1684. , m_src(src)
  1685. {
  1686. }
  1687. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1688. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  1689. void visit_operands_impl(Function<void(Operand&)> visitor)
  1690. {
  1691. visitor(m_dst);
  1692. visitor(m_src);
  1693. }
  1694. Operand dst() const { return m_dst; }
  1695. Operand src() const { return m_src; }
  1696. private:
  1697. Operand m_dst;
  1698. Operand m_src;
  1699. };
  1700. class Throw final : public Instruction {
  1701. public:
  1702. constexpr static bool IsTerminator = true;
  1703. explicit Throw(Operand src)
  1704. : Instruction(Type::Throw)
  1705. , m_src(src)
  1706. {
  1707. }
  1708. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1709. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  1710. void visit_operands_impl(Function<void(Operand&)> visitor)
  1711. {
  1712. visitor(m_src);
  1713. }
  1714. Operand src() const { return m_src; }
  1715. private:
  1716. Operand m_src;
  1717. };
  1718. class ThrowIfNotObject final : public Instruction {
  1719. public:
  1720. ThrowIfNotObject(Operand src)
  1721. : Instruction(Type::ThrowIfNotObject)
  1722. , m_src(src)
  1723. {
  1724. }
  1725. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1726. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  1727. void visit_operands_impl(Function<void(Operand&)> visitor)
  1728. {
  1729. visitor(m_src);
  1730. }
  1731. Operand src() const { return m_src; }
  1732. private:
  1733. Operand m_src;
  1734. };
  1735. class ThrowIfNullish final : public Instruction {
  1736. public:
  1737. explicit ThrowIfNullish(Operand src)
  1738. : Instruction(Type::ThrowIfNullish)
  1739. , m_src(src)
  1740. {
  1741. }
  1742. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1743. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  1744. void visit_operands_impl(Function<void(Operand&)> visitor)
  1745. {
  1746. visitor(m_src);
  1747. }
  1748. Operand src() const { return m_src; }
  1749. private:
  1750. Operand m_src;
  1751. };
  1752. class ThrowIfTDZ final : public Instruction {
  1753. public:
  1754. explicit ThrowIfTDZ(Operand src)
  1755. : Instruction(Type::ThrowIfTDZ)
  1756. , m_src(src)
  1757. {
  1758. }
  1759. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1760. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  1761. void visit_operands_impl(Function<void(Operand&)> visitor)
  1762. {
  1763. visitor(m_src);
  1764. }
  1765. Operand src() const { return m_src; }
  1766. private:
  1767. Operand m_src;
  1768. };
  1769. class EnterUnwindContext final : public Instruction {
  1770. public:
  1771. constexpr static bool IsTerminator = true;
  1772. EnterUnwindContext(Label entry_point)
  1773. : Instruction(Type::EnterUnwindContext)
  1774. , m_entry_point(move(entry_point))
  1775. {
  1776. }
  1777. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  1778. void visit_labels_impl(Function<void(Label&)> visitor)
  1779. {
  1780. visitor(m_entry_point);
  1781. }
  1782. auto& entry_point() const { return m_entry_point; }
  1783. private:
  1784. Label m_entry_point;
  1785. };
  1786. class ScheduleJump final : public Instruction {
  1787. public:
  1788. // Note: We use this instruction to tell the next `finally` block to
  1789. // continue execution with a specific break/continue target;
  1790. constexpr static bool IsTerminator = true;
  1791. ScheduleJump(Label target)
  1792. : Instruction(Type::ScheduleJump)
  1793. , m_target(target)
  1794. {
  1795. }
  1796. Label target() const { return m_target; }
  1797. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  1798. void visit_labels_impl(Function<void(Label&)> visitor)
  1799. {
  1800. visitor(m_target);
  1801. }
  1802. private:
  1803. Label m_target;
  1804. };
  1805. class LeaveLexicalEnvironment final : public Instruction {
  1806. public:
  1807. LeaveLexicalEnvironment()
  1808. : Instruction(Type::LeaveLexicalEnvironment)
  1809. {
  1810. }
  1811. void execute_impl(Bytecode::Interpreter&) const;
  1812. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  1813. };
  1814. class LeavePrivateEnvironment final : public Instruction {
  1815. public:
  1816. LeavePrivateEnvironment()
  1817. : Instruction(Type::LeavePrivateEnvironment)
  1818. {
  1819. }
  1820. void execute_impl(Bytecode::Interpreter&) const;
  1821. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  1822. };
  1823. class LeaveUnwindContext final : public Instruction {
  1824. public:
  1825. LeaveUnwindContext()
  1826. : Instruction(Type::LeaveUnwindContext)
  1827. {
  1828. }
  1829. void execute_impl(Bytecode::Interpreter&) const;
  1830. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  1831. };
  1832. class ContinuePendingUnwind final : public Instruction {
  1833. public:
  1834. constexpr static bool IsTerminator = true;
  1835. explicit ContinuePendingUnwind(Label resume_target)
  1836. : Instruction(Type::ContinuePendingUnwind)
  1837. , m_resume_target(resume_target)
  1838. {
  1839. }
  1840. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  1841. void visit_labels_impl(Function<void(Label&)> visitor)
  1842. {
  1843. visitor(m_resume_target);
  1844. }
  1845. auto& resume_target() const { return m_resume_target; }
  1846. private:
  1847. Label m_resume_target;
  1848. };
  1849. class Yield final : public Instruction {
  1850. public:
  1851. constexpr static bool IsTerminator = true;
  1852. explicit Yield(Label continuation_label, Operand value)
  1853. : Instruction(Type::Yield)
  1854. , m_continuation_label(continuation_label)
  1855. , m_value(value)
  1856. {
  1857. }
  1858. explicit Yield(nullptr_t, Operand value)
  1859. : Instruction(Type::Yield)
  1860. , m_value(value)
  1861. {
  1862. }
  1863. void execute_impl(Bytecode::Interpreter&) const;
  1864. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  1865. void visit_labels_impl(Function<void(Label&)> visitor)
  1866. {
  1867. if (m_continuation_label.has_value())
  1868. visitor(m_continuation_label.value());
  1869. }
  1870. void visit_operands_impl(Function<void(Operand&)> visitor)
  1871. {
  1872. visitor(m_value);
  1873. }
  1874. auto& continuation() const { return m_continuation_label; }
  1875. Operand value() const { return m_value; }
  1876. private:
  1877. Optional<Label> m_continuation_label;
  1878. Operand m_value;
  1879. };
  1880. class PrepareYield final : public Instruction {
  1881. public:
  1882. explicit PrepareYield(Operand dest, Operand value)
  1883. : Instruction(Type::PrepareYield)
  1884. , m_dest(dest)
  1885. , m_value(value)
  1886. {
  1887. }
  1888. void execute_impl(Bytecode::Interpreter&) const;
  1889. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  1890. void visit_operands_impl(Function<void(Operand&)> visitor)
  1891. {
  1892. visitor(m_dest);
  1893. visitor(m_value);
  1894. }
  1895. Operand destination() const { return m_dest; }
  1896. Operand value() const { return m_value; }
  1897. private:
  1898. Operand m_dest;
  1899. Operand m_value;
  1900. };
  1901. class Await final : public Instruction {
  1902. public:
  1903. constexpr static bool IsTerminator = true;
  1904. explicit Await(Label continuation_label, Operand argument)
  1905. : Instruction(Type::Await)
  1906. , m_continuation_label(continuation_label)
  1907. , m_argument(argument)
  1908. {
  1909. }
  1910. void execute_impl(Bytecode::Interpreter&) const;
  1911. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  1912. void visit_labels_impl(Function<void(Label&)> visitor)
  1913. {
  1914. visitor(m_continuation_label);
  1915. }
  1916. void visit_operands_impl(Function<void(Operand&)> visitor)
  1917. {
  1918. visitor(m_argument);
  1919. }
  1920. auto& continuation() const { return m_continuation_label; }
  1921. Operand argument() const { return m_argument; }
  1922. private:
  1923. Label m_continuation_label;
  1924. Operand m_argument;
  1925. };
  1926. class GetIterator final : public Instruction {
  1927. public:
  1928. GetIterator(Operand dst, Operand iterable, IteratorHint hint = IteratorHint::Sync)
  1929. : Instruction(Type::GetIterator)
  1930. , m_dst(dst)
  1931. , m_iterable(iterable)
  1932. , m_hint(hint)
  1933. {
  1934. }
  1935. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1936. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  1937. void visit_operands_impl(Function<void(Operand&)> visitor)
  1938. {
  1939. visitor(m_dst);
  1940. visitor(m_iterable);
  1941. }
  1942. Operand dst() const { return m_dst; }
  1943. Operand iterable() const { return m_iterable; }
  1944. IteratorHint hint() const { return m_hint; }
  1945. private:
  1946. Operand m_dst;
  1947. Operand m_iterable;
  1948. IteratorHint m_hint { IteratorHint::Sync };
  1949. };
  1950. class GetObjectFromIteratorRecord final : public Instruction {
  1951. public:
  1952. GetObjectFromIteratorRecord(Operand object, Operand iterator_record)
  1953. : Instruction(Type::GetObjectFromIteratorRecord)
  1954. , m_object(object)
  1955. , m_iterator_record(iterator_record)
  1956. {
  1957. }
  1958. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1959. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  1960. void visit_operands_impl(Function<void(Operand&)> visitor)
  1961. {
  1962. visitor(m_object);
  1963. visitor(m_iterator_record);
  1964. }
  1965. Operand object() const { return m_object; }
  1966. Operand iterator_record() const { return m_iterator_record; }
  1967. private:
  1968. Operand m_object;
  1969. Operand m_iterator_record;
  1970. };
  1971. class GetNextMethodFromIteratorRecord final : public Instruction {
  1972. public:
  1973. GetNextMethodFromIteratorRecord(Operand next_method, Operand iterator_record)
  1974. : Instruction(Type::GetNextMethodFromIteratorRecord)
  1975. , m_next_method(next_method)
  1976. , m_iterator_record(iterator_record)
  1977. {
  1978. }
  1979. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1980. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  1981. void visit_operands_impl(Function<void(Operand&)> visitor)
  1982. {
  1983. visitor(m_next_method);
  1984. visitor(m_iterator_record);
  1985. }
  1986. Operand next_method() const { return m_next_method; }
  1987. Operand iterator_record() const { return m_iterator_record; }
  1988. private:
  1989. Operand m_next_method;
  1990. Operand m_iterator_record;
  1991. };
  1992. class GetMethod final : public Instruction {
  1993. public:
  1994. GetMethod(Operand dst, Operand object, IdentifierTableIndex property)
  1995. : Instruction(Type::GetMethod)
  1996. , m_dst(dst)
  1997. , m_object(object)
  1998. , m_property(property)
  1999. {
  2000. }
  2001. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  2002. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  2003. void visit_operands_impl(Function<void(Operand&)> visitor)
  2004. {
  2005. visitor(m_dst);
  2006. visitor(m_object);
  2007. }
  2008. Operand dst() const { return m_dst; }
  2009. Operand object() const { return m_object; }
  2010. IdentifierTableIndex property() const { return m_property; }
  2011. private:
  2012. Operand m_dst;
  2013. Operand m_object;
  2014. IdentifierTableIndex m_property;
  2015. };
  2016. class GetObjectPropertyIterator final : public Instruction {
  2017. public:
  2018. GetObjectPropertyIterator(Operand dst, Operand object)
  2019. : Instruction(Type::GetObjectPropertyIterator)
  2020. , m_dst(dst)
  2021. , m_object(object)
  2022. {
  2023. }
  2024. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  2025. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  2026. void visit_operands_impl(Function<void(Operand&)> visitor)
  2027. {
  2028. visitor(m_dst);
  2029. visitor(m_object);
  2030. }
  2031. Operand dst() const { return m_dst; }
  2032. Operand object() const { return m_object; }
  2033. private:
  2034. Operand m_dst;
  2035. Operand m_object;
  2036. };
  2037. class IteratorClose final : public Instruction {
  2038. public:
  2039. IteratorClose(Operand iterator_record, Completion::Type completion_type, Optional<Value> completion_value)
  2040. : Instruction(Type::IteratorClose)
  2041. , m_iterator_record(iterator_record)
  2042. , m_completion_type(completion_type)
  2043. , m_completion_value(completion_value)
  2044. {
  2045. }
  2046. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  2047. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  2048. void visit_operands_impl(Function<void(Operand&)> visitor)
  2049. {
  2050. visitor(m_iterator_record);
  2051. }
  2052. Operand iterator_record() const { return m_iterator_record; }
  2053. Completion::Type completion_type() const { return m_completion_type; }
  2054. Optional<Value> const& completion_value() const { return m_completion_value; }
  2055. private:
  2056. Operand m_iterator_record;
  2057. Completion::Type m_completion_type { Completion::Type::Normal };
  2058. Optional<Value> m_completion_value;
  2059. };
  2060. class AsyncIteratorClose final : public Instruction {
  2061. public:
  2062. AsyncIteratorClose(Operand iterator_record, Completion::Type completion_type, Optional<Value> completion_value)
  2063. : Instruction(Type::AsyncIteratorClose)
  2064. , m_iterator_record(iterator_record)
  2065. , m_completion_type(completion_type)
  2066. , m_completion_value(completion_value)
  2067. {
  2068. }
  2069. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  2070. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  2071. void visit_operands_impl(Function<void(Operand&)> visitor)
  2072. {
  2073. visitor(m_iterator_record);
  2074. }
  2075. Operand iterator_record() const { return m_iterator_record; }
  2076. Completion::Type completion_type() const { return m_completion_type; }
  2077. Optional<Value> const& completion_value() const { return m_completion_value; }
  2078. private:
  2079. Operand m_iterator_record;
  2080. Completion::Type m_completion_type { Completion::Type::Normal };
  2081. Optional<Value> m_completion_value;
  2082. };
  2083. class IteratorNext final : public Instruction {
  2084. public:
  2085. IteratorNext(Operand dst, Operand iterator_record)
  2086. : Instruction(Type::IteratorNext)
  2087. , m_dst(dst)
  2088. , m_iterator_record(iterator_record)
  2089. {
  2090. }
  2091. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  2092. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  2093. void visit_operands_impl(Function<void(Operand&)> visitor)
  2094. {
  2095. visitor(m_dst);
  2096. visitor(m_iterator_record);
  2097. }
  2098. Operand dst() const { return m_dst; }
  2099. Operand iterator_record() const { return m_iterator_record; }
  2100. private:
  2101. Operand m_dst;
  2102. Operand m_iterator_record;
  2103. };
  2104. class ResolveThisBinding final : public Instruction {
  2105. public:
  2106. explicit ResolveThisBinding(Operand dst)
  2107. : Instruction(Type::ResolveThisBinding)
  2108. , m_dst(dst)
  2109. {
  2110. }
  2111. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  2112. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  2113. void visit_operands_impl(Function<void(Operand&)> visitor)
  2114. {
  2115. visitor(m_dst);
  2116. }
  2117. Operand dst() const { return m_dst; }
  2118. private:
  2119. Operand m_dst;
  2120. };
  2121. class ResolveSuperBase final : public Instruction {
  2122. public:
  2123. explicit ResolveSuperBase(Operand dst)
  2124. : Instruction(Type::ResolveSuperBase)
  2125. , m_dst(dst)
  2126. {
  2127. }
  2128. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  2129. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  2130. void visit_operands_impl(Function<void(Operand&)> visitor)
  2131. {
  2132. visitor(m_dst);
  2133. }
  2134. Operand dst() const { return m_dst; }
  2135. private:
  2136. Operand m_dst;
  2137. };
  2138. class GetNewTarget final : public Instruction {
  2139. public:
  2140. explicit GetNewTarget(Operand dst)
  2141. : Instruction(Type::GetNewTarget)
  2142. , m_dst(dst)
  2143. {
  2144. }
  2145. void execute_impl(Bytecode::Interpreter&) const;
  2146. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  2147. void visit_operands_impl(Function<void(Operand&)> visitor)
  2148. {
  2149. visitor(m_dst);
  2150. }
  2151. Operand dst() const { return m_dst; }
  2152. private:
  2153. Operand m_dst;
  2154. };
  2155. class GetImportMeta final : public Instruction {
  2156. public:
  2157. explicit GetImportMeta(Operand dst)
  2158. : Instruction(Type::GetImportMeta)
  2159. , m_dst(dst)
  2160. {
  2161. }
  2162. void execute_impl(Bytecode::Interpreter&) const;
  2163. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  2164. void visit_operands_impl(Function<void(Operand&)> visitor)
  2165. {
  2166. visitor(m_dst);
  2167. }
  2168. Operand dst() const { return m_dst; }
  2169. private:
  2170. Operand m_dst;
  2171. };
  2172. class TypeofVariable final : public Instruction {
  2173. public:
  2174. TypeofVariable(Operand dst, IdentifierTableIndex identifier)
  2175. : Instruction(Type::TypeofVariable)
  2176. , m_dst(dst)
  2177. , m_identifier(identifier)
  2178. {
  2179. }
  2180. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  2181. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  2182. void visit_operands_impl(Function<void(Operand&)> visitor)
  2183. {
  2184. visitor(m_dst);
  2185. }
  2186. Operand dst() const { return m_dst; }
  2187. IdentifierTableIndex identifier() const { return m_identifier; }
  2188. private:
  2189. Operand m_dst;
  2190. IdentifierTableIndex m_identifier;
  2191. };
  2192. class End final : public Instruction {
  2193. public:
  2194. constexpr static bool IsTerminator = true;
  2195. explicit End(Operand value)
  2196. : Instruction(Type::End)
  2197. , m_value(value)
  2198. {
  2199. }
  2200. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  2201. void visit_operands_impl(Function<void(Operand&)> visitor)
  2202. {
  2203. visitor(m_value);
  2204. }
  2205. Operand value() const { return m_value; }
  2206. private:
  2207. Operand m_value;
  2208. };
  2209. class Dump final : public Instruction {
  2210. public:
  2211. explicit Dump(StringView text, Operand value)
  2212. : Instruction(Type::Dump)
  2213. , m_text(text)
  2214. , m_value(value)
  2215. {
  2216. }
  2217. void execute_impl(Bytecode::Interpreter&) const;
  2218. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  2219. void visit_operands_impl(Function<void(Operand&)> visitor)
  2220. {
  2221. visitor(m_value);
  2222. }
  2223. private:
  2224. StringView m_text;
  2225. Operand m_value;
  2226. };
  2227. }