pyexec.c 18 KB

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