Flutter Engine
The Flutter Engine
program_visitor.h
Go to the documentation of this file.
1// Copyright (c) 2015, 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_PROGRAM_VISITOR_H_
6#define RUNTIME_VM_PROGRAM_VISITOR_H_
7
8#if !defined(DART_PRECOMPILED_RUNTIME)
9
10#include "vm/allocation.h"
11
12namespace dart {
13
14// Currently, we have three types of abstract visitors that can be extended and
15// used for program walking:
16//
17// * ClassVisitor, a visitor for classes in the program.
18// * FunctionVisitor, a visitor for functions in the program.
19// * CodeVisitor, a visitor for code objects in the program.
20//
21// To find the functions in a program, we must traverse the classes in the
22// program, and similarly for code objects and functions. Thus, each
23// FunctionVisitor is also a ClassVisitor, and each CodeVisitor is also a
24// FunctionVisitor (and thus a ClassVisitor).
25//
26// Only the most specific visitor method is abstract. Derived visitors have a
27// default empty implementation for base visitor methods to limit boilerplate
28// needed when extending. For example, subclasses of CodeVisitor that only do
29// per-Code work do not need to add empty implementations for VisitClass and
30// VisitFunction.
31//
32// There are no guarantees for the order in which objects of a given type will
33// be visited, but each object will be visited only once. In addition, each
34// object is visited before any visitable sub-objects it contains. For example,
35// this means a FunctionVisitor with a VisitClass implementation that drops
36// methods from a class will not visit the dropped methods unless they are also
37// found via another source of function objects.
38//
39// Note that WalkProgram only visits objects in the isolate heap. Deduplicating
40// visitors that want to use VM objects as canonical when possible should
41// instead add the appropriate VM objects first in their constructor.
42
43class Class;
44class Code;
45class Function;
46
47class CodeVisitor;
48class FunctionVisitor;
49
50class ClassVisitor : public ValueObject {
51 public:
52 virtual ~ClassVisitor() {}
53
54 virtual bool IsFunctionVisitor() const { return false; }
56 return const_cast<FunctionVisitor*>(
57 const_cast<ClassVisitor*>(this)->AsFunctionVisitor());
58 }
60 if (!IsFunctionVisitor()) return nullptr;
61 return reinterpret_cast<FunctionVisitor*>(this);
62 }
63
64 virtual bool IsCodeVisitor() const { return false; }
65 const CodeVisitor* AsCodeVisitor() const {
66 return const_cast<CodeVisitor*>(
67 const_cast<ClassVisitor*>(this)->AsCodeVisitor());
68 }
70 if (!IsCodeVisitor()) return nullptr;
71 return reinterpret_cast<CodeVisitor*>(this);
72 }
73
74 virtual void VisitClass(const Class& cls) = 0;
75};
76
78 public:
79 bool IsFunctionVisitor() const { return true; }
80 virtual void VisitClass(const Class& cls) {}
81 virtual void VisitFunction(const Function& function) = 0;
82};
83
85 public:
86 bool IsCodeVisitor() const { return true; }
87 virtual void VisitFunction(const Function& function) {}
88 virtual void VisitCode(const Code& code) = 0;
89};
90
91class Thread;
92class IsolateGroup;
93
94class ProgramVisitor : public AllStatic {
95 public:
96 // Walks all non-null class, function, and code objects in the program as
97 // necessary for the given visitor.
98 static void WalkProgram(Zone* zone,
99 IsolateGroup* isolate_group,
100 ClassVisitor* visitor);
101
102 static void Dedup(Thread* thread);
103#if defined(DART_PRECOMPILER)
104 static void AssignUnits(Thread* thread);
105 static uint32_t Hash(Thread* thread);
106#endif
107
108 private:
109 static void BindStaticCalls(Thread* thread);
110 static void ShareMegamorphicBuckets(Thread* thread);
111 static void NormalizeAndDedupCompressedStackMaps(Thread* thread);
112 static void DedupPcDescriptors(Thread* thread);
113 static void DedupDeoptEntries(Thread* thread);
114#if defined(DART_PRECOMPILER)
115 static void DedupCatchEntryMovesMaps(Thread* thread);
116 static void DedupUnlinkedCalls(Thread* thread);
117 static void PruneSubclasses(Thread* thread);
118#endif
119 static void DedupCodeSourceMaps(Thread* thread);
120 static void DedupLists(Thread* thread);
121 static void DedupInstructions(Thread* thread);
122};
123
124} // namespace dart
125
126#endif // !defined(DART_PRECOMPILED_RUNTIME)
127
128#endif // RUNTIME_VM_PROGRAM_VISITOR_H_
virtual void VisitClass(const Class &cls)=0
const CodeVisitor * AsCodeVisitor() const
CodeVisitor * AsCodeVisitor()
virtual bool IsCodeVisitor() const
const FunctionVisitor * AsFunctionVisitor() const
FunctionVisitor * AsFunctionVisitor()
virtual bool IsFunctionVisitor() const
virtual void VisitCode(const Code &code)=0
bool IsCodeVisitor() const
virtual void VisitFunction(const Function &function)
virtual void VisitClass(const Class &cls)
virtual void VisitFunction(const Function &function)=0
bool IsFunctionVisitor() const
static void Dedup(Thread *thread)
static void WalkProgram(Zone *zone, IsolateGroup *isolate_group, ClassVisitor *visitor)
Dart_NativeFunction function
Definition: fuchsia.cc:51
Definition: dart_vm.cc:33
static uint32_t Hash(uint32_t key)
Definition: hashmap_test.cc:65