blob: 60d117e9e26dd37ac8a98473df74a96f69182466 [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
36// Inline representation of a lambda closure.
37// Contains the target method and the set of packed captured variables as a copy.
38//
39// The closure itself is logically immutable, although in practice any object references
40// it (recursively) contains can be moved and updated by the GC.
41struct PACKED(sizeof(ArtLambdaMethod*)) Closure {
42 // Get the size of the Closure in bytes.
43 // This is necessary in order to allocate a large enough area to copy the Closure into.
44 // Do *not* copy the closure with memcpy, since references also need to get moved.
45 size_t GetSize() const;
46
47 // Copy this closure into the target, whose memory size is specified by target_size.
48 // Any object references are fixed up during the copy (if there was a read barrier).
49 // The target_size must be at least as large as GetSize().
50 void CopyTo(void* target, size_t target_size) const;
51
Igor Murashkinfc1ccd72015-07-30 15:11:09 -070052 // How many variables were captured?
53 size_t GetNumberOfCapturedVariables() const;
54
55 // Returns a type descriptor string that represents each captured variable.
56 // e.g. "Ljava/lang/Object;ZB" would mean a capture tuple of (Object, boolean, byte)
57 const char* GetCapturedVariablesTypeDescriptor() const;
58
59 // Returns the short type for the captured variable at index.
60 // Index must be less than the number of captured variables.
61 ShortyFieldType GetCapturedShortyType(size_t index) const;
62
63 // Returns the 32-bit representation of a non-wide primitive at the captured variable index.
64 // Smaller types are zero extended.
65 // Index must be less than the number of captured variables.
66 uint32_t GetCapturedPrimitiveNarrow(size_t index) const;
67 // Returns the 64-bit representation of a wide primitive at the captured variable index.
68 // Smaller types are zero extended.
69 // Index must be less than the number of captured variables.
70 uint64_t GetCapturedPrimitiveWide(size_t index) const;
71 // Returns the object reference at the captured variable index.
72 // The type at the index *must* be an object reference or a CHECK failure will occur.
73 // Index must be less than the number of captured variables.
74 mirror::Object* GetCapturedObject(size_t index) const SHARED_REQUIRES(Locks::mutator_lock_);
75
76 // Gets the size of a nested capture closure in bytes, at the captured variable index.
77 // The type at the index *must* be a lambda closure or a CHECK failure will occur.
78 size_t GetCapturedClosureSize(size_t index) const;
79
80 // Copies a nested lambda closure at the captured variable index.
81 // The destination must have enough room for the closure (see GetCapturedClosureSize).
82 void CopyCapturedClosure(size_t index, void* destination, size_t destination_room) const;
83
84 private:
85 // Read out any non-lambda value as a copy.
86 template <typename T>
87 T GetCapturedVariable(size_t index) const;
88
89 // Reconstruct the closure's captured variable info at runtime.
90 struct VariableInfo {
91 size_t index_;
92 ShortyFieldType variable_type_;
93 size_t offset_;
94 size_t count_;
95
96 enum Flags {
97 kIndex = 0x1,
98 kVariableType = 0x2,
99 kOffset = 0x4,
100 kCount = 0x8,
101 };
102
103 // Traverse to the end of the type descriptor list instead of stopping at some particular index.
104 static constexpr size_t kUpToIndexMax = static_cast<size_t>(-1);
105 };
106
107 // Parse a type descriptor, stopping at index "upto_index".
108 // Returns only the information requested in flags. All other fields are indeterminate.
109 template <VariableInfo::Flags flags>
110 inline VariableInfo ALWAYS_INLINE ParseTypeDescriptor(const char* type_descriptor,
111 size_t upto_index) const;
112
113 // Convenience function to call ParseTypeDescriptor with just the type and offset.
114 void GetCapturedVariableTypeAndOffset(size_t index,
115 ShortyFieldType* out_type,
116 size_t* out_offset) const;
117
118 // How many bytes do the captured variables take up? Runtime sizeof(captured_variables).
119 size_t GetCapturedVariablesSize() const;
120 // Get the size in bytes of the variable_type which is potentially stored at offset.
121 size_t GetCapturedVariableSize(ShortyFieldType variable_type, size_t offset) const;
122 // Get the starting offset (in bytes) for the 0th captured variable.
123 // All offsets are relative to 'captured_'.
124 size_t GetStartingOffset() const;
125 // Get the offset for this index.
126 // All offsets are relative to 'captuerd_'.
127 size_t GetCapturedVariableOffset(size_t index) const;
128
129 // Cast the data at '(char*)captured_[offset]' into T, returning its address.
130 // This value should not be de-referenced directly since its unaligned.
131 template <typename T>
132 inline const uint8_t* GetUnsafeAtOffset(size_t offset) const;
133
134 // Copy the data at the offset into the destination. DCHECKs that
135 // the destination_room is large enough (in bytes) to fit the data.
136 template <typename T>
137 inline void CopyUnsafeAtOffset(size_t offset,
138 void* destination,
139 size_t src_size = sizeof(T),
140 size_t destination_room = sizeof(T)) const;
141
142 // Get the closure size from an unaligned (i.e. interior) closure pointer.
143 static size_t GetClosureSize(const uint8_t* closure);
144
145 ///////////////////////////////////////////////////////////////////////////////////
146
147 // Compile-time known lambda information such as the type descriptor and size.
148 ArtLambdaMethod* lambda_info_;
149
150 // A contiguous list of captured variables, and possibly the closure size.
151 // The runtime size can always be determined through GetSize().
152 union {
153 // Read from here if the closure size is static (ArtLambdaMethod::IsStatic)
154 uint8_t static_variables_[0];
155 struct {
156 // Read from here if the closure size is dynamic (ArtLambdaMethod::IsDynamic)
157 size_t size_; // The lambda_info_ and the size_ itself is also included as part of the size.
158 uint8_t variables_[0];
159 } dynamic_;
160 } captured_[0];
161 // captured_ will always consist of one array element at runtime.
162 // Set to [0] so that 'size_' is not counted in sizeof(Closure).
163
164 friend class ClosureBuilder;
165 friend class ClosureTest;
166};
167
168} // namespace lambda
169} // namespace art
170
171#endif // ART_RUNTIME_LAMBDA_CLOSURE_H_