blob: 2fa8c1df07eddc57494a13a20b3d709bdbf6aaa7 [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
27#include <stdint.h>
28#include <stdio.h>
29#include <string.h>
30#include <assert.h>
31
Damien George6b239c22016-11-16 16:04:57 +110032#include "py/reader.h"
Damien George6810f2c2016-11-16 11:55:41 +110033#include "py/emitglue.h"
34#include "py/persistentcode.h"
35#include "py/bc.h"
36
37#if MICROPY_PERSISTENT_CODE_LOAD || MICROPY_PERSISTENT_CODE_SAVE
38
39#include "py/smallint.h"
40
Damien George6a110482017-02-17 00:19:34 +110041// The current version of .mpy files
Damien Georgedd11af22017-04-19 09:45:59 +100042#define MPY_VERSION (2)
Damien George6a110482017-02-17 00:19:34 +110043
Damien George6810f2c2016-11-16 11:55:41 +110044// The feature flags byte encodes 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#if MICROPY_PERSISTENT_CODE_LOAD || (MICROPY_PERSISTENT_CODE_SAVE && !MICROPY_DYNAMIC_COMPILER)
57// The bytecode will depend on the number of bits in a small-int, and
58// this function computes that (could make it a fixed constant, but it
59// would need to be defined in mpconfigport.h).
60STATIC int mp_small_int_bits(void) {
61 mp_int_t i = MP_SMALL_INT_MAX;
62 int n = 1;
63 while (i != 0) {
64 i >>= 1;
65 ++n;
66 }
67 return n;
68}
69#endif
70
71typedef struct _bytecode_prelude_t {
72 uint n_state;
73 uint n_exc_stack;
74 uint scope_flags;
75 uint n_pos_args;
76 uint n_kwonly_args;
77 uint n_def_pos_args;
78 uint code_info_size;
79} bytecode_prelude_t;
80
81// ip will point to start of opcodes
82// ip2 will point to simple_name, source_file qstrs
83STATIC void extract_prelude(const byte **ip, const byte **ip2, bytecode_prelude_t *prelude) {
84 prelude->n_state = mp_decode_uint(ip);
85 prelude->n_exc_stack = mp_decode_uint(ip);
86 prelude->scope_flags = *(*ip)++;
87 prelude->n_pos_args = *(*ip)++;
88 prelude->n_kwonly_args = *(*ip)++;
89 prelude->n_def_pos_args = *(*ip)++;
90 *ip2 = *ip;
91 prelude->code_info_size = mp_decode_uint(ip2);
92 *ip += prelude->code_info_size;
93 while (*(*ip)++ != 255) {
94 }
95}
96
97#endif // MICROPY_PERSISTENT_CODE_LOAD || MICROPY_PERSISTENT_CODE_SAVE
98
99#if MICROPY_PERSISTENT_CODE_LOAD
100
101#include "py/parsenum.h"
102#include "py/bc0.h"
103
104STATIC int read_byte(mp_reader_t *reader) {
Damien George6b239c22016-11-16 16:04:57 +1100105 return reader->readbyte(reader->data);
Damien George6810f2c2016-11-16 11:55:41 +1100106}
107
108STATIC void read_bytes(mp_reader_t *reader, byte *buf, size_t len) {
109 while (len-- > 0) {
Damien George6b239c22016-11-16 16:04:57 +1100110 *buf++ = reader->readbyte(reader->data);
Damien George6810f2c2016-11-16 11:55:41 +1100111 }
112}
113
Damien George891dc5c2017-02-16 16:04:13 +1100114STATIC size_t read_uint(mp_reader_t *reader) {
115 size_t unum = 0;
Damien George6810f2c2016-11-16 11:55:41 +1100116 for (;;) {
Damien George6b239c22016-11-16 16:04:57 +1100117 byte b = reader->readbyte(reader->data);
Damien George6810f2c2016-11-16 11:55:41 +1100118 unum = (unum << 7) | (b & 0x7f);
119 if ((b & 0x80) == 0) {
120 break;
121 }
122 }
123 return unum;
124}
125
126STATIC qstr load_qstr(mp_reader_t *reader) {
Damien George891dc5c2017-02-16 16:04:13 +1100127 size_t len = read_uint(reader);
Damien George6810f2c2016-11-16 11:55:41 +1100128 char *str = m_new(char, len);
129 read_bytes(reader, (byte*)str, len);
130 qstr qst = qstr_from_strn(str, len);
131 m_del(char, str, len);
132 return qst;
133}
134
135STATIC mp_obj_t load_obj(mp_reader_t *reader) {
136 byte obj_type = read_byte(reader);
137 if (obj_type == 'e') {
138 return MP_OBJ_FROM_PTR(&mp_const_ellipsis_obj);
139 } else {
140 size_t len = read_uint(reader);
141 vstr_t vstr;
142 vstr_init_len(&vstr, len);
143 read_bytes(reader, (byte*)vstr.buf, len);
144 if (obj_type == 's' || obj_type == 'b') {
145 return mp_obj_new_str_from_vstr(obj_type == 's' ? &mp_type_str : &mp_type_bytes, &vstr);
146 } else if (obj_type == 'i') {
147 return mp_parse_num_integer(vstr.buf, vstr.len, 10, NULL);
148 } else {
149 assert(obj_type == 'f' || obj_type == 'c');
150 return mp_parse_num_decimal(vstr.buf, vstr.len, obj_type == 'c', false, NULL);
151 }
152 }
153}
154
155STATIC void load_bytecode_qstrs(mp_reader_t *reader, byte *ip, byte *ip_top) {
156 while (ip < ip_top) {
157 size_t sz;
158 uint f = mp_opcode_format(ip, &sz);
159 if (f == MP_OPCODE_QSTR) {
160 qstr qst = load_qstr(reader);
161 ip[1] = qst;
162 ip[2] = qst >> 8;
163 }
164 ip += sz;
165 }
166}
167
168STATIC mp_raw_code_t *load_raw_code(mp_reader_t *reader) {
169 // load bytecode
Damien George891dc5c2017-02-16 16:04:13 +1100170 size_t bc_len = read_uint(reader);
Damien George6810f2c2016-11-16 11:55:41 +1100171 byte *bytecode = m_new(byte, bc_len);
172 read_bytes(reader, bytecode, bc_len);
173
174 // extract prelude
175 const byte *ip = bytecode;
176 const byte *ip2;
177 bytecode_prelude_t prelude;
178 extract_prelude(&ip, &ip2, &prelude);
179
180 // load qstrs and link global qstr ids into bytecode
181 qstr simple_name = load_qstr(reader);
182 qstr source_file = load_qstr(reader);
183 ((byte*)ip2)[0] = simple_name; ((byte*)ip2)[1] = simple_name >> 8;
184 ((byte*)ip2)[2] = source_file; ((byte*)ip2)[3] = source_file >> 8;
185 load_bytecode_qstrs(reader, (byte*)ip, bytecode + bc_len);
186
187 // load constant table
Damien George891dc5c2017-02-16 16:04:13 +1100188 size_t n_obj = read_uint(reader);
189 size_t n_raw_code = read_uint(reader);
Damien George6810f2c2016-11-16 11:55:41 +1100190 mp_uint_t *const_table = m_new(mp_uint_t, prelude.n_pos_args + prelude.n_kwonly_args + n_obj + n_raw_code);
191 mp_uint_t *ct = const_table;
Damien George891dc5c2017-02-16 16:04:13 +1100192 for (size_t i = 0; i < prelude.n_pos_args + prelude.n_kwonly_args; ++i) {
Damien George6810f2c2016-11-16 11:55:41 +1100193 *ct++ = (mp_uint_t)MP_OBJ_NEW_QSTR(load_qstr(reader));
194 }
Damien George891dc5c2017-02-16 16:04:13 +1100195 for (size_t i = 0; i < n_obj; ++i) {
Damien George6810f2c2016-11-16 11:55:41 +1100196 *ct++ = (mp_uint_t)load_obj(reader);
197 }
Damien George891dc5c2017-02-16 16:04:13 +1100198 for (size_t i = 0; i < n_raw_code; ++i) {
Damien George6810f2c2016-11-16 11:55:41 +1100199 *ct++ = (mp_uint_t)(uintptr_t)load_raw_code(reader);
200 }
201
202 // create raw_code and return it
203 mp_raw_code_t *rc = mp_emit_glue_new_raw_code();
204 mp_emit_glue_assign_bytecode(rc, bytecode, bc_len, const_table,
205 #if MICROPY_PERSISTENT_CODE_SAVE
206 n_obj, n_raw_code,
207 #endif
208 prelude.scope_flags);
209 return rc;
210}
211
212mp_raw_code_t *mp_raw_code_load(mp_reader_t *reader) {
213 byte header[4];
214 read_bytes(reader, header, sizeof(header));
Damien George6a110482017-02-17 00:19:34 +1100215 if (header[0] != 'M'
216 || header[1] != MPY_VERSION
217 || header[2] != MPY_FEATURE_FLAGS
218 || header[3] > mp_small_int_bits()) {
Damien George6810f2c2016-11-16 11:55:41 +1100219 mp_raise_ValueError("incompatible .mpy file");
220 }
Damien George6b239c22016-11-16 16:04:57 +1100221 mp_raw_code_t *rc = load_raw_code(reader);
222 reader->close(reader->data);
223 return rc;
Damien George6810f2c2016-11-16 11:55:41 +1100224}
225
226mp_raw_code_t *mp_raw_code_load_mem(const byte *buf, size_t len) {
Damien George6b239c22016-11-16 16:04:57 +1100227 mp_reader_t reader;
Damien George18310342017-03-14 11:16:31 +1100228 mp_reader_new_mem(&reader, buf, len, 0);
Damien George6810f2c2016-11-16 11:55:41 +1100229 return mp_raw_code_load(&reader);
230}
231
Damien George6810f2c2016-11-16 11:55:41 +1100232mp_raw_code_t *mp_raw_code_load_file(const char *filename) {
Damien George6810f2c2016-11-16 11:55:41 +1100233 mp_reader_t reader;
Damien George18310342017-03-14 11:16:31 +1100234 mp_reader_new_file(&reader, filename);
Damien George6b239c22016-11-16 16:04:57 +1100235 return mp_raw_code_load(&reader);
Damien George6810f2c2016-11-16 11:55:41 +1100236}
237
Damien George6810f2c2016-11-16 11:55:41 +1100238#endif // MICROPY_PERSISTENT_CODE_LOAD
239
240#if MICROPY_PERSISTENT_CODE_SAVE
241
242#include "py/objstr.h"
243
244STATIC void mp_print_bytes(mp_print_t *print, const byte *data, size_t len) {
245 print->print_strn(print->data, (const char*)data, len);
246}
247
248#define BYTES_FOR_INT ((BYTES_PER_WORD * 8 + 6) / 7)
Damien George891dc5c2017-02-16 16:04:13 +1100249STATIC void mp_print_uint(mp_print_t *print, size_t n) {
Damien George6810f2c2016-11-16 11:55:41 +1100250 byte buf[BYTES_FOR_INT];
251 byte *p = buf + sizeof(buf);
252 *--p = n & 0x7f;
253 n >>= 7;
254 for (; n != 0; n >>= 7) {
255 *--p = 0x80 | (n & 0x7f);
256 }
257 print->print_strn(print->data, (char*)p, buf + sizeof(buf) - p);
258}
259
260STATIC void save_qstr(mp_print_t *print, qstr qst) {
261 size_t len;
262 const byte *str = qstr_data(qst, &len);
263 mp_print_uint(print, len);
264 mp_print_bytes(print, str, len);
265}
266
267STATIC void save_obj(mp_print_t *print, mp_obj_t o) {
268 if (MP_OBJ_IS_STR_OR_BYTES(o)) {
269 byte obj_type;
270 if (MP_OBJ_IS_STR(o)) {
271 obj_type = 's';
272 } else {
273 obj_type = 'b';
274 }
275 mp_uint_t len;
276 const char *str = mp_obj_str_get_data(o, &len);
277 mp_print_bytes(print, &obj_type, 1);
278 mp_print_uint(print, len);
279 mp_print_bytes(print, (const byte*)str, len);
280 } else if (MP_OBJ_TO_PTR(o) == &mp_const_ellipsis_obj) {
281 byte obj_type = 'e';
282 mp_print_bytes(print, &obj_type, 1);
283 } else {
284 // we save numbers using a simplistic text representation
285 // TODO could be improved
286 byte obj_type;
287 if (MP_OBJ_IS_TYPE(o, &mp_type_int)) {
288 obj_type = 'i';
Damien George72732fe2017-06-08 00:28:28 +1000289 #if MICROPY_PY_BUILTINS_COMPLEX
290 } else if (MP_OBJ_IS_TYPE(o, &mp_type_complex)) {
Damien George6810f2c2016-11-16 11:55:41 +1100291 obj_type = 'c';
Damien George72732fe2017-06-08 00:28:28 +1000292 #endif
293 } else {
294 assert(mp_obj_is_float(o));
295 obj_type = 'f';
Damien George6810f2c2016-11-16 11:55:41 +1100296 }
297 vstr_t vstr;
298 mp_print_t pr;
299 vstr_init_print(&vstr, 10, &pr);
300 mp_obj_print_helper(&pr, o, PRINT_REPR);
301 mp_print_bytes(print, &obj_type, 1);
302 mp_print_uint(print, vstr.len);
303 mp_print_bytes(print, (const byte*)vstr.buf, vstr.len);
304 vstr_clear(&vstr);
305 }
306}
307
308STATIC void save_bytecode_qstrs(mp_print_t *print, const byte *ip, const byte *ip_top) {
309 while (ip < ip_top) {
310 size_t sz;
311 uint f = mp_opcode_format(ip, &sz);
312 if (f == MP_OPCODE_QSTR) {
313 qstr qst = ip[1] | (ip[2] << 8);
314 save_qstr(print, qst);
315 }
316 ip += sz;
317 }
318}
319
320STATIC void save_raw_code(mp_print_t *print, mp_raw_code_t *rc) {
321 if (rc->kind != MP_CODE_BYTECODE) {
322 mp_raise_ValueError("can only save bytecode");
323 }
324
325 // save bytecode
326 mp_print_uint(print, rc->data.u_byte.bc_len);
327 mp_print_bytes(print, rc->data.u_byte.bytecode, rc->data.u_byte.bc_len);
328
329 // extract prelude
330 const byte *ip = rc->data.u_byte.bytecode;
331 const byte *ip2;
332 bytecode_prelude_t prelude;
333 extract_prelude(&ip, &ip2, &prelude);
334
335 // save qstrs
336 save_qstr(print, ip2[0] | (ip2[1] << 8)); // simple_name
337 save_qstr(print, ip2[2] | (ip2[3] << 8)); // source_file
338 save_bytecode_qstrs(print, ip, rc->data.u_byte.bytecode + rc->data.u_byte.bc_len);
339
340 // save constant table
341 mp_print_uint(print, rc->data.u_byte.n_obj);
342 mp_print_uint(print, rc->data.u_byte.n_raw_code);
343 const mp_uint_t *const_table = rc->data.u_byte.const_table;
344 for (uint i = 0; i < prelude.n_pos_args + prelude.n_kwonly_args; ++i) {
345 mp_obj_t o = (mp_obj_t)*const_table++;
346 save_qstr(print, MP_OBJ_QSTR_VALUE(o));
347 }
348 for (uint i = 0; i < rc->data.u_byte.n_obj; ++i) {
349 save_obj(print, (mp_obj_t)*const_table++);
350 }
351 for (uint i = 0; i < rc->data.u_byte.n_raw_code; ++i) {
352 save_raw_code(print, (mp_raw_code_t*)(uintptr_t)*const_table++);
353 }
354}
355
356void mp_raw_code_save(mp_raw_code_t *rc, mp_print_t *print) {
357 // header contains:
358 // byte 'M'
359 // byte version
360 // byte feature flags
361 // byte number of bits in a small int
Damien George6a110482017-02-17 00:19:34 +1100362 byte header[4] = {'M', MPY_VERSION, MPY_FEATURE_FLAGS_DYNAMIC,
Damien George6810f2c2016-11-16 11:55:41 +1100363 #if MICROPY_DYNAMIC_COMPILER
364 mp_dynamic_compiler.small_int_bits,
365 #else
366 mp_small_int_bits(),
367 #endif
368 };
369 mp_print_bytes(print, header, sizeof(header));
370
371 save_raw_code(print, rc);
372}
373
374// here we define mp_raw_code_save_file depending on the port
375// TODO abstract this away properly
376
377#if defined(__i386__) || defined(__x86_64__) || (defined(__arm__) && (defined(__unix__)))
378
379#include <unistd.h>
380#include <sys/stat.h>
381#include <fcntl.h>
382
383STATIC void fd_print_strn(void *env, const char *str, size_t len) {
384 int fd = (intptr_t)env;
385 ssize_t ret = write(fd, str, len);
386 (void)ret;
387}
388
389void mp_raw_code_save_file(mp_raw_code_t *rc, const char *filename) {
390 int fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0644);
391 mp_print_t fd_print = {(void*)(intptr_t)fd, fd_print_strn};
392 mp_raw_code_save(rc, &fd_print);
393 close(fd);
394}
395
396#else
397#error mp_raw_code_save_file not implemented for this platform
398#endif
399
400#endif // MICROPY_PERSISTENT_CODE_SAVE