Generator.cpp 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /*
  2. * Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/OwnPtr.h>
  7. #include <LibJS/AST.h>
  8. #include <LibJS/Bytecode/Block.h>
  9. #include <LibJS/Bytecode/Generator.h>
  10. #include <LibJS/Bytecode/Instruction.h>
  11. #include <LibJS/Bytecode/Register.h>
  12. #include <LibJS/Forward.h>
  13. namespace JS::Bytecode {
  14. Generator::Generator()
  15. {
  16. m_block = Block::create();
  17. }
  18. Generator::~Generator()
  19. {
  20. }
  21. OwnPtr<Block> Generator::generate(ASTNode const& node)
  22. {
  23. Generator generator;
  24. [[maybe_unused]] auto dummy = node.generate_bytecode(generator);
  25. generator.m_block->set_register_count({}, generator.m_next_register);
  26. generator.m_block->seal();
  27. return move(generator.m_block);
  28. }
  29. void Generator::grow(size_t additional_size)
  30. {
  31. m_block->grow(additional_size);
  32. }
  33. void* Generator::next_slot()
  34. {
  35. return m_block->next_slot();
  36. }
  37. Register Generator::allocate_register()
  38. {
  39. VERIFY(m_next_register != NumericLimits<u32>::max());
  40. return Register { m_next_register++ };
  41. }
  42. Label Generator::make_label() const
  43. {
  44. return Label { m_block->instruction_stream().size() };
  45. }
  46. Label Generator::nearest_continuable_scope() const
  47. {
  48. return m_continuable_scopes.last();
  49. }
  50. void Generator::begin_continuable_scope()
  51. {
  52. m_continuable_scopes.append(make_label());
  53. }
  54. void Generator::end_continuable_scope()
  55. {
  56. m_continuable_scopes.take_last();
  57. }
  58. }