blob: 8769ef584d2d70b4d14a24e93950e9b4846206b2 [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
Damien George7e90e222019-05-02 09:59:21 +100033// The current version of .mpy files
Damien George5716c5c2019-09-26 16:39:37 +100034#define MPY_VERSION 5
Damien George7e90e222019-05-02 09:59:21 +100035
Damien Georgef4601af2019-11-01 21:04:07 +110036// 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 George9ac949c2019-11-30 23:00:56 +110058 #define MPY_FEATURE_ARCH (MP_NATIVE_ARCH_X86)
Damien Georgef4601af2019-11-01 21:04:07 +110059#elif MICROPY_EMIT_X64
Damien George9ac949c2019-11-30 23:00:56 +110060 #define MPY_FEATURE_ARCH (MP_NATIVE_ARCH_X64)
Damien Georgef4601af2019-11-01 21:04:07 +110061#elif MICROPY_EMIT_THUMB
Damien George9ac949c2019-11-30 23:00:56 +110062 #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 Georgef4601af2019-11-01 21:04:07 +110074#elif MICROPY_EMIT_ARM
Damien George9ac949c2019-11-30 23:00:56 +110075 #define MPY_FEATURE_ARCH (MP_NATIVE_ARCH_ARMV6)
Damien Georgef4601af2019-11-01 21:04:07 +110076#elif MICROPY_EMIT_XTENSA
Damien George9ac949c2019-11-30 23:00:56 +110077 #define MPY_FEATURE_ARCH (MP_NATIVE_ARCH_XTENSA)
Damien Georgef4601af2019-11-01 21:04:07 +110078#elif MICROPY_EMIT_XTENSAWIN
Damien George9ac949c2019-11-30 23:00:56 +110079 #define MPY_FEATURE_ARCH (MP_NATIVE_ARCH_XTENSAWIN)
Damien Georgef4601af2019-11-01 21:04:07 +110080#else
Damien George9ac949c2019-11-30 23:00:56 +110081 #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 Georgef4601af2019-11-01 21:04:07 +110086#endif
87
Damien George80df3772019-10-30 16:26:11 +110088// 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 George1396a022019-02-21 15:18:33 +110092enum {
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 George9adedce2019-09-13 13:15:12 +1000103 MP_NATIVE_ARCH_XTENSAWIN,
Damien George1396a022019-02-21 15:18:33 +1100104};
105
Damien George6810f2c2016-11-16 11:55:41 +1100106mp_raw_code_t *mp_raw_code_load(mp_reader_t *reader);
107mp_raw_code_t *mp_raw_code_load_mem(const byte *buf, size_t len);
108mp_raw_code_t *mp_raw_code_load_file(const char *filename);
109
110void mp_raw_code_save(mp_raw_code_t *rc, mp_print_t *print);
111void mp_raw_code_save_file(mp_raw_code_t *rc, const char *filename);
112
Damien Georgeb47e1552019-10-06 23:29:40 +1100113void mp_native_relocate(void *reloc, uint8_t *text, uintptr_t reloc_text);
114
Damien George6810f2c2016-11-16 11:55:41 +1100115#endif // MICROPY_INCLUDED_PY_PERSISTENTCODE_H