Generator.cpp 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. {
  16. }
  17. Generator::~Generator()
  18. {
  19. }
  20. Executable Generator::generate(ASTNode const& node, bool is_in_generator_function)
  21. {
  22. Generator generator;
  23. generator.switch_to_basic_block(generator.make_block());
  24. if (is_in_generator_function) {
  25. generator.enter_generator_context();
  26. // Immediately yield with no value.
  27. auto& start_block = generator.make_block();
  28. generator.emit<Bytecode::Op::Yield>(Label { start_block });
  29. generator.switch_to_basic_block(start_block);
  30. }
  31. node.generate_bytecode(generator);
  32. if (is_in_generator_function) {
  33. // Terminate all unterminated blocks with yield return
  34. for (auto& block : generator.m_root_basic_blocks) {
  35. if (block.is_terminated())
  36. continue;
  37. generator.switch_to_basic_block(block);
  38. generator.emit<Bytecode::Op::LoadImmediate>(js_undefined());
  39. generator.emit<Bytecode::Op::Yield>(nullptr);
  40. }
  41. }
  42. return { move(generator.m_root_basic_blocks), move(generator.m_string_table), generator.m_next_register };
  43. }
  44. void Generator::grow(size_t additional_size)
  45. {
  46. VERIFY(m_current_basic_block);
  47. m_current_basic_block->grow(additional_size);
  48. }
  49. void* Generator::next_slot()
  50. {
  51. VERIFY(m_current_basic_block);
  52. return m_current_basic_block->next_slot();
  53. }
  54. Register Generator::allocate_register()
  55. {
  56. VERIFY(m_next_register != NumericLimits<u32>::max());
  57. return Register { m_next_register++ };
  58. }
  59. Label Generator::nearest_continuable_scope() const
  60. {
  61. return m_continuable_scopes.last();
  62. }
  63. void Generator::begin_continuable_scope(Label continue_target)
  64. {
  65. m_continuable_scopes.append(continue_target);
  66. }
  67. void Generator::end_continuable_scope()
  68. {
  69. m_continuable_scopes.take_last();
  70. }
  71. Label Generator::nearest_breakable_scope() const
  72. {
  73. return m_breakable_scopes.last();
  74. }
  75. void Generator::begin_breakable_scope(Label breakable_target)
  76. {
  77. m_breakable_scopes.append(breakable_target);
  78. }
  79. void Generator::end_breakable_scope()
  80. {
  81. m_breakable_scopes.take_last();
  82. }
  83. }