persistentcode.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * This file is part of the MicroPython project, http://micropython.org/
  3. *
  4. * The MIT License (MIT)
  5. *
  6. * Copyright (c) 2013-2016 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_PERSISTENTCODE_H
  27. #define MICROPY_INCLUDED_PY_PERSISTENTCODE_H
  28. #include "py/mpprint.h"
  29. #include "py/reader.h"
  30. #include "py/emitglue.h"
  31. // The current version of .mpy files. A bytecode-only .mpy file can be loaded
  32. // as long as MPY_VERSION matches, but a native .mpy (i.e. one with an arch
  33. // set) must also match MPY_SUB_VERSION. This allows 3 additional updates to
  34. // the native ABI per bytecode revision.
  35. #define MPY_VERSION 6
  36. #define MPY_SUB_VERSION 3
  37. // Macros to encode/decode sub-version to/from the feature byte. This replaces
  38. // the bits previously used to encode the flags (map caching and unicode)
  39. // which are no longer used starting at .mpy version 6.
  40. #define MPY_FEATURE_ENCODE_SUB_VERSION(version) (version)
  41. #define MPY_FEATURE_DECODE_SUB_VERSION(feat) ((feat) & 3)
  42. // Macros to encode/decode native architecture to/from the feature byte
  43. #define MPY_FEATURE_ENCODE_ARCH(arch) ((arch) << 2)
  44. #define MPY_FEATURE_DECODE_ARCH(feat) ((feat) >> 2)
  45. // Define the host architecture
  46. #if MICROPY_EMIT_X86
  47. #define MPY_FEATURE_ARCH (MP_NATIVE_ARCH_X86)
  48. #elif MICROPY_EMIT_X64
  49. #define MPY_FEATURE_ARCH (MP_NATIVE_ARCH_X64)
  50. #elif MICROPY_EMIT_THUMB
  51. #if defined(__thumb2__)
  52. #if defined(__ARM_FP) && (__ARM_FP & 8) == 8
  53. #define MPY_FEATURE_ARCH (MP_NATIVE_ARCH_ARMV7EMDP)
  54. #elif defined(__ARM_FP) && (__ARM_FP & 4) == 4
  55. #define MPY_FEATURE_ARCH (MP_NATIVE_ARCH_ARMV7EMSP)
  56. #else
  57. #define MPY_FEATURE_ARCH (MP_NATIVE_ARCH_ARMV7EM)
  58. #endif
  59. #else
  60. #define MPY_FEATURE_ARCH (MP_NATIVE_ARCH_ARMV6M)
  61. #endif
  62. #define MPY_FEATURE_ARCH_TEST(x) (MP_NATIVE_ARCH_ARMV6M <= (x) && (x) <= MPY_FEATURE_ARCH)
  63. #elif MICROPY_EMIT_ARM
  64. #define MPY_FEATURE_ARCH (MP_NATIVE_ARCH_ARMV6)
  65. #elif MICROPY_EMIT_XTENSA
  66. #define MPY_FEATURE_ARCH (MP_NATIVE_ARCH_XTENSA)
  67. #elif MICROPY_EMIT_XTENSAWIN
  68. #define MPY_FEATURE_ARCH (MP_NATIVE_ARCH_XTENSAWIN)
  69. #else
  70. #define MPY_FEATURE_ARCH (MP_NATIVE_ARCH_NONE)
  71. #endif
  72. #ifndef MPY_FEATURE_ARCH_TEST
  73. #define MPY_FEATURE_ARCH_TEST(x) ((x) == MPY_FEATURE_ARCH)
  74. #endif
  75. // 16-bit little-endian integer with the second and third bytes of supported .mpy files
  76. #define MPY_FILE_HEADER_INT (MPY_VERSION \
  77. | (MPY_FEATURE_ENCODE_SUB_VERSION(MPY_SUB_VERSION) | MPY_FEATURE_ENCODE_ARCH(MPY_FEATURE_ARCH)) << 8)
  78. enum {
  79. MP_NATIVE_ARCH_NONE = 0,
  80. MP_NATIVE_ARCH_X86,
  81. MP_NATIVE_ARCH_X64,
  82. MP_NATIVE_ARCH_ARMV6,
  83. MP_NATIVE_ARCH_ARMV6M,
  84. MP_NATIVE_ARCH_ARMV7M,
  85. MP_NATIVE_ARCH_ARMV7EM,
  86. MP_NATIVE_ARCH_ARMV7EMSP,
  87. MP_NATIVE_ARCH_ARMV7EMDP,
  88. MP_NATIVE_ARCH_XTENSA,
  89. MP_NATIVE_ARCH_XTENSAWIN,
  90. };
  91. enum {
  92. MP_PERSISTENT_OBJ_FUN_TABLE = 0,
  93. MP_PERSISTENT_OBJ_NONE,
  94. MP_PERSISTENT_OBJ_FALSE,
  95. MP_PERSISTENT_OBJ_TRUE,
  96. MP_PERSISTENT_OBJ_ELLIPSIS,
  97. MP_PERSISTENT_OBJ_STR,
  98. MP_PERSISTENT_OBJ_BYTES,
  99. MP_PERSISTENT_OBJ_INT,
  100. MP_PERSISTENT_OBJ_FLOAT,
  101. MP_PERSISTENT_OBJ_COMPLEX,
  102. MP_PERSISTENT_OBJ_TUPLE,
  103. };
  104. void mp_raw_code_load(mp_reader_t *reader, mp_compiled_module_t *ctx);
  105. void mp_raw_code_load_mem(const byte *buf, size_t len, mp_compiled_module_t *ctx);
  106. void mp_raw_code_load_file(qstr filename, mp_compiled_module_t *ctx);
  107. void mp_raw_code_save(mp_compiled_module_t *cm, mp_print_t *print);
  108. void mp_raw_code_save_file(mp_compiled_module_t *cm, qstr filename);
  109. void mp_native_relocate(void *reloc, uint8_t *text, uintptr_t reloc_text);
  110. #endif // MICROPY_INCLUDED_PY_PERSISTENTCODE_H