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 | |
Jim Mussared | d94141e | 2022-09-17 23:57:12 +1000 | [diff] [blame] | 33 | // The current version of .mpy files. A bytecode-only .mpy file can be loaded |
| 34 | // as long as MPY_VERSION matches, but a native .mpy (i.e. one with an arch |
| 35 | // set) must also match MPY_SUB_VERSION. This allows 3 additional updates to |
| 36 | // the native ABI per bytecode revision. |
Damien George | f2040bf | 2021-10-22 22:22:47 +1100 | [diff] [blame] | 37 | #define MPY_VERSION 6 |
Damien George | bdbc869 | 2024-03-25 12:25:01 +1100 | [diff] [blame] | 38 | #define MPY_SUB_VERSION 3 |
Damien George | 7e90e22 | 2019-05-02 09:59:21 +1000 | [diff] [blame] | 39 | |
Jim Mussared | d94141e | 2022-09-17 23:57:12 +1000 | [diff] [blame] | 40 | // Macros to encode/decode sub-version to/from the feature byte. This replaces |
| 41 | // the bits previously used to encode the flags (map caching and unicode) |
| 42 | // which are no longer used starting at .mpy version 6. |
| 43 | #define MPY_FEATURE_ENCODE_SUB_VERSION(version) (version) |
| 44 | #define MPY_FEATURE_DECODE_SUB_VERSION(feat) ((feat) & 3) |
Damien George | f4601af | 2019-11-01 21:04:07 +1100 | [diff] [blame] | 45 | |
| 46 | // Macros to encode/decode native architecture to/from the feature byte |
| 47 | #define MPY_FEATURE_ENCODE_ARCH(arch) ((arch) << 2) |
| 48 | #define MPY_FEATURE_DECODE_ARCH(feat) ((feat) >> 2) |
| 49 | |
Damien George | f4601af | 2019-11-01 21:04:07 +1100 | [diff] [blame] | 50 | // Define the host architecture |
| 51 | #if MICROPY_EMIT_X86 |
Damien George | 9ac949c | 2019-11-30 23:00:56 +1100 | [diff] [blame] | 52 | #define MPY_FEATURE_ARCH (MP_NATIVE_ARCH_X86) |
Damien George | f4601af | 2019-11-01 21:04:07 +1100 | [diff] [blame] | 53 | #elif MICROPY_EMIT_X64 |
Damien George | 9ac949c | 2019-11-30 23:00:56 +1100 | [diff] [blame] | 54 | #define MPY_FEATURE_ARCH (MP_NATIVE_ARCH_X64) |
Damien George | f4601af | 2019-11-01 21:04:07 +1100 | [diff] [blame] | 55 | #elif MICROPY_EMIT_THUMB |
Damien George | 9ac949c | 2019-11-30 23:00:56 +1100 | [diff] [blame] | 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 |
Damien George | 17ac687 | 2022-05-23 17:58:30 +1000 | [diff] [blame] | 65 | #define MPY_FEATURE_ARCH (MP_NATIVE_ARCH_ARMV6M) |
Damien George | 9ac949c | 2019-11-30 23:00:56 +1100 | [diff] [blame] | 66 | #endif |
| 67 | #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] | 68 | #elif MICROPY_EMIT_ARM |
Damien George | 9ac949c | 2019-11-30 23:00:56 +1100 | [diff] [blame] | 69 | #define MPY_FEATURE_ARCH (MP_NATIVE_ARCH_ARMV6) |
Damien George | f4601af | 2019-11-01 21:04:07 +1100 | [diff] [blame] | 70 | #elif MICROPY_EMIT_XTENSA |
Damien George | 9ac949c | 2019-11-30 23:00:56 +1100 | [diff] [blame] | 71 | #define MPY_FEATURE_ARCH (MP_NATIVE_ARCH_XTENSA) |
Damien George | f4601af | 2019-11-01 21:04:07 +1100 | [diff] [blame] | 72 | #elif MICROPY_EMIT_XTENSAWIN |
Damien George | 9ac949c | 2019-11-30 23:00:56 +1100 | [diff] [blame] | 73 | #define MPY_FEATURE_ARCH (MP_NATIVE_ARCH_XTENSAWIN) |
Alessandro Gatti | 99f5659 | 2024-06-16 20:40:28 +0200 | [diff] [blame] | 74 | #elif MICROPY_EMIT_RV32 |
| 75 | #define MPY_FEATURE_ARCH (MP_NATIVE_ARCH_RV32IMC) |
Damien George | f4601af | 2019-11-01 21:04:07 +1100 | [diff] [blame] | 76 | #else |
Damien George | 9ac949c | 2019-11-30 23:00:56 +1100 | [diff] [blame] | 77 | #define MPY_FEATURE_ARCH (MP_NATIVE_ARCH_NONE) |
| 78 | #endif |
| 79 | |
| 80 | #ifndef MPY_FEATURE_ARCH_TEST |
| 81 | #define MPY_FEATURE_ARCH_TEST(x) ((x) == MPY_FEATURE_ARCH) |
Damien George | f4601af | 2019-11-01 21:04:07 +1100 | [diff] [blame] | 82 | #endif |
| 83 | |
Damien George | 80df377 | 2019-10-30 16:26:11 +1100 | [diff] [blame] | 84 | // 16-bit little-endian integer with the second and third bytes of supported .mpy files |
| 85 | #define MPY_FILE_HEADER_INT (MPY_VERSION \ |
Jim Mussared | d94141e | 2022-09-17 23:57:12 +1000 | [diff] [blame] | 86 | | (MPY_FEATURE_ENCODE_SUB_VERSION(MPY_SUB_VERSION) | MPY_FEATURE_ENCODE_ARCH(MPY_FEATURE_ARCH)) << 8) |
Damien George | 80df377 | 2019-10-30 16:26:11 +1100 | [diff] [blame] | 87 | |
Damien George | 1396a02 | 2019-02-21 15:18:33 +1100 | [diff] [blame] | 88 | enum { |
| 89 | MP_NATIVE_ARCH_NONE = 0, |
| 90 | MP_NATIVE_ARCH_X86, |
| 91 | MP_NATIVE_ARCH_X64, |
| 92 | MP_NATIVE_ARCH_ARMV6, |
| 93 | MP_NATIVE_ARCH_ARMV6M, |
| 94 | MP_NATIVE_ARCH_ARMV7M, |
| 95 | MP_NATIVE_ARCH_ARMV7EM, |
| 96 | MP_NATIVE_ARCH_ARMV7EMSP, |
| 97 | MP_NATIVE_ARCH_ARMV7EMDP, |
| 98 | MP_NATIVE_ARCH_XTENSA, |
Damien George | 9adedce | 2019-09-13 13:15:12 +1000 | [diff] [blame] | 99 | MP_NATIVE_ARCH_XTENSAWIN, |
Alessandro Gatti | 99f5659 | 2024-06-16 20:40:28 +0200 | [diff] [blame] | 100 | MP_NATIVE_ARCH_RV32IMC, |
Damien George | 9dbc787 | 2024-03-07 11:38:27 +1100 | [diff] [blame] | 101 | MP_NATIVE_ARCH_DEBUG, // this entry should always be last |
Damien George | 1396a02 | 2019-02-21 15:18:33 +1100 | [diff] [blame] | 102 | }; |
| 103 | |
Damien George | 42d0bd2 | 2022-04-07 22:18:37 +1000 | [diff] [blame] | 104 | enum { |
| 105 | MP_PERSISTENT_OBJ_FUN_TABLE = 0, |
Damien George | 4ca9698 | 2022-03-31 14:28:19 +1100 | [diff] [blame] | 106 | MP_PERSISTENT_OBJ_NONE, |
| 107 | MP_PERSISTENT_OBJ_FALSE, |
| 108 | MP_PERSISTENT_OBJ_TRUE, |
Damien George | 42d0bd2 | 2022-04-07 22:18:37 +1000 | [diff] [blame] | 109 | MP_PERSISTENT_OBJ_ELLIPSIS, |
| 110 | MP_PERSISTENT_OBJ_STR, |
| 111 | MP_PERSISTENT_OBJ_BYTES, |
| 112 | MP_PERSISTENT_OBJ_INT, |
| 113 | MP_PERSISTENT_OBJ_FLOAT, |
| 114 | MP_PERSISTENT_OBJ_COMPLEX, |
Damien George | 4ca9698 | 2022-03-31 14:28:19 +1100 | [diff] [blame] | 115 | MP_PERSISTENT_OBJ_TUPLE, |
Damien George | 42d0bd2 | 2022-04-07 22:18:37 +1000 | [diff] [blame] | 116 | }; |
| 117 | |
Damien George | 2283b6d | 2022-12-07 14:42:35 +1100 | [diff] [blame] | 118 | void mp_raw_code_load(mp_reader_t *reader, mp_compiled_module_t *ctx); |
| 119 | void mp_raw_code_load_mem(const byte *buf, size_t len, mp_compiled_module_t *ctx); |
Jim Mussared | 5015779 | 2023-09-27 13:43:50 +1000 | [diff] [blame] | 120 | void mp_raw_code_load_file(qstr filename, mp_compiled_module_t *ctx); |
Damien George | 6810f2c | 2016-11-16 11:55:41 +1100 | [diff] [blame] | 121 | |
Damien George | f2040bf | 2021-10-22 22:22:47 +1100 | [diff] [blame] | 122 | void mp_raw_code_save(mp_compiled_module_t *cm, mp_print_t *print); |
Jim Mussared | 5015779 | 2023-09-27 13:43:50 +1000 | [diff] [blame] | 123 | void mp_raw_code_save_file(mp_compiled_module_t *cm, qstr filename); |
Damien George | 6810f2c | 2016-11-16 11:55:41 +1100 | [diff] [blame] | 124 | |
Damien George | b47e155 | 2019-10-06 23:29:40 +1100 | [diff] [blame] | 125 | void mp_native_relocate(void *reloc, uint8_t *text, uintptr_t reloc_text); |
| 126 | |
Damien George | 6810f2c | 2016-11-16 11:55:41 +1100 | [diff] [blame] | 127 | #endif // MICROPY_INCLUDED_PY_PERSISTENTCODE_H |