blob: 7e5edce756a162e911152ba145c32ebdfc25b5ac [file] [log] [blame]
Damien George04b91472014-05-03 23:27:38 +01001/*
2 * This file is part of the Micro Python project, http://micropython.org/
3 *
4 * The MIT License (MIT)
5 *
6 * Copyright (c) 2013, 2014 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
Damien George2326d522014-03-27 23:26:35 +000027// This code glues the code emitters to the runtime.
28
Damien Georged8c834c2015-11-02 21:55:42 +000029#include <stdint.h>
Damien George440f0412014-03-28 18:38:20 +000030#include <stdio.h>
Damien Georged1e443d2014-03-29 11:39:36 +000031#include <string.h>
Damien George2326d522014-03-27 23:26:35 +000032#include <assert.h>
33
Damien George51dfcb42015-01-01 20:27:54 +000034#include "py/emitglue.h"
35#include "py/runtime0.h"
36#include "py/bc.h"
Damien George2326d522014-03-27 23:26:35 +000037
38#if 0 // print debugging info
39#define DEBUG_PRINT (1)
40#define WRITE_CODE (1)
41#define DEBUG_printf DEBUG_printf
42#define DEBUG_OP_printf(...) DEBUG_printf(__VA_ARGS__)
43#else // don't print debugging info
44#define DEBUG_printf(...) (void)0
45#define DEBUG_OP_printf(...) (void)0
46#endif
47
Paul Sokolovsky295ea122015-11-21 16:34:57 +020048#if MICROPY_DEBUG_PRINTERS
49mp_uint_t mp_verbose_flag = 0;
50#endif
51
Damien Georgedf8127a2014-04-13 11:04:33 +010052mp_raw_code_t *mp_emit_glue_new_raw_code(void) {
53 mp_raw_code_t *rc = m_new0(mp_raw_code_t, 1);
54 rc->kind = MP_CODE_RESERVED;
55 return rc;
Damien George2326d522014-03-27 23:26:35 +000056}
57
Damien Georged8c834c2015-11-02 21:55:42 +000058void mp_emit_glue_assign_bytecode(mp_raw_code_t *rc, const byte *code, mp_uint_t len,
59 const mp_uint_t *const_table,
60 #if MICROPY_PERSISTENT_CODE_SAVE
61 uint16_t n_obj, uint16_t n_raw_code,
62 #endif
63 mp_uint_t scope_flags) {
64
Damien Georgeccc85ea2014-05-10 13:40:46 +010065 rc->kind = MP_CODE_BYTECODE;
Damien Georgedf8127a2014-04-13 11:04:33 +010066 rc->scope_flags = scope_flags;
Damien Georged8c834c2015-11-02 21:55:42 +000067 rc->data.u_byte.bytecode = code;
Damien George713ea182015-10-23 01:23:11 +010068 rc->data.u_byte.const_table = const_table;
Damien Georged8c834c2015-11-02 21:55:42 +000069 #if MICROPY_PERSISTENT_CODE_SAVE
70 rc->data.u_byte.bc_len = len;
71 rc->data.u_byte.n_obj = n_obj;
72 rc->data.u_byte.n_raw_code = n_raw_code;
73 #endif
Damien George2326d522014-03-27 23:26:35 +000074
75#ifdef DEBUG_PRINT
Damien George3a3db4d2015-10-22 23:45:37 +010076 DEBUG_printf("assign byte code: code=%p len=" UINT_FMT " flags=%x\n", code, len, (uint)scope_flags);
Damien George2326d522014-03-27 23:26:35 +000077#endif
Paul Sokolovsky6b344d72014-05-05 00:50:05 +030078#if MICROPY_DEBUG_PRINTERS
Paul Sokolovsky429e3f02014-10-26 23:20:22 +020079 if (mp_verbose_flag >= 2) {
Damien George713ea182015-10-23 01:23:11 +010080 mp_bytecode_print(rc, code, len, const_table);
Paul Sokolovsky6b344d72014-05-05 00:50:05 +030081 }
Damien George2326d522014-03-27 23:26:35 +000082#endif
83}
84
Damien George2ac4af62014-08-15 16:45:41 +010085#if MICROPY_EMIT_NATIVE || MICROPY_EMIT_INLINE_THUMB
Damien George713ea182015-10-23 01:23:11 +010086void mp_emit_glue_assign_native(mp_raw_code_t *rc, mp_raw_code_kind_t kind, void *fun_data, mp_uint_t fun_len, const mp_uint_t *const_table, mp_uint_t n_pos_args, mp_uint_t scope_flags, mp_uint_t type_sig) {
Damien Georgeccc85ea2014-05-10 13:40:46 +010087 assert(kind == MP_CODE_NATIVE_PY || kind == MP_CODE_NATIVE_VIPER || kind == MP_CODE_NATIVE_ASM);
88 rc->kind = kind;
Damien George99886182015-04-06 22:38:53 +010089 rc->scope_flags = scope_flags;
90 rc->n_pos_args = n_pos_args;
Damien George32444b72015-01-24 23:14:12 +000091 rc->data.u_native.fun_data = fun_data;
Damien George713ea182015-10-23 01:23:11 +010092 rc->data.u_native.const_table = const_table;
Damien George32444b72015-01-24 23:14:12 +000093 rc->data.u_native.type_sig = type_sig;
Damien George2326d522014-03-27 23:26:35 +000094
95#ifdef DEBUG_PRINT
Damien George3a3db4d2015-10-22 23:45:37 +010096 DEBUG_printf("assign native: kind=%d fun=%p len=" UINT_FMT " n_pos_args=" UINT_FMT " flags=%x\n", kind, fun_data, fun_len, n_pos_args, (uint)scope_flags);
Damien George3c658a42014-08-24 16:28:17 +010097 for (mp_uint_t i = 0; i < fun_len; i++) {
Damien George2326d522014-03-27 23:26:35 +000098 if (i > 0 && i % 16 == 0) {
99 DEBUG_printf("\n");
100 }
Damien George3c658a42014-08-24 16:28:17 +0100101 DEBUG_printf(" %02x", ((byte*)fun_data)[i]);
Damien George2326d522014-03-27 23:26:35 +0000102 }
103 DEBUG_printf("\n");
104
105#ifdef WRITE_CODE
Damien George915197a2014-05-12 23:11:14 +0100106 FILE *fp_write_code = fopen("out-code", "wb");
Damien Georgeb427d6a2014-08-26 23:35:57 +0100107 fwrite(fun_data, fun_len, 1, fp_write_code);
Damien George915197a2014-05-12 23:11:14 +0100108 fclose(fp_write_code);
Damien George2326d522014-03-27 23:26:35 +0000109#endif
Damien Georgeff8dd3f2015-01-20 12:47:20 +0000110#else
111 (void)fun_len;
Damien George2326d522014-03-27 23:26:35 +0000112#endif
113}
Damien George2ac4af62014-08-15 16:45:41 +0100114#endif
Damien George2326d522014-03-27 23:26:35 +0000115
Damien Georgeed0c1122016-01-31 22:19:42 +0000116mp_obj_t mp_make_function_from_raw_code(const mp_raw_code_t *rc, mp_obj_t def_args, mp_obj_t def_kw_args) {
Damien Georgedf8127a2014-04-13 11:04:33 +0100117 DEBUG_OP_printf("make_function_from_raw_code %p\n", rc);
118 assert(rc != NULL);
Damien George2326d522014-03-27 23:26:35 +0000119
Damien George69b89d22014-04-11 13:38:30 +0000120 // def_args must be MP_OBJ_NULL or a tuple
121 assert(def_args == MP_OBJ_NULL || MP_OBJ_IS_TYPE(def_args, &mp_type_tuple));
122
Damien Georgef0778a72014-06-07 22:01:00 +0100123 // def_kw_args must be MP_OBJ_NULL or a dict
124 assert(def_kw_args == MP_OBJ_NULL || MP_OBJ_IS_TYPE(def_kw_args, &mp_type_dict));
Damien Georgee337f1e2014-03-31 15:18:37 +0100125
Damien Georgedf8127a2014-04-13 11:04:33 +0100126 // make the function, depending on the raw code kind
Damien George2326d522014-03-27 23:26:35 +0000127 mp_obj_t fun;
Damien Georgedf8127a2014-04-13 11:04:33 +0100128 switch (rc->kind) {
Damien Georgeccc85ea2014-05-10 13:40:46 +0100129 case MP_CODE_BYTECODE:
Damien Georged2d64f02015-01-14 21:32:42 +0000130 no_other_choice:
Damien Georged8c834c2015-11-02 21:55:42 +0000131 fun = mp_obj_new_fun_bc(def_args, def_kw_args, rc->data.u_byte.bytecode, rc->data.u_byte.const_table);
Damien George2326d522014-03-27 23:26:35 +0000132 break;
Damien George2ac4af62014-08-15 16:45:41 +0100133 #if MICROPY_EMIT_NATIVE
Damien Georgeccc85ea2014-05-10 13:40:46 +0100134 case MP_CODE_NATIVE_PY:
Damien George713ea182015-10-23 01:23:11 +0100135 fun = mp_obj_new_fun_native(def_args, def_kw_args, rc->data.u_native.fun_data, rc->data.u_native.const_table);
Damien George2326d522014-03-27 23:26:35 +0000136 break;
Damien Georgeccc85ea2014-05-10 13:40:46 +0100137 case MP_CODE_NATIVE_VIPER:
Damien George32444b72015-01-24 23:14:12 +0000138 fun = mp_obj_new_fun_viper(rc->n_pos_args, rc->data.u_native.fun_data, rc->data.u_native.type_sig);
Damien George2ac4af62014-08-15 16:45:41 +0100139 break;
140 #endif
141 #if MICROPY_EMIT_INLINE_THUMB
Damien Georgeccc85ea2014-05-10 13:40:46 +0100142 case MP_CODE_NATIVE_ASM:
Damien George8f54c082016-01-15 15:20:43 +0000143 fun = mp_obj_new_fun_asm(rc->n_pos_args, rc->data.u_native.fun_data, rc->data.u_native.type_sig);
Damien George2326d522014-03-27 23:26:35 +0000144 break;
Damien George2ac4af62014-08-15 16:45:41 +0100145 #endif
Damien George2326d522014-03-27 23:26:35 +0000146 default:
Damien Georgedf8127a2014-04-13 11:04:33 +0100147 // raw code was never set (this should not happen)
Damien George2326d522014-03-27 23:26:35 +0000148 assert(0);
Damien Georged2d64f02015-01-14 21:32:42 +0000149 goto no_other_choice; // to help flow control analysis
Damien George2326d522014-03-27 23:26:35 +0000150 }
151
152 // check for generator functions and if so wrap in generator object
Damien Georgedf8127a2014-04-13 11:04:33 +0100153 if ((rc->scope_flags & MP_SCOPE_FLAG_GENERATOR) != 0) {
Damien George2326d522014-03-27 23:26:35 +0000154 fun = mp_obj_new_gen_wrap(fun);
155 }
156
157 return fun;
158}
159
Damien Georgeed0c1122016-01-31 22:19:42 +0000160mp_obj_t mp_make_closure_from_raw_code(const mp_raw_code_t *rc, mp_uint_t n_closed_over, const mp_obj_t *args) {
Damien George3c658a42014-08-24 16:28:17 +0100161 DEBUG_OP_printf("make_closure_from_raw_code %p " UINT_FMT " %p\n", rc, n_closed_over, args);
Damien George2326d522014-03-27 23:26:35 +0000162 // make function object
Damien George3558f622014-04-20 17:50:40 +0100163 mp_obj_t ffun;
164 if (n_closed_over & 0x100) {
165 // default positional and keyword args given
166 ffun = mp_make_function_from_raw_code(rc, args[0], args[1]);
167 } else {
168 // default positional and keyword args not given
169 ffun = mp_make_function_from_raw_code(rc, MP_OBJ_NULL, MP_OBJ_NULL);
170 }
Damien George2326d522014-03-27 23:26:35 +0000171 // wrap function in closure object
Damien George3558f622014-04-20 17:50:40 +0100172 return mp_obj_new_closure(ffun, n_closed_over & 0xff, args + ((n_closed_over >> 7) & 2));
Damien George2326d522014-03-27 23:26:35 +0000173}
Damien Georged8c834c2015-11-02 21:55:42 +0000174
175#if MICROPY_PERSISTENT_CODE
176
Damien George1404d622016-01-08 13:35:35 +0000177#include "py/smallint.h"
178
179// The feature flags byte encodes the compile-time config options that
180// affect the generate bytecode.
181#define MPY_FEATURE_FLAGS ( \
182 ((MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE) << 0) \
183 | ((MICROPY_PY_BUILTINS_STR_UNICODE) << 1) \
184 )
Damien Georgeea235202016-02-11 22:30:53 +0000185// This is a version of the flags that can be configured at runtime.
186#define MPY_FEATURE_FLAGS_DYNAMIC ( \
187 ((MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE_DYNAMIC) << 0) \
188 | ((MICROPY_PY_BUILTINS_STR_UNICODE_DYNAMIC) << 1) \
189 )
Damien George1404d622016-01-08 13:35:35 +0000190
Damien Georgeea235202016-02-11 22:30:53 +0000191#if MICROPY_PERSISTENT_CODE_LOAD || (MICROPY_PERSISTENT_CODE_SAVE && !MICROPY_DYNAMIC_COMPILER)
Damien George1404d622016-01-08 13:35:35 +0000192// The bytecode will depend on the number of bits in a small-int, and
193// this function computes that (could make it a fixed constant, but it
194// would need to be defined in mpconfigport.h).
195STATIC int mp_small_int_bits(void) {
196 mp_int_t i = MP_SMALL_INT_MAX;
197 int n = 1;
198 while (i != 0) {
199 i >>= 1;
200 ++n;
201 }
202 return n;
203}
Damien Georgeea235202016-02-11 22:30:53 +0000204#endif
Damien George1404d622016-01-08 13:35:35 +0000205
Damien Georged8c834c2015-11-02 21:55:42 +0000206typedef struct _bytecode_prelude_t {
207 uint n_state;
208 uint n_exc_stack;
209 uint scope_flags;
210 uint n_pos_args;
211 uint n_kwonly_args;
212 uint n_def_pos_args;
213 uint code_info_size;
214} bytecode_prelude_t;
215
216// ip will point to start of opcodes
217// ip2 will point to simple_name, source_file qstrs
218STATIC void extract_prelude(const byte **ip, const byte **ip2, bytecode_prelude_t *prelude) {
219 prelude->n_state = mp_decode_uint(ip);
220 prelude->n_exc_stack = mp_decode_uint(ip);
221 prelude->scope_flags = *(*ip)++;
222 prelude->n_pos_args = *(*ip)++;
223 prelude->n_kwonly_args = *(*ip)++;
224 prelude->n_def_pos_args = *(*ip)++;
225 *ip2 = *ip;
226 prelude->code_info_size = mp_decode_uint(ip2);
227 *ip += prelude->code_info_size;
228 while (*(*ip)++ != 255) {
229 }
230}
231
232#endif // MICROPY_PERSISTENT_CODE
233
234#if MICROPY_PERSISTENT_CODE_LOAD
235
Damien George44e6e342015-11-23 11:54:12 +0000236#include "py/parsenum.h"
Damien Georged8c834c2015-11-02 21:55:42 +0000237#include "py/bc0.h"
238
Damien George44e6e342015-11-23 11:54:12 +0000239STATIC int read_byte(mp_reader_t *reader) {
240 return reader->read_byte(reader->data);
241}
242
Damien Georged8c834c2015-11-02 21:55:42 +0000243STATIC void read_bytes(mp_reader_t *reader, byte *buf, size_t len) {
244 while (len-- > 0) {
245 *buf++ = reader->read_byte(reader->data);
246 }
247}
248
249STATIC mp_uint_t read_uint(mp_reader_t *reader) {
250 mp_uint_t unum = 0;
251 for (;;) {
252 byte b = reader->read_byte(reader->data);
253 unum = (unum << 7) | (b & 0x7f);
254 if ((b & 0x80) == 0) {
255 break;
256 }
257 }
258 return unum;
259}
260
261STATIC qstr load_qstr(mp_reader_t *reader) {
262 mp_uint_t len = read_uint(reader);
263 char *str = m_new(char, len);
264 read_bytes(reader, (byte*)str, len);
265 qstr qst = qstr_from_strn(str, len);
266 m_del(char, str, len);
267 return qst;
268}
269
270STATIC mp_obj_t load_obj(mp_reader_t *reader) {
Damien George44e6e342015-11-23 11:54:12 +0000271 byte obj_type = read_byte(reader);
272 if (obj_type == 'e') {
Damien George999cedb2015-11-27 17:01:44 +0000273 return MP_OBJ_FROM_PTR(&mp_const_ellipsis_obj);
Damien George44e6e342015-11-23 11:54:12 +0000274 } else {
275 size_t len = read_uint(reader);
276 vstr_t vstr;
277 vstr_init_len(&vstr, len);
278 read_bytes(reader, (byte*)vstr.buf, len);
279 if (obj_type == 's' || obj_type == 'b') {
280 return mp_obj_new_str_from_vstr(obj_type == 's' ? &mp_type_str : &mp_type_bytes, &vstr);
281 } else if (obj_type == 'i') {
282 return mp_parse_num_integer(vstr.buf, vstr.len, 10, NULL);
283 } else {
284 assert(obj_type == 'f' || obj_type == 'c');
285 return mp_parse_num_decimal(vstr.buf, vstr.len, obj_type == 'c', false, NULL);
286 }
287 }
Damien Georged8c834c2015-11-02 21:55:42 +0000288}
289
290STATIC void load_bytecode_qstrs(mp_reader_t *reader, byte *ip, byte *ip_top) {
291 while (ip < ip_top) {
292 size_t sz;
293 uint f = mp_opcode_format(ip, &sz);
294 if (f == MP_OPCODE_QSTR) {
295 qstr qst = load_qstr(reader);
296 ip[1] = qst;
297 ip[2] = qst >> 8;
298 }
299 ip += sz;
300 }
301}
302
303STATIC mp_raw_code_t *load_raw_code(mp_reader_t *reader) {
304 // load bytecode
305 mp_uint_t bc_len = read_uint(reader);
306 byte *bytecode = m_new(byte, bc_len);
307 read_bytes(reader, bytecode, bc_len);
308
309 // extract prelude
310 const byte *ip = bytecode;
311 const byte *ip2;
312 bytecode_prelude_t prelude;
313 extract_prelude(&ip, &ip2, &prelude);
314
315 // load qstrs and link global qstr ids into bytecode
316 qstr simple_name = load_qstr(reader);
317 qstr source_file = load_qstr(reader);
318 ((byte*)ip2)[0] = simple_name; ((byte*)ip2)[1] = simple_name >> 8;
319 ((byte*)ip2)[2] = source_file; ((byte*)ip2)[3] = source_file >> 8;
320 load_bytecode_qstrs(reader, (byte*)ip, bytecode + bc_len);
321
322 // load constant table
323 mp_uint_t n_obj = read_uint(reader);
324 mp_uint_t n_raw_code = read_uint(reader);
325 mp_uint_t *const_table = m_new(mp_uint_t, prelude.n_pos_args + prelude.n_kwonly_args + n_obj + n_raw_code);
326 mp_uint_t *ct = const_table;
327 for (mp_uint_t i = 0; i < prelude.n_pos_args + prelude.n_kwonly_args; ++i) {
328 *ct++ = (mp_uint_t)MP_OBJ_NEW_QSTR(load_qstr(reader));
329 }
330 for (mp_uint_t i = 0; i < n_obj; ++i) {
331 *ct++ = (mp_uint_t)load_obj(reader);
332 }
333 for (mp_uint_t i = 0; i < n_raw_code; ++i) {
Damien George999cedb2015-11-27 17:01:44 +0000334 *ct++ = (mp_uint_t)(uintptr_t)load_raw_code(reader);
Damien Georged8c834c2015-11-02 21:55:42 +0000335 }
336
337 // create raw_code and return it
338 mp_raw_code_t *rc = mp_emit_glue_new_raw_code();
339 mp_emit_glue_assign_bytecode(rc, bytecode, bc_len, const_table,
340 #if MICROPY_PERSISTENT_CODE_SAVE
341 n_obj, n_raw_code,
342 #endif
343 prelude.scope_flags);
344 return rc;
345}
346
347mp_raw_code_t *mp_raw_code_load(mp_reader_t *reader) {
Damien George1404d622016-01-08 13:35:35 +0000348 byte header[4];
349 read_bytes(reader, header, sizeof(header));
Damien Georged8c834c2015-11-02 21:55:42 +0000350 if (strncmp((char*)header, "M\x00", 2) != 0) {
351 nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError,
352 "invalid .mpy file"));
353 }
Damien Georgeea235202016-02-11 22:30:53 +0000354 if (header[2] != MPY_FEATURE_FLAGS || header[3] > mp_small_int_bits()) {
Damien George39a8deb2015-11-23 10:58:16 +0000355 nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError,
356 "incompatible .mpy file"));
357 }
Damien Georged8c834c2015-11-02 21:55:42 +0000358 return load_raw_code(reader);
359}
360
Damien Georgeb5b1f2c2015-11-20 12:44:20 +0000361typedef struct _mp_mem_reader_t {
362 const byte *cur;
363 const byte *end;
364} mp_mem_reader_t;
365
366STATIC mp_uint_t mp_mem_reader_next_byte(void *br_in) {
367 mp_mem_reader_t *br = br_in;
368 if (br->cur < br->end) {
369 return *br->cur++;
370 } else {
371 return (mp_uint_t)-1;
372 }
373}
374
375mp_raw_code_t *mp_raw_code_load_mem(const byte *buf, size_t len) {
376 mp_mem_reader_t mr = {buf, buf + len};
377 mp_reader_t reader = {&mr, mp_mem_reader_next_byte};
378 return mp_raw_code_load(&reader);
379}
380
Damien Georged8c834c2015-11-02 21:55:42 +0000381// here we define mp_raw_code_load_file depending on the port
382// TODO abstract this away properly
383
Markus Fix4f008032016-03-07 12:15:01 +0100384#if defined(__i386__) || defined(__x86_64__) || (defined(__arm__) && (defined(__unix__)))
Damien Georged8c834c2015-11-02 21:55:42 +0000385// unix file reader
386
387#include <sys/stat.h>
388#include <fcntl.h>
Damien Georgeab8012b2015-12-18 12:33:08 +0000389#include <unistd.h>
Damien Georged8c834c2015-11-02 21:55:42 +0000390
391typedef struct _mp_lexer_file_buf_t {
392 int fd;
393 byte buf[20];
394 mp_uint_t len;
395 mp_uint_t pos;
396} mp_lexer_file_buf_t;
397
398STATIC mp_uint_t file_buf_next_byte(void *fb_in) {
399 mp_lexer_file_buf_t *fb = fb_in;
400 if (fb->pos >= fb->len) {
401 if (fb->len == 0) {
402 return (mp_uint_t)-1;
403 } else {
404 int n = read(fb->fd, fb->buf, sizeof(fb->buf));
405 if (n <= 0) {
406 fb->len = 0;
407 return (mp_uint_t)-1;
408 }
409 fb->len = n;
410 fb->pos = 0;
411 }
412 }
413 return fb->buf[fb->pos++];
414}
415
416mp_raw_code_t *mp_raw_code_load_file(const char *filename) {
417 mp_lexer_file_buf_t fb;
418 fb.fd = open(filename, O_RDONLY, 0644);
419 int n = read(fb.fd, fb.buf, sizeof(fb.buf));
420 fb.len = n;
421 fb.pos = 0;
422 mp_reader_t reader;
423 reader.data = &fb;
424 reader.read_byte = file_buf_next_byte;
425 mp_raw_code_t *rc = mp_raw_code_load(&reader);
426 close(fb.fd);
427 return rc;
428}
429
Damien Georgef1487272015-11-20 12:42:26 +0000430#elif defined(__thumb2__)
431// fatfs file reader (assume thumb2 arch uses fatfs...)
Damien Georged8c834c2015-11-02 21:55:42 +0000432
433#include "lib/fatfs/ff.h"
434
435typedef struct _mp_lexer_file_buf_t {
436 FIL fp;
437 byte buf[20];
438 uint16_t len;
439 uint16_t pos;
440} mp_lexer_file_buf_t;
441
442STATIC mp_uint_t file_buf_next_byte(void *fb_in) {
443 mp_lexer_file_buf_t *fb = fb_in;
444 if (fb->pos >= fb->len) {
445 if (fb->len < sizeof(fb->buf)) {
446 return (mp_uint_t)-1;
447 } else {
448 UINT n;
449 f_read(&fb->fp, fb->buf, sizeof(fb->buf), &n);
450 if (n == 0) {
451 return (mp_uint_t)-1;
452 }
453 fb->len = n;
454 fb->pos = 0;
455 }
456 }
457 return fb->buf[fb->pos++];
458}
459
460mp_raw_code_t *mp_raw_code_load_file(const char *filename) {
461 mp_lexer_file_buf_t fb;
462 /*FRESULT res =*/ f_open(&fb.fp, filename, FA_READ);
463 UINT n;
464 f_read(&fb.fp, fb.buf, sizeof(fb.buf), &n);
465 fb.len = n;
466 fb.pos = 0;
467
468 mp_reader_t reader;
469 reader.data = &fb;
470 reader.read_byte = file_buf_next_byte;
471 mp_raw_code_t *rc = mp_raw_code_load(&reader);
472
473 f_close(&fb.fp);
474
475 return rc;
476}
477
478#endif
479
480#endif // MICROPY_PERSISTENT_CODE_LOAD
481
482#if MICROPY_PERSISTENT_CODE_SAVE
Damien George44e6e342015-11-23 11:54:12 +0000483
484#include "py/objstr.h"
485
Damien Georged8c834c2015-11-02 21:55:42 +0000486STATIC void mp_print_bytes(mp_print_t *print, const byte *data, size_t len) {
487 print->print_strn(print->data, (const char*)data, len);
488}
489
490#define BYTES_FOR_INT ((BYTES_PER_WORD * 8 + 6) / 7)
491STATIC void mp_print_uint(mp_print_t *print, mp_uint_t n) {
492 byte buf[BYTES_FOR_INT];
493 byte *p = buf + sizeof(buf);
494 *--p = n & 0x7f;
495 n >>= 7;
496 for (; n != 0; n >>= 7) {
497 *--p = 0x80 | (n & 0x7f);
498 }
499 print->print_strn(print->data, (char*)p, buf + sizeof(buf) - p);
500}
501
502STATIC void save_qstr(mp_print_t *print, qstr qst) {
Damien Georgec3f64d92015-11-27 12:23:18 +0000503 size_t len;
Damien Georged8c834c2015-11-02 21:55:42 +0000504 const byte *str = qstr_data(qst, &len);
505 mp_print_uint(print, len);
506 mp_print_bytes(print, str, len);
507}
508
509STATIC void save_obj(mp_print_t *print, mp_obj_t o) {
Damien George44e6e342015-11-23 11:54:12 +0000510 if (MP_OBJ_IS_STR_OR_BYTES(o)) {
511 byte obj_type;
512 if (MP_OBJ_IS_STR(o)) {
513 obj_type = 's';
514 } else {
515 obj_type = 'b';
516 }
Damien Georged8c834c2015-11-02 21:55:42 +0000517 mp_uint_t len;
518 const char *str = mp_obj_str_get_data(o, &len);
Damien George44e6e342015-11-23 11:54:12 +0000519 mp_print_bytes(print, &obj_type, 1);
Damien Georged8c834c2015-11-02 21:55:42 +0000520 mp_print_uint(print, len);
521 mp_print_bytes(print, (const byte*)str, len);
Damien George999cedb2015-11-27 17:01:44 +0000522 } else if (MP_OBJ_TO_PTR(o) == &mp_const_ellipsis_obj) {
Damien George44e6e342015-11-23 11:54:12 +0000523 byte obj_type = 'e';
524 mp_print_bytes(print, &obj_type, 1);
Damien Georged8c834c2015-11-02 21:55:42 +0000525 } else {
Damien George44e6e342015-11-23 11:54:12 +0000526 // we save numbers using a simplistic text representation
527 // TODO could be improved
528 byte obj_type;
529 if (MP_OBJ_IS_TYPE(o, &mp_type_int)) {
530 obj_type = 'i';
Damien George8bb49312016-01-13 15:24:41 +0000531 } else if (mp_obj_is_float(o)) {
Damien George44e6e342015-11-23 11:54:12 +0000532 obj_type = 'f';
533 } else {
534 assert(MP_OBJ_IS_TYPE(o, &mp_type_complex));
535 obj_type = 'c';
536 }
537 vstr_t vstr;
538 mp_print_t pr;
539 vstr_init_print(&vstr, 10, &pr);
540 mp_obj_print_helper(&pr, o, PRINT_REPR);
541 mp_print_bytes(print, &obj_type, 1);
542 mp_print_uint(print, vstr.len);
543 mp_print_bytes(print, (const byte*)vstr.buf, vstr.len);
544 vstr_clear(&vstr);
Damien Georged8c834c2015-11-02 21:55:42 +0000545 }
546}
547
548STATIC void save_bytecode_qstrs(mp_print_t *print, const byte *ip, const byte *ip_top) {
549 while (ip < ip_top) {
550 size_t sz;
551 uint f = mp_opcode_format(ip, &sz);
552 if (f == MP_OPCODE_QSTR) {
553 qstr qst = ip[1] | (ip[2] << 8);
554 save_qstr(print, qst);
555 }
556 ip += sz;
557 }
558}
559
560STATIC void save_raw_code(mp_print_t *print, mp_raw_code_t *rc) {
561 if (rc->kind != MP_CODE_BYTECODE) {
562 nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError,
563 "can only save bytecode"));
564 }
565
566 // save bytecode
567 mp_print_uint(print, rc->data.u_byte.bc_len);
568 mp_print_bytes(print, rc->data.u_byte.bytecode, rc->data.u_byte.bc_len);
569
570 // extract prelude
571 const byte *ip = rc->data.u_byte.bytecode;
572 const byte *ip2;
573 bytecode_prelude_t prelude;
574 extract_prelude(&ip, &ip2, &prelude);
575
576 // save qstrs
577 save_qstr(print, ip2[0] | (ip2[1] << 8)); // simple_name
578 save_qstr(print, ip2[2] | (ip2[3] << 8)); // source_file
579 save_bytecode_qstrs(print, ip, rc->data.u_byte.bytecode + rc->data.u_byte.bc_len);
580
581 // save constant table
582 mp_print_uint(print, rc->data.u_byte.n_obj);
583 mp_print_uint(print, rc->data.u_byte.n_raw_code);
584 const mp_uint_t *const_table = rc->data.u_byte.const_table;
585 for (uint i = 0; i < prelude.n_pos_args + prelude.n_kwonly_args; ++i) {
586 mp_obj_t o = (mp_obj_t)*const_table++;
587 save_qstr(print, MP_OBJ_QSTR_VALUE(o));
588 }
589 for (uint i = 0; i < rc->data.u_byte.n_obj; ++i) {
590 save_obj(print, (mp_obj_t)*const_table++);
591 }
592 for (uint i = 0; i < rc->data.u_byte.n_raw_code; ++i) {
Damien George999cedb2015-11-27 17:01:44 +0000593 save_raw_code(print, (mp_raw_code_t*)(uintptr_t)*const_table++);
Damien Georged8c834c2015-11-02 21:55:42 +0000594 }
595}
596
597void mp_raw_code_save(mp_raw_code_t *rc, mp_print_t *print) {
Damien George39a8deb2015-11-23 10:58:16 +0000598 // header contains:
599 // byte 'M'
600 // byte version
Damien George1404d622016-01-08 13:35:35 +0000601 // byte feature flags
602 // byte number of bits in a small int
Damien Georgeea235202016-02-11 22:30:53 +0000603 byte header[4] = {'M', 0, MPY_FEATURE_FLAGS_DYNAMIC,
604 #if MICROPY_DYNAMIC_COMPILER
605 mp_dynamic_compiler.small_int_bits,
606 #else
607 mp_small_int_bits(),
608 #endif
609 };
Damien George1404d622016-01-08 13:35:35 +0000610 mp_print_bytes(print, header, sizeof(header));
Damien George39a8deb2015-11-23 10:58:16 +0000611
Damien Georged8c834c2015-11-02 21:55:42 +0000612 save_raw_code(print, rc);
613}
614
615// here we define mp_raw_code_save_file depending on the port
616// TODO abstract this away properly
617
Markus Fix4f008032016-03-07 12:15:01 +0100618#if defined(__i386__) || defined(__x86_64__) || (defined(__arm__) && (defined(__unix__)))
Damien Georged8c834c2015-11-02 21:55:42 +0000619
620#include <unistd.h>
621#include <sys/stat.h>
622#include <fcntl.h>
623
Damien George4e7107a2015-11-27 12:19:25 +0000624STATIC void fd_print_strn(void *env, const char *str, size_t len) {
625 int fd = (intptr_t)env;
Damien Georged8c834c2015-11-02 21:55:42 +0000626 ssize_t ret = write(fd, str, len);
627 (void)ret;
628}
629
630void mp_raw_code_save_file(mp_raw_code_t *rc, const char *filename) {
631 int fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0644);
Damien George999cedb2015-11-27 17:01:44 +0000632 mp_print_t fd_print = {(void*)(intptr_t)fd, fd_print_strn};
Damien Georged8c834c2015-11-02 21:55:42 +0000633 mp_raw_code_save(rc, &fd_print);
634 close(fd);
635}
636
637#else
638#error mp_raw_code_save_file not implemented for this platform
639#endif
640
641#endif // MICROPY_PERSISTENT_CODE_SAVE