main.c 17 KB

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