Executable.cpp 3.5 KB

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