emitglue.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /*
  2. * This file is part of the MicroPython 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. // This code glues the code emitters to the runtime.
  27. #include <stdint.h>
  28. #include <stdio.h>
  29. #include <string.h>
  30. #include <assert.h>
  31. #include "py/emitglue.h"
  32. #include "py/runtime0.h"
  33. #include "py/bc.h"
  34. #include "py/objfun.h"
  35. #include "py/profile.h"
  36. #if MICROPY_DEBUG_VERBOSE // print debugging info
  37. #define DEBUG_PRINT (1)
  38. #define WRITE_CODE (1)
  39. #define DEBUG_printf DEBUG_printf
  40. #define DEBUG_OP_printf(...) DEBUG_printf(__VA_ARGS__)
  41. #else // don't print debugging info
  42. #define DEBUG_printf(...) (void)0
  43. #define DEBUG_OP_printf(...) (void)0
  44. #endif
  45. #if MICROPY_DEBUG_PRINTERS
  46. mp_uint_t mp_verbose_flag = 0;
  47. #endif
  48. mp_raw_code_t *mp_emit_glue_new_raw_code(void) {
  49. mp_raw_code_t *rc = m_new0(mp_raw_code_t, 1);
  50. rc->kind = MP_CODE_RESERVED;
  51. #if MICROPY_PY_SYS_SETTRACE
  52. rc->line_of_definition = 0;
  53. #endif
  54. return rc;
  55. }
  56. void mp_emit_glue_assign_bytecode(mp_raw_code_t *rc, const byte *code,
  57. mp_raw_code_t **children,
  58. #if MICROPY_PERSISTENT_CODE_SAVE
  59. size_t len,
  60. uint16_t n_children,
  61. #endif
  62. uint16_t scope_flags) {
  63. rc->kind = MP_CODE_BYTECODE;
  64. rc->is_generator = (scope_flags & MP_SCOPE_FLAG_GENERATOR) != 0;
  65. rc->fun_data = code;
  66. rc->children = children;
  67. #if MICROPY_PERSISTENT_CODE_SAVE
  68. rc->fun_data_len = len;
  69. rc->n_children = n_children;
  70. #endif
  71. #if MICROPY_PY_SYS_SETTRACE
  72. mp_bytecode_prelude_t *prelude = &rc->prelude;
  73. mp_prof_extract_prelude(code, prelude);
  74. #endif
  75. #if DEBUG_PRINT
  76. #if !MICROPY_PERSISTENT_CODE_SAVE
  77. const size_t len = 0;
  78. #endif
  79. DEBUG_printf("assign byte code: code=%p len=" UINT_FMT " flags=%x\n", code, len, (uint)scope_flags);
  80. #endif
  81. }
  82. #if MICROPY_EMIT_MACHINE_CODE
  83. void mp_emit_glue_assign_native(mp_raw_code_t *rc, mp_raw_code_kind_t kind, const void *fun_data, mp_uint_t fun_len,
  84. mp_raw_code_t **children,
  85. #if MICROPY_PERSISTENT_CODE_SAVE
  86. uint16_t n_children,
  87. uint16_t prelude_offset,
  88. #endif
  89. uint16_t scope_flags, uint32_t asm_n_pos_args, uint32_t asm_type_sig
  90. ) {
  91. assert(kind == MP_CODE_NATIVE_PY || kind == MP_CODE_NATIVE_VIPER || kind == MP_CODE_NATIVE_ASM);
  92. // Some architectures require flushing/invalidation of the I/D caches,
  93. // so that the generated native code which was created in data RAM will
  94. // be available for execution from instruction RAM.
  95. #if MICROPY_EMIT_THUMB || MICROPY_EMIT_INLINE_THUMB
  96. #if __ICACHE_PRESENT == 1
  97. // Flush D-cache, so the code emitted is stored in RAM.
  98. MP_HAL_CLEAN_DCACHE(fun_data, fun_len);
  99. // Invalidate I-cache, so the newly-created code is reloaded from RAM.
  100. SCB_InvalidateICache();
  101. #endif
  102. #elif MICROPY_EMIT_ARM
  103. #if (defined(__linux__) && defined(__GNUC__)) || __ARM_ARCH == 7
  104. __builtin___clear_cache((void *)fun_data, (uint8_t *)fun_data + fun_len);
  105. #elif defined(__arm__)
  106. // Flush I-cache and D-cache.
  107. asm volatile (
  108. "0:"
  109. "mrc p15, 0, r15, c7, c10, 3\n" // test and clean D-cache
  110. "bne 0b\n"
  111. "mov r0, #0\n"
  112. "mcr p15, 0, r0, c7, c7, 0\n" // invalidate I-cache and D-cache
  113. : : : "r0", "cc");
  114. #endif
  115. #endif
  116. rc->kind = kind;
  117. rc->is_generator = (scope_flags & MP_SCOPE_FLAG_GENERATOR) != 0;
  118. rc->fun_data = fun_data;
  119. #if MICROPY_PERSISTENT_CODE_SAVE
  120. rc->fun_data_len = fun_len;
  121. #endif
  122. rc->children = children;
  123. #if MICROPY_PERSISTENT_CODE_SAVE
  124. rc->n_children = n_children;
  125. rc->prelude_offset = prelude_offset;
  126. #endif
  127. #if MICROPY_EMIT_INLINE_ASM
  128. // These two entries are only needed for MP_CODE_NATIVE_ASM.
  129. rc->asm_n_pos_args = asm_n_pos_args;
  130. rc->asm_type_sig = asm_type_sig;
  131. #endif
  132. #if DEBUG_PRINT
  133. DEBUG_printf("assign native: kind=%d fun=%p len=" UINT_FMT " flags=%x\n", kind, fun_data, fun_len, (uint)scope_flags);
  134. for (mp_uint_t i = 0; i < fun_len; i++) {
  135. if (i > 0 && i % 16 == 0) {
  136. DEBUG_printf("\n");
  137. }
  138. DEBUG_printf(" %02x", ((const byte *)fun_data)[i]);
  139. }
  140. DEBUG_printf("\n");
  141. #ifdef WRITE_CODE
  142. FILE *fp_write_code = fopen("out-code", "wb");
  143. fwrite(fun_data, fun_len, 1, fp_write_code);
  144. fclose(fp_write_code);
  145. #endif
  146. #else
  147. (void)fun_len;
  148. #endif
  149. }
  150. #endif
  151. mp_obj_t mp_make_function_from_proto_fun(mp_proto_fun_t proto_fun, const mp_module_context_t *context, const mp_obj_t *def_args) {
  152. DEBUG_OP_printf("make_function_from_proto_fun %p\n", proto_fun);
  153. assert(proto_fun != NULL);
  154. // def_args must be MP_OBJ_NULL or a tuple
  155. assert(def_args == NULL || def_args[0] == MP_OBJ_NULL || mp_obj_is_type(def_args[0], &mp_type_tuple));
  156. // def_kw_args must be MP_OBJ_NULL or a dict
  157. assert(def_args == NULL || def_args[1] == MP_OBJ_NULL || mp_obj_is_type(def_args[1], &mp_type_dict));
  158. #if MICROPY_MODULE_FROZEN_MPY
  159. if (mp_proto_fun_is_bytecode(proto_fun)) {
  160. const uint8_t *bc = proto_fun;
  161. mp_obj_t fun = mp_obj_new_fun_bc(def_args, bc, context, NULL);
  162. MP_BC_PRELUDE_SIG_DECODE(bc);
  163. if (scope_flags & MP_SCOPE_FLAG_GENERATOR) {
  164. ((mp_obj_base_t *)MP_OBJ_TO_PTR(fun))->type = &mp_type_gen_wrap;
  165. }
  166. return fun;
  167. }
  168. #endif
  169. // the proto-function is a mp_raw_code_t
  170. const mp_raw_code_t *rc = proto_fun;
  171. // make the function, depending on the raw code kind
  172. mp_obj_t fun;
  173. switch (rc->kind) {
  174. #if MICROPY_EMIT_NATIVE
  175. case MP_CODE_NATIVE_PY:
  176. fun = mp_obj_new_fun_native(def_args, rc->fun_data, context, rc->children);
  177. // Check for a generator function, and if so change the type of the object
  178. if (rc->is_generator) {
  179. ((mp_obj_base_t *)MP_OBJ_TO_PTR(fun))->type = &mp_type_native_gen_wrap;
  180. }
  181. break;
  182. case MP_CODE_NATIVE_VIPER:
  183. fun = mp_obj_new_fun_viper(rc->fun_data, context, rc->children);
  184. break;
  185. #endif
  186. #if MICROPY_EMIT_INLINE_ASM
  187. case MP_CODE_NATIVE_ASM:
  188. fun = mp_obj_new_fun_asm(rc->asm_n_pos_args, rc->fun_data, rc->asm_type_sig);
  189. break;
  190. #endif
  191. default:
  192. // rc->kind should always be set and BYTECODE is the only remaining case
  193. assert(rc->kind == MP_CODE_BYTECODE);
  194. fun = mp_obj_new_fun_bc(def_args, rc->fun_data, context, rc->children);
  195. // check for generator functions and if so change the type of the object
  196. if (rc->is_generator) {
  197. ((mp_obj_base_t *)MP_OBJ_TO_PTR(fun))->type = &mp_type_gen_wrap;
  198. }
  199. #if MICROPY_PY_SYS_SETTRACE
  200. mp_obj_fun_bc_t *self_fun = (mp_obj_fun_bc_t *)MP_OBJ_TO_PTR(fun);
  201. self_fun->rc = rc;
  202. #endif
  203. break;
  204. }
  205. return fun;
  206. }
  207. mp_obj_t mp_make_closure_from_proto_fun(mp_proto_fun_t proto_fun, const mp_module_context_t *context, mp_uint_t n_closed_over, const mp_obj_t *args) {
  208. DEBUG_OP_printf("make_closure_from_proto_fun %p " UINT_FMT " %p\n", proto_fun, n_closed_over, args);
  209. // make function object
  210. mp_obj_t ffun;
  211. if (n_closed_over & 0x100) {
  212. // default positional and keyword args given
  213. ffun = mp_make_function_from_proto_fun(proto_fun, context, args);
  214. } else {
  215. // default positional and keyword args not given
  216. ffun = mp_make_function_from_proto_fun(proto_fun, context, NULL);
  217. }
  218. // wrap function in closure object
  219. return mp_obj_new_closure(ffun, n_closed_over & 0xff, args + ((n_closed_over >> 7) & 2));
  220. }