diff --git a/include/MicroPython/gc.h b/include/MicroPython/gc.h index 711212ac4..e5286cd86 100644 --- a/include/MicroPython/gc.h +++ b/include/MicroPython/gc.h @@ -18,3 +18,4 @@ typedef struct _gc_info_t { } gc_info_t; void gc_info(gc_info_t *info); +void gc_dump_info(void); diff --git a/include/MicroPython/gccollect.h b/include/MicroPython/gccollect.h index 9187f0e9d..a6471bf2e 100644 --- a/include/MicroPython/gccollect.h +++ b/include/MicroPython/gccollect.h @@ -1,7 +1,16 @@ +// variables defining memory layout +// (these probably belong somewhere else...) +extern uint32_t _text_end; +extern uint32_t _data_start_init; extern uint32_t _ram_start; +extern uint32_t _data_start; +extern uint32_t _data_end; +extern uint32_t _bss_start; +extern uint32_t _bss_end; extern uint32_t _heap_start; -extern uint32_t _ram_end; extern uint32_t _heap_end; +extern uint32_t _stack_end; +extern uint32_t _ram_end; void gc_collect(void); diff --git a/include/MicroPython/lexer.h b/include/MicroPython/lexer.h index 13fbfb5d3..f41c962e7 100644 --- a/include/MicroPython/lexer.h +++ b/include/MicroPython/lexer.h @@ -136,5 +136,17 @@ bool mp_lexer_is_kind(mp_lexer_t *lex, mp_token_kind_t kind); bool mp_lexer_show_error_pythonic_prefix(mp_lexer_t *lex); bool mp_lexer_show_error_pythonic(mp_lexer_t *lex, const char *msg); -// used to import a module; must be implemented for a specific port -mp_lexer_t *mp_import_open_file(qstr mod_name); +/******************************************************************/ +// platform specific import function; must be implemented for a specific port +// TODO tidy up, rename, or put elsewhere + +//mp_lexer_t *mp_import_open_file(qstr mod_name); + +typedef enum { + MP_IMPORT_STAT_NO_EXIST, + MP_IMPORT_STAT_DIR, + MP_IMPORT_STAT_FILE, +} mp_import_stat_t; + +mp_import_stat_t mp_import_stat(const char *path); +mp_lexer_t *mp_lexer_new_from_file(const char *filename); diff --git a/include/MicroPython/libmp.h b/include/MicroPython/libmp.h index 6110c71d3..3dc573795 100644 --- a/include/MicroPython/libmp.h +++ b/include/MicroPython/libmp.h @@ -2,17 +2,16 @@ #define __LIBMP_H__ #include #include -#include "std.h" #include "misc.h" -#include "ff.h" #include "mpconfig.h" #include "qstr.h" +#include "obj.h" +#include "std.h" #include "nlr.h" #include "misc.h" #include "lexer.h" #include "lexerfatfs.h" #include "parse.h" -#include "obj.h" #include "compile.h" #include "runtime0.h" #include "runtime.h" @@ -22,10 +21,14 @@ #include "storage.h" #include "usb.h" #include "systick.h" +#include "pyexec.h" +#include "pendsv.h" +#include "ff.h" int libmp_init(); void libmp_do_repl(void); +void libmp_int_repl(); bool libmp_do_file(const char *filename); +bool libmp_code_running(); vstr_t* libmp_get_line(); -void libmp_line_feed(); #endif /* __LIBMP_H__ */ diff --git a/include/MicroPython/misc.h b/include/MicroPython/misc.h index 52498c70b..989650c17 100644 --- a/include/MicroPython/misc.h +++ b/include/MicroPython/misc.h @@ -10,6 +10,15 @@ typedef unsigned char byte; typedef unsigned int uint; +/** generic ops *************************************************/ + +#ifndef MIN +#define MIN(x, y) ((x) < (y) ? (x) : (y)) +#endif +#ifndef MAX +#define MAX(x, y) ((x) > (y) ? (x) : (y)) +#endif + /** memomry allocation ******************************************/ // TODO make a lazy m_renew that can increase by a smaller amount than requested (but by at least 1 more element) @@ -59,10 +68,17 @@ typedef struct _vstr_t { int alloc; int len; char *buf; - bool had_error; + struct { + bool had_error : 1; + bool fixed_buf : 1; + }; } vstr_t; +// convenience macro to declare a vstr with a fixed size buffer on the stack +#define VSTR_FIXED(vstr, alloc) vstr_t vstr; char vstr##_buf[(alloc)]; vstr_init_fixed_buf(&vstr, (alloc), vstr##_buf); + void vstr_init(vstr_t *vstr, int alloc); +void vstr_init_fixed_buf(vstr_t *vstr, int alloc, char *buf); void vstr_clear(vstr_t *vstr); vstr_t *vstr_new(void); vstr_t *vstr_new_size(int alloc); @@ -72,7 +88,7 @@ bool vstr_had_error(vstr_t *vstr); char *vstr_str(vstr_t *vstr); int vstr_len(vstr_t *vstr); void vstr_hint_size(vstr_t *vstr, int size); -char *vstr_extend(vstr_t *vstr, int size); +char *vstr_extend(vstr_t *vstr, int size); bool vstr_set_size(vstr_t *vstr, int size); bool vstr_shrink(vstr_t *vstr); char *vstr_add_len(vstr_t *vstr, int len); @@ -85,8 +101,23 @@ void vstr_add_strn(vstr_t *vstr, const char *str, int len); void vstr_cut_tail(vstr_t *vstr, int len); void vstr_printf(vstr_t *vstr, const char *fmt, ...); +/** non-dynamic size-bounded variable buffer/string *************/ + +#define CHECKBUF(buf, max_size) char buf[max_size + 1]; uint buf##_len = max_size; char *buf##_p = buf; +#define CHECKBUF_RESET(buf, max_size) buf##_len = max_size; buf##_p = buf; +#define CHECKBUF_APPEND(buf, src, src_len) \ + { int l = MIN(src_len, buf##_len); \ + memcpy(buf##_p, src, l); \ + buf##_len -= l; \ + buf##_p += l; } +#define CHECKBUF_APPEND_0(buf) { *buf##_p = 0; } +#define CHECKBUF_LEN(buf) (buf##_p - buf) + #ifdef va_start void vstr_vprintf(vstr_t *vstr, const char *fmt, va_list ap); #endif +// Debugging helpers +int DEBUG_printf(const char *fmt, ...); + #endif // _INCLUDED_MINILIB_H diff --git a/include/MicroPython/mpconfig.h b/include/MicroPython/mpconfig.h index 38b74b733..00e2439e4 100644 --- a/include/MicroPython/mpconfig.h +++ b/include/MicroPython/mpconfig.h @@ -40,6 +40,7 @@ #endif // Whether to build functions that print debugging info: +// mp_token_show // mp_byte_code_print // mp_parse_node_print #ifndef MICROPY_DEBUG_PRINTERS @@ -101,9 +102,25 @@ typedef long long mp_longint_impl_t; #define MICROPY_CPYTHON_COMPAT (1) #endif +// Maximum length of a path in the filesystem +// So we can allocate a buffer on the stack for path manipulation in import +#ifndef MICROPY_PATH_MAX +#define MICROPY_PATH_MAX (512) +#endif + +// Additional builtin function definitions - see runtime.c:builtin_table for format. +#ifndef MICROPY_EXTRA_BUILTINS +#define MICROPY_EXTRA_BUILTINS +#endif /*****************************************************************************/ /* Miscellaneous settings */ +// Allow to override static modifier for global objects, e.g. to use with +// object code analysis tools which don't support static symbols. +#ifndef STATIC +#define STATIC static +#endif + #define BITS_PER_BYTE (8) #define BITS_PER_WORD (BITS_PER_BYTE * BYTES_PER_WORD) // machine_int_t value with most significant bit set diff --git a/include/MicroPython/mpconfigboard.h b/include/MicroPython/mpconfigboard.h new file mode 100644 index 000000000..44ae6fd0b --- /dev/null +++ b/include/MicroPython/mpconfigboard.h @@ -0,0 +1,43 @@ +#define STM32F4DISC + +#define MICROPY_HW_BOARD_NAME "F4DISC" + +#define MICROPY_HW_HAS_SWITCH (0) +#define MICROPY_HW_HAS_SDCARD (0) +#define MICROPY_HW_HAS_MMA7660 (0) +#define MICROPY_HW_HAS_LIS3DSH (0) +#define MICROPY_HW_HAS_LCD (0) +#define MICROPY_HW_HAS_WLAN (0) +#define MICROPY_HW_ENABLE_RNG (0) +#define MICROPY_HW_ENABLE_RTC (0) +#define MICROPY_HW_ENABLE_TIMER (0) +#define MICROPY_HW_ENABLE_SERVO (0) +#define MICROPY_HW_ENABLE_AUDIO (0) + +#define USRSW_PORT (GPIOA) +#define USRSW_PIN (GPIO_Pin_0) +#define USRSW_PUPD (GPIO_PuPd_NOPULL) +#define USRSW_EXTI_PIN (EXTI_PinSource0) +#define USRSW_EXTI_PORT (EXTI_PortSourceGPIOA) +#define USRSW_EXTI_LINE (EXTI_Line0) +#define USRSW_EXTI_IRQN (EXTI0_IRQn) +#define USRSW_EXTI_EDGE (EXTI_Trigger_Falling) + +/* LED */ +#define PYB_LED1_PORT (GPIOD) +#define PYB_LED1_PIN (GPIO_Pin_14) + +#define PYB_LED2_PORT (GPIOD) +#define PYB_LED2_PIN (GPIO_Pin_12) + +#define PYB_LED3_PORT (GPIOD) +#define PYB_LED3_PIN (GPIO_Pin_15) + +#define PYB_LED4_PORT (GPIOD) +#define PYB_LED4_PIN (GPIO_Pin_13) + +#define PYB_OTYPE (GPIO_OType_PP) + +#define PYB_LED_ON(port, pin) (port->BSRRL = pin) +#define PYB_LED_OFF(port, pin) (port->BSRRH = pin) + diff --git a/include/MicroPython/mpconfigport.h b/include/MicroPython/mpconfigport.h index ee6c2fead..1699ef7d0 100644 --- a/include/MicroPython/mpconfigport.h +++ b/include/MicroPython/mpconfigport.h @@ -7,11 +7,15 @@ #define MICROPY_ENABLE_GC (1) #define MICROPY_ENABLE_REPL_HELPERS (1) #define MICROPY_ENABLE_FLOAT (1) +#define MICROPY_PATH_MAX (128) // type definitions for the specific machine #define BYTES_PER_WORD (4) +#define UINT_FMT "%lu" +#define INT_FMT "%ld" + typedef int32_t machine_int_t; // must be pointer size typedef uint32_t machine_uint_t; // must be pointer size typedef void *machine_ptr_t; // must be of pointer size @@ -20,146 +24,26 @@ typedef float machine_float_t; machine_float_t machine_sqrt(machine_float_t x); +// There is no classical C heap in bare-metal ports, only Python +// garbage-collected heap. For completeness, emulate C heap via +// GC heap. Note that MicroPython core never uses malloc() and friends, +// so these defines are mostly to help extension module writers. +#define malloc gc_alloc +#define free gc_free +#define realloc gc_realloc + // board specific definitions -// choose 1 of these boards -//#define PYBOARD3 -//#define PYBOARD4 -#define STM32F4DISC - -#if defined (PYBOARD3) - #define MICROPY_HW_BOARD_NAME "PYBv3" - - #define MICROPY_HW_HAS_SWITCH (1) - #define MICROPY_HW_HAS_SDCARD (1) - #define MICROPY_HW_HAS_MMA7660 (1) - #define MICROPY_HW_HAS_LIS3DSH (0) - #define MICROPY_HW_HAS_LCD (0) - #define MICROPY_HW_HAS_WLAN (0) - #define MICROPY_HW_ENABLE_RNG (1) - #define MICROPY_HW_ENABLE_RTC (1) - #define MICROPY_HW_ENABLE_TIMER (1) - #define MICROPY_HW_ENABLE_SERVO (1) - #define MICROPY_HW_ENABLE_AUDIO (0) - - #define USRSW_PORT (GPIOA) - #define USRSW_PIN (GPIO_Pin_13) - #define USRSW_PUPD (GPIO_PuPd_UP) - #define USRSW_EXTI_PIN (EXTI_PinSource13) - #define USRSW_EXTI_PORT (EXTI_PortSourceGPIOA) - #define USRSW_EXTI_LINE (EXTI_Line13) - #define USRSW_EXTI_IRQN (EXTI15_10_IRQn) - #define USRSW_EXTI_EDGE (EXTI_Trigger_Rising) - - /* LED */ - #define PYB_LED1_PORT (GPIOA) - #define PYB_LED1_PIN (GPIO_Pin_8) - - #define PYB_LED2_PORT (GPIOA) - #define PYB_LED2_PIN (GPIO_Pin_10) - - #define PYB_LED3_PORT (GPIOC) - #define PYB_LED3_PIN (GPIO_Pin_4) - - #define PYB_LED4_PORT (GPIOC) - #define PYB_LED4_PIN (GPIO_Pin_5) - - #define PYB_OTYPE (GPIO_OType_OD) - - #define PYB_LED_ON(port, pin) (port->BSRRH = pin) - #define PYB_LED_OFF(port, pin) (port->BSRRL = pin) - -#elif defined (PYBOARD4) - #define MICROPY_HW_BOARD_NAME "PYBv4" - - #define MICROPY_HW_HAS_SWITCH (1) - #define MICROPY_HW_HAS_SDCARD (1) - #define MICROPY_HW_HAS_MMA7660 (1) - #define MICROPY_HW_HAS_LIS3DSH (0) - #define MICROPY_HW_HAS_LCD (1) - #define MICROPY_HW_HAS_WLAN (0) - #define MICROPY_HW_ENABLE_RNG (1) - #define MICROPY_HW_ENABLE_RTC (1) - #define MICROPY_HW_ENABLE_TIMER (1) - #define MICROPY_HW_ENABLE_SERVO (1) - #define MICROPY_HW_ENABLE_AUDIO (0) - - #define USRSW_PORT (GPIOB) - #define USRSW_PIN (GPIO_Pin_3) - #define USRSW_PUPD (GPIO_PuPd_UP) - #define USRSW_EXTI_PIN (EXTI_PinSource3) - #define USRSW_EXTI_PORT (EXTI_PortSourceGPIOB) - #define USRSW_EXTI_LINE (EXTI_Line3) - #define USRSW_EXTI_IRQN (EXTI15_10_IRQn) - #define USRSW_EXTI_EDGE (EXTI_Trigger_Rising) - - /* LED */ - #define PYB_LED1_PORT (GPIOA) - #define PYB_LED1_PIN (GPIO_Pin_13) - - #define PYB_LED2_PORT (GPIOA) - #define PYB_LED2_PIN (GPIO_Pin_14) - - #define PYB_LED3_PORT (GPIOA) - #define PYB_LED3_PIN (GPIO_Pin_15) - - #define PYB_LED4_PORT (GPIOB) - #define PYB_LED4_PIN (GPIO_Pin_4) - - #define PYB_OTYPE (GPIO_OType_PP) - - #define PYB_LED_ON(port, pin) (port->BSRRL = pin) - #define PYB_LED_OFF(port, pin) (port->BSRRH = pin) - -#elif defined (STM32F4DISC) - #define MICROPY_HW_BOARD_NAME "F4DISC" - - #define MICROPY_HW_HAS_SWITCH (0) - #define MICROPY_HW_HAS_SDCARD (0) - #define MICROPY_HW_HAS_MMA7660 (0) - #define MICROPY_HW_HAS_LIS3DSH (0) - #define MICROPY_HW_HAS_LCD (0) - #define MICROPY_HW_HAS_WLAN (0) - #define MICROPY_HW_ENABLE_RNG (0) - #define MICROPY_HW_ENABLE_RTC (0) - #define MICROPY_HW_ENABLE_TIMER (0) - #define MICROPY_HW_ENABLE_SERVO (0) - #define MICROPY_HW_ENABLE_AUDIO (0) - - #define USRSW_PORT (GPIOA) - #define USRSW_PIN (GPIO_Pin_0) - #define USRSW_PUPD (GPIO_PuPd_NOPULL) - #define USRSW_EXTI_PIN (EXTI_PinSource0) - #define USRSW_EXTI_PORT (EXTI_PortSourceGPIOA) - #define USRSW_EXTI_LINE (EXTI_Line0) - #define USRSW_EXTI_IRQN (EXTI0_IRQn) - #define USRSW_EXTI_EDGE (EXTI_Trigger_Falling) - - /* LED */ - #define PYB_LED1_PORT (GPIOD) - #define PYB_LED1_PIN (GPIO_Pin_14) - - #define PYB_LED2_PORT (GPIOD) - #define PYB_LED2_PIN (GPIO_Pin_12) - - #define PYB_LED3_PORT (GPIOD) - #define PYB_LED3_PIN (GPIO_Pin_15) - - #define PYB_LED4_PORT (GPIOD) - #define PYB_LED4_PIN (GPIO_Pin_13) - - #define PYB_OTYPE (GPIO_OType_PP) - - #define PYB_LED_ON(port, pin) (port->BSRRL = pin) - #define PYB_LED_OFF(port, pin) (port->BSRRH = pin) - -#endif +#include "mpconfigboard.h" //#define STM32F40_41xxx //#define USE_STDPERIPH_DRIVER +#if !defined(HSE_VALUE) //#define HSE_VALUE (8000000) +#endif #define USE_DEVICE_MODE //#define USE_HOST_MODE #define sys_tick_counter systick_current_millis() #define sys_tick_has_passed systick_has_passed +#define sys_tick_delay_ms systick_sleep diff --git a/include/MicroPython/obj.h b/include/MicroPython/obj.h index 0ba4ae1b5..c2b127c32 100644 --- a/include/MicroPython/obj.h +++ b/include/MicroPython/obj.h @@ -66,11 +66,11 @@ typedef struct _mp_obj_base_t mp_obj_base_t; // 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 -#define MP_DECLARE_CONST_STATICMETHOD_OBJ(obj_name) extern const mp_obj_staticmethod_t obj_name -#define MP_DECLARE_CONST_CLASSMETHOD_OBJ(obj_name) extern const mp_obj_classmethod_t obj_name +#define MP_DECLARE_CONST_STATICMETHOD_OBJ(obj_name) extern const mp_obj_static_class_method_t obj_name +#define MP_DECLARE_CONST_CLASSMETHOD_OBJ(obj_name) extern const mp_obj_static_class_method_t obj_name -#define MP_DEFINE_CONST_STATICMETHOD_OBJ(obj_name, fun_name) const mp_obj_staticmethod_t obj_name = {{&mp_type_staticmethod}, fun_name} -#define MP_DEFINE_CONST_CLASSMETHOD_OBJ(obj_name, fun_name) const mp_obj_classmethod_t obj_name = {{&mp_type_classmethod}, fun_name} +#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; @@ -142,7 +142,7 @@ typedef struct _mp_stream_p_t { struct _mp_obj_type_t { mp_obj_base_t base; - const char *name; + qstr name; mp_print_fun_t print; mp_make_new_fun_t make_new; // to make an instance of the type @@ -150,6 +150,12 @@ struct _mp_obj_type_t { mp_unary_op_fun_t unary_op; // can return NULL if op not supported 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_item_fun_t store_item; + mp_fun_1_t getiter; mp_fun_1_t iternext; @@ -161,12 +167,6 @@ struct _mp_obj_type_t { const mp_method_t *methods; - 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_item_fun_t store_item; - // these are for dynamically created types (classes) mp_obj_t bases_tuple; mp_obj_t locals_dict; @@ -180,7 +180,6 @@ struct _mp_obj_type_t { abs float complex hash bool int none str equal int str - less int get_array_n tuple list unpack seq list tuple @@ -189,9 +188,27 @@ struct _mp_obj_type_t { 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_BaseException; +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_ImportError; +extern const mp_obj_type_t mp_type_IndentationError; +extern const mp_obj_type_t mp_type_IndexError; +extern const mp_obj_type_t mp_type_KeyError; +extern const mp_obj_type_t mp_type_NameError; +extern const mp_obj_type_t mp_type_SyntaxError; +extern const mp_obj_type_t mp_type_TypeError; +extern const mp_obj_type_t mp_type_ValueError; +extern const mp_obj_type_t mp_type_OverflowError; +extern const mp_obj_type_t mp_type_OSError; +extern const mp_obj_type_t mp_type_NotImplementedError; +extern const mp_obj_type_t mp_type_StopIteration; + // Constant objects, globally accessible -extern const mp_obj_type_t mp_const_type; extern const mp_obj_t mp_const_none; extern const mp_obj_t mp_const_false; extern const mp_obj_t mp_const_true; @@ -201,7 +218,7 @@ extern const mp_obj_t mp_const_stop_iteration; // special object indicating end // General API for objects -mp_obj_t mp_obj_new_type(const char *name, mp_obj_t bases_tuple, mp_obj_t locals_dict); +mp_obj_t mp_obj_new_type(qstr name, mp_obj_t bases_tuple, mp_obj_t locals_dict); mp_obj_t mp_obj_new_none(void); mp_obj_t mp_obj_new_bool(bool value); mp_obj_t mp_obj_new_cell(mp_obj_t obj); @@ -214,14 +231,12 @@ mp_obj_t mp_obj_new_bytes(const byte* data, uint len); 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(qstr id); -mp_obj_t mp_obj_new_exception_msg(qstr id, const char *msg); -mp_obj_t mp_obj_new_exception_msg_1_arg(qstr id, const char *fmt, const char *a1); -mp_obj_t mp_obj_new_exception_msg_2_args(qstr id, const char *fmt, const char *a1, const char *a2); -mp_obj_t mp_obj_new_exception_msg_varg(qstr id, 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_exception(const mp_obj_type_t *exc_type); +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(int 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, uint n_state, 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); @@ -231,12 +246,14 @@ mp_obj_t mp_obj_new_list(uint n, mp_obj_t *items); mp_obj_t mp_obj_new_dict(int n_args); mp_obj_t mp_obj_new_set(int n_args, mp_obj_t *items); mp_obj_t mp_obj_new_slice(mp_obj_t start, mp_obj_t stop, mp_obj_t step); +mp_obj_t mp_obj_new_super(mp_obj_t type, mp_obj_t obj); mp_obj_t mp_obj_new_bound_meth(mp_obj_t meth, mp_obj_t self); mp_obj_t mp_obj_new_getitem_iter(mp_obj_t *args); 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(mp_obj_t object, mp_obj_t classinfo); 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); @@ -276,8 +293,9 @@ machine_int_t mp_obj_int_get(mp_obj_t self_in); machine_int_t mp_obj_int_get_checked(mp_obj_t self_in); // exception -extern const mp_obj_type_t exception_type; -qstr mp_obj_exception_get_type(mp_obj_t self_in); +bool mp_obj_is_exception_type(mp_obj_t self_in); +bool mp_obj_is_exception_instance(mp_obj_t self_in); +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); @@ -290,7 +308,7 @@ uint mp_obj_str_get_hash(mp_obj_t self_in); uint mp_obj_str_get_len(mp_obj_t self_in); qstr mp_obj_str_get_qstr(mp_obj_t self_in); // use this if you will anyway convert the string to a qstr const char *mp_obj_str_get_str(mp_obj_t self_in); // use this only if you need the string to be null terminated -const byte *mp_obj_str_get_data(mp_obj_t self_in, uint *len); +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 @@ -370,6 +388,10 @@ 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); 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; @@ -385,18 +407,18 @@ struct _mp_map_t *mp_obj_module_get_globals(mp_obj_t self_in); extern const mp_obj_type_t mp_type_staticmethod; extern const mp_obj_type_t mp_type_classmethod; -typedef struct _mp_obj_staticmethod_t { +// this structure is used for instances of both staticmethod and classmethod +typedef struct _mp_obj_static_class_method_t { mp_obj_base_t base; mp_obj_t fun; -} mp_obj_staticmethod_t; - -typedef struct _mp_obj_classmethod_t { - mp_obj_base_t base; - mp_obj_t fun; -} mp_obj_classmethod_t; +} mp_obj_static_class_method_t; // sequence helpers 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_sz) memcpy(dest, src, len * sizeof(item_sz)) +#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)); } 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); +mp_obj_t mp_seq_count_obj(const mp_obj_t *items, uint len, mp_obj_t value); diff --git a/include/MicroPython/parse.h b/include/MicroPython/parse.h index 9797873d1..6e299ef69 100644 --- a/include/MicroPython/parse.h +++ b/include/MicroPython/parse.h @@ -14,7 +14,8 @@ struct _mp_lexer_t; // makes sure the top 5 bits of x are all cleared (positive number) or all set (negavite number) // these macros can probably go somewhere else because they are used more than just in the parser #define MP_UINT_HIGH_5_BITS (~((~((machine_uint_t)0)) >> 5)) -#define MP_FIT_SMALL_INT(x) (((((machine_uint_t)(x)) & MP_UINT_HIGH_5_BITS) == 0) || ((((machine_uint_t)(x)) & MP_UINT_HIGH_5_BITS) == MP_UINT_HIGH_5_BITS)) +// parser's small ints are different from VM small int +#define MP_PARSE_FITS_SMALL_INT(x) (((((machine_uint_t)(x)) & MP_UINT_HIGH_5_BITS) == 0) || ((((machine_uint_t)(x)) & MP_UINT_HIGH_5_BITS) == MP_UINT_HIGH_5_BITS)) #define MP_PARSE_NODE_NULL (0) #define MP_PARSE_NODE_ID (0x1) @@ -63,5 +64,11 @@ typedef enum { MP_PARSE_EVAL_INPUT, } mp_parse_input_kind_t; -// returns MP_PARSE_NODE_NULL on error, and then exc_id_out and exc_msg_out are valid -mp_parse_node_t mp_parse(struct _mp_lexer_t *lex, mp_parse_input_kind_t input_kind, qstr *exc_id_out, const char **exc_msg_out); +typedef enum { + MP_PARSE_ERROR_UNEXPECTED_INDENT, + MP_PARSE_ERROR_UNMATCHED_UNINDENT, + MP_PARSE_ERROR_INVALID_SYNTAX, +} mp_parse_error_kind_t; + +// returns MP_PARSE_NODE_NULL on error, and then parse_error_kind_out is valid +mp_parse_node_t mp_parse(struct _mp_lexer_t *lex, mp_parse_input_kind_t input_kind, mp_parse_error_kind_t *parse_error_kind_out); diff --git a/include/MicroPython/pendsv.h b/include/MicroPython/pendsv.h new file mode 100644 index 000000000..b07ab7137 --- /dev/null +++ b/include/MicroPython/pendsv.h @@ -0,0 +1,3 @@ +void pendsv_init(void); +void pendsv_nlr_jump(mp_obj_t o); +void pendsv_isr_handler(void); diff --git a/include/MicroPython/pyexec.h b/include/MicroPython/pyexec.h new file mode 100644 index 000000000..3d28ec5ce --- /dev/null +++ b/include/MicroPython/pyexec.h @@ -0,0 +1,5 @@ +void pyexec_raw_repl(void); +void pyexec_repl(void); +bool pyexec_file(const char *filename); + +mp_obj_t pyb_set_repl_info(mp_obj_t o_value); diff --git a/include/MicroPython/qstrdefs.generated.h b/include/MicroPython/qstrdefs.generated.h index f002b07ef..05bfbc9ea 100644 --- a/include/MicroPython/qstrdefs.generated.h +++ b/include/MicroPython/qstrdefs.generated.h @@ -3,6 +3,7 @@ Q(__build_class__, (const byte*)"\x01\x06\x0f\x00" "__build_class__") Q(__class__, (const byte*)"\x92\x03\x09\x00" "__class__") Q(__doc__, (const byte*)"\xb2\x02\x07\x00" "__doc__") +Q(__import__, (const byte*)"\x17\x04\x0a\x00" "__import__") Q(__init__, (const byte*)"\x30\x03\x08\x00" "__init__") Q(__locals__, (const byte*)"\xfa\x03\x0a\x00" "__locals__") Q(__main__, (const byte*)"\x21\x03\x08\x00" "__main__") @@ -14,6 +15,7 @@ Q(__repl_print__, (const byte*)"\xbb\x05\x0e\x00" "__repl_print__") Q(__bool__, (const byte*)"\x28\x03\x08\x00" "__bool__") Q(__len__, (const byte*)"\xbb\x02\x07\x00" "__len__") Q(__getitem__, (const byte*)"\x6b\x04\x0b\x00" "__getitem__") +Q(__setitem__, (const byte*)"\x77\x04\x0b\x00" "__setitem__") Q(__add__, (const byte*)"\xa5\x02\x07\x00" "__add__") Q(__sub__, (const byte*)"\xc6\x02\x07\x00" "__sub__") Q(micropython, (const byte*)"\xbc\x04\x0b\x00" "micropython") @@ -23,17 +25,21 @@ Q(viper, (const byte*)"\x26\x02\x05\x00" "viper") Q(asm_thumb, (const byte*)"\xc0\x03\x09\x00" "asm_thumb") Q(Ellipsis, (const byte*)"\x45\x03\x08\x00" "Ellipsis") Q(StopIteration, (const byte*)"\x55\x05\x0d\x00" "StopIteration") +Q(BaseException, (const byte*)"\x2a\x05\x0d\x00" "BaseException") Q(AssertionError, (const byte*)"\xc2\x05\x0e\x00" "AssertionError") Q(AttributeError, (const byte*)"\xbe\x05\x0e\x00" "AttributeError") +Q(ImportError, (const byte*)"\x85\x04\x0b\x00" "ImportError") Q(IndentationError, (const byte*)"\x87\x06\x10\x00" "IndentationError") Q(IndexError, (const byte*)"\x02\x04\x0a\x00" "IndexError") Q(KeyError, (const byte*)"\x33\x03\x08\x00" "KeyError") Q(NameError, (const byte*)"\x8b\x03\x09\x00" "NameError") +Q(NotImplementedError, (const byte*)"\xaf\x07\x13\x00" "NotImplementedError") Q(OSError, (const byte*)"\xac\x02\x07\x00" "OSError") Q(SyntaxError, (const byte*)"\x91\x04\x0b\x00" "SyntaxError") Q(TypeError, (const byte*)"\xac\x03\x09\x00" "TypeError") Q(ValueError, (const byte*)"\x07\x04\x0a\x00" "ValueError") Q(OverflowError, (const byte*)"\x5e\x05\x0d\x00" "OverflowError") +Q(NoneType, (const byte*)"\x32\x03\x08\x00" "NoneType") Q(abs, (const byte*)"\x36\x01\x03\x00" "abs") Q(all, (const byte*)"\x39\x01\x03\x00" "all") Q(any, (const byte*)"\x48\x01\x03\x00" "any") @@ -43,11 +49,14 @@ Q(bytearray, (const byte*)"\xd3\x03\x09\x00" "bytearray") Q(bytes, (const byte*)"\x27\x02\x05\x00" "bytes") Q(callable, (const byte*)"\x30\x03\x08\x00" "callable") Q(chr, (const byte*)"\x3d\x01\x03\x00" "chr") +Q(classmethod, (const byte*)"\x97\x04\x0b\x00" "classmethod") Q(complex, (const byte*)"\xf8\x02\x07\x00" "complex") Q(dict, (const byte*)"\xa4\x01\x04\x00" "dict") +Q(dir, (const byte*)"\x3f\x01\x03\x00" "dir") Q(divmod, (const byte*)"\x83\x02\x06\x00" "divmod") Q(enumerate, (const byte*)"\xc6\x03\x09\x00" "enumerate") Q(eval, (const byte*)"\xa8\x01\x04\x00" "eval") +Q(exec, (const byte*)"\xa5\x01\x04\x00" "exec") Q(filter, (const byte*)"\x86\x02\x06\x00" "filter") Q(float, (const byte*)"\x16\x02\x05\x00" "float") Q(hash, (const byte*)"\xa4\x01\x04\x00" "hash") @@ -63,14 +72,18 @@ Q(max, (const byte*)"\x46\x01\x03\x00" "max") Q(min, (const byte*)"\x44\x01\x03\x00" "min") Q(next, (const byte*)"\xbf\x01\x04\x00" "next") Q(ord, (const byte*)"\x45\x01\x03\x00" "ord") +Q(path, (const byte*)"\xad\x01\x04\x00" "path") Q(pow, (const byte*)"\x56\x01\x03\x00" "pow") Q(print, (const byte*)"\x2d\x02\x05\x00" "print") Q(range, (const byte*)"\x0d\x02\x05\x00" "range") Q(repr, (const byte*)"\xb9\x01\x04\x00" "repr") Q(set, (const byte*)"\x4c\x01\x03\x00" "set") Q(sorted, (const byte*)"\x91\x02\x06\x00" "sorted") +Q(staticmethod, (const byte*)"\x09\x05\x0c\x00" "staticmethod") Q(sum, (const byte*)"\x55\x01\x03\x00" "sum") +Q(super, (const byte*)"\x2f\x02\x05\x00" "super") Q(str, (const byte*)"\x59\x01\x03\x00" "str") +Q(sys, (const byte*)"\x5f\x01\x03\x00" "sys") Q(tuple, (const byte*)"\x2a\x02\x05\x00" "tuple") Q(type, (const byte*)"\xc2\x01\x04\x00" "type") Q(zip, (const byte*)"\x53\x01\x03\x00" "zip") @@ -80,6 +93,16 @@ Q(sort, (const byte*)"\xc8\x01\x04\x00" "sort") Q(join, (const byte*)"\xb0\x01\x04\x00" "join") Q(strip, (const byte*)"\x32\x02\x05\x00" "strip") Q(format, (const byte*)"\x89\x02\x06\x00" "format") +Q(key, (const byte*)"\x49\x01\x03\x00" "key") +Q(reverse, (const byte*)"\xfc\x02\x07\x00" "reverse") +Q(bound_method, (const byte*)"\xf8\x04\x0c\x00" "bound_method") +Q(closure, (const byte*)"\xfd\x02\x07\x00" "closure") +Q(dict_view, (const byte*)"\xbe\x03\x09\x00" "dict_view") +Q(function, (const byte*)"\x66\x03\x08\x00" "function") +Q(generator, (const byte*)"\xc7\x03\x09\x00" "generator") +Q(iterator, (const byte*)"\x6a\x03\x08\x00" "iterator") +Q(module, (const byte*)"\x86\x02\x06\x00" "module") +Q(slice, (const byte*)"\x10\x02\x05\x00" "slice") Q(_lt_module_gt_, (const byte*)"\x00\x03\x08\x00" "") Q(_lt_lambda_gt_, (const byte*)"\xdb\x02\x08\x00" "") Q(_lt_listcomp_gt_, (const byte*)"\xe5\x03\x0a\x00" "") @@ -103,15 +126,30 @@ Q(switch, (const byte*)"\x92\x02\x06\x00" "switch") Q(servo, (const byte*)"\x2f\x02\x05\x00" "servo") Q(pwm, (const byte*)"\x54\x01\x03\x00" "pwm") Q(accel, (const byte*)"\xf8\x01\x05\x00" "accel") -Q(mma_read, (const byte*)"\x36\x03\x08\x00" "mma_read") -Q(mma_mode, (const byte*)"\x3f\x03\x08\x00" "mma_mode") +Q(accel_read, (const byte*)"\xf3\x03\x0a\x00" "accel_read") +Q(accel_mode, (const byte*)"\xfc\x03\x0a\x00" "accel_mode") Q(hid, (const byte*)"\x35\x01\x03\x00" "hid") Q(time, (const byte*)"\xaf\x01\x04\x00" "time") Q(rand, (const byte*)"\xa5\x01\x04\x00" "rand") Q(Led, (const byte*)"\x15\x01\x03\x00" "Led") +Q(LCD, (const byte*)"\xd3\x00\x03\x00" "LCD") Q(Servo, (const byte*)"\x0f\x02\x05\x00" "Servo") +Q(SDcard, (const byte*)"\x31\x02\x06\x00" "SDcard") Q(I2C, (const byte*)"\xbe\x00\x03\x00" "I2C") Q(gpio, (const byte*)"\xaf\x01\x04\x00" "gpio") +Q(gpio_in, (const byte*)"\xe5\x02\x07\x00" "gpio_in") +Q(gpio_out, (const byte*)"\x66\x03\x08\x00" "gpio_out") Q(Usart, (const byte*)"\x0f\x02\x05\x00" "Usart") Q(ADC, (const byte*)"\xc8\x00\x03\x00" "ADC") Q(open, (const byte*)"\xb2\x01\x04\x00" "open") +Q(File, (const byte*)"\x80\x01\x04\x00" "File") +Q(0_colon__slash_, (const byte*)"\x99\x00\x03\x00" "0:/") +Q(0_colon__slash_src, (const byte*)"\xe1\x01\x06\x00" "0:/src") +Q(0_colon__slash_lib, (const byte*)"\xd0\x01\x06\x00" "0:/lib") +Q(Pin, (const byte*)"\x27\x01\x03\x00" "Pin") +Q(PinMap, (const byte*)"\x45\x02\x06\x00" "PinMap") +Q(PinAF, (const byte*)"\xae\x01\x05\x00" "PinAF") +Q(PinNamed, (const byte*)"\x0c\x03\x08\x00" "PinNamed") +Q(Image, (const byte*)"\xe3\x01\x05\x00" "Image") +Q(Imlib, (const byte*)"\xed\x01\x05\x00" "Imlib") +Q(Sensor, (const byte*)"\x7a\x02\x06\x00" "Sensor") diff --git a/include/MicroPython/runtime.h b/include/MicroPython/runtime.h index aafe1a06a..7215cc889 100644 --- a/include/MicroPython/runtime.h +++ b/include/MicroPython/runtime.h @@ -1,3 +1,5 @@ +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); + int rt_is_true(mp_obj_t arg); mp_obj_t rt_load_const_dec(qstr qstr); @@ -37,8 +39,10 @@ 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); struct _mp_map_t; struct _mp_map_t *rt_locals_get(void); @@ -46,3 +50,4 @@ 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; diff --git a/include/MicroPython/runtime0.h b/include/MicroPython/runtime0.h index 33fd80e64..07fcf0705 100644 --- a/include/MicroPython/runtime0.h +++ b/include/MicroPython/runtime0.h @@ -1,3 +1,17 @@ +// taken from python source, Include/code.h +#define MP_SCOPE_FLAG_OPTIMISED 0x01 +#define MP_SCOPE_FLAG_NEWLOCALS 0x02 +#define MP_SCOPE_FLAG_VARARGS 0x04 +#define MP_SCOPE_FLAG_VARKEYWORDS 0x08 +#define MP_SCOPE_FLAG_NESTED 0x10 +#define MP_SCOPE_FLAG_GENERATOR 0x20 +/* The MP_SCOPE_FLAG_NOFREE flag is set if there are no free or cell variables. + This information is redundant, but it allows a single flag test + to determine whether there is any extra work to be done when the + call frame is setup. +*/ +#define MP_SCOPE_FLAG_NOFREE 0x40 + typedef enum { RT_UNARY_OP_BOOL, // __bool__ RT_UNARY_OP_LEN, // __len__ @@ -83,6 +97,6 @@ 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, bool is_generator); +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); diff --git a/include/MicroPython/std.h b/include/MicroPython/std.h index 98c719676..843ddd827 100644 --- a/include/MicroPython/std.h +++ b/include/MicroPython/std.h @@ -2,11 +2,6 @@ typedef unsigned int size_t; void __assert_func(void); -void *malloc(size_t n); -void free(void *ptr); -void *calloc(size_t sz, size_t n); -void *realloc(void *ptr, size_t n); - void *memcpy(void *dest, const void *src, size_t n); void *memmove(void *dest, const void *src, size_t n); void *memset(void *s, int c, size_t n); @@ -14,7 +9,6 @@ void *memset(void *s, int c, size_t n); size_t strlen(const char *str); int strcmp(const char *s1, const char *s2); int strncmp(const char *s1, const char *s2, size_t n); -char *strndup(const char *s, size_t n); char *strcpy(char *dest, const char *src); char *strcat(char *dest, const char *src); char *strchr(const char *s, int c); diff --git a/include/MicroPython/storage.h b/include/MicroPython/storage.h index fe37e8b27..4d153d2f6 100644 --- a/include/MicroPython/storage.h +++ b/include/MicroPython/storage.h @@ -1,3 +1,5 @@ +#define FLASH_BLOCK_SIZE (512) + void storage_init(void); uint32_t storage_get_block_size(void); uint32_t storage_get_block_count(void); diff --git a/include/MicroPython/usb.h b/include/MicroPython/usb.h index 61931ffc8..1f7fa1809 100644 --- a/include/MicroPython/usb.h +++ b/include/MicroPython/usb.h @@ -1,4 +1,6 @@ #define VCP_CHAR_NONE (0) +#define VCP_CHAR_CTRL_A (1) +#define VCP_CHAR_CTRL_B (2) #define VCP_CHAR_CTRL_C (3) #define VCP_CHAR_CTRL_D (4) diff --git a/lib/libmp.a b/lib/libmp.a index 1ef09c4d5..0b9cfbbbd 100644 Binary files a/lib/libmp.a and b/lib/libmp.a differ diff --git a/lib/libstdperiph.a b/lib/libstdperiph.a index d96ac912a..2977bba27 100644 Binary files a/lib/libstdperiph.a and b/lib/libstdperiph.a differ