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#ifndef V8_CODEGEN_CODE_REFERENCE_H_
61cb0ef41Sopenharmony_ci#define V8_CODEGEN_CODE_REFERENCE_H_
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ci#include "src/handles/handles.h"
91cb0ef41Sopenharmony_ci#include "src/objects/code.h"
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_cinamespace v8 {
121cb0ef41Sopenharmony_cinamespace internal {
131cb0ef41Sopenharmony_ci
141cb0ef41Sopenharmony_ciclass Code;
151cb0ef41Sopenharmony_ciclass CodeDesc;
161cb0ef41Sopenharmony_ci
171cb0ef41Sopenharmony_cinamespace wasm {
181cb0ef41Sopenharmony_ciclass WasmCode;
191cb0ef41Sopenharmony_ci}  // namespace wasm
201cb0ef41Sopenharmony_ci
211cb0ef41Sopenharmony_ciclass CodeReference {
221cb0ef41Sopenharmony_ci public:
231cb0ef41Sopenharmony_ci  CodeReference() : kind_(Kind::NONE), null_(nullptr) {}
241cb0ef41Sopenharmony_ci  explicit CodeReference(const wasm::WasmCode* wasm_code)
251cb0ef41Sopenharmony_ci      : kind_(Kind::WASM), wasm_code_(wasm_code) {}
261cb0ef41Sopenharmony_ci  explicit CodeReference(const CodeDesc* code_desc)
271cb0ef41Sopenharmony_ci      : kind_(Kind::CODE_DESC), code_desc_(code_desc) {}
281cb0ef41Sopenharmony_ci  explicit CodeReference(Handle<Code> js_code)
291cb0ef41Sopenharmony_ci      : kind_(Kind::JS), js_code_(js_code) {}
301cb0ef41Sopenharmony_ci
311cb0ef41Sopenharmony_ci  Address constant_pool() const;
321cb0ef41Sopenharmony_ci  Address instruction_start() const;
331cb0ef41Sopenharmony_ci  Address instruction_end() const;
341cb0ef41Sopenharmony_ci  int instruction_size() const;
351cb0ef41Sopenharmony_ci  const byte* relocation_start() const;
361cb0ef41Sopenharmony_ci  const byte* relocation_end() const;
371cb0ef41Sopenharmony_ci  int relocation_size() const;
381cb0ef41Sopenharmony_ci  Address code_comments() const;
391cb0ef41Sopenharmony_ci  int code_comments_size() const;
401cb0ef41Sopenharmony_ci
411cb0ef41Sopenharmony_ci  bool is_null() const { return kind_ == Kind::NONE; }
421cb0ef41Sopenharmony_ci  bool is_js() const { return kind_ == Kind::JS; }
431cb0ef41Sopenharmony_ci  bool is_wasm_code() const { return kind_ == Kind::WASM; }
441cb0ef41Sopenharmony_ci
451cb0ef41Sopenharmony_ci  Handle<Code> as_js_code() const {
461cb0ef41Sopenharmony_ci    DCHECK_EQ(Kind::JS, kind_);
471cb0ef41Sopenharmony_ci    return js_code_;
481cb0ef41Sopenharmony_ci  }
491cb0ef41Sopenharmony_ci
501cb0ef41Sopenharmony_ci  const wasm::WasmCode* as_wasm_code() const {
511cb0ef41Sopenharmony_ci    DCHECK_EQ(Kind::WASM, kind_);
521cb0ef41Sopenharmony_ci    return wasm_code_;
531cb0ef41Sopenharmony_ci  }
541cb0ef41Sopenharmony_ci
551cb0ef41Sopenharmony_ci private:
561cb0ef41Sopenharmony_ci  enum class Kind { NONE, JS, WASM, CODE_DESC } kind_;
571cb0ef41Sopenharmony_ci  union {
581cb0ef41Sopenharmony_ci    std::nullptr_t null_;
591cb0ef41Sopenharmony_ci    const wasm::WasmCode* wasm_code_;
601cb0ef41Sopenharmony_ci    const CodeDesc* code_desc_;
611cb0ef41Sopenharmony_ci    Handle<Code> js_code_;
621cb0ef41Sopenharmony_ci  };
631cb0ef41Sopenharmony_ci
641cb0ef41Sopenharmony_ci  DISALLOW_NEW_AND_DELETE()
651cb0ef41Sopenharmony_ci};
661cb0ef41Sopenharmony_ciASSERT_TRIVIALLY_COPYABLE(CodeReference);
671cb0ef41Sopenharmony_ci
681cb0ef41Sopenharmony_ci}  // namespace internal
691cb0ef41Sopenharmony_ci}  // namespace v8
701cb0ef41Sopenharmony_ci
711cb0ef41Sopenharmony_ci#endif  // V8_CODEGEN_CODE_REFERENCE_H_
72