blob: 4731af992d2d46b9752afab2a2593a52af452b3e [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
Damien George32444b72015-01-24 23:14:12 +000048struct _mp_raw_code_t {
49 mp_raw_code_kind_t kind : 3;
50 mp_uint_t scope_flags : 7;
51 mp_uint_t n_pos_args : 11;
Damien George32444b72015-01-24 23:14:12 +000052 union {
53 struct {
Damien Georged8c834c2015-11-02 21:55:42 +000054 const byte *bytecode;
Damien George713ea182015-10-23 01:23:11 +010055 const mp_uint_t *const_table;
Damien Georged8c834c2015-11-02 21:55:42 +000056 #if MICROPY_PERSISTENT_CODE_SAVE
57 mp_uint_t bc_len;
58 uint16_t n_obj;
59 uint16_t n_raw_code;
60 #endif
Damien George32444b72015-01-24 23:14:12 +000061 } u_byte;
62 struct {
63 void *fun_data;
Damien George713ea182015-10-23 01:23:11 +010064 const mp_uint_t *const_table;
Damien George32444b72015-01-24 23:14:12 +000065 mp_uint_t type_sig; // for viper, compressed as 2-bit types; ret is MSB, then arg0, arg1, etc
66 } u_native;
67 } data;
68};
69
Damien Georgedf8127a2014-04-13 11:04:33 +010070mp_raw_code_t *mp_emit_glue_new_raw_code(void) {
71 mp_raw_code_t *rc = m_new0(mp_raw_code_t, 1);
72 rc->kind = MP_CODE_RESERVED;
73 return rc;
Damien George2326d522014-03-27 23:26:35 +000074}
75
Damien Georged8c834c2015-11-02 21:55:42 +000076void mp_emit_glue_assign_bytecode(mp_raw_code_t *rc, const byte *code, mp_uint_t len,
77 const mp_uint_t *const_table,
78 #if MICROPY_PERSISTENT_CODE_SAVE
79 uint16_t n_obj, uint16_t n_raw_code,
80 #endif
81 mp_uint_t scope_flags) {
82
Damien Georgeccc85ea2014-05-10 13:40:46 +010083 rc->kind = MP_CODE_BYTECODE;
Damien Georgedf8127a2014-04-13 11:04:33 +010084 rc->scope_flags = scope_flags;
Damien Georged8c834c2015-11-02 21:55:42 +000085 rc->data.u_byte.bytecode = code;
Damien George713ea182015-10-23 01:23:11 +010086 rc->data.u_byte.const_table = const_table;
Damien Georged8c834c2015-11-02 21:55:42 +000087 #if MICROPY_PERSISTENT_CODE_SAVE
88 rc->data.u_byte.bc_len = len;
89 rc->data.u_byte.n_obj = n_obj;
90 rc->data.u_byte.n_raw_code = n_raw_code;
91 #endif
Damien George2326d522014-03-27 23:26:35 +000092
93#ifdef DEBUG_PRINT
Damien George3a3db4d2015-10-22 23:45:37 +010094 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 +000095#endif
Paul Sokolovsky6b344d72014-05-05 00:50:05 +030096#if MICROPY_DEBUG_PRINTERS
Paul Sokolovsky429e3f02014-10-26 23:20:22 +020097 if (mp_verbose_flag >= 2) {
Damien George713ea182015-10-23 01:23:11 +010098 mp_bytecode_print(rc, code, len, const_table);
Paul Sokolovsky6b344d72014-05-05 00:50:05 +030099 }
Damien George2326d522014-03-27 23:26:35 +0000100#endif
101}
102
Damien George2ac4af62014-08-15 16:45:41 +0100103#if MICROPY_EMIT_NATIVE || MICROPY_EMIT_INLINE_THUMB
Damien George713ea182015-10-23 01:23:11 +0100104void 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 +0100105 assert(kind == MP_CODE_NATIVE_PY || kind == MP_CODE_NATIVE_VIPER || kind == MP_CODE_NATIVE_ASM);
106 rc->kind = kind;
Damien George99886182015-04-06 22:38:53 +0100107 rc->scope_flags = scope_flags;
108 rc->n_pos_args = n_pos_args;
Damien George32444b72015-01-24 23:14:12 +0000109 rc->data.u_native.fun_data = fun_data;
Damien George713ea182015-10-23 01:23:11 +0100110 rc->data.u_native.const_table = const_table;
Damien George32444b72015-01-24 23:14:12 +0000111 rc->data.u_native.type_sig = type_sig;
Damien George2326d522014-03-27 23:26:35 +0000112
113#ifdef DEBUG_PRINT
Damien George3a3db4d2015-10-22 23:45:37 +0100114 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 +0100115 for (mp_uint_t i = 0; i < fun_len; i++) {
Damien George2326d522014-03-27 23:26:35 +0000116 if (i > 0 && i % 16 == 0) {
117 DEBUG_printf("\n");
118 }
Damien George3c658a42014-08-24 16:28:17 +0100119 DEBUG_printf(" %02x", ((byte*)fun_data)[i]);
Damien George2326d522014-03-27 23:26:35 +0000120 }
121 DEBUG_printf("\n");
122
123#ifdef WRITE_CODE
Damien George915197a2014-05-12 23:11:14 +0100124 FILE *fp_write_code = fopen("out-code", "wb");
Damien Georgeb427d6a2014-08-26 23:35:57 +0100125 fwrite(fun_data, fun_len, 1, fp_write_code);
Damien George915197a2014-05-12 23:11:14 +0100126 fclose(fp_write_code);
Damien George2326d522014-03-27 23:26:35 +0000127#endif
Damien Georgeff8dd3f2015-01-20 12:47:20 +0000128#else
129 (void)fun_len;
Damien George2326d522014-03-27 23:26:35 +0000130#endif
131}
Damien George2ac4af62014-08-15 16:45:41 +0100132#endif
Damien George2326d522014-03-27 23:26:35 +0000133
Damien Georgedf8127a2014-04-13 11:04:33 +0100134mp_obj_t mp_make_function_from_raw_code(mp_raw_code_t *rc, mp_obj_t def_args, mp_obj_t def_kw_args) {
135 DEBUG_OP_printf("make_function_from_raw_code %p\n", rc);
136 assert(rc != NULL);
Damien George2326d522014-03-27 23:26:35 +0000137
Damien George69b89d22014-04-11 13:38:30 +0000138 // def_args must be MP_OBJ_NULL or a tuple
139 assert(def_args == MP_OBJ_NULL || MP_OBJ_IS_TYPE(def_args, &mp_type_tuple));
140
Damien Georgef0778a72014-06-07 22:01:00 +0100141 // def_kw_args must be MP_OBJ_NULL or a dict
142 assert(def_kw_args == MP_OBJ_NULL || MP_OBJ_IS_TYPE(def_kw_args, &mp_type_dict));
Damien Georgee337f1e2014-03-31 15:18:37 +0100143
Damien Georgedf8127a2014-04-13 11:04:33 +0100144 // make the function, depending on the raw code kind
Damien George2326d522014-03-27 23:26:35 +0000145 mp_obj_t fun;
Damien Georgedf8127a2014-04-13 11:04:33 +0100146 switch (rc->kind) {
Damien Georgeccc85ea2014-05-10 13:40:46 +0100147 case MP_CODE_BYTECODE:
Damien Georged2d64f02015-01-14 21:32:42 +0000148 no_other_choice:
Damien Georged8c834c2015-11-02 21:55:42 +0000149 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 +0000150 break;
Damien George2ac4af62014-08-15 16:45:41 +0100151 #if MICROPY_EMIT_NATIVE
Damien Georgeccc85ea2014-05-10 13:40:46 +0100152 case MP_CODE_NATIVE_PY:
Damien George713ea182015-10-23 01:23:11 +0100153 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 +0000154 break;
Damien Georgeccc85ea2014-05-10 13:40:46 +0100155 case MP_CODE_NATIVE_VIPER:
Damien George32444b72015-01-24 23:14:12 +0000156 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 +0100157 break;
158 #endif
159 #if MICROPY_EMIT_INLINE_THUMB
Damien Georgeccc85ea2014-05-10 13:40:46 +0100160 case MP_CODE_NATIVE_ASM:
Damien George32444b72015-01-24 23:14:12 +0000161 fun = mp_obj_new_fun_asm(rc->n_pos_args, rc->data.u_native.fun_data);
Damien George2326d522014-03-27 23:26:35 +0000162 break;
Damien George2ac4af62014-08-15 16:45:41 +0100163 #endif
Damien George2326d522014-03-27 23:26:35 +0000164 default:
Damien Georgedf8127a2014-04-13 11:04:33 +0100165 // raw code was never set (this should not happen)
Damien George2326d522014-03-27 23:26:35 +0000166 assert(0);
Damien Georged2d64f02015-01-14 21:32:42 +0000167 goto no_other_choice; // to help flow control analysis
Damien George2326d522014-03-27 23:26:35 +0000168 }
169
170 // check for generator functions and if so wrap in generator object
Damien Georgedf8127a2014-04-13 11:04:33 +0100171 if ((rc->scope_flags & MP_SCOPE_FLAG_GENERATOR) != 0) {
Damien George2326d522014-03-27 23:26:35 +0000172 fun = mp_obj_new_gen_wrap(fun);
173 }
174
175 return fun;
176}
177
Damien George3c658a42014-08-24 16:28:17 +0100178mp_obj_t mp_make_closure_from_raw_code(mp_raw_code_t *rc, mp_uint_t n_closed_over, const mp_obj_t *args) {
179 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 +0000180 // make function object
Damien George3558f622014-04-20 17:50:40 +0100181 mp_obj_t ffun;
182 if (n_closed_over & 0x100) {
183 // default positional and keyword args given
184 ffun = mp_make_function_from_raw_code(rc, args[0], args[1]);
185 } else {
186 // default positional and keyword args not given
187 ffun = mp_make_function_from_raw_code(rc, MP_OBJ_NULL, MP_OBJ_NULL);
188 }
Damien George2326d522014-03-27 23:26:35 +0000189 // wrap function in closure object
Damien George3558f622014-04-20 17:50:40 +0100190 return mp_obj_new_closure(ffun, n_closed_over & 0xff, args + ((n_closed_over >> 7) & 2));
Damien George2326d522014-03-27 23:26:35 +0000191}
Damien Georged8c834c2015-11-02 21:55:42 +0000192
193#if MICROPY_PERSISTENT_CODE
194
195typedef struct _bytecode_prelude_t {
196 uint n_state;
197 uint n_exc_stack;
198 uint scope_flags;
199 uint n_pos_args;
200 uint n_kwonly_args;
201 uint n_def_pos_args;
202 uint code_info_size;
203} bytecode_prelude_t;
204
205// ip will point to start of opcodes
206// ip2 will point to simple_name, source_file qstrs
207STATIC void extract_prelude(const byte **ip, const byte **ip2, bytecode_prelude_t *prelude) {
208 prelude->n_state = mp_decode_uint(ip);
209 prelude->n_exc_stack = mp_decode_uint(ip);
210 prelude->scope_flags = *(*ip)++;
211 prelude->n_pos_args = *(*ip)++;
212 prelude->n_kwonly_args = *(*ip)++;
213 prelude->n_def_pos_args = *(*ip)++;
214 *ip2 = *ip;
215 prelude->code_info_size = mp_decode_uint(ip2);
216 *ip += prelude->code_info_size;
217 while (*(*ip)++ != 255) {
218 }
219}
220
221#endif // MICROPY_PERSISTENT_CODE
222
223#if MICROPY_PERSISTENT_CODE_LOAD
224
225#include "py/bc0.h"
226
227STATIC void read_bytes(mp_reader_t *reader, byte *buf, size_t len) {
228 while (len-- > 0) {
229 *buf++ = reader->read_byte(reader->data);
230 }
231}
232
233STATIC mp_uint_t read_uint(mp_reader_t *reader) {
234 mp_uint_t unum = 0;
235 for (;;) {
236 byte b = reader->read_byte(reader->data);
237 unum = (unum << 7) | (b & 0x7f);
238 if ((b & 0x80) == 0) {
239 break;
240 }
241 }
242 return unum;
243}
244
245STATIC qstr load_qstr(mp_reader_t *reader) {
246 mp_uint_t len = read_uint(reader);
247 char *str = m_new(char, len);
248 read_bytes(reader, (byte*)str, len);
249 qstr qst = qstr_from_strn(str, len);
250 m_del(char, str, len);
251 return qst;
252}
253
254STATIC mp_obj_t load_obj(mp_reader_t *reader) {
255 (void)reader;
256 assert(0);
257 return MP_OBJ_NULL;
258}
259
260STATIC void load_bytecode_qstrs(mp_reader_t *reader, byte *ip, byte *ip_top) {
261 while (ip < ip_top) {
262 size_t sz;
263 uint f = mp_opcode_format(ip, &sz);
264 if (f == MP_OPCODE_QSTR) {
265 qstr qst = load_qstr(reader);
266 ip[1] = qst;
267 ip[2] = qst >> 8;
268 }
269 ip += sz;
270 }
271}
272
273STATIC mp_raw_code_t *load_raw_code(mp_reader_t *reader) {
274 // load bytecode
275 mp_uint_t bc_len = read_uint(reader);
276 byte *bytecode = m_new(byte, bc_len);
277 read_bytes(reader, bytecode, bc_len);
278
279 // extract prelude
280 const byte *ip = bytecode;
281 const byte *ip2;
282 bytecode_prelude_t prelude;
283 extract_prelude(&ip, &ip2, &prelude);
284
285 // load qstrs and link global qstr ids into bytecode
286 qstr simple_name = load_qstr(reader);
287 qstr source_file = load_qstr(reader);
288 ((byte*)ip2)[0] = simple_name; ((byte*)ip2)[1] = simple_name >> 8;
289 ((byte*)ip2)[2] = source_file; ((byte*)ip2)[3] = source_file >> 8;
290 load_bytecode_qstrs(reader, (byte*)ip, bytecode + bc_len);
291
292 // load constant table
293 mp_uint_t n_obj = read_uint(reader);
294 mp_uint_t n_raw_code = read_uint(reader);
295 mp_uint_t *const_table = m_new(mp_uint_t, prelude.n_pos_args + prelude.n_kwonly_args + n_obj + n_raw_code);
296 mp_uint_t *ct = const_table;
297 for (mp_uint_t i = 0; i < prelude.n_pos_args + prelude.n_kwonly_args; ++i) {
298 *ct++ = (mp_uint_t)MP_OBJ_NEW_QSTR(load_qstr(reader));
299 }
300 for (mp_uint_t i = 0; i < n_obj; ++i) {
301 *ct++ = (mp_uint_t)load_obj(reader);
302 }
303 for (mp_uint_t i = 0; i < n_raw_code; ++i) {
304 *ct++ = (mp_uint_t)load_raw_code(reader);
305 }
306
307 // create raw_code and return it
308 mp_raw_code_t *rc = mp_emit_glue_new_raw_code();
309 mp_emit_glue_assign_bytecode(rc, bytecode, bc_len, const_table,
310 #if MICROPY_PERSISTENT_CODE_SAVE
311 n_obj, n_raw_code,
312 #endif
313 prelude.scope_flags);
314 return rc;
315}
316
317mp_raw_code_t *mp_raw_code_load(mp_reader_t *reader) {
318 byte header[2];
319 read_bytes(reader, header, 2);
320 if (strncmp((char*)header, "M\x00", 2) != 0) {
321 nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError,
322 "invalid .mpy file"));
323 }
324 return load_raw_code(reader);
325}
326
327// here we define mp_raw_code_load_file depending on the port
328// TODO abstract this away properly
329
330#if defined(__i386__) || defined(__x86_64__)
331// unix file reader
332
333#include <sys/stat.h>
334#include <fcntl.h>
335
336typedef struct _mp_lexer_file_buf_t {
337 int fd;
338 byte buf[20];
339 mp_uint_t len;
340 mp_uint_t pos;
341} mp_lexer_file_buf_t;
342
343STATIC mp_uint_t file_buf_next_byte(void *fb_in) {
344 mp_lexer_file_buf_t *fb = fb_in;
345 if (fb->pos >= fb->len) {
346 if (fb->len == 0) {
347 return (mp_uint_t)-1;
348 } else {
349 int n = read(fb->fd, fb->buf, sizeof(fb->buf));
350 if (n <= 0) {
351 fb->len = 0;
352 return (mp_uint_t)-1;
353 }
354 fb->len = n;
355 fb->pos = 0;
356 }
357 }
358 return fb->buf[fb->pos++];
359}
360
361mp_raw_code_t *mp_raw_code_load_file(const char *filename) {
362 mp_lexer_file_buf_t fb;
363 fb.fd = open(filename, O_RDONLY, 0644);
364 int n = read(fb.fd, fb.buf, sizeof(fb.buf));
365 fb.len = n;
366 fb.pos = 0;
367 mp_reader_t reader;
368 reader.data = &fb;
369 reader.read_byte = file_buf_next_byte;
370 mp_raw_code_t *rc = mp_raw_code_load(&reader);
371 close(fb.fd);
372 return rc;
373}
374
Damien Georgef1487272015-11-20 12:42:26 +0000375#elif defined(__thumb2__)
376// fatfs file reader (assume thumb2 arch uses fatfs...)
Damien Georged8c834c2015-11-02 21:55:42 +0000377
378#include "lib/fatfs/ff.h"
379
380typedef struct _mp_lexer_file_buf_t {
381 FIL fp;
382 byte buf[20];
383 uint16_t len;
384 uint16_t pos;
385} mp_lexer_file_buf_t;
386
387STATIC mp_uint_t file_buf_next_byte(void *fb_in) {
388 mp_lexer_file_buf_t *fb = fb_in;
389 if (fb->pos >= fb->len) {
390 if (fb->len < sizeof(fb->buf)) {
391 return (mp_uint_t)-1;
392 } else {
393 UINT n;
394 f_read(&fb->fp, fb->buf, sizeof(fb->buf), &n);
395 if (n == 0) {
396 return (mp_uint_t)-1;
397 }
398 fb->len = n;
399 fb->pos = 0;
400 }
401 }
402 return fb->buf[fb->pos++];
403}
404
405mp_raw_code_t *mp_raw_code_load_file(const char *filename) {
406 mp_lexer_file_buf_t fb;
407 /*FRESULT res =*/ f_open(&fb.fp, filename, FA_READ);
408 UINT n;
409 f_read(&fb.fp, fb.buf, sizeof(fb.buf), &n);
410 fb.len = n;
411 fb.pos = 0;
412
413 mp_reader_t reader;
414 reader.data = &fb;
415 reader.read_byte = file_buf_next_byte;
416 mp_raw_code_t *rc = mp_raw_code_load(&reader);
417
418 f_close(&fb.fp);
419
420 return rc;
421}
422
423#endif
424
425#endif // MICROPY_PERSISTENT_CODE_LOAD
426
427#if MICROPY_PERSISTENT_CODE_SAVE
428STATIC void mp_print_bytes(mp_print_t *print, const byte *data, size_t len) {
429 print->print_strn(print->data, (const char*)data, len);
430}
431
432#define BYTES_FOR_INT ((BYTES_PER_WORD * 8 + 6) / 7)
433STATIC void mp_print_uint(mp_print_t *print, mp_uint_t n) {
434 byte buf[BYTES_FOR_INT];
435 byte *p = buf + sizeof(buf);
436 *--p = n & 0x7f;
437 n >>= 7;
438 for (; n != 0; n >>= 7) {
439 *--p = 0x80 | (n & 0x7f);
440 }
441 print->print_strn(print->data, (char*)p, buf + sizeof(buf) - p);
442}
443
444STATIC void save_qstr(mp_print_t *print, qstr qst) {
445 mp_uint_t len;
446 const byte *str = qstr_data(qst, &len);
447 mp_print_uint(print, len);
448 mp_print_bytes(print, str, len);
449}
450
451STATIC void save_obj(mp_print_t *print, mp_obj_t o) {
452 if (MP_OBJ_IS_STR(o)) {
453 byte buf[] = {'s'};
454 mp_print_bytes(print, buf, 1);
455 mp_uint_t len;
456 const char *str = mp_obj_str_get_data(o, &len);
457 mp_print_uint(print, len);
458 mp_print_bytes(print, (const byte*)str, len);
459 } else if (MP_OBJ_IS_TYPE(o, &mp_type_bytes)) {
460 byte buf[] = {'b'};
461 mp_print_bytes(print, buf, 1);
462 mp_uint_t len;
463 const char *str = mp_obj_str_get_data(o, &len);
464 mp_print_uint(print, len);
465 mp_print_bytes(print, (const byte*)str, len);
466 } else if (MP_OBJ_IS_TYPE(o, &mp_type_int)) {
467 byte buf[] = {'i'};
468 mp_print_bytes(print, buf, 1);
469 // TODO
470 } else if (MP_OBJ_IS_TYPE(o, &mp_type_float)) {
471 byte buf[] = {'f'};
472 mp_print_bytes(print, buf, 1);
473 // TODO
474 } else if (MP_OBJ_IS_TYPE(o, &mp_type_complex)) {
475 byte buf[] = {'c'};
476 mp_print_bytes(print, buf, 1);
477 // TODO
478 } else if (o == &mp_const_ellipsis_obj) {
479 byte buf[] = {'e'};
480 mp_print_bytes(print, buf, 1);
481 } else {
482 mp_obj_print(o, PRINT_STR);
483 assert(0);
484 }
485}
486
487STATIC void save_bytecode_qstrs(mp_print_t *print, const byte *ip, const byte *ip_top) {
488 while (ip < ip_top) {
489 size_t sz;
490 uint f = mp_opcode_format(ip, &sz);
491 if (f == MP_OPCODE_QSTR) {
492 qstr qst = ip[1] | (ip[2] << 8);
493 save_qstr(print, qst);
494 }
495 ip += sz;
496 }
497}
498
499STATIC void save_raw_code(mp_print_t *print, mp_raw_code_t *rc) {
500 if (rc->kind != MP_CODE_BYTECODE) {
501 nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError,
502 "can only save bytecode"));
503 }
504
505 // save bytecode
506 mp_print_uint(print, rc->data.u_byte.bc_len);
507 mp_print_bytes(print, rc->data.u_byte.bytecode, rc->data.u_byte.bc_len);
508
509 // extract prelude
510 const byte *ip = rc->data.u_byte.bytecode;
511 const byte *ip2;
512 bytecode_prelude_t prelude;
513 extract_prelude(&ip, &ip2, &prelude);
514
515 // save qstrs
516 save_qstr(print, ip2[0] | (ip2[1] << 8)); // simple_name
517 save_qstr(print, ip2[2] | (ip2[3] << 8)); // source_file
518 save_bytecode_qstrs(print, ip, rc->data.u_byte.bytecode + rc->data.u_byte.bc_len);
519
520 // save constant table
521 mp_print_uint(print, rc->data.u_byte.n_obj);
522 mp_print_uint(print, rc->data.u_byte.n_raw_code);
523 const mp_uint_t *const_table = rc->data.u_byte.const_table;
524 for (uint i = 0; i < prelude.n_pos_args + prelude.n_kwonly_args; ++i) {
525 mp_obj_t o = (mp_obj_t)*const_table++;
526 save_qstr(print, MP_OBJ_QSTR_VALUE(o));
527 }
528 for (uint i = 0; i < rc->data.u_byte.n_obj; ++i) {
529 save_obj(print, (mp_obj_t)*const_table++);
530 }
531 for (uint i = 0; i < rc->data.u_byte.n_raw_code; ++i) {
532 save_raw_code(print, (mp_raw_code_t*)*const_table++);
533 }
534}
535
536void mp_raw_code_save(mp_raw_code_t *rc, mp_print_t *print) {
537 mp_print_bytes(print, (const byte*)"M\x00", 2);
538 save_raw_code(print, rc);
539}
540
541// here we define mp_raw_code_save_file depending on the port
542// TODO abstract this away properly
543
544#if defined(__i386__) || defined(__x86_64__)
545
546#include <unistd.h>
547#include <sys/stat.h>
548#include <fcntl.h>
549
550STATIC void fd_print_strn(void *env, const char *str, mp_uint_t len) {
551 int fd = (mp_int_t)env;
552 ssize_t ret = write(fd, str, len);
553 (void)ret;
554}
555
556void mp_raw_code_save_file(mp_raw_code_t *rc, const char *filename) {
557 int fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0644);
558 mp_print_t fd_print = {(void*)(mp_int_t)fd, fd_print_strn};
559 mp_raw_code_save(rc, &fd_print);
560 close(fd);
561}
562
563#else
564#error mp_raw_code_save_file not implemented for this platform
565#endif
566
567#endif // MICROPY_PERSISTENT_CODE_SAVE