Op.h 59 KB

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