Generator.cpp 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. #include <LibJS/Forward.h>
  13. namespace JS::Bytecode {
  14. Generator::Generator()
  15. : m_string_table(make<StringTable>())
  16. {
  17. }
  18. Generator::~Generator()
  19. {
  20. }
  21. Executable Generator::generate(ASTNode const& node)
  22. {
  23. Generator generator;
  24. generator.switch_to_basic_block(generator.make_block());
  25. node.generate_bytecode(generator);
  26. return { move(generator.m_root_basic_blocks), move(generator.m_string_table), generator.m_next_register };
  27. }
  28. void Generator::grow(size_t additional_size)
  29. {
  30. VERIFY(m_current_basic_block);
  31. m_current_basic_block->grow(additional_size);
  32. }
  33. void* Generator::next_slot()
  34. {
  35. VERIFY(m_current_basic_block);
  36. return m_current_basic_block->next_slot();
  37. }
  38. Register Generator::allocate_register()
  39. {
  40. VERIFY(m_next_register != NumericLimits<u32>::max());
  41. return Register { m_next_register++ };
  42. }
  43. Label Generator::nearest_continuable_scope() const
  44. {
  45. return m_continuable_scopes.last();
  46. }
  47. void Generator::begin_continuable_scope(Label continue_target)
  48. {
  49. m_continuable_scopes.append(continue_target);
  50. }
  51. void Generator::end_continuable_scope()
  52. {
  53. m_continuable_scopes.take_last();
  54. }
  55. Label Generator::nearest_breakable_scope() const
  56. {
  57. return m_breakable_scopes.last();
  58. }
  59. void Generator::begin_breakable_scope(Label breakable_target)
  60. {
  61. m_breakable_scopes.append(breakable_target);
  62. }
  63. void Generator::end_breakable_scope()
  64. {
  65. m_breakable_scopes.take_last();
  66. }
  67. }