bc.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. /*
  2. * This file is part of the MicroPython project, http://micropython.org/
  3. *
  4. * The MIT License (MIT)
  5. *
  6. * Copyright (c) 2014 Damien P. George
  7. * Copyright (c) 2014 Paul Sokolovsky
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. */
  27. #include <stdbool.h>
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #include <assert.h>
  32. #include "py/mpconfig.h"
  33. #include "py/misc.h"
  34. #include "py/mpstate.h"
  35. #include "py/runtime.h"
  36. #include "py/bc0.h"
  37. #include "py/bc.h"
  38. #if MICROPY_DEBUG_VERBOSE // print debugging info
  39. #define DEBUG_PRINT (1)
  40. #define DEBUG_printf DEBUG_printf
  41. #else // don't print debugging info
  42. #define DEBUG_PRINT (0)
  43. #define DEBUG_printf(...) (void)0
  44. #endif
  45. mp_uint_t mp_decode_uint(const byte **ptr) {
  46. mp_uint_t unum = 0;
  47. byte val;
  48. const byte *p = *ptr;
  49. do {
  50. val = *p++;
  51. unum = (unum << 7) | (val & 0x7f);
  52. } while ((val & 0x80) != 0);
  53. *ptr = p;
  54. return unum;
  55. }
  56. // This function is used to help reduce stack usage at the caller, for the case when
  57. // the caller doesn't need to increase the ptr argument. If ptr is a local variable
  58. // and the caller uses mp_decode_uint(&ptr) instead of this function, then the compiler
  59. // must allocate a slot on the stack for ptr, and this slot cannot be reused for
  60. // anything else in the function because the pointer may have been stored in a global
  61. // and reused later in the function.
  62. mp_uint_t mp_decode_uint_value(const byte *ptr) {
  63. return mp_decode_uint(&ptr);
  64. }
  65. // This function is used to help reduce stack usage at the caller, for the case when
  66. // the caller doesn't need the actual value and just wants to skip over it.
  67. const byte *mp_decode_uint_skip(const byte *ptr) {
  68. while ((*ptr++) & 0x80) {
  69. }
  70. return ptr;
  71. }
  72. STATIC NORETURN void fun_pos_args_mismatch(mp_obj_fun_bc_t *f, size_t expected, size_t given) {
  73. #if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
  74. // generic message, used also for other argument issues
  75. (void)f;
  76. (void)expected;
  77. (void)given;
  78. mp_arg_error_terse_mismatch();
  79. #elif MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_NORMAL
  80. (void)f;
  81. nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
  82. "function takes %d positional arguments but %d were given", expected, given));
  83. #elif MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_DETAILED
  84. nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
  85. "%q() takes %d positional arguments but %d were given",
  86. mp_obj_fun_get_name(MP_OBJ_FROM_PTR(f)), expected, given));
  87. #endif
  88. }
  89. #if DEBUG_PRINT
  90. STATIC void dump_args(const mp_obj_t *a, size_t sz) {
  91. DEBUG_printf("%p: ", a);
  92. for (size_t i = 0; i < sz; i++) {
  93. DEBUG_printf("%p ", a[i]);
  94. }
  95. DEBUG_printf("\n");
  96. }
  97. #else
  98. #define dump_args(...) (void)0
  99. #endif
  100. // On entry code_state should be allocated somewhere (stack/heap) and
  101. // contain the following valid entries:
  102. // - code_state->fun_bc should contain a pointer to the function object
  103. // - code_state->ip should contain the offset in bytes from the pointer
  104. // code_state->fun_bc->bytecode to the entry n_state (0 for bytecode, non-zero for native)
  105. void mp_setup_code_state(mp_code_state_t *code_state, size_t n_args, size_t n_kw, const mp_obj_t *args) {
  106. // This function is pretty complicated. It's main aim is to be efficient in speed and RAM
  107. // usage for the common case of positional only args.
  108. // get the function object that we want to set up (could be bytecode or native code)
  109. mp_obj_fun_bc_t *self = code_state->fun_bc;
  110. // ip comes in as an offset into bytecode, so turn it into a true pointer
  111. code_state->ip = self->bytecode + (size_t)code_state->ip;
  112. #if MICROPY_STACKLESS
  113. code_state->prev = NULL;
  114. #endif
  115. // get params
  116. size_t n_state = mp_decode_uint(&code_state->ip);
  117. code_state->ip = mp_decode_uint_skip(code_state->ip); // skip n_exc_stack
  118. size_t scope_flags = *code_state->ip++;
  119. size_t n_pos_args = *code_state->ip++;
  120. size_t n_kwonly_args = *code_state->ip++;
  121. size_t n_def_pos_args = *code_state->ip++;
  122. code_state->sp = &code_state->state[0] - 1;
  123. code_state->exc_sp = (mp_exc_stack_t*)(code_state->state + n_state) - 1;
  124. // zero out the local stack to begin with
  125. memset(code_state->state, 0, n_state * sizeof(*code_state->state));
  126. const mp_obj_t *kwargs = args + n_args;
  127. // var_pos_kw_args points to the stack where the var-args tuple, and var-kw dict, should go (if they are needed)
  128. mp_obj_t *var_pos_kw_args = &code_state->state[n_state - 1 - n_pos_args - n_kwonly_args];
  129. // check positional arguments
  130. if (n_args > n_pos_args) {
  131. // given more than enough arguments
  132. if ((scope_flags & MP_SCOPE_FLAG_VARARGS) == 0) {
  133. fun_pos_args_mismatch(self, n_pos_args, n_args);
  134. }
  135. // put extra arguments in varargs tuple
  136. *var_pos_kw_args-- = mp_obj_new_tuple(n_args - n_pos_args, args + n_pos_args);
  137. n_args = n_pos_args;
  138. } else {
  139. if ((scope_flags & MP_SCOPE_FLAG_VARARGS) != 0) {
  140. DEBUG_printf("passing empty tuple as *args\n");
  141. *var_pos_kw_args-- = mp_const_empty_tuple;
  142. }
  143. // Apply processing and check below only if we don't have kwargs,
  144. // otherwise, kw handling code below has own extensive checks.
  145. if (n_kw == 0 && (scope_flags & MP_SCOPE_FLAG_DEFKWARGS) == 0) {
  146. if (n_args >= (size_t)(n_pos_args - n_def_pos_args)) {
  147. // given enough arguments, but may need to use some default arguments
  148. for (size_t i = n_args; i < n_pos_args; i++) {
  149. code_state->state[n_state - 1 - i] = self->extra_args[i - (n_pos_args - n_def_pos_args)];
  150. }
  151. } else {
  152. fun_pos_args_mismatch(self, n_pos_args - n_def_pos_args, n_args);
  153. }
  154. }
  155. }
  156. // copy positional args into state
  157. for (size_t i = 0; i < n_args; i++) {
  158. code_state->state[n_state - 1 - i] = args[i];
  159. }
  160. // check keyword arguments
  161. if (n_kw != 0 || (scope_flags & MP_SCOPE_FLAG_DEFKWARGS) != 0) {
  162. DEBUG_printf("Initial args: ");
  163. dump_args(code_state->state + n_state - n_pos_args - n_kwonly_args, n_pos_args + n_kwonly_args);
  164. mp_obj_t dict = MP_OBJ_NULL;
  165. if ((scope_flags & MP_SCOPE_FLAG_VARKEYWORDS) != 0) {
  166. dict = mp_obj_new_dict(n_kw); // TODO: better go conservative with 0?
  167. *var_pos_kw_args = dict;
  168. }
  169. // get pointer to arg_names array
  170. const mp_obj_t *arg_names = (const mp_obj_t*)self->const_table;
  171. for (size_t i = 0; i < n_kw; i++) {
  172. // the keys in kwargs are expected to be qstr objects
  173. mp_obj_t wanted_arg_name = kwargs[2 * i];
  174. for (size_t j = 0; j < n_pos_args + n_kwonly_args; j++) {
  175. if (wanted_arg_name == arg_names[j]) {
  176. if (code_state->state[n_state - 1 - j] != MP_OBJ_NULL) {
  177. nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
  178. "function got multiple values for argument '%q'", MP_OBJ_QSTR_VALUE(wanted_arg_name)));
  179. }
  180. code_state->state[n_state - 1 - j] = kwargs[2 * i + 1];
  181. goto continue2;
  182. }
  183. }
  184. // Didn't find name match with positional args
  185. if ((scope_flags & MP_SCOPE_FLAG_VARKEYWORDS) == 0) {
  186. if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
  187. mp_raise_TypeError("unexpected keyword argument");
  188. } else {
  189. nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
  190. "unexpected keyword argument '%q'", MP_OBJ_QSTR_VALUE(wanted_arg_name)));
  191. }
  192. }
  193. mp_obj_dict_store(dict, kwargs[2 * i], kwargs[2 * i + 1]);
  194. continue2:;
  195. }
  196. DEBUG_printf("Args with kws flattened: ");
  197. dump_args(code_state->state + n_state - n_pos_args - n_kwonly_args, n_pos_args + n_kwonly_args);
  198. // fill in defaults for positional args
  199. mp_obj_t *d = &code_state->state[n_state - n_pos_args];
  200. mp_obj_t *s = &self->extra_args[n_def_pos_args - 1];
  201. for (size_t i = n_def_pos_args; i > 0; i--, d++, s--) {
  202. if (*d == MP_OBJ_NULL) {
  203. *d = *s;
  204. }
  205. }
  206. DEBUG_printf("Args after filling default positional: ");
  207. dump_args(code_state->state + n_state - n_pos_args - n_kwonly_args, n_pos_args + n_kwonly_args);
  208. // Check that all mandatory positional args are specified
  209. while (d < &code_state->state[n_state]) {
  210. if (*d++ == MP_OBJ_NULL) {
  211. nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
  212. "function missing required positional argument #%d", &code_state->state[n_state] - d));
  213. }
  214. }
  215. // Check that all mandatory keyword args are specified
  216. // Fill in default kw args if we have them
  217. for (size_t i = 0; i < n_kwonly_args; i++) {
  218. if (code_state->state[n_state - 1 - n_pos_args - i] == MP_OBJ_NULL) {
  219. mp_map_elem_t *elem = NULL;
  220. if ((scope_flags & MP_SCOPE_FLAG_DEFKWARGS) != 0) {
  221. elem = mp_map_lookup(&((mp_obj_dict_t*)MP_OBJ_TO_PTR(self->extra_args[n_def_pos_args]))->map, arg_names[n_pos_args + i], MP_MAP_LOOKUP);
  222. }
  223. if (elem != NULL) {
  224. code_state->state[n_state - 1 - n_pos_args - i] = elem->value;
  225. } else {
  226. nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
  227. "function missing required keyword argument '%q'", MP_OBJ_QSTR_VALUE(arg_names[n_pos_args + i])));
  228. }
  229. }
  230. }
  231. } else {
  232. // no keyword arguments given
  233. if (n_kwonly_args != 0) {
  234. mp_raise_TypeError("function missing keyword-only argument");
  235. }
  236. if ((scope_flags & MP_SCOPE_FLAG_VARKEYWORDS) != 0) {
  237. *var_pos_kw_args = mp_obj_new_dict(0);
  238. }
  239. }
  240. // get the ip and skip argument names
  241. const byte *ip = code_state->ip;
  242. // jump over code info (source file and line-number mapping)
  243. ip += mp_decode_uint_value(ip);
  244. // bytecode prelude: initialise closed over variables
  245. size_t local_num;
  246. while ((local_num = *ip++) != 255) {
  247. code_state->state[n_state - 1 - local_num] =
  248. mp_obj_new_cell(code_state->state[n_state - 1 - local_num]);
  249. }
  250. // now that we skipped over the prelude, set the ip for the VM
  251. code_state->ip = ip;
  252. DEBUG_printf("Calling: n_pos_args=%d, n_kwonly_args=%d\n", n_pos_args, n_kwonly_args);
  253. dump_args(code_state->state + n_state - n_pos_args - n_kwonly_args, n_pos_args + n_kwonly_args);
  254. dump_args(code_state->state, n_state);
  255. }
  256. #if MICROPY_PERSISTENT_CODE_LOAD || MICROPY_PERSISTENT_CODE_SAVE
  257. // The following table encodes the number of bytes that a specific opcode
  258. // takes up. There are 3 special opcodes that always have an extra byte:
  259. // MP_BC_MAKE_CLOSURE
  260. // MP_BC_MAKE_CLOSURE_DEFARGS
  261. // MP_BC_RAISE_VARARGS
  262. // There are 4 special opcodes that have an extra byte only when
  263. // MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE is enabled:
  264. // MP_BC_LOAD_NAME
  265. // MP_BC_LOAD_GLOBAL
  266. // MP_BC_LOAD_ATTR
  267. // MP_BC_STORE_ATTR
  268. #define OC4(a, b, c, d) (a | (b << 2) | (c << 4) | (d << 6))
  269. #define U (0) // undefined opcode
  270. #define B (MP_OPCODE_BYTE) // single byte
  271. #define Q (MP_OPCODE_QSTR) // single byte plus 2-byte qstr
  272. #define V (MP_OPCODE_VAR_UINT) // single byte plus variable encoded unsigned int
  273. #define O (MP_OPCODE_OFFSET) // single byte plus 2-byte bytecode offset
  274. STATIC const byte opcode_format_table[64] = {
  275. OC4(U, U, U, U), // 0x00-0x03
  276. OC4(U, U, U, U), // 0x04-0x07
  277. OC4(U, U, U, U), // 0x08-0x0b
  278. OC4(U, U, U, U), // 0x0c-0x0f
  279. OC4(B, B, B, U), // 0x10-0x13
  280. OC4(V, U, Q, V), // 0x14-0x17
  281. OC4(B, V, V, Q), // 0x18-0x1b
  282. OC4(Q, Q, Q, Q), // 0x1c-0x1f
  283. OC4(B, B, V, V), // 0x20-0x23
  284. OC4(Q, Q, Q, B), // 0x24-0x27
  285. OC4(V, V, Q, Q), // 0x28-0x2b
  286. OC4(U, U, U, U), // 0x2c-0x2f
  287. OC4(B, B, B, B), // 0x30-0x33
  288. OC4(B, O, O, O), // 0x34-0x37
  289. OC4(O, O, U, U), // 0x38-0x3b
  290. OC4(U, O, B, O), // 0x3c-0x3f
  291. OC4(O, B, B, O), // 0x40-0x43
  292. OC4(B, B, O, B), // 0x44-0x47
  293. OC4(U, U, U, U), // 0x48-0x4b
  294. OC4(U, U, U, U), // 0x4c-0x4f
  295. OC4(V, V, U, V), // 0x50-0x53
  296. OC4(B, U, V, V), // 0x54-0x57
  297. OC4(V, V, V, B), // 0x58-0x5b
  298. OC4(B, B, B, U), // 0x5c-0x5f
  299. OC4(V, V, V, V), // 0x60-0x63
  300. OC4(V, V, V, V), // 0x64-0x67
  301. OC4(Q, Q, B, U), // 0x68-0x6b
  302. OC4(U, U, U, U), // 0x6c-0x6f
  303. OC4(B, B, B, B), // 0x70-0x73
  304. OC4(B, B, B, B), // 0x74-0x77
  305. OC4(B, B, B, B), // 0x78-0x7b
  306. OC4(B, B, B, B), // 0x7c-0x7f
  307. OC4(B, B, B, B), // 0x80-0x83
  308. OC4(B, B, B, B), // 0x84-0x87
  309. OC4(B, B, B, B), // 0x88-0x8b
  310. OC4(B, B, B, B), // 0x8c-0x8f
  311. OC4(B, B, B, B), // 0x90-0x93
  312. OC4(B, B, B, B), // 0x94-0x97
  313. OC4(B, B, B, B), // 0x98-0x9b
  314. OC4(B, B, B, B), // 0x9c-0x9f
  315. OC4(B, B, B, B), // 0xa0-0xa3
  316. OC4(B, B, B, B), // 0xa4-0xa7
  317. OC4(B, B, B, B), // 0xa8-0xab
  318. OC4(B, B, B, B), // 0xac-0xaf
  319. OC4(B, B, B, B), // 0xb0-0xb3
  320. OC4(B, B, B, B), // 0xb4-0xb7
  321. OC4(B, B, B, B), // 0xb8-0xbb
  322. OC4(B, B, B, B), // 0xbc-0xbf
  323. OC4(B, B, B, B), // 0xc0-0xc3
  324. OC4(B, B, B, B), // 0xc4-0xc7
  325. OC4(B, B, B, B), // 0xc8-0xcb
  326. OC4(B, B, B, B), // 0xcc-0xcf
  327. OC4(B, B, B, B), // 0xd0-0xd3
  328. OC4(U, U, U, B), // 0xd4-0xd7
  329. OC4(B, B, B, B), // 0xd8-0xdb
  330. OC4(B, B, B, B), // 0xdc-0xdf
  331. OC4(B, B, B, B), // 0xe0-0xe3
  332. OC4(B, B, B, B), // 0xe4-0xe7
  333. OC4(B, B, B, B), // 0xe8-0xeb
  334. OC4(B, B, B, B), // 0xec-0xef
  335. OC4(B, B, B, B), // 0xf0-0xf3
  336. OC4(B, B, B, B), // 0xf4-0xf7
  337. OC4(U, U, U, U), // 0xf8-0xfb
  338. OC4(U, U, U, U), // 0xfc-0xff
  339. };
  340. #undef OC4
  341. #undef U
  342. #undef B
  343. #undef Q
  344. #undef V
  345. #undef O
  346. uint mp_opcode_format(const byte *ip, size_t *opcode_size) {
  347. uint f = (opcode_format_table[*ip >> 2] >> (2 * (*ip & 3))) & 3;
  348. const byte *ip_start = ip;
  349. if (f == MP_OPCODE_QSTR) {
  350. ip += 3;
  351. } else {
  352. int extra_byte = (
  353. *ip == MP_BC_RAISE_VARARGS
  354. || *ip == MP_BC_MAKE_CLOSURE
  355. || *ip == MP_BC_MAKE_CLOSURE_DEFARGS
  356. #if MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE
  357. || *ip == MP_BC_LOAD_NAME
  358. || *ip == MP_BC_LOAD_GLOBAL
  359. || *ip == MP_BC_LOAD_ATTR
  360. || *ip == MP_BC_STORE_ATTR
  361. #endif
  362. );
  363. ip += 1;
  364. if (f == MP_OPCODE_VAR_UINT) {
  365. while ((*ip++ & 0x80) != 0) {
  366. }
  367. } else if (f == MP_OPCODE_OFFSET) {
  368. ip += 2;
  369. }
  370. ip += extra_byte;
  371. }
  372. *opcode_size = ip - ip_start;
  373. return f;
  374. }
  375. #endif // MICROPY_PERSISTENT_CODE_LOAD || MICROPY_PERSISTENT_CODE_SAVE