blob: 9c37db8fccf7c0c3d8588f6336a296e6d6768b43 [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";
Igor Murashkinfc1ccd72015-07-30 15:11:09 -070065 }
66 }
67}
68
69void ClosureBuilder::CaptureVariableLambda(Closure* closure) {
70 DCHECK(closure != nullptr); // null closures not allowed, target method must be null instead.
71 values_.push_back(reinterpret_cast<ShortyFieldTypeTraits::MaxType>(closure));
72
73 if (LIKELY(is_dynamic_size_ == false)) {
74 // Write in the extra bytes to store the dynamic size the first time.
75 is_dynamic_size_ = true;
76 size_ += sizeof(Closure::captured_[0].dynamic_.size_);
77 }
78
79 // A closure may be sized dynamically, so always query it for the true size.
80 size_ += closure->GetSize();
81}
82
83size_t ClosureBuilder::GetSize() const {
84 return size_;
85}
86
87size_t ClosureBuilder::GetCaptureCount() const {
88 return values_.size();
89}
90
91Closure* ClosureBuilder::CreateInPlace(void* memory, ArtLambdaMethod* target_method) const {
92 DCHECK(memory != nullptr);
93 DCHECK(target_method != nullptr);
94 DCHECK_EQ(is_dynamic_size_, target_method->IsDynamicSize());
95
96 CHECK_EQ(target_method->GetNumberOfCapturedVariables(), values_.size())
97 << "number of variables captured at runtime does not match "
98 << "number of variables captured at compile time";
99
100 Closure* closure = new (memory) Closure;
101 closure->lambda_info_ = target_method;
102
103 static_assert(offsetof(Closure, captured_) == kInitialSize, "wrong initial size");
104
105 size_t written_size;
106 if (UNLIKELY(is_dynamic_size_)) {
107 // The closure size must be set dynamically (i.e. nested lambdas).
108 closure->captured_[0].dynamic_.size_ = GetSize();
109 size_t header_size = offsetof(Closure, captured_[0].dynamic_.variables_);
110 DCHECK_LE(header_size, GetSize());
111 size_t variables_size = GetSize() - header_size;
112 written_size =
113 WriteValues(target_method,
114 closure->captured_[0].dynamic_.variables_,
115 header_size,
116 variables_size);
117 } else {
118 // The closure size is known statically (i.e. no nested lambdas).
119 DCHECK(GetSize() == target_method->GetStaticClosureSize());
120 size_t header_size = offsetof(Closure, captured_[0].static_variables_);
121 DCHECK_LE(header_size, GetSize());
122 size_t variables_size = GetSize() - header_size;
123 written_size =
124 WriteValues(target_method,
125 closure->captured_[0].static_variables_,
126 header_size,
127 variables_size);
128 }
129
130 DCHECK_EQ(written_size, closure->GetSize());
131
132 return closure;
133}
134
135size_t ClosureBuilder::WriteValues(ArtLambdaMethod* target_method,
136 uint8_t variables[],
137 size_t header_size,
138 size_t variables_size) const {
139 size_t total_size = header_size;
140 const char* shorty_types = target_method->GetCapturedVariablesShortyTypeDescriptor();
141
142 size_t variables_offset = 0;
143 size_t remaining_size = variables_size;
144
145 const size_t shorty_count = target_method->GetNumberOfCapturedVariables();
146 for (size_t i = 0; i < shorty_count; ++i) {
147 ShortyFieldType shorty{shorty_types[i]}; // NOLINT [readability/braces] [4]
148
149 size_t var_size;
150 if (LIKELY(shorty.IsStaticSize())) {
151 // TODO: needs more work to support concurrent GC, e.g. read barriers
152 if (kUseReadBarrier == false) {
153 if (UNLIKELY(shorty.IsObject())) {
154 UNIMPLEMENTED(FATAL) << "can't yet safely write objects with read barrier";
155 }
156 } else {
157 if (UNLIKELY(shorty.IsObject())) {
158 UNIMPLEMENTED(FATAL) << "writing objects not yet supported, no GC support";
159 }
160 }
161
162 var_size = shorty.GetStaticSize();
163 DCHECK_LE(var_size, sizeof(values_[i]));
164
165 // Safe even for objects (non-read barrier case) if we never suspend
166 // while the ClosureBuilder is live.
167 // FIXME: Need to add GC support for references in a closure.
168 memcpy(&variables[variables_offset], &values_[i], var_size);
169 } else {
170 DCHECK(shorty.IsLambda())
171 << " don't support writing dynamically sized types other than lambda";
172
173 ShortyFieldTypeTraits::MaxType closure_raw = values_[i];
174 Closure* nested_closure = reinterpret_cast<Closure*>(closure_raw);
175
176 DCHECK(nested_closure != nullptr);
177 nested_closure->CopyTo(&variables[variables_offset], remaining_size);
178
179 var_size = nested_closure->GetSize();
180 }
181
182 total_size += var_size;
183 DCHECK_GE(remaining_size, var_size);
184 remaining_size -= var_size;
185
186 variables_offset += var_size;
187 }
188
189 DCHECK_EQ('\0', shorty_types[shorty_count]);
190 DCHECK_EQ(variables_offset, variables_size);
191
192 return total_size;
193}
194
195
196} // namespace lambda
197} // namespace art