Damien George | 6810f2c | 2016-11-16 11:55:41 +1100 | [diff] [blame] | 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 | |
Damien George | 6b239c2 | 2016-11-16 16:04:57 +1100 | [diff] [blame] | 29 | #include "py/mpprint.h" |
| 30 | #include "py/reader.h" |
| 31 | #include "py/emitglue.h" |
Damien George | 6810f2c | 2016-11-16 11:55:41 +1100 | [diff] [blame] | 32 | |
Damien George | 7e90e22 | 2019-05-02 09:59:21 +1000 | [diff] [blame] | 33 | // The current version of .mpy files |
Damien George | 5716c5c | 2019-09-26 16:39:37 +1000 | [diff] [blame] | 34 | #define MPY_VERSION 5 |
Damien George | 7e90e22 | 2019-05-02 09:59:21 +1000 | [diff] [blame] | 35 | |
Damien George | f4601af | 2019-11-01 21:04:07 +1100 | [diff] [blame] | 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 |
Damien George | 9ac949c | 2019-11-30 23:00:56 +1100 | [diff] [blame] | 58 | #define MPY_FEATURE_ARCH (MP_NATIVE_ARCH_X86) |
Damien George | f4601af | 2019-11-01 21:04:07 +1100 | [diff] [blame] | 59 | #elif MICROPY_EMIT_X64 |
Damien George | 9ac949c | 2019-11-30 23:00:56 +1100 | [diff] [blame] | 60 | #define MPY_FEATURE_ARCH (MP_NATIVE_ARCH_X64) |
Damien George | f4601af | 2019-11-01 21:04:07 +1100 | [diff] [blame] | 61 | #elif MICROPY_EMIT_THUMB |
Damien George | 9ac949c | 2019-11-30 23:00:56 +1100 | [diff] [blame] | 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) |
Damien George | f4601af | 2019-11-01 21:04:07 +1100 | [diff] [blame] | 74 | #elif MICROPY_EMIT_ARM |
Damien George | 9ac949c | 2019-11-30 23:00:56 +1100 | [diff] [blame] | 75 | #define MPY_FEATURE_ARCH (MP_NATIVE_ARCH_ARMV6) |
Damien George | f4601af | 2019-11-01 21:04:07 +1100 | [diff] [blame] | 76 | #elif MICROPY_EMIT_XTENSA |
Damien George | 9ac949c | 2019-11-30 23:00:56 +1100 | [diff] [blame] | 77 | #define MPY_FEATURE_ARCH (MP_NATIVE_ARCH_XTENSA) |
Damien George | f4601af | 2019-11-01 21:04:07 +1100 | [diff] [blame] | 78 | #elif MICROPY_EMIT_XTENSAWIN |
Damien George | 9ac949c | 2019-11-30 23:00:56 +1100 | [diff] [blame] | 79 | #define MPY_FEATURE_ARCH (MP_NATIVE_ARCH_XTENSAWIN) |
Damien George | f4601af | 2019-11-01 21:04:07 +1100 | [diff] [blame] | 80 | #else |
Damien George | 9ac949c | 2019-11-30 23:00:56 +1100 | [diff] [blame] | 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) |
Damien George | f4601af | 2019-11-01 21:04:07 +1100 | [diff] [blame] | 86 | #endif |
| 87 | |
Damien George | 80df377 | 2019-10-30 16:26:11 +1100 | [diff] [blame] | 88 | // 16-bit little-endian integer with the second and third bytes of supported .mpy files |
| 89 | #define MPY_FILE_HEADER_INT (MPY_VERSION \ |
| 90 | | (MPY_FEATURE_ENCODE_FLAGS(MPY_FEATURE_FLAGS) | MPY_FEATURE_ENCODE_ARCH(MPY_FEATURE_ARCH)) << 8) |
| 91 | |
Damien George | 1396a02 | 2019-02-21 15:18:33 +1100 | [diff] [blame] | 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, |
Damien George | 9adedce | 2019-09-13 13:15:12 +1000 | [diff] [blame] | 103 | MP_NATIVE_ARCH_XTENSAWIN, |
Damien George | 1396a02 | 2019-02-21 15:18:33 +1100 | [diff] [blame] | 104 | }; |
| 105 | |
Damien George | 6810f2c | 2016-11-16 11:55:41 +1100 | [diff] [blame] | 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 | |
Damien George | b47e155 | 2019-10-06 23:29:40 +1100 | [diff] [blame] | 113 | void mp_native_relocate(void *reloc, uint8_t *text, uintptr_t reloc_text); |
| 114 | |
Damien George | 6810f2c | 2016-11-16 11:55:41 +1100 | [diff] [blame] | 115 | #endif // MICROPY_INCLUDED_PY_PERSISTENTCODE_H |