nativeglue.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. *
  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 <stdio.h>
  27. #include <string.h>
  28. #include <assert.h>
  29. #include "py/runtime.h"
  30. #include "py/smallint.h"
  31. #include "py/emitglue.h"
  32. #include "py/bc.h"
  33. #if MICROPY_DEBUG_VERBOSE // print debugging info
  34. #define DEBUG_printf DEBUG_printf
  35. #else // don't print debugging info
  36. #define DEBUG_printf(...) (void)0
  37. #endif
  38. #if MICROPY_EMIT_NATIVE
  39. // convert a MicroPython object to a valid native value based on type
  40. mp_uint_t mp_convert_obj_to_native(mp_obj_t obj, mp_uint_t type) {
  41. DEBUG_printf("mp_convert_obj_to_native(%p, " UINT_FMT ")\n", obj, type);
  42. switch (type & 0xf) {
  43. case MP_NATIVE_TYPE_OBJ: return (mp_uint_t)obj;
  44. case MP_NATIVE_TYPE_BOOL:
  45. case MP_NATIVE_TYPE_INT:
  46. case MP_NATIVE_TYPE_UINT: return mp_obj_get_int_truncated(obj);
  47. default: { // cast obj to a pointer
  48. mp_buffer_info_t bufinfo;
  49. if (mp_get_buffer(obj, &bufinfo, MP_BUFFER_RW)) {
  50. return (mp_uint_t)bufinfo.buf;
  51. } else {
  52. // assume obj is an integer that represents an address
  53. return mp_obj_get_int_truncated(obj);
  54. }
  55. }
  56. }
  57. }
  58. #endif
  59. #if MICROPY_EMIT_NATIVE || MICROPY_EMIT_INLINE_ASM
  60. // convert a native value to a MicroPython object based on type
  61. mp_obj_t mp_convert_native_to_obj(mp_uint_t val, mp_uint_t type) {
  62. DEBUG_printf("mp_convert_native_to_obj(" UINT_FMT ", " UINT_FMT ")\n", val, type);
  63. switch (type & 0xf) {
  64. case MP_NATIVE_TYPE_OBJ: return (mp_obj_t)val;
  65. case MP_NATIVE_TYPE_BOOL: return mp_obj_new_bool(val);
  66. case MP_NATIVE_TYPE_INT: return mp_obj_new_int(val);
  67. case MP_NATIVE_TYPE_UINT: return mp_obj_new_int_from_uint(val);
  68. default: // a pointer
  69. // we return just the value of the pointer as an integer
  70. return mp_obj_new_int_from_uint(val);
  71. }
  72. }
  73. #endif
  74. #if MICROPY_EMIT_NATIVE
  75. // wrapper that accepts n_args and n_kw in one argument
  76. // (native emitter can only pass at most 3 arguments to a function)
  77. mp_obj_t mp_native_call_function_n_kw(mp_obj_t fun_in, size_t n_args_kw, const mp_obj_t *args) {
  78. return mp_call_function_n_kw(fun_in, n_args_kw & 0xff, (n_args_kw >> 8) & 0xff, args);
  79. }
  80. // wrapper that makes raise obj and raises it
  81. // END_FINALLY opcode requires that we don't raise if o==None
  82. void mp_native_raise(mp_obj_t o) {
  83. if (o != mp_const_none) {
  84. nlr_raise(mp_make_raise_obj(o));
  85. }
  86. }
  87. // wrapper that handles iterator buffer
  88. STATIC mp_obj_t mp_native_getiter(mp_obj_t obj, mp_obj_iter_buf_t *iter) {
  89. if (iter == NULL) {
  90. return mp_getiter(obj, NULL);
  91. } else {
  92. obj = mp_getiter(obj, iter);
  93. if (obj != MP_OBJ_FROM_PTR(iter)) {
  94. // Iterator didn't use the stack so indicate that with MP_OBJ_NULL.
  95. iter->base.type = MP_OBJ_NULL;
  96. iter->buf[0] = obj;
  97. }
  98. return NULL;
  99. }
  100. }
  101. // wrapper that handles iterator buffer
  102. STATIC mp_obj_t mp_native_iternext(mp_obj_iter_buf_t *iter) {
  103. mp_obj_t obj;
  104. if (iter->base.type == MP_OBJ_NULL) {
  105. obj = iter->buf[0];
  106. } else {
  107. obj = MP_OBJ_FROM_PTR(iter);
  108. }
  109. return mp_iternext(obj);
  110. }
  111. // these must correspond to the respective enum in runtime0.h
  112. void *const mp_fun_table[MP_F_NUMBER_OF] = {
  113. mp_convert_obj_to_native,
  114. mp_convert_native_to_obj,
  115. mp_load_name,
  116. mp_load_global,
  117. mp_load_build_class,
  118. mp_load_attr,
  119. mp_load_method,
  120. mp_load_super_method,
  121. mp_store_name,
  122. mp_store_global,
  123. mp_store_attr,
  124. mp_obj_subscr,
  125. mp_obj_is_true,
  126. mp_unary_op,
  127. mp_binary_op,
  128. mp_obj_new_tuple,
  129. mp_obj_new_list,
  130. mp_obj_list_append,
  131. mp_obj_new_dict,
  132. mp_obj_dict_store,
  133. #if MICROPY_PY_BUILTINS_SET
  134. mp_obj_set_store,
  135. mp_obj_new_set,
  136. #endif
  137. mp_make_function_from_raw_code,
  138. mp_native_call_function_n_kw,
  139. mp_call_method_n_kw,
  140. mp_call_method_n_kw_var,
  141. mp_native_getiter,
  142. mp_native_iternext,
  143. nlr_push,
  144. nlr_pop,
  145. mp_native_raise,
  146. mp_import_name,
  147. mp_import_from,
  148. mp_import_all,
  149. #if MICROPY_PY_BUILTINS_SLICE
  150. mp_obj_new_slice,
  151. #endif
  152. mp_unpack_sequence,
  153. mp_unpack_ex,
  154. mp_delete_name,
  155. mp_delete_global,
  156. mp_obj_new_cell,
  157. mp_make_closure_from_raw_code,
  158. mp_setup_code_state,
  159. mp_small_int_floor_divide,
  160. mp_small_int_modulo,
  161. };
  162. /*
  163. void mp_f_vector(mp_fun_kind_t fun_kind) {
  164. (mp_f_table[fun_kind])();
  165. }
  166. */
  167. #endif // MICROPY_EMIT_NATIVE