Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 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 | #include "dex_compilation_unit.h" |
| 18 | |
Vladimir Marko | c1c3452 | 2018-10-31 13:56:49 +0000 | [diff] [blame] | 19 | #include "art_field.h" |
David Sehr | c431b9d | 2018-03-02 12:01:51 -0800 | [diff] [blame] | 20 | #include "base/utils.h" |
Vladimir Marko | c1c3452 | 2018-10-31 13:56:49 +0000 | [diff] [blame] | 21 | #include "dex/class_accessor-inl.h" |
David Sehr | 9e734c7 | 2018-01-04 17:56:19 -0800 | [diff] [blame] | 22 | #include "dex/code_item_accessors-inl.h" |
David Sehr | b2ec9f5 | 2018-02-21 13:20:31 -0800 | [diff] [blame] | 23 | #include "dex/descriptors_names.h" |
Vladimir Marko | c1c3452 | 2018-10-31 13:56:49 +0000 | [diff] [blame] | 24 | #include "mirror/class-inl.h" |
Mathieu Chartier | 736b560 | 2015-09-02 14:54:11 -0700 | [diff] [blame] | 25 | #include "mirror/dex_cache.h" |
Vladimir Marko | c1c3452 | 2018-10-31 13:56:49 +0000 | [diff] [blame] | 26 | #include "scoped_thread_state_change-inl.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 27 | |
| 28 | namespace art { |
| 29 | |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 30 | DexCompilationUnit::DexCompilationUnit(Handle<mirror::ClassLoader> class_loader, |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 31 | ClassLinker* class_linker, |
| 32 | const DexFile& dex_file, |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 33 | const dex::CodeItem* code_item, |
Ian Rogers | ee39a10 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 34 | uint16_t class_def_idx, |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 35 | uint32_t method_idx, |
Vladimir Marko | 2730db0 | 2014-01-27 11:15:17 +0000 | [diff] [blame] | 36 | uint32_t access_flags, |
Mathieu Chartier | 736b560 | 2015-09-02 14:54:11 -0700 | [diff] [blame] | 37 | const VerifiedMethod* verified_method, |
Vladimir Marko | a2c211c | 2018-11-01 09:50:52 +0000 | [diff] [blame] | 38 | Handle<mirror::DexCache> dex_cache, |
| 39 | Handle<mirror::Class> compiling_class) |
Vladimir Marko | df73984 | 2016-03-23 16:59:07 +0000 | [diff] [blame] | 40 | : class_loader_(class_loader), |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 41 | class_linker_(class_linker), |
| 42 | dex_file_(&dex_file), |
| 43 | code_item_(code_item), |
| 44 | class_def_idx_(class_def_idx), |
| 45 | dex_method_idx_(method_idx), |
Vladimir Marko | 2730db0 | 2014-01-27 11:15:17 +0000 | [diff] [blame] | 46 | access_flags_(access_flags), |
Mathieu Chartier | 736b560 | 2015-09-02 14:54:11 -0700 | [diff] [blame] | 47 | verified_method_(verified_method), |
Mathieu Chartier | 73f21d4 | 2018-01-02 14:26:50 -0800 | [diff] [blame] | 48 | dex_cache_(dex_cache), |
Vladimir Marko | a2c211c | 2018-11-01 09:50:52 +0000 | [diff] [blame] | 49 | code_item_accessor_(dex_file, code_item), |
| 50 | compiling_class_(compiling_class) {} |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 51 | |
| 52 | const std::string& DexCompilationUnit::GetSymbol() { |
| 53 | if (symbol_.empty()) { |
| 54 | symbol_ = "dex_"; |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 55 | symbol_ += MangleForJni(dex_file_->PrettyMethod(dex_method_idx_)); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 56 | } |
| 57 | return symbol_; |
| 58 | } |
| 59 | |
Vladimir Marko | c1c3452 | 2018-10-31 13:56:49 +0000 | [diff] [blame] | 60 | bool DexCompilationUnit::RequiresConstructorBarrier() const { |
| 61 | // Constructor barriers are applicable only for <init> methods. |
| 62 | DCHECK(!IsStatic()); |
| 63 | DCHECK(IsConstructor()); |
| 64 | |
| 65 | // We require a constructor barrier if there are final instance fields. |
| 66 | if (GetCompilingClass().GetReference() != nullptr && !GetCompilingClass().IsNull()) { |
| 67 | // Decoding class data can be slow, so iterate over fields of the compiling class if resolved. |
| 68 | ScopedObjectAccess soa(Thread::Current()); |
| 69 | ObjPtr<mirror::Class> compiling_class = GetCompilingClass().Get(); |
| 70 | for (size_t i = 0, size = compiling_class->NumInstanceFields(); i != size; ++i) { |
| 71 | ArtField* field = compiling_class->GetInstanceField(i); |
| 72 | if (field->IsFinal()) { |
| 73 | return true; |
| 74 | } |
| 75 | } |
| 76 | } else { |
| 77 | // Iterate over field definitions in the class data. |
| 78 | ClassAccessor accessor(*GetDexFile(), GetClassDefIndex()); |
| 79 | for (const ClassAccessor::Field& field : accessor.GetInstanceFields()) { |
| 80 | if (field.IsFinal()) { |
| 81 | return true; |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | return false; |
| 86 | } |
| 87 | |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 88 | } // namespace art |