• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "ecmascript/js_runtime_options.h"
17 
18 #include <cerrno>
19 #include <cstdlib>
20 #include <iostream>
21 #include <getopt.h>
22 
23 #include "ecmascript/mem/mem_common.h"
24 
25 namespace panda::ecmascript {
26 const std::string PUBLIC_API COMMON_HELP_HEAD_MSG =
27     "Usage: jsvm  <option>  <filename.abc>\n"
28     "\n"
29     "Options:\n";
30 
31 const std::string PUBLIC_API COMPILER_HELP_HEAD_MSG =
32     "Usage: ark_aot_compiler  <option>  --aot-file=<filename>  <filename.abc>\n"
33     "\n"
34     "Options:\n";
35 
36 const std::string PUBLIC_API STUB_HELP_HEAD_MSG =
37     "Usage: ark_stub_compiler <option>\n"
38     "\n"
39     "Options:\n";
40 
41 const std::string PUBLIC_API HELP_OPTION_MSG =
42     "--aot-file:                           Path (file suffix not needed) to AOT output file. Default: 'aot_file'\n"
43     "--ark-properties:                     Set ark properties\n"
44     "--ark-bundle-name:                    Set ark bundle name\n"
45     "--asm-interpreter:                    Enable asm interpreter. Default: 'true'\n"
46     "--asm-opcode-disable-range:           Opcode range when asm interpreter is enabled.\n"
47     "--compiler-assert-types:              Enable type assertion for type inference tests. Default: 'false'\n"
48     "--builtins-dts:                       Builtins.d.abc file path for AOT.\n"
49     "--builtins-lazy:                     Load some builtins function later.This option is only valid in workervm.\n"
50     "--compiler-log:                       Log Option For aot compiler and stub compiler,\n"
51     "                                      'none': no log,\n"
52     "                                      'allllircirasm' or 'all012': print all log for all methods,\n"
53     "                                      'allcir' or 'all0': print IR info for all methods,\n"
54     "                                      'allllir' or 'all1': print llir info for all methods,\n"
55     "                                      'allasm' or 'all2': print asm log for all methods,\n"
56     "                                      'alltype' or 'all3': print type infer log for all methods,\n"
57     "                                      'cerllircirasm' or 'cer0112': print all log for certain method defined "
58                                            "in 'mlist-for-log',\n"
59     "                                      'cercir' or 'cer0': print IR for methods in 'mlist-for-log',\n"
60     "                                      'cerasm' or 'cer2': print log for methods in 'mlist-for-log',\n"
61     "                                      Default: 'none'\n"
62     "--compiler-log-methods:               Specific method list for compiler log, only used when compiler-log. "
63                                            "Default: 'none'\n"
64     "--compiler-type-threshold:            enable to skip methods whose type is no more than threshold. Default: -1\n"
65     "--compiler-log-snapshot:              Enable to print snapshot information. Default: 'false'\n"
66     "--compiler-opt-global-typeinfer:      Enable global typeinfer for aot compiler: Default: 'false'\n"
67     "--compiler-log-time:                  Enable to print pass compiler time. Default: 'false'\n"
68     "--enable-ark-tools:                   Enable ark tools to debug. Default: 'false'\n"
69     "--compiler-trace-bc:                  Enable tracing bytecode for aot runtime. Default: 'false'\n"
70     "--compiler-trace-deopt:               Enable tracing deopt for aot runtime. Default: 'false'\n"
71     "--compiler-trace-inline:              Enable tracing inline function for aot runtime. Default: 'false'\n"
72     "--compiler-max-inline-bytecodes       Set max bytecodes count which aot function can be inlined. Default: '25'\n"
73     "--compiler-deopt-threshold:           Set max count which aot function can occur deoptimization. Default: '10'\n"
74     "--compiler-stress-deopt:              Enable stress deopt for aot compiler. Default: 'false'\n"
75     "--compiler-opt-code-profiler:         Enable opt code Bytecode Statistics for aot runtime. Default: 'false'\n"
76     "--enable-force-gc:                    Enable force gc when allocating object. Default: 'true'\n"
77     "--enable-ic:                          Switch of inline cache. Default: 'true'\n"
78     "--enable-runtime-stat:                Enable statistics of runtime state. Default: 'false'\n"
79     "--compiler-opt-type-lowering:         Enable all type optimization pass for aot compiler. Default: 'true'\n"
80     "--compiler-opt-early-elimination:     Enable EarlyElimination for aot compiler. Default: 'true'\n"
81     "--compiler-opt-later-elimination:     Enable LaterElimination for aot compiler. Default: 'true'\n"
82     "--compiler-opt-value-numbering:       Enable ValueNumbering for aot compiler. Default: 'true'\n"
83     "--compiler-opt-inlining:              Enable inlining function for aot compiler: Default: 'true'\n"
84     "--compiler-opt-pgotype:               Enable pgo type for aot compiler: Default: 'true'\n"
85     "--compiler-opt-track-field:           Enable track field for aot compiler: Default: 'false'\n"
86     "--entry-point:                        Full name of entrypoint function. Default: '_GLOBAL::func_main_0'\n"
87     "--force-full-gc:                      If true trigger full gc, else trigger semi and old gc. Default: 'true'\n"
88     "--framework-abc-file:                 Snapshot file. Default: 'strip.native.min.abc'\n"
89     "--gc-long-paused-time:                Set gc's longPauseTime in millisecond. Default: '40'\n"
90     "--gc-thread-num:                      Set gc thread number. Default: '7'\n"
91     "--heap-size-limit:                    Max heap size (MB). Default: '512'\n"
92     "--help:                               Print this message and exit\n"
93     "--icu-data-path:                      Path to generated icu data file. Default: 'default'\n"
94     "--enable-worker:                      Whether is worker vm. Default: 'false'\n"
95     "--log-level:                          Log level: ['debug', 'info', 'warning', 'error', 'fatal'].\n"
96     "--log-components:                     Enable logs from specified components: ['all', 'gc', 'ecma',\n"
97     "                                      'interpreter', 'debugger', 'compiler', 'builtins', 'trace', 'all']. \n"
98     "                                      Default: 'all'\n"
99     "--log-debug:                          Enable debug or above logs for components: ['all', 'gc', 'ecma',\n"
100     "                                      'interpreter', 'debugger', 'compiler', 'builtins', 'trace', 'all'].\n"
101     "                                      Default: 'all'\n"
102     "--log-error:                          Enable error log for components: ['all', 'gc', 'ecma',\n"
103     "                                      'interpreter', 'debugger', 'compiler', 'builtins', 'trace', 'all']. \n"
104     "                                      Default: 'all'\n"
105     "--log-fatal:                          Enable fatal log for components: ['all', 'gc', 'ecma',\n"
106     "                                      'interpreter', 'debugger', 'compiler', 'builtins', 'trace', 'all']. \n"
107     "                                      Default: 'all'\n"
108     "--log-info:                           Enable info log for components: ['all', 'gc', 'ecma',\n"
109     "                                      'interpreter', 'debugger', 'compiler', 'builtins', 'trace', 'all']. \n"
110     "                                      Default: 'all'\n"
111     "--log-warning:                        Enable warning log for components: ['all', 'gc', 'ecma',\n"
112     "                                      'interpreter', 'debugger', 'compiler', 'trace', 'builtins', \n"
113     "                                      'all']. Default: 'all'\n"
114     "--compiler-opt-max-method:            Enable aot compiler to skip method larger than limit (KB). Default: '32'\n"
115     "--compiler-module-methods:            The number of max compiled methods in a module. Default: '100'\n"
116     "--max-unmovable-space:                Set max unmovable space capacity\n"
117     "--merge-abc:                          ABC file is merge abc. Default: 'false'\n"
118     "--compiler-opt-level:                 Optimization level configuration of aot compiler. Default: '3'\n"
119     "--options:                            Print compiler and runtime options\n"
120     "--compiler-print-type-info:           Enable print type info. Default: 'false'\n"
121     "--serializer-buffer-size-limit:       Max serializer buffer size used by the VM in Byte. Default size is 2GB\n"
122     "--snapshot-file:                      Snapshot file. Default: '/system/etc/snapshot'\n"
123     "--startup-time:                       Print the start time of command execution. Default: 'false'\n"
124     "--stub-file:                          Path of file includes common stubs module compiled by stub compiler. "
125                                            "Default: 'stub.an'\n"
126     "--enable-pgo-profiler:                Enable pgo profiler to sample jsfunction call and output to file. "
127                                            "Default: 'false'\n"
128     "--compiler-pgo-hotness-threshold:     Set hotness threshold for pgo in aot compiler. Default: '2'\n"
129     "--compiler-pgo-profiler-path:         The pgo file output dir or the pgo file dir of AOT compiler. Default: ''\n"
130     "--compiler-target-triple:             CPU triple for aot compiler or stub compiler. \n"
131     "                                      values: ['x86_64-unknown-linux-gnu', 'arm-unknown-linux-gnu', \n"
132     "                                      'aarch64-unknown-linux-gnu'], Default: 'x86_64-unknown-linux-gnu'\n"
133     "--enable-print-execute-time:          Enable print execute panda file spent time\n"
134     "--compiler-verify-vtable:             Verify vtable result for aot compiler. Default: 'false'\n"
135     "--compiler-select-methods             Compiler selected methods for aot. Only work in full compiling mode\n"
136     "                                      Format:--compile-methods=record1:m1,m2,record2:m3\n"
137     "--compiler-skip-methods               Compiler skpped methods for aot. Only work in full compiling mode\n"
138     "                                      Format:--compile-skip-methods=record1:m1,m2,record2:m3\n"
139     "--target-compiler-mode                The compilation mode at the device side, including partial, full and none."
140     "                                      Default: ''\n"
141     "--hap-path                            The path of the app hap. Default: ''\n"
142     "--hap-abc-offset                      The offset of the abc file in app hap. Default: '0'\n"
143     "--hap-abc-size                        The size of the abc file in app hap. Default: '0'\n"
144     "--compiler-fast-compile               Disable some time-consuming pass. Default: 'true'\n"
145     "--compiler-no-check                   Enable remove checks for aot compiler. Default: 'false'\n"
146     "--compiler-opt-loop-peeling:          Enable loop peeling for aot compiler: Default: 'false'\n\n";
147 
ParseCommand(const int argc,const char ** argv)148 bool JSRuntimeOptions::ParseCommand(const int argc, const char **argv)
149 {
150     const struct option longOptions[] = {
151         {"aot-file", required_argument, nullptr, OPTION_AOT_FILE},
152         {"ark-properties", required_argument, nullptr, OPTION_ARK_PROPERTIES},
153         {"ark-bundleName", required_argument, nullptr, OPTION_ARK_BUNDLENAME},
154         {"asm-interpreter", required_argument, nullptr, OPTION_ENABLE_ASM_INTERPRETER},
155         {"asm-opcode-disable-range", required_argument, nullptr, OPTION_ASM_OPCODE_DISABLE_RANGE},
156         {"compiler-assert-types", required_argument, nullptr, OPTION_COMPILER_ASSERT_TYPES},
157         {"builtins-dts", required_argument, nullptr, OPTION_BUILTINS_DTS},
158         {"builtins-lazy", required_argument, nullptr, OPTION_ENABLE_BUILTINS_LAZY},
159         {"compiler-log", required_argument, nullptr, OPTION_COMPILER_LOG_OPT},
160         {"compiler-log-methods", required_argument, nullptr, OPTION_COMPILER_LOG_METHODS},
161         {"compiler-log-snapshot", required_argument, nullptr, OPTION_COMPILER_LOG_SNAPSHOT},
162         {"compiler-log-time", required_argument, nullptr, OPTION_COMPILER_LOG_TIME},
163         {"compiler-opt-global-typeinfer", required_argument, nullptr, OPTION_COMPILER_OPT_GLOBAL_TYPEINFER},
164         {"compiler-type-threshold", required_argument, nullptr, OPTION_COMPILER_TYPE_THRESHOLD},
165         {"enable-ark-tools", required_argument, nullptr, OPTION_ENABLE_ARK_TOOLS},
166         {"compiler-trace-bc", required_argument, nullptr, OPTION_COMPILER_TRACE_BC},
167         {"compiler-trace-deopt", required_argument, nullptr, OPTION_COMPILER_TRACE_DEOPT},
168         {"compiler-trace-inline", required_argument, nullptr, OPTION_COMPILER_TRACE_INLINE},
169         {"compiler-max-inline-bytecodes", required_argument, nullptr, OPTION_COMPILER_MAX_INLINE_BYTECODES},
170         {"compiler-deopt-threshold", required_argument, nullptr, OPTION_COMPILER_DEOPT_THRESHOLD},
171         {"compiler-stress-deopt", required_argument, nullptr, OPTION_COMPILER_STRESS_DEOPT},
172         {"compiler-opt-code-profiler", required_argument, nullptr, OPTION_COMPILER_OPT_CODE_PROFILER},
173         {"enable-force-gc", required_argument, nullptr, OPTION_ENABLE_FORCE_GC},
174         {"enable-ic", required_argument, nullptr, OPTION_ENABLE_IC},
175         {"enable-runtime-stat", required_argument, nullptr, OPTION_ENABLE_RUNTIME_STAT},
176         {"compiler-opt-type-lowering", required_argument, nullptr, OPTION_COMPILER_OPT_TYPE_LOWERING},
177         {"compiler-opt-early-elimination", required_argument, nullptr, OPTION_COMPILER_OPT_EARLY_ELIMINATION},
178         {"compiler-opt-later-elimination", required_argument, nullptr, OPTION_COMPILER_OPT_LATER_ELIMINATION},
179         {"compiler-opt-value-numbering", required_argument, nullptr, OPTION_COMPILER_OPT_VALUE_NUMBERING},
180         {"compiler-opt-inlining", required_argument, nullptr, OPTION_COMPILER_OPT_INLINING},
181         {"compiler-opt-pgotype", required_argument, nullptr, OPTION_COMPILER_OPT_PGOTYPE},
182         {"compiler-opt-track-field", required_argument, nullptr, OPTION_COMPILER_OPT_TRACK_FIELD},
183         {"entry-point", required_argument, nullptr, OPTION_ENTRY_POINT},
184         {"force-full-gc", required_argument, nullptr, OPTION_FORCE_FULL_GC},
185         {"gc-thread-num", required_argument, nullptr, OPTION_GC_THREADNUM},
186         {"heap-size-limit", required_argument, nullptr, OPTION_HEAP_SIZE_LIMIT},
187         {"help", no_argument, nullptr, OPTION_HELP},
188         {"icu-data-path", required_argument, nullptr, OPTION_ICU_DATA_PATH},
189         {"enable-worker", required_argument, nullptr, OPTION_ENABLE_WORKER},
190         {"log-components", required_argument, nullptr, OPTION_LOG_COMPONENTS},
191         {"log-debug", required_argument, nullptr, OPTION_LOG_DEBUG},
192         {"log-error", required_argument, nullptr, OPTION_LOG_ERROR},
193         {"log-fatal", required_argument, nullptr, OPTION_LOG_FATAL},
194         {"log-info", required_argument, nullptr, OPTION_LOG_INFO},
195         {"log-level", required_argument, nullptr, OPTION_LOG_LEVEL},
196         {"log-warning", required_argument, nullptr, OPTION_LOG_WARNING},
197         {"gc-long-paused-time", required_argument, nullptr, OPTION_GC_LONG_PAUSED_TIME},
198         {"compiler-opt-max-method", required_argument, nullptr, OPTION_COMPILER_OPT_MAX_METHOD},
199         {"compiler-module-methods", required_argument, nullptr, OPTION_COMPILER_MODULE_METHODS},
200         {"max-unmovable-space", required_argument, nullptr, OPTION_MAX_UNMOVABLE_SPACE},
201         {"merge-abc", required_argument, nullptr, OPTION_MERGE_ABC},
202         {"compiler-opt-level", required_argument, nullptr, OPTION_ASM_OPT_LEVEL},
203         {"options", no_argument, nullptr, OPTION_OPTIONS},
204         {"compiler-print-type-info", required_argument, nullptr, OPTION_COMPILER_PRINT_TYPE_INFO},
205         {"reloc-mode", required_argument, nullptr, OPTION_RELOCATION_MODE},
206         {"serializer-buffer-size-limit", required_argument, nullptr, OPTION_SERIALIZER_BUFFER_SIZE_LIMIT},
207         {"startup-time", required_argument, nullptr, OPTION_STARTUP_TIME},
208         {"stub-file", required_argument, nullptr, OPTION_STUB_FILE},
209         {"compiler-target-triple", required_argument, nullptr, OPTION_COMPILER_TARGET_TRIPLE},
210         {"enable-print-execute-time", required_argument, nullptr, OPTION_PRINT_EXECUTE_TIME},
211         {"enable-pgo-profiler", required_argument, nullptr, OPTION_ENABLE_PGO_PROFILER},
212         {"compiler-pgo-profiler-path", required_argument, nullptr, OPTION_COMPILER_PGO_PROFILER_PATH},
213         {"compiler-pgo-hotness-threshold", required_argument, nullptr, OPTION_COMPILER_PGO_HOTNESS_THRESHOLD},
214         {"compiler-verify-vtable", required_argument, nullptr, OPTION_COMPILER_VERIFY_VTABLE},
215         {"compiler-select-methods", required_argument, nullptr, OPTION_COMPILER_SELECT_METHODS},
216         {"compiler-skip-methods", required_argument, nullptr, OPTION_COMPILER_SKIP_METHODS},
217         {"target-compiler-mode", required_argument, nullptr, OPTION_TARGET_COMPILER_MODE},
218         {"hap-path", required_argument, nullptr, OPTION_HAP_PATH},
219         {"hap-abc-offset", required_argument, nullptr, OPTION_HAP_ABC_OFFSET},
220         {"hap-abc-size", required_argument, nullptr, OPTION_HAP_ABC_SIZE},
221         {"compiler-no-check", required_argument, nullptr, OPTION_COMPILER_NOCHECK},
222         {"compiler-fast-compile", required_argument, nullptr, OPTION_FAST_AOT_COMPILE_MODE},
223         {"compiler-opt-loop-peeling", required_argument, nullptr, OPTION_COMPILER_OPT_LOOP_PEELING},
224         {nullptr, 0, nullptr, 0},
225     };
226 
227     int index = 0;
228     opterr = 0;
229     bool ret = false;
230     int option = 0;
231     arg_list_t argListStr;
232     uint32_t argUint32 = 0;
233     uint64_t argUInt64 = 0;
234     int argInt = 0;
235     bool argBool = false;
236     double argDouble = 0.0;
237     static std::string COLON = ":";
238 
239     if (argc <= 1) {
240         return true;
241     }
242 
243     while (true) {
244         option = getopt_long_only(argc, const_cast<char **>(argv), "", longOptions, &index);
245         LOG_ECMA(INFO) << "option: " << option << ", optopt: " << optopt << ", optind: " << optind;
246 
247         if (optind <= 0 || optind > argc) {
248             return false;
249         }
250 
251         if (option == -1) {
252             return true;
253         }
254 
255         // unknown option or required_argument option has no argument
256         if (option == '?') {
257             ret = SetDefaultValue(const_cast<char *>(argv[optind - 1]));
258             if (ret) {
259                 continue;
260             } else {
261                 return ret;
262             }
263         }
264 
265         WasSet(option);
266         switch (option) {
267             case OPTION_AOT_FILE:
268                 SetAOTOutputFile(optarg);
269                 break;
270             case OPTION_ARK_PROPERTIES:
271                 ret = ParseIntParam("ark-properties", &argInt);
272                 if (ret) {
273                     SetArkProperties(argInt);
274                 } else {
275                     return false;
276                 }
277                 break;
278             case OPTION_ARK_BUNDLENAME:
279                 SetArkBundleName(optarg);
280                 break;
281             case OPTION_ENABLE_ASM_INTERPRETER:
282                 ret = ParseBoolParam(&argBool);
283                 if (ret) {
284                     SetEnableAsmInterpreter(argBool);
285                 } else {
286                     return false;
287                 }
288                 break;
289             case OPTION_ASM_OPCODE_DISABLE_RANGE:
290                 SetAsmOpcodeDisableRange(optarg);
291                 break;
292             case OPTION_COMPILER_ASSERT_TYPES:
293                 ret = ParseBoolParam(&argBool);
294                 if (ret) {
295                     SetAssertTypes(argBool);
296                 } else {
297                     return false;
298                 }
299                 break;
300             case OPTION_BUILTINS_DTS:
301                 SetBuiltinsDTS(optarg);
302                 break;
303             case OPTION_ENABLE_BUILTINS_LAZY:
304                 ret = ParseBoolParam(&argBool);
305                 if (ret) {
306                     SetEnableBuiltinsLazy(argBool);
307                 } else {
308                     return false;
309                 }
310                 break;
311             case OPTION_COMPILER_LOG_OPT:
312                 SetCompilerLogOption(optarg);
313                 break;
314             case OPTION_COMPILER_LOG_SNAPSHOT:
315                 ret = ParseBoolParam(&argBool);
316                 if (ret) {
317                     SetCompilerLogSnapshot(argBool);
318                 } else {
319                     return false;
320                 }
321                 break;
322             case OPTION_COMPILER_LOG_TIME:
323                 ret = ParseBoolParam(&argBool);
324                 if (ret) {
325                     SetCompilerLogTime(argBool);
326                 } else {
327                     return false;
328                 }
329                 break;
330             case OPTION_ENABLE_ARK_TOOLS:
331                 ret = ParseBoolParam(&argBool);
332                 if (ret) {
333                     SetEnableArkTools(argBool);
334                 } else {
335                     return false;
336                 }
337                 break;
338             case OPTION_COMPILER_TRACE_BC:
339                 ret = ParseBoolParam(&argBool);
340                 if (ret) {
341                     SetTraceBc(argBool);
342                 } else {
343                     return false;
344                 }
345                 break;
346             case OPTION_COMPILER_TRACE_DEOPT:
347                 ret = ParseBoolParam(&argBool);
348                 if (ret) {
349                     SetTraceDeopt(argBool);
350                 } else {
351                     return false;
352                 }
353                 break;
354             case OPTION_COMPILER_TRACE_INLINE:
355                 ret = ParseBoolParam(&argBool);
356                 if (ret) {
357                     SetTraceInline(argBool);
358                 } else {
359                     return false;
360                 }
361                 break;
362             case OPTION_COMPILER_MAX_INLINE_BYTECODES:
363                 ret = ParseUint32Param("max-inline-bytecodes", &argUint32);
364                 if (ret) {
365                     SetMaxInlineBytecodes(argUint32);
366                 } else {
367                     return false;
368                 }
369                 break;
370             case OPTION_COMPILER_DEOPT_THRESHOLD:
371                 ret = ParseUint32Param("deopt-threshol", &argUint32);
372                 if (ret) {
373                     SetDeoptThreshold(argUint32);
374                 } else {
375                     return false;
376                 }
377                 break;
378             case OPTION_COMPILER_STRESS_DEOPT:
379                 ret = ParseBoolParam(&argBool);
380                 if (ret) {
381                     SetStressDeopt(argBool);
382                 } else {
383                     return false;
384                 }
385                 break;
386             case OPTION_COMPILER_OPT_CODE_PROFILER:
387                 ret = ParseBoolParam(&argBool);
388                 if (ret) {
389                     SetOptCodeProfiler(argBool);
390                 } else {
391                     return false;
392                 }
393                 break;
394             case OPTION_ENABLE_FORCE_GC:
395                 ret = ParseBoolParam(&argBool);
396                 if (ret) {
397                     SetEnableForceGC(argBool);
398                 } else {
399                     return false;
400                 }
401                 break;
402             case OPTION_FORCE_FULL_GC:
403                 ret = ParseBoolParam(&argBool);
404                 if (ret) {
405                     SetForceFullGC(argBool);
406                 } else {
407                     return false;
408                 }
409                 break;
410             case OPTION_ENABLE_IC:
411                 ret = ParseBoolParam(&argBool);
412                 if (ret) {
413                     SetEnableIC(argBool);
414                 } else {
415                     return false;
416                 }
417                 break;
418             case OPTION_ENABLE_RUNTIME_STAT:
419                 ret = ParseBoolParam(&argBool);
420                 if (ret) {
421                     SetEnableRuntimeStat(argBool);
422                 } else {
423                     return false;
424                 }
425                 break;
426             case OPTION_GC_THREADNUM:
427                 ret = ParseUint32Param("gc-thread-num", &argUint32);
428                 if (ret) {
429                     SetGcThreadNum(argUint32);
430                 } else {
431                     return false;
432                 }
433                 break;
434             case OPTION_HEAP_SIZE_LIMIT:
435                 ret = ParseUint32Param("heap-size-limit", &argUint32);
436                 if (ret) {
437                     SetHeapSizeLimit(argUint32);
438                 } else {
439                     return false;
440                 }
441                 break;
442             case OPTION_HELP:
443                 return false;
444             case OPTION_ICU_DATA_PATH:
445                 SetIcuDataPath(optarg);
446                 break;
447             case OPTION_ENABLE_WORKER:
448                 ret = ParseBoolParam(&argBool);
449                 if (ret) {
450                     SetIsWorker(argBool);
451                 } else {
452                     return false;
453                 }
454                 break;
455             case OPTION_LOG_COMPONENTS:
456                 ParseListArgParam(optarg, &argListStr, COLON);
457                 SetLogComponents(argListStr);
458                 break;
459             case OPTION_LOG_DEBUG:
460                 ParseListArgParam(optarg, &argListStr, COLON);
461                 SetLogDebug(argListStr);
462                 break;
463             case OPTION_LOG_ERROR:
464                 ParseListArgParam(optarg, &argListStr, COLON);
465                 SetLogError(argListStr);
466                 break;
467             case OPTION_LOG_FATAL:
468                 ParseListArgParam(optarg, &argListStr, COLON);
469                 SetLogFatal(argListStr);
470                 break;
471             case OPTION_LOG_INFO:
472                 ParseListArgParam(optarg, &argListStr, COLON);
473                 SetLogInfo(argListStr);
474                 break;
475             case OPTION_LOG_LEVEL:
476                 SetLogLevel(optarg);
477                 break;
478             case OPTION_LOG_WARNING:
479                 ParseListArgParam(optarg, &argListStr, COLON);
480                 SetLogWarning(argListStr);
481                 break;
482             case OPTION_GC_LONG_PAUSED_TIME:
483                 ret = ParseUint32Param("gc-long-paused-time", &argUint32);
484                 if (ret) {
485                     SetLongPauseTime(argUint32);
486                 } else {
487                     return false;
488                 }
489                 break;
490             case OPTION_COMPILER_OPT_MAX_METHOD:
491                 ret = ParseUint32Param("compiler-opt-max-method", &argUint32);
492                 if (ret) {
493                     SetMaxAotMethodSize(argUint32);
494                 } else {
495                     return false;
496                 }
497                 break;
498             case OPTION_COMPILER_MODULE_METHODS:
499                 ret = ParseUint32Param("compiler-module-methods", &argUint32);
500                 if (!ret) {
501                     return false;
502                 }
503                 SetCompilerModuleMethods(argUint32);
504                 break;
505             case OPTION_COMPILER_TYPE_THRESHOLD:
506                 ret = ParseDoubleParam("compiler-type-threshold", &argDouble);
507                 if (ret) {
508                     SetTypeThreshold(argDouble);
509                 } else {
510                     return false;
511                 }
512                 break;
513             case OPTION_MAX_UNMOVABLE_SPACE:
514                 ret = ParseUint32Param("max-unmovable-space", &argUint32);
515                 if (ret) {
516                     SetMaxNonmovableSpaceCapacity(argUint32);
517                 } else {
518                     return false;
519                 }
520                 break;
521             case OPTION_COMPILER_LOG_METHODS:
522                 SetMethodsListForLog(optarg);
523                 break;
524             case OPTION_ASM_OPT_LEVEL:
525                 ret = ParseUint32Param("compiler-opt-level", &argUint32);
526                 if (ret) {
527                     SetOptLevel(argUint32);
528                 } else {
529                     return false;
530                 }
531                 break;
532             case OPTION_COMPILER_PRINT_TYPE_INFO:
533                 ret = ParseBoolParam(&argBool);
534                 if (ret) {
535                     SetPrintTypeInfo(argBool);
536                 } else {
537                     return false;
538                 }
539                 break;
540             case OPTION_PRINT_EXECUTE_TIME:
541                 ret = ParseBoolParam(&argBool);
542                 if (ret) {
543                     SetEnablePrintExecuteTime(argBool);
544                 } else {
545                     return false;
546                 }
547                 break;
548             case OPTION_ENABLE_PGO_PROFILER:
549                 ret = ParseBoolParam(&argBool);
550                 if (ret) {
551                     SetEnablePGOProfiler(argBool);
552                 } else {
553                     return false;
554                 }
555                 break;
556             case OPTION_COMPILER_PGO_PROFILER_PATH:
557                 if (*optarg == '\0') {
558                     return false;
559                 }
560                 SetPGOProfilerPath(optarg);
561                 break;
562             case OPTION_COMPILER_PGO_HOTNESS_THRESHOLD:
563                 ret = ParseUint32Param("pgo-hotness-threshold", &argUint32);
564                 if (ret) {
565                     SetPGOHotnessThreshold(argUint32);
566                 } else {
567                     return false;
568                 }
569                 break;
570             case OPTION_RELOCATION_MODE:
571                 ret = ParseUint32Param("reloc-mode", &argUint32);
572                 if (ret) {
573                     SetRelocMode(argUint32);
574                 } else {
575                     return false;
576                 }
577                 break;
578             case OPTION_SERIALIZER_BUFFER_SIZE_LIMIT:
579                 ret = ParseUint64Param("serializer-buffer-size-limit", &argUInt64);
580                 if (ret) {
581                     SetSerializerBufferSizeLimit(argUInt64);
582                 } else {
583                     return false;
584                 }
585                 break;
586             case OPTION_STARTUP_TIME:
587                 ret = ParseBoolParam(&argBool);
588                 if (ret) {
589                     SetStartupTime(argBool);
590                 } else {
591                     return false;
592                 }
593                 break;
594             case OPTION_STUB_FILE:
595                 SetStubFile(optarg);
596                 break;
597             case OPTION_COMPILER_TARGET_TRIPLE:
598                 SetTargetTriple(optarg);
599                 break;
600             case OPTION_ENTRY_POINT:
601                 SetEntryPoint(optarg);
602                 break;
603             case OPTION_MERGE_ABC:
604                 ret = ParseBoolParam(&argBool);
605                 if (ret) {
606                     SetMergeAbc(argBool);
607                 } else {
608                     return false;
609                 }
610                 break;
611             case OPTION_COMPILER_OPT_TYPE_LOWERING:
612                 ret = ParseBoolParam(&argBool);
613                 if (ret) {
614                     SetEnableTypeLowering(argBool);
615                 } else {
616                     return false;
617                 }
618                 break;
619             case OPTION_COMPILER_OPT_EARLY_ELIMINATION:
620                 ret = ParseBoolParam(&argBool);
621                 if (ret) {
622                     SetEnableEarlyElimination(argBool);
623                 } else {
624                     return false;
625                 }
626                 break;
627             case OPTION_COMPILER_OPT_LATER_ELIMINATION:
628                 ret = ParseBoolParam(&argBool);
629                 if (ret) {
630                     SetEnableLaterElimination(argBool);
631                 } else {
632                     return false;
633                 }
634                 break;
635             case OPTION_COMPILER_OPT_VALUE_NUMBERING:
636                 ret = ParseBoolParam(&argBool);
637                 if (ret) {
638                     SetEnableValueNumbering(argBool);
639                 } else {
640                     return false;
641                 }
642                 break;
643             case OPTION_COMPILER_OPT_INLINING:
644                 ret = ParseBoolParam(&argBool);
645                 if (ret) {
646                     SetEnableOptInlining(argBool);
647                 } else {
648                     return false;
649                 }
650                 break;
651             case OPTION_COMPILER_OPT_PGOTYPE:
652                 ret = ParseBoolParam(&argBool);
653                 if (ret) {
654                     SetEnableOptPGOType(argBool);
655                 } else {
656                     return false;
657                 }
658                 break;
659             case OPTION_COMPILER_OPT_TRACK_FIELD:
660                 ret = ParseBoolParam(&argBool);
661                 if (ret) {
662                     SetEnableOptTrackField(argBool);
663                 } else {
664                     return false;
665                 }
666                 break;
667             case OPTION_COMPILER_OPT_GLOBAL_TYPEINFER:
668                 ret = ParseBoolParam(&argBool);
669                 if (ret) {
670                     SetEnableGlobalTypeInfer(argBool);
671                 } else {
672                     return false;
673                 }
674                 break;
675             case OPTION_COMPILER_VERIFY_VTABLE:
676                 ret = ParseBoolParam(&argBool);
677                 if (ret) {
678                     SetVerifyVTable(argBool);
679                 } else {
680                     return false;
681                 }
682                 break;
683             case OPTION_COMPILER_SELECT_METHODS:
684                 SetCompilerSelectMethods(optarg);
685                 break;
686             case OPTION_COMPILER_SKIP_METHODS:
687                 SetCompilerSkipMethods(optarg);
688                 break;
689             case OPTION_TARGET_COMPILER_MODE:
690                 SetTargetCompilerMode(optarg);
691                 break;
692             case OPTION_HAP_PATH:
693                 SetHapPath(optarg);
694                 break;
695             case OPTION_HAP_ABC_OFFSET:
696                 ret = ParseUint32Param("hap-abc-offset", &argUint32);
697                 if (!ret) {
698                     return false;
699                 }
700                 SetHapAbcOffset(argUint32);
701                 break;
702             case OPTION_HAP_ABC_SIZE:
703                 ret = ParseUint32Param("hap-abc-size", &argUint32);
704                 if (!ret) {
705                     return false;
706                 }
707                 SetHapAbcSize(argUint32);
708                 break;
709             case OPTION_COMPILER_NOCHECK:
710                 ret = ParseBoolParam(&argBool);
711                 if (!ret) {
712                     return false;
713                 }
714                 SetCompilerNoCheck(argBool);
715                 break;
716             case OPTION_FAST_AOT_COMPILE_MODE:
717                 ret = ParseBoolParam(&argBool);
718                 if (!ret) {
719                     return false;
720                 }
721                 SetFastAOTCompileMode(argBool);
722                 break;
723             case OPTION_COMPILER_OPT_LOOP_PEELING:
724                 ret = ParseBoolParam(&argBool);
725                 if (ret) {
726                     SetEnableOptLoopPeeling(argBool);
727                 } else {
728                     return false;
729                 }
730                 break;
731             default:
732                 LOG_ECMA(ERROR) << "Invalid option\n";
733                 return false;
734         }
735     }
736 }
737 
SetDefaultValue(char * argv)738 bool JSRuntimeOptions::SetDefaultValue(char* argv)
739 {
740     WasSet(optopt);
741 
742     if (optopt == OPTION_DEFAULT) { // unknown option
743         LOG_ECMA(ERROR) << " Invalid option \"" << argv << "\"";
744         return false;
745     }
746 
747     if (optopt > OPTION_OPTIONS) { // unknown argument
748         LOG_ECMA(ERROR) << "getopt: \"" << argv <<"\" argument has invalid parameter value \n";
749         return false;
750     }
751     return true;
752 }
753 
ParseBoolParam(bool * argBool)754 bool JSRuntimeOptions::ParseBoolParam(bool* argBool)
755 {
756     if ((strcmp(optarg, "false") == 0) || (strcmp(optarg, "0") == 0)) {
757         *argBool = false;
758     } else {
759         *argBool = true;
760     }
761     return true;
762 }
763 
ParseDoubleParam(const std::string & option,double * argDouble)764 bool JSRuntimeOptions::ParseDoubleParam(const std::string &option, double *argDouble)
765 {
766     *argDouble = std::stod(optarg, nullptr);
767     if (errno == ERANGE) {
768         LOG_ECMA(ERROR) << "getopt: \"" << option << "\" argument has invalid parameter value \"" << optarg <<"\"\n";
769         return false;
770     }
771     return true;
772 }
773 
ParseIntParam(const std::string & option,int * argInt)774 bool JSRuntimeOptions::ParseIntParam(const std::string &option, int *argInt)
775 {
776     if (StartsWith(optarg, "0x")) {
777         const int HEX = 16;
778         *argInt = std::stoi(optarg, nullptr, HEX);
779     } else {
780         *argInt = std::stoi(optarg);
781     }
782 
783     if (errno == ERANGE) {
784         LOG_ECMA(ERROR) << "getopt: \"" << option << "\" argument has invalid parameter value \"" << optarg <<"\"\n";
785         return false;
786     }
787     return true;
788 }
789 
ParseUint32Param(const std::string & option,uint32_t * argUInt32)790 bool JSRuntimeOptions::ParseUint32Param(const std::string &option, uint32_t *argUInt32)
791 {
792     if (StartsWith(optarg, "0x")) {
793         const int HEX = 16;
794         *argUInt32 = std::strtoull(optarg, nullptr, HEX);
795     } else {
796         const int DEC = 10;
797         *argUInt32 = std::strtoull(optarg, nullptr, DEC);
798     }
799 
800     if (errno == ERANGE) {
801         LOG_ECMA(ERROR) << "getopt: \"" << option << "\" argument has invalid parameter value \"" << optarg <<"\"\n";
802         return false;
803     }
804     return true;
805 }
806 
ParseUint64Param(const std::string & option,uint64_t * argUInt64)807 bool JSRuntimeOptions::ParseUint64Param(const std::string &option, uint64_t *argUInt64)
808 {
809     if (StartsWith(optarg, "0x")) {
810         const int HEX = 16;
811         *argUInt64 = std::strtoull(optarg, nullptr, HEX);
812     } else {
813         const int DEC = 10;
814         *argUInt64 = std::strtoull(optarg, nullptr, DEC);
815     }
816 
817     if (errno == ERANGE) {
818         LOG_ECMA(ERROR) << "getopt: \"" << option << "\" argument has invalid parameter value \"" << optarg <<"\"\n";
819         return false;
820     }
821     return true;
822 }
823 
ParseListArgParam(const std::string & option,arg_list_t * argListStr,std::string delimiter)824 void JSRuntimeOptions::ParseListArgParam(const std::string &option, arg_list_t *argListStr, std::string delimiter)
825 {
826     argListStr->clear();
827     std::size_t strIndex = 0;
828     std::size_t pos = option.find_first_of(delimiter, strIndex);
829     while ((pos < option.size()) && (pos > strIndex)) {
830         argListStr->push_back(option.substr(strIndex, pos - strIndex));
831         strIndex = pos;
832         strIndex = option.find_first_not_of(delimiter, strIndex);
833         pos = option.find_first_of(delimiter, strIndex);
834     }
835     if (pos > strIndex) {
836         argListStr->push_back(option.substr(strIndex, pos - strIndex));
837     }
838     return;
839 }
840 
SetTargetBuiltinsDtsPath()841 void JSRuntimeOptions::SetTargetBuiltinsDtsPath()
842 {
843     WasSet(CommandValues::OPTION_BUILTINS_DTS);
844     std::string builtinsDtsPath = TARGET_BUILTINS_DTS_PATH;
845     SetBuiltinsDTS(builtinsDtsPath);
846 }
847 
SetOptionsForTargetCompilation()848 void JSRuntimeOptions::SetOptionsForTargetCompilation()
849 {
850     if (IsTargetCompilerMode()) {
851         SetTargetBuiltinsDtsPath();
852         SetTargetTriple("aarch64-unknown-linux-gnu");
853         if (IsPartialCompilerMode()) {
854             SetEnableOptPGOType(true);
855             if (IsPGOProfilerPathEmpty()) {
856                 LOG_ECMA(ERROR) << "no pgo profile file in partial mode!";
857             }
858         } else {
859             SetEnableOptPGOType(false);
860             SetPGOProfilerPath("");
861         }
862     }
863 }
864 }
865