Op.h 69 KB

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