pyexec.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  1. /*
  2. * This file is part of the MicroPython project, http://micropython.org/
  3. *
  4. * The MIT License (MIT)
  5. *
  6. * Copyright (c) 2013, 2014 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 <stdlib.h>
  27. #include <stdio.h>
  28. #include <stdint.h>
  29. #include <string.h>
  30. #include "py/compile.h"
  31. #include "py/runtime.h"
  32. #include "py/repl.h"
  33. #include "py/gc.h"
  34. #include "py/frozenmod.h"
  35. #include "py/mphal.h"
  36. #if MICROPY_HW_ENABLE_USB
  37. #include "irq.h"
  38. #include "usb.h"
  39. #endif
  40. #include "lib/mp-readline/readline.h"
  41. #include "lib/utils/pyexec.h"
  42. #include "genhdr/mpversion.h"
  43. pyexec_mode_kind_t pyexec_mode_kind = PYEXEC_MODE_FRIENDLY_REPL;
  44. int pyexec_system_exit = 0;
  45. STATIC bool repl_display_debugging_info = 0;
  46. #define EXEC_FLAG_PRINT_EOF (1)
  47. #define EXEC_FLAG_ALLOW_DEBUGGING (2)
  48. #define EXEC_FLAG_IS_REPL (4)
  49. #define EXEC_FLAG_SOURCE_IS_RAW_CODE (8)
  50. #define EXEC_FLAG_SOURCE_IS_VSTR (16)
  51. #define EXEC_FLAG_SOURCE_IS_FILENAME (32)
  52. // parses, compiles and executes the code in the lexer
  53. // frees the lexer before returning
  54. // EXEC_FLAG_PRINT_EOF prints 2 EOF chars: 1 after normal output, 1 after exception output
  55. // EXEC_FLAG_ALLOW_DEBUGGING allows debugging info to be printed after executing the code
  56. // EXEC_FLAG_IS_REPL is used for REPL inputs (flag passed on to mp_compile)
  57. STATIC int parse_compile_execute(const void *source, mp_parse_input_kind_t input_kind, int exec_flags) {
  58. int ret = 0;
  59. uint32_t start = 0;
  60. // by default a SystemExit exception returns 0
  61. pyexec_system_exit = 0;
  62. nlr_buf_t nlr;
  63. if (nlr_push(&nlr) == 0) {
  64. mp_obj_t module_fun;
  65. #if MICROPY_MODULE_FROZEN_MPY
  66. if (exec_flags & EXEC_FLAG_SOURCE_IS_RAW_CODE) {
  67. // source is a raw_code object, create the function
  68. module_fun = mp_make_function_from_raw_code(source, MP_OBJ_NULL, MP_OBJ_NULL);
  69. } else
  70. #endif
  71. {
  72. #if MICROPY_ENABLE_COMPILER
  73. mp_lexer_t *lex;
  74. if (exec_flags & EXEC_FLAG_SOURCE_IS_VSTR) {
  75. const vstr_t *vstr = source;
  76. lex = mp_lexer_new_from_str_len(MP_QSTR__lt_stdin_gt_, vstr->buf, vstr->len, 0);
  77. } else if (exec_flags & EXEC_FLAG_SOURCE_IS_FILENAME) {
  78. lex = mp_lexer_new_from_file(source);
  79. } else {
  80. lex = (mp_lexer_t*)source;
  81. }
  82. // source is a lexer, parse and compile the script
  83. qstr source_name = lex->source_name;
  84. mp_parse_tree_t parse_tree = mp_parse(lex, input_kind);
  85. module_fun = mp_compile(&parse_tree, source_name, MP_EMIT_OPT_NONE, exec_flags & EXEC_FLAG_IS_REPL);
  86. #else
  87. mp_raise_msg(&mp_type_RuntimeError, "script compilation not supported");
  88. #endif
  89. }
  90. // execute code
  91. mp_hal_set_interrupt_char(CHAR_CTRL_C); // allow ctrl-C to interrupt us
  92. start = mp_hal_ticks_ms();
  93. mp_call_function_0(module_fun);
  94. mp_hal_set_interrupt_char(-1); // disable interrupt
  95. nlr_pop();
  96. ret = 1;
  97. if (exec_flags & EXEC_FLAG_PRINT_EOF) {
  98. mp_hal_stdout_tx_strn("\x04", 1);
  99. }
  100. } else {
  101. // uncaught exception
  102. // FIXME it could be that an interrupt happens just before we disable it here
  103. mp_hal_set_interrupt_char(-1); // disable interrupt
  104. // print EOF after normal output
  105. if (exec_flags & EXEC_FLAG_PRINT_EOF) {
  106. mp_hal_stdout_tx_strn("\x04", 1);
  107. }
  108. // check for SystemExit
  109. if (mp_obj_is_subclass_fast(MP_OBJ_FROM_PTR(((mp_obj_base_t*)nlr.ret_val)->type), MP_OBJ_FROM_PTR(&mp_type_SystemExit))) {
  110. // at the moment, the value of SystemExit is unused
  111. ret = pyexec_system_exit;
  112. } else {
  113. mp_obj_print_exception(&mp_plat_print, MP_OBJ_FROM_PTR(nlr.ret_val));
  114. ret = 0;
  115. }
  116. }
  117. // display debugging info if wanted
  118. if ((exec_flags & EXEC_FLAG_ALLOW_DEBUGGING) && repl_display_debugging_info) {
  119. mp_uint_t ticks = mp_hal_ticks_ms() - start; // TODO implement a function that does this properly
  120. printf("took " UINT_FMT " ms\n", ticks);
  121. // qstr info
  122. {
  123. size_t n_pool, n_qstr, n_str_data_bytes, n_total_bytes;
  124. qstr_pool_info(&n_pool, &n_qstr, &n_str_data_bytes, &n_total_bytes);
  125. printf("qstr:\n n_pool=%u\n n_qstr=%u\n "
  126. "n_str_data_bytes=%u\n n_total_bytes=%u\n",
  127. (unsigned)n_pool, (unsigned)n_qstr, (unsigned)n_str_data_bytes, (unsigned)n_total_bytes);
  128. }
  129. #if MICROPY_ENABLE_GC
  130. // run collection and print GC info
  131. gc_collect();
  132. gc_dump_info();
  133. #endif
  134. }
  135. if (exec_flags & EXEC_FLAG_PRINT_EOF) {
  136. mp_hal_stdout_tx_strn("\x04", 1);
  137. }
  138. return ret;
  139. }
  140. #if MICROPY_ENABLE_COMPILER
  141. #if MICROPY_REPL_EVENT_DRIVEN
  142. typedef struct _repl_t {
  143. // This structure originally also held current REPL line,
  144. // but it was moved to MP_STATE_VM(repl_line) as containing
  145. // root pointer. Still keep structure in case more state
  146. // will be added later.
  147. //vstr_t line;
  148. bool cont_line;
  149. } repl_t;
  150. repl_t repl;
  151. STATIC int pyexec_raw_repl_process_char(int c);
  152. STATIC int pyexec_friendly_repl_process_char(int c);
  153. void pyexec_event_repl_init(void) {
  154. MP_STATE_VM(repl_line) = vstr_new(32);
  155. repl.cont_line = false;
  156. // no prompt before printing friendly REPL banner or entering raw REPL
  157. readline_init(MP_STATE_VM(repl_line), "");
  158. if (pyexec_mode_kind == PYEXEC_MODE_RAW_REPL) {
  159. pyexec_raw_repl_process_char(CHAR_CTRL_A);
  160. } else {
  161. pyexec_friendly_repl_process_char(CHAR_CTRL_B);
  162. }
  163. }
  164. STATIC int pyexec_raw_repl_process_char(int c) {
  165. if (c == CHAR_CTRL_A) {
  166. // reset raw REPL
  167. mp_hal_stdout_tx_str("raw REPL; CTRL-B to exit\r\n");
  168. goto reset;
  169. } else if (c == CHAR_CTRL_B) {
  170. // change to friendly REPL
  171. pyexec_mode_kind = PYEXEC_MODE_FRIENDLY_REPL;
  172. vstr_reset(MP_STATE_VM(repl_line));
  173. repl.cont_line = false;
  174. pyexec_friendly_repl_process_char(CHAR_CTRL_B);
  175. return 0;
  176. } else if (c == CHAR_CTRL_C) {
  177. // clear line
  178. vstr_reset(MP_STATE_VM(repl_line));
  179. return 0;
  180. } else if (c == CHAR_CTRL_D) {
  181. // input finished
  182. } else {
  183. // let through any other raw 8-bit value
  184. vstr_add_byte(MP_STATE_VM(repl_line), c);
  185. return 0;
  186. }
  187. // indicate reception of command
  188. mp_hal_stdout_tx_str("OK");
  189. if (MP_STATE_VM(repl_line)->len == 0) {
  190. // exit for a soft reset
  191. mp_hal_stdout_tx_str("\r\n");
  192. vstr_clear(MP_STATE_VM(repl_line));
  193. return PYEXEC_FORCED_EXIT;
  194. }
  195. int ret = parse_compile_execute(MP_STATE_VM(repl_line), MP_PARSE_FILE_INPUT, EXEC_FLAG_PRINT_EOF | EXEC_FLAG_SOURCE_IS_VSTR);
  196. if (ret & PYEXEC_FORCED_EXIT) {
  197. return ret;
  198. }
  199. reset:
  200. vstr_reset(MP_STATE_VM(repl_line));
  201. mp_hal_stdout_tx_str(">");
  202. return 0;
  203. }
  204. STATIC int pyexec_friendly_repl_process_char(int c) {
  205. int ret = readline_process_char(c);
  206. if (!repl.cont_line) {
  207. if (ret == CHAR_CTRL_A) {
  208. // change to raw REPL
  209. pyexec_mode_kind = PYEXEC_MODE_RAW_REPL;
  210. mp_hal_stdout_tx_str("\r\n");
  211. pyexec_raw_repl_process_char(CHAR_CTRL_A);
  212. return 0;
  213. } else if (ret == CHAR_CTRL_B) {
  214. // reset friendly REPL
  215. mp_hal_stdout_tx_str("\r\n");
  216. mp_hal_stdout_tx_str("MicroPython " MICROPY_GIT_TAG " on " MICROPY_BUILD_DATE "; " MICROPY_HW_BOARD_NAME " with " MICROPY_HW_MCU_NAME "\r\n");
  217. #if MICROPY_PY_BUILTINS_HELP
  218. mp_hal_stdout_tx_str("Type \"help()\" for more information.\r\n");
  219. #endif
  220. goto input_restart;
  221. } else if (ret == CHAR_CTRL_C) {
  222. // break
  223. mp_hal_stdout_tx_str("\r\n");
  224. goto input_restart;
  225. } else if (ret == CHAR_CTRL_D) {
  226. // exit for a soft reset
  227. mp_hal_stdout_tx_str("\r\n");
  228. vstr_clear(MP_STATE_VM(repl_line));
  229. return PYEXEC_FORCED_EXIT;
  230. }
  231. if (ret < 0) {
  232. return 0;
  233. }
  234. if (!mp_repl_continue_with_input(vstr_null_terminated_str(MP_STATE_VM(repl_line)))) {
  235. goto exec;
  236. }
  237. vstr_add_byte(MP_STATE_VM(repl_line), '\n');
  238. repl.cont_line = true;
  239. readline_note_newline("... ");
  240. return 0;
  241. } else {
  242. if (ret == CHAR_CTRL_C) {
  243. // cancel everything
  244. mp_hal_stdout_tx_str("\r\n");
  245. repl.cont_line = false;
  246. goto input_restart;
  247. } else if (ret == CHAR_CTRL_D) {
  248. // stop entering compound statement
  249. goto exec;
  250. }
  251. if (ret < 0) {
  252. return 0;
  253. }
  254. if (mp_repl_continue_with_input(vstr_null_terminated_str(MP_STATE_VM(repl_line)))) {
  255. vstr_add_byte(MP_STATE_VM(repl_line), '\n');
  256. readline_note_newline("... ");
  257. return 0;
  258. }
  259. exec: ;
  260. int ret = parse_compile_execute(MP_STATE_VM(repl_line), MP_PARSE_SINGLE_INPUT, EXEC_FLAG_ALLOW_DEBUGGING | EXEC_FLAG_IS_REPL | EXEC_FLAG_SOURCE_IS_VSTR);
  261. if (ret & PYEXEC_FORCED_EXIT) {
  262. return ret;
  263. }
  264. input_restart:
  265. vstr_reset(MP_STATE_VM(repl_line));
  266. repl.cont_line = false;
  267. readline_init(MP_STATE_VM(repl_line), ">>> ");
  268. return 0;
  269. }
  270. }
  271. uint8_t pyexec_repl_active;
  272. int pyexec_event_repl_process_char(int c) {
  273. pyexec_repl_active = 1;
  274. int res;
  275. if (pyexec_mode_kind == PYEXEC_MODE_RAW_REPL) {
  276. res = pyexec_raw_repl_process_char(c);
  277. } else {
  278. res = pyexec_friendly_repl_process_char(c);
  279. }
  280. pyexec_repl_active = 0;
  281. return res;
  282. }
  283. #else // MICROPY_REPL_EVENT_DRIVEN
  284. int pyexec_raw_repl(void) {
  285. vstr_t line;
  286. vstr_init(&line, 32);
  287. raw_repl_reset:
  288. mp_hal_stdout_tx_str("raw REPL; CTRL-B to exit\r\n");
  289. for (;;) {
  290. vstr_reset(&line);
  291. mp_hal_stdout_tx_str(">");
  292. for (;;) {
  293. int c = mp_hal_stdin_rx_chr();
  294. if (c == CHAR_CTRL_A) {
  295. // reset raw REPL
  296. goto raw_repl_reset;
  297. } else if (c == CHAR_CTRL_B) {
  298. // change to friendly REPL
  299. mp_hal_stdout_tx_str("\r\n");
  300. vstr_clear(&line);
  301. pyexec_mode_kind = PYEXEC_MODE_FRIENDLY_REPL;
  302. return 0;
  303. } else if (c == CHAR_CTRL_C) {
  304. // clear line
  305. vstr_reset(&line);
  306. } else if (c == CHAR_CTRL_D) {
  307. // input finished
  308. break;
  309. } else {
  310. // let through any other raw 8-bit value
  311. vstr_add_byte(&line, c);
  312. }
  313. }
  314. // indicate reception of command
  315. mp_hal_stdout_tx_str("OK");
  316. if (line.len == 0) {
  317. // exit for a soft reset
  318. mp_hal_stdout_tx_str("\r\n");
  319. vstr_clear(&line);
  320. return PYEXEC_FORCED_EXIT;
  321. }
  322. int ret = parse_compile_execute(&line, MP_PARSE_FILE_INPUT, EXEC_FLAG_PRINT_EOF | EXEC_FLAG_SOURCE_IS_VSTR);
  323. if (ret & PYEXEC_FORCED_EXIT) {
  324. return ret;
  325. }
  326. }
  327. }
  328. int pyexec_friendly_repl(void) {
  329. vstr_t line;
  330. vstr_init(&line, 32);
  331. #if defined(USE_HOST_MODE) && MICROPY_HW_HAS_LCD
  332. // in host mode, we enable the LCD for the repl
  333. mp_obj_t lcd_o = mp_call_function_0(mp_load_name(qstr_from_str("LCD")));
  334. mp_call_function_1(mp_load_attr(lcd_o, qstr_from_str("light")), mp_const_true);
  335. #endif
  336. friendly_repl_reset:
  337. mp_hal_stdout_tx_str("MicroPython " MICROPY_GIT_TAG " on " MICROPY_BUILD_DATE "; " MICROPY_HW_BOARD_NAME " with " MICROPY_HW_MCU_NAME "\r\n");
  338. #if MICROPY_PY_BUILTINS_HELP
  339. mp_hal_stdout_tx_str("Type \"help()\" for more information.\r\n");
  340. #endif
  341. // to test ctrl-C
  342. /*
  343. {
  344. uint32_t x[4] = {0x424242, 0xdeaddead, 0x242424, 0xdeadbeef};
  345. for (;;) {
  346. nlr_buf_t nlr;
  347. printf("pyexec_repl: %p\n", x);
  348. mp_hal_set_interrupt_char(CHAR_CTRL_C);
  349. if (nlr_push(&nlr) == 0) {
  350. for (;;) {
  351. }
  352. } else {
  353. printf("break\n");
  354. }
  355. }
  356. }
  357. */
  358. for (;;) {
  359. input_restart:
  360. #if MICROPY_HW_ENABLE_USB
  361. if (usb_vcp_is_enabled()) {
  362. // If the user gets to here and interrupts are disabled then
  363. // they'll never see the prompt, traceback etc. The USB REPL needs
  364. // interrupts to be enabled or no transfers occur. So we try to
  365. // do the user a favor and reenable interrupts.
  366. if (query_irq() == IRQ_STATE_DISABLED) {
  367. enable_irq(IRQ_STATE_ENABLED);
  368. mp_hal_stdout_tx_str("PYB: enabling IRQs\r\n");
  369. }
  370. }
  371. #endif
  372. vstr_reset(&line);
  373. int ret = readline(&line, ">>> ");
  374. mp_parse_input_kind_t parse_input_kind = MP_PARSE_SINGLE_INPUT;
  375. if (ret == CHAR_CTRL_A) {
  376. // change to raw REPL
  377. mp_hal_stdout_tx_str("\r\n");
  378. vstr_clear(&line);
  379. pyexec_mode_kind = PYEXEC_MODE_RAW_REPL;
  380. return 0;
  381. } else if (ret == CHAR_CTRL_B) {
  382. // reset friendly REPL
  383. mp_hal_stdout_tx_str("\r\n");
  384. goto friendly_repl_reset;
  385. } else if (ret == CHAR_CTRL_C) {
  386. // break
  387. mp_hal_stdout_tx_str("\r\n");
  388. continue;
  389. } else if (ret == CHAR_CTRL_D) {
  390. // exit for a soft reset
  391. mp_hal_stdout_tx_str("\r\n");
  392. vstr_clear(&line);
  393. return PYEXEC_FORCED_EXIT;
  394. } else if (ret == CHAR_CTRL_E) {
  395. // paste mode
  396. mp_hal_stdout_tx_str("\r\npaste mode; Ctrl-C to cancel, Ctrl-D to finish\r\n=== ");
  397. vstr_reset(&line);
  398. for (;;) {
  399. char c = mp_hal_stdin_rx_chr();
  400. if (c == CHAR_CTRL_C) {
  401. // cancel everything
  402. mp_hal_stdout_tx_str("\r\n");
  403. goto input_restart;
  404. } else if (c == CHAR_CTRL_D) {
  405. // end of input
  406. mp_hal_stdout_tx_str("\r\n");
  407. break;
  408. } else {
  409. // add char to buffer and echo
  410. vstr_add_byte(&line, c);
  411. if (c == '\r') {
  412. mp_hal_stdout_tx_str("\r\n=== ");
  413. } else {
  414. mp_hal_stdout_tx_strn(&c, 1);
  415. }
  416. }
  417. }
  418. parse_input_kind = MP_PARSE_FILE_INPUT;
  419. } else if (vstr_len(&line) == 0) {
  420. continue;
  421. } else {
  422. // got a line with non-zero length, see if it needs continuing
  423. while (mp_repl_continue_with_input(vstr_null_terminated_str(&line))) {
  424. vstr_add_byte(&line, '\n');
  425. ret = readline(&line, "... ");
  426. if (ret == CHAR_CTRL_C) {
  427. // cancel everything
  428. mp_hal_stdout_tx_str("\r\n");
  429. goto input_restart;
  430. } else if (ret == CHAR_CTRL_D) {
  431. // stop entering compound statement
  432. break;
  433. }
  434. }
  435. }
  436. ret = parse_compile_execute(&line, parse_input_kind, EXEC_FLAG_ALLOW_DEBUGGING | EXEC_FLAG_IS_REPL | EXEC_FLAG_SOURCE_IS_VSTR);
  437. if (ret & PYEXEC_FORCED_EXIT) {
  438. return ret;
  439. }
  440. }
  441. }
  442. #endif // MICROPY_REPL_EVENT_DRIVEN
  443. #endif // MICROPY_ENABLE_COMPILER
  444. int pyexec_file(const char *filename) {
  445. return parse_compile_execute(filename, MP_PARSE_FILE_INPUT, EXEC_FLAG_SOURCE_IS_FILENAME);
  446. }
  447. #if MICROPY_MODULE_FROZEN
  448. int pyexec_frozen_module(const char *name) {
  449. void *frozen_data;
  450. int frozen_type = mp_find_frozen_module(name, strlen(name), &frozen_data);
  451. switch (frozen_type) {
  452. #if MICROPY_MODULE_FROZEN_STR
  453. case MP_FROZEN_STR:
  454. return parse_compile_execute(frozen_data, MP_PARSE_FILE_INPUT, 0);
  455. #endif
  456. #if MICROPY_MODULE_FROZEN_MPY
  457. case MP_FROZEN_MPY:
  458. return parse_compile_execute(frozen_data, MP_PARSE_FILE_INPUT, EXEC_FLAG_SOURCE_IS_RAW_CODE);
  459. #endif
  460. default:
  461. printf("could not find module '%s'\n", name);
  462. return false;
  463. }
  464. }
  465. #endif
  466. mp_obj_t pyb_set_repl_info(mp_obj_t o_value) {
  467. repl_display_debugging_info = mp_obj_get_int(o_value);
  468. return mp_const_none;
  469. }
  470. MP_DEFINE_CONST_FUN_OBJ_1(pyb_set_repl_info_obj, pyb_set_repl_info);