1 /*
2  * Copyright (C) 2012 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 
17 #ifndef ART_COMPILER_DRIVER_DEX_COMPILATION_UNIT_H_
18 #define ART_COMPILER_DRIVER_DEX_COMPILATION_UNIT_H_
19 
20 #include <stdint.h>
21 
22 #include "base/arena_object.h"
23 #include "dex/code_item_accessors.h"
24 #include "dex/dex_file.h"
25 #include "handle.h"
26 
27 namespace art {
28 namespace mirror {
29 class Class;
30 class ClassLoader;
31 class DexCache;
32 }  // namespace mirror
33 class ClassLinker;
34 class VerifiedMethod;
35 
36 class DexCompilationUnit : public DeletableArenaObject<kArenaAllocMisc> {
37  public:
38   DexCompilationUnit(Handle<mirror::ClassLoader> class_loader,
39                      ClassLinker* class_linker,
40                      const DexFile& dex_file,
41                      const dex::CodeItem* code_item,
42                      uint16_t class_def_idx,
43                      uint32_t method_idx,
44                      uint32_t access_flags,
45                      const VerifiedMethod* verified_method,
46                      Handle<mirror::DexCache> dex_cache,
47                      Handle<mirror::Class> compiling_class = Handle<mirror::Class>());
48 
GetClassLoader()49   Handle<mirror::ClassLoader> GetClassLoader() const {
50     return class_loader_;
51   }
52 
GetClassLinker()53   ClassLinker* GetClassLinker() const {
54     return class_linker_;
55   }
56 
GetDexFile()57   const DexFile* GetDexFile() const {
58     return dex_file_;
59   }
60 
GetClassDefIndex()61   uint16_t GetClassDefIndex() const {
62     return class_def_idx_;
63   }
64 
GetDexMethodIndex()65   uint32_t GetDexMethodIndex() const {
66     return dex_method_idx_;
67   }
68 
GetCodeItem()69   const dex::CodeItem* GetCodeItem() const {
70     return code_item_;
71   }
72 
GetShorty()73   const char* GetShorty() const {
74     const dex::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
75     return dex_file_->GetMethodShorty(method_id);
76   }
77 
GetShorty(uint32_t * shorty_len)78   const char* GetShorty(uint32_t* shorty_len) const {
79     const dex::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
80     return dex_file_->GetMethodShorty(method_id, shorty_len);
81   }
82 
GetAccessFlags()83   uint32_t GetAccessFlags() const {
84     return access_flags_;
85   }
86 
IsConstructor()87   bool IsConstructor() const {
88     return ((access_flags_ & kAccConstructor) != 0);
89   }
90 
IsNative()91   bool IsNative() const {
92     return ((access_flags_ & kAccNative) != 0);
93   }
94 
IsStatic()95   bool IsStatic() const {
96     return ((access_flags_ & kAccStatic) != 0);
97   }
98 
IsSynchronized()99   bool IsSynchronized() const {
100     return ((access_flags_ & kAccSynchronized) != 0);
101   }
102 
GetVerifiedMethod()103   const VerifiedMethod* GetVerifiedMethod() const {
104     return verified_method_;
105   }
106 
ClearVerifiedMethod()107   void ClearVerifiedMethod() {
108     verified_method_ = nullptr;
109   }
110 
111   const std::string& GetSymbol();
112 
GetDexCache()113   Handle<mirror::DexCache> GetDexCache() const {
114     return dex_cache_;
115   }
116 
GetCodeItemAccessor()117   const CodeItemDataAccessor& GetCodeItemAccessor() const {
118     return code_item_accessor_;
119   }
120 
GetCompilingClass()121   Handle<mirror::Class> GetCompilingClass() const {
122     return compiling_class_;
123   }
124 
125   // Does this <init> method require a constructor barrier (prior to the return)?
126   // The answer is "yes", if and only if the class has any instance final fields.
127   // (This must not be called for any non-<init> methods; the answer would be "no").
128   //
129   // ---
130   //
131   // JLS 17.5.1 "Semantics of final fields" mandates that all final fields are frozen at the end
132   // of the invoked constructor. The constructor barrier is a conservative implementation means of
133   // enforcing the freezes happen-before the object being constructed is observable by another
134   // thread.
135   //
136   // Note: This question only makes sense for instance constructors;
137   // static constructors (despite possibly having finals) never need
138   // a barrier.
139   //
140   // JLS 12.4.2 "Detailed Initialization Procedure" approximately describes
141   // class initialization as:
142   //
143   //   lock(class.lock)
144   //     class.state = initializing
145   //   unlock(class.lock)
146   //
147   //   invoke <clinit>
148   //
149   //   lock(class.lock)
150   //     class.state = initialized
151   //   unlock(class.lock)              <-- acts as a release
152   //
153   // The last operation in the above example acts as an atomic release
154   // for any stores in <clinit>, which ends up being stricter
155   // than what a constructor barrier needs.
156   //
157   // See also QuasiAtomic::ThreadFenceForConstructor().
158   bool RequiresConstructorBarrier() const;
159 
160  private:
161   const Handle<mirror::ClassLoader> class_loader_;
162 
163   ClassLinker* const class_linker_;
164 
165   const DexFile* const dex_file_;
166 
167   const dex::CodeItem* const code_item_;
168   const uint16_t class_def_idx_;
169   const uint32_t dex_method_idx_;
170   const uint32_t access_flags_;
171   const VerifiedMethod* verified_method_;
172 
173   const Handle<mirror::DexCache> dex_cache_;
174 
175   const CodeItemDataAccessor code_item_accessor_;
176 
177   Handle<mirror::Class> compiling_class_;
178 
179   std::string symbol_;
180 };
181 
182 }  // namespace art
183 
184 #endif  // ART_COMPILER_DRIVER_DEX_COMPILATION_UNIT_H_
185