bc.c 16 KB

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