Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 1 | // Copyright 2011 Google Inc. All Rights Reserved. |
| 2 | |
| 3 | #include <stdio.h> |
| 4 | #include <stdlib.h> |
Elliott Hughes | 234da57 | 2011-11-03 22:13:06 -0700 | [diff] [blame] | 5 | #include <sys/file.h> |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 6 | |
| 7 | #include <string> |
| 8 | #include <vector> |
| 9 | |
| 10 | #include "class_linker.h" |
| 11 | #include "class_loader.h" |
| 12 | #include "compiler.h" |
Ian Rogers | 5e863dd | 2011-11-02 20:00:10 -0700 | [diff] [blame] | 13 | #include "file.h" |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 14 | #include "image_writer.h" |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 15 | #include "oat_writer.h" |
Ian Rogers | 5e863dd | 2011-11-02 20:00:10 -0700 | [diff] [blame] | 16 | #include "os.h" |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 17 | #include "runtime.h" |
| 18 | #include "stringpiece.h" |
| 19 | |
| 20 | namespace art { |
| 21 | |
| 22 | static void usage() { |
| 23 | fprintf(stderr, |
| 24 | "Usage: dex2oat [options]...\n" |
| 25 | "\n"); |
| 26 | fprintf(stderr, |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 27 | " --dex-file=<dex-file>: specifies a .dex file to compile. At least one .dex\n" |
| 28 | " file must be specified. \n" |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 29 | " Example: --dex-file=/system/framework/core.jar\n" |
| 30 | "\n"); |
| 31 | fprintf(stderr, |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 32 | " --image=<file.art>: specifies the required output image filename.\n" |
Brian Carlstrom | 47a0d5a | 2011-10-12 21:20:05 -0700 | [diff] [blame] | 33 | " Example: --image=/data/art-cache/boot.art\n" |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 34 | "\n"); |
| 35 | // TODO: remove this by inferring from --image |
| 36 | fprintf(stderr, |
| 37 | " --oat=<file.oat>: specifies the required oat filename.\n" |
Brian Carlstrom | 47a0d5a | 2011-10-12 21:20:05 -0700 | [diff] [blame] | 38 | " Example: --image=/data/art-cache/boot.oat\n" |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 39 | "\n"); |
| 40 | fprintf(stderr, |
| 41 | " --base=<hex-address>: specifies the base address when creating a boot image.\n" |
| 42 | " Example: --base=0x50000000\n" |
| 43 | "\n"); |
| 44 | fprintf(stderr, |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 45 | " --boot-image=<file.art>: provide the image file for the boot class path.\n" |
Brian Carlstrom | 47a0d5a | 2011-10-12 21:20:05 -0700 | [diff] [blame] | 46 | " Example: --boot-image=/data/art-cache/boot.art\n" |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 47 | "\n"); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 48 | fprintf(stderr, |
| 49 | " --method may be used to limit compilation to a subset of methods.\n" |
| 50 | " Example: --method=Ljava/lang/Object;<init>()V\n" |
| 51 | "\n"); |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 52 | fprintf(stderr, |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 53 | " --host-prefix may be used to translate host paths to target paths during\n" |
| 54 | " cross compilation.\n" |
| 55 | " Example: --host-prefix=out/target/product/crespo\n" |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 56 | "\n"); |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 57 | fprintf(stderr, |
jeffhao | 5d84040 | 2011-10-24 17:09:45 -0700 | [diff] [blame] | 58 | " --runtime-arg <argument>: used to specify various arguments for the runtime,\n" |
| 59 | " such as initial heap size, maximum heap size, and verbose output.\n" |
| 60 | " Use a separate --runtime-arg switch for each argument.\n" |
| 61 | " Example: --runtime-arg -Xms256m\n" |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 62 | "\n"); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 63 | exit(EXIT_FAILURE); |
| 64 | } |
| 65 | |
Elliott Hughes | 234da57 | 2011-11-03 22:13:06 -0700 | [diff] [blame] | 66 | class FileJanitor { |
| 67 | public: |
| 68 | FileJanitor(const std::string& filename, int fd) |
| 69 | : filename_(filename), fd_(fd), do_unlink_(true) { |
| 70 | } |
| 71 | |
| 72 | void KeepFile() { |
| 73 | do_unlink_ = false; |
| 74 | } |
| 75 | |
| 76 | ~FileJanitor() { |
| 77 | if (fd_ != -1) { |
| 78 | flock(fd_, LOCK_UN); |
| 79 | } |
| 80 | if (do_unlink_) { |
| 81 | unlink(filename_.c_str()); |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | private: |
| 86 | std::string filename_; |
| 87 | int fd_; |
| 88 | bool do_unlink_; |
| 89 | }; |
| 90 | |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 91 | int dex2oat(int argc, char** argv) { |
| 92 | // Skip over argv[0]. |
| 93 | argv++; |
| 94 | argc--; |
| 95 | |
| 96 | if (argc == 0) { |
| 97 | fprintf(stderr, "no arguments specified\n"); |
| 98 | usage(); |
| 99 | } |
| 100 | |
| 101 | std::vector<const char*> dex_filenames; |
| 102 | std::vector<const char*> method_names; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 103 | std::string oat_filename; |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 104 | const char* image_filename = NULL; |
| 105 | std::string boot_image_option; |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 106 | uintptr_t image_base = 0; |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 107 | std::string host_prefix; |
jeffhao | 5d84040 | 2011-10-24 17:09:45 -0700 | [diff] [blame] | 108 | std::vector<const char*> runtime_args; |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 109 | |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 110 | for (int i = 0; i < argc; i++) { |
| 111 | const StringPiece option(argv[i]); |
Brian Carlstrom | b7bbba4 | 2011-10-13 14:58:47 -0700 | [diff] [blame] | 112 | if (false) { |
| 113 | LOG(INFO) << "dex2oat: option[" << i << "]=" << argv[i]; |
| 114 | } |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 115 | if (option.starts_with("--dex-file=")) { |
| 116 | dex_filenames.push_back(option.substr(strlen("--dex-file=")).data()); |
| 117 | } else if (option.starts_with("--method=")) { |
| 118 | method_names.push_back(option.substr(strlen("--method=")).data()); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 119 | } else if (option.starts_with("--oat=")) { |
| 120 | oat_filename = option.substr(strlen("--oat=")).data(); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 121 | } else if (option.starts_with("--image=")) { |
| 122 | image_filename = option.substr(strlen("--image=")).data(); |
| 123 | } else if (option.starts_with("--base=")) { |
| 124 | const char* image_base_str = option.substr(strlen("--base=")).data(); |
| 125 | char* end; |
| 126 | image_base = strtoul(image_base_str, &end, 16); |
| 127 | if (end == image_base_str || *end != '\0') { |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 128 | fprintf(stderr, "Failed to parse hexadecimal value for option %s\n", option.data()); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 129 | usage(); |
| 130 | } |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 131 | } else if (option.starts_with("--boot-image=")) { |
| 132 | const char* boot_image_filename = option.substr(strlen("--boot-image=")).data(); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 133 | boot_image_option.clear(); |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 134 | boot_image_option += "-Ximage:"; |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 135 | boot_image_option += boot_image_filename; |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 136 | } else if (option.starts_with("--host-prefix=")) { |
| 137 | host_prefix = option.substr(strlen("--host-prefix=")).data(); |
jeffhao | 5d84040 | 2011-10-24 17:09:45 -0700 | [diff] [blame] | 138 | } else if (option == "--runtime-arg") { |
| 139 | if (++i >= argc) { |
| 140 | fprintf(stderr, "Missing required argument for --runtime-arg\n"); |
| 141 | usage(); |
| 142 | } |
| 143 | runtime_args.push_back(argv[i]); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 144 | } else { |
| 145 | fprintf(stderr, "unknown argument %s\n", option.data()); |
| 146 | usage(); |
| 147 | } |
| 148 | } |
| 149 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 150 | if (oat_filename == NULL) { |
Elliott Hughes | 362f9bc | 2011-10-17 18:56:41 -0700 | [diff] [blame] | 151 | fprintf(stderr, "--oat file name not specified\n"); |
| 152 | return EXIT_FAILURE; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 153 | } |
| 154 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 155 | if (image_filename == NULL && boot_image_option.empty()) { |
Elliott Hughes | 362f9bc | 2011-10-17 18:56:41 -0700 | [diff] [blame] | 156 | fprintf(stderr, "Either --image or --boot-image must be specified\n"); |
| 157 | return EXIT_FAILURE; |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 158 | } |
| 159 | |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 160 | if (dex_filenames.empty()) { |
Elliott Hughes | 362f9bc | 2011-10-17 18:56:41 -0700 | [diff] [blame] | 161 | fprintf(stderr, "no --dex-file values specified\n"); |
| 162 | return EXIT_FAILURE; |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 163 | } |
| 164 | |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 165 | if (boot_image_option.empty()) { |
| 166 | if (image_base == 0) { |
| 167 | fprintf(stderr, "non-zero --base not specified\n"); |
| 168 | return EXIT_FAILURE; |
| 169 | } |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 170 | } |
| 171 | |
Elliott Hughes | 234da57 | 2011-11-03 22:13:06 -0700 | [diff] [blame] | 172 | // Create the output file if we can, or open it read-only if we weren't first. |
| 173 | bool did_create = true; |
| 174 | int fd = open(oat_filename.c_str(), O_EXCL | O_CREAT | O_TRUNC | O_RDWR, 0666); |
| 175 | if (fd == -1) { |
| 176 | if (errno != EEXIST) { |
Ian Rogers | 5e863dd | 2011-11-02 20:00:10 -0700 | [diff] [blame] | 177 | PLOG(ERROR) << "Unable to create oat file " << oat_filename; |
| 178 | return EXIT_FAILURE; |
| 179 | } |
Elliott Hughes | 234da57 | 2011-11-03 22:13:06 -0700 | [diff] [blame] | 180 | did_create = false; |
| 181 | fd = open(oat_filename.c_str(), O_RDONLY); |
| 182 | if (fd == -1) { |
| 183 | PLOG(ERROR) << "Unable to open oat file for reading " << oat_filename; |
| 184 | return EXIT_FAILURE; |
| 185 | } |
Ian Rogers | 5e863dd | 2011-11-02 20:00:10 -0700 | [diff] [blame] | 186 | } |
Elliott Hughes | 234da57 | 2011-11-03 22:13:06 -0700 | [diff] [blame] | 187 | |
| 188 | // Handles removing the file on failure and unlocking on both failure and success. |
| 189 | FileJanitor file_janitor(oat_filename, fd); |
| 190 | |
| 191 | // TODO: TEMP_FAILURE_RETRY on flock calls |
| 192 | |
| 193 | // If we won the creation race, block trying to take the lock (since we're going to be doing |
| 194 | // the work, we need the lock). If we lost the creation race, spin trying to take the lock |
| 195 | // non-blocking until we fail -- at which point we know the other guy has the lock -- and then |
| 196 | // block trying to take the now-taken lock. |
| 197 | if (did_create) { |
| 198 | LOG(INFO) << "This process created " << oat_filename; |
| 199 | while (flock(fd, LOCK_EX) != 0) { |
| 200 | // Try again. |
| 201 | } |
| 202 | LOG(INFO) << "This process created and locked " << oat_filename; |
| 203 | } else { |
| 204 | LOG(INFO) << "Another process has already created " << oat_filename; |
| 205 | while (flock(fd, LOCK_EX | LOCK_NB) == 0) { |
| 206 | // Give up the lock and hope the creator has taken the lock next time round. |
| 207 | flock(fd, LOCK_UN); |
| 208 | } |
| 209 | // Now a non-blocking attempt to take the lock has failed, we know the other guy has the |
| 210 | // lock, so block waiting to take it. |
| 211 | LOG(INFO) << "Another process is already working on " << oat_filename; |
| 212 | if (flock(fd, LOCK_EX) != 0) { |
| 213 | PLOG(ERROR) << "Waiter unable to wait for creator to finish " << oat_filename; |
| 214 | return EXIT_FAILURE; |
| 215 | } |
| 216 | // We have the lock and the creator has finished. |
| 217 | // TODO: check the creator did a good job by checking the header. |
| 218 | LOG(INFO) << "Another process finished working on " << oat_filename; |
| 219 | // Job done. |
| 220 | file_janitor.KeepFile(); |
| 221 | return EXIT_SUCCESS; |
| 222 | } |
| 223 | |
| 224 | // If we get this far, we won the creation race and have locked the file. |
| 225 | UniquePtr<File> oat_file(OS::FileFromFd(oat_filename.c_str(), fd)); |
| 226 | |
| 227 | LOG(INFO) << "dex2oat: " << oat_file->name(); |
Ian Rogers | 5e863dd | 2011-11-02 20:00:10 -0700 | [diff] [blame] | 228 | |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 229 | Runtime::Options options; |
Brian Carlstrom | 5de8fe5 | 2011-10-16 14:10:09 -0700 | [diff] [blame] | 230 | options.push_back(std::make_pair("compiler", reinterpret_cast<void*>(NULL))); |
Brian Carlstrom | b7bbba4 | 2011-10-13 14:58:47 -0700 | [diff] [blame] | 231 | std::string boot_class_path_string; |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 232 | if (boot_image_option.empty()) { |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 233 | boot_class_path_string += "-Xbootclasspath:"; |
| 234 | for (size_t i = 0; i < dex_filenames.size()-1; i++) { |
| 235 | boot_class_path_string += dex_filenames[i]; |
| 236 | boot_class_path_string += ":"; |
| 237 | } |
| 238 | boot_class_path_string += dex_filenames[dex_filenames.size()-1]; |
| 239 | options.push_back(std::make_pair(boot_class_path_string.c_str(), reinterpret_cast<void*>(NULL))); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 240 | } else { |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 241 | options.push_back(std::make_pair(boot_image_option.c_str(), reinterpret_cast<void*>(NULL))); |
| 242 | } |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 243 | if (!host_prefix.empty()) { |
| 244 | options.push_back(std::make_pair("host-prefix", host_prefix.c_str())); |
| 245 | } |
jeffhao | 5d84040 | 2011-10-24 17:09:45 -0700 | [diff] [blame] | 246 | for (size_t i = 0; i < runtime_args.size(); i++) { |
| 247 | options.push_back(std::make_pair(runtime_args[i], reinterpret_cast<void*>(NULL))); |
| 248 | } |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 249 | UniquePtr<Runtime> runtime(Runtime::Create(options, false)); |
| 250 | if (runtime.get() == NULL) { |
Ian Rogers | 3fe7957 | 2011-11-02 18:24:30 -0700 | [diff] [blame] | 251 | LOG(ERROR) << "Could not create runtime"; |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 252 | return EXIT_FAILURE; |
| 253 | } |
| 254 | ClassLinker* class_linker = runtime->GetClassLinker(); |
| 255 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 256 | // If we have an existing boot image, position new space after its oat file |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 257 | if (Heap::GetSpaces().size() > 1) { |
| 258 | Space* last_image_space = Heap::GetSpaces()[Heap::GetSpaces().size()-2]; |
| 259 | CHECK(last_image_space != NULL); |
| 260 | CHECK(last_image_space->IsImageSpace()); |
| 261 | CHECK(!Heap::GetSpaces()[Heap::GetSpaces().size()-1]->IsImageSpace()); |
| 262 | byte* oat_limit_addr = last_image_space->GetImageHeader().GetOatLimitAddr(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 263 | image_base = RoundUp(reinterpret_cast<uintptr_t>(oat_limit_addr), kPageSize); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | // ClassLoader creation needs to come after Runtime::Create |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 267 | SirtRef<ClassLoader> class_loader(NULL); |
| 268 | if (!boot_image_option.empty()) { |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 269 | std::vector<const DexFile*> dex_files; |
| 270 | DexFile::OpenDexFiles(dex_filenames, dex_files, host_prefix); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 271 | for (size_t i = 0; i < dex_files.size(); i++) { |
| 272 | class_linker->RegisterDexFile(*dex_files[i]); |
| 273 | } |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 274 | class_loader.reset(PathClassLoader::AllocCompileTime(dex_files)); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 275 | } |
| 276 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 277 | // if we loaded an existing image, we will reuse values from the image roots. |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 278 | if (!runtime->HasJniStubArray()) { |
| 279 | runtime->SetJniStubArray(JniCompiler::CreateJniStub(kThumb2)); |
| 280 | } |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 281 | if (!runtime->HasAbstractMethodErrorStubArray()) { |
| 282 | runtime->SetAbstractMethodErrorStubArray(Compiler::CreateAbstractMethodErrorStub(kThumb2)); |
| 283 | } |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 284 | for (int i = 0; i < Runtime::kLastTrampolineMethodType; i++) { |
Ian Rogers | 1cb0a1d | 2011-10-06 15:24:35 -0700 | [diff] [blame] | 285 | Runtime::TrampolineType type = Runtime::TrampolineType(i); |
| 286 | if (!runtime->HasResolutionStubArray(type)) { |
| 287 | runtime->SetResolutionStubArray(Compiler::CreateResolutionStub(kThumb2, type), type); |
| 288 | } |
Ian Rogers | ad25ac5 | 2011-10-04 19:13:33 -0700 | [diff] [blame] | 289 | } |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 290 | for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) { |
| 291 | Runtime::CalleeSaveType type = Runtime::CalleeSaveType(i); |
| 292 | if (!runtime->HasCalleeSaveMethod(type)) { |
| 293 | runtime->SetCalleeSaveMethod(runtime->CreateCalleeSaveMethod(kThumb2, type), type); |
| 294 | } |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 295 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 296 | Compiler compiler(kThumb2, image_filename != NULL); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 297 | if (method_names.empty()) { |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 298 | compiler.CompileAll(class_loader.get()); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 299 | } else { |
| 300 | for (size_t i = 0; i < method_names.size(); i++) { |
| 301 | // names are actually class_descriptor + name + signature. |
| 302 | // example: Ljava/lang/Object;<init>()V |
| 303 | StringPiece method_name = method_names[i]; |
| 304 | size_t end_of_class_descriptor = method_name.find(';'); |
| 305 | if (end_of_class_descriptor == method_name.npos) { |
Ian Rogers | 3fe7957 | 2011-11-02 18:24:30 -0700 | [diff] [blame] | 306 | LOG(ERROR) << "Could not find class descriptor in method " << method_name << "'"; |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 307 | return EXIT_FAILURE; |
| 308 | } |
| 309 | end_of_class_descriptor++; // want to include ; |
| 310 | std::string class_descriptor = method_name.substr(0, end_of_class_descriptor).ToString(); |
| 311 | size_t end_of_name = method_name.find('(', end_of_class_descriptor); |
| 312 | if (end_of_name == method_name.npos) { |
Ian Rogers | 3fe7957 | 2011-11-02 18:24:30 -0700 | [diff] [blame] | 313 | LOG(ERROR) << "Could not find start of method signature in method '" << method_name << "'"; |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 314 | return EXIT_FAILURE; |
| 315 | } |
| 316 | std::string name = method_name.substr(end_of_class_descriptor, |
| 317 | end_of_name - end_of_class_descriptor).ToString(); |
| 318 | std::string signature = method_name.substr(end_of_name).ToString(); |
| 319 | |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 320 | Class* klass = class_linker->FindClass(class_descriptor, class_loader.get()); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 321 | if (klass == NULL) { |
Ian Rogers | 3fe7957 | 2011-11-02 18:24:30 -0700 | [diff] [blame] | 322 | LOG(ERROR) << "Could not find class for descriptor '" << class_descriptor |
| 323 | << "' in method '" << method_name << "'"; |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 324 | return EXIT_FAILURE; |
| 325 | } |
| 326 | Method* method = klass->FindDirectMethod(name, signature); |
| 327 | if (method == NULL) { |
| 328 | method = klass->FindVirtualMethod(name, signature); |
| 329 | } |
| 330 | if (method == NULL) { |
Ian Rogers | 3fe7957 | 2011-11-02 18:24:30 -0700 | [diff] [blame] | 331 | LOG(ERROR) << "Could not find method '" << method_name << "' with signature '" |
| 332 | << signature << "' in class '" << class_descriptor << "' for method argument '" |
| 333 | << method_name << "'"; |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 334 | return EXIT_FAILURE; |
| 335 | } |
| 336 | compiler.CompileOne(method); |
| 337 | } |
| 338 | } |
| 339 | |
Elliott Hughes | 234da57 | 2011-11-03 22:13:06 -0700 | [diff] [blame] | 340 | if (!OatWriter::Create(oat_file.get(), class_loader.get(), compiler)) { |
| 341 | LOG(ERROR) << "Failed to create oat file " << oat_file->name(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 342 | return EXIT_FAILURE; |
| 343 | } |
| 344 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 345 | if (image_filename == NULL) { |
Ian Rogers | 9717051 | 2011-11-06 21:06:20 -0800 | [diff] [blame^] | 346 | file_janitor.KeepFile(); |
| 347 | LOG(INFO) << "Oat file written successfully " << oat_file->name(); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 348 | return EXIT_SUCCESS; |
| 349 | } |
| 350 | CHECK(compiler.IsImage()); |
| 351 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 352 | ImageWriter image_writer; |
Elliott Hughes | 234da57 | 2011-11-03 22:13:06 -0700 | [diff] [blame] | 353 | if (!image_writer.Write(image_filename, image_base, oat_file->name(), host_prefix)) { |
Ian Rogers | 3fe7957 | 2011-11-02 18:24:30 -0700 | [diff] [blame] | 354 | LOG(ERROR) << "Failed to create image file " << image_filename; |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 355 | return EXIT_FAILURE; |
| 356 | } |
| 357 | |
Elliott Hughes | 234da57 | 2011-11-03 22:13:06 -0700 | [diff] [blame] | 358 | // We wrote the file successfully, and want to keep it. |
| 359 | LOG(INFO) << "Image written successfully " << image_filename; |
| 360 | file_janitor.KeepFile(); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 361 | return EXIT_SUCCESS; |
| 362 | } |
| 363 | |
| 364 | } // namespace art |
| 365 | |
| 366 | int main(int argc, char** argv) { |
| 367 | return art::dex2oat(argc, argv); |
| 368 | } |