blob: 38ec063ed2023c0242fdf13db6b3b7597cc9b22e [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#ifndef ART_RUNTIME_LAMBDA_CLOSURE_H_
17#define ART_RUNTIME_LAMBDA_CLOSURE_H_
18
19#include "base/macros.h"
20#include "base/mutex.h" // For Locks::mutator_lock_.
21#include "lambda/shorty_field_type.h"
22
23#include <stdint.h>
24
25namespace art {
26class ArtMethod; // forward declaration
27
28namespace mirror {
29class Object; // forward declaration
30} // namespace mirror
31
32namespace lambda {
33class ArtLambdaMethod; // forward declaration
34class ClosureBuilder; // forward declaration
35
Igor Murashkin457e8742015-10-22 17:37:50 -070036// TODO: Remove these constants once closures are supported properly.
37
38// Does the lambda closure support containing references? If so, all the users of lambdas
39// must be updated to also support references.
40static constexpr const bool kClosureSupportsReferences = false;
41// Does the lambda closure support being garbage collected? If so, all the users of lambdas
42// must be updated to also support garbage collection.
43static constexpr const bool kClosureSupportsGarbageCollection = false;
44// Does the lambda closure support being garbage collected with a read barrier? If so,
45// all the users of the lambdas msut also be updated to support read barrier GC.
46static constexpr const bool kClosureSupportsReadBarrier = false;
47
48// Is this closure being stored as a 'long' in shadow frames and the quick ABI?
49static constexpr const bool kClosureIsStoredAsLong = true;
50
51
52// Raw memory layout for the lambda closure.
53//
54// WARNING:
55// * This should only be used by the compiler and tests, as they need to offsetof the raw fields.
56// * Runtime/interpreter should always access closures through a Closure pointer.
57struct ClosureStorage {
58 // Compile-time known lambda information such as the type descriptor and size.
59 ArtLambdaMethod* lambda_info_;
60
61 // A contiguous list of captured variables, and possibly the closure size.
62 // The runtime size can always be determined through GetSize().
63 union {
64 // Read from here if the closure size is static (ArtLambdaMethod::IsStatic)
65 uint8_t static_variables_[0];
66 struct {
67 // Read from here if the closure size is dynamic (ArtLambdaMethod::IsDynamic)
68 size_t size_; // The lambda_info_ and the size_ itself is also included as part of the size.
69 uint8_t variables_[0];
70 } dynamic_;
71 } captured_[0];
72 // captured_ will always consist of one array element at runtime.
73 // Set to [0] so that 'size_' is not counted in sizeof(Closure).
74};
75
Igor Murashkinfc1ccd72015-07-30 15:11:09 -070076// Inline representation of a lambda closure.
77// Contains the target method and the set of packed captured variables as a copy.
78//
79// The closure itself is logically immutable, although in practice any object references
80// it (recursively) contains can be moved and updated by the GC.
Igor Murashkin457e8742015-10-22 17:37:50 -070081struct Closure : private ClosureStorage {
Igor Murashkinfc1ccd72015-07-30 15:11:09 -070082 // Get the size of the Closure in bytes.
83 // This is necessary in order to allocate a large enough area to copy the Closure into.
84 // Do *not* copy the closure with memcpy, since references also need to get moved.
85 size_t GetSize() const;
86
87 // Copy this closure into the target, whose memory size is specified by target_size.
88 // Any object references are fixed up during the copy (if there was a read barrier).
89 // The target_size must be at least as large as GetSize().
90 void CopyTo(void* target, size_t target_size) const;
91
Igor Murashkin6918bf12015-09-27 19:19:06 -070092 // Get the target method, i.e. the method that will be dispatched into with invoke-lambda.
93 ArtMethod* GetTargetMethod() const;
94
Igor Murashkin457e8742015-10-22 17:37:50 -070095 // Get the static lambda info that never changes.
96 ArtLambdaMethod* GetLambdaInfo() const;
97
Igor Murashkin6918bf12015-09-27 19:19:06 -070098 // Calculates the hash code. Value is recomputed each time.
99 uint32_t GetHashCode() const SHARED_REQUIRES(Locks::mutator_lock_);
100
101 // Is this the same closure as other? e.g. same target method, same variables captured.
102 //
103 // Determines whether the two Closures are interchangeable instances.
104 // Does *not* call Object#equals recursively. If two Closures compare ReferenceEquals true that
105 // means that they are interchangeable values (usually for the purpose of boxing/unboxing).
106 bool ReferenceEquals(const Closure* other) const SHARED_REQUIRES(Locks::mutator_lock_);
107
Igor Murashkinfc1ccd72015-07-30 15:11:09 -0700108 // How many variables were captured?
109 size_t GetNumberOfCapturedVariables() const;
110
111 // Returns a type descriptor string that represents each captured variable.
112 // e.g. "Ljava/lang/Object;ZB" would mean a capture tuple of (Object, boolean, byte)
113 const char* GetCapturedVariablesTypeDescriptor() const;
114
115 // Returns the short type for the captured variable at index.
116 // Index must be less than the number of captured variables.
117 ShortyFieldType GetCapturedShortyType(size_t index) const;
118
119 // Returns the 32-bit representation of a non-wide primitive at the captured variable index.
120 // Smaller types are zero extended.
121 // Index must be less than the number of captured variables.
122 uint32_t GetCapturedPrimitiveNarrow(size_t index) const;
123 // Returns the 64-bit representation of a wide primitive at the captured variable index.
124 // Smaller types are zero extended.
125 // Index must be less than the number of captured variables.
126 uint64_t GetCapturedPrimitiveWide(size_t index) const;
127 // Returns the object reference at the captured variable index.
128 // The type at the index *must* be an object reference or a CHECK failure will occur.
129 // Index must be less than the number of captured variables.
130 mirror::Object* GetCapturedObject(size_t index) const SHARED_REQUIRES(Locks::mutator_lock_);
131
132 // Gets the size of a nested capture closure in bytes, at the captured variable index.
133 // The type at the index *must* be a lambda closure or a CHECK failure will occur.
134 size_t GetCapturedClosureSize(size_t index) const;
135
136 // Copies a nested lambda closure at the captured variable index.
137 // The destination must have enough room for the closure (see GetCapturedClosureSize).
138 void CopyCapturedClosure(size_t index, void* destination, size_t destination_room) const;
139
140 private:
141 // Read out any non-lambda value as a copy.
142 template <typename T>
143 T GetCapturedVariable(size_t index) const;
144
145 // Reconstruct the closure's captured variable info at runtime.
146 struct VariableInfo {
147 size_t index_;
148 ShortyFieldType variable_type_;
149 size_t offset_;
150 size_t count_;
151
152 enum Flags {
153 kIndex = 0x1,
154 kVariableType = 0x2,
155 kOffset = 0x4,
156 kCount = 0x8,
157 };
158
159 // Traverse to the end of the type descriptor list instead of stopping at some particular index.
160 static constexpr size_t kUpToIndexMax = static_cast<size_t>(-1);
161 };
162
163 // Parse a type descriptor, stopping at index "upto_index".
164 // Returns only the information requested in flags. All other fields are indeterminate.
165 template <VariableInfo::Flags flags>
166 inline VariableInfo ALWAYS_INLINE ParseTypeDescriptor(const char* type_descriptor,
167 size_t upto_index) const;
168
169 // Convenience function to call ParseTypeDescriptor with just the type and offset.
170 void GetCapturedVariableTypeAndOffset(size_t index,
171 ShortyFieldType* out_type,
172 size_t* out_offset) const;
173
174 // How many bytes do the captured variables take up? Runtime sizeof(captured_variables).
175 size_t GetCapturedVariablesSize() const;
176 // Get the size in bytes of the variable_type which is potentially stored at offset.
177 size_t GetCapturedVariableSize(ShortyFieldType variable_type, size_t offset) const;
178 // Get the starting offset (in bytes) for the 0th captured variable.
179 // All offsets are relative to 'captured_'.
180 size_t GetStartingOffset() const;
181 // Get the offset for this index.
182 // All offsets are relative to 'captuerd_'.
183 size_t GetCapturedVariableOffset(size_t index) const;
184
185 // Cast the data at '(char*)captured_[offset]' into T, returning its address.
186 // This value should not be de-referenced directly since its unaligned.
187 template <typename T>
188 inline const uint8_t* GetUnsafeAtOffset(size_t offset) const;
189
190 // Copy the data at the offset into the destination. DCHECKs that
191 // the destination_room is large enough (in bytes) to fit the data.
192 template <typename T>
193 inline void CopyUnsafeAtOffset(size_t offset,
194 void* destination,
195 size_t src_size = sizeof(T),
196 size_t destination_room = sizeof(T)) const;
197
198 // Get the closure size from an unaligned (i.e. interior) closure pointer.
199 static size_t GetClosureSize(const uint8_t* closure);
200
201 ///////////////////////////////////////////////////////////////////////////////////
Igor Murashkin457e8742015-10-22 17:37:50 -0700202 // NOTE: Actual fields are declared in ClosureStorage.
Igor Murashkinfc1ccd72015-07-30 15:11:09 -0700203 friend class ClosureTest;
204};
205
Igor Murashkin457e8742015-10-22 17:37:50 -0700206// ABI guarantees:
207// * Closure same size as a ClosureStorage
208// * ClosureStorage begins at the same point a Closure would begin.
209static_assert(sizeof(Closure) == sizeof(ClosureStorage), "Closure size must match ClosureStorage");
210
Igor Murashkinfc1ccd72015-07-30 15:11:09 -0700211} // namespace lambda
212} // namespace art
213
214#endif // ART_RUNTIME_LAMBDA_CLOSURE_H_