Generator.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /*
  2. * Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibJS/AST.h>
  7. #include <LibJS/Bytecode/BasicBlock.h>
  8. #include <LibJS/Bytecode/Generator.h>
  9. #include <LibJS/Bytecode/Instruction.h>
  10. #include <LibJS/Bytecode/Op.h>
  11. #include <LibJS/Bytecode/Register.h>
  12. namespace JS::Bytecode {
  13. Generator::Generator()
  14. : m_string_table(make<StringTable>())
  15. , m_identifier_table(make<IdentifierTable>())
  16. {
  17. }
  18. Generator::~Generator()
  19. {
  20. }
  21. CodeGenerationErrorOr<NonnullOwnPtr<Executable>> Generator::generate(ASTNode const& node, FunctionKind enclosing_function_kind)
  22. {
  23. Generator generator;
  24. generator.switch_to_basic_block(generator.make_block());
  25. generator.m_enclosing_function_kind = enclosing_function_kind;
  26. if (generator.is_in_generator_or_async_function()) {
  27. // Immediately yield with no value.
  28. auto& start_block = generator.make_block();
  29. generator.emit<Bytecode::Op::Yield>(Label { start_block });
  30. generator.switch_to_basic_block(start_block);
  31. }
  32. TRY(node.generate_bytecode(generator));
  33. if (generator.is_in_generator_or_async_function()) {
  34. // Terminate all unterminated blocks with yield return
  35. for (auto& block : generator.m_root_basic_blocks) {
  36. if (block.is_terminated())
  37. continue;
  38. generator.switch_to_basic_block(block);
  39. generator.emit<Bytecode::Op::LoadImmediate>(js_undefined());
  40. generator.emit<Bytecode::Op::Yield>(nullptr);
  41. }
  42. }
  43. return adopt_own(*new Executable {
  44. .name = {},
  45. .basic_blocks = move(generator.m_root_basic_blocks),
  46. .string_table = move(generator.m_string_table),
  47. .identifier_table = move(generator.m_identifier_table),
  48. .number_of_registers = generator.m_next_register });
  49. }
  50. void Generator::grow(size_t additional_size)
  51. {
  52. VERIFY(m_current_basic_block);
  53. m_current_basic_block->grow(additional_size);
  54. }
  55. void* Generator::next_slot()
  56. {
  57. VERIFY(m_current_basic_block);
  58. return m_current_basic_block->next_slot();
  59. }
  60. Register Generator::allocate_register()
  61. {
  62. VERIFY(m_next_register != NumericLimits<u32>::max());
  63. return Register { m_next_register++ };
  64. }
  65. Label Generator::nearest_continuable_scope() const
  66. {
  67. return m_continuable_scopes.last();
  68. }
  69. void Generator::begin_variable_scope(BindingMode mode, SurroundingScopeKind kind)
  70. {
  71. m_variable_scopes.append({ kind, mode, {} });
  72. if (mode != BindingMode::Global) {
  73. start_boundary(mode == BindingMode::Lexical ? BlockBoundaryType::LeaveLexicalEnvironment : BlockBoundaryType::LeaveVariableEnvironment);
  74. emit<Bytecode::Op::CreateEnvironment>(
  75. mode == BindingMode::Lexical
  76. ? Bytecode::Op::EnvironmentMode::Lexical
  77. : Bytecode::Op::EnvironmentMode::Var);
  78. }
  79. }
  80. void Generator::end_variable_scope()
  81. {
  82. auto mode = m_variable_scopes.take_last().mode;
  83. if (mode != BindingMode::Global) {
  84. end_boundary(mode == BindingMode::Lexical ? BlockBoundaryType::LeaveLexicalEnvironment : BlockBoundaryType::LeaveVariableEnvironment);
  85. if (!m_current_basic_block->is_terminated()) {
  86. emit<Bytecode::Op::LeaveEnvironment>(
  87. mode == BindingMode::Lexical
  88. ? Bytecode::Op::EnvironmentMode::Lexical
  89. : Bytecode::Op::EnvironmentMode::Var);
  90. }
  91. }
  92. }
  93. void Generator::begin_continuable_scope(Label continue_target)
  94. {
  95. m_continuable_scopes.append(continue_target);
  96. start_boundary(BlockBoundaryType::Continue);
  97. }
  98. void Generator::end_continuable_scope()
  99. {
  100. m_continuable_scopes.take_last();
  101. end_boundary(BlockBoundaryType::Continue);
  102. }
  103. Label Generator::nearest_breakable_scope() const
  104. {
  105. return m_breakable_scopes.last();
  106. }
  107. void Generator::begin_breakable_scope(Label breakable_target)
  108. {
  109. m_breakable_scopes.append(breakable_target);
  110. start_boundary(BlockBoundaryType::Break);
  111. }
  112. void Generator::end_breakable_scope()
  113. {
  114. m_breakable_scopes.take_last();
  115. end_boundary(BlockBoundaryType::Break);
  116. }
  117. CodeGenerationErrorOr<void> Generator::emit_load_from_reference(JS::ASTNode const& node)
  118. {
  119. if (is<Identifier>(node)) {
  120. auto& identifier = static_cast<Identifier const&>(node);
  121. emit<Bytecode::Op::GetVariable>(intern_identifier(identifier.string()));
  122. return {};
  123. }
  124. if (is<MemberExpression>(node)) {
  125. auto& expression = static_cast<MemberExpression const&>(node);
  126. TRY(expression.object().generate_bytecode(*this));
  127. auto object_reg = allocate_register();
  128. emit<Bytecode::Op::Store>(object_reg);
  129. if (expression.is_computed()) {
  130. TRY(expression.property().generate_bytecode(*this));
  131. emit<Bytecode::Op::GetByValue>(object_reg);
  132. } else if (expression.property().is_identifier()) {
  133. auto identifier_table_ref = intern_identifier(verify_cast<Identifier>(expression.property()).string());
  134. emit<Bytecode::Op::GetById>(identifier_table_ref);
  135. } else {
  136. return CodeGenerationError {
  137. &expression,
  138. "Unimplemented non-computed member expression"sv
  139. };
  140. }
  141. return {};
  142. }
  143. VERIFY_NOT_REACHED();
  144. }
  145. CodeGenerationErrorOr<void> Generator::emit_store_to_reference(JS::ASTNode const& node)
  146. {
  147. if (is<Identifier>(node)) {
  148. auto& identifier = static_cast<Identifier const&>(node);
  149. emit<Bytecode::Op::SetVariable>(intern_identifier(identifier.string()));
  150. return {};
  151. }
  152. if (is<MemberExpression>(node)) {
  153. // NOTE: The value is in the accumulator, so we have to store that away first.
  154. auto value_reg = allocate_register();
  155. emit<Bytecode::Op::Store>(value_reg);
  156. auto& expression = static_cast<MemberExpression const&>(node);
  157. TRY(expression.object().generate_bytecode(*this));
  158. auto object_reg = allocate_register();
  159. emit<Bytecode::Op::Store>(object_reg);
  160. if (expression.is_computed()) {
  161. TRY(expression.property().generate_bytecode(*this));
  162. auto property_reg = allocate_register();
  163. emit<Bytecode::Op::Store>(property_reg);
  164. emit<Bytecode::Op::Load>(value_reg);
  165. emit<Bytecode::Op::PutByValue>(object_reg, property_reg);
  166. } else if (expression.property().is_identifier()) {
  167. emit<Bytecode::Op::Load>(value_reg);
  168. auto identifier_table_ref = intern_identifier(verify_cast<Identifier>(expression.property()).string());
  169. emit<Bytecode::Op::PutById>(object_reg, identifier_table_ref);
  170. } else {
  171. return CodeGenerationError {
  172. &expression,
  173. "Unimplemented non-computed member expression"sv
  174. };
  175. }
  176. return {};
  177. }
  178. VERIFY_NOT_REACHED();
  179. }
  180. String CodeGenerationError::to_string()
  181. {
  182. return String::formatted("CodeGenerationError in {}: {}", failing_node ? failing_node->class_name() : "<unknown node>", reason_literal);
  183. }
  184. }