Op.h 66 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018
  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 JumpNullish final : public Jump {
  954. public:
  955. explicit JumpNullish(Operand condition, Label true_target, Label false_target)
  956. : Jump(Type::JumpNullish, 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. class JumpUndefined final : public Jump {
  967. public:
  968. explicit JumpUndefined(Operand condition, Label true_target, Label false_target)
  969. : Jump(Type::JumpUndefined, move(true_target), move(false_target), sizeof(*this))
  970. , m_condition(condition)
  971. {
  972. }
  973. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  974. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  975. Operand condition() const { return m_condition; }
  976. private:
  977. Operand m_condition;
  978. };
  979. enum class CallType {
  980. Call,
  981. Construct,
  982. DirectEval,
  983. };
  984. class Call final : public Instruction {
  985. public:
  986. Call(CallType type, Operand dst, Operand callee, Operand this_value, ReadonlySpan<Operand> arguments, Optional<StringTableIndex> expression_string = {}, Optional<Builtin> builtin = {})
  987. : Instruction(Type::Call, length_impl(arguments.size()))
  988. , m_dst(dst)
  989. , m_callee(callee)
  990. , m_this_value(this_value)
  991. , m_argument_count(arguments.size())
  992. , m_type(type)
  993. , m_expression_string(expression_string)
  994. , m_builtin(builtin)
  995. {
  996. for (size_t i = 0; i < arguments.size(); ++i)
  997. m_arguments[i] = arguments[i];
  998. }
  999. size_t length_impl(size_t argument_count) const
  1000. {
  1001. return round_up_to_power_of_two(alignof(void*), sizeof(*this) + sizeof(Operand) * argument_count);
  1002. }
  1003. CallType call_type() const { return m_type; }
  1004. Operand dst() const { return m_dst; }
  1005. Operand callee() const { return m_callee; }
  1006. Operand this_value() const { return m_this_value; }
  1007. Optional<StringTableIndex> const& expression_string() const { return m_expression_string; }
  1008. u32 argument_count() const { return m_argument_count; }
  1009. Optional<Builtin> const& builtin() const { return m_builtin; }
  1010. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1011. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  1012. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  1013. private:
  1014. Operand m_dst;
  1015. Operand m_callee;
  1016. Operand m_this_value;
  1017. u32 m_argument_count { 0 };
  1018. CallType m_type;
  1019. Optional<StringTableIndex> m_expression_string;
  1020. Optional<Builtin> m_builtin;
  1021. Operand m_arguments[];
  1022. };
  1023. class CallWithArgumentArray final : public Instruction {
  1024. public:
  1025. CallWithArgumentArray(CallType type, Operand dst, Operand callee, Operand this_value, Operand arguments, Optional<StringTableIndex> expression_string = {})
  1026. : Instruction(Type::CallWithArgumentArray, sizeof(*this))
  1027. , m_dst(dst)
  1028. , m_callee(callee)
  1029. , m_this_value(this_value)
  1030. , m_arguments(arguments)
  1031. , m_type(type)
  1032. , m_expression_string(expression_string)
  1033. {
  1034. }
  1035. Operand dst() const { return m_dst; }
  1036. CallType call_type() const { return m_type; }
  1037. Operand callee() const { return m_callee; }
  1038. Operand this_value() const { return m_this_value; }
  1039. Operand arguments() const { return m_arguments; }
  1040. Optional<StringTableIndex> const& expression_string() const { return m_expression_string; }
  1041. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1042. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  1043. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  1044. private:
  1045. Operand m_dst;
  1046. Operand m_callee;
  1047. Operand m_this_value;
  1048. Operand m_arguments;
  1049. CallType m_type;
  1050. Optional<StringTableIndex> m_expression_string;
  1051. };
  1052. class SuperCallWithArgumentArray : public Instruction {
  1053. public:
  1054. explicit SuperCallWithArgumentArray(Operand dst, Operand arguments, bool is_synthetic)
  1055. : Instruction(Type::SuperCallWithArgumentArray, sizeof(*this))
  1056. , m_dst(dst)
  1057. , m_arguments(arguments)
  1058. , m_is_synthetic(is_synthetic)
  1059. {
  1060. }
  1061. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1062. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  1063. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  1064. Operand dst() const { return m_dst; }
  1065. Operand arguments() const { return m_arguments; }
  1066. bool is_synthetic() const { return m_is_synthetic; }
  1067. private:
  1068. Operand m_dst;
  1069. Operand m_arguments;
  1070. bool m_is_synthetic;
  1071. };
  1072. class NewClass final : public Instruction {
  1073. public:
  1074. explicit NewClass(Operand dst, Optional<Operand> super_class, ClassExpression const& class_expression, Optional<IdentifierTableIndex> lhs_name)
  1075. : Instruction(Type::NewClass, sizeof(*this))
  1076. , m_dst(dst)
  1077. , m_super_class(super_class)
  1078. , m_class_expression(class_expression)
  1079. , m_lhs_name(lhs_name)
  1080. {
  1081. }
  1082. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1083. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  1084. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  1085. Operand dst() const { return m_dst; }
  1086. Optional<Operand> const& super_class() const { return m_super_class; }
  1087. ClassExpression const& class_expression() const { return m_class_expression; }
  1088. Optional<IdentifierTableIndex> const& lhs_name() const { return m_lhs_name; }
  1089. private:
  1090. Operand m_dst;
  1091. Optional<Operand> m_super_class;
  1092. ClassExpression const& m_class_expression;
  1093. Optional<IdentifierTableIndex> m_lhs_name;
  1094. };
  1095. class NewFunction final : public Instruction {
  1096. public:
  1097. explicit NewFunction(Operand dst, FunctionExpression const& function_node, Optional<IdentifierTableIndex> lhs_name, Optional<Operand> home_object = {})
  1098. : Instruction(Type::NewFunction, sizeof(*this))
  1099. , m_dst(dst)
  1100. , m_function_node(function_node)
  1101. , m_lhs_name(lhs_name)
  1102. , m_home_object(move(home_object))
  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. FunctionExpression const& function_node() const { return m_function_node; }
  1110. Optional<IdentifierTableIndex> const& lhs_name() const { return m_lhs_name; }
  1111. Optional<Operand> const& home_object() const { return m_home_object; }
  1112. private:
  1113. Operand m_dst;
  1114. FunctionExpression const& m_function_node;
  1115. Optional<IdentifierTableIndex> m_lhs_name;
  1116. Optional<Operand> m_home_object;
  1117. };
  1118. class BlockDeclarationInstantiation final : public Instruction {
  1119. public:
  1120. explicit BlockDeclarationInstantiation(ScopeNode const& scope_node)
  1121. : Instruction(Type::BlockDeclarationInstantiation, sizeof(*this))
  1122. , m_scope_node(scope_node)
  1123. {
  1124. }
  1125. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1126. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  1127. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  1128. ScopeNode const& scope_node() const { return m_scope_node; }
  1129. private:
  1130. ScopeNode const& m_scope_node;
  1131. };
  1132. class Return final : public Instruction {
  1133. public:
  1134. constexpr static bool IsTerminator = true;
  1135. explicit Return(Optional<Operand> value = {})
  1136. : Instruction(Type::Return, sizeof(*this))
  1137. , m_value(value)
  1138. {
  1139. }
  1140. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1141. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  1142. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  1143. Optional<Operand> const& value() const { return m_value; }
  1144. private:
  1145. Optional<Operand> m_value;
  1146. };
  1147. class Increment final : public Instruction {
  1148. public:
  1149. explicit Increment(Operand dst)
  1150. : Instruction(Type::Increment, sizeof(*this))
  1151. , m_dst(dst)
  1152. {
  1153. }
  1154. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1155. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  1156. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  1157. Operand dst() const { return m_dst; }
  1158. private:
  1159. Operand m_dst;
  1160. };
  1161. class PostfixIncrement final : public Instruction {
  1162. public:
  1163. explicit PostfixIncrement(Operand dst, Operand src)
  1164. : Instruction(Type::PostfixIncrement, sizeof(*this))
  1165. , m_dst(dst)
  1166. , m_src(src)
  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. Operand dst() const { return m_dst; }
  1173. Operand src() const { return m_src; }
  1174. private:
  1175. Operand m_dst;
  1176. Operand m_src;
  1177. };
  1178. class Decrement final : public Instruction {
  1179. public:
  1180. explicit Decrement(Operand dst)
  1181. : Instruction(Type::Decrement, sizeof(*this))
  1182. , m_dst(dst)
  1183. {
  1184. }
  1185. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1186. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  1187. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  1188. Operand dst() const { return m_dst; }
  1189. private:
  1190. Operand m_dst;
  1191. };
  1192. class PostfixDecrement final : public Instruction {
  1193. public:
  1194. explicit PostfixDecrement(Operand dst, Operand src)
  1195. : Instruction(Type::PostfixDecrement, sizeof(*this))
  1196. , m_dst(dst)
  1197. , m_src(src)
  1198. {
  1199. }
  1200. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1201. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  1202. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  1203. Operand dst() const { return m_dst; }
  1204. Operand src() const { return m_src; }
  1205. private:
  1206. Operand m_dst;
  1207. Operand m_src;
  1208. };
  1209. class Throw final : public Instruction {
  1210. public:
  1211. constexpr static bool IsTerminator = true;
  1212. explicit Throw(Operand src)
  1213. : Instruction(Type::Throw, sizeof(*this))
  1214. , m_src(src)
  1215. {
  1216. }
  1217. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1218. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  1219. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  1220. Operand src() const { return m_src; }
  1221. private:
  1222. Operand m_src;
  1223. };
  1224. class ThrowIfNotObject final : public Instruction {
  1225. public:
  1226. ThrowIfNotObject(Operand src)
  1227. : Instruction(Type::ThrowIfNotObject, sizeof(*this))
  1228. , m_src(src)
  1229. {
  1230. }
  1231. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1232. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  1233. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  1234. Operand src() const { return m_src; }
  1235. private:
  1236. Operand m_src;
  1237. };
  1238. class ThrowIfNullish final : public Instruction {
  1239. public:
  1240. explicit ThrowIfNullish(Operand src)
  1241. : Instruction(Type::ThrowIfNullish, sizeof(*this))
  1242. , m_src(src)
  1243. {
  1244. }
  1245. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1246. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  1247. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  1248. Operand src() const { return m_src; }
  1249. private:
  1250. Operand m_src;
  1251. };
  1252. class ThrowIfTDZ final : public Instruction {
  1253. public:
  1254. explicit ThrowIfTDZ(Operand src)
  1255. : Instruction(Type::ThrowIfTDZ, sizeof(*this))
  1256. , m_src(src)
  1257. {
  1258. }
  1259. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1260. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  1261. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  1262. Operand src() const { return m_src; }
  1263. private:
  1264. Operand m_src;
  1265. };
  1266. class EnterUnwindContext final : public Instruction {
  1267. public:
  1268. constexpr static bool IsTerminator = true;
  1269. EnterUnwindContext(Label entry_point, Optional<Label> handler, Optional<Label> finalizer)
  1270. : Instruction(Type::EnterUnwindContext, sizeof(*this))
  1271. , m_entry_point(move(entry_point))
  1272. , m_handler(move(handler))
  1273. , m_finalizer(move(finalizer))
  1274. {
  1275. }
  1276. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1277. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  1278. void replace_references_impl(BasicBlock const& from, BasicBlock const& to)
  1279. {
  1280. if (&m_entry_point.block() == &from)
  1281. m_entry_point = Label { to };
  1282. if (m_handler.has_value() && &m_handler->block() == &from)
  1283. m_handler = Label { to };
  1284. if (m_finalizer.has_value() && &m_finalizer->block() == &from)
  1285. m_finalizer = Label { to };
  1286. }
  1287. auto& entry_point() const { return m_entry_point; }
  1288. auto& handler() const { return m_handler; }
  1289. auto& finalizer() const { return m_finalizer; }
  1290. private:
  1291. Label m_entry_point;
  1292. Optional<Label> m_handler;
  1293. Optional<Label> m_finalizer;
  1294. };
  1295. class ScheduleJump final : public Instruction {
  1296. public:
  1297. // Note: We use this instruction to tell the next `finally` block to
  1298. // continue execution with a specific break/continue target;
  1299. // FIXME: We currently don't clear the interpreter internal flag, when we change
  1300. // the control-flow (`break`, `continue`) in a finally-block,
  1301. // FIXME: .NET on x86_64 uses a call to the finally instead, which could make this
  1302. // easier, at the cost of making control-flow changes (`break`, `continue`, `return`)
  1303. // in the finally-block more difficult, but as stated above, those
  1304. // aren't handled 100% correctly at the moment anyway
  1305. // It might be worth investigating a similar mechanism
  1306. constexpr static bool IsTerminator = true;
  1307. ScheduleJump(Label target)
  1308. : Instruction(Type::ScheduleJump, sizeof(*this))
  1309. , m_target(target)
  1310. {
  1311. }
  1312. Label target() const { return m_target; }
  1313. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1314. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  1315. void replace_references_impl(BasicBlock const& from, BasicBlock const& to)
  1316. {
  1317. if (&m_target.block() == &from)
  1318. m_target = Label { to };
  1319. }
  1320. private:
  1321. Label m_target;
  1322. };
  1323. class LeaveLexicalEnvironment final : public Instruction {
  1324. public:
  1325. LeaveLexicalEnvironment()
  1326. : Instruction(Type::LeaveLexicalEnvironment, sizeof(*this))
  1327. {
  1328. }
  1329. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1330. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  1331. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  1332. };
  1333. class LeaveUnwindContext final : public Instruction {
  1334. public:
  1335. LeaveUnwindContext()
  1336. : Instruction(Type::LeaveUnwindContext, sizeof(*this))
  1337. {
  1338. }
  1339. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1340. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  1341. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  1342. };
  1343. class ContinuePendingUnwind final : public Instruction {
  1344. public:
  1345. constexpr static bool IsTerminator = true;
  1346. explicit ContinuePendingUnwind(Label resume_target)
  1347. : Instruction(Type::ContinuePendingUnwind, sizeof(*this))
  1348. , m_resume_target(resume_target)
  1349. {
  1350. }
  1351. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1352. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  1353. void replace_references_impl(BasicBlock const& from, BasicBlock const& to)
  1354. {
  1355. if (&m_resume_target.block() == &from)
  1356. m_resume_target = Label { to };
  1357. }
  1358. auto& resume_target() const { return m_resume_target; }
  1359. private:
  1360. Label m_resume_target;
  1361. };
  1362. class Yield final : public Instruction {
  1363. public:
  1364. constexpr static bool IsTerminator = true;
  1365. explicit Yield(Label continuation_label, Operand value)
  1366. : Instruction(Type::Yield, sizeof(*this))
  1367. , m_continuation_label(continuation_label)
  1368. , m_value(value)
  1369. {
  1370. }
  1371. explicit Yield(nullptr_t, Operand value)
  1372. : Instruction(Type::Yield, sizeof(*this))
  1373. , m_value(value)
  1374. {
  1375. }
  1376. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1377. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  1378. void replace_references_impl(BasicBlock const& from, BasicBlock const& to)
  1379. {
  1380. if (m_continuation_label.has_value() && &m_continuation_label->block() == &from)
  1381. m_continuation_label = Label { to };
  1382. }
  1383. auto& continuation() const { return m_continuation_label; }
  1384. Operand value() const { return m_value; }
  1385. private:
  1386. Optional<Label> m_continuation_label;
  1387. Operand m_value;
  1388. };
  1389. class Await final : public Instruction {
  1390. public:
  1391. constexpr static bool IsTerminator = true;
  1392. explicit Await(Label continuation_label, Operand argument)
  1393. : Instruction(Type::Await, sizeof(*this))
  1394. , m_continuation_label(continuation_label)
  1395. , m_argument(argument)
  1396. {
  1397. }
  1398. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1399. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  1400. void replace_references_impl(BasicBlock const& from, BasicBlock const& to)
  1401. {
  1402. if (&m_continuation_label.block() == &from)
  1403. m_continuation_label = Label { to };
  1404. }
  1405. auto& continuation() const { return m_continuation_label; }
  1406. Operand argument() const { return m_argument; }
  1407. private:
  1408. Label m_continuation_label;
  1409. Operand m_argument;
  1410. };
  1411. class GetIterator final : public Instruction {
  1412. public:
  1413. GetIterator(Operand dst, Operand iterable, IteratorHint hint = IteratorHint::Sync)
  1414. : Instruction(Type::GetIterator, sizeof(*this))
  1415. , m_dst(dst)
  1416. , m_iterable(iterable)
  1417. , m_hint(hint)
  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&, BasicBlock const&) { }
  1423. Operand dst() const { return m_dst; }
  1424. Operand iterable() const { return m_iterable; }
  1425. IteratorHint hint() const { return m_hint; }
  1426. private:
  1427. Operand m_dst;
  1428. Operand m_iterable;
  1429. IteratorHint m_hint { IteratorHint::Sync };
  1430. };
  1431. class GetObjectFromIteratorRecord final : public Instruction {
  1432. public:
  1433. GetObjectFromIteratorRecord(Operand object, Operand iterator_record)
  1434. : Instruction(Type::GetObjectFromIteratorRecord, sizeof(*this))
  1435. , m_object(object)
  1436. , m_iterator_record(iterator_record)
  1437. {
  1438. }
  1439. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1440. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  1441. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  1442. Operand object() const { return m_object; }
  1443. Operand iterator_record() const { return m_iterator_record; }
  1444. private:
  1445. Operand m_object;
  1446. Operand m_iterator_record;
  1447. };
  1448. class GetNextMethodFromIteratorRecord final : public Instruction {
  1449. public:
  1450. GetNextMethodFromIteratorRecord(Operand next_method, Operand iterator_record)
  1451. : Instruction(Type::GetNextMethodFromIteratorRecord, sizeof(*this))
  1452. , m_next_method(next_method)
  1453. , m_iterator_record(iterator_record)
  1454. {
  1455. }
  1456. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1457. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  1458. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  1459. Operand next_method() const { return m_next_method; }
  1460. Operand iterator_record() const { return m_iterator_record; }
  1461. private:
  1462. Operand m_next_method;
  1463. Operand m_iterator_record;
  1464. };
  1465. class GetMethod final : public Instruction {
  1466. public:
  1467. GetMethod(Operand dst, Operand object, IdentifierTableIndex property)
  1468. : Instruction(Type::GetMethod, sizeof(*this))
  1469. , m_dst(dst)
  1470. , m_object(object)
  1471. , m_property(property)
  1472. {
  1473. }
  1474. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1475. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  1476. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  1477. Operand dst() const { return m_dst; }
  1478. Operand object() const { return m_object; }
  1479. IdentifierTableIndex property() const { return m_property; }
  1480. private:
  1481. Operand m_dst;
  1482. Operand m_object;
  1483. IdentifierTableIndex m_property;
  1484. };
  1485. class GetObjectPropertyIterator final : public Instruction {
  1486. public:
  1487. GetObjectPropertyIterator(Operand dst, Operand object)
  1488. : Instruction(Type::GetObjectPropertyIterator, sizeof(*this))
  1489. , m_dst(dst)
  1490. , m_object(object)
  1491. {
  1492. }
  1493. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1494. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  1495. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  1496. Operand dst() const { return m_dst; }
  1497. Operand object() const { return m_object; }
  1498. private:
  1499. Operand m_dst;
  1500. Operand m_object;
  1501. };
  1502. class IteratorClose final : public Instruction {
  1503. public:
  1504. IteratorClose(Operand iterator_record, Completion::Type completion_type, Optional<Value> completion_value)
  1505. : Instruction(Type::IteratorClose, sizeof(*this))
  1506. , m_iterator_record(iterator_record)
  1507. , m_completion_type(completion_type)
  1508. , m_completion_value(completion_value)
  1509. {
  1510. }
  1511. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1512. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  1513. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  1514. Operand iterator_record() const { return m_iterator_record; }
  1515. Completion::Type completion_type() const { return m_completion_type; }
  1516. Optional<Value> const& completion_value() const { return m_completion_value; }
  1517. private:
  1518. Operand m_iterator_record;
  1519. Completion::Type m_completion_type { Completion::Type::Normal };
  1520. Optional<Value> m_completion_value;
  1521. };
  1522. class AsyncIteratorClose final : public Instruction {
  1523. public:
  1524. AsyncIteratorClose(Operand iterator_record, Completion::Type completion_type, Optional<Value> completion_value)
  1525. : Instruction(Type::AsyncIteratorClose, sizeof(*this))
  1526. , m_iterator_record(iterator_record)
  1527. , m_completion_type(completion_type)
  1528. , m_completion_value(completion_value)
  1529. {
  1530. }
  1531. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1532. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  1533. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  1534. Operand iterator_record() const { return m_iterator_record; }
  1535. Completion::Type completion_type() const { return m_completion_type; }
  1536. Optional<Value> const& completion_value() const { return m_completion_value; }
  1537. private:
  1538. Operand m_iterator_record;
  1539. Completion::Type m_completion_type { Completion::Type::Normal };
  1540. Optional<Value> m_completion_value;
  1541. };
  1542. class IteratorNext final : public Instruction {
  1543. public:
  1544. IteratorNext(Operand dst, Operand iterator_record)
  1545. : Instruction(Type::IteratorNext, sizeof(*this))
  1546. , m_dst(dst)
  1547. , m_iterator_record(iterator_record)
  1548. {
  1549. }
  1550. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1551. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  1552. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  1553. Operand dst() const { return m_dst; }
  1554. Operand iterator_record() const { return m_iterator_record; }
  1555. private:
  1556. Operand m_dst;
  1557. Operand m_iterator_record;
  1558. };
  1559. class ResolveThisBinding final : public Instruction {
  1560. public:
  1561. explicit ResolveThisBinding(Operand dst)
  1562. : Instruction(Type::ResolveThisBinding, sizeof(*this))
  1563. , m_dst(dst)
  1564. {
  1565. }
  1566. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1567. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  1568. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  1569. Operand dst() const { return m_dst; }
  1570. private:
  1571. Operand m_dst;
  1572. };
  1573. class ResolveSuperBase final : public Instruction {
  1574. public:
  1575. explicit ResolveSuperBase(Operand dst)
  1576. : Instruction(Type::ResolveSuperBase, sizeof(*this))
  1577. , m_dst(dst)
  1578. {
  1579. }
  1580. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1581. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  1582. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  1583. Operand dst() const { return m_dst; }
  1584. private:
  1585. Operand m_dst;
  1586. };
  1587. class GetNewTarget final : public Instruction {
  1588. public:
  1589. explicit GetNewTarget(Operand dst)
  1590. : Instruction(Type::GetNewTarget, sizeof(*this))
  1591. , m_dst(dst)
  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. private:
  1599. Operand m_dst;
  1600. };
  1601. class GetImportMeta final : public Instruction {
  1602. public:
  1603. explicit GetImportMeta(Operand dst)
  1604. : Instruction(Type::GetImportMeta, sizeof(*this))
  1605. , m_dst(dst)
  1606. {
  1607. }
  1608. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1609. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  1610. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  1611. Operand dst() const { return m_dst; }
  1612. private:
  1613. Operand m_dst;
  1614. };
  1615. class TypeofVariable final : public Instruction {
  1616. public:
  1617. TypeofVariable(Operand dst, IdentifierTableIndex identifier)
  1618. : Instruction(Type::TypeofVariable, sizeof(*this))
  1619. , m_dst(dst)
  1620. , m_identifier(identifier)
  1621. {
  1622. }
  1623. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1624. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  1625. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  1626. Operand dst() const { return m_dst; }
  1627. IdentifierTableIndex identifier() const { return m_identifier; }
  1628. private:
  1629. Operand m_dst;
  1630. IdentifierTableIndex m_identifier;
  1631. };
  1632. class End final : public Instruction {
  1633. public:
  1634. constexpr static bool IsTerminator = true;
  1635. explicit End(Operand value)
  1636. : Instruction(Type::End, sizeof(*this))
  1637. , m_value(value)
  1638. {
  1639. }
  1640. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1641. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  1642. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  1643. Operand value() const { return m_value; }
  1644. private:
  1645. Operand m_value;
  1646. };
  1647. class Dump final : public Instruction {
  1648. public:
  1649. explicit Dump(StringView text, Operand value)
  1650. : Instruction(Type::Dump, sizeof(*this))
  1651. , m_text(text)
  1652. , m_value(value)
  1653. {
  1654. }
  1655. ThrowCompletionOr<void> execute_impl(Bytecode::Interpreter&) const;
  1656. ByteString to_byte_string_impl(Bytecode::Executable const&) const;
  1657. void replace_references_impl(BasicBlock const&, BasicBlock const&) { }
  1658. private:
  1659. StringView m_text;
  1660. Operand m_value;
  1661. };
  1662. }
  1663. namespace JS::Bytecode {
  1664. ALWAYS_INLINE ThrowCompletionOr<void> Instruction::execute(Bytecode::Interpreter& interpreter) const
  1665. {
  1666. #define __BYTECODE_OP(op) \
  1667. case Instruction::Type::op: \
  1668. return static_cast<Bytecode::Op::op const&>(*this).execute_impl(interpreter);
  1669. switch (type()) {
  1670. ENUMERATE_BYTECODE_OPS(__BYTECODE_OP)
  1671. default:
  1672. VERIFY_NOT_REACHED();
  1673. }
  1674. #undef __BYTECODE_OP
  1675. }
  1676. }