Generator.cpp 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. namespace JS::Bytecode {
  13. Generator::Generator()
  14. {
  15. m_block = Block::create();
  16. }
  17. Generator::~Generator()
  18. {
  19. }
  20. OwnPtr<Block> Generator::generate(ASTNode const& node)
  21. {
  22. Generator generator;
  23. node.generate_bytecode(generator);
  24. generator.m_block->set_register_count({}, generator.m_next_register);
  25. return move(generator.m_block);
  26. }
  27. Register Generator::allocate_register()
  28. {
  29. VERIFY(m_next_register != NumericLimits<u32>::max());
  30. return Register { m_next_register++ };
  31. }
  32. Label Generator::make_label() const
  33. {
  34. return Label { m_block->instruction_stream().size() };
  35. }
  36. Label Generator::nearest_continuable_scope() const
  37. {
  38. return m_continuable_scopes.last();
  39. }
  40. void Generator::begin_continuable_scope()
  41. {
  42. m_continuable_scopes.append(make_label());
  43. }
  44. void Generator::end_continuable_scope()
  45. {
  46. m_continuable_scopes.take_last();
  47. }
  48. }