emitglue.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. // These must fit in 8 bits; see scope.h
  32. enum {
  33. MP_EMIT_OPT_NONE,
  34. MP_EMIT_OPT_BYTECODE,
  35. MP_EMIT_OPT_NATIVE_PYTHON,
  36. MP_EMIT_OPT_VIPER,
  37. MP_EMIT_OPT_ASM,
  38. };
  39. typedef enum {
  40. MP_CODE_UNUSED,
  41. MP_CODE_RESERVED,
  42. MP_CODE_BYTECODE,
  43. MP_CODE_NATIVE_PY,
  44. MP_CODE_NATIVE_VIPER,
  45. MP_CODE_NATIVE_ASM,
  46. } mp_raw_code_kind_t;
  47. // compiled bytecode: instance in RAM, referenced by outer scope, usually freed after first (and only) use
  48. // mpy file: instance in RAM, created when .mpy file is loaded (same comments as above)
  49. // frozen: instance in ROM
  50. typedef struct _mp_raw_code_t {
  51. mp_uint_t kind : 3; // of type mp_raw_code_kind_t
  52. mp_uint_t scope_flags : 7;
  53. mp_uint_t n_pos_args : 11;
  54. const void *fun_data;
  55. #if MICROPY_PERSISTENT_CODE_SAVE || MICROPY_DEBUG_PRINTERS
  56. size_t fun_data_len; // so mp_raw_code_save and mp_bytecode_print work
  57. #endif
  58. struct _mp_raw_code_t **children;
  59. #if MICROPY_PERSISTENT_CODE_SAVE
  60. size_t n_children;
  61. #if MICROPY_PY_SYS_SETTRACE
  62. mp_bytecode_prelude_t prelude;
  63. // line_of_definition is a Python source line where the raw_code was
  64. // created e.g. MP_BC_MAKE_FUNCTION. This is different from lineno info
  65. // stored in prelude, which provides line number for first statement of
  66. // a function. Required to properly implement "call" trace event.
  67. mp_uint_t line_of_definition;
  68. #endif
  69. #if MICROPY_EMIT_MACHINE_CODE
  70. uint16_t prelude_offset;
  71. #endif
  72. #endif
  73. #if MICROPY_EMIT_MACHINE_CODE
  74. mp_uint_t type_sig; // for viper, compressed as 2-bit types; ret is MSB, then arg0, arg1, etc
  75. #endif
  76. } mp_raw_code_t;
  77. mp_raw_code_t *mp_emit_glue_new_raw_code(void);
  78. void mp_emit_glue_assign_bytecode(mp_raw_code_t *rc, const byte *code,
  79. #if MICROPY_PERSISTENT_CODE_SAVE || MICROPY_DEBUG_PRINTERS
  80. size_t len,
  81. #endif
  82. mp_raw_code_t **children,
  83. #if MICROPY_PERSISTENT_CODE_SAVE
  84. size_t n_children,
  85. #endif
  86. mp_uint_t scope_flags);
  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. 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);
  95. 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);
  96. #endif // MICROPY_INCLUDED_PY_EMITGLUE_H