emitglue.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. #ifndef MICROPY_INCLUDED_PY_EMITGLUE_H
  27. #define MICROPY_INCLUDED_PY_EMITGLUE_H
  28. #include "py/obj.h"
  29. #include "py/bc.h"
  30. // These variables and functions glue the code emitters to the runtime.
  31. // Used with mp_raw_code_t::proto_fun_indicator to detect if a mp_proto_fun_t is a
  32. // mp_raw_code_t struct or a direct pointer to bytecode.
  33. #define MP_PROTO_FUN_INDICATOR_RAW_CODE_0 (0)
  34. #define MP_PROTO_FUN_INDICATOR_RAW_CODE_1 (0)
  35. // These must fit in 8 bits; see scope.h
  36. enum {
  37. MP_EMIT_OPT_NONE,
  38. MP_EMIT_OPT_BYTECODE,
  39. MP_EMIT_OPT_NATIVE_PYTHON,
  40. MP_EMIT_OPT_VIPER,
  41. MP_EMIT_OPT_ASM,
  42. };
  43. typedef enum {
  44. MP_CODE_UNUSED,
  45. MP_CODE_RESERVED,
  46. MP_CODE_BYTECODE,
  47. MP_CODE_NATIVE_PY,
  48. MP_CODE_NATIVE_VIPER,
  49. MP_CODE_NATIVE_ASM,
  50. } mp_raw_code_kind_t;
  51. // An mp_proto_fun_t points to static information about a non-instantiated function.
  52. // A function object is created from this information, and that object can then be executed.
  53. // It points either to bytecode, or an mp_raw_code_t struct.
  54. typedef const void *mp_proto_fun_t;
  55. // Bytecode is distinguished from an mp_raw_code_t struct by the first two bytes: bytecode
  56. // is guaranteed to have either its first or second byte non-zero. So if both bytes are
  57. // zero then the mp_proto_fun_t pointer must be an mp_raw_code_t.
  58. static inline bool mp_proto_fun_is_bytecode(mp_proto_fun_t proto_fun) {
  59. const uint8_t *header = (const uint8_t *)proto_fun;
  60. return (header[0] | (header[1] << 8)) != (MP_PROTO_FUN_INDICATOR_RAW_CODE_0 | (MP_PROTO_FUN_INDICATOR_RAW_CODE_1 << 8));
  61. }
  62. // The mp_raw_code_t struct appears in the following places:
  63. // compiled bytecode: instance in RAM, referenced by outer scope, usually freed after first (and only) use
  64. // mpy file: instance in RAM, created when .mpy file is loaded (same comments as above)
  65. // frozen: instance in ROM
  66. typedef struct _mp_raw_code_t {
  67. uint8_t proto_fun_indicator[2];
  68. uint8_t kind; // of type mp_raw_code_kind_t; only 3 bits used
  69. bool is_generator;
  70. const void *fun_data;
  71. struct _mp_raw_code_t **children;
  72. #if MICROPY_PERSISTENT_CODE_SAVE
  73. uint32_t fun_data_len; // for mp_raw_code_save
  74. uint16_t n_children;
  75. #if MICROPY_EMIT_MACHINE_CODE
  76. uint16_t prelude_offset;
  77. #endif
  78. #if MICROPY_PY_SYS_SETTRACE
  79. // line_of_definition is a Python source line where the raw_code was
  80. // created e.g. MP_BC_MAKE_FUNCTION. This is different from lineno info
  81. // stored in prelude, which provides line number for first statement of
  82. // a function. Required to properly implement "call" trace event.
  83. uint32_t line_of_definition;
  84. mp_bytecode_prelude_t prelude;
  85. #endif
  86. #endif
  87. #if MICROPY_EMIT_INLINE_ASM
  88. uint32_t asm_n_pos_args : 8;
  89. uint32_t asm_type_sig : 24; // compressed as 2-bit types; ret is MSB, then arg0, arg1, etc
  90. #endif
  91. } mp_raw_code_t;
  92. // Version of mp_raw_code_t but without the asm_n_pos_args/asm_type_sig entries, which are
  93. // only needed when the kind is MP_CODE_NATIVE_ASM. So this struct can be used when the
  94. // kind is MP_CODE_BYTECODE, MP_CODE_NATIVE_PY or MP_CODE_NATIVE_VIPER, to reduce its size.
  95. typedef struct _mp_raw_code_truncated_t {
  96. uint8_t proto_fun_indicator[2];
  97. uint8_t kind;
  98. bool is_generator;
  99. const void *fun_data;
  100. struct _mp_raw_code_t **children;
  101. #if MICROPY_PERSISTENT_CODE_SAVE
  102. uint32_t fun_data_len;
  103. uint16_t n_children;
  104. #if MICROPY_EMIT_MACHINE_CODE
  105. uint16_t prelude_offset;
  106. #endif
  107. #if MICROPY_PY_SYS_SETTRACE
  108. uint32_t line_of_definition;
  109. mp_bytecode_prelude_t prelude;
  110. #endif
  111. #endif
  112. } mp_raw_code_truncated_t;
  113. mp_raw_code_t *mp_emit_glue_new_raw_code(void);
  114. void mp_emit_glue_assign_bytecode(mp_raw_code_t *rc, const byte *code,
  115. mp_raw_code_t **children,
  116. #if MICROPY_PERSISTENT_CODE_SAVE
  117. size_t len,
  118. uint16_t n_children,
  119. #endif
  120. uint16_t scope_flags);
  121. 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,
  122. mp_raw_code_t **children,
  123. #if MICROPY_PERSISTENT_CODE_SAVE
  124. uint16_t n_children,
  125. uint16_t prelude_offset,
  126. #endif
  127. uint16_t scope_flags, uint32_t asm_n_pos_args, uint32_t asm_type_sig);
  128. 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);
  129. 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);
  130. #endif // MICROPY_INCLUDED_PY_EMITGLUE_H