Generator.cpp 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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/Register.h>
  11. #include <LibJS/Forward.h>
  12. namespace JS::Bytecode {
  13. Generator::Generator()
  14. {
  15. }
  16. Generator::~Generator()
  17. {
  18. }
  19. ExecutionUnit Generator::generate(ASTNode const& node)
  20. {
  21. Generator generator;
  22. generator.switch_to_basic_block(generator.make_block());
  23. node.generate_bytecode(generator);
  24. return { move(generator.m_root_basic_blocks), generator.m_next_register };
  25. }
  26. void Generator::grow(size_t additional_size)
  27. {
  28. VERIFY(m_current_basic_block);
  29. m_current_basic_block->grow(additional_size);
  30. }
  31. void* Generator::next_slot()
  32. {
  33. VERIFY(m_current_basic_block);
  34. return m_current_basic_block->next_slot();
  35. }
  36. Register Generator::allocate_register()
  37. {
  38. VERIFY(m_next_register != NumericLimits<u32>::max());
  39. return Register { m_next_register++ };
  40. }
  41. Label Generator::nearest_continuable_scope() const
  42. {
  43. return m_continuable_scopes.last();
  44. }
  45. void Generator::begin_continuable_scope(Label continue_target)
  46. {
  47. m_continuable_scopes.append(continue_target);
  48. }
  49. void Generator::end_continuable_scope()
  50. {
  51. m_continuable_scopes.take_last();
  52. }
  53. }