[cleanup] Move builtins related APIs from Heap class

... to Builtins class.

Bug: v8:12244, v8:11880
Change-Id: Ia96e476b904618b5fc45d2e401cedc2f67e36e7d
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/3245346
Reviewed-by: Jakob Gruber <jgruber@chromium.org>
Reviewed-by: Michael Lippautz <mlippautz@chromium.org>
Commit-Queue: Igor Sheludko <ishell@chromium.org>
Cr-Commit-Position: refs/heads/main@{#77571}
This commit is contained in:
Igor Sheludko 2021-10-26 17:59:06 +02:00 committed by V8 LUCI CQ
parent 103b3595c1
commit d9068f6c5d
13 changed files with 57 additions and 79 deletions

View File

@ -9209,7 +9209,7 @@ JSEntryStubs Isolate::GetJSEntryStubs() {
{i::Builtin::kJSRunMicrotasksEntry,
&entry_stubs.js_run_microtasks_entry_stub}}};
for (auto& pair : stubs) {
i::Code js_entry = isolate->heap()->builtin(pair.first);
i::Code js_entry = isolate->builtins()->code(pair.first);
pair.second->code.start =
reinterpret_cast<const void*>(js_entry.InstructionStart());
pair.second->code.length_in_bytes = js_entry.InstructionSize();

View File

@ -165,18 +165,33 @@ Handle<Code> Builtins::OrdinaryToPrimitive(OrdinaryToPrimitiveHint hint) {
UNREACHABLE();
}
void Builtins::set_code(Builtin builtin, Code code) {
DCHECK_EQ(builtin, code.builtin_id());
isolate_->heap()->set_builtin(builtin, code);
FullObjectSlot Builtins::builtin_slot(Builtin builtin) {
Address* location = &isolate_->builtin_table()[Builtins::ToInt(builtin)];
return FullObjectSlot(location);
}
Code Builtins::code(Builtin builtin_enum) {
return isolate_->heap()->builtin(builtin_enum);
FullObjectSlot Builtins::builtin_tier0_slot(Builtin builtin) {
DCHECK(IsTier0(builtin));
Address* location =
&isolate_->builtin_tier0_table()[Builtins::ToInt(builtin)];
return FullObjectSlot(location);
}
void Builtins::set_code(Builtin builtin, Code code) {
DCHECK_EQ(builtin, code.builtin_id());
DCHECK(Internals::HasHeapObjectTag(code.ptr()));
// The given builtin may be uninitialized thus we cannot check its type here.
isolate_->builtin_table()[Builtins::ToInt(builtin)] = code.ptr();
}
Code Builtins::code(Builtin builtin) {
Address ptr = isolate_->builtin_table()[Builtins::ToInt(builtin)];
return Code::cast(Object(ptr));
}
Handle<Code> Builtins::code_handle(Builtin builtin) {
return Handle<Code>(
reinterpret_cast<Address*>(isolate_->heap()->builtin_address(builtin)));
Address* location = &isolate_->builtin_table()[Builtins::ToInt(builtin)];
return Handle<Code>(location);
}
// static
@ -272,15 +287,12 @@ bool Builtins::IsBuiltin(const Code code) {
bool Builtins::IsBuiltinHandle(Handle<HeapObject> maybe_code,
Builtin* builtin) const {
Heap* heap = isolate_->heap();
Address handle_location = maybe_code.address();
Address end =
heap->builtin_address(static_cast<Builtin>(Builtins::kBuiltinCount));
if (handle_location >= end) return false;
Address start = heap->builtin_address(static_cast<Builtin>(0));
if (handle_location < start) return false;
*builtin = FromInt(static_cast<int>(handle_location - start) >>
kSystemPointerSizeLog2);
Address* handle_location = maybe_code.location();
Address* builtins_table = isolate_->builtin_table();
if (handle_location < builtins_table) return false;
Address* builtins_table_end = &builtins_table[Builtins::kBuiltinCount];
if (handle_location >= builtins_table_end) return false;
*builtin = FromInt(static_cast<int>(handle_location - builtins_table));
return true;
}
@ -298,8 +310,8 @@ void Builtins::InitializeIsolateDataTables(Isolate* isolate) {
// The entry table.
for (Builtin i = Builtins::kFirst; i <= Builtins::kLast; ++i) {
DCHECK(Builtins::IsBuiltinId(isolate->heap()->builtin(i).builtin_id()));
DCHECK(isolate->heap()->builtin(i).is_off_heap_trampoline());
DCHECK(Builtins::IsBuiltinId(isolate->builtins()->code(i).builtin_id()));
DCHECK(isolate->builtins()->code(i).is_off_heap_trampoline());
isolate_data->builtin_entry_table()[ToInt(i)] =
embedded_data.InstructionStartOfBuiltin(i);
}

View File

@ -276,6 +276,11 @@ class Builtins {
js_entry_handler_offset_ = offset;
}
// Returns given builtin's slot in the main builtin table.
FullObjectSlot builtin_slot(Builtin builtin);
// Returns given builtin's slot in the tier0 builtin table.
FullObjectSlot builtin_tier0_slot(Builtin builtin);
private:
static void Generate_CallFunction(MacroAssembler* masm,
ConvertReceiverMode mode);

View File

@ -199,8 +199,7 @@ ExternalReference ExternalReference::isolate_address(Isolate* isolate) {
}
ExternalReference ExternalReference::builtins_address(Isolate* isolate) {
return ExternalReference(
isolate->heap()->builtin_address(Builtins::FromInt(0)));
return ExternalReference(isolate->builtin_table());
}
ExternalReference ExternalReference::handle_scope_implementer_address(

View File

@ -718,7 +718,7 @@ void NativeFrame::ComputeCallerState(State* state) const {
}
Code EntryFrame::unchecked_code() const {
return isolate()->heap()->builtin(Builtin::kJSEntry);
return isolate()->builtins()->code(Builtin::kJSEntry);
}
void EntryFrame::ComputeCallerState(State* state) const {
@ -740,7 +740,7 @@ StackFrame::Type CWasmEntryFrame::GetCallerState(State* state) const {
#endif // V8_ENABLE_WEBASSEMBLY
Code ConstructEntryFrame::unchecked_code() const {
return isolate()->heap()->builtin(Builtin::kJSConstructEntry);
return isolate()->builtins()->code(Builtin::kJSConstructEntry);
}
void ExitFrame::ComputeCallerState(State* state) const {

View File

@ -393,7 +393,7 @@ size_t Isolate::HashIsolateForEmbeddedBlob() {
// Hash data sections of builtin code objects.
for (Builtin builtin = Builtins::kFirst; builtin <= Builtins::kLast;
++builtin) {
Code code = heap_.builtin(builtin);
Code code = builtins()->code(builtin);
DCHECK(Internals::HasHeapObjectTag(code.ptr()));
uint8_t* const code_ptr =
@ -3787,7 +3787,7 @@ bool Isolate::Init(SnapshotData* startup_snapshot_data,
// this at mksnapshot-time, but not at runtime.
// See also: https://crbug.com/v8/8713.
heap_.SetInterpreterEntryTrampolineForProfiling(
heap_.builtin(Builtin::kInterpreterEntryTrampoline));
builtins()->code(Builtin::kInterpreterEntryTrampoline));
#endif
builtins_constants_table_builder_->Finalize();

View File

@ -1141,6 +1141,9 @@ class V8_EXPORT_PRIVATE Isolate final : private HiddenFactory {
Address* builtin_entry_table() { return isolate_data_.builtin_entry_table(); }
V8_INLINE Address* builtin_table() { return isolate_data_.builtin_table(); }
V8_INLINE Address* builtin_tier0_table() {
return isolate_data_.builtin_tier0_table();
}
bool IsBuiltinTableHandleLocation(Address* handle_location);

View File

@ -1492,7 +1492,7 @@ Handle<WasmJSFunctionData> Factory::NewWasmJSFunctionData(
result.set_serialized_signature(*serialized_sig);
// Default value, will be overwritten by the caller.
result.set_wasm_to_js_wrapper_code(
isolate()->heap()->builtin(Builtin::kAbort));
isolate()->builtins()->code(Builtin::kAbort));
return handle(result, isolate());
}

View File

@ -111,11 +111,7 @@ base::EnumSet<CodeFlushMode> Heap::GetCodeFlushMode(Isolate* isolate) {
return code_flush_mode;
}
Isolate* Heap::isolate() {
return reinterpret_cast<Isolate*>(
reinterpret_cast<intptr_t>(this) -
reinterpret_cast<size_t>(reinterpret_cast<Isolate*>(16)->heap()) + 16);
}
Isolate* Heap::isolate() { return Isolate::FromHeap(this); }
int64_t Heap::external_memory() { return external_memory_.total(); }

View File

@ -4643,35 +4643,6 @@ void Heap::RegisterCodeObject(Handle<Code> code) {
}
}
// TODO(ishell): move builtin accessors out from Heap.
Code Heap::builtin(Builtin builtin) {
DCHECK(Builtins::IsBuiltinId(builtin));
return Code::cast(
Object(isolate()->builtin_table()[static_cast<int>(builtin)]));
}
Address Heap::builtin_address(Builtin builtin) {
const int index = Builtins::ToInt(builtin);
DCHECK(Builtins::IsBuiltinId(builtin) || index == Builtins::kBuiltinCount);
// Note: Must return an address within the full builtin_table for
// IterateBuiltins to work.
return reinterpret_cast<Address>(&isolate()->builtin_table()[index]);
}
Address Heap::builtin_tier0_address(Builtin builtin) {
const int index = static_cast<int>(builtin);
DCHECK(Builtins::IsBuiltinId(builtin) || index == Builtins::kBuiltinCount);
return reinterpret_cast<Address>(
&isolate()->isolate_data()->builtin_tier0_table()[index]);
}
void Heap::set_builtin(Builtin builtin, Code code) {
DCHECK(Builtins::IsBuiltinId(builtin));
DCHECK(Internals::HasHeapObjectTag(code.ptr()));
// The given builtin may be uninitialized thus we cannot check its type here.
isolate()->builtin_table()[Builtins::ToInt(builtin)] = code.ptr();
}
void Heap::IterateWeakRoots(RootVisitor* v, base::EnumSet<SkipRoot> options) {
DCHECK(!options.contains(SkipRoot::kWeak));
@ -4910,16 +4881,17 @@ void Heap::IterateWeakGlobalHandles(RootVisitor* v) {
}
void Heap::IterateBuiltins(RootVisitor* v) {
Builtins* builtins = isolate()->builtins();
for (Builtin builtin = Builtins::kFirst; builtin <= Builtins::kLast;
++builtin) {
v->VisitRootPointer(Root::kBuiltins, Builtins::name(builtin),
FullObjectSlot(builtin_address(builtin)));
builtins->builtin_slot(builtin));
}
for (Builtin builtin = Builtins::kFirst; builtin <= Builtins::kLastTier0;
++builtin) {
v->VisitRootPointer(Root::kBuiltins, Builtins::name(builtin),
FullObjectSlot(builtin_tier0_address(builtin)));
builtins->builtin_tier0_slot(builtin));
}
// The entry table doesn't need to be updated since all builtins are embedded.
@ -7035,7 +7007,7 @@ Code Heap::GcSafeFindCodeForInnerPointer(Address inner_pointer) {
Builtin maybe_builtin =
InstructionStream::TryLookupCode(isolate(), inner_pointer);
if (Builtins::IsBuiltinId(maybe_builtin)) {
return builtin(maybe_builtin);
return isolate()->builtins()->code(maybe_builtin);
}
if (V8_ENABLE_THIRD_PARTY_HEAP_BOOL) {

View File

@ -1044,15 +1044,6 @@ class Heap {
// Invoked when GC was requested via the stack guard.
void HandleGCRequest();
// ===========================================================================
// Builtins. =================================================================
// ===========================================================================
V8_EXPORT_PRIVATE Code builtin(Builtin builtin);
Address builtin_address(Builtin builtin);
Address builtin_tier0_address(Builtin builtin);
void set_builtin(Builtin builtin, Code code);
// ===========================================================================
// Iterators. ================================================================
// ===========================================================================

View File

@ -27219,18 +27219,18 @@ TEST(GetJSEntryStubs) {
v8::JSEntryStubs entry_stubs = isolate->GetJSEntryStubs();
v8::JSEntryStub entry_stub = entry_stubs.js_entry_stub;
CHECK_EQ(i_isolate->heap()->builtin(i::Builtin::kJSEntry).InstructionStart(),
CHECK_EQ(i_isolate->builtins()->code(i::Builtin::kJSEntry).InstructionStart(),
reinterpret_cast<i::Address>(entry_stub.code.start));
v8::JSEntryStub construct_stub = entry_stubs.js_construct_entry_stub;
CHECK_EQ(i_isolate->heap()
->builtin(i::Builtin::kJSConstructEntry)
CHECK_EQ(i_isolate->builtins()
->code(i::Builtin::kJSConstructEntry)
.InstructionStart(),
reinterpret_cast<i::Address>(construct_stub.code.start));
v8::JSEntryStub microtask_stub = entry_stubs.js_run_microtasks_entry_stub;
CHECK_EQ(i_isolate->heap()
->builtin(i::Builtin::kJSRunMicrotasksEntry)
CHECK_EQ(i_isolate->builtins()
->code(i::Builtin::kJSRunMicrotasksEntry)
.InstructionStart(),
reinterpret_cast<i::Address>(microtask_stub.code.start));
}

View File

@ -456,7 +456,7 @@ TEST(Unwind_JSEntry_Fail_CodePagesAPI) {
CHECK_LE(pages_length, arraysize(code_pages));
RegisterState register_state;
Code js_entry = i_isolate->heap()->builtin(Builtin::kJSEntry);
Code js_entry = i_isolate->builtins()->code(Builtin::kJSEntry);
byte* start = reinterpret_cast<byte*>(js_entry.InstructionStart());
register_state.pc = start + 10;
@ -638,7 +638,7 @@ TEST(PCIsInV8_InJSEntryRange_CodePagesAPI) {
isolate->CopyCodePages(arraysize(code_pages), code_pages);
CHECK_LE(pages_length, arraysize(code_pages));
Code js_entry = i_isolate->heap()->builtin(Builtin::kJSEntry);
Code js_entry = i_isolate->builtins()->code(Builtin::kJSEntry);
byte* start = reinterpret_cast<byte*>(js_entry.InstructionStart());
size_t length = js_entry.InstructionSize();