blob: fde7a4625dcebe97d6dfdf5b2b5d3d9f83e38baf [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
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#define MPY_FEATURE_ARCH (MP_NATIVE_ARCH_ARMV7M)
63#elif MICROPY_EMIT_ARM
64#define MPY_FEATURE_ARCH (MP_NATIVE_ARCH_ARMV6)
65#elif MICROPY_EMIT_XTENSA
66#define MPY_FEATURE_ARCH (MP_NATIVE_ARCH_XTENSA)
67#elif MICROPY_EMIT_XTENSAWIN
68#define MPY_FEATURE_ARCH (MP_NATIVE_ARCH_XTENSAWIN)
69#else
70#define MPY_FEATURE_ARCH (MP_NATIVE_ARCH_NONE)
71#endif
72
Damien George80df3772019-10-30 16:26:11 +110073// 16-bit little-endian integer with the second and third bytes of supported .mpy files
74#define MPY_FILE_HEADER_INT (MPY_VERSION \
75 | (MPY_FEATURE_ENCODE_FLAGS(MPY_FEATURE_FLAGS) | MPY_FEATURE_ENCODE_ARCH(MPY_FEATURE_ARCH)) << 8)
76
Damien George1396a022019-02-21 15:18:33 +110077enum {
78 MP_NATIVE_ARCH_NONE = 0,
79 MP_NATIVE_ARCH_X86,
80 MP_NATIVE_ARCH_X64,
81 MP_NATIVE_ARCH_ARMV6,
82 MP_NATIVE_ARCH_ARMV6M,
83 MP_NATIVE_ARCH_ARMV7M,
84 MP_NATIVE_ARCH_ARMV7EM,
85 MP_NATIVE_ARCH_ARMV7EMSP,
86 MP_NATIVE_ARCH_ARMV7EMDP,
87 MP_NATIVE_ARCH_XTENSA,
Damien George9adedce2019-09-13 13:15:12 +100088 MP_NATIVE_ARCH_XTENSAWIN,
Damien George1396a022019-02-21 15:18:33 +110089};
90
Damien George6810f2c2016-11-16 11:55:41 +110091mp_raw_code_t *mp_raw_code_load(mp_reader_t *reader);
92mp_raw_code_t *mp_raw_code_load_mem(const byte *buf, size_t len);
93mp_raw_code_t *mp_raw_code_load_file(const char *filename);
94
95void mp_raw_code_save(mp_raw_code_t *rc, mp_print_t *print);
96void mp_raw_code_save_file(mp_raw_code_t *rc, const char *filename);
97
Damien Georgeb47e1552019-10-06 23:29:40 +110098void mp_native_relocate(void *reloc, uint8_t *text, uintptr_t reloc_text);
99
Damien George6810f2c2016-11-16 11:55:41 +1100100#endif // MICROPY_INCLUDED_PY_PERSISTENTCODE_H