Files
sdk/runtime/vm/compiler/frontend/constant_reader.h
T
Martin Kustermann 148af29811 [vm] Disentagle dependencies in the frontend & compiler
** KernelProgramInfo

The VM can load multiple kernel files into an isolate. Each kernel file
is represented as a [KernelProgramInfo] object. Loading a kernel blob
will result in a tree of objects

  Library
   - TopLevelClass
     + Field
     + Function
   + Class
     + Field
     + Function
       + ... closure functions ...

The entire tree belongs to one [KernelProgramInfo] object. There are
two exceptions
* after a hot-reload we inject [PatchClass]es (as owners of
old [Field]/[Function] objects) that identify the old kernel data
* expression evaluation functions will get a special data array with
information about the expression evaluation kernel data

=> We attach the [KernelProgramInfo] to the root of the tree, namely
  the [Library]

=> We introduce `{Field,Function,Class,Library}::KernelProgramInfo()`
  that will get that object - by walking up the chain.

=> We introduce `UntaggedPatchClass::kernel_program_info_` field that
  allows saving the old [KernelProgramInfo] (on which the old
  [Field]/[Function] objects are based upon) in the [PatchClass].

=> We include the [KernelProgramInfo] in the expression evaluation
  data array.

** Scripts

A [Script] object should represent a .dart source file. It contains the
source, line offsets, etc. of that dart source file. The contents of a
dart source file may be used by many classes/libraries or even many
kernel blobs (e.g. via our copying mixin application).

When we build a tree like above, we'll attach a reference to a
[Script] object to [Class] objects. But members of a class may come from
a different script, in which case we inject an intermediary [PatchClass]
that references that other script.

When the compiler frontend loads kernel, builds flow graph, etc we walk
down the kernel binary (the tree above) - all belonging to the same
[KernelProgramInfo] object, but sub-parts may belong to different
[Script]s.

=> We use the existing [ActiveClass] class that keeps track of the
current class & member inside the class we're operating on.
=> Whenever we need the script we get it from the active member
function/class.

** Misc

We make kernel related classes (e.g. [KernelReaderHelper]) independent
of [Script].

We make the [Script] objects hold on to the [KernelProgramInfo] they are
based upon (e.g. for lazy source reading). But since a given [Script]
doesn't belong to a particular kernel blob (multiple kernel blobs may
contain information about the same script) we don't expose this in the
public API.

=> In the future we could have a unique [Script] object per url (instead
of having several as we do now)

TEST=ci

Change-Id: I7bd698f497b0bd8ab8e546540bb1dad5b5a6dd48
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/313381
Reviewed-by: Jens Johansen <jensj@google.com>
Commit-Queue: Martin Kustermann <kustermann@google.com>
Reviewed-by: Slava Egorov <vegorov@google.com>
2023-07-13 08:17:38 +00:00

71 lines
2.2 KiB
C++

// Copyright (c) 2018, 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.
#ifndef RUNTIME_VM_COMPILER_FRONTEND_CONSTANT_READER_H_
#define RUNTIME_VM_COMPILER_FRONTEND_CONSTANT_READER_H_
#if defined(DART_PRECOMPILED_RUNTIME)
#error "AOT runtime should not use compiler sources (including header files)"
#endif // defined(DART_PRECOMPILED_RUNTIME)
#include "vm/compiler/frontend/kernel_translation_helper.h"
#include "vm/hash_table.h"
#include "vm/object.h"
namespace dart {
namespace kernel {
// Reads and caches constants from the kernel constant pool.
class ConstantReader {
public:
ConstantReader(KernelReaderHelper* helper, ActiveClass* active_class);
virtual ~ConstantReader() {}
bool IsPragmaInstanceConstant(intptr_t constant_index,
intptr_t* pragma_name_constant_index,
intptr_t* pragma_options_constant_index);
bool IsStringConstant(intptr_t constant_index, const char* name);
bool GetStringConstant(intptr_t constant_index, String* out_value);
InstancePtr ReadConstantInitializer();
InstancePtr ReadConstantExpression();
ObjectPtr ReadAnnotations();
// Peeks to see if constant at the given index will evaluate to
// instance of the given clazz.
bool IsInstanceConstant(intptr_t constant_index, const Class& clazz);
// Reads a constant at the given index (possibly by recursing
// into sub-constants).
InstancePtr ReadConstant(intptr_t constant_index);
intptr_t NumConstants();
private:
InstancePtr ReadConstantInternal(intptr_t constant_index);
intptr_t NavigateToIndex(KernelReaderHelper* reader, intptr_t constant_index);
intptr_t NumConstants(KernelReaderHelper* reader);
ScriptPtr Script() {
if (active_class_ != nullptr) {
return active_class_->ActiveScript();
}
return Script::null();
}
KernelReaderHelper* helper_;
Zone* zone_;
TranslationHelper& translation_helper_;
ActiveClass* active_class_;
Object& result_;
DISALLOW_COPY_AND_ASSIGN(ConstantReader);
};
} // namespace kernel
} // namespace dart
#endif // RUNTIME_VM_COMPILER_FRONTEND_CONSTANT_READER_H_