emitglue.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. #if MICROPY_PERSISTENT_CODE_SAVE || MICROPY_DEBUG_PRINTERS
  58. size_t len,
  59. #endif
  60. mp_raw_code_t **children,
  61. #if MICROPY_PERSISTENT_CODE_SAVE
  62. size_t n_children,
  63. #endif
  64. mp_uint_t scope_flags) {
  65. rc->kind = MP_CODE_BYTECODE;
  66. rc->scope_flags = scope_flags;
  67. rc->fun_data = code;
  68. #if MICROPY_PERSISTENT_CODE_SAVE || MICROPY_DEBUG_PRINTERS
  69. rc->fun_data_len = len;
  70. #endif
  71. rc->children = children;
  72. #if MICROPY_PERSISTENT_CODE_SAVE
  73. rc->n_children = n_children;
  74. #endif
  75. #if MICROPY_PY_SYS_SETTRACE
  76. mp_bytecode_prelude_t *prelude = &rc->prelude;
  77. mp_prof_extract_prelude(code, prelude);
  78. #endif
  79. #if DEBUG_PRINT
  80. #if !(MICROPY_PERSISTENT_CODE_SAVE || MICROPY_DEBUG_PRINTERS)
  81. const size_t len = 0;
  82. #endif
  83. DEBUG_printf("assign byte code: code=%p len=" UINT_FMT " flags=%x\n", code, len, (uint)scope_flags);
  84. #endif
  85. }
  86. #if MICROPY_EMIT_MACHINE_CODE
  87. void mp_emit_glue_assign_native(mp_raw_code_t *rc, mp_raw_code_kind_t kind, void *fun_data, mp_uint_t fun_len,
  88. mp_raw_code_t **children,
  89. #if MICROPY_PERSISTENT_CODE_SAVE
  90. size_t n_children,
  91. uint16_t prelude_offset,
  92. #endif
  93. mp_uint_t scope_flags, mp_uint_t n_pos_args, mp_uint_t type_sig) {
  94. assert(kind == MP_CODE_NATIVE_PY || kind == MP_CODE_NATIVE_VIPER || kind == MP_CODE_NATIVE_ASM);
  95. // Some architectures require flushing/invalidation of the I/D caches,
  96. // so that the generated native code which was created in data RAM will
  97. // be available for execution from instruction RAM.
  98. #if MICROPY_EMIT_THUMB || MICROPY_EMIT_INLINE_THUMB
  99. #if __ICACHE_PRESENT == 1
  100. // Flush D-cache, so the code emitted is stored in RAM.
  101. MP_HAL_CLEAN_DCACHE(fun_data, fun_len);
  102. // Invalidate I-cache, so the newly-created code is reloaded from RAM.
  103. SCB_InvalidateICache();
  104. #endif
  105. #elif MICROPY_EMIT_ARM
  106. #if (defined(__linux__) && defined(__GNUC__)) || __ARM_ARCH == 7
  107. __builtin___clear_cache(fun_data, (uint8_t *)fun_data + fun_len);
  108. #elif defined(__arm__)
  109. // Flush I-cache and D-cache.
  110. asm volatile (
  111. "0:"
  112. "mrc p15, 0, r15, c7, c10, 3\n" // test and clean D-cache
  113. "bne 0b\n"
  114. "mov r0, #0\n"
  115. "mcr p15, 0, r0, c7, c7, 0\n" // invalidate I-cache and D-cache
  116. : : : "r0", "cc");
  117. #endif
  118. #endif
  119. rc->kind = kind;
  120. rc->scope_flags = scope_flags;
  121. rc->fun_data = fun_data;
  122. #if MICROPY_PERSISTENT_CODE_SAVE || MICROPY_DEBUG_PRINTERS
  123. rc->fun_data_len = fun_len;
  124. #endif
  125. rc->children = children;
  126. #if MICROPY_PERSISTENT_CODE_SAVE
  127. rc->n_children = n_children;
  128. rc->prelude_offset = prelude_offset;
  129. #endif
  130. // These two entries are only needed for MP_CODE_NATIVE_ASM.
  131. rc->n_pos_args = n_pos_args;
  132. rc->type_sig = type_sig;
  133. #if DEBUG_PRINT
  134. DEBUG_printf("assign native: kind=%d fun=%p len=" UINT_FMT " n_pos_args=" UINT_FMT " flags=%x\n", kind, fun_data, fun_len, n_pos_args, (uint)scope_flags);
  135. for (mp_uint_t i = 0; i < fun_len; i++) {
  136. if (i > 0 && i % 16 == 0) {
  137. DEBUG_printf("\n");
  138. }
  139. DEBUG_printf(" %02x", ((byte *)fun_data)[i]);
  140. }
  141. DEBUG_printf("\n");
  142. #ifdef WRITE_CODE
  143. FILE *fp_write_code = fopen("out-code", "wb");
  144. fwrite(fun_data, fun_len, 1, fp_write_code);
  145. fclose(fp_write_code);
  146. #endif
  147. #else
  148. (void)fun_len;
  149. #endif
  150. }
  151. #endif
  152. mp_obj_t mp_make_function_from_raw_code(const mp_raw_code_t *rc, const mp_module_context_t *context, const mp_obj_t *def_args) {
  153. DEBUG_OP_printf("make_function_from_raw_code %p\n", rc);
  154. assert(rc != NULL);
  155. // def_args must be MP_OBJ_NULL or a tuple
  156. assert(def_args == NULL || def_args[0] == MP_OBJ_NULL || mp_obj_is_type(def_args[0], &mp_type_tuple));
  157. // def_kw_args must be MP_OBJ_NULL or a dict
  158. assert(def_args == NULL || def_args[1] == MP_OBJ_NULL || mp_obj_is_type(def_args[1], &mp_type_dict));
  159. // make the function, depending on the raw code kind
  160. mp_obj_t fun;
  161. switch (rc->kind) {
  162. #if MICROPY_EMIT_NATIVE
  163. case MP_CODE_NATIVE_PY:
  164. case MP_CODE_NATIVE_VIPER:
  165. fun = mp_obj_new_fun_native(def_args, rc->fun_data, context, rc->children);
  166. // Check for a generator function, and if so change the type of the object
  167. if ((rc->scope_flags & MP_SCOPE_FLAG_GENERATOR) != 0) {
  168. ((mp_obj_base_t *)MP_OBJ_TO_PTR(fun))->type = &mp_type_native_gen_wrap;
  169. }
  170. break;
  171. #endif
  172. #if MICROPY_EMIT_INLINE_ASM
  173. case MP_CODE_NATIVE_ASM:
  174. fun = mp_obj_new_fun_asm(rc->n_pos_args, rc->fun_data, rc->type_sig);
  175. break;
  176. #endif
  177. default:
  178. // rc->kind should always be set and BYTECODE is the only remaining case
  179. assert(rc->kind == MP_CODE_BYTECODE);
  180. fun = mp_obj_new_fun_bc(def_args, rc->fun_data, context, rc->children);
  181. // check for generator functions and if so change the type of the object
  182. if ((rc->scope_flags & MP_SCOPE_FLAG_GENERATOR) != 0) {
  183. ((mp_obj_base_t *)MP_OBJ_TO_PTR(fun))->type = &mp_type_gen_wrap;
  184. }
  185. #if MICROPY_PY_SYS_SETTRACE
  186. mp_obj_fun_bc_t *self_fun = (mp_obj_fun_bc_t *)MP_OBJ_TO_PTR(fun);
  187. self_fun->rc = rc;
  188. #endif
  189. break;
  190. }
  191. return fun;
  192. }
  193. mp_obj_t mp_make_closure_from_raw_code(const mp_raw_code_t *rc, const mp_module_context_t *context, mp_uint_t n_closed_over, const mp_obj_t *args) {
  194. DEBUG_OP_printf("make_closure_from_raw_code %p " UINT_FMT " %p\n", rc, n_closed_over, args);
  195. // make function object
  196. mp_obj_t ffun;
  197. if (n_closed_over & 0x100) {
  198. // default positional and keyword args given
  199. ffun = mp_make_function_from_raw_code(rc, context, args);
  200. } else {
  201. // default positional and keyword args not given
  202. ffun = mp_make_function_from_raw_code(rc, context, NULL);
  203. }
  204. // wrap function in closure object
  205. return mp_obj_new_closure(ffun, n_closed_over & 0xff, args + ((n_closed_over >> 7) & 2));
  206. }