Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
kernel_loader.h
Go to the documentation of this file.
1// Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file
2// for details. All rights reserved. Use of this source code is governed by a
3// BSD-style license that can be found in the LICENSE file.
4
5#ifndef RUNTIME_VM_KERNEL_LOADER_H_
6#define RUNTIME_VM_KERNEL_LOADER_H_
7
8#if !defined(DART_PRECOMPILED_RUNTIME)
9
10#include "vm/bit_vector.h"
13#include "vm/hash_map.h"
14#include "vm/kernel.h"
15#include "vm/object.h"
16#include "vm/symbols.h"
17
18namespace dart {
19namespace kernel {
20
21class KernelLoader;
22
24 public:
27 Heap::Space space)
28 : TranslationHelper(thread, space),
29 loader_(loader),
30 library_lookup_handle_(Library::Handle(thread->zone())) {}
32
33 virtual LibraryPtr LookupLibraryByKernelLibrary(NameIndex library,
34 bool required = true);
35 virtual ClassPtr LookupClassByKernelClass(NameIndex klass,
36 bool required = true);
37
38 private:
39 KernelLoader* loader_;
40
41#if defined(DEBUG)
42 class LibraryLookupHandleScope {
43 public:
44 explicit LibraryLookupHandleScope(Library& lib) : lib_(lib) {
45 ASSERT(lib_.IsNull());
46 }
47
48 ~LibraryLookupHandleScope() { lib_ = Library::null(); }
49
50 private:
51 Library& lib_;
52
53 DISALLOW_COPY_AND_ASSIGN(LibraryLookupHandleScope);
54 };
55#endif // defined(DEBUG)
56
57 // Preallocated handle for use in LookupClassByKernelClass().
58 Library& library_lookup_handle_;
59
60 DISALLOW_COPY_AND_ASSIGN(BuildingTranslationHelper);
61};
62
63template <typename VmType>
64class Mapping {
65 public:
66 bool Lookup(intptr_t canonical_name, VmType** handle) {
67 typename MapType::Pair* pair = map_.LookupPair(canonical_name);
68 if (pair != nullptr) {
69 *handle = pair->value;
70 return true;
71 }
72 return false;
73 }
74
75 void Insert(intptr_t canonical_name, VmType* object) {
76 map_.Insert(canonical_name, object);
77 }
78
79 private:
80 typedef IntMap<VmType*> MapType;
81 MapType map_;
82};
83
85 public:
86 explicit LibraryIndex(const TypedDataView& kernel_data);
87
88 intptr_t class_count() const { return class_count_; }
89 intptr_t procedure_count() const { return procedure_count_; }
90
91 intptr_t ClassOffset(intptr_t index) const {
92 return reader_.ReadUInt32At(class_index_offset_ + index * 4);
93 }
94
95 intptr_t ProcedureOffset(intptr_t index) const {
96 return reader_.ReadUInt32At(procedure_index_offset_ + index * 4);
97 }
98
99 intptr_t SizeOfClassAtOffset(intptr_t class_offset) const {
100 for (intptr_t i = 0, offset = class_index_offset_; i < class_count_;
101 ++i, offset += 4) {
102 if (static_cast<intptr_t>(reader_.ReadUInt32At(offset)) == class_offset) {
103 return reader_.ReadUInt32At(offset + 4) - class_offset;
104 }
105 }
106 UNREACHABLE();
107 return -1;
108 }
109
110 intptr_t SourceReferencesOffset() { return source_references_offset_; }
111
112 private:
113 Reader reader_;
114 intptr_t source_references_offset_;
115 intptr_t class_index_offset_;
116 intptr_t class_count_;
117 intptr_t procedure_index_offset_;
118 intptr_t procedure_count_;
119
121};
122
124 public:
125 // |class_offset| is the offset of class' kernel data in |kernel_data|.
126 // The size of the class' kernel data is |class_size|.
127 ClassIndex(const TypedDataBase& kernel_data,
128 intptr_t class_offset,
129 intptr_t class_size);
130
131 intptr_t procedure_count() const { return procedure_count_; }
132
133 intptr_t ProcedureOffset(intptr_t index) const {
134 return reader_.ReadUInt32At(procedure_index_offset_ + index * 4);
135 }
136
137 private:
138 void Init(intptr_t class_offset, intptr_t class_size);
139
140 Reader reader_;
141 intptr_t procedure_count_;
142 intptr_t procedure_index_offset_;
143
145};
146
149
150 const String* uri = nullptr;
151 const String* sources = nullptr;
152 const TypedData* line_starts = nullptr;
153};
154
159
160 static Key KeyOf(Pair kv) { return kv; }
161
162 static Value ValueOf(Pair kv) { return kv; }
163
164 static inline uword Hash(Key key) { return key->uri->Hash(); }
165
166 static inline bool IsKeyEqual(Pair kv, Key key) {
167 // Only compare uri.
168 return kv->uri->CompareTo(*key->uri) == 0;
169 }
170};
171
172class KernelLoader : public ValueObject {
173 public:
174 explicit KernelLoader(
175 Program* program,
177 static Object& LoadEntireProgram(Program* program,
178 bool process_pending_classes = true);
179
180 // Returns the library containing the main procedure, null if there
181 // was no main procedure, or a failure object if there was an error.
182 ObjectPtr LoadProgram(bool process_pending_classes = true);
183
184 // Load given library.
185 void LoadLibrary(const Library& library);
186
187 // Returns the function which will evaluate the expression, or a failure
188 // object if there was an error.
190 const String& klass);
191
192 // Finds all libraries that have been modified in this incremental
193 // version of the kernel program file.
194 //
195 // When [force_reload] is false and if [p_num_classes], [p_num_procedures] are
196 // not nullptr, then they are populated with number of classes and top-level
197 // procedures in [program].
198 static void FindModifiedLibraries(Program* program,
199 IsolateGroup* isolate_group,
200 BitVector* modified_libs,
201 bool force_reload,
202 bool* is_empty_program,
203 intptr_t* p_num_classes,
204 intptr_t* p_num_procedures);
205
206 static StringPtr FindSourceForScript(const uint8_t* kernel_buffer,
207 intptr_t kernel_buffer_length,
208 const String& url);
209
210 static void FinishLoading(const Class& klass);
211
213 void ReadLoadingUnits();
214
215 // Get closure Function from cache or create it if it is not created yet.
216 // [func_decl_offset] is an offset FunctionExpression or FunctionDeclaration.
217 static FunctionPtr GetClosureFunction(Thread* thread,
218 intptr_t func_decl_offset,
219 const Function& member_function,
220 const Function& parent_function,
221 const Object& closure_owner);
222
223 static void index_programs(kernel::Reader* reader,
224 GrowableArray<intptr_t>* subprogram_file_starts);
225
226 private:
227 // Pragma bits
236 using FfiNativePragma =
238
239 void FinishTopLevelClassLoading(const Class& toplevel_class,
240 const Library& library,
241 const LibraryIndex& library_index);
242
243 bool IsClassName(NameIndex name, const String& library, const String& klass);
244
245 void ReadVMAnnotations(intptr_t annotation_count,
246 uint32_t* pragma_bits,
247 String* native_name = nullptr);
248
249 KernelLoader(const KernelProgramInfo& kernel_program_info,
250 const TypedDataBase& kernel_data,
251 intptr_t data_program_offset);
252
253 void InitializeFields(
255
256 LibraryPtr LoadLibrary(intptr_t index);
257
258 const String& LibraryUri(intptr_t library_index) {
259 return translation_helper_.DartSymbolPlain(
260 translation_helper_.CanonicalNameString(
261 library_canonical_name(library_index)));
262 }
263
264 intptr_t library_offset(intptr_t index) {
265 kernel::Reader reader(program_->binary());
266 return reader.ReadFromIndexNoReset(reader.size(),
268 program_->library_count() + 1, index);
269 }
270
271 NameIndex library_canonical_name(intptr_t index) {
272 kernel::Reader reader(program_->binary());
273 reader.set_offset(library_offset(index));
274
275 // Start reading library.
276 // Note that this needs to be keep in sync with LibraryHelper.
277 reader.ReadFlags();
278 reader.ReadUInt(); // Read major language version.
279 reader.ReadUInt(); // Read minor language version.
280 return reader.ReadCanonicalNameReference();
281 }
282
283 uint8_t CharacterAt(StringIndex string_index, intptr_t index);
284
285 void walk_incremental_kernel(BitVector* modified_libs,
286 bool* is_empty_program,
287 intptr_t* p_num_classes,
288 intptr_t* p_num_procedures);
289
290 void LoadPreliminaryClass(ClassHelper* class_helper,
291 intptr_t type_parameter_count);
292
293 void ReadInferredType(const Field& field, intptr_t kernel_offset);
294 void CheckForInitializer(const Field& field);
295
296 void LoadClass(const Library& library,
297 const Class& toplevel_class,
298 intptr_t class_end,
299 Class* out_class);
300
301 void FinishClassLoading(const Class& klass,
302 const Library& library,
303 const Class& toplevel_class,
304 intptr_t class_offset,
305 const ClassIndex& class_index,
306 ClassHelper* class_helper);
307
308 void LoadProcedure(const Library& library,
309 const Class& owner,
310 bool in_class,
311 intptr_t procedure_end);
312
313 ArrayPtr MakeFieldsArray();
314 ArrayPtr MakeFunctionsArray();
315
316 ScriptPtr LoadScriptAt(
317 intptr_t index,
318 DirectChainedHashMap<UriToSourceTableTrait>* uri_to_source_table);
319
320 // If klass's script is not the script at the uri index, return a PatchClass
321 // for klass whose script corresponds to the uri index.
322 // Otherwise return klass.
323 const Object& ClassForScriptAt(const Class& klass, intptr_t source_uri_index);
324 ScriptPtr ScriptAt(intptr_t source_uri_index) {
325 return kernel_program_info_.ScriptAt(source_uri_index);
326 }
327
328 // Reads field initializer and returns the initial field value.
329 ObjectPtr ReadInitialFieldValue(const Field& field,
330 FieldHelper* field_helper);
331
332 // Generates field getter and setter functions.
333 void GenerateFieldAccessors(const Class& klass,
334 const Field& field,
335 FieldHelper* field_helper);
336
337 void LoadLibraryImportsAndExports(Library* library,
338 const Class& toplevel_class);
339
340 LibraryPtr LookupLibraryOrNull(NameIndex library);
341 LibraryPtr LookupLibrary(NameIndex library);
342 LibraryPtr LookupLibraryFromClass(NameIndex klass);
343 ClassPtr LookupClass(const Library& library, NameIndex klass);
344
345 UntaggedFunction::Kind GetFunctionType(ProcedureHelper::Kind procedure_kind);
346
347 // Read local function (either FunctionExpression or FunctionDeclaration)
348 // and create corresponding Function object.
349 // If [closure_owner] is not null, it overrides closure function owner.
350 FunctionPtr LoadClosureFunction(const Function& parent_function,
351 const Object& closure_owner);
352
353 Program* program_;
354
355 Thread* thread_;
356 Zone* zone_;
357 NoActiveIsolateScope no_active_isolate_scope_;
358 Array& patch_classes_;
359 ActiveClass active_class_;
360 // This is the offset of the current library within
361 // the whole kernel program.
362 intptr_t library_kernel_offset_;
363 // This is the offset by which offsets, which are set relative
364 // to their library's kernel data, have to be corrected.
365 intptr_t correction_offset_;
366 bool loading_native_wrappers_library_;
367
368 NameIndex skip_vmservice_library_;
369
370 TypedDataView& library_kernel_data_;
371 KernelProgramInfo& kernel_program_info_;
372 BuildingTranslationHelper translation_helper_;
373 KernelReaderHelper helper_;
374 ConstantReader constant_reader_;
375 TypeTranslator type_translator_;
376 InferredTypeMetadataHelper inferred_type_metadata_helper_;
377
378 Object& static_field_value_;
379
380 Smi& name_index_handle_;
381
382 // We "re-use" the normal .dill file format for encoding compiled evaluation
383 // expressions from the debugger. This allows us to also reuse the normal
384 // a) kernel loader b) flow graph building code. The encoding is either one
385 // of the following two options:
386 //
387 // * Option a) The expression is evaluated inside an instance method call
388 // context:
389 //
390 // Program:
391 // |> library "evaluate:source"
392 // |> class "#DebugClass"
393 // |> procedure ":Eval"
394 //
395 // * Option b) The expression is evaluated outside an instance method call
396 // context:
397 //
398 // Program:
399 // |> library "evaluate:source"
400 // |> procedure ":Eval"
401 //
402 // See
403 // * pkg/front_end/lib/src/fasta/incremental_compiler.dart,
404 // compileExpression
405 // * pkg/front_end/lib/src/fasta/kernel/utils.dart,
406 // createExpressionEvaluationComponent
407 //
408 Library& expression_evaluation_library_;
409
410 GrowableArray<const Function*> functions_;
411 GrowableArray<const Field*> fields_;
412
414
416};
417
418FunctionPtr CreateFieldInitializerFunction(Thread* thread,
419 Zone* zone,
420 const Field& field);
421
422} // namespace kernel
423} // namespace dart
424
425#endif // !defined(DART_PRECOMPILED_RUNTIME)
426#endif // RUNTIME_VM_KERNEL_LOADER_H_
#define UNREACHABLE()
Definition assert.h:248
Pair * LookupPair(const Key &key) const
Definition hash_map.h:558
void Insert(const Key &key, const Value &value)
Definition hash_map.h:543
ScriptPtr ScriptAt(intptr_t index) const
Definition object.cc:15126
intptr_t CompareTo(const String &other) const
Definition object.cc:23717
virtual ClassPtr LookupClassByKernelClass(NameIndex klass, bool required=true)
BuildingTranslationHelper(KernelLoader *loader, Thread *thread, Heap::Space space)
virtual LibraryPtr LookupLibraryByKernelLibrary(NameIndex library, bool required=true)
intptr_t procedure_count() const
intptr_t ProcedureOffset(intptr_t index) const
static Object & LoadEntireProgram(Program *program, bool process_pending_classes=true)
static void index_programs(kernel::Reader *reader, GrowableArray< intptr_t > *subprogram_file_starts)
ObjectPtr LoadExpressionEvaluationFunction(const String &library_url, const String &klass)
void LoadLibrary(const Library &library)
friend class BuildingTranslationHelper
KernelLoader(Program *program, DirectChainedHashMap< UriToSourceTableTrait > *uri_to_source_table)
static StringPtr FindSourceForScript(const uint8_t *kernel_buffer, intptr_t kernel_buffer_length, const String &url)
static void FinishLoading(const Class &klass)
static void FindModifiedLibraries(Program *program, IsolateGroup *isolate_group, BitVector *modified_libs, bool force_reload, bool *is_empty_program, intptr_t *p_num_classes, intptr_t *p_num_procedures)
ObjectPtr LoadProgram(bool process_pending_classes=true)
static FunctionPtr GetClosureFunction(Thread *thread, intptr_t func_decl_offset, const Function &member_function, const Function &parent_function, const Object &closure_owner)
intptr_t SizeOfClassAtOffset(intptr_t class_offset) const
intptr_t class_count() const
intptr_t procedure_count() const
intptr_t ProcedureOffset(intptr_t index) const
intptr_t ClassOffset(intptr_t index) const
bool Lookup(intptr_t canonical_name, VmType **handle)
void Insert(intptr_t canonical_name, VmType *object)
uint32_t ReadUInt32At(intptr_t offset) const
const String & DartSymbolPlain(const char *content) const
StringIndex CanonicalNameString(NameIndex name)
#define ASSERT(E)
void Init()
FunctionPtr CreateFieldInitializerFunction(Thread *thread, Zone *zone, const Field &field)
static constexpr int LibraryCountFieldCountFromEnd
const char *const name
uintptr_t uword
Definition globals.h:501
#define DISALLOW_COPY_AND_ASSIGN(TypeName)
Definition globals.h:581
Point offset
static bool IsKeyEqual(Pair kv, Key key)
const UriToSourceTableEntry * Key