blob: 56bb9fb8059daa78b0a523f8d5721470ae175dbc [file] [log] [blame]
Igor Murashkinfc1ccd72015-07-30 15:11:09 -07001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16#include "lambda/closure_builder.h"
17
18#include "base/macros.h"
19#include "base/value_object.h"
20#include "lambda/art_lambda_method.h"
21#include "lambda/closure.h"
22#include "lambda/shorty_field_type.h"
23#include "runtime/mirror/object_reference.h"
24
25#include <stdint.h>
26#include <vector>
27
28namespace art {
29namespace lambda {
30
31/*
32 * GC support TODOs:
33 * (Although there's some code for storing objects, it is UNIMPLEMENTED(FATAL) because it is
34 * incomplete).
35 *
36 * 1) GC needs to be able to traverse the Closure and visit any references.
37 * It might be possible to get away with global roots in the short term.
38 *
39 * 2) Add brooks read barrier support. We can store the black/gray/white bits
40 * in the lower 2 bits of the lambda art method pointer. Whenever a closure is copied
41 * [to the stack] we'd need to add a cold path to turn it black.
42 * (since there's only 3 colors, I can use the 4th value to indicate no-refs).
43 * e.g. 0x0 = gray, 0x1 = white, 0x2 = black, 0x3 = no-nested-references
44 * - Alternatively the GC can mark reference-less closures as always-black,
45 * although it would need extra work to check for references.
46 */
47
48void ClosureBuilder::CaptureVariableObject(mirror::Object* object) {
49 auto compressed_reference = mirror::CompressedReference<mirror::Object>::FromMirrorPtr(object);
50 ShortyFieldTypeTraits::MaxType storage = 0;
51
52 static_assert(sizeof(storage) >= sizeof(compressed_reference),
53 "not enough room to store a compressed reference");
54 memcpy(&storage, &compressed_reference, sizeof(compressed_reference));
55
56 values_.push_back(storage);
57 size_ += kObjectReferenceSize;
58
59 static_assert(kObjectReferenceSize == sizeof(compressed_reference), "reference size mismatch");
60
61 // TODO: needs more work to support concurrent GC
62 if (kIsDebugBuild) {
63 if (kUseReadBarrier) {
64 UNIMPLEMENTED(FATAL) << "can't yet safely capture objects with read barrier";
65 UNREACHABLE();
66 }
67 }
68}
69
70void ClosureBuilder::CaptureVariableLambda(Closure* closure) {
71 DCHECK(closure != nullptr); // null closures not allowed, target method must be null instead.
72 values_.push_back(reinterpret_cast<ShortyFieldTypeTraits::MaxType>(closure));
73
74 if (LIKELY(is_dynamic_size_ == false)) {
75 // Write in the extra bytes to store the dynamic size the first time.
76 is_dynamic_size_ = true;
77 size_ += sizeof(Closure::captured_[0].dynamic_.size_);
78 }
79
80 // A closure may be sized dynamically, so always query it for the true size.
81 size_ += closure->GetSize();
82}
83
84size_t ClosureBuilder::GetSize() const {
85 return size_;
86}
87
88size_t ClosureBuilder::GetCaptureCount() const {
89 return values_.size();
90}
91
92Closure* ClosureBuilder::CreateInPlace(void* memory, ArtLambdaMethod* target_method) const {
93 DCHECK(memory != nullptr);
94 DCHECK(target_method != nullptr);
95 DCHECK_EQ(is_dynamic_size_, target_method->IsDynamicSize());
96
97 CHECK_EQ(target_method->GetNumberOfCapturedVariables(), values_.size())
98 << "number of variables captured at runtime does not match "
99 << "number of variables captured at compile time";
100
101 Closure* closure = new (memory) Closure;
102 closure->lambda_info_ = target_method;
103
104 static_assert(offsetof(Closure, captured_) == kInitialSize, "wrong initial size");
105
106 size_t written_size;
107 if (UNLIKELY(is_dynamic_size_)) {
108 // The closure size must be set dynamically (i.e. nested lambdas).
109 closure->captured_[0].dynamic_.size_ = GetSize();
110 size_t header_size = offsetof(Closure, captured_[0].dynamic_.variables_);
111 DCHECK_LE(header_size, GetSize());
112 size_t variables_size = GetSize() - header_size;
113 written_size =
114 WriteValues(target_method,
115 closure->captured_[0].dynamic_.variables_,
116 header_size,
117 variables_size);
118 } else {
119 // The closure size is known statically (i.e. no nested lambdas).
120 DCHECK(GetSize() == target_method->GetStaticClosureSize());
121 size_t header_size = offsetof(Closure, captured_[0].static_variables_);
122 DCHECK_LE(header_size, GetSize());
123 size_t variables_size = GetSize() - header_size;
124 written_size =
125 WriteValues(target_method,
126 closure->captured_[0].static_variables_,
127 header_size,
128 variables_size);
129 }
130
131 DCHECK_EQ(written_size, closure->GetSize());
132
133 return closure;
134}
135
136size_t ClosureBuilder::WriteValues(ArtLambdaMethod* target_method,
137 uint8_t variables[],
138 size_t header_size,
139 size_t variables_size) const {
140 size_t total_size = header_size;
141 const char* shorty_types = target_method->GetCapturedVariablesShortyTypeDescriptor();
142
143 size_t variables_offset = 0;
144 size_t remaining_size = variables_size;
145
146 const size_t shorty_count = target_method->GetNumberOfCapturedVariables();
147 for (size_t i = 0; i < shorty_count; ++i) {
148 ShortyFieldType shorty{shorty_types[i]}; // NOLINT [readability/braces] [4]
149
150 size_t var_size;
151 if (LIKELY(shorty.IsStaticSize())) {
152 // TODO: needs more work to support concurrent GC, e.g. read barriers
153 if (kUseReadBarrier == false) {
154 if (UNLIKELY(shorty.IsObject())) {
155 UNIMPLEMENTED(FATAL) << "can't yet safely write objects with read barrier";
156 }
157 } else {
158 if (UNLIKELY(shorty.IsObject())) {
159 UNIMPLEMENTED(FATAL) << "writing objects not yet supported, no GC support";
160 }
161 }
162
163 var_size = shorty.GetStaticSize();
164 DCHECK_LE(var_size, sizeof(values_[i]));
165
166 // Safe even for objects (non-read barrier case) if we never suspend
167 // while the ClosureBuilder is live.
168 // FIXME: Need to add GC support for references in a closure.
169 memcpy(&variables[variables_offset], &values_[i], var_size);
170 } else {
171 DCHECK(shorty.IsLambda())
172 << " don't support writing dynamically sized types other than lambda";
173
174 ShortyFieldTypeTraits::MaxType closure_raw = values_[i];
175 Closure* nested_closure = reinterpret_cast<Closure*>(closure_raw);
176
177 DCHECK(nested_closure != nullptr);
178 nested_closure->CopyTo(&variables[variables_offset], remaining_size);
179
180 var_size = nested_closure->GetSize();
181 }
182
183 total_size += var_size;
184 DCHECK_GE(remaining_size, var_size);
185 remaining_size -= var_size;
186
187 variables_offset += var_size;
188 }
189
190 DCHECK_EQ('\0', shorty_types[shorty_count]);
191 DCHECK_EQ(variables_offset, variables_size);
192
193 return total_size;
194}
195
196
197} // namespace lambda
198} // namespace art