mirror of
https://github.com/dart-lang/sdk
synced 2024-09-05 00:13:50 +00:00
![Vyacheslav Egorov](/assets/img/avatar_default.png)
This is the next step towards preventing compiler from directly peeking into runtime and instead interact with runtime through a well defined surface. The goal of the refactoring to locate all places where compiler accesses some runtime information and partion those accesses into two categories: - creating objects in the host runtime (e.g. allocating strings, numbers, etc) during compilation; - accessing properties of the target runtime (e.g. offsets of fields) to embed those into the generated code; This change introduces dart::compiler and dart::compiler::target namespaces. All code in the compiler will gradually be moved into dart::compiler namespace. One of the motivations for this change is to be able to prevent access to globally defined host constants like kWordSize by shadowing them in the dart::compiler namespace. The nested namespace dart::compiler::target hosts all information about target runtime that compiler could access, e.g. compiler::target::kWordSize defines word size of the target which will eventually be made different from the host kWordSize (defined by dart::kWordSize). The API for compiler to runtime interaction is placed into compiler_api.h. Note that we still permit runtime to access compiler internals directly - this is not going to be decoupled as part of this work. Issue https://github.com/dart-lang/sdk/issues/31709 Change-Id: If4396d295879391becfa6c38d4802bbff81f5b20 Reviewed-on: https://dart-review.googlesource.com/c/90242 Commit-Queue: Vyacheslav Egorov <vegorov@google.com> Reviewed-by: Martin Kustermann <kustermann@google.com>
106 lines
3.2 KiB
C++
106 lines
3.2 KiB
C++
// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
|
|
// for details. All rights reserved. Use of this source code is governed by a
|
|
// BSD-style license that can be found in the LICENSE file.
|
|
|
|
#include "vm/handles.h"
|
|
|
|
#include "platform/assert.h"
|
|
#include "platform/utils.h"
|
|
#include "vm/dart_api_state.h"
|
|
#include "vm/flags.h"
|
|
#include "vm/os.h"
|
|
#include "vm/raw_object.h"
|
|
#include "vm/visitor.h"
|
|
#include "vm/zone.h"
|
|
|
|
#include "vm/handles_impl.h"
|
|
|
|
namespace dart {
|
|
|
|
DEFINE_FLAG(bool, verify_handles, false, "Verify handles.");
|
|
|
|
VMHandles::~VMHandles() {
|
|
if (FLAG_trace_handles) {
|
|
OS::PrintErr("*** Handle Counts for 0x(%" Px "):Zone = %d,Scoped = %d\n",
|
|
reinterpret_cast<intptr_t>(this), CountZoneHandles(),
|
|
CountScopedHandles());
|
|
OS::PrintErr("*** Deleting VM handle block 0x%" Px "\n",
|
|
reinterpret_cast<intptr_t>(this));
|
|
}
|
|
}
|
|
|
|
void VMHandles::VisitObjectPointers(ObjectPointerVisitor* visitor) {
|
|
return Handles<kVMHandleSizeInWords, kVMHandlesPerChunk,
|
|
kOffsetOfRawPtr>::VisitObjectPointers(visitor);
|
|
}
|
|
|
|
#if defined(DEBUG)
|
|
static bool IsCurrentApiNativeScope(Zone* zone) {
|
|
ApiNativeScope* scope = ApiNativeScope::Current();
|
|
return (scope != NULL) && (scope->zone() == zone);
|
|
}
|
|
#endif // DEBUG
|
|
|
|
uword VMHandles::AllocateHandle(Zone* zone) {
|
|
DEBUG_ASSERT(!IsCurrentApiNativeScope(zone));
|
|
return Handles<kVMHandleSizeInWords, kVMHandlesPerChunk,
|
|
kOffsetOfRawPtr>::AllocateHandle(zone);
|
|
}
|
|
|
|
uword VMHandles::AllocateZoneHandle(Zone* zone) {
|
|
DEBUG_ASSERT(!IsCurrentApiNativeScope(zone));
|
|
return Handles<kVMHandleSizeInWords, kVMHandlesPerChunk,
|
|
kOffsetOfRawPtr>::AllocateZoneHandle(zone);
|
|
}
|
|
|
|
bool VMHandles::IsZoneHandle(uword handle) {
|
|
return Handles<kVMHandleSizeInWords, kVMHandlesPerChunk,
|
|
kOffsetOfRawPtr>::IsZoneHandle(handle);
|
|
}
|
|
|
|
int VMHandles::ScopedHandleCount() {
|
|
Thread* thread = Thread::Current();
|
|
ASSERT(thread->zone() != NULL);
|
|
VMHandles* handles = thread->zone()->handles();
|
|
return handles->CountScopedHandles();
|
|
}
|
|
|
|
int VMHandles::ZoneHandleCount() {
|
|
Thread* thread = Thread::Current();
|
|
ASSERT(thread->zone() != NULL);
|
|
VMHandles* handles = thread->zone()->handles();
|
|
return handles->CountZoneHandles();
|
|
}
|
|
|
|
void HandleScope::Initialize() {
|
|
ASSERT(thread()->MayAllocateHandles());
|
|
VMHandles* handles = thread()->zone()->handles();
|
|
ASSERT(handles != NULL);
|
|
saved_handle_block_ = handles->scoped_blocks_;
|
|
saved_handle_slot_ = handles->scoped_blocks_->next_handle_slot();
|
|
#if defined(DEBUG)
|
|
link_ = thread()->top_handle_scope();
|
|
thread()->set_top_handle_scope(this);
|
|
#endif
|
|
}
|
|
|
|
HandleScope::HandleScope(ThreadState* thread) : StackResource(thread) {
|
|
Initialize();
|
|
}
|
|
|
|
HandleScope::~HandleScope() {
|
|
ASSERT(thread()->zone() != NULL);
|
|
VMHandles* handles = thread()->zone()->handles();
|
|
ASSERT(handles != NULL);
|
|
handles->scoped_blocks_ = saved_handle_block_;
|
|
handles->scoped_blocks_->set_next_handle_slot(saved_handle_slot_);
|
|
#if defined(DEBUG)
|
|
handles->VerifyScopedHandleState();
|
|
handles->ZapFreeScopedHandles();
|
|
ASSERT(thread()->top_handle_scope() == this);
|
|
thread()->set_top_handle_scope(link_);
|
|
#endif
|
|
}
|
|
|
|
} // namespace dart
|