1// Copyright (c) 2018, 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_COMPILER_COMPILER_STATE_H_
6#define RUNTIME_VM_COMPILER_COMPILER_STATE_H_
7
8#if defined(DART_PRECOMPILED_RUNTIME)
9#error "AOT runtime should not use compiler sources (including header files)"
10#endif // defined(DART_PRECOMPILED_RUNTIME)
11
12#include "vm/compiler/api/deopt_id.h"
13#include "vm/compiler/cha.h"
14#include "vm/heap/safepoint.h"
15#include "vm/thread.h"
16
17namespace dart {
18
19class Function;
20class LocalScope;
21class LocalVariable;
22class SlotCache;
23class Slot;
24
25enum class CompilerTracing {
26 kOn,
27 kOff,
28};
29
30// Global compiler state attached to the thread.
31class CompilerState : public ThreadStackResource {
32 public:
33 CompilerState(Thread* thread,
34 bool is_aot,
35 CompilerTracing tracing = CompilerTracing::kOn)
36 : ThreadStackResource(thread),
37 cha_(thread),
38 is_aot_(is_aot),
39 tracing_(tracing) {
40 previous_ = thread->SetCompilerState(this);
41 }
42
43 ~CompilerState() {
44 ASSERT(&thread()->compiler_state() == this);
45 thread()->SetCompilerState(previous_);
46 }
47
48 CHA& cha() { return cha_; }
49
50 intptr_t deopt_id() const { return deopt_id_; }
51 void set_deopt_id(int value) {
52 ASSERT(value >= 0);
53 deopt_id_ = value;
54 }
55
56 intptr_t GetNextDeoptId() {
57 ASSERT(deopt_id_ != DeoptId::kNone);
58 const intptr_t id = deopt_id_;
59 deopt_id_ = DeoptId::Next(deopt_id_);
60 return id;
61 }
62
63 static CompilerState& Current() {
64 return Thread::Current()->compiler_state();
65 }
66
67 SlotCache* slot_cache() const { return slot_cache_; }
68 void set_slot_cache(SlotCache* cache) { slot_cache_ = cache; }
69
70 // Create a dummy list of local variables representing a context object
71 // with the given number of captured variables and given ID.
72 //
73 // Used during bytecode to IL translation because AllocateContext and
74 // CloneContext IL instructions need a list of local varaibles and bytecode
75 // does not record this information.
76 //
77 // TODO(vegorov): create context classes for distinct context IDs and
78 // populate them with slots without creating variables.
79 // Beware that context_id is satured at 8-bits, so multiple contexts may
80 // share id 255.
81 const ZoneGrowableArray<const Slot*>& GetDummyContextSlots(
82 intptr_t context_id,
83 intptr_t num_context_slots);
84
85 // Create a dummy LocalVariable that represents a captured local variable
86 // at the given index in the context with given ID.
87 //
88 // Used during bytecode to IL translation because StoreInstanceField and
89 // LoadField IL instructions need Slot, which can only be created from a
90 // LocalVariable.
91 //
92 // This function returns the same variable when it is called with the
93 // same index.
94 //
95 // TODO(vegorov): disambiguate slots for different context IDs.
96 // Beware that context_id is saturated at 8-bits, so multiple contexts may
97 // share id 255.
98 LocalVariable* GetDummyCapturedVariable(intptr_t context_id, intptr_t index);
99
100 bool is_aot() const { return is_aot_; }
101
102 bool should_trace() const { return tracing_ == CompilerTracing::kOn; }
103
104 static bool ShouldTrace() { return Current().should_trace(); }
105
106 static CompilerTracing ShouldTrace(const Function& func);
107
108 // Returns class Comparable<T> from dart:core.
109 const Class& ComparableClass();
110
111 private:
112 CHA cha_;
113 intptr_t deopt_id_ = 0;
114
115 // Cache for Slot objects created during compilation (see slot.h).
116 SlotCache* slot_cache_ = nullptr;
117
118 // Caches for dummy LocalVariables and context Slots created during bytecode
119 // to IL translation.
120 ZoneGrowableArray<ZoneGrowableArray<const Slot*>*>* dummy_slots_ = nullptr;
121 ZoneGrowableArray<LocalVariable*>* dummy_captured_vars_ = nullptr;
122
123 const bool is_aot_;
124
125 const CompilerTracing tracing_;
126
127 // Lookup cache for various classes (to avoid polluting object store with
128 // compiler specific classes).
129 const Class* comparable_class_ = nullptr;
130
131 CompilerState* previous_;
132};
133
134class DeoptIdScope : public ThreadStackResource {
135 public:
136 DeoptIdScope(Thread* thread, intptr_t deopt_id)
137 : ThreadStackResource(thread),
138 prev_deopt_id_(thread->compiler_state().deopt_id()) {
139 thread->compiler_state().set_deopt_id(deopt_id);
140 }
141
142 ~DeoptIdScope() { thread()->compiler_state().set_deopt_id(prev_deopt_id_); }
143
144 private:
145 const intptr_t prev_deopt_id_;
146
147 DISALLOW_COPY_AND_ASSIGN(DeoptIdScope);
148};
149
150/// Ensures that there were no deopt id allocations during the lifetime of this
151/// object.
152class AssertNoDeoptIdsAllocatedScope : public ThreadStackResource {
153 public:
154 explicit AssertNoDeoptIdsAllocatedScope(Thread* thread)
155 : ThreadStackResource(thread),
156 prev_deopt_id_(thread->compiler_state().deopt_id()) {}
157
158 ~AssertNoDeoptIdsAllocatedScope() {
159 ASSERT(thread()->compiler_state().deopt_id() == prev_deopt_id_);
160 }
161
162 private:
163 const intptr_t prev_deopt_id_;
164
165 DISALLOW_COPY_AND_ASSIGN(AssertNoDeoptIdsAllocatedScope);
166};
167
168} // namespace dart
169
170#endif // RUNTIME_VM_COMPILER_COMPILER_STATE_H_
171