persistentcode.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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
  32. #define MPY_VERSION 5
  33. // Macros to encode/decode flags to/from the feature byte
  34. #define MPY_FEATURE_ENCODE_FLAGS(flags) (flags)
  35. #define MPY_FEATURE_DECODE_FLAGS(feat) ((feat) & 3)
  36. // Macros to encode/decode native architecture to/from the feature byte
  37. #define MPY_FEATURE_ENCODE_ARCH(arch) ((arch) << 2)
  38. #define MPY_FEATURE_DECODE_ARCH(feat) ((feat) >> 2)
  39. // The feature flag bits encode the compile-time config options that
  40. // affect the generate bytecode.
  41. #define MPY_FEATURE_FLAGS ( \
  42. ((MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE) << 0) \
  43. | ((MICROPY_PY_BUILTINS_STR_UNICODE) << 1) \
  44. )
  45. // This is a version of the flags that can be configured at runtime.
  46. #define MPY_FEATURE_FLAGS_DYNAMIC ( \
  47. ((MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE_DYNAMIC) << 0) \
  48. | ((MICROPY_PY_BUILTINS_STR_UNICODE_DYNAMIC) << 1) \
  49. )
  50. // Define the host architecture
  51. #if MICROPY_EMIT_X86
  52. #define MPY_FEATURE_ARCH (MP_NATIVE_ARCH_X86)
  53. #elif MICROPY_EMIT_X64
  54. #define MPY_FEATURE_ARCH (MP_NATIVE_ARCH_X64)
  55. #elif MICROPY_EMIT_THUMB
  56. #if defined(__thumb2__)
  57. #if defined(__ARM_FP) && (__ARM_FP & 8) == 8
  58. #define MPY_FEATURE_ARCH (MP_NATIVE_ARCH_ARMV7EMDP)
  59. #elif defined(__ARM_FP) && (__ARM_FP & 4) == 4
  60. #define MPY_FEATURE_ARCH (MP_NATIVE_ARCH_ARMV7EMSP)
  61. #else
  62. #define MPY_FEATURE_ARCH (MP_NATIVE_ARCH_ARMV7EM)
  63. #endif
  64. #else
  65. #define MPY_FEATURE_ARCH (MP_NATIVE_ARCH_ARMV7M)
  66. #endif
  67. #define MPY_FEATURE_ARCH_TEST(x) (MP_NATIVE_ARCH_ARMV6M <= (x) && (x) <= MPY_FEATURE_ARCH)
  68. #elif MICROPY_EMIT_ARM
  69. #define MPY_FEATURE_ARCH (MP_NATIVE_ARCH_ARMV6)
  70. #elif MICROPY_EMIT_XTENSA
  71. #define MPY_FEATURE_ARCH (MP_NATIVE_ARCH_XTENSA)
  72. #elif MICROPY_EMIT_XTENSAWIN
  73. #define MPY_FEATURE_ARCH (MP_NATIVE_ARCH_XTENSAWIN)
  74. #else
  75. #define MPY_FEATURE_ARCH (MP_NATIVE_ARCH_NONE)
  76. #endif
  77. #ifndef MPY_FEATURE_ARCH_TEST
  78. #define MPY_FEATURE_ARCH_TEST(x) ((x) == MPY_FEATURE_ARCH)
  79. #endif
  80. // 16-bit little-endian integer with the second and third bytes of supported .mpy files
  81. #define MPY_FILE_HEADER_INT (MPY_VERSION \
  82. | (MPY_FEATURE_ENCODE_FLAGS(MPY_FEATURE_FLAGS) | MPY_FEATURE_ENCODE_ARCH(MPY_FEATURE_ARCH)) << 8)
  83. enum {
  84. MP_NATIVE_ARCH_NONE = 0,
  85. MP_NATIVE_ARCH_X86,
  86. MP_NATIVE_ARCH_X64,
  87. MP_NATIVE_ARCH_ARMV6,
  88. MP_NATIVE_ARCH_ARMV6M,
  89. MP_NATIVE_ARCH_ARMV7M,
  90. MP_NATIVE_ARCH_ARMV7EM,
  91. MP_NATIVE_ARCH_ARMV7EMSP,
  92. MP_NATIVE_ARCH_ARMV7EMDP,
  93. MP_NATIVE_ARCH_XTENSA,
  94. MP_NATIVE_ARCH_XTENSAWIN,
  95. };
  96. mp_raw_code_t *mp_raw_code_load(mp_reader_t *reader);
  97. mp_raw_code_t *mp_raw_code_load_mem(const byte *buf, size_t len);
  98. mp_raw_code_t *mp_raw_code_load_file(const char *filename);
  99. void mp_raw_code_save(mp_raw_code_t *rc, mp_print_t *print);
  100. void mp_raw_code_save_file(mp_raw_code_t *rc, const char *filename);
  101. void mp_native_relocate(void *reloc, uint8_t *text, uintptr_t reloc_text);
  102. #endif // MICROPY_INCLUDED_PY_PERSISTENTCODE_H