PageRenderTime 44ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

/py/compile.c

https://github.com/jtbattle/micropython
C | 3665 lines | 2885 code | 347 blank | 433 comment | 1051 complexity | 2ff9bcdf9404c0e273c8d9fb58f0a97c MD5 | raw file
  1. /*
  2. * This file is part of the Micro Python 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 <stdbool.h>
  27. #include <stdint.h>
  28. #include <stdio.h>
  29. #include <string.h>
  30. #include <assert.h>
  31. #include <math.h>
  32. #include "misc.h"
  33. #include "mpconfig.h"
  34. #include "qstr.h"
  35. #include "lexer.h"
  36. #include "parse.h"
  37. #include "runtime0.h"
  38. #include "obj.h"
  39. #include "emitglue.h"
  40. #include "scope.h"
  41. #include "emit.h"
  42. #include "compile.h"
  43. #include "runtime.h"
  44. #include "builtin.h"
  45. #include "smallint.h"
  46. // TODO need to mangle __attr names
  47. #define MICROPY_EMIT_NATIVE (MICROPY_EMIT_X64 || MICROPY_EMIT_THUMB)
  48. typedef enum {
  49. PN_none = 0,
  50. #define DEF_RULE(rule, comp, kind, ...) PN_##rule,
  51. #include "grammar.h"
  52. #undef DEF_RULE
  53. PN_maximum_number_of,
  54. PN_string, // special node for non-interned string
  55. } pn_kind_t;
  56. #define EMIT(fun) (comp->emit_method_table->fun(comp->emit))
  57. #define EMIT_ARG(fun, ...) (comp->emit_method_table->fun(comp->emit, __VA_ARGS__))
  58. #define EMIT_INLINE_ASM(fun) (comp->emit_inline_asm_method_table->fun(comp->emit_inline_asm))
  59. #define EMIT_INLINE_ASM_ARG(fun, ...) (comp->emit_inline_asm_method_table->fun(comp->emit_inline_asm, __VA_ARGS__))
  60. typedef struct _compiler_t {
  61. qstr source_file;
  62. uint8_t is_repl;
  63. uint8_t pass; // holds enum type pass_kind_t
  64. uint8_t had_error; // try to keep compiler clean from nlr
  65. uint8_t func_arg_is_super; // used to compile special case of super() function call
  66. uint next_label;
  67. uint16_t break_label; // highest bit set indicates we are breaking out of a for loop
  68. uint16_t continue_label;
  69. int break_continue_except_level;
  70. uint16_t cur_except_level; // increased for SETUP_EXCEPT, SETUP_FINALLY; decreased for POP_BLOCK, POP_EXCEPT
  71. uint8_t have_star;
  72. uint16_t num_dict_params;
  73. uint16_t num_default_params;
  74. scope_t *scope_head;
  75. scope_t *scope_cur;
  76. emit_t *emit; // current emitter
  77. const emit_method_table_t *emit_method_table; // current emit method table
  78. emit_inline_asm_t *emit_inline_asm; // current emitter for inline asm
  79. const emit_inline_asm_method_table_t *emit_inline_asm_method_table; // current emit method table for inline asm
  80. } compiler_t;
  81. STATIC void compile_syntax_error(compiler_t *comp, mp_parse_node_t pn, const char *msg) {
  82. // TODO store the error message to a variable in compiler_t instead of printing it
  83. if (MP_PARSE_NODE_IS_STRUCT(pn)) {
  84. printf(" File \"%s\", line " UINT_FMT "\n", qstr_str(comp->source_file), (machine_uint_t)((mp_parse_node_struct_t*)pn)->source_line);
  85. } else {
  86. printf(" File \"%s\"\n", qstr_str(comp->source_file));
  87. }
  88. printf("SyntaxError: %s\n", msg);
  89. comp->had_error = true;
  90. }
  91. STATIC const mp_map_elem_t mp_constants_table[] = {
  92. // Extra constants as defined by a port
  93. MICROPY_PORT_CONSTANTS
  94. };
  95. STATIC const mp_map_t mp_constants_map = {
  96. .all_keys_are_qstrs = 1,
  97. .table_is_fixed_array = 1,
  98. .used = ARRAY_SIZE(mp_constants_table),
  99. .alloc = ARRAY_SIZE(mp_constants_table),
  100. .table = (mp_map_elem_t*)mp_constants_table,
  101. };
  102. // this function is essentially a simple preprocessor
  103. STATIC mp_parse_node_t fold_constants(compiler_t *comp, mp_parse_node_t pn, mp_map_t *consts) {
  104. if (0) {
  105. // dummy
  106. #if MICROPY_COMP_CONST
  107. } else if (MP_PARSE_NODE_IS_ID(pn)) {
  108. // lookup identifier in table of dynamic constants
  109. qstr qst = MP_PARSE_NODE_LEAF_ARG(pn);
  110. mp_map_elem_t *elem = mp_map_lookup(consts, MP_OBJ_NEW_QSTR(qst), MP_MAP_LOOKUP);
  111. if (elem != NULL) {
  112. pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, MP_OBJ_SMALL_INT_VALUE(elem->value));
  113. }
  114. #endif
  115. } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
  116. mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
  117. // fold some parse nodes before folding their arguments
  118. switch (MP_PARSE_NODE_STRUCT_KIND(pns)) {
  119. #if MICROPY_COMP_CONST
  120. case PN_expr_stmt:
  121. if (!MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
  122. mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
  123. if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_expr_stmt_assign) {
  124. if (MP_PARSE_NODE_IS_ID(pns->nodes[0])
  125. && MP_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_power)
  126. && MP_PARSE_NODE_IS_ID(((mp_parse_node_struct_t*)pns1->nodes[0])->nodes[0])
  127. && MP_PARSE_NODE_LEAF_ARG(((mp_parse_node_struct_t*)pns1->nodes[0])->nodes[0]) == MP_QSTR_const
  128. && MP_PARSE_NODE_IS_STRUCT_KIND(((mp_parse_node_struct_t*)pns1->nodes[0])->nodes[1], PN_trailer_paren)
  129. && MP_PARSE_NODE_IS_NULL(((mp_parse_node_struct_t*)pns1->nodes[0])->nodes[2])
  130. ) {
  131. // code to assign dynamic constants: id = const(value)
  132. // get the id
  133. qstr id_qstr = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
  134. // get the value
  135. mp_parse_node_t pn_value = ((mp_parse_node_struct_t*)((mp_parse_node_struct_t*)pns1->nodes[0])->nodes[1])->nodes[0];
  136. pn_value = fold_constants(comp, pn_value, consts);
  137. if (!MP_PARSE_NODE_IS_SMALL_INT(pn_value)) {
  138. compile_syntax_error(comp, (mp_parse_node_t)pns, "constant must be an integer");
  139. break;
  140. }
  141. machine_int_t value = MP_PARSE_NODE_LEAF_SMALL_INT(pn_value);
  142. // store the value in the table of dynamic constants
  143. mp_map_elem_t *elem = mp_map_lookup(consts, MP_OBJ_NEW_QSTR(id_qstr), MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
  144. if (elem->value != MP_OBJ_NULL) {
  145. compile_syntax_error(comp, (mp_parse_node_t)pns, "constant redefined");
  146. break;
  147. }
  148. elem->value = MP_OBJ_NEW_SMALL_INT(value);
  149. // replace const(value) with value
  150. pns1->nodes[0] = pn_value;
  151. // finished folding this assignment
  152. return pn;
  153. }
  154. }
  155. }
  156. break;
  157. #endif
  158. case PN_string:
  159. return pn;
  160. }
  161. // fold arguments
  162. int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
  163. for (int i = 0; i < n; i++) {
  164. pns->nodes[i] = fold_constants(comp, pns->nodes[i], consts);
  165. }
  166. // try to fold this parse node
  167. switch (MP_PARSE_NODE_STRUCT_KIND(pns)) {
  168. case PN_atom_paren:
  169. if (n == 1 && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0])) {
  170. // (int)
  171. pn = pns->nodes[0];
  172. }
  173. break;
  174. case PN_expr:
  175. if (n == 2 && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[1])) {
  176. // int | int
  177. machine_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
  178. machine_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[1]);
  179. pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 | arg1);
  180. }
  181. break;
  182. case PN_and_expr:
  183. if (n == 2 && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[1])) {
  184. // int & int
  185. machine_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
  186. machine_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[1]);
  187. pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 & arg1);
  188. }
  189. break;
  190. case PN_shift_expr:
  191. if (n == 3 && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[2])) {
  192. machine_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
  193. machine_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[2]);
  194. if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_DBL_LESS)) {
  195. // int << int
  196. if (!(arg1 >= BITS_PER_WORD || arg0 > (MP_SMALL_INT_MAX >> arg1) || arg0 < (MP_SMALL_INT_MIN >> arg1))) {
  197. pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 << arg1);
  198. }
  199. } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_DBL_MORE)) {
  200. // int >> int
  201. pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0 >> arg1);
  202. } else {
  203. // shouldn't happen
  204. assert(0);
  205. }
  206. }
  207. break;
  208. case PN_arith_expr:
  209. // overflow checking here relies on SMALL_INT being strictly smaller than machine_int_t
  210. if (n == 3 && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[2])) {
  211. machine_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
  212. machine_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[2]);
  213. if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_PLUS)) {
  214. // int + int
  215. arg0 += arg1;
  216. } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_MINUS)) {
  217. // int - int
  218. arg0 -= arg1;
  219. } else {
  220. // shouldn't happen
  221. assert(0);
  222. }
  223. if (MP_SMALL_INT_FITS(arg0)) {
  224. //printf("%ld + %ld\n", arg0, arg1);
  225. pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0);
  226. }
  227. }
  228. break;
  229. case PN_term:
  230. if (n == 3 && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[2])) {
  231. machine_int_t arg0 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
  232. machine_int_t arg1 = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[2]);
  233. if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_STAR)) {
  234. // int * int
  235. if (!mp_small_int_mul_overflow(arg0, arg1)) {
  236. arg0 *= arg1;
  237. if (MP_SMALL_INT_FITS(arg0)) {
  238. pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg0);
  239. }
  240. }
  241. } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_SLASH)) {
  242. // int / int
  243. // pass
  244. } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_PERCENT)) {
  245. // int%int
  246. pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, mp_small_int_modulo(arg0, arg1));
  247. } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_DBL_SLASH)) {
  248. if (arg1 != 0) {
  249. // int // int
  250. pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, mp_small_int_floor_divide(arg0, arg1));
  251. }
  252. } else {
  253. // shouldn't happen
  254. assert(0);
  255. }
  256. }
  257. break;
  258. case PN_factor_2:
  259. if (MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[1])) {
  260. machine_int_t arg = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[1]);
  261. if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_PLUS)) {
  262. // +int
  263. pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg);
  264. } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_MINUS)) {
  265. // -int
  266. pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, -arg);
  267. } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_TILDE)) {
  268. // ~int
  269. pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, ~arg);
  270. } else {
  271. // shouldn't happen
  272. assert(0);
  273. }
  274. }
  275. break;
  276. case PN_power:
  277. if (0) {
  278. #if MICROPY_EMIT_CPYTHON
  279. } else if (MP_PARSE_NODE_IS_SMALL_INT(pns->nodes[0]) && MP_PARSE_NODE_IS_NULL(pns->nodes[1]) && !MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
  280. // int ** x
  281. // can overflow; enabled only to compare with CPython
  282. mp_parse_node_struct_t* pns2 = (mp_parse_node_struct_t*)pns->nodes[2];
  283. if (MP_PARSE_NODE_IS_SMALL_INT(pns2->nodes[0])) {
  284. int power = MP_PARSE_NODE_LEAF_SMALL_INT(pns2->nodes[0]);
  285. if (power >= 0) {
  286. int ans = 1;
  287. int base = MP_PARSE_NODE_LEAF_SMALL_INT(pns->nodes[0]);
  288. for (; power > 0; power--) {
  289. ans *= base;
  290. }
  291. pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, ans);
  292. }
  293. }
  294. #endif
  295. } else if (MP_PARSE_NODE_IS_ID(pns->nodes[0]) && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_trailer_period) && MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
  296. // id.id
  297. // look it up in constant table, see if it can be replaced with an integer
  298. mp_parse_node_struct_t* pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
  299. assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
  300. qstr q_base = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
  301. qstr q_attr = MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]);
  302. mp_map_elem_t *elem = mp_map_lookup((mp_map_t*)&mp_constants_map, MP_OBJ_NEW_QSTR(q_base), MP_MAP_LOOKUP);
  303. if (elem != NULL) {
  304. mp_obj_t dest[2];
  305. mp_load_method_maybe(elem->value, q_attr, dest);
  306. if (MP_OBJ_IS_SMALL_INT(dest[0]) && dest[1] == NULL) {
  307. machine_int_t val = MP_OBJ_SMALL_INT_VALUE(dest[0]);
  308. if (MP_SMALL_INT_FITS(val)) {
  309. pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, val);
  310. }
  311. }
  312. }
  313. }
  314. break;
  315. }
  316. }
  317. return pn;
  318. }
  319. STATIC void compile_trailer_paren_helper(compiler_t *comp, mp_parse_node_t pn_arglist, bool is_method_call, int n_positional_extra);
  320. void compile_comprehension(compiler_t *comp, mp_parse_node_struct_t *pns, scope_kind_t kind);
  321. STATIC void compile_node(compiler_t *comp, mp_parse_node_t pn);
  322. STATIC uint comp_next_label(compiler_t *comp) {
  323. return comp->next_label++;
  324. }
  325. STATIC void compile_increase_except_level(compiler_t *comp) {
  326. comp->cur_except_level += 1;
  327. if (comp->cur_except_level > comp->scope_cur->exc_stack_size) {
  328. comp->scope_cur->exc_stack_size = comp->cur_except_level;
  329. }
  330. }
  331. STATIC void compile_decrease_except_level(compiler_t *comp) {
  332. assert(comp->cur_except_level > 0);
  333. comp->cur_except_level -= 1;
  334. }
  335. STATIC scope_t *scope_new_and_link(compiler_t *comp, scope_kind_t kind, mp_parse_node_t pn, uint emit_options) {
  336. scope_t *scope = scope_new(kind, pn, comp->source_file, emit_options);
  337. scope->parent = comp->scope_cur;
  338. scope->next = NULL;
  339. if (comp->scope_head == NULL) {
  340. comp->scope_head = scope;
  341. } else {
  342. scope_t *s = comp->scope_head;
  343. while (s->next != NULL) {
  344. s = s->next;
  345. }
  346. s->next = scope;
  347. }
  348. return scope;
  349. }
  350. STATIC void apply_to_single_or_list(compiler_t *comp, mp_parse_node_t pn, int pn_list_kind, void (*f)(compiler_t*, mp_parse_node_t)) {
  351. if (MP_PARSE_NODE_IS_STRUCT(pn) && MP_PARSE_NODE_STRUCT_KIND((mp_parse_node_struct_t*)pn) == pn_list_kind) {
  352. mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
  353. int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
  354. for (int i = 0; i < num_nodes; i++) {
  355. f(comp, pns->nodes[i]);
  356. }
  357. } else if (!MP_PARSE_NODE_IS_NULL(pn)) {
  358. f(comp, pn);
  359. }
  360. }
  361. STATIC int list_get(mp_parse_node_t *pn, int pn_kind, mp_parse_node_t **nodes) {
  362. if (MP_PARSE_NODE_IS_NULL(*pn)) {
  363. *nodes = NULL;
  364. return 0;
  365. } else if (MP_PARSE_NODE_IS_LEAF(*pn)) {
  366. *nodes = pn;
  367. return 1;
  368. } else {
  369. mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)(*pn);
  370. if (MP_PARSE_NODE_STRUCT_KIND(pns) != pn_kind) {
  371. *nodes = pn;
  372. return 1;
  373. } else {
  374. *nodes = pns->nodes;
  375. return MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
  376. }
  377. }
  378. }
  379. void compile_do_nothing(compiler_t *comp, mp_parse_node_struct_t *pns) {
  380. }
  381. void compile_generic_all_nodes(compiler_t *comp, mp_parse_node_struct_t *pns) {
  382. int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
  383. for (int i = 0; i < num_nodes; i++) {
  384. compile_node(comp, pns->nodes[i]);
  385. }
  386. }
  387. #if MICROPY_EMIT_CPYTHON
  388. STATIC bool cpython_c_tuple_is_const(mp_parse_node_t pn) {
  389. if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_string)) {
  390. return true;
  391. }
  392. if (!MP_PARSE_NODE_IS_LEAF(pn)) {
  393. return false;
  394. }
  395. if (MP_PARSE_NODE_IS_ID(pn)) {
  396. return false;
  397. }
  398. return true;
  399. }
  400. STATIC void cpython_c_print_quoted_str(vstr_t *vstr, const char *str, uint len, bool bytes) {
  401. bool has_single_quote = false;
  402. bool has_double_quote = false;
  403. for (int i = 0; i < len; i++) {
  404. if (str[i] == '\'') {
  405. has_single_quote = true;
  406. } else if (str[i] == '"') {
  407. has_double_quote = true;
  408. }
  409. }
  410. if (bytes) {
  411. vstr_printf(vstr, "b");
  412. }
  413. bool quote_single = false;
  414. if (has_single_quote && !has_double_quote) {
  415. vstr_printf(vstr, "\"");
  416. } else {
  417. quote_single = true;
  418. vstr_printf(vstr, "'");
  419. }
  420. for (int i = 0; i < len; i++) {
  421. if (str[i] == '\n') {
  422. vstr_printf(vstr, "\\n");
  423. } else if (str[i] == '\\') {
  424. vstr_printf(vstr, "\\\\");
  425. } else if (str[i] == '\'' && quote_single) {
  426. vstr_printf(vstr, "\\'");
  427. } else {
  428. vstr_printf(vstr, "%c", str[i]);
  429. }
  430. }
  431. if (has_single_quote && !has_double_quote) {
  432. vstr_printf(vstr, "\"");
  433. } else {
  434. vstr_printf(vstr, "'");
  435. }
  436. }
  437. STATIC void cpython_c_tuple_emit_const(compiler_t *comp, mp_parse_node_t pn, vstr_t *vstr) {
  438. if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_string)) {
  439. mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
  440. cpython_c_print_quoted_str(vstr, (const char*)pns->nodes[0], (machine_uint_t)pns->nodes[1], false);
  441. return;
  442. }
  443. assert(MP_PARSE_NODE_IS_LEAF(pn));
  444. if (MP_PARSE_NODE_IS_SMALL_INT(pn)) {
  445. vstr_printf(vstr, INT_FMT, MP_PARSE_NODE_LEAF_SMALL_INT(pn));
  446. return;
  447. }
  448. int arg = MP_PARSE_NODE_LEAF_ARG(pn);
  449. switch (MP_PARSE_NODE_LEAF_KIND(pn)) {
  450. case MP_PARSE_NODE_ID: assert(0);
  451. case MP_PARSE_NODE_INTEGER: vstr_printf(vstr, "%s", qstr_str(arg)); break;
  452. case MP_PARSE_NODE_DECIMAL: vstr_printf(vstr, "%s", qstr_str(arg)); break;
  453. case MP_PARSE_NODE_STRING:
  454. case MP_PARSE_NODE_BYTES: {
  455. uint len;
  456. const byte *str = qstr_data(arg, &len);
  457. cpython_c_print_quoted_str(vstr, (const char*)str, len, MP_PARSE_NODE_LEAF_KIND(pn) == MP_PARSE_NODE_BYTES);
  458. break;
  459. }
  460. case MP_PARSE_NODE_TOKEN:
  461. switch (arg) {
  462. case MP_TOKEN_KW_FALSE: vstr_printf(vstr, "False"); break;
  463. case MP_TOKEN_KW_NONE: vstr_printf(vstr, "None"); break;
  464. case MP_TOKEN_KW_TRUE: vstr_printf(vstr, "True"); break;
  465. default: assert(0); // shouldn't happen
  466. }
  467. break;
  468. default: assert(0);
  469. }
  470. }
  471. STATIC void cpython_c_tuple(compiler_t *comp, mp_parse_node_t pn, mp_parse_node_struct_t *pns_list) {
  472. int n = 0;
  473. if (pns_list != NULL) {
  474. n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns_list);
  475. }
  476. int total = n;
  477. bool is_const = true;
  478. if (!MP_PARSE_NODE_IS_NULL(pn)) {
  479. total += 1;
  480. if (!cpython_c_tuple_is_const(pn)) {
  481. is_const = false;
  482. }
  483. }
  484. for (int i = 0; i < n; i++) {
  485. if (!cpython_c_tuple_is_const(pns_list->nodes[i])) {
  486. is_const = false;
  487. break;
  488. }
  489. }
  490. if (total > 0 && is_const) {
  491. bool need_comma = false;
  492. vstr_t *vstr = vstr_new();
  493. vstr_printf(vstr, "(");
  494. if (!MP_PARSE_NODE_IS_NULL(pn)) {
  495. cpython_c_tuple_emit_const(comp, pn, vstr);
  496. need_comma = true;
  497. }
  498. for (int i = 0; i < n; i++) {
  499. if (need_comma) {
  500. vstr_printf(vstr, ", ");
  501. }
  502. cpython_c_tuple_emit_const(comp, pns_list->nodes[i], vstr);
  503. need_comma = true;
  504. }
  505. if (total == 1) {
  506. vstr_printf(vstr, ",)");
  507. } else {
  508. vstr_printf(vstr, ")");
  509. }
  510. EMIT_ARG(load_const_verbatim_str, vstr_str(vstr));
  511. vstr_free(vstr);
  512. } else {
  513. if (!MP_PARSE_NODE_IS_NULL(pn)) {
  514. compile_node(comp, pn);
  515. }
  516. for (int i = 0; i < n; i++) {
  517. compile_node(comp, pns_list->nodes[i]);
  518. }
  519. EMIT_ARG(build_tuple, total);
  520. }
  521. }
  522. #endif
  523. // funnelling all tuple creations through this function is purely so we can optionally agree with CPython
  524. STATIC void c_tuple(compiler_t *comp, mp_parse_node_t pn, mp_parse_node_struct_t *pns_list) {
  525. #if MICROPY_EMIT_CPYTHON
  526. cpython_c_tuple(comp, pn, pns_list);
  527. #else
  528. int total = 0;
  529. if (!MP_PARSE_NODE_IS_NULL(pn)) {
  530. compile_node(comp, pn);
  531. total += 1;
  532. }
  533. if (pns_list != NULL) {
  534. int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns_list);
  535. for (int i = 0; i < n; i++) {
  536. compile_node(comp, pns_list->nodes[i]);
  537. }
  538. total += n;
  539. }
  540. EMIT_ARG(build_tuple, total);
  541. #endif
  542. }
  543. void compile_generic_tuple(compiler_t *comp, mp_parse_node_struct_t *pns) {
  544. // a simple tuple expression
  545. c_tuple(comp, MP_PARSE_NODE_NULL, pns);
  546. }
  547. STATIC bool node_is_const_false(mp_parse_node_t pn) {
  548. return MP_PARSE_NODE_IS_TOKEN_KIND(pn, MP_TOKEN_KW_FALSE);
  549. // untested: || (MP_PARSE_NODE_IS_SMALL_INT(pn) && MP_PARSE_NODE_LEAF_SMALL_INT(pn) == 0);
  550. }
  551. STATIC bool node_is_const_true(mp_parse_node_t pn) {
  552. return MP_PARSE_NODE_IS_TOKEN_KIND(pn, MP_TOKEN_KW_TRUE) || (MP_PARSE_NODE_IS_SMALL_INT(pn) && MP_PARSE_NODE_LEAF_SMALL_INT(pn) == 1);
  553. }
  554. #if MICROPY_EMIT_CPYTHON
  555. // the is_nested variable is purely to match with CPython, which doesn't fully optimise not's
  556. STATIC void cpython_c_if_cond(compiler_t *comp, mp_parse_node_t pn, bool jump_if, int label, bool is_nested) {
  557. if (node_is_const_false(pn)) {
  558. if (jump_if == false) {
  559. EMIT_ARG(jump, label);
  560. }
  561. return;
  562. } else if (node_is_const_true(pn)) {
  563. if (jump_if == true) {
  564. EMIT_ARG(jump, label);
  565. }
  566. return;
  567. } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
  568. mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
  569. int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
  570. if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_or_test) {
  571. if (jump_if == false) {
  572. uint label2 = comp_next_label(comp);
  573. for (int i = 0; i < n - 1; i++) {
  574. cpython_c_if_cond(comp, pns->nodes[i], true, label2, true);
  575. }
  576. cpython_c_if_cond(comp, pns->nodes[n - 1], false, label, true);
  577. EMIT_ARG(label_assign, label2);
  578. } else {
  579. for (int i = 0; i < n; i++) {
  580. cpython_c_if_cond(comp, pns->nodes[i], true, label, true);
  581. }
  582. }
  583. return;
  584. } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_and_test) {
  585. if (jump_if == false) {
  586. for (int i = 0; i < n; i++) {
  587. cpython_c_if_cond(comp, pns->nodes[i], false, label, true);
  588. }
  589. } else {
  590. uint label2 = comp_next_label(comp);
  591. for (int i = 0; i < n - 1; i++) {
  592. cpython_c_if_cond(comp, pns->nodes[i], false, label2, true);
  593. }
  594. cpython_c_if_cond(comp, pns->nodes[n - 1], true, label, true);
  595. EMIT_ARG(label_assign, label2);
  596. }
  597. return;
  598. } else if (!is_nested && MP_PARSE_NODE_STRUCT_KIND(pns) == PN_not_test_2) {
  599. cpython_c_if_cond(comp, pns->nodes[0], !jump_if, label, true);
  600. return;
  601. }
  602. }
  603. // nothing special, fall back to default compiling for node and jump
  604. compile_node(comp, pn);
  605. if (jump_if == false) {
  606. EMIT_ARG(pop_jump_if_false, label);
  607. } else {
  608. EMIT_ARG(pop_jump_if_true, label);
  609. }
  610. }
  611. #endif
  612. STATIC void c_if_cond(compiler_t *comp, mp_parse_node_t pn, bool jump_if, int label) {
  613. #if MICROPY_EMIT_CPYTHON
  614. cpython_c_if_cond(comp, pn, jump_if, label, false);
  615. #else
  616. if (node_is_const_false(pn)) {
  617. if (jump_if == false) {
  618. EMIT_ARG(jump, label);
  619. }
  620. return;
  621. } else if (node_is_const_true(pn)) {
  622. if (jump_if == true) {
  623. EMIT_ARG(jump, label);
  624. }
  625. return;
  626. } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
  627. mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
  628. int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
  629. if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_or_test) {
  630. if (jump_if == false) {
  631. uint label2 = comp_next_label(comp);
  632. for (int i = 0; i < n - 1; i++) {
  633. c_if_cond(comp, pns->nodes[i], true, label2);
  634. }
  635. c_if_cond(comp, pns->nodes[n - 1], false, label);
  636. EMIT_ARG(label_assign, label2);
  637. } else {
  638. for (int i = 0; i < n; i++) {
  639. c_if_cond(comp, pns->nodes[i], true, label);
  640. }
  641. }
  642. return;
  643. } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_and_test) {
  644. if (jump_if == false) {
  645. for (int i = 0; i < n; i++) {
  646. c_if_cond(comp, pns->nodes[i], false, label);
  647. }
  648. } else {
  649. uint label2 = comp_next_label(comp);
  650. for (int i = 0; i < n - 1; i++) {
  651. c_if_cond(comp, pns->nodes[i], false, label2);
  652. }
  653. c_if_cond(comp, pns->nodes[n - 1], true, label);
  654. EMIT_ARG(label_assign, label2);
  655. }
  656. return;
  657. } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_not_test_2) {
  658. c_if_cond(comp, pns->nodes[0], !jump_if, label);
  659. return;
  660. }
  661. }
  662. // nothing special, fall back to default compiling for node and jump
  663. compile_node(comp, pn);
  664. if (jump_if == false) {
  665. EMIT_ARG(pop_jump_if_false, label);
  666. } else {
  667. EMIT_ARG(pop_jump_if_true, label);
  668. }
  669. #endif
  670. }
  671. typedef enum { ASSIGN_STORE, ASSIGN_AUG_LOAD, ASSIGN_AUG_STORE } assign_kind_t;
  672. void c_assign(compiler_t *comp, mp_parse_node_t pn, assign_kind_t kind);
  673. void c_assign_power(compiler_t *comp, mp_parse_node_struct_t *pns, assign_kind_t assign_kind) {
  674. if (assign_kind != ASSIGN_AUG_STORE) {
  675. compile_node(comp, pns->nodes[0]);
  676. }
  677. if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
  678. mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
  679. if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_power_trailers) {
  680. int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
  681. if (assign_kind != ASSIGN_AUG_STORE) {
  682. for (int i = 0; i < n - 1; i++) {
  683. compile_node(comp, pns1->nodes[i]);
  684. }
  685. }
  686. assert(MP_PARSE_NODE_IS_STRUCT(pns1->nodes[n - 1]));
  687. pns1 = (mp_parse_node_struct_t*)pns1->nodes[n - 1];
  688. }
  689. if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_paren) {
  690. goto cannot_assign;
  691. } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_bracket) {
  692. if (assign_kind == ASSIGN_AUG_STORE) {
  693. EMIT(rot_three);
  694. EMIT(store_subscr);
  695. } else {
  696. compile_node(comp, pns1->nodes[0]);
  697. if (assign_kind == ASSIGN_AUG_LOAD) {
  698. EMIT(dup_top_two);
  699. EMIT(load_subscr);
  700. } else {
  701. EMIT(store_subscr);
  702. }
  703. }
  704. } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_period) {
  705. assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
  706. if (assign_kind == ASSIGN_AUG_LOAD) {
  707. EMIT(dup_top);
  708. EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
  709. } else {
  710. if (assign_kind == ASSIGN_AUG_STORE) {
  711. EMIT(rot_two);
  712. }
  713. EMIT_ARG(store_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
  714. }
  715. } else {
  716. goto cannot_assign;
  717. }
  718. } else {
  719. goto cannot_assign;
  720. }
  721. if (!MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
  722. goto cannot_assign;
  723. }
  724. return;
  725. cannot_assign:
  726. compile_syntax_error(comp, (mp_parse_node_t)pns, "can't assign to expression");
  727. }
  728. // we need to allow for a caller passing in 1 initial node (node_head) followed by an array of nodes (nodes_tail)
  729. void c_assign_tuple(compiler_t *comp, mp_parse_node_t node_head, uint num_tail, mp_parse_node_t *nodes_tail) {
  730. uint num_head = (node_head == MP_PARSE_NODE_NULL) ? 0 : 1;
  731. // look for star expression
  732. int have_star_index = -1;
  733. if (num_head != 0 && MP_PARSE_NODE_IS_STRUCT_KIND(node_head, PN_star_expr)) {
  734. EMIT_ARG(unpack_ex, 0, num_tail);
  735. have_star_index = 0;
  736. }
  737. for (int i = 0; i < num_tail; i++) {
  738. if (MP_PARSE_NODE_IS_STRUCT_KIND(nodes_tail[i], PN_star_expr)) {
  739. if (have_star_index < 0) {
  740. EMIT_ARG(unpack_ex, num_head + i, num_tail - i - 1);
  741. have_star_index = num_head + i;
  742. } else {
  743. compile_syntax_error(comp, nodes_tail[i], "multiple *x in assignment");
  744. return;
  745. }
  746. }
  747. }
  748. if (have_star_index < 0) {
  749. EMIT_ARG(unpack_sequence, num_head + num_tail);
  750. }
  751. if (num_head != 0) {
  752. if (0 == have_star_index) {
  753. c_assign(comp, ((mp_parse_node_struct_t*)node_head)->nodes[0], ASSIGN_STORE);
  754. } else {
  755. c_assign(comp, node_head, ASSIGN_STORE);
  756. }
  757. }
  758. for (int i = 0; i < num_tail; i++) {
  759. if (num_head + i == have_star_index) {
  760. c_assign(comp, ((mp_parse_node_struct_t*)nodes_tail[i])->nodes[0], ASSIGN_STORE);
  761. } else {
  762. c_assign(comp, nodes_tail[i], ASSIGN_STORE);
  763. }
  764. }
  765. }
  766. // assigns top of stack to pn
  767. void c_assign(compiler_t *comp, mp_parse_node_t pn, assign_kind_t assign_kind) {
  768. tail_recursion:
  769. if (MP_PARSE_NODE_IS_NULL(pn)) {
  770. assert(0);
  771. } else if (MP_PARSE_NODE_IS_LEAF(pn)) {
  772. if (MP_PARSE_NODE_IS_ID(pn)) {
  773. int arg = MP_PARSE_NODE_LEAF_ARG(pn);
  774. switch (assign_kind) {
  775. case ASSIGN_STORE:
  776. case ASSIGN_AUG_STORE:
  777. EMIT_ARG(store_id, arg);
  778. break;
  779. case ASSIGN_AUG_LOAD:
  780. EMIT_ARG(load_id, arg);
  781. break;
  782. }
  783. } else {
  784. compile_syntax_error(comp, pn, "can't assign to literal");
  785. return;
  786. }
  787. } else {
  788. mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
  789. switch (MP_PARSE_NODE_STRUCT_KIND(pns)) {
  790. case PN_power:
  791. // lhs is an index or attribute
  792. c_assign_power(comp, pns, assign_kind);
  793. break;
  794. case PN_testlist_star_expr:
  795. case PN_exprlist:
  796. // lhs is a tuple
  797. if (assign_kind != ASSIGN_STORE) {
  798. goto bad_aug;
  799. }
  800. c_assign_tuple(comp, MP_PARSE_NODE_NULL, MP_PARSE_NODE_STRUCT_NUM_NODES(pns), pns->nodes);
  801. break;
  802. case PN_atom_paren:
  803. // lhs is something in parenthesis
  804. if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
  805. // empty tuple
  806. goto cannot_assign;
  807. } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
  808. pns = (mp_parse_node_struct_t*)pns->nodes[0];
  809. goto testlist_comp;
  810. } else {
  811. // parenthesis around 1 item, is just that item
  812. pn = pns->nodes[0];
  813. goto tail_recursion;
  814. }
  815. break;
  816. case PN_atom_bracket:
  817. // lhs is something in brackets
  818. if (assign_kind != ASSIGN_STORE) {
  819. goto bad_aug;
  820. }
  821. if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
  822. // empty list, assignment allowed
  823. c_assign_tuple(comp, MP_PARSE_NODE_NULL, 0, NULL);
  824. } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
  825. pns = (mp_parse_node_struct_t*)pns->nodes[0];
  826. goto testlist_comp;
  827. } else {
  828. // brackets around 1 item
  829. c_assign_tuple(comp, pns->nodes[0], 0, NULL);
  830. }
  831. break;
  832. default:
  833. goto cannot_assign;
  834. }
  835. return;
  836. testlist_comp:
  837. // lhs is a sequence
  838. if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
  839. mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
  840. if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3b) {
  841. // sequence of one item, with trailing comma
  842. assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[0]));
  843. c_assign_tuple(comp, pns->nodes[0], 0, NULL);
  844. } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3c) {
  845. // sequence of many items
  846. uint n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns2);
  847. c_assign_tuple(comp, pns->nodes[0], n, pns2->nodes);
  848. } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_comp_for) {
  849. // TODO can we ever get here? can it be compiled?
  850. goto cannot_assign;
  851. } else {
  852. // sequence with 2 items
  853. goto sequence_with_2_items;
  854. }
  855. } else {
  856. // sequence with 2 items
  857. sequence_with_2_items:
  858. c_assign_tuple(comp, MP_PARSE_NODE_NULL, 2, pns->nodes);
  859. }
  860. return;
  861. }
  862. return;
  863. cannot_assign:
  864. compile_syntax_error(comp, pn, "can't assign to expression");
  865. return;
  866. bad_aug:
  867. compile_syntax_error(comp, pn, "illegal expression for augmented assignment");
  868. }
  869. // stuff for lambda and comprehensions and generators
  870. // if we are not in CPython compatibility mode then:
  871. // if n_pos_defaults > 0 then there is a tuple on the stack with the positional defaults
  872. // if n_kw_defaults > 0 then there is a dictionary on the stack with the keyword defaults
  873. // if both exist, the tuple is above the dictionary (ie the first pop gets the tuple)
  874. void close_over_variables_etc(compiler_t *comp, scope_t *this_scope, int n_pos_defaults, int n_kw_defaults) {
  875. assert(n_pos_defaults >= 0);
  876. assert(n_kw_defaults >= 0);
  877. // make closed over variables, if any
  878. // ensure they are closed over in the order defined in the outer scope (mainly to agree with CPython)
  879. int nfree = 0;
  880. if (comp->scope_cur->kind != SCOPE_MODULE) {
  881. for (int i = 0; i < comp->scope_cur->id_info_len; i++) {
  882. id_info_t *id = &comp->scope_cur->id_info[i];
  883. if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
  884. for (int j = 0; j < this_scope->id_info_len; j++) {
  885. id_info_t *id2 = &this_scope->id_info[j];
  886. if (id2->kind == ID_INFO_KIND_FREE && id->qstr == id2->qstr) {
  887. #if MICROPY_EMIT_CPYTHON
  888. EMIT_ARG(load_closure, id->qstr, id->local_num);
  889. #else
  890. // in Micro Python we load closures using LOAD_FAST
  891. EMIT_ARG(load_fast, id->qstr, id->flags, id->local_num);
  892. #endif
  893. nfree += 1;
  894. }
  895. }
  896. }
  897. }
  898. }
  899. // make the function/closure
  900. if (nfree == 0) {
  901. EMIT_ARG(make_function, this_scope, n_pos_defaults, n_kw_defaults);
  902. } else {
  903. EMIT_ARG(make_closure, this_scope, nfree, n_pos_defaults, n_kw_defaults);
  904. }
  905. }
  906. void compile_funcdef_param(compiler_t *comp, mp_parse_node_t pn) {
  907. if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_star)) {
  908. comp->have_star = true;
  909. /* don't need to distinguish bare from named star
  910. mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
  911. if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
  912. // bare star
  913. } else {
  914. // named star
  915. }
  916. */
  917. } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_dbl_star)) {
  918. // named double star
  919. // TODO do we need to do anything with this?
  920. } else {
  921. mp_parse_node_t pn_id;
  922. mp_parse_node_t pn_colon;
  923. mp_parse_node_t pn_equal;
  924. if (MP_PARSE_NODE_IS_ID(pn)) {
  925. // this parameter is just an id
  926. pn_id = pn;
  927. pn_colon = MP_PARSE_NODE_NULL;
  928. pn_equal = MP_PARSE_NODE_NULL;
  929. } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_name)) {
  930. // this parameter has a colon and/or equal specifier
  931. mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
  932. pn_id = pns->nodes[0];
  933. pn_colon = pns->nodes[1];
  934. pn_equal = pns->nodes[2];
  935. } else {
  936. // XXX what to do here?
  937. assert(0);
  938. return;
  939. }
  940. if (MP_PARSE_NODE_IS_NULL(pn_equal)) {
  941. // this parameter does not have a default value
  942. // check for non-default parameters given after default parameters (allowed by parser, but not syntactically valid)
  943. if (!comp->have_star && comp->num_default_params != 0) {
  944. compile_syntax_error(comp, pn, "non-default argument follows default argument");
  945. return;
  946. }
  947. } else {
  948. // this parameter has a default value
  949. // in CPython, None (and True, False?) as default parameters are loaded with LOAD_NAME; don't understandy why
  950. if (comp->have_star) {
  951. comp->num_dict_params += 1;
  952. #if MICROPY_EMIT_CPYTHON
  953. EMIT_ARG(load_const_str, MP_PARSE_NODE_LEAF_ARG(pn_id), false);
  954. compile_node(comp, pn_equal);
  955. #else
  956. // in Micro Python we put the default dict parameters into a dictionary using the bytecode
  957. if (comp->num_dict_params == 1) {
  958. // in Micro Python we put the default positional parameters into a tuple using the bytecode
  959. // we need to do this here before we start building the map for the default keywords
  960. if (comp->num_default_params > 0) {
  961. EMIT_ARG(build_tuple, comp->num_default_params);
  962. } else {
  963. EMIT(load_null); // sentinel indicating empty default positional args
  964. }
  965. // first default dict param, so make the map
  966. EMIT_ARG(build_map, 0);
  967. }
  968. // compile value then key, then store it to the dict
  969. compile_node(comp, pn_equal);
  970. EMIT_ARG(load_const_str, MP_PARSE_NODE_LEAF_ARG(pn_id), false);
  971. EMIT(store_map);
  972. #endif
  973. } else {
  974. comp->num_default_params += 1;
  975. compile_node(comp, pn_equal);
  976. }
  977. }
  978. // TODO pn_colon not implemented
  979. (void)pn_colon;
  980. }
  981. }
  982. // leaves function object on stack
  983. // returns function name
  984. qstr compile_funcdef_helper(compiler_t *comp, mp_parse_node_struct_t *pns, uint emit_options) {
  985. if (comp->pass == MP_PASS_SCOPE) {
  986. // create a new scope for this function
  987. scope_t *s = scope_new_and_link(comp, SCOPE_FUNCTION, (mp_parse_node_t)pns, emit_options);
  988. // store the function scope so the compiling function can use it at each pass
  989. pns->nodes[4] = (mp_parse_node_t)s;
  990. }
  991. // save variables (probably don't need to do this, since we can't have nested definitions..?)
  992. uint old_have_star = comp->have_star;
  993. uint old_num_dict_params = comp->num_dict_params;
  994. uint old_num_default_params = comp->num_default_params;
  995. // compile default parameters
  996. comp->have_star = false;
  997. comp->num_dict_params = 0;
  998. comp->num_default_params = 0;
  999. apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_funcdef_param);
  1000. if (comp->had_error) {
  1001. return MP_QSTR_NULL;
  1002. }
  1003. #if !MICROPY_EMIT_CPYTHON
  1004. // in Micro Python we put the default positional parameters into a tuple using the bytecode
  1005. // the default keywords args may have already made the tuple; if not, do it now
  1006. if (comp->num_default_params > 0 && comp->num_dict_params == 0) {
  1007. EMIT_ARG(build_tuple, comp->num_default_params);
  1008. EMIT(load_null); // sentinel indicating empty default keyword args
  1009. }
  1010. #endif
  1011. // get the scope for this function
  1012. scope_t *fscope = (scope_t*)pns->nodes[4];
  1013. // make the function
  1014. close_over_variables_etc(comp, fscope, comp->num_default_params, comp->num_dict_params);
  1015. // restore variables
  1016. comp->have_star = old_have_star;
  1017. comp->num_dict_params = old_num_dict_params;
  1018. comp->num_default_params = old_num_default_params;
  1019. // return its name (the 'f' in "def f(...):")
  1020. return fscope->simple_name;
  1021. }
  1022. // leaves class object on stack
  1023. // returns class name
  1024. qstr compile_classdef_helper(compiler_t *comp, mp_parse_node_struct_t *pns, uint emit_options) {
  1025. if (comp->pass == MP_PASS_SCOPE) {
  1026. // create a new scope for this class
  1027. scope_t *s = scope_new_and_link(comp, SCOPE_CLASS, (mp_parse_node_t)pns, emit_options);
  1028. // store the class scope so the compiling function can use it at each pass
  1029. pns->nodes[3] = (mp_parse_node_t)s;
  1030. }
  1031. EMIT(load_build_class);
  1032. // scope for this class
  1033. scope_t *cscope = (scope_t*)pns->nodes[3];
  1034. // compile the class
  1035. close_over_variables_etc(comp, cscope, 0, 0);
  1036. // get its name
  1037. EMIT_ARG(load_const_str, cscope->simple_name, false);
  1038. // nodes[1] has parent classes, if any
  1039. // empty parenthesis (eg class C():) gets here as an empty PN_classdef_2 and needs special handling
  1040. mp_parse_node_t parents = pns->nodes[1];
  1041. if (MP_PARSE_NODE_IS_STRUCT_KIND(parents, PN_classdef_2)) {
  1042. parents = MP_PARSE_NODE_NULL;
  1043. }
  1044. comp->func_arg_is_super = false;
  1045. compile_trailer_paren_helper(comp, parents, false, 2);
  1046. // return its name (the 'C' in class C(...):")
  1047. return cscope->simple_name;
  1048. }
  1049. // returns true if it was a built-in decorator (even if the built-in had an error)
  1050. STATIC bool compile_built_in_decorator(compiler_t *comp, int name_len, mp_parse_node_t *name_nodes, uint *emit_options) {
  1051. if (MP_PARSE_NODE_LEAF_ARG(name_nodes[0]) != MP_QSTR_micropython) {
  1052. return false;
  1053. }
  1054. if (name_len != 2) {
  1055. compile_syntax_error(comp, name_nodes[0], "invalid micropython decorator");
  1056. return true;
  1057. }
  1058. qstr attr = MP_PARSE_NODE_LEAF_ARG(name_nodes[1]);
  1059. if (attr == MP_QSTR_bytecode) {
  1060. *emit_options = MP_EMIT_OPT_BYTECODE;
  1061. #if MICROPY_EMIT_NATIVE
  1062. } else if (attr == MP_QSTR_native) {
  1063. *emit_options = MP_EMIT_OPT_NATIVE_PYTHON;
  1064. } else if (attr == MP_QSTR_viper) {
  1065. *emit_options = MP_EMIT_OPT_VIPER;
  1066. #endif
  1067. #if MICROPY_EMIT_INLINE_THUMB
  1068. } else if (attr == MP_QSTR_asm_thumb) {
  1069. *emit_options = MP_EMIT_OPT_ASM_THUMB;
  1070. #endif
  1071. } else {
  1072. compile_syntax_error(comp, name_nodes[1], "invalid micropython decorator");
  1073. }
  1074. return true;
  1075. }
  1076. void compile_decorated(compiler_t *comp, mp_parse_node_struct_t *pns) {
  1077. // get the list of decorators
  1078. mp_parse_node_t *nodes;
  1079. int n = list_get(&pns->nodes[0], PN_decorators, &nodes);
  1080. // inherit emit options for this function/class definition
  1081. uint emit_options = comp->scope_cur->emit_options;
  1082. // compile each decorator
  1083. int num_built_in_decorators = 0;
  1084. for (int i = 0; i < n; i++) {
  1085. assert(MP_PARSE_NODE_IS_STRUCT_KIND(nodes[i], PN_decorator)); // should be
  1086. mp_parse_node_struct_t *pns_decorator = (mp_parse_node_struct_t*)nodes[i];
  1087. // nodes[0] contains the decorator function, which is a dotted name
  1088. mp_parse_node_t *name_nodes;
  1089. int name_len = list_get(&pns_decorator->nodes[0], PN_dotted_name, &name_nodes);
  1090. // check for built-in decorators
  1091. if (compile_built_in_decorator(comp, name_len, name_nodes, &emit_options)) {
  1092. // this was a built-in
  1093. num_built_in_decorators += 1;
  1094. } else {
  1095. // not a built-in, compile normally
  1096. // compile the decorator function
  1097. compile_node(comp, name_nodes[0]);
  1098. for (int i = 1; i < name_len; i++) {
  1099. assert(MP_PARSE_NODE_IS_ID(name_nodes[i])); // should be
  1100. EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(name_nodes[i]));
  1101. }
  1102. // nodes[1] contains arguments to the decorator function, if any
  1103. if (!MP_PARSE_NODE_IS_NULL(pns_decorator->nodes[1])) {
  1104. // call the decorator function with the arguments in nodes[1]
  1105. comp->func_arg_is_super = false;
  1106. compile_node(comp, pns_decorator->nodes[1]);
  1107. }
  1108. }
  1109. }
  1110. // compile the body (funcdef or classdef) and get its name
  1111. mp_parse_node_struct_t *pns_body = (mp_parse_node_struct_t*)pns->nodes[1];
  1112. qstr body_name = 0;
  1113. if (MP_PARSE_NODE_STRUCT_KIND(pns_body) == PN_funcdef) {
  1114. body_name = compile_funcdef_helper(comp, pns_body, emit_options);
  1115. } else if (MP_PARSE_NODE_STRUCT_KIND(pns_body) == PN_classdef) {
  1116. body_name = compile_classdef_helper(comp, pns_body, emit_options);
  1117. } else {
  1118. // shouldn't happen
  1119. assert(0);
  1120. }
  1121. // call each decorator
  1122. for (int i = 0; i < n - num_built_in_decorators; i++) {
  1123. EMIT_ARG(call_function, 1, 0, 0);
  1124. }
  1125. // store func/class object into name
  1126. EMIT_ARG(store_id, body_name);
  1127. }
  1128. void compile_funcdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
  1129. qstr fname = compile_funcdef_helper(comp, pns, comp->scope_cur->emit_options);
  1130. // store function object into function name
  1131. EMIT_ARG(store_id, fname);
  1132. }
  1133. void c_del_stmt(compiler_t *comp, mp_parse_node_t pn) {
  1134. if (MP_PARSE_NODE_IS_ID(pn)) {
  1135. EMIT_ARG(delete_id, MP_PARSE_NODE_LEAF_ARG(pn));
  1136. } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_power)) {
  1137. mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
  1138. compile_node(comp, pns->nodes[0]); // base of the power node
  1139. if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
  1140. mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
  1141. if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_power_trailers) {
  1142. int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
  1143. for (int i = 0; i < n - 1; i++) {
  1144. compile_node(comp, pns1->nodes[i]);
  1145. }
  1146. assert(MP_PARSE_NODE_IS_STRUCT(pns1->nodes[n - 1]));
  1147. pns1 = (mp_parse_node_struct_t*)pns1->nodes[n - 1];
  1148. }
  1149. if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_paren) {
  1150. // can't delete function calls
  1151. goto cannot_delete;
  1152. } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_bracket) {
  1153. compile_node(comp, pns1->nodes[0]);
  1154. EMIT(delete_subscr);
  1155. } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_trailer_period) {
  1156. assert(MP_PARSE_NODE_IS_ID(pns1->nodes[0]));
  1157. EMIT_ARG(delete_attr, MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0]));
  1158. } else {
  1159. goto cannot_delete;
  1160. }
  1161. } else {
  1162. goto cannot_delete;
  1163. }
  1164. if (!MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
  1165. goto cannot_delete;
  1166. }
  1167. } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_atom_paren)) {
  1168. pn = ((mp_parse_node_struct_t*)pn)->nodes[0];
  1169. if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_testlist_comp)) {
  1170. mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
  1171. // TODO perhaps factorise testlist_comp code with other uses of PN_testlist_comp
  1172. if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
  1173. mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
  1174. if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_testlist_comp_3b) {
  1175. // sequence of one item, with trailing comma
  1176. assert(MP_PARSE_NODE_IS_NULL(pns1->nodes[0]));
  1177. c_del_stmt(comp, pns->nodes[0]);
  1178. } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_testlist_comp_3c) {
  1179. // sequence of many items
  1180. int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1);
  1181. c_del_stmt(comp, pns->nodes[0]);
  1182. for (int i = 0; i < n; i++) {
  1183. c_del_stmt(comp, pns1->nodes[i]);
  1184. }
  1185. } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_comp_for) {
  1186. // TODO not implemented; can't del comprehension?
  1187. goto cannot_delete;
  1188. } else {
  1189. // sequence with 2 items
  1190. goto sequence_with_2_items;
  1191. }
  1192. } else {
  1193. // sequence with 2 items
  1194. sequence_with_2_items:
  1195. c_del_stmt(comp, pns->nodes[0]);
  1196. c_del_stmt(comp, pns->nodes[1]);
  1197. }
  1198. } else {
  1199. // tuple with 1 element
  1200. c_del_stmt(comp, pn);
  1201. }
  1202. } else {
  1203. // TODO is there anything else to implement?
  1204. goto cannot_delete;
  1205. }
  1206. return;
  1207. cannot_delete:
  1208. compile_syntax_error(comp, (mp_parse_node_t)pn, "can't delete expression");
  1209. }
  1210. void compile_del_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
  1211. apply_to_single_or_list(comp, pns->nodes[0], PN_exprlist, c_del_stmt);
  1212. }
  1213. void compile_break_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
  1214. if (comp->break_label == 0) {
  1215. compile_syntax_error(comp, (mp_parse_node_t)pns, "'break' outside loop");
  1216. }
  1217. EMIT_ARG(break_loop, comp->break_label, comp->cur_except_level - comp->break_continue_except_level);
  1218. }
  1219. void compile_continue_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
  1220. if (comp->continue_label == 0) {
  1221. compile_syntax_error(comp, (mp_parse_node_t)pns, "'continue' outside loop");
  1222. }
  1223. EMIT_ARG(continue_loop, comp->continue_label, comp->cur_except_level - comp->break_continue_except_level);
  1224. }
  1225. void compile_return_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
  1226. if (comp->scope_cur->kind != SCOPE_FUNCTION) {
  1227. compile_syntax_error(comp, (mp_parse_node_t)pns, "'return' outside function");
  1228. return;
  1229. }
  1230. if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
  1231. // no argument to 'return', so return None
  1232. EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
  1233. } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_test_if_expr)) {
  1234. // special case when returning an if-expression; to match CPython optimisation
  1235. mp_parse_node_struct_t *pns_test_if_expr = (mp_parse_node_struct_t*)pns->nodes[0];
  1236. mp_parse_node_struct_t *pns_test_if_else = (mp_parse_node_struct_t*)pns_test_if_expr->nodes[1];
  1237. uint l_fail = comp_next_label(comp);
  1238. c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition
  1239. compile_node(comp, pns_test_if_expr->nodes[0]); // success value
  1240. EMIT(return_value);
  1241. EMIT_ARG(label_assign, l_fail);
  1242. compile_node(comp, pns_test_if_else->nodes[1]); // failure value
  1243. } else {
  1244. compile_node(comp, pns->nodes[0]);
  1245. }
  1246. EMIT(return_value);
  1247. }
  1248. void compile_yield_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
  1249. compile_node(comp, pns->nodes[0]);
  1250. EMIT(pop_top);
  1251. }
  1252. void compile_raise_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
  1253. if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
  1254. // raise
  1255. EMIT_ARG(raise_varargs, 0);
  1256. } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_raise_stmt_arg)) {
  1257. // raise x from y
  1258. pns = (mp_parse_node_struct_t*)pns->nodes[0];
  1259. compile_node(comp, pns->nodes[0]);
  1260. compile_node(comp, pns->nodes[1]);
  1261. EMIT_ARG(raise_varargs, 2);
  1262. } else {
  1263. // raise x
  1264. compile_node(comp, pns->nodes[0]);
  1265. EMIT_ARG(raise_varargs, 1);
  1266. }
  1267. }
  1268. // q_base holds the base of the name
  1269. // eg a -> q_base=a
  1270. // a.b.c -> q_base=a
  1271. void do_import_name(compiler_t *comp, mp_parse_node_t pn, qstr *q_base) {
  1272. bool is_as = false;
  1273. if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dotted_as_name)) {
  1274. mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
  1275. // a name of the form x as y; unwrap it
  1276. *q_base = MP_PARSE_NODE_LEAF_ARG(pns->nodes[1]);
  1277. pn = pns->nodes[0];
  1278. is_as = true;
  1279. }
  1280. if (MP_PARSE_NODE_IS_NULL(pn)) {
  1281. // empty name (eg, from . import x)
  1282. *q_base = MP_QSTR_;
  1283. EMIT_ARG(import_name, MP_QSTR_); // import the empty string
  1284. } else if (MP_PARSE_NODE_IS_ID(pn)) {
  1285. // just a simple name
  1286. qstr q_full = MP_PARSE_NODE_LEAF_ARG(pn);
  1287. if (!is_as) {
  1288. *q_base = q_full;
  1289. }
  1290. EMIT_ARG(import_name, q_full);
  1291. } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
  1292. mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
  1293. if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dotted_name) {
  1294. // a name of the form a.b.c
  1295. if (!is_as) {
  1296. *q_base = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
  1297. }
  1298. int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
  1299. int len = n - 1;
  1300. for (int i = 0; i < n; i++) {
  1301. len += qstr_len(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
  1302. }
  1303. byte *q_ptr;
  1304. byte *str_dest = qstr_build_start(len, &q_ptr);
  1305. for (int i = 0; i < n; i++) {
  1306. if (i > 0) {
  1307. *str_dest++ = '.';
  1308. }
  1309. uint str_src_len;
  1310. const byte *str_src = qstr_data(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]), &str_src_len);
  1311. memcpy(str_dest, str_src, str_src_len);
  1312. str_dest += str_src_len;
  1313. }
  1314. qstr q_full = qstr_build_end(q_ptr);
  1315. EMIT_ARG(import_name, q_full);
  1316. if (is_as) {
  1317. for (int i = 1; i < n; i++) {
  1318. EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
  1319. }
  1320. }
  1321. } else {
  1322. // shouldn't happen
  1323. assert(0);
  1324. }
  1325. } else {
  1326. // shouldn't happen
  1327. assert(0);
  1328. }
  1329. }
  1330. void compile_dotted_as_name(compiler_t *comp, mp_parse_node_t pn) {
  1331. EMIT_ARG(load_const_small_int, 0); // level 0 import
  1332. EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE); // not importing from anything
  1333. qstr q_base;
  1334. do_import_name(comp, pn, &q_base);
  1335. EMIT_ARG(store_id, q_base);
  1336. }
  1337. void compile_import_name(compiler_t *comp, mp_parse_node_struct_t *pns) {
  1338. apply_to_single_or_list(comp, pns->nodes[0], PN_dotted_as_names, compile_dotted_as_name);
  1339. }
  1340. void compile_import_from(compiler_t *comp, mp_parse_node_struct_t *pns) {
  1341. mp_parse_node_t pn_import_source = pns->nodes[0];
  1342. // extract the preceeding .'s (if any) for a relative import, to compute the import level
  1343. uint import_level = 0;
  1344. do {
  1345. mp_parse_node_t pn_rel;
  1346. if (MP_PARSE_NODE_IS_TOKEN(pn_import_source) || MP_PARSE_NODE_IS_STRUCT_KIND(pn_import_source, PN_one_or_more_period_or_ellipsis)) {
  1347. // This covers relative imports with dots only like "from .. import"
  1348. pn_rel = pn_import_source;
  1349. pn_import_source = MP_PARSE_NODE_NULL;
  1350. } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_import_source, PN_import_from_2b)) {
  1351. // This covers relative imports starting with dot(s) like "from .foo import"
  1352. mp_parse_node_struct_t *pns_2b = (mp_parse_node_struct_t*)pn_import_source;
  1353. pn_rel = pns_2b->nodes[0];
  1354. pn_import_source = pns_2b->nodes[1];
  1355. assert(!MP_PARSE_NODE_IS_NULL(pn_import_source)); // should not be
  1356. } else {
  1357. // Not a relative import
  1358. break;
  1359. }
  1360. // get the list of . and/or ...'s
  1361. mp_parse_node_t *nodes;
  1362. int n = list_get(&pn_rel, PN_one_or_more_period_or_ellipsis, &nodes);
  1363. // count the total number of .'s
  1364. for (int i = 0; i < n; i++) {
  1365. if (MP_PARSE_NODE_IS_TOKEN_KIND(nodes[i], MP_TOKEN_DEL_PERIOD)) {
  1366. import_level++;
  1367. } else {
  1368. // should be an MP_TOKEN_ELLIPSIS
  1369. import_level += 3;
  1370. }
  1371. }
  1372. } while (0);
  1373. if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[1], MP_TOKEN_OP_STAR)) {
  1374. EMIT_ARG(load_const_small_int, import_level);
  1375. // build the "fromlist" tuple
  1376. #if MICROPY_EMIT_CPYTHON
  1377. EMIT_ARG(load_const_verbatim_str, "('*',)");
  1378. #else
  1379. EMIT_ARG(load_const_str, MP_QSTR__star_, false);
  1380. EMIT_ARG(build_tuple, 1);
  1381. #endif
  1382. // do the import
  1383. qstr dummy_q;
  1384. do_import_name(comp, pn_import_source, &dummy_q);
  1385. EMIT(import_star);
  1386. } else {
  1387. EMIT_ARG(load_const_small_int, import_level);
  1388. // build the "fromlist" tuple
  1389. mp_parse_node_t *pn_nodes;
  1390. int n = list_get(&pns->nodes[1], PN_import_as_names, &pn_nodes);
  1391. #if MICROPY_EMIT_CPYTHON
  1392. {
  1393. vstr_t *vstr = vstr_new();
  1394. vstr_printf(vstr, "(");
  1395. for (int i = 0; i < n; i++) {
  1396. assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
  1397. mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
  1398. qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
  1399. if (i > 0) {
  1400. vstr_printf(vstr, ", ");
  1401. }
  1402. vstr_printf(vstr, "'");
  1403. uint len;
  1404. const byte *str = qstr_data(id2, &len);
  1405. vstr_add_strn(vstr, (const char*)str, len);
  1406. vstr_printf(vstr, "'");
  1407. }
  1408. if (n == 1) {
  1409. vstr_printf(vstr, ",");
  1410. }
  1411. vstr_printf(vstr, ")");
  1412. EMIT_ARG(load_const_verbatim_str, vstr_str(vstr));
  1413. vstr_free(vstr);
  1414. }
  1415. #else
  1416. for (int i = 0; i < n; i++) {
  1417. assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
  1418. mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
  1419. qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
  1420. EMIT_ARG(load_const_str, id2, false);
  1421. }
  1422. EMIT_ARG(build_tuple, n);
  1423. #endif
  1424. // do the import
  1425. qstr dummy_q;
  1426. do_import_name(comp, pn_import_source, &dummy_q);
  1427. for (int i = 0; i < n; i++) {
  1428. assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_nodes[i], PN_import_as_name));
  1429. mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pn_nodes[i];
  1430. qstr id2 = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[0]); // should be id
  1431. EMIT_ARG(import_from, id2);
  1432. if (MP_PARSE_NODE_IS_NULL(pns3->nodes[1])) {
  1433. EMIT_ARG(store_id, id2);
  1434. } else {
  1435. EMIT_ARG(store_id, MP_PARSE_NODE_LEAF_ARG(pns3->nodes[1]));
  1436. }
  1437. }
  1438. EMIT(pop_top);
  1439. }
  1440. }
  1441. void compile_global_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
  1442. if (comp->pass == MP_PASS_SCOPE) {
  1443. if (MP_PARSE_NODE_IS_LEAF(pns->nodes[0])) {
  1444. scope_declare_global(comp->scope_cur, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]));
  1445. } else {
  1446. pns = (mp_parse_node_struct_t*)pns->nodes[0];
  1447. int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
  1448. for (int i = 0; i < num_nodes; i++) {
  1449. scope_declare_global(comp->scope_cur, MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
  1450. }
  1451. }
  1452. }
  1453. }
  1454. void compile_nonlocal_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
  1455. if (comp->pass == MP_PASS_SCOPE) {
  1456. if (MP_PARSE_NODE_IS_LEAF(pns->nodes[0])) {
  1457. scope_declare_nonlocal(comp->scope_cur, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]));
  1458. } else {
  1459. pns = (mp_parse_node_struct_t*)pns->nodes[0];
  1460. int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
  1461. for (int i = 0; i < num_nodes; i++) {
  1462. scope_declare_nonlocal(comp->scope_cur, MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
  1463. }
  1464. }
  1465. }
  1466. }
  1467. void compile_assert_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
  1468. uint l_end = comp_next_label(comp);
  1469. c_if_cond(comp, pns->nodes[0], true, l_end);
  1470. EMIT_ARG(load_global, MP_QSTR_AssertionError); // we load_global instead of load_id, to be consistent with CPython
  1471. if (!MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
  1472. // assertion message
  1473. compile_node(comp, pns->nodes[1]);
  1474. EMIT_ARG(call_function, 1, 0, 0);
  1475. }
  1476. EMIT_ARG(raise_varargs, 1);
  1477. EMIT_ARG(label_assign, l_end);
  1478. }
  1479. void compile_if_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
  1480. // TODO proper and/or short circuiting
  1481. uint l_end = comp_next_label(comp);
  1482. uint l_fail = comp_next_label(comp);
  1483. c_if_cond(comp, pns->nodes[0], false, l_fail); // if condition
  1484. compile_node(comp, pns->nodes[1]); // if block
  1485. if (
  1486. #if !MICROPY_EMIT_CPYTHON
  1487. // optimisation to not jump over non-existent elif/else blocks (this optimisation is not in CPython)
  1488. !(MP_PARSE_NODE_IS_NULL(pns->nodes[2]) && MP_PARSE_NODE_IS_NULL(pns->nodes[3])) &&
  1489. #endif
  1490. // optimisation to not jump if last instruction was return
  1491. !EMIT(last_emit_was_return_value)
  1492. ) {
  1493. // jump over elif/else blocks
  1494. EMIT_ARG(jump, l_end);
  1495. }
  1496. EMIT_ARG(label_assign, l_fail);
  1497. if (!MP_PARSE_NODE_IS_NULL(pns->nodes[2])) {
  1498. // compile elif blocks
  1499. mp_parse_node_struct_t *pns_elif = (mp_parse_node_struct_t*)pns->nodes[2];
  1500. if (MP_PARSE_NODE_STRUCT_KIND(pns_elif) == PN_if_stmt_elif_list) {
  1501. // multiple elif blocks
  1502. int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns_elif);
  1503. for (int i = 0; i < n; i++) {
  1504. mp_parse_node_struct_t *pns_elif2 = (mp_parse_node_struct_t*)pns_elif->nodes[i];
  1505. l_fail = comp_next_label(comp);
  1506. c_if_cond(comp, pns_elif2->nodes[0], false, l_fail); // elif condition
  1507. compile_node(comp, pns_elif2->nodes[1]); // elif block
  1508. if (!EMIT(last_emit_was_return_value)) { // simple optimisation to align with CPython
  1509. EMIT_ARG(jump, l_end);
  1510. }
  1511. EMIT_ARG(label_assign, l_fail);
  1512. }
  1513. } else {
  1514. // a single elif block
  1515. l_fail = comp_next_label(comp);
  1516. c_if_cond(comp, pns_elif->nodes[0], false, l_fail); // elif condition
  1517. compile_node(comp, pns_elif->nodes[1]); // elif block
  1518. if (!EMIT(last_emit_was_return_value)) { // simple optimisation to align with CPython
  1519. EMIT_ARG(jump, l_end);
  1520. }
  1521. EMIT_ARG(label_assign, l_fail);
  1522. }
  1523. }
  1524. // compile else block
  1525. compile_node(comp, pns->nodes[3]); // can be null
  1526. EMIT_ARG(label_assign, l_end);
  1527. }
  1528. #define START_BREAK_CONTINUE_BLOCK \
  1529. uint old_break_label = comp->break_label; \
  1530. uint old_continue_label = comp->continue_label; \
  1531. uint break_label = comp_next_label(comp); \
  1532. uint continue_label = comp_next_label(comp); \
  1533. comp->break_label = break_label; \
  1534. comp->continue_label = continue_label; \
  1535. comp->break_continue_except_level = comp->cur_except_level;
  1536. #define END_BREAK_CONTINUE_BLOCK \
  1537. comp->break_label = old_break_label; \
  1538. comp->continue_label = old_continue_label; \
  1539. comp->break_continue_except_level = comp->cur_except_level;
  1540. void compile_while_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
  1541. START_BREAK_CONTINUE_BLOCK
  1542. // compared to CPython, we have an optimised version of while loops
  1543. #if MICROPY_EMIT_CPYTHON
  1544. uint done_label = comp_next_label(comp);
  1545. EMIT_ARG(setup_loop, break_label);
  1546. EMIT_ARG(label_assign, continue_label);
  1547. c_if_cond(comp, pns->nodes[0], false, done_label); // condition
  1548. compile_node(comp, pns->nodes[1]); // body
  1549. if (!EMIT(last_emit_was_return_value)) {
  1550. EMIT_ARG(jump, continue_label);
  1551. }
  1552. EMIT_ARG(label_assign, done_label);
  1553. // CPython does not emit POP_BLOCK if the condition was a constant; don't undertand why
  1554. // this is a small hack to agree with CPython
  1555. if (!node_is_const_true(pns->nodes[0])) {
  1556. EMIT(pop_block);
  1557. }
  1558. #else
  1559. uint top_label = comp_next_label(comp);
  1560. EMIT_ARG(jump, continue_label);
  1561. EMIT_ARG(label_assign, top_label);
  1562. compile_node(comp, pns->nodes[1]); // body
  1563. EMIT_ARG(label_assign, continue_label);
  1564. c_if_cond(comp, pns->nodes[0], true, top_label); // condition
  1565. #endif
  1566. // break/continue apply to outer loop (if any) in the else block
  1567. END_BREAK_CONTINUE_BLOCK
  1568. compile_node(comp, pns->nodes[2]); // else
  1569. EMIT_ARG(label_assign, break_label);
  1570. }
  1571. // TODO preload end and step onto stack if they are not constants
  1572. // Note that, as per semantics of for .. range, the final failing value should not be stored in the loop variable
  1573. // And, if the loop never runs, the loop variable should never be assigned
  1574. void compile_for_stmt_optimised_range(compiler_t *comp, mp_parse_node_t pn_var, mp_parse_node_t pn_start, mp_parse_node_t pn_end, mp_parse_node_t pn_step, mp_parse_node_t pn_body, mp_parse_node_t pn_else) {
  1575. START_BREAK_CONTINUE_BLOCK
  1576. // note that we don't need to pop anything when breaking from an optimise for loop
  1577. uint top_label = comp_next_label(comp);
  1578. uint entry_label = comp_next_label(comp);
  1579. // compile: start, duplicated on stack
  1580. compile_node(comp, pn_start);
  1581. EMIT(dup_top);
  1582. EMIT_ARG(jump, entry_label);
  1583. EMIT_ARG(label_assign, top_label);
  1584. // at this point we actually have 1 less element on the stack
  1585. EMIT_ARG(adjust_stack_size, -1);
  1586. // store next value to var
  1587. c_assign(comp, pn_var, ASSIGN_STORE);
  1588. // compile body
  1589. compile_node(comp, pn_body);
  1590. EMIT_ARG(label_assign, continue_label);
  1591. // compile: var + step, duplicated on stack
  1592. compile_node(comp, pn_var);
  1593. compile_node(comp, pn_step);
  1594. EMIT_ARG(binary_op, MP_BINARY_OP_INPLACE_ADD);
  1595. EMIT(dup_top);
  1596. EMIT_ARG(label_assign, entry_label);
  1597. // compile: if var <cond> end: goto top
  1598. compile_node(comp, pn_end);
  1599. assert(MP_PARSE_NODE_IS_SMALL_INT(pn_step));
  1600. if (MP_PARSE_NODE_LEAF_SMALL_INT(pn_step) >= 0) {
  1601. EMIT_ARG(binary_op, MP_BINARY_OP_LESS);
  1602. } else {
  1603. EMIT_ARG(binary_op, MP_BINARY_OP_MORE);
  1604. }
  1605. EMIT_ARG(pop_jump_if_true, top_label);
  1606. // discard final value of var that failed the loop condition
  1607. EMIT(pop_top);
  1608. // break/continue apply to outer loop (if any) in the else block
  1609. END_BREAK_CONTINUE_BLOCK
  1610. compile_node(comp, pn_else);
  1611. EMIT_ARG(label_assign, break_label);
  1612. }
  1613. void compile_for_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
  1614. #if !MICROPY_EMIT_CPYTHON
  1615. // this bit optimises: for <x> in range(...), turning it into an explicitly incremented variable
  1616. // this is actually slower, but uses no heap memory
  1617. // for viper it will be much, much faster
  1618. if (/*comp->scope_cur->emit_options == MP_EMIT_OPT_VIPER &&*/ MP_PARSE_NODE_IS_ID(pns->nodes[0]) && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_power)) {
  1619. mp_parse_node_struct_t *pns_it = (mp_parse_node_struct_t*)pns->nodes[1];
  1620. if (MP_PARSE_NODE_IS_ID(pns_it->nodes[0])
  1621. && MP_PARSE_NODE_LEAF_ARG(pns_it->nodes[0]) == MP_QSTR_range
  1622. && MP_PARSE_NODE_IS_STRUCT_KIND(pns_it->nodes[1], PN_trailer_paren)
  1623. && MP_PARSE_NODE_IS_NULL(pns_it->nodes[2])) {
  1624. mp_parse_node_t pn_range_args = ((mp_parse_node_struct_t*)pns_it->nodes[1])->nodes[0];
  1625. mp_parse_node_t *args;
  1626. int n_args = list_get(&pn_range_args, PN_arglist, &args);
  1627. mp_parse_node_t pn_range_start;
  1628. mp_parse_node_t pn_range_end;
  1629. mp_parse_node_t pn_range_step;
  1630. bool optimize = false;
  1631. if (1 <= n_args && n_args <= 3) {
  1632. optimize = true;
  1633. if (n_args == 1) {
  1634. pn_range_start = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 0);
  1635. pn_range_end = args[0];
  1636. pn_range_step = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 1);
  1637. } else if (n_args == 2) {
  1638. pn_range_start = args[0];
  1639. pn_range_end = args[1];
  1640. pn_range_step = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, 1);
  1641. } else {
  1642. pn_range_start = args[0];
  1643. pn_range_end = args[1];
  1644. pn_range_step = args[2];
  1645. // We need to know sign of step. This is possible only if it's constant
  1646. if (!MP_PARSE_NODE_IS_SMALL_INT(pn_range_step)) {
  1647. optimize = false;
  1648. }
  1649. }
  1650. }
  1651. if (optimize) {
  1652. compile_for_stmt_optimised_range(comp, pns->nodes[0], pn_range_start, pn_range_end, pn_range_step, pns->nodes[2], pns->nodes[3]);
  1653. return;
  1654. }
  1655. }
  1656. }
  1657. #endif
  1658. START_BREAK_CONTINUE_BLOCK
  1659. comp->break_label |= MP_EMIT_BREAK_FROM_FOR;
  1660. uint pop_label = comp_next_label(comp);
  1661. uint end_label = comp_next_label(comp);
  1662. // I don't think our implementation needs SETUP_LOOP/POP_BLOCK for for-statements
  1663. #if MICROPY_EMIT_CPYTHON
  1664. EMIT_ARG(setup_loop, end_label);
  1665. #endif
  1666. compile_node(comp, pns->nodes[1]); // iterator
  1667. EMIT(get_iter);
  1668. EMIT_ARG(label_assign, continue_label);
  1669. EMIT_ARG(for_iter, pop_label);
  1670. c_assign(comp, pns->nodes[0], ASSIGN_STORE); // variable
  1671. compile_node(comp, pns->nodes[2]); // body
  1672. if (!EMIT(last_emit_was_return_value)) {
  1673. EMIT_ARG(jump, continue_label);
  1674. }
  1675. EMIT_ARG(label_assign, pop_label);
  1676. EMIT(for_iter_end);
  1677. // break/continue apply to outer loop (if any) in the else block
  1678. END_BREAK_CONTINUE_BLOCK
  1679. #if MICROPY_EMIT_CPYTHON
  1680. EMIT(pop_block);
  1681. #endif
  1682. compile_node(comp, pns->nodes[3]); // else (not tested)
  1683. EMIT_ARG(label_assign, break_label);
  1684. EMIT_ARG(label_assign, end_label);
  1685. }
  1686. void compile_try_except(compiler_t *comp, mp_parse_node_t pn_body, int n_except, mp_parse_node_t *pn_excepts, mp_parse_node_t pn_else) {
  1687. // setup code
  1688. uint l1 = comp_next_label(comp);
  1689. uint success_label = comp_next_label(comp);
  1690. EMIT_ARG(setup_except, l1);
  1691. compile_increase_except_level(comp);
  1692. compile_node(comp, pn_body); // body
  1693. EMIT(pop_block);
  1694. EMIT_ARG(jump, success_label); // jump over exception handler
  1695. EMIT_ARG(label_assign, l1); // start of exception handler
  1696. EMIT_ARG(adjust_stack_size, 6); // stack adjust for the 3 exception items, +3 for possible UNWIND_JUMP state
  1697. uint l2 = comp_next_label(comp);
  1698. for (int i = 0; i < n_except; i++) {
  1699. assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn_excepts[i], PN_try_stmt_except)); // should be
  1700. mp_parse_node_struct_t *pns_except = (mp_parse_node_struct_t*)pn_excepts[i];
  1701. qstr qstr_exception_local = 0;
  1702. uint end_finally_label = comp_next_label(comp);
  1703. if (MP_PARSE_NODE_IS_NULL(pns_except->nodes[0])) {
  1704. // this is a catch all exception handler
  1705. if (i + 1 != n_except) {
  1706. compile_syntax_error(comp, pn_excepts[i], "default 'except:' must be last");
  1707. return;
  1708. }
  1709. } else {
  1710. // this exception handler requires a match to a certain type of exception
  1711. mp_parse_node_t pns_exception_expr = pns_except->nodes[0];
  1712. if (MP_PARSE_NODE_IS_STRUCT(pns_exception_expr)) {
  1713. mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pns_exception_expr;
  1714. if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_try_stmt_as_name) {
  1715. // handler binds the exception to a local
  1716. pns_exception_expr = pns3->nodes[0];
  1717. qstr_exception_local = MP_PARSE_NODE_LEAF_ARG(pns3->nodes[1]);
  1718. }
  1719. }
  1720. EMIT(dup_top);
  1721. compile_node(comp, pns_exception_expr);
  1722. EMIT_ARG(binary_op, MP_BINARY_OP_EXCEPTION_MATCH);
  1723. EMIT_ARG(pop_jump_if_false, end_finally_label);
  1724. }
  1725. EMIT(pop_top);
  1726. if (qstr_exception_local == 0) {
  1727. EMIT(pop_top);
  1728. } else {
  1729. EMIT_ARG(store_id, qstr_exception_local);
  1730. }
  1731. EMIT(pop_top);
  1732. uint l3 = 0;
  1733. if (qstr_exception_local != 0) {
  1734. l3 = comp_next_label(comp);
  1735. EMIT_ARG(setup_finally, l3);
  1736. compile_increase_except_level(comp);
  1737. }
  1738. compile_node(comp, pns_except->nodes[1]);
  1739. if (qstr_exception_local != 0) {
  1740. EMIT(pop_block);
  1741. }
  1742. EMIT(pop_except);
  1743. if (qstr_exception_local != 0) {
  1744. EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
  1745. EMIT_ARG(label_assign, l3);
  1746. EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
  1747. EMIT_ARG(store_id, qstr_exception_local);
  1748. EMIT_ARG(delete_id, qstr_exception_local);
  1749. compile_decrease_except_level(comp);
  1750. EMIT(end_finally);
  1751. }
  1752. EMIT_ARG(jump, l2);
  1753. EMIT_ARG(label_assign, end_finally_label);
  1754. EMIT_ARG(adjust_stack_size, 3); // stack adjust for the 3 exception items
  1755. }
  1756. compile_decrease_except_level(comp);
  1757. EMIT(end_finally);
  1758. EMIT_ARG(adjust_stack_size, -5); // stack adjust
  1759. EMIT_ARG(label_assign, success_label);
  1760. compile_node(comp, pn_else); // else block, can be null
  1761. EMIT_ARG(label_assign, l2);
  1762. }
  1763. void compile_try_finally(compiler_t *comp, mp_parse_node_t pn_body, int n_except, mp_parse_node_t *pn_except, mp_parse_node_t pn_else, mp_parse_node_t pn_finally) {
  1764. uint l_finally_block = comp_next_label(comp);
  1765. EMIT_ARG(setup_finally, l_finally_block);
  1766. compile_increase_except_level(comp);
  1767. if (n_except == 0) {
  1768. assert(MP_PARSE_NODE_IS_NULL(pn_else));
  1769. EMIT_ARG(adjust_stack_size, 3); // stack adjust for possible UNWIND_JUMP state
  1770. compile_node(comp, pn_body);
  1771. EMIT_ARG(adjust_stack_size, -3);
  1772. } else {
  1773. compile_try_except(comp, pn_body, n_except, pn_except, pn_else);
  1774. }
  1775. EMIT(pop_block);
  1776. EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
  1777. EMIT_ARG(label_assign, l_finally_block);
  1778. compile_node(comp, pn_finally);
  1779. compile_decrease_except_level(comp);
  1780. EMIT(end_finally);
  1781. }
  1782. void compile_try_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
  1783. if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
  1784. mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
  1785. if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_finally) {
  1786. // just try-finally
  1787. compile_try_finally(comp, pns->nodes[0], 0, NULL, MP_PARSE_NODE_NULL, pns2->nodes[0]);
  1788. } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_try_stmt_except_and_more) {
  1789. // try-except and possibly else and/or finally
  1790. mp_parse_node_t *pn_excepts;
  1791. int n_except = list_get(&pns2->nodes[0], PN_try_stmt_except_list, &pn_excepts);
  1792. if (MP_PARSE_NODE_IS_NULL(pns2->nodes[2])) {
  1793. // no finally
  1794. compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, pns2->nodes[1]);
  1795. } else {
  1796. // have finally
  1797. compile_try_finally(comp, pns->nodes[0], n_except, pn_excepts, pns2->nodes[1], ((mp_parse_node_struct_t*)pns2->nodes[2])->nodes[0]);
  1798. }
  1799. } else {
  1800. // just try-except
  1801. mp_parse_node_t *pn_excepts;
  1802. int n_except = list_get(&pns->nodes[1], PN_try_stmt_except_list, &pn_excepts);
  1803. compile_try_except(comp, pns->nodes[0], n_except, pn_excepts, MP_PARSE_NODE_NULL);
  1804. }
  1805. } else {
  1806. // shouldn't happen
  1807. assert(0);
  1808. }
  1809. }
  1810. void compile_with_stmt_helper(compiler_t *comp, int n, mp_parse_node_t *nodes, mp_parse_node_t body) {
  1811. if (n == 0) {
  1812. // no more pre-bits, compile the body of the with
  1813. compile_node(comp, body);
  1814. } else {
  1815. uint l_end = comp_next_label(comp);
  1816. if (MP_PARSE_NODE_IS_STRUCT_KIND(nodes[0], PN_with_item)) {
  1817. // this pre-bit is of the form "a as b"
  1818. mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)nodes[0];
  1819. compile_node(comp, pns->nodes[0]);
  1820. EMIT_ARG(setup_with, l_end);
  1821. c_assign(comp, pns->nodes[1], ASSIGN_STORE);
  1822. } else {
  1823. // this pre-bit is just an expression
  1824. compile_node(comp, nodes[0]);
  1825. EMIT_ARG(setup_with, l_end);
  1826. EMIT(pop_top);
  1827. }
  1828. compile_increase_except_level(comp);
  1829. // compile additional pre-bits and the body
  1830. compile_with_stmt_helper(comp, n - 1, nodes + 1, body);
  1831. // finish this with block
  1832. EMIT(pop_block);
  1833. EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
  1834. EMIT_ARG(label_assign, l_end);
  1835. EMIT(with_cleanup);
  1836. compile_decrease_except_level(comp);
  1837. EMIT(end_finally);
  1838. }
  1839. }
  1840. void compile_with_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
  1841. // get the nodes for the pre-bit of the with (the a as b, c as d, ... bit)
  1842. mp_parse_node_t *nodes;
  1843. int n = list_get(&pns->nodes[0], PN_with_stmt_list, &nodes);
  1844. assert(n > 0);
  1845. // compile in a nested fashion
  1846. compile_with_stmt_helper(comp, n, nodes, pns->nodes[1]);
  1847. }
  1848. void compile_expr_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
  1849. if (MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
  1850. if (comp->is_repl && comp->scope_cur->kind == SCOPE_MODULE) {
  1851. // for REPL, evaluate then print the expression
  1852. EMIT_ARG(load_id, MP_QSTR___repl_print__);
  1853. compile_node(comp, pns->nodes[0]);
  1854. EMIT_ARG(call_function, 1, 0, 0);
  1855. EMIT(pop_top);
  1856. } else {
  1857. // for non-REPL, evaluate then discard the expression
  1858. if ((MP_PARSE_NODE_IS_LEAF(pns->nodes[0]) && !MP_PARSE_NODE_IS_ID(pns->nodes[0]))
  1859. || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_string)) {
  1860. // do nothing with a lonely constant
  1861. } else {
  1862. compile_node(comp, pns->nodes[0]); // just an expression
  1863. EMIT(pop_top); // discard last result since this is a statement and leaves nothing on the stack
  1864. }
  1865. }
  1866. } else {
  1867. mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
  1868. int kind = MP_PARSE_NODE_STRUCT_KIND(pns1);
  1869. if (kind == PN_expr_stmt_augassign) {
  1870. c_assign(comp, pns->nodes[0], ASSIGN_AUG_LOAD); // lhs load for aug assign
  1871. compile_node(comp, pns1->nodes[1]); // rhs
  1872. assert(MP_PARSE_NODE_IS_TOKEN(pns1->nodes[0]));
  1873. mp_binary_op_t op;
  1874. switch (MP_PARSE_NODE_LEAF_ARG(pns1->nodes[0])) {
  1875. case MP_TOKEN_DEL_PIPE_EQUAL: op = MP_BINARY_OP_INPLACE_OR; break;
  1876. case MP_TOKEN_DEL_CARET_EQUAL: op = MP_BINARY_OP_INPLACE_XOR; break;
  1877. case MP_TOKEN_DEL_AMPERSAND_EQUAL: op = MP_BINARY_OP_INPLACE_AND; break;
  1878. case MP_TOKEN_DEL_DBL_LESS_EQUAL: op = MP_BINARY_OP_INPLACE_LSHIFT; break;
  1879. case MP_TOKEN_DEL_DBL_MORE_EQUAL: op = MP_BINARY_OP_INPLACE_RSHIFT; break;
  1880. case MP_TOKEN_DEL_PLUS_EQUAL: op = MP_BINARY_OP_INPLACE_ADD; break;
  1881. case MP_TOKEN_DEL_MINUS_EQUAL: op = MP_BINARY_OP_INPLACE_SUBTRACT; break;
  1882. case MP_TOKEN_DEL_STAR_EQUAL: op = MP_BINARY_OP_INPLACE_MULTIPLY; break;
  1883. case MP_TOKEN_DEL_DBL_SLASH_EQUAL: op = MP_BINARY_OP_INPLACE_FLOOR_DIVIDE; break;
  1884. case MP_TOKEN_DEL_SLASH_EQUAL: op = MP_BINARY_OP_INPLACE_TRUE_DIVIDE; break;
  1885. case MP_TOKEN_DEL_PERCENT_EQUAL: op = MP_BINARY_OP_INPLACE_MODULO; break;
  1886. case MP_TOKEN_DEL_DBL_STAR_EQUAL: op = MP_BINARY_OP_INPLACE_POWER; break;
  1887. default: assert(0); op = MP_BINARY_OP_INPLACE_OR; // shouldn't happen
  1888. }
  1889. EMIT_ARG(binary_op, op);
  1890. c_assign(comp, pns->nodes[0], ASSIGN_AUG_STORE); // lhs store for aug assign
  1891. } else if (kind == PN_expr_stmt_assign_list) {
  1892. int rhs = MP_PARSE_NODE_STRUCT_NUM_NODES(pns1) - 1;
  1893. compile_node(comp, ((mp_parse_node_struct_t*)pns1->nodes[rhs])->nodes[0]); // rhs
  1894. // following CPython, we store left-most first
  1895. if (rhs > 0) {
  1896. EMIT(dup_top);
  1897. }
  1898. c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store
  1899. for (int i = 0; i < rhs; i++) {
  1900. if (i + 1 < rhs) {
  1901. EMIT(dup_top);
  1902. }
  1903. c_assign(comp, ((mp_parse_node_struct_t*)pns1->nodes[i])->nodes[0], ASSIGN_STORE); // middle store
  1904. }
  1905. } else if (kind == PN_expr_stmt_assign) {
  1906. if (MP_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_testlist_star_expr)
  1907. && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr)
  1908. && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns1->nodes[0]) == 2
  1909. && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[0]) == 2) {
  1910. // optimisation for a, b = c, d; to match CPython's optimisation
  1911. mp_parse_node_struct_t* pns10 = (mp_parse_node_struct_t*)pns1->nodes[0];
  1912. mp_parse_node_struct_t* pns0 = (mp_parse_node_struct_t*)pns->nodes[0];
  1913. if (MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[0], PN_star_expr)
  1914. || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[1], PN_star_expr)) {
  1915. // can't optimise when it's a star expression on the lhs
  1916. goto no_optimisation;
  1917. }
  1918. compile_node(comp, pns10->nodes[0]); // rhs
  1919. compile_node(comp, pns10->nodes[1]); // rhs
  1920. EMIT(rot_two);
  1921. c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store
  1922. c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store
  1923. } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns1->nodes[0], PN_testlist_star_expr)
  1924. && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_star_expr)
  1925. && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns1->nodes[0]) == 3
  1926. && MP_PARSE_NODE_STRUCT_NUM_NODES((mp_parse_node_struct_t*)pns->nodes[0]) == 3) {
  1927. // optimisation for a, b, c = d, e, f; to match CPython's optimisation
  1928. mp_parse_node_struct_t* pns10 = (mp_parse_node_struct_t*)pns1->nodes[0];
  1929. mp_parse_node_struct_t* pns0 = (mp_parse_node_struct_t*)pns->nodes[0];
  1930. if (MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[0], PN_star_expr)
  1931. || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[1], PN_star_expr)
  1932. || MP_PARSE_NODE_IS_STRUCT_KIND(pns0->nodes[2], PN_star_expr)) {
  1933. // can't optimise when it's a star expression on the lhs
  1934. goto no_optimisation;
  1935. }
  1936. compile_node(comp, pns10->nodes[0]); // rhs
  1937. compile_node(comp, pns10->nodes[1]); // rhs
  1938. compile_node(comp, pns10->nodes[2]); // rhs
  1939. EMIT(rot_three);
  1940. EMIT(rot_two);
  1941. c_assign(comp, pns0->nodes[0], ASSIGN_STORE); // lhs store
  1942. c_assign(comp, pns0->nodes[1], ASSIGN_STORE); // lhs store
  1943. c_assign(comp, pns0->nodes[2], ASSIGN_STORE); // lhs store
  1944. } else {
  1945. no_optimisation:
  1946. compile_node(comp, pns1->nodes[0]); // rhs
  1947. c_assign(comp, pns->nodes[0], ASSIGN_STORE); // lhs store
  1948. }
  1949. } else {
  1950. // shouldn't happen
  1951. assert(0);
  1952. }
  1953. }
  1954. }
  1955. void c_binary_op(compiler_t *comp, mp_parse_node_struct_t *pns, mp_binary_op_t binary_op) {
  1956. int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
  1957. compile_node(comp, pns->nodes[0]);
  1958. for (int i = 1; i < num_nodes; i += 1) {
  1959. compile_node(comp, pns->nodes[i]);
  1960. EMIT_ARG(binary_op, binary_op);
  1961. }
  1962. }
  1963. void compile_test_if_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
  1964. assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_test_if_else));
  1965. mp_parse_node_struct_t *pns_test_if_else = (mp_parse_node_struct_t*)pns->nodes[1];
  1966. uint l_fail = comp_next_label(comp);
  1967. uint l_end = comp_next_label(comp);
  1968. c_if_cond(comp, pns_test_if_else->nodes[0], false, l_fail); // condition
  1969. compile_node(comp, pns->nodes[0]); // success value
  1970. EMIT_ARG(jump, l_end);
  1971. EMIT_ARG(label_assign, l_fail);
  1972. EMIT_ARG(adjust_stack_size, -1); // adjust stack size
  1973. compile_node(comp, pns_test_if_else->nodes[1]); // failure value
  1974. EMIT_ARG(label_assign, l_end);
  1975. }
  1976. void compile_lambdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
  1977. // TODO default params etc for lambda; possibly just use funcdef code
  1978. //mp_parse_node_t pn_params = pns->nodes[0];
  1979. //mp_parse_node_t pn_body = pns->nodes[1];
  1980. if (comp->pass == MP_PASS_SCOPE) {
  1981. // create a new scope for this lambda
  1982. scope_t *s = scope_new_and_link(comp, SCOPE_LAMBDA, (mp_parse_node_t)pns, comp->scope_cur->emit_options);
  1983. // store the lambda scope so the compiling function (this one) can use it at each pass
  1984. pns->nodes[2] = (mp_parse_node_t)s;
  1985. }
  1986. // get the scope for this lambda
  1987. scope_t *this_scope = (scope_t*)pns->nodes[2];
  1988. // make the lambda
  1989. close_over_variables_etc(comp, this_scope, 0, 0);
  1990. }
  1991. void compile_or_test(compiler_t *comp, mp_parse_node_struct_t *pns) {
  1992. uint l_end = comp_next_label(comp);
  1993. int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
  1994. for (int i = 0; i < n; i += 1) {
  1995. compile_node(comp, pns->nodes[i]);
  1996. if (i + 1 < n) {
  1997. EMIT_ARG(jump_if_true_or_pop, l_end);
  1998. }
  1999. }
  2000. EMIT_ARG(label_assign, l_end);
  2001. }
  2002. void compile_and_test(compiler_t *comp, mp_parse_node_struct_t *pns) {
  2003. uint l_end = comp_next_label(comp);
  2004. int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
  2005. for (int i = 0; i < n; i += 1) {
  2006. compile_node(comp, pns->nodes[i]);
  2007. if (i + 1 < n) {
  2008. EMIT_ARG(jump_if_false_or_pop, l_end);
  2009. }
  2010. }
  2011. EMIT_ARG(label_assign, l_end);
  2012. }
  2013. void compile_not_test_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
  2014. compile_node(comp, pns->nodes[0]);
  2015. EMIT_ARG(unary_op, MP_UNARY_OP_NOT);
  2016. }
  2017. void compile_comparison(compiler_t *comp, mp_parse_node_struct_t *pns) {
  2018. int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
  2019. compile_node(comp, pns->nodes[0]);
  2020. bool multi = (num_nodes > 3);
  2021. uint l_fail = 0;
  2022. if (multi) {
  2023. l_fail = comp_next_label(comp);
  2024. }
  2025. for (int i = 1; i + 1 < num_nodes; i += 2) {
  2026. compile_node(comp, pns->nodes[i + 1]);
  2027. if (i + 2 < num_nodes) {
  2028. EMIT(dup_top);
  2029. EMIT(rot_three);
  2030. }
  2031. if (MP_PARSE_NODE_IS_TOKEN(pns->nodes[i])) {
  2032. mp_binary_op_t op;
  2033. switch (MP_PARSE_NODE_LEAF_ARG(pns->nodes[i])) {
  2034. case MP_TOKEN_OP_LESS: op = MP_BINARY_OP_LESS; break;
  2035. case MP_TOKEN_OP_MORE: op = MP_BINARY_OP_MORE; break;
  2036. case MP_TOKEN_OP_DBL_EQUAL: op = MP_BINARY_OP_EQUAL; break;
  2037. case MP_TOKEN_OP_LESS_EQUAL: op = MP_BINARY_OP_LESS_EQUAL; break;
  2038. case MP_TOKEN_OP_MORE_EQUAL: op = MP_BINARY_OP_MORE_EQUAL; break;
  2039. case MP_TOKEN_OP_NOT_EQUAL: op = MP_BINARY_OP_NOT_EQUAL; break;
  2040. case MP_TOKEN_KW_IN: op = MP_BINARY_OP_IN; break;
  2041. default: assert(0); op = MP_BINARY_OP_LESS; // shouldn't happen
  2042. }
  2043. EMIT_ARG(binary_op, op);
  2044. } else if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[i])) {
  2045. mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[i];
  2046. int kind = MP_PARSE_NODE_STRUCT_KIND(pns2);
  2047. if (kind == PN_comp_op_not_in) {
  2048. EMIT_ARG(binary_op, MP_BINARY_OP_NOT_IN);
  2049. } else if (kind == PN_comp_op_is) {
  2050. if (MP_PARSE_NODE_IS_NULL(pns2->nodes[0])) {
  2051. EMIT_ARG(binary_op, MP_BINARY_OP_IS);
  2052. } else {
  2053. EMIT_ARG(binary_op, MP_BINARY_OP_IS_NOT);
  2054. }
  2055. } else {
  2056. // shouldn't happen
  2057. assert(0);
  2058. }
  2059. } else {
  2060. // shouldn't happen
  2061. assert(0);
  2062. }
  2063. if (i + 2 < num_nodes) {
  2064. EMIT_ARG(jump_if_false_or_pop, l_fail);
  2065. }
  2066. }
  2067. if (multi) {
  2068. uint l_end = comp_next_label(comp);
  2069. EMIT_ARG(jump, l_end);
  2070. EMIT_ARG(label_assign, l_fail);
  2071. EMIT_ARG(adjust_stack_size, 1);
  2072. EMIT(rot_two);
  2073. EMIT(pop_top);
  2074. EMIT_ARG(label_assign, l_end);
  2075. }
  2076. }
  2077. void compile_star_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
  2078. compile_syntax_error(comp, (mp_parse_node_t)pns, "*x must be assignment target");
  2079. }
  2080. void compile_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
  2081. c_binary_op(comp, pns, MP_BINARY_OP_OR);
  2082. }
  2083. void compile_xor_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
  2084. c_binary_op(comp, pns, MP_BINARY_OP_XOR);
  2085. }
  2086. void compile_and_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
  2087. c_binary_op(comp, pns, MP_BINARY_OP_AND);
  2088. }
  2089. void compile_shift_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
  2090. int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
  2091. compile_node(comp, pns->nodes[0]);
  2092. for (int i = 1; i + 1 < num_nodes; i += 2) {
  2093. compile_node(comp, pns->nodes[i + 1]);
  2094. if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_LESS)) {
  2095. EMIT_ARG(binary_op, MP_BINARY_OP_LSHIFT);
  2096. } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_MORE)) {
  2097. EMIT_ARG(binary_op, MP_BINARY_OP_RSHIFT);
  2098. } else {
  2099. // shouldn't happen
  2100. assert(0);
  2101. }
  2102. }
  2103. }
  2104. void compile_arith_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
  2105. int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
  2106. compile_node(comp, pns->nodes[0]);
  2107. for (int i = 1; i + 1 < num_nodes; i += 2) {
  2108. compile_node(comp, pns->nodes[i + 1]);
  2109. if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PLUS)) {
  2110. EMIT_ARG(binary_op, MP_BINARY_OP_ADD);
  2111. } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_MINUS)) {
  2112. EMIT_ARG(binary_op, MP_BINARY_OP_SUBTRACT);
  2113. } else {
  2114. // shouldn't happen
  2115. assert(0);
  2116. }
  2117. }
  2118. }
  2119. void compile_term(compiler_t *comp, mp_parse_node_struct_t *pns) {
  2120. int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
  2121. compile_node(comp, pns->nodes[0]);
  2122. for (int i = 1; i + 1 < num_nodes; i += 2) {
  2123. compile_node(comp, pns->nodes[i + 1]);
  2124. if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_STAR)) {
  2125. EMIT_ARG(binary_op, MP_BINARY_OP_MULTIPLY);
  2126. } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_DBL_SLASH)) {
  2127. EMIT_ARG(binary_op, MP_BINARY_OP_FLOOR_DIVIDE);
  2128. } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_SLASH)) {
  2129. EMIT_ARG(binary_op, MP_BINARY_OP_TRUE_DIVIDE);
  2130. } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[i], MP_TOKEN_OP_PERCENT)) {
  2131. EMIT_ARG(binary_op, MP_BINARY_OP_MODULO);
  2132. } else {
  2133. // shouldn't happen
  2134. assert(0);
  2135. }
  2136. }
  2137. }
  2138. void compile_factor_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
  2139. compile_node(comp, pns->nodes[1]);
  2140. if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_PLUS)) {
  2141. EMIT_ARG(unary_op, MP_UNARY_OP_POSITIVE);
  2142. } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_MINUS)) {
  2143. EMIT_ARG(unary_op, MP_UNARY_OP_NEGATIVE);
  2144. } else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_TILDE)) {
  2145. EMIT_ARG(unary_op, MP_UNARY_OP_INVERT);
  2146. } else {
  2147. // shouldn't happen
  2148. assert(0);
  2149. }
  2150. }
  2151. void compile_power(compiler_t *comp, mp_parse_node_struct_t *pns) {
  2152. // this is to handle special super() call
  2153. comp->func_arg_is_super = MP_PARSE_NODE_IS_ID(pns->nodes[0]) && MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]) == MP_QSTR_super;
  2154. compile_generic_all_nodes(comp, pns);
  2155. }
  2156. STATIC void compile_trailer_paren_helper(compiler_t *comp, mp_parse_node_t pn_arglist, bool is_method_call, int n_positional_extra) {
  2157. // function to call is on top of stack
  2158. #if !MICROPY_EMIT_CPYTHON
  2159. // this is to handle special super() call
  2160. if (MP_PARSE_NODE_IS_NULL(pn_arglist) && comp->func_arg_is_super && comp->scope_cur->kind == SCOPE_FUNCTION) {
  2161. EMIT_ARG(load_id, MP_QSTR___class__);
  2162. // get first argument to function
  2163. bool found = false;
  2164. for (int i = 0; i < comp->scope_cur->id_info_len; i++) {
  2165. if (comp->scope_cur->id_info[i].flags & ID_FLAG_IS_PARAM) {
  2166. EMIT_ARG(load_fast, MP_QSTR_, comp->scope_cur->id_info[i].flags, comp->scope_cur->id_info[i].local_num);
  2167. found = true;
  2168. break;
  2169. }
  2170. }
  2171. if (!found) {
  2172. printf("TypeError: super() call cannot find self\n");
  2173. return;
  2174. }
  2175. EMIT_ARG(call_function, 2, 0, 0);
  2176. return;
  2177. }
  2178. #endif
  2179. // get the list of arguments
  2180. mp_parse_node_t *args;
  2181. int n_args = list_get(&pn_arglist, PN_arglist, &args);
  2182. // compile the arguments
  2183. // Rather than calling compile_node on the list, we go through the list of args
  2184. // explicitly here so that we can count the number of arguments and give sensible
  2185. // error messages.
  2186. int n_positional = n_positional_extra;
  2187. uint n_keyword = 0;
  2188. uint star_flags = 0;
  2189. for (int i = 0; i < n_args; i++) {
  2190. if (MP_PARSE_NODE_IS_STRUCT(args[i])) {
  2191. mp_parse_node_struct_t *pns_arg = (mp_parse_node_struct_t*)args[i];
  2192. if (MP_PARSE_NODE_STRUCT_KIND(pns_arg) == PN_arglist_star) {
  2193. if (star_flags & MP_EMIT_STAR_FLAG_SINGLE) {
  2194. compile_syntax_error(comp, (mp_parse_node_t)pns_arg, "can't have multiple *x");
  2195. return;
  2196. }
  2197. star_flags |= MP_EMIT_STAR_FLAG_SINGLE;
  2198. compile_node(comp, pns_arg->nodes[0]);
  2199. } else if (MP_PARSE_NODE_STRUCT_KIND(pns_arg) == PN_arglist_dbl_star) {
  2200. if (star_flags & MP_EMIT_STAR_FLAG_DOUBLE) {
  2201. compile_syntax_error(comp, (mp_parse_node_t)pns_arg, "can't have multiple **x");
  2202. return;
  2203. }
  2204. star_flags |= MP_EMIT_STAR_FLAG_DOUBLE;
  2205. compile_node(comp, pns_arg->nodes[0]);
  2206. } else if (MP_PARSE_NODE_STRUCT_KIND(pns_arg) == PN_argument) {
  2207. assert(MP_PARSE_NODE_IS_STRUCT(pns_arg->nodes[1])); // should always be
  2208. mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns_arg->nodes[1];
  2209. if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_argument_3) {
  2210. if (!MP_PARSE_NODE_IS_ID(pns_arg->nodes[0])) {
  2211. compile_syntax_error(comp, (mp_parse_node_t)pns_arg, "LHS of keyword arg must be an id");
  2212. return;
  2213. }
  2214. EMIT_ARG(load_const_str, MP_PARSE_NODE_LEAF_ARG(pns_arg->nodes[0]), false);
  2215. compile_node(comp, pns2->nodes[0]);
  2216. n_keyword += 1;
  2217. } else {
  2218. assert(MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for); // should always be
  2219. compile_comprehension(comp, pns_arg, SCOPE_GEN_EXPR);
  2220. n_positional++;
  2221. }
  2222. } else {
  2223. goto normal_argument;
  2224. }
  2225. } else {
  2226. normal_argument:
  2227. if (n_keyword > 0) {
  2228. compile_syntax_error(comp, args[i], "non-keyword arg after keyword arg");
  2229. return;
  2230. }
  2231. compile_node(comp, args[i]);
  2232. n_positional++;
  2233. }
  2234. }
  2235. // emit the function/method call
  2236. if (is_method_call) {
  2237. EMIT_ARG(call_method, n_positional, n_keyword, star_flags);
  2238. } else {
  2239. EMIT_ARG(call_function, n_positional, n_keyword, star_flags);
  2240. }
  2241. }
  2242. void compile_power_trailers(compiler_t *comp, mp_parse_node_struct_t *pns) {
  2243. int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
  2244. for (int i = 0; i < num_nodes; i++) {
  2245. if (i + 1 < num_nodes && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[i], PN_trailer_period) && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[i + 1], PN_trailer_paren)) {
  2246. // optimisation for method calls a.f(...), following PyPy
  2247. mp_parse_node_struct_t *pns_period = (mp_parse_node_struct_t*)pns->nodes[i];
  2248. mp_parse_node_struct_t *pns_paren = (mp_parse_node_struct_t*)pns->nodes[i + 1];
  2249. EMIT_ARG(load_method, MP_PARSE_NODE_LEAF_ARG(pns_period->nodes[0])); // get the method
  2250. compile_trailer_paren_helper(comp, pns_paren->nodes[0], true, 0);
  2251. i += 1;
  2252. } else {
  2253. compile_node(comp, pns->nodes[i]);
  2254. }
  2255. comp->func_arg_is_super = false;
  2256. }
  2257. }
  2258. void compile_power_dbl_star(compiler_t *comp, mp_parse_node_struct_t *pns) {
  2259. compile_node(comp, pns->nodes[0]);
  2260. EMIT_ARG(binary_op, MP_BINARY_OP_POWER);
  2261. }
  2262. void compile_atom_string(compiler_t *comp, mp_parse_node_struct_t *pns) {
  2263. // a list of strings
  2264. // check type of list (string or bytes) and count total number of bytes
  2265. int n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
  2266. int n_bytes = 0;
  2267. int string_kind = MP_PARSE_NODE_NULL;
  2268. for (int i = 0; i < n; i++) {
  2269. int pn_kind;
  2270. if (MP_PARSE_NODE_IS_LEAF(pns->nodes[i])) {
  2271. pn_kind = MP_PARSE_NODE_LEAF_KIND(pns->nodes[i]);
  2272. assert(pn_kind == MP_PARSE_NODE_STRING || pn_kind == MP_PARSE_NODE_BYTES);
  2273. n_bytes += qstr_len(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]));
  2274. } else {
  2275. assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[i]));
  2276. mp_parse_node_struct_t *pns_string = (mp_parse_node_struct_t*)pns->nodes[i];
  2277. assert(MP_PARSE_NODE_STRUCT_KIND(pns_string) == PN_string);
  2278. pn_kind = MP_PARSE_NODE_STRING;
  2279. n_bytes += (machine_uint_t)pns_string->nodes[1];
  2280. }
  2281. if (i == 0) {
  2282. string_kind = pn_kind;
  2283. } else if (pn_kind != string_kind) {
  2284. compile_syntax_error(comp, (mp_parse_node_t)pns, "cannot mix bytes and nonbytes literals");
  2285. return;
  2286. }
  2287. }
  2288. // concatenate string/bytes
  2289. byte *q_ptr;
  2290. byte *s_dest = qstr_build_start(n_bytes, &q_ptr);
  2291. for (int i = 0; i < n; i++) {
  2292. if (MP_PARSE_NODE_IS_LEAF(pns->nodes[i])) {
  2293. uint s_len;
  2294. const byte *s = qstr_data(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]), &s_len);
  2295. memcpy(s_dest, s, s_len);
  2296. s_dest += s_len;
  2297. } else {
  2298. mp_parse_node_struct_t *pns_string = (mp_parse_node_struct_t*)pns->nodes[i];
  2299. memcpy(s_dest, (const char*)pns_string->nodes[0], (machine_uint_t)pns_string->nodes[1]);
  2300. s_dest += (machine_uint_t)pns_string->nodes[1];
  2301. }
  2302. }
  2303. qstr q = qstr_build_end(q_ptr);
  2304. EMIT_ARG(load_const_str, q, string_kind == MP_PARSE_NODE_BYTES);
  2305. }
  2306. // pns needs to have 2 nodes, first is lhs of comprehension, second is PN_comp_for node
  2307. void compile_comprehension(compiler_t *comp, mp_parse_node_struct_t *pns, scope_kind_t kind) {
  2308. assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
  2309. assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
  2310. mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t*)pns->nodes[1];
  2311. if (comp->pass == MP_PASS_SCOPE) {
  2312. // create a new scope for this comprehension
  2313. scope_t *s = scope_new_and_link(comp, kind, (mp_parse_node_t)pns, comp->scope_cur->emit_options);
  2314. // store the comprehension scope so the compiling function (this one) can use it at each pass
  2315. pns_comp_for->nodes[3] = (mp_parse_node_t)s;
  2316. }
  2317. // get the scope for this comprehension
  2318. scope_t *this_scope = (scope_t*)pns_comp_for->nodes[3];
  2319. // compile the comprehension
  2320. close_over_variables_etc(comp, this_scope, 0, 0);
  2321. compile_node(comp, pns_comp_for->nodes[1]); // source of the iterator
  2322. EMIT(get_iter);
  2323. EMIT_ARG(call_function, 1, 0, 0);
  2324. }
  2325. void compile_atom_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
  2326. if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
  2327. // an empty tuple
  2328. c_tuple(comp, MP_PARSE_NODE_NULL, NULL);
  2329. } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
  2330. pns = (mp_parse_node_struct_t*)pns->nodes[0];
  2331. assert(!MP_PARSE_NODE_IS_NULL(pns->nodes[1]));
  2332. if (MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])) {
  2333. mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[1];
  2334. if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3b) {
  2335. // tuple of one item, with trailing comma
  2336. assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[0]));
  2337. c_tuple(comp, pns->nodes[0], NULL);
  2338. } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_testlist_comp_3c) {
  2339. // tuple of many items
  2340. c_tuple(comp, pns->nodes[0], pns2);
  2341. } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_comp_for) {
  2342. // generator expression
  2343. compile_comprehension(comp, pns, SCOPE_GEN_EXPR);
  2344. } else {
  2345. // tuple with 2 items
  2346. goto tuple_with_2_items;
  2347. }
  2348. } else {
  2349. // tuple with 2 items
  2350. tuple_with_2_items:
  2351. c_tuple(comp, MP_PARSE_NODE_NULL, pns);
  2352. }
  2353. } else {
  2354. // parenthesis around a single item, is just that item
  2355. compile_node(comp, pns->nodes[0]);
  2356. }
  2357. }
  2358. void compile_atom_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
  2359. if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
  2360. // empty list
  2361. EMIT_ARG(build_list, 0);
  2362. } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
  2363. mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)pns->nodes[0];
  2364. if (MP_PARSE_NODE_IS_STRUCT(pns2->nodes[1])) {
  2365. mp_parse_node_struct_t *pns3 = (mp_parse_node_struct_t*)pns2->nodes[1];
  2366. if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3b) {
  2367. // list of one item, with trailing comma
  2368. assert(MP_PARSE_NODE_IS_NULL(pns3->nodes[0]));
  2369. compile_node(comp, pns2->nodes[0]);
  2370. EMIT_ARG(build_list, 1);
  2371. } else if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_testlist_comp_3c) {
  2372. // list of many items
  2373. compile_node(comp, pns2->nodes[0]);
  2374. compile_generic_all_nodes(comp, pns3);
  2375. EMIT_ARG(build_list, 1 + MP_PARSE_NODE_STRUCT_NUM_NODES(pns3));
  2376. } else if (MP_PARSE_NODE_STRUCT_KIND(pns3) == PN_comp_for) {
  2377. // list comprehension
  2378. compile_comprehension(comp, pns2, SCOPE_LIST_COMP);
  2379. } else {
  2380. // list with 2 items
  2381. goto list_with_2_items;
  2382. }
  2383. } else {
  2384. // list with 2 items
  2385. list_with_2_items:
  2386. compile_node(comp, pns2->nodes[0]);
  2387. compile_node(comp, pns2->nodes[1]);
  2388. EMIT_ARG(build_list, 2);
  2389. }
  2390. } else {
  2391. // list with 1 item
  2392. compile_node(comp, pns->nodes[0]);
  2393. EMIT_ARG(build_list, 1);
  2394. }
  2395. }
  2396. void compile_atom_brace(compiler_t *comp, mp_parse_node_struct_t *pns) {
  2397. mp_parse_node_t pn = pns->nodes[0];
  2398. if (MP_PARSE_NODE_IS_NULL(pn)) {
  2399. // empty dict
  2400. EMIT_ARG(build_map, 0);
  2401. } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
  2402. pns = (mp_parse_node_struct_t*)pn;
  2403. if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker_item) {
  2404. // dict with one element
  2405. EMIT_ARG(build_map, 1);
  2406. compile_node(comp, pn);
  2407. EMIT(store_map);
  2408. } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_dictorsetmaker) {
  2409. assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should succeed
  2410. mp_parse_node_struct_t *pns1 = (mp_parse_node_struct_t*)pns->nodes[1];
  2411. if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_dictorsetmaker_list) {
  2412. // dict/set with multiple elements
  2413. // get tail elements (2nd, 3rd, ...)
  2414. mp_parse_node_t *nodes;
  2415. int n = list_get(&pns1->nodes[0], PN_dictorsetmaker_list2, &nodes);
  2416. // first element sets whether it's a dict or set
  2417. bool is_dict;
  2418. if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
  2419. // a dictionary
  2420. EMIT_ARG(build_map, 1 + n);
  2421. compile_node(comp, pns->nodes[0]);
  2422. EMIT(store_map);
  2423. is_dict = true;
  2424. } else {
  2425. // a set
  2426. compile_node(comp, pns->nodes[0]); // 1st value of set
  2427. is_dict = false;
  2428. }
  2429. // process rest of elements
  2430. for (int i = 0; i < n; i++) {
  2431. mp_parse_node_t pn = nodes[i];
  2432. bool is_key_value = MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_dictorsetmaker_item);
  2433. compile_node(comp, pn);
  2434. if (is_dict) {
  2435. if (!is_key_value) {
  2436. compile_syntax_error(comp, (mp_parse_node_t)pns, "expecting key:value for dictionary");
  2437. return;
  2438. }
  2439. EMIT(store_map);
  2440. } else {
  2441. if (is_key_value) {
  2442. compile_syntax_error(comp, (mp_parse_node_t)pns, "expecting just a value for set");
  2443. return;
  2444. }
  2445. }
  2446. }
  2447. // if it's a set, build it
  2448. if (!is_dict) {
  2449. EMIT_ARG(build_set, 1 + n);
  2450. }
  2451. } else if (MP_PARSE_NODE_STRUCT_KIND(pns1) == PN_comp_for) {
  2452. // dict/set comprehension
  2453. if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_dictorsetmaker_item)) {
  2454. // a dictionary comprehension
  2455. compile_comprehension(comp, pns, SCOPE_DICT_COMP);
  2456. } else {
  2457. // a set comprehension
  2458. compile_comprehension(comp, pns, SCOPE_SET_COMP);
  2459. }
  2460. } else {
  2461. // shouldn't happen
  2462. assert(0);
  2463. }
  2464. } else {
  2465. // set with one element
  2466. goto set_with_one_element;
  2467. }
  2468. } else {
  2469. // set with one element
  2470. set_with_one_element:
  2471. compile_node(comp, pn);
  2472. EMIT_ARG(build_set, 1);
  2473. }
  2474. }
  2475. void compile_trailer_paren(compiler_t *comp, mp_parse_node_struct_t *pns) {
  2476. compile_trailer_paren_helper(comp, pns->nodes[0], false, 0);
  2477. }
  2478. void compile_trailer_bracket(compiler_t *comp, mp_parse_node_struct_t *pns) {
  2479. // object who's index we want is on top of stack
  2480. compile_node(comp, pns->nodes[0]); // the index
  2481. EMIT(load_subscr);
  2482. }
  2483. void compile_trailer_period(compiler_t *comp, mp_parse_node_struct_t *pns) {
  2484. // object who's attribute we want is on top of stack
  2485. EMIT_ARG(load_attr, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0])); // attribute to get
  2486. }
  2487. void compile_subscript_3_helper(compiler_t *comp, mp_parse_node_struct_t *pns) {
  2488. assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3); // should always be
  2489. mp_parse_node_t pn = pns->nodes[0];
  2490. if (MP_PARSE_NODE_IS_NULL(pn)) {
  2491. // [?:]
  2492. EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
  2493. EMIT_ARG(build_slice, 2);
  2494. } else if (MP_PARSE_NODE_IS_STRUCT(pn)) {
  2495. pns = (mp_parse_node_struct_t*)pn;
  2496. if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3c) {
  2497. EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
  2498. pn = pns->nodes[0];
  2499. if (MP_PARSE_NODE_IS_NULL(pn)) {
  2500. // [?::]
  2501. EMIT_ARG(build_slice, 2);
  2502. } else {
  2503. // [?::x]
  2504. compile_node(comp, pn);
  2505. EMIT_ARG(build_slice, 3);
  2506. }
  2507. } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_subscript_3d) {
  2508. compile_node(comp, pns->nodes[0]);
  2509. assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
  2510. pns = (mp_parse_node_struct_t*)pns->nodes[1];
  2511. assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_sliceop); // should always be
  2512. if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
  2513. // [?:x:]
  2514. EMIT_ARG(build_slice, 2);
  2515. } else {
  2516. // [?:x:x]
  2517. compile_node(comp, pns->nodes[0]);
  2518. EMIT_ARG(build_slice, 3);
  2519. }
  2520. } else {
  2521. // [?:x]
  2522. compile_node(comp, pn);
  2523. EMIT_ARG(build_slice, 2);
  2524. }
  2525. } else {
  2526. // [?:x]
  2527. compile_node(comp, pn);
  2528. EMIT_ARG(build_slice, 2);
  2529. }
  2530. }
  2531. void compile_subscript_2(compiler_t *comp, mp_parse_node_struct_t *pns) {
  2532. compile_node(comp, pns->nodes[0]); // start of slice
  2533. assert(MP_PARSE_NODE_IS_STRUCT(pns->nodes[1])); // should always be
  2534. compile_subscript_3_helper(comp, (mp_parse_node_struct_t*)pns->nodes[1]);
  2535. }
  2536. void compile_subscript_3(compiler_t *comp, mp_parse_node_struct_t *pns) {
  2537. EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
  2538. compile_subscript_3_helper(comp, pns);
  2539. }
  2540. void compile_dictorsetmaker_item(compiler_t *comp, mp_parse_node_struct_t *pns) {
  2541. // if this is called then we are compiling a dict key:value pair
  2542. compile_node(comp, pns->nodes[1]); // value
  2543. compile_node(comp, pns->nodes[0]); // key
  2544. }
  2545. void compile_classdef(compiler_t *comp, mp_parse_node_struct_t *pns) {
  2546. qstr cname = compile_classdef_helper(comp, pns, comp->scope_cur->emit_options);
  2547. // store class object into class name
  2548. EMIT_ARG(store_id, cname);
  2549. }
  2550. void compile_yield_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
  2551. if (comp->scope_cur->kind != SCOPE_FUNCTION && comp->scope_cur->kind != SCOPE_LAMBDA) {
  2552. compile_syntax_error(comp, (mp_parse_node_t)pns, "'yield' outside function");
  2553. return;
  2554. }
  2555. if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
  2556. EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
  2557. EMIT(yield_value);
  2558. } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_yield_arg_from)) {
  2559. pns = (mp_parse_node_struct_t*)pns->nodes[0];
  2560. compile_node(comp, pns->nodes[0]);
  2561. EMIT(get_iter);
  2562. EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
  2563. EMIT(yield_from);
  2564. } else {
  2565. compile_node(comp, pns->nodes[0]);
  2566. EMIT(yield_value);
  2567. }
  2568. }
  2569. typedef void (*compile_function_t)(compiler_t*, mp_parse_node_struct_t*);
  2570. STATIC compile_function_t compile_function[] = {
  2571. NULL,
  2572. #define nc NULL
  2573. #define c(f) compile_##f
  2574. #define DEF_RULE(rule, comp, kind, ...) comp,
  2575. #include "grammar.h"
  2576. #undef nc
  2577. #undef c
  2578. #undef DEF_RULE
  2579. };
  2580. void compile_node(compiler_t *comp, mp_parse_node_t pn) {
  2581. if (MP_PARSE_NODE_IS_NULL(pn)) {
  2582. // pass
  2583. } else if (MP_PARSE_NODE_IS_SMALL_INT(pn)) {
  2584. machine_int_t arg = MP_PARSE_NODE_LEAF_SMALL_INT(pn);
  2585. EMIT_ARG(load_const_small_int, arg);
  2586. } else if (MP_PARSE_NODE_IS_LEAF(pn)) {
  2587. machine_uint_t arg = MP_PARSE_NODE_LEAF_ARG(pn);
  2588. switch (MP_PARSE_NODE_LEAF_KIND(pn)) {
  2589. case MP_PARSE_NODE_ID: EMIT_ARG(load_id, arg); break;
  2590. case MP_PARSE_NODE_INTEGER: EMIT_ARG(load_const_int, arg); break;
  2591. case MP_PARSE_NODE_DECIMAL: EMIT_ARG(load_const_dec, arg); break;
  2592. case MP_PARSE_NODE_STRING: EMIT_ARG(load_const_str, arg, false); break;
  2593. case MP_PARSE_NODE_BYTES: EMIT_ARG(load_const_str, arg, true); break;
  2594. case MP_PARSE_NODE_TOKEN:
  2595. if (arg == MP_TOKEN_NEWLINE) {
  2596. // this can occur when file_input lets through a NEWLINE (eg if file starts with a newline)
  2597. // or when single_input lets through a NEWLINE (user enters a blank line)
  2598. // do nothing
  2599. } else {
  2600. EMIT_ARG(load_const_tok, arg);
  2601. }
  2602. break;
  2603. default: assert(0);
  2604. }
  2605. } else {
  2606. mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
  2607. EMIT_ARG(set_line_number, pns->source_line);
  2608. if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_string) {
  2609. EMIT_ARG(load_const_str, qstr_from_strn((const char*)pns->nodes[0], (machine_uint_t)pns->nodes[1]), false);
  2610. } else {
  2611. compile_function_t f = compile_function[MP_PARSE_NODE_STRUCT_KIND(pns)];
  2612. if (f == NULL) {
  2613. printf("node %u cannot be compiled\n", (uint)MP_PARSE_NODE_STRUCT_KIND(pns));
  2614. #if MICROPY_DEBUG_PRINTERS
  2615. mp_parse_node_print(pn, 0);
  2616. #endif
  2617. compile_syntax_error(comp, pn, "internal compiler error");
  2618. } else {
  2619. f(comp, pns);
  2620. }
  2621. }
  2622. }
  2623. }
  2624. void compile_scope_func_lambda_param(compiler_t *comp, mp_parse_node_t pn, pn_kind_t pn_name, pn_kind_t pn_star, pn_kind_t pn_dbl_star, bool allow_annotations) {
  2625. // TODO verify that *k and **k are last etc
  2626. qstr param_name = MP_QSTR_NULL;
  2627. uint param_flag = ID_FLAG_IS_PARAM;
  2628. mp_parse_node_t pn_annotation = MP_PARSE_NODE_NULL;
  2629. if (MP_PARSE_NODE_IS_ID(pn)) {
  2630. param_name = MP_PARSE_NODE_LEAF_ARG(pn);
  2631. if (comp->have_star) {
  2632. // comes after a star, so counts as a keyword-only parameter
  2633. comp->scope_cur->num_kwonly_args += 1;
  2634. } else {
  2635. // comes before a star, so counts as a positional parameter
  2636. comp->scope_cur->num_pos_args += 1;
  2637. }
  2638. } else {
  2639. assert(MP_PARSE_NODE_IS_STRUCT(pn));
  2640. mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
  2641. if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_name) {
  2642. param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
  2643. //int node_index = 1; unused
  2644. if (allow_annotations) {
  2645. if (!MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
  2646. // this parameter has an annotation
  2647. pn_annotation = pns->nodes[1];
  2648. }
  2649. //node_index = 2; unused
  2650. }
  2651. /* this is obsolete now that num dict/default params are calculated in compile_funcdef_param
  2652. if (!MP_PARSE_NODE_IS_NULL(pns->nodes[node_index])) {
  2653. // this parameter has a default value
  2654. if (comp->have_star) {
  2655. comp->scope_cur->num_dict_params += 1;
  2656. } else {
  2657. comp->scope_cur->num_default_params += 1;
  2658. }
  2659. }
  2660. */
  2661. if (comp->have_star) {
  2662. // comes after a star, so counts as a keyword-only parameter
  2663. comp->scope_cur->num_kwonly_args += 1;
  2664. } else {
  2665. // comes before a star, so counts as a positional parameter
  2666. comp->scope_cur->num_pos_args += 1;
  2667. }
  2668. } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_star) {
  2669. comp->have_star = true;
  2670. param_flag = ID_FLAG_IS_PARAM | ID_FLAG_IS_STAR_PARAM;
  2671. if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
  2672. // bare star
  2673. // TODO see http://www.python.org/dev/peps/pep-3102/
  2674. //assert(comp->scope_cur->num_dict_params == 0);
  2675. } else if (MP_PARSE_NODE_IS_ID(pns->nodes[0])) {
  2676. // named star
  2677. comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARARGS;
  2678. param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
  2679. } else if (allow_annotations && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_tfpdef)) {
  2680. // named star with possible annotation
  2681. comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARARGS;
  2682. pns = (mp_parse_node_struct_t*)pns->nodes[0];
  2683. param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
  2684. pn_annotation = pns->nodes[1];
  2685. } else {
  2686. // shouldn't happen
  2687. assert(0);
  2688. }
  2689. } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == pn_dbl_star) {
  2690. param_name = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]);
  2691. param_flag = ID_FLAG_IS_PARAM | ID_FLAG_IS_DBL_STAR_PARAM;
  2692. if (allow_annotations && !MP_PARSE_NODE_IS_NULL(pns->nodes[1])) {
  2693. // this parameter has an annotation
  2694. pn_annotation = pns->nodes[1];
  2695. }
  2696. comp->scope_cur->scope_flags |= MP_SCOPE_FLAG_VARKEYWORDS;
  2697. } else {
  2698. // TODO anything to implement?
  2699. assert(0);
  2700. }
  2701. }
  2702. if (param_name != MP_QSTR_NULL) {
  2703. if (!MP_PARSE_NODE_IS_NULL(pn_annotation)) {
  2704. // TODO this parameter has an annotation
  2705. }
  2706. bool added;
  2707. id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, param_name, &added);
  2708. if (!added) {
  2709. compile_syntax_error(comp, pn, "name reused for argument");
  2710. return;
  2711. }
  2712. id_info->kind = ID_INFO_KIND_LOCAL;
  2713. id_info->flags = param_flag;
  2714. }
  2715. }
  2716. STATIC void compile_scope_func_param(compiler_t *comp, mp_parse_node_t pn) {
  2717. compile_scope_func_lambda_param(comp, pn, PN_typedargslist_name, PN_typedargslist_star, PN_typedargslist_dbl_star, true);
  2718. }
  2719. STATIC void compile_scope_lambda_param(compiler_t *comp, mp_parse_node_t pn) {
  2720. compile_scope_func_lambda_param(comp, pn, PN_varargslist_name, PN_varargslist_star, PN_varargslist_dbl_star, false);
  2721. }
  2722. void compile_scope_comp_iter(compiler_t *comp, mp_parse_node_t pn_iter, mp_parse_node_t pn_inner_expr, int l_top, int for_depth) {
  2723. tail_recursion:
  2724. if (MP_PARSE_NODE_IS_NULL(pn_iter)) {
  2725. // no more nested if/for; compile inner expression
  2726. compile_node(comp, pn_inner_expr);
  2727. if (comp->scope_cur->kind == SCOPE_LIST_COMP) {
  2728. EMIT_ARG(list_append, for_depth + 2);
  2729. } else if (comp->scope_cur->kind == SCOPE_DICT_COMP) {
  2730. EMIT_ARG(map_add, for_depth + 2);
  2731. } else if (comp->scope_cur->kind == SCOPE_SET_COMP) {
  2732. EMIT_ARG(set_add, for_depth + 2);
  2733. } else {
  2734. EMIT(yield_value);
  2735. EMIT(pop_top);
  2736. }
  2737. } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_if)) {
  2738. // if condition
  2739. mp_parse_node_struct_t *pns_comp_if = (mp_parse_node_struct_t*)pn_iter;
  2740. c_if_cond(comp, pns_comp_if->nodes[0], false, l_top);
  2741. pn_iter = pns_comp_if->nodes[1];
  2742. goto tail_recursion;
  2743. } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn_iter, PN_comp_for)) {
  2744. // for loop
  2745. mp_parse_node_struct_t *pns_comp_for2 = (mp_parse_node_struct_t*)pn_iter;
  2746. compile_node(comp, pns_comp_for2->nodes[1]);
  2747. uint l_end2 = comp_next_label(comp);
  2748. uint l_top2 = comp_next_label(comp);
  2749. EMIT(get_iter);
  2750. EMIT_ARG(label_assign, l_top2);
  2751. EMIT_ARG(for_iter, l_end2);
  2752. c_assign(comp, pns_comp_for2->nodes[0], ASSIGN_STORE);
  2753. compile_scope_comp_iter(comp, pns_comp_for2->nodes[2], pn_inner_expr, l_top2, for_depth + 1);
  2754. EMIT_ARG(jump, l_top2);
  2755. EMIT_ARG(label_assign, l_end2);
  2756. EMIT(for_iter_end);
  2757. } else {
  2758. // shouldn't happen
  2759. assert(0);
  2760. }
  2761. }
  2762. STATIC void check_for_doc_string(compiler_t *comp, mp_parse_node_t pn) {
  2763. #if MICROPY_EMIT_CPYTHON || MICROPY_ENABLE_DOC_STRING
  2764. // see http://www.python.org/dev/peps/pep-0257/
  2765. // look for the first statement
  2766. if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
  2767. // a statement; fall through
  2768. } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_file_input_2)) {
  2769. // file input; find the first non-newline node
  2770. mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
  2771. int num_nodes = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
  2772. for (int i = 0; i < num_nodes; i++) {
  2773. pn = pns->nodes[i];
  2774. if (!(MP_PARSE_NODE_IS_LEAF(pn) && MP_PARSE_NODE_LEAF_KIND(pn) == MP_PARSE_NODE_TOKEN && MP_PARSE_NODE_LEAF_ARG(pn) == MP_TOKEN_NEWLINE)) {
  2775. // not a newline, so this is the first statement; finish search
  2776. break;
  2777. }
  2778. }
  2779. // if we didn't find a non-newline then it's okay to fall through; pn will be a newline and so doc-string test below will fail gracefully
  2780. } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_suite_block_stmts)) {
  2781. // a list of statements; get the first one
  2782. pn = ((mp_parse_node_struct_t*)pn)->nodes[0];
  2783. } else {
  2784. return;
  2785. }
  2786. // check the first statement for a doc string
  2787. if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_expr_stmt)) {
  2788. mp_parse_node_struct_t* pns = (mp_parse_node_struct_t*)pn;
  2789. if ((MP_PARSE_NODE_IS_LEAF(pns->nodes[0])
  2790. && MP_PARSE_NODE_LEAF_KIND(pns->nodes[0]) == MP_PARSE_NODE_STRING)
  2791. || MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_string)) {
  2792. // compile the doc string
  2793. compile_node(comp, pns->nodes[0]);
  2794. // store the doc string
  2795. EMIT_ARG(store_id, MP_QSTR___doc__);
  2796. }
  2797. }
  2798. #endif
  2799. }
  2800. STATIC void compile_scope(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
  2801. comp->pass = pass;
  2802. comp->scope_cur = scope;
  2803. comp->next_label = 1;
  2804. EMIT_ARG(start_pass, pass, scope);
  2805. if (comp->pass == MP_PASS_SCOPE) {
  2806. // reset maximum stack sizes in scope
  2807. // they will be computed in this first pass
  2808. scope->stack_size = 0;
  2809. scope->exc_stack_size = 0;
  2810. }
  2811. #if MICROPY_EMIT_CPYTHON
  2812. if (comp->pass == MP_PASS_EMIT) {
  2813. scope_print_info(scope);
  2814. }
  2815. #endif
  2816. // compile
  2817. if (MP_PARSE_NODE_IS_STRUCT_KIND(scope->pn, PN_eval_input)) {
  2818. assert(scope->kind == SCOPE_MODULE);
  2819. mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
  2820. compile_node(comp, pns->nodes[0]); // compile the expression
  2821. EMIT(return_value);
  2822. } else if (scope->kind == SCOPE_MODULE) {
  2823. if (!comp->is_repl) {
  2824. check_for_doc_string(comp, scope->pn);
  2825. }
  2826. compile_node(comp, scope->pn);
  2827. EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
  2828. EMIT(return_value);
  2829. } else if (scope->kind == SCOPE_FUNCTION) {
  2830. assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
  2831. mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
  2832. assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef);
  2833. // work out number of parameters, keywords and default parameters, and add them to the id_info array
  2834. // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
  2835. if (comp->pass == MP_PASS_SCOPE) {
  2836. comp->have_star = false;
  2837. apply_to_single_or_list(comp, pns->nodes[1], PN_typedargslist, compile_scope_func_param);
  2838. }
  2839. // pns->nodes[2] is return/whole function annotation
  2840. compile_node(comp, pns->nodes[3]); // 3 is function body
  2841. // emit return if it wasn't the last opcode
  2842. if (!EMIT(last_emit_was_return_value)) {
  2843. EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
  2844. EMIT(return_value);
  2845. }
  2846. } else if (scope->kind == SCOPE_LAMBDA) {
  2847. assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
  2848. mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
  2849. assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 3);
  2850. // work out number of parameters, keywords and default parameters, and add them to the id_info array
  2851. // must be done before compiling the body so that arguments are numbered first (for LOAD_FAST etc)
  2852. if (comp->pass == MP_PASS_SCOPE) {
  2853. comp->have_star = false;
  2854. apply_to_single_or_list(comp, pns->nodes[0], PN_varargslist, compile_scope_lambda_param);
  2855. }
  2856. compile_node(comp, pns->nodes[1]); // 1 is lambda body
  2857. // if the lambda is a generator, then we return None, not the result of the expression of the lambda
  2858. if (scope->scope_flags & MP_SCOPE_FLAG_GENERATOR) {
  2859. EMIT(pop_top);
  2860. EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
  2861. }
  2862. EMIT(return_value);
  2863. } else if (scope->kind == SCOPE_LIST_COMP || scope->kind == SCOPE_DICT_COMP || scope->kind == SCOPE_SET_COMP || scope->kind == SCOPE_GEN_EXPR) {
  2864. // a bit of a hack at the moment
  2865. assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
  2866. mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
  2867. assert(MP_PARSE_NODE_STRUCT_NUM_NODES(pns) == 2);
  2868. assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[1], PN_comp_for));
  2869. mp_parse_node_struct_t *pns_comp_for = (mp_parse_node_struct_t*)pns->nodes[1];
  2870. // We need a unique name for the comprehension argument (the iterator).
  2871. // CPython uses .0, but we should be able to use anything that won't
  2872. // clash with a user defined variable. Best to use an existing qstr,
  2873. // so we use the blank qstr.
  2874. #if MICROPY_EMIT_CPYTHON
  2875. qstr qstr_arg = QSTR_FROM_STR_STATIC(".0");
  2876. #else
  2877. qstr qstr_arg = MP_QSTR_;
  2878. #endif
  2879. if (comp->pass == MP_PASS_SCOPE) {
  2880. bool added;
  2881. id_info_t *id_info = scope_find_or_add_id(comp->scope_cur, qstr_arg, &added);
  2882. assert(added);
  2883. id_info->kind = ID_INFO_KIND_LOCAL;
  2884. scope->num_pos_args = 1;
  2885. }
  2886. if (scope->kind == SCOPE_LIST_COMP) {
  2887. EMIT_ARG(build_list, 0);
  2888. } else if (scope->kind == SCOPE_DICT_COMP) {
  2889. EMIT_ARG(build_map, 0);
  2890. } else if (scope->kind == SCOPE_SET_COMP) {
  2891. EMIT_ARG(build_set, 0);
  2892. }
  2893. uint l_end = comp_next_label(comp);
  2894. uint l_top = comp_next_label(comp);
  2895. EMIT_ARG(load_id, qstr_arg);
  2896. EMIT_ARG(label_assign, l_top);
  2897. EMIT_ARG(for_iter, l_end);
  2898. c_assign(comp, pns_comp_for->nodes[0], ASSIGN_STORE);
  2899. compile_scope_comp_iter(comp, pns_comp_for->nodes[2], pns->nodes[0], l_top, 0);
  2900. EMIT_ARG(jump, l_top);
  2901. EMIT_ARG(label_assign, l_end);
  2902. EMIT(for_iter_end);
  2903. if (scope->kind == SCOPE_GEN_EXPR) {
  2904. EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
  2905. }
  2906. EMIT(return_value);
  2907. } else {
  2908. assert(scope->kind == SCOPE_CLASS);
  2909. assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
  2910. mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
  2911. assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_classdef);
  2912. if (comp->pass == MP_PASS_SCOPE) {
  2913. bool added;
  2914. id_info_t *id_info = scope_find_or_add_id(scope, MP_QSTR___class__, &added);
  2915. assert(added);
  2916. id_info->kind = ID_INFO_KIND_LOCAL;
  2917. }
  2918. EMIT_ARG(load_id, MP_QSTR___name__);
  2919. EMIT_ARG(store_id, MP_QSTR___module__);
  2920. EMIT_ARG(load_const_str, MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]), false); // 0 is class name
  2921. EMIT_ARG(store_id, MP_QSTR___qualname__);
  2922. check_for_doc_string(comp, pns->nodes[2]);
  2923. compile_node(comp, pns->nodes[2]); // 2 is class body
  2924. id_info_t *id = scope_find(scope, MP_QSTR___class__);
  2925. assert(id != NULL);
  2926. if (id->kind == ID_INFO_KIND_LOCAL) {
  2927. EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
  2928. } else {
  2929. #if MICROPY_EMIT_CPYTHON
  2930. EMIT_ARG(load_closure, MP_QSTR___class__, 0); // XXX check this is the correct local num
  2931. #else
  2932. EMIT_ARG(load_fast, MP_QSTR___class__, id->flags, id->local_num);
  2933. #endif
  2934. }
  2935. EMIT(return_value);
  2936. }
  2937. EMIT(end_pass);
  2938. // make sure we match all the exception levels
  2939. assert(comp->cur_except_level == 0);
  2940. }
  2941. #if MICROPY_EMIT_INLINE_THUMB
  2942. // requires 3 passes: SCOPE, CODE_SIZE, EMIT
  2943. STATIC void compile_scope_inline_asm(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
  2944. comp->pass = pass;
  2945. comp->scope_cur = scope;
  2946. comp->next_label = 1;
  2947. if (scope->kind != SCOPE_FUNCTION) {
  2948. printf("Error: inline assembler must be a function\n");
  2949. return;
  2950. }
  2951. if (comp->pass > MP_PASS_SCOPE) {
  2952. EMIT_INLINE_ASM_ARG(start_pass, comp->pass, comp->scope_cur);
  2953. }
  2954. // get the function definition parse node
  2955. assert(MP_PARSE_NODE_IS_STRUCT(scope->pn));
  2956. mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)scope->pn;
  2957. assert(MP_PARSE_NODE_STRUCT_KIND(pns) == PN_funcdef);
  2958. //qstr f_id = MP_PARSE_NODE_LEAF_ARG(pns->nodes[0]); // function name
  2959. // parameters are in pns->nodes[1]
  2960. if (comp->pass == MP_PASS_CODE_SIZE) {
  2961. mp_parse_node_t *pn_params;
  2962. int n_params = list_get(&pns->nodes[1], PN_typedargslist, &pn_params);
  2963. scope->num_pos_args = EMIT_INLINE_ASM_ARG(count_params, n_params, pn_params);
  2964. }
  2965. assert(MP_PARSE_NODE_IS_NULL(pns->nodes[2])); // type
  2966. mp_parse_node_t pn_body = pns->nodes[3]; // body
  2967. mp_parse_node_t *nodes;
  2968. int num = list_get(&pn_body, PN_suite_block_stmts, &nodes);
  2969. /*
  2970. if (comp->pass == MP_PASS_EMIT) {
  2971. //printf("----\n");
  2972. scope_print_info(scope);
  2973. }
  2974. */
  2975. for (int i = 0; i < num; i++) {
  2976. assert(MP_PARSE_NODE_IS_STRUCT(nodes[i]));
  2977. mp_parse_node_struct_t *pns2 = (mp_parse_node_struct_t*)nodes[i];
  2978. if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_pass_stmt) {
  2979. // no instructions
  2980. continue;
  2981. } else if (MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_expr_stmt) {
  2982. // an instruction; fall through
  2983. } else {
  2984. // not an instruction; error
  2985. compile_syntax_error(comp, nodes[i], "inline assembler expecting an instruction");
  2986. return;
  2987. }
  2988. assert(MP_PARSE_NODE_IS_STRUCT(pns2->nodes[0]));
  2989. assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[1]));
  2990. pns2 = (mp_parse_node_struct_t*)pns2->nodes[0];
  2991. assert(MP_PARSE_NODE_STRUCT_KIND(pns2) == PN_power);
  2992. assert(MP_PARSE_NODE_IS_ID(pns2->nodes[0]));
  2993. assert(MP_PARSE_NODE_IS_STRUCT_KIND(pns2->nodes[1], PN_trailer_paren));
  2994. assert(MP_PARSE_NODE_IS_NULL(pns2->nodes[2]));
  2995. qstr op = MP_PARSE_NODE_LEAF_ARG(pns2->nodes[0]);
  2996. pns2 = (mp_parse_node_struct_t*)pns2->nodes[1]; // PN_trailer_paren
  2997. mp_parse_node_t *pn_arg;
  2998. int n_args = list_get(&pns2->nodes[0], PN_arglist, &pn_arg);
  2999. // emit instructions
  3000. if (op == MP_QSTR_label) {
  3001. if (!(n_args == 1 && MP_PARSE_NODE_IS_ID(pn_arg[0]))) {
  3002. compile_syntax_error(comp, nodes[i], "inline assembler 'label' requires 1 argument");
  3003. return;
  3004. }
  3005. uint lab = comp_next_label(comp);
  3006. if (pass > MP_PASS_SCOPE) {
  3007. EMIT_INLINE_ASM_ARG(label, lab, MP_PARSE_NODE_LEAF_ARG(pn_arg[0]));
  3008. }
  3009. } else if (op == MP_QSTR_align) {
  3010. if (!(n_args == 1 && MP_PARSE_NODE_IS_SMALL_INT(pn_arg[0]))) {
  3011. compile_syntax_error(comp, nodes[i], "inline assembler 'align' requires 1 argument");
  3012. return;
  3013. }
  3014. if (pass > MP_PASS_SCOPE) {
  3015. EMIT_INLINE_ASM_ARG(align, MP_PARSE_NODE_LEAF_SMALL_INT(pn_arg[0]));
  3016. }
  3017. } else if (op == MP_QSTR_data) {
  3018. if (!(n_args >= 2 && MP_PARSE_NODE_IS_SMALL_INT(pn_arg[0]))) {
  3019. compile_syntax_error(comp, nodes[i], "inline assembler 'data' requires at least 2 arguments");
  3020. return;
  3021. }
  3022. if (pass > MP_PASS_SCOPE) {
  3023. machine_int_t bytesize = MP_PARSE_NODE_LEAF_SMALL_INT(pn_arg[0]);
  3024. for (uint i = 1; i < n_args; i++) {
  3025. if (!MP_PARSE_NODE_IS_SMALL_INT(pn_arg[i])) {
  3026. compile_syntax_error(comp, nodes[i], "inline assembler 'data' requires integer arguments");
  3027. return;
  3028. }
  3029. EMIT_INLINE_ASM_ARG(data, bytesize, MP_PARSE_NODE_LEAF_SMALL_INT(pn_arg[i]));
  3030. }
  3031. }
  3032. } else {
  3033. if (pass > MP_PASS_SCOPE) {
  3034. EMIT_INLINE_ASM_ARG(op, op, n_args, pn_arg);
  3035. }
  3036. }
  3037. }
  3038. if (comp->pass > MP_PASS_SCOPE) {
  3039. bool success = EMIT_INLINE_ASM(end_pass);
  3040. if (!success) {
  3041. comp->had_error = true;
  3042. }
  3043. }
  3044. }
  3045. #endif
  3046. STATIC void compile_scope_compute_things(compiler_t *comp, scope_t *scope) {
  3047. #if !MICROPY_EMIT_CPYTHON
  3048. // in Micro Python we put the *x parameter after all other parameters (except **y)
  3049. if (scope->scope_flags & MP_SCOPE_FLAG_VARARGS) {
  3050. id_info_t *id_param = NULL;
  3051. for (int i = scope->id_info_len - 1; i >= 0; i--) {
  3052. id_info_t *id = &scope->id_info[i];
  3053. if (id->flags & ID_FLAG_IS_STAR_PARAM) {
  3054. if (id_param != NULL) {
  3055. // swap star param with last param
  3056. id_info_t temp = *id_param; *id_param = *id; *id = temp;
  3057. }
  3058. break;
  3059. } else if (id_param == NULL && id->flags == ID_FLAG_IS_PARAM) {
  3060. id_param = id;
  3061. }
  3062. }
  3063. }
  3064. #endif
  3065. // in functions, turn implicit globals into explicit globals
  3066. // compute the index of each local
  3067. scope->num_locals = 0;
  3068. for (int i = 0; i < scope->id_info_len; i++) {
  3069. id_info_t *id = &scope->id_info[i];
  3070. if (scope->kind == SCOPE_CLASS && id->qstr == MP_QSTR___class__) {
  3071. // __class__ is not counted as a local; if it's used then it becomes a ID_INFO_KIND_CELL
  3072. continue;
  3073. }
  3074. if (scope->kind >= SCOPE_FUNCTION && scope->kind <= SCOPE_GEN_EXPR && id->kind == ID_INFO_KIND_GLOBAL_IMPLICIT) {
  3075. id->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
  3076. }
  3077. // params always count for 1 local, even if they are a cell
  3078. if (id->kind == ID_INFO_KIND_LOCAL || (id->flags & ID_FLAG_IS_PARAM)) {
  3079. id->local_num = scope->num_locals++;
  3080. }
  3081. }
  3082. // compute the index of cell vars (freevars[idx] in CPython)
  3083. #if MICROPY_EMIT_CPYTHON
  3084. int num_cell = 0;
  3085. #endif
  3086. for (int i = 0; i < scope->id_info_len; i++) {
  3087. id_info_t *id = &scope->id_info[i];
  3088. #if MICROPY_EMIT_CPYTHON
  3089. // in CPython the cells are numbered starting from 0
  3090. if (id->kind == ID_INFO_KIND_CELL) {
  3091. id->local_num = num_cell;
  3092. num_cell += 1;
  3093. }
  3094. #else
  3095. // in Micro Python the cells come right after the fast locals
  3096. // parameters are not counted here, since they remain at the start
  3097. // of the locals, even if they are cell vars
  3098. if (id->kind == ID_INFO_KIND_CELL && !(id->flags & ID_FLAG_IS_PARAM)) {
  3099. id->local_num = scope->num_locals;
  3100. scope->num_locals += 1;
  3101. }
  3102. #endif
  3103. }
  3104. // compute the index of free vars (freevars[idx] in CPython)
  3105. // make sure they are in the order of the parent scope
  3106. if (scope->parent != NULL) {
  3107. int num_free = 0;
  3108. for (int i = 0; i < scope->parent->id_info_len; i++) {
  3109. id_info_t *id = &scope->parent->id_info[i];
  3110. if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
  3111. for (int j = 0; j < scope->id_info_len; j++) {
  3112. id_info_t *id2 = &scope->id_info[j];
  3113. if (id2->kind == ID_INFO_KIND_FREE && id->qstr == id2->qstr) {
  3114. assert(!(id2->flags & ID_FLAG_IS_PARAM)); // free vars should not be params
  3115. #if MICROPY_EMIT_CPYTHON
  3116. // in CPython the frees are numbered after the cells
  3117. id2->local_num = num_cell + num_free;
  3118. #else
  3119. // in Micro Python the frees come first, before the params
  3120. id2->local_num = num_free;
  3121. #endif
  3122. num_free += 1;
  3123. }
  3124. }
  3125. }
  3126. }
  3127. #if !MICROPY_EMIT_CPYTHON
  3128. // in Micro Python shift all other locals after the free locals
  3129. if (num_free > 0) {
  3130. for (int i = 0; i < scope->id_info_len; i++) {
  3131. id_info_t *id = &scope->id_info[i];
  3132. if (id->kind != ID_INFO_KIND_FREE || (id->flags & ID_FLAG_IS_PARAM)) {
  3133. id->local_num += num_free;
  3134. }
  3135. }
  3136. scope->num_pos_args += num_free; // free vars are counted as params for passing them into the function
  3137. scope->num_locals += num_free;
  3138. }
  3139. #endif
  3140. }
  3141. // compute scope_flags
  3142. #if MICROPY_EMIT_CPYTHON
  3143. // these flags computed here are for CPython compatibility only
  3144. if (scope->kind == SCOPE_FUNCTION || scope->kind == SCOPE_LAMBDA || scope->kind == SCOPE_LIST_COMP || scope->kind == SCOPE_DICT_COMP || scope->kind == SCOPE_SET_COMP || scope->kind == SCOPE_GEN_EXPR) {
  3145. assert(scope->parent != NULL);
  3146. scope->scope_flags |= MP_SCOPE_FLAG_NEWLOCALS;
  3147. scope->scope_flags |= MP_SCOPE_FLAG_OPTIMISED;
  3148. if ((SCOPE_FUNCTION <= scope->parent->kind && scope->parent->kind <= SCOPE_SET_COMP) || (scope->parent->kind == SCOPE_CLASS && scope->parent->parent->kind == SCOPE_FUNCTION)) {
  3149. scope->scope_flags |= MP_SCOPE_FLAG_NESTED;
  3150. }
  3151. }
  3152. #endif
  3153. int num_free = 0;
  3154. for (int i = 0; i < scope->id_info_len; i++) {
  3155. id_info_t *id = &scope->id_info[i];
  3156. if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
  3157. num_free += 1;
  3158. }
  3159. }
  3160. if (num_free == 0) {
  3161. scope->scope_flags |= MP_SCOPE_FLAG_NOFREE;
  3162. }
  3163. }
  3164. mp_obj_t mp_compile(mp_parse_node_t pn, qstr source_file, uint emit_opt, bool is_repl) {
  3165. compiler_t *comp = m_new0(compiler_t, 1);
  3166. comp->source_file = source_file;
  3167. comp->is_repl = is_repl;
  3168. // optimise constants
  3169. mp_map_t consts;
  3170. mp_map_init(&consts, 0);
  3171. pn = fold_constants(comp, pn, &consts);
  3172. mp_map_deinit(&consts);
  3173. // set the outer scope
  3174. scope_t *module_scope = scope_new_and_link(comp, SCOPE_MODULE, pn, emit_opt);
  3175. // compile pass 1
  3176. comp->emit = emit_pass1_new();
  3177. comp->emit_method_table = &emit_pass1_method_table;
  3178. comp->emit_inline_asm = NULL;
  3179. comp->emit_inline_asm_method_table = NULL;
  3180. uint max_num_labels = 0;
  3181. for (scope_t *s = comp->scope_head; s != NULL && !comp->had_error; s = s->next) {
  3182. if (false) {
  3183. #if MICROPY_EMIT_INLINE_THUMB
  3184. } else if (s->emit_options == MP_EMIT_OPT_ASM_THUMB) {
  3185. compile_scope_inline_asm(comp, s, MP_PASS_SCOPE);
  3186. #endif
  3187. } else {
  3188. compile_scope(comp, s, MP_PASS_SCOPE);
  3189. }
  3190. // update maximim number of labels needed
  3191. if (comp->next_label > max_num_labels) {
  3192. max_num_labels = comp->next_label;
  3193. }
  3194. }
  3195. // compute some things related to scope and identifiers
  3196. for (scope_t *s = comp->scope_head; s != NULL && !comp->had_error; s = s->next) {
  3197. compile_scope_compute_things(comp, s);
  3198. }
  3199. // finish with pass 1
  3200. emit_pass1_free(comp->emit);
  3201. // compile pass 2 and 3
  3202. #if !MICROPY_EMIT_CPYTHON
  3203. emit_t *emit_bc = NULL;
  3204. #if MICROPY_EMIT_NATIVE
  3205. emit_t *emit_native = NULL;
  3206. #endif
  3207. #if MICROPY_EMIT_INLINE_THUMB
  3208. emit_inline_asm_t *emit_inline_thumb = NULL;
  3209. #endif
  3210. #endif // !MICROPY_EMIT_CPYTHON
  3211. for (scope_t *s = comp->scope_head; s != NULL && !comp->had_error; s = s->next) {
  3212. if (false) {
  3213. // dummy
  3214. #if MICROPY_EMIT_INLINE_THUMB
  3215. } else if (s->emit_options == MP_EMIT_OPT_ASM_THUMB) {
  3216. // inline assembly for thumb
  3217. if (emit_inline_thumb == NULL) {
  3218. emit_inline_thumb = emit_inline_thumb_new(max_num_labels);
  3219. }
  3220. comp->emit = NULL;
  3221. comp->emit_method_table = NULL;
  3222. comp->emit_inline_asm = emit_inline_thumb;
  3223. comp->emit_inline_asm_method_table = &emit_inline_thumb_method_table;
  3224. compile_scope_inline_asm(comp, s, MP_PASS_CODE_SIZE);
  3225. if (!comp->had_error) {
  3226. compile_scope_inline_asm(comp, s, MP_PASS_EMIT);
  3227. }
  3228. #endif
  3229. } else {
  3230. // choose the emit type
  3231. #if MICROPY_EMIT_CPYTHON
  3232. comp->emit = emit_cpython_new(max_num_labels);
  3233. comp->emit_method_table = &emit_cpython_method_table;
  3234. #else
  3235. switch (s->emit_options) {
  3236. #if MICROPY_EMIT_NATIVE
  3237. case MP_EMIT_OPT_NATIVE_PYTHON:
  3238. case MP_EMIT_OPT_VIPER:
  3239. #if MICROPY_EMIT_X64
  3240. if (emit_native == NULL) {
  3241. emit_native = emit_native_x64_new(max_num_labels);
  3242. }
  3243. comp->emit_method_table = &emit_native_x64_method_table;
  3244. #elif MICROPY_EMIT_THUMB
  3245. if (emit_native == NULL) {
  3246. emit_native = emit_native_thumb_new(max_num_labels);
  3247. }
  3248. comp->emit_method_table = &emit_native_thumb_method_table;
  3249. #endif
  3250. comp->emit = emit_native;
  3251. comp->emit_method_table->set_native_types(comp->emit, s->emit_options == MP_EMIT_OPT_VIPER);
  3252. // native emitters need an extra pass to compute stack size
  3253. compile_scope(comp, s, MP_PASS_STACK_SIZE);
  3254. break;
  3255. #endif // MICROPY_EMIT_NATIVE
  3256. default:
  3257. if (emit_bc == NULL) {
  3258. emit_bc = emit_bc_new(max_num_labels);
  3259. }
  3260. comp->emit = emit_bc;
  3261. comp->emit_method_table = &emit_bc_method_table;
  3262. break;
  3263. }
  3264. #endif // !MICROPY_EMIT_CPYTHON
  3265. // second last pass: compute code size
  3266. if (!comp->had_error) {
  3267. compile_scope(comp, s, MP_PASS_CODE_SIZE);
  3268. }
  3269. // final pass: emit code
  3270. if (!comp->had_error) {
  3271. compile_scope(comp, s, MP_PASS_EMIT);
  3272. }
  3273. }
  3274. }
  3275. // free the emitters
  3276. #if !MICROPY_EMIT_CPYTHON
  3277. if (emit_bc != NULL) {
  3278. emit_bc_free(emit_bc);
  3279. }
  3280. #if MICROPY_EMIT_NATIVE
  3281. if (emit_native != NULL) {
  3282. #if MICROPY_EMIT_X64
  3283. emit_native_x64_free(emit_native);
  3284. #elif MICROPY_EMIT_THUMB
  3285. emit_native_thumb_free(emit_native);
  3286. #endif
  3287. }
  3288. #endif
  3289. #if MICROPY_EMIT_INLINE_THUMB
  3290. if (emit_inline_thumb != NULL) {
  3291. emit_inline_thumb_free(emit_inline_thumb);
  3292. }
  3293. #endif
  3294. #endif // !MICROPY_EMIT_CPYTHON
  3295. // free the scopes
  3296. mp_raw_code_t *outer_raw_code = module_scope->raw_code;
  3297. for (scope_t *s = module_scope; s;) {
  3298. scope_t *next = s->next;
  3299. scope_free(s);
  3300. s = next;
  3301. }
  3302. // free the compiler
  3303. bool had_error = comp->had_error;
  3304. m_del_obj(compiler_t, comp);
  3305. if (had_error) {
  3306. // TODO return a proper error message
  3307. return mp_const_none;
  3308. } else {
  3309. #if MICROPY_EMIT_CPYTHON
  3310. // can't create code, so just return true
  3311. (void)outer_raw_code; // to suppress warning that outer_raw_code is unused
  3312. return mp_const_true;
  3313. #else
  3314. // return function that executes the outer module
  3315. return mp_make_function_from_raw_code(outer_raw_code, MP_OBJ_NULL, MP_OBJ_NULL);
  3316. #endif
  3317. }
  3318. }