ASTCodegen.cpp 65 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611
  1. /*
  2. * Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, Linus Groh <linusg@serenityos.org>
  4. * Copyright (c) 2021, Gunnar Beutner <gbeutner@serenityos.org>
  5. * Copyright (c) 2021, Marcin Gasperowicz <xnooga@gmail.com>
  6. *
  7. * SPDX-License-Identifier: BSD-2-Clause
  8. */
  9. #include <AK/Format.h>
  10. #include <LibJS/AST.h>
  11. #include <LibJS/Bytecode/Generator.h>
  12. #include <LibJS/Bytecode/Instruction.h>
  13. #include <LibJS/Bytecode/Op.h>
  14. #include <LibJS/Bytecode/Register.h>
  15. #include <LibJS/Bytecode/StringTable.h>
  16. #include <LibJS/Runtime/Environment.h>
  17. namespace JS {
  18. Bytecode::CodeGenerationErrorOr<void> ASTNode::generate_bytecode(Bytecode::Generator&) const
  19. {
  20. return Bytecode::CodeGenerationError {
  21. this,
  22. "Missing generate_bytecode()"sv,
  23. };
  24. }
  25. Bytecode::CodeGenerationErrorOr<void> ScopeNode::generate_bytecode(Bytecode::Generator& generator) const
  26. {
  27. Optional<Bytecode::CodeGenerationError> maybe_error;
  28. size_t pushed_scope_count = 0;
  29. auto const failing_completion = Completion(Completion::Type::Throw, {}, {});
  30. if (is<BlockStatement>(*this) || is<SwitchStatement>(*this)) {
  31. // Perform the steps of BlockDeclarationInstantiation.
  32. if (has_lexical_declarations()) {
  33. generator.begin_variable_scope(Bytecode::Generator::BindingMode::Lexical, Bytecode::Generator::SurroundingScopeKind::Block);
  34. pushed_scope_count++;
  35. }
  36. (void)for_each_lexically_scoped_declaration([&](Declaration const& declaration) -> ThrowCompletionOr<void> {
  37. auto is_constant_declaration = declaration.is_constant_declaration();
  38. declaration.for_each_bound_name([&](auto const& name) {
  39. auto index = generator.intern_identifier(name);
  40. if (is_constant_declaration || !generator.has_binding(index)) {
  41. generator.register_binding(index);
  42. generator.emit<Bytecode::Op::CreateVariable>(index, Bytecode::Op::EnvironmentMode::Lexical, is_constant_declaration);
  43. }
  44. });
  45. if (is<FunctionDeclaration>(declaration)) {
  46. auto& function_declaration = static_cast<FunctionDeclaration const&>(declaration);
  47. auto const& name = function_declaration.name();
  48. auto index = generator.intern_identifier(name);
  49. generator.emit<Bytecode::Op::NewFunction>(function_declaration);
  50. generator.emit<Bytecode::Op::SetVariable>(index, Bytecode::Op::SetVariable::InitializationMode::Initialize);
  51. }
  52. return {};
  53. });
  54. } else if (is<Program>(*this)) {
  55. // Perform the steps of GlobalDeclarationInstantiation.
  56. generator.begin_variable_scope(Bytecode::Generator::BindingMode::Global, Bytecode::Generator::SurroundingScopeKind::Global);
  57. pushed_scope_count++;
  58. // 1. Let lexNames be the LexicallyDeclaredNames of script.
  59. // 2. Let varNames be the VarDeclaredNames of script.
  60. // 3. For each element name of lexNames, do
  61. (void)for_each_lexically_declared_name([&](auto const& name) -> ThrowCompletionOr<void> {
  62. auto identifier = generator.intern_identifier(name);
  63. // a. If env.HasVarDeclaration(name) is true, throw a SyntaxError exception.
  64. // b. If env.HasLexicalDeclaration(name) is true, throw a SyntaxError exception.
  65. if (generator.has_binding(identifier)) {
  66. // FIXME: Throw an actual SyntaxError instance.
  67. generator.emit<Bytecode::Op::NewString>(generator.intern_string(String::formatted("SyntaxError: toplevel variable already declared: {}", name)));
  68. generator.emit<Bytecode::Op::Throw>();
  69. return {};
  70. }
  71. // FIXME: c. If hasRestrictedGlobalProperty is true, throw a SyntaxError exception.
  72. // d. If hasRestrictedGlobal is true, throw a SyntaxError exception.
  73. return {};
  74. });
  75. // 4. For each element name of varNames, do
  76. (void)for_each_var_declared_name([&](auto const& name) -> ThrowCompletionOr<void> {
  77. auto identifier = generator.intern_identifier(name);
  78. // a. If env.HasLexicalDeclaration(name) is true, throw a SyntaxError exception.
  79. if (generator.has_binding(identifier)) {
  80. // FIXME: Throw an actual SyntaxError instance.
  81. generator.emit<Bytecode::Op::NewString>(generator.intern_string(String::formatted("SyntaxError: toplevel variable already declared: {}", name)));
  82. generator.emit<Bytecode::Op::Throw>();
  83. }
  84. return {};
  85. });
  86. // 5. Let varDeclarations be the VarScopedDeclarations of script.
  87. // 6. Let functionsToInitialize be a new empty List.
  88. Vector<FunctionDeclaration const&> functions_to_initialize;
  89. // 7. Let declaredFunctionNames be a new empty List.
  90. HashTable<FlyString> declared_function_names;
  91. // 8. For each element d of varDeclarations, in reverse List order, do
  92. (void)for_each_var_function_declaration_in_reverse_order([&](FunctionDeclaration const& function) -> ThrowCompletionOr<void> {
  93. // a. If d is neither a VariableDeclaration nor a ForBinding nor a BindingIdentifier, then
  94. // i. Assert: d is either a FunctionDeclaration, a GeneratorDeclaration, an AsyncFunctionDeclaration, or an AsyncGeneratorDeclaration.
  95. // Note: This is checked in for_each_var_function_declaration_in_reverse_order.
  96. // ii. NOTE: If there are multiple function declarations for the same name, the last declaration is used.
  97. // iii. Let fn be the sole element of the BoundNames of d.
  98. // iv. If fn is not an element of declaredFunctionNames, then
  99. if (declared_function_names.set(function.name()) != AK::HashSetResult::InsertedNewEntry)
  100. return {};
  101. // FIXME: 1. Let fnDefinable be ? env.CanDeclareGlobalFunction(fn).
  102. // FIXME: 2. If fnDefinable is false, throw a TypeError exception.
  103. // 3. Append fn to declaredFunctionNames.
  104. // Note: Already done in step iv. above.
  105. // 4. Insert d as the first element of functionsToInitialize.
  106. functions_to_initialize.prepend(function);
  107. return {};
  108. });
  109. // 9. Let declaredVarNames be a new empty List.
  110. HashTable<FlyString> declared_var_names;
  111. // 10. For each element d of varDeclarations, do
  112. (void)for_each_var_scoped_variable_declaration([&](Declaration const& declaration) {
  113. // a. If d is a VariableDeclaration, a ForBinding, or a BindingIdentifier, then
  114. // Note: This is done in for_each_var_scoped_variable_declaration.
  115. // i. For each String vn of the BoundNames of d, do
  116. return declaration.for_each_bound_name([&](auto const& name) -> ThrowCompletionOr<void> {
  117. // 1. If vn is not an element of declaredFunctionNames, then
  118. if (declared_function_names.contains(name))
  119. return {};
  120. // FIXME: a. Let vnDefinable be ? env.CanDeclareGlobalVar(vn).
  121. // FIXME: b. If vnDefinable is false, throw a TypeError exception.
  122. // c. If vn is not an element of declaredVarNames, then
  123. // i. Append vn to declaredVarNames.
  124. declared_var_names.set(name);
  125. return {};
  126. });
  127. });
  128. // 11. NOTE: No abnormal terminations occur after this algorithm step if the global object is an ordinary object. However, if the global object is a Proxy exotic object it may exhibit behaviours that cause abnormal terminations in some of the following steps.
  129. // 12. NOTE: Annex B.3.2.2 adds additional steps at this point.
  130. // 12. Let strict be IsStrict of script.
  131. // 13. If strict is false, then
  132. if (!verify_cast<Program>(*this).is_strict_mode()) {
  133. // a. Let declaredFunctionOrVarNames be the list-concatenation of declaredFunctionNames and declaredVarNames.
  134. // b. For each FunctionDeclaration f that is directly contained in the StatementList of a Block, CaseClause, or DefaultClause Contained within script, do
  135. (void)for_each_function_hoistable_with_annexB_extension([&](FunctionDeclaration& function_declaration) {
  136. // i. Let F be StringValue of the BindingIdentifier of f.
  137. auto& function_name = function_declaration.name();
  138. // ii. If replacing the FunctionDeclaration f with a VariableStatement that has F as a BindingIdentifier would not produce any Early Errors for script, then
  139. // Note: This step is already performed during parsing and for_each_function_hoistable_with_annexB_extension so this always passes here.
  140. // 1. If env.HasLexicalDeclaration(F) is false, then
  141. auto index = generator.intern_identifier(function_name);
  142. if (generator.has_binding(index, Bytecode::Generator::BindingMode::Lexical))
  143. return;
  144. // FIXME: a. Let fnDefinable be ? env.CanDeclareGlobalVar(F).
  145. // b. If fnDefinable is true, then
  146. // i. NOTE: A var binding for F is only instantiated here if it is neither a VarDeclaredName nor the name of another FunctionDeclaration.
  147. // ii. If declaredFunctionOrVarNames does not contain F, then
  148. if (!declared_function_names.contains(function_name) && !declared_var_names.contains(function_name)) {
  149. // i. Perform ? env.CreateGlobalVarBinding(F, false).
  150. generator.emit<Bytecode::Op::CreateVariable>(index, Bytecode::Op::EnvironmentMode::Var, false);
  151. // ii. Append F to declaredFunctionOrVarNames.
  152. declared_function_names.set(function_name);
  153. }
  154. // iii. When the FunctionDeclaration f is evaluated, perform the following steps in place of the FunctionDeclaration Evaluation algorithm provided in 15.2.6:
  155. // i. Let genv be the running execution context's VariableEnvironment.
  156. // ii. Let benv be the running execution context's LexicalEnvironment.
  157. // iii. Let fobj be ! benv.GetBindingValue(F, false).
  158. // iv. Perform ? genv.SetMutableBinding(F, fobj, false).
  159. // v. Return NormalCompletion(empty).
  160. function_declaration.set_should_do_additional_annexB_steps();
  161. });
  162. }
  163. // 15. For each element d of lexDeclarations, do
  164. (void)for_each_lexically_scoped_declaration([&](Declaration const& declaration) -> ThrowCompletionOr<void> {
  165. // a. NOTE: Lexically declared names are only instantiated here but not initialized.
  166. // b. For each element dn of the BoundNames of d, do
  167. return declaration.for_each_bound_name([&](auto const& name) -> ThrowCompletionOr<void> {
  168. auto identifier = generator.intern_identifier(name);
  169. // i. If IsConstantDeclaration of d is true, then
  170. generator.register_binding(identifier);
  171. if (declaration.is_constant_declaration()) {
  172. // 1. Perform ? env.CreateImmutableBinding(dn, true).
  173. generator.emit<Bytecode::Op::CreateVariable>(identifier, Bytecode::Op::EnvironmentMode::Lexical, true);
  174. } else {
  175. // ii. Else,
  176. // 1. Perform ? env.CreateMutableBinding(dn, false).
  177. generator.emit<Bytecode::Op::CreateVariable>(identifier, Bytecode::Op::EnvironmentMode::Lexical, false);
  178. }
  179. return {};
  180. });
  181. });
  182. // 16. For each Parse Node f of functionsToInitialize, do
  183. for (auto& function_declaration : functions_to_initialize) {
  184. // FIXME: Do this more correctly.
  185. // a. Let fn be the sole element of the BoundNames of f.
  186. // b. Let fo be InstantiateFunctionObject of f with arguments env and privateEnv.
  187. generator.emit<Bytecode::Op::NewFunction>(function_declaration);
  188. // c. Perform ? env.CreateGlobalFunctionBinding(fn, fo, false).
  189. auto const& name = function_declaration.name();
  190. auto index = generator.intern_identifier(name);
  191. if (!generator.has_binding(index)) {
  192. generator.register_binding(index, Bytecode::Generator::BindingMode::Var);
  193. generator.emit<Bytecode::Op::CreateVariable>(index, Bytecode::Op::EnvironmentMode::Lexical, false);
  194. }
  195. generator.emit<Bytecode::Op::SetVariable>(index, Bytecode::Op::SetVariable::InitializationMode::Initialize);
  196. }
  197. // 17. For each String vn of declaredVarNames, do
  198. // a. Perform ? env.CreateGlobalVarBinding(vn, false).
  199. for (auto& var_name : declared_var_names)
  200. generator.register_binding(generator.intern_identifier(var_name), Bytecode::Generator::BindingMode::Var);
  201. } else {
  202. // Perform the steps of FunctionDeclarationInstantiation.
  203. generator.begin_variable_scope(Bytecode::Generator::BindingMode::Var, Bytecode::Generator::SurroundingScopeKind::Function);
  204. pushed_scope_count++;
  205. if (has_lexical_declarations()) {
  206. generator.begin_variable_scope(Bytecode::Generator::BindingMode::Lexical, Bytecode::Generator::SurroundingScopeKind::Function);
  207. pushed_scope_count++;
  208. }
  209. // FIXME: Implement this boi correctly.
  210. (void)for_each_lexically_scoped_declaration([&](Declaration const& declaration) -> ThrowCompletionOr<void> {
  211. auto is_constant_declaration = declaration.is_constant_declaration();
  212. declaration.for_each_bound_name([&](auto const& name) {
  213. auto index = generator.intern_identifier(name);
  214. if (is_constant_declaration || !generator.has_binding(index)) {
  215. generator.register_binding(index);
  216. generator.emit<Bytecode::Op::CreateVariable>(index, Bytecode::Op::EnvironmentMode::Lexical, is_constant_declaration);
  217. }
  218. });
  219. if (is<FunctionDeclaration>(declaration)) {
  220. auto& function_declaration = static_cast<FunctionDeclaration const&>(declaration);
  221. if (auto result = function_declaration.generate_bytecode(generator); result.is_error()) {
  222. maybe_error = result.release_error();
  223. // To make `for_each_lexically_scoped_declaration` happy.
  224. return failing_completion;
  225. }
  226. auto const& name = function_declaration.name();
  227. auto index = generator.intern_identifier(name);
  228. if (!generator.has_binding(index)) {
  229. generator.register_binding(index);
  230. generator.emit<Bytecode::Op::CreateVariable>(index, Bytecode::Op::EnvironmentMode::Lexical, false);
  231. }
  232. generator.emit<Bytecode::Op::SetVariable>(index, Bytecode::Op::SetVariable::InitializationMode::InitializeOrSet);
  233. }
  234. return {};
  235. });
  236. }
  237. if (maybe_error.has_value())
  238. return maybe_error.release_value();
  239. for (auto& child : children()) {
  240. TRY(child.generate_bytecode(generator));
  241. if (generator.is_current_block_terminated())
  242. break;
  243. }
  244. for (size_t i = 0; i < pushed_scope_count; ++i)
  245. generator.end_variable_scope();
  246. return {};
  247. }
  248. Bytecode::CodeGenerationErrorOr<void> EmptyStatement::generate_bytecode(Bytecode::Generator&) const
  249. {
  250. return {};
  251. }
  252. Bytecode::CodeGenerationErrorOr<void> ExpressionStatement::generate_bytecode(Bytecode::Generator& generator) const
  253. {
  254. return m_expression->generate_bytecode(generator);
  255. }
  256. Bytecode::CodeGenerationErrorOr<void> BinaryExpression::generate_bytecode(Bytecode::Generator& generator) const
  257. {
  258. TRY(m_lhs->generate_bytecode(generator));
  259. auto lhs_reg = generator.allocate_register();
  260. generator.emit<Bytecode::Op::Store>(lhs_reg);
  261. TRY(m_rhs->generate_bytecode(generator));
  262. switch (m_op) {
  263. case BinaryOp::Addition:
  264. generator.emit<Bytecode::Op::Add>(lhs_reg);
  265. break;
  266. case BinaryOp::Subtraction:
  267. generator.emit<Bytecode::Op::Sub>(lhs_reg);
  268. break;
  269. case BinaryOp::Multiplication:
  270. generator.emit<Bytecode::Op::Mul>(lhs_reg);
  271. break;
  272. case BinaryOp::Division:
  273. generator.emit<Bytecode::Op::Div>(lhs_reg);
  274. break;
  275. case BinaryOp::Modulo:
  276. generator.emit<Bytecode::Op::Mod>(lhs_reg);
  277. break;
  278. case BinaryOp::Exponentiation:
  279. generator.emit<Bytecode::Op::Exp>(lhs_reg);
  280. break;
  281. case BinaryOp::GreaterThan:
  282. generator.emit<Bytecode::Op::GreaterThan>(lhs_reg);
  283. break;
  284. case BinaryOp::GreaterThanEquals:
  285. generator.emit<Bytecode::Op::GreaterThanEquals>(lhs_reg);
  286. break;
  287. case BinaryOp::LessThan:
  288. generator.emit<Bytecode::Op::LessThan>(lhs_reg);
  289. break;
  290. case BinaryOp::LessThanEquals:
  291. generator.emit<Bytecode::Op::LessThanEquals>(lhs_reg);
  292. break;
  293. case BinaryOp::LooselyInequals:
  294. generator.emit<Bytecode::Op::LooselyInequals>(lhs_reg);
  295. break;
  296. case BinaryOp::LooselyEquals:
  297. generator.emit<Bytecode::Op::LooselyEquals>(lhs_reg);
  298. break;
  299. case BinaryOp::StrictlyInequals:
  300. generator.emit<Bytecode::Op::StrictlyInequals>(lhs_reg);
  301. break;
  302. case BinaryOp::StrictlyEquals:
  303. generator.emit<Bytecode::Op::StrictlyEquals>(lhs_reg);
  304. break;
  305. case BinaryOp::BitwiseAnd:
  306. generator.emit<Bytecode::Op::BitwiseAnd>(lhs_reg);
  307. break;
  308. case BinaryOp::BitwiseOr:
  309. generator.emit<Bytecode::Op::BitwiseOr>(lhs_reg);
  310. break;
  311. case BinaryOp::BitwiseXor:
  312. generator.emit<Bytecode::Op::BitwiseXor>(lhs_reg);
  313. break;
  314. case BinaryOp::LeftShift:
  315. generator.emit<Bytecode::Op::LeftShift>(lhs_reg);
  316. break;
  317. case BinaryOp::RightShift:
  318. generator.emit<Bytecode::Op::RightShift>(lhs_reg);
  319. break;
  320. case BinaryOp::UnsignedRightShift:
  321. generator.emit<Bytecode::Op::UnsignedRightShift>(lhs_reg);
  322. break;
  323. case BinaryOp::In:
  324. generator.emit<Bytecode::Op::In>(lhs_reg);
  325. break;
  326. case BinaryOp::InstanceOf:
  327. generator.emit<Bytecode::Op::InstanceOf>(lhs_reg);
  328. break;
  329. default:
  330. VERIFY_NOT_REACHED();
  331. }
  332. return {};
  333. }
  334. Bytecode::CodeGenerationErrorOr<void> LogicalExpression::generate_bytecode(Bytecode::Generator& generator) const
  335. {
  336. TRY(m_lhs->generate_bytecode(generator));
  337. // lhs
  338. // jump op (true) end (false) rhs
  339. // rhs
  340. // jump always (true) end
  341. // end
  342. auto& rhs_block = generator.make_block();
  343. auto& end_block = generator.make_block();
  344. switch (m_op) {
  345. case LogicalOp::And:
  346. generator.emit<Bytecode::Op::JumpConditional>().set_targets(
  347. Bytecode::Label { rhs_block },
  348. Bytecode::Label { end_block });
  349. break;
  350. case LogicalOp::Or:
  351. generator.emit<Bytecode::Op::JumpConditional>().set_targets(
  352. Bytecode::Label { end_block },
  353. Bytecode::Label { rhs_block });
  354. break;
  355. case LogicalOp::NullishCoalescing:
  356. generator.emit<Bytecode::Op::JumpNullish>().set_targets(
  357. Bytecode::Label { rhs_block },
  358. Bytecode::Label { end_block });
  359. break;
  360. default:
  361. VERIFY_NOT_REACHED();
  362. }
  363. generator.switch_to_basic_block(rhs_block);
  364. TRY(m_rhs->generate_bytecode(generator));
  365. generator.emit<Bytecode::Op::Jump>().set_targets(
  366. Bytecode::Label { end_block },
  367. {});
  368. generator.switch_to_basic_block(end_block);
  369. return {};
  370. }
  371. Bytecode::CodeGenerationErrorOr<void> UnaryExpression::generate_bytecode(Bytecode::Generator& generator) const
  372. {
  373. TRY(m_lhs->generate_bytecode(generator));
  374. switch (m_op) {
  375. case UnaryOp::BitwiseNot:
  376. generator.emit<Bytecode::Op::BitwiseNot>();
  377. break;
  378. case UnaryOp::Not:
  379. generator.emit<Bytecode::Op::Not>();
  380. break;
  381. case UnaryOp::Plus:
  382. generator.emit<Bytecode::Op::UnaryPlus>();
  383. break;
  384. case UnaryOp::Minus:
  385. generator.emit<Bytecode::Op::UnaryMinus>();
  386. break;
  387. case UnaryOp::Typeof:
  388. generator.emit<Bytecode::Op::Typeof>();
  389. break;
  390. case UnaryOp::Void:
  391. generator.emit<Bytecode::Op::LoadImmediate>(js_undefined());
  392. break;
  393. default:
  394. return Bytecode::CodeGenerationError {
  395. this,
  396. "Unimplemented operation"sv,
  397. };
  398. }
  399. return {};
  400. }
  401. Bytecode::CodeGenerationErrorOr<void> NumericLiteral::generate_bytecode(Bytecode::Generator& generator) const
  402. {
  403. generator.emit<Bytecode::Op::LoadImmediate>(m_value);
  404. return {};
  405. }
  406. Bytecode::CodeGenerationErrorOr<void> BooleanLiteral::generate_bytecode(Bytecode::Generator& generator) const
  407. {
  408. generator.emit<Bytecode::Op::LoadImmediate>(Value(m_value));
  409. return {};
  410. }
  411. Bytecode::CodeGenerationErrorOr<void> NullLiteral::generate_bytecode(Bytecode::Generator& generator) const
  412. {
  413. generator.emit<Bytecode::Op::LoadImmediate>(js_null());
  414. return {};
  415. }
  416. Bytecode::CodeGenerationErrorOr<void> BigIntLiteral::generate_bytecode(Bytecode::Generator& generator) const
  417. {
  418. generator.emit<Bytecode::Op::NewBigInt>(Crypto::SignedBigInteger::from_base(10, m_value.substring(0, m_value.length() - 1)));
  419. return {};
  420. }
  421. Bytecode::CodeGenerationErrorOr<void> StringLiteral::generate_bytecode(Bytecode::Generator& generator) const
  422. {
  423. generator.emit<Bytecode::Op::NewString>(generator.intern_string(m_value));
  424. return {};
  425. }
  426. Bytecode::CodeGenerationErrorOr<void> RegExpLiteral::generate_bytecode(Bytecode::Generator& generator) const
  427. {
  428. auto source_index = generator.intern_string(m_pattern);
  429. auto flags_index = generator.intern_string(m_flags);
  430. generator.emit<Bytecode::Op::NewRegExp>(source_index, flags_index);
  431. return {};
  432. }
  433. Bytecode::CodeGenerationErrorOr<void> Identifier::generate_bytecode(Bytecode::Generator& generator) const
  434. {
  435. generator.emit<Bytecode::Op::GetVariable>(generator.intern_identifier(m_string));
  436. return {};
  437. }
  438. Bytecode::CodeGenerationErrorOr<void> AssignmentExpression::generate_bytecode(Bytecode::Generator& generator) const
  439. {
  440. // FIXME: Implement this for BindingPatterns too.
  441. auto& lhs = m_lhs.get<NonnullRefPtr<Expression>>();
  442. if (m_op == AssignmentOp::Assignment) {
  443. TRY(m_rhs->generate_bytecode(generator));
  444. return generator.emit_store_to_reference(lhs);
  445. }
  446. TRY(generator.emit_load_from_reference(lhs));
  447. Bytecode::BasicBlock* rhs_block_ptr { nullptr };
  448. Bytecode::BasicBlock* end_block_ptr { nullptr };
  449. // Logical assignments short circuit.
  450. if (m_op == AssignmentOp::AndAssignment) { // &&=
  451. rhs_block_ptr = &generator.make_block();
  452. end_block_ptr = &generator.make_block();
  453. generator.emit<Bytecode::Op::JumpConditional>().set_targets(
  454. Bytecode::Label { *rhs_block_ptr },
  455. Bytecode::Label { *end_block_ptr });
  456. } else if (m_op == AssignmentOp::OrAssignment) { // ||=
  457. rhs_block_ptr = &generator.make_block();
  458. end_block_ptr = &generator.make_block();
  459. generator.emit<Bytecode::Op::JumpConditional>().set_targets(
  460. Bytecode::Label { *end_block_ptr },
  461. Bytecode::Label { *rhs_block_ptr });
  462. } else if (m_op == AssignmentOp::NullishAssignment) { // ??=
  463. rhs_block_ptr = &generator.make_block();
  464. end_block_ptr = &generator.make_block();
  465. generator.emit<Bytecode::Op::JumpNullish>().set_targets(
  466. Bytecode::Label { *rhs_block_ptr },
  467. Bytecode::Label { *end_block_ptr });
  468. }
  469. if (rhs_block_ptr)
  470. generator.switch_to_basic_block(*rhs_block_ptr);
  471. // lhs_reg is a part of the rhs_block because the store isn't necessary
  472. // if the logical assignment condition fails.
  473. auto lhs_reg = generator.allocate_register();
  474. generator.emit<Bytecode::Op::Store>(lhs_reg);
  475. TRY(m_rhs->generate_bytecode(generator));
  476. switch (m_op) {
  477. case AssignmentOp::AdditionAssignment:
  478. generator.emit<Bytecode::Op::Add>(lhs_reg);
  479. break;
  480. case AssignmentOp::SubtractionAssignment:
  481. generator.emit<Bytecode::Op::Sub>(lhs_reg);
  482. break;
  483. case AssignmentOp::MultiplicationAssignment:
  484. generator.emit<Bytecode::Op::Mul>(lhs_reg);
  485. break;
  486. case AssignmentOp::DivisionAssignment:
  487. generator.emit<Bytecode::Op::Div>(lhs_reg);
  488. break;
  489. case AssignmentOp::ModuloAssignment:
  490. generator.emit<Bytecode::Op::Mod>(lhs_reg);
  491. break;
  492. case AssignmentOp::ExponentiationAssignment:
  493. generator.emit<Bytecode::Op::Exp>(lhs_reg);
  494. break;
  495. case AssignmentOp::BitwiseAndAssignment:
  496. generator.emit<Bytecode::Op::BitwiseAnd>(lhs_reg);
  497. break;
  498. case AssignmentOp::BitwiseOrAssignment:
  499. generator.emit<Bytecode::Op::BitwiseOr>(lhs_reg);
  500. break;
  501. case AssignmentOp::BitwiseXorAssignment:
  502. generator.emit<Bytecode::Op::BitwiseXor>(lhs_reg);
  503. break;
  504. case AssignmentOp::LeftShiftAssignment:
  505. generator.emit<Bytecode::Op::LeftShift>(lhs_reg);
  506. break;
  507. case AssignmentOp::RightShiftAssignment:
  508. generator.emit<Bytecode::Op::RightShift>(lhs_reg);
  509. break;
  510. case AssignmentOp::UnsignedRightShiftAssignment:
  511. generator.emit<Bytecode::Op::UnsignedRightShift>(lhs_reg);
  512. break;
  513. case AssignmentOp::AndAssignment:
  514. case AssignmentOp::OrAssignment:
  515. case AssignmentOp::NullishAssignment:
  516. break; // These are handled above.
  517. default:
  518. return Bytecode::CodeGenerationError {
  519. this,
  520. "Unimplemented operation"sv,
  521. };
  522. }
  523. TRY(generator.emit_store_to_reference(lhs));
  524. if (end_block_ptr) {
  525. generator.emit<Bytecode::Op::Jump>().set_targets(
  526. Bytecode::Label { *end_block_ptr },
  527. {});
  528. generator.switch_to_basic_block(*end_block_ptr);
  529. }
  530. return {};
  531. }
  532. Bytecode::CodeGenerationErrorOr<void> WhileStatement::generate_bytecode(Bytecode::Generator& generator) const
  533. {
  534. // test
  535. // jump if_false (true) end (false) body
  536. // body
  537. // jump always (true) test
  538. // end
  539. auto& test_block = generator.make_block();
  540. auto& body_block = generator.make_block();
  541. auto& end_block = generator.make_block();
  542. // Init result register
  543. generator.emit<Bytecode::Op::LoadImmediate>(js_undefined());
  544. auto result_reg = generator.allocate_register();
  545. generator.emit<Bytecode::Op::Store>(result_reg);
  546. // jump to the test block
  547. generator.emit<Bytecode::Op::Jump>().set_targets(
  548. Bytecode::Label { test_block },
  549. {});
  550. generator.switch_to_basic_block(test_block);
  551. TRY(m_test->generate_bytecode(generator));
  552. generator.emit<Bytecode::Op::JumpConditional>().set_targets(
  553. Bytecode::Label { body_block },
  554. Bytecode::Label { end_block });
  555. generator.switch_to_basic_block(body_block);
  556. generator.begin_continuable_scope(Bytecode::Label { test_block });
  557. generator.begin_breakable_scope(Bytecode::Label { end_block });
  558. TRY(m_body->generate_bytecode(generator));
  559. if (!generator.is_current_block_terminated()) {
  560. generator.emit<Bytecode::Op::Jump>().set_targets(
  561. Bytecode::Label { test_block },
  562. {});
  563. generator.end_continuable_scope();
  564. generator.end_breakable_scope();
  565. generator.switch_to_basic_block(end_block);
  566. generator.emit<Bytecode::Op::Load>(result_reg);
  567. }
  568. return {};
  569. }
  570. Bytecode::CodeGenerationErrorOr<void> DoWhileStatement::generate_bytecode(Bytecode::Generator& generator) const
  571. {
  572. // jump always (true) body
  573. // test
  574. // jump if_false (true) end (false) body
  575. // body
  576. // jump always (true) test
  577. // end
  578. auto& test_block = generator.make_block();
  579. auto& body_block = generator.make_block();
  580. auto& end_block = generator.make_block();
  581. // Init result register
  582. generator.emit<Bytecode::Op::LoadImmediate>(js_undefined());
  583. auto result_reg = generator.allocate_register();
  584. generator.emit<Bytecode::Op::Store>(result_reg);
  585. // jump to the body block
  586. generator.emit<Bytecode::Op::Jump>().set_targets(
  587. Bytecode::Label { body_block },
  588. {});
  589. generator.switch_to_basic_block(test_block);
  590. TRY(m_test->generate_bytecode(generator));
  591. generator.emit<Bytecode::Op::JumpConditional>().set_targets(
  592. Bytecode::Label { body_block },
  593. Bytecode::Label { end_block });
  594. generator.switch_to_basic_block(body_block);
  595. generator.begin_continuable_scope(Bytecode::Label { test_block });
  596. generator.begin_breakable_scope(Bytecode::Label { end_block });
  597. TRY(m_body->generate_bytecode(generator));
  598. if (!generator.is_current_block_terminated()) {
  599. generator.emit<Bytecode::Op::Jump>().set_targets(
  600. Bytecode::Label { test_block },
  601. {});
  602. generator.end_continuable_scope();
  603. generator.end_breakable_scope();
  604. generator.switch_to_basic_block(end_block);
  605. generator.emit<Bytecode::Op::Load>(result_reg);
  606. }
  607. return {};
  608. }
  609. Bytecode::CodeGenerationErrorOr<void> ForStatement::generate_bytecode(Bytecode::Generator& generator) const
  610. {
  611. // init
  612. // jump always (true) test
  613. // test
  614. // jump if_true (true) body (false) end
  615. // body
  616. // jump always (true) update
  617. // update
  618. // jump always (true) test
  619. // end
  620. // If 'test' is missing, fuse the 'test' and 'body' basic blocks
  621. // If 'update' is missing, fuse the 'body' and 'update' basic blocks
  622. Bytecode::BasicBlock* test_block_ptr { nullptr };
  623. Bytecode::BasicBlock* body_block_ptr { nullptr };
  624. Bytecode::BasicBlock* update_block_ptr { nullptr };
  625. auto& end_block = generator.make_block();
  626. if (m_init)
  627. TRY(m_init->generate_bytecode(generator));
  628. body_block_ptr = &generator.make_block();
  629. if (m_test)
  630. test_block_ptr = &generator.make_block();
  631. else
  632. test_block_ptr = body_block_ptr;
  633. if (m_update)
  634. update_block_ptr = &generator.make_block();
  635. else
  636. update_block_ptr = body_block_ptr;
  637. generator.emit<Bytecode::Op::LoadImmediate>(js_undefined());
  638. auto result_reg = generator.allocate_register();
  639. generator.emit<Bytecode::Op::Store>(result_reg);
  640. generator.emit<Bytecode::Op::Jump>().set_targets(
  641. Bytecode::Label { *test_block_ptr },
  642. {});
  643. if (m_test) {
  644. generator.switch_to_basic_block(*test_block_ptr);
  645. TRY(m_test->generate_bytecode(generator));
  646. generator.emit<Bytecode::Op::JumpConditional>().set_targets(
  647. Bytecode::Label { *body_block_ptr },
  648. Bytecode::Label { end_block });
  649. }
  650. generator.switch_to_basic_block(*body_block_ptr);
  651. generator.begin_continuable_scope(Bytecode::Label { *update_block_ptr });
  652. generator.begin_breakable_scope(Bytecode::Label { end_block });
  653. TRY(m_body->generate_bytecode(generator));
  654. generator.end_continuable_scope();
  655. if (!generator.is_current_block_terminated()) {
  656. if (m_update) {
  657. generator.emit<Bytecode::Op::Jump>().set_targets(
  658. Bytecode::Label { *update_block_ptr },
  659. {});
  660. generator.switch_to_basic_block(*update_block_ptr);
  661. TRY(m_update->generate_bytecode(generator));
  662. }
  663. generator.emit<Bytecode::Op::Jump>().set_targets(
  664. Bytecode::Label { *test_block_ptr },
  665. {});
  666. generator.end_breakable_scope();
  667. generator.switch_to_basic_block(end_block);
  668. generator.emit<Bytecode::Op::Load>(result_reg);
  669. }
  670. return {};
  671. }
  672. Bytecode::CodeGenerationErrorOr<void> ObjectExpression::generate_bytecode(Bytecode::Generator& generator) const
  673. {
  674. generator.emit<Bytecode::Op::NewObject>();
  675. if (m_properties.is_empty())
  676. return {};
  677. auto object_reg = generator.allocate_register();
  678. generator.emit<Bytecode::Op::Store>(object_reg);
  679. for (auto& property : m_properties) {
  680. if (property.type() != ObjectProperty::Type::KeyValue)
  681. return Bytecode::CodeGenerationError {
  682. this,
  683. "Unimplemented property kind"sv,
  684. };
  685. if (is<StringLiteral>(property.key())) {
  686. auto& string_literal = static_cast<StringLiteral const&>(property.key());
  687. Bytecode::IdentifierTableIndex key_name = generator.intern_identifier(string_literal.value());
  688. TRY(property.value().generate_bytecode(generator));
  689. generator.emit<Bytecode::Op::PutById>(object_reg, key_name);
  690. } else {
  691. TRY(property.key().generate_bytecode(generator));
  692. auto property_reg = generator.allocate_register();
  693. generator.emit<Bytecode::Op::Store>(property_reg);
  694. TRY(property.value().generate_bytecode(generator));
  695. generator.emit<Bytecode::Op::PutByValue>(object_reg, property_reg);
  696. }
  697. }
  698. generator.emit<Bytecode::Op::Load>(object_reg);
  699. return {};
  700. }
  701. Bytecode::CodeGenerationErrorOr<void> ArrayExpression::generate_bytecode(Bytecode::Generator& generator) const
  702. {
  703. Vector<Bytecode::Register> element_regs;
  704. for (auto& element : m_elements) {
  705. if (element) {
  706. TRY(element->generate_bytecode(generator));
  707. if (is<SpreadExpression>(*element)) {
  708. return Bytecode::CodeGenerationError {
  709. this,
  710. "Unimplemented element kind: SpreadExpression"sv,
  711. };
  712. }
  713. } else {
  714. generator.emit<Bytecode::Op::LoadImmediate>(Value {});
  715. }
  716. auto element_reg = generator.allocate_register();
  717. generator.emit<Bytecode::Op::Store>(element_reg);
  718. element_regs.append(element_reg);
  719. }
  720. generator.emit_with_extra_register_slots<Bytecode::Op::NewArray>(element_regs.size(), element_regs);
  721. return {};
  722. }
  723. Bytecode::CodeGenerationErrorOr<void> MemberExpression::generate_bytecode(Bytecode::Generator& generator) const
  724. {
  725. return generator.emit_load_from_reference(*this);
  726. }
  727. Bytecode::CodeGenerationErrorOr<void> FunctionDeclaration::generate_bytecode(Bytecode::Generator& generator) const
  728. {
  729. if (m_is_hoisted) {
  730. auto index = generator.intern_identifier(name());
  731. generator.emit<Bytecode::Op::GetVariable>(index);
  732. generator.emit<Bytecode::Op::SetVariable>(index, Bytecode::Op::SetVariable::InitializationMode::Initialize, Bytecode::Op::EnvironmentMode::Var);
  733. }
  734. return {};
  735. }
  736. Bytecode::CodeGenerationErrorOr<void> FunctionExpression::generate_bytecode(Bytecode::Generator& generator) const
  737. {
  738. generator.emit<Bytecode::Op::NewFunction>(*this);
  739. return {};
  740. }
  741. static Bytecode::CodeGenerationErrorOr<void> generate_binding_pattern_bytecode(Bytecode::Generator& generator, BindingPattern const& pattern, Bytecode::Op::SetVariable::InitializationMode, Bytecode::Register const& value_reg);
  742. static Bytecode::CodeGenerationErrorOr<void> generate_object_binding_pattern_bytecode(Bytecode::Generator& generator, BindingPattern const& pattern, Bytecode::Op::SetVariable::InitializationMode initialization_mode, Bytecode::Register const& value_reg)
  743. {
  744. Vector<Bytecode::Register> excluded_property_names;
  745. auto has_rest = false;
  746. if (pattern.entries.size() > 0)
  747. has_rest = pattern.entries[pattern.entries.size() - 1].is_rest;
  748. for (auto& [name, alias, initializer, is_rest] : pattern.entries) {
  749. if (is_rest) {
  750. VERIFY(name.has<NonnullRefPtr<Identifier>>());
  751. VERIFY(alias.has<Empty>());
  752. VERIFY(!initializer);
  753. auto identifier = name.get<NonnullRefPtr<Identifier>>()->string();
  754. auto interned_identifier = generator.intern_identifier(identifier);
  755. generator.emit_with_extra_register_slots<Bytecode::Op::CopyObjectExcludingProperties>(excluded_property_names.size(), value_reg, excluded_property_names);
  756. generator.emit<Bytecode::Op::SetVariable>(interned_identifier, initialization_mode);
  757. return {};
  758. }
  759. Bytecode::StringTableIndex name_index;
  760. if (name.has<NonnullRefPtr<Identifier>>()) {
  761. auto identifier = name.get<NonnullRefPtr<Identifier>>()->string();
  762. name_index = generator.intern_string(identifier);
  763. if (has_rest) {
  764. auto excluded_name_reg = generator.allocate_register();
  765. excluded_property_names.append(excluded_name_reg);
  766. generator.emit<Bytecode::Op::NewString>(name_index);
  767. generator.emit<Bytecode::Op::Store>(excluded_name_reg);
  768. }
  769. generator.emit<Bytecode::Op::Load>(value_reg);
  770. generator.emit<Bytecode::Op::GetById>(generator.intern_identifier(identifier));
  771. } else {
  772. auto expression = name.get<NonnullRefPtr<Expression>>();
  773. TRY(expression->generate_bytecode(generator));
  774. if (has_rest) {
  775. auto excluded_name_reg = generator.allocate_register();
  776. excluded_property_names.append(excluded_name_reg);
  777. generator.emit<Bytecode::Op::Store>(excluded_name_reg);
  778. }
  779. generator.emit<Bytecode::Op::GetByValue>(value_reg);
  780. }
  781. if (initializer) {
  782. auto& if_undefined_block = generator.make_block();
  783. auto& if_not_undefined_block = generator.make_block();
  784. generator.emit<Bytecode::Op::JumpUndefined>().set_targets(
  785. Bytecode::Label { if_undefined_block },
  786. Bytecode::Label { if_not_undefined_block });
  787. generator.switch_to_basic_block(if_undefined_block);
  788. TRY(initializer->generate_bytecode(generator));
  789. generator.emit<Bytecode::Op::Jump>().set_targets(
  790. Bytecode::Label { if_not_undefined_block },
  791. {});
  792. generator.switch_to_basic_block(if_not_undefined_block);
  793. }
  794. if (alias.has<NonnullRefPtr<BindingPattern>>()) {
  795. auto& binding_pattern = *alias.get<NonnullRefPtr<BindingPattern>>();
  796. auto nested_value_reg = generator.allocate_register();
  797. generator.emit<Bytecode::Op::Store>(nested_value_reg);
  798. TRY(generate_binding_pattern_bytecode(generator, binding_pattern, initialization_mode, nested_value_reg));
  799. } else if (alias.has<Empty>()) {
  800. if (name.has<NonnullRefPtr<Expression>>()) {
  801. // This needs some sort of SetVariableByValue opcode, as it's a runtime binding
  802. return Bytecode::CodeGenerationError {
  803. name.get<NonnullRefPtr<Expression>>().ptr(),
  804. "Unimplemented name/alias pair: Empty/Expression"sv,
  805. };
  806. }
  807. auto& identifier = alias.get<NonnullRefPtr<Identifier>>()->string();
  808. generator.emit<Bytecode::Op::SetVariable>(generator.intern_identifier(identifier), initialization_mode);
  809. } else {
  810. auto& identifier = alias.get<NonnullRefPtr<Identifier>>()->string();
  811. generator.emit<Bytecode::Op::SetVariable>(generator.intern_identifier(identifier), initialization_mode);
  812. }
  813. }
  814. return {};
  815. }
  816. static Bytecode::CodeGenerationErrorOr<void> generate_array_binding_pattern_bytecode(Bytecode::Generator& generator, BindingPattern const& pattern, Bytecode::Op::SetVariable::InitializationMode initialization_mode, Bytecode::Register const& value_reg)
  817. {
  818. /*
  819. * Consider the following destructuring assignment:
  820. *
  821. * let [a, b, c, d, e] = o;
  822. *
  823. * It would be fairly trivial to just loop through this iterator, getting the value
  824. * at each step and assigning them to the binding sequentially. However, this is not
  825. * correct: once an iterator is exhausted, it must not be called again. This complicates
  826. * the bytecode. In order to accomplish this, we do the following:
  827. *
  828. * - Reserve a special boolean register which holds 'true' if the iterator is exhausted,
  829. * and false otherwise
  830. * - When we are retrieving the value which should be bound, we first check this register.
  831. * If it is 'true', we load undefined into the accumulator. Otherwise, we grab the next
  832. * value from the iterator and store it into the accumulator.
  833. *
  834. * Note that the is_exhausted register does not need to be loaded with false because the
  835. * first IteratorNext bytecode is _not_ proceeded by an exhausted check, as it is
  836. * unnecessary.
  837. */
  838. auto is_iterator_exhausted_register = generator.allocate_register();
  839. auto iterator_reg = generator.allocate_register();
  840. generator.emit<Bytecode::Op::Load>(value_reg);
  841. generator.emit<Bytecode::Op::GetIterator>();
  842. generator.emit<Bytecode::Op::Store>(iterator_reg);
  843. bool first = true;
  844. auto temp_iterator_result_reg = generator.allocate_register();
  845. auto assign_accumulator_to_alias = [&](auto& alias) {
  846. return alias.visit(
  847. [&](Empty) -> Bytecode::CodeGenerationErrorOr<void> {
  848. // This element is an elision
  849. return {};
  850. },
  851. [&](NonnullRefPtr<Identifier> const& identifier) -> Bytecode::CodeGenerationErrorOr<void> {
  852. auto interned_index = generator.intern_identifier(identifier->string());
  853. generator.emit<Bytecode::Op::SetVariable>(interned_index, initialization_mode);
  854. return {};
  855. },
  856. [&](NonnullRefPtr<BindingPattern> const& pattern) -> Bytecode::CodeGenerationErrorOr<void> {
  857. // Store the accumulator value in a permanent register
  858. auto target_reg = generator.allocate_register();
  859. generator.emit<Bytecode::Op::Store>(target_reg);
  860. return generate_binding_pattern_bytecode(generator, pattern, initialization_mode, target_reg);
  861. },
  862. [&](NonnullRefPtr<MemberExpression> const& expr) -> Bytecode::CodeGenerationErrorOr<void> {
  863. return Bytecode::CodeGenerationError {
  864. expr.ptr(),
  865. "Unimplemented alias mode: MemberExpression"sv,
  866. };
  867. });
  868. };
  869. for (auto& [name, alias, initializer, is_rest] : pattern.entries) {
  870. VERIFY(name.has<Empty>());
  871. if (is_rest) {
  872. if (first) {
  873. // The iterator has not been called, and is thus known to be not exhausted
  874. generator.emit<Bytecode::Op::Load>(iterator_reg);
  875. generator.emit<Bytecode::Op::IteratorToArray>();
  876. } else {
  877. auto& if_exhausted_block = generator.make_block();
  878. auto& if_not_exhausted_block = generator.make_block();
  879. auto& continuation_block = generator.make_block();
  880. generator.emit<Bytecode::Op::Load>(is_iterator_exhausted_register);
  881. generator.emit<Bytecode::Op::JumpConditional>().set_targets(
  882. Bytecode::Label { if_exhausted_block },
  883. Bytecode::Label { if_not_exhausted_block });
  884. generator.switch_to_basic_block(if_exhausted_block);
  885. generator.emit<Bytecode::Op::NewArray>();
  886. generator.emit<Bytecode::Op::Jump>().set_targets(
  887. Bytecode::Label { continuation_block },
  888. {});
  889. generator.switch_to_basic_block(if_not_exhausted_block);
  890. generator.emit<Bytecode::Op::Load>(iterator_reg);
  891. generator.emit<Bytecode::Op::IteratorToArray>();
  892. generator.emit<Bytecode::Op::Jump>().set_targets(
  893. Bytecode::Label { continuation_block },
  894. {});
  895. generator.switch_to_basic_block(continuation_block);
  896. }
  897. return assign_accumulator_to_alias(alias);
  898. }
  899. // In the first iteration of the loop, a few things are true which can save
  900. // us some bytecode:
  901. // - the iterator result is still in the accumulator, so we can avoid a load
  902. // - the iterator is not yet exhausted, which can save us a jump and some
  903. // creation
  904. auto& iterator_is_exhausted_block = generator.make_block();
  905. if (!first) {
  906. auto& iterator_is_not_exhausted_block = generator.make_block();
  907. generator.emit<Bytecode::Op::Load>(is_iterator_exhausted_register);
  908. generator.emit<Bytecode::Op::JumpConditional>().set_targets(
  909. Bytecode::Label { iterator_is_exhausted_block },
  910. Bytecode::Label { iterator_is_not_exhausted_block });
  911. generator.switch_to_basic_block(iterator_is_not_exhausted_block);
  912. generator.emit<Bytecode::Op::Load>(iterator_reg);
  913. }
  914. generator.emit<Bytecode::Op::IteratorNext>();
  915. generator.emit<Bytecode::Op::Store>(temp_iterator_result_reg);
  916. generator.emit<Bytecode::Op::IteratorResultDone>();
  917. generator.emit<Bytecode::Op::Store>(is_iterator_exhausted_register);
  918. // We still have to check for exhaustion here. If the iterator is exhausted,
  919. // we need to bail before trying to get the value
  920. auto& no_bail_block = generator.make_block();
  921. generator.emit<Bytecode::Op::JumpConditional>().set_targets(
  922. Bytecode::Label { iterator_is_exhausted_block },
  923. Bytecode::Label { no_bail_block });
  924. generator.switch_to_basic_block(no_bail_block);
  925. // Get the next value in the iterator
  926. generator.emit<Bytecode::Op::Load>(temp_iterator_result_reg);
  927. generator.emit<Bytecode::Op::IteratorResultValue>();
  928. auto& create_binding_block = generator.make_block();
  929. generator.emit<Bytecode::Op::Jump>().set_targets(
  930. Bytecode::Label { create_binding_block },
  931. {});
  932. // The iterator is exhausted, so we just load undefined and continue binding
  933. generator.switch_to_basic_block(iterator_is_exhausted_block);
  934. generator.emit<Bytecode::Op::LoadImmediate>(js_undefined());
  935. generator.emit<Bytecode::Op::Jump>().set_targets(
  936. Bytecode::Label { create_binding_block },
  937. {});
  938. // Create the actual binding. The value which this entry must bind is now in the
  939. // accumulator. We can proceed, processing the alias as a nested destructuring
  940. // pattern if necessary.
  941. generator.switch_to_basic_block(create_binding_block);
  942. TRY(assign_accumulator_to_alias(alias));
  943. first = false;
  944. }
  945. return {};
  946. }
  947. static Bytecode::CodeGenerationErrorOr<void> generate_binding_pattern_bytecode(Bytecode::Generator& generator, BindingPattern const& pattern, Bytecode::Op::SetVariable::InitializationMode initialization_mode, Bytecode::Register const& value_reg)
  948. {
  949. if (pattern.kind == BindingPattern::Kind::Object)
  950. return generate_object_binding_pattern_bytecode(generator, pattern, initialization_mode, value_reg);
  951. return generate_array_binding_pattern_bytecode(generator, pattern, initialization_mode, value_reg);
  952. }
  953. Bytecode::CodeGenerationErrorOr<void> VariableDeclaration::generate_bytecode(Bytecode::Generator& generator) const
  954. {
  955. for (auto& declarator : m_declarations) {
  956. if (declarator.init())
  957. TRY(declarator.init()->generate_bytecode(generator));
  958. else
  959. generator.emit<Bytecode::Op::LoadImmediate>(js_undefined());
  960. auto initialization_mode = is_lexical_declaration() ? Bytecode::Op::SetVariable::InitializationMode::Initialize : Bytecode::Op::SetVariable::InitializationMode::Set;
  961. TRY(declarator.target().visit(
  962. [&](NonnullRefPtr<Identifier> const& id) -> Bytecode::CodeGenerationErrorOr<void> {
  963. generator.emit<Bytecode::Op::SetVariable>(generator.intern_identifier(id->string()), initialization_mode);
  964. return {};
  965. },
  966. [&](NonnullRefPtr<BindingPattern> const& pattern) -> Bytecode::CodeGenerationErrorOr<void> {
  967. auto value_register = generator.allocate_register();
  968. generator.emit<Bytecode::Op::Store>(value_register);
  969. return generate_binding_pattern_bytecode(generator, pattern, initialization_mode, value_register);
  970. }));
  971. }
  972. return {};
  973. }
  974. Bytecode::CodeGenerationErrorOr<void> CallExpression::generate_bytecode(Bytecode::Generator& generator) const
  975. {
  976. auto callee_reg = generator.allocate_register();
  977. auto this_reg = generator.allocate_register();
  978. generator.emit<Bytecode::Op::LoadImmediate>(js_undefined());
  979. generator.emit<Bytecode::Op::Store>(this_reg);
  980. if (is<NewExpression>(this)) {
  981. TRY(m_callee->generate_bytecode(generator));
  982. generator.emit<Bytecode::Op::Store>(callee_reg);
  983. } else if (is<SuperExpression>(*m_callee)) {
  984. return Bytecode::CodeGenerationError {
  985. this,
  986. "Unimplemented callee kind: SuperExpression"sv,
  987. };
  988. } else if (is<MemberExpression>(*m_callee)) {
  989. auto& member_expression = static_cast<const MemberExpression&>(*m_callee);
  990. if (is<SuperExpression>(member_expression.object())) {
  991. return Bytecode::CodeGenerationError {
  992. this,
  993. "Unimplemented callee kind: MemberExpression on SuperExpression"sv,
  994. };
  995. }
  996. TRY(member_expression.object().generate_bytecode(generator));
  997. generator.emit<Bytecode::Op::Store>(this_reg);
  998. if (member_expression.is_computed()) {
  999. TRY(member_expression.property().generate_bytecode(generator));
  1000. generator.emit<Bytecode::Op::GetByValue>(this_reg);
  1001. } else {
  1002. auto identifier_table_ref = generator.intern_identifier(verify_cast<Identifier>(member_expression.property()).string());
  1003. generator.emit<Bytecode::Op::GetById>(identifier_table_ref);
  1004. }
  1005. generator.emit<Bytecode::Op::Store>(callee_reg);
  1006. } else {
  1007. // FIXME: this = global object in sloppy mode.
  1008. TRY(m_callee->generate_bytecode(generator));
  1009. generator.emit<Bytecode::Op::Store>(callee_reg);
  1010. }
  1011. Vector<Bytecode::Register> argument_registers;
  1012. for (auto& arg : m_arguments) {
  1013. TRY(arg.value->generate_bytecode(generator));
  1014. auto arg_reg = generator.allocate_register();
  1015. generator.emit<Bytecode::Op::Store>(arg_reg);
  1016. argument_registers.append(arg_reg);
  1017. }
  1018. Bytecode::Op::Call::CallType call_type;
  1019. if (is<NewExpression>(*this)) {
  1020. call_type = Bytecode::Op::Call::CallType::Construct;
  1021. } else {
  1022. call_type = Bytecode::Op::Call::CallType::Call;
  1023. }
  1024. generator.emit_with_extra_register_slots<Bytecode::Op::Call>(argument_registers.size(), call_type, callee_reg, this_reg, argument_registers);
  1025. return {};
  1026. }
  1027. Bytecode::CodeGenerationErrorOr<void> ReturnStatement::generate_bytecode(Bytecode::Generator& generator) const
  1028. {
  1029. if (m_argument)
  1030. TRY(m_argument->generate_bytecode(generator));
  1031. if (generator.is_in_generator_or_async_function())
  1032. generator.emit<Bytecode::Op::Yield>(nullptr);
  1033. else
  1034. generator.emit<Bytecode::Op::Return>();
  1035. return {};
  1036. }
  1037. Bytecode::CodeGenerationErrorOr<void> YieldExpression::generate_bytecode(Bytecode::Generator& generator) const
  1038. {
  1039. VERIFY(generator.is_in_generator_function());
  1040. if (m_is_yield_from) {
  1041. return Bytecode::CodeGenerationError {
  1042. this,
  1043. "Unimplemented form: `yield*`"sv,
  1044. };
  1045. }
  1046. if (m_argument)
  1047. TRY(m_argument->generate_bytecode(generator));
  1048. auto& continuation_block = generator.make_block();
  1049. generator.emit<Bytecode::Op::Yield>(Bytecode::Label { continuation_block });
  1050. generator.switch_to_basic_block(continuation_block);
  1051. return {};
  1052. }
  1053. Bytecode::CodeGenerationErrorOr<void> IfStatement::generate_bytecode(Bytecode::Generator& generator) const
  1054. {
  1055. // test
  1056. // jump if_true (true) true (false) false
  1057. // true
  1058. // jump always (true) end
  1059. // false
  1060. // jump always (true) end
  1061. // end
  1062. auto& true_block = generator.make_block();
  1063. auto& false_block = generator.make_block();
  1064. TRY(m_predicate->generate_bytecode(generator));
  1065. generator.emit<Bytecode::Op::JumpConditional>().set_targets(
  1066. Bytecode::Label { true_block },
  1067. Bytecode::Label { false_block });
  1068. Bytecode::Op::Jump* true_block_jump { nullptr };
  1069. generator.switch_to_basic_block(true_block);
  1070. generator.emit<Bytecode::Op::LoadImmediate>(js_undefined());
  1071. TRY(m_consequent->generate_bytecode(generator));
  1072. if (!generator.is_current_block_terminated())
  1073. true_block_jump = &generator.emit<Bytecode::Op::Jump>();
  1074. generator.switch_to_basic_block(false_block);
  1075. auto& end_block = generator.make_block();
  1076. generator.emit<Bytecode::Op::LoadImmediate>(js_undefined());
  1077. if (m_alternate)
  1078. TRY(m_alternate->generate_bytecode(generator));
  1079. if (!generator.is_current_block_terminated())
  1080. generator.emit<Bytecode::Op::Jump>().set_targets(Bytecode::Label { end_block }, {});
  1081. if (true_block_jump)
  1082. true_block_jump->set_targets(Bytecode::Label { end_block }, {});
  1083. generator.switch_to_basic_block(end_block);
  1084. return {};
  1085. }
  1086. Bytecode::CodeGenerationErrorOr<void> ContinueStatement::generate_bytecode(Bytecode::Generator& generator) const
  1087. {
  1088. generator.emit<Bytecode::Op::Jump>().set_targets(
  1089. generator.nearest_continuable_scope(),
  1090. {});
  1091. return {};
  1092. }
  1093. Bytecode::CodeGenerationErrorOr<void> DebuggerStatement::generate_bytecode(Bytecode::Generator&) const
  1094. {
  1095. return {};
  1096. }
  1097. Bytecode::CodeGenerationErrorOr<void> ConditionalExpression::generate_bytecode(Bytecode::Generator& generator) const
  1098. {
  1099. // test
  1100. // jump if_true (true) true (false) false
  1101. // true
  1102. // jump always (true) end
  1103. // false
  1104. // jump always (true) end
  1105. // end
  1106. auto& true_block = generator.make_block();
  1107. auto& false_block = generator.make_block();
  1108. auto& end_block = generator.make_block();
  1109. TRY(m_test->generate_bytecode(generator));
  1110. generator.emit<Bytecode::Op::JumpConditional>().set_targets(
  1111. Bytecode::Label { true_block },
  1112. Bytecode::Label { false_block });
  1113. generator.switch_to_basic_block(true_block);
  1114. TRY(m_consequent->generate_bytecode(generator));
  1115. generator.emit<Bytecode::Op::Jump>().set_targets(
  1116. Bytecode::Label { end_block },
  1117. {});
  1118. generator.switch_to_basic_block(false_block);
  1119. TRY(m_alternate->generate_bytecode(generator));
  1120. generator.emit<Bytecode::Op::Jump>().set_targets(
  1121. Bytecode::Label { end_block },
  1122. {});
  1123. generator.switch_to_basic_block(end_block);
  1124. return {};
  1125. }
  1126. Bytecode::CodeGenerationErrorOr<void> SequenceExpression::generate_bytecode(Bytecode::Generator& generator) const
  1127. {
  1128. for (auto& expression : m_expressions)
  1129. TRY(expression.generate_bytecode(generator));
  1130. return {};
  1131. }
  1132. Bytecode::CodeGenerationErrorOr<void> TemplateLiteral::generate_bytecode(Bytecode::Generator& generator) const
  1133. {
  1134. auto string_reg = generator.allocate_register();
  1135. for (size_t i = 0; i < m_expressions.size(); i++) {
  1136. TRY(m_expressions[i].generate_bytecode(generator));
  1137. if (i == 0) {
  1138. generator.emit<Bytecode::Op::Store>(string_reg);
  1139. } else {
  1140. generator.emit<Bytecode::Op::ConcatString>(string_reg);
  1141. }
  1142. }
  1143. generator.emit<Bytecode::Op::Load>(string_reg);
  1144. return {};
  1145. }
  1146. Bytecode::CodeGenerationErrorOr<void> TaggedTemplateLiteral::generate_bytecode(Bytecode::Generator& generator) const
  1147. {
  1148. TRY(m_tag->generate_bytecode(generator));
  1149. auto tag_reg = generator.allocate_register();
  1150. generator.emit<Bytecode::Op::Store>(tag_reg);
  1151. Vector<Bytecode::Register> string_regs;
  1152. auto& expressions = m_template_literal->expressions();
  1153. for (size_t i = 0; i < expressions.size(); ++i) {
  1154. if (i % 2 != 0)
  1155. continue;
  1156. TRY(expressions[i].generate_bytecode(generator));
  1157. auto string_reg = generator.allocate_register();
  1158. generator.emit<Bytecode::Op::Store>(string_reg);
  1159. string_regs.append(string_reg);
  1160. }
  1161. generator.emit_with_extra_register_slots<Bytecode::Op::NewArray>(string_regs.size(), string_regs);
  1162. auto strings_reg = generator.allocate_register();
  1163. generator.emit<Bytecode::Op::Store>(strings_reg);
  1164. Vector<Bytecode::Register> argument_regs;
  1165. argument_regs.append(strings_reg);
  1166. for (size_t i = 0; i < expressions.size(); ++i) {
  1167. if (i % 2 == 0)
  1168. continue;
  1169. TRY(expressions[i].generate_bytecode(generator));
  1170. auto string_reg = generator.allocate_register();
  1171. generator.emit<Bytecode::Op::Store>(string_reg);
  1172. argument_regs.append(string_reg);
  1173. }
  1174. Vector<Bytecode::Register> raw_string_regs;
  1175. for (auto& raw_string : m_template_literal->raw_strings()) {
  1176. TRY(raw_string.generate_bytecode(generator));
  1177. auto raw_string_reg = generator.allocate_register();
  1178. generator.emit<Bytecode::Op::Store>(raw_string_reg);
  1179. raw_string_regs.append(raw_string_reg);
  1180. }
  1181. generator.emit_with_extra_register_slots<Bytecode::Op::NewArray>(raw_string_regs.size(), raw_string_regs);
  1182. auto raw_strings_reg = generator.allocate_register();
  1183. generator.emit<Bytecode::Op::Store>(raw_strings_reg);
  1184. generator.emit<Bytecode::Op::Load>(strings_reg);
  1185. generator.emit<Bytecode::Op::PutById>(raw_strings_reg, generator.intern_identifier("raw"));
  1186. generator.emit<Bytecode::Op::LoadImmediate>(js_undefined());
  1187. auto this_reg = generator.allocate_register();
  1188. generator.emit<Bytecode::Op::Store>(this_reg);
  1189. generator.emit_with_extra_register_slots<Bytecode::Op::Call>(argument_regs.size(), Bytecode::Op::Call::CallType::Call, tag_reg, this_reg, move(argument_regs));
  1190. return {};
  1191. }
  1192. Bytecode::CodeGenerationErrorOr<void> UpdateExpression::generate_bytecode(Bytecode::Generator& generator) const
  1193. {
  1194. TRY(generator.emit_load_from_reference(*m_argument));
  1195. Optional<Bytecode::Register> previous_value_for_postfix_reg;
  1196. if (!m_prefixed) {
  1197. previous_value_for_postfix_reg = generator.allocate_register();
  1198. generator.emit<Bytecode::Op::Store>(*previous_value_for_postfix_reg);
  1199. }
  1200. if (m_op == UpdateOp::Increment)
  1201. generator.emit<Bytecode::Op::Increment>();
  1202. else
  1203. generator.emit<Bytecode::Op::Decrement>();
  1204. TRY(generator.emit_store_to_reference(*m_argument));
  1205. if (!m_prefixed)
  1206. generator.emit<Bytecode::Op::Load>(*previous_value_for_postfix_reg);
  1207. return {};
  1208. }
  1209. Bytecode::CodeGenerationErrorOr<void> ThrowStatement::generate_bytecode(Bytecode::Generator& generator) const
  1210. {
  1211. TRY(m_argument->generate_bytecode(generator));
  1212. generator.emit<Bytecode::Op::Throw>();
  1213. return {};
  1214. }
  1215. Bytecode::CodeGenerationErrorOr<void> BreakStatement::generate_bytecode(Bytecode::Generator& generator) const
  1216. {
  1217. generator.emit<Bytecode::Op::Jump>().set_targets(
  1218. generator.nearest_breakable_scope(),
  1219. {});
  1220. return {};
  1221. }
  1222. Bytecode::CodeGenerationErrorOr<void> TryStatement::generate_bytecode(Bytecode::Generator& generator) const
  1223. {
  1224. auto& saved_block = generator.current_block();
  1225. Optional<Bytecode::Label> handler_target;
  1226. Optional<Bytecode::Label> finalizer_target;
  1227. Bytecode::BasicBlock* next_block { nullptr };
  1228. if (m_finalizer) {
  1229. auto& finalizer_block = generator.make_block();
  1230. generator.switch_to_basic_block(finalizer_block);
  1231. TRY(m_finalizer->generate_bytecode(generator));
  1232. if (!generator.is_current_block_terminated()) {
  1233. next_block = &generator.make_block();
  1234. auto next_target = Bytecode::Label { *next_block };
  1235. generator.emit<Bytecode::Op::ContinuePendingUnwind>(next_target);
  1236. }
  1237. finalizer_target = Bytecode::Label { finalizer_block };
  1238. }
  1239. if (m_handler) {
  1240. auto& handler_block = generator.make_block();
  1241. generator.switch_to_basic_block(handler_block);
  1242. TRY(m_handler->parameter().visit(
  1243. [&](FlyString const& parameter) -> Bytecode::CodeGenerationErrorOr<void> {
  1244. if (!parameter.is_empty()) {
  1245. // FIXME: We need a separate DeclarativeEnvironment here
  1246. generator.emit<Bytecode::Op::SetVariable>(generator.intern_identifier(parameter));
  1247. }
  1248. return {};
  1249. },
  1250. [&](NonnullRefPtr<BindingPattern> const&) -> Bytecode::CodeGenerationErrorOr<void> {
  1251. // FIXME: Implement this path when the above DeclarativeEnvironment issue is dealt with.
  1252. return Bytecode::CodeGenerationError {
  1253. this,
  1254. "Unimplemented catch argument: BindingPattern"sv,
  1255. };
  1256. }));
  1257. TRY(m_handler->body().generate_bytecode(generator));
  1258. handler_target = Bytecode::Label { handler_block };
  1259. if (!generator.is_current_block_terminated()) {
  1260. if (m_finalizer) {
  1261. generator.emit<Bytecode::Op::LeaveUnwindContext>();
  1262. generator.emit<Bytecode::Op::Jump>(finalizer_target);
  1263. } else {
  1264. VERIFY(!next_block);
  1265. next_block = &generator.make_block();
  1266. auto next_target = Bytecode::Label { *next_block };
  1267. generator.emit<Bytecode::Op::Jump>(next_target);
  1268. }
  1269. }
  1270. }
  1271. auto& target_block = generator.make_block();
  1272. generator.switch_to_basic_block(saved_block);
  1273. generator.emit<Bytecode::Op::EnterUnwindContext>(Bytecode::Label { target_block }, handler_target, finalizer_target);
  1274. generator.switch_to_basic_block(target_block);
  1275. TRY(m_block->generate_bytecode(generator));
  1276. if (!generator.is_current_block_terminated()) {
  1277. if (m_finalizer) {
  1278. generator.emit<Bytecode::Op::Jump>(finalizer_target);
  1279. } else {
  1280. auto& block = generator.make_block();
  1281. generator.emit<Bytecode::Op::FinishUnwind>(Bytecode::Label { block });
  1282. next_block = &block;
  1283. }
  1284. }
  1285. generator.switch_to_basic_block(next_block ? *next_block : saved_block);
  1286. return {};
  1287. }
  1288. Bytecode::CodeGenerationErrorOr<void> SwitchStatement::generate_bytecode(Bytecode::Generator& generator) const
  1289. {
  1290. auto discriminant_reg = generator.allocate_register();
  1291. TRY(m_discriminant->generate_bytecode(generator));
  1292. generator.emit<Bytecode::Op::Store>(discriminant_reg);
  1293. Vector<Bytecode::BasicBlock&> case_blocks;
  1294. Bytecode::BasicBlock* default_block { nullptr };
  1295. Bytecode::BasicBlock* next_test_block = &generator.make_block();
  1296. generator.emit<Bytecode::Op::Jump>().set_targets(Bytecode::Label { *next_test_block }, {});
  1297. for (auto& switch_case : m_cases) {
  1298. auto& case_block = generator.make_block();
  1299. if (switch_case.test()) {
  1300. generator.switch_to_basic_block(*next_test_block);
  1301. TRY(switch_case.test()->generate_bytecode(generator));
  1302. generator.emit<Bytecode::Op::StrictlyEquals>(discriminant_reg);
  1303. next_test_block = &generator.make_block();
  1304. generator.emit<Bytecode::Op::JumpConditional>().set_targets(Bytecode::Label { case_block }, Bytecode::Label { *next_test_block });
  1305. } else {
  1306. default_block = &case_block;
  1307. }
  1308. case_blocks.append(case_block);
  1309. }
  1310. generator.switch_to_basic_block(*next_test_block);
  1311. auto& end_block = generator.make_block();
  1312. if (default_block != nullptr) {
  1313. generator.emit<Bytecode::Op::Jump>().set_targets(Bytecode::Label { *default_block }, {});
  1314. } else {
  1315. generator.emit<Bytecode::Op::LoadImmediate>(js_undefined());
  1316. generator.emit<Bytecode::Op::Jump>().set_targets(Bytecode::Label { end_block }, {});
  1317. }
  1318. auto current_block = case_blocks.begin();
  1319. generator.begin_breakable_scope(Bytecode::Label { end_block });
  1320. for (auto& switch_case : m_cases) {
  1321. generator.switch_to_basic_block(*current_block);
  1322. generator.emit<Bytecode::Op::LoadImmediate>(js_undefined());
  1323. for (auto& statement : switch_case.children()) {
  1324. TRY(statement.generate_bytecode(generator));
  1325. }
  1326. if (!generator.is_current_block_terminated()) {
  1327. auto next_block = current_block;
  1328. next_block++;
  1329. if (next_block.is_end()) {
  1330. generator.emit<Bytecode::Op::Jump>().set_targets(Bytecode::Label { end_block }, {});
  1331. } else {
  1332. generator.emit<Bytecode::Op::Jump>().set_targets(Bytecode::Label { *next_block }, {});
  1333. }
  1334. }
  1335. current_block++;
  1336. }
  1337. generator.end_breakable_scope();
  1338. generator.switch_to_basic_block(end_block);
  1339. return {};
  1340. }
  1341. Bytecode::CodeGenerationErrorOr<void> ClassDeclaration::generate_bytecode(Bytecode::Generator& generator) const
  1342. {
  1343. TRY(m_class_expression->generate_bytecode(generator));
  1344. generator.emit<Bytecode::Op::SetVariable>(generator.intern_identifier(m_class_expression.ptr()->name()), Bytecode::Op::SetVariable::InitializationMode::Initialize);
  1345. return {};
  1346. }
  1347. Bytecode::CodeGenerationErrorOr<void> ClassExpression::generate_bytecode(Bytecode::Generator& generator) const
  1348. {
  1349. generator.emit<Bytecode::Op::NewClass>(*this);
  1350. return {};
  1351. }
  1352. Bytecode::CodeGenerationErrorOr<void> ThisExpression::generate_bytecode(Bytecode::Generator& generator) const
  1353. {
  1354. generator.emit<Bytecode::Op::ResolveThisBinding>();
  1355. return {};
  1356. }
  1357. Bytecode::CodeGenerationErrorOr<void> AwaitExpression::generate_bytecode(Bytecode::Generator& generator) const
  1358. {
  1359. VERIFY(generator.is_in_async_function());
  1360. // Transform `await expr` to `yield expr`
  1361. TRY(m_argument->generate_bytecode(generator));
  1362. auto& continuation_block = generator.make_block();
  1363. generator.emit<Bytecode::Op::Yield>(Bytecode::Label { continuation_block });
  1364. generator.switch_to_basic_block(continuation_block);
  1365. return {};
  1366. }
  1367. }