Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 1 | // Copyright 2011 Google Inc. All Rights Reserved. |
| 2 | |
| 3 | #include "src/dex_file.h" |
| 4 | #include "src/heap.h" |
| 5 | #include "src/globals.h" |
| 6 | #include "src/logging.h" |
| 7 | #include "src/object.h" |
| 8 | #include "src/raw_dex_file.h" |
| 9 | |
| 10 | namespace art { |
| 11 | |
Carl Shapiro | 80d4dde | 2011-06-28 16:24:07 -0700 | [diff] [blame] | 12 | DexFile* DexFile::OpenFile(const char* filename) { |
| 13 | RawDexFile* raw = RawDexFile::OpenFile(filename); |
| 14 | return Open(raw); |
| 15 | } |
| 16 | |
| 17 | DexFile* DexFile::OpenBase64(const char* base64) { |
| 18 | RawDexFile* raw = RawDexFile::OpenBase64(base64); |
| 19 | return Open(raw); |
| 20 | } |
| 21 | |
| 22 | DexFile* DexFile::Open(RawDexFile* raw) { |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 23 | if (raw == NULL) { |
| 24 | return NULL; |
| 25 | } |
| 26 | DexFile* dex_file = new DexFile(raw); |
| 27 | dex_file->Init(); |
| 28 | return dex_file; |
| 29 | } |
| 30 | |
| 31 | DexFile::~DexFile() { |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 32 | delete[] strings_; |
| 33 | delete[] classes_; |
| 34 | delete[] methods_; |
| 35 | delete[] fields_; |
| 36 | } |
| 37 | |
| 38 | void DexFile::Init() { |
| 39 | num_strings_ = raw_->NumStringIds(); |
| 40 | strings_ = new String*[num_strings_]; |
| 41 | |
| 42 | num_classes_ = raw_->NumTypeIds(); |
| 43 | classes_ = new Class*[num_classes_]; |
| 44 | |
| 45 | num_methods_ = raw_->NumMethodIds(); |
| 46 | methods_ = new Method*[num_methods_]; |
| 47 | |
| 48 | num_fields_ = raw_->NumFieldIds(); |
| 49 | fields_ = new Field*[num_fields_]; |
| 50 | } |
| 51 | |
| 52 | Class* DexFile::LoadClass(const char* descriptor) { |
| 53 | const RawDexFile::ClassDef* class_def = raw_->FindClassDef(descriptor); |
| 54 | if (class_def == NULL) { |
| 55 | return NULL; |
| 56 | } else { |
| 57 | return LoadClass(*class_def); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | Class* DexFile::LoadClass(const RawDexFile::ClassDef& class_def) { |
| 62 | const byte* class_data = raw_->GetClassData(class_def); |
Carl Shapiro | 3ee755d | 2011-06-28 12:11:04 -0700 | [diff] [blame] | 63 | RawDexFile::ClassDataHeader header = raw_->ReadClassDataHeader(&class_data); |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 64 | |
| 65 | const char* descriptor = raw_->GetClassDescriptor(class_def); |
| 66 | CHECK(descriptor != NULL); |
| 67 | |
| 68 | // Allocate storage for the new class object. |
| 69 | size_t size = Class::Size(header.static_fields_size_); |
| 70 | Class* klass = Heap::AllocClass(size); |
Carl Shapiro | 3ee755d | 2011-06-28 12:11:04 -0700 | [diff] [blame] | 71 | CHECK(klass != NULL); // TODO: throw an OOME |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 72 | |
Carl Shapiro | 3ee755d | 2011-06-28 12:11:04 -0700 | [diff] [blame] | 73 | klass->klass_ = NULL; // TODO |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 74 | klass->descriptor_ = descriptor; |
| 75 | klass->access_flags_ = class_def.access_flags_; |
| 76 | klass->class_loader_ = NULL; // TODO |
| 77 | klass->dex_file_ = this; |
| 78 | klass->primitive_type_ = Class::kPrimNot; |
Carl Shapiro | 894d0fa | 2011-06-30 14:48:49 -0700 | [diff] [blame^] | 79 | klass->status_ = Class::kStatusIdx; |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 80 | |
| 81 | klass->super_ = reinterpret_cast<Class*>(class_def.superclass_idx_); |
Carl Shapiro | 894d0fa | 2011-06-30 14:48:49 -0700 | [diff] [blame^] | 82 | klass->super_idx_ = class_def.superclass_idx_; |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 83 | |
Carl Shapiro | 3ee755d | 2011-06-28 12:11:04 -0700 | [diff] [blame] | 84 | klass->num_sfields_ = header.static_fields_size_; |
| 85 | klass->num_ifields_ = header.instance_fields_size_; |
| 86 | klass->num_dmethods_ = header.direct_methods_size_; |
| 87 | klass->num_vmethods_ = header.virtual_methods_size_; |
| 88 | |
| 89 | klass->source_file_ = raw_->dexGetSourceFile(class_def); |
| 90 | |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 91 | // Load class interfaces. |
Carl Shapiro | 3ee755d | 2011-06-28 12:11:04 -0700 | [diff] [blame] | 92 | LoadInterfaces(class_def, klass); |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 93 | |
| 94 | // Load static fields. |
Carl Shapiro | 3ee755d | 2011-06-28 12:11:04 -0700 | [diff] [blame] | 95 | if (klass->num_sfields_ != 0) { |
| 96 | uint32_t last_idx = 0; |
| 97 | for (size_t i = 0; i < klass->num_sfields_; ++i) { |
| 98 | RawDexFile::Field raw_field; |
| 99 | raw_->dexReadClassDataField(&class_data, &raw_field, &last_idx); |
| 100 | LoadField(klass, raw_field, &klass->sfields_[i]); |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 101 | } |
| 102 | } |
| 103 | |
| 104 | // Load instance fields. |
Carl Shapiro | 3ee755d | 2011-06-28 12:11:04 -0700 | [diff] [blame] | 105 | if (klass->num_ifields_ != 0) { |
| 106 | // TODO: append instance fields to class object |
| 107 | klass->ifields_ = new IField[klass->num_ifields_]; |
| 108 | uint32_t last_idx = 0; |
| 109 | for (size_t i = 0; i < klass->num_ifields_; ++i) { |
| 110 | RawDexFile::Field raw_field; |
| 111 | raw_->dexReadClassDataField(&class_data, &raw_field, &last_idx); |
| 112 | LoadField(klass, raw_field, &klass->ifields_[i]); |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 113 | } |
| 114 | } |
| 115 | |
| 116 | // Load direct methods. |
Carl Shapiro | 3ee755d | 2011-06-28 12:11:04 -0700 | [diff] [blame] | 117 | if (klass->num_dmethods_ != 0) { |
| 118 | // TODO: append direct methods to class object |
| 119 | klass->dmethods_ = new Method[klass->num_dmethods_]; |
| 120 | uint32_t last_idx = 0; |
| 121 | for (size_t i = 0; i < klass->num_dmethods_; ++i) { |
| 122 | RawDexFile::Method raw_method; |
| 123 | raw_->dexReadClassDataMethod(&class_data, &raw_method, &last_idx); |
| 124 | LoadMethod(klass, raw_method, &klass->dmethods_[i]); |
| 125 | // TODO: register maps |
| 126 | } |
| 127 | } |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 128 | |
| 129 | // Load virtual methods. |
Carl Shapiro | 3ee755d | 2011-06-28 12:11:04 -0700 | [diff] [blame] | 130 | if (klass->num_vmethods_ != 0) { |
| 131 | // TODO: append virtual methods to class object |
| 132 | klass->vmethods_ = new Method[klass->num_vmethods_]; |
| 133 | memset(klass->vmethods_, 0xff, sizeof(Method)); |
| 134 | uint32_t last_idx = 0; |
| 135 | for (size_t i = 0; i < klass->num_vmethods_; ++i) { |
| 136 | RawDexFile::Method raw_method; |
| 137 | raw_->dexReadClassDataMethod(&class_data, &raw_method, &last_idx); |
| 138 | LoadMethod(klass, raw_method, &klass->vmethods_[i]); |
| 139 | // TODO: register maps |
| 140 | } |
| 141 | } |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 142 | |
| 143 | return klass; |
| 144 | } |
| 145 | |
Carl Shapiro | 3ee755d | 2011-06-28 12:11:04 -0700 | [diff] [blame] | 146 | void DexFile::LoadInterfaces(const RawDexFile::ClassDef& class_def, |
| 147 | Class* klass) { |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 148 | const RawDexFile::TypeList* list = raw_->GetInterfacesList(class_def); |
| 149 | if (list != NULL) { |
| 150 | klass->interface_count_ = list->Size(); |
| 151 | klass->interfaces_ = new Class*[list->Size()]; |
| 152 | for (size_t i = 0; i < list->Size(); ++i) { |
| 153 | const RawDexFile::TypeItem& type_item = list->GetTypeItem(i); |
| 154 | klass->interfaces_[i] = reinterpret_cast<Class*>(type_item.type_idx_); |
| 155 | } |
| 156 | } |
| 157 | } |
| 158 | |
Carl Shapiro | 3ee755d | 2011-06-28 12:11:04 -0700 | [diff] [blame] | 159 | void DexFile::LoadField(Class* klass, const RawDexFile::Field& src, |
| 160 | Field* dst) { |
| 161 | const RawDexFile::FieldId& field_id = raw_->GetFieldId(src.field_idx_); |
| 162 | dst->klass_ = klass; |
| 163 | dst->name_ = raw_->dexStringById(field_id.name_idx_); |
| 164 | dst->signature_ = raw_->dexStringByTypeIdx(field_id.type_idx_); |
| 165 | dst->access_flags_ = src.access_flags_; |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 166 | } |
| 167 | |
Carl Shapiro | 3ee755d | 2011-06-28 12:11:04 -0700 | [diff] [blame] | 168 | void DexFile::LoadMethod(Class* klass, const RawDexFile::Method& src, |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 169 | Method* dst) { |
Carl Shapiro | 3ee755d | 2011-06-28 12:11:04 -0700 | [diff] [blame] | 170 | const RawDexFile::MethodId& method_id = raw_->GetMethodId(src.method_idx_); |
| 171 | dst->klass_ = klass; |
| 172 | dst->name_ = raw_->dexStringById(method_id.name_idx_); |
| 173 | dst->dex_file_ = this; |
| 174 | dst->proto_idx_ = method_id.proto_idx_; |
| 175 | dst->shorty_ = raw_->GetShorty(method_id.proto_idx_); |
| 176 | dst->access_flags_ = src.access_flags_; |
| 177 | |
| 178 | // TODO: check for finalize method |
| 179 | |
| 180 | const RawDexFile::Code* code_item = raw_->GetCode(src); |
| 181 | if (code_item != NULL) { |
| 182 | dst->num_registers_ = code_item->registers_size_; |
| 183 | dst->num_ins_ = code_item->ins_size_; |
| 184 | dst->num_outs_ = code_item->outs_size_; |
| 185 | dst->insns_ = code_item->insns_; |
| 186 | } else { |
| 187 | uint16_t num_args = dst->NumArgRegisters(); |
| 188 | if (!dst->IsStatic()) { |
| 189 | ++num_args; |
| 190 | } |
| 191 | dst->num_registers_ = dst->num_ins_ + num_args; |
| 192 | // TODO: native methods |
| 193 | } |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 194 | } |
| 195 | |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 196 | } // namespace art |