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