diff --git a/include/MicroPython/compile.h b/include/MicroPython/compile.h index 552d36fa5..10fd603f2 100644 --- a/include/MicroPython/compile.h +++ b/include/MicroPython/compile.h @@ -1 +1,10 @@ -mp_obj_t mp_compile(mp_parse_node_t pn, qstr source_file, bool is_repl); +// These must fit in 8 bits; see scope.h +enum { + MP_EMIT_OPT_NONE, + MP_EMIT_OPT_BYTE_CODE, + MP_EMIT_OPT_NATIVE_PYTHON, + MP_EMIT_OPT_VIPER, + MP_EMIT_OPT_ASM_THUMB, +}; + +mp_obj_t mp_compile(mp_parse_node_t pn, qstr source_file, uint emit_opt, bool is_repl); diff --git a/include/MicroPython/gc.h b/include/MicroPython/gc.h index c8a1a996c..2ae7bbe5f 100644 --- a/include/MicroPython/gc.h +++ b/include/MicroPython/gc.h @@ -1,9 +1,17 @@ void gc_init(void *start, void *end); + +// These lock/unlock functions can be nested. +// They can be used to prevent the GC from allocating/freeing. +void gc_lock(void); +void gc_unlock(void); + +// A given port must implement gc_collect by using the other collect functions. +void gc_collect(void); void gc_collect_start(void); void gc_collect_root(void **ptrs, machine_uint_t len); void gc_collect_end(void); -void gc_collect(void); -void *gc_alloc(machine_uint_t n_bytes); + +void *gc_alloc(machine_uint_t n_bytes, bool has_finaliser); void gc_free(void *ptr); machine_uint_t gc_nbytes(void *ptr); void *gc_realloc(void *ptr, machine_uint_t n_bytes); diff --git a/include/MicroPython/libmp.h b/include/MicroPython/libmp.h index 54aa6d5b4..87acf7521 100644 --- a/include/MicroPython/libmp.h +++ b/include/MicroPython/libmp.h @@ -23,7 +23,6 @@ #include "pyexec.h" #include "pendsv.h" #include "ff.h" -#include "map.h" int libmp_init(); void libmp_do_repl(void); diff --git a/include/MicroPython/map.h b/include/MicroPython/map.h deleted file mode 100644 index afebfda6e..000000000 --- a/include/MicroPython/map.h +++ /dev/null @@ -1,38 +0,0 @@ -typedef struct _mp_map_elem_t { - mp_obj_t key; - mp_obj_t value; -} mp_map_elem_t; - -typedef struct _mp_map_t { - machine_uint_t all_keys_are_qstrs : 1; - machine_uint_t table_is_fixed_array : 1; - machine_uint_t used : (8 * sizeof(machine_uint_t) - 2); - machine_uint_t alloc; - mp_map_elem_t *table; -} mp_map_t; - -typedef struct _mp_set_t { - machine_uint_t alloc; - machine_uint_t used; - mp_obj_t *table; -} mp_set_t; - -typedef enum _mp_map_lookup_kind_t { - MP_MAP_LOOKUP, // 0 - MP_MAP_LOOKUP_ADD_IF_NOT_FOUND, // 1 - MP_MAP_LOOKUP_REMOVE_IF_FOUND, // 2 - MP_MAP_LOOKUP_FIRST = 4, -} mp_map_lookup_kind_t; - -int get_doubling_prime_greater_or_equal_to(int x); -void mp_map_init(mp_map_t *map, int n); -void mp_map_init_fixed_table(mp_map_t *map, int n, const mp_obj_t *table); -mp_map_t *mp_map_new(int n); -void mp_map_deinit(mp_map_t *map); -void mp_map_free(mp_map_t *map); -mp_map_elem_t* mp_map_lookup(mp_map_t *map, mp_obj_t index, mp_map_lookup_kind_t lookup_kind); -void mp_map_clear(mp_map_t *map); - -void mp_set_init(mp_set_t *set, int n); -mp_obj_t mp_set_lookup(mp_set_t *set, mp_obj_t index, mp_map_lookup_kind_t lookup_kind); -void mp_set_clear(mp_set_t *set); diff --git a/include/MicroPython/misc.h b/include/MicroPython/misc.h index 19a21d52e..3f538b98e 100644 --- a/include/MicroPython/misc.h +++ b/include/MicroPython/misc.h @@ -27,15 +27,23 @@ typedef unsigned int uint; #define m_new0(type, num) ((type*)(m_malloc0(sizeof(type) * (num)))) #define m_new_obj(type) (m_new(type, 1)) #define m_new_obj_var(obj_type, var_type, var_num) ((obj_type*)m_malloc(sizeof(obj_type) + sizeof(var_type) * (var_num))) +#if MICROPY_ENABLE_FINALISER +#define m_new_obj_with_finaliser(type) ((type*)(m_malloc_with_finaliser(sizeof(type)))) +#else +#define m_new_obj_with_finaliser(type) m_new_obj(type) +#endif #define m_renew(type, ptr, old_num, new_num) ((type*)(m_realloc((ptr), sizeof(type) * (old_num), sizeof(type) * (new_num)))) #define m_del(type, ptr, num) m_free(ptr, sizeof(type) * (num)) #define m_del_obj(type, ptr) (m_del(type, ptr, 1)) #define m_del_var(obj_type, var_type, var_num, ptr) (m_free(ptr, sizeof(obj_type) + sizeof(var_type) * (var_num))) void *m_malloc(int num_bytes); +void *m_malloc_maybe(int num_bytes); +void *m_malloc_with_finaliser(int num_bytes); void *m_malloc0(int num_bytes); void *m_realloc(void *ptr, int old_num_bytes, int new_num_bytes); void m_free(void *ptr, int num_bytes); +void *m_malloc_fail(int num_bytes); int m_get_total_bytes_allocated(void); int m_get_current_bytes_allocated(void); @@ -54,12 +62,6 @@ bool unichar_isprint(unichar c); bool unichar_isdigit(unichar c); bool unichar_isxdigit(unichar c); -/** string ******************************************************/ - -/* -#define streq(s1, s2) (strcmp((s1), (s2)) == 0) -*/ - /** variable string *********************************************/ typedef struct _vstr_t { diff --git a/include/MicroPython/mpconfig.h b/include/MicroPython/mpconfig.h index 09cc37913..1f2862ab0 100644 --- a/include/MicroPython/mpconfig.h +++ b/include/MicroPython/mpconfig.h @@ -55,6 +55,11 @@ #define MICROPY_ENABLE_GC (0) #endif +// Whether to enable finalisers in the garbage collector (ie call __del__) +#ifndef MICROPY_ENABLE_GC_FINALISER +#define MICROPY_ENABLE_GC_FINALISER (0) +#endif + // Whether to include REPL helper function #ifndef MICROPY_ENABLE_REPL_HELPERS #define MICROPY_ENABLE_REPL_HELPERS (0) @@ -105,6 +110,16 @@ typedef double mp_float_t; #define MICROPY_ENABLE_FLOAT (0) #endif +// Whether to provide "io" module +#ifndef MICROPY_ENABLE_MOD_IO +#define MICROPY_ENABLE_MOD_IO (1) +#endif + +// Whether to provide "struct" module +#ifndef MICROPY_ENABLE_MOD_STRUCT +#define MICROPY_ENABLE_MOD_STRUCT (1) +#endif + // Whether to support slice object and correspondingly // slice subscript operators #ifndef MICROPY_ENABLE_SLICE diff --git a/include/MicroPython/mpconfigport.h b/include/MicroPython/mpconfigport.h index b658fd93b..cbc97c9fa 100644 --- a/include/MicroPython/mpconfigport.h +++ b/include/MicroPython/mpconfigport.h @@ -5,10 +5,12 @@ #define MICROPY_EMIT_THUMB (1) #define MICROPY_EMIT_INLINE_THUMB (1) #define MICROPY_ENABLE_GC (1) +#define MICROPY_ENABLE_FINALISER (1) #define MICROPY_ENABLE_REPL_HELPERS (1) #define MICROPY_LONGINT_IMPL (MICROPY_LONGINT_IMPL_MPZ) #define MICROPY_FLOAT_IMPL (MICROPY_FLOAT_IMPL_FLOAT) #define MICROPY_PATH_MAX (128) +#define MICROPY_ENABLE_MOD_IO (0) /* Enable FatFS LFNs 0: Disable LFN feature. 1: Enable LFN with static working buffer on the BSS. Always NOT reentrant. @@ -18,6 +20,8 @@ #define MICROPY_ENABLE_LFN (2) #define MICROPY_LFN_CODE_PAGE (437) /* 1=SFN/ANSI 437=LFN/U.S.(OEM) */ +extern const struct _mp_obj_fun_native_t mp_builtin_open_obj; + // type definitions for the specific machine #define BYTES_PER_WORD (4) diff --git a/include/MicroPython/nlr.h b/include/MicroPython/nlr.h index 4cc66d8c2..4e931919d 100644 --- a/include/MicroPython/nlr.h +++ b/include/MicroPython/nlr.h @@ -11,7 +11,11 @@ struct _nlr_buf_t { #if defined(__i386__) void *regs[6]; #elif defined(__x86_64__) + #if defined(__CYGWIN__) + void *regs[12]; + #else void *regs[8]; + #endif #elif defined(__thumb2__) void *regs[10]; #else @@ -22,3 +26,21 @@ struct _nlr_buf_t { unsigned int nlr_push(nlr_buf_t *); void nlr_pop(void); void nlr_jump(void *val) __attribute__((noreturn)); + +// This must be implemented by a port. It's called by nlr_jump +// if no nlr buf has been pushed. It must not return, but rather +// should bail out with a fatal error. +void nlr_jump_fail(void *val); + +// use nlr_raise instead of nlr_jump so that debugging is easier +#ifndef DEBUG +#define nlr_raise(val) nlr_jump(val) +#else +#define nlr_raise(val) \ + do { \ + void *_val = val; \ + assert(_val != NULL); \ + assert(mp_obj_is_exception_instance(_val)); \ + nlr_jump(_val); \ + } while (0) +#endif diff --git a/include/MicroPython/obj.h b/include/MicroPython/obj.h index b52b5a0c7..b944d2fcb 100644 --- a/include/MicroPython/obj.h +++ b/include/MicroPython/obj.h @@ -23,6 +23,11 @@ typedef struct _mp_obj_base_t mp_obj_base_t; #define MP_OBJ_NULL ((mp_obj_t)NULL) +// The SENTINEL object is used for various internal purposes where one needs +// an object which is unique from all other objects, including MP_OBJ_NULL. + +#define MP_OBJ_SENTINEL ((mp_obj_t)8) + // These macros check for small int, qstr or object, and access small int and qstr values // - xxxx...xxx1: a small int, bits 1 and above are the value // - xxxx...xx10: a qstr, bits 2 and above are the value @@ -36,8 +41,8 @@ typedef struct _mp_obj_base_t mp_obj_base_t; #define MP_OBJ_IS_QSTR(o) ((((mp_small_int_t)(o)) & 3) == 2) #define MP_OBJ_IS_OBJ(o) ((((mp_small_int_t)(o)) & 3) == 0) #define MP_OBJ_IS_TYPE(o, t) (MP_OBJ_IS_OBJ(o) && (((mp_obj_base_t*)(o))->type == (t))) // this does not work for checking a string, use below macro for that -#define MP_OBJ_IS_INT(o) (MP_OBJ_IS_SMALL_INT(o) || MP_OBJ_IS_TYPE(o, &int_type)) -#define MP_OBJ_IS_STR(o) (MP_OBJ_IS_QSTR(o) || MP_OBJ_IS_TYPE(o, &str_type)) +#define MP_OBJ_IS_INT(o) (MP_OBJ_IS_SMALL_INT(o) || MP_OBJ_IS_TYPE(o, &mp_type_int)) +#define MP_OBJ_IS_STR(o) (MP_OBJ_IS_QSTR(o) || MP_OBJ_IS_TYPE(o, &mp_type_str)) #define MP_OBJ_SMALL_INT_VALUE(o) (((mp_small_int_t)(o)) >> 1) #define MP_OBJ_NEW_SMALL_INT(small_int) ((mp_obj_t)(((small_int) << 1) | 1)) @@ -50,7 +55,7 @@ typedef struct _mp_obj_base_t mp_obj_base_t; #define MP_DECLARE_CONST_FUN_OBJ(obj_name) extern const mp_obj_fun_native_t obj_name -#define MP_DEFINE_CONST_FUN_OBJ_VOID_PTR(obj_name, is_kw, n_args_min, n_args_max, fun_name) const mp_obj_fun_native_t obj_name = {{&fun_native_type}, is_kw, n_args_min, n_args_max, (void *)fun_name} +#define MP_DEFINE_CONST_FUN_OBJ_VOID_PTR(obj_name, is_kw, n_args_min, n_args_max, fun_name) const mp_obj_fun_native_t obj_name = {{&mp_type_fun_native}, is_kw, n_args_min, n_args_max, (void *)fun_name} #define MP_DEFINE_CONST_FUN_OBJ_0(obj_name, fun_name) MP_DEFINE_CONST_FUN_OBJ_VOID_PTR(obj_name, false, 0, 0, (mp_fun_0_t)fun_name) #define MP_DEFINE_CONST_FUN_OBJ_1(obj_name, fun_name) MP_DEFINE_CONST_FUN_OBJ_VOID_PTR(obj_name, false, 1, 1, (mp_fun_1_t)fun_name) #define MP_DEFINE_CONST_FUN_OBJ_2(obj_name, fun_name) MP_DEFINE_CONST_FUN_OBJ_VOID_PTR(obj_name, false, 2, 2, (mp_fun_2_t)fun_name) @@ -59,6 +64,21 @@ typedef struct _mp_obj_base_t mp_obj_base_t; #define MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(obj_name, n_args_min, n_args_max, fun_name) MP_DEFINE_CONST_FUN_OBJ_VOID_PTR(obj_name, false, n_args_min, n_args_max, (mp_fun_var_t)fun_name) #define MP_DEFINE_CONST_FUN_OBJ_KW(obj_name, n_args_min, fun_name) MP_DEFINE_CONST_FUN_OBJ_VOID_PTR(obj_name, true, n_args_min, MP_OBJ_FUN_ARGS_MAX, (mp_fun_kw_t)fun_name) +// This macro is used to define constant dict objects +// You can put "static" in front of the definition to make it local + +#define MP_DEFINE_CONST_DICT(dict_name, table_name) \ + const mp_obj_dict_t dict_name = { \ + .base = {&mp_type_dict}, \ + .map = { \ + .all_keys_are_qstrs = 1, \ + .table_is_fixed_array = 1, \ + .used = sizeof(table_name) / sizeof(mp_map_elem_t), \ + .alloc = sizeof(table_name) / sizeof(mp_map_elem_t), \ + .table = (mp_map_elem_t*)table_name, \ + }, \ + } + // These macros are used to declare and define constant staticmethond and classmethod objects // You can put "static" in front of the definitions to make them local @@ -68,9 +88,58 @@ typedef struct _mp_obj_base_t mp_obj_base_t; #define MP_DEFINE_CONST_STATICMETHOD_OBJ(obj_name, fun_name) const mp_obj_static_class_method_t obj_name = {{&mp_type_staticmethod}, fun_name} #define MP_DEFINE_CONST_CLASSMETHOD_OBJ(obj_name, fun_name) const mp_obj_static_class_method_t obj_name = {{&mp_type_classmethod}, fun_name} -// Need to declare this here so we are not dependent on map.h -struct _mp_map_t; -struct _mp_map_elem_t; +// Underlying map/hash table implementation (not dict object or map function) + +typedef struct _mp_map_elem_t { + mp_obj_t key; + mp_obj_t value; +} mp_map_elem_t; + +// TODO maybe have a truncated mp_map_t for fixed tables, since alloc=used +// put alloc last in the structure, so the truncated version does not need it +// this would save 1 ROM word for all ROM objects that have a locals_dict +// would also need a trucated dict structure + +typedef struct _mp_map_t { + machine_uint_t all_keys_are_qstrs : 1; + machine_uint_t table_is_fixed_array : 1; + machine_uint_t used : (8 * sizeof(machine_uint_t) - 2); + machine_uint_t alloc; + mp_map_elem_t *table; +} mp_map_t; + +// These can be or'd together +typedef enum _mp_map_lookup_kind_t { + MP_MAP_LOOKUP, // 0 + MP_MAP_LOOKUP_ADD_IF_NOT_FOUND, // 1 + MP_MAP_LOOKUP_REMOVE_IF_FOUND, // 2 +} mp_map_lookup_kind_t; + +#define MP_MAP_SLOT_IS_FILLED(map, pos) ((map)->table[pos].key != MP_OBJ_NULL && (map)->table[pos].key != MP_OBJ_SENTINEL) + +void mp_map_init(mp_map_t *map, int n); +void mp_map_init_fixed_table(mp_map_t *map, int n, const mp_obj_t *table); +mp_map_t *mp_map_new(int n); +void mp_map_deinit(mp_map_t *map); +void mp_map_free(mp_map_t *map); +mp_map_elem_t* mp_map_lookup(mp_map_t *map, mp_obj_t index, mp_map_lookup_kind_t lookup_kind); +void mp_map_clear(mp_map_t *map); +void mp_map_dump(mp_map_t *map); + +// Underlying set implementation (not set object) + +typedef struct _mp_set_t { + machine_uint_t alloc; + machine_uint_t used; + mp_obj_t *table; +} mp_set_t; + +#define MP_SET_SLOT_IS_FILLED(set, pos) ((set)->table[pos] != MP_OBJ_NULL && (set)->table[pos] != MP_OBJ_SENTINEL) + +void mp_set_init(mp_set_t *set, int n); +mp_obj_t mp_set_lookup(mp_set_t *set, mp_obj_t index, mp_map_lookup_kind_t lookup_kind); +mp_obj_t mp_set_remove_first(mp_set_t *set); +void mp_set_clear(mp_set_t *set); // Type definitions for methods @@ -80,10 +149,12 @@ typedef mp_obj_t (*mp_fun_2_t)(mp_obj_t, mp_obj_t); typedef mp_obj_t (*mp_fun_3_t)(mp_obj_t, mp_obj_t, mp_obj_t); typedef mp_obj_t (*mp_fun_t)(void); typedef mp_obj_t (*mp_fun_var_t)(uint n, const mp_obj_t *); -typedef mp_obj_t (*mp_fun_kw_t)(uint n, const mp_obj_t *, struct _mp_map_t *); +typedef mp_obj_t (*mp_fun_kw_t)(uint n, const mp_obj_t *, mp_map_t *); typedef enum { - PRINT_STR, PRINT_REPR + PRINT_STR, + PRINT_REPR, + PRINT_EXC, // Special format for printing exception in unhandled exception message } mp_print_kind_t; typedef void (*mp_print_fun_t)(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t o, mp_print_kind_t kind); @@ -92,11 +163,11 @@ typedef mp_obj_t (*mp_call_fun_t)(mp_obj_t fun, uint n_args, uint n_kw, const mp typedef mp_obj_t (*mp_unary_op_fun_t)(int op, mp_obj_t); typedef mp_obj_t (*mp_binary_op_fun_t)(int op, mp_obj_t, mp_obj_t); typedef void (*mp_load_attr_fun_t)(mp_obj_t self_in, qstr attr, mp_obj_t *dest); // for fail, do nothing; for attr, dest[0] = value; for method, dest[0] = method, dest[1] = self -typedef bool (*mp_store_attr_fun_t)(mp_obj_t self_in, qstr attr, mp_obj_t value); // return true if store succeeded -typedef bool (*mp_store_item_fun_t)(mp_obj_t self_in, mp_obj_t index, mp_obj_t value); // return true if store succeeded +typedef bool (*mp_store_attr_fun_t)(mp_obj_t self_in, qstr attr, mp_obj_t value); // return true if store succeeded; if value==MP_OBJ_NULL then delete +typedef bool (*mp_store_item_fun_t)(mp_obj_t self_in, mp_obj_t index, mp_obj_t value); // return true if store succeeded; if value==MP_OBJ_NULL then delete typedef struct _mp_method_t { - const char *name; + qstr name; mp_const_obj_t fun; } mp_method_t; @@ -125,6 +196,8 @@ typedef struct _buffer_info_t { typedef struct _mp_buffer_p_t { machine_int_t (*get_buffer)(mp_obj_t obj, buffer_info_t *bufinfo, int flags); } mp_buffer_p_t; +bool mp_get_buffer(mp_obj_t obj, buffer_info_t *bufinfo); +void mp_get_buffer_raise(mp_obj_t obj, buffer_info_t *bufinfo); // Stream protocol typedef struct _mp_stream_p_t { @@ -146,21 +219,20 @@ struct _mp_obj_type_t { mp_binary_op_fun_t binary_op; // can return NULL if op not supported mp_load_attr_fun_t load_attr; - mp_store_attr_fun_t store_attr; - // Implements container[index] = val; note that load_item is implemented - // by binary_op(RT_BINARY_OP_SUBSCR) + mp_store_attr_fun_t store_attr; // if value is MP_OBJ_NULL, then delete that attribute + + // Implements container[index] = val. If val == MP_OBJ_NULL, then it's a delete. + // Note that load_item is implemented by binary_op(RT_BINARY_OP_SUBSCR) mp_store_item_fun_t store_item; mp_fun_1_t getiter; - mp_fun_1_t iternext; + mp_fun_1_t iternext; // may return MP_OBJ_NULL as an optimisation instead of raising StopIteration() (with no args) // Alternatively, pointer(s) to interfaces to save space // in mp_obj_type_t at the expense of extra pointer and extra dereference // when actually used. mp_buffer_p_t buffer_p; - mp_stream_p_t stream_p; - - const mp_method_t *methods; + const mp_stream_p_t *stream_p; // these are for dynamically created types (classes) mp_obj_t bases_tuple; @@ -184,16 +256,40 @@ typedef struct _mp_obj_type_t mp_obj_type_t; // Constant types, globally accessible extern const mp_obj_type_t mp_type_type; +extern const mp_obj_type_t mp_type_object; +extern const mp_obj_type_t mp_type_NoneType; +extern const mp_obj_type_t mp_type_bool; +extern const mp_obj_type_t mp_type_int; +extern const mp_obj_type_t mp_type_str; +extern const mp_obj_type_t mp_type_bytes; +extern const mp_obj_type_t mp_type_bytearray; +extern const mp_obj_type_t mp_type_float; +extern const mp_obj_type_t mp_type_complex; +extern const mp_obj_type_t mp_type_tuple; +extern const mp_obj_type_t mp_type_list; +extern const mp_obj_type_t mp_type_map; // map (the python builtin, not the dict implementation detail) +extern const mp_obj_type_t mp_type_enumerate; +extern const mp_obj_type_t mp_type_filter; +extern const mp_obj_type_t mp_type_dict; +extern const mp_obj_type_t mp_type_set; +extern const mp_obj_type_t mp_type_slice; +extern const mp_obj_type_t mp_type_zip; +extern const mp_obj_type_t mp_type_array; +extern const mp_obj_type_t mp_type_super; +extern const mp_obj_type_t mp_type_gen_instance; +extern const mp_obj_type_t mp_type_fun_native; +extern const mp_obj_type_t mp_type_fun_bc; +extern const mp_obj_type_t mp_type_module; +extern const mp_obj_type_t mp_type_staticmethod; +extern const mp_obj_type_t mp_type_classmethod; // Exceptions extern const mp_obj_type_t mp_type_BaseException; extern const mp_obj_type_t mp_type_ArithmeticError; extern const mp_obj_type_t mp_type_AssertionError; extern const mp_obj_type_t mp_type_AttributeError; -extern const mp_obj_type_t mp_type_BufferError; extern const mp_obj_type_t mp_type_EOFError; extern const mp_obj_type_t mp_type_Exception; -extern const mp_obj_type_t mp_type_FloatingPointError; extern const mp_obj_type_t mp_type_GeneratorExit; extern const mp_obj_type_t mp_type_IOError; extern const mp_obj_type_t mp_type_ImportError; @@ -211,17 +307,22 @@ extern const mp_obj_type_t mp_type_StopIteration; extern const mp_obj_type_t mp_type_SyntaxError; extern const mp_obj_type_t mp_type_SystemError; extern const mp_obj_type_t mp_type_TypeError; -extern const mp_obj_type_t mp_type_UnboundLocalError; extern const mp_obj_type_t mp_type_ValueError; extern const mp_obj_type_t mp_type_ZeroDivisionError; // Constant objects, globally accessible -extern const mp_obj_t mp_const_none; -extern const mp_obj_t mp_const_false; -extern const mp_obj_t mp_const_true; -extern const mp_obj_t mp_const_empty_tuple; -extern const mp_obj_t mp_const_ellipsis; -extern const mp_obj_t mp_const_stop_iteration; // special object indicating end of iteration (not StopIteration exception!) +// The macros are for convenience only +#define mp_const_none ((mp_obj_t)&mp_const_none_obj) +#define mp_const_false ((mp_obj_t)&mp_const_false_obj) +#define mp_const_true ((mp_obj_t)&mp_const_true_obj) +#define mp_const_empty_tuple ((mp_obj_t)&mp_const_empty_tuple_obj) +extern const struct _mp_obj_none_t mp_const_none_obj; +extern const struct _mp_obj_bool_t mp_const_false_obj; +extern const struct _mp_obj_bool_t mp_const_true_obj; +extern const struct _mp_obj_tuple_t mp_const_empty_tuple_obj; +extern const struct _mp_obj_ellipsis_t mp_const_ellipsis_obj; +extern const struct _mp_obj_exception_t mp_const_MemoryError_obj; +extern const struct _mp_obj_exception_t mp_const_GeneratorExit_obj; // General API for objects @@ -240,14 +341,14 @@ mp_obj_t mp_obj_new_float(mp_float_t val); mp_obj_t mp_obj_new_complex(mp_float_t real, mp_float_t imag); #endif mp_obj_t mp_obj_new_exception(const mp_obj_type_t *exc_type); +mp_obj_t mp_obj_new_exception_args(const mp_obj_type_t *exc_type, uint n_args, const mp_obj_t *args); mp_obj_t mp_obj_new_exception_msg(const mp_obj_type_t *exc_type, const char *msg); mp_obj_t mp_obj_new_exception_msg_varg(const mp_obj_type_t *exc_type, const char *fmt, ...); // counts args by number of % symbols in fmt, excluding %%; can only handle void* sizes (ie no float/double!) mp_obj_t mp_obj_new_range(int start, int stop, int step); mp_obj_t mp_obj_new_range_iterator(int cur, int stop, int step); -mp_obj_t mp_obj_new_fun_bc(uint scope_flags, qstr *args, uint n_args, mp_obj_t def_args, uint n_state, const byte *code); +mp_obj_t mp_obj_new_fun_bc(uint scope_flags, qstr *args, uint n_args, mp_obj_t def_args, const byte *code); mp_obj_t mp_obj_new_fun_asm(uint n_args, void *fun); mp_obj_t mp_obj_new_gen_wrap(mp_obj_t fun); -mp_obj_t mp_obj_new_gen_instance(const byte *bytecode, uint n_state, int n_args, const mp_obj_t *args); mp_obj_t mp_obj_new_closure(mp_obj_t fun, mp_obj_t closure_tuple); mp_obj_t mp_obj_new_tuple(uint n, const mp_obj_t *items); mp_obj_t mp_obj_new_list(uint n, mp_obj_t *items); @@ -261,18 +362,20 @@ mp_obj_t mp_obj_new_module(qstr module_name); mp_obj_type_t *mp_obj_get_type(mp_obj_t o_in); const char *mp_obj_get_type_str(mp_obj_t o_in); -bool mp_obj_is_subclass_fast(mp_obj_t object, mp_obj_t classinfo); // arguments should be type objects +bool mp_obj_is_subclass_fast(mp_const_obj_t object, mp_const_obj_t classinfo); // arguments should be type objects void mp_obj_print_helper(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t o_in, mp_print_kind_t kind); void mp_obj_print(mp_obj_t o, mp_print_kind_t kind); void mp_obj_print_exception(mp_obj_t exc); +int mp_obj_is_true(mp_obj_t arg); +bool mp_obj_is_integer(mp_obj_t o_in); // returns true if o_in is bool, small int, or long int bool mp_obj_is_callable(mp_obj_t o_in); machine_int_t mp_obj_hash(mp_obj_t o_in); bool mp_obj_equal(mp_obj_t o1, mp_obj_t o2); -bool mp_obj_less(mp_obj_t o1, mp_obj_t o2); machine_int_t mp_obj_get_int(mp_obj_t arg); +bool mp_obj_get_int_maybe(mp_obj_t arg, machine_int_t *value); #if MICROPY_ENABLE_FLOAT mp_float_t mp_obj_get_float(mp_obj_t self_in); void mp_obj_get_complex(mp_obj_t self_in, mp_float_t *real, mp_float_t *imag); @@ -283,14 +386,7 @@ void mp_obj_get_array_fixed_n(mp_obj_t o, uint len, mp_obj_t **items); uint mp_get_index(const mp_obj_type_t *type, machine_uint_t len, mp_obj_t index, bool is_slice); mp_obj_t mp_obj_len_maybe(mp_obj_t o_in); /* may return NULL */ -// object -extern const mp_obj_type_t mp_type_object; - -// none -extern const mp_obj_type_t none_type; - // bool -extern const mp_obj_type_t bool_type; #define MP_BOOL(x) (x ? mp_const_true : mp_const_false) // cell @@ -298,7 +394,6 @@ mp_obj_t mp_obj_cell_get(mp_obj_t self_in); void mp_obj_cell_set(mp_obj_t self_in, mp_obj_t obj); // int -extern const mp_obj_type_t int_type; // For long int, returns value truncated to machine_int_t machine_int_t mp_obj_int_get(mp_obj_t self_in); #if MICROPY_ENABLE_FLOAT @@ -310,12 +405,13 @@ machine_int_t mp_obj_int_get_checked(mp_obj_t self_in); // exception bool mp_obj_is_exception_type(mp_obj_t self_in); bool mp_obj_is_exception_instance(mp_obj_t self_in); +bool mp_obj_exception_match(mp_obj_t exc, const mp_obj_type_t *exc_type); void mp_obj_exception_clear_traceback(mp_obj_t self_in); void mp_obj_exception_add_traceback(mp_obj_t self_in, qstr file, machine_uint_t line, qstr block); void mp_obj_exception_get_traceback(mp_obj_t self_in, machine_uint_t *n, machine_uint_t **values); +mp_obj_t mp_obj_exception_get_value(mp_obj_t self_in); // str -extern const mp_obj_type_t str_type; mp_obj_t mp_obj_str_builder_start(const mp_obj_type_t *type, uint len, byte **data); mp_obj_t mp_obj_str_builder_end(mp_obj_t o_in); bool mp_obj_str_equal(mp_obj_t s1, mp_obj_t s2); @@ -326,66 +422,51 @@ const char *mp_obj_str_get_str(mp_obj_t self_in); // use this only if you need t const char *mp_obj_str_get_data(mp_obj_t self_in, uint *len); void mp_str_print_quoted(void (*print)(void *env, const char *fmt, ...), void *env, const byte *str_data, uint str_len); -// bytes -extern const mp_obj_type_t bytes_type; - #if MICROPY_ENABLE_FLOAT // float typedef struct _mp_obj_float_t { mp_obj_base_t base; mp_float_t value; } mp_obj_float_t; -extern const mp_obj_type_t mp_type_float; mp_float_t mp_obj_float_get(mp_obj_t self_in); mp_obj_t mp_obj_float_binary_op(int op, mp_float_t lhs_val, mp_obj_t rhs); // complex -extern const mp_obj_type_t mp_type_complex; void mp_obj_complex_get(mp_obj_t self_in, mp_float_t *real, mp_float_t *imag); mp_obj_t mp_obj_complex_binary_op(int op, mp_float_t lhs_real, mp_float_t lhs_imag, mp_obj_t rhs_in); #endif // tuple -extern const mp_obj_type_t tuple_type; void mp_obj_tuple_get(mp_obj_t self_in, uint *len, mp_obj_t **items); void mp_obj_tuple_del(mp_obj_t self_in); machine_int_t mp_obj_tuple_hash(mp_obj_t self_in); +mp_obj_t mp_obj_tuple_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_obj_t *args); // list -extern const mp_obj_type_t list_type; mp_obj_t mp_obj_list_append(mp_obj_t self_in, mp_obj_t arg); void mp_obj_list_get(mp_obj_t self_in, uint *len, mp_obj_t **items); +void mp_obj_list_set_len(mp_obj_t self_in, uint len); void mp_obj_list_store(mp_obj_t self_in, mp_obj_t index, mp_obj_t value); -mp_obj_t mp_obj_list_sort(uint n_args, const mp_obj_t *args, struct _mp_map_t *kwargs); - -// map (the python builtin, not the dict implementation detail) -extern const mp_obj_type_t map_type; - -// enumerate -extern const mp_obj_type_t enumerate_type; - -// filter -extern const mp_obj_type_t filter_type; +mp_obj_t mp_obj_list_sort(uint n_args, const mp_obj_t *args, mp_map_t *kwargs); // dict -extern const mp_obj_type_t dict_type; +typedef struct _mp_obj_dict_t { + mp_obj_base_t base; + mp_map_t map; +} mp_obj_dict_t; +void mp_obj_dict_init(mp_obj_dict_t *dict, int n_args); uint mp_obj_dict_len(mp_obj_t self_in); mp_obj_t mp_obj_dict_store(mp_obj_t self_in, mp_obj_t key, mp_obj_t value); -struct _mp_map_t *mp_obj_dict_get_map(mp_obj_t self_in); +mp_obj_t mp_obj_dict_delete(mp_obj_t self_in, mp_obj_t key); +mp_map_t *mp_obj_dict_get_map(mp_obj_t self_in); // set -extern const mp_obj_type_t set_type; void mp_obj_set_store(mp_obj_t self_in, mp_obj_t item); // slice -extern const mp_obj_type_t slice_type; void mp_obj_slice_get(mp_obj_t self_in, machine_int_t *start, machine_int_t *stop, machine_int_t *step); -// zip -extern const mp_obj_type_t zip_type; - // array -extern const mp_obj_type_t mp_type_array; uint mp_obj_array_len(mp_obj_t self_in); mp_obj_t mp_obj_new_bytearray_by_ref(uint n, void *items); @@ -402,33 +483,22 @@ typedef struct _mp_obj_fun_native_t { // need this so we can define const object // such functions won't be able to access the global scope, but that's probably okay } mp_obj_fun_native_t; -extern const mp_obj_type_t fun_native_type; -extern const mp_obj_type_t fun_bc_type; -void mp_obj_fun_bc_get(mp_obj_t self_in, int *n_args, uint *n_state, const byte **code); +void mp_obj_fun_bc_get(mp_obj_t self_in, int *n_args, const byte **code); +bool mp_obj_fun_prepare_simple_args(mp_obj_t self_in, uint n_args, uint n_kw, const mp_obj_t *args, + uint *out_args1_len, const mp_obj_t **out_args1, uint *out_args2_len, const mp_obj_t **out_args2); mp_obj_t mp_identity(mp_obj_t self); MP_DECLARE_CONST_FUN_OBJ(mp_identity_obj); -// super -extern const mp_obj_type_t super_type; - -// generator -extern const mp_obj_type_t gen_instance_type; - // module typedef struct _mp_obj_module_t { mp_obj_base_t base; qstr name; - struct _mp_map_t *globals; + mp_obj_dict_t *globals; } mp_obj_module_t; -extern const mp_obj_type_t mp_type_module; -struct _mp_map_t *mp_obj_module_get_globals(mp_obj_t self_in); +mp_obj_dict_t *mp_obj_module_get_globals(mp_obj_t self_in); // staticmethod and classmethod types; defined here so we can make const versions - -extern const mp_obj_type_t mp_type_staticmethod; -extern const mp_obj_type_t mp_type_classmethod; - // this structure is used for instances of both staticmethod and classmethod typedef struct _mp_obj_static_class_method_t { mp_obj_base_t base; @@ -439,7 +509,7 @@ typedef struct _mp_obj_static_class_method_t { void mp_seq_multiply(const void *items, uint item_sz, uint len, uint times, void *dest); bool m_seq_get_fast_slice_indexes(machine_uint_t len, mp_obj_t slice, machine_uint_t *begin, machine_uint_t *end); #define m_seq_copy(dest, src, len, item_t) memcpy(dest, src, len * sizeof(item_t)) -#define m_seq_cat(dest, src1, len1, src2, len2, item_t) { memcpy(dest, src1, len1 * sizeof(item_t)); memcpy(dest + len1, src2, len2 * sizeof(item_t)); } +#define m_seq_cat(dest, src1, len1, src2, len2, item_t) { memcpy(dest, src1, (len1) * sizeof(item_t)); memcpy(dest + (len1), src2, (len2) * sizeof(item_t)); } bool mp_seq_cmp_bytes(int op, const byte *data1, uint len1, const byte *data2, uint len2); bool mp_seq_cmp_objs(int op, const mp_obj_t *items1, uint len1, const mp_obj_t *items2, uint len2); mp_obj_t mp_seq_index_obj(const mp_obj_t *items, uint len, uint n_args, const mp_obj_t *args); diff --git a/include/MicroPython/qstrdefs.generated.h b/include/MicroPython/qstrdefs.generated.h index 9c14bdcc8..337e246fe 100644 --- a/include/MicroPython/qstrdefs.generated.h +++ b/include/MicroPython/qstrdefs.generated.h @@ -13,13 +13,18 @@ Q(__next__, (const byte*)"\x02\x73\x08\x00" "__next__") Q(__qualname__, (const byte*)"\x6b\x00\x0c\x00" "__qualname__") Q(__repl_print__, (const byte*)"\x00\xbb\x0e\x00" "__repl_print__") Q(__bool__, (const byte*)"\x2b\x65\x08\x00" "__bool__") +Q(__enter__, (const byte*)"\x6d\xba\x09\x00" "__enter__") +Q(__exit__, (const byte*)"\x45\xf8\x08\x00" "__exit__") Q(__len__, (const byte*)"\xe2\xb0\x07\x00" "__len__") +Q(__iter__, (const byte*)"\xcf\x32\x08\x00" "__iter__") Q(__getitem__, (const byte*)"\x26\x39\x0b\x00" "__getitem__") Q(__setitem__, (const byte*)"\x32\x3e\x0b\x00" "__setitem__") Q(__add__, (const byte*)"\xc4\x82\x07\x00" "__add__") Q(__sub__, (const byte*)"\x21\x09\x07\x00" "__sub__") Q(__repr__, (const byte*)"\x10\x0b\x08\x00" "__repr__") Q(__str__, (const byte*)"\xd0\xcd\x07\x00" "__str__") +Q(__getattr__, (const byte*)"\x40\xf8\x0b\x00" "__getattr__") +Q(__del__, (const byte*)"\x68\x37\x07\x00" "__del__") Q(micropython, (const byte*)"\x0b\x7c\x0b\x00" "micropython") Q(byte_code, (const byte*)"\x9d\x04\x09\x00" "byte_code") Q(native, (const byte*)"\x84\x0b\x06\x00" "native") @@ -70,6 +75,9 @@ Q(bool, (const byte*)"\xeb\x3c\x04\x00" "bool") Q(bytearray, (const byte*)"\x76\xa3\x09\x00" "bytearray") Q(bytes, (const byte*)"\x5c\xb2\x05\x00" "bytes") Q(callable, (const byte*)"\x0d\x70\x08\x00" "callable") +#if MICROPY_ENABLE_MOD_STRUCT +Q(calcsize, (const byte*)"\x4d\x38\x08\x00" "calcsize") +#endif Q(chr, (const byte*)"\xdc\x4c\x03\x00" "chr") Q(classmethod, (const byte*)"\xb4\x8c\x0b\x00" "classmethod") Q(collections, (const byte*)"\xe0\xc8\x0b\x00" "collections") @@ -82,19 +90,25 @@ Q(eval, (const byte*)"\x9b\xa6\x04\x00" "eval") Q(exec, (const byte*)"\x1e\xc0\x04\x00" "exec") Q(filter, (const byte*)"\x25\xbe\x06\x00" "filter") Q(float, (const byte*)"\x35\x44\x05\x00" "float") +Q(from_bytes, (const byte*)"\x35\x74\x0a\x00" "from_bytes") +Q(getattr, (const byte*)"\xc0\x17\x07\x00" "getattr") +Q(globals, (const byte*)"\x9d\x49\x07\x00" "globals") Q(hash, (const byte*)"\xb7\x70\x04\x00" "hash") Q(id, (const byte*)"\x28\x6f\x02\x00" "id") +Q(io, (const byte*)"\x23\x6f\x02\x00" "io") Q(int, (const byte*)"\x16\x53\x03\x00" "int") Q(isinstance, (const byte*)"\xb6\xbe\x0a\x00" "isinstance") Q(issubclass, (const byte*)"\xb5\x7f\x0a\x00" "issubclass") Q(iter, (const byte*)"\x8f\x21\x04\x00" "iter") Q(len, (const byte*)"\x62\x40\x03\x00" "len") Q(list, (const byte*)"\x27\x1d\x04\x00" "list") +Q(locals, (const byte*)"\x3b\xa1\x06\x00" "locals") Q(map, (const byte*)"\xb9\x43\x03\x00" "map") Q(max, (const byte*)"\xb1\x43\x03\x00" "max") Q(min, (const byte*)"\xaf\x42\x03\x00" "min") Q(namedtuple, (const byte*)"\x1e\x16\x0a\x00" "namedtuple") Q(next, (const byte*)"\x42\x88\x04\x00" "next") +Q(open, (const byte*)"\xd1\x3a\x04\x00" "open") Q(ord, (const byte*)"\x1c\x5e\x03\x00" "ord") Q(path, (const byte*)"\x88\xce\x04\x00" "path") Q(pow, (const byte*)"\x2d\x73\x03\x00" "pow") @@ -107,19 +121,66 @@ Q(staticmethod, (const byte*)"\x62\xaf\x0c\x00" "staticmethod") Q(sum, (const byte*)"\x2e\x8d\x03\x00" "sum") Q(super, (const byte*)"\xc4\xb2\x05\x00" "super") Q(str, (const byte*)"\x50\x8d\x03\x00" "str") +#if MICROPY_ENABLE_MOD_STRUCT +Q(struct, (const byte*)"\x12\x90\x06\x00" "struct") +#endif Q(sys, (const byte*)"\xbc\x8e\x03\x00" "sys") +Q(to_bytes, (const byte*)"\xd8\x3e\x08\x00" "to_bytes") Q(tuple, (const byte*)"\xfd\x41\x05\x00" "tuple") Q(type, (const byte*)"\x9d\x7f\x04\x00" "type") +#if MICROPY_ENABLE_MOD_STRUCT +Q(unpack, (const byte*)"\x07\x3c\x06\x00" "unpack") +#endif Q(value, (const byte*)"\x4e\x34\x05\x00" "value") Q(zip, (const byte*)"\xe6\xac\x03\x00" "zip") -Q(append, (const byte*)"\x6b\x97\x06\x00" "append") +Q(sep, (const byte*)"\x23\x8f\x03\x00" "sep") +Q(end, (const byte*)"\x0a\x23\x03\x00" "end") +Q(clear, (const byte*)"\x7c\xa0\x05\x00" "clear") +Q(copy, (const byte*)"\xe0\xdb\x04\x00" "copy") +Q(fromkeys, (const byte*)"\x37\xbd\x08\x00" "fromkeys") +Q(get, (const byte*)"\x33\x3b\x03\x00" "get") +Q(items, (const byte*)"\xe3\x53\x05\x00" "items") +Q(keys, (const byte*)"\x01\x13\x04\x00" "keys") Q(pop, (const byte*)"\x2a\x73\x03\x00" "pop") +Q(popitem, (const byte*)"\xbf\x2c\x07\x00" "popitem") +Q(setdefault, (const byte*)"\x6c\xa3\x0a\x00" "setdefault") +Q(update, (const byte*)"\xb4\x76\x06\x00" "update") +Q(values, (const byte*)"\x7d\xbe\x06\x00" "values") +Q(append, (const byte*)"\x6b\x97\x06\x00" "append") +Q(close, (const byte*)"\x33\x67\x05\x00" "close") +Q(send, (const byte*)"\xb9\x76\x04\x00" "send") +Q(throw, (const byte*)"\xb3\x44\x05\x00" "throw") +Q(count, (const byte*)"\xa6\x4d\x05\x00" "count") +Q(extend, (const byte*)"\x63\xe8\x06\x00" "extend") +Q(index, (const byte*)"\x7b\x28\x05\x00" "index") +Q(remove, (const byte*)"\x63\x8a\x06\x00" "remove") +Q(insert, (const byte*)"\x12\x54\x06\x00" "insert") Q(sort, (const byte*)"\xbf\x9d\x04\x00" "sort") Q(join, (const byte*)"\xa7\x5c\x04\x00" "join") Q(strip, (const byte*)"\x29\x1e\x05\x00" "strip") Q(format, (const byte*)"\x26\x33\x06\x00" "format") Q(key, (const byte*)"\x32\x6d\x03\x00" "key") Q(reverse, (const byte*)"\x25\x2a\x07\x00" "reverse") +Q(add, (const byte*)"\x44\x32\x03\x00" "add") +Q(discard, (const byte*)"\x0f\x71\x07\x00" "discard") +Q(difference, (const byte*)"\x72\x24\x0a\x00" "difference") +Q(difference_update, (const byte*)"\x9c\xfa\x11\x00" "difference_update") +Q(intersection, (const byte*)"\x28\x2a\x0c\x00" "intersection") +Q(intersection_update, (const byte*)"\x06\xdd\x13\x00" "intersection_update") +Q(isdisjoint, (const byte*)"\xf7\x68\x0a\x00" "isdisjoint") +Q(issubset, (const byte*)"\xb9\xc1\x08\x00" "issubset") +Q(issuperset, (const byte*)"\xfc\xec\x0a\x00" "issuperset") +Q(symmetric_difference, (const byte*)"\xce\x67\x14\x00" "symmetric_difference") +Q(symmetric_difference_update, (const byte*)"\x60\xf8\x1b\x00" "symmetric_difference_update") +Q(union, (const byte*)"\xf6\x7c\x05\x00" "union") +Q(find, (const byte*)"\x00\x34\x04\x00" "find") +Q(rfind, (const byte*)"\xd2\x9c\x05\x00" "rfind") +Q(rindex, (const byte*)"\xe9\x2b\x06\x00" "rindex") +Q(split, (const byte*)"\xb7\x33\x05\x00" "split") +Q(startswith, (const byte*)"\x74\xe8\x0a\x00" "startswith") +Q(replace, (const byte*)"\x49\x25\x07\x00" "replace") +Q(partition, (const byte*)"\x87\xe5\x09\x00" "partition") +Q(rpartition, (const byte*)"\x15\xd0\x0a\x00" "rpartition") Q(bound_method, (const byte*)"\x97\xa2\x0c\x00" "bound_method") Q(closure, (const byte*)"\x74\xca\x07\x00" "closure") Q(dict_view, (const byte*)"\x2d\xa9\x09\x00" "dict_view") @@ -206,12 +267,13 @@ Q(LCD, (const byte*)"\xce\xdc\x03\x00" "LCD") Q(Servo, (const byte*)"\x98\xd9\x05\x00" "Servo") Q(SD, (const byte*)"\xf2\x74\x02\x00" "SD") Q(SDcard, (const byte*)"\xc6\x0b\x06\x00" "SDcard") +Q(power, (const byte*)"\xda\xed\x05\x00" "power") +Q(present, (const byte*)"\xee\x18\x07\x00" "present") Q(I2C, (const byte*)"\x5d\xdf\x03\x00" "I2C") Q(Usart, (const byte*)"\xc4\xc9\x05\x00" "Usart") Q(ADC, (const byte*)"\x63\xb6\x03\x00" "ADC") Q(ADC_all, (const byte*)"\xfd\x74\x07\x00" "ADC_all") Q(Audio, (const byte*)"\x73\x12\x05\x00" "Audio") -Q(open, (const byte*)"\xd1\x3a\x04\x00" "open") Q(File, (const byte*)"\xa3\x97\x04\x00" "File") Q(0_colon__slash_, (const byte*)"\xc0\x69\x03\x00" "0:/") Q(0_colon__slash_src, (const byte*)"\xe2\xe4\x06\x00" "0:/src") @@ -229,9 +291,23 @@ Q(PULL_UP, (const byte*)"\xba\x5e\x07\x00" "PULL_UP") Q(PULL_DOWN, (const byte*)"\xad\xfb\x09\x00" "PULL_DOWN") Q(PUSH_PULL, (const byte*)"\x81\x5e\x09\x00" "PUSH_PULL") Q(OPEN_DRAIN, (const byte*)"\x5e\x48\x0a\x00" "OPEN_DRAIN") +Q(image, (const byte*)"\x42\xa0\x05\x00" "image") Q(Image, (const byte*)"\x62\xa0\x05\x00" "Image") Q(Surf, (const byte*)"\x17\xb7\x04\x00" "Surf") Q(Cascade, (const byte*)"\xf7\x46\x07\x00" "Cascade") +Q(save, (const byte*)"\xa4\x83\x04\x00" "save") +Q(blit, (const byte*)"\xf6\x50\x04\x00" "blit") +Q(histeq, (const byte*)"\x57\x90\x06\x00" "histeq") +Q(median, (const byte*)"\x0f\x95\x06\x00" "median") +Q(threshold, (const byte*)"\xf2\x2f\x09\x00" "threshold") +Q(draw_circle, (const byte*)"\x68\x49\x0b\x00" "draw_circle") +Q(draw_rectangle, (const byte*)"\x5b\x40\x0e\x00" "draw_rectangle") +Q(draw_keypoints, (const byte*)"\x32\xb7\x0e\x00" "draw_keypoints") +Q(find_blobs, (const byte*)"\x0f\xb9\x0a\x00" "find_blobs") +Q(find_template, (const byte*)"\x2f\x7f\x0d\x00" "find_template") +Q(find_features, (const byte*)"\xf8\x12\x0d\x00" "find_features") +Q(find_keypoints, (const byte*)"\x57\xb6\x0e\x00" "find_keypoints") +Q(find_keypoints_match, (const byte*)"\xdb\xd7\x14\x00" "find_keypoints_match") Q(led, (const byte*)"\x68\x40\x03\x00" "led") Q(RED, (const byte*)"\x96\x06\x03\x00" "RED") Q(GREEN, (const byte*)"\xde\x98\x05\x00" "GREEN") @@ -244,6 +320,9 @@ Q(ticks, (const byte*)"\x43\x08\x05\x00" "ticks") Q(clock, (const byte*)"\x2d\x65\x05\x00" "clock") Q(sleep, (const byte*)"\xea\x27\x05\x00" "sleep") Q(Clock, (const byte*)"\x4d\x86\x05\x00" "Clock") +Q(tick, (const byte*)"\x30\xc2\x04\x00" "tick") +Q(fps, (const byte*)"\xc0\x38\x03\x00" "fps") +Q(avg, (const byte*)"\x15\x34\x03\x00" "avg") Q(sensor, (const byte*)"\x53\xcc\x06\x00" "sensor") Q(RGB565, (const byte*)"\x64\xcc\x06\x00" "RGB565") Q(YUV422, (const byte*)"\x0b\xfa\x06\x00" "YUV422") @@ -274,7 +353,10 @@ Q(IN, (const byte*)"\x22\x73\x02\x00" "IN") Q(OUT, (const byte*)"\x0b\xe3\x03\x00" "OUT") Q(gpio, (const byte*)"\x54\xd8\x04\x00" "gpio") Q(GPIO, (const byte*)"\x14\xe0\x04\x00" "GPIO") +Q(low, (const byte*)"\x31\x3f\x03\x00" "low") +Q(high, (const byte*)"\x2b\x4c\x04\x00" "high") Q(spi, (const byte*)"\xcf\x8d\x03\x00" "spi") Q(read, (const byte*)"\xb7\xf9\x04\x00" "read") Q(write, (const byte*)"\x98\xa8\x05\x00" "write") Q(write_image, (const byte*)"\xc0\xef\x0b\x00" "write_image") +Q(file, (const byte*)"\xc3\x34\x04\x00" "file") diff --git a/include/MicroPython/repl.h b/include/MicroPython/repl.h index 23259fa90..cba77aad0 100644 --- a/include/MicroPython/repl.h +++ b/include/MicroPython/repl.h @@ -1,3 +1,3 @@ #if MICROPY_ENABLE_REPL_HELPERS -bool mp_repl_is_compound_stmt(const char *line); +bool mp_repl_continue_with_input(const char *input); #endif diff --git a/include/MicroPython/runtime.h b/include/MicroPython/runtime.h index 3980e50cc..867d63352 100644 --- a/include/MicroPython/runtime.h +++ b/include/MicroPython/runtime.h @@ -1,54 +1,67 @@ -void rt_check_nargs(int n_args, machine_uint_t n_args_min, machine_uint_t n_args_max, int n_kw, bool is_kw); +typedef enum { + MP_VM_RETURN_NORMAL, + MP_VM_RETURN_YIELD, + MP_VM_RETURN_EXCEPTION, +} mp_vm_return_kind_t; -int rt_is_true(mp_obj_t arg); +void mp_init(void); +void mp_deinit(void); -mp_obj_t rt_load_const_dec(qstr qstr); -mp_obj_t rt_load_const_str(qstr qstr); -mp_obj_t rt_load_const_bytes(qstr qstr); -mp_obj_t rt_load_name(qstr qstr); -mp_obj_t rt_load_global(qstr qstr); -mp_obj_t rt_load_build_class(void); -mp_obj_t rt_get_cell(mp_obj_t cell); -void rt_set_cell(mp_obj_t cell, mp_obj_t val); -void rt_store_name(qstr qstr, mp_obj_t obj); -void rt_store_global(qstr qstr, mp_obj_t obj); -void rt_delete_name(qstr qstr); -mp_obj_t rt_unary_op(int op, mp_obj_t arg); -mp_obj_t rt_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs); -mp_obj_t rt_make_function_from_id(int unique_code_id, mp_obj_t def_args); -mp_obj_t rt_make_function_n(int n_args, void *fun); // fun must have the correct signature for n_args fixed arguments -mp_obj_t rt_make_function_var(int n_args_min, mp_fun_var_t fun); -mp_obj_t rt_make_function_var_between(int n_args_min, int n_args_max, mp_fun_var_t fun); // min and max are inclusive -mp_obj_t rt_make_closure_from_id(int unique_code_id, mp_obj_t closure_tuple); -mp_obj_t rt_call_function_0(mp_obj_t fun); -mp_obj_t rt_call_function_1(mp_obj_t fun, mp_obj_t arg); -mp_obj_t rt_call_function_2(mp_obj_t fun, mp_obj_t arg1, mp_obj_t arg2); -mp_obj_t rt_call_function_n_kw_for_native(mp_obj_t fun_in, uint n_args_kw, const mp_obj_t *args); -mp_obj_t rt_call_function_n_kw(mp_obj_t fun, uint n_args, uint n_kw, const mp_obj_t *args); -mp_obj_t rt_call_method_n_kw(uint n_args, uint n_kw, const mp_obj_t *args); -mp_obj_t rt_build_tuple(int n_args, mp_obj_t *items); -mp_obj_t rt_build_list(int n_args, mp_obj_t *items); -mp_obj_t rt_list_append(mp_obj_t list, mp_obj_t arg); -mp_obj_t rt_build_set(int n_args, mp_obj_t *items); -mp_obj_t rt_store_set(mp_obj_t set, mp_obj_t item); -void rt_unpack_sequence(mp_obj_t seq, uint num, mp_obj_t *items); -mp_obj_t rt_build_map(int n_args); -mp_obj_t rt_store_map(mp_obj_t map, mp_obj_t key, mp_obj_t value); -mp_obj_t rt_load_attr(mp_obj_t base, qstr attr); -void rt_load_method(mp_obj_t base, qstr attr, mp_obj_t *dest); -void rt_store_attr(mp_obj_t base, qstr attr, mp_obj_t val); -void rt_store_subscr(mp_obj_t base, mp_obj_t index, mp_obj_t val); -mp_obj_t rt_getiter(mp_obj_t o); -mp_obj_t rt_iternext(mp_obj_t o); -mp_obj_t rt_make_raise_obj(mp_obj_t o); -mp_obj_t rt_import_name(qstr name, mp_obj_t fromlist, mp_obj_t level); -mp_obj_t rt_import_from(mp_obj_t module, qstr name); -void rt_import_all(mp_obj_t module); +void mp_check_nargs(int n_args, machine_uint_t n_args_min, machine_uint_t n_args_max, int n_kw, bool is_kw); -struct _mp_map_t; -struct _mp_map_t *rt_locals_get(void); -void rt_locals_set(struct _mp_map_t *m); -struct _mp_map_t *rt_globals_get(void); -void rt_globals_set(struct _mp_map_t *m); -struct _mp_map_t *rt_loaded_modules_get(void); -extern mp_obj_t sys_path; +mp_obj_dict_t *mp_locals_get(void); +void mp_locals_set(mp_obj_dict_t *d); +mp_obj_dict_t *mp_globals_get(void); +void mp_globals_set(mp_obj_dict_t *d); + +mp_obj_t mp_load_name(qstr qstr); +mp_obj_t mp_load_global(qstr qstr); +mp_obj_t mp_load_build_class(void); +void mp_store_name(qstr qstr, mp_obj_t obj); +void mp_store_global(qstr qstr, mp_obj_t obj); +void mp_delete_name(qstr qstr); +void mp_delete_global(qstr qstr); + +mp_obj_t mp_unary_op(int op, mp_obj_t arg); +mp_obj_t mp_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs); + +mp_obj_t mp_load_const_dec(qstr qstr); +mp_obj_t mp_load_const_str(qstr qstr); +mp_obj_t mp_load_const_bytes(qstr qstr); + +mp_obj_t mp_make_function_from_id(uint unique_code_id, mp_obj_t def_args, mp_obj_t def_kw_args); +mp_obj_t mp_make_function_from_id_and_free(uint unique_code_id, mp_obj_t def_args, mp_obj_t def_kw_args); +mp_obj_t mp_make_function_n(int n_args, void *fun); // fun must have the correct signature for n_args fixed arguments +mp_obj_t mp_make_function_var(int n_args_min, mp_fun_var_t fun); +mp_obj_t mp_make_function_var_between(int n_args_min, int n_args_max, mp_fun_var_t fun); // min and max are inclusive +mp_obj_t mp_make_closure_from_id(uint unique_code_id, mp_obj_t closure_tuple, mp_obj_t def_args, mp_obj_t def_kw_args); + +mp_obj_t mp_call_function_0(mp_obj_t fun); +mp_obj_t mp_call_function_1(mp_obj_t fun, mp_obj_t arg); +mp_obj_t mp_call_function_2(mp_obj_t fun, mp_obj_t arg1, mp_obj_t arg2); +mp_obj_t mp_call_function_n_kw_for_native(mp_obj_t fun_in, uint n_args_kw, const mp_obj_t *args); +mp_obj_t mp_call_function_n_kw(mp_obj_t fun, uint n_args, uint n_kw, const mp_obj_t *args); +mp_obj_t mp_call_method_n_kw(uint n_args, uint n_kw, const mp_obj_t *args); +mp_obj_t mp_call_method_n_kw_var(bool have_self, uint n_args_n_kw, const mp_obj_t *args); + +void mp_unpack_sequence(mp_obj_t seq, uint num, mp_obj_t *items); +void mp_unpack_ex(mp_obj_t seq, uint num, mp_obj_t *items); +mp_obj_t mp_store_map(mp_obj_t map, mp_obj_t key, mp_obj_t value); +mp_obj_t mp_load_attr(mp_obj_t base, qstr attr); +void mp_load_method(mp_obj_t base, qstr attr, mp_obj_t *dest); +void mp_load_method_maybe(mp_obj_t base, qstr attr, mp_obj_t *dest); +void mp_store_attr(mp_obj_t base, qstr attr, mp_obj_t val); +void mp_store_subscr(mp_obj_t base, mp_obj_t index, mp_obj_t val); + +mp_obj_t mp_getiter(mp_obj_t o); +mp_obj_t mp_iternext_allow_raise(mp_obj_t o); // may return MP_OBJ_NULL instead of raising StopIteration() +mp_obj_t mp_iternext(mp_obj_t o); // will always return MP_OBJ_NULL instead of raising StopIteration(...) +mp_vm_return_kind_t mp_resume(mp_obj_t self_in, mp_obj_t send_value, mp_obj_t throw_value, mp_obj_t *ret_val); + +mp_obj_t mp_make_raise_obj(mp_obj_t o); + +extern mp_obj_t mp_sys_path; +mp_map_t *mp_loaded_modules_get(void); +mp_obj_t mp_import_name(qstr name, mp_obj_t fromlist, mp_obj_t level); +mp_obj_t mp_import_from(mp_obj_t module, qstr name); +void mp_import_all(mp_obj_t module); diff --git a/include/MicroPython/runtime0.h b/include/MicroPython/runtime0.h index 07fcf0705..6ee70aa01 100644 --- a/include/MicroPython/runtime0.h +++ b/include/MicroPython/runtime0.h @@ -1,4 +1,5 @@ // taken from python source, Include/code.h +// These must fit in 8 bits; see scope.h #define MP_SCOPE_FLAG_OPTIMISED 0x01 #define MP_SCOPE_FLAG_NEWLOCALS 0x02 #define MP_SCOPE_FLAG_VARARGS 0x04 @@ -13,90 +14,89 @@ #define MP_SCOPE_FLAG_NOFREE 0x40 typedef enum { - RT_UNARY_OP_BOOL, // __bool__ - RT_UNARY_OP_LEN, // __len__ - RT_UNARY_OP_POSITIVE, - RT_UNARY_OP_NEGATIVE, - RT_UNARY_OP_INVERT, + MP_UNARY_OP_BOOL, // __bool__ + MP_UNARY_OP_LEN, // __len__ + MP_UNARY_OP_POSITIVE, + MP_UNARY_OP_NEGATIVE, + MP_UNARY_OP_INVERT, // these are not supported by the runtime and must be synthesised by the emitter - RT_UNARY_OP_NOT, -} rt_unary_op_t; + MP_UNARY_OP_NOT, +} mp_unary_op_t; typedef enum { - RT_BINARY_OP_SUBSCR, - RT_BINARY_OP_OR, - RT_BINARY_OP_XOR, - RT_BINARY_OP_AND, - RT_BINARY_OP_LSHIFT, - RT_BINARY_OP_RSHIFT, - RT_BINARY_OP_ADD, - RT_BINARY_OP_SUBTRACT, - RT_BINARY_OP_MULTIPLY, - RT_BINARY_OP_FLOOR_DIVIDE, - RT_BINARY_OP_TRUE_DIVIDE, - RT_BINARY_OP_MODULO, - RT_BINARY_OP_POWER, - RT_BINARY_OP_INPLACE_OR, - RT_BINARY_OP_INPLACE_XOR, - RT_BINARY_OP_INPLACE_AND, - RT_BINARY_OP_INPLACE_LSHIFT, - RT_BINARY_OP_INPLACE_RSHIFT, - RT_BINARY_OP_INPLACE_ADD, - RT_BINARY_OP_INPLACE_SUBTRACT, - RT_BINARY_OP_INPLACE_MULTIPLY, - RT_BINARY_OP_INPLACE_FLOOR_DIVIDE, - RT_BINARY_OP_INPLACE_TRUE_DIVIDE, - RT_BINARY_OP_INPLACE_MODULO, - RT_BINARY_OP_INPLACE_POWER, + MP_BINARY_OP_SUBSCR, + MP_BINARY_OP_OR, + MP_BINARY_OP_XOR, + MP_BINARY_OP_AND, + MP_BINARY_OP_LSHIFT, + MP_BINARY_OP_RSHIFT, + MP_BINARY_OP_ADD, + MP_BINARY_OP_SUBTRACT, + MP_BINARY_OP_MULTIPLY, + MP_BINARY_OP_FLOOR_DIVIDE, + MP_BINARY_OP_TRUE_DIVIDE, + MP_BINARY_OP_MODULO, + MP_BINARY_OP_POWER, + MP_BINARY_OP_INPLACE_OR, + MP_BINARY_OP_INPLACE_XOR, + MP_BINARY_OP_INPLACE_AND, + MP_BINARY_OP_INPLACE_LSHIFT, + MP_BINARY_OP_INPLACE_RSHIFT, + MP_BINARY_OP_INPLACE_ADD, + MP_BINARY_OP_INPLACE_SUBTRACT, + MP_BINARY_OP_INPLACE_MULTIPLY, + MP_BINARY_OP_INPLACE_FLOOR_DIVIDE, + MP_BINARY_OP_INPLACE_TRUE_DIVIDE, + MP_BINARY_OP_INPLACE_MODULO, + MP_BINARY_OP_INPLACE_POWER, // these should return a bool - RT_BINARY_OP_LESS, - RT_BINARY_OP_MORE, - RT_BINARY_OP_EQUAL, - RT_BINARY_OP_LESS_EQUAL, - RT_BINARY_OP_MORE_EQUAL, - RT_BINARY_OP_NOT_EQUAL, - RT_BINARY_OP_IN, - RT_BINARY_OP_IS, - RT_BINARY_OP_EXCEPTION_MATCH, + MP_BINARY_OP_LESS, + MP_BINARY_OP_MORE, + MP_BINARY_OP_EQUAL, + MP_BINARY_OP_LESS_EQUAL, + MP_BINARY_OP_MORE_EQUAL, + MP_BINARY_OP_NOT_EQUAL, + MP_BINARY_OP_IN, + MP_BINARY_OP_IS, + MP_BINARY_OP_EXCEPTION_MATCH, // these are not supported by the runtime and must be synthesised by the emitter - RT_BINARY_OP_NOT_IN, - RT_BINARY_OP_IS_NOT, -} rt_binary_op_t; + MP_BINARY_OP_NOT_IN, + MP_BINARY_OP_IS_NOT, +} mp_binary_op_t; typedef enum { - RT_F_LOAD_CONST_DEC = 0, - RT_F_LOAD_CONST_STR, - RT_F_LOAD_NAME, - RT_F_LOAD_GLOBAL, - RT_F_LOAD_BUILD_CLASS, - RT_F_LOAD_ATTR, - RT_F_LOAD_METHOD, - RT_F_STORE_NAME, - RT_F_STORE_ATTR, - RT_F_STORE_SUBSCR, - RT_F_IS_TRUE, - RT_F_UNARY_OP, - RT_F_BINARY_OP, - RT_F_BUILD_TUPLE, - RT_F_BUILD_LIST, - RT_F_LIST_APPEND, - RT_F_BUILD_MAP, - RT_F_STORE_MAP, - RT_F_BUILD_SET, - RT_F_STORE_SET, - RT_F_MAKE_FUNCTION_FROM_ID, - RT_F_CALL_FUNCTION_N_KW_FOR_NATIVE, - RT_F_CALL_METHOD_N_KW, - RT_F_GETITER, - RT_F_ITERNEXT, - RT_F_NUMBER_OF, -} rt_fun_kind_t; + MP_F_LOAD_CONST_DEC = 0, + MP_F_LOAD_CONST_INT, + MP_F_LOAD_CONST_STR, + MP_F_LOAD_NAME, + MP_F_LOAD_GLOBAL, + MP_F_LOAD_BUILD_CLASS, + MP_F_LOAD_ATTR, + MP_F_LOAD_METHOD, + MP_F_STORE_NAME, + MP_F_STORE_ATTR, + MP_F_STORE_SUBSCR, + MP_F_OBJ_IS_TRUE, + MP_F_UNARY_OP, + MP_F_BINARY_OP, + MP_F_BUILD_TUPLE, + MP_F_BUILD_LIST, + MP_F_LIST_APPEND, + MP_F_BUILD_MAP, + MP_F_STORE_MAP, + MP_F_BUILD_SET, + MP_F_STORE_SET, + MP_F_MAKE_FUNCTION_FROM_ID, + MP_F_CALL_FUNCTION_N_KW_FOR_NATIVE, + MP_F_CALL_METHOD_N_KW, + MP_F_GETITER, + MP_F_ITERNEXT, + MP_F_IMPORT_NAME, + MP_F_IMPORT_FROM, + MP_F_IMPORT_ALL, + MP_F_NEW_SLICE, + MP_F_UNPACK_SEQUENCE, + MP_F_NUMBER_OF, +} mp_fun_kind_t; -extern void *const rt_fun_table[RT_F_NUMBER_OF]; - -void rt_init(void); -void rt_deinit(void); -uint rt_get_unique_code_id(void); -void rt_assign_byte_code(uint unique_code_id, byte *code, uint len, int n_args, int n_locals, int n_stack, uint scope_flags, qstr *arg_names); -void rt_assign_native_code(uint unique_code_id, void *f, uint len, int n_args); -void rt_assign_inline_asm_code(uint unique_code_id, void *f, uint len, int n_args); +extern void *const mp_fun_table[MP_F_NUMBER_OF]; diff --git a/lib/libdsp.a b/lib/libdsp.a index dacd20ee5..057712a05 100644 Binary files a/lib/libdsp.a and b/lib/libdsp.a differ diff --git a/lib/libmp.a b/lib/libmp.a index 7a517a4bb..58b79246e 100644 Binary files a/lib/libmp.a and b/lib/libmp.a differ diff --git a/lib/libstdperiph.a b/lib/libstdperiph.a index d6c2fc898..fe66d55e5 100644 Binary files a/lib/libstdperiph.a and b/lib/libstdperiph.a differ