1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef ART_COMPILER_DRIVER_COMPILER_OPTIONS_MAP_INL_H_
18 #define ART_COMPILER_DRIVER_COMPILER_OPTIONS_MAP_INL_H_
19 
20 #include "compiler_options_map.h"
21 
22 #include <memory>
23 
24 #include "android-base/logging.h"
25 #include "android-base/macros.h"
26 #include "android-base/stringprintf.h"
27 
28 #include "base/macros.h"
29 #include "cmdline_parser.h"
30 #include "compiler_options.h"
31 
32 namespace art {
33 
34 template <class Base>
ReadCompilerOptions(Base & map,CompilerOptions * options,std::string * error_msg)35 inline bool ReadCompilerOptions(Base& map, CompilerOptions* options, std::string* error_msg) {
36   if (map.Exists(Base::CompilerFilter)) {
37     CompilerFilter::Filter compiler_filter;
38     if (!CompilerFilter::ParseCompilerFilter(map.Get(Base::CompilerFilter)->c_str(),
39                                              &compiler_filter)) {
40       *error_msg = android::base::StringPrintf("Unknown --compiler-filter value %s",
41                                                map.Get(Base::CompilerFilter)->c_str());
42       return false;
43     }
44     options->SetCompilerFilter(compiler_filter);
45   }
46   map.AssignIfExists(Base::HugeMethodMaxThreshold, &options->huge_method_threshold_);
47   map.AssignIfExists(Base::LargeMethodMaxThreshold, &options->large_method_threshold_);
48   map.AssignIfExists(Base::NumDexMethodsThreshold, &options->num_dex_methods_threshold_);
49   map.AssignIfExists(Base::InlineMaxCodeUnitsThreshold, &options->inline_max_code_units_);
50   map.AssignIfExists(Base::GenerateDebugInfo, &options->generate_debug_info_);
51   map.AssignIfExists(Base::GenerateMiniDebugInfo, &options->generate_mini_debug_info_);
52   map.AssignIfExists(Base::GenerateBuildID, &options->generate_build_id_);
53   if (map.Exists(Base::Debuggable)) {
54     options->debuggable_ = true;
55   }
56   if (map.Exists(Base::Baseline)) {
57     options->baseline_ = true;
58   }
59   map.AssignIfExists(Base::TopKProfileThreshold, &options->top_k_profile_threshold_);
60   map.AssignIfExists(Base::AbortOnHardVerifierFailure, &options->abort_on_hard_verifier_failure_);
61   map.AssignIfExists(Base::AbortOnSoftVerifierFailure, &options->abort_on_soft_verifier_failure_);
62   if (map.Exists(Base::DumpInitFailures)) {
63     if (!options->ParseDumpInitFailures(*map.Get(Base::DumpInitFailures), error_msg)) {
64       return false;
65     }
66   }
67   map.AssignIfExists(Base::DumpCFG, &options->dump_cfg_file_name_);
68   if (map.Exists(Base::DumpCFGAppend)) {
69     options->dump_cfg_append_ = true;
70   }
71   if (map.Exists(Base::RegisterAllocationStrategy)) {
72     if (!options->ParseRegisterAllocationStrategy(*map.Get(Base::DumpInitFailures), error_msg)) {
73       return false;
74     }
75   }
76   map.AssignIfExists(Base::VerboseMethods, &options->verbose_methods_);
77   options->deduplicate_code_ = map.GetOrDefault(Base::DeduplicateCode);
78   if (map.Exists(Base::CountHotnessInCompiledCode)) {
79     options->count_hotness_in_compiled_code_ = true;
80   }
81   map.AssignIfExists(Base::ResolveStartupConstStrings, &options->resolve_startup_const_strings_);
82   map.AssignIfExists(Base::InitializeAppImageClasses, &options->initialize_app_image_classes_);
83   if (map.Exists(Base::CheckProfiledMethods)) {
84     options->check_profiled_methods_ = *map.Get(Base::CheckProfiledMethods);
85   }
86   map.AssignIfExists(Base::MaxImageBlockSize, &options->max_image_block_size_);
87 
88   if (map.Exists(Base::DumpTimings)) {
89     options->dump_timings_ = true;
90   }
91 
92   if (map.Exists(Base::DumpPassTimings)) {
93     options->dump_pass_timings_ = true;
94   }
95 
96   if (map.Exists(Base::DumpStats)) {
97     options->dump_stats_ = true;
98   }
99 
100   return true;
101 }
102 
103 #pragma GCC diagnostic push
104 #pragma GCC diagnostic ignored "-Wframe-larger-than="
105 
106 template <typename Map, typename Builder>
AddCompilerOptionsArgumentParserOptions(Builder & b)107 inline void AddCompilerOptionsArgumentParserOptions(Builder& b) {
108   b.
109       Define("--compiler-filter=_")
110           .template WithType<std::string>()
111           .IntoKey(Map::CompilerFilter)
112 
113       .Define("--huge-method-max=_")
114           .template WithType<unsigned int>()
115           .IntoKey(Map::HugeMethodMaxThreshold)
116       .Define("--large-method-max=_")
117           .template WithType<unsigned int>()
118           .IntoKey(Map::LargeMethodMaxThreshold)
119       .Define("--num-dex-methods=_")
120           .template WithType<unsigned int>()
121           .IntoKey(Map::NumDexMethodsThreshold)
122       .Define("--inline-max-code-units=_")
123           .template WithType<unsigned int>()
124           .IntoKey(Map::InlineMaxCodeUnitsThreshold)
125 
126       .Define({"--generate-debug-info", "-g", "--no-generate-debug-info"})
127           .WithValues({true, true, false})
128           .IntoKey(Map::GenerateDebugInfo)
129       .Define({"--generate-mini-debug-info", "--no-generate-mini-debug-info"})
130           .WithValues({true, false})
131           .IntoKey(Map::GenerateMiniDebugInfo)
132 
133       .Define({"--generate-build-id", "--no-generate-build-id"})
134           .WithValues({true, false})
135           .IntoKey(Map::GenerateBuildID)
136 
137       .Define({"--deduplicate-code=_"})
138           .template WithType<bool>()
139           .WithValueMap({{"false", false}, {"true", true}})
140           .IntoKey(Map::DeduplicateCode)
141 
142       .Define({"--count-hotness-in-compiled-code"})
143           .IntoKey(Map::CountHotnessInCompiledCode)
144 
145       .Define({"--check-profiled-methods=_"})
146           .template WithType<ProfileMethodsCheck>()
147           .WithValueMap({{"log", ProfileMethodsCheck::kLog},
148                          {"abort", ProfileMethodsCheck::kAbort}})
149           .IntoKey(Map::CheckProfiledMethods)
150 
151       .Define({"--dump-timings"})
152           .IntoKey(Map::DumpTimings)
153 
154       .Define({"--dump-pass-timings"})
155           .IntoKey(Map::DumpPassTimings)
156 
157       .Define({"--dump-stats"})
158           .IntoKey(Map::DumpStats)
159 
160       .Define("--debuggable")
161           .IntoKey(Map::Debuggable)
162 
163       .Define("--baseline")
164           .IntoKey(Map::Baseline)
165 
166       .Define("--top-k-profile-threshold=_")
167           .template WithType<double>().WithRange(0.0, 100.0)
168           .IntoKey(Map::TopKProfileThreshold)
169 
170       .Define({"--abort-on-hard-verifier-error", "--no-abort-on-hard-verifier-error"})
171           .WithValues({true, false})
172           .IntoKey(Map::AbortOnHardVerifierFailure)
173       .Define({"--abort-on-soft-verifier-error", "--no-abort-on-soft-verifier-error"})
174           .WithValues({true, false})
175           .IntoKey(Map::AbortOnSoftVerifierFailure)
176 
177       .Define("--dump-init-failures=_")
178           .template WithType<std::string>()
179           .IntoKey(Map::DumpInitFailures)
180 
181       .Define("--dump-cfg=_")
182           .template WithType<std::string>()
183           .IntoKey(Map::DumpCFG)
184       .Define("--dump-cfg-append")
185           .IntoKey(Map::DumpCFGAppend)
186 
187       .Define("--register-allocation-strategy=_")
188           .template WithType<std::string>()
189           .IntoKey(Map::RegisterAllocationStrategy)
190 
191       .Define("--resolve-startup-const-strings=_")
192           .template WithType<bool>()
193           .WithValueMap({{"false", false}, {"true", true}})
194           .IntoKey(Map::ResolveStartupConstStrings)
195 
196       .Define("--initialize-app-image-classes=_")
197           .template WithType<bool>()
198           .WithValueMap({{"false", false}, {"true", true}})
199           .IntoKey(Map::InitializeAppImageClasses)
200 
201       .Define("--verbose-methods=_")
202           .template WithType<ParseStringList<','>>()
203           .IntoKey(Map::VerboseMethods)
204 
205       .Define("--max-image-block-size=_")
206           .template WithType<unsigned int>()
207           .IntoKey(Map::MaxImageBlockSize);
208 }
209 
210 #pragma GCC diagnostic pop
211 
212 }  // namespace art
213 
214 #endif  // ART_COMPILER_DRIVER_COMPILER_OPTIONS_MAP_INL_H_
215