1// Copyright 2018 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "src/code-reference.h"
6
7#include "src/code-desc.h"
8#include "src/globals.h"
9#include "src/handles-inl.h"
10#include "src/objects-inl.h"
11#include "src/wasm/wasm-code-manager.h"
12
13namespace v8 {
14namespace internal {
15
16namespace {
17struct JSOps {
18 Handle<Code> code;
19
20 Address constant_pool() const { return code->constant_pool(); }
21 Address instruction_start() const { return code->InstructionStart(); }
22 Address instruction_end() const { return code->InstructionEnd(); }
23 int instruction_size() const { return code->InstructionSize(); }
24 const byte* relocation_start() const { return code->relocation_start(); }
25 const byte* relocation_end() const { return code->relocation_end(); }
26 int relocation_size() const { return code->relocation_size(); }
27 Address code_comments() const { return code->code_comments(); }
28 int code_comments_size() const { return code->code_comments_size(); }
29};
30
31struct WasmOps {
32 const wasm::WasmCode* code;
33
34 Address constant_pool() const { return code->constant_pool(); }
35 Address instruction_start() const {
36 return reinterpret_cast<Address>(code->instructions().start());
37 }
38 Address instruction_end() const {
39 return reinterpret_cast<Address>(code->instructions().start() +
40 code->instructions().size());
41 }
42 int instruction_size() const { return code->instructions().length(); }
43 const byte* relocation_start() const { return code->reloc_info().start(); }
44 const byte* relocation_end() const {
45 return code->reloc_info().start() + code->reloc_info().length();
46 }
47 int relocation_size() const { return code->reloc_info().length(); }
48 Address code_comments() const { return code->code_comments(); }
49 int code_comments_size() const { return code->code_comments_size(); }
50};
51
52struct CodeDescOps {
53 const CodeDesc* code_desc;
54
55 Address constant_pool() const {
56 return instruction_start() + code_desc->constant_pool_offset;
57 }
58 Address instruction_start() const {
59 return reinterpret_cast<Address>(code_desc->buffer);
60 }
61 Address instruction_end() const {
62 return instruction_start() + code_desc->instr_size;
63 }
64 int instruction_size() const { return code_desc->instr_size; }
65 const byte* relocation_start() const {
66 return code_desc->buffer + code_desc->reloc_offset;
67 }
68 const byte* relocation_end() const {
69 return code_desc->buffer + code_desc->buffer_size;
70 }
71 int relocation_size() const { return code_desc->reloc_size; }
72 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} // namespace
78
79#define DISPATCH(ret, method) \
80 ret CodeReference::method() const { \
81 DCHECK(!is_null()); \
82 switch (kind_) { \
83 case JS: \
84 return JSOps{js_code_}.method(); \
85 case WASM: \
86 return WasmOps{wasm_code_}.method(); \
87 case CODE_DESC: \
88 return CodeDescOps{code_desc_}.method(); \
89 default: \
90 UNREACHABLE(); \
91 } \
92 }
93
94DISPATCH(Address, constant_pool)
95DISPATCH(Address, instruction_start)
96DISPATCH(Address, instruction_end)
97DISPATCH(int, instruction_size)
98DISPATCH(const byte*, relocation_start)
99DISPATCH(const byte*, relocation_end)
100DISPATCH(int, relocation_size)
101DISPATCH(Address, code_comments)
102DISPATCH(int, code_comments_size)
103
104#undef DISPATCH
105
106} // namespace internal
107} // namespace v8
108