blob: 6c967ed6f49e97eee28f13aea6fab03916abe8f9 [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"
13#include "runtime.h"
14#include "stringpiece.h"
15
16namespace art {
17
18static void usage() {
19 fprintf(stderr,
20 "Usage: dex2oat [options]...\n"
21 "\n");
22 fprintf(stderr,
Brian Carlstrom78128a62011-09-15 17:21:19 -070023 " --dex-file=<dex-file>: specifies a .dex file to compile. At least one .dex\n"
24 " file must be specified. \n"
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070025 " 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 Carlstrom27ec9612011-09-19 20:20:38 -070042 " image specified with --boot. \n"
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070043 " 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 Carlstrom16192862011-09-12 17:50:06 -070049 fprintf(stderr,
50 " --strip-prefix may be used to strip a path prefix from dex file names in the\n"
Brian Carlstrom27ec9612011-09-19 20:20:38 -070051 " the generated image to match the target file system layout.\n"
Brian Carlstrom16192862011-09-12 17:50:06 -070052 " Example: --strip-prefix=out/target/product/crespo\n"
53 "\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;
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 Carlstrom16192862011-09-12 17:50:06 -070083 std::string strip_location_prefix;
Brian Carlstrom27ec9612011-09-19 20:20:38 -070084 const char* Xms = NULL;
85 const char* Xmx = NULL;
Brian Carlstrom16192862011-09-12 17:50:06 -070086
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070087 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 Carlstrom27ec9612011-09-19 20:20:38 -0700100 fprintf(stderr, "Failed to parse hexadecimal value for option %s\n", option.data());
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700101 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 Carlstrom16192862011-09-12 17:50:06 -0700110 } else if (option.starts_with("--strip-prefix=")) {
111 strip_location_prefix = option.substr(strlen("--strip-prefix=")).data();
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700112 } else if (option.starts_with("-Xms")) {
113 Xms = option.data();
114 } else if (option.starts_with("-Xmx")) {
115 Xmx = option.data();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700116 } 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 Carlstrom78128a62011-09-15 17:21:19 -0700127 if (dex_filenames.empty()) {
128 fprintf(stderr, "no --dex-file values specified\n");
129 return EXIT_FAILURE;
130 }
131
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700132 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 Carlstrom78128a62011-09-15 17:21:19 -0700139 fprintf(stderr, "no --boot-dex-file values specified with --boot\n");
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700140 return EXIT_FAILURE;
141 }
142 }
143
144 std::vector<const DexFile*> dex_files;
Brian Carlstrom78128a62011-09-15 17:21:19 -0700145 DexFile::OpenDexFiles(dex_filenames, dex_files, strip_location_prefix);
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700146
147 std::vector<const DexFile*> boot_dex_files;
Brian Carlstrom78128a62011-09-15 17:21:19 -0700148 DexFile::OpenDexFiles(boot_dex_filenames, boot_dex_files, strip_location_prefix);
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700149
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 Carlstrom27ec9612011-09-19 20:20:38 -0700157 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 Carlstrom69b15fb2011-09-03 12:25:21 -0700163 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 Carlstrom16192862011-09-12 17:50:06 -0700188 // if we loaded an existing image, we will reuse its stub array.
189 if (!runtime->HasJniStubArray()) {
190 runtime->SetJniStubArray(JniCompiler::CreateJniStub(kThumb2));
191 }
Ian Rogersff1ed472011-09-20 13:46:24 -0700192 // similarly for the callee save method
193 if (!runtime->HasCalleeSaveMethod()) {
194 runtime->SetCalleeSaveMethod(runtime->CreateCalleeSaveMethod(kThumb2));
195 }
Ian Rogers2c8f6532011-09-02 17:16:34 -0700196 Compiler compiler(kThumb2);
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700197 if (method_names.empty()) {
198 compiler.CompileAll(class_loader);
199 } else {
200 for (size_t i = 0; i < method_names.size(); i++) {
201 // names are actually class_descriptor + name + signature.
202 // example: Ljava/lang/Object;<init>()V
203 StringPiece method_name = method_names[i];
204 size_t end_of_class_descriptor = method_name.find(';');
205 if (end_of_class_descriptor == method_name.npos) {
206 fprintf(stderr, "could not find class descriptor in method %s\n", method_name.data());
207 return EXIT_FAILURE;
208 }
209 end_of_class_descriptor++; // want to include ;
210 std::string class_descriptor = method_name.substr(0, end_of_class_descriptor).ToString();
211 size_t end_of_name = method_name.find('(', end_of_class_descriptor);
212 if (end_of_name == method_name.npos) {
213 fprintf(stderr, "could not find start of method signature in method %s\n", method_name.data());
214 return EXIT_FAILURE;
215 }
216 std::string name = method_name.substr(end_of_class_descriptor,
217 end_of_name - end_of_class_descriptor).ToString();
218 std::string signature = method_name.substr(end_of_name).ToString();
219
220 Class* klass = class_linker->FindClass(class_descriptor, class_loader);
221 if (klass == NULL) {
222 fprintf(stderr, "could not find class for descriptor %s in method %s\n",
223 class_descriptor.c_str(), method_name.data());
224 return EXIT_FAILURE;
225 }
226 Method* method = klass->FindDirectMethod(name, signature);
227 if (method == NULL) {
228 method = klass->FindVirtualMethod(name, signature);
229 }
230 if (method == NULL) {
231 fprintf(stderr, "could not find method %s with signature %s in class %s for method argument %s\n",
232 name.c_str(),
233 signature.c_str(),
234 class_descriptor.c_str(),
235 method_name.data());
236 return EXIT_FAILURE;
237 }
238 compiler.CompileOne(method);
239 }
240 }
241
242 ImageWriter writer;
243 if (!writer.Write(image_filename, image_base)) {
244 fprintf(stderr, "could not write image %s\n", image_filename);
245 return EXIT_FAILURE;
246 }
247
248 return EXIT_SUCCESS;
249}
250
251} // namespace art
252
253int main(int argc, char** argv) {
254 return art::dex2oat(argc, argv);
255}