Executable.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * Copyright (c) 2021-2024, Andreas Kling <andreas@ladybird.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibJS/Bytecode/BasicBlock.h>
  7. #include <LibJS/Bytecode/Executable.h>
  8. #include <LibJS/Bytecode/Instruction.h>
  9. #include <LibJS/Bytecode/RegexTable.h>
  10. #include <LibJS/SourceCode.h>
  11. namespace JS::Bytecode {
  12. JS_DEFINE_ALLOCATOR(Executable);
  13. Executable::Executable(
  14. Vector<u8> bytecode,
  15. NonnullOwnPtr<IdentifierTable> identifier_table,
  16. NonnullOwnPtr<StringTable> string_table,
  17. NonnullOwnPtr<RegexTable> regex_table,
  18. Vector<Value> constants,
  19. NonnullRefPtr<SourceCode const> source_code,
  20. size_t number_of_property_lookup_caches,
  21. size_t number_of_global_variable_caches,
  22. size_t number_of_registers,
  23. bool is_strict_mode)
  24. : bytecode(move(bytecode))
  25. , string_table(move(string_table))
  26. , identifier_table(move(identifier_table))
  27. , regex_table(move(regex_table))
  28. , constants(move(constants))
  29. , source_code(move(source_code))
  30. , number_of_registers(number_of_registers)
  31. , is_strict_mode(is_strict_mode)
  32. {
  33. property_lookup_caches.resize(number_of_property_lookup_caches);
  34. global_variable_caches.resize(number_of_global_variable_caches);
  35. }
  36. Executable::~Executable() = default;
  37. void Executable::dump() const
  38. {
  39. warnln("\033[37;1mJS bytecode executable\033[0m \"{}\"", name);
  40. InstructionStreamIterator it(bytecode, this);
  41. size_t basic_block_offset_index = 0;
  42. while (!it.at_end()) {
  43. bool print_basic_block_marker = false;
  44. if (basic_block_offset_index < basic_block_start_offsets.size()
  45. && it.offset() == basic_block_start_offsets[basic_block_offset_index]) {
  46. ++basic_block_offset_index;
  47. print_basic_block_marker = true;
  48. }
  49. StringBuilder builder;
  50. builder.appendff("[{:4x}] ", it.offset());
  51. if (print_basic_block_marker)
  52. builder.appendff("{:4}: ", basic_block_offset_index - 1);
  53. else
  54. builder.append(" "sv);
  55. builder.append((*it).to_byte_string(*this));
  56. warnln("{}", builder.string_view());
  57. ++it;
  58. }
  59. if (!exception_handlers.is_empty()) {
  60. warnln("");
  61. warnln("Exception handlers:");
  62. for (auto& handlers : exception_handlers) {
  63. warnln(" from {:4x} to {:4x} handler {:4x} finalizer {:4x}",
  64. handlers.start_offset,
  65. handlers.end_offset,
  66. handlers.handler_offset.value_or(0),
  67. handlers.finalizer_offset.value_or(0));
  68. }
  69. }
  70. warnln("");
  71. }
  72. void Executable::visit_edges(Visitor& visitor)
  73. {
  74. Base::visit_edges(visitor);
  75. visitor.visit(constants);
  76. }
  77. Optional<Executable::ExceptionHandlers const&> Executable::exception_handlers_for_offset(size_t offset) const
  78. {
  79. for (auto& handlers : exception_handlers) {
  80. if (handlers.start_offset <= offset && offset < handlers.end_offset)
  81. return handlers;
  82. }
  83. return {};
  84. }
  85. UnrealizedSourceRange Executable::source_range_at(size_t offset) const
  86. {
  87. if (offset >= bytecode.size())
  88. return {};
  89. auto it = InstructionStreamIterator(bytecode.span().slice(offset), this);
  90. VERIFY(!it.at_end());
  91. auto mapping = source_map.get(offset);
  92. if (!mapping.has_value())
  93. return {};
  94. return UnrealizedSourceRange {
  95. .source_code = source_code,
  96. .start_offset = mapping->source_start_offset,
  97. .end_offset = mapping->source_end_offset,
  98. };
  99. }
  100. }