blob: 455b8b900d2bad9be3aa82c7235393fb64db2db6 [file] [log] [blame]
Brian Carlstrom69b15fb2011-09-03 12:25:21 -07001// 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 Carlstrome24fa612011-09-29 00:53:55 -070013#include "oat_writer.h"
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070014#include "runtime.h"
15#include "stringpiece.h"
16
17namespace art {
18
19static void usage() {
20 fprintf(stderr,
21 "Usage: dex2oat [options]...\n"
22 "\n");
23 fprintf(stderr,
Brian Carlstrom78128a62011-09-15 17:21:19 -070024 " --dex-file=<dex-file>: specifies a .dex file to compile. At least one .dex\n"
25 " file must be specified. \n"
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070026 " Example: --dex-file=/system/framework/core.jar\n"
27 "\n");
28 fprintf(stderr,
Brian Carlstrome24fa612011-09-29 00:53:55 -070029 " --image=<file.art>: specifies the required output image filename.\n"
Brian Carlstrom47a0d5a2011-10-12 21:20:05 -070030 " Example: --image=/data/art-cache/boot.art\n"
Brian Carlstrome24fa612011-09-29 00:53:55 -070031 "\n");
32 // TODO: remove this by inferring from --image
33 fprintf(stderr,
34 " --oat=<file.oat>: specifies the required oat filename.\n"
Brian Carlstrom47a0d5a2011-10-12 21:20:05 -070035 " Example: --image=/data/art-cache/boot.oat\n"
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070036 "\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 Carlstrome24fa612011-09-29 00:53:55 -070042 " --boot-image=<file.art>: provide the image file for the boot class path.\n"
Brian Carlstrom47a0d5a2011-10-12 21:20:05 -070043 " Example: --boot-image=/data/art-cache/boot.art\n"
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070044 "\n");
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070045 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 Carlstrom16192862011-09-12 17:50:06 -070049 fprintf(stderr,
Brian Carlstrom58ae9412011-10-04 00:56:06 -070050 " --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 Carlstrom16192862011-09-12 17:50:06 -070053 "\n");
Brian Carlstrom27ec9612011-09-19 20:20:38 -070054 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 Carlstrom69b15fb2011-09-03 12:25:21 -070064 exit(EXIT_FAILURE);
65}
66
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070067int 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;
Brian Carlstrome24fa612011-09-29 00:53:55 -070079 std::string oat_filename;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070080 const char* image_filename = NULL;
81 std::string boot_image_option;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070082 uintptr_t image_base = 0;
Brian Carlstrom58ae9412011-10-04 00:56:06 -070083 std::string host_prefix;
Brian Carlstrom27ec9612011-09-19 20:20:38 -070084 const char* Xms = NULL;
85 const char* Xmx = NULL;
Elliott Hughesf8e01272011-10-17 11:29:05 -070086 const char* verbose = NULL;
Brian Carlstrom16192862011-09-12 17:50:06 -070087
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070088 for (int i = 0; i < argc; i++) {
89 const StringPiece option(argv[i]);
Brian Carlstromb7bbba42011-10-13 14:58:47 -070090 if (false) {
91 LOG(INFO) << "dex2oat: option[" << i << "]=" << argv[i];
92 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070093 if (option.starts_with("--dex-file=")) {
94 dex_filenames.push_back(option.substr(strlen("--dex-file=")).data());
95 } else if (option.starts_with("--method=")) {
96 method_names.push_back(option.substr(strlen("--method=")).data());
Brian Carlstrome24fa612011-09-29 00:53:55 -070097 } else if (option.starts_with("--oat=")) {
98 oat_filename = option.substr(strlen("--oat=")).data();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070099 } else if (option.starts_with("--image=")) {
100 image_filename = option.substr(strlen("--image=")).data();
101 } else if (option.starts_with("--base=")) {
102 const char* image_base_str = option.substr(strlen("--base=")).data();
103 char* end;
104 image_base = strtoul(image_base_str, &end, 16);
105 if (end == image_base_str || *end != '\0') {
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700106 fprintf(stderr, "Failed to parse hexadecimal value for option %s\n", option.data());
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700107 usage();
108 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700109 } else if (option.starts_with("--boot-image=")) {
110 const char* boot_image_filename = option.substr(strlen("--boot-image=")).data();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700111 boot_image_option.clear();
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700112 boot_image_option += "-Ximage:";
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700113 boot_image_option += boot_image_filename;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700114 } else if (option.starts_with("--host-prefix=")) {
115 host_prefix = option.substr(strlen("--host-prefix=")).data();
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700116 } else if (option.starts_with("-Xms")) {
117 Xms = option.data();
118 } else if (option.starts_with("-Xmx")) {
119 Xmx = option.data();
Elliott Hughesf8e01272011-10-17 11:29:05 -0700120 } else if (option.starts_with("-verbose:")) {
121 verbose = option.data();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700122 } else {
123 fprintf(stderr, "unknown argument %s\n", option.data());
124 usage();
125 }
126 }
127
Brian Carlstrome24fa612011-09-29 00:53:55 -0700128 if (oat_filename == NULL) {
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700129 fprintf(stderr, "--oat file name not specified\n");
130 return EXIT_FAILURE;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700131 }
132
Brian Carlstromaded5f72011-10-07 17:15:04 -0700133 if (image_filename == NULL && boot_image_option.empty()) {
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700134 fprintf(stderr, "Either --image or --boot-image must be specified\n");
135 return EXIT_FAILURE;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700136 }
137
Brian Carlstrom78128a62011-09-15 17:21:19 -0700138 if (dex_filenames.empty()) {
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700139 fprintf(stderr, "no --dex-file values specified\n");
140 return EXIT_FAILURE;
Brian Carlstrom78128a62011-09-15 17:21:19 -0700141 }
142
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700143 if (boot_image_option.empty()) {
144 if (image_base == 0) {
145 fprintf(stderr, "non-zero --base not specified\n");
146 return EXIT_FAILURE;
147 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700148 }
149
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700150 Runtime::Options options;
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700151 options.push_back(std::make_pair("compiler", reinterpret_cast<void*>(NULL)));
Brian Carlstromb7bbba42011-10-13 14:58:47 -0700152 std::string boot_class_path_string;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700153 if (boot_image_option.empty()) {
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700154 boot_class_path_string += "-Xbootclasspath:";
155 for (size_t i = 0; i < dex_filenames.size()-1; i++) {
156 boot_class_path_string += dex_filenames[i];
157 boot_class_path_string += ":";
158 }
159 boot_class_path_string += dex_filenames[dex_filenames.size()-1];
160 options.push_back(std::make_pair(boot_class_path_string.c_str(), reinterpret_cast<void*>(NULL)));
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700161 } else {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700162 options.push_back(std::make_pair(boot_image_option.c_str(), reinterpret_cast<void*>(NULL)));
163 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700164 if (Xms != NULL) {
165 options.push_back(std::make_pair(Xms, reinterpret_cast<void*>(NULL)));
166 }
167 if (Xmx != NULL) {
168 options.push_back(std::make_pair(Xmx, reinterpret_cast<void*>(NULL)));
169 }
Elliott Hughesf8e01272011-10-17 11:29:05 -0700170 if (verbose != NULL) {
171 options.push_back(std::make_pair(verbose, reinterpret_cast<void*>(NULL)));
172 }
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700173 if (!host_prefix.empty()) {
174 options.push_back(std::make_pair("host-prefix", host_prefix.c_str()));
175 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700176 UniquePtr<Runtime> runtime(Runtime::Create(options, false));
177 if (runtime.get() == NULL) {
178 fprintf(stderr, "could not create runtime\n");
179 return EXIT_FAILURE;
180 }
181 ClassLinker* class_linker = runtime->GetClassLinker();
182
Brian Carlstrome24fa612011-09-29 00:53:55 -0700183 // If we have an existing boot image, position new space after its oat file
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700184 if (Heap::GetSpaces().size() > 1) {
185 Space* last_image_space = Heap::GetSpaces()[Heap::GetSpaces().size()-2];
186 CHECK(last_image_space != NULL);
187 CHECK(last_image_space->IsImageSpace());
188 CHECK(!Heap::GetSpaces()[Heap::GetSpaces().size()-1]->IsImageSpace());
189 byte* oat_limit_addr = last_image_space->GetImageHeader().GetOatLimitAddr();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700190 image_base = RoundUp(reinterpret_cast<uintptr_t>(oat_limit_addr), kPageSize);
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700191 }
192
193 // ClassLoader creation needs to come after Runtime::Create
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700194 SirtRef<ClassLoader> class_loader(NULL);
195 if (!boot_image_option.empty()) {
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700196 std::vector<const DexFile*> dex_files;
197 DexFile::OpenDexFiles(dex_filenames, dex_files, host_prefix);
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700198 for (size_t i = 0; i < dex_files.size(); i++) {
199 class_linker->RegisterDexFile(*dex_files[i]);
200 }
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700201 class_loader.reset(PathClassLoader::AllocCompileTime(dex_files));
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700202 }
203
Brian Carlstrome24fa612011-09-29 00:53:55 -0700204 // if we loaded an existing image, we will reuse values from the image roots.
Brian Carlstrom16192862011-09-12 17:50:06 -0700205 if (!runtime->HasJniStubArray()) {
206 runtime->SetJniStubArray(JniCompiler::CreateJniStub(kThumb2));
207 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700208 if (!runtime->HasAbstractMethodErrorStubArray()) {
209 runtime->SetAbstractMethodErrorStubArray(Compiler::CreateAbstractMethodErrorStub(kThumb2));
210 }
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700211 for (int i = 0; i < Runtime::kLastTrampolineMethodType; i++) {
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700212 Runtime::TrampolineType type = Runtime::TrampolineType(i);
213 if (!runtime->HasResolutionStubArray(type)) {
214 runtime->SetResolutionStubArray(Compiler::CreateResolutionStub(kThumb2, type), type);
215 }
Ian Rogersad25ac52011-10-04 19:13:33 -0700216 }
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700217 for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) {
218 Runtime::CalleeSaveType type = Runtime::CalleeSaveType(i);
219 if (!runtime->HasCalleeSaveMethod(type)) {
220 runtime->SetCalleeSaveMethod(runtime->CreateCalleeSaveMethod(kThumb2, type), type);
221 }
Ian Rogersff1ed472011-09-20 13:46:24 -0700222 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700223 Compiler compiler(kThumb2, image_filename != NULL);
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700224 if (method_names.empty()) {
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700225 compiler.CompileAll(class_loader.get());
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700226 } else {
227 for (size_t i = 0; i < method_names.size(); i++) {
228 // names are actually class_descriptor + name + signature.
229 // example: Ljava/lang/Object;<init>()V
230 StringPiece method_name = method_names[i];
231 size_t end_of_class_descriptor = method_name.find(';');
232 if (end_of_class_descriptor == method_name.npos) {
233 fprintf(stderr, "could not find class descriptor in method %s\n", method_name.data());
234 return EXIT_FAILURE;
235 }
236 end_of_class_descriptor++; // want to include ;
237 std::string class_descriptor = method_name.substr(0, end_of_class_descriptor).ToString();
238 size_t end_of_name = method_name.find('(', end_of_class_descriptor);
239 if (end_of_name == method_name.npos) {
240 fprintf(stderr, "could not find start of method signature in method %s\n", method_name.data());
241 return EXIT_FAILURE;
242 }
243 std::string name = method_name.substr(end_of_class_descriptor,
244 end_of_name - end_of_class_descriptor).ToString();
245 std::string signature = method_name.substr(end_of_name).ToString();
246
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700247 Class* klass = class_linker->FindClass(class_descriptor, class_loader.get());
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700248 if (klass == NULL) {
249 fprintf(stderr, "could not find class for descriptor %s in method %s\n",
250 class_descriptor.c_str(), method_name.data());
251 return EXIT_FAILURE;
252 }
253 Method* method = klass->FindDirectMethod(name, signature);
254 if (method == NULL) {
255 method = klass->FindVirtualMethod(name, signature);
256 }
257 if (method == NULL) {
258 fprintf(stderr, "could not find method %s with signature %s in class %s for method argument %s\n",
259 name.c_str(),
260 signature.c_str(),
261 class_descriptor.c_str(),
262 method_name.data());
263 return EXIT_FAILURE;
264 }
265 compiler.CompileOne(method);
266 }
267 }
268
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700269 if (!OatWriter::Create(oat_filename, class_loader.get(), compiler)) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700270 fprintf(stderr, "Failed to create oat file %s\n", oat_filename.c_str());
271 return EXIT_FAILURE;
272 }
273
Brian Carlstromaded5f72011-10-07 17:15:04 -0700274 if (image_filename == NULL) {
275 return EXIT_SUCCESS;
276 }
277 CHECK(compiler.IsImage());
278
Brian Carlstrome24fa612011-09-29 00:53:55 -0700279 ImageWriter image_writer;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700280 if (!image_writer.Write(image_filename, image_base, oat_filename, host_prefix)) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700281 fprintf(stderr, "Failed to create image file %s\n", image_filename);
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700282 return EXIT_FAILURE;
283 }
284
285 return EXIT_SUCCESS;
286}
287
288} // namespace art
289
290int main(int argc, char** argv) {
291 return art::dex2oat(argc, argv);
292}