Generator.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625
  1. /*
  2. * Copyright (c) 2021-2024, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/TemporaryChange.h>
  7. #include <LibJS/AST.h>
  8. #include <LibJS/Bytecode/BasicBlock.h>
  9. #include <LibJS/Bytecode/Generator.h>
  10. #include <LibJS/Bytecode/Instruction.h>
  11. #include <LibJS/Bytecode/Op.h>
  12. #include <LibJS/Bytecode/Register.h>
  13. #include <LibJS/Runtime/VM.h>
  14. namespace JS::Bytecode {
  15. Generator::Generator()
  16. : m_string_table(make<StringTable>())
  17. , m_identifier_table(make<IdentifierTable>())
  18. , m_regex_table(make<RegexTable>())
  19. {
  20. }
  21. CodeGenerationErrorOr<NonnullGCPtr<Executable>> Generator::generate(VM& vm, ASTNode const& node, ReadonlySpan<FunctionParameter> parameters, FunctionKind enclosing_function_kind)
  22. {
  23. Generator generator;
  24. for (auto const& parameter : parameters) {
  25. if (auto const* identifier = parameter.binding.get_pointer<NonnullRefPtr<Identifier const>>();
  26. identifier && (*identifier)->is_local()) {
  27. generator.set_local_initialized((*identifier)->local_variable_index());
  28. }
  29. }
  30. generator.switch_to_basic_block(generator.make_block());
  31. SourceLocationScope scope(generator, node);
  32. generator.m_enclosing_function_kind = enclosing_function_kind;
  33. if (generator.is_in_generator_or_async_function()) {
  34. // Immediately yield with no value.
  35. auto& start_block = generator.make_block();
  36. generator.emit<Bytecode::Op::Yield>(Label { start_block }, generator.add_constant(js_undefined()));
  37. generator.switch_to_basic_block(start_block);
  38. // NOTE: This doesn't have to handle received throw/return completions, as GeneratorObject::resume_abrupt
  39. // will not enter the generator from the SuspendedStart state and immediately completes the generator.
  40. }
  41. auto last_value = TRY(node.generate_bytecode(generator));
  42. if (!generator.current_block().is_terminated() && last_value.has_value()) {
  43. generator.emit<Bytecode::Op::End>(last_value.value());
  44. }
  45. if (generator.is_in_generator_or_async_function()) {
  46. // Terminate all unterminated blocks with yield return
  47. for (auto& block : generator.m_root_basic_blocks) {
  48. if (block->is_terminated())
  49. continue;
  50. generator.switch_to_basic_block(*block);
  51. generator.emit<Bytecode::Op::Yield>(nullptr, generator.add_constant(js_undefined()));
  52. }
  53. }
  54. bool is_strict_mode = false;
  55. if (is<Program>(node))
  56. is_strict_mode = static_cast<Program const&>(node).is_strict_mode();
  57. else if (is<FunctionBody>(node))
  58. is_strict_mode = static_cast<FunctionBody const&>(node).in_strict_mode();
  59. else if (is<FunctionDeclaration>(node))
  60. is_strict_mode = static_cast<FunctionDeclaration const&>(node).is_strict_mode();
  61. else if (is<FunctionExpression>(node))
  62. is_strict_mode = static_cast<FunctionExpression const&>(node).is_strict_mode();
  63. auto executable = vm.heap().allocate_without_realm<Executable>(
  64. move(generator.m_identifier_table),
  65. move(generator.m_string_table),
  66. move(generator.m_regex_table),
  67. move(generator.m_constants),
  68. node.source_code(),
  69. generator.m_next_property_lookup_cache,
  70. generator.m_next_global_variable_cache,
  71. generator.m_next_environment_variable_cache,
  72. generator.m_next_register,
  73. move(generator.m_root_basic_blocks),
  74. is_strict_mode);
  75. return executable;
  76. }
  77. void Generator::grow(size_t additional_size)
  78. {
  79. VERIFY(m_current_basic_block);
  80. m_current_basic_block->grow(additional_size);
  81. }
  82. Register Generator::allocate_register()
  83. {
  84. VERIFY(m_next_register != NumericLimits<u32>::max());
  85. return Register { m_next_register++ };
  86. }
  87. Generator::SourceLocationScope::SourceLocationScope(Generator& generator, ASTNode const& node)
  88. : m_generator(generator)
  89. , m_previous_node(m_generator.m_current_ast_node)
  90. {
  91. m_generator.m_current_ast_node = &node;
  92. }
  93. Generator::SourceLocationScope::~SourceLocationScope()
  94. {
  95. m_generator.m_current_ast_node = m_previous_node;
  96. }
  97. Generator::UnwindContext::UnwindContext(Generator& generator, Optional<Label> finalizer)
  98. : m_generator(generator)
  99. , m_finalizer(finalizer)
  100. , m_previous_context(m_generator.m_current_unwind_context)
  101. {
  102. m_generator.m_current_unwind_context = this;
  103. }
  104. Generator::UnwindContext::~UnwindContext()
  105. {
  106. VERIFY(m_generator.m_current_unwind_context == this);
  107. m_generator.m_current_unwind_context = m_previous_context;
  108. }
  109. Label Generator::nearest_continuable_scope() const
  110. {
  111. return m_continuable_scopes.last().bytecode_target;
  112. }
  113. void Generator::block_declaration_instantiation(ScopeNode const& scope_node)
  114. {
  115. start_boundary(BlockBoundaryType::LeaveLexicalEnvironment);
  116. emit<Bytecode::Op::BlockDeclarationInstantiation>(scope_node);
  117. }
  118. void Generator::begin_variable_scope()
  119. {
  120. start_boundary(BlockBoundaryType::LeaveLexicalEnvironment);
  121. emit<Bytecode::Op::CreateLexicalEnvironment>();
  122. }
  123. void Generator::end_variable_scope()
  124. {
  125. end_boundary(BlockBoundaryType::LeaveLexicalEnvironment);
  126. if (!m_current_basic_block->is_terminated()) {
  127. emit<Bytecode::Op::LeaveLexicalEnvironment>();
  128. }
  129. }
  130. void Generator::begin_continuable_scope(Label continue_target, Vector<DeprecatedFlyString> const& language_label_set)
  131. {
  132. m_continuable_scopes.append({ continue_target, language_label_set });
  133. start_boundary(BlockBoundaryType::Continue);
  134. }
  135. void Generator::end_continuable_scope()
  136. {
  137. m_continuable_scopes.take_last();
  138. end_boundary(BlockBoundaryType::Continue);
  139. }
  140. Label Generator::nearest_breakable_scope() const
  141. {
  142. return m_breakable_scopes.last().bytecode_target;
  143. }
  144. void Generator::begin_breakable_scope(Label breakable_target, Vector<DeprecatedFlyString> const& language_label_set)
  145. {
  146. m_breakable_scopes.append({ breakable_target, language_label_set });
  147. start_boundary(BlockBoundaryType::Break);
  148. }
  149. void Generator::end_breakable_scope()
  150. {
  151. m_breakable_scopes.take_last();
  152. end_boundary(BlockBoundaryType::Break);
  153. }
  154. CodeGenerationErrorOr<Generator::ReferenceOperands> Generator::emit_super_reference(MemberExpression const& expression)
  155. {
  156. VERIFY(is<SuperExpression>(expression.object()));
  157. // https://tc39.es/ecma262/#sec-super-keyword-runtime-semantics-evaluation
  158. // 1. Let env be GetThisEnvironment().
  159. // 2. Let actualThis be ? env.GetThisBinding().
  160. auto actual_this = Operand(allocate_register());
  161. emit<Bytecode::Op::ResolveThisBinding>(actual_this);
  162. Optional<Bytecode::Operand> computed_property_value;
  163. if (expression.is_computed()) {
  164. // SuperProperty : super [ Expression ]
  165. // 3. Let propertyNameReference be ? Evaluation of Expression.
  166. // 4. Let propertyNameValue be ? GetValue(propertyNameReference).
  167. computed_property_value = TRY(expression.property().generate_bytecode(*this)).value();
  168. }
  169. // 5/7. Return ? MakeSuperPropertyReference(actualThis, propertyKey, strict).
  170. // https://tc39.es/ecma262/#sec-makesuperpropertyreference
  171. // 1. Let env be GetThisEnvironment().
  172. // 2. Assert: env.HasSuperBinding() is true.
  173. // 3. Let baseValue be ? env.GetSuperBase().
  174. auto base_value = Operand(allocate_register());
  175. emit<Bytecode::Op::ResolveSuperBase>(base_value);
  176. // 4. Return the Reference Record { [[Base]]: baseValue, [[ReferencedName]]: propertyKey, [[Strict]]: strict, [[ThisValue]]: actualThis }.
  177. return ReferenceOperands {
  178. .base = base_value,
  179. .referenced_name = computed_property_value,
  180. .this_value = actual_this,
  181. };
  182. }
  183. CodeGenerationErrorOr<Generator::ReferenceOperands> Generator::emit_load_from_reference(JS::ASTNode const& node, Optional<Operand> preferred_dst)
  184. {
  185. if (is<Identifier>(node)) {
  186. auto& identifier = static_cast<Identifier const&>(node);
  187. auto loaded_value = TRY(identifier.generate_bytecode(*this, preferred_dst)).value();
  188. return ReferenceOperands {
  189. .loaded_value = loaded_value,
  190. };
  191. }
  192. if (!is<MemberExpression>(node)) {
  193. return CodeGenerationError {
  194. &node,
  195. "Unimplemented/invalid node used as a reference"sv
  196. };
  197. }
  198. auto& expression = static_cast<MemberExpression const&>(node);
  199. // https://tc39.es/ecma262/#sec-super-keyword-runtime-semantics-evaluation
  200. if (is<SuperExpression>(expression.object())) {
  201. auto super_reference = TRY(emit_super_reference(expression));
  202. auto dst = preferred_dst.has_value() ? preferred_dst.value() : Operand(allocate_register());
  203. if (super_reference.referenced_name.has_value()) {
  204. // 5. Let propertyKey be ? ToPropertyKey(propertyNameValue).
  205. // FIXME: This does ToPropertyKey out of order, which is observable by Symbol.toPrimitive!
  206. emit<Bytecode::Op::GetByValueWithThis>(dst, *super_reference.base, *super_reference.referenced_name, *super_reference.this_value);
  207. } else {
  208. // 3. Let propertyKey be StringValue of IdentifierName.
  209. auto identifier_table_ref = intern_identifier(verify_cast<Identifier>(expression.property()).string());
  210. emit_get_by_id_with_this(dst, *super_reference.base, identifier_table_ref, *super_reference.this_value);
  211. }
  212. super_reference.loaded_value = dst;
  213. return super_reference;
  214. }
  215. auto base = TRY(expression.object().generate_bytecode(*this)).value();
  216. if (expression.is_computed()) {
  217. auto property = TRY(expression.property().generate_bytecode(*this)).value();
  218. auto dst = preferred_dst.has_value() ? preferred_dst.value() : Operand(allocate_register());
  219. emit<Bytecode::Op::GetByValue>(dst, base, property);
  220. return ReferenceOperands {
  221. .base = base,
  222. .referenced_name = property,
  223. .this_value = base,
  224. .loaded_value = dst,
  225. };
  226. }
  227. if (expression.property().is_identifier()) {
  228. auto identifier_table_ref = intern_identifier(verify_cast<Identifier>(expression.property()).string());
  229. auto dst = preferred_dst.has_value() ? preferred_dst.value() : Operand(allocate_register());
  230. emit_get_by_id(dst, base, identifier_table_ref);
  231. return ReferenceOperands {
  232. .base = base,
  233. .referenced_identifier = identifier_table_ref,
  234. .this_value = base,
  235. .loaded_value = dst,
  236. };
  237. }
  238. if (expression.property().is_private_identifier()) {
  239. auto identifier_table_ref = intern_identifier(verify_cast<PrivateIdentifier>(expression.property()).string());
  240. auto dst = preferred_dst.has_value() ? preferred_dst.value() : Operand(allocate_register());
  241. emit<Bytecode::Op::GetPrivateById>(dst, base, identifier_table_ref);
  242. return ReferenceOperands {
  243. .base = base,
  244. .referenced_private_identifier = identifier_table_ref,
  245. .this_value = base,
  246. .loaded_value = dst,
  247. };
  248. }
  249. return CodeGenerationError {
  250. &expression,
  251. "Unimplemented non-computed member expression"sv
  252. };
  253. }
  254. CodeGenerationErrorOr<void> Generator::emit_store_to_reference(JS::ASTNode const& node, Operand value)
  255. {
  256. if (is<Identifier>(node)) {
  257. auto& identifier = static_cast<Identifier const&>(node);
  258. emit_set_variable(identifier, value);
  259. return {};
  260. }
  261. if (is<MemberExpression>(node)) {
  262. auto& expression = static_cast<MemberExpression const&>(node);
  263. // https://tc39.es/ecma262/#sec-super-keyword-runtime-semantics-evaluation
  264. if (is<SuperExpression>(expression.object())) {
  265. auto super_reference = TRY(emit_super_reference(expression));
  266. // 4. Return the Reference Record { [[Base]]: baseValue, [[ReferencedName]]: propertyKey, [[Strict]]: strict, [[ThisValue]]: actualThis }.
  267. if (super_reference.referenced_name.has_value()) {
  268. // 5. Let propertyKey be ? ToPropertyKey(propertyNameValue).
  269. // FIXME: This does ToPropertyKey out of order, which is observable by Symbol.toPrimitive!
  270. emit<Bytecode::Op::PutByValueWithThis>(*super_reference.base, *super_reference.referenced_name, *super_reference.this_value, value);
  271. } else {
  272. // 3. Let propertyKey be StringValue of IdentifierName.
  273. auto identifier_table_ref = intern_identifier(verify_cast<Identifier>(expression.property()).string());
  274. emit<Bytecode::Op::PutByIdWithThis>(*super_reference.base, *super_reference.this_value, identifier_table_ref, value, Bytecode::Op::PropertyKind::KeyValue, next_property_lookup_cache());
  275. }
  276. } else {
  277. auto object = TRY(expression.object().generate_bytecode(*this)).value();
  278. if (expression.is_computed()) {
  279. auto property = TRY(expression.property().generate_bytecode(*this)).value();
  280. emit<Bytecode::Op::PutByValue>(object, property, value);
  281. } else if (expression.property().is_identifier()) {
  282. auto identifier_table_ref = intern_identifier(verify_cast<Identifier>(expression.property()).string());
  283. emit<Bytecode::Op::PutById>(object, identifier_table_ref, value, Bytecode::Op::PropertyKind::KeyValue, next_property_lookup_cache());
  284. } else if (expression.property().is_private_identifier()) {
  285. auto identifier_table_ref = intern_identifier(verify_cast<PrivateIdentifier>(expression.property()).string());
  286. emit<Bytecode::Op::PutPrivateById>(object, identifier_table_ref, value);
  287. } else {
  288. return CodeGenerationError {
  289. &expression,
  290. "Unimplemented non-computed member expression"sv
  291. };
  292. }
  293. }
  294. return {};
  295. }
  296. return CodeGenerationError {
  297. &node,
  298. "Unimplemented/invalid node used a reference"sv
  299. };
  300. }
  301. CodeGenerationErrorOr<void> Generator::emit_store_to_reference(ReferenceOperands const& reference, Operand value)
  302. {
  303. if (reference.referenced_private_identifier.has_value()) {
  304. emit<Bytecode::Op::PutPrivateById>(*reference.base, *reference.referenced_private_identifier, value);
  305. return {};
  306. }
  307. if (reference.referenced_identifier.has_value()) {
  308. if (reference.base == reference.this_value)
  309. emit<Bytecode::Op::PutById>(*reference.base, *reference.referenced_identifier, value, Bytecode::Op::PropertyKind::KeyValue, next_property_lookup_cache());
  310. else
  311. emit<Bytecode::Op::PutByIdWithThis>(*reference.base, *reference.this_value, *reference.referenced_identifier, value, Bytecode::Op::PropertyKind::KeyValue, next_property_lookup_cache());
  312. return {};
  313. }
  314. if (reference.base == reference.this_value)
  315. emit<Bytecode::Op::PutByValue>(*reference.base, *reference.referenced_name, value);
  316. else
  317. emit<Bytecode::Op::PutByValueWithThis>(*reference.base, *reference.referenced_name, *reference.this_value, value);
  318. return {};
  319. }
  320. CodeGenerationErrorOr<Optional<Operand>> Generator::emit_delete_reference(JS::ASTNode const& node)
  321. {
  322. if (is<Identifier>(node)) {
  323. auto& identifier = static_cast<Identifier const&>(node);
  324. if (identifier.is_local()) {
  325. return add_constant(Value(false));
  326. }
  327. auto dst = Operand(allocate_register());
  328. emit<Bytecode::Op::DeleteVariable>(dst, intern_identifier(identifier.string()));
  329. return dst;
  330. }
  331. if (is<MemberExpression>(node)) {
  332. auto& expression = static_cast<MemberExpression const&>(node);
  333. // https://tc39.es/ecma262/#sec-super-keyword-runtime-semantics-evaluation
  334. if (is<SuperExpression>(expression.object())) {
  335. auto super_reference = TRY(emit_super_reference(expression));
  336. auto dst = Operand(allocate_register());
  337. if (super_reference.referenced_name.has_value()) {
  338. emit<Bytecode::Op::DeleteByValueWithThis>(dst, *super_reference.base, *super_reference.this_value, *super_reference.referenced_name);
  339. } else {
  340. auto identifier_table_ref = intern_identifier(verify_cast<Identifier>(expression.property()).string());
  341. emit<Bytecode::Op::DeleteByIdWithThis>(dst, *super_reference.base, *super_reference.this_value, identifier_table_ref);
  342. }
  343. return Optional<Operand> {};
  344. }
  345. auto object = TRY(expression.object().generate_bytecode(*this)).value();
  346. auto dst = Operand(allocate_register());
  347. if (expression.is_computed()) {
  348. auto property = TRY(expression.property().generate_bytecode(*this)).value();
  349. emit<Bytecode::Op::DeleteByValue>(dst, object, property);
  350. } else if (expression.property().is_identifier()) {
  351. auto identifier_table_ref = intern_identifier(verify_cast<Identifier>(expression.property()).string());
  352. emit<Bytecode::Op::DeleteById>(dst, object, identifier_table_ref);
  353. } else {
  354. // NOTE: Trying to delete a private field generates a SyntaxError in the parser.
  355. return CodeGenerationError {
  356. &expression,
  357. "Unimplemented non-computed member expression"sv
  358. };
  359. }
  360. return dst;
  361. }
  362. // Though this will have no deletion effect, we still have to evaluate the node as it can have side effects.
  363. // For example: delete a(); delete ++c.b; etc.
  364. // 13.5.1.2 Runtime Semantics: Evaluation, https://tc39.es/ecma262/#sec-delete-operator-runtime-semantics-evaluation
  365. // 1. Let ref be the result of evaluating UnaryExpression.
  366. // 2. ReturnIfAbrupt(ref).
  367. (void)TRY(node.generate_bytecode(*this));
  368. // 3. If ref is not a Reference Record, return true.
  369. // NOTE: The rest of the steps are handled by Delete{Variable,ByValue,Id}.
  370. return add_constant(Value(true));
  371. }
  372. void Generator::emit_set_variable(JS::Identifier const& identifier, Operand value, Bytecode::Op::SetVariable::InitializationMode initialization_mode, Bytecode::Op::EnvironmentMode mode)
  373. {
  374. if (identifier.is_local()) {
  375. if (value.is_local() && value.index() == identifier.local_variable_index()) {
  376. // Moving a local to itself is a no-op.
  377. return;
  378. }
  379. emit<Bytecode::Op::SetLocal>(identifier.local_variable_index(), value);
  380. } else {
  381. emit<Bytecode::Op::SetVariable>(intern_identifier(identifier.string()), value, next_environment_variable_cache(), initialization_mode, mode);
  382. }
  383. }
  384. void Generator::generate_scoped_jump(JumpType type)
  385. {
  386. TemporaryChange temp { m_current_unwind_context, m_current_unwind_context };
  387. bool last_was_finally = false;
  388. for (size_t i = m_boundaries.size(); i > 0; --i) {
  389. auto boundary = m_boundaries[i - 1];
  390. using enum BlockBoundaryType;
  391. switch (boundary) {
  392. case Break:
  393. if (type == JumpType::Break) {
  394. emit<Op::Jump>(nearest_breakable_scope());
  395. return;
  396. }
  397. break;
  398. case Continue:
  399. if (type == JumpType::Continue) {
  400. emit<Op::Jump>(nearest_continuable_scope());
  401. return;
  402. }
  403. break;
  404. case Unwind:
  405. VERIFY(last_was_finally || !m_current_unwind_context->finalizer().has_value());
  406. if (!last_was_finally) {
  407. VERIFY(m_current_unwind_context && m_current_unwind_context->handler().has_value());
  408. emit<Bytecode::Op::LeaveUnwindContext>();
  409. m_current_unwind_context = m_current_unwind_context->previous();
  410. }
  411. last_was_finally = false;
  412. break;
  413. case LeaveLexicalEnvironment:
  414. emit<Bytecode::Op::LeaveLexicalEnvironment>();
  415. break;
  416. case ReturnToFinally: {
  417. VERIFY(m_current_unwind_context->finalizer().has_value());
  418. m_current_unwind_context = m_current_unwind_context->previous();
  419. auto jump_type_name = type == JumpType::Break ? "break"sv : "continue"sv;
  420. auto block_name = MUST(String::formatted("{}.{}", current_block().name(), jump_type_name));
  421. auto& block = make_block(block_name);
  422. emit<Op::ScheduleJump>(Label { block });
  423. switch_to_basic_block(block);
  424. last_was_finally = true;
  425. break;
  426. };
  427. }
  428. }
  429. VERIFY_NOT_REACHED();
  430. }
  431. void Generator::generate_labelled_jump(JumpType type, DeprecatedFlyString const& label)
  432. {
  433. TemporaryChange temp { m_current_unwind_context, m_current_unwind_context };
  434. size_t current_boundary = m_boundaries.size();
  435. bool last_was_finally = false;
  436. auto const& jumpable_scopes = type == JumpType::Continue ? m_continuable_scopes : m_breakable_scopes;
  437. for (auto const& jumpable_scope : jumpable_scopes.in_reverse()) {
  438. for (; current_boundary > 0; --current_boundary) {
  439. auto boundary = m_boundaries[current_boundary - 1];
  440. if (boundary == BlockBoundaryType::Unwind) {
  441. VERIFY(last_was_finally || !m_current_unwind_context->finalizer().has_value());
  442. if (!last_was_finally) {
  443. VERIFY(m_current_unwind_context && m_current_unwind_context->handler().has_value());
  444. emit<Bytecode::Op::LeaveUnwindContext>();
  445. m_current_unwind_context = m_current_unwind_context->previous();
  446. }
  447. last_was_finally = false;
  448. } else if (boundary == BlockBoundaryType::LeaveLexicalEnvironment) {
  449. emit<Bytecode::Op::LeaveLexicalEnvironment>();
  450. } else if (boundary == BlockBoundaryType::ReturnToFinally) {
  451. VERIFY(m_current_unwind_context->finalizer().has_value());
  452. m_current_unwind_context = m_current_unwind_context->previous();
  453. auto jump_type_name = type == JumpType::Break ? "break"sv : "continue"sv;
  454. auto block_name = MUST(String::formatted("{}.{}", current_block().name(), jump_type_name));
  455. auto& block = make_block(block_name);
  456. emit<Op::ScheduleJump>(Label { block });
  457. switch_to_basic_block(block);
  458. last_was_finally = true;
  459. } else if ((type == JumpType::Continue && boundary == BlockBoundaryType::Continue) || (type == JumpType::Break && boundary == BlockBoundaryType::Break)) {
  460. // Make sure we don't process this boundary twice if the current jumpable scope doesn't contain the target label.
  461. --current_boundary;
  462. break;
  463. }
  464. }
  465. if (jumpable_scope.language_label_set.contains_slow(label)) {
  466. emit<Op::Jump>(jumpable_scope.bytecode_target);
  467. return;
  468. }
  469. }
  470. // We must have a jumpable scope available that contains the label, as this should be enforced by the parser.
  471. VERIFY_NOT_REACHED();
  472. }
  473. void Generator::generate_break()
  474. {
  475. generate_scoped_jump(JumpType::Break);
  476. }
  477. void Generator::generate_break(DeprecatedFlyString const& break_label)
  478. {
  479. generate_labelled_jump(JumpType::Break, break_label);
  480. }
  481. void Generator::generate_continue()
  482. {
  483. generate_scoped_jump(JumpType::Continue);
  484. }
  485. void Generator::generate_continue(DeprecatedFlyString const& continue_label)
  486. {
  487. generate_labelled_jump(JumpType::Continue, continue_label);
  488. }
  489. void Generator::push_home_object(Operand object)
  490. {
  491. m_home_objects.append(object);
  492. }
  493. void Generator::pop_home_object()
  494. {
  495. m_home_objects.take_last();
  496. }
  497. void Generator::emit_new_function(Operand dst, FunctionExpression const& function_node, Optional<IdentifierTableIndex> lhs_name)
  498. {
  499. if (m_home_objects.is_empty()) {
  500. emit<Op::NewFunction>(dst, function_node, lhs_name);
  501. } else {
  502. emit<Op::NewFunction>(dst, function_node, lhs_name, m_home_objects.last());
  503. }
  504. }
  505. CodeGenerationErrorOr<Optional<Operand>> Generator::emit_named_evaluation_if_anonymous_function(Expression const& expression, Optional<IdentifierTableIndex> lhs_name, Optional<Operand> preferred_dst)
  506. {
  507. if (is<FunctionExpression>(expression)) {
  508. auto const& function_expression = static_cast<FunctionExpression const&>(expression);
  509. if (!function_expression.has_name()) {
  510. return TRY(function_expression.generate_bytecode_with_lhs_name(*this, move(lhs_name), preferred_dst)).value();
  511. }
  512. }
  513. if (is<ClassExpression>(expression)) {
  514. auto const& class_expression = static_cast<ClassExpression const&>(expression);
  515. if (!class_expression.has_name()) {
  516. return TRY(class_expression.generate_bytecode_with_lhs_name(*this, move(lhs_name), preferred_dst)).value();
  517. }
  518. }
  519. return expression.generate_bytecode(*this, preferred_dst);
  520. }
  521. void Generator::emit_get_by_id(Operand dst, Operand base, IdentifierTableIndex id)
  522. {
  523. emit<Op::GetById>(dst, base, id, m_next_property_lookup_cache++);
  524. }
  525. void Generator::emit_get_by_id_with_this(Operand dst, Operand base, IdentifierTableIndex id, Operand this_value)
  526. {
  527. emit<Op::GetByIdWithThis>(dst, base, id, this_value, m_next_property_lookup_cache++);
  528. }
  529. void Generator::emit_iterator_value(Operand dst, Operand result)
  530. {
  531. emit_get_by_id(dst, result, intern_identifier("value"sv));
  532. }
  533. void Generator::emit_iterator_complete(Operand dst, Operand result)
  534. {
  535. emit_get_by_id(dst, result, intern_identifier("done"sv));
  536. }
  537. bool Generator::is_local_initialized(u32 local_index) const
  538. {
  539. return m_initialized_locals.find(local_index) != m_initialized_locals.end();
  540. }
  541. void Generator::set_local_initialized(u32 local_index)
  542. {
  543. m_initialized_locals.set(local_index);
  544. }
  545. }