bc.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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/bc0.h"
  31. #include "py/bc.h"
  32. #include "py/objfun.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. void mp_encode_uint(void *env, mp_encode_uint_allocator_t allocator, mp_uint_t val) {
  40. // We store each 7 bits in a separate byte, and that's how many bytes needed
  41. byte buf[MP_ENCODE_UINT_MAX_BYTES];
  42. byte *p = buf + sizeof(buf);
  43. // We encode in little-ending order, but store in big-endian, to help decoding
  44. do {
  45. *--p = val & 0x7f;
  46. val >>= 7;
  47. } while (val != 0);
  48. byte *c = allocator(env, buf + sizeof(buf) - p);
  49. if (c != NULL) {
  50. while (p != buf + sizeof(buf) - 1) {
  51. *c++ = *p++ | 0x80;
  52. }
  53. *c = *p;
  54. }
  55. }
  56. mp_uint_t mp_decode_uint(const byte **ptr) {
  57. mp_uint_t unum = 0;
  58. byte val;
  59. const byte *p = *ptr;
  60. do {
  61. val = *p++;
  62. unum = (unum << 7) | (val & 0x7f);
  63. } while ((val & 0x80) != 0);
  64. *ptr = p;
  65. return unum;
  66. }
  67. // This function is used to help reduce stack usage at the caller, for the case when
  68. // the caller doesn't need to increase the ptr argument. If ptr is a local variable
  69. // and the caller uses mp_decode_uint(&ptr) instead of this function, then the compiler
  70. // must allocate a slot on the stack for ptr, and this slot cannot be reused for
  71. // anything else in the function because the pointer may have been stored in a global
  72. // and reused later in the function.
  73. mp_uint_t mp_decode_uint_value(const byte *ptr) {
  74. return mp_decode_uint(&ptr);
  75. }
  76. // This function is used to help reduce stack usage at the caller, for the case when
  77. // the caller doesn't need the actual value and just wants to skip over it.
  78. const byte *mp_decode_uint_skip(const byte *ptr) {
  79. while ((*ptr++) & 0x80) {
  80. }
  81. return ptr;
  82. }
  83. static NORETURN void fun_pos_args_mismatch(mp_obj_fun_bc_t *f, size_t expected, size_t given) {
  84. #if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE
  85. // generic message, used also for other argument issues
  86. (void)f;
  87. (void)expected;
  88. (void)given;
  89. mp_arg_error_terse_mismatch();
  90. #elif MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_NORMAL
  91. (void)f;
  92. mp_raise_msg_varg(&mp_type_TypeError,
  93. MP_ERROR_TEXT("function takes %d positional arguments but %d were given"), expected, given);
  94. #elif MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_DETAILED
  95. mp_raise_msg_varg(&mp_type_TypeError,
  96. MP_ERROR_TEXT("%q() takes %d positional arguments but %d were given"),
  97. mp_obj_fun_get_name(MP_OBJ_FROM_PTR(f)), expected, given);
  98. #endif
  99. }
  100. #if DEBUG_PRINT
  101. static void dump_args(const mp_obj_t *a, size_t sz) {
  102. DEBUG_printf("%p: ", a);
  103. for (size_t i = 0; i < sz; i++) {
  104. DEBUG_printf("%p ", a[i]);
  105. }
  106. DEBUG_printf("\n");
  107. }
  108. #else
  109. #define dump_args(...) (void)0
  110. #endif
  111. // On entry code_state should be allocated somewhere (stack/heap) and
  112. // contain the following valid entries:
  113. // - code_state->fun_bc should contain a pointer to the function object
  114. // - code_state->ip should contain a pointer to the beginning of the prelude
  115. // - code_state->sp should be: &code_state->state[0] - 1
  116. // - code_state->n_state should be the number of objects in the local state
  117. static void mp_setup_code_state_helper(mp_code_state_t *code_state, size_t n_args, size_t n_kw, const mp_obj_t *args) {
  118. // This function is pretty complicated. It's main aim is to be efficient in speed and RAM
  119. // usage for the common case of positional only args.
  120. // get the function object that we want to set up (could be bytecode or native code)
  121. mp_obj_fun_bc_t *self = code_state->fun_bc;
  122. // Get cached n_state (rather than decode it again)
  123. size_t n_state = code_state->n_state;
  124. // Decode prelude
  125. size_t n_state_unused, n_exc_stack_unused, scope_flags, n_pos_args, n_kwonly_args, n_def_pos_args;
  126. MP_BC_PRELUDE_SIG_DECODE_INTO(code_state->ip, n_state_unused, n_exc_stack_unused, scope_flags, n_pos_args, n_kwonly_args, n_def_pos_args);
  127. MP_BC_PRELUDE_SIZE_DECODE(code_state->ip);
  128. (void)n_state_unused;
  129. (void)n_exc_stack_unused;
  130. mp_obj_t *code_state_state = code_state->sp + 1;
  131. code_state->exc_sp_idx = 0;
  132. // zero out the local stack to begin with
  133. memset(code_state_state, 0, n_state * sizeof(*code_state->state));
  134. const mp_obj_t *kwargs = args + n_args;
  135. // var_pos_kw_args points to the stack where the var-args tuple, and var-kw dict, should go (if they are needed)
  136. mp_obj_t *var_pos_kw_args = &code_state_state[n_state - 1 - n_pos_args - n_kwonly_args];
  137. // check positional arguments
  138. if (n_args > n_pos_args) {
  139. // given more than enough arguments
  140. if ((scope_flags & MP_SCOPE_FLAG_VARARGS) == 0) {
  141. fun_pos_args_mismatch(self, n_pos_args, n_args);
  142. }
  143. // put extra arguments in varargs tuple
  144. *var_pos_kw_args-- = mp_obj_new_tuple(n_args - n_pos_args, args + n_pos_args);
  145. n_args = n_pos_args;
  146. } else {
  147. if ((scope_flags & MP_SCOPE_FLAG_VARARGS) != 0) {
  148. DEBUG_printf("passing empty tuple as *args\n");
  149. *var_pos_kw_args-- = mp_const_empty_tuple;
  150. }
  151. // Apply processing and check below only if we don't have kwargs,
  152. // otherwise, kw handling code below has own extensive checks.
  153. if (n_kw == 0 && (scope_flags & MP_SCOPE_FLAG_DEFKWARGS) == 0) {
  154. if (n_args >= (size_t)(n_pos_args - n_def_pos_args)) {
  155. // given enough arguments, but may need to use some default arguments
  156. for (size_t i = n_args; i < n_pos_args; i++) {
  157. code_state_state[n_state - 1 - i] = self->extra_args[i - (n_pos_args - n_def_pos_args)];
  158. }
  159. } else {
  160. fun_pos_args_mismatch(self, n_pos_args - n_def_pos_args, n_args);
  161. }
  162. }
  163. }
  164. // copy positional args into state
  165. for (size_t i = 0; i < n_args; i++) {
  166. code_state_state[n_state - 1 - i] = args[i];
  167. }
  168. // check keyword arguments
  169. if (n_kw != 0 || (scope_flags & MP_SCOPE_FLAG_DEFKWARGS) != 0) {
  170. DEBUG_printf("Initial args: ");
  171. dump_args(code_state_state + n_state - n_pos_args - n_kwonly_args, n_pos_args + n_kwonly_args);
  172. mp_obj_t dict = MP_OBJ_NULL;
  173. if ((scope_flags & MP_SCOPE_FLAG_VARKEYWORDS) != 0) {
  174. dict = mp_obj_new_dict(n_kw); // TODO: better go conservative with 0?
  175. *var_pos_kw_args = dict;
  176. }
  177. for (size_t i = 0; i < n_kw; i++) {
  178. // the keys in kwargs are expected to be qstr objects
  179. mp_obj_t wanted_arg_name = kwargs[2 * i];
  180. // get pointer to arg_names array
  181. const uint8_t *arg_names = code_state->ip;
  182. arg_names = mp_decode_uint_skip(arg_names);
  183. for (size_t j = 0; j < n_pos_args + n_kwonly_args; j++) {
  184. qstr arg_qstr = mp_decode_uint(&arg_names);
  185. #if MICROPY_EMIT_BYTECODE_USES_QSTR_TABLE
  186. arg_qstr = self->context->constants.qstr_table[arg_qstr];
  187. #endif
  188. if (wanted_arg_name == MP_OBJ_NEW_QSTR(arg_qstr)) {
  189. if (code_state_state[n_state - 1 - j] != MP_OBJ_NULL) {
  190. error_multiple:
  191. mp_raise_msg_varg(&mp_type_TypeError,
  192. MP_ERROR_TEXT("function got multiple values for argument '%q'"), MP_OBJ_QSTR_VALUE(wanted_arg_name));
  193. }
  194. code_state_state[n_state - 1 - j] = kwargs[2 * i + 1];
  195. goto continue2;
  196. }
  197. }
  198. // Didn't find name match with positional args
  199. if ((scope_flags & MP_SCOPE_FLAG_VARKEYWORDS) == 0) {
  200. #if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE
  201. mp_raise_TypeError(MP_ERROR_TEXT("unexpected keyword argument"));
  202. #else
  203. mp_raise_msg_varg(&mp_type_TypeError,
  204. MP_ERROR_TEXT("unexpected keyword argument '%q'"), MP_OBJ_QSTR_VALUE(wanted_arg_name));
  205. #endif
  206. }
  207. mp_map_elem_t *elem = mp_map_lookup(mp_obj_dict_get_map(dict), wanted_arg_name, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
  208. if (elem->value == MP_OBJ_NULL) {
  209. elem->value = kwargs[2 * i + 1];
  210. } else {
  211. goto error_multiple;
  212. }
  213. continue2:;
  214. }
  215. DEBUG_printf("Args with kws flattened: ");
  216. dump_args(code_state_state + n_state - n_pos_args - n_kwonly_args, n_pos_args + n_kwonly_args);
  217. // fill in defaults for positional args
  218. mp_obj_t *d = &code_state_state[n_state - n_pos_args];
  219. mp_obj_t *s = &self->extra_args[n_def_pos_args - 1];
  220. for (size_t i = n_def_pos_args; i > 0; i--, d++, s--) {
  221. if (*d == MP_OBJ_NULL) {
  222. *d = *s;
  223. }
  224. }
  225. DEBUG_printf("Args after filling default positional: ");
  226. dump_args(code_state_state + n_state - n_pos_args - n_kwonly_args, n_pos_args + n_kwonly_args);
  227. // Check that all mandatory positional args are specified
  228. while (d < &code_state_state[n_state]) {
  229. if (*d++ == MP_OBJ_NULL) {
  230. mp_raise_msg_varg(&mp_type_TypeError,
  231. MP_ERROR_TEXT("function missing required positional argument #%d"), &code_state_state[n_state] - d);
  232. }
  233. }
  234. // Check that all mandatory keyword args are specified
  235. // Fill in default kw args if we have them
  236. const uint8_t *arg_names = mp_decode_uint_skip(code_state->ip);
  237. for (size_t i = 0; i < n_pos_args; i++) {
  238. arg_names = mp_decode_uint_skip(arg_names);
  239. }
  240. for (size_t i = 0; i < n_kwonly_args; i++) {
  241. qstr arg_qstr = mp_decode_uint(&arg_names);
  242. #if MICROPY_EMIT_BYTECODE_USES_QSTR_TABLE
  243. arg_qstr = self->context->constants.qstr_table[arg_qstr];
  244. #endif
  245. if (code_state_state[n_state - 1 - n_pos_args - i] == MP_OBJ_NULL) {
  246. mp_map_elem_t *elem = NULL;
  247. if ((scope_flags & MP_SCOPE_FLAG_DEFKWARGS) != 0) {
  248. elem = mp_map_lookup(&((mp_obj_dict_t *)MP_OBJ_TO_PTR(self->extra_args[n_def_pos_args]))->map, MP_OBJ_NEW_QSTR(arg_qstr), MP_MAP_LOOKUP);
  249. }
  250. if (elem != NULL) {
  251. code_state_state[n_state - 1 - n_pos_args - i] = elem->value;
  252. } else {
  253. mp_raise_msg_varg(&mp_type_TypeError,
  254. MP_ERROR_TEXT("function missing required keyword argument '%q'"), arg_qstr);
  255. }
  256. }
  257. }
  258. } else {
  259. // no keyword arguments given
  260. if (n_kwonly_args != 0) {
  261. mp_raise_TypeError(MP_ERROR_TEXT("function missing keyword-only argument"));
  262. }
  263. if ((scope_flags & MP_SCOPE_FLAG_VARKEYWORDS) != 0) {
  264. *var_pos_kw_args = mp_obj_new_dict(0);
  265. }
  266. }
  267. // jump over code info (source file, argument names and line-number mapping)
  268. const uint8_t *ip = code_state->ip + n_info;
  269. // bytecode prelude: initialise closed over variables
  270. for (; n_cell; --n_cell) {
  271. size_t local_num = *ip++;
  272. code_state_state[n_state - 1 - local_num] =
  273. mp_obj_new_cell(code_state_state[n_state - 1 - local_num]);
  274. }
  275. // now that we skipped over the prelude, set the ip for the VM
  276. code_state->ip = ip;
  277. DEBUG_printf("Calling: n_pos_args=%d, n_kwonly_args=%d\n", n_pos_args, n_kwonly_args);
  278. dump_args(code_state_state + n_state - n_pos_args - n_kwonly_args, n_pos_args + n_kwonly_args);
  279. dump_args(code_state_state, n_state);
  280. }
  281. // On entry code_state should be allocated somewhere (stack/heap) and
  282. // contain the following valid entries:
  283. // - code_state->fun_bc should contain a pointer to the function object
  284. // - code_state->n_state should be the number of objects in the local state
  285. void mp_setup_code_state(mp_code_state_t *code_state, size_t n_args, size_t n_kw, const mp_obj_t *args) {
  286. code_state->ip = code_state->fun_bc->bytecode;
  287. code_state->sp = &code_state->state[0] - 1;
  288. #if MICROPY_STACKLESS
  289. code_state->prev = NULL;
  290. #endif
  291. #if MICROPY_PY_SYS_SETTRACE
  292. code_state->prev_state = NULL;
  293. code_state->frame = NULL;
  294. #endif
  295. mp_setup_code_state_helper(code_state, n_args, n_kw, args);
  296. }
  297. #if MICROPY_EMIT_NATIVE
  298. // On entry code_state should be allocated somewhere (stack/heap) and
  299. // contain the following valid entries:
  300. // - code_state->fun_bc should contain a pointer to the function object
  301. // - code_state->n_state should be the number of objects in the local state
  302. void mp_setup_code_state_native(mp_code_state_native_t *code_state, size_t n_args, size_t n_kw, const mp_obj_t *args) {
  303. code_state->ip = mp_obj_fun_native_get_prelude_ptr(code_state->fun_bc);
  304. code_state->sp = &code_state->state[0] - 1;
  305. mp_setup_code_state_helper((mp_code_state_t *)code_state, n_args, n_kw, args);
  306. }
  307. #endif