emitglue.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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/profile.h"
  35. #if MICROPY_DEBUG_VERBOSE // print debugging info
  36. #define DEBUG_PRINT (1)
  37. #define WRITE_CODE (1)
  38. #define DEBUG_printf DEBUG_printf
  39. #define DEBUG_OP_printf(...) DEBUG_printf(__VA_ARGS__)
  40. #else // don't print debugging info
  41. #define DEBUG_printf(...) (void)0
  42. #define DEBUG_OP_printf(...) (void)0
  43. #endif
  44. #if MICROPY_DEBUG_PRINTERS
  45. mp_uint_t mp_verbose_flag = 0;
  46. #endif
  47. mp_raw_code_t *mp_emit_glue_new_raw_code(void) {
  48. mp_raw_code_t *rc = m_new0(mp_raw_code_t, 1);
  49. rc->kind = MP_CODE_RESERVED;
  50. #if MICROPY_PY_SYS_SETTRACE
  51. rc->line_of_definition = 0;
  52. #endif
  53. return rc;
  54. }
  55. void mp_emit_glue_assign_bytecode(mp_raw_code_t *rc, const byte *code,
  56. #if MICROPY_PERSISTENT_CODE_SAVE || MICROPY_DEBUG_PRINTERS
  57. size_t len,
  58. #endif
  59. const mp_uint_t *const_table,
  60. #if MICROPY_PERSISTENT_CODE_SAVE
  61. uint16_t n_obj, uint16_t n_raw_code,
  62. #endif
  63. mp_uint_t scope_flags) {
  64. rc->kind = MP_CODE_BYTECODE;
  65. rc->scope_flags = scope_flags;
  66. rc->fun_data = code;
  67. rc->const_table = const_table;
  68. #if MICROPY_PERSISTENT_CODE_SAVE
  69. rc->fun_data_len = len;
  70. rc->n_obj = n_obj;
  71. rc->n_raw_code = n_raw_code;
  72. #endif
  73. #if MICROPY_PY_SYS_SETTRACE
  74. mp_bytecode_prelude_t *prelude = &rc->prelude;
  75. mp_prof_extract_prelude(code, prelude);
  76. #endif
  77. #ifdef DEBUG_PRINT
  78. #if !MICROPY_DEBUG_PRINTERS
  79. const size_t len = 0;
  80. #endif
  81. DEBUG_printf("assign byte code: code=%p len=" UINT_FMT " flags=%x\n", code, len, (uint)scope_flags);
  82. #endif
  83. #if MICROPY_DEBUG_PRINTERS
  84. if (mp_verbose_flag >= 2) {
  85. mp_bytecode_print(rc, code, len, const_table);
  86. }
  87. #endif
  88. }
  89. #if MICROPY_EMIT_MACHINE_CODE
  90. 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, const mp_uint_t *const_table,
  91. #if MICROPY_PERSISTENT_CODE_SAVE
  92. uint16_t prelude_offset,
  93. uint16_t n_obj, uint16_t n_raw_code,
  94. uint16_t n_qstr, mp_qstr_link_entry_t *qstr_link,
  95. #endif
  96. mp_uint_t n_pos_args, mp_uint_t scope_flags, mp_uint_t type_sig) {
  97. assert(kind == MP_CODE_NATIVE_PY || kind == MP_CODE_NATIVE_VIPER || kind == MP_CODE_NATIVE_ASM);
  98. rc->kind = kind;
  99. rc->scope_flags = scope_flags;
  100. rc->n_pos_args = n_pos_args;
  101. rc->fun_data = fun_data;
  102. rc->const_table = const_table;
  103. rc->type_sig = type_sig;
  104. #if MICROPY_PERSISTENT_CODE_SAVE
  105. rc->fun_data_len = fun_len;
  106. rc->prelude_offset = prelude_offset;
  107. rc->n_obj = n_obj;
  108. rc->n_raw_code = n_raw_code;
  109. rc->n_qstr= n_qstr;
  110. rc->qstr_link = qstr_link;
  111. #endif
  112. #ifdef DEBUG_PRINT
  113. 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);
  114. for (mp_uint_t i = 0; i < fun_len; i++) {
  115. if (i > 0 && i % 16 == 0) {
  116. DEBUG_printf("\n");
  117. }
  118. DEBUG_printf(" %02x", ((byte*)fun_data)[i]);
  119. }
  120. DEBUG_printf("\n");
  121. #ifdef WRITE_CODE
  122. FILE *fp_write_code = fopen("out-code", "wb");
  123. fwrite(fun_data, fun_len, 1, fp_write_code);
  124. fclose(fp_write_code);
  125. #endif
  126. #else
  127. (void)fun_len;
  128. #endif
  129. }
  130. #endif
  131. mp_obj_t mp_make_function_from_raw_code(const mp_raw_code_t *rc, mp_obj_t def_args, mp_obj_t def_kw_args) {
  132. DEBUG_OP_printf("make_function_from_raw_code %p\n", rc);
  133. assert(rc != NULL);
  134. // def_args must be MP_OBJ_NULL or a tuple
  135. assert(def_args == MP_OBJ_NULL || mp_obj_is_type(def_args, &mp_type_tuple));
  136. // def_kw_args must be MP_OBJ_NULL or a dict
  137. assert(def_kw_args == MP_OBJ_NULL || mp_obj_is_type(def_kw_args, &mp_type_dict));
  138. // make the function, depending on the raw code kind
  139. mp_obj_t fun;
  140. switch (rc->kind) {
  141. #if MICROPY_EMIT_NATIVE
  142. case MP_CODE_NATIVE_PY:
  143. case MP_CODE_NATIVE_VIPER:
  144. fun = mp_obj_new_fun_native(def_args, def_kw_args, rc->fun_data, rc->const_table);
  145. // Check for a generator function, and if so change the type of the object
  146. if ((rc->scope_flags & MP_SCOPE_FLAG_GENERATOR) != 0) {
  147. ((mp_obj_base_t*)MP_OBJ_TO_PTR(fun))->type = &mp_type_native_gen_wrap;
  148. }
  149. break;
  150. #endif
  151. #if MICROPY_EMIT_INLINE_ASM
  152. case MP_CODE_NATIVE_ASM:
  153. fun = mp_obj_new_fun_asm(rc->n_pos_args, rc->fun_data, rc->type_sig);
  154. break;
  155. #endif
  156. default:
  157. // rc->kind should always be set and BYTECODE is the only remaining case
  158. assert(rc->kind == MP_CODE_BYTECODE);
  159. fun = mp_obj_new_fun_bc(def_args, def_kw_args, rc->fun_data, rc->const_table);
  160. // check for generator functions and if so change the type of the object
  161. if ((rc->scope_flags & MP_SCOPE_FLAG_GENERATOR) != 0) {
  162. ((mp_obj_base_t*)MP_OBJ_TO_PTR(fun))->type = &mp_type_gen_wrap;
  163. }
  164. #if MICROPY_PY_SYS_SETTRACE
  165. mp_obj_fun_bc_t *self_fun = (mp_obj_fun_bc_t *)MP_OBJ_TO_PTR(fun);
  166. self_fun->rc = rc;
  167. #endif
  168. break;
  169. }
  170. return fun;
  171. }
  172. mp_obj_t mp_make_closure_from_raw_code(const mp_raw_code_t *rc, mp_uint_t n_closed_over, const mp_obj_t *args) {
  173. DEBUG_OP_printf("make_closure_from_raw_code %p " UINT_FMT " %p\n", rc, n_closed_over, args);
  174. // make function object
  175. mp_obj_t ffun;
  176. if (n_closed_over & 0x100) {
  177. // default positional and keyword args given
  178. ffun = mp_make_function_from_raw_code(rc, args[0], args[1]);
  179. } else {
  180. // default positional and keyword args not given
  181. ffun = mp_make_function_from_raw_code(rc, MP_OBJ_NULL, MP_OBJ_NULL);
  182. }
  183. // wrap function in closure object
  184. return mp_obj_new_closure(ffun, n_closed_over & 0xff, args + ((n_closed_over >> 7) & 2));
  185. }