main.c 17 KB

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