blob: 26fe3dec91d4a92ebd235872f7e6a41be24d9343 [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 George32444b72015-01-24 23:14:12 +000052struct _mp_raw_code_t {
53 mp_raw_code_kind_t kind : 3;
54 mp_uint_t scope_flags : 7;
55 mp_uint_t n_pos_args : 11;
Damien George32444b72015-01-24 23:14:12 +000056 union {
57 struct {
Damien Georged8c834c2015-11-02 21:55:42 +000058 const byte *bytecode;
Damien George713ea182015-10-23 01:23:11 +010059 const mp_uint_t *const_table;
Damien Georged8c834c2015-11-02 21:55:42 +000060 #if MICROPY_PERSISTENT_CODE_SAVE
61 mp_uint_t bc_len;
62 uint16_t n_obj;
63 uint16_t n_raw_code;
64 #endif
Damien George32444b72015-01-24 23:14:12 +000065 } u_byte;
66 struct {
67 void *fun_data;
Damien George713ea182015-10-23 01:23:11 +010068 const mp_uint_t *const_table;
Damien George32444b72015-01-24 23:14:12 +000069 mp_uint_t type_sig; // for viper, compressed as 2-bit types; ret is MSB, then arg0, arg1, etc
70 } u_native;
71 } data;
72};
73
Damien Georgedf8127a2014-04-13 11:04:33 +010074mp_raw_code_t *mp_emit_glue_new_raw_code(void) {
75 mp_raw_code_t *rc = m_new0(mp_raw_code_t, 1);
76 rc->kind = MP_CODE_RESERVED;
77 return rc;
Damien George2326d522014-03-27 23:26:35 +000078}
79
Damien Georged8c834c2015-11-02 21:55:42 +000080void mp_emit_glue_assign_bytecode(mp_raw_code_t *rc, const byte *code, mp_uint_t len,
81 const mp_uint_t *const_table,
82 #if MICROPY_PERSISTENT_CODE_SAVE
83 uint16_t n_obj, uint16_t n_raw_code,
84 #endif
85 mp_uint_t scope_flags) {
86
Damien Georgeccc85ea2014-05-10 13:40:46 +010087 rc->kind = MP_CODE_BYTECODE;
Damien Georgedf8127a2014-04-13 11:04:33 +010088 rc->scope_flags = scope_flags;
Damien Georged8c834c2015-11-02 21:55:42 +000089 rc->data.u_byte.bytecode = code;
Damien George713ea182015-10-23 01:23:11 +010090 rc->data.u_byte.const_table = const_table;
Damien Georged8c834c2015-11-02 21:55:42 +000091 #if MICROPY_PERSISTENT_CODE_SAVE
92 rc->data.u_byte.bc_len = len;
93 rc->data.u_byte.n_obj = n_obj;
94 rc->data.u_byte.n_raw_code = n_raw_code;
95 #endif
Damien George2326d522014-03-27 23:26:35 +000096
97#ifdef DEBUG_PRINT
Damien George3a3db4d2015-10-22 23:45:37 +010098 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 +000099#endif
Paul Sokolovsky6b344d72014-05-05 00:50:05 +0300100#if MICROPY_DEBUG_PRINTERS
Paul Sokolovsky429e3f02014-10-26 23:20:22 +0200101 if (mp_verbose_flag >= 2) {
Damien George713ea182015-10-23 01:23:11 +0100102 mp_bytecode_print(rc, code, len, const_table);
Paul Sokolovsky6b344d72014-05-05 00:50:05 +0300103 }
Damien George2326d522014-03-27 23:26:35 +0000104#endif
105}
106
Damien George2ac4af62014-08-15 16:45:41 +0100107#if MICROPY_EMIT_NATIVE || MICROPY_EMIT_INLINE_THUMB
Damien George713ea182015-10-23 01:23:11 +0100108void 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 +0100109 assert(kind == MP_CODE_NATIVE_PY || kind == MP_CODE_NATIVE_VIPER || kind == MP_CODE_NATIVE_ASM);
110 rc->kind = kind;
Damien George99886182015-04-06 22:38:53 +0100111 rc->scope_flags = scope_flags;
112 rc->n_pos_args = n_pos_args;
Damien George32444b72015-01-24 23:14:12 +0000113 rc->data.u_native.fun_data = fun_data;
Damien George713ea182015-10-23 01:23:11 +0100114 rc->data.u_native.const_table = const_table;
Damien George32444b72015-01-24 23:14:12 +0000115 rc->data.u_native.type_sig = type_sig;
Damien George2326d522014-03-27 23:26:35 +0000116
117#ifdef DEBUG_PRINT
Damien George3a3db4d2015-10-22 23:45:37 +0100118 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 +0100119 for (mp_uint_t i = 0; i < fun_len; i++) {
Damien George2326d522014-03-27 23:26:35 +0000120 if (i > 0 && i % 16 == 0) {
121 DEBUG_printf("\n");
122 }
Damien George3c658a42014-08-24 16:28:17 +0100123 DEBUG_printf(" %02x", ((byte*)fun_data)[i]);
Damien George2326d522014-03-27 23:26:35 +0000124 }
125 DEBUG_printf("\n");
126
127#ifdef WRITE_CODE
Damien George915197a2014-05-12 23:11:14 +0100128 FILE *fp_write_code = fopen("out-code", "wb");
Damien Georgeb427d6a2014-08-26 23:35:57 +0100129 fwrite(fun_data, fun_len, 1, fp_write_code);
Damien George915197a2014-05-12 23:11:14 +0100130 fclose(fp_write_code);
Damien George2326d522014-03-27 23:26:35 +0000131#endif
Damien Georgeff8dd3f2015-01-20 12:47:20 +0000132#else
133 (void)fun_len;
Damien George2326d522014-03-27 23:26:35 +0000134#endif
135}
Damien George2ac4af62014-08-15 16:45:41 +0100136#endif
Damien George2326d522014-03-27 23:26:35 +0000137
Damien Georgedf8127a2014-04-13 11:04:33 +0100138mp_obj_t mp_make_function_from_raw_code(mp_raw_code_t *rc, mp_obj_t def_args, mp_obj_t def_kw_args) {
139 DEBUG_OP_printf("make_function_from_raw_code %p\n", rc);
140 assert(rc != NULL);
Damien George2326d522014-03-27 23:26:35 +0000141
Damien George69b89d22014-04-11 13:38:30 +0000142 // def_args must be MP_OBJ_NULL or a tuple
143 assert(def_args == MP_OBJ_NULL || MP_OBJ_IS_TYPE(def_args, &mp_type_tuple));
144
Damien Georgef0778a72014-06-07 22:01:00 +0100145 // def_kw_args must be MP_OBJ_NULL or a dict
146 assert(def_kw_args == MP_OBJ_NULL || MP_OBJ_IS_TYPE(def_kw_args, &mp_type_dict));
Damien Georgee337f1e2014-03-31 15:18:37 +0100147
Damien Georgedf8127a2014-04-13 11:04:33 +0100148 // make the function, depending on the raw code kind
Damien George2326d522014-03-27 23:26:35 +0000149 mp_obj_t fun;
Damien Georgedf8127a2014-04-13 11:04:33 +0100150 switch (rc->kind) {
Damien Georgeccc85ea2014-05-10 13:40:46 +0100151 case MP_CODE_BYTECODE:
Damien Georged2d64f02015-01-14 21:32:42 +0000152 no_other_choice:
Damien Georged8c834c2015-11-02 21:55:42 +0000153 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 +0000154 break;
Damien George2ac4af62014-08-15 16:45:41 +0100155 #if MICROPY_EMIT_NATIVE
Damien Georgeccc85ea2014-05-10 13:40:46 +0100156 case MP_CODE_NATIVE_PY:
Damien George713ea182015-10-23 01:23:11 +0100157 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 +0000158 break;
Damien Georgeccc85ea2014-05-10 13:40:46 +0100159 case MP_CODE_NATIVE_VIPER:
Damien George32444b72015-01-24 23:14:12 +0000160 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 +0100161 break;
162 #endif
163 #if MICROPY_EMIT_INLINE_THUMB
Damien Georgeccc85ea2014-05-10 13:40:46 +0100164 case MP_CODE_NATIVE_ASM:
Damien George32444b72015-01-24 23:14:12 +0000165 fun = mp_obj_new_fun_asm(rc->n_pos_args, rc->data.u_native.fun_data);
Damien George2326d522014-03-27 23:26:35 +0000166 break;
Damien George2ac4af62014-08-15 16:45:41 +0100167 #endif
Damien George2326d522014-03-27 23:26:35 +0000168 default:
Damien Georgedf8127a2014-04-13 11:04:33 +0100169 // raw code was never set (this should not happen)
Damien George2326d522014-03-27 23:26:35 +0000170 assert(0);
Damien Georged2d64f02015-01-14 21:32:42 +0000171 goto no_other_choice; // to help flow control analysis
Damien George2326d522014-03-27 23:26:35 +0000172 }
173
174 // check for generator functions and if so wrap in generator object
Damien Georgedf8127a2014-04-13 11:04:33 +0100175 if ((rc->scope_flags & MP_SCOPE_FLAG_GENERATOR) != 0) {
Damien George2326d522014-03-27 23:26:35 +0000176 fun = mp_obj_new_gen_wrap(fun);
177 }
178
179 return fun;
180}
181
Damien George3c658a42014-08-24 16:28:17 +0100182mp_obj_t mp_make_closure_from_raw_code(mp_raw_code_t *rc, mp_uint_t n_closed_over, const mp_obj_t *args) {
183 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 +0000184 // make function object
Damien George3558f622014-04-20 17:50:40 +0100185 mp_obj_t ffun;
186 if (n_closed_over & 0x100) {
187 // default positional and keyword args given
188 ffun = mp_make_function_from_raw_code(rc, args[0], args[1]);
189 } else {
190 // default positional and keyword args not given
191 ffun = mp_make_function_from_raw_code(rc, MP_OBJ_NULL, MP_OBJ_NULL);
192 }
Damien George2326d522014-03-27 23:26:35 +0000193 // wrap function in closure object
Damien George3558f622014-04-20 17:50:40 +0100194 return mp_obj_new_closure(ffun, n_closed_over & 0xff, args + ((n_closed_over >> 7) & 2));
Damien George2326d522014-03-27 23:26:35 +0000195}
Damien Georged8c834c2015-11-02 21:55:42 +0000196
197#if MICROPY_PERSISTENT_CODE
198
199typedef struct _bytecode_prelude_t {
200 uint n_state;
201 uint n_exc_stack;
202 uint scope_flags;
203 uint n_pos_args;
204 uint n_kwonly_args;
205 uint n_def_pos_args;
206 uint code_info_size;
207} bytecode_prelude_t;
208
209// ip will point to start of opcodes
210// ip2 will point to simple_name, source_file qstrs
211STATIC void extract_prelude(const byte **ip, const byte **ip2, bytecode_prelude_t *prelude) {
212 prelude->n_state = mp_decode_uint(ip);
213 prelude->n_exc_stack = mp_decode_uint(ip);
214 prelude->scope_flags = *(*ip)++;
215 prelude->n_pos_args = *(*ip)++;
216 prelude->n_kwonly_args = *(*ip)++;
217 prelude->n_def_pos_args = *(*ip)++;
218 *ip2 = *ip;
219 prelude->code_info_size = mp_decode_uint(ip2);
220 *ip += prelude->code_info_size;
221 while (*(*ip)++ != 255) {
222 }
223}
224
225#endif // MICROPY_PERSISTENT_CODE
226
227#if MICROPY_PERSISTENT_CODE_LOAD
228
Damien George44e6e342015-11-23 11:54:12 +0000229#include "py/parsenum.h"
Damien Georged8c834c2015-11-02 21:55:42 +0000230#include "py/bc0.h"
231
Damien George44e6e342015-11-23 11:54:12 +0000232STATIC int read_byte(mp_reader_t *reader) {
233 return reader->read_byte(reader->data);
234}
235
Damien Georged8c834c2015-11-02 21:55:42 +0000236STATIC void read_bytes(mp_reader_t *reader, byte *buf, size_t len) {
237 while (len-- > 0) {
238 *buf++ = reader->read_byte(reader->data);
239 }
240}
241
242STATIC mp_uint_t read_uint(mp_reader_t *reader) {
243 mp_uint_t unum = 0;
244 for (;;) {
245 byte b = reader->read_byte(reader->data);
246 unum = (unum << 7) | (b & 0x7f);
247 if ((b & 0x80) == 0) {
248 break;
249 }
250 }
251 return unum;
252}
253
254STATIC qstr load_qstr(mp_reader_t *reader) {
255 mp_uint_t len = read_uint(reader);
256 char *str = m_new(char, len);
257 read_bytes(reader, (byte*)str, len);
258 qstr qst = qstr_from_strn(str, len);
259 m_del(char, str, len);
260 return qst;
261}
262
263STATIC mp_obj_t load_obj(mp_reader_t *reader) {
Damien George44e6e342015-11-23 11:54:12 +0000264 byte obj_type = read_byte(reader);
265 if (obj_type == 'e') {
266 return (mp_obj_t)&mp_const_ellipsis_obj;
267 } else {
268 size_t len = read_uint(reader);
269 vstr_t vstr;
270 vstr_init_len(&vstr, len);
271 read_bytes(reader, (byte*)vstr.buf, len);
272 if (obj_type == 's' || obj_type == 'b') {
273 return mp_obj_new_str_from_vstr(obj_type == 's' ? &mp_type_str : &mp_type_bytes, &vstr);
274 } else if (obj_type == 'i') {
275 return mp_parse_num_integer(vstr.buf, vstr.len, 10, NULL);
276 } else {
277 assert(obj_type == 'f' || obj_type == 'c');
278 return mp_parse_num_decimal(vstr.buf, vstr.len, obj_type == 'c', false, NULL);
279 }
280 }
Damien Georged8c834c2015-11-02 21:55:42 +0000281}
282
283STATIC void load_bytecode_qstrs(mp_reader_t *reader, byte *ip, byte *ip_top) {
284 while (ip < ip_top) {
285 size_t sz;
286 uint f = mp_opcode_format(ip, &sz);
287 if (f == MP_OPCODE_QSTR) {
288 qstr qst = load_qstr(reader);
289 ip[1] = qst;
290 ip[2] = qst >> 8;
291 }
292 ip += sz;
293 }
294}
295
296STATIC mp_raw_code_t *load_raw_code(mp_reader_t *reader) {
297 // load bytecode
298 mp_uint_t bc_len = read_uint(reader);
299 byte *bytecode = m_new(byte, bc_len);
300 read_bytes(reader, bytecode, bc_len);
301
302 // extract prelude
303 const byte *ip = bytecode;
304 const byte *ip2;
305 bytecode_prelude_t prelude;
306 extract_prelude(&ip, &ip2, &prelude);
307
308 // load qstrs and link global qstr ids into bytecode
309 qstr simple_name = load_qstr(reader);
310 qstr source_file = load_qstr(reader);
311 ((byte*)ip2)[0] = simple_name; ((byte*)ip2)[1] = simple_name >> 8;
312 ((byte*)ip2)[2] = source_file; ((byte*)ip2)[3] = source_file >> 8;
313 load_bytecode_qstrs(reader, (byte*)ip, bytecode + bc_len);
314
315 // load constant table
316 mp_uint_t n_obj = read_uint(reader);
317 mp_uint_t n_raw_code = read_uint(reader);
318 mp_uint_t *const_table = m_new(mp_uint_t, prelude.n_pos_args + prelude.n_kwonly_args + n_obj + n_raw_code);
319 mp_uint_t *ct = const_table;
320 for (mp_uint_t i = 0; i < prelude.n_pos_args + prelude.n_kwonly_args; ++i) {
321 *ct++ = (mp_uint_t)MP_OBJ_NEW_QSTR(load_qstr(reader));
322 }
323 for (mp_uint_t i = 0; i < n_obj; ++i) {
324 *ct++ = (mp_uint_t)load_obj(reader);
325 }
326 for (mp_uint_t i = 0; i < n_raw_code; ++i) {
327 *ct++ = (mp_uint_t)load_raw_code(reader);
328 }
329
330 // create raw_code and return it
331 mp_raw_code_t *rc = mp_emit_glue_new_raw_code();
332 mp_emit_glue_assign_bytecode(rc, bytecode, bc_len, const_table,
333 #if MICROPY_PERSISTENT_CODE_SAVE
334 n_obj, n_raw_code,
335 #endif
336 prelude.scope_flags);
337 return rc;
338}
339
340mp_raw_code_t *mp_raw_code_load(mp_reader_t *reader) {
Damien George39a8deb2015-11-23 10:58:16 +0000341 byte header[3];
342 read_bytes(reader, header, 3);
Damien Georged8c834c2015-11-02 21:55:42 +0000343 if (strncmp((char*)header, "M\x00", 2) != 0) {
344 nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError,
345 "invalid .mpy file"));
346 }
Damien George39a8deb2015-11-23 10:58:16 +0000347 if (header[2] != MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE) {
348 nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError,
349 "incompatible .mpy file"));
350 }
Damien Georged8c834c2015-11-02 21:55:42 +0000351 return load_raw_code(reader);
352}
353
Damien Georgeb5b1f2c2015-11-20 12:44:20 +0000354typedef struct _mp_mem_reader_t {
355 const byte *cur;
356 const byte *end;
357} mp_mem_reader_t;
358
359STATIC mp_uint_t mp_mem_reader_next_byte(void *br_in) {
360 mp_mem_reader_t *br = br_in;
361 if (br->cur < br->end) {
362 return *br->cur++;
363 } else {
364 return (mp_uint_t)-1;
365 }
366}
367
368mp_raw_code_t *mp_raw_code_load_mem(const byte *buf, size_t len) {
369 mp_mem_reader_t mr = {buf, buf + len};
370 mp_reader_t reader = {&mr, mp_mem_reader_next_byte};
371 return mp_raw_code_load(&reader);
372}
373
Damien Georged8c834c2015-11-02 21:55:42 +0000374// here we define mp_raw_code_load_file depending on the port
375// TODO abstract this away properly
376
377#if defined(__i386__) || defined(__x86_64__)
378// unix file reader
379
380#include <sys/stat.h>
381#include <fcntl.h>
382
383typedef struct _mp_lexer_file_buf_t {
384 int fd;
385 byte buf[20];
386 mp_uint_t len;
387 mp_uint_t pos;
388} mp_lexer_file_buf_t;
389
390STATIC mp_uint_t file_buf_next_byte(void *fb_in) {
391 mp_lexer_file_buf_t *fb = fb_in;
392 if (fb->pos >= fb->len) {
393 if (fb->len == 0) {
394 return (mp_uint_t)-1;
395 } else {
396 int n = read(fb->fd, fb->buf, sizeof(fb->buf));
397 if (n <= 0) {
398 fb->len = 0;
399 return (mp_uint_t)-1;
400 }
401 fb->len = n;
402 fb->pos = 0;
403 }
404 }
405 return fb->buf[fb->pos++];
406}
407
408mp_raw_code_t *mp_raw_code_load_file(const char *filename) {
409 mp_lexer_file_buf_t fb;
410 fb.fd = open(filename, O_RDONLY, 0644);
411 int n = read(fb.fd, fb.buf, sizeof(fb.buf));
412 fb.len = n;
413 fb.pos = 0;
414 mp_reader_t reader;
415 reader.data = &fb;
416 reader.read_byte = file_buf_next_byte;
417 mp_raw_code_t *rc = mp_raw_code_load(&reader);
418 close(fb.fd);
419 return rc;
420}
421
Damien Georgef1487272015-11-20 12:42:26 +0000422#elif defined(__thumb2__)
423// fatfs file reader (assume thumb2 arch uses fatfs...)
Damien Georged8c834c2015-11-02 21:55:42 +0000424
425#include "lib/fatfs/ff.h"
426
427typedef struct _mp_lexer_file_buf_t {
428 FIL fp;
429 byte buf[20];
430 uint16_t len;
431 uint16_t pos;
432} mp_lexer_file_buf_t;
433
434STATIC mp_uint_t file_buf_next_byte(void *fb_in) {
435 mp_lexer_file_buf_t *fb = fb_in;
436 if (fb->pos >= fb->len) {
437 if (fb->len < sizeof(fb->buf)) {
438 return (mp_uint_t)-1;
439 } else {
440 UINT n;
441 f_read(&fb->fp, fb->buf, sizeof(fb->buf), &n);
442 if (n == 0) {
443 return (mp_uint_t)-1;
444 }
445 fb->len = n;
446 fb->pos = 0;
447 }
448 }
449 return fb->buf[fb->pos++];
450}
451
452mp_raw_code_t *mp_raw_code_load_file(const char *filename) {
453 mp_lexer_file_buf_t fb;
454 /*FRESULT res =*/ f_open(&fb.fp, filename, FA_READ);
455 UINT n;
456 f_read(&fb.fp, fb.buf, sizeof(fb.buf), &n);
457 fb.len = n;
458 fb.pos = 0;
459
460 mp_reader_t reader;
461 reader.data = &fb;
462 reader.read_byte = file_buf_next_byte;
463 mp_raw_code_t *rc = mp_raw_code_load(&reader);
464
465 f_close(&fb.fp);
466
467 return rc;
468}
469
470#endif
471
472#endif // MICROPY_PERSISTENT_CODE_LOAD
473
474#if MICROPY_PERSISTENT_CODE_SAVE
Damien George44e6e342015-11-23 11:54:12 +0000475
476#include "py/objstr.h"
477
Damien Georged8c834c2015-11-02 21:55:42 +0000478STATIC void mp_print_bytes(mp_print_t *print, const byte *data, size_t len) {
479 print->print_strn(print->data, (const char*)data, len);
480}
481
482#define BYTES_FOR_INT ((BYTES_PER_WORD * 8 + 6) / 7)
483STATIC void mp_print_uint(mp_print_t *print, mp_uint_t n) {
484 byte buf[BYTES_FOR_INT];
485 byte *p = buf + sizeof(buf);
486 *--p = n & 0x7f;
487 n >>= 7;
488 for (; n != 0; n >>= 7) {
489 *--p = 0x80 | (n & 0x7f);
490 }
491 print->print_strn(print->data, (char*)p, buf + sizeof(buf) - p);
492}
493
494STATIC void save_qstr(mp_print_t *print, qstr qst) {
Damien Georgec3f64d92015-11-27 12:23:18 +0000495 size_t len;
Damien Georged8c834c2015-11-02 21:55:42 +0000496 const byte *str = qstr_data(qst, &len);
497 mp_print_uint(print, len);
498 mp_print_bytes(print, str, len);
499}
500
501STATIC void save_obj(mp_print_t *print, mp_obj_t o) {
Damien George44e6e342015-11-23 11:54:12 +0000502 if (MP_OBJ_IS_STR_OR_BYTES(o)) {
503 byte obj_type;
504 if (MP_OBJ_IS_STR(o)) {
505 obj_type = 's';
506 } else {
507 obj_type = 'b';
508 }
Damien Georged8c834c2015-11-02 21:55:42 +0000509 mp_uint_t len;
510 const char *str = mp_obj_str_get_data(o, &len);
Damien George44e6e342015-11-23 11:54:12 +0000511 mp_print_bytes(print, &obj_type, 1);
Damien Georged8c834c2015-11-02 21:55:42 +0000512 mp_print_uint(print, len);
513 mp_print_bytes(print, (const byte*)str, len);
Damien Georged8c834c2015-11-02 21:55:42 +0000514 } else if (o == &mp_const_ellipsis_obj) {
Damien George44e6e342015-11-23 11:54:12 +0000515 byte obj_type = 'e';
516 mp_print_bytes(print, &obj_type, 1);
Damien Georged8c834c2015-11-02 21:55:42 +0000517 } else {
Damien George44e6e342015-11-23 11:54:12 +0000518 // we save numbers using a simplistic text representation
519 // TODO could be improved
520 byte obj_type;
521 if (MP_OBJ_IS_TYPE(o, &mp_type_int)) {
522 obj_type = 'i';
523 } else if (MP_OBJ_IS_TYPE(o, &mp_type_float)) {
524 obj_type = 'f';
525 } else {
526 assert(MP_OBJ_IS_TYPE(o, &mp_type_complex));
527 obj_type = 'c';
528 }
529 vstr_t vstr;
530 mp_print_t pr;
531 vstr_init_print(&vstr, 10, &pr);
532 mp_obj_print_helper(&pr, o, PRINT_REPR);
533 mp_print_bytes(print, &obj_type, 1);
534 mp_print_uint(print, vstr.len);
535 mp_print_bytes(print, (const byte*)vstr.buf, vstr.len);
536 vstr_clear(&vstr);
Damien Georged8c834c2015-11-02 21:55:42 +0000537 }
538}
539
540STATIC void save_bytecode_qstrs(mp_print_t *print, const byte *ip, const byte *ip_top) {
541 while (ip < ip_top) {
542 size_t sz;
543 uint f = mp_opcode_format(ip, &sz);
544 if (f == MP_OPCODE_QSTR) {
545 qstr qst = ip[1] | (ip[2] << 8);
546 save_qstr(print, qst);
547 }
548 ip += sz;
549 }
550}
551
552STATIC void save_raw_code(mp_print_t *print, mp_raw_code_t *rc) {
553 if (rc->kind != MP_CODE_BYTECODE) {
554 nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError,
555 "can only save bytecode"));
556 }
557
558 // save bytecode
559 mp_print_uint(print, rc->data.u_byte.bc_len);
560 mp_print_bytes(print, rc->data.u_byte.bytecode, rc->data.u_byte.bc_len);
561
562 // extract prelude
563 const byte *ip = rc->data.u_byte.bytecode;
564 const byte *ip2;
565 bytecode_prelude_t prelude;
566 extract_prelude(&ip, &ip2, &prelude);
567
568 // save qstrs
569 save_qstr(print, ip2[0] | (ip2[1] << 8)); // simple_name
570 save_qstr(print, ip2[2] | (ip2[3] << 8)); // source_file
571 save_bytecode_qstrs(print, ip, rc->data.u_byte.bytecode + rc->data.u_byte.bc_len);
572
573 // save constant table
574 mp_print_uint(print, rc->data.u_byte.n_obj);
575 mp_print_uint(print, rc->data.u_byte.n_raw_code);
576 const mp_uint_t *const_table = rc->data.u_byte.const_table;
577 for (uint i = 0; i < prelude.n_pos_args + prelude.n_kwonly_args; ++i) {
578 mp_obj_t o = (mp_obj_t)*const_table++;
579 save_qstr(print, MP_OBJ_QSTR_VALUE(o));
580 }
581 for (uint i = 0; i < rc->data.u_byte.n_obj; ++i) {
582 save_obj(print, (mp_obj_t)*const_table++);
583 }
584 for (uint i = 0; i < rc->data.u_byte.n_raw_code; ++i) {
585 save_raw_code(print, (mp_raw_code_t*)*const_table++);
586 }
587}
588
589void mp_raw_code_save(mp_raw_code_t *rc, mp_print_t *print) {
Damien George39a8deb2015-11-23 10:58:16 +0000590 // header contains:
591 // byte 'M'
592 // byte version
593 // byte feature flags (right now just OPT_CACHE_MAP_LOOKUP_IN_BYTECODE)
594 byte header[3] = {'M', 0, MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE};
595 mp_print_bytes(print, header, 3);
596
Damien Georged8c834c2015-11-02 21:55:42 +0000597 save_raw_code(print, rc);
598}
599
600// here we define mp_raw_code_save_file depending on the port
601// TODO abstract this away properly
602
603#if defined(__i386__) || defined(__x86_64__)
604
605#include <unistd.h>
606#include <sys/stat.h>
607#include <fcntl.h>
608
Damien George4e7107a2015-11-27 12:19:25 +0000609STATIC void fd_print_strn(void *env, const char *str, size_t len) {
610 int fd = (intptr_t)env;
Damien Georged8c834c2015-11-02 21:55:42 +0000611 ssize_t ret = write(fd, str, len);
612 (void)ret;
613}
614
615void mp_raw_code_save_file(mp_raw_code_t *rc, const char *filename) {
616 int fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0644);
617 mp_print_t fd_print = {(void*)(mp_int_t)fd, fd_print_strn};
618 mp_raw_code_save(rc, &fd_print);
619 close(fd);
620}
621
622#else
623#error mp_raw_code_save_file not implemented for this platform
624#endif
625
626#endif // MICROPY_PERSISTENT_CODE_SAVE