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" |
| 13 | #include "runtime.h" |
| 14 | #include "stringpiece.h" |
| 15 | |
| 16 | namespace art { |
| 17 | |
| 18 | static void usage() { |
| 19 | fprintf(stderr, |
| 20 | "Usage: dex2oat [options]...\n" |
| 21 | "\n"); |
| 22 | fprintf(stderr, |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 23 | " --dex-file=<dex-file>: specifies a .dex file to compile. At least one .dex\n" |
| 24 | " file must be specified. \n" |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 25 | " Example: --dex-file=/system/framework/core.jar\n" |
| 26 | "\n"); |
| 27 | fprintf(stderr, |
| 28 | " --image=<file>: specifies the required output image filename.\n" |
| 29 | " Example: --image=/system/framework/boot.oat\n" |
| 30 | "\n"); |
| 31 | fprintf(stderr, |
| 32 | " --base=<hex-address>: specifies the base address when creating a boot image.\n" |
| 33 | " Example: --base=0x50000000\n" |
| 34 | "\n"); |
| 35 | fprintf(stderr, |
| 36 | " --boot=<oat-file>: provide the oat file for the boot class path.\n" |
| 37 | " Example: --boot=/system/framework/boot.oat\n" |
| 38 | "\n"); |
| 39 | // TODO: remove this by making boot image contain boot DexFile information? |
| 40 | fprintf(stderr, |
| 41 | " --boot-dex-file=<dex-file>: specifies a .dex file that is part of the boot\n" |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame^] | 42 | " image specified with --boot. \n" |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 43 | " Example: --boot-dex-file=/system/framework/core.jar\n" |
| 44 | "\n"); |
| 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, |
| 50 | " --strip-prefix may be used to strip a path prefix from dex file names in the\n" |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame^] | 51 | " the generated image to match the target file system layout.\n" |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 52 | " Example: --strip-prefix=out/target/product/crespo\n" |
| 53 | "\n"); |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame^] | 54 | fprintf(stderr, |
| 55 | " -Xms<n> may be used to specify an initial heap size for the runtime used to\n" |
| 56 | " run dex2oat\n" |
| 57 | " Example: -Xms256m\n" |
| 58 | "\n"); |
| 59 | fprintf(stderr, |
| 60 | " -Xmx<n> may be used to specify a maximum heap size for the runtime used to\n" |
| 61 | " run dex2oat\n" |
| 62 | " Example: -Xmx256m\n" |
| 63 | "\n"); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 64 | exit(EXIT_FAILURE); |
| 65 | } |
| 66 | |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 67 | int dex2oat(int argc, char** argv) { |
| 68 | // Skip over argv[0]. |
| 69 | argv++; |
| 70 | argc--; |
| 71 | |
| 72 | if (argc == 0) { |
| 73 | fprintf(stderr, "no arguments specified\n"); |
| 74 | usage(); |
| 75 | } |
| 76 | |
| 77 | std::vector<const char*> dex_filenames; |
| 78 | std::vector<const char*> method_names; |
| 79 | const char* image_filename = NULL; |
| 80 | std::string boot_image_option; |
| 81 | std::vector<const char*> boot_dex_filenames; |
| 82 | uintptr_t image_base = 0; |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 83 | std::string strip_location_prefix; |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame^] | 84 | const char* Xms = NULL; |
| 85 | const char* Xmx = NULL; |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 86 | |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 87 | for (int i = 0; i < argc; i++) { |
| 88 | const StringPiece option(argv[i]); |
| 89 | if (option.starts_with("--dex-file=")) { |
| 90 | dex_filenames.push_back(option.substr(strlen("--dex-file=")).data()); |
| 91 | } else if (option.starts_with("--method=")) { |
| 92 | method_names.push_back(option.substr(strlen("--method=")).data()); |
| 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 | } |
| 103 | } else if (option.starts_with("--boot=")) { |
| 104 | const char* boot_image_filename = option.substr(strlen("--boot=")).data(); |
| 105 | boot_image_option.clear(); |
| 106 | boot_image_option += "-Xbootimage:"; |
| 107 | boot_image_option += boot_image_filename; |
| 108 | } else if (option.starts_with("--boot-dex-file=")) { |
| 109 | boot_dex_filenames.push_back(option.substr(strlen("--boot-dex-file=")).data()); |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 110 | } else if (option.starts_with("--strip-prefix=")) { |
| 111 | strip_location_prefix = option.substr(strlen("--strip-prefix=")).data(); |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame^] | 112 | } else if (option.starts_with("-Xms")) { |
| 113 | Xms = option.data(); |
| 114 | } else if (option.starts_with("-Xmx")) { |
| 115 | Xmx = option.data(); |
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 | |
| 122 | if (image_filename == NULL) { |
| 123 | fprintf(stderr, "--image file name not specified\n"); |
| 124 | return EXIT_FAILURE; |
| 125 | } |
| 126 | |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 127 | if (dex_filenames.empty()) { |
| 128 | fprintf(stderr, "no --dex-file values specified\n"); |
| 129 | return EXIT_FAILURE; |
| 130 | } |
| 131 | |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 132 | if (boot_image_option.empty()) { |
| 133 | if (image_base == 0) { |
| 134 | fprintf(stderr, "non-zero --base not specified\n"); |
| 135 | return EXIT_FAILURE; |
| 136 | } |
| 137 | } else { |
| 138 | if (boot_dex_filenames.empty()) { |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 139 | fprintf(stderr, "no --boot-dex-file values specified with --boot\n"); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 140 | return EXIT_FAILURE; |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | std::vector<const DexFile*> dex_files; |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 145 | DexFile::OpenDexFiles(dex_filenames, dex_files, strip_location_prefix); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 146 | |
| 147 | std::vector<const DexFile*> boot_dex_files; |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 148 | DexFile::OpenDexFiles(boot_dex_filenames, boot_dex_files, strip_location_prefix); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 149 | |
| 150 | Runtime::Options options; |
| 151 | if (boot_image_option.empty()) { |
| 152 | options.push_back(std::make_pair("bootclasspath", &dex_files)); |
| 153 | } else { |
| 154 | options.push_back(std::make_pair("bootclasspath", &boot_dex_files)); |
| 155 | options.push_back(std::make_pair(boot_image_option.c_str(), reinterpret_cast<void*>(NULL))); |
| 156 | } |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame^] | 157 | if (Xms != NULL) { |
| 158 | options.push_back(std::make_pair(Xms, reinterpret_cast<void*>(NULL))); |
| 159 | } |
| 160 | if (Xmx != NULL) { |
| 161 | options.push_back(std::make_pair(Xmx, reinterpret_cast<void*>(NULL))); |
| 162 | } |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 163 | UniquePtr<Runtime> runtime(Runtime::Create(options, false)); |
| 164 | if (runtime.get() == NULL) { |
| 165 | fprintf(stderr, "could not create runtime\n"); |
| 166 | return EXIT_FAILURE; |
| 167 | } |
| 168 | ClassLinker* class_linker = runtime->GetClassLinker(); |
| 169 | |
| 170 | // If we have an existing boot image, position new space after it |
| 171 | if (!boot_image_option.empty()) { |
| 172 | Space* boot_space = Heap::GetBootSpace(); |
| 173 | CHECK(boot_space != NULL); |
| 174 | image_base = RoundUp(reinterpret_cast<uintptr_t>(boot_space->GetLimit()), kPageSize); |
| 175 | } |
| 176 | |
| 177 | // ClassLoader creation needs to come after Runtime::Create |
| 178 | const ClassLoader* class_loader; |
| 179 | if (boot_image_option.empty()) { |
| 180 | class_loader = NULL; |
| 181 | } else { |
| 182 | for (size_t i = 0; i < dex_files.size(); i++) { |
| 183 | class_linker->RegisterDexFile(*dex_files[i]); |
| 184 | } |
| 185 | class_loader = PathClassLoader::Alloc(dex_files); |
| 186 | } |
| 187 | |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 188 | // if we loaded an existing image, we will reuse its stub array. |
| 189 | if (!runtime->HasJniStubArray()) { |
| 190 | runtime->SetJniStubArray(JniCompiler::CreateJniStub(kThumb2)); |
| 191 | } |
| 192 | |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 193 | Compiler compiler(kThumb2); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 194 | if (method_names.empty()) { |
| 195 | compiler.CompileAll(class_loader); |
| 196 | } else { |
| 197 | for (size_t i = 0; i < method_names.size(); i++) { |
| 198 | // names are actually class_descriptor + name + signature. |
| 199 | // example: Ljava/lang/Object;<init>()V |
| 200 | StringPiece method_name = method_names[i]; |
| 201 | size_t end_of_class_descriptor = method_name.find(';'); |
| 202 | if (end_of_class_descriptor == method_name.npos) { |
| 203 | fprintf(stderr, "could not find class descriptor in method %s\n", method_name.data()); |
| 204 | return EXIT_FAILURE; |
| 205 | } |
| 206 | end_of_class_descriptor++; // want to include ; |
| 207 | std::string class_descriptor = method_name.substr(0, end_of_class_descriptor).ToString(); |
| 208 | size_t end_of_name = method_name.find('(', end_of_class_descriptor); |
| 209 | if (end_of_name == method_name.npos) { |
| 210 | fprintf(stderr, "could not find start of method signature in method %s\n", method_name.data()); |
| 211 | return EXIT_FAILURE; |
| 212 | } |
| 213 | std::string name = method_name.substr(end_of_class_descriptor, |
| 214 | end_of_name - end_of_class_descriptor).ToString(); |
| 215 | std::string signature = method_name.substr(end_of_name).ToString(); |
| 216 | |
| 217 | Class* klass = class_linker->FindClass(class_descriptor, class_loader); |
| 218 | if (klass == NULL) { |
| 219 | fprintf(stderr, "could not find class for descriptor %s in method %s\n", |
| 220 | class_descriptor.c_str(), method_name.data()); |
| 221 | return EXIT_FAILURE; |
| 222 | } |
| 223 | Method* method = klass->FindDirectMethod(name, signature); |
| 224 | if (method == NULL) { |
| 225 | method = klass->FindVirtualMethod(name, signature); |
| 226 | } |
| 227 | if (method == NULL) { |
| 228 | fprintf(stderr, "could not find method %s with signature %s in class %s for method argument %s\n", |
| 229 | name.c_str(), |
| 230 | signature.c_str(), |
| 231 | class_descriptor.c_str(), |
| 232 | method_name.data()); |
| 233 | return EXIT_FAILURE; |
| 234 | } |
| 235 | compiler.CompileOne(method); |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | ImageWriter writer; |
| 240 | if (!writer.Write(image_filename, image_base)) { |
| 241 | fprintf(stderr, "could not write image %s\n", image_filename); |
| 242 | return EXIT_FAILURE; |
| 243 | } |
| 244 | |
| 245 | return EXIT_SUCCESS; |
| 246 | } |
| 247 | |
| 248 | } // namespace art |
| 249 | |
| 250 | int main(int argc, char** argv) { |
| 251 | return art::dex2oat(argc, argv); |
| 252 | } |