From 6810f2c134f9329e9dc18f4e0d3a1936ca6d8011 Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 16 Nov 2016 11:55:41 +1100 Subject: py: Factor persistent code load/save funcs into persistentcode.[ch]. --- py/persistentcode.c | 499 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 499 insertions(+) create mode 100644 py/persistentcode.c (limited to 'py/persistentcode.c') diff --git a/py/persistentcode.c b/py/persistentcode.c new file mode 100644 index 000000000..31f147a09 --- /dev/null +++ b/py/persistentcode.c @@ -0,0 +1,499 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2013-2016 Damien P. George + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include +#include +#include +#include + +#include "py/emitglue.h" +#include "py/persistentcode.h" +#include "py/bc.h" + +#if MICROPY_PERSISTENT_CODE_LOAD || MICROPY_PERSISTENT_CODE_SAVE + +#include "py/smallint.h" + +// The feature flags byte encodes the compile-time config options that +// affect the generate bytecode. +#define MPY_FEATURE_FLAGS ( \ + ((MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE) << 0) \ + | ((MICROPY_PY_BUILTINS_STR_UNICODE) << 1) \ + ) +// This is a version of the flags that can be configured at runtime. +#define MPY_FEATURE_FLAGS_DYNAMIC ( \ + ((MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE_DYNAMIC) << 0) \ + | ((MICROPY_PY_BUILTINS_STR_UNICODE_DYNAMIC) << 1) \ + ) + +#if MICROPY_PERSISTENT_CODE_LOAD || (MICROPY_PERSISTENT_CODE_SAVE && !MICROPY_DYNAMIC_COMPILER) +// The bytecode will depend on the number of bits in a small-int, and +// this function computes that (could make it a fixed constant, but it +// would need to be defined in mpconfigport.h). +STATIC int mp_small_int_bits(void) { + mp_int_t i = MP_SMALL_INT_MAX; + int n = 1; + while (i != 0) { + i >>= 1; + ++n; + } + return n; +} +#endif + +typedef struct _bytecode_prelude_t { + uint n_state; + uint n_exc_stack; + uint scope_flags; + uint n_pos_args; + uint n_kwonly_args; + uint n_def_pos_args; + uint code_info_size; +} bytecode_prelude_t; + +// ip will point to start of opcodes +// ip2 will point to simple_name, source_file qstrs +STATIC void extract_prelude(const byte **ip, const byte **ip2, bytecode_prelude_t *prelude) { + prelude->n_state = mp_decode_uint(ip); + prelude->n_exc_stack = mp_decode_uint(ip); + prelude->scope_flags = *(*ip)++; + prelude->n_pos_args = *(*ip)++; + prelude->n_kwonly_args = *(*ip)++; + prelude->n_def_pos_args = *(*ip)++; + *ip2 = *ip; + prelude->code_info_size = mp_decode_uint(ip2); + *ip += prelude->code_info_size; + while (*(*ip)++ != 255) { + } +} + +#endif // MICROPY_PERSISTENT_CODE_LOAD || MICROPY_PERSISTENT_CODE_SAVE + +#if MICROPY_PERSISTENT_CODE_LOAD + +#include "py/parsenum.h" +#include "py/bc0.h" + +STATIC int read_byte(mp_reader_t *reader) { + return reader->read_byte(reader->data); +} + +STATIC void read_bytes(mp_reader_t *reader, byte *buf, size_t len) { + while (len-- > 0) { + *buf++ = reader->read_byte(reader->data); + } +} + +STATIC mp_uint_t read_uint(mp_reader_t *reader) { + mp_uint_t unum = 0; + for (;;) { + byte b = reader->read_byte(reader->data); + unum = (unum << 7) | (b & 0x7f); + if ((b & 0x80) == 0) { + break; + } + } + return unum; +} + +STATIC qstr load_qstr(mp_reader_t *reader) { + mp_uint_t len = read_uint(reader); + char *str = m_new(char, len); + read_bytes(reader, (byte*)str, len); + qstr qst = qstr_from_strn(str, len); + m_del(char, str, len); + return qst; +} + +STATIC mp_obj_t load_obj(mp_reader_t *reader) { + byte obj_type = read_byte(reader); + if (obj_type == 'e') { + return MP_OBJ_FROM_PTR(&mp_const_ellipsis_obj); + } else { + size_t len = read_uint(reader); + vstr_t vstr; + vstr_init_len(&vstr, len); + read_bytes(reader, (byte*)vstr.buf, len); + if (obj_type == 's' || obj_type == 'b') { + return mp_obj_new_str_from_vstr(obj_type == 's' ? &mp_type_str : &mp_type_bytes, &vstr); + } else if (obj_type == 'i') { + return mp_parse_num_integer(vstr.buf, vstr.len, 10, NULL); + } else { + assert(obj_type == 'f' || obj_type == 'c'); + return mp_parse_num_decimal(vstr.buf, vstr.len, obj_type == 'c', false, NULL); + } + } +} + +STATIC void load_bytecode_qstrs(mp_reader_t *reader, byte *ip, byte *ip_top) { + while (ip < ip_top) { + size_t sz; + uint f = mp_opcode_format(ip, &sz); + if (f == MP_OPCODE_QSTR) { + qstr qst = load_qstr(reader); + ip[1] = qst; + ip[2] = qst >> 8; + } + ip += sz; + } +} + +STATIC mp_raw_code_t *load_raw_code(mp_reader_t *reader) { + // load bytecode + mp_uint_t bc_len = read_uint(reader); + byte *bytecode = m_new(byte, bc_len); + read_bytes(reader, bytecode, bc_len); + + // extract prelude + const byte *ip = bytecode; + const byte *ip2; + bytecode_prelude_t prelude; + extract_prelude(&ip, &ip2, &prelude); + + // load qstrs and link global qstr ids into bytecode + qstr simple_name = load_qstr(reader); + qstr source_file = load_qstr(reader); + ((byte*)ip2)[0] = simple_name; ((byte*)ip2)[1] = simple_name >> 8; + ((byte*)ip2)[2] = source_file; ((byte*)ip2)[3] = source_file >> 8; + load_bytecode_qstrs(reader, (byte*)ip, bytecode + bc_len); + + // load constant table + mp_uint_t n_obj = read_uint(reader); + mp_uint_t n_raw_code = read_uint(reader); + mp_uint_t *const_table = m_new(mp_uint_t, prelude.n_pos_args + prelude.n_kwonly_args + n_obj + n_raw_code); + mp_uint_t *ct = const_table; + for (mp_uint_t i = 0; i < prelude.n_pos_args + prelude.n_kwonly_args; ++i) { + *ct++ = (mp_uint_t)MP_OBJ_NEW_QSTR(load_qstr(reader)); + } + for (mp_uint_t i = 0; i < n_obj; ++i) { + *ct++ = (mp_uint_t)load_obj(reader); + } + for (mp_uint_t i = 0; i < n_raw_code; ++i) { + *ct++ = (mp_uint_t)(uintptr_t)load_raw_code(reader); + } + + // create raw_code and return it + mp_raw_code_t *rc = mp_emit_glue_new_raw_code(); + mp_emit_glue_assign_bytecode(rc, bytecode, bc_len, const_table, + #if MICROPY_PERSISTENT_CODE_SAVE + n_obj, n_raw_code, + #endif + prelude.scope_flags); + return rc; +} + +mp_raw_code_t *mp_raw_code_load(mp_reader_t *reader) { + byte header[4]; + read_bytes(reader, header, sizeof(header)); + if (strncmp((char*)header, "M\x00", 2) != 0) { + mp_raise_ValueError("invalid .mpy file"); + } + if (header[2] != MPY_FEATURE_FLAGS || header[3] > mp_small_int_bits()) { + mp_raise_ValueError("incompatible .mpy file"); + } + return load_raw_code(reader); +} + +typedef struct _mp_mem_reader_t { + const byte *cur; + const byte *end; +} mp_mem_reader_t; + +STATIC mp_uint_t mp_mem_reader_next_byte(void *br_in) { + mp_mem_reader_t *br = br_in; + if (br->cur < br->end) { + return *br->cur++; + } else { + return (mp_uint_t)-1; + } +} + +mp_raw_code_t *mp_raw_code_load_mem(const byte *buf, size_t len) { + mp_mem_reader_t mr = {buf, buf + len}; + mp_reader_t reader = {&mr, mp_mem_reader_next_byte}; + return mp_raw_code_load(&reader); +} + +// here we define mp_raw_code_load_file depending on the port +// TODO abstract this away properly + +#if defined(__i386__) || defined(__x86_64__) || defined(__aarch64__) || defined(__unix__) +// unix file reader + +#include +#include +#include + +typedef struct _mp_lexer_file_buf_t { + int fd; + byte buf[20]; + mp_uint_t len; + mp_uint_t pos; +} mp_lexer_file_buf_t; + +STATIC mp_uint_t file_buf_next_byte(void *fb_in) { + mp_lexer_file_buf_t *fb = fb_in; + if (fb->pos >= fb->len) { + if (fb->len == 0) { + return (mp_uint_t)-1; + } else { + int n = read(fb->fd, fb->buf, sizeof(fb->buf)); + if (n <= 0) { + fb->len = 0; + return (mp_uint_t)-1; + } + fb->len = n; + fb->pos = 0; + } + } + return fb->buf[fb->pos++]; +} + +mp_raw_code_t *mp_raw_code_load_file(const char *filename) { + mp_lexer_file_buf_t fb; + fb.fd = open(filename, O_RDONLY, 0644); + int n = read(fb.fd, fb.buf, sizeof(fb.buf)); + fb.len = n; + fb.pos = 0; + mp_reader_t reader; + reader.data = &fb; + reader.read_byte = file_buf_next_byte; + mp_raw_code_t *rc = mp_raw_code_load(&reader); + close(fb.fd); + return rc; +} + +#elif defined(__thumb2__) || defined(__xtensa__) +// fatfs file reader (assume thumb2 arch uses fatfs...) + +#include "lib/fatfs/ff.h" + +typedef struct _mp_lexer_file_buf_t { + FIL fp; + byte buf[20]; + uint16_t len; + uint16_t pos; +} mp_lexer_file_buf_t; + +STATIC mp_uint_t file_buf_next_byte(void *fb_in) { + mp_lexer_file_buf_t *fb = fb_in; + if (fb->pos >= fb->len) { + if (fb->len < sizeof(fb->buf)) { + return (mp_uint_t)-1; + } else { + UINT n; + f_read(&fb->fp, fb->buf, sizeof(fb->buf), &n); + if (n == 0) { + return (mp_uint_t)-1; + } + fb->len = n; + fb->pos = 0; + } + } + return fb->buf[fb->pos++]; +} + +mp_raw_code_t *mp_raw_code_load_file(const char *filename) { + mp_lexer_file_buf_t fb; + /*FRESULT res =*/ f_open(&fb.fp, filename, FA_READ); + UINT n; + f_read(&fb.fp, fb.buf, sizeof(fb.buf), &n); + fb.len = n; + fb.pos = 0; + + mp_reader_t reader; + reader.data = &fb; + reader.read_byte = file_buf_next_byte; + mp_raw_code_t *rc = mp_raw_code_load(&reader); + + f_close(&fb.fp); + + return rc; +} + +#endif + +#endif // MICROPY_PERSISTENT_CODE_LOAD + +#if MICROPY_PERSISTENT_CODE_SAVE + +#include "py/objstr.h" + +STATIC void mp_print_bytes(mp_print_t *print, const byte *data, size_t len) { + print->print_strn(print->data, (const char*)data, len); +} + +#define BYTES_FOR_INT ((BYTES_PER_WORD * 8 + 6) / 7) +STATIC void mp_print_uint(mp_print_t *print, mp_uint_t n) { + byte buf[BYTES_FOR_INT]; + byte *p = buf + sizeof(buf); + *--p = n & 0x7f; + n >>= 7; + for (; n != 0; n >>= 7) { + *--p = 0x80 | (n & 0x7f); + } + print->print_strn(print->data, (char*)p, buf + sizeof(buf) - p); +} + +STATIC void save_qstr(mp_print_t *print, qstr qst) { + size_t len; + const byte *str = qstr_data(qst, &len); + mp_print_uint(print, len); + mp_print_bytes(print, str, len); +} + +STATIC void save_obj(mp_print_t *print, mp_obj_t o) { + if (MP_OBJ_IS_STR_OR_BYTES(o)) { + byte obj_type; + if (MP_OBJ_IS_STR(o)) { + obj_type = 's'; + } else { + obj_type = 'b'; + } + mp_uint_t len; + const char *str = mp_obj_str_get_data(o, &len); + mp_print_bytes(print, &obj_type, 1); + mp_print_uint(print, len); + mp_print_bytes(print, (const byte*)str, len); + } else if (MP_OBJ_TO_PTR(o) == &mp_const_ellipsis_obj) { + byte obj_type = 'e'; + mp_print_bytes(print, &obj_type, 1); + } else { + // we save numbers using a simplistic text representation + // TODO could be improved + byte obj_type; + if (MP_OBJ_IS_TYPE(o, &mp_type_int)) { + obj_type = 'i'; + } else if (mp_obj_is_float(o)) { + obj_type = 'f'; + } else { + assert(MP_OBJ_IS_TYPE(o, &mp_type_complex)); + obj_type = 'c'; + } + vstr_t vstr; + mp_print_t pr; + vstr_init_print(&vstr, 10, &pr); + mp_obj_print_helper(&pr, o, PRINT_REPR); + mp_print_bytes(print, &obj_type, 1); + mp_print_uint(print, vstr.len); + mp_print_bytes(print, (const byte*)vstr.buf, vstr.len); + vstr_clear(&vstr); + } +} + +STATIC void save_bytecode_qstrs(mp_print_t *print, const byte *ip, const byte *ip_top) { + while (ip < ip_top) { + size_t sz; + uint f = mp_opcode_format(ip, &sz); + if (f == MP_OPCODE_QSTR) { + qstr qst = ip[1] | (ip[2] << 8); + save_qstr(print, qst); + } + ip += sz; + } +} + +STATIC void save_raw_code(mp_print_t *print, mp_raw_code_t *rc) { + if (rc->kind != MP_CODE_BYTECODE) { + mp_raise_ValueError("can only save bytecode"); + } + + // save bytecode + mp_print_uint(print, rc->data.u_byte.bc_len); + mp_print_bytes(print, rc->data.u_byte.bytecode, rc->data.u_byte.bc_len); + + // extract prelude + const byte *ip = rc->data.u_byte.bytecode; + const byte *ip2; + bytecode_prelude_t prelude; + extract_prelude(&ip, &ip2, &prelude); + + // save qstrs + save_qstr(print, ip2[0] | (ip2[1] << 8)); // simple_name + save_qstr(print, ip2[2] | (ip2[3] << 8)); // source_file + save_bytecode_qstrs(print, ip, rc->data.u_byte.bytecode + rc->data.u_byte.bc_len); + + // save constant table + mp_print_uint(print, rc->data.u_byte.n_obj); + mp_print_uint(print, rc->data.u_byte.n_raw_code); + const mp_uint_t *const_table = rc->data.u_byte.const_table; + for (uint i = 0; i < prelude.n_pos_args + prelude.n_kwonly_args; ++i) { + mp_obj_t o = (mp_obj_t)*const_table++; + save_qstr(print, MP_OBJ_QSTR_VALUE(o)); + } + for (uint i = 0; i < rc->data.u_byte.n_obj; ++i) { + save_obj(print, (mp_obj_t)*const_table++); + } + for (uint i = 0; i < rc->data.u_byte.n_raw_code; ++i) { + save_raw_code(print, (mp_raw_code_t*)(uintptr_t)*const_table++); + } +} + +void mp_raw_code_save(mp_raw_code_t *rc, mp_print_t *print) { + // header contains: + // byte 'M' + // byte version + // byte feature flags + // byte number of bits in a small int + byte header[4] = {'M', 0, MPY_FEATURE_FLAGS_DYNAMIC, + #if MICROPY_DYNAMIC_COMPILER + mp_dynamic_compiler.small_int_bits, + #else + mp_small_int_bits(), + #endif + }; + mp_print_bytes(print, header, sizeof(header)); + + save_raw_code(print, rc); +} + +// here we define mp_raw_code_save_file depending on the port +// TODO abstract this away properly + +#if defined(__i386__) || defined(__x86_64__) || (defined(__arm__) && (defined(__unix__))) + +#include +#include +#include + +STATIC void fd_print_strn(void *env, const char *str, size_t len) { + int fd = (intptr_t)env; + ssize_t ret = write(fd, str, len); + (void)ret; +} + +void mp_raw_code_save_file(mp_raw_code_t *rc, const char *filename) { + int fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0644); + mp_print_t fd_print = {(void*)(intptr_t)fd, fd_print_strn}; + mp_raw_code_save(rc, &fd_print); + close(fd); +} + +#else +#error mp_raw_code_save_file not implemented for this platform +#endif + +#endif // MICROPY_PERSISTENT_CODE_SAVE -- cgit v1.2.3 From 6b239c271c9c7041f9741c2a0f348f350808ad8a Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 16 Nov 2016 16:04:57 +1100 Subject: py: Factor out persistent-code reader into separate files. Implementations of persistent-code reader are provided for POSIX systems and systems using FatFS. Macros to use these are MICROPY_READER_POSIX and MICROPY_READER_FATFS respectively. If an alternative implementation is needed then a port can define the function mp_reader_new_file. --- cc3200/mpconfigport.h | 1 + esp8266/esp8266.ld | 1 + esp8266/esp8266_512k.ld | 1 + esp8266/mpconfigport.h | 1 + examples/embedding/mpconfigport_minimal.h | 1 + extmod/vfs_fat_reader.c | 88 ++++++++++++++++++ py/mpconfig.h | 10 ++ py/persistentcode.c | 129 +++----------------------- py/persistentcode.h | 10 +- py/py.mk | 2 + py/reader.c | 146 ++++++++++++++++++++++++++++++ py/reader.h | 45 +++++++++ stmhal/mpconfigport.h | 1 + unix/mpconfigport.h | 1 + 14 files changed, 316 insertions(+), 121 deletions(-) create mode 100644 extmod/vfs_fat_reader.c create mode 100644 py/reader.c create mode 100644 py/reader.h (limited to 'py/persistentcode.c') diff --git a/cc3200/mpconfigport.h b/cc3200/mpconfigport.h index bf3e691bd..08f926f40 100644 --- a/cc3200/mpconfigport.h +++ b/cc3200/mpconfigport.h @@ -55,6 +55,7 @@ #define MICROPY_FLOAT_IMPL (MICROPY_FLOAT_IMPL_NONE) #define MICROPY_OPT_COMPUTED_GOTO (0) #define MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE (0) +#define MICROPY_READER_FATFS (1) #ifndef DEBUG // we need ram on the launchxl while debugging #define MICROPY_CPYTHON_COMPAT (1) #else diff --git a/esp8266/esp8266.ld b/esp8266/esp8266.ld index 546ffd8c5..a6a4b930b 100644 --- a/esp8266/esp8266.ld +++ b/esp8266/esp8266.ld @@ -92,6 +92,7 @@ SECTIONS *py/formatfloat.o*(.literal* .text*) *py/frozenmod.o*(.literal* .text*) *py/gc.o*(.literal* .text*) + *py/reader*.o*(.literal* .text*) *py/lexer*.o*(.literal* .text*) *py/malloc*.o*(.literal* .text*) *py/map*.o*(.literal* .text*) diff --git a/esp8266/esp8266_512k.ld b/esp8266/esp8266_512k.ld index e744d0f46..2bffcab80 100644 --- a/esp8266/esp8266_512k.ld +++ b/esp8266/esp8266_512k.ld @@ -92,6 +92,7 @@ SECTIONS *py/formatfloat.o*(.literal* .text*) *py/frozenmod.o*(.literal* .text*) *py/gc.o*(.literal* .text*) + *py/reader*.o*(.literal* .text*) *py/lexer*.o*(.literal* .text*) *py/malloc*.o*(.literal* .text*) *py/map*.o*(.literal* .text*) diff --git a/esp8266/mpconfigport.h b/esp8266/mpconfigport.h index 602b3e9c8..adbec5d0d 100644 --- a/esp8266/mpconfigport.h +++ b/esp8266/mpconfigport.h @@ -16,6 +16,7 @@ #define MICROPY_MEM_STATS (0) #define MICROPY_DEBUG_PRINTERS (1) #define MICROPY_DEBUG_PRINTER_DEST mp_debug_print +#define MICROPY_READER_FATFS (1) #define MICROPY_ENABLE_GC (1) #define MICROPY_STACK_CHECK (1) #define MICROPY_ENABLE_EMERGENCY_EXCEPTION_BUF (1) diff --git a/examples/embedding/mpconfigport_minimal.h b/examples/embedding/mpconfigport_minimal.h index 90011b3f8..48d200858 100644 --- a/examples/embedding/mpconfigport_minimal.h +++ b/examples/embedding/mpconfigport_minimal.h @@ -33,6 +33,7 @@ #define MICROPY_COMP_CONST (0) #define MICROPY_MEM_STATS (0) #define MICROPY_DEBUG_PRINTERS (0) +#define MICROPY_READER_POSIX (1) #define MICROPY_HELPER_REPL (1) #define MICROPY_HELPER_LEXER_UNIX (1) #define MICROPY_ENABLE_SOURCE_LINE (0) diff --git a/extmod/vfs_fat_reader.c b/extmod/vfs_fat_reader.c new file mode 100644 index 000000000..7a00f18de --- /dev/null +++ b/extmod/vfs_fat_reader.c @@ -0,0 +1,88 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2013-2016 Damien P. George + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include +#include + +#include "py/mperrno.h" +#include "py/reader.h" + +#if MICROPY_READER_FATFS + +#include "lib/fatfs/ff.h" +#include "extmod/vfs_fat_file.h" + +typedef struct _mp_reader_fatfs_t { + FIL fp; + uint16_t len; + uint16_t pos; + byte buf[20]; +} mp_reader_fatfs_t; + +STATIC mp_uint_t mp_reader_fatfs_readbyte(void *data) { + mp_reader_fatfs_t *reader = (mp_reader_fatfs_t*)data; + if (reader->pos >= reader->len) { + if (reader->len < sizeof(reader->buf)) { + return MP_READER_EOF; + } else { + UINT n; + f_read(&reader->fp, reader->buf, sizeof(reader->buf), &n); + if (n == 0) { + return MP_READER_EOF; + } + reader->len = n; + reader->pos = 0; + } + } + return reader->buf[reader->pos++]; +} + +STATIC void mp_reader_fatfs_close(void *data) { + mp_reader_fatfs_t *reader = (mp_reader_fatfs_t*)data; + f_close(&reader->fp); + m_del_obj(mp_reader_fatfs_t, reader); +} + +int mp_reader_new_file(mp_reader_t *reader, const char *filename) { + mp_reader_fatfs_t *rf = m_new_obj_maybe(mp_reader_fatfs_t); + if (rf == NULL) { + return MP_ENOMEM; + } + FRESULT res = f_open(&rf->fp, filename, FA_READ); + if (res != FR_OK) { + return fresult_to_errno_table[res]; + } + UINT n; + f_read(&rf->fp, rf->buf, sizeof(rf->buf), &n); + rf->len = n; + rf->pos = 0; + reader->data = rf; + reader->readbyte = mp_reader_fatfs_readbyte; + reader->close = mp_reader_fatfs_close; + return 0; // success +} + +#endif diff --git a/py/mpconfig.h b/py/mpconfig.h index 3945a1a5a..1980e649c 100644 --- a/py/mpconfig.h +++ b/py/mpconfig.h @@ -381,6 +381,16 @@ /*****************************************************************************/ /* Python internal features */ +// Whether to use the POSIX reader for importing files +#ifndef MICROPY_READER_POSIX +#define MICROPY_READER_POSIX (0) +#endif + +// Whether to use the FatFS reader for importing files +#ifndef MICROPY_READER_FATFS +#define MICROPY_READER_FATFS (0) +#endif + // Hook for the VM at the start of the opcode loop (can contain variable // definitions usable by the other hook functions) #ifndef MICROPY_VM_HOOK_INIT diff --git a/py/persistentcode.c b/py/persistentcode.c index 31f147a09..99b01f8e2 100644 --- a/py/persistentcode.c +++ b/py/persistentcode.c @@ -29,6 +29,7 @@ #include #include +#include "py/reader.h" #include "py/emitglue.h" #include "py/persistentcode.h" #include "py/bc.h" @@ -98,19 +99,19 @@ STATIC void extract_prelude(const byte **ip, const byte **ip2, bytecode_prelude_ #include "py/bc0.h" STATIC int read_byte(mp_reader_t *reader) { - return reader->read_byte(reader->data); + return reader->readbyte(reader->data); } STATIC void read_bytes(mp_reader_t *reader, byte *buf, size_t len) { while (len-- > 0) { - *buf++ = reader->read_byte(reader->data); + *buf++ = reader->readbyte(reader->data); } } STATIC mp_uint_t read_uint(mp_reader_t *reader) { mp_uint_t unum = 0; for (;;) { - byte b = reader->read_byte(reader->data); + byte b = reader->readbyte(reader->data); unum = (unum << 7) | (b & 0x7f); if ((b & 0x80) == 0) { break; @@ -214,128 +215,28 @@ mp_raw_code_t *mp_raw_code_load(mp_reader_t *reader) { if (header[2] != MPY_FEATURE_FLAGS || header[3] > mp_small_int_bits()) { mp_raise_ValueError("incompatible .mpy file"); } - return load_raw_code(reader); -} - -typedef struct _mp_mem_reader_t { - const byte *cur; - const byte *end; -} mp_mem_reader_t; - -STATIC mp_uint_t mp_mem_reader_next_byte(void *br_in) { - mp_mem_reader_t *br = br_in; - if (br->cur < br->end) { - return *br->cur++; - } else { - return (mp_uint_t)-1; - } + mp_raw_code_t *rc = load_raw_code(reader); + reader->close(reader->data); + return rc; } mp_raw_code_t *mp_raw_code_load_mem(const byte *buf, size_t len) { - mp_mem_reader_t mr = {buf, buf + len}; - mp_reader_t reader = {&mr, mp_mem_reader_next_byte}; - return mp_raw_code_load(&reader); -} - -// here we define mp_raw_code_load_file depending on the port -// TODO abstract this away properly - -#if defined(__i386__) || defined(__x86_64__) || defined(__aarch64__) || defined(__unix__) -// unix file reader - -#include -#include -#include - -typedef struct _mp_lexer_file_buf_t { - int fd; - byte buf[20]; - mp_uint_t len; - mp_uint_t pos; -} mp_lexer_file_buf_t; - -STATIC mp_uint_t file_buf_next_byte(void *fb_in) { - mp_lexer_file_buf_t *fb = fb_in; - if (fb->pos >= fb->len) { - if (fb->len == 0) { - return (mp_uint_t)-1; - } else { - int n = read(fb->fd, fb->buf, sizeof(fb->buf)); - if (n <= 0) { - fb->len = 0; - return (mp_uint_t)-1; - } - fb->len = n; - fb->pos = 0; - } - } - return fb->buf[fb->pos++]; -} - -mp_raw_code_t *mp_raw_code_load_file(const char *filename) { - mp_lexer_file_buf_t fb; - fb.fd = open(filename, O_RDONLY, 0644); - int n = read(fb.fd, fb.buf, sizeof(fb.buf)); - fb.len = n; - fb.pos = 0; mp_reader_t reader; - reader.data = &fb; - reader.read_byte = file_buf_next_byte; - mp_raw_code_t *rc = mp_raw_code_load(&reader); - close(fb.fd); - return rc; -} - -#elif defined(__thumb2__) || defined(__xtensa__) -// fatfs file reader (assume thumb2 arch uses fatfs...) - -#include "lib/fatfs/ff.h" - -typedef struct _mp_lexer_file_buf_t { - FIL fp; - byte buf[20]; - uint16_t len; - uint16_t pos; -} mp_lexer_file_buf_t; - -STATIC mp_uint_t file_buf_next_byte(void *fb_in) { - mp_lexer_file_buf_t *fb = fb_in; - if (fb->pos >= fb->len) { - if (fb->len < sizeof(fb->buf)) { - return (mp_uint_t)-1; - } else { - UINT n; - f_read(&fb->fp, fb->buf, sizeof(fb->buf), &n); - if (n == 0) { - return (mp_uint_t)-1; - } - fb->len = n; - fb->pos = 0; - } + if (!mp_reader_new_mem(&reader, buf, len, 0)) { + m_malloc_fail(BYTES_PER_WORD); // we need to raise a MemoryError } - return fb->buf[fb->pos++]; + return mp_raw_code_load(&reader); } mp_raw_code_t *mp_raw_code_load_file(const char *filename) { - mp_lexer_file_buf_t fb; - /*FRESULT res =*/ f_open(&fb.fp, filename, FA_READ); - UINT n; - f_read(&fb.fp, fb.buf, sizeof(fb.buf), &n); - fb.len = n; - fb.pos = 0; - mp_reader_t reader; - reader.data = &fb; - reader.read_byte = file_buf_next_byte; - mp_raw_code_t *rc = mp_raw_code_load(&reader); - - f_close(&fb.fp); - - return rc; + int ret = mp_reader_new_file(&reader, filename); + if (ret != 0) { + mp_raise_OSError(ret); + } + return mp_raw_code_load(&reader); } -#endif - #endif // MICROPY_PERSISTENT_CODE_LOAD #if MICROPY_PERSISTENT_CODE_SAVE diff --git a/py/persistentcode.h b/py/persistentcode.h index 8859bc577..d04e0b633 100644 --- a/py/persistentcode.h +++ b/py/persistentcode.h @@ -26,13 +26,9 @@ #ifndef MICROPY_INCLUDED_PY_PERSISTENTCODE_H #define MICROPY_INCLUDED_PY_PERSISTENTCODE_H -#include "py/obj.h" - -typedef struct _mp_reader_t { - void *data; - mp_uint_t (*read_byte)(void *data); - void (*close)(void *data); -} mp_reader_t; +#include "py/mpprint.h" +#include "py/reader.h" +#include "py/emitglue.h" mp_raw_code_t *mp_raw_code_load(mp_reader_t *reader); mp_raw_code_t *mp_raw_code_load_mem(const byte *buf, size_t len); diff --git a/py/py.mk b/py/py.mk index b0962d178..4630c21a9 100644 --- a/py/py.mk +++ b/py/py.mk @@ -113,6 +113,7 @@ PY_O_BASENAME = \ mpprint.o \ unicode.o \ mpz.o \ + reader.o \ lexer.o \ lexerstr.o \ lexerunix.o \ @@ -228,6 +229,7 @@ PY_O_BASENAME = \ ../extmod/vfs_fat_ffconf.o \ ../extmod/vfs_fat_diskio.o \ ../extmod/vfs_fat_file.o \ + ../extmod/vfs_fat_reader.o \ ../extmod/vfs_fat_lexer.o \ ../extmod/vfs_fat_misc.o \ ../extmod/utime_mphal.o \ diff --git a/py/reader.c b/py/reader.c new file mode 100644 index 000000000..d14fc416c --- /dev/null +++ b/py/reader.c @@ -0,0 +1,146 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2013-2016 Damien P. George + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include +#include + +#include "py/mperrno.h" +#include "py/reader.h" + +typedef struct _mp_reader_mem_t { + size_t free_len; // if >0 mem is freed on close by: m_free(beg, free_len) + const byte *beg; + const byte *cur; + const byte *end; +} mp_reader_mem_t; + +STATIC mp_uint_t mp_reader_mem_readbyte(void *data) { + mp_reader_mem_t *reader = (mp_reader_mem_t*)data; + if (reader->cur < reader->end) { + return *reader->cur++; + } else { + return MP_READER_EOF; + } +} + +STATIC void mp_reader_mem_close(void *data) { + mp_reader_mem_t *reader = (mp_reader_mem_t*)data; + if (reader->free_len > 0) { + m_del(char, (char*)reader->beg, reader->free_len); + } + m_del_obj(mp_reader_mem_t, reader); +} + +bool mp_reader_new_mem(mp_reader_t *reader, const byte *buf, size_t len, size_t free_len) { + mp_reader_mem_t *rm = m_new_obj_maybe(mp_reader_mem_t); + if (rm == NULL) { + return false; + } + rm->free_len = free_len; + rm->beg = buf; + rm->cur = buf; + rm->end = buf + len; + reader->data = rm; + reader->readbyte = mp_reader_mem_readbyte; + reader->close = mp_reader_mem_close; + return true; +} + +#if MICROPY_READER_POSIX + +#include +#include +#include +#include + +typedef struct _mp_reader_posix_t { + bool close_fd; + int fd; + size_t len; + size_t pos; + byte buf[20]; +} mp_reader_posix_t; + +STATIC mp_uint_t mp_reader_posix_readbyte(void *data) { + mp_reader_posix_t *reader = (mp_reader_posix_t*)data; + if (reader->pos >= reader->len) { + if (reader->len == 0) { + return MP_READER_EOF; + } else { + int n = read(reader->fd, reader->buf, sizeof(reader->buf)); + if (n <= 0) { + reader->len = 0; + return MP_READER_EOF; + } + reader->len = n; + reader->pos = 0; + } + } + return reader->buf[reader->pos++]; +} + +STATIC void mp_reader_posix_close(void *data) { + mp_reader_posix_t *reader = (mp_reader_posix_t*)data; + if (reader->close_fd) { + close(reader->fd); + } + m_del_obj(mp_reader_posix_t, reader); +} + +STATIC int mp_reader_new_file_from_fd(mp_reader_t *reader, int fd, bool close_fd) { + mp_reader_posix_t *rp = m_new_obj_maybe(mp_reader_posix_t); + if (rp == NULL) { + if (close_fd) { + close(fd); + } + return MP_ENOMEM; + } + rp->close_fd = close_fd; + rp->fd = fd; + int n = read(rp->fd, rp->buf, sizeof(rp->buf)); + if (n == -1) { + if (close_fd) { + close(fd); + } + return errno; + } + rp->len = n; + rp->pos = 0; + reader->data = rp; + reader->readbyte = mp_reader_posix_readbyte; + reader->close = mp_reader_posix_close; + return 0; // success +} + +int mp_reader_new_file(mp_reader_t *reader, const char *filename) { + int fd = open(filename, O_RDONLY, 0644); + if (fd < 0) { + return errno; + } + return mp_reader_new_file_from_fd(reader, fd, true); +} + +#endif diff --git a/py/reader.h b/py/reader.h new file mode 100644 index 000000000..f39cc90f8 --- /dev/null +++ b/py/reader.h @@ -0,0 +1,45 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2013-2016 Damien P. George + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +#ifndef MICROPY_INCLUDED_PY_READER_H +#define MICROPY_INCLUDED_PY_READER_H + +#include "py/obj.h" + +// the readbyte function must return the next byte in the input stream +// it must return MP_READER_EOF if end of stream +// it can be called again after returning MP_READER_EOF, and in that case must return MP_READER_EOF +#define MP_READER_EOF ((mp_uint_t)(-1)) + +typedef struct _mp_reader_t { + void *data; + mp_uint_t (*readbyte)(void *data); + void (*close)(void *data); +} mp_reader_t; + +bool mp_reader_new_mem(mp_reader_t *reader, const byte *buf, size_t len, size_t free_len); +int mp_reader_new_file(mp_reader_t *reader, const char *filename); + +#endif // MICROPY_INCLUDED_PY_READER_H diff --git a/stmhal/mpconfigport.h b/stmhal/mpconfigport.h index 264db3029..3eec0fcce 100644 --- a/stmhal/mpconfigport.h +++ b/stmhal/mpconfigport.h @@ -51,6 +51,7 @@ #define MICROPY_OPT_COMPUTED_GOTO (1) #define MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE (0) #define MICROPY_OPT_MPZ_BITWISE (1) +#define MICROPY_READER_FATFS (1) // fatfs configuration used in ffconf.h #define MICROPY_FATFS_ENABLE_LFN (1) diff --git a/unix/mpconfigport.h b/unix/mpconfigport.h index f4f8d2d20..86e460566 100644 --- a/unix/mpconfigport.h +++ b/unix/mpconfigport.h @@ -54,6 +54,7 @@ // Printing debug to stderr may give tests which // check stdout a chance to pass, etc. #define MICROPY_DEBUG_PRINTER_DEST mp_stderr_print +#define MICROPY_READER_POSIX (1) #define MICROPY_USE_READLINE_HISTORY (1) #define MICROPY_HELPER_REPL (1) #define MICROPY_REPL_EMACS_KEYS (1) -- cgit v1.2.3