main.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include <stdlib.h>
  6. #include "bh_platform.h"
  7. #include "bh_read_file.h"
  8. #include "wasm_export.h"
  9. #include "aot_export.h"
  10. #include <llvm-c/Support.h>
  11. #if BH_HAS_DLFCN
  12. #include <dlfcn.h>
  13. typedef uint32 (*get_native_lib_func)(char **p_module_name,
  14. NativeSymbol **p_native_symbols);
  15. static uint32
  16. load_and_register_native_libs(const char **native_lib_list,
  17. uint32 native_lib_count,
  18. void **native_handle_list)
  19. {
  20. uint32 i, native_handle_count = 0, n_native_symbols;
  21. NativeSymbol *native_symbols;
  22. char *module_name;
  23. void *handle;
  24. for (i = 0; i < native_lib_count; i++) {
  25. /* open the native library */
  26. if (!(handle = dlopen(native_lib_list[i], RTLD_NOW | RTLD_GLOBAL))
  27. && !(handle = dlopen(native_lib_list[i], RTLD_LAZY))) {
  28. LOG_WARNING("warning: failed to load native library %s",
  29. native_lib_list[i]);
  30. continue;
  31. }
  32. /* lookup get_native_lib func */
  33. get_native_lib_func get_native_lib = dlsym(handle, "get_native_lib");
  34. if (!get_native_lib) {
  35. LOG_WARNING("warning: failed to lookup `get_native_lib` function "
  36. "from native lib %s",
  37. native_lib_list[i]);
  38. dlclose(handle);
  39. continue;
  40. }
  41. n_native_symbols = get_native_lib(&module_name, &native_symbols);
  42. /* register native symbols */
  43. if (!(n_native_symbols > 0 && module_name && native_symbols
  44. && wasm_runtime_register_natives(module_name, native_symbols,
  45. n_native_symbols))) {
  46. LOG_WARNING("warning: failed to register native lib %s",
  47. native_lib_list[i]);
  48. dlclose(handle);
  49. continue;
  50. }
  51. native_handle_list[native_handle_count++] = handle;
  52. }
  53. return native_handle_count;
  54. }
  55. static void
  56. unregister_and_unload_native_libs(uint32 native_lib_count,
  57. void **native_handle_list)
  58. {
  59. uint32 i, n_native_symbols;
  60. NativeSymbol *native_symbols;
  61. char *module_name;
  62. void *handle;
  63. for (i = 0; i < native_lib_count; i++) {
  64. handle = native_handle_list[i];
  65. /* lookup get_native_lib func */
  66. get_native_lib_func get_native_lib = dlsym(handle, "get_native_lib");
  67. if (!get_native_lib) {
  68. LOG_WARNING("warning: failed to lookup `get_native_lib` function "
  69. "from native lib %p",
  70. handle);
  71. continue;
  72. }
  73. n_native_symbols = get_native_lib(&module_name, &native_symbols);
  74. if (n_native_symbols == 0 || module_name == NULL
  75. || native_symbols == NULL) {
  76. LOG_WARNING("warning: get_native_lib returned different values for "
  77. "native lib %p",
  78. handle);
  79. continue;
  80. }
  81. /* unregister native symbols */
  82. if (!wasm_runtime_unregister_natives(module_name, native_symbols)) {
  83. LOG_WARNING("warning: failed to unregister native lib %p", handle);
  84. continue;
  85. }
  86. dlclose(handle);
  87. }
  88. }
  89. #endif
  90. /* clang-format off */
  91. static void
  92. print_help()
  93. {
  94. printf("Usage: wamrc [options] -o output_file wasm_file\n");
  95. printf(" --target=<arch-name> Set the target arch, which has the general format: <arch><sub>\n");
  96. printf(" <arch> = x86_64, i386, aarch64, arm, thumb, xtensa, mips,\n");
  97. printf(" riscv64, riscv32.\n");
  98. printf(" Default is host arch, e.g. x86_64\n");
  99. printf(" <sub> = for ex. on arm or thumb: v5, v6m, v7a, v7m, etc.\n");
  100. printf(" Use --target=help to list supported targets\n");
  101. printf(" --target-abi=<abi> Set the target ABI, e.g. gnu, eabi, gnueabihf, msvc, etc.\n");
  102. printf(" Default is gnu if target isn't riscv64 or riscv32\n");
  103. printf(" For target riscv64 and riscv32, default is lp64d and ilp32d\n");
  104. printf(" Use --target-abi=help to list all the ABI supported\n");
  105. printf(" --cpu=<cpu> Set the target CPU (default: host CPU, e.g. skylake)\n");
  106. printf(" Use --cpu=help to list all the CPU supported\n");
  107. printf(" --cpu-features=<features> Enable or disable the CPU features\n");
  108. printf(" Use +feature to enable a feature, or -feature to disable it\n");
  109. printf(" For example, --cpu-features=+feature1,-feature2\n");
  110. printf(" Use --cpu-features=+help to list all the features supported\n");
  111. printf(" --opt-level=n Set the optimization level (0 to 3, default is 3)\n");
  112. printf(" --size-level=n Set the code size level (0 to 3, default is 3)\n");
  113. printf(" 0 - Large code model\n");
  114. printf(" 1 - Medium code model\n");
  115. printf(" 2 - Kernel code model\n");
  116. printf(" 3 - Small code model\n");
  117. printf(" -sgx Generate code for SGX platform (Intel Software Guard Extensions)\n");
  118. printf(" --bounds-checks=1/0 Enable or disable the bounds checks for memory access:\n");
  119. printf(" by default it is disabled in all 64-bit platforms except SGX and\n");
  120. printf(" in these platforms runtime does bounds checks with hardware trap,\n");
  121. printf(" and by default it is enabled in all 32-bit platforms\n");
  122. printf(" CAVEAT: --bounds-checks=0 enables some optimizations\n");
  123. printf(" which make the compiled AOT module incompatible\n");
  124. printf(" with a runtime without the hardware bounds checks.\n");
  125. printf(" --stack-bounds-checks=1/0 Enable or disable the bounds checks for native stack:\n");
  126. printf(" if the option isn't set, the status is same as `--bounds-check`,\n");
  127. printf(" if the option is set, the status is same as the option value\n");
  128. printf(" --stack-usage=<file> Generate a stack-usage file.\n");
  129. printf(" Similarly to `clang -fstack-usage`.\n");
  130. printf(" --format=<format> Specifies the format of the output file\n");
  131. printf(" The format supported:\n");
  132. printf(" aot (default) AoT file\n");
  133. printf(" object Native object file\n");
  134. printf(" llvmir-unopt Unoptimized LLVM IR\n");
  135. printf(" llvmir-opt Optimized LLVM IR\n");
  136. printf(" --disable-bulk-memory Disable the MVP bulk memory feature\n");
  137. printf(" --enable-multi-thread Enable multi-thread feature, the dependent features bulk-memory and\n");
  138. printf(" thread-mgr will be enabled automatically\n");
  139. printf(" --enable-tail-call Enable the post-MVP tail call feature\n");
  140. printf(" --disable-simd Disable the post-MVP 128-bit SIMD feature:\n");
  141. printf(" currently 128-bit SIMD is supported for x86-64 and aarch64 targets,\n");
  142. printf(" and by default it is enabled in them and disabled in other targets\n");
  143. printf(" --disable-ref-types Disable the MVP reference types feature, it will be disabled forcibly if\n");
  144. printf(" GC is enabled\n");
  145. printf(" --disable-aux-stack-check Disable auxiliary stack overflow/underflow check\n");
  146. printf(" --enable-dump-call-stack Enable stack trace feature\n");
  147. printf(" --call-stack-features=<features>\n");
  148. printf(" A comma-separated list of features when generating call stacks.\n");
  149. printf(" By default, all features are enabled. To disable all features,\n");
  150. printf(" provide an empty list (i.e. --call-stack-features=). This flag\n");
  151. printf(" only only takes effect when --enable-dump-call-stack is set.\n");
  152. printf(" Available features: bounds-checks, ip, func-idx, trap-ip, values.\n");
  153. printf(" --enable-perf-profiling Enable function performance profiling\n");
  154. printf(" --enable-memory-profiling Enable memory usage profiling\n");
  155. printf(" --xip A shorthand of --enable-indirect-mode --disable-llvm-intrinsics\n");
  156. printf(" --enable-indirect-mode Enable call function through symbol table but not direct call\n");
  157. printf(" --enable-gc Enable GC (Garbage Collection) feature\n");
  158. printf(" --disable-llvm-intrinsics Disable the LLVM built-in intrinsics\n");
  159. printf(" --enable-builtin-intrinsics=<flags>\n");
  160. printf(" Enable the specified built-in intrinsics, it will override the default\n");
  161. printf(" settings. It only takes effect when --disable-llvm-intrinsics is set.\n");
  162. printf(" Available flags: all, i32.common, i64.common, f32.common, f64.common,\n");
  163. printf(" i32.clz, i32.ctz, etc, refer to doc/xip.md for full list\n");
  164. printf(" Use comma to separate, please refer to doc/xip.md for full list.\n");
  165. printf(" --disable-llvm-lto Disable the LLVM link time optimization\n");
  166. printf(" --enable-llvm-pgo Enable LLVM PGO (Profile-Guided Optimization)\n");
  167. printf(" --enable-llvm-passes=<passes>\n");
  168. printf(" Enable the specified LLVM passes, using comma to separate\n");
  169. printf(" --use-prof-file=<file> Use profile file collected by LLVM PGO (Profile-Guided Optimization)\n");
  170. printf(" --enable-segue[=<flags>] Enable using segment register GS as the base address of linear memory,\n");
  171. printf(" only available on linux x86-64, which may improve performance,\n");
  172. printf(" flags can be: i32.load, i64.load, f32.load, f64.load, v128.load,\n");
  173. printf(" i32.store, i64.store, f32.store, f64.store, v128.store\n");
  174. printf(" Use comma to separate, e.g. --enable-segue=i32.load,i64.store\n");
  175. printf(" and --enable-segue means all flags are added.\n");
  176. printf(" --emit-custom-sections=<section names>\n");
  177. printf(" Emit the specified custom sections to AoT file, using comma to separate\n");
  178. printf(" multiple names, e.g.\n");
  179. printf(" --emit-custom-sections=section1,section2,sectionN\n");
  180. #if BH_HAS_DLFCN
  181. printf(" --native-lib=<lib> Register native libraries to the WASM module, which\n");
  182. printf(" are shared object (.so) files, for example:\n");
  183. printf(" --native-lib=test1.so --native-lib=test2.so\n");
  184. #endif
  185. printf(" --invoke-c-api-import Treat unknown import function as wasm-c-api import function and\n");
  186. printf(" quick call it from AOT code\n");
  187. #if WASM_ENABLE_LINUX_PERF != 0
  188. printf(" --enable-linux-perf Enable linux perf support\n");
  189. #endif
  190. printf(" --mllvm=<option> Add the LLVM command line option\n");
  191. printf(" --enable-shared-heap Enable shared heap feature\n");
  192. printf(" --enable-multi-module Enable multi-module(loader linking) feature. By default, it is \n");
  193. printf(" instantiation linking\n");
  194. printf(" -v=n Set log verbose level (0 to 5, default is 2), larger with more log\n");
  195. printf(" --version Show version information\n");
  196. printf("Examples: wamrc -o test.aot test.wasm\n");
  197. printf(" wamrc --target=i386 -o test.aot test.wasm\n");
  198. printf(" wamrc --target=i386 --format=object -o test.o test.wasm\n");
  199. printf(" wamrc --target-abi=help\n");
  200. printf(" wamrc --target=x86_64 --cpu=help\n");
  201. }
  202. /* clang-format on */
  203. #define PRINT_HELP_AND_EXIT() \
  204. do { \
  205. print_help(); \
  206. goto fail0; \
  207. } while (0)
  208. /**
  209. * Split a string into an array of strings
  210. * Returns NULL on failure
  211. * Memory must be freed by caller
  212. * Based on: http://stackoverflow.com/a/11198630/471795
  213. */
  214. static char **
  215. split_string(char *str, int *count, const char *delimer)
  216. {
  217. char **res = NULL, **res1;
  218. char *p;
  219. int idx = 0;
  220. /* split string and append tokens to 'res' */
  221. do {
  222. p = strtok(str, delimer);
  223. str = NULL;
  224. res1 = res;
  225. res = (char **)realloc(res1, sizeof(char *) * (uint32)(idx + 1));
  226. if (res == NULL) {
  227. free(res1);
  228. return NULL;
  229. }
  230. res[idx++] = p;
  231. } while (p);
  232. /**
  233. * Due to the section name,
  234. * res[0] might contain a '\' to indicate a space
  235. * func\name -> func name
  236. */
  237. p = strchr(res[0], '\\');
  238. while (p) {
  239. *p = ' ';
  240. p = strchr(p, '\\');
  241. }
  242. if (count) {
  243. *count = idx - 1;
  244. }
  245. return res;
  246. }
  247. static bool
  248. parse_call_stack_features(char *features_str,
  249. AOTCallStackFeatures *out_features)
  250. {
  251. int size = 0;
  252. char **features;
  253. bool ret = true;
  254. bh_assert(features_str);
  255. bh_assert(out_features);
  256. /* non-empty feature list */
  257. features = split_string(features_str, &size, ",");
  258. if (!features) {
  259. return false;
  260. }
  261. while (size--) {
  262. if (!strcmp(features[size], "bounds-checks")) {
  263. out_features->bounds_checks = true;
  264. }
  265. else if (!strcmp(features[size], "ip")) {
  266. out_features->ip = true;
  267. }
  268. else if (!strcmp(features[size], "trap-ip")) {
  269. out_features->trap_ip = true;
  270. }
  271. else if (!strcmp(features[size], "values")) {
  272. out_features->values = true;
  273. }
  274. else if (!strcmp(features[size], "func-idx")) {
  275. out_features->func_idx = true;
  276. }
  277. else {
  278. ret = false;
  279. printf("Unsupported feature %s\n", features[size]);
  280. goto finish;
  281. }
  282. }
  283. finish:
  284. free(features);
  285. return ret;
  286. }
  287. static bool
  288. can_enable_tiny_frame(const AOTCompOption *opt)
  289. {
  290. return !opt->call_stack_features.values && !opt->enable_gc
  291. && !opt->enable_perf_profiling;
  292. }
  293. static uint32
  294. resolve_segue_flags(char *str_flags)
  295. {
  296. uint32 segue_flags = 0;
  297. int32 flag_count, i;
  298. char **flag_list;
  299. flag_list = split_string(str_flags, &flag_count, ",");
  300. if (flag_list) {
  301. for (i = 0; i < flag_count; i++) {
  302. if (!strcmp(flag_list[i], "i32.load")) {
  303. segue_flags |= 1 << 0;
  304. }
  305. else if (!strcmp(flag_list[i], "i64.load")) {
  306. segue_flags |= 1 << 1;
  307. }
  308. else if (!strcmp(flag_list[i], "f32.load")) {
  309. segue_flags |= 1 << 2;
  310. }
  311. else if (!strcmp(flag_list[i], "f64.load")) {
  312. segue_flags |= 1 << 3;
  313. }
  314. else if (!strcmp(flag_list[i], "v128.load")) {
  315. segue_flags |= 1 << 4;
  316. }
  317. else if (!strcmp(flag_list[i], "i32.store")) {
  318. segue_flags |= 1 << 8;
  319. }
  320. else if (!strcmp(flag_list[i], "i64.store")) {
  321. segue_flags |= 1 << 9;
  322. }
  323. else if (!strcmp(flag_list[i], "f32.store")) {
  324. segue_flags |= 1 << 10;
  325. }
  326. else if (!strcmp(flag_list[i], "f64.store")) {
  327. segue_flags |= 1 << 11;
  328. }
  329. else if (!strcmp(flag_list[i], "v128.store")) {
  330. segue_flags |= 1 << 12;
  331. }
  332. else {
  333. /* invalid flag */
  334. segue_flags = (uint32)-1;
  335. break;
  336. }
  337. }
  338. free(flag_list);
  339. }
  340. return segue_flags;
  341. }
  342. /* When print help info for target/cpu/target-abi/cpu-features, load this dummy
  343. * wasm file content rather than from an input file, the dummy wasm file content
  344. * is: magic header + version number */
  345. static unsigned char dummy_wasm_file[8] = { 0x00, 0x61, 0x73, 0x6D,
  346. 0x01, 0x00, 0x00, 0x00 };
  347. int
  348. main(int argc, char *argv[])
  349. {
  350. char *wasm_file_name = NULL, *out_file_name = NULL;
  351. char **llvm_options = NULL;
  352. size_t llvm_options_count = 0;
  353. uint8 *wasm_file = NULL;
  354. uint32 wasm_file_size;
  355. wasm_module_t wasm_module = NULL;
  356. aot_comp_data_t comp_data = NULL;
  357. aot_comp_context_t comp_ctx = NULL;
  358. RuntimeInitArgs init_args;
  359. AOTCompOption option = { 0 };
  360. char error_buf[128];
  361. int log_verbose_level = 2;
  362. bool sgx_mode = false, size_level_set = false, use_dummy_wasm = false;
  363. int exit_status = EXIT_FAILURE;
  364. #if BH_HAS_DLFCN
  365. const char *native_lib_list[8] = { NULL };
  366. uint32 native_lib_count = 0;
  367. void *native_handle_list[8] = { NULL };
  368. uint32 native_handle_count = 0;
  369. #endif
  370. #if WASM_ENABLE_LINUX_PERF != 0
  371. bool enable_linux_perf = false;
  372. #endif
  373. option.opt_level = 3;
  374. option.size_level = 3;
  375. option.output_format = AOT_FORMAT_FILE;
  376. /* default value, enable or disable depends on the platform */
  377. option.bounds_checks = 2;
  378. /* default value, enable or disable depends on the platform */
  379. option.stack_bounds_checks = 2;
  380. option.enable_simd = true;
  381. option.enable_aux_stack_check = true;
  382. option.enable_bulk_memory = true;
  383. option.enable_ref_types = true;
  384. option.enable_gc = false;
  385. aot_call_stack_features_init_default(&option.call_stack_features);
  386. /* Process options */
  387. for (argc--, argv++; argc > 0 && argv[0][0] == '-'; argc--, argv++) {
  388. if (!strcmp(argv[0], "-o")) {
  389. argc--, argv++;
  390. if (argc < 2)
  391. PRINT_HELP_AND_EXIT();
  392. out_file_name = argv[0];
  393. }
  394. else if (!strncmp(argv[0], "--target=", 9)) {
  395. if (argv[0][9] == '\0')
  396. PRINT_HELP_AND_EXIT();
  397. option.target_arch = argv[0] + 9;
  398. if (!strcmp(option.target_arch, "help")) {
  399. use_dummy_wasm = true;
  400. }
  401. }
  402. else if (!strncmp(argv[0], "--target-abi=", 13)) {
  403. if (argv[0][13] == '\0')
  404. PRINT_HELP_AND_EXIT();
  405. option.target_abi = argv[0] + 13;
  406. if (!strcmp(option.target_abi, "help")) {
  407. use_dummy_wasm = true;
  408. }
  409. }
  410. else if (!strncmp(argv[0], "--cpu=", 6)) {
  411. if (argv[0][6] == '\0')
  412. PRINT_HELP_AND_EXIT();
  413. option.target_cpu = argv[0] + 6;
  414. if (!strcmp(option.target_cpu, "help")) {
  415. use_dummy_wasm = true;
  416. }
  417. }
  418. else if (!strncmp(argv[0], "--cpu-features=", 15)) {
  419. if (argv[0][15] == '\0')
  420. PRINT_HELP_AND_EXIT();
  421. option.cpu_features = argv[0] + 15;
  422. if (!strcmp(option.cpu_features, "+help")) {
  423. use_dummy_wasm = true;
  424. }
  425. }
  426. else if (!strncmp(argv[0], "--opt-level=", 12)) {
  427. if (argv[0][12] == '\0')
  428. PRINT_HELP_AND_EXIT();
  429. option.opt_level = (uint32)atoi(argv[0] + 12);
  430. if (option.opt_level > 3)
  431. option.opt_level = 3;
  432. }
  433. else if (!strncmp(argv[0], "--size-level=", 13)) {
  434. if (argv[0][13] == '\0')
  435. PRINT_HELP_AND_EXIT();
  436. option.size_level = (uint32)atoi(argv[0] + 13);
  437. if (option.size_level > 3)
  438. option.size_level = 3;
  439. size_level_set = true;
  440. }
  441. else if (!strcmp(argv[0], "-sgx")) {
  442. sgx_mode = true;
  443. }
  444. else if (!strncmp(argv[0], "--bounds-checks=", 16)) {
  445. option.bounds_checks = (atoi(argv[0] + 16) == 1) ? 1 : 0;
  446. }
  447. else if (!strncmp(argv[0], "--stack-bounds-checks=", 22)) {
  448. option.stack_bounds_checks = (atoi(argv[0] + 22) == 1) ? 1 : 0;
  449. }
  450. else if (!strncmp(argv[0], "--stack-usage=", 14)) {
  451. option.stack_usage_file = argv[0] + 14;
  452. }
  453. else if (!strncmp(argv[0], "--format=", 9)) {
  454. if (argv[0][9] == '\0')
  455. PRINT_HELP_AND_EXIT();
  456. if (!strcmp(argv[0] + 9, "aot"))
  457. option.output_format = AOT_FORMAT_FILE;
  458. else if (!strcmp(argv[0] + 9, "object"))
  459. option.output_format = AOT_OBJECT_FILE;
  460. else if (!strcmp(argv[0] + 9, "llvmir-unopt"))
  461. option.output_format = AOT_LLVMIR_UNOPT_FILE;
  462. else if (!strcmp(argv[0] + 9, "llvmir-opt"))
  463. option.output_format = AOT_LLVMIR_OPT_FILE;
  464. else {
  465. printf("Invalid format %s.\n", argv[0] + 9);
  466. PRINT_HELP_AND_EXIT();
  467. }
  468. }
  469. else if (!strncmp(argv[0], "-v=", 3)) {
  470. log_verbose_level = atoi(argv[0] + 3);
  471. if (log_verbose_level < 0 || log_verbose_level > 5)
  472. PRINT_HELP_AND_EXIT();
  473. }
  474. else if (!strcmp(argv[0], "--disable-bulk-memory")) {
  475. option.enable_bulk_memory = false;
  476. }
  477. else if (!strcmp(argv[0], "--enable-multi-thread")) {
  478. option.enable_bulk_memory = true;
  479. option.enable_thread_mgr = true;
  480. }
  481. else if (!strcmp(argv[0], "--enable-tail-call")) {
  482. option.enable_tail_call = true;
  483. }
  484. else if (!strcmp(argv[0], "--enable-simd")) {
  485. /* obsolete option, kept for compatibility */
  486. option.enable_simd = true;
  487. }
  488. else if (!strcmp(argv[0], "--disable-simd")) {
  489. option.enable_simd = false;
  490. }
  491. else if (!strcmp(argv[0], "--disable-ref-types")) {
  492. option.enable_ref_types = false;
  493. }
  494. else if (!strcmp(argv[0], "--disable-aux-stack-check")) {
  495. option.enable_aux_stack_check = false;
  496. }
  497. else if (!strcmp(argv[0], "--enable-dump-call-stack")) {
  498. option.aux_stack_frame_type = AOT_STACK_FRAME_TYPE_STANDARD;
  499. }
  500. else if (!strncmp(argv[0], "--call-stack-features=", 22)) {
  501. /* Reset all the features, only enable the user-defined ones */
  502. memset(&option.call_stack_features, 0,
  503. sizeof(AOTCallStackFeatures));
  504. if (argv[0][22] != '\0') {
  505. if (!parse_call_stack_features(argv[0] + 22,
  506. &option.call_stack_features)) {
  507. printf("Failed to parse call-stack-features\n");
  508. PRINT_HELP_AND_EXIT();
  509. }
  510. }
  511. }
  512. else if (!strcmp(argv[0], "--enable-perf-profiling")) {
  513. option.aux_stack_frame_type = AOT_STACK_FRAME_TYPE_STANDARD;
  514. option.enable_perf_profiling = true;
  515. }
  516. else if (!strcmp(argv[0], "--enable-memory-profiling")) {
  517. option.enable_memory_profiling = true;
  518. option.enable_stack_estimation = true;
  519. }
  520. else if (!strcmp(argv[0], "--xip")) {
  521. option.is_indirect_mode = true;
  522. option.disable_llvm_intrinsics = true;
  523. }
  524. else if (!strcmp(argv[0], "--enable-indirect-mode")) {
  525. option.is_indirect_mode = true;
  526. }
  527. else if (!strcmp(argv[0], "--enable-gc")) {
  528. option.aux_stack_frame_type = AOT_STACK_FRAME_TYPE_STANDARD;
  529. option.enable_gc = true;
  530. }
  531. else if (!strcmp(argv[0], "--disable-llvm-intrinsics")) {
  532. option.disable_llvm_intrinsics = true;
  533. }
  534. else if (!strncmp(argv[0], "--enable-builtin-intrinsics=", 28)) {
  535. if (argv[0][28] == '\0')
  536. PRINT_HELP_AND_EXIT();
  537. option.builtin_intrinsics = argv[0] + 28;
  538. }
  539. else if (!strcmp(argv[0], "--disable-llvm-lto")) {
  540. option.disable_llvm_lto = true;
  541. }
  542. else if (!strcmp(argv[0], "--enable-llvm-pgo")) {
  543. option.enable_llvm_pgo = true;
  544. }
  545. else if (!strncmp(argv[0], "--enable-llvm-passes=", 21)) {
  546. if (argv[0][21] == '\0')
  547. PRINT_HELP_AND_EXIT();
  548. option.llvm_passes = argv[0] + 21;
  549. }
  550. else if (!strncmp(argv[0], "--use-prof-file=", 16)) {
  551. if (argv[0][16] == '\0')
  552. PRINT_HELP_AND_EXIT();
  553. option.use_prof_file = argv[0] + 16;
  554. }
  555. else if (!strcmp(argv[0], "--enable-segue")) {
  556. /* all flags are enabled */
  557. option.segue_flags = 0x1F1F;
  558. }
  559. else if (!strncmp(argv[0], "--enable-segue=", 15)) {
  560. option.segue_flags = resolve_segue_flags(argv[0] + 15);
  561. if (option.segue_flags == (uint32)-1)
  562. PRINT_HELP_AND_EXIT();
  563. }
  564. else if (!strncmp(argv[0], "--emit-custom-sections=", 23)) {
  565. int len = 0;
  566. if (option.custom_sections) {
  567. free(option.custom_sections);
  568. }
  569. option.custom_sections = split_string(argv[0] + 23, &len, ",");
  570. if (!option.custom_sections) {
  571. printf("Failed to process emit-custom-sections: alloc "
  572. "memory failed\n");
  573. PRINT_HELP_AND_EXIT();
  574. }
  575. option.custom_sections_count = len;
  576. }
  577. #if BH_HAS_DLFCN
  578. else if (!strncmp(argv[0], "--native-lib=", 13)) {
  579. if (argv[0][13] == '\0')
  580. PRINT_HELP_AND_EXIT();
  581. if (native_lib_count >= sizeof(native_lib_list) / sizeof(char *)) {
  582. printf("Only allow max native lib number %d\n",
  583. (int)(sizeof(native_lib_list) / sizeof(char *)));
  584. goto fail0;
  585. }
  586. native_lib_list[native_lib_count++] = argv[0] + 13;
  587. }
  588. #endif
  589. else if (!strcmp(argv[0], "--invoke-c-api-import")) {
  590. option.quick_invoke_c_api_import = true;
  591. }
  592. #if WASM_ENABLE_LINUX_PERF != 0
  593. else if (!strcmp(argv[0], "--enable-linux-perf")) {
  594. enable_linux_perf = true;
  595. }
  596. #endif
  597. else if (!strncmp(argv[0], "--mllvm=", 8)) {
  598. void *np;
  599. if (argv[0][8] == '\0')
  600. PRINT_HELP_AND_EXIT();
  601. if (llvm_options_count == 0)
  602. llvm_options_count += 2;
  603. else
  604. llvm_options_count++;
  605. np = realloc(llvm_options, llvm_options_count * sizeof(char *));
  606. if (np == NULL) {
  607. printf("Memory allocation failure\n");
  608. goto fail0;
  609. }
  610. llvm_options = np;
  611. if (llvm_options_count == 2)
  612. llvm_options[llvm_options_count - 2] = "wamrc";
  613. llvm_options[llvm_options_count - 1] = argv[0] + 8;
  614. }
  615. else if (!strcmp(argv[0], "--enable-shared-heap")) {
  616. option.enable_shared_heap = true;
  617. }
  618. else if (!strcmp(argv[0], "--enable-multi-module")) {
  619. option.enable_multi_module = true;
  620. }
  621. else if (!strcmp(argv[0], "--version")) {
  622. uint32 major, minor, patch;
  623. wasm_runtime_get_version(&major, &minor, &patch);
  624. printf("wamrc %u.%u.%u\n", major, minor, patch);
  625. return 0;
  626. }
  627. else
  628. PRINT_HELP_AND_EXIT();
  629. }
  630. if (!use_dummy_wasm && (argc == 0 || !out_file_name))
  631. PRINT_HELP_AND_EXIT();
  632. if (option.aux_stack_frame_type == AOT_STACK_FRAME_TYPE_STANDARD
  633. && can_enable_tiny_frame(&option)) {
  634. LOG_VERBOSE("Use tiny frame mode for stack frames");
  635. option.aux_stack_frame_type = AOT_STACK_FRAME_TYPE_TINY;
  636. /* for now we only enable frame per function for a TINY frame mode */
  637. option.call_stack_features.frame_per_function = true;
  638. }
  639. if (!option.call_stack_features.func_idx
  640. && (option.enable_gc || option.enable_perf_profiling)) {
  641. LOG_WARNING("'func-idx' call stack feature will be automatically "
  642. "enabled for GC and perf profiling mode");
  643. option.call_stack_features.func_idx = true;
  644. }
  645. if (!size_level_set) {
  646. /**
  647. * Set opt level to 1 by default for Windows and MacOS as
  648. * they can not memory map out 0-2GB memory and might not
  649. * be able to meet the requirements of some AOT relocation
  650. * operations.
  651. */
  652. if (option.target_abi && !strcmp(option.target_abi, "msvc")) {
  653. LOG_VERBOSE("Set size level to 1 for Windows AOT file");
  654. option.size_level = 1;
  655. }
  656. #if defined(_WIN32) || defined(_WIN32_) \
  657. || ((defined(__APPLE__) || defined(__MACH__)) && !defined(__arm64__))
  658. if (!option.target_arch && !option.target_abi) {
  659. LOG_VERBOSE("Set size level to 1 for Windows or MacOS AOT file");
  660. option.size_level = 1;
  661. }
  662. #endif
  663. }
  664. if (option.enable_gc && !option.call_stack_features.values) {
  665. LOG_WARNING("Call stack feature 'values' must be enabled for GC. The "
  666. "feature will be enabled automatically.");
  667. option.call_stack_features.values = true;
  668. }
  669. if (sgx_mode) {
  670. option.size_level = 1;
  671. option.is_sgx_platform = true;
  672. }
  673. if (option.enable_gc) {
  674. option.enable_ref_types = false;
  675. }
  676. if (!use_dummy_wasm) {
  677. wasm_file_name = argv[0];
  678. if (!strcmp(wasm_file_name, out_file_name)) {
  679. printf("Error: input file and output file are the same");
  680. return -1;
  681. }
  682. }
  683. memset(&init_args, 0, sizeof(RuntimeInitArgs));
  684. init_args.mem_alloc_type = Alloc_With_Allocator;
  685. init_args.mem_alloc_option.allocator.malloc_func = malloc;
  686. init_args.mem_alloc_option.allocator.realloc_func = realloc;
  687. init_args.mem_alloc_option.allocator.free_func = free;
  688. #if WASM_ENABLE_LINUX_PERF != 0
  689. init_args.enable_linux_perf = enable_linux_perf;
  690. #endif
  691. /* initialize runtime environment */
  692. if (!wasm_runtime_full_init(&init_args)) {
  693. printf("Init runtime environment failed.\n");
  694. return -1;
  695. }
  696. bh_log_set_verbose_level(log_verbose_level);
  697. #if BH_HAS_DLFCN
  698. bh_print_time("Begin to load native libs");
  699. native_handle_count = load_and_register_native_libs(
  700. native_lib_list, native_lib_count, native_handle_list);
  701. #endif
  702. if (llvm_options_count > 0)
  703. LLVMParseCommandLineOptions(llvm_options_count,
  704. (const char **)llvm_options, "wamrc");
  705. bh_print_time("Begin to load wasm file");
  706. if (use_dummy_wasm) {
  707. /* load WASM byte buffer from dummy buffer */
  708. wasm_file_size = sizeof(dummy_wasm_file);
  709. wasm_file = dummy_wasm_file;
  710. }
  711. else {
  712. /* load WASM byte buffer from WASM bin file */
  713. if (!(wasm_file = (uint8 *)bh_read_file_to_buffer(wasm_file_name,
  714. &wasm_file_size)))
  715. goto fail1;
  716. }
  717. if (wasm_file_size >= 4 /* length of MAGIC NUMBER */
  718. && get_package_type(wasm_file, wasm_file_size)
  719. != Wasm_Module_Bytecode) {
  720. printf("Invalid wasm file: magic header not detected\n");
  721. goto fail2;
  722. }
  723. /* load WASM module */
  724. if (!(wasm_module = wasm_runtime_load(wasm_file, wasm_file_size, error_buf,
  725. sizeof(error_buf)))) {
  726. printf("%s\n", error_buf);
  727. goto fail2;
  728. }
  729. if (!(comp_data = aot_create_comp_data(wasm_module, option.target_arch,
  730. option.enable_gc))) {
  731. printf("%s\n", aot_get_last_error());
  732. goto fail3;
  733. }
  734. #if WASM_ENABLE_DEBUG_AOT != 0
  735. if (!create_dwarf_extractor(comp_data, wasm_file_name)) {
  736. printf("%s:create dwarf extractor failed\n", wasm_file_name);
  737. }
  738. #endif
  739. bh_print_time("Begin to create compile context");
  740. if (!(comp_ctx = aot_create_comp_context(comp_data, &option))) {
  741. printf("%s\n", aot_get_last_error());
  742. goto fail4;
  743. }
  744. bh_print_time("Begin to compile");
  745. if (!aot_compile_wasm(comp_ctx)) {
  746. printf("%s\n", aot_get_last_error());
  747. goto fail5;
  748. }
  749. switch (option.output_format) {
  750. case AOT_LLVMIR_UNOPT_FILE:
  751. case AOT_LLVMIR_OPT_FILE:
  752. if (!aot_emit_llvm_file(comp_ctx, out_file_name)) {
  753. printf("%s\n", aot_get_last_error());
  754. goto fail5;
  755. }
  756. break;
  757. case AOT_OBJECT_FILE:
  758. if (!aot_emit_object_file(comp_ctx, out_file_name)) {
  759. printf("%s\n", aot_get_last_error());
  760. goto fail5;
  761. }
  762. break;
  763. case AOT_FORMAT_FILE:
  764. if (!aot_emit_aot_file(comp_ctx, comp_data, out_file_name)) {
  765. printf("%s\n", aot_get_last_error());
  766. goto fail5;
  767. }
  768. break;
  769. default:
  770. break;
  771. }
  772. bh_print_time("Compile end");
  773. printf("Compile success, file %s was generated.\n", out_file_name);
  774. exit_status = EXIT_SUCCESS;
  775. fail5:
  776. /* Destroy compiler context */
  777. aot_destroy_comp_context(comp_ctx);
  778. fail4:
  779. /* Destroy compile data */
  780. aot_destroy_comp_data(comp_data);
  781. fail3:
  782. /* Unload WASM module */
  783. wasm_runtime_unload(wasm_module);
  784. fail2:
  785. /* free the file buffer */
  786. if (!use_dummy_wasm) {
  787. wasm_runtime_free(wasm_file);
  788. }
  789. fail1:
  790. #if BH_HAS_DLFCN
  791. unregister_and_unload_native_libs(native_handle_count, native_handle_list);
  792. #endif
  793. /* Destroy runtime environment */
  794. wasm_runtime_destroy();
  795. fail0:
  796. /* free option.custom_sections */
  797. if (option.custom_sections) {
  798. free(option.custom_sections);
  799. }
  800. free(llvm_options);
  801. bh_print_time("wamrc return");
  802. return exit_status;
  803. }