main.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  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. /* clang-format off */
  11. static void
  12. print_help()
  13. {
  14. printf("Usage: wamrc [options] -o output_file wasm_file\n");
  15. printf(" --target=<arch-name> Set the target arch, which has the general format: <arch><sub>\n");
  16. printf(" <arch> = x86_64, i386, aarch64, arm, thumb, xtensa, mips,\n");
  17. printf(" riscv64, riscv32.\n");
  18. printf(" Default is host arch, e.g. x86_64\n");
  19. printf(" <sub> = for ex. on arm or thumb: v5, v6m, v7a, v7m, etc.\n");
  20. printf(" Use --target=help to list supported targets\n");
  21. printf(" --target-abi=<abi> Set the target ABI, e.g. gnu, eabi, gnueabihf, msvc, etc.\n");
  22. printf(" Default is gnu if target isn't riscv64 or riscv32\n");
  23. printf(" For target riscv64 and riscv32, default is lp64d and ilp32d\n");
  24. printf(" Use --target-abi=help to list all the ABI supported\n");
  25. printf(" --cpu=<cpu> Set the target CPU (default: host CPU, e.g. skylake)\n");
  26. printf(" Use --cpu=help to list all the CPU supported\n");
  27. printf(" --cpu-features=<features> Enable or disable the CPU features\n");
  28. printf(" Use +feature to enable a feature, or -feature to disable it\n");
  29. printf(" For example, --cpu-features=+feature1,-feature2\n");
  30. printf(" Use --cpu-features=+help to list all the features supported\n");
  31. printf(" --opt-level=n Set the optimization level (0 to 3, default is 3)\n");
  32. printf(" --size-level=n Set the code size level (0 to 3, default is 3)\n");
  33. printf(" -sgx Generate code for SGX platform (Intel Software Guard Extention)\n");
  34. printf(" --bounds-checks=1/0 Enable or disable the bounds checks for memory access:\n");
  35. printf(" by default it is disabled in all 64-bit platforms except SGX and\n");
  36. printf(" in these platforms runtime does bounds checks with hardware trap,\n");
  37. printf(" and by default it is enabled in all 32-bit platforms\n");
  38. printf(" --stack-bounds-checks=1/0 Enable or disable the bounds checks for native stack:\n");
  39. printf(" if the option isn't set, the status is same as `--bounds-check`,\n");
  40. printf(" if the option is set:\n");
  41. printf(" (1) it is always enabled when `--bounds-checks` is enabled,\n");
  42. printf(" (2) else it is enabled/disabled according to the option value\n");
  43. printf(" --format=<format> Specifies the format of the output file\n");
  44. printf(" The format supported:\n");
  45. printf(" aot (default) AoT file\n");
  46. printf(" object Native object file\n");
  47. printf(" llvmir-unopt Unoptimized LLVM IR\n");
  48. printf(" llvmir-opt Optimized LLVM IR\n");
  49. printf(" --disable-bulk-memory Disable the MVP bulk memory feature\n");
  50. printf(" --enable-multi-thread Enable multi-thread feature, the dependent features bulk-memory and\n");
  51. printf(" thread-mgr will be enabled automatically\n");
  52. printf(" --enable-tail-call Enable the post-MVP tail call feature\n");
  53. printf(" --disable-simd Disable the post-MVP 128-bit SIMD feature:\n");
  54. printf(" currently 128-bit SIMD is supported for x86-64 and aarch64 targets,\n");
  55. printf(" and by default it is enabled in them and disabled in other targets\n");
  56. printf(" --disable-ref-types Disable the MVP reference types feature\n");
  57. printf(" --disable-aux-stack-check Disable auxiliary stack overflow/underflow check\n");
  58. printf(" --enable-dump-call-stack Enable stack trace feature\n");
  59. printf(" --enable-perf-profiling Enable function performance profiling\n");
  60. printf(" --enable-memory-profiling Enable memory usage profiling\n");
  61. printf(" --enable-indirect-mode Enalbe call function through symbol table but not direct call\n");
  62. printf(" --disable-llvm-intrinsics Disable the LLVM built-in intrinsics\n");
  63. printf(" --disable-llvm-lto Disable the LLVM link time optimization\n");
  64. printf(" --emit-custom-sections=<section names>\n");
  65. printf(" Emit the specified custom sections to AoT file, using comma to separate\n");
  66. printf(" multiple names, e.g.\n");
  67. printf(" --emit-custom-sections=section1,section2,sectionN\n");
  68. printf(" -v=n Set log verbose level (0 to 5, default is 2), larger with more log\n");
  69. printf(" --version Show version information\n");
  70. printf("Examples: wamrc -o test.aot test.wasm\n");
  71. printf(" wamrc --target=i386 -o test.aot test.wasm\n");
  72. printf(" wamrc --target=i386 --format=object -o test.o test.wasm\n");
  73. }
  74. /* clang-format on */
  75. #define PRINT_HELP_AND_EXIT() \
  76. do { \
  77. print_help(); \
  78. goto fail0; \
  79. } while (0)
  80. /**
  81. * Split a strings into an array of strings
  82. * Returns NULL on failure
  83. * Memory must be freed by caller
  84. * Based on: http://stackoverflow.com/a/11198630/471795
  85. */
  86. static char **
  87. split_string(char *str, int *count, const char *delimer)
  88. {
  89. char **res = NULL, **res1;
  90. char *p;
  91. int idx = 0;
  92. /* split string and append tokens to 'res' */
  93. do {
  94. p = strtok(str, delimer);
  95. str = NULL;
  96. res1 = res;
  97. res = (char **)realloc(res1, sizeof(char *) * (uint32)(idx + 1));
  98. if (res == NULL) {
  99. free(res1);
  100. return NULL;
  101. }
  102. res[idx++] = p;
  103. } while (p);
  104. /**
  105. * Due to the section name,
  106. * res[0] might contain a '\' to indicate a space
  107. * func\name -> func name
  108. */
  109. p = strchr(res[0], '\\');
  110. while (p) {
  111. *p = ' ';
  112. p = strchr(p, '\\');
  113. }
  114. if (count) {
  115. *count = idx - 1;
  116. }
  117. return res;
  118. }
  119. int
  120. main(int argc, char *argv[])
  121. {
  122. char *wasm_file_name = NULL, *out_file_name = NULL;
  123. uint8 *wasm_file = NULL;
  124. uint32 wasm_file_size;
  125. wasm_module_t wasm_module = NULL;
  126. aot_comp_data_t comp_data = NULL;
  127. aot_comp_context_t comp_ctx = NULL;
  128. RuntimeInitArgs init_args;
  129. AOTCompOption option = { 0 };
  130. char error_buf[128];
  131. int log_verbose_level = 2;
  132. bool sgx_mode = false, size_level_set = false;
  133. int exit_status = EXIT_FAILURE;
  134. option.opt_level = 3;
  135. option.size_level = 3;
  136. option.output_format = AOT_FORMAT_FILE;
  137. /* default value, enable or disable depends on the platform */
  138. option.bounds_checks = 2;
  139. /* default value, enable or disable depends on the platform */
  140. option.stack_bounds_checks = 2;
  141. option.enable_simd = true;
  142. option.enable_aux_stack_check = true;
  143. option.enable_bulk_memory = true;
  144. option.enable_ref_types = true;
  145. /* Process options */
  146. for (argc--, argv++; argc > 0 && argv[0][0] == '-'; argc--, argv++) {
  147. if (!strcmp(argv[0], "-o")) {
  148. argc--, argv++;
  149. if (argc < 2)
  150. PRINT_HELP_AND_EXIT();
  151. out_file_name = argv[0];
  152. }
  153. else if (!strncmp(argv[0], "--target=", 9)) {
  154. if (argv[0][9] == '\0')
  155. PRINT_HELP_AND_EXIT();
  156. option.target_arch = argv[0] + 9;
  157. }
  158. else if (!strncmp(argv[0], "--target-abi=", 13)) {
  159. if (argv[0][13] == '\0')
  160. PRINT_HELP_AND_EXIT();
  161. option.target_abi = argv[0] + 13;
  162. }
  163. else if (!strncmp(argv[0], "--cpu=", 6)) {
  164. if (argv[0][6] == '\0')
  165. PRINT_HELP_AND_EXIT();
  166. option.target_cpu = argv[0] + 6;
  167. }
  168. else if (!strncmp(argv[0], "--cpu-features=", 15)) {
  169. if (argv[0][15] == '\0')
  170. PRINT_HELP_AND_EXIT();
  171. option.cpu_features = argv[0] + 15;
  172. }
  173. else if (!strncmp(argv[0], "--opt-level=", 12)) {
  174. if (argv[0][12] == '\0')
  175. PRINT_HELP_AND_EXIT();
  176. option.opt_level = (uint32)atoi(argv[0] + 12);
  177. if (option.opt_level > 3)
  178. option.opt_level = 3;
  179. }
  180. else if (!strncmp(argv[0], "--size-level=", 13)) {
  181. if (argv[0][13] == '\0')
  182. PRINT_HELP_AND_EXIT();
  183. option.size_level = (uint32)atoi(argv[0] + 13);
  184. if (option.size_level > 3)
  185. option.size_level = 3;
  186. size_level_set = true;
  187. }
  188. else if (!strcmp(argv[0], "-sgx")) {
  189. sgx_mode = true;
  190. }
  191. else if (!strncmp(argv[0], "--bounds-checks=", 16)) {
  192. option.bounds_checks = (atoi(argv[0] + 16) == 1) ? 1 : 0;
  193. }
  194. else if (!strncmp(argv[0], "--stack-bounds-checks=", 22)) {
  195. option.stack_bounds_checks = (atoi(argv[0] + 22) == 1) ? 1 : 0;
  196. }
  197. else if (!strncmp(argv[0], "--format=", 9)) {
  198. if (argv[0][9] == '\0')
  199. PRINT_HELP_AND_EXIT();
  200. if (!strcmp(argv[0] + 9, "aot"))
  201. option.output_format = AOT_FORMAT_FILE;
  202. else if (!strcmp(argv[0] + 9, "object"))
  203. option.output_format = AOT_OBJECT_FILE;
  204. else if (!strcmp(argv[0] + 9, "llvmir-unopt"))
  205. option.output_format = AOT_LLVMIR_UNOPT_FILE;
  206. else if (!strcmp(argv[0] + 9, "llvmir-opt"))
  207. option.output_format = AOT_LLVMIR_OPT_FILE;
  208. else {
  209. printf("Invalid format %s.\n", argv[0] + 9);
  210. PRINT_HELP_AND_EXIT();
  211. }
  212. }
  213. else if (!strncmp(argv[0], "-v=", 3)) {
  214. log_verbose_level = atoi(argv[0] + 3);
  215. if (log_verbose_level < 0 || log_verbose_level > 5)
  216. PRINT_HELP_AND_EXIT();
  217. }
  218. else if (!strcmp(argv[0], "--disable-bulk-memory")) {
  219. option.enable_bulk_memory = false;
  220. }
  221. else if (!strcmp(argv[0], "--enable-multi-thread")) {
  222. option.enable_bulk_memory = true;
  223. option.enable_thread_mgr = true;
  224. option.enable_ref_types = false;
  225. }
  226. else if (!strcmp(argv[0], "--enable-tail-call")) {
  227. option.enable_tail_call = true;
  228. }
  229. else if (!strcmp(argv[0], "--enable-simd")) {
  230. /* obsolete option, kept for compatibility */
  231. option.enable_simd = true;
  232. }
  233. else if (!strcmp(argv[0], "--disable-simd")) {
  234. option.enable_simd = false;
  235. }
  236. else if (!strcmp(argv[0], "--disable-ref-types")) {
  237. option.enable_ref_types = false;
  238. }
  239. else if (!strcmp(argv[0], "--disable-aux-stack-check")) {
  240. option.enable_aux_stack_check = false;
  241. }
  242. else if (!strcmp(argv[0], "--enable-dump-call-stack")) {
  243. option.enable_aux_stack_frame = true;
  244. }
  245. else if (!strcmp(argv[0], "--enable-perf-profiling")) {
  246. option.enable_aux_stack_frame = true;
  247. }
  248. else if (!strcmp(argv[0], "--enable-memory-profiling")) {
  249. option.enable_stack_estimation = true;
  250. }
  251. else if (!strcmp(argv[0], "--enable-indirect-mode")) {
  252. option.is_indirect_mode = true;
  253. }
  254. else if (!strcmp(argv[0], "--disable-llvm-intrinsics")) {
  255. option.disable_llvm_intrinsics = true;
  256. }
  257. else if (!strcmp(argv[0], "--disable-llvm-lto")) {
  258. option.disable_llvm_lto = true;
  259. }
  260. else if (!strncmp(argv[0], "--emit-custom-sections=", 23)) {
  261. int len = 0;
  262. if (option.custom_sections) {
  263. free(option.custom_sections);
  264. }
  265. option.custom_sections = split_string(argv[0] + 23, &len, ",");
  266. if (!option.custom_sections) {
  267. printf("Failed to process emit-custom-sections: alloc "
  268. "memory failed\n");
  269. PRINT_HELP_AND_EXIT();
  270. }
  271. option.custom_sections_count = len;
  272. }
  273. else if (!strncmp(argv[0], "--version", 9)) {
  274. uint32 major, minor, patch;
  275. wasm_runtime_get_version(&major, &minor, &patch);
  276. printf("wamrc %u.%u.%u\n", major, minor, patch);
  277. return 0;
  278. }
  279. else
  280. PRINT_HELP_AND_EXIT();
  281. }
  282. if (argc == 0 || !out_file_name)
  283. PRINT_HELP_AND_EXIT();
  284. if (!size_level_set) {
  285. /**
  286. * Set opt level to 1 by default for Windows and MacOS as
  287. * they can not memory map out 0-2GB memory and might not
  288. * be able to meet the requirements of some AOT relocation
  289. * operations.
  290. */
  291. if (option.target_abi && !strcmp(option.target_abi, "msvc")) {
  292. LOG_VERBOSE("Set size level to 1 for Windows AOT file");
  293. option.size_level = 1;
  294. }
  295. #if defined(_WIN32) || defined(_WIN32_) || defined(__APPLE__) \
  296. || defined(__MACH__)
  297. if (!option.target_abi) {
  298. LOG_VERBOSE("Set size level to 1 for Windows or MacOS AOT file");
  299. option.size_level = 1;
  300. }
  301. #endif
  302. }
  303. if (sgx_mode) {
  304. option.size_level = 1;
  305. option.is_sgx_platform = true;
  306. }
  307. wasm_file_name = argv[0];
  308. if (!strcmp(wasm_file_name, out_file_name)) {
  309. printf("Error: input file and output file are the same");
  310. return -1;
  311. }
  312. memset(&init_args, 0, sizeof(RuntimeInitArgs));
  313. init_args.mem_alloc_type = Alloc_With_Allocator;
  314. init_args.mem_alloc_option.allocator.malloc_func = malloc;
  315. init_args.mem_alloc_option.allocator.realloc_func = realloc;
  316. init_args.mem_alloc_option.allocator.free_func = free;
  317. /* initialize runtime environment */
  318. if (!wasm_runtime_full_init(&init_args)) {
  319. printf("Init runtime environment failed.\n");
  320. return -1;
  321. }
  322. bh_log_set_verbose_level(log_verbose_level);
  323. bh_print_time("Begin to load wasm file");
  324. /* load WASM byte buffer from WASM bin file */
  325. if (!(wasm_file =
  326. (uint8 *)bh_read_file_to_buffer(wasm_file_name, &wasm_file_size)))
  327. goto fail1;
  328. if (get_package_type(wasm_file, wasm_file_size) != Wasm_Module_Bytecode) {
  329. printf("Invalid file type: expected wasm file but got other\n");
  330. goto fail2;
  331. }
  332. /* load WASM module */
  333. if (!(wasm_module = wasm_runtime_load(wasm_file, wasm_file_size, error_buf,
  334. sizeof(error_buf)))) {
  335. printf("%s\n", error_buf);
  336. goto fail2;
  337. }
  338. if (!(comp_data = aot_create_comp_data(wasm_module))) {
  339. printf("%s\n", aot_get_last_error());
  340. goto fail3;
  341. }
  342. #if WASM_ENABLE_DEBUG_AOT != 0
  343. if (!create_dwarf_extractor(comp_data, wasm_file_name)) {
  344. printf("%s:create dwarf extractor failed\n", wasm_file_name);
  345. }
  346. #endif
  347. bh_print_time("Begin to create compile context");
  348. if (!(comp_ctx = aot_create_comp_context(comp_data, &option))) {
  349. printf("%s\n", aot_get_last_error());
  350. goto fail4;
  351. }
  352. bh_print_time("Begin to compile");
  353. if (!aot_compile_wasm(comp_ctx)) {
  354. printf("%s\n", aot_get_last_error());
  355. goto fail5;
  356. }
  357. switch (option.output_format) {
  358. case AOT_LLVMIR_UNOPT_FILE:
  359. case AOT_LLVMIR_OPT_FILE:
  360. if (!aot_emit_llvm_file(comp_ctx, out_file_name)) {
  361. printf("%s\n", aot_get_last_error());
  362. goto fail5;
  363. }
  364. break;
  365. case AOT_OBJECT_FILE:
  366. if (!aot_emit_object_file(comp_ctx, out_file_name)) {
  367. printf("%s\n", aot_get_last_error());
  368. goto fail5;
  369. }
  370. break;
  371. case AOT_FORMAT_FILE:
  372. if (!aot_emit_aot_file(comp_ctx, comp_data, out_file_name)) {
  373. printf("%s\n", aot_get_last_error());
  374. goto fail5;
  375. }
  376. break;
  377. default:
  378. break;
  379. }
  380. bh_print_time("Compile end");
  381. printf("Compile success, file %s was generated.\n", out_file_name);
  382. exit_status = EXIT_SUCCESS;
  383. fail5:
  384. /* Destroy compiler context */
  385. aot_destroy_comp_context(comp_ctx);
  386. fail4:
  387. /* Destroy compile data */
  388. aot_destroy_comp_data(comp_data);
  389. fail3:
  390. /* Unload WASM module */
  391. wasm_runtime_unload(wasm_module);
  392. fail2:
  393. /* free the file buffer */
  394. wasm_runtime_free(wasm_file);
  395. fail1:
  396. /* Destroy runtime environment */
  397. wasm_runtime_destroy();
  398. fail0:
  399. /* free option.custom_sections */
  400. if (option.custom_sections) {
  401. free(option.custom_sections);
  402. }
  403. bh_print_time("wamrc return");
  404. return exit_status;
  405. }