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 | #include "vm/compiler/compiler_state.h" |
6 | |
7 | #include <functional> |
8 | |
9 | #include "vm/compiler/aot/precompiler.h" |
10 | #include "vm/compiler/backend/il_printer.h" |
11 | #include "vm/compiler/backend/slot.h" |
12 | #include "vm/growable_array.h" |
13 | #include "vm/scopes.h" |
14 | |
15 | namespace dart { |
16 | |
17 | template <typename T> |
18 | T* PutIfAbsent(Thread* thread, |
19 | ZoneGrowableArray<T*>** array_slot, |
20 | intptr_t index, |
21 | std::function<T*()> create) { |
22 | auto array = *array_slot; |
23 | |
24 | if (array == nullptr) { |
25 | Zone* const Z = thread->zone(); |
26 | *array_slot = array = new (Z) ZoneGrowableArray<T*>(Z, index + 1); |
27 | } |
28 | |
29 | while (array->length() <= index) { |
30 | array->Add(nullptr); |
31 | } |
32 | |
33 | if (array->At(index) == nullptr) { |
34 | (*array)[index] = create(); |
35 | } |
36 | return array->At(index); |
37 | } |
38 | |
39 | LocalVariable* CompilerState::GetDummyCapturedVariable(intptr_t context_id, |
40 | intptr_t index) { |
41 | return PutIfAbsent<LocalVariable>( |
42 | thread(), &dummy_captured_vars_, index, [&]() { |
43 | Zone* const Z = thread()->zone(); |
44 | const AbstractType& dynamic_type = |
45 | AbstractType::ZoneHandle(Z, Type::DynamicType()); |
46 | const String& name = String::ZoneHandle( |
47 | Z, Symbols::NewFormatted(thread(), ":context_var%" Pd, index)); |
48 | LocalVariable* var = new (Z) |
49 | LocalVariable(TokenPosition::kNoSource, TokenPosition::kNoSource, |
50 | name, dynamic_type, /*param_type=*/nullptr); |
51 | var->set_is_captured(); |
52 | var->set_index(VariableIndex(index)); |
53 | return var; |
54 | }); |
55 | } |
56 | |
57 | const ZoneGrowableArray<const Slot*>& CompilerState::GetDummyContextSlots( |
58 | intptr_t context_id, |
59 | intptr_t num_context_variables) { |
60 | return *PutIfAbsent<ZoneGrowableArray<const Slot*>>( |
61 | thread(), &dummy_slots_, num_context_variables, [&]() { |
62 | Zone* const Z = thread()->zone(); |
63 | |
64 | auto slots = |
65 | new (Z) ZoneGrowableArray<const Slot*>(num_context_variables); |
66 | for (intptr_t i = 0; i < num_context_variables; i++) { |
67 | LocalVariable* var = GetDummyCapturedVariable(context_id, i); |
68 | slots->Add(&Slot::GetContextVariableSlotFor(thread(), *var)); |
69 | } |
70 | |
71 | return slots; |
72 | }); |
73 | } |
74 | |
75 | CompilerTracing CompilerState::ShouldTrace(const Function& func) { |
76 | return FlowGraphPrinter::ShouldPrint(func) ? CompilerTracing::kOn |
77 | : CompilerTracing::kOff; |
78 | } |
79 | |
80 | const Class& CompilerState::ComparableClass() { |
81 | if (comparable_class_ == nullptr) { |
82 | Thread* thread = Thread::Current(); |
83 | Zone* zone = thread->zone(); |
84 | |
85 | // When obfuscation is enabled we need to obfuscate the name of the |
86 | // class before looking it up. |
87 | String& name = String::Handle(zone, Symbols::New(thread, "Comparable" )); |
88 | if (thread->isolate()->obfuscate()) { |
89 | Obfuscator obfuscator(thread, Object::null_string()); |
90 | name = obfuscator.Rename(name); |
91 | } |
92 | |
93 | const Library& lib = Library::Handle(zone, Library::CoreLibrary()); |
94 | const Class& cls = Class::ZoneHandle(zone, lib.LookupClass(name)); |
95 | ASSERT(!cls.IsNull()); |
96 | comparable_class_ = &cls; |
97 | } |
98 | return *comparable_class_; |
99 | } |
100 | |
101 | } // namespace dart |
102 | |