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> |
| 5 | |
| 6 | #include <string> |
| 7 | #include <vector> |
| 8 | |
| 9 | #include "class_linker.h" |
| 10 | #include "class_loader.h" |
| 11 | #include "compiler.h" |
| 12 | #include "image_writer.h" |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 13 | #include "oat_writer.h" |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 14 | #include "runtime.h" |
| 15 | #include "stringpiece.h" |
| 16 | |
| 17 | namespace art { |
| 18 | |
| 19 | static void usage() { |
| 20 | fprintf(stderr, |
| 21 | "Usage: dex2oat [options]...\n" |
| 22 | "\n"); |
| 23 | fprintf(stderr, |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 24 | " --dex-file=<dex-file>: specifies a .dex file to compile. At least one .dex\n" |
| 25 | " file must be specified. \n" |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 26 | " Example: --dex-file=/system/framework/core.jar\n" |
| 27 | "\n"); |
| 28 | fprintf(stderr, |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 29 | " --image=<file.art>: specifies the required output image filename.\n" |
Brian Carlstrom | 47a0d5a | 2011-10-12 21:20:05 -0700 | [diff] [blame] | 30 | " Example: --image=/data/art-cache/boot.art\n" |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 31 | "\n"); |
| 32 | // TODO: remove this by inferring from --image |
| 33 | fprintf(stderr, |
| 34 | " --oat=<file.oat>: specifies the required oat filename.\n" |
Brian Carlstrom | 47a0d5a | 2011-10-12 21:20:05 -0700 | [diff] [blame] | 35 | " Example: --image=/data/art-cache/boot.oat\n" |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 36 | "\n"); |
| 37 | fprintf(stderr, |
| 38 | " --base=<hex-address>: specifies the base address when creating a boot image.\n" |
| 39 | " Example: --base=0x50000000\n" |
| 40 | "\n"); |
| 41 | fprintf(stderr, |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 42 | " --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] | 43 | " Example: --boot-image=/data/art-cache/boot.art\n" |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 44 | "\n"); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 45 | fprintf(stderr, |
| 46 | " --method may be used to limit compilation to a subset of methods.\n" |
| 47 | " Example: --method=Ljava/lang/Object;<init>()V\n" |
| 48 | "\n"); |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 49 | fprintf(stderr, |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 50 | " --host-prefix may be used to translate host paths to target paths during\n" |
| 51 | " cross compilation.\n" |
| 52 | " Example: --host-prefix=out/target/product/crespo\n" |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 53 | "\n"); |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 54 | fprintf(stderr, |
jeffhao | 5d84040 | 2011-10-24 17:09:45 -0700 | [diff] [blame] | 55 | " --runtime-arg <argument>: used to specify various arguments for the runtime,\n" |
| 56 | " such as initial heap size, maximum heap size, and verbose output.\n" |
| 57 | " Use a separate --runtime-arg switch for each argument.\n" |
| 58 | " Example: --runtime-arg -Xms256m\n" |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 59 | "\n"); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 60 | exit(EXIT_FAILURE); |
| 61 | } |
| 62 | |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 63 | int dex2oat(int argc, char** argv) { |
| 64 | // Skip over argv[0]. |
| 65 | argv++; |
| 66 | argc--; |
| 67 | |
| 68 | if (argc == 0) { |
| 69 | fprintf(stderr, "no arguments specified\n"); |
| 70 | usage(); |
| 71 | } |
| 72 | |
| 73 | std::vector<const char*> dex_filenames; |
| 74 | std::vector<const char*> method_names; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 75 | std::string oat_filename; |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 76 | const char* image_filename = NULL; |
| 77 | std::string boot_image_option; |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 78 | uintptr_t image_base = 0; |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 79 | std::string host_prefix; |
jeffhao | 5d84040 | 2011-10-24 17:09:45 -0700 | [diff] [blame] | 80 | std::vector<const char*> runtime_args; |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 81 | |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 82 | for (int i = 0; i < argc; i++) { |
| 83 | const StringPiece option(argv[i]); |
Brian Carlstrom | b7bbba4 | 2011-10-13 14:58:47 -0700 | [diff] [blame] | 84 | if (false) { |
| 85 | LOG(INFO) << "dex2oat: option[" << i << "]=" << argv[i]; |
| 86 | } |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 87 | if (option.starts_with("--dex-file=")) { |
| 88 | dex_filenames.push_back(option.substr(strlen("--dex-file=")).data()); |
| 89 | } else if (option.starts_with("--method=")) { |
| 90 | method_names.push_back(option.substr(strlen("--method=")).data()); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 91 | } else if (option.starts_with("--oat=")) { |
| 92 | oat_filename = option.substr(strlen("--oat=")).data(); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 93 | } else if (option.starts_with("--image=")) { |
| 94 | image_filename = option.substr(strlen("--image=")).data(); |
| 95 | } else if (option.starts_with("--base=")) { |
| 96 | const char* image_base_str = option.substr(strlen("--base=")).data(); |
| 97 | char* end; |
| 98 | image_base = strtoul(image_base_str, &end, 16); |
| 99 | if (end == image_base_str || *end != '\0') { |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 100 | 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] | 101 | usage(); |
| 102 | } |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 103 | } else if (option.starts_with("--boot-image=")) { |
| 104 | const char* boot_image_filename = option.substr(strlen("--boot-image=")).data(); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 105 | boot_image_option.clear(); |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 106 | boot_image_option += "-Ximage:"; |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 107 | boot_image_option += boot_image_filename; |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 108 | } else if (option.starts_with("--host-prefix=")) { |
| 109 | host_prefix = option.substr(strlen("--host-prefix=")).data(); |
jeffhao | 5d84040 | 2011-10-24 17:09:45 -0700 | [diff] [blame] | 110 | } else if (option == "--runtime-arg") { |
| 111 | if (++i >= argc) { |
| 112 | fprintf(stderr, "Missing required argument for --runtime-arg\n"); |
| 113 | usage(); |
| 114 | } |
| 115 | runtime_args.push_back(argv[i]); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 116 | } else { |
| 117 | fprintf(stderr, "unknown argument %s\n", option.data()); |
| 118 | usage(); |
| 119 | } |
| 120 | } |
| 121 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 122 | if (oat_filename == NULL) { |
Elliott Hughes | 362f9bc | 2011-10-17 18:56:41 -0700 | [diff] [blame] | 123 | fprintf(stderr, "--oat file name not specified\n"); |
| 124 | return EXIT_FAILURE; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 125 | } |
| 126 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 127 | if (image_filename == NULL && boot_image_option.empty()) { |
Elliott Hughes | 362f9bc | 2011-10-17 18:56:41 -0700 | [diff] [blame] | 128 | fprintf(stderr, "Either --image or --boot-image must be specified\n"); |
| 129 | return EXIT_FAILURE; |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 130 | } |
| 131 | |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 132 | if (dex_filenames.empty()) { |
Elliott Hughes | 362f9bc | 2011-10-17 18:56:41 -0700 | [diff] [blame] | 133 | fprintf(stderr, "no --dex-file values specified\n"); |
| 134 | return EXIT_FAILURE; |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 135 | } |
| 136 | |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 137 | if (boot_image_option.empty()) { |
| 138 | if (image_base == 0) { |
| 139 | fprintf(stderr, "non-zero --base not specified\n"); |
| 140 | return EXIT_FAILURE; |
| 141 | } |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 142 | } |
| 143 | |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 144 | Runtime::Options options; |
Brian Carlstrom | 5de8fe5 | 2011-10-16 14:10:09 -0700 | [diff] [blame] | 145 | options.push_back(std::make_pair("compiler", reinterpret_cast<void*>(NULL))); |
Brian Carlstrom | b7bbba4 | 2011-10-13 14:58:47 -0700 | [diff] [blame] | 146 | std::string boot_class_path_string; |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 147 | if (boot_image_option.empty()) { |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 148 | boot_class_path_string += "-Xbootclasspath:"; |
| 149 | for (size_t i = 0; i < dex_filenames.size()-1; i++) { |
| 150 | boot_class_path_string += dex_filenames[i]; |
| 151 | boot_class_path_string += ":"; |
| 152 | } |
| 153 | boot_class_path_string += dex_filenames[dex_filenames.size()-1]; |
| 154 | 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] | 155 | } else { |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 156 | options.push_back(std::make_pair(boot_image_option.c_str(), reinterpret_cast<void*>(NULL))); |
| 157 | } |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 158 | if (!host_prefix.empty()) { |
| 159 | options.push_back(std::make_pair("host-prefix", host_prefix.c_str())); |
| 160 | } |
jeffhao | 5d84040 | 2011-10-24 17:09:45 -0700 | [diff] [blame] | 161 | for (size_t i = 0; i < runtime_args.size(); i++) { |
| 162 | options.push_back(std::make_pair(runtime_args[i], reinterpret_cast<void*>(NULL))); |
| 163 | } |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 164 | UniquePtr<Runtime> runtime(Runtime::Create(options, false)); |
| 165 | if (runtime.get() == NULL) { |
| 166 | fprintf(stderr, "could not create runtime\n"); |
| 167 | return EXIT_FAILURE; |
| 168 | } |
| 169 | ClassLinker* class_linker = runtime->GetClassLinker(); |
| 170 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 171 | // 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] | 172 | if (Heap::GetSpaces().size() > 1) { |
| 173 | Space* last_image_space = Heap::GetSpaces()[Heap::GetSpaces().size()-2]; |
| 174 | CHECK(last_image_space != NULL); |
| 175 | CHECK(last_image_space->IsImageSpace()); |
| 176 | CHECK(!Heap::GetSpaces()[Heap::GetSpaces().size()-1]->IsImageSpace()); |
| 177 | byte* oat_limit_addr = last_image_space->GetImageHeader().GetOatLimitAddr(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 178 | image_base = RoundUp(reinterpret_cast<uintptr_t>(oat_limit_addr), kPageSize); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | // ClassLoader creation needs to come after Runtime::Create |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 182 | SirtRef<ClassLoader> class_loader(NULL); |
| 183 | if (!boot_image_option.empty()) { |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 184 | std::vector<const DexFile*> dex_files; |
| 185 | DexFile::OpenDexFiles(dex_filenames, dex_files, host_prefix); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 186 | for (size_t i = 0; i < dex_files.size(); i++) { |
| 187 | class_linker->RegisterDexFile(*dex_files[i]); |
| 188 | } |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 189 | class_loader.reset(PathClassLoader::AllocCompileTime(dex_files)); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 190 | } |
| 191 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 192 | // 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] | 193 | if (!runtime->HasJniStubArray()) { |
| 194 | runtime->SetJniStubArray(JniCompiler::CreateJniStub(kThumb2)); |
| 195 | } |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 196 | if (!runtime->HasAbstractMethodErrorStubArray()) { |
| 197 | runtime->SetAbstractMethodErrorStubArray(Compiler::CreateAbstractMethodErrorStub(kThumb2)); |
| 198 | } |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 199 | for (int i = 0; i < Runtime::kLastTrampolineMethodType; i++) { |
Ian Rogers | 1cb0a1d | 2011-10-06 15:24:35 -0700 | [diff] [blame] | 200 | Runtime::TrampolineType type = Runtime::TrampolineType(i); |
| 201 | if (!runtime->HasResolutionStubArray(type)) { |
| 202 | runtime->SetResolutionStubArray(Compiler::CreateResolutionStub(kThumb2, type), type); |
| 203 | } |
Ian Rogers | ad25ac5 | 2011-10-04 19:13:33 -0700 | [diff] [blame] | 204 | } |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 205 | for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) { |
| 206 | Runtime::CalleeSaveType type = Runtime::CalleeSaveType(i); |
| 207 | if (!runtime->HasCalleeSaveMethod(type)) { |
| 208 | runtime->SetCalleeSaveMethod(runtime->CreateCalleeSaveMethod(kThumb2, type), type); |
| 209 | } |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 210 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 211 | Compiler compiler(kThumb2, image_filename != NULL); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 212 | if (method_names.empty()) { |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 213 | compiler.CompileAll(class_loader.get()); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 214 | } else { |
| 215 | for (size_t i = 0; i < method_names.size(); i++) { |
| 216 | // names are actually class_descriptor + name + signature. |
| 217 | // example: Ljava/lang/Object;<init>()V |
| 218 | StringPiece method_name = method_names[i]; |
| 219 | size_t end_of_class_descriptor = method_name.find(';'); |
| 220 | if (end_of_class_descriptor == method_name.npos) { |
| 221 | fprintf(stderr, "could not find class descriptor in method %s\n", method_name.data()); |
| 222 | return EXIT_FAILURE; |
| 223 | } |
| 224 | end_of_class_descriptor++; // want to include ; |
| 225 | std::string class_descriptor = method_name.substr(0, end_of_class_descriptor).ToString(); |
| 226 | size_t end_of_name = method_name.find('(', end_of_class_descriptor); |
| 227 | if (end_of_name == method_name.npos) { |
| 228 | fprintf(stderr, "could not find start of method signature in method %s\n", method_name.data()); |
| 229 | return EXIT_FAILURE; |
| 230 | } |
| 231 | std::string name = method_name.substr(end_of_class_descriptor, |
| 232 | end_of_name - end_of_class_descriptor).ToString(); |
| 233 | std::string signature = method_name.substr(end_of_name).ToString(); |
| 234 | |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 235 | Class* klass = class_linker->FindClass(class_descriptor, class_loader.get()); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 236 | if (klass == NULL) { |
| 237 | fprintf(stderr, "could not find class for descriptor %s in method %s\n", |
| 238 | class_descriptor.c_str(), method_name.data()); |
| 239 | return EXIT_FAILURE; |
| 240 | } |
| 241 | Method* method = klass->FindDirectMethod(name, signature); |
| 242 | if (method == NULL) { |
| 243 | method = klass->FindVirtualMethod(name, signature); |
| 244 | } |
| 245 | if (method == NULL) { |
| 246 | fprintf(stderr, "could not find method %s with signature %s in class %s for method argument %s\n", |
| 247 | name.c_str(), |
| 248 | signature.c_str(), |
| 249 | class_descriptor.c_str(), |
| 250 | method_name.data()); |
| 251 | return EXIT_FAILURE; |
| 252 | } |
| 253 | compiler.CompileOne(method); |
| 254 | } |
| 255 | } |
| 256 | |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 257 | if (!OatWriter::Create(oat_filename, class_loader.get(), compiler)) { |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 258 | fprintf(stderr, "Failed to create oat file %s\n", oat_filename.c_str()); |
| 259 | return EXIT_FAILURE; |
| 260 | } |
| 261 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 262 | if (image_filename == NULL) { |
| 263 | return EXIT_SUCCESS; |
| 264 | } |
| 265 | CHECK(compiler.IsImage()); |
| 266 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 267 | ImageWriter image_writer; |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 268 | if (!image_writer.Write(image_filename, image_base, oat_filename, host_prefix)) { |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 269 | fprintf(stderr, "Failed to create image file %s\n", image_filename); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 270 | return EXIT_FAILURE; |
| 271 | } |
| 272 | |
| 273 | return EXIT_SUCCESS; |
| 274 | } |
| 275 | |
| 276 | } // namespace art |
| 277 | |
| 278 | int main(int argc, char** argv) { |
| 279 | return art::dex2oat(argc, argv); |
| 280 | } |