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 #ifndef V8_CODEGEN_CODE_REFERENCE_H_ 6 #define V8_CODEGEN_CODE_REFERENCE_H_ 7 8 #include "src/handles/handles.h" 9 #include "src/objects/code.h" 10 11 namespace v8 { 12 namespace internal { 13 14 class Code; 15 class CodeDesc; 16 17 namespace wasm { 18 class WasmCode; 19 } // namespace wasm 20 21 class CodeReference { 22 public: CodeReference()23 CodeReference() : kind_(Kind::NONE), null_(nullptr) {} CodeReference(const wasm::WasmCode * wasm_code)24 explicit CodeReference(const wasm::WasmCode* wasm_code) 25 : kind_(Kind::WASM), wasm_code_(wasm_code) {} CodeReference(const CodeDesc * code_desc)26 explicit CodeReference(const CodeDesc* code_desc) 27 : kind_(Kind::CODE_DESC), code_desc_(code_desc) {} CodeReference(Handle<Code> js_code)28 explicit CodeReference(Handle<Code> js_code) 29 : kind_(Kind::JS), js_code_(js_code) {} 30 31 Address constant_pool() const; 32 Address instruction_start() const; 33 Address instruction_end() const; 34 int instruction_size() const; 35 const byte* relocation_start() const; 36 const byte* relocation_end() const; 37 int relocation_size() const; 38 Address code_comments() const; 39 int code_comments_size() const; 40 is_null()41 bool is_null() const { return kind_ == Kind::NONE; } is_js()42 bool is_js() const { return kind_ == Kind::JS; } is_wasm_code()43 bool is_wasm_code() const { return kind_ == Kind::WASM; } 44 as_js_code()45 Handle<Code> as_js_code() const { 46 DCHECK_EQ(Kind::JS, kind_); 47 return js_code_; 48 } 49 as_wasm_code()50 const wasm::WasmCode* as_wasm_code() const { 51 DCHECK_EQ(Kind::WASM, kind_); 52 return wasm_code_; 53 } 54 55 private: 56 enum class Kind { NONE, JS, WASM, CODE_DESC } kind_; 57 union { 58 std::nullptr_t null_; 59 const wasm::WasmCode* wasm_code_; 60 const CodeDesc* code_desc_; 61 Handle<Code> js_code_; 62 }; 63 64 DISALLOW_NEW_AND_DELETE() 65 }; 66 ASSERT_TRIVIALLY_COPYABLE(CodeReference); 67 68 } // namespace internal 69 } // namespace v8 70 71 #endif // V8_CODEGEN_CODE_REFERENCE_H_ 72