blob: 9b6c74ef92ce2e5cb48fc1034ac33ead09c4cbbf [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"
Ian Rogers5e863dd2011-11-02 20:00:10 -070012#include "file.h"
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070013#include "image_writer.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070014#include "oat_writer.h"
Ian Rogers5e863dd2011-11-02 20:00:10 -070015#include "os.h"
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070016#include "runtime.h"
17#include "stringpiece.h"
18
19namespace art {
20
21static void usage() {
22 fprintf(stderr,
23 "Usage: dex2oat [options]...\n"
24 "\n");
25 fprintf(stderr,
Brian Carlstrom78128a62011-09-15 17:21:19 -070026 " --dex-file=<dex-file>: specifies a .dex file to compile. At least one .dex\n"
27 " file must be specified. \n"
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070028 " Example: --dex-file=/system/framework/core.jar\n"
29 "\n");
30 fprintf(stderr,
Brian Carlstrome24fa612011-09-29 00:53:55 -070031 " --image=<file.art>: specifies the required output image filename.\n"
Brian Carlstrom47a0d5a2011-10-12 21:20:05 -070032 " Example: --image=/data/art-cache/boot.art\n"
Brian Carlstrome24fa612011-09-29 00:53:55 -070033 "\n");
34 // TODO: remove this by inferring from --image
35 fprintf(stderr,
36 " --oat=<file.oat>: specifies the required oat filename.\n"
Brian Carlstrom47a0d5a2011-10-12 21:20:05 -070037 " Example: --image=/data/art-cache/boot.oat\n"
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070038 "\n");
39 fprintf(stderr,
40 " --base=<hex-address>: specifies the base address when creating a boot image.\n"
41 " Example: --base=0x50000000\n"
42 "\n");
43 fprintf(stderr,
Brian Carlstrome24fa612011-09-29 00:53:55 -070044 " --boot-image=<file.art>: provide the image file for the boot class path.\n"
Brian Carlstrom47a0d5a2011-10-12 21:20:05 -070045 " Example: --boot-image=/data/art-cache/boot.art\n"
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070046 "\n");
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070047 fprintf(stderr,
48 " --method may be used to limit compilation to a subset of methods.\n"
49 " Example: --method=Ljava/lang/Object;<init>()V\n"
50 "\n");
Brian Carlstrom16192862011-09-12 17:50:06 -070051 fprintf(stderr,
Brian Carlstrom58ae9412011-10-04 00:56:06 -070052 " --host-prefix may be used to translate host paths to target paths during\n"
53 " cross compilation.\n"
54 " Example: --host-prefix=out/target/product/crespo\n"
Brian Carlstrom16192862011-09-12 17:50:06 -070055 "\n");
Brian Carlstrom27ec9612011-09-19 20:20:38 -070056 fprintf(stderr,
jeffhao5d840402011-10-24 17:09:45 -070057 " --runtime-arg <argument>: used to specify various arguments for the runtime,\n"
58 " such as initial heap size, maximum heap size, and verbose output.\n"
59 " Use a separate --runtime-arg switch for each argument.\n"
60 " Example: --runtime-arg -Xms256m\n"
Brian Carlstrom27ec9612011-09-19 20:20:38 -070061 "\n");
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070062 exit(EXIT_FAILURE);
63}
64
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070065int dex2oat(int argc, char** argv) {
66 // Skip over argv[0].
67 argv++;
68 argc--;
69
70 if (argc == 0) {
71 fprintf(stderr, "no arguments specified\n");
72 usage();
73 }
74
75 std::vector<const char*> dex_filenames;
76 std::vector<const char*> method_names;
Brian Carlstrome24fa612011-09-29 00:53:55 -070077 std::string oat_filename;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070078 const char* image_filename = NULL;
79 std::string boot_image_option;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070080 uintptr_t image_base = 0;
Brian Carlstrom58ae9412011-10-04 00:56:06 -070081 std::string host_prefix;
jeffhao5d840402011-10-24 17:09:45 -070082 std::vector<const char*> runtime_args;
Brian Carlstrom16192862011-09-12 17:50:06 -070083
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070084 for (int i = 0; i < argc; i++) {
85 const StringPiece option(argv[i]);
Brian Carlstromb7bbba42011-10-13 14:58:47 -070086 if (false) {
87 LOG(INFO) << "dex2oat: option[" << i << "]=" << argv[i];
88 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070089 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());
Brian Carlstrome24fa612011-09-29 00:53:55 -070093 } else if (option.starts_with("--oat=")) {
94 oat_filename = option.substr(strlen("--oat=")).data();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070095 } else if (option.starts_with("--image=")) {
96 image_filename = option.substr(strlen("--image=")).data();
97 } else if (option.starts_with("--base=")) {
98 const char* image_base_str = option.substr(strlen("--base=")).data();
99 char* end;
100 image_base = strtoul(image_base_str, &end, 16);
101 if (end == image_base_str || *end != '\0') {
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700102 fprintf(stderr, "Failed to parse hexadecimal value for option %s\n", option.data());
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700103 usage();
104 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700105 } else if (option.starts_with("--boot-image=")) {
106 const char* boot_image_filename = option.substr(strlen("--boot-image=")).data();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700107 boot_image_option.clear();
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700108 boot_image_option += "-Ximage:";
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700109 boot_image_option += boot_image_filename;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700110 } else if (option.starts_with("--host-prefix=")) {
111 host_prefix = option.substr(strlen("--host-prefix=")).data();
jeffhao5d840402011-10-24 17:09:45 -0700112 } else if (option == "--runtime-arg") {
113 if (++i >= argc) {
114 fprintf(stderr, "Missing required argument for --runtime-arg\n");
115 usage();
116 }
117 runtime_args.push_back(argv[i]);
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700118 } else {
119 fprintf(stderr, "unknown argument %s\n", option.data());
120 usage();
121 }
122 }
123
Brian Carlstrome24fa612011-09-29 00:53:55 -0700124 if (oat_filename == NULL) {
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700125 fprintf(stderr, "--oat file name not specified\n");
126 return EXIT_FAILURE;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700127 }
128
Brian Carlstromaded5f72011-10-07 17:15:04 -0700129 if (image_filename == NULL && boot_image_option.empty()) {
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700130 fprintf(stderr, "Either --image or --boot-image must be specified\n");
131 return EXIT_FAILURE;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700132 }
133
Brian Carlstrom78128a62011-09-15 17:21:19 -0700134 if (dex_filenames.empty()) {
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700135 fprintf(stderr, "no --dex-file values specified\n");
136 return EXIT_FAILURE;
Brian Carlstrom78128a62011-09-15 17:21:19 -0700137 }
138
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700139 if (boot_image_option.empty()) {
140 if (image_base == 0) {
141 fprintf(stderr, "non-zero --base not specified\n");
142 return EXIT_FAILURE;
143 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700144 }
145
Ian Rogers5e863dd2011-11-02 20:00:10 -0700146 // Check early that the result of compilation can be written
147 if (OS::FileExists(oat_filename.c_str())) {
148 // File exists, check we can write to it
149 UniquePtr<File> file(OS::OpenFile(oat_filename.c_str(), true));
150 if (file.get() == NULL) {
151 PLOG(ERROR) << "Unable to create oat file " << oat_filename;
152 return EXIT_FAILURE;
153 }
154 }
155 LOG(INFO) << "dex2oat: " << oat_filename;
156
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700157 Runtime::Options options;
Brian Carlstrom5de8fe52011-10-16 14:10:09 -0700158 options.push_back(std::make_pair("compiler", reinterpret_cast<void*>(NULL)));
Brian Carlstromb7bbba42011-10-13 14:58:47 -0700159 std::string boot_class_path_string;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700160 if (boot_image_option.empty()) {
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700161 boot_class_path_string += "-Xbootclasspath:";
162 for (size_t i = 0; i < dex_filenames.size()-1; i++) {
163 boot_class_path_string += dex_filenames[i];
164 boot_class_path_string += ":";
165 }
166 boot_class_path_string += dex_filenames[dex_filenames.size()-1];
167 options.push_back(std::make_pair(boot_class_path_string.c_str(), reinterpret_cast<void*>(NULL)));
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700168 } else {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700169 options.push_back(std::make_pair(boot_image_option.c_str(), reinterpret_cast<void*>(NULL)));
170 }
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700171 if (!host_prefix.empty()) {
172 options.push_back(std::make_pair("host-prefix", host_prefix.c_str()));
173 }
jeffhao5d840402011-10-24 17:09:45 -0700174 for (size_t i = 0; i < runtime_args.size(); i++) {
175 options.push_back(std::make_pair(runtime_args[i], reinterpret_cast<void*>(NULL)));
176 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700177 UniquePtr<Runtime> runtime(Runtime::Create(options, false));
178 if (runtime.get() == NULL) {
Ian Rogers3fe79572011-11-02 18:24:30 -0700179 LOG(ERROR) << "Could not create runtime";
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700180 return EXIT_FAILURE;
181 }
182 ClassLinker* class_linker = runtime->GetClassLinker();
183
Brian Carlstrome24fa612011-09-29 00:53:55 -0700184 // If we have an existing boot image, position new space after its oat file
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700185 if (Heap::GetSpaces().size() > 1) {
186 Space* last_image_space = Heap::GetSpaces()[Heap::GetSpaces().size()-2];
187 CHECK(last_image_space != NULL);
188 CHECK(last_image_space->IsImageSpace());
189 CHECK(!Heap::GetSpaces()[Heap::GetSpaces().size()-1]->IsImageSpace());
190 byte* oat_limit_addr = last_image_space->GetImageHeader().GetOatLimitAddr();
Brian Carlstrome24fa612011-09-29 00:53:55 -0700191 image_base = RoundUp(reinterpret_cast<uintptr_t>(oat_limit_addr), kPageSize);
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700192 }
193
194 // ClassLoader creation needs to come after Runtime::Create
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700195 SirtRef<ClassLoader> class_loader(NULL);
196 if (!boot_image_option.empty()) {
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700197 std::vector<const DexFile*> dex_files;
198 DexFile::OpenDexFiles(dex_filenames, dex_files, host_prefix);
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700199 for (size_t i = 0; i < dex_files.size(); i++) {
200 class_linker->RegisterDexFile(*dex_files[i]);
201 }
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700202 class_loader.reset(PathClassLoader::AllocCompileTime(dex_files));
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700203 }
204
Brian Carlstrome24fa612011-09-29 00:53:55 -0700205 // if we loaded an existing image, we will reuse values from the image roots.
Brian Carlstrom16192862011-09-12 17:50:06 -0700206 if (!runtime->HasJniStubArray()) {
207 runtime->SetJniStubArray(JniCompiler::CreateJniStub(kThumb2));
208 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700209 if (!runtime->HasAbstractMethodErrorStubArray()) {
210 runtime->SetAbstractMethodErrorStubArray(Compiler::CreateAbstractMethodErrorStub(kThumb2));
211 }
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700212 for (int i = 0; i < Runtime::kLastTrampolineMethodType; i++) {
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700213 Runtime::TrampolineType type = Runtime::TrampolineType(i);
214 if (!runtime->HasResolutionStubArray(type)) {
215 runtime->SetResolutionStubArray(Compiler::CreateResolutionStub(kThumb2, type), type);
216 }
Ian Rogersad25ac52011-10-04 19:13:33 -0700217 }
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700218 for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) {
219 Runtime::CalleeSaveType type = Runtime::CalleeSaveType(i);
220 if (!runtime->HasCalleeSaveMethod(type)) {
221 runtime->SetCalleeSaveMethod(runtime->CreateCalleeSaveMethod(kThumb2, type), type);
222 }
Ian Rogersff1ed472011-09-20 13:46:24 -0700223 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700224 Compiler compiler(kThumb2, image_filename != NULL);
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700225 if (method_names.empty()) {
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700226 compiler.CompileAll(class_loader.get());
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700227 } else {
228 for (size_t i = 0; i < method_names.size(); i++) {
229 // names are actually class_descriptor + name + signature.
230 // example: Ljava/lang/Object;<init>()V
231 StringPiece method_name = method_names[i];
232 size_t end_of_class_descriptor = method_name.find(';');
233 if (end_of_class_descriptor == method_name.npos) {
Ian Rogers3fe79572011-11-02 18:24:30 -0700234 LOG(ERROR) << "Could not find class descriptor in method " << method_name << "'";
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700235 return EXIT_FAILURE;
236 }
237 end_of_class_descriptor++; // want to include ;
238 std::string class_descriptor = method_name.substr(0, end_of_class_descriptor).ToString();
239 size_t end_of_name = method_name.find('(', end_of_class_descriptor);
240 if (end_of_name == method_name.npos) {
Ian Rogers3fe79572011-11-02 18:24:30 -0700241 LOG(ERROR) << "Could not find start of method signature in method '" << method_name << "'";
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700242 return EXIT_FAILURE;
243 }
244 std::string name = method_name.substr(end_of_class_descriptor,
245 end_of_name - end_of_class_descriptor).ToString();
246 std::string signature = method_name.substr(end_of_name).ToString();
247
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700248 Class* klass = class_linker->FindClass(class_descriptor, class_loader.get());
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700249 if (klass == NULL) {
Ian Rogers3fe79572011-11-02 18:24:30 -0700250 LOG(ERROR) << "Could not find class for descriptor '" << class_descriptor
251 << "' in method '" << method_name << "'";
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700252 return EXIT_FAILURE;
253 }
254 Method* method = klass->FindDirectMethod(name, signature);
255 if (method == NULL) {
256 method = klass->FindVirtualMethod(name, signature);
257 }
258 if (method == NULL) {
Ian Rogers3fe79572011-11-02 18:24:30 -0700259 LOG(ERROR) << "Could not find method '" << method_name << "' with signature '"
260 << signature << "' in class '" << class_descriptor << "' for method argument '"
261 << method_name << "'";
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700262 return EXIT_FAILURE;
263 }
264 compiler.CompileOne(method);
265 }
266 }
267
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700268 if (!OatWriter::Create(oat_filename, class_loader.get(), compiler)) {
Ian Rogers3fe79572011-11-02 18:24:30 -0700269 LOG(ERROR) << "Failed to create oat file " << oat_filename;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700270 return EXIT_FAILURE;
271 }
272
Brian Carlstromaded5f72011-10-07 17:15:04 -0700273 if (image_filename == NULL) {
274 return EXIT_SUCCESS;
275 }
276 CHECK(compiler.IsImage());
277
Brian Carlstrome24fa612011-09-29 00:53:55 -0700278 ImageWriter image_writer;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700279 if (!image_writer.Write(image_filename, image_base, oat_filename, host_prefix)) {
Ian Rogers3fe79572011-11-02 18:24:30 -0700280 LOG(ERROR) << "Failed to create image file " << image_filename;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700281 return EXIT_FAILURE;
282 }
283
284 return EXIT_SUCCESS;
285}
286
287} // namespace art
288
289int main(int argc, char** argv) {
290 return art::dex2oat(argc, argv);
291}