11cb0ef41Sopenharmony_ci// Copyright 2018 the V8 project authors. All rights reserved.
21cb0ef41Sopenharmony_ci// Use of this source code is governed by a BSD-style license that can be
31cb0ef41Sopenharmony_ci// found in the LICENSE file.
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ci#include "src/codegen/code-reference.h"
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ci#include "src/codegen/code-desc.h"
81cb0ef41Sopenharmony_ci#include "src/common/globals.h"
91cb0ef41Sopenharmony_ci#include "src/handles/handles-inl.h"
101cb0ef41Sopenharmony_ci#include "src/objects/objects-inl.h"
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_ci#if V8_ENABLE_WEBASSEMBLY
131cb0ef41Sopenharmony_ci#include "src/wasm/wasm-code-manager.h"
141cb0ef41Sopenharmony_ci#endif  // V8_ENABLE_WEBASSEMBLY
151cb0ef41Sopenharmony_ci
161cb0ef41Sopenharmony_cinamespace v8 {
171cb0ef41Sopenharmony_cinamespace internal {
181cb0ef41Sopenharmony_ci
191cb0ef41Sopenharmony_cinamespace {
201cb0ef41Sopenharmony_cistruct JSOps {
211cb0ef41Sopenharmony_ci  Handle<Code> code;
221cb0ef41Sopenharmony_ci
231cb0ef41Sopenharmony_ci  Address constant_pool() const { return code->constant_pool(); }
241cb0ef41Sopenharmony_ci  Address instruction_start() const { return code->InstructionStart(); }
251cb0ef41Sopenharmony_ci  Address instruction_end() const { return code->InstructionEnd(); }
261cb0ef41Sopenharmony_ci  int instruction_size() const { return code->InstructionSize(); }
271cb0ef41Sopenharmony_ci  const byte* relocation_start() const { return code->relocation_start(); }
281cb0ef41Sopenharmony_ci  const byte* relocation_end() const { return code->relocation_end(); }
291cb0ef41Sopenharmony_ci  int relocation_size() const { return code->relocation_size(); }
301cb0ef41Sopenharmony_ci  Address code_comments() const { return code->code_comments(); }
311cb0ef41Sopenharmony_ci  int code_comments_size() const { return code->code_comments_size(); }
321cb0ef41Sopenharmony_ci};
331cb0ef41Sopenharmony_ci
341cb0ef41Sopenharmony_ci#if V8_ENABLE_WEBASSEMBLY
351cb0ef41Sopenharmony_cistruct WasmOps {
361cb0ef41Sopenharmony_ci  const wasm::WasmCode* code;
371cb0ef41Sopenharmony_ci
381cb0ef41Sopenharmony_ci  Address constant_pool() const { return code->constant_pool(); }
391cb0ef41Sopenharmony_ci  Address instruction_start() const {
401cb0ef41Sopenharmony_ci    return reinterpret_cast<Address>(code->instructions().begin());
411cb0ef41Sopenharmony_ci  }
421cb0ef41Sopenharmony_ci  Address instruction_end() const {
431cb0ef41Sopenharmony_ci    return reinterpret_cast<Address>(code->instructions().begin() +
441cb0ef41Sopenharmony_ci                                     code->instructions().size());
451cb0ef41Sopenharmony_ci  }
461cb0ef41Sopenharmony_ci  int instruction_size() const { return code->instructions().length(); }
471cb0ef41Sopenharmony_ci  const byte* relocation_start() const { return code->reloc_info().begin(); }
481cb0ef41Sopenharmony_ci  const byte* relocation_end() const {
491cb0ef41Sopenharmony_ci    return code->reloc_info().begin() + code->reloc_info().length();
501cb0ef41Sopenharmony_ci  }
511cb0ef41Sopenharmony_ci  int relocation_size() const { return code->reloc_info().length(); }
521cb0ef41Sopenharmony_ci  Address code_comments() const { return code->code_comments(); }
531cb0ef41Sopenharmony_ci  int code_comments_size() const { return code->code_comments_size(); }
541cb0ef41Sopenharmony_ci};
551cb0ef41Sopenharmony_ci#endif  // V8_ENABLE_WEBASSEMBLY
561cb0ef41Sopenharmony_ci
571cb0ef41Sopenharmony_cistruct CodeDescOps {
581cb0ef41Sopenharmony_ci  const CodeDesc* code_desc;
591cb0ef41Sopenharmony_ci
601cb0ef41Sopenharmony_ci  Address constant_pool() const {
611cb0ef41Sopenharmony_ci    return instruction_start() + code_desc->constant_pool_offset;
621cb0ef41Sopenharmony_ci  }
631cb0ef41Sopenharmony_ci  Address instruction_start() const {
641cb0ef41Sopenharmony_ci    return reinterpret_cast<Address>(code_desc->buffer);
651cb0ef41Sopenharmony_ci  }
661cb0ef41Sopenharmony_ci  Address instruction_end() const {
671cb0ef41Sopenharmony_ci    return instruction_start() + code_desc->instr_size;
681cb0ef41Sopenharmony_ci  }
691cb0ef41Sopenharmony_ci  int instruction_size() const { return code_desc->instr_size; }
701cb0ef41Sopenharmony_ci  const byte* relocation_start() const {
711cb0ef41Sopenharmony_ci    return code_desc->buffer + code_desc->reloc_offset;
721cb0ef41Sopenharmony_ci  }
731cb0ef41Sopenharmony_ci  const byte* relocation_end() const {
741cb0ef41Sopenharmony_ci    return code_desc->buffer + code_desc->buffer_size;
751cb0ef41Sopenharmony_ci  }
761cb0ef41Sopenharmony_ci  int relocation_size() const { return code_desc->reloc_size; }
771cb0ef41Sopenharmony_ci  Address code_comments() const {
781cb0ef41Sopenharmony_ci    return instruction_start() + code_desc->code_comments_offset;
791cb0ef41Sopenharmony_ci  }
801cb0ef41Sopenharmony_ci  int code_comments_size() const { return code_desc->code_comments_size; }
811cb0ef41Sopenharmony_ci};
821cb0ef41Sopenharmony_ci}  // namespace
831cb0ef41Sopenharmony_ci
841cb0ef41Sopenharmony_ci#if V8_ENABLE_WEBASSEMBLY
851cb0ef41Sopenharmony_ci#define DISPATCH(ret, method)                    \
861cb0ef41Sopenharmony_ci  ret CodeReference::method() const {            \
871cb0ef41Sopenharmony_ci    DCHECK(!is_null());                          \
881cb0ef41Sopenharmony_ci    switch (kind_) {                             \
891cb0ef41Sopenharmony_ci      case Kind::JS:                             \
901cb0ef41Sopenharmony_ci        return JSOps{js_code_}.method();         \
911cb0ef41Sopenharmony_ci      case Kind::WASM:                           \
921cb0ef41Sopenharmony_ci        return WasmOps{wasm_code_}.method();     \
931cb0ef41Sopenharmony_ci      case Kind::CODE_DESC:                      \
941cb0ef41Sopenharmony_ci        return CodeDescOps{code_desc_}.method(); \
951cb0ef41Sopenharmony_ci      default:                                   \
961cb0ef41Sopenharmony_ci        UNREACHABLE();                           \
971cb0ef41Sopenharmony_ci    }                                            \
981cb0ef41Sopenharmony_ci  }
991cb0ef41Sopenharmony_ci#else
1001cb0ef41Sopenharmony_ci#define DISPATCH(ret, method)                              \
1011cb0ef41Sopenharmony_ci  ret CodeReference::method() const {                      \
1021cb0ef41Sopenharmony_ci    DCHECK(!is_null());                                    \
1031cb0ef41Sopenharmony_ci    DCHECK(kind_ == Kind::JS || kind_ == Kind::CODE_DESC); \
1041cb0ef41Sopenharmony_ci    if (kind_ == Kind::JS) {                               \
1051cb0ef41Sopenharmony_ci      return JSOps{js_code_}.method();                     \
1061cb0ef41Sopenharmony_ci    } else {                                               \
1071cb0ef41Sopenharmony_ci      return CodeDescOps{code_desc_}.method();             \
1081cb0ef41Sopenharmony_ci    }                                                      \
1091cb0ef41Sopenharmony_ci  }
1101cb0ef41Sopenharmony_ci#endif  // V8_ENABLE_WEBASSEMBLY
1111cb0ef41Sopenharmony_ci
1121cb0ef41Sopenharmony_ciDISPATCH(Address, constant_pool)
1131cb0ef41Sopenharmony_ciDISPATCH(Address, instruction_start)
1141cb0ef41Sopenharmony_ciDISPATCH(Address, instruction_end)
1151cb0ef41Sopenharmony_ciDISPATCH(int, instruction_size)
1161cb0ef41Sopenharmony_ciDISPATCH(const byte*, relocation_start)
1171cb0ef41Sopenharmony_ciDISPATCH(const byte*, relocation_end)
1181cb0ef41Sopenharmony_ciDISPATCH(int, relocation_size)
1191cb0ef41Sopenharmony_ciDISPATCH(Address, code_comments)
1201cb0ef41Sopenharmony_ciDISPATCH(int, code_comments_size)
1211cb0ef41Sopenharmony_ci
1221cb0ef41Sopenharmony_ci#undef DISPATCH
1231cb0ef41Sopenharmony_ci
1241cb0ef41Sopenharmony_ci}  // namespace internal
1251cb0ef41Sopenharmony_ci}  // namespace v8
126