v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
instruction-stream.cc
Go to the documentation of this file.
1// Copyright 2023 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
6
13
14namespace v8 {
15namespace internal {
16
18 intptr_t delta) {
20 if (!TryGetCodeUnchecked(&code, kAcquireLoad)) return;
21 // This is called during evacuation and code.instruction_stream() will point
22 // to the old object. So pass *this directly to the RelocIterator.
23 for (WritableRelocIterator it(jit_allocation, *this, constant_pool(),
25 !it.done(); it.next()) {
26 it.rinfo()->apply(delta);
27 }
29}
30
31// This function performs the relocations but doesn't trigger any write barriers
32// yet. We skip the write barriers here with UNSAFE_SKIP_WRITE_BARRIER but the
33// caller needs to call RelocateFromDescWriteBarriers afterwards.
35 WritableJitAllocation& jit_allocation, Heap* heap, const CodeDesc& desc,
37 WriteBarrierPromise write_barrier_promise;
38 Assembler* origin = desc.origin;
39 const int mode_mask = RelocInfo::PostCodegenRelocationMask();
40 for (WritableRelocIterator it(jit_allocation, *this, constant_pool,
41 mode_mask);
42 !it.done(); it.next()) {
43 // IMPORTANT:
44 // this code needs be stay in sync with RelocateFromDescWriteBarriers below.
45
46 RelocInfo::Mode mode = it.rinfo()->rmode();
48 DirectHandle<HeapObject> p = it.rinfo()->target_object_handle(origin);
49 it.rinfo()->set_target_object(*this, *p, UNSAFE_SKIP_WRITE_BARRIER,
51 write_barrier_promise.RegisterAddress(it.rinfo()->pc());
52 } else if (RelocInfo::IsCodeTargetMode(mode)) {
53 // Rewrite code handles to direct pointers to the first instruction in the
54 // code object.
55 DirectHandle<HeapObject> p = it.rinfo()->target_object_handle(origin);
56 DCHECK(IsCode(*p));
57 Tagged<InstructionStream> target_istream =
58 Cast<Code>(*p)->instruction_stream();
59 it.rinfo()->set_target_address(*this, target_istream->instruction_start(),
62 write_barrier_promise.RegisterAddress(it.rinfo()->pc());
63 } else if (RelocInfo::IsNearBuiltinEntry(mode)) {
64 // Rewrite builtin IDs to PC-relative offset to the builtin entry point.
65 Builtin builtin = it.rinfo()->target_builtin_at(origin);
66 Address p = Builtins::EntryOf(builtin, heap->isolate());
67 // This won't trigger a write barrier, but setting mode to
68 // UPDATE_WRITE_BARRIER to make it clear that we didn't forget about it
69 // below.
70 it.rinfo()->set_target_address(*this, p, UPDATE_WRITE_BARRIER,
72 DCHECK_EQ(p, it.rinfo()->target_address());
73 } else if (RelocInfo::IsWasmStubCall(mode)) {
74#if V8_ENABLE_WEBASSEMBLY
75 // Map wasm stub id to builtin.
76 uint32_t stub_call_tag = it.rinfo()->wasm_call_tag();
77 DCHECK_LT(stub_call_tag,
78 static_cast<uint32_t>(Builtin::kFirstBytecodeHandler));
79 Builtin builtin = static_cast<Builtin>(stub_call_tag);
80 // Store the builtin address in relocation info.
81 Address entry = Builtins::EntryOf(builtin, heap->isolate());
82 it.rinfo()->set_wasm_stub_call_address(entry);
83#else
85#endif
86 } else {
87 intptr_t delta =
88 instruction_start() - reinterpret_cast<Address>(desc.buffer);
89 it.rinfo()->apply(delta);
90 }
91 }
92 return write_barrier_promise;
93}
94
96 Heap* heap, const CodeDesc& desc, Address constant_pool,
97 WriteBarrierPromise& write_barrier_promise,
98 const DisallowGarbageCollection& no_gc) {
99 const int mode_mask = RelocInfo::PostCodegenRelocationMask();
100 for (RelocIterator it(code(kAcquireLoad), mode_mask); !it.done(); it.next()) {
101 // IMPORTANT:
102 // this code needs be stay in sync with RelocateFromDesc above.
103
104 RelocInfo::Mode mode = it.rinfo()->rmode();
106 Tagged<HeapObject> p = it.rinfo()->target_object(heap->isolate());
108 write_barrier_promise.ResolveAddress(it.rinfo()->pc());
109 } else if (RelocInfo::IsCodeTargetMode(mode)) {
110 Tagged<InstructionStream> target_istream =
111 InstructionStream::FromTargetAddress(it.rinfo()->target_address());
112 WriteBarrier::ForRelocInfo(*this, it.rinfo(), target_istream,
114 write_barrier_promise.ResolveAddress(it.rinfo()->pc());
115 }
116 }
117}
118
119#ifdef DEBUG
121 DCHECK(delayed_write_barriers_.insert(address).second);
122}
123
125 DCHECK_EQ(delayed_write_barriers_.erase(address), 1);
126}
127InstructionStream::WriteBarrierPromise::~WriteBarrierPromise() {
128 DCHECK(delayed_write_barriers_.empty());
129}
130#endif
131
132} // namespace internal
133} // namespace v8
static Address EntryOf(Builtin builtin, Isolate *isolate)
Address address() const
void RelocateFromDescWriteBarriers(Heap *heap, const CodeDesc &desc, Address constant_pool, WriteBarrierPromise &promise, const DisallowGarbageCollection &no_gc)
static Tagged< InstructionStream > FromTargetAddress(Address address)
bool TryGetCodeUnchecked(Tagged< Code > *code_out, AcquireLoadTag tag) const
void Relocate(WritableJitAllocation &jit_allocation, intptr_t delta)
WriteBarrierPromise RelocateFromDesc(WritableJitAllocation &jit_allocation, Heap *heap, const CodeDesc &desc, Address constant_pool, const DisallowGarbageCollection &no_gc)
static const int kApplyMask
Definition reloc-info.h:369
static constexpr bool IsCodeTargetMode(Mode mode)
Definition reloc-info.h:197
static constexpr bool IsNearBuiltinEntry(Mode mode)
Definition reloc-info.h:247
static int PostCodegenRelocationMask()
Definition reloc-info.h:390
static constexpr bool IsWasmStubCall(Mode mode)
Definition reloc-info.h:214
static constexpr bool IsEmbeddedObjectMode(Mode mode)
Definition reloc-info.h:209
static void ForRelocInfo(Tagged< InstructionStream > host, RelocInfo *rinfo, Tagged< HeapObject > value, WriteBarrierMode mode=UPDATE_WRITE_BARRIER)
Handle< Code > code
@ UPDATE_WRITE_BARRIER
Definition objects.h:55
@ UNSAFE_SKIP_WRITE_BARRIER
Definition objects.h:53
void FlushInstructionCache(void *start, size_t size)
Tagged< To > Cast(Tagged< From > value, const v8::SourceLocation &loc=INIT_SOURCE_LOCATION_IN_DEBUG)
Definition casting.h:150
static constexpr AcquireLoadTag kAcquireLoad
Definition globals.h:2908
#define DCHECK(condition)
Definition logging.h:482
#define DCHECK_LT(v1, v2)
Definition logging.h:489
#define DCHECK_EQ(v1, v2)
Definition logging.h:485