blob: f0b7f70f7d7fa434995b3e06489e248e56f2c941 [file] [log] [blame]
Damien George6810f2c2016-11-16 11:55:41 +11001/*
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 George6b239c22016-11-16 16:04:57 +110029#include "py/mpprint.h"
30#include "py/reader.h"
31#include "py/emitglue.h"
Damien George6810f2c2016-11-16 11:55:41 +110032
Jim Mussaredd94141e2022-09-17 23:57:12 +100033// 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 Georgef2040bf2021-10-22 22:22:47 +110037#define MPY_VERSION 6
Damien Georgebdbc8692024-03-25 12:25:01 +110038#define MPY_SUB_VERSION 3
Damien George7e90e222019-05-02 09:59:21 +100039
Jim Mussaredd94141e2022-09-17 23:57:12 +100040// 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 Georgef4601af2019-11-01 21:04:07 +110045
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 Georgef4601af2019-11-01 21:04:07 +110050// Define the host architecture
51#if MICROPY_EMIT_X86
Damien George9ac949c2019-11-30 23:00:56 +110052 #define MPY_FEATURE_ARCH (MP_NATIVE_ARCH_X86)
Damien Georgef4601af2019-11-01 21:04:07 +110053#elif MICROPY_EMIT_X64
Damien George9ac949c2019-11-30 23:00:56 +110054 #define MPY_FEATURE_ARCH (MP_NATIVE_ARCH_X64)
Damien Georgef4601af2019-11-01 21:04:07 +110055#elif MICROPY_EMIT_THUMB
Damien George9ac949c2019-11-30 23:00:56 +110056 #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 George17ac6872022-05-23 17:58:30 +100065 #define MPY_FEATURE_ARCH (MP_NATIVE_ARCH_ARMV6M)
Damien George9ac949c2019-11-30 23:00:56 +110066 #endif
67 #define MPY_FEATURE_ARCH_TEST(x) (MP_NATIVE_ARCH_ARMV6M <= (x) && (x) <= MPY_FEATURE_ARCH)
Damien Georgef4601af2019-11-01 21:04:07 +110068#elif MICROPY_EMIT_ARM
Damien George9ac949c2019-11-30 23:00:56 +110069 #define MPY_FEATURE_ARCH (MP_NATIVE_ARCH_ARMV6)
Damien Georgef4601af2019-11-01 21:04:07 +110070#elif MICROPY_EMIT_XTENSA
Damien George9ac949c2019-11-30 23:00:56 +110071 #define MPY_FEATURE_ARCH (MP_NATIVE_ARCH_XTENSA)
Damien Georgef4601af2019-11-01 21:04:07 +110072#elif MICROPY_EMIT_XTENSAWIN
Damien George9ac949c2019-11-30 23:00:56 +110073 #define MPY_FEATURE_ARCH (MP_NATIVE_ARCH_XTENSAWIN)
Alessandro Gatti99f56592024-06-16 20:40:28 +020074#elif MICROPY_EMIT_RV32
75 #define MPY_FEATURE_ARCH (MP_NATIVE_ARCH_RV32IMC)
Damien Georgef4601af2019-11-01 21:04:07 +110076#else
Damien George9ac949c2019-11-30 23:00:56 +110077 #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 Georgef4601af2019-11-01 21:04:07 +110082#endif
83
Damien George80df3772019-10-30 16:26:11 +110084// 16-bit little-endian integer with the second and third bytes of supported .mpy files
85#define MPY_FILE_HEADER_INT (MPY_VERSION \
Jim Mussaredd94141e2022-09-17 23:57:12 +100086 | (MPY_FEATURE_ENCODE_SUB_VERSION(MPY_SUB_VERSION) | MPY_FEATURE_ENCODE_ARCH(MPY_FEATURE_ARCH)) << 8)
Damien George80df3772019-10-30 16:26:11 +110087
Damien George1396a022019-02-21 15:18:33 +110088enum {
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 George9adedce2019-09-13 13:15:12 +100099 MP_NATIVE_ARCH_XTENSAWIN,
Alessandro Gatti99f56592024-06-16 20:40:28 +0200100 MP_NATIVE_ARCH_RV32IMC,
Damien George9dbc7872024-03-07 11:38:27 +1100101 MP_NATIVE_ARCH_DEBUG, // this entry should always be last
Damien George1396a022019-02-21 15:18:33 +1100102};
103
Damien George42d0bd22022-04-07 22:18:37 +1000104enum {
105 MP_PERSISTENT_OBJ_FUN_TABLE = 0,
Damien George4ca96982022-03-31 14:28:19 +1100106 MP_PERSISTENT_OBJ_NONE,
107 MP_PERSISTENT_OBJ_FALSE,
108 MP_PERSISTENT_OBJ_TRUE,
Damien George42d0bd22022-04-07 22:18:37 +1000109 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 George4ca96982022-03-31 14:28:19 +1100115 MP_PERSISTENT_OBJ_TUPLE,
Damien George42d0bd22022-04-07 22:18:37 +1000116};
117
Damien George2283b6d2022-12-07 14:42:35 +1100118void mp_raw_code_load(mp_reader_t *reader, mp_compiled_module_t *ctx);
119void mp_raw_code_load_mem(const byte *buf, size_t len, mp_compiled_module_t *ctx);
Jim Mussared50157792023-09-27 13:43:50 +1000120void mp_raw_code_load_file(qstr filename, mp_compiled_module_t *ctx);
Damien George6810f2c2016-11-16 11:55:41 +1100121
Damien Georgef2040bf2021-10-22 22:22:47 +1100122void mp_raw_code_save(mp_compiled_module_t *cm, mp_print_t *print);
Jim Mussared50157792023-09-27 13:43:50 +1000123void mp_raw_code_save_file(mp_compiled_module_t *cm, qstr filename);
Damien George6810f2c2016-11-16 11:55:41 +1100124
Damien Georgeb47e1552019-10-06 23:29:40 +1100125void mp_native_relocate(void *reloc, uint8_t *text, uintptr_t reloc_text);
126
Damien George6810f2c2016-11-16 11:55:41 +1100127#endif // MICROPY_INCLUDED_PY_PERSISTENTCODE_H