code-reference.cc 3.81 KB
Newer Older
1 2 3 4
// Copyright 2018 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5
#include "src/codegen/code-reference.h"
6

7
#include "src/codegen/code-desc.h"
8
#include "src/common/globals.h"
9
#include "src/handles/handles-inl.h"
10
#include "src/objects/objects-inl.h"
11 12 13 14 15
#include "src/wasm/wasm-code-manager.h"

namespace v8 {
namespace internal {

16 17 18 19 20 21 22 23 24 25 26 27
namespace {
struct JSOps {
  Handle<Code> code;

  Address constant_pool() const { return code->constant_pool(); }
  Address instruction_start() const { return code->InstructionStart(); }
  Address instruction_end() const { return code->InstructionEnd(); }
  int instruction_size() const { return code->InstructionSize(); }
  const byte* relocation_start() const { return code->relocation_start(); }
  const byte* relocation_end() const { return code->relocation_end(); }
  int relocation_size() const { return code->relocation_size(); }
  Address code_comments() const { return code->code_comments(); }
28
  int code_comments_size() const { return code->code_comments_size(); }
29 30 31 32 33 34 35
};

struct WasmOps {
  const wasm::WasmCode* code;

  Address constant_pool() const { return code->constant_pool(); }
  Address instruction_start() const {
36
    return reinterpret_cast<Address>(code->instructions().begin());
37 38
  }
  Address instruction_end() const {
39
    return reinterpret_cast<Address>(code->instructions().begin() +
40 41 42
                                     code->instructions().size());
  }
  int instruction_size() const { return code->instructions().length(); }
43
  const byte* relocation_start() const { return code->reloc_info().begin(); }
44
  const byte* relocation_end() const {
45
    return code->reloc_info().begin() + code->reloc_info().length();
46 47 48
  }
  int relocation_size() const { return code->reloc_info().length(); }
  Address code_comments() const { return code->code_comments(); }
49
  int code_comments_size() const { return code->code_comments_size(); }
50 51 52 53 54 55
};

struct CodeDescOps {
  const CodeDesc* code_desc;

  Address constant_pool() const {
56
    return instruction_start() + code_desc->constant_pool_offset;
57 58 59 60 61 62 63 64 65
  }
  Address instruction_start() const {
    return reinterpret_cast<Address>(code_desc->buffer);
  }
  Address instruction_end() const {
    return instruction_start() + code_desc->instr_size;
  }
  int instruction_size() const { return code_desc->instr_size; }
  const byte* relocation_start() const {
66
    return code_desc->buffer + code_desc->reloc_offset;
67 68 69 70 71 72
  }
  const byte* relocation_end() const {
    return code_desc->buffer + code_desc->buffer_size;
  }
  int relocation_size() const { return code_desc->reloc_size; }
  Address code_comments() const {
73
    return instruction_start() + code_desc->code_comments_offset;
74
  }
75
  int code_comments_size() const { return code_desc->code_comments_size; }
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
};
}  // namespace

#define DISPATCH(ret, method)                    \
  ret CodeReference::method() const {            \
    DCHECK(!is_null());                          \
    switch (kind_) {                             \
      case JS:                                   \
        return JSOps{js_code_}.method();         \
      case WASM:                                 \
        return WasmOps{wasm_code_}.method();     \
      case CODE_DESC:                            \
        return CodeDescOps{code_desc_}.method(); \
      default:                                   \
        UNREACHABLE();                           \
    }                                            \
  }

94 95 96 97 98 99 100 101
DISPATCH(Address, constant_pool)
DISPATCH(Address, instruction_start)
DISPATCH(Address, instruction_end)
DISPATCH(int, instruction_size)
DISPATCH(const byte*, relocation_start)
DISPATCH(const byte*, relocation_end)
DISPATCH(int, relocation_size)
DISPATCH(Address, code_comments)
102
DISPATCH(int, code_comments_size)
103 104

#undef DISPATCH
105

106 107
}  // namespace internal
}  // namespace v8