Generator.cpp 7.0 KB

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