main.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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 int
  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(" --format=<format> Specifies the format of the output file\n");
  39. printf(" The format supported:\n");
  40. printf(" aot (default) AoT file\n");
  41. printf(" object Native object file\n");
  42. printf(" llvmir-unopt Unoptimized LLVM IR\n");
  43. printf(" llvmir-opt Optimized LLVM IR\n");
  44. printf(" --disable-bulk-memory Disable the MVP bulk memory feature\n");
  45. printf(" --enable-multi-thread Enable multi-thread feature, the dependent features bulk-memory and\n");
  46. printf(" thread-mgr will be enabled automatically\n");
  47. printf(" --enable-tail-call Enable the post-MVP tail call feature\n");
  48. printf(" --disable-simd Disable the post-MVP 128-bit SIMD feature:\n");
  49. printf(" currently 128-bit SIMD is only supported for x86-64 target,\n");
  50. printf(" and by default it is enabled in x86-64 target and disabled\n");
  51. printf(" in other targets\n");
  52. printf(" --disable-ref-types Disable the MVP reference types feature\n");
  53. printf(" --disable-aux-stack-check Disable auxiliary stack overflow/underflow check\n");
  54. printf(" --enable-dump-call-stack Enable stack trace feature\n");
  55. printf(" --enable-perf-profiling Enable function performance profiling\n");
  56. printf(" --enable-indirect-mode Enalbe call function through symbol table but not direct call\n");
  57. printf(" --disable-llvm-intrinsics Disable the LLVM built-in intrinsics\n");
  58. printf(" --disable-llvm-lto Disable the LLVM link time optimization\n");
  59. printf(" -v=n Set log verbose level (0 to 5, default is 2), larger with more log\n");
  60. printf("Examples: wamrc -o test.aot test.wasm\n");
  61. printf(" wamrc --target=i386 -o test.aot test.wasm\n");
  62. printf(" wamrc --target=i386 --format=object -o test.o test.wasm\n");
  63. return 1;
  64. }
  65. /* clang-format on */
  66. int
  67. main(int argc, char *argv[])
  68. {
  69. char *wasm_file_name = NULL, *out_file_name = NULL;
  70. uint8 *wasm_file = NULL;
  71. uint32 wasm_file_size;
  72. wasm_module_t wasm_module = NULL;
  73. aot_comp_data_t comp_data = NULL;
  74. aot_comp_context_t comp_ctx = NULL;
  75. RuntimeInitArgs init_args;
  76. AOTCompOption option = { 0 };
  77. char error_buf[128];
  78. int log_verbose_level = 2;
  79. bool sgx_mode = false;
  80. int exit_status = EXIT_FAILURE;
  81. option.opt_level = 3;
  82. option.size_level = 3;
  83. option.output_format = AOT_FORMAT_FILE;
  84. /* default value, enable or disable depends on the platform */
  85. option.bounds_checks = 2;
  86. option.enable_simd = true;
  87. option.enable_aux_stack_check = true;
  88. option.enable_bulk_memory = true;
  89. option.enable_ref_types = true;
  90. /* Process options */
  91. for (argc--, argv++; argc > 0 && argv[0][0] == '-'; argc--, argv++) {
  92. if (!strcmp(argv[0], "-o")) {
  93. argc--, argv++;
  94. if (argc < 2)
  95. return print_help();
  96. out_file_name = argv[0];
  97. }
  98. else if (!strncmp(argv[0], "--target=", 9)) {
  99. if (argv[0][9] == '\0')
  100. return print_help();
  101. option.target_arch = argv[0] + 9;
  102. }
  103. else if (!strncmp(argv[0], "--target-abi=", 13)) {
  104. if (argv[0][13] == '\0')
  105. return print_help();
  106. option.target_abi = argv[0] + 13;
  107. }
  108. else if (!strncmp(argv[0], "--cpu=", 6)) {
  109. if (argv[0][6] == '\0')
  110. return print_help();
  111. option.target_cpu = argv[0] + 6;
  112. }
  113. else if (!strncmp(argv[0], "--cpu-features=", 15)) {
  114. if (argv[0][15] == '\0')
  115. return print_help();
  116. option.cpu_features = argv[0] + 15;
  117. }
  118. else if (!strncmp(argv[0], "--opt-level=", 12)) {
  119. if (argv[0][12] == '\0')
  120. return print_help();
  121. option.opt_level = (uint32)atoi(argv[0] + 12);
  122. if (option.opt_level > 3)
  123. option.opt_level = 3;
  124. }
  125. else if (!strncmp(argv[0], "--size-level=", 13)) {
  126. if (argv[0][13] == '\0')
  127. return print_help();
  128. option.size_level = (uint32)atoi(argv[0] + 13);
  129. if (option.size_level > 3)
  130. option.size_level = 3;
  131. }
  132. else if (!strcmp(argv[0], "-sgx")) {
  133. sgx_mode = true;
  134. }
  135. else if (!strncmp(argv[0], "--bounds-checks=", 16)) {
  136. option.bounds_checks = (atoi(argv[0] + 16) == 1) ? 1 : 0;
  137. }
  138. else if (!strncmp(argv[0], "--format=", 9)) {
  139. if (argv[0][9] == '\0')
  140. return print_help();
  141. if (!strcmp(argv[0] + 9, "aot"))
  142. option.output_format = AOT_FORMAT_FILE;
  143. else if (!strcmp(argv[0] + 9, "object"))
  144. option.output_format = AOT_OBJECT_FILE;
  145. else if (!strcmp(argv[0] + 9, "llvmir-unopt"))
  146. option.output_format = AOT_LLVMIR_UNOPT_FILE;
  147. else if (!strcmp(argv[0] + 9, "llvmir-opt"))
  148. option.output_format = AOT_LLVMIR_OPT_FILE;
  149. else {
  150. printf("Invalid format %s.\n", argv[0] + 9);
  151. return print_help();
  152. }
  153. }
  154. else if (!strncmp(argv[0], "-v=", 3)) {
  155. log_verbose_level = atoi(argv[0] + 3);
  156. if (log_verbose_level < 0 || log_verbose_level > 5)
  157. return print_help();
  158. }
  159. else if (!strcmp(argv[0], "--disable-bulk-memory")) {
  160. option.enable_bulk_memory = false;
  161. }
  162. else if (!strcmp(argv[0], "--enable-multi-thread")) {
  163. option.enable_bulk_memory = true;
  164. option.enable_thread_mgr = true;
  165. option.enable_ref_types = false;
  166. }
  167. else if (!strcmp(argv[0], "--enable-tail-call")) {
  168. option.enable_tail_call = true;
  169. }
  170. else if (!strcmp(argv[0], "--enable-simd")) {
  171. /* obsolete option, kept for compatibility */
  172. option.enable_simd = true;
  173. }
  174. else if (!strcmp(argv[0], "--disable-simd")) {
  175. option.enable_simd = false;
  176. }
  177. else if (!strcmp(argv[0], "--disable-ref-types")) {
  178. option.enable_ref_types = false;
  179. }
  180. else if (!strcmp(argv[0], "--disable-aux-stack-check")) {
  181. option.enable_aux_stack_check = false;
  182. }
  183. else if (!strcmp(argv[0], "--enable-dump-call-stack")) {
  184. option.enable_aux_stack_frame = true;
  185. }
  186. else if (!strcmp(argv[0], "--enable-perf-profiling")) {
  187. option.enable_aux_stack_frame = true;
  188. }
  189. else if (!strcmp(argv[0], "--enable-indirect-mode")) {
  190. option.is_indirect_mode = true;
  191. }
  192. else if (!strcmp(argv[0], "--disable-llvm-intrinsics")) {
  193. option.disable_llvm_intrinsics = true;
  194. }
  195. else if (!strcmp(argv[0], "--disable-llvm-lto")) {
  196. option.disable_llvm_lto = true;
  197. }
  198. else
  199. return print_help();
  200. }
  201. if (argc == 0 || !out_file_name)
  202. return print_help();
  203. if (sgx_mode) {
  204. option.size_level = 1;
  205. option.is_sgx_platform = true;
  206. }
  207. wasm_file_name = argv[0];
  208. memset(&init_args, 0, sizeof(RuntimeInitArgs));
  209. init_args.mem_alloc_type = Alloc_With_Allocator;
  210. init_args.mem_alloc_option.allocator.malloc_func = malloc;
  211. init_args.mem_alloc_option.allocator.realloc_func = realloc;
  212. init_args.mem_alloc_option.allocator.free_func = free;
  213. /* initialize runtime environment */
  214. if (!wasm_runtime_full_init(&init_args)) {
  215. printf("Init runtime environment failed.\n");
  216. return -1;
  217. }
  218. bh_log_set_verbose_level(log_verbose_level);
  219. bh_print_time("Begin to load wasm file");
  220. /* load WASM byte buffer from WASM bin file */
  221. if (!(wasm_file =
  222. (uint8 *)bh_read_file_to_buffer(wasm_file_name, &wasm_file_size)))
  223. goto fail1;
  224. /* load WASM module */
  225. if (!(wasm_module = wasm_runtime_load(wasm_file, wasm_file_size, error_buf,
  226. sizeof(error_buf)))) {
  227. printf("%s\n", error_buf);
  228. goto fail2;
  229. }
  230. if (!(comp_data = aot_create_comp_data(wasm_module))) {
  231. printf("%s\n", aot_get_last_error());
  232. goto fail3;
  233. }
  234. #if WASM_ENABLE_DEBUG_AOT != 0
  235. if (!create_dwarf_extractor(comp_data, wasm_file_name)) {
  236. printf("%s:create dwarf extractor failed\n", wasm_file_name);
  237. }
  238. #endif
  239. bh_print_time("Begin to create compile context");
  240. if (!(comp_ctx = aot_create_comp_context(comp_data, &option))) {
  241. printf("%s\n", aot_get_last_error());
  242. goto fail4;
  243. }
  244. bh_print_time("Begin to compile");
  245. if (!aot_compile_wasm(comp_ctx)) {
  246. printf("%s\n", aot_get_last_error());
  247. goto fail5;
  248. }
  249. switch (option.output_format) {
  250. case AOT_LLVMIR_UNOPT_FILE:
  251. case AOT_LLVMIR_OPT_FILE:
  252. if (!aot_emit_llvm_file(comp_ctx, out_file_name)) {
  253. printf("%s\n", aot_get_last_error());
  254. goto fail5;
  255. }
  256. break;
  257. case AOT_OBJECT_FILE:
  258. if (!aot_emit_object_file(comp_ctx, out_file_name)) {
  259. printf("%s\n", aot_get_last_error());
  260. goto fail5;
  261. }
  262. break;
  263. case AOT_FORMAT_FILE:
  264. if (!aot_emit_aot_file(comp_ctx, comp_data, out_file_name)) {
  265. printf("%s\n", aot_get_last_error());
  266. goto fail5;
  267. }
  268. break;
  269. default:
  270. break;
  271. }
  272. bh_print_time("Compile end");
  273. printf("Compile success, file %s was generated.\n", out_file_name);
  274. exit_status = EXIT_SUCCESS;
  275. fail5:
  276. /* Destroy compiler context */
  277. aot_destroy_comp_context(comp_ctx);
  278. fail4:
  279. /* Destroy compile data */
  280. aot_destroy_comp_data(comp_data);
  281. fail3:
  282. /* Unload WASM module */
  283. wasm_runtime_unload(wasm_module);
  284. fail2:
  285. /* free the file buffer */
  286. wasm_runtime_free(wasm_file);
  287. fail1:
  288. /* Destroy runtime environment */
  289. wasm_runtime_destroy();
  290. bh_print_time("wamrc return");
  291. return exit_status;
  292. }