37 #if MICROPY_PERSISTENT_CODE_LOAD || MICROPY_PERSISTENT_CODE_SAVE 42 #define MPY_VERSION (3) 46 #define MPY_FEATURE_FLAGS ( \ 47 ((MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE) << 0) \ 48 | ((MICROPY_PY_BUILTINS_STR_UNICODE) << 1) \ 51 #define MPY_FEATURE_FLAGS_DYNAMIC ( \ 52 ((MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE_DYNAMIC) << 0) \ 53 | ((MICROPY_PY_BUILTINS_STR_UNICODE_DYNAMIC) << 1) \ 56 #if MICROPY_PERSISTENT_CODE_LOAD || (MICROPY_PERSISTENT_CODE_SAVE && !MICROPY_DYNAMIC_COMPILER) 60 STATIC int mp_small_int_bits(
void) {
71 typedef struct _bytecode_prelude_t {
83 STATIC void extract_prelude(
const byte **ip,
const byte **ip2, bytecode_prelude_t *prelude) {
86 prelude->scope_flags = *(*ip)++;
87 prelude->n_pos_args = *(*ip)++;
88 prelude->n_kwonly_args = *(*ip)++;
89 prelude->n_def_pos_args = *(*ip)++;
92 *ip += prelude->code_info_size;
93 while (*(*ip)++ != 255) {
97 #endif // MICROPY_PERSISTENT_CODE_LOAD || MICROPY_PERSISTENT_CODE_SAVE 99 #if MICROPY_PERSISTENT_CODE_LOAD 117 unum = (unum << 7) | (b & 0x7f);
118 if ((b & 0x80) == 0) {
126 size_t len = read_uint(reader);
127 char *str =
m_new(
char, len);
128 read_bytes(reader, (
byte*)str, len);
130 m_del(
char, str, len);
135 byte obj_type = read_byte(reader);
136 if (obj_type ==
'e') {
139 size_t len = read_uint(reader);
142 read_bytes(reader, (
byte*)vstr.
buf, len);
143 if (obj_type ==
's' || obj_type ==
'b') {
145 }
else if (obj_type ==
'i') {
148 assert(obj_type ==
'f' || obj_type ==
'c');
155 while (ip < ip_top) {
157 uint f = mp_opcode_format(ip, &sz);
158 if (f == MP_OPCODE_QSTR) {
159 qstr qst = load_qstr(reader);
169 size_t bc_len = read_uint(reader);
171 read_bytes(reader, bytecode, bc_len);
174 const byte *ip = bytecode;
176 bytecode_prelude_t prelude;
177 extract_prelude(&ip, &ip2, &prelude);
180 qstr simple_name = load_qstr(reader);
181 qstr source_file = load_qstr(reader);
182 ((
byte*)ip2)[0] = simple_name; ((
byte*)ip2)[1] = simple_name >> 8;
183 ((
byte*)ip2)[2] = source_file; ((
byte*)ip2)[3] = source_file >> 8;
184 load_bytecode_qstrs(reader, (
byte*)ip, bytecode + bc_len);
187 size_t n_obj = read_uint(reader);
188 size_t n_raw_code = read_uint(reader);
191 for (
size_t i = 0; i < prelude.n_pos_args + prelude.n_kwonly_args; ++i) {
194 for (
size_t i = 0; i < n_obj; ++i) {
197 for (
size_t i = 0; i < n_raw_code; ++i) {
207 prelude.scope_flags);
213 read_bytes(reader, header,
sizeof(header));
215 || header[1] != MPY_VERSION
216 || header[2] != MPY_FEATURE_FLAGS
217 || header[3] > mp_small_int_bits()) {
237 #endif // MICROPY_PERSISTENT_CODE_LOAD 239 #if MICROPY_PERSISTENT_CODE_SAVE 247 #define BYTES_FOR_INT ((BYTES_PER_WORD * 8 + 6) / 7) 249 byte buf[BYTES_FOR_INT];
250 byte *p = buf +
sizeof(buf);
253 for (; n != 0; n >>= 7) {
254 *--p = 0x80 | (n & 0x7f);
262 mp_print_uint(print, len);
263 mp_print_bytes(print, str, len);
276 mp_print_bytes(print, &obj_type, 1);
277 mp_print_uint(print, len);
278 mp_print_bytes(print, (
const byte*)str, len);
281 mp_print_bytes(print, &obj_type, 1);
288 #if MICROPY_PY_BUILTINS_COMPLEX 300 mp_print_bytes(print, &obj_type, 1);
301 mp_print_uint(print, vstr.
len);
302 mp_print_bytes(print, (
const byte*)vstr.
buf, vstr.
len);
308 while (ip < ip_top) {
310 uint f = mp_opcode_format(ip, &sz);
311 if (f == MP_OPCODE_QSTR) {
312 qstr qst = ip[1] | (ip[2] << 8);
313 save_qstr(print, qst);
331 bytecode_prelude_t prelude;
332 extract_prelude(&ip, &ip2, &prelude);
335 save_qstr(print, ip2[0] | (ip2[1] << 8));
336 save_qstr(print, ip2[2] | (ip2[3] << 8));
341 mp_print_uint(print, rc->
data.
u_byte.n_raw_code);
343 for (
uint i = 0; i < prelude.n_pos_args + prelude.n_kwonly_args; ++i) {
348 save_obj(print, (
mp_obj_t)*const_table++);
361 byte header[4] = {
'M', MPY_VERSION, MPY_FEATURE_FLAGS_DYNAMIC,
362 #if MICROPY_DYNAMIC_COMPILER 363 mp_dynamic_compiler.small_int_bits,
368 mp_print_bytes(print, header,
sizeof(header));
370 save_raw_code(print, rc);
376 #if defined(__i386__) || defined(__x86_64__) || defined(__unix__) 379 #include <sys/stat.h> 382 STATIC void fd_print_strn(
void *env,
const char *str,
size_t len) {
384 ssize_t ret = write(fd, str, len);
389 int fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0644);
396 #error mp_raw_code_save_file not implemented for this platform 399 #endif // MICROPY_PERSISTENT_CODE_SAVE
qstr qstr_from_strn(const char *str, size_t len)
union _mp_raw_code_t::@15 data
struct _mp_raw_code_t::@15::@16 u_byte
mp_raw_code_t * mp_raw_code_load_file(const char *filename)
mp_print_strn_t print_strn
#define MP_OBJ_IS_TYPE(o, t)
#define m_del(type, ptr, num)
void vstr_init_len(vstr_t *vstr, size_t len)
#define MP_OBJ_QSTR_VALUE(o)
void mp_raw_code_save_file(mp_raw_code_t *rc, const char *filename)
mp_raw_code_t * mp_raw_code_load_mem(const byte *buf, size_t len)
#define MP_OBJ_FROM_PTR(p)
#define MP_OBJ_NEW_QSTR(qst)
void mp_reader_new_file(mp_reader_t *reader, const char *filename)
const mp_obj_type_t mp_type_bytes
#define mp_obj_is_float(o)
const mp_obj_type_t mp_type_complex
const char * mp_obj_str_get_data(mp_obj_t self_in, size_t *len)
void mp_obj_print_helper(const mp_print_t *print, mp_obj_t o_in, mp_print_kind_t kind)
const mp_obj_type_t mp_type_str
mp_raw_code_t * mp_raw_code_load(mp_reader_t *reader)
mp_obj_t mp_obj_new_str_from_vstr(const mp_obj_type_t *type, vstr_t *vstr)
mp_uint_t mp_decode_uint(const byte **ptr)
mp_obj_t mp_parse_num_decimal(const char *str, size_t len, bool allow_imag, bool force_complex, mp_lexer_t *lex)
const byte * qstr_data(qstr q, size_t *len)
NORETURN void mp_raise_ValueError(const char *msg)
const struct _mp_obj_singleton_t mp_const_ellipsis_obj
mp_raw_code_t * mp_emit_glue_new_raw_code(void)
void(* close)(void *data)
void mp_raw_code_save(mp_raw_code_t *rc, mp_print_t *print)
mp_obj_t mp_parse_num_integer(const char *restrict str_, size_t len, int base, mp_lexer_t *lex)
mp_uint_t(* readbyte)(void *data)
const mp_obj_type_t mp_type_int
void mp_reader_new_mem(mp_reader_t *reader, const byte *buf, size_t len, size_t free_len)
void mp_emit_glue_assign_bytecode(mp_raw_code_t *rc, const byte *code, mp_uint_t len, const mp_uint_t *const_table, mp_uint_t scope_flags)
#define MP_OBJ_IS_STR_OR_BYTES(o)
void vstr_init_print(vstr_t *vstr, size_t alloc, struct _mp_print_t *print)
void vstr_clear(vstr_t *vstr)
#define MICROPY_PERSISTENT_CODE_SAVE