main.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /*
  2. * This file is part of the MicroPython project, http://micropython.org/
  3. *
  4. * The MIT License (MIT)
  5. *
  6. * Copyright (c) 2013-2016 Damien P. George
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. */
  26. #include <stdio.h>
  27. #include <string.h>
  28. #include <stdlib.h>
  29. #include <unistd.h>
  30. #include "py/compile.h"
  31. #include "py/persistentcode.h"
  32. #include "py/runtime.h"
  33. #include "py/gc.h"
  34. #include "py/stackctrl.h"
  35. #ifdef _WIN32
  36. #include "ports/windows/fmode.h"
  37. #endif
  38. // Command line options, with their defaults
  39. STATIC uint emit_opt = MP_EMIT_OPT_NONE;
  40. mp_uint_t mp_verbose_flag = 0;
  41. // Heap size of GC heap (if enabled)
  42. // Make it larger on a 64 bit machine, because pointers are larger.
  43. long heap_size = 1024*1024 * (sizeof(mp_uint_t) / 4);
  44. STATIC void stderr_print_strn(void *env, const char *str, mp_uint_t len) {
  45. (void)env;
  46. ssize_t dummy = write(STDERR_FILENO, str, len);
  47. (void)dummy;
  48. }
  49. STATIC const mp_print_t mp_stderr_print = {NULL, stderr_print_strn};
  50. STATIC int compile_and_save(const char *file, const char *output_file, const char *source_file) {
  51. nlr_buf_t nlr;
  52. if (nlr_push(&nlr) == 0) {
  53. mp_lexer_t *lex = mp_lexer_new_from_file(file);
  54. qstr source_name;
  55. if (source_file == NULL) {
  56. source_name = lex->source_name;
  57. } else {
  58. source_name = qstr_from_str(source_file);
  59. }
  60. #if MICROPY_PY___FILE__
  61. if (input_kind == MP_PARSE_FILE_INPUT) {
  62. mp_store_global(MP_QSTR___file__, MP_OBJ_NEW_QSTR(source_name));
  63. }
  64. #endif
  65. mp_parse_tree_t parse_tree = mp_parse(lex, MP_PARSE_FILE_INPUT);
  66. mp_raw_code_t *rc = mp_compile_to_raw_code(&parse_tree, source_name, emit_opt, false);
  67. vstr_t vstr;
  68. vstr_init(&vstr, 16);
  69. if (output_file == NULL) {
  70. vstr_add_str(&vstr, file);
  71. vstr_cut_tail_bytes(&vstr, 2);
  72. vstr_add_str(&vstr, "mpy");
  73. } else {
  74. vstr_add_str(&vstr, output_file);
  75. }
  76. mp_raw_code_save_file(rc, vstr_null_terminated_str(&vstr));
  77. vstr_clear(&vstr);
  78. nlr_pop();
  79. return 0;
  80. } else {
  81. // uncaught exception
  82. mp_obj_print_exception(&mp_stderr_print, (mp_obj_t)nlr.ret_val);
  83. return 1;
  84. }
  85. }
  86. STATIC int usage(char **argv) {
  87. printf(
  88. "usage: %s [<opts>] [-X <implopt>] <input filename>\n"
  89. "Options:\n"
  90. "-o : output file for compiled bytecode (defaults to input with .mpy extension)\n"
  91. "-s : source filename to embed in the compiled bytecode (defaults to input file)\n"
  92. "-v : verbose (trace various operations); can be multiple\n"
  93. "-O[N] : apply bytecode optimizations of level N\n"
  94. "\n"
  95. "Target specific options:\n"
  96. "-msmall-int-bits=number : set the maximum bits used to encode a small-int\n"
  97. "-mno-unicode : don't support unicode in compiled strings\n"
  98. "-mcache-lookup-bc : cache map lookups in the bytecode\n"
  99. "\n"
  100. "Implementation specific options:\n", argv[0]
  101. );
  102. int impl_opts_cnt = 0;
  103. printf(
  104. " emit={bytecode,native,viper} -- set the default code emitter\n"
  105. );
  106. impl_opts_cnt++;
  107. printf(
  108. " heapsize=<n> -- set the heap size for the GC (default %ld)\n"
  109. , heap_size);
  110. impl_opts_cnt++;
  111. if (impl_opts_cnt == 0) {
  112. printf(" (none)\n");
  113. }
  114. return 1;
  115. }
  116. // Process options which set interpreter init options
  117. STATIC void pre_process_options(int argc, char **argv) {
  118. for (int a = 1; a < argc; a++) {
  119. if (argv[a][0] == '-') {
  120. if (strcmp(argv[a], "-X") == 0) {
  121. if (a + 1 >= argc) {
  122. exit(usage(argv));
  123. }
  124. if (strcmp(argv[a + 1], "emit=bytecode") == 0) {
  125. emit_opt = MP_EMIT_OPT_BYTECODE;
  126. } else if (strcmp(argv[a + 1], "emit=native") == 0) {
  127. emit_opt = MP_EMIT_OPT_NATIVE_PYTHON;
  128. } else if (strcmp(argv[a + 1], "emit=viper") == 0) {
  129. emit_opt = MP_EMIT_OPT_VIPER;
  130. } else if (strncmp(argv[a + 1], "heapsize=", sizeof("heapsize=") - 1) == 0) {
  131. char *end;
  132. heap_size = strtol(argv[a + 1] + sizeof("heapsize=") - 1, &end, 0);
  133. // Don't bring unneeded libc dependencies like tolower()
  134. // If there's 'w' immediately after number, adjust it for
  135. // target word size. Note that it should be *before* size
  136. // suffix like K or M, to avoid confusion with kilowords,
  137. // etc. the size is still in bytes, just can be adjusted
  138. // for word size (taking 32bit as baseline).
  139. bool word_adjust = false;
  140. if ((*end | 0x20) == 'w') {
  141. word_adjust = true;
  142. end++;
  143. }
  144. if ((*end | 0x20) == 'k') {
  145. heap_size *= 1024;
  146. } else if ((*end | 0x20) == 'm') {
  147. heap_size *= 1024 * 1024;
  148. }
  149. if (word_adjust) {
  150. heap_size = heap_size * BYTES_PER_WORD / 4;
  151. }
  152. } else {
  153. exit(usage(argv));
  154. }
  155. a++;
  156. }
  157. }
  158. }
  159. }
  160. MP_NOINLINE int main_(int argc, char **argv) {
  161. mp_stack_set_limit(40000 * (BYTES_PER_WORD / 4));
  162. pre_process_options(argc, argv);
  163. char *heap = malloc(heap_size);
  164. gc_init(heap, heap + heap_size);
  165. mp_init();
  166. #ifdef _WIN32
  167. set_fmode_binary();
  168. #endif
  169. mp_obj_list_init(mp_sys_path, 0);
  170. mp_obj_list_init(mp_sys_argv, 0);
  171. // set default compiler configuration
  172. mp_dynamic_compiler.small_int_bits = 31;
  173. mp_dynamic_compiler.opt_cache_map_lookup_in_bytecode = 0;
  174. mp_dynamic_compiler.py_builtins_str_unicode = 1;
  175. const char *input_file = NULL;
  176. const char *output_file = NULL;
  177. const char *source_file = NULL;
  178. // parse main options
  179. for (int a = 1; a < argc; a++) {
  180. if (argv[a][0] == '-') {
  181. if (strcmp(argv[a], "-X") == 0) {
  182. a += 1;
  183. } else if (strcmp(argv[a], "-v") == 0) {
  184. mp_verbose_flag++;
  185. } else if (strncmp(argv[a], "-O", 2) == 0) {
  186. if (unichar_isdigit(argv[a][2])) {
  187. MP_STATE_VM(mp_optimise_value) = argv[a][2] & 0xf;
  188. } else {
  189. MP_STATE_VM(mp_optimise_value) = 0;
  190. for (char *p = argv[a] + 1; *p && *p == 'O'; p++, MP_STATE_VM(mp_optimise_value)++);
  191. }
  192. } else if (strcmp(argv[a], "-o") == 0) {
  193. if (a + 1 >= argc) {
  194. exit(usage(argv));
  195. }
  196. a += 1;
  197. output_file = argv[a];
  198. } else if (strcmp(argv[a], "-s") == 0) {
  199. if (a + 1 >= argc) {
  200. exit(usage(argv));
  201. }
  202. a += 1;
  203. source_file = argv[a];
  204. } else if (strncmp(argv[a], "-msmall-int-bits=", sizeof("-msmall-int-bits=") - 1) == 0) {
  205. char *end;
  206. mp_dynamic_compiler.small_int_bits =
  207. strtol(argv[a] + sizeof("-msmall-int-bits=") - 1, &end, 0);
  208. if (*end) {
  209. return usage(argv);
  210. }
  211. // TODO check that small_int_bits is within range of host's capabilities
  212. } else if (strcmp(argv[a], "-mno-cache-lookup-bc") == 0) {
  213. mp_dynamic_compiler.opt_cache_map_lookup_in_bytecode = 0;
  214. } else if (strcmp(argv[a], "-mcache-lookup-bc") == 0) {
  215. mp_dynamic_compiler.opt_cache_map_lookup_in_bytecode = 1;
  216. } else if (strcmp(argv[a], "-mno-unicode") == 0) {
  217. mp_dynamic_compiler.py_builtins_str_unicode = 0;
  218. } else if (strcmp(argv[a], "-municode") == 0) {
  219. mp_dynamic_compiler.py_builtins_str_unicode = 1;
  220. } else {
  221. return usage(argv);
  222. }
  223. } else {
  224. if (input_file != NULL) {
  225. mp_printf(&mp_stderr_print, "multiple input files\n");
  226. exit(1);
  227. }
  228. input_file = argv[a];
  229. }
  230. }
  231. if (input_file == NULL) {
  232. mp_printf(&mp_stderr_print, "no input file\n");
  233. exit(1);
  234. }
  235. int ret = compile_and_save(input_file, output_file, source_file);
  236. #if MICROPY_PY_MICROPYTHON_MEM_INFO
  237. if (mp_verbose_flag) {
  238. mp_micropython_mem_info(0, NULL);
  239. }
  240. #endif
  241. mp_deinit();
  242. return ret & 0xff;
  243. }
  244. int main(int argc, char **argv) {
  245. mp_stack_ctrl_init();
  246. return main_(argc, argv);
  247. }
  248. uint mp_import_stat(const char *path) {
  249. (void)path;
  250. return MP_IMPORT_STAT_NO_EXIST;
  251. }
  252. void nlr_jump_fail(void *val) {
  253. printf("FATAL: uncaught NLR %p\n", val);
  254. exit(1);
  255. }